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