license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
[email protected] | 77848d1 | 2008-11-14 00:00:22 | [diff] [blame] | 5 | #include <math.h> // ceil |
| 6 | |
[email protected] | 68bf915 | 2008-09-25 19:47:30 | [diff] [blame] | 7 | #include "base/compiler_specific.h" |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 8 | #include "net/base/completion_callback.h" |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 9 | #include "net/base/mock_host_resolver.h" |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 10 | #include "net/base/ssl_info.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 11 | #include "net/base/test_completion_callback.h" |
| 12 | #include "net/base/upload_data.h" |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 13 | #include "net/http/http_auth_handler_ntlm.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 14 | #include "net/http/http_network_session.h" |
| 15 | #include "net/http/http_network_transaction.h" |
| 16 | #include "net/http/http_transaction_unittest.h" |
[email protected] | 51fff29d | 2008-12-19 22:17:53 | [diff] [blame] | 17 | #include "net/proxy/proxy_config_service_fixed.h" |
[email protected] | f7984fc6 | 2009-06-22 23:26:44 | [diff] [blame] | 18 | #include "net/socket/client_socket_factory.h" |
| 19 | #include "net/socket/socket_test_util.h" |
| 20 | #include "net/socket/ssl_client_socket.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 21 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 22 | #include "testing/platform_test.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 23 | |
| 24 | //----------------------------------------------------------------------------- |
| 25 | |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 26 | namespace net { |
| 27 | |
[email protected] | db8f44c | 2008-12-13 04:52:01 | [diff] [blame] | 28 | // Create a proxy service which fails on all requests (falls back to direct). |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 29 | ProxyService* CreateNullProxyService() { |
| 30 | return ProxyService::CreateNull(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 31 | } |
| 32 | |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 33 | // Helper to manage the lifetimes of the dependencies for a |
| 34 | // HttpNetworkTransaction. |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 35 | class SessionDependencies { |
| 36 | public: |
| 37 | // Default set of dependencies -- "null" proxy service. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 38 | SessionDependencies() : host_resolver(new MockHostResolver), |
[email protected] | 94a0d3d9 | 2009-06-27 01:50:14 | [diff] [blame] | 39 | proxy_service(CreateNullProxyService()) {} |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 40 | |
| 41 | // Custom proxy service dependency. |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 42 | explicit SessionDependencies(ProxyService* proxy_service) |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 43 | : host_resolver(new MockHostResolver), proxy_service(proxy_service) {} |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 44 | |
[email protected] | a2c2fb9 | 2009-07-18 07:31:04 | [diff] [blame] | 45 | scoped_refptr<MockHostResolverBase> host_resolver; |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 46 | scoped_refptr<ProxyService> proxy_service; |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 47 | MockClientSocketFactory socket_factory; |
| 48 | }; |
| 49 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 50 | ProxyService* CreateFixedProxyService(const std::string& proxy) { |
[email protected] | ab501a6a | 2009-05-12 15:07:50 | [diff] [blame] | 51 | net::ProxyConfig proxy_config; |
| 52 | proxy_config.proxy_rules.ParseFromString(proxy); |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 53 | return ProxyService::CreateFixed(proxy_config); |
[email protected] | 51fff29d | 2008-12-19 22:17:53 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 57 | HttpNetworkSession* CreateSession(SessionDependencies* session_deps) { |
[email protected] | 94a0d3d9 | 2009-06-27 01:50:14 | [diff] [blame] | 58 | return new HttpNetworkSession(session_deps->host_resolver, |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 59 | session_deps->proxy_service, |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 60 | &session_deps->socket_factory); |
[email protected] | e8d53619 | 2008-10-17 22:21:14 | [diff] [blame] | 61 | } |
| 62 | |
[email protected] | 89836e2 | 2008-09-25 20:33:42 | [diff] [blame] | 63 | class HttpNetworkTransactionTest : public PlatformTest { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 64 | public: |
[email protected] | 0e75a73 | 2008-10-16 20:36:09 | [diff] [blame] | 65 | virtual void TearDown() { |
| 66 | // Empty the current queue. |
| 67 | MessageLoop::current()->RunAllPending(); |
| 68 | PlatformTest::TearDown(); |
| 69 | } |
| 70 | |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 71 | protected: |
| 72 | void KeepAliveConnectionResendRequestTest(const MockRead& read_failure); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 73 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 74 | struct SimpleGetHelperResult { |
| 75 | int rv; |
| 76 | std::string status_line; |
| 77 | std::string response_data; |
| 78 | }; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 79 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 80 | SimpleGetHelperResult SimpleGetHelper(MockRead data_reads[]) { |
| 81 | SimpleGetHelperResult out; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 82 | |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 83 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 84 | scoped_ptr<HttpTransaction> trans( |
| 85 | new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 86 | CreateSession(&session_deps), |
| 87 | &session_deps.socket_factory)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 88 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 89 | HttpRequestInfo request; |
| 90 | request.method = "GET"; |
| 91 | request.url = GURL("http://www.google.com/"); |
| 92 | request.load_flags = 0; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 93 | |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 94 | StaticMockSocket data(data_reads, NULL); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 95 | session_deps.socket_factory.AddMockSocket(&data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 96 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 97 | TestCompletionCallback callback; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 98 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 99 | int rv = trans->Start(&request, &callback, NULL); |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 100 | EXPECT_EQ(ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 101 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 102 | out.rv = callback.WaitForResult(); |
| 103 | if (out.rv != OK) |
| 104 | return out; |
| 105 | |
| 106 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 107 | EXPECT_TRUE(response != NULL); |
| 108 | |
| 109 | EXPECT_TRUE(response->headers != NULL); |
| 110 | out.status_line = response->headers->GetStatusLine(); |
| 111 | |
| 112 | rv = ReadTransaction(trans.get(), &out.response_data); |
| 113 | EXPECT_EQ(OK, rv); |
| 114 | |
[email protected] | aecfbf2 | 2008-10-16 02:02:47 | [diff] [blame] | 115 | return out; |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 116 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 117 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 118 | void ConnectStatusHelperWithExpectedStatus(const MockRead& status, |
| 119 | int expected_status); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 120 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 121 | void ConnectStatusHelper(const MockRead& status); |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 122 | }; |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 123 | |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 124 | // Fill |str| with a long header list that consumes >= |size| bytes. |
| 125 | void FillLargeHeadersString(std::string* str, int size) { |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 126 | const char* row = |
| 127 | "SomeHeaderName: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n"; |
| 128 | const int sizeof_row = strlen(row); |
| 129 | const int num_rows = static_cast<int>( |
| 130 | ceil(static_cast<float>(size) / sizeof_row)); |
| 131 | const int sizeof_data = num_rows * sizeof_row; |
| 132 | DCHECK(sizeof_data >= size); |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 133 | str->reserve(sizeof_data); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 134 | |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 135 | for (int i = 0; i < num_rows; ++i) |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 136 | str->append(row, sizeof_row); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 137 | } |
| 138 | |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 139 | // Alternative functions that eliminate randomness and dependency on the local |
| 140 | // host name so that the generated NTLM messages are reproducible. |
[email protected] | fe2bc6a | 2009-03-23 16:52:20 | [diff] [blame] | 141 | void MockGenerateRandom1(uint8* output, size_t n) { |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 142 | static const uint8 bytes[] = { |
| 143 | 0x55, 0x29, 0x66, 0x26, 0x6b, 0x9c, 0x73, 0x54 |
| 144 | }; |
| 145 | static size_t current_byte = 0; |
| 146 | for (size_t i = 0; i < n; ++i) { |
| 147 | output[i] = bytes[current_byte++]; |
| 148 | current_byte %= arraysize(bytes); |
| 149 | } |
| 150 | } |
| 151 | |
[email protected] | fe2bc6a | 2009-03-23 16:52:20 | [diff] [blame] | 152 | void MockGenerateRandom2(uint8* output, size_t n) { |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 153 | static const uint8 bytes[] = { |
| 154 | 0x96, 0x79, 0x85, 0xe7, 0x49, 0x93, 0x70, 0xa1, |
| 155 | 0x4e, 0xe7, 0x87, 0x45, 0x31, 0x5b, 0xd3, 0x1f |
| 156 | }; |
| 157 | static size_t current_byte = 0; |
| 158 | for (size_t i = 0; i < n; ++i) { |
| 159 | output[i] = bytes[current_byte++]; |
| 160 | current_byte %= arraysize(bytes); |
| 161 | } |
| 162 | } |
| 163 | |
[email protected] | fe2bc6a | 2009-03-23 16:52:20 | [diff] [blame] | 164 | std::string MockGetHostName() { |
| 165 | return "WTC-WIN7"; |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 166 | } |
| 167 | |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 168 | class CaptureGroupNameSocketPool : public ClientSocketPool { |
| 169 | public: |
| 170 | CaptureGroupNameSocketPool() { |
| 171 | } |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 172 | const std::string last_group_name_received() const { |
| 173 | return last_group_name_; |
| 174 | } |
| 175 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 176 | virtual int RequestSocket(const std::string& group_name, |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 177 | const void* socket_params, |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 178 | int priority, |
| 179 | ClientSocketHandle* handle, |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 180 | CompletionCallback* callback, |
| 181 | LoadLog* load_log) { |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 182 | last_group_name_ = group_name; |
| 183 | return ERR_IO_PENDING; |
| 184 | } |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 185 | virtual void CancelRequest(const std::string& group_name, |
| 186 | const ClientSocketHandle* handle) { } |
| 187 | virtual void ReleaseSocket(const std::string& group_name, |
| 188 | ClientSocket* socket) {} |
| 189 | virtual void CloseIdleSockets() {} |
| 190 | virtual HostResolver* GetHostResolver() const { |
| 191 | return NULL; |
| 192 | } |
| 193 | virtual int IdleSocketCount() const { |
| 194 | return 0; |
| 195 | } |
| 196 | virtual int IdleSocketCountInGroup(const std::string& group_name) const { |
| 197 | return 0; |
| 198 | } |
| 199 | virtual LoadState GetLoadState(const std::string& group_name, |
| 200 | const ClientSocketHandle* handle) const { |
| 201 | return LOAD_STATE_IDLE; |
| 202 | } |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 203 | |
| 204 | private: |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 205 | std::string last_group_name_; |
| 206 | }; |
| 207 | |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 208 | //----------------------------------------------------------------------------- |
| 209 | |
| 210 | TEST_F(HttpNetworkTransactionTest, Basic) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 211 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 212 | scoped_ptr<HttpTransaction> trans( |
| 213 | new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 214 | CreateSession(&session_deps), |
| 215 | &session_deps.socket_factory)); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | TEST_F(HttpNetworkTransactionTest, SimpleGET) { |
| 219 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 220 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 221 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 222 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 223 | }; |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 224 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 225 | EXPECT_EQ(OK, out.rv); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 226 | EXPECT_EQ("HTTP/1.0 200 OK", out.status_line); |
| 227 | EXPECT_EQ("hello world", out.response_data); |
| 228 | } |
| 229 | |
| 230 | // Response with no status line. |
| 231 | TEST_F(HttpNetworkTransactionTest, SimpleGETNoHeaders) { |
| 232 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 233 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 234 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 235 | }; |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 236 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 237 | EXPECT_EQ(OK, out.rv); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 238 | EXPECT_EQ("HTTP/0.9 200 OK", out.status_line); |
| 239 | EXPECT_EQ("hello world", out.response_data); |
| 240 | } |
| 241 | |
| 242 | // Allow up to 4 bytes of junk to precede status line. |
| 243 | TEST_F(HttpNetworkTransactionTest, StatusLineJunk2Bytes) { |
| 244 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 245 | MockRead("xxxHTTP/1.0 404 Not Found\nServer: blah\n\nDATA"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 246 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 247 | }; |
| 248 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 249 | EXPECT_EQ(OK, out.rv); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 250 | EXPECT_EQ("HTTP/1.0 404 Not Found", out.status_line); |
| 251 | EXPECT_EQ("DATA", out.response_data); |
| 252 | } |
| 253 | |
| 254 | // Allow up to 4 bytes of junk to precede status line. |
| 255 | TEST_F(HttpNetworkTransactionTest, StatusLineJunk4Bytes) { |
| 256 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 257 | MockRead("\n\nQJHTTP/1.0 404 Not Found\nServer: blah\n\nDATA"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 258 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 259 | }; |
| 260 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 261 | EXPECT_EQ(OK, out.rv); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 262 | EXPECT_EQ("HTTP/1.0 404 Not Found", out.status_line); |
| 263 | EXPECT_EQ("DATA", out.response_data); |
| 264 | } |
| 265 | |
| 266 | // Beyond 4 bytes of slop and it should fail to find a status line. |
| 267 | TEST_F(HttpNetworkTransactionTest, StatusLineJunk5Bytes) { |
| 268 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 269 | MockRead("xxxxxHTTP/1.1 404 Not Found\nServer: blah"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 270 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 271 | }; |
| 272 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 273 | EXPECT_EQ(OK, out.rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 274 | EXPECT_EQ("HTTP/0.9 200 OK", out.status_line); |
| 275 | EXPECT_EQ("xxxxxHTTP/1.1 404 Not Found\nServer: blah", out.response_data); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | // Same as StatusLineJunk4Bytes, except the read chunks are smaller. |
| 279 | TEST_F(HttpNetworkTransactionTest, StatusLineJunk4Bytes_Slow) { |
| 280 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 281 | MockRead("\n"), |
| 282 | MockRead("\n"), |
| 283 | MockRead("Q"), |
| 284 | MockRead("J"), |
| 285 | MockRead("HTTP/1.0 404 Not Found\nServer: blah\n\nDATA"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 286 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 287 | }; |
| 288 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 289 | EXPECT_EQ(OK, out.rv); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 290 | EXPECT_EQ("HTTP/1.0 404 Not Found", out.status_line); |
| 291 | EXPECT_EQ("DATA", out.response_data); |
| 292 | } |
| 293 | |
| 294 | // Close the connection before enough bytes to have a status line. |
| 295 | TEST_F(HttpNetworkTransactionTest, StatusLinePartial) { |
| 296 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 297 | MockRead("HTT"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 298 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 299 | }; |
| 300 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 301 | EXPECT_EQ(OK, out.rv); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 302 | EXPECT_EQ("HTTP/0.9 200 OK", out.status_line); |
| 303 | EXPECT_EQ("HTT", out.response_data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 304 | } |
| 305 | |
[email protected] | f9d44aa | 2008-09-23 23:57:17 | [diff] [blame] | 306 | // Simulate a 204 response, lacking a Content-Length header, sent over a |
| 307 | // persistent connection. The response should still terminate since a 204 |
| 308 | // cannot have a response body. |
| 309 | TEST_F(HttpNetworkTransactionTest, StopsReading204) { |
| 310 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 311 | MockRead("HTTP/1.1 204 No Content\r\n\r\n"), |
| 312 | MockRead("junk"), // Should not be read!! |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 313 | MockRead(false, OK), |
[email protected] | f9d44aa | 2008-09-23 23:57:17 | [diff] [blame] | 314 | }; |
| 315 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 316 | EXPECT_EQ(OK, out.rv); |
[email protected] | f9d44aa | 2008-09-23 23:57:17 | [diff] [blame] | 317 | EXPECT_EQ("HTTP/1.1 204 No Content", out.status_line); |
| 318 | EXPECT_EQ("", out.response_data); |
| 319 | } |
| 320 | |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 321 | // Do a request using the HEAD method. Verify that we don't try to read the |
| 322 | // message body (since HEAD has none). |
| 323 | TEST_F(HttpNetworkTransactionTest, Head) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 324 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 325 | scoped_ptr<HttpTransaction> trans( |
| 326 | new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 327 | CreateSession(&session_deps), |
| 328 | &session_deps.socket_factory)); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 329 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 330 | HttpRequestInfo request; |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 331 | request.method = "HEAD"; |
| 332 | request.url = GURL("http://www.google.com/"); |
| 333 | request.load_flags = 0; |
| 334 | |
| 335 | MockWrite data_writes1[] = { |
| 336 | MockWrite("HEAD / HTTP/1.1\r\n" |
| 337 | "Host: www.google.com\r\n" |
| 338 | "Connection: keep-alive\r\n" |
| 339 | "Content-Length: 0\r\n\r\n"), |
| 340 | }; |
| 341 | MockRead data_reads1[] = { |
| 342 | MockRead("HTTP/1.1 404 Not Found\r\n"), |
| 343 | MockRead("Server: Blah\r\n"), |
| 344 | MockRead("Content-Length: 1234\r\n\r\n"), |
| 345 | |
| 346 | // No response body because the test stops reading here. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 347 | MockRead(false, ERR_UNEXPECTED), // Should not be reached. |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 348 | }; |
| 349 | |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 350 | StaticMockSocket data1(data_reads1, data_writes1); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 351 | session_deps.socket_factory.AddMockSocket(&data1); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 352 | |
| 353 | TestCompletionCallback callback1; |
| 354 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 355 | int rv = trans->Start(&request, &callback1, NULL); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 356 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 357 | |
| 358 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 359 | EXPECT_EQ(OK, rv); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 360 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 361 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 362 | EXPECT_FALSE(response == NULL); |
| 363 | |
| 364 | // Check that the headers got parsed. |
| 365 | EXPECT_TRUE(response->headers != NULL); |
| 366 | EXPECT_EQ(1234, response->headers->GetContentLength()); |
| 367 | EXPECT_EQ("HTTP/1.1 404 Not Found", response->headers->GetStatusLine()); |
| 368 | |
| 369 | std::string server_header; |
| 370 | void* iter = NULL; |
| 371 | bool has_server_header = response->headers->EnumerateHeader( |
| 372 | &iter, "Server", &server_header); |
| 373 | EXPECT_TRUE(has_server_header); |
| 374 | EXPECT_EQ("Blah", server_header); |
| 375 | |
| 376 | // Reading should give EOF right away, since there is no message body |
| 377 | // (despite non-zero content-length). |
| 378 | std::string response_data; |
| 379 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 380 | EXPECT_EQ(OK, rv); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 381 | EXPECT_EQ("", response_data); |
| 382 | } |
| 383 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 384 | TEST_F(HttpNetworkTransactionTest, ReuseConnection) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 385 | SessionDependencies session_deps; |
| 386 | scoped_refptr<HttpNetworkSession> session = CreateSession(&session_deps); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 387 | |
| 388 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 389 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 390 | MockRead("hello"), |
| 391 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 392 | MockRead("world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 393 | MockRead(false, OK), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 394 | }; |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 395 | StaticMockSocket data(data_reads, NULL); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 396 | session_deps.socket_factory.AddMockSocket(&data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 397 | |
| 398 | const char* kExpectedResponseData[] = { |
| 399 | "hello", "world" |
| 400 | }; |
| 401 | |
| 402 | for (int i = 0; i < 2; ++i) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 403 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 404 | new HttpNetworkTransaction(session, &session_deps.socket_factory)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 405 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 406 | HttpRequestInfo request; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 407 | request.method = "GET"; |
| 408 | request.url = GURL("http://www.google.com/"); |
| 409 | request.load_flags = 0; |
| 410 | |
| 411 | TestCompletionCallback callback; |
| 412 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 413 | int rv = trans->Start(&request, &callback, NULL); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 414 | EXPECT_EQ(ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 415 | |
| 416 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 417 | EXPECT_EQ(OK, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 418 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 419 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 420 | EXPECT_TRUE(response != NULL); |
| 421 | |
| 422 | EXPECT_TRUE(response->headers != NULL); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 423 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 424 | |
| 425 | std::string response_data; |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 426 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 427 | EXPECT_EQ(OK, rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 428 | EXPECT_EQ(kExpectedResponseData[i], response_data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | |
| 432 | TEST_F(HttpNetworkTransactionTest, Ignores100) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 433 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 434 | scoped_ptr<HttpTransaction> trans( |
| 435 | new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 436 | CreateSession(&session_deps), |
| 437 | &session_deps.socket_factory)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 438 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 439 | HttpRequestInfo request; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 440 | request.method = "POST"; |
| 441 | request.url = GURL("http://www.foo.com/"); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 442 | request.upload_data = new UploadData; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 443 | request.upload_data->AppendBytes("foo", 3); |
| 444 | request.load_flags = 0; |
| 445 | |
| 446 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 447 | MockRead("HTTP/1.0 100 Continue\r\n\r\n"), |
| 448 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 449 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 450 | MockRead(false, OK), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 451 | }; |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 452 | StaticMockSocket data(data_reads, NULL); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 453 | session_deps.socket_factory.AddMockSocket(&data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 454 | |
| 455 | TestCompletionCallback callback; |
| 456 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 457 | int rv = trans->Start(&request, &callback, NULL); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 458 | EXPECT_EQ(ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 459 | |
| 460 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 461 | EXPECT_EQ(OK, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 462 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 463 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 464 | EXPECT_TRUE(response != NULL); |
| 465 | |
| 466 | EXPECT_TRUE(response->headers != NULL); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 467 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 468 | |
| 469 | std::string response_data; |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 470 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 471 | EXPECT_EQ(OK, rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 472 | EXPECT_EQ("hello world", response_data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 473 | } |
| 474 | |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 475 | // This test is almost the same as Ignores100 above, but the response contains |
| 476 | // a 102 instead of a 100. Also, instead of HTTP/1.0 the response is |
| 477 | // HTTP/1.1. |
| 478 | TEST_F(HttpNetworkTransactionTest, Ignores1xx) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 479 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 480 | scoped_ptr<HttpTransaction> trans( |
| 481 | new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 482 | CreateSession(&session_deps), |
| 483 | &session_deps.socket_factory)); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 484 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 485 | HttpRequestInfo request; |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 486 | request.method = "GET"; |
| 487 | request.url = GURL("http://www.foo.com/"); |
| 488 | request.load_flags = 0; |
| 489 | |
| 490 | MockRead data_reads[] = { |
| 491 | MockRead("HTTP/1.1 102 Unspecified status code\r\n\r\n"), |
| 492 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 493 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 494 | MockRead(false, OK), |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 495 | }; |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 496 | StaticMockSocket data(data_reads, NULL); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 497 | session_deps.socket_factory.AddMockSocket(&data); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 498 | |
| 499 | TestCompletionCallback callback; |
| 500 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 501 | int rv = trans->Start(&request, &callback, NULL); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 502 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 503 | |
| 504 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 505 | EXPECT_EQ(OK, rv); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 506 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 507 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 508 | EXPECT_TRUE(response != NULL); |
| 509 | |
| 510 | EXPECT_TRUE(response->headers != NULL); |
| 511 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 512 | |
| 513 | std::string response_data; |
| 514 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 515 | EXPECT_EQ(OK, rv); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 516 | EXPECT_EQ("hello world", response_data); |
| 517 | } |
| 518 | |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 519 | // read_failure specifies a read failure that should cause the network |
| 520 | // transaction to resend the request. |
| 521 | void HttpNetworkTransactionTest::KeepAliveConnectionResendRequestTest( |
| 522 | const MockRead& read_failure) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 523 | SessionDependencies session_deps; |
| 524 | scoped_refptr<HttpNetworkSession> session = CreateSession(&session_deps); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 525 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 526 | HttpRequestInfo request; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 527 | request.method = "GET"; |
| 528 | request.url = GURL("http://www.foo.com/"); |
| 529 | request.load_flags = 0; |
| 530 | |
| 531 | MockRead data1_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 532 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 533 | MockRead("hello"), |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 534 | read_failure, // Now, we reuse the connection and fail the first read. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 535 | }; |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 536 | StaticMockSocket data1(data1_reads, NULL); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 537 | session_deps.socket_factory.AddMockSocket(&data1); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 538 | |
| 539 | MockRead data2_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 540 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 541 | MockRead("world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 542 | MockRead(true, OK), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 543 | }; |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 544 | StaticMockSocket data2(data2_reads, NULL); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 545 | session_deps.socket_factory.AddMockSocket(&data2); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 546 | |
| 547 | const char* kExpectedResponseData[] = { |
| 548 | "hello", "world" |
| 549 | }; |
| 550 | |
| 551 | for (int i = 0; i < 2; ++i) { |
| 552 | TestCompletionCallback callback; |
| 553 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 554 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 555 | new HttpNetworkTransaction(session, &session_deps.socket_factory)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 556 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 557 | int rv = trans->Start(&request, &callback, NULL); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 558 | EXPECT_EQ(ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 559 | |
| 560 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 561 | EXPECT_EQ(OK, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 562 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 563 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 564 | EXPECT_TRUE(response != NULL); |
| 565 | |
| 566 | EXPECT_TRUE(response->headers != NULL); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 567 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 568 | |
| 569 | std::string response_data; |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 570 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 571 | EXPECT_EQ(OK, rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 572 | EXPECT_EQ(kExpectedResponseData[i], response_data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 573 | } |
| 574 | } |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 575 | |
| 576 | TEST_F(HttpNetworkTransactionTest, KeepAliveConnectionReset) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 577 | MockRead read_failure(true, ERR_CONNECTION_RESET); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 578 | KeepAliveConnectionResendRequestTest(read_failure); |
| 579 | } |
| 580 | |
| 581 | TEST_F(HttpNetworkTransactionTest, KeepAliveConnectionEOF) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 582 | MockRead read_failure(false, OK); // EOF |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 583 | KeepAliveConnectionResendRequestTest(read_failure); |
| 584 | } |
| 585 | |
| 586 | TEST_F(HttpNetworkTransactionTest, NonKeepAliveConnectionReset) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 587 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 588 | scoped_ptr<HttpTransaction> trans( |
| 589 | new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 590 | CreateSession(&session_deps), |
| 591 | &session_deps.socket_factory)); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 592 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 593 | HttpRequestInfo request; |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 594 | request.method = "GET"; |
| 595 | request.url = GURL("http://www.google.com/"); |
| 596 | request.load_flags = 0; |
| 597 | |
| 598 | MockRead data_reads[] = { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 599 | MockRead(true, ERR_CONNECTION_RESET), |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 600 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), // Should not be used |
| 601 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 602 | MockRead(false, OK), |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 603 | }; |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 604 | StaticMockSocket data(data_reads, NULL); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 605 | session_deps.socket_factory.AddMockSocket(&data); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 606 | |
| 607 | TestCompletionCallback callback; |
| 608 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 609 | int rv = trans->Start(&request, &callback, NULL); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 610 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 611 | |
| 612 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 613 | EXPECT_EQ(ERR_CONNECTION_RESET, rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 614 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 615 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 616 | EXPECT_TRUE(response == NULL); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | // What do various browsers do when the server closes a non-keepalive |
| 620 | // connection without sending any response header or body? |
| 621 | // |
| 622 | // IE7: error page |
| 623 | // Safari 3.1.2 (Windows): error page |
| 624 | // Firefox 3.0.1: blank page |
| 625 | // Opera 9.52: after five attempts, blank page |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 626 | // Us with WinHTTP: error page (ERR_INVALID_RESPONSE) |
| 627 | // Us: error page (EMPTY_RESPONSE) |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 628 | TEST_F(HttpNetworkTransactionTest, NonKeepAliveConnectionEOF) { |
| 629 | MockRead data_reads[] = { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 630 | MockRead(false, OK), // EOF |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 631 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), // Should not be used |
| 632 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 633 | MockRead(false, OK), |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 634 | }; |
| 635 | SimpleGetHelperResult out = SimpleGetHelper(data_reads); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 636 | EXPECT_EQ(ERR_EMPTY_RESPONSE, out.rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 637 | } |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 638 | |
| 639 | // Test the request-challenge-retry sequence for basic auth. |
| 640 | // (basic auth is the easiest to mock, because it has no randomness). |
| 641 | TEST_F(HttpNetworkTransactionTest, BasicAuth) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 642 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 643 | scoped_ptr<HttpTransaction> trans( |
| 644 | new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 645 | CreateSession(&session_deps), |
| 646 | &session_deps.socket_factory)); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 647 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 648 | HttpRequestInfo request; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 649 | request.method = "GET"; |
| 650 | request.url = GURL("http://www.google.com/"); |
| 651 | request.load_flags = 0; |
| 652 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 653 | MockWrite data_writes1[] = { |
| 654 | MockWrite("GET / HTTP/1.1\r\n" |
| 655 | "Host: www.google.com\r\n" |
| 656 | "Connection: keep-alive\r\n\r\n"), |
| 657 | }; |
| 658 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 659 | MockRead data_reads1[] = { |
| 660 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 661 | // Give a couple authenticate options (only the middle one is actually |
| 662 | // supported). |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 663 | MockRead("WWW-Authenticate: Basic\r\n"), // Malformed |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 664 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 665 | MockRead("WWW-Authenticate: UNSUPPORTED realm=\"FOO\"\r\n"), |
| 666 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 667 | // Large content-length -- won't matter, as connection will be reset. |
| 668 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 669 | MockRead(false, ERR_FAILED), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 670 | }; |
| 671 | |
| 672 | // After calling trans->RestartWithAuth(), this is the request we should |
| 673 | // be issuing -- the final header line contains the credentials. |
| 674 | MockWrite data_writes2[] = { |
| 675 | MockWrite("GET / HTTP/1.1\r\n" |
| 676 | "Host: www.google.com\r\n" |
| 677 | "Connection: keep-alive\r\n" |
| 678 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 679 | }; |
| 680 | |
| 681 | // Lastly, the server responds with the actual content. |
| 682 | MockRead data_reads2[] = { |
| 683 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 684 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 685 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 686 | MockRead(false, OK), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 687 | }; |
| 688 | |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 689 | StaticMockSocket data1(data_reads1, data_writes1); |
| 690 | StaticMockSocket data2(data_reads2, data_writes2); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 691 | session_deps.socket_factory.AddMockSocket(&data1); |
| 692 | session_deps.socket_factory.AddMockSocket(&data2); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 693 | |
| 694 | TestCompletionCallback callback1; |
| 695 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 696 | int rv = trans->Start(&request, &callback1, NULL); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 697 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 698 | |
| 699 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 700 | EXPECT_EQ(OK, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 701 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 702 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 703 | EXPECT_FALSE(response == NULL); |
| 704 | |
| 705 | // The password prompt info should have been set in response->auth_challenge. |
| 706 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 707 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 708 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 709 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 710 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 711 | |
| 712 | TestCompletionCallback callback2; |
| 713 | |
| 714 | rv = trans->RestartWithAuth(L"foo", L"bar", &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 715 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 716 | |
| 717 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 718 | EXPECT_EQ(OK, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 719 | |
| 720 | response = trans->GetResponseInfo(); |
| 721 | EXPECT_FALSE(response == NULL); |
| 722 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 723 | EXPECT_EQ(100, response->headers->GetContentLength()); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 724 | } |
| 725 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 726 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
| 727 | // connection. |
| 728 | TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAlive) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 729 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 730 | scoped_ptr<HttpTransaction> trans( |
| 731 | new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 732 | CreateSession(&session_deps), |
| 733 | &session_deps.socket_factory)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 734 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 735 | HttpRequestInfo request; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 736 | request.method = "GET"; |
| 737 | request.url = GURL("http://www.google.com/"); |
| 738 | request.load_flags = 0; |
| 739 | |
| 740 | MockWrite data_writes1[] = { |
| 741 | MockWrite("GET / HTTP/1.1\r\n" |
| 742 | "Host: www.google.com\r\n" |
| 743 | "Connection: keep-alive\r\n\r\n"), |
| 744 | |
| 745 | // After calling trans->RestartWithAuth(), this is the request we should |
| 746 | // be issuing -- the final header line contains the credentials. |
| 747 | MockWrite("GET / HTTP/1.1\r\n" |
| 748 | "Host: www.google.com\r\n" |
| 749 | "Connection: keep-alive\r\n" |
| 750 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 751 | }; |
| 752 | |
| 753 | MockRead data_reads1[] = { |
| 754 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 755 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 756 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 757 | MockRead("Content-Length: 14\r\n\r\n"), |
| 758 | MockRead("Unauthorized\r\n"), |
| 759 | |
| 760 | // Lastly, the server responds with the actual content. |
| 761 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 762 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 763 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 764 | MockRead(false, OK), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 765 | }; |
| 766 | |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 767 | StaticMockSocket data1(data_reads1, data_writes1); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 768 | session_deps.socket_factory.AddMockSocket(&data1); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 769 | |
| 770 | TestCompletionCallback callback1; |
| 771 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 772 | int rv = trans->Start(&request, &callback1, NULL); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 773 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 774 | |
| 775 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 776 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 777 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 778 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 779 | EXPECT_FALSE(response == NULL); |
| 780 | |
| 781 | // The password prompt info should have been set in response->auth_challenge. |
| 782 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 783 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 784 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 785 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 786 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 787 | |
| 788 | TestCompletionCallback callback2; |
| 789 | |
| 790 | rv = trans->RestartWithAuth(L"foo", L"bar", &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 791 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 792 | |
| 793 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 794 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 795 | |
| 796 | response = trans->GetResponseInfo(); |
| 797 | EXPECT_FALSE(response == NULL); |
| 798 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 799 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 800 | } |
| 801 | |
| 802 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
| 803 | // connection and with no response body to drain. |
| 804 | TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAliveNoBody) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 805 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 806 | scoped_ptr<HttpTransaction> trans( |
| 807 | new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 808 | CreateSession(&session_deps), |
| 809 | &session_deps.socket_factory)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 810 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 811 | HttpRequestInfo request; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 812 | request.method = "GET"; |
| 813 | request.url = GURL("http://www.google.com/"); |
| 814 | request.load_flags = 0; |
| 815 | |
| 816 | MockWrite data_writes1[] = { |
| 817 | MockWrite("GET / HTTP/1.1\r\n" |
| 818 | "Host: www.google.com\r\n" |
| 819 | "Connection: keep-alive\r\n\r\n"), |
| 820 | |
| 821 | // After calling trans->RestartWithAuth(), this is the request we should |
| 822 | // be issuing -- the final header line contains the credentials. |
| 823 | MockWrite("GET / HTTP/1.1\r\n" |
| 824 | "Host: www.google.com\r\n" |
| 825 | "Connection: keep-alive\r\n" |
| 826 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 827 | }; |
| 828 | |
| 829 | // Respond with 5 kb of response body. |
| 830 | std::string large_body_string("Unauthorized"); |
| 831 | large_body_string.append(5 * 1024, ' '); |
| 832 | large_body_string.append("\r\n"); |
| 833 | |
| 834 | MockRead data_reads1[] = { |
| 835 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 836 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 837 | MockRead("Content-Length: 0\r\n\r\n"), |
| 838 | |
| 839 | // Lastly, the server responds with the actual content. |
| 840 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 841 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 842 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 843 | MockRead(false, OK), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 844 | }; |
| 845 | |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 846 | StaticMockSocket data1(data_reads1, data_writes1); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 847 | session_deps.socket_factory.AddMockSocket(&data1); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 848 | |
| 849 | TestCompletionCallback callback1; |
| 850 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 851 | int rv = trans->Start(&request, &callback1, NULL); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 852 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 853 | |
| 854 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 855 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 856 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 857 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 858 | EXPECT_FALSE(response == NULL); |
| 859 | |
| 860 | // The password prompt info should have been set in response->auth_challenge. |
| 861 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 862 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 863 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 864 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 865 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 866 | |
| 867 | TestCompletionCallback callback2; |
| 868 | |
| 869 | rv = trans->RestartWithAuth(L"foo", L"bar", &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 870 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 871 | |
| 872 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 873 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 874 | |
| 875 | response = trans->GetResponseInfo(); |
| 876 | EXPECT_FALSE(response == NULL); |
| 877 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 878 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 879 | } |
| 880 | |
| 881 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
| 882 | // connection and with a large response body to drain. |
| 883 | TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAliveLargeBody) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 884 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 885 | scoped_ptr<HttpTransaction> trans( |
| 886 | new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 887 | CreateSession(&session_deps), |
| 888 | &session_deps.socket_factory)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 889 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 890 | HttpRequestInfo request; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 891 | request.method = "GET"; |
| 892 | request.url = GURL("http://www.google.com/"); |
| 893 | request.load_flags = 0; |
| 894 | |
| 895 | MockWrite data_writes1[] = { |
| 896 | MockWrite("GET / HTTP/1.1\r\n" |
| 897 | "Host: www.google.com\r\n" |
| 898 | "Connection: keep-alive\r\n\r\n"), |
| 899 | |
| 900 | // After calling trans->RestartWithAuth(), this is the request we should |
| 901 | // be issuing -- the final header line contains the credentials. |
| 902 | MockWrite("GET / HTTP/1.1\r\n" |
| 903 | "Host: www.google.com\r\n" |
| 904 | "Connection: keep-alive\r\n" |
| 905 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 906 | }; |
| 907 | |
| 908 | // Respond with 5 kb of response body. |
| 909 | std::string large_body_string("Unauthorized"); |
| 910 | large_body_string.append(5 * 1024, ' '); |
| 911 | large_body_string.append("\r\n"); |
| 912 | |
| 913 | MockRead data_reads1[] = { |
| 914 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 915 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 916 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 917 | // 5134 = 12 + 5 * 1024 + 2 |
| 918 | MockRead("Content-Length: 5134\r\n\r\n"), |
| 919 | MockRead(true, large_body_string.data(), large_body_string.size()), |
| 920 | |
| 921 | // Lastly, the server responds with the actual content. |
| 922 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 923 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 924 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 925 | MockRead(false, OK), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 926 | }; |
| 927 | |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 928 | StaticMockSocket data1(data_reads1, data_writes1); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 929 | session_deps.socket_factory.AddMockSocket(&data1); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 930 | |
| 931 | TestCompletionCallback callback1; |
| 932 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 933 | int rv = trans->Start(&request, &callback1, NULL); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 934 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 935 | |
| 936 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 937 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 938 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 939 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 940 | EXPECT_FALSE(response == NULL); |
| 941 | |
| 942 | // The password prompt info should have been set in response->auth_challenge. |
| 943 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 944 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 945 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 946 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 947 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 948 | |
| 949 | TestCompletionCallback callback2; |
| 950 | |
| 951 | rv = trans->RestartWithAuth(L"foo", L"bar", &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 952 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 953 | |
| 954 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 955 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 956 | |
| 957 | response = trans->GetResponseInfo(); |
| 958 | EXPECT_FALSE(response == NULL); |
| 959 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 960 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 961 | } |
| 962 | |
| 963 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
| 964 | // proxy connection, when setting up an SSL tunnel. |
| 965 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyKeepAlive) { |
| 966 | // Configure against proxy server "myproxy:70". |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 967 | SessionDependencies session_deps(CreateFixedProxyService("myproxy:70")); |
| 968 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 969 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 970 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 971 | session.get(), &session_deps.socket_factory)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 972 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 973 | HttpRequestInfo request; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 974 | request.method = "GET"; |
| 975 | request.url = GURL("https://www.google.com/"); |
| 976 | request.load_flags = 0; |
| 977 | |
| 978 | // Since we have proxy, should try to establish tunnel. |
| 979 | MockWrite data_writes1[] = { |
| 980 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 981 | "Host: www.google.com\r\n" |
| 982 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 983 | |
| 984 | // After calling trans->RestartWithAuth(), this is the request we should |
| 985 | // be issuing -- the final header line contains the credentials. |
| 986 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 987 | "Host: www.google.com\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 988 | "Proxy-Connection: keep-alive\r\n" |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 989 | "Proxy-Authorization: Basic Zm9vOmJheg==\r\n\r\n"), |
| 990 | }; |
| 991 | |
| 992 | // The proxy responds to the connect with a 407, using a persistent |
| 993 | // connection. |
| 994 | MockRead data_reads1[] = { |
| 995 | // No credentials. |
| 996 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 997 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 998 | MockRead("Content-Length: 10\r\n\r\n"), |
| 999 | MockRead("0123456789"), |
| 1000 | |
| 1001 | // Wrong credentials (wrong password). |
| 1002 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 1003 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1004 | MockRead("Content-Length: 10\r\n\r\n"), |
| 1005 | // No response body because the test stops reading here. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1006 | MockRead(false, ERR_UNEXPECTED), // Should not be reached. |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1007 | }; |
| 1008 | |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 1009 | StaticMockSocket data1(data_reads1, data_writes1); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1010 | session_deps.socket_factory.AddMockSocket(&data1); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1011 | |
| 1012 | TestCompletionCallback callback1; |
| 1013 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 1014 | int rv = trans->Start(&request, &callback1, NULL); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1015 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1016 | |
| 1017 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1018 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1019 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1020 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1021 | EXPECT_FALSE(response == NULL); |
| 1022 | |
| 1023 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 1024 | EXPECT_EQ(407, response->headers->response_code()); |
| 1025 | EXPECT_EQ(10, response->headers->GetContentLength()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1026 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1027 | |
| 1028 | // The password prompt info should have been set in response->auth_challenge. |
| 1029 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 1030 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 1031 | EXPECT_EQ(L"myproxy:70", response->auth_challenge->host_and_port); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1032 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 1033 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 1034 | |
| 1035 | TestCompletionCallback callback2; |
| 1036 | |
| 1037 | // Wrong password (should be "bar"). |
| 1038 | rv = trans->RestartWithAuth(L"foo", L"baz", &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1039 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1040 | |
| 1041 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1042 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1043 | |
| 1044 | response = trans->GetResponseInfo(); |
| 1045 | EXPECT_FALSE(response == NULL); |
| 1046 | |
| 1047 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 1048 | EXPECT_EQ(407, response->headers->response_code()); |
| 1049 | EXPECT_EQ(10, response->headers->GetContentLength()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1050 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1051 | |
| 1052 | // The password prompt info should have been set in response->auth_challenge. |
| 1053 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 1054 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 1055 | EXPECT_EQ(L"myproxy:70", response->auth_challenge->host_and_port); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1056 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 1057 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 1058 | } |
| 1059 | |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1060 | // Test that we don't read the response body when we fail to establish a tunnel, |
| 1061 | // even if the user cancels the proxy's auth attempt. |
| 1062 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyCancelTunnel) { |
| 1063 | // Configure against proxy server "myproxy:70". |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1064 | SessionDependencies session_deps(CreateFixedProxyService("myproxy:70")); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1065 | |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 1066 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1067 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1068 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1069 | session.get(), &session_deps.socket_factory)); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1070 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1071 | HttpRequestInfo request; |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1072 | request.method = "GET"; |
| 1073 | request.url = GURL("https://www.google.com/"); |
| 1074 | request.load_flags = 0; |
| 1075 | |
| 1076 | // Since we have proxy, should try to establish tunnel. |
| 1077 | MockWrite data_writes[] = { |
| 1078 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 1079 | "Host: www.google.com\r\n" |
| 1080 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1081 | }; |
| 1082 | |
| 1083 | // The proxy responds to the connect with a 407. |
| 1084 | MockRead data_reads[] = { |
| 1085 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 1086 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1087 | MockRead("Content-Length: 10\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1088 | MockRead(false, ERR_UNEXPECTED), // Should not be reached. |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1089 | }; |
| 1090 | |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 1091 | StaticMockSocket data(data_reads, data_writes); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1092 | session_deps.socket_factory.AddMockSocket(&data); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1093 | |
| 1094 | TestCompletionCallback callback; |
| 1095 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 1096 | int rv = trans->Start(&request, &callback, NULL); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1097 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1098 | |
| 1099 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1100 | EXPECT_EQ(OK, rv); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1101 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1102 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1103 | EXPECT_FALSE(response == NULL); |
| 1104 | |
| 1105 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 1106 | EXPECT_EQ(407, response->headers->response_code()); |
| 1107 | EXPECT_EQ(10, response->headers->GetContentLength()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1108 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1109 | |
| 1110 | std::string response_data; |
| 1111 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1112 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, rv); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1113 | } |
| 1114 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 1115 | void HttpNetworkTransactionTest::ConnectStatusHelperWithExpectedStatus( |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 1116 | const MockRead& status, int expected_status) { |
| 1117 | // Configure against proxy server "myproxy:70". |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1118 | SessionDependencies session_deps(CreateFixedProxyService("myproxy:70")); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 1119 | |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1120 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 1121 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1122 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1123 | session.get(), &session_deps.socket_factory)); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 1124 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1125 | HttpRequestInfo request; |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 1126 | request.method = "GET"; |
| 1127 | request.url = GURL("https://www.google.com/"); |
| 1128 | request.load_flags = 0; |
| 1129 | |
| 1130 | // Since we have proxy, should try to establish tunnel. |
| 1131 | MockWrite data_writes[] = { |
| 1132 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 1133 | "Host: www.google.com\r\n" |
| 1134 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 1135 | }; |
| 1136 | |
| 1137 | MockRead data_reads[] = { |
| 1138 | status, |
| 1139 | MockRead("Content-Length: 10\r\n\r\n"), |
| 1140 | // No response body because the test stops reading here. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1141 | MockRead(false, ERR_UNEXPECTED), // Should not be reached. |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 1142 | }; |
| 1143 | |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 1144 | StaticMockSocket data(data_reads, data_writes); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1145 | session_deps.socket_factory.AddMockSocket(&data); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 1146 | |
| 1147 | TestCompletionCallback callback; |
| 1148 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 1149 | int rv = trans->Start(&request, &callback, NULL); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1150 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 1151 | |
| 1152 | rv = callback.WaitForResult(); |
| 1153 | EXPECT_EQ(expected_status, rv); |
| 1154 | } |
| 1155 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 1156 | void HttpNetworkTransactionTest::ConnectStatusHelper(const MockRead& status) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 1157 | ConnectStatusHelperWithExpectedStatus( |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1158 | status, ERR_TUNNEL_CONNECTION_FAILED); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | TEST_F(HttpNetworkTransactionTest, ConnectStatus100) { |
| 1162 | ConnectStatusHelper(MockRead("HTTP/1.1 100 Continue\r\n")); |
| 1163 | } |
| 1164 | |
| 1165 | TEST_F(HttpNetworkTransactionTest, ConnectStatus101) { |
| 1166 | ConnectStatusHelper(MockRead("HTTP/1.1 101 Switching Protocols\r\n")); |
| 1167 | } |
| 1168 | |
| 1169 | TEST_F(HttpNetworkTransactionTest, ConnectStatus201) { |
| 1170 | ConnectStatusHelper(MockRead("HTTP/1.1 201 Created\r\n")); |
| 1171 | } |
| 1172 | |
| 1173 | TEST_F(HttpNetworkTransactionTest, ConnectStatus202) { |
| 1174 | ConnectStatusHelper(MockRead("HTTP/1.1 202 Accepted\r\n")); |
| 1175 | } |
| 1176 | |
| 1177 | TEST_F(HttpNetworkTransactionTest, ConnectStatus203) { |
| 1178 | ConnectStatusHelper( |
| 1179 | MockRead("HTTP/1.1 203 Non-Authoritative Information\r\n")); |
| 1180 | } |
| 1181 | |
| 1182 | TEST_F(HttpNetworkTransactionTest, ConnectStatus204) { |
| 1183 | ConnectStatusHelper(MockRead("HTTP/1.1 204 No Content\r\n")); |
| 1184 | } |
| 1185 | |
| 1186 | TEST_F(HttpNetworkTransactionTest, ConnectStatus205) { |
| 1187 | ConnectStatusHelper(MockRead("HTTP/1.1 205 Reset Content\r\n")); |
| 1188 | } |
| 1189 | |
| 1190 | TEST_F(HttpNetworkTransactionTest, ConnectStatus206) { |
| 1191 | ConnectStatusHelper(MockRead("HTTP/1.1 206 Partial Content\r\n")); |
| 1192 | } |
| 1193 | |
| 1194 | TEST_F(HttpNetworkTransactionTest, ConnectStatus300) { |
| 1195 | ConnectStatusHelper(MockRead("HTTP/1.1 300 Multiple Choices\r\n")); |
| 1196 | } |
| 1197 | |
| 1198 | TEST_F(HttpNetworkTransactionTest, ConnectStatus301) { |
| 1199 | ConnectStatusHelper(MockRead("HTTP/1.1 301 Moved Permanently\r\n")); |
| 1200 | } |
| 1201 | |
| 1202 | TEST_F(HttpNetworkTransactionTest, ConnectStatus302) { |
| 1203 | ConnectStatusHelper(MockRead("HTTP/1.1 302 Found\r\n")); |
| 1204 | } |
| 1205 | |
| 1206 | TEST_F(HttpNetworkTransactionTest, ConnectStatus303) { |
| 1207 | ConnectStatusHelper(MockRead("HTTP/1.1 303 See Other\r\n")); |
| 1208 | } |
| 1209 | |
| 1210 | TEST_F(HttpNetworkTransactionTest, ConnectStatus304) { |
| 1211 | ConnectStatusHelper(MockRead("HTTP/1.1 304 Not Modified\r\n")); |
| 1212 | } |
| 1213 | |
| 1214 | TEST_F(HttpNetworkTransactionTest, ConnectStatus305) { |
| 1215 | ConnectStatusHelper(MockRead("HTTP/1.1 305 Use Proxy\r\n")); |
| 1216 | } |
| 1217 | |
| 1218 | TEST_F(HttpNetworkTransactionTest, ConnectStatus306) { |
| 1219 | ConnectStatusHelper(MockRead("HTTP/1.1 306\r\n")); |
| 1220 | } |
| 1221 | |
| 1222 | TEST_F(HttpNetworkTransactionTest, ConnectStatus307) { |
| 1223 | ConnectStatusHelper(MockRead("HTTP/1.1 307 Temporary Redirect\r\n")); |
| 1224 | } |
| 1225 | |
| 1226 | TEST_F(HttpNetworkTransactionTest, ConnectStatus400) { |
| 1227 | ConnectStatusHelper(MockRead("HTTP/1.1 400 Bad Request\r\n")); |
| 1228 | } |
| 1229 | |
| 1230 | TEST_F(HttpNetworkTransactionTest, ConnectStatus401) { |
| 1231 | ConnectStatusHelper(MockRead("HTTP/1.1 401 Unauthorized\r\n")); |
| 1232 | } |
| 1233 | |
| 1234 | TEST_F(HttpNetworkTransactionTest, ConnectStatus402) { |
| 1235 | ConnectStatusHelper(MockRead("HTTP/1.1 402 Payment Required\r\n")); |
| 1236 | } |
| 1237 | |
| 1238 | TEST_F(HttpNetworkTransactionTest, ConnectStatus403) { |
| 1239 | ConnectStatusHelper(MockRead("HTTP/1.1 403 Forbidden\r\n")); |
| 1240 | } |
| 1241 | |
| 1242 | TEST_F(HttpNetworkTransactionTest, ConnectStatus404) { |
| 1243 | ConnectStatusHelper(MockRead("HTTP/1.1 404 Not Found\r\n")); |
| 1244 | } |
| 1245 | |
| 1246 | TEST_F(HttpNetworkTransactionTest, ConnectStatus405) { |
| 1247 | ConnectStatusHelper(MockRead("HTTP/1.1 405 Method Not Allowed\r\n")); |
| 1248 | } |
| 1249 | |
| 1250 | TEST_F(HttpNetworkTransactionTest, ConnectStatus406) { |
| 1251 | ConnectStatusHelper(MockRead("HTTP/1.1 406 Not Acceptable\r\n")); |
| 1252 | } |
| 1253 | |
| 1254 | TEST_F(HttpNetworkTransactionTest, ConnectStatus407) { |
| 1255 | ConnectStatusHelperWithExpectedStatus( |
| 1256 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1257 | ERR_PROXY_AUTH_REQUESTED); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | TEST_F(HttpNetworkTransactionTest, ConnectStatus408) { |
| 1261 | ConnectStatusHelper(MockRead("HTTP/1.1 408 Request Timeout\r\n")); |
| 1262 | } |
| 1263 | |
| 1264 | TEST_F(HttpNetworkTransactionTest, ConnectStatus409) { |
| 1265 | ConnectStatusHelper(MockRead("HTTP/1.1 409 Conflict\r\n")); |
| 1266 | } |
| 1267 | |
| 1268 | TEST_F(HttpNetworkTransactionTest, ConnectStatus410) { |
| 1269 | ConnectStatusHelper(MockRead("HTTP/1.1 410 Gone\r\n")); |
| 1270 | } |
| 1271 | |
| 1272 | TEST_F(HttpNetworkTransactionTest, ConnectStatus411) { |
| 1273 | ConnectStatusHelper(MockRead("HTTP/1.1 411 Length Required\r\n")); |
| 1274 | } |
| 1275 | |
| 1276 | TEST_F(HttpNetworkTransactionTest, ConnectStatus412) { |
| 1277 | ConnectStatusHelper(MockRead("HTTP/1.1 412 Precondition Failed\r\n")); |
| 1278 | } |
| 1279 | |
| 1280 | TEST_F(HttpNetworkTransactionTest, ConnectStatus413) { |
| 1281 | ConnectStatusHelper(MockRead("HTTP/1.1 413 Request Entity Too Large\r\n")); |
| 1282 | } |
| 1283 | |
| 1284 | TEST_F(HttpNetworkTransactionTest, ConnectStatus414) { |
| 1285 | ConnectStatusHelper(MockRead("HTTP/1.1 414 Request-URI Too Long\r\n")); |
| 1286 | } |
| 1287 | |
| 1288 | TEST_F(HttpNetworkTransactionTest, ConnectStatus415) { |
| 1289 | ConnectStatusHelper(MockRead("HTTP/1.1 415 Unsupported Media Type\r\n")); |
| 1290 | } |
| 1291 | |
| 1292 | TEST_F(HttpNetworkTransactionTest, ConnectStatus416) { |
| 1293 | ConnectStatusHelper( |
| 1294 | MockRead("HTTP/1.1 416 Requested Range Not Satisfiable\r\n")); |
| 1295 | } |
| 1296 | |
| 1297 | TEST_F(HttpNetworkTransactionTest, ConnectStatus417) { |
| 1298 | ConnectStatusHelper(MockRead("HTTP/1.1 417 Expectation Failed\r\n")); |
| 1299 | } |
| 1300 | |
| 1301 | TEST_F(HttpNetworkTransactionTest, ConnectStatus500) { |
| 1302 | ConnectStatusHelper(MockRead("HTTP/1.1 500 Internal Server Error\r\n")); |
| 1303 | } |
| 1304 | |
| 1305 | TEST_F(HttpNetworkTransactionTest, ConnectStatus501) { |
| 1306 | ConnectStatusHelper(MockRead("HTTP/1.1 501 Not Implemented\r\n")); |
| 1307 | } |
| 1308 | |
| 1309 | TEST_F(HttpNetworkTransactionTest, ConnectStatus502) { |
| 1310 | ConnectStatusHelper(MockRead("HTTP/1.1 502 Bad Gateway\r\n")); |
| 1311 | } |
| 1312 | |
| 1313 | TEST_F(HttpNetworkTransactionTest, ConnectStatus503) { |
| 1314 | ConnectStatusHelper(MockRead("HTTP/1.1 503 Service Unavailable\r\n")); |
| 1315 | } |
| 1316 | |
| 1317 | TEST_F(HttpNetworkTransactionTest, ConnectStatus504) { |
| 1318 | ConnectStatusHelper(MockRead("HTTP/1.1 504 Gateway Timeout\r\n")); |
| 1319 | } |
| 1320 | |
| 1321 | TEST_F(HttpNetworkTransactionTest, ConnectStatus505) { |
| 1322 | ConnectStatusHelper(MockRead("HTTP/1.1 505 HTTP Version Not Supported\r\n")); |
| 1323 | } |
| 1324 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1325 | // Test the flow when both the proxy server AND origin server require |
| 1326 | // authentication. Again, this uses basic auth for both since that is |
| 1327 | // the simplest to mock. |
| 1328 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyThenServer) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1329 | SessionDependencies session_deps(CreateFixedProxyService("myproxy:70")); |
[email protected] | db8f44c | 2008-12-13 04:52:01 | [diff] [blame] | 1330 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1331 | // Configure against proxy server "myproxy:70". |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1332 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1333 | CreateSession(&session_deps), |
| 1334 | &session_deps.socket_factory)); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1335 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1336 | HttpRequestInfo request; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1337 | request.method = "GET"; |
| 1338 | request.url = GURL("http://www.google.com/"); |
| 1339 | request.load_flags = 0; |
| 1340 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1341 | MockWrite data_writes1[] = { |
| 1342 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 1343 | "Host: www.google.com\r\n" |
| 1344 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 1345 | }; |
| 1346 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1347 | MockRead data_reads1[] = { |
| 1348 | MockRead("HTTP/1.0 407 Unauthorized\r\n"), |
| 1349 | // Give a couple authenticate options (only the middle one is actually |
| 1350 | // supported). |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 1351 | MockRead("Proxy-Authenticate: Basic\r\n"), // Malformed |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1352 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1353 | MockRead("Proxy-Authenticate: UNSUPPORTED realm=\"FOO\"\r\n"), |
| 1354 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1355 | // Large content-length -- won't matter, as connection will be reset. |
| 1356 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1357 | MockRead(false, ERR_FAILED), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1358 | }; |
| 1359 | |
| 1360 | // After calling trans->RestartWithAuth() the first time, this is the |
| 1361 | // request we should be issuing -- the final header line contains the |
| 1362 | // proxy's credentials. |
| 1363 | MockWrite data_writes2[] = { |
| 1364 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 1365 | "Host: www.google.com\r\n" |
| 1366 | "Proxy-Connection: keep-alive\r\n" |
| 1367 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1368 | }; |
| 1369 | |
| 1370 | // Now the proxy server lets the request pass through to origin server. |
| 1371 | // The origin server responds with a 401. |
| 1372 | MockRead data_reads2[] = { |
| 1373 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 1374 | // Note: We are using the same realm-name as the proxy server. This is |
| 1375 | // completely valid, as realms are unique across hosts. |
| 1376 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1377 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1378 | MockRead("Content-Length: 2000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1379 | MockRead(false, ERR_FAILED), // Won't be reached. |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1380 | }; |
| 1381 | |
| 1382 | // After calling trans->RestartWithAuth() the second time, we should send |
| 1383 | // the credentials for both the proxy and origin server. |
| 1384 | MockWrite data_writes3[] = { |
| 1385 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 1386 | "Host: www.google.com\r\n" |
| 1387 | "Proxy-Connection: keep-alive\r\n" |
| 1388 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n" |
| 1389 | "Authorization: Basic Zm9vMjpiYXIy\r\n\r\n"), |
| 1390 | }; |
| 1391 | |
| 1392 | // Lastly we get the desired content. |
| 1393 | MockRead data_reads3[] = { |
| 1394 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 1395 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1396 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1397 | MockRead(false, OK), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1398 | }; |
| 1399 | |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 1400 | StaticMockSocket data1(data_reads1, data_writes1); |
| 1401 | StaticMockSocket data2(data_reads2, data_writes2); |
| 1402 | StaticMockSocket data3(data_reads3, data_writes3); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1403 | session_deps.socket_factory.AddMockSocket(&data1); |
| 1404 | session_deps.socket_factory.AddMockSocket(&data2); |
| 1405 | session_deps.socket_factory.AddMockSocket(&data3); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1406 | |
| 1407 | TestCompletionCallback callback1; |
| 1408 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 1409 | int rv = trans->Start(&request, &callback1, NULL); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1410 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1411 | |
| 1412 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1413 | EXPECT_EQ(OK, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1414 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1415 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1416 | EXPECT_FALSE(response == NULL); |
| 1417 | |
| 1418 | // The password prompt info should have been set in response->auth_challenge. |
| 1419 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 1420 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 1421 | EXPECT_EQ(L"myproxy:70", response->auth_challenge->host_and_port); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1422 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 1423 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 1424 | |
| 1425 | TestCompletionCallback callback2; |
| 1426 | |
| 1427 | rv = trans->RestartWithAuth(L"foo", L"bar", &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1428 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1429 | |
| 1430 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1431 | EXPECT_EQ(OK, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1432 | |
| 1433 | response = trans->GetResponseInfo(); |
| 1434 | EXPECT_FALSE(response == NULL); |
| 1435 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 1436 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 1437 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1438 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 1439 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 1440 | |
| 1441 | TestCompletionCallback callback3; |
| 1442 | |
| 1443 | rv = trans->RestartWithAuth(L"foo2", L"bar2", &callback3); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1444 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1445 | |
| 1446 | rv = callback3.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1447 | EXPECT_EQ(OK, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1448 | |
| 1449 | response = trans->GetResponseInfo(); |
| 1450 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 1451 | EXPECT_EQ(100, response->headers->GetContentLength()); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1452 | } |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 1453 | |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 1454 | // The NTLM authentication unit tests were generated by capturing the HTTP |
| 1455 | // requests and responses using Fiddler 2 and inspecting the generated random |
| 1456 | // bytes in the debugger. |
| 1457 | |
| 1458 | // Enter the correct password and authenticate successfully. |
| 1459 | TEST_F(HttpNetworkTransactionTest, NTLMAuth1) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1460 | HttpAuthHandlerNTLM::ScopedProcSetter proc_setter(MockGenerateRandom1, |
[email protected] | fe2bc6a | 2009-03-23 16:52:20 | [diff] [blame] | 1461 | MockGetHostName); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1462 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 1463 | scoped_ptr<HttpTransaction> trans( |
| 1464 | new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1465 | CreateSession(&session_deps), |
| 1466 | &session_deps.socket_factory)); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 1467 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1468 | HttpRequestInfo request; |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 1469 | request.method = "GET"; |
| 1470 | request.url = GURL("http://172.22.68.17/kids/login.aspx"); |
| 1471 | request.load_flags = 0; |
| 1472 | |
| 1473 | MockWrite data_writes1[] = { |
| 1474 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 1475 | "Host: 172.22.68.17\r\n" |
| 1476 | "Connection: keep-alive\r\n\r\n"), |
| 1477 | }; |
| 1478 | |
| 1479 | MockRead data_reads1[] = { |
| 1480 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
| 1481 | // Negotiate and NTLM are often requested together. We only support NTLM. |
| 1482 | MockRead("WWW-Authenticate: Negotiate\r\n"), |
| 1483 | MockRead("WWW-Authenticate: NTLM\r\n"), |
| 1484 | MockRead("Connection: close\r\n"), |
| 1485 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 1486 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 1487 | // Missing content -- won't matter, as connection will be reset. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1488 | MockRead(false, ERR_UNEXPECTED), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 1489 | }; |
| 1490 | |
| 1491 | MockWrite data_writes2[] = { |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1492 | // After restarting with a null identity, this is the |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 1493 | // request we should be issuing -- the final header line contains a Type |
| 1494 | // 1 message. |
| 1495 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 1496 | "Host: 172.22.68.17\r\n" |
| 1497 | "Connection: keep-alive\r\n" |
| 1498 | "Authorization: NTLM " |
| 1499 | "TlRMTVNTUAABAAAAB4IIAAAAAAAAAAAAAAAAAAAAAAA=\r\n\r\n"), |
| 1500 | |
| 1501 | // After calling trans->RestartWithAuth(), we should send a Type 3 message |
| 1502 | // (the credentials for the origin server). The second request continues |
| 1503 | // on the same connection. |
| 1504 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 1505 | "Host: 172.22.68.17\r\n" |
| 1506 | "Connection: keep-alive\r\n" |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 1507 | "Authorization: NTLM TlRMTVNTUAADAAAAGAAYAGgAAAAYABgAgA" |
| 1508 | "AAAAAAAABAAAAAGAAYAEAAAAAQABAAWAAAAAAAAAAAAAAABYIIAHQA" |
| 1509 | "ZQBzAHQAaQBuAGcALQBuAHQAbABtAFcAVABDAC0AVwBJAE4ANwBVKW" |
| 1510 | "Yma5xzVAAAAAAAAAAAAAAAAAAAAACH+gWcm+YsP9Tqb9zCR3WAeZZX" |
| 1511 | "ahlhx5I=\r\n\r\n"), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 1512 | }; |
| 1513 | |
| 1514 | MockRead data_reads2[] = { |
| 1515 | // The origin server responds with a Type 2 message. |
| 1516 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
| 1517 | MockRead("WWW-Authenticate: NTLM " |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 1518 | "TlRMTVNTUAACAAAADAAMADgAAAAFgokCjGpMpPGlYKkAAAAAAAAAALo" |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 1519 | "AugBEAAAABQEoCgAAAA9HAE8ATwBHAEwARQACAAwARwBPAE8ARwBMAE" |
| 1520 | "UAAQAaAEEASwBFAEUAUwBBAFIAQQAtAEMATwBSAFAABAAeAGMAbwByA" |
| 1521 | "HAALgBnAG8AbwBnAGwAZQAuAGMAbwBtAAMAQABhAGsAZQBlAHMAYQBy" |
| 1522 | "AGEALQBjAG8AcgBwAC4AYQBkAC4AYwBvAHIAcAAuAGcAbwBvAGcAbAB" |
| 1523 | "lAC4AYwBvAG0ABQAeAGMAbwByAHAALgBnAG8AbwBnAGwAZQAuAGMAbw" |
| 1524 | "BtAAAAAAA=\r\n"), |
| 1525 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 1526 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 1527 | MockRead("You are not authorized to view this page\r\n"), |
| 1528 | |
| 1529 | // Lastly we get the desired content. |
| 1530 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1531 | MockRead("Content-Type: text/html; charset=utf-8\r\n"), |
| 1532 | MockRead("Content-Length: 13\r\n\r\n"), |
| 1533 | MockRead("Please Login\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1534 | MockRead(false, OK), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 1535 | }; |
| 1536 | |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 1537 | StaticMockSocket data1(data_reads1, data_writes1); |
| 1538 | StaticMockSocket data2(data_reads2, data_writes2); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1539 | session_deps.socket_factory.AddMockSocket(&data1); |
| 1540 | session_deps.socket_factory.AddMockSocket(&data2); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 1541 | |
| 1542 | TestCompletionCallback callback1; |
| 1543 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 1544 | int rv = trans->Start(&request, &callback1, NULL); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1545 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 1546 | |
| 1547 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1548 | EXPECT_EQ(OK, rv); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 1549 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1550 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
| 1551 | TestCompletionCallback callback2; |
| 1552 | rv = trans->RestartWithAuth(std::wstring(), std::wstring(), &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1553 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1554 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1555 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1556 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 1557 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1558 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 1559 | EXPECT_FALSE(response == NULL); |
| 1560 | |
| 1561 | // The password prompt info should have been set in response->auth_challenge. |
| 1562 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 1563 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 1564 | EXPECT_EQ(L"172.22.68.17:80", response->auth_challenge->host_and_port); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 1565 | EXPECT_EQ(L"", response->auth_challenge->realm); |
| 1566 | EXPECT_EQ(L"ntlm", response->auth_challenge->scheme); |
| 1567 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1568 | TestCompletionCallback callback3; |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 1569 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1570 | rv = trans->RestartWithAuth(L"testing-ntlm", L"testing-ntlm", &callback3); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1571 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 1572 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1573 | rv = callback3.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1574 | EXPECT_EQ(OK, rv); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 1575 | |
| 1576 | response = trans->GetResponseInfo(); |
| 1577 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 1578 | EXPECT_EQ(13, response->headers->GetContentLength()); |
| 1579 | } |
| 1580 | |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 1581 | // Enter a wrong password, and then the correct one. |
| 1582 | TEST_F(HttpNetworkTransactionTest, NTLMAuth2) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1583 | HttpAuthHandlerNTLM::ScopedProcSetter proc_setter(MockGenerateRandom2, |
[email protected] | fe2bc6a | 2009-03-23 16:52:20 | [diff] [blame] | 1584 | MockGetHostName); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1585 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 1586 | scoped_ptr<HttpTransaction> trans( |
| 1587 | new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1588 | CreateSession(&session_deps), |
| 1589 | &session_deps.socket_factory)); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 1590 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1591 | HttpRequestInfo request; |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 1592 | request.method = "GET"; |
| 1593 | request.url = GURL("http://172.22.68.17/kids/login.aspx"); |
| 1594 | request.load_flags = 0; |
| 1595 | |
| 1596 | MockWrite data_writes1[] = { |
| 1597 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 1598 | "Host: 172.22.68.17\r\n" |
| 1599 | "Connection: keep-alive\r\n\r\n"), |
| 1600 | }; |
| 1601 | |
| 1602 | MockRead data_reads1[] = { |
| 1603 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
| 1604 | // Negotiate and NTLM are often requested together. We only support NTLM. |
| 1605 | MockRead("WWW-Authenticate: Negotiate\r\n"), |
| 1606 | MockRead("WWW-Authenticate: NTLM\r\n"), |
| 1607 | MockRead("Connection: close\r\n"), |
| 1608 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 1609 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 1610 | // Missing content -- won't matter, as connection will be reset. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1611 | MockRead(false, ERR_UNEXPECTED), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 1612 | }; |
| 1613 | |
| 1614 | MockWrite data_writes2[] = { |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1615 | // After restarting with a null identity, this is the |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 1616 | // request we should be issuing -- the final header line contains a Type |
| 1617 | // 1 message. |
| 1618 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 1619 | "Host: 172.22.68.17\r\n" |
| 1620 | "Connection: keep-alive\r\n" |
| 1621 | "Authorization: NTLM " |
| 1622 | "TlRMTVNTUAABAAAAB4IIAAAAAAAAAAAAAAAAAAAAAAA=\r\n\r\n"), |
| 1623 | |
| 1624 | // After calling trans->RestartWithAuth(), we should send a Type 3 message |
| 1625 | // (the credentials for the origin server). The second request continues |
| 1626 | // on the same connection. |
| 1627 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 1628 | "Host: 172.22.68.17\r\n" |
| 1629 | "Connection: keep-alive\r\n" |
| 1630 | "Authorization: NTLM TlRMTVNTUAADAAAAGAAYAGgAAAAYABgAgA" |
| 1631 | "AAAAAAAABAAAAAGAAYAEAAAAAQABAAWAAAAAAAAAAAAAAABYIIAHQA" |
| 1632 | "ZQBzAHQAaQBuAGcALQBuAHQAbABtAFcAVABDAC0AVwBJAE4ANwCWeY" |
| 1633 | "XnSZNwoQAAAAAAAAAAAAAAAAAAAADLa34/phTTKzNTWdub+uyFleOj" |
| 1634 | "4Ww7b7E=\r\n\r\n"), |
| 1635 | }; |
| 1636 | |
| 1637 | MockRead data_reads2[] = { |
| 1638 | // The origin server responds with a Type 2 message. |
| 1639 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
| 1640 | MockRead("WWW-Authenticate: NTLM " |
| 1641 | "TlRMTVNTUAACAAAADAAMADgAAAAFgokCbVWUZezVGpAAAAAAAAAAALo" |
| 1642 | "AugBEAAAABQEoCgAAAA9HAE8ATwBHAEwARQACAAwARwBPAE8ARwBMAE" |
| 1643 | "UAAQAaAEEASwBFAEUAUwBBAFIAQQAtAEMATwBSAFAABAAeAGMAbwByA" |
| 1644 | "HAALgBnAG8AbwBnAGwAZQAuAGMAbwBtAAMAQABhAGsAZQBlAHMAYQBy" |
| 1645 | "AGEALQBjAG8AcgBwAC4AYQBkAC4AYwBvAHIAcAAuAGcAbwBvAGcAbAB" |
| 1646 | "lAC4AYwBvAG0ABQAeAGMAbwByAHAALgBnAG8AbwBnAGwAZQAuAGMAbw" |
| 1647 | "BtAAAAAAA=\r\n"), |
| 1648 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 1649 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 1650 | MockRead("You are not authorized to view this page\r\n"), |
| 1651 | |
| 1652 | // Wrong password. |
| 1653 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
| 1654 | MockRead("WWW-Authenticate: Negotiate\r\n"), |
| 1655 | MockRead("WWW-Authenticate: NTLM\r\n"), |
| 1656 | MockRead("Connection: close\r\n"), |
| 1657 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 1658 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 1659 | // Missing content -- won't matter, as connection will be reset. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1660 | MockRead(false, ERR_UNEXPECTED), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 1661 | }; |
| 1662 | |
| 1663 | MockWrite data_writes3[] = { |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1664 | // After restarting with a null identity, this is the |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 1665 | // request we should be issuing -- the final header line contains a Type |
| 1666 | // 1 message. |
| 1667 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 1668 | "Host: 172.22.68.17\r\n" |
| 1669 | "Connection: keep-alive\r\n" |
| 1670 | "Authorization: NTLM " |
| 1671 | "TlRMTVNTUAABAAAAB4IIAAAAAAAAAAAAAAAAAAAAAAA=\r\n\r\n"), |
| 1672 | |
| 1673 | // After calling trans->RestartWithAuth(), we should send a Type 3 message |
| 1674 | // (the credentials for the origin server). The second request continues |
| 1675 | // on the same connection. |
| 1676 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 1677 | "Host: 172.22.68.17\r\n" |
| 1678 | "Connection: keep-alive\r\n" |
| 1679 | "Authorization: NTLM TlRMTVNTUAADAAAAGAAYAGgAAAAYABgAgA" |
| 1680 | "AAAAAAAABAAAAAGAAYAEAAAAAQABAAWAAAAAAAAAAAAAAABYIIAHQA" |
| 1681 | "ZQBzAHQAaQBuAGcALQBuAHQAbABtAFcAVABDAC0AVwBJAE4ANwBO54" |
| 1682 | "dFMVvTHwAAAAAAAAAAAAAAAAAAAACS7sT6Uzw7L0L//WUqlIaVWpbI" |
| 1683 | "+4MUm7c=\r\n\r\n"), |
| 1684 | }; |
| 1685 | |
| 1686 | MockRead data_reads3[] = { |
| 1687 | // The origin server responds with a Type 2 message. |
| 1688 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
| 1689 | MockRead("WWW-Authenticate: NTLM " |
| 1690 | "TlRMTVNTUAACAAAADAAMADgAAAAFgokCL24VN8dgOR8AAAAAAAAAALo" |
| 1691 | "AugBEAAAABQEoCgAAAA9HAE8ATwBHAEwARQACAAwARwBPAE8ARwBMAE" |
| 1692 | "UAAQAaAEEASwBFAEUAUwBBAFIAQQAtAEMATwBSAFAABAAeAGMAbwByA" |
| 1693 | "HAALgBnAG8AbwBnAGwAZQAuAGMAbwBtAAMAQABhAGsAZQBlAHMAYQBy" |
| 1694 | "AGEALQBjAG8AcgBwAC4AYQBkAC4AYwBvAHIAcAAuAGcAbwBvAGcAbAB" |
| 1695 | "lAC4AYwBvAG0ABQAeAGMAbwByAHAALgBnAG8AbwBnAGwAZQAuAGMAbw" |
| 1696 | "BtAAAAAAA=\r\n"), |
| 1697 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 1698 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 1699 | MockRead("You are not authorized to view this page\r\n"), |
| 1700 | |
| 1701 | // Lastly we get the desired content. |
| 1702 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1703 | MockRead("Content-Type: text/html; charset=utf-8\r\n"), |
| 1704 | MockRead("Content-Length: 13\r\n\r\n"), |
| 1705 | MockRead("Please Login\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1706 | MockRead(false, OK), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 1707 | }; |
| 1708 | |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 1709 | StaticMockSocket data1(data_reads1, data_writes1); |
| 1710 | StaticMockSocket data2(data_reads2, data_writes2); |
| 1711 | StaticMockSocket data3(data_reads3, data_writes3); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1712 | session_deps.socket_factory.AddMockSocket(&data1); |
| 1713 | session_deps.socket_factory.AddMockSocket(&data2); |
| 1714 | session_deps.socket_factory.AddMockSocket(&data3); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 1715 | |
| 1716 | TestCompletionCallback callback1; |
| 1717 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 1718 | int rv = trans->Start(&request, &callback1, NULL); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1719 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 1720 | |
| 1721 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1722 | EXPECT_EQ(OK, rv); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 1723 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1724 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 1725 | TestCompletionCallback callback2; |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1726 | rv = trans->RestartWithAuth(std::wstring(), std::wstring(), &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1727 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 1728 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1729 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1730 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 1731 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1732 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 1733 | EXPECT_FALSE(response == NULL); |
| 1734 | |
| 1735 | // The password prompt info should have been set in response->auth_challenge. |
| 1736 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 1737 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 1738 | EXPECT_EQ(L"172.22.68.17:80", response->auth_challenge->host_and_port); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 1739 | EXPECT_EQ(L"", response->auth_challenge->realm); |
| 1740 | EXPECT_EQ(L"ntlm", response->auth_challenge->scheme); |
| 1741 | |
| 1742 | TestCompletionCallback callback3; |
| 1743 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1744 | // Enter the wrong password. |
| 1745 | rv = trans->RestartWithAuth(L"testing-ntlm", L"wrongpassword", &callback3); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1746 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 1747 | |
| 1748 | rv = callback3.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1749 | EXPECT_EQ(OK, rv); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 1750 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1751 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
| 1752 | TestCompletionCallback callback4; |
| 1753 | rv = trans->RestartWithAuth(std::wstring(), std::wstring(), &callback4); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1754 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1755 | rv = callback4.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1756 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1757 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 1758 | |
| 1759 | response = trans->GetResponseInfo(); |
| 1760 | EXPECT_FALSE(response == NULL); |
| 1761 | |
| 1762 | // The password prompt info should have been set in response->auth_challenge. |
| 1763 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 1764 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 1765 | EXPECT_EQ(L"172.22.68.17:80", response->auth_challenge->host_and_port); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1766 | EXPECT_EQ(L"", response->auth_challenge->realm); |
| 1767 | EXPECT_EQ(L"ntlm", response->auth_challenge->scheme); |
| 1768 | |
| 1769 | TestCompletionCallback callback5; |
| 1770 | |
| 1771 | // Now enter the right password. |
| 1772 | rv = trans->RestartWithAuth(L"testing-ntlm", L"testing-ntlm", &callback5); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1773 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1774 | |
| 1775 | rv = callback5.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1776 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1777 | |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 1778 | response = trans->GetResponseInfo(); |
| 1779 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 1780 | EXPECT_EQ(13, response->headers->GetContentLength()); |
| 1781 | } |
| 1782 | |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 1783 | // Test reading a server response which has only headers, and no body. |
| 1784 | // After some maximum number of bytes is consumed, the transaction should |
| 1785 | // fail with ERR_RESPONSE_HEADERS_TOO_BIG. |
| 1786 | TEST_F(HttpNetworkTransactionTest, LargeHeadersNoBody) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1787 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 1788 | scoped_ptr<HttpTransaction> trans( |
| 1789 | new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1790 | CreateSession(&session_deps), |
| 1791 | &session_deps.socket_factory)); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 1792 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1793 | HttpRequestInfo request; |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 1794 | request.method = "GET"; |
| 1795 | request.url = GURL("http://www.google.com/"); |
| 1796 | request.load_flags = 0; |
| 1797 | |
| 1798 | // Respond with 50 kb of headers (we should fail after 32 kb). |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 1799 | std::string large_headers_string; |
| 1800 | FillLargeHeadersString(&large_headers_string, 50 * 1024); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 1801 | |
| 1802 | MockRead data_reads[] = { |
| 1803 | MockRead("HTTP/1.0 200 OK\r\n"), |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 1804 | MockRead(true, large_headers_string.data(), large_headers_string.size()), |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 1805 | MockRead("\r\nBODY"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1806 | MockRead(false, OK), |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 1807 | }; |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 1808 | StaticMockSocket data(data_reads, NULL); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1809 | session_deps.socket_factory.AddMockSocket(&data); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 1810 | |
| 1811 | TestCompletionCallback callback; |
| 1812 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 1813 | int rv = trans->Start(&request, &callback, NULL); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1814 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 1815 | |
| 1816 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1817 | EXPECT_EQ(ERR_RESPONSE_HEADERS_TOO_BIG, rv); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 1818 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1819 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 1820 | EXPECT_TRUE(response == NULL); |
| 1821 | } |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 1822 | |
| 1823 | // Make sure that we don't try to reuse a TCPClientSocket when failing to |
| 1824 | // establish tunnel. |
| 1825 | // http://code.google.com/p/chromium/issues/detail?id=3772 |
| 1826 | TEST_F(HttpNetworkTransactionTest, DontRecycleTCPSocketForSSLTunnel) { |
| 1827 | // Configure against proxy server "myproxy:70". |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1828 | SessionDependencies session_deps(CreateFixedProxyService("myproxy:70")); |
[email protected] | db8f44c | 2008-12-13 04:52:01 | [diff] [blame] | 1829 | |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1830 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 1831 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1832 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1833 | session.get(), &session_deps.socket_factory)); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 1834 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1835 | HttpRequestInfo request; |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 1836 | request.method = "GET"; |
| 1837 | request.url = GURL("https://www.google.com/"); |
| 1838 | request.load_flags = 0; |
| 1839 | |
| 1840 | // Since we have proxy, should try to establish tunnel. |
| 1841 | MockWrite data_writes1[] = { |
| 1842 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 1843 | "Host: www.google.com\r\n" |
| 1844 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 1845 | }; |
| 1846 | |
[email protected] | 77848d1 | 2008-11-14 00:00:22 | [diff] [blame] | 1847 | // The proxy responds to the connect with a 404, using a persistent |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 1848 | // connection. Usually a proxy would return 501 (not implemented), |
| 1849 | // or 200 (tunnel established). |
| 1850 | MockRead data_reads1[] = { |
| 1851 | MockRead("HTTP/1.1 404 Not Found\r\n"), |
| 1852 | MockRead("Content-Length: 10\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1853 | MockRead(false, ERR_UNEXPECTED), // Should not be reached. |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 1854 | }; |
| 1855 | |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 1856 | StaticMockSocket data1(data_reads1, data_writes1); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1857 | session_deps.socket_factory.AddMockSocket(&data1); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 1858 | |
| 1859 | TestCompletionCallback callback1; |
| 1860 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 1861 | int rv = trans->Start(&request, &callback1, NULL); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1862 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 1863 | |
| 1864 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1865 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, rv); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 1866 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1867 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 1868 | EXPECT_TRUE(response == NULL); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 1869 | |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 1870 | // Empty the current queue. This is necessary because idle sockets are |
| 1871 | // added to the connection pool asynchronously with a PostTask. |
| 1872 | MessageLoop::current()->RunAllPending(); |
| 1873 | |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 1874 | // We now check to make sure the TCPClientSocket was not added back to |
| 1875 | // the pool. |
[email protected] | df0c05c6 | 2009-06-17 15:50:58 | [diff] [blame] | 1876 | EXPECT_EQ(0, session->connection_pool()->IdleSocketCount()); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 1877 | trans.reset(); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 1878 | MessageLoop::current()->RunAllPending(); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 1879 | // Make sure that the socket didn't get recycled after calling the destructor. |
[email protected] | df0c05c6 | 2009-06-17 15:50:58 | [diff] [blame] | 1880 | EXPECT_EQ(0, session->connection_pool()->IdleSocketCount()); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 1881 | } |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 1882 | |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 1883 | // Make sure that we recycle a socket after reading all of the response body. |
| 1884 | TEST_F(HttpNetworkTransactionTest, RecycleSocket) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1885 | SessionDependencies session_deps; |
| 1886 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 1887 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1888 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1889 | session.get(), &session_deps.socket_factory)); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 1890 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1891 | HttpRequestInfo request; |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 1892 | request.method = "GET"; |
| 1893 | request.url = GURL("http://www.google.com/"); |
| 1894 | request.load_flags = 0; |
| 1895 | |
| 1896 | MockRead data_reads[] = { |
| 1897 | // A part of the response body is received with the response headers. |
| 1898 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\nhel"), |
| 1899 | // The rest of the response body is received in two parts. |
| 1900 | MockRead("lo"), |
| 1901 | MockRead(" world"), |
| 1902 | MockRead("junk"), // Should not be read!! |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1903 | MockRead(false, OK), |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 1904 | }; |
| 1905 | |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 1906 | StaticMockSocket data(data_reads, NULL); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1907 | session_deps.socket_factory.AddMockSocket(&data); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 1908 | |
| 1909 | TestCompletionCallback callback; |
| 1910 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 1911 | int rv = trans->Start(&request, &callback, NULL); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1912 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 1913 | |
| 1914 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1915 | EXPECT_EQ(OK, rv); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 1916 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1917 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 1918 | EXPECT_TRUE(response != NULL); |
| 1919 | |
| 1920 | EXPECT_TRUE(response->headers != NULL); |
| 1921 | std::string status_line = response->headers->GetStatusLine(); |
| 1922 | EXPECT_EQ("HTTP/1.1 200 OK", status_line); |
| 1923 | |
[email protected] | df0c05c6 | 2009-06-17 15:50:58 | [diff] [blame] | 1924 | EXPECT_EQ(0, session->connection_pool()->IdleSocketCount()); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 1925 | |
| 1926 | std::string response_data; |
| 1927 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1928 | EXPECT_EQ(OK, rv); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 1929 | EXPECT_EQ("hello world", response_data); |
| 1930 | |
| 1931 | // Empty the current queue. This is necessary because idle sockets are |
| 1932 | // added to the connection pool asynchronously with a PostTask. |
| 1933 | MessageLoop::current()->RunAllPending(); |
| 1934 | |
| 1935 | // We now check to make sure the socket was added back to the pool. |
[email protected] | df0c05c6 | 2009-06-17 15:50:58 | [diff] [blame] | 1936 | EXPECT_EQ(1, session->connection_pool()->IdleSocketCount()); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 1937 | } |
| 1938 | |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 1939 | // Make sure that we recycle a socket after a zero-length response. |
| 1940 | // http://crbug.com/9880 |
| 1941 | TEST_F(HttpNetworkTransactionTest, RecycleSocketAfterZeroContentLength) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1942 | SessionDependencies session_deps; |
| 1943 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 1944 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1945 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1946 | session.get(), &session_deps.socket_factory)); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 1947 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1948 | HttpRequestInfo request; |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 1949 | request.method = "GET"; |
| 1950 | request.url = GURL("http://www.google.com/csi?v=3&s=web&action=&" |
| 1951 | "tran=undefined&ei=mAXcSeegAo-SMurloeUN&" |
| 1952 | "e=17259,18167,19592,19773,19981,20133,20173,20233&" |
| 1953 | "rt=prt.2642,ol.2649,xjs.2951"); |
| 1954 | request.load_flags = 0; |
| 1955 | |
| 1956 | MockRead data_reads[] = { |
| 1957 | MockRead("HTTP/1.1 204 No Content\r\n" |
| 1958 | "Content-Length: 0\r\n" |
| 1959 | "Content-Type: text/html\r\n\r\n"), |
| 1960 | MockRead("junk"), // Should not be read!! |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1961 | MockRead(false, OK), |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 1962 | }; |
| 1963 | |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 1964 | StaticMockSocket data(data_reads, NULL); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1965 | session_deps.socket_factory.AddMockSocket(&data); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 1966 | |
| 1967 | TestCompletionCallback callback; |
| 1968 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 1969 | int rv = trans->Start(&request, &callback, NULL); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1970 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 1971 | |
| 1972 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1973 | EXPECT_EQ(OK, rv); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 1974 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1975 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 1976 | EXPECT_TRUE(response != NULL); |
| 1977 | |
| 1978 | EXPECT_TRUE(response->headers != NULL); |
| 1979 | std::string status_line = response->headers->GetStatusLine(); |
| 1980 | EXPECT_EQ("HTTP/1.1 204 No Content", status_line); |
| 1981 | |
[email protected] | df0c05c6 | 2009-06-17 15:50:58 | [diff] [blame] | 1982 | EXPECT_EQ(0, session->connection_pool()->IdleSocketCount()); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 1983 | |
| 1984 | std::string response_data; |
| 1985 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1986 | EXPECT_EQ(OK, rv); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 1987 | EXPECT_EQ("", response_data); |
| 1988 | |
| 1989 | // Empty the current queue. This is necessary because idle sockets are |
| 1990 | // added to the connection pool asynchronously with a PostTask. |
| 1991 | MessageLoop::current()->RunAllPending(); |
| 1992 | |
| 1993 | // We now check to make sure the socket was added back to the pool. |
[email protected] | df0c05c6 | 2009-06-17 15:50:58 | [diff] [blame] | 1994 | EXPECT_EQ(1, session->connection_pool()->IdleSocketCount()); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 1995 | } |
| 1996 | |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 1997 | TEST_F(HttpNetworkTransactionTest, ResendRequestOnWriteBodyError) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1998 | HttpRequestInfo request[2]; |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 1999 | // Transaction 1: a GET request that succeeds. The socket is recycled |
| 2000 | // after use. |
| 2001 | request[0].method = "GET"; |
| 2002 | request[0].url = GURL("http://www.google.com/"); |
| 2003 | request[0].load_flags = 0; |
| 2004 | // Transaction 2: a POST request. Reuses the socket kept alive from |
| 2005 | // transaction 1. The first attempts fails when writing the POST data. |
| 2006 | // This causes the transaction to retry with a new socket. The second |
| 2007 | // attempt succeeds. |
| 2008 | request[1].method = "POST"; |
| 2009 | request[1].url = GURL("http://www.google.com/login.cgi"); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2010 | request[1].upload_data = new UploadData; |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 2011 | request[1].upload_data->AppendBytes("foo", 3); |
| 2012 | request[1].load_flags = 0; |
| 2013 | |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2014 | SessionDependencies session_deps; |
| 2015 | scoped_refptr<HttpNetworkSession> session = CreateSession(&session_deps); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 2016 | |
| 2017 | // The first socket is used for transaction 1 and the first attempt of |
| 2018 | // transaction 2. |
| 2019 | |
| 2020 | // The response of transaction 1. |
| 2021 | MockRead data_reads1[] = { |
| 2022 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\n"), |
| 2023 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2024 | MockRead(false, OK), |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 2025 | }; |
| 2026 | // The mock write results of transaction 1 and the first attempt of |
| 2027 | // transaction 2. |
| 2028 | MockWrite data_writes1[] = { |
| 2029 | MockWrite(false, 64), // GET |
| 2030 | MockWrite(false, 93), // POST |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2031 | MockWrite(false, ERR_CONNECTION_ABORTED), // POST data |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 2032 | }; |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 2033 | StaticMockSocket data1(data_reads1, data_writes1); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 2034 | |
| 2035 | // The second socket is used for the second attempt of transaction 2. |
| 2036 | |
| 2037 | // The response of transaction 2. |
| 2038 | MockRead data_reads2[] = { |
| 2039 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 7\r\n\r\n"), |
| 2040 | MockRead("welcome"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2041 | MockRead(false, OK), |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 2042 | }; |
| 2043 | // The mock write results of the second attempt of transaction 2. |
| 2044 | MockWrite data_writes2[] = { |
| 2045 | MockWrite(false, 93), // POST |
| 2046 | MockWrite(false, 3), // POST data |
| 2047 | }; |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 2048 | StaticMockSocket data2(data_reads2, data_writes2); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 2049 | |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2050 | session_deps.socket_factory.AddMockSocket(&data1); |
| 2051 | session_deps.socket_factory.AddMockSocket(&data2); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 2052 | |
| 2053 | const char* kExpectedResponseData[] = { |
| 2054 | "hello world", "welcome" |
| 2055 | }; |
| 2056 | |
| 2057 | for (int i = 0; i < 2; ++i) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2058 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2059 | new HttpNetworkTransaction(session, &session_deps.socket_factory)); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 2060 | |
| 2061 | TestCompletionCallback callback; |
| 2062 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 2063 | int rv = trans->Start(&request[i], &callback, NULL); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2064 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 2065 | |
| 2066 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2067 | EXPECT_EQ(OK, rv); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 2068 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2069 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 2070 | EXPECT_TRUE(response != NULL); |
| 2071 | |
| 2072 | EXPECT_TRUE(response->headers != NULL); |
| 2073 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 2074 | |
| 2075 | std::string response_data; |
| 2076 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2077 | EXPECT_EQ(OK, rv); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 2078 | EXPECT_EQ(kExpectedResponseData[i], response_data); |
| 2079 | } |
| 2080 | } |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2081 | |
| 2082 | // Test the request-challenge-retry sequence for basic auth when there is |
| 2083 | // an identity in the URL. The request should be sent as normal, but when |
| 2084 | // it fails the identity from the URL is used to answer the challenge. |
| 2085 | TEST_F(HttpNetworkTransactionTest, AuthIdentityInUrl) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2086 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 2087 | scoped_ptr<HttpTransaction> trans( |
| 2088 | new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2089 | CreateSession(&session_deps), |
| 2090 | &session_deps.socket_factory)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2091 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2092 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2093 | request.method = "GET"; |
| 2094 | // Note: the URL has a username:password in it. |
[email protected] | a97cca4 | 2009-08-14 01:00:29 | [diff] [blame] | 2095 | request.url = GURL("http://foo:b@[email protected]/"); |
| 2096 | |
| 2097 | // The password contains an escaped character -- for this test to pass it |
| 2098 | // will need to be unescaped by HttpNetworkTransaction. |
| 2099 | EXPECT_EQ("b%40r", request.url.password()); |
| 2100 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2101 | request.load_flags = 0; |
| 2102 | |
| 2103 | MockWrite data_writes1[] = { |
| 2104 | MockWrite("GET / HTTP/1.1\r\n" |
| 2105 | "Host: www.google.com\r\n" |
| 2106 | "Connection: keep-alive\r\n\r\n"), |
| 2107 | }; |
| 2108 | |
| 2109 | MockRead data_reads1[] = { |
| 2110 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 2111 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 2112 | MockRead("Content-Length: 10\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2113 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2114 | }; |
| 2115 | |
| 2116 | // After the challenge above, the transaction will be restarted using the |
| 2117 | // identity from the url (foo, bar) to answer the challenge. |
| 2118 | MockWrite data_writes2[] = { |
| 2119 | MockWrite("GET / HTTP/1.1\r\n" |
| 2120 | "Host: www.google.com\r\n" |
| 2121 | "Connection: keep-alive\r\n" |
[email protected] | a97cca4 | 2009-08-14 01:00:29 | [diff] [blame] | 2122 | "Authorization: Basic Zm9vOmJAcg==\r\n\r\n"), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2123 | }; |
| 2124 | |
| 2125 | MockRead data_reads2[] = { |
| 2126 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 2127 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2128 | MockRead(false, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2129 | }; |
| 2130 | |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 2131 | StaticMockSocket data1(data_reads1, data_writes1); |
| 2132 | StaticMockSocket data2(data_reads2, data_writes2); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2133 | session_deps.socket_factory.AddMockSocket(&data1); |
| 2134 | session_deps.socket_factory.AddMockSocket(&data2); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2135 | |
| 2136 | TestCompletionCallback callback1; |
| 2137 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 2138 | int rv = trans->Start(&request, &callback1, NULL); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2139 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2140 | |
| 2141 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2142 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2143 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2144 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
| 2145 | TestCompletionCallback callback2; |
| 2146 | rv = trans->RestartWithAuth(std::wstring(), std::wstring(), &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2147 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2148 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2149 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2150 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 2151 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2152 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2153 | EXPECT_FALSE(response == NULL); |
| 2154 | |
| 2155 | // There is no challenge info, since the identity in URL worked. |
| 2156 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 2157 | |
| 2158 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 2159 | |
| 2160 | // Empty the current queue. |
| 2161 | MessageLoop::current()->RunAllPending(); |
| 2162 | } |
| 2163 | |
| 2164 | // Test that previously tried username/passwords for a realm get re-used. |
| 2165 | TEST_F(HttpNetworkTransactionTest, BasicAuthCacheAndPreauth) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2166 | SessionDependencies session_deps; |
| 2167 | scoped_refptr<HttpNetworkSession> session = CreateSession(&session_deps); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2168 | |
| 2169 | // Transaction 1: authenticate (foo, bar) on MyRealm1 |
| 2170 | { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2171 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2172 | session, &session_deps.socket_factory)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2173 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2174 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2175 | request.method = "GET"; |
| 2176 | request.url = GURL("http://www.google.com/x/y/z"); |
| 2177 | request.load_flags = 0; |
| 2178 | |
| 2179 | MockWrite data_writes1[] = { |
| 2180 | MockWrite("GET /x/y/z HTTP/1.1\r\n" |
| 2181 | "Host: www.google.com\r\n" |
| 2182 | "Connection: keep-alive\r\n\r\n"), |
| 2183 | }; |
| 2184 | |
| 2185 | MockRead data_reads1[] = { |
| 2186 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 2187 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 2188 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2189 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2190 | }; |
| 2191 | |
| 2192 | // Resend with authorization (username=foo, password=bar) |
| 2193 | MockWrite data_writes2[] = { |
| 2194 | MockWrite("GET /x/y/z HTTP/1.1\r\n" |
| 2195 | "Host: www.google.com\r\n" |
| 2196 | "Connection: keep-alive\r\n" |
| 2197 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 2198 | }; |
| 2199 | |
| 2200 | // Sever accepts the authorization. |
| 2201 | MockRead data_reads2[] = { |
| 2202 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 2203 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2204 | MockRead(false, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2205 | }; |
| 2206 | |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 2207 | StaticMockSocket data1(data_reads1, data_writes1); |
| 2208 | StaticMockSocket data2(data_reads2, data_writes2); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2209 | session_deps.socket_factory.AddMockSocket(&data1); |
| 2210 | session_deps.socket_factory.AddMockSocket(&data2); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2211 | |
| 2212 | TestCompletionCallback callback1; |
| 2213 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 2214 | int rv = trans->Start(&request, &callback1, NULL); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2215 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2216 | |
| 2217 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2218 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2219 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2220 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2221 | EXPECT_FALSE(response == NULL); |
| 2222 | |
| 2223 | // The password prompt info should have been set in |
| 2224 | // response->auth_challenge. |
| 2225 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 2226 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 2227 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2228 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 2229 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 2230 | |
| 2231 | TestCompletionCallback callback2; |
| 2232 | |
| 2233 | rv = trans->RestartWithAuth(L"foo", L"bar", &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2234 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2235 | |
| 2236 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2237 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2238 | |
| 2239 | response = trans->GetResponseInfo(); |
| 2240 | EXPECT_FALSE(response == NULL); |
| 2241 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 2242 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 2243 | } |
| 2244 | |
| 2245 | // ------------------------------------------------------------------------ |
| 2246 | |
| 2247 | // Transaction 2: authenticate (foo2, bar2) on MyRealm2 |
| 2248 | { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2249 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2250 | session, &session_deps.socket_factory)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2251 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2252 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2253 | request.method = "GET"; |
| 2254 | // Note that Transaction 1 was at /x/y/z, so this is in the same |
| 2255 | // protection space as MyRealm1. |
| 2256 | request.url = GURL("http://www.google.com/x/y/a/b"); |
| 2257 | request.load_flags = 0; |
| 2258 | |
| 2259 | MockWrite data_writes1[] = { |
| 2260 | MockWrite("GET /x/y/a/b HTTP/1.1\r\n" |
| 2261 | "Host: www.google.com\r\n" |
| 2262 | "Connection: keep-alive\r\n" |
| 2263 | // Send preemptive authorization for MyRealm1 |
| 2264 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 2265 | }; |
| 2266 | |
| 2267 | // The server didn't like the preemptive authorization, and |
| 2268 | // challenges us for a different realm (MyRealm2). |
| 2269 | MockRead data_reads1[] = { |
| 2270 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 2271 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm2\"\r\n"), |
| 2272 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2273 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2274 | }; |
| 2275 | |
| 2276 | // Resend with authorization for MyRealm2 (username=foo2, password=bar2) |
| 2277 | MockWrite data_writes2[] = { |
| 2278 | MockWrite("GET /x/y/a/b HTTP/1.1\r\n" |
| 2279 | "Host: www.google.com\r\n" |
| 2280 | "Connection: keep-alive\r\n" |
| 2281 | "Authorization: Basic Zm9vMjpiYXIy\r\n\r\n"), |
| 2282 | }; |
| 2283 | |
| 2284 | // Sever accepts the authorization. |
| 2285 | MockRead data_reads2[] = { |
| 2286 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 2287 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2288 | MockRead(false, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2289 | }; |
| 2290 | |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 2291 | StaticMockSocket data1(data_reads1, data_writes1); |
| 2292 | StaticMockSocket data2(data_reads2, data_writes2); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2293 | session_deps.socket_factory.AddMockSocket(&data1); |
| 2294 | session_deps.socket_factory.AddMockSocket(&data2); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2295 | |
| 2296 | TestCompletionCallback callback1; |
| 2297 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 2298 | int rv = trans->Start(&request, &callback1, NULL); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2299 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2300 | |
| 2301 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2302 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2303 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2304 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2305 | EXPECT_FALSE(response == NULL); |
| 2306 | |
| 2307 | // The password prompt info should have been set in |
| 2308 | // response->auth_challenge. |
| 2309 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 2310 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 2311 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2312 | EXPECT_EQ(L"MyRealm2", response->auth_challenge->realm); |
| 2313 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 2314 | |
| 2315 | TestCompletionCallback callback2; |
| 2316 | |
| 2317 | rv = trans->RestartWithAuth(L"foo2", L"bar2", &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2318 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2319 | |
| 2320 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2321 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2322 | |
| 2323 | response = trans->GetResponseInfo(); |
| 2324 | EXPECT_FALSE(response == NULL); |
| 2325 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 2326 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 2327 | } |
| 2328 | |
| 2329 | // ------------------------------------------------------------------------ |
| 2330 | |
| 2331 | // Transaction 3: Resend a request in MyRealm's protection space -- |
| 2332 | // succeed with preemptive authorization. |
| 2333 | { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2334 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2335 | session, &session_deps.socket_factory)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2336 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2337 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2338 | request.method = "GET"; |
| 2339 | request.url = GURL("http://www.google.com/x/y/z2"); |
| 2340 | request.load_flags = 0; |
| 2341 | |
| 2342 | MockWrite data_writes1[] = { |
| 2343 | MockWrite("GET /x/y/z2 HTTP/1.1\r\n" |
| 2344 | "Host: www.google.com\r\n" |
| 2345 | "Connection: keep-alive\r\n" |
| 2346 | // The authorization for MyRealm1 gets sent preemptively |
| 2347 | // (since the url is in the same protection space) |
| 2348 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 2349 | }; |
| 2350 | |
| 2351 | // Sever accepts the preemptive authorization |
| 2352 | MockRead data_reads1[] = { |
| 2353 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 2354 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2355 | MockRead(false, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2356 | }; |
| 2357 | |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 2358 | StaticMockSocket data1(data_reads1, data_writes1); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2359 | session_deps.socket_factory.AddMockSocket(&data1); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2360 | |
| 2361 | TestCompletionCallback callback1; |
| 2362 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 2363 | int rv = trans->Start(&request, &callback1, NULL); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2364 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2365 | |
| 2366 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2367 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2368 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2369 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2370 | EXPECT_FALSE(response == NULL); |
| 2371 | |
| 2372 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 2373 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 2374 | } |
| 2375 | |
| 2376 | // ------------------------------------------------------------------------ |
| 2377 | |
| 2378 | // Transaction 4: request another URL in MyRealm (however the |
| 2379 | // url is not known to belong to the protection space, so no pre-auth). |
| 2380 | { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2381 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2382 | session, &session_deps.socket_factory)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2383 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2384 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2385 | request.method = "GET"; |
| 2386 | request.url = GURL("http://www.google.com/x/1"); |
| 2387 | request.load_flags = 0; |
| 2388 | |
| 2389 | MockWrite data_writes1[] = { |
| 2390 | MockWrite("GET /x/1 HTTP/1.1\r\n" |
| 2391 | "Host: www.google.com\r\n" |
| 2392 | "Connection: keep-alive\r\n\r\n"), |
| 2393 | }; |
| 2394 | |
| 2395 | MockRead data_reads1[] = { |
| 2396 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 2397 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 2398 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2399 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2400 | }; |
| 2401 | |
| 2402 | // Resend with authorization from MyRealm's cache. |
| 2403 | MockWrite data_writes2[] = { |
| 2404 | MockWrite("GET /x/1 HTTP/1.1\r\n" |
| 2405 | "Host: www.google.com\r\n" |
| 2406 | "Connection: keep-alive\r\n" |
| 2407 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 2408 | }; |
| 2409 | |
| 2410 | // Sever accepts the authorization. |
| 2411 | MockRead data_reads2[] = { |
| 2412 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 2413 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2414 | MockRead(false, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2415 | }; |
| 2416 | |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 2417 | StaticMockSocket data1(data_reads1, data_writes1); |
| 2418 | StaticMockSocket data2(data_reads2, data_writes2); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2419 | session_deps.socket_factory.AddMockSocket(&data1); |
| 2420 | session_deps.socket_factory.AddMockSocket(&data2); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2421 | |
| 2422 | TestCompletionCallback callback1; |
| 2423 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 2424 | int rv = trans->Start(&request, &callback1, NULL); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2425 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2426 | |
| 2427 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2428 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2429 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2430 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
| 2431 | TestCompletionCallback callback2; |
| 2432 | rv = trans->RestartWithAuth(std::wstring(), std::wstring(), &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2433 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2434 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2435 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2436 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 2437 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2438 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2439 | EXPECT_FALSE(response == NULL); |
| 2440 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 2441 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 2442 | } |
| 2443 | |
| 2444 | // ------------------------------------------------------------------------ |
| 2445 | |
| 2446 | // Transaction 5: request a URL in MyRealm, but the server rejects the |
| 2447 | // cached identity. Should invalidate and re-prompt. |
| 2448 | { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2449 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2450 | session, &session_deps.socket_factory)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2451 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2452 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2453 | request.method = "GET"; |
| 2454 | request.url = GURL("http://www.google.com/p/q/t"); |
| 2455 | request.load_flags = 0; |
| 2456 | |
| 2457 | MockWrite data_writes1[] = { |
| 2458 | MockWrite("GET /p/q/t HTTP/1.1\r\n" |
| 2459 | "Host: www.google.com\r\n" |
| 2460 | "Connection: keep-alive\r\n\r\n"), |
| 2461 | }; |
| 2462 | |
| 2463 | MockRead data_reads1[] = { |
| 2464 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 2465 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 2466 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2467 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2468 | }; |
| 2469 | |
| 2470 | // Resend with authorization from cache for MyRealm. |
| 2471 | MockWrite data_writes2[] = { |
| 2472 | MockWrite("GET /p/q/t HTTP/1.1\r\n" |
| 2473 | "Host: www.google.com\r\n" |
| 2474 | "Connection: keep-alive\r\n" |
| 2475 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 2476 | }; |
| 2477 | |
| 2478 | // Sever rejects the authorization. |
| 2479 | MockRead data_reads2[] = { |
| 2480 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 2481 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 2482 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2483 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2484 | }; |
| 2485 | |
| 2486 | // At this point we should prompt for new credentials for MyRealm. |
| 2487 | // Restart with username=foo3, password=foo4. |
| 2488 | MockWrite data_writes3[] = { |
| 2489 | MockWrite("GET /p/q/t HTTP/1.1\r\n" |
| 2490 | "Host: www.google.com\r\n" |
| 2491 | "Connection: keep-alive\r\n" |
| 2492 | "Authorization: Basic Zm9vMzpiYXIz\r\n\r\n"), |
| 2493 | }; |
| 2494 | |
| 2495 | // Sever accepts the authorization. |
| 2496 | MockRead data_reads3[] = { |
| 2497 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 2498 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2499 | MockRead(false, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2500 | }; |
| 2501 | |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 2502 | StaticMockSocket data1(data_reads1, data_writes1); |
| 2503 | StaticMockSocket data2(data_reads2, data_writes2); |
| 2504 | StaticMockSocket data3(data_reads3, data_writes3); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2505 | session_deps.socket_factory.AddMockSocket(&data1); |
| 2506 | session_deps.socket_factory.AddMockSocket(&data2); |
| 2507 | session_deps.socket_factory.AddMockSocket(&data3); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2508 | |
| 2509 | TestCompletionCallback callback1; |
| 2510 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 2511 | int rv = trans->Start(&request, &callback1, NULL); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2512 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2513 | |
| 2514 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2515 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2516 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2517 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
| 2518 | TestCompletionCallback callback2; |
| 2519 | rv = trans->RestartWithAuth(std::wstring(), std::wstring(), &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2520 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2521 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2522 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2523 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 2524 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2525 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2526 | EXPECT_FALSE(response == NULL); |
| 2527 | |
| 2528 | // The password prompt info should have been set in |
| 2529 | // response->auth_challenge. |
| 2530 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 2531 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 2532 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2533 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 2534 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 2535 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2536 | TestCompletionCallback callback3; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2537 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2538 | rv = trans->RestartWithAuth(L"foo3", L"bar3", &callback3); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2539 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2540 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2541 | rv = callback3.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2542 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2543 | |
| 2544 | response = trans->GetResponseInfo(); |
| 2545 | EXPECT_FALSE(response == NULL); |
| 2546 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 2547 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 2548 | } |
| 2549 | } |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 2550 | |
| 2551 | // Test the ResetStateForRestart() private method. |
| 2552 | TEST_F(HttpNetworkTransactionTest, ResetStateForRestart) { |
| 2553 | // Create a transaction (the dependencies aren't important). |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2554 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 2555 | scoped_ptr<HttpNetworkTransaction> trans( |
| 2556 | new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2557 | CreateSession(&session_deps), |
| 2558 | &session_deps.socket_factory)); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 2559 | |
| 2560 | // Setup some state (which we expect ResetStateForRestart() will clear). |
[email protected] | ffeb088 | 2009-04-30 21:51:25 | [diff] [blame] | 2561 | trans->header_buf_->Realloc(10); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 2562 | trans->header_buf_capacity_ = 10; |
| 2563 | trans->header_buf_len_ = 3; |
| 2564 | trans->header_buf_body_offset_ = 11; |
| 2565 | trans->header_buf_http_offset_ = 0; |
| 2566 | trans->response_body_length_ = 100; |
| 2567 | trans->response_body_read_ = 1; |
| 2568 | trans->read_buf_ = new IOBuffer(15); |
| 2569 | trans->read_buf_len_ = 15; |
[email protected] | ffeb088 | 2009-04-30 21:51:25 | [diff] [blame] | 2570 | trans->request_headers_->headers_ = "Authorization: NTLM"; |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 2571 | trans->request_headers_bytes_sent_ = 3; |
| 2572 | |
| 2573 | // Setup state in response_ |
| 2574 | trans->response_.auth_challenge = new AuthChallengeInfo(); |
| 2575 | trans->response_.ssl_info.cert_status = -15; |
| 2576 | trans->response_.response_time = base::Time::Now(); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 2577 | trans->response_.was_cached = true; // (Wouldn't ever actually be true...) |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 2578 | |
| 2579 | { // Setup state for response_.vary_data |
| 2580 | HttpRequestInfo request; |
| 2581 | std::string temp("HTTP/1.1 200 OK\nVary: foo, bar\n\n"); |
| 2582 | std::replace(temp.begin(), temp.end(), '\n', '\0'); |
| 2583 | scoped_refptr<HttpResponseHeaders> response = new HttpResponseHeaders(temp); |
| 2584 | request.extra_headers = "Foo: 1\nbar: 23"; |
| 2585 | EXPECT_TRUE(trans->response_.vary_data.Init(request, *response)); |
| 2586 | } |
| 2587 | |
| 2588 | // Cause the above state to be reset. |
| 2589 | trans->ResetStateForRestart(); |
| 2590 | |
| 2591 | // Verify that the state that needed to be reset, has been reset. |
[email protected] | ffeb088 | 2009-04-30 21:51:25 | [diff] [blame] | 2592 | EXPECT_EQ(NULL, trans->header_buf_->headers()); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 2593 | EXPECT_EQ(0, trans->header_buf_capacity_); |
| 2594 | EXPECT_EQ(0, trans->header_buf_len_); |
| 2595 | EXPECT_EQ(-1, trans->header_buf_body_offset_); |
| 2596 | EXPECT_EQ(-1, trans->header_buf_http_offset_); |
| 2597 | EXPECT_EQ(-1, trans->response_body_length_); |
| 2598 | EXPECT_EQ(0, trans->response_body_read_); |
| 2599 | EXPECT_EQ(NULL, trans->read_buf_.get()); |
| 2600 | EXPECT_EQ(0, trans->read_buf_len_); |
[email protected] | ffeb088 | 2009-04-30 21:51:25 | [diff] [blame] | 2601 | EXPECT_EQ("", trans->request_headers_->headers_); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 2602 | EXPECT_EQ(0U, trans->request_headers_bytes_sent_); |
| 2603 | EXPECT_EQ(NULL, trans->response_.auth_challenge.get()); |
| 2604 | EXPECT_EQ(NULL, trans->response_.headers.get()); |
| 2605 | EXPECT_EQ(false, trans->response_.was_cached); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 2606 | EXPECT_EQ(0, trans->response_.ssl_info.cert_status); |
| 2607 | EXPECT_FALSE(trans->response_.vary_data.is_valid()); |
| 2608 | } |
| 2609 | |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 2610 | // Test HTTPS connections to a site with a bad certificate |
| 2611 | TEST_F(HttpNetworkTransactionTest, HTTPSBadCertificate) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2612 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 2613 | scoped_ptr<HttpTransaction> trans( |
| 2614 | new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2615 | CreateSession(&session_deps), |
| 2616 | &session_deps.socket_factory)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 2617 | |
| 2618 | HttpRequestInfo request; |
| 2619 | request.method = "GET"; |
| 2620 | request.url = GURL("https://www.google.com/"); |
| 2621 | request.load_flags = 0; |
| 2622 | |
| 2623 | MockWrite data_writes[] = { |
| 2624 | MockWrite("GET / HTTP/1.1\r\n" |
| 2625 | "Host: www.google.com\r\n" |
| 2626 | "Connection: keep-alive\r\n\r\n"), |
| 2627 | }; |
| 2628 | |
| 2629 | MockRead data_reads[] = { |
| 2630 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 2631 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2632 | MockRead("Content-Length: 100\r\n\r\n"), |
| 2633 | MockRead(false, OK), |
| 2634 | }; |
| 2635 | |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 2636 | StaticMockSocket ssl_bad_certificate; |
| 2637 | StaticMockSocket data(data_reads, data_writes); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 2638 | MockSSLSocket ssl_bad(true, ERR_CERT_AUTHORITY_INVALID); |
| 2639 | MockSSLSocket ssl(true, OK); |
| 2640 | |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2641 | session_deps.socket_factory.AddMockSocket(&ssl_bad_certificate); |
| 2642 | session_deps.socket_factory.AddMockSocket(&data); |
| 2643 | session_deps.socket_factory.AddMockSSLSocket(&ssl_bad); |
| 2644 | session_deps.socket_factory.AddMockSSLSocket(&ssl); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 2645 | |
| 2646 | TestCompletionCallback callback; |
| 2647 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 2648 | int rv = trans->Start(&request, &callback, NULL); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 2649 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2650 | |
| 2651 | rv = callback.WaitForResult(); |
| 2652 | EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, rv); |
| 2653 | |
| 2654 | rv = trans->RestartIgnoringLastError(&callback); |
| 2655 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2656 | |
| 2657 | rv = callback.WaitForResult(); |
| 2658 | EXPECT_EQ(OK, rv); |
| 2659 | |
| 2660 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 2661 | |
| 2662 | EXPECT_FALSE(response == NULL); |
| 2663 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 2664 | } |
| 2665 | |
| 2666 | // Test HTTPS connections to a site with a bad certificate, going through a |
| 2667 | // proxy |
| 2668 | TEST_F(HttpNetworkTransactionTest, HTTPSBadCertificateViaProxy) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2669 | SessionDependencies session_deps(CreateFixedProxyService("myproxy:70")); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 2670 | |
| 2671 | HttpRequestInfo request; |
| 2672 | request.method = "GET"; |
| 2673 | request.url = GURL("https://www.google.com/"); |
| 2674 | request.load_flags = 0; |
| 2675 | |
| 2676 | MockWrite proxy_writes[] = { |
| 2677 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 2678 | "Host: www.google.com\r\n" |
| 2679 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 2680 | }; |
| 2681 | |
| 2682 | MockRead proxy_reads[] = { |
| 2683 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2684 | MockRead(false, OK) |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 2685 | }; |
| 2686 | |
| 2687 | MockWrite data_writes[] = { |
| 2688 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 2689 | "Host: www.google.com\r\n" |
| 2690 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 2691 | MockWrite("GET / HTTP/1.1\r\n" |
| 2692 | "Host: www.google.com\r\n" |
| 2693 | "Connection: keep-alive\r\n\r\n"), |
| 2694 | }; |
| 2695 | |
| 2696 | MockRead data_reads[] = { |
| 2697 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
| 2698 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 2699 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2700 | MockRead("Content-Length: 100\r\n\r\n"), |
| 2701 | MockRead(false, OK), |
| 2702 | }; |
| 2703 | |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 2704 | StaticMockSocket ssl_bad_certificate(proxy_reads, proxy_writes); |
| 2705 | StaticMockSocket data(data_reads, data_writes); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 2706 | MockSSLSocket ssl_bad(true, ERR_CERT_AUTHORITY_INVALID); |
| 2707 | MockSSLSocket ssl(true, OK); |
| 2708 | |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2709 | session_deps.socket_factory.AddMockSocket(&ssl_bad_certificate); |
| 2710 | session_deps.socket_factory.AddMockSocket(&data); |
| 2711 | session_deps.socket_factory.AddMockSSLSocket(&ssl_bad); |
| 2712 | session_deps.socket_factory.AddMockSSLSocket(&ssl); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 2713 | |
| 2714 | TestCompletionCallback callback; |
| 2715 | |
| 2716 | for (int i = 0; i < 2; i++) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2717 | session_deps.socket_factory.ResetNextMockIndexes(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 2718 | |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 2719 | scoped_ptr<HttpTransaction> trans( |
| 2720 | new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2721 | CreateSession(&session_deps), |
| 2722 | &session_deps.socket_factory)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 2723 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 2724 | int rv = trans->Start(&request, &callback, NULL); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 2725 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2726 | |
| 2727 | rv = callback.WaitForResult(); |
| 2728 | EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, rv); |
| 2729 | |
| 2730 | rv = trans->RestartIgnoringLastError(&callback); |
| 2731 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2732 | |
| 2733 | rv = callback.WaitForResult(); |
| 2734 | EXPECT_EQ(OK, rv); |
| 2735 | |
| 2736 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 2737 | |
| 2738 | EXPECT_FALSE(response == NULL); |
| 2739 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 2740 | } |
| 2741 | } |
| 2742 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2743 | TEST_F(HttpNetworkTransactionTest, BuildRequest_UserAgent) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2744 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 2745 | scoped_ptr<HttpTransaction> trans( |
| 2746 | new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2747 | CreateSession(&session_deps), |
| 2748 | &session_deps.socket_factory)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2749 | |
| 2750 | HttpRequestInfo request; |
| 2751 | request.method = "GET"; |
| 2752 | request.url = GURL("http://www.google.com/"); |
| 2753 | request.user_agent = "Chromium Ultra Awesome X Edition"; |
| 2754 | |
| 2755 | MockWrite data_writes[] = { |
| 2756 | MockWrite("GET / HTTP/1.1\r\n" |
| 2757 | "Host: www.google.com\r\n" |
| 2758 | "Connection: keep-alive\r\n" |
| 2759 | "User-Agent: Chromium Ultra Awesome X Edition\r\n\r\n"), |
| 2760 | }; |
| 2761 | |
| 2762 | // Lastly, the server responds with the actual content. |
| 2763 | MockRead data_reads[] = { |
| 2764 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 2765 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2766 | MockRead("Content-Length: 100\r\n\r\n"), |
| 2767 | MockRead(false, OK), |
| 2768 | }; |
| 2769 | |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 2770 | StaticMockSocket data(data_reads, data_writes); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2771 | session_deps.socket_factory.AddMockSocket(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2772 | |
| 2773 | TestCompletionCallback callback; |
| 2774 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 2775 | int rv = trans->Start(&request, &callback, NULL); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2776 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2777 | |
| 2778 | rv = callback.WaitForResult(); |
| 2779 | EXPECT_EQ(OK, rv); |
| 2780 | } |
| 2781 | |
| 2782 | TEST_F(HttpNetworkTransactionTest, BuildRequest_Referer) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2783 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 2784 | scoped_ptr<HttpTransaction> trans( |
| 2785 | new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2786 | CreateSession(&session_deps), |
| 2787 | &session_deps.socket_factory)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2788 | |
| 2789 | HttpRequestInfo request; |
| 2790 | request.method = "GET"; |
| 2791 | request.url = GURL("http://www.google.com/"); |
| 2792 | request.load_flags = 0; |
| 2793 | request.referrer = GURL("http://the.previous.site.com/"); |
| 2794 | |
| 2795 | MockWrite data_writes[] = { |
| 2796 | MockWrite("GET / HTTP/1.1\r\n" |
| 2797 | "Host: www.google.com\r\n" |
| 2798 | "Connection: keep-alive\r\n" |
| 2799 | "Referer: http://the.previous.site.com/\r\n\r\n"), |
| 2800 | }; |
| 2801 | |
| 2802 | // Lastly, the server responds with the actual content. |
| 2803 | MockRead data_reads[] = { |
| 2804 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 2805 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2806 | MockRead("Content-Length: 100\r\n\r\n"), |
| 2807 | MockRead(false, OK), |
| 2808 | }; |
| 2809 | |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 2810 | StaticMockSocket data(data_reads, data_writes); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2811 | session_deps.socket_factory.AddMockSocket(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2812 | |
| 2813 | TestCompletionCallback callback; |
| 2814 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 2815 | int rv = trans->Start(&request, &callback, NULL); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2816 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2817 | |
| 2818 | rv = callback.WaitForResult(); |
| 2819 | EXPECT_EQ(OK, rv); |
| 2820 | } |
| 2821 | |
| 2822 | TEST_F(HttpNetworkTransactionTest, BuildRequest_PostContentLengthZero) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2823 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 2824 | scoped_ptr<HttpTransaction> trans( |
| 2825 | new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2826 | CreateSession(&session_deps), |
| 2827 | &session_deps.socket_factory)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2828 | |
| 2829 | HttpRequestInfo request; |
| 2830 | request.method = "POST"; |
| 2831 | request.url = GURL("http://www.google.com/"); |
| 2832 | |
| 2833 | MockWrite data_writes[] = { |
| 2834 | MockWrite("POST / HTTP/1.1\r\n" |
| 2835 | "Host: www.google.com\r\n" |
| 2836 | "Connection: keep-alive\r\n" |
| 2837 | "Content-Length: 0\r\n\r\n"), |
| 2838 | }; |
| 2839 | |
| 2840 | // Lastly, the server responds with the actual content. |
| 2841 | MockRead data_reads[] = { |
| 2842 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 2843 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2844 | MockRead("Content-Length: 100\r\n\r\n"), |
| 2845 | MockRead(false, OK), |
| 2846 | }; |
| 2847 | |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 2848 | StaticMockSocket data(data_reads, data_writes); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2849 | session_deps.socket_factory.AddMockSocket(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2850 | |
| 2851 | TestCompletionCallback callback; |
| 2852 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 2853 | int rv = trans->Start(&request, &callback, NULL); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2854 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2855 | |
| 2856 | rv = callback.WaitForResult(); |
| 2857 | EXPECT_EQ(OK, rv); |
| 2858 | } |
| 2859 | |
| 2860 | TEST_F(HttpNetworkTransactionTest, BuildRequest_PutContentLengthZero) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2861 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 2862 | scoped_ptr<HttpTransaction> trans( |
| 2863 | new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2864 | CreateSession(&session_deps), |
| 2865 | &session_deps.socket_factory)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2866 | |
| 2867 | HttpRequestInfo request; |
| 2868 | request.method = "PUT"; |
| 2869 | request.url = GURL("http://www.google.com/"); |
| 2870 | |
| 2871 | MockWrite data_writes[] = { |
| 2872 | MockWrite("PUT / HTTP/1.1\r\n" |
| 2873 | "Host: www.google.com\r\n" |
| 2874 | "Connection: keep-alive\r\n" |
| 2875 | "Content-Length: 0\r\n\r\n"), |
| 2876 | }; |
| 2877 | |
| 2878 | // Lastly, the server responds with the actual content. |
| 2879 | MockRead data_reads[] = { |
| 2880 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 2881 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2882 | MockRead("Content-Length: 100\r\n\r\n"), |
| 2883 | MockRead(false, OK), |
| 2884 | }; |
| 2885 | |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 2886 | StaticMockSocket data(data_reads, data_writes); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2887 | session_deps.socket_factory.AddMockSocket(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2888 | |
| 2889 | TestCompletionCallback callback; |
| 2890 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 2891 | int rv = trans->Start(&request, &callback, NULL); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2892 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2893 | |
| 2894 | rv = callback.WaitForResult(); |
| 2895 | EXPECT_EQ(OK, rv); |
| 2896 | } |
| 2897 | |
| 2898 | TEST_F(HttpNetworkTransactionTest, BuildRequest_HeadContentLengthZero) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2899 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 2900 | scoped_ptr<HttpTransaction> trans( |
| 2901 | new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2902 | CreateSession(&session_deps), |
| 2903 | &session_deps.socket_factory)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2904 | |
| 2905 | HttpRequestInfo request; |
| 2906 | request.method = "HEAD"; |
| 2907 | request.url = GURL("http://www.google.com/"); |
| 2908 | |
| 2909 | MockWrite data_writes[] = { |
| 2910 | MockWrite("HEAD / HTTP/1.1\r\n" |
| 2911 | "Host: www.google.com\r\n" |
| 2912 | "Connection: keep-alive\r\n" |
| 2913 | "Content-Length: 0\r\n\r\n"), |
| 2914 | }; |
| 2915 | |
| 2916 | // Lastly, the server responds with the actual content. |
| 2917 | MockRead data_reads[] = { |
| 2918 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 2919 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2920 | MockRead("Content-Length: 100\r\n\r\n"), |
| 2921 | MockRead(false, OK), |
| 2922 | }; |
| 2923 | |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 2924 | StaticMockSocket data(data_reads, data_writes); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2925 | session_deps.socket_factory.AddMockSocket(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2926 | |
| 2927 | TestCompletionCallback callback; |
| 2928 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 2929 | int rv = trans->Start(&request, &callback, NULL); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2930 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2931 | |
| 2932 | rv = callback.WaitForResult(); |
| 2933 | EXPECT_EQ(OK, rv); |
| 2934 | } |
| 2935 | |
| 2936 | TEST_F(HttpNetworkTransactionTest, BuildRequest_CacheControlNoCache) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2937 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 2938 | scoped_ptr<HttpTransaction> trans( |
| 2939 | new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2940 | CreateSession(&session_deps), |
| 2941 | &session_deps.socket_factory)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2942 | |
| 2943 | HttpRequestInfo request; |
| 2944 | request.method = "GET"; |
| 2945 | request.url = GURL("http://www.google.com/"); |
| 2946 | request.load_flags = LOAD_BYPASS_CACHE; |
| 2947 | |
| 2948 | MockWrite data_writes[] = { |
| 2949 | MockWrite("GET / HTTP/1.1\r\n" |
| 2950 | "Host: www.google.com\r\n" |
| 2951 | "Connection: keep-alive\r\n" |
| 2952 | "Pragma: no-cache\r\n" |
| 2953 | "Cache-Control: no-cache\r\n\r\n"), |
| 2954 | }; |
| 2955 | |
| 2956 | // Lastly, the server responds with the actual content. |
| 2957 | MockRead data_reads[] = { |
| 2958 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 2959 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2960 | MockRead("Content-Length: 100\r\n\r\n"), |
| 2961 | MockRead(false, OK), |
| 2962 | }; |
| 2963 | |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 2964 | StaticMockSocket data(data_reads, data_writes); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2965 | session_deps.socket_factory.AddMockSocket(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2966 | |
| 2967 | TestCompletionCallback callback; |
| 2968 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 2969 | int rv = trans->Start(&request, &callback, NULL); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2970 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2971 | |
| 2972 | rv = callback.WaitForResult(); |
| 2973 | EXPECT_EQ(OK, rv); |
| 2974 | } |
| 2975 | |
| 2976 | TEST_F(HttpNetworkTransactionTest, |
| 2977 | BuildRequest_CacheControlValidateCache) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2978 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 2979 | scoped_ptr<HttpTransaction> trans( |
| 2980 | new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2981 | CreateSession(&session_deps), |
| 2982 | &session_deps.socket_factory)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2983 | |
| 2984 | HttpRequestInfo request; |
| 2985 | request.method = "GET"; |
| 2986 | request.url = GURL("http://www.google.com/"); |
| 2987 | request.load_flags = LOAD_VALIDATE_CACHE; |
| 2988 | |
| 2989 | MockWrite data_writes[] = { |
| 2990 | MockWrite("GET / HTTP/1.1\r\n" |
| 2991 | "Host: www.google.com\r\n" |
| 2992 | "Connection: keep-alive\r\n" |
| 2993 | "Cache-Control: max-age=0\r\n\r\n"), |
| 2994 | }; |
| 2995 | |
| 2996 | // Lastly, the server responds with the actual content. |
| 2997 | MockRead data_reads[] = { |
| 2998 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 2999 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 3000 | MockRead("Content-Length: 100\r\n\r\n"), |
| 3001 | MockRead(false, OK), |
| 3002 | }; |
| 3003 | |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 3004 | StaticMockSocket data(data_reads, data_writes); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 3005 | session_deps.socket_factory.AddMockSocket(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3006 | |
| 3007 | TestCompletionCallback callback; |
| 3008 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 3009 | int rv = trans->Start(&request, &callback, NULL); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3010 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3011 | |
| 3012 | rv = callback.WaitForResult(); |
| 3013 | EXPECT_EQ(OK, rv); |
| 3014 | } |
| 3015 | |
| 3016 | TEST_F(HttpNetworkTransactionTest, BuildRequest_ExtraHeaders) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 3017 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 3018 | scoped_ptr<HttpTransaction> trans( |
| 3019 | new HttpNetworkTransaction( |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 3020 | CreateSession(&session_deps), |
| 3021 | &session_deps.socket_factory)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3022 | |
| 3023 | HttpRequestInfo request; |
| 3024 | request.method = "GET"; |
| 3025 | request.url = GURL("http://www.google.com/"); |
| 3026 | request.extra_headers = "FooHeader: Bar\r\n"; |
| 3027 | |
| 3028 | MockWrite data_writes[] = { |
| 3029 | MockWrite("GET / HTTP/1.1\r\n" |
| 3030 | "Host: www.google.com\r\n" |
| 3031 | "Connection: keep-alive\r\n" |
| 3032 | "FooHeader: Bar\r\n\r\n"), |
| 3033 | }; |
| 3034 | |
| 3035 | // Lastly, the server responds with the actual content. |
| 3036 | MockRead data_reads[] = { |
| 3037 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3038 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 3039 | MockRead("Content-Length: 100\r\n\r\n"), |
| 3040 | MockRead(false, OK), |
| 3041 | }; |
| 3042 | |
[email protected] | 69b43fe | 2009-06-15 09:47:37 | [diff] [blame] | 3043 | StaticMockSocket data(data_reads, data_writes); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 3044 | session_deps.socket_factory.AddMockSocket(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3045 | |
| 3046 | TestCompletionCallback callback; |
| 3047 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 3048 | int rv = trans->Start(&request, &callback, NULL); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3049 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3050 | |
| 3051 | rv = callback.WaitForResult(); |
| 3052 | EXPECT_EQ(OK, rv); |
| 3053 | } |
| 3054 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 3055 | TEST_F(HttpNetworkTransactionTest, SOCKS4_HTTP_GET) { |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 3056 | SessionDependencies session_deps( |
| 3057 | CreateFixedProxyService("socks4://myproxy:1080")); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 3058 | |
| 3059 | scoped_ptr<HttpTransaction> trans( |
| 3060 | new HttpNetworkTransaction( |
| 3061 | CreateSession(&session_deps), |
| 3062 | &session_deps.socket_factory)); |
| 3063 | |
| 3064 | HttpRequestInfo request; |
| 3065 | request.method = "GET"; |
| 3066 | request.url = GURL("http://www.google.com/"); |
| 3067 | request.load_flags = 0; |
| 3068 | |
| 3069 | char write_buffer[] = { 0x04, 0x01, 0x00, 0x50, 127, 0, 0, 1, 0 }; |
| 3070 | char read_buffer[] = { 0x00, 0x5A, 0x00, 0x00, 0, 0, 0, 0 }; |
| 3071 | |
| 3072 | MockWrite data_writes[] = { |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 3073 | MockWrite(true, write_buffer, arraysize(write_buffer)), |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 3074 | MockWrite("GET / HTTP/1.1\r\n" |
| 3075 | "Host: www.google.com\r\n" |
| 3076 | "Connection: keep-alive\r\n\r\n") |
| 3077 | }; |
| 3078 | |
| 3079 | MockRead data_reads[] = { |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 3080 | MockWrite(true, read_buffer, arraysize(read_buffer)), |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 3081 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3082 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), |
| 3083 | MockRead("Payload"), |
| 3084 | MockRead(false, OK) |
| 3085 | }; |
| 3086 | |
| 3087 | StaticMockSocket data(data_reads, data_writes); |
| 3088 | session_deps.socket_factory.AddMockSocket(&data); |
| 3089 | |
| 3090 | TestCompletionCallback callback; |
| 3091 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 3092 | int rv = trans->Start(&request, &callback, NULL); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 3093 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3094 | |
| 3095 | rv = callback.WaitForResult(); |
| 3096 | EXPECT_EQ(OK, rv); |
| 3097 | |
| 3098 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 3099 | EXPECT_FALSE(response == NULL); |
| 3100 | |
| 3101 | std::string response_text; |
| 3102 | rv = ReadTransaction(trans.get(), &response_text); |
| 3103 | EXPECT_EQ(OK, rv); |
| 3104 | EXPECT_EQ("Payload", response_text); |
| 3105 | } |
| 3106 | |
| 3107 | TEST_F(HttpNetworkTransactionTest, SOCKS4_SSL_GET) { |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 3108 | SessionDependencies session_deps( |
| 3109 | CreateFixedProxyService("socks4://myproxy:1080")); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 3110 | |
| 3111 | scoped_ptr<HttpTransaction> trans( |
| 3112 | new HttpNetworkTransaction( |
| 3113 | CreateSession(&session_deps), |
| 3114 | &session_deps.socket_factory)); |
| 3115 | |
| 3116 | HttpRequestInfo request; |
| 3117 | request.method = "GET"; |
| 3118 | request.url = GURL("https://www.google.com/"); |
| 3119 | request.load_flags = 0; |
| 3120 | |
| 3121 | unsigned char write_buffer[] = { 0x04, 0x01, 0x01, 0xBB, 127, 0, 0, 1, 0 }; |
| 3122 | unsigned char read_buffer[] = { 0x00, 0x5A, 0x00, 0x00, 0, 0, 0, 0 }; |
| 3123 | |
| 3124 | MockWrite data_writes[] = { |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 3125 | MockWrite(true, reinterpret_cast<char*>(write_buffer), |
| 3126 | arraysize(write_buffer)), |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 3127 | MockWrite("GET / HTTP/1.1\r\n" |
| 3128 | "Host: www.google.com\r\n" |
| 3129 | "Connection: keep-alive\r\n\r\n") |
| 3130 | }; |
| 3131 | |
| 3132 | MockRead data_reads[] = { |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 3133 | MockWrite(true, reinterpret_cast<char*>(read_buffer), |
| 3134 | arraysize(read_buffer)), |
| 3135 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3136 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), |
| 3137 | MockRead("Payload"), |
| 3138 | MockRead(false, OK) |
| 3139 | }; |
| 3140 | |
| 3141 | StaticMockSocket data(data_reads, data_writes); |
| 3142 | session_deps.socket_factory.AddMockSocket(&data); |
| 3143 | |
| 3144 | MockSSLSocket ssl(true, OK); |
| 3145 | session_deps.socket_factory.AddMockSSLSocket(&ssl); |
| 3146 | |
| 3147 | TestCompletionCallback callback; |
| 3148 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 3149 | int rv = trans->Start(&request, &callback, NULL); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 3150 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3151 | |
| 3152 | rv = callback.WaitForResult(); |
| 3153 | EXPECT_EQ(OK, rv); |
| 3154 | |
| 3155 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 3156 | EXPECT_FALSE(response == NULL); |
| 3157 | |
| 3158 | std::string response_text; |
| 3159 | rv = ReadTransaction(trans.get(), &response_text); |
| 3160 | EXPECT_EQ(OK, rv); |
| 3161 | EXPECT_EQ("Payload", response_text); |
| 3162 | } |
| 3163 | |
| 3164 | TEST_F(HttpNetworkTransactionTest, SOCKS5_HTTP_GET) { |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 3165 | SessionDependencies session_deps( |
| 3166 | CreateFixedProxyService("socks5://myproxy:1080")); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 3167 | |
| 3168 | scoped_ptr<HttpTransaction> trans( |
| 3169 | new HttpNetworkTransaction( |
| 3170 | CreateSession(&session_deps), |
| 3171 | &session_deps.socket_factory)); |
| 3172 | |
| 3173 | HttpRequestInfo request; |
| 3174 | request.method = "GET"; |
| 3175 | request.url = GURL("http://www.google.com/"); |
| 3176 | request.load_flags = 0; |
| 3177 | |
| 3178 | const char kSOCKS5GreetRequest[] = { 0x05, 0x01, 0x00 }; |
| 3179 | const char kSOCKS5GreetResponse[] = { 0x05, 0x00 }; |
| 3180 | const char kSOCKS5OkRequest[] = |
| 3181 | { 0x05, 0x01, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; |
| 3182 | const char kSOCKS5OkResponse[] = |
| 3183 | { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; |
| 3184 | |
| 3185 | MockWrite data_writes[] = { |
| 3186 | MockWrite(true, kSOCKS5GreetRequest, arraysize(kSOCKS5GreetRequest)), |
| 3187 | MockWrite(true, kSOCKS5OkRequest, arraysize(kSOCKS5OkRequest)), |
| 3188 | MockWrite("GET / HTTP/1.1\r\n" |
| 3189 | "Host: www.google.com\r\n" |
| 3190 | "Connection: keep-alive\r\n\r\n") |
| 3191 | }; |
| 3192 | |
| 3193 | MockRead data_reads[] = { |
| 3194 | MockWrite(true, kSOCKS5GreetResponse, arraysize(kSOCKS5GreetResponse)), |
| 3195 | MockWrite(true, kSOCKS5OkResponse, arraysize(kSOCKS5OkResponse)), |
| 3196 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3197 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), |
| 3198 | MockRead("Payload"), |
| 3199 | MockRead(false, OK) |
| 3200 | }; |
| 3201 | |
| 3202 | StaticMockSocket data(data_reads, data_writes); |
| 3203 | session_deps.socket_factory.AddMockSocket(&data); |
| 3204 | |
| 3205 | TestCompletionCallback callback; |
| 3206 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 3207 | int rv = trans->Start(&request, &callback, NULL); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 3208 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3209 | |
| 3210 | rv = callback.WaitForResult(); |
| 3211 | EXPECT_EQ(OK, rv); |
| 3212 | |
| 3213 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 3214 | EXPECT_FALSE(response == NULL); |
| 3215 | |
| 3216 | std::string response_text; |
| 3217 | rv = ReadTransaction(trans.get(), &response_text); |
| 3218 | EXPECT_EQ(OK, rv); |
| 3219 | EXPECT_EQ("Payload", response_text); |
| 3220 | } |
| 3221 | |
| 3222 | TEST_F(HttpNetworkTransactionTest, SOCKS5_SSL_GET) { |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 3223 | SessionDependencies session_deps( |
| 3224 | CreateFixedProxyService("socks5://myproxy:1080")); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 3225 | |
| 3226 | scoped_ptr<HttpTransaction> trans( |
| 3227 | new HttpNetworkTransaction( |
| 3228 | CreateSession(&session_deps), |
| 3229 | &session_deps.socket_factory)); |
| 3230 | |
| 3231 | HttpRequestInfo request; |
| 3232 | request.method = "GET"; |
| 3233 | request.url = GURL("https://www.google.com/"); |
| 3234 | request.load_flags = 0; |
| 3235 | |
| 3236 | const char kSOCKS5GreetRequest[] = { 0x05, 0x01, 0x00 }; |
| 3237 | const char kSOCKS5GreetResponse[] = { 0x05, 0x00 }; |
| 3238 | const unsigned char kSOCKS5OkRequest[] = |
| 3239 | { 0x05, 0x01, 0x00, 0x01, 127, 0, 0, 1, 0x01, 0xBB }; |
| 3240 | const char kSOCKS5OkResponse[] = |
| 3241 | { 0x05, 0x00, 0x00, 0x01, 0, 0, 0, 0, 0x00, 0x00 }; |
| 3242 | |
| 3243 | MockWrite data_writes[] = { |
| 3244 | MockWrite(true, kSOCKS5GreetRequest, arraysize(kSOCKS5GreetRequest)), |
| 3245 | MockWrite(true, reinterpret_cast<const char*>(kSOCKS5OkRequest), |
| 3246 | arraysize(kSOCKS5OkRequest)), |
| 3247 | MockWrite("GET / HTTP/1.1\r\n" |
| 3248 | "Host: www.google.com\r\n" |
| 3249 | "Connection: keep-alive\r\n\r\n") |
| 3250 | }; |
| 3251 | |
| 3252 | MockRead data_reads[] = { |
| 3253 | MockWrite(true, kSOCKS5GreetResponse, arraysize(kSOCKS5GreetResponse)), |
| 3254 | MockWrite(true, kSOCKS5OkResponse, arraysize(kSOCKS5OkResponse)), |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 3255 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3256 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), |
| 3257 | MockRead("Payload"), |
| 3258 | MockRead(false, OK) |
| 3259 | }; |
| 3260 | |
| 3261 | StaticMockSocket data(data_reads, data_writes); |
| 3262 | session_deps.socket_factory.AddMockSocket(&data); |
| 3263 | |
| 3264 | MockSSLSocket ssl(true, OK); |
| 3265 | session_deps.socket_factory.AddMockSSLSocket(&ssl); |
| 3266 | |
| 3267 | TestCompletionCallback callback; |
| 3268 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 3269 | int rv = trans->Start(&request, &callback, NULL); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 3270 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3271 | |
| 3272 | rv = callback.WaitForResult(); |
| 3273 | EXPECT_EQ(OK, rv); |
| 3274 | |
| 3275 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 3276 | EXPECT_FALSE(response == NULL); |
| 3277 | |
| 3278 | std::string response_text; |
| 3279 | rv = ReadTransaction(trans.get(), &response_text); |
| 3280 | EXPECT_EQ(OK, rv); |
| 3281 | EXPECT_EQ("Payload", response_text); |
| 3282 | } |
| 3283 | |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 3284 | // Tests that for connection endpoints the group names are correctly set. |
| 3285 | TEST_F(HttpNetworkTransactionTest, GroupNameForProxyConnections) { |
| 3286 | const struct { |
| 3287 | const std::string proxy_server; |
| 3288 | const std::string url; |
| 3289 | const std::string expected_group_name; |
| 3290 | } tests[] = { |
| 3291 | { |
| 3292 | "", // no proxy (direct) |
| 3293 | "http://www.google.com/direct", |
| 3294 | "http://www.google.com/", |
| 3295 | }, |
| 3296 | { |
| 3297 | "http_proxy", |
| 3298 | "http://www.google.com/http_proxy_normal", |
| 3299 | "proxy/http_proxy:80/", |
| 3300 | }, |
| 3301 | { |
| 3302 | "socks4://socks_proxy:1080", |
| 3303 | "http://www.google.com/socks4_direct", |
| 3304 | "proxy/socks4://socks_proxy:1080/http://www.google.com/", |
| 3305 | }, |
| 3306 | |
| 3307 | // SSL Tests |
| 3308 | { |
| 3309 | "", |
| 3310 | "https://www.google.com/direct_ssl", |
| 3311 | "https://www.google.com/", |
| 3312 | }, |
| 3313 | { |
| 3314 | "http_proxy", |
| 3315 | "https://www.google.com/http_connect_ssl", |
| 3316 | "proxy/http_proxy:80/https://www.google.com/", |
| 3317 | }, |
| 3318 | { |
| 3319 | "socks4://socks_proxy:1080", |
| 3320 | "https://www.google.com/socks4_ssl", |
| 3321 | "proxy/socks4://socks_proxy:1080/https://www.google.com/", |
| 3322 | }, |
| 3323 | }; |
| 3324 | |
| 3325 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 3326 | SessionDependencies session_deps( |
| 3327 | CreateFixedProxyService(tests[i].proxy_server)); |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 3328 | |
| 3329 | scoped_refptr<CaptureGroupNameSocketPool> conn_pool( |
| 3330 | new CaptureGroupNameSocketPool()); |
| 3331 | |
| 3332 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 3333 | session->connection_pool_ = conn_pool.get(); |
| 3334 | |
| 3335 | scoped_ptr<HttpTransaction> trans( |
| 3336 | new HttpNetworkTransaction( |
| 3337 | session.get(), |
| 3338 | &session_deps.socket_factory)); |
| 3339 | |
| 3340 | HttpRequestInfo request; |
| 3341 | request.method = "GET"; |
| 3342 | request.url = GURL(tests[i].url); |
| 3343 | request.load_flags = 0; |
| 3344 | |
| 3345 | TestCompletionCallback callback; |
| 3346 | |
| 3347 | // We do not complete this request, the dtor will clean the transaction up. |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 3348 | EXPECT_EQ(ERR_IO_PENDING, trans->Start(&request, &callback, NULL)); |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 3349 | EXPECT_EQ(tests[i].expected_group_name, |
| 3350 | conn_pool->last_group_name_received()); |
| 3351 | } |
| 3352 | } |
| 3353 | |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 3354 | TEST_F(HttpNetworkTransactionTest, ReconsiderProxyAfterFailedConnection) { |
[email protected] | 5c6a17e | 2009-06-10 00:54:54 | [diff] [blame] | 3355 | SessionDependencies session_deps( |
| 3356 | CreateFixedProxyService("myproxy:70;foobar:80")); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 3357 | |
| 3358 | session_deps.host_resolver->rules()->AddSimulatedFailure("*"); |
| 3359 | |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 3360 | scoped_ptr<HttpTransaction> trans( |
| 3361 | new HttpNetworkTransaction( |
| 3362 | CreateSession(&session_deps), |
| 3363 | &session_deps.socket_factory)); |
| 3364 | |
| 3365 | HttpRequestInfo request; |
| 3366 | request.method = "GET"; |
| 3367 | request.url = GURL("http://www.google.com/"); |
| 3368 | |
| 3369 | TestCompletionCallback callback; |
| 3370 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 3371 | int rv = trans->Start(&request, &callback, NULL); |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 3372 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3373 | |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 3374 | rv = callback.WaitForResult(); |
| 3375 | EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); |
| 3376 | } |
| 3377 | |
[email protected] | f3e6c1e | 2009-06-15 20:52:12 | [diff] [blame] | 3378 | // Host resolution observer used by |
| 3379 | // HttpNetworkTransactionTest.ResolveMadeWithReferrer to check that host |
| 3380 | // resovle requests are issued with a referrer of |expected_referrer|. |
| 3381 | class ResolutionReferrerObserver : public HostResolver::Observer { |
| 3382 | public: |
| 3383 | explicit ResolutionReferrerObserver(const GURL& expected_referrer) |
| 3384 | : expected_referrer_(expected_referrer), |
| 3385 | called_start_with_referrer_(false), |
| 3386 | called_finish_with_referrer_(false) { |
| 3387 | } |
| 3388 | |
| 3389 | virtual void OnStartResolution(int id, |
| 3390 | const HostResolver::RequestInfo& info) { |
| 3391 | if (info.referrer() == expected_referrer_) |
| 3392 | called_start_with_referrer_ = true; |
| 3393 | } |
| 3394 | |
| 3395 | virtual void OnFinishResolutionWithStatus( |
| 3396 | int id, bool was_resolved, const HostResolver::RequestInfo& info ) { |
| 3397 | if (info.referrer() == expected_referrer_) |
| 3398 | called_finish_with_referrer_ = true; |
| 3399 | } |
| 3400 | |
[email protected] | eb255d3 | 2009-06-17 02:11:03 | [diff] [blame] | 3401 | virtual void OnCancelResolution(int id, |
| 3402 | const HostResolver::RequestInfo& info ) { |
| 3403 | FAIL() << "Should not be cancelling any requests!"; |
| 3404 | } |
| 3405 | |
[email protected] | f3e6c1e | 2009-06-15 20:52:12 | [diff] [blame] | 3406 | bool did_complete_with_expected_referrer() const { |
| 3407 | return called_start_with_referrer_ && called_finish_with_referrer_; |
| 3408 | } |
| 3409 | |
| 3410 | private: |
| 3411 | GURL expected_referrer_; |
| 3412 | bool called_start_with_referrer_; |
| 3413 | bool called_finish_with_referrer_; |
| 3414 | |
| 3415 | DISALLOW_COPY_AND_ASSIGN(ResolutionReferrerObserver); |
| 3416 | }; |
| 3417 | |
| 3418 | // Make sure that when HostResolver::Resolve() is invoked, it passes through |
| 3419 | // the "referrer". This is depended on by the DNS prefetch observer. |
| 3420 | TEST_F(HttpNetworkTransactionTest, ResolveMadeWithReferrer) { |
| 3421 | GURL referrer = GURL("http://expected-referrer/"); |
| 3422 | EXPECT_TRUE(referrer.is_valid()); |
| 3423 | ResolutionReferrerObserver resolution_observer(referrer); |
| 3424 | |
| 3425 | SessionDependencies session_deps; |
| 3426 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction( |
| 3427 | CreateSession(&session_deps), &session_deps.socket_factory)); |
| 3428 | |
| 3429 | // Attach an observer to watch the host resolutions being made. |
[email protected] | 94a0d3d9 | 2009-06-27 01:50:14 | [diff] [blame] | 3430 | session_deps.host_resolver->AddObserver(&resolution_observer); |
[email protected] | f3e6c1e | 2009-06-15 20:52:12 | [diff] [blame] | 3431 | |
| 3432 | // Connect up a mock socket which will fail when reading. |
| 3433 | MockRead data_reads[] = { |
| 3434 | MockRead(false, ERR_FAILED), |
| 3435 | }; |
| 3436 | StaticMockSocket data(data_reads, NULL); |
| 3437 | session_deps.socket_factory.AddMockSocket(&data); |
| 3438 | |
| 3439 | // Issue a request, containing an HTTP referrer. |
| 3440 | HttpRequestInfo request; |
| 3441 | request.method = "GET"; |
| 3442 | request.referrer = referrer; |
| 3443 | request.url = GURL("http://www.google.com/"); |
| 3444 | |
| 3445 | // Run the request until it fails reading from the socket. |
| 3446 | TestCompletionCallback callback; |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 3447 | int rv = trans->Start(&request, &callback, NULL); |
[email protected] | f3e6c1e | 2009-06-15 20:52:12 | [diff] [blame] | 3448 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3449 | rv = callback.WaitForResult(); |
| 3450 | EXPECT_EQ(ERR_FAILED, rv); |
| 3451 | |
| 3452 | // Check that the host resolution observer saw |referrer|. |
| 3453 | EXPECT_TRUE(resolution_observer.did_complete_with_expected_referrer()); |
| 3454 | } |
| 3455 | |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 3456 | // Make sure that when the load flags contain LOAD_BYPASS_CACHE, the resolver's |
| 3457 | // host cache is bypassed. |
| 3458 | TEST_F(HttpNetworkTransactionTest, BypassHostCacheOnRefresh) { |
| 3459 | SessionDependencies session_deps; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 3460 | |
[email protected] | a2c2fb9 | 2009-07-18 07:31:04 | [diff] [blame] | 3461 | // Select a host resolver that does caching. |
| 3462 | session_deps.host_resolver = new MockCachingHostResolver; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 3463 | |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 3464 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction( |
| 3465 | CreateSession(&session_deps), &session_deps.socket_factory)); |
| 3466 | |
| 3467 | // Warm up the host cache so it has an entry for "www.google.com" (by doing |
| 3468 | // a synchronous lookup.) |
| 3469 | AddressList addrlist; |
[email protected] | 94a0d3d9 | 2009-06-27 01:50:14 | [diff] [blame] | 3470 | int rv = session_deps.host_resolver->Resolve( |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 3471 | HostResolver::RequestInfo("www.google.com", 80), &addrlist, |
| 3472 | NULL, NULL, NULL); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 3473 | EXPECT_EQ(OK, rv); |
| 3474 | |
| 3475 | // Verify that it was added to host cache, by doing a subsequent async lookup |
| 3476 | // and confirming it completes synchronously. |
| 3477 | TestCompletionCallback resolve_callback; |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 3478 | rv = session_deps.host_resolver->Resolve( |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 3479 | HostResolver::RequestInfo("www.google.com", 80), &addrlist, |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 3480 | &resolve_callback, NULL, NULL); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 3481 | ASSERT_EQ(OK, rv); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 3482 | |
| 3483 | // Inject a failure the next time that "www.google.com" is resolved. This way |
| 3484 | // we can tell if the next lookup hit the cache, or the "network". |
| 3485 | // (cache --> success, "network" --> failure). |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 3486 | session_deps.host_resolver->rules()->AddSimulatedFailure("www.google.com"); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 3487 | |
| 3488 | // Connect up a mock socket which will fail with ERR_UNEXPECTED during the |
| 3489 | // first read -- this won't be reached as the host resolution will fail first. |
| 3490 | MockRead data_reads[] = { MockRead(false, ERR_UNEXPECTED) }; |
| 3491 | StaticMockSocket data(data_reads, NULL); |
| 3492 | session_deps.socket_factory.AddMockSocket(&data); |
| 3493 | |
| 3494 | // Issue a request, asking to bypass the cache(s). |
| 3495 | HttpRequestInfo request; |
| 3496 | request.method = "GET"; |
| 3497 | request.load_flags = LOAD_BYPASS_CACHE; |
| 3498 | request.url = GURL("http://www.google.com/"); |
| 3499 | |
| 3500 | // Run the request. |
| 3501 | TestCompletionCallback callback; |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 3502 | rv = trans->Start(&request, &callback, NULL); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 3503 | ASSERT_EQ(ERR_IO_PENDING, rv); |
| 3504 | rv = callback.WaitForResult(); |
| 3505 | |
| 3506 | // If we bypassed the cache, we would have gotten a failure while resolving |
| 3507 | // "www.google.com". |
| 3508 | EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); |
| 3509 | } |
| 3510 | |
[email protected] | a97cca4 | 2009-08-14 01:00:29 | [diff] [blame] | 3511 | TEST_F(HttpNetworkTransactionTest, GetIdentifyFromUrl) { |
| 3512 | struct { |
| 3513 | const char* input_url; |
| 3514 | const wchar_t* expected_username; |
| 3515 | const wchar_t* expected_password; |
| 3516 | } tests[] = { |
| 3517 | { |
| 3518 | "http://username:[email protected]", |
| 3519 | L"username", |
| 3520 | L"password", |
| 3521 | }, |
| 3522 | { // Test for http://crbug.com/19200 |
| 3523 | "http://username:p@[email protected]", |
| 3524 | L"username", |
| 3525 | L"p@ssword", |
| 3526 | }, |
| 3527 | { // Username contains %20. |
| 3528 | "http://use rname:[email protected]", |
| 3529 | L"use rname", |
| 3530 | L"password", |
| 3531 | }, |
| 3532 | { // The URL canonicalizer for userinfo does not recognize non-ascii |
| 3533 | // escapes it seems... So things like %00 will NOT be unescapable, |
| 3534 | // since they are canonicalized by escaping the %... |
| 3535 | "http://use%00rname:[email protected]", |
| 3536 | L"use%2500rname", |
| 3537 | L"password", |
| 3538 | }, |
| 3539 | { // Use a '+' in the username. |
| 3540 | "http://use+rname:[email protected]", |
| 3541 | L"use+rname", |
| 3542 | L"password", |
| 3543 | }, |
| 3544 | { // Use a '&' in the password. |
| 3545 | "http://username:p&[email protected]", |
| 3546 | L"username", |
| 3547 | L"p&ssword", |
| 3548 | }, |
| 3549 | }; |
| 3550 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 3551 | SCOPED_TRACE(StringPrintf("Test[%d]: %s", i, tests[i].input_url)); |
| 3552 | GURL url(tests[i].input_url); |
| 3553 | |
| 3554 | std::wstring username, password; |
| 3555 | HttpNetworkTransaction::GetIdentifyFromUrl(url, &username, &password); |
| 3556 | |
| 3557 | EXPECT_EQ(tests[i].expected_username, username); |
| 3558 | EXPECT_EQ(tests[i].expected_password, password); |
| 3559 | } |
| 3560 | } |
| 3561 | |
| 3562 | // Try extracting a username which was encoded with UTF8. |
| 3563 | TEST_F(HttpNetworkTransactionTest, GetIdentifyFromUrl_UTF8) { |
| 3564 | GURL url(WideToUTF16(L"http://foo:\x4f60\[email protected]")); |
| 3565 | |
| 3566 | EXPECT_EQ("foo", url.username()); |
| 3567 | EXPECT_EQ("%E4%BD%A0%E5%A5%BD", url.password()); |
| 3568 | |
| 3569 | // Extract the unescaped identity. |
| 3570 | std::wstring username, password; |
| 3571 | HttpNetworkTransaction::GetIdentifyFromUrl(url, &username, &password); |
| 3572 | |
| 3573 | // Verify that it was decoded as UTF8. |
| 3574 | EXPECT_EQ(L"foo", username); |
| 3575 | EXPECT_EQ(L"\x4f60\x597d", password); |
| 3576 | } |
| 3577 | |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 3578 | } // namespace net |