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