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