[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] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5 | #include "net/http/http_network_transaction.h" |
| 6 | |
[email protected] | 77848d1 | 2008-11-14 00:00:22 | [diff] [blame] | 7 | #include <math.h> // ceil |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 8 | #include <vector> |
[email protected] | 77848d1 | 2008-11-14 00:00:22 | [diff] [blame] | 9 | |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 10 | #include "base/basictypes.h" |
[email protected] | 68bf915 | 2008-09-25 19:47:30 | [diff] [blame] | 11 | #include "base/compiler_specific.h" |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 12 | #include "base/file_path.h" |
| 13 | #include "base/file_util.h" |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 14 | #include "base/scoped_ptr.h" |
[email protected] | be1ce6a7 | 2010-08-03 14:35:22 | [diff] [blame] | 15 | #include "base/utf_string_conversions.h" |
[email protected] | 277d594 | 2010-08-11 21:02:35 | [diff] [blame] | 16 | #include "net/base/auth.h" |
[email protected] | 169d001 | 2010-05-10 23:20:12 | [diff] [blame] | 17 | #include "net/base/capturing_net_log.h" |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 18 | #include "net/base/completion_callback.h" |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 19 | #include "net/base/mock_host_resolver.h" |
[email protected] | 169d001 | 2010-05-10 23:20:12 | [diff] [blame] | 20 | #include "net/base/net_log.h" |
| 21 | #include "net/base/net_log_unittest.h" |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 22 | #include "net/base/request_priority.h" |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 23 | #include "net/base/ssl_config_service_defaults.h" |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 24 | #include "net/base/ssl_info.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 25 | #include "net/base/test_completion_callback.h" |
| 26 | #include "net/base/upload_data.h" |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 27 | #include "net/http/http_auth_handler_digest.h" |
[email protected] | 3fd9dae | 2010-06-21 11:39:00 | [diff] [blame] | 28 | #include "net/http/http_auth_handler_mock.h" |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 29 | #include "net/http/http_auth_handler_ntlm.h" |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 30 | #include "net/http/http_basic_stream.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 31 | #include "net/http/http_network_session.h" |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 32 | #include "net/http/http_stream.h" |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 33 | #include "net/http/http_stream_factory.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 34 | #include "net/http/http_transaction_unittest.h" |
[email protected] | 51fff29d | 2008-12-19 22:17:53 | [diff] [blame] | 35 | #include "net/proxy/proxy_config_service_fixed.h" |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 36 | #include "net/proxy/proxy_resolver.h" |
| 37 | #include "net/proxy/proxy_service.h" |
[email protected] | f7984fc6 | 2009-06-22 23:26:44 | [diff] [blame] | 38 | #include "net/socket/client_socket_factory.h" |
| 39 | #include "net/socket/socket_test_util.h" |
| 40 | #include "net/socket/ssl_client_socket.h" |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 41 | #include "net/spdy/spdy_framer.h" |
| 42 | #include "net/spdy/spdy_session.h" |
| 43 | #include "net/spdy/spdy_session_pool.h" |
| 44 | #include "net/spdy/spdy_test_util.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 45 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 46 | #include "testing/platform_test.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 47 | |
| 48 | //----------------------------------------------------------------------------- |
| 49 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 50 | namespace { |
| 51 | |
| 52 | const string16 kBar(ASCIIToUTF16("bar")); |
| 53 | const string16 kBar2(ASCIIToUTF16("bar2")); |
| 54 | const string16 kBar3(ASCIIToUTF16("bar3")); |
| 55 | const string16 kBaz(ASCIIToUTF16("baz")); |
| 56 | const string16 kFirst(ASCIIToUTF16("first")); |
| 57 | const string16 kFoo(ASCIIToUTF16("foo")); |
| 58 | const string16 kFoo2(ASCIIToUTF16("foo2")); |
| 59 | const string16 kFoo3(ASCIIToUTF16("foo3")); |
| 60 | const string16 kFou(ASCIIToUTF16("fou")); |
| 61 | const string16 kSecond(ASCIIToUTF16("second")); |
| 62 | const string16 kTestingNTLM(ASCIIToUTF16("testing-ntlm")); |
| 63 | const string16 kWrongPassword(ASCIIToUTF16("wrongpassword")); |
| 64 | |
| 65 | } // namespace |
| 66 | |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 67 | namespace net { |
| 68 | |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 69 | class HttpNetworkSessionPeer { |
| 70 | public: |
| 71 | explicit HttpNetworkSessionPeer( |
| 72 | const scoped_refptr<HttpNetworkSession>& session) |
| 73 | : session_(session) {} |
| 74 | |
| 75 | void SetTCPSocketPool(const scoped_refptr<TCPClientSocketPool>& pool) { |
| 76 | session_->tcp_socket_pool_ = pool; |
| 77 | } |
| 78 | |
| 79 | void SetSocketPoolForSOCKSProxy( |
| 80 | const HostPortPair& socks_proxy, |
| 81 | const scoped_refptr<SOCKSClientSocketPool>& pool) { |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 82 | session_->socks_socket_pools_[socks_proxy] = pool; |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void SetSocketPoolForHTTPProxy( |
| 86 | const HostPortPair& http_proxy, |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 87 | const scoped_refptr<HttpProxyClientSocketPool>& pool) { |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 88 | session_->http_proxy_socket_pools_[http_proxy] = pool; |
| 89 | } |
| 90 | |
| 91 | void SetSSLSocketPool(const scoped_refptr<SSLClientSocketPool>& pool) { |
| 92 | session_->ssl_socket_pool_ = pool; |
| 93 | } |
| 94 | |
| 95 | void SetSocketPoolForSSLWithProxy( |
| 96 | const HostPortPair& proxy_host, |
| 97 | const scoped_refptr<SSLClientSocketPool>& pool) { |
| 98 | session_->ssl_socket_pools_for_proxies_[proxy_host] = pool; |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | private: |
| 102 | const scoped_refptr<HttpNetworkSession> session_; |
| 103 | |
| 104 | DISALLOW_COPY_AND_ASSIGN(HttpNetworkSessionPeer); |
| 105 | }; |
| 106 | |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 107 | // Helper to manage the lifetimes of the dependencies for a |
| 108 | // HttpNetworkTransaction. |
[email protected] | ac03952 | 2010-06-15 16:39:44 | [diff] [blame] | 109 | struct SessionDependencies { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 110 | // Default set of dependencies -- "null" proxy service. |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 111 | SessionDependencies() |
| 112 | : host_resolver(new MockHostResolver), |
[email protected] | dded3e2 | 2010-02-05 04:08:37 | [diff] [blame] | 113 | proxy_service(ProxyService::CreateNull()), |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 114 | ssl_config_service(new SSLConfigServiceDefaults), |
[email protected] | a554a826 | 2010-05-20 00:13:52 | [diff] [blame] | 115 | http_auth_handler_factory(HttpAuthHandlerFactory::CreateDefault()), |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 116 | spdy_session_pool(new SpdySessionPool()), |
| 117 | net_log(NULL) {} |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 118 | |
| 119 | // Custom proxy service dependency. |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 120 | explicit SessionDependencies(ProxyService* proxy_service) |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 121 | : host_resolver(new MockHostResolver), |
| 122 | proxy_service(proxy_service), |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 123 | ssl_config_service(new SSLConfigServiceDefaults), |
[email protected] | a554a826 | 2010-05-20 00:13:52 | [diff] [blame] | 124 | http_auth_handler_factory(HttpAuthHandlerFactory::CreateDefault()), |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 125 | spdy_session_pool(new SpdySessionPool()), |
| 126 | net_log(NULL) {} |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 127 | |
[email protected] | a2c2fb9 | 2009-07-18 07:31:04 | [diff] [blame] | 128 | scoped_refptr<MockHostResolverBase> host_resolver; |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 129 | scoped_refptr<ProxyService> proxy_service; |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 130 | scoped_refptr<SSLConfigService> ssl_config_service; |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 131 | MockClientSocketFactory socket_factory; |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 132 | scoped_ptr<HttpAuthHandlerFactory> http_auth_handler_factory; |
[email protected] | a554a826 | 2010-05-20 00:13:52 | [diff] [blame] | 133 | scoped_refptr<SpdySessionPool> spdy_session_pool; |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 134 | NetLog* net_log; |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 135 | }; |
| 136 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 137 | ProxyService* CreateFixedProxyService(const std::string& proxy) { |
[email protected] | ab501a6a | 2009-05-12 15:07:50 | [diff] [blame] | 138 | net::ProxyConfig proxy_config; |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 139 | proxy_config.proxy_rules().ParseFromString(proxy); |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 140 | return ProxyService::CreateFixed(proxy_config); |
[email protected] | 51fff29d | 2008-12-19 22:17:53 | [diff] [blame] | 141 | } |
| 142 | |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 143 | HttpNetworkSession* CreateSession(SessionDependencies* session_deps) { |
[email protected] | 66761b95 | 2010-06-25 21:30:38 | [diff] [blame] | 144 | return new HttpNetworkSession(session_deps->host_resolver, |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 145 | session_deps->proxy_service, |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 146 | &session_deps->socket_factory, |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 147 | session_deps->ssl_config_service, |
[email protected] | a554a826 | 2010-05-20 00:13:52 | [diff] [blame] | 148 | session_deps->spdy_session_pool, |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 149 | session_deps->http_auth_handler_factory.get(), |
[email protected] | ac03952 | 2010-06-15 16:39:44 | [diff] [blame] | 150 | NULL, |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 151 | session_deps->net_log); |
[email protected] | e8d53619 | 2008-10-17 22:21:14 | [diff] [blame] | 152 | } |
| 153 | |
[email protected] | 89836e2 | 2008-09-25 20:33:42 | [diff] [blame] | 154 | class HttpNetworkTransactionTest : public PlatformTest { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 155 | public: |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 156 | virtual void SetUp() { |
| 157 | spdy::SpdyFramer::set_enable_compression_default(false); |
| 158 | } |
| 159 | |
[email protected] | 0e75a73 | 2008-10-16 20:36:09 | [diff] [blame] | 160 | virtual void TearDown() { |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 161 | spdy::SpdyFramer::set_enable_compression_default(true); |
[email protected] | 0e75a73 | 2008-10-16 20:36:09 | [diff] [blame] | 162 | // Empty the current queue. |
| 163 | MessageLoop::current()->RunAllPending(); |
| 164 | PlatformTest::TearDown(); |
| 165 | } |
| 166 | |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 167 | protected: |
| 168 | void KeepAliveConnectionResendRequestTest(const MockRead& read_failure); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 169 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 170 | struct SimpleGetHelperResult { |
| 171 | int rv; |
| 172 | std::string status_line; |
| 173 | std::string response_data; |
| 174 | }; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 175 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 176 | SimpleGetHelperResult SimpleGetHelper(MockRead data_reads[], |
| 177 | size_t reads_count) { |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 178 | SimpleGetHelperResult out; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 179 | |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 180 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 181 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 182 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 183 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 184 | HttpRequestInfo request; |
| 185 | request.method = "GET"; |
| 186 | request.url = GURL("http://www.google.com/"); |
| 187 | request.load_flags = 0; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 188 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 189 | StaticSocketDataProvider data(data_reads, reads_count, NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 190 | session_deps.socket_factory.AddSocketDataProvider(&data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 191 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 192 | TestCompletionCallback callback; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 193 | |
[email protected] | 169d001 | 2010-05-10 23:20:12 | [diff] [blame] | 194 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 195 | int rv = trans->Start(&request, &callback, log.bound()); |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 196 | EXPECT_EQ(ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 197 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 198 | out.rv = callback.WaitForResult(); |
| 199 | if (out.rv != OK) |
| 200 | return out; |
| 201 | |
| 202 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 203 | EXPECT_TRUE(response != NULL); |
| 204 | |
| 205 | EXPECT_TRUE(response->headers != NULL); |
| 206 | out.status_line = response->headers->GetStatusLine(); |
| 207 | |
| 208 | rv = ReadTransaction(trans.get(), &out.response_data); |
| 209 | EXPECT_EQ(OK, rv); |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 210 | size_t pos = ExpectLogContainsSomewhere( |
[email protected] | 169d001 | 2010-05-10 23:20:12 | [diff] [blame] | 211 | log.entries(), 0, NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_HEADERS, |
| 212 | NetLog::PHASE_NONE); |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 213 | ExpectLogContainsSomewhere( |
| 214 | log.entries(), pos, |
| 215 | NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS, |
| 216 | NetLog::PHASE_NONE); |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 217 | |
[email protected] | aecfbf2 | 2008-10-16 02:02:47 | [diff] [blame] | 218 | return out; |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 219 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 220 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 221 | void ConnectStatusHelperWithExpectedStatus(const MockRead& status, |
| 222 | int expected_status); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 223 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 224 | void ConnectStatusHelper(const MockRead& status); |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 225 | }; |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 226 | |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 227 | // Fill |str| with a long header list that consumes >= |size| bytes. |
| 228 | void FillLargeHeadersString(std::string* str, int size) { |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 229 | const char* row = |
| 230 | "SomeHeaderName: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n"; |
| 231 | const int sizeof_row = strlen(row); |
| 232 | const int num_rows = static_cast<int>( |
| 233 | ceil(static_cast<float>(size) / sizeof_row)); |
| 234 | const int sizeof_data = num_rows * sizeof_row; |
| 235 | DCHECK(sizeof_data >= size); |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 236 | str->reserve(sizeof_data); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 237 | |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 238 | for (int i = 0; i < num_rows; ++i) |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 239 | str->append(row, sizeof_row); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 240 | } |
| 241 | |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 242 | // Alternative functions that eliminate randomness and dependency on the local |
| 243 | // host name so that the generated NTLM messages are reproducible. |
[email protected] | fe2bc6a | 2009-03-23 16:52:20 | [diff] [blame] | 244 | void MockGenerateRandom1(uint8* output, size_t n) { |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 245 | static const uint8 bytes[] = { |
| 246 | 0x55, 0x29, 0x66, 0x26, 0x6b, 0x9c, 0x73, 0x54 |
| 247 | }; |
| 248 | static size_t current_byte = 0; |
| 249 | for (size_t i = 0; i < n; ++i) { |
| 250 | output[i] = bytes[current_byte++]; |
| 251 | current_byte %= arraysize(bytes); |
| 252 | } |
| 253 | } |
| 254 | |
[email protected] | fe2bc6a | 2009-03-23 16:52:20 | [diff] [blame] | 255 | void MockGenerateRandom2(uint8* output, size_t n) { |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 256 | static const uint8 bytes[] = { |
| 257 | 0x96, 0x79, 0x85, 0xe7, 0x49, 0x93, 0x70, 0xa1, |
| 258 | 0x4e, 0xe7, 0x87, 0x45, 0x31, 0x5b, 0xd3, 0x1f |
| 259 | }; |
| 260 | static size_t current_byte = 0; |
| 261 | for (size_t i = 0; i < n; ++i) { |
| 262 | output[i] = bytes[current_byte++]; |
| 263 | current_byte %= arraysize(bytes); |
| 264 | } |
| 265 | } |
| 266 | |
[email protected] | fe2bc6a | 2009-03-23 16:52:20 | [diff] [blame] | 267 | std::string MockGetHostName() { |
| 268 | return "WTC-WIN7"; |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 269 | } |
| 270 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 271 | template<typename ParentPool> |
| 272 | class CaptureGroupNameSocketPool : public ParentPool { |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 273 | public: |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 274 | explicit CaptureGroupNameSocketPool(HttpNetworkSession* session); |
| 275 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 276 | const std::string last_group_name_received() const { |
| 277 | return last_group_name_; |
| 278 | } |
| 279 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 280 | virtual int RequestSocket(const std::string& group_name, |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 281 | const void* socket_params, |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 282 | RequestPriority priority, |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 283 | ClientSocketHandle* handle, |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 284 | CompletionCallback* callback, |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 285 | const BoundNetLog& net_log) { |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 286 | last_group_name_ = group_name; |
| 287 | return ERR_IO_PENDING; |
| 288 | } |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 289 | virtual void CancelRequest(const std::string& group_name, |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 290 | ClientSocketHandle* handle) {} |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 291 | virtual void ReleaseSocket(const std::string& group_name, |
| 292 | ClientSocket* socket) {} |
| 293 | virtual void CloseIdleSockets() {} |
| 294 | virtual HostResolver* GetHostResolver() const { |
| 295 | return NULL; |
| 296 | } |
| 297 | virtual int IdleSocketCount() const { |
| 298 | return 0; |
| 299 | } |
| 300 | virtual int IdleSocketCountInGroup(const std::string& group_name) const { |
| 301 | return 0; |
| 302 | } |
| 303 | virtual LoadState GetLoadState(const std::string& group_name, |
| 304 | const ClientSocketHandle* handle) const { |
| 305 | return LOAD_STATE_IDLE; |
| 306 | } |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 307 | virtual base::TimeDelta ConnectionTimeout() const { |
| 308 | return base::TimeDelta(); |
| 309 | } |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 310 | |
| 311 | private: |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 312 | std::string last_group_name_; |
| 313 | }; |
| 314 | |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 315 | typedef CaptureGroupNameSocketPool<TCPClientSocketPool> |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 316 | CaptureGroupNameTCPSocketPool; |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 317 | typedef CaptureGroupNameSocketPool<HttpProxyClientSocketPool> |
| 318 | CaptureGroupNameHttpProxySocketPool; |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 319 | typedef CaptureGroupNameSocketPool<SOCKSClientSocketPool> |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 320 | CaptureGroupNameSOCKSSocketPool; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 321 | typedef CaptureGroupNameSocketPool<SSLClientSocketPool> |
| 322 | CaptureGroupNameSSLSocketPool; |
| 323 | |
| 324 | template<typename ParentPool> |
| 325 | CaptureGroupNameSocketPool<ParentPool>::CaptureGroupNameSocketPool( |
| 326 | HttpNetworkSession* session) |
| 327 | : ParentPool(0, 0, NULL, session->host_resolver(), NULL, NULL) {} |
| 328 | |
| 329 | template<> |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 330 | CaptureGroupNameHttpProxySocketPool::CaptureGroupNameSocketPool( |
| 331 | HttpNetworkSession* session) |
| 332 | : HttpProxyClientSocketPool(0, 0, NULL, session->host_resolver(), NULL, |
| 333 | NULL, NULL) {} |
| 334 | |
| 335 | template<> |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 336 | CaptureGroupNameSSLSocketPool::CaptureGroupNameSocketPool( |
| 337 | HttpNetworkSession* session) |
| 338 | : SSLClientSocketPool(0, 0, NULL, session->host_resolver(), NULL, NULL, |
| 339 | NULL, NULL, NULL) {} |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 340 | |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 341 | //----------------------------------------------------------------------------- |
| 342 | |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 343 | // This is the expected list of advertised protocols from the browser's NPN |
| 344 | // list. |
| 345 | static const char kExpectedNPNString[] = "\x08http/1.1\x06spdy/2"; |
| 346 | |
| 347 | // This is the expected return from a current server advertising SPDY. |
| 348 | static const char kAlternateProtocolHttpHeader[] = |
| 349 | "Alternate-Protocol: 443:npn-spdy/2\r\n\r\n"; |
| 350 | |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 351 | TEST_F(HttpNetworkTransactionTest, Basic) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 352 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 353 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 354 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | TEST_F(HttpNetworkTransactionTest, SimpleGET) { |
| 358 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 359 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 360 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 361 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 362 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 363 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 364 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 365 | EXPECT_EQ(OK, out.rv); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 366 | EXPECT_EQ("HTTP/1.0 200 OK", out.status_line); |
| 367 | EXPECT_EQ("hello world", out.response_data); |
| 368 | } |
| 369 | |
| 370 | // Response with no status line. |
| 371 | TEST_F(HttpNetworkTransactionTest, SimpleGETNoHeaders) { |
| 372 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 373 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 374 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 375 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 376 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 377 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 378 | EXPECT_EQ(OK, out.rv); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 379 | EXPECT_EQ("HTTP/0.9 200 OK", out.status_line); |
| 380 | EXPECT_EQ("hello world", out.response_data); |
| 381 | } |
| 382 | |
| 383 | // Allow up to 4 bytes of junk to precede status line. |
| 384 | TEST_F(HttpNetworkTransactionTest, StatusLineJunk2Bytes) { |
| 385 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 386 | MockRead("xxxHTTP/1.0 404 Not Found\nServer: blah\n\nDATA"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 387 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 388 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 389 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 390 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 391 | EXPECT_EQ(OK, out.rv); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 392 | EXPECT_EQ("HTTP/1.0 404 Not Found", out.status_line); |
| 393 | EXPECT_EQ("DATA", out.response_data); |
| 394 | } |
| 395 | |
| 396 | // Allow up to 4 bytes of junk to precede status line. |
| 397 | TEST_F(HttpNetworkTransactionTest, StatusLineJunk4Bytes) { |
| 398 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 399 | MockRead("\n\nQJHTTP/1.0 404 Not Found\nServer: blah\n\nDATA"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 400 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 401 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 402 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 403 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 404 | EXPECT_EQ(OK, out.rv); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 405 | EXPECT_EQ("HTTP/1.0 404 Not Found", out.status_line); |
| 406 | EXPECT_EQ("DATA", out.response_data); |
| 407 | } |
| 408 | |
| 409 | // Beyond 4 bytes of slop and it should fail to find a status line. |
| 410 | TEST_F(HttpNetworkTransactionTest, StatusLineJunk5Bytes) { |
| 411 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 412 | MockRead("xxxxxHTTP/1.1 404 Not Found\nServer: blah"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 413 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 414 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 415 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 416 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 417 | EXPECT_EQ(OK, out.rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 418 | EXPECT_EQ("HTTP/0.9 200 OK", out.status_line); |
| 419 | EXPECT_EQ("xxxxxHTTP/1.1 404 Not Found\nServer: blah", out.response_data); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | // Same as StatusLineJunk4Bytes, except the read chunks are smaller. |
| 423 | TEST_F(HttpNetworkTransactionTest, StatusLineJunk4Bytes_Slow) { |
| 424 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 425 | MockRead("\n"), |
| 426 | MockRead("\n"), |
| 427 | MockRead("Q"), |
| 428 | MockRead("J"), |
| 429 | MockRead("HTTP/1.0 404 Not Found\nServer: blah\n\nDATA"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 430 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 431 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 432 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 433 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 434 | EXPECT_EQ(OK, out.rv); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 435 | EXPECT_EQ("HTTP/1.0 404 Not Found", out.status_line); |
| 436 | EXPECT_EQ("DATA", out.response_data); |
| 437 | } |
| 438 | |
| 439 | // Close the connection before enough bytes to have a status line. |
| 440 | TEST_F(HttpNetworkTransactionTest, StatusLinePartial) { |
| 441 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 442 | MockRead("HTT"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 443 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 444 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 445 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 446 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 447 | EXPECT_EQ(OK, out.rv); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 448 | EXPECT_EQ("HTTP/0.9 200 OK", out.status_line); |
| 449 | EXPECT_EQ("HTT", out.response_data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 450 | } |
| 451 | |
[email protected] | f9d44aa | 2008-09-23 23:57:17 | [diff] [blame] | 452 | // Simulate a 204 response, lacking a Content-Length header, sent over a |
| 453 | // persistent connection. The response should still terminate since a 204 |
| 454 | // cannot have a response body. |
| 455 | TEST_F(HttpNetworkTransactionTest, StopsReading204) { |
| 456 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 457 | MockRead("HTTP/1.1 204 No Content\r\n\r\n"), |
| 458 | MockRead("junk"), // Should not be read!! |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 459 | MockRead(false, OK), |
[email protected] | f9d44aa | 2008-09-23 23:57:17 | [diff] [blame] | 460 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 461 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 462 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 463 | EXPECT_EQ(OK, out.rv); |
[email protected] | f9d44aa | 2008-09-23 23:57:17 | [diff] [blame] | 464 | EXPECT_EQ("HTTP/1.1 204 No Content", out.status_line); |
| 465 | EXPECT_EQ("", out.response_data); |
| 466 | } |
| 467 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 468 | // A simple request using chunked encoding with some extra data after. |
| 469 | // (Like might be seen in a pipelined response.) |
| 470 | TEST_F(HttpNetworkTransactionTest, ChunkedEncoding) { |
| 471 | MockRead data_reads[] = { |
| 472 | MockRead("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"), |
| 473 | MockRead("5\r\nHello\r\n"), |
| 474 | MockRead("1\r\n"), |
| 475 | MockRead(" \r\n"), |
| 476 | MockRead("5\r\nworld\r\n"), |
| 477 | MockRead("0\r\n\r\nHTTP/1.1 200 OK\r\n"), |
| 478 | MockRead(false, OK), |
| 479 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 480 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 481 | arraysize(data_reads)); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 482 | EXPECT_EQ(OK, out.rv); |
| 483 | EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); |
| 484 | EXPECT_EQ("Hello world", out.response_data); |
| 485 | } |
| 486 | |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 487 | // Do a request using the HEAD method. Verify that we don't try to read the |
| 488 | // message body (since HEAD has none). |
| 489 | TEST_F(HttpNetworkTransactionTest, Head) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 490 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 491 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 492 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 493 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 494 | HttpRequestInfo request; |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 495 | request.method = "HEAD"; |
| 496 | request.url = GURL("http://www.google.com/"); |
| 497 | request.load_flags = 0; |
| 498 | |
| 499 | MockWrite data_writes1[] = { |
| 500 | MockWrite("HEAD / HTTP/1.1\r\n" |
| 501 | "Host: www.google.com\r\n" |
| 502 | "Connection: keep-alive\r\n" |
| 503 | "Content-Length: 0\r\n\r\n"), |
| 504 | }; |
| 505 | MockRead data_reads1[] = { |
| 506 | MockRead("HTTP/1.1 404 Not Found\r\n"), |
| 507 | MockRead("Server: Blah\r\n"), |
| 508 | MockRead("Content-Length: 1234\r\n\r\n"), |
| 509 | |
| 510 | // No response body because the test stops reading here. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 511 | MockRead(false, ERR_UNEXPECTED), // Should not be reached. |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 512 | }; |
| 513 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 514 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 515 | data_writes1, arraysize(data_writes1)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 516 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 517 | |
| 518 | TestCompletionCallback callback1; |
| 519 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 520 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 521 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 522 | |
| 523 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 524 | EXPECT_EQ(OK, rv); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 525 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 526 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 527 | EXPECT_FALSE(response == NULL); |
| 528 | |
| 529 | // Check that the headers got parsed. |
| 530 | EXPECT_TRUE(response->headers != NULL); |
| 531 | EXPECT_EQ(1234, response->headers->GetContentLength()); |
| 532 | EXPECT_EQ("HTTP/1.1 404 Not Found", response->headers->GetStatusLine()); |
| 533 | |
| 534 | std::string server_header; |
| 535 | void* iter = NULL; |
| 536 | bool has_server_header = response->headers->EnumerateHeader( |
| 537 | &iter, "Server", &server_header); |
| 538 | EXPECT_TRUE(has_server_header); |
| 539 | EXPECT_EQ("Blah", server_header); |
| 540 | |
| 541 | // Reading should give EOF right away, since there is no message body |
| 542 | // (despite non-zero content-length). |
| 543 | std::string response_data; |
| 544 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 545 | EXPECT_EQ(OK, rv); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 546 | EXPECT_EQ("", response_data); |
| 547 | } |
| 548 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 549 | TEST_F(HttpNetworkTransactionTest, ReuseConnection) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 550 | SessionDependencies session_deps; |
| 551 | scoped_refptr<HttpNetworkSession> session = CreateSession(&session_deps); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 552 | |
| 553 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 554 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 555 | MockRead("hello"), |
| 556 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 557 | MockRead("world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 558 | MockRead(false, OK), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 559 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 560 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 561 | session_deps.socket_factory.AddSocketDataProvider(&data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 562 | |
| 563 | const char* kExpectedResponseData[] = { |
| 564 | "hello", "world" |
| 565 | }; |
| 566 | |
| 567 | for (int i = 0; i < 2; ++i) { |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 568 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 569 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 570 | HttpRequestInfo request; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 571 | request.method = "GET"; |
| 572 | request.url = GURL("http://www.google.com/"); |
| 573 | request.load_flags = 0; |
| 574 | |
| 575 | TestCompletionCallback callback; |
| 576 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 577 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 578 | EXPECT_EQ(ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 579 | |
| 580 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 581 | EXPECT_EQ(OK, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 582 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 583 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 584 | EXPECT_TRUE(response != NULL); |
| 585 | |
| 586 | EXPECT_TRUE(response->headers != NULL); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 587 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 588 | |
| 589 | std::string response_data; |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 590 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 591 | EXPECT_EQ(OK, rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 592 | EXPECT_EQ(kExpectedResponseData[i], response_data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | |
| 596 | TEST_F(HttpNetworkTransactionTest, Ignores100) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 597 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 598 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 599 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 600 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 601 | HttpRequestInfo request; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 602 | request.method = "POST"; |
| 603 | request.url = GURL("http://www.foo.com/"); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 604 | request.upload_data = new UploadData; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 605 | request.upload_data->AppendBytes("foo", 3); |
| 606 | request.load_flags = 0; |
| 607 | |
| 608 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 609 | MockRead("HTTP/1.0 100 Continue\r\n\r\n"), |
| 610 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 611 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 612 | MockRead(false, OK), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 613 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 614 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 615 | session_deps.socket_factory.AddSocketDataProvider(&data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 616 | |
| 617 | TestCompletionCallback callback; |
| 618 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 619 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 620 | EXPECT_EQ(ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 621 | |
| 622 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 623 | EXPECT_EQ(OK, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 624 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 625 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 626 | EXPECT_TRUE(response != NULL); |
| 627 | |
| 628 | EXPECT_TRUE(response->headers != NULL); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 629 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 630 | |
| 631 | std::string response_data; |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 632 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 633 | EXPECT_EQ(OK, rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 634 | EXPECT_EQ("hello world", response_data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 635 | } |
| 636 | |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 637 | // This test is almost the same as Ignores100 above, but the response contains |
| 638 | // 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] | 639 | // HTTP/1.1 and the two status headers are read in one read. |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 640 | TEST_F(HttpNetworkTransactionTest, Ignores1xx) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 641 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 642 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 643 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 644 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 645 | HttpRequestInfo request; |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 646 | request.method = "GET"; |
| 647 | request.url = GURL("http://www.foo.com/"); |
| 648 | request.load_flags = 0; |
| 649 | |
| 650 | MockRead data_reads[] = { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 651 | MockRead("HTTP/1.1 102 Unspecified status code\r\n\r\n" |
| 652 | "HTTP/1.1 200 OK\r\n\r\n"), |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 653 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 654 | MockRead(false, OK), |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 655 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 656 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 657 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 658 | |
| 659 | TestCompletionCallback callback; |
| 660 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 661 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 662 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 663 | |
| 664 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 665 | EXPECT_EQ(OK, rv); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 666 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 667 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 668 | EXPECT_TRUE(response != NULL); |
| 669 | |
| 670 | EXPECT_TRUE(response->headers != NULL); |
| 671 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 672 | |
| 673 | std::string response_data; |
| 674 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 675 | EXPECT_EQ(OK, rv); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 676 | EXPECT_EQ("hello world", response_data); |
| 677 | } |
| 678 | |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 679 | TEST_F(HttpNetworkTransactionTest, Incomplete100ThenEOF) { |
| 680 | SessionDependencies session_deps; |
| 681 | scoped_ptr<HttpTransaction> trans( |
| 682 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 683 | |
| 684 | HttpRequestInfo request; |
| 685 | request.method = "POST"; |
| 686 | request.url = GURL("http://www.foo.com/"); |
| 687 | request.load_flags = 0; |
| 688 | |
| 689 | MockRead data_reads[] = { |
| 690 | MockRead(false, "HTTP/1.0 100 Continue\r\n"), |
| 691 | MockRead(true, 0), |
| 692 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 693 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 694 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 695 | |
| 696 | TestCompletionCallback callback; |
| 697 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 698 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 699 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 700 | |
| 701 | rv = callback.WaitForResult(); |
| 702 | EXPECT_EQ(OK, rv); |
| 703 | |
| 704 | std::string response_data; |
| 705 | rv = ReadTransaction(trans.get(), &response_data); |
| 706 | EXPECT_EQ(OK, rv); |
| 707 | EXPECT_EQ("", response_data); |
| 708 | } |
| 709 | |
| 710 | TEST_F(HttpNetworkTransactionTest, EmptyResponse) { |
| 711 | SessionDependencies session_deps; |
| 712 | scoped_ptr<HttpTransaction> trans( |
| 713 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 714 | |
| 715 | HttpRequestInfo request; |
| 716 | request.method = "POST"; |
| 717 | request.url = GURL("http://www.foo.com/"); |
| 718 | request.load_flags = 0; |
| 719 | |
| 720 | MockRead data_reads[] = { |
| 721 | MockRead(true, 0), |
| 722 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 723 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 724 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 725 | |
| 726 | TestCompletionCallback callback; |
| 727 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 728 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 729 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 730 | |
| 731 | rv = callback.WaitForResult(); |
| 732 | EXPECT_EQ(ERR_EMPTY_RESPONSE, rv); |
| 733 | } |
| 734 | |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 735 | // read_failure specifies a read failure that should cause the network |
| 736 | // transaction to resend the request. |
| 737 | void HttpNetworkTransactionTest::KeepAliveConnectionResendRequestTest( |
| 738 | const MockRead& read_failure) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 739 | SessionDependencies session_deps; |
| 740 | scoped_refptr<HttpNetworkSession> session = CreateSession(&session_deps); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 741 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 742 | HttpRequestInfo request; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 743 | request.method = "GET"; |
| 744 | request.url = GURL("http://www.foo.com/"); |
| 745 | request.load_flags = 0; |
| 746 | |
| 747 | MockRead data1_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 748 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 749 | MockRead("hello"), |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 750 | read_failure, // Now, we reuse the connection and fail the first read. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 751 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 752 | StaticSocketDataProvider data1(data1_reads, arraysize(data1_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 753 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 754 | |
| 755 | MockRead data2_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 756 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 757 | MockRead("world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 758 | MockRead(true, OK), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 759 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 760 | StaticSocketDataProvider data2(data2_reads, arraysize(data2_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 761 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 762 | |
| 763 | const char* kExpectedResponseData[] = { |
| 764 | "hello", "world" |
| 765 | }; |
| 766 | |
| 767 | for (int i = 0; i < 2; ++i) { |
| 768 | TestCompletionCallback callback; |
| 769 | |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 770 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 771 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 772 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 773 | EXPECT_EQ(ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 774 | |
| 775 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 776 | EXPECT_EQ(OK, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 777 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 778 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 779 | EXPECT_TRUE(response != NULL); |
| 780 | |
| 781 | EXPECT_TRUE(response->headers != NULL); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 782 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 783 | |
| 784 | std::string response_data; |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 785 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 786 | EXPECT_EQ(OK, rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 787 | EXPECT_EQ(kExpectedResponseData[i], response_data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 788 | } |
| 789 | } |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 790 | |
| 791 | TEST_F(HttpNetworkTransactionTest, KeepAliveConnectionReset) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 792 | MockRead read_failure(true, ERR_CONNECTION_RESET); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 793 | KeepAliveConnectionResendRequestTest(read_failure); |
| 794 | } |
| 795 | |
| 796 | TEST_F(HttpNetworkTransactionTest, KeepAliveConnectionEOF) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 797 | MockRead read_failure(false, OK); // EOF |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 798 | KeepAliveConnectionResendRequestTest(read_failure); |
| 799 | } |
| 800 | |
| 801 | TEST_F(HttpNetworkTransactionTest, NonKeepAliveConnectionReset) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 802 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 803 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 804 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 805 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 806 | HttpRequestInfo request; |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 807 | request.method = "GET"; |
| 808 | request.url = GURL("http://www.google.com/"); |
| 809 | request.load_flags = 0; |
| 810 | |
| 811 | MockRead data_reads[] = { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 812 | MockRead(true, ERR_CONNECTION_RESET), |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 813 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), // Should not be used |
| 814 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 815 | MockRead(false, OK), |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 816 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 817 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 818 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 819 | |
| 820 | TestCompletionCallback callback; |
| 821 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 822 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 823 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 824 | |
| 825 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 826 | EXPECT_EQ(ERR_CONNECTION_RESET, rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 827 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 828 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 829 | EXPECT_TRUE(response == NULL); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | // What do various browsers do when the server closes a non-keepalive |
| 833 | // connection without sending any response header or body? |
| 834 | // |
| 835 | // IE7: error page |
| 836 | // Safari 3.1.2 (Windows): error page |
| 837 | // Firefox 3.0.1: blank page |
| 838 | // Opera 9.52: after five attempts, blank page |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 839 | // Us with WinHTTP: error page (ERR_INVALID_RESPONSE) |
| 840 | // Us: error page (EMPTY_RESPONSE) |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 841 | TEST_F(HttpNetworkTransactionTest, NonKeepAliveConnectionEOF) { |
| 842 | MockRead data_reads[] = { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 843 | MockRead(false, OK), // EOF |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 844 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), // Should not be used |
| 845 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 846 | MockRead(false, OK), |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 847 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 848 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 849 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 850 | EXPECT_EQ(ERR_EMPTY_RESPONSE, out.rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 851 | } |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 852 | |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 853 | // Test that we correctly reuse a keep-alive connection after receiving a 304. |
| 854 | TEST_F(HttpNetworkTransactionTest, KeepAliveAfter304) { |
| 855 | SessionDependencies session_deps; |
| 856 | scoped_refptr<HttpNetworkSession> session = CreateSession(&session_deps); |
| 857 | |
| 858 | HttpRequestInfo request; |
| 859 | request.method = "GET"; |
| 860 | request.url = GURL("http://www.foo.com/"); |
| 861 | request.load_flags = 0; |
| 862 | |
| 863 | MockRead data1_reads[] = { |
| 864 | MockRead("HTTP/1.1 304 Not Modified\r\n\r\n"), |
| 865 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 866 | MockRead("hello"), |
| 867 | }; |
| 868 | StaticSocketDataProvider data1(data1_reads, arraysize(data1_reads), NULL, 0); |
| 869 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 870 | |
| 871 | MockRead data2_reads[] = { |
| 872 | MockRead(false, ERR_UNEXPECTED), // Should not be reached. |
| 873 | }; |
| 874 | StaticSocketDataProvider data2(data2_reads, arraysize(data2_reads), NULL, 0); |
| 875 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 876 | |
| 877 | for (int i = 0; i < 2; ++i) { |
| 878 | TestCompletionCallback callback; |
| 879 | |
| 880 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 881 | |
| 882 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 883 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 884 | |
| 885 | rv = callback.WaitForResult(); |
| 886 | EXPECT_EQ(OK, rv); |
| 887 | |
| 888 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 889 | EXPECT_TRUE(response != NULL); |
| 890 | |
| 891 | EXPECT_TRUE(response->headers != NULL); |
| 892 | if (i == 0) { |
| 893 | EXPECT_EQ("HTTP/1.1 304 Not Modified", |
| 894 | response->headers->GetStatusLine()); |
| 895 | // We intentionally don't read the response in this case, to reflect how |
| 896 | // HttpCache::Transaction uses HttpNetworkTransaction. |
| 897 | } else { |
| 898 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 899 | std::string response_data; |
| 900 | rv = ReadTransaction(trans.get(), &response_data); |
| 901 | EXPECT_EQ(OK, rv); |
| 902 | EXPECT_EQ("hello", response_data); |
| 903 | } |
| 904 | } |
| 905 | } |
| 906 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 907 | // Test the request-challenge-retry sequence for basic auth. |
| 908 | // (basic auth is the easiest to mock, because it has no randomness). |
| 909 | TEST_F(HttpNetworkTransactionTest, BasicAuth) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 910 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 911 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 912 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 913 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 914 | HttpRequestInfo request; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 915 | request.method = "GET"; |
| 916 | request.url = GURL("http://www.google.com/"); |
| 917 | request.load_flags = 0; |
| 918 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 919 | MockWrite data_writes1[] = { |
| 920 | MockWrite("GET / HTTP/1.1\r\n" |
| 921 | "Host: www.google.com\r\n" |
| 922 | "Connection: keep-alive\r\n\r\n"), |
| 923 | }; |
| 924 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 925 | MockRead data_reads1[] = { |
| 926 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 927 | // Give a couple authenticate options (only the middle one is actually |
| 928 | // supported). |
[email protected] | 22927ad | 2009-09-21 19:56:19 | [diff] [blame] | 929 | MockRead("WWW-Authenticate: Basic invalid\r\n"), // Malformed. |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 930 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 931 | MockRead("WWW-Authenticate: UNSUPPORTED realm=\"FOO\"\r\n"), |
| 932 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 933 | // Large content-length -- won't matter, as connection will be reset. |
| 934 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 935 | MockRead(false, ERR_FAILED), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 936 | }; |
| 937 | |
| 938 | // After calling trans->RestartWithAuth(), this is the request we should |
| 939 | // be issuing -- the final header line contains the credentials. |
| 940 | MockWrite data_writes2[] = { |
| 941 | MockWrite("GET / HTTP/1.1\r\n" |
| 942 | "Host: www.google.com\r\n" |
| 943 | "Connection: keep-alive\r\n" |
| 944 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 945 | }; |
| 946 | |
| 947 | // Lastly, the server responds with the actual content. |
| 948 | MockRead data_reads2[] = { |
| 949 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 950 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 951 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 952 | MockRead(false, OK), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 953 | }; |
| 954 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 955 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 956 | data_writes1, arraysize(data_writes1)); |
| 957 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 958 | data_writes2, arraysize(data_writes2)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 959 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 960 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 961 | |
| 962 | TestCompletionCallback callback1; |
| 963 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 964 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 965 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 966 | |
| 967 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 968 | EXPECT_EQ(OK, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 969 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 970 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 971 | EXPECT_FALSE(response == NULL); |
| 972 | |
| 973 | // The password prompt info should have been set in response->auth_challenge. |
| 974 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 975 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 976 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 977 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 978 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 979 | |
| 980 | TestCompletionCallback callback2; |
| 981 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 982 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 983 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 984 | |
| 985 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 986 | EXPECT_EQ(OK, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 987 | |
| 988 | response = trans->GetResponseInfo(); |
| 989 | EXPECT_FALSE(response == NULL); |
| 990 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 991 | EXPECT_EQ(100, response->headers->GetContentLength()); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 992 | } |
| 993 | |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 994 | TEST_F(HttpNetworkTransactionTest, DoNotSendAuth) { |
| 995 | SessionDependencies session_deps; |
| 996 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 997 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 998 | |
| 999 | HttpRequestInfo request; |
| 1000 | request.method = "GET"; |
| 1001 | request.url = GURL("http://www.google.com/"); |
| 1002 | request.load_flags = net::LOAD_DO_NOT_SEND_AUTH_DATA; |
| 1003 | |
| 1004 | MockWrite data_writes[] = { |
| 1005 | MockWrite("GET / HTTP/1.1\r\n" |
| 1006 | "Host: www.google.com\r\n" |
| 1007 | "Connection: keep-alive\r\n\r\n"), |
| 1008 | }; |
| 1009 | |
| 1010 | MockRead data_reads[] = { |
| 1011 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 1012 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1013 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1014 | // Large content-length -- won't matter, as connection will be reset. |
| 1015 | MockRead("Content-Length: 10000\r\n\r\n"), |
| 1016 | MockRead(false, ERR_FAILED), |
| 1017 | }; |
| 1018 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1019 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 1020 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1021 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1022 | TestCompletionCallback callback; |
| 1023 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1024 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1025 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1026 | |
| 1027 | rv = callback.WaitForResult(); |
| 1028 | EXPECT_EQ(0, rv); |
| 1029 | |
| 1030 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 1031 | ASSERT_FALSE(response == NULL); |
| 1032 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 1033 | } |
| 1034 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1035 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
| 1036 | // connection. |
| 1037 | TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAlive) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1038 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 1039 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 1040 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1041 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1042 | HttpRequestInfo request; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1043 | request.method = "GET"; |
| 1044 | request.url = GURL("http://www.google.com/"); |
| 1045 | request.load_flags = 0; |
| 1046 | |
| 1047 | MockWrite data_writes1[] = { |
| 1048 | MockWrite("GET / HTTP/1.1\r\n" |
| 1049 | "Host: www.google.com\r\n" |
| 1050 | "Connection: keep-alive\r\n\r\n"), |
| 1051 | |
| 1052 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1053 | // be issuing -- the final header line contains the credentials. |
| 1054 | MockWrite("GET / HTTP/1.1\r\n" |
| 1055 | "Host: www.google.com\r\n" |
| 1056 | "Connection: keep-alive\r\n" |
| 1057 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1058 | }; |
| 1059 | |
| 1060 | MockRead data_reads1[] = { |
| 1061 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 1062 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1063 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1064 | MockRead("Content-Length: 14\r\n\r\n"), |
| 1065 | MockRead("Unauthorized\r\n"), |
| 1066 | |
| 1067 | // Lastly, the server responds with the actual content. |
| 1068 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1069 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1070 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1071 | MockRead(false, OK), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1072 | }; |
| 1073 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1074 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1075 | data_writes1, arraysize(data_writes1)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1076 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1077 | |
| 1078 | TestCompletionCallback callback1; |
| 1079 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1080 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1081 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1082 | |
| 1083 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1084 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1085 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1086 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1087 | EXPECT_FALSE(response == NULL); |
| 1088 | |
| 1089 | // The password prompt info should have been set in response->auth_challenge. |
| 1090 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 1091 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 1092 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1093 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 1094 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 1095 | |
| 1096 | TestCompletionCallback callback2; |
| 1097 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 1098 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1099 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1100 | |
| 1101 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1102 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1103 | |
| 1104 | response = trans->GetResponseInfo(); |
| 1105 | EXPECT_FALSE(response == NULL); |
| 1106 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 1107 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 1108 | } |
| 1109 | |
| 1110 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
| 1111 | // connection and with no response body to drain. |
| 1112 | TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAliveNoBody) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1113 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 1114 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 1115 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1116 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1117 | HttpRequestInfo request; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1118 | request.method = "GET"; |
| 1119 | request.url = GURL("http://www.google.com/"); |
| 1120 | request.load_flags = 0; |
| 1121 | |
| 1122 | MockWrite data_writes1[] = { |
| 1123 | MockWrite("GET / HTTP/1.1\r\n" |
| 1124 | "Host: www.google.com\r\n" |
| 1125 | "Connection: keep-alive\r\n\r\n"), |
| 1126 | |
| 1127 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1128 | // be issuing -- the final header line contains the credentials. |
| 1129 | MockWrite("GET / HTTP/1.1\r\n" |
| 1130 | "Host: www.google.com\r\n" |
| 1131 | "Connection: keep-alive\r\n" |
| 1132 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1133 | }; |
| 1134 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1135 | MockRead data_reads1[] = { |
| 1136 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 1137 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1138 | MockRead("Content-Length: 0\r\n\r\n"), // No response body. |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1139 | |
| 1140 | // Lastly, the server responds with the actual content. |
| 1141 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1142 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1143 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1144 | MockRead(false, OK), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1145 | }; |
| 1146 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1147 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1148 | data_writes1, arraysize(data_writes1)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1149 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1150 | |
| 1151 | TestCompletionCallback callback1; |
| 1152 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1153 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1154 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1155 | |
| 1156 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1157 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1158 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1159 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1160 | EXPECT_FALSE(response == NULL); |
| 1161 | |
| 1162 | // The password prompt info should have been set in response->auth_challenge. |
| 1163 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 1164 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 1165 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1166 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 1167 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 1168 | |
| 1169 | TestCompletionCallback callback2; |
| 1170 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 1171 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1172 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1173 | |
| 1174 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1175 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1176 | |
| 1177 | response = trans->GetResponseInfo(); |
| 1178 | EXPECT_FALSE(response == NULL); |
| 1179 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 1180 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 1181 | } |
| 1182 | |
| 1183 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
| 1184 | // connection and with a large response body to drain. |
| 1185 | TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAliveLargeBody) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1186 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 1187 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 1188 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1189 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1190 | HttpRequestInfo request; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1191 | request.method = "GET"; |
| 1192 | request.url = GURL("http://www.google.com/"); |
| 1193 | request.load_flags = 0; |
| 1194 | |
| 1195 | MockWrite data_writes1[] = { |
| 1196 | MockWrite("GET / HTTP/1.1\r\n" |
| 1197 | "Host: www.google.com\r\n" |
| 1198 | "Connection: keep-alive\r\n\r\n"), |
| 1199 | |
| 1200 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1201 | // be issuing -- the final header line contains the credentials. |
| 1202 | MockWrite("GET / HTTP/1.1\r\n" |
| 1203 | "Host: www.google.com\r\n" |
| 1204 | "Connection: keep-alive\r\n" |
| 1205 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1206 | }; |
| 1207 | |
| 1208 | // Respond with 5 kb of response body. |
| 1209 | std::string large_body_string("Unauthorized"); |
| 1210 | large_body_string.append(5 * 1024, ' '); |
| 1211 | large_body_string.append("\r\n"); |
| 1212 | |
| 1213 | MockRead data_reads1[] = { |
| 1214 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 1215 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1216 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1217 | // 5134 = 12 + 5 * 1024 + 2 |
| 1218 | MockRead("Content-Length: 5134\r\n\r\n"), |
| 1219 | MockRead(true, large_body_string.data(), large_body_string.size()), |
| 1220 | |
| 1221 | // Lastly, the server responds with the actual content. |
| 1222 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1223 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1224 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1225 | MockRead(false, OK), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1226 | }; |
| 1227 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1228 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1229 | data_writes1, arraysize(data_writes1)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1230 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1231 | |
| 1232 | TestCompletionCallback callback1; |
| 1233 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1234 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1235 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1236 | |
| 1237 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1238 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1239 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1240 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1241 | EXPECT_FALSE(response == NULL); |
| 1242 | |
| 1243 | // The password prompt info should have been set in response->auth_challenge. |
| 1244 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 1245 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 1246 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1247 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 1248 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 1249 | |
| 1250 | TestCompletionCallback callback2; |
| 1251 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 1252 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1253 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1254 | |
| 1255 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1256 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1257 | |
| 1258 | response = trans->GetResponseInfo(); |
| 1259 | EXPECT_FALSE(response == NULL); |
| 1260 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 1261 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 1262 | } |
| 1263 | |
| 1264 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1265 | // connection, but the server gets impatient and closes the connection. |
| 1266 | TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAliveImpatientServer) { |
| 1267 | SessionDependencies session_deps; |
| 1268 | scoped_ptr<HttpTransaction> trans( |
| 1269 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 1270 | |
| 1271 | HttpRequestInfo request; |
| 1272 | request.method = "GET"; |
| 1273 | request.url = GURL("http://www.google.com/"); |
| 1274 | request.load_flags = 0; |
| 1275 | |
| 1276 | MockWrite data_writes1[] = { |
| 1277 | MockWrite("GET / HTTP/1.1\r\n" |
| 1278 | "Host: www.google.com\r\n" |
| 1279 | "Connection: keep-alive\r\n\r\n"), |
| 1280 | // This simulates the seemingly successful write to a closed connection |
| 1281 | // if the bug is not fixed. |
| 1282 | MockWrite("GET / HTTP/1.1\r\n" |
| 1283 | "Host: www.google.com\r\n" |
| 1284 | "Connection: keep-alive\r\n" |
| 1285 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1286 | }; |
| 1287 | |
| 1288 | MockRead data_reads1[] = { |
| 1289 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 1290 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1291 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1292 | MockRead("Content-Length: 14\r\n\r\n"), |
| 1293 | // Tell MockTCPClientSocket to simulate the server closing the connection. |
| 1294 | MockRead(false, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| 1295 | MockRead("Unauthorized\r\n"), |
| 1296 | MockRead(false, OK), // The server closes the connection. |
| 1297 | }; |
| 1298 | |
| 1299 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1300 | // be issuing -- the final header line contains the credentials. |
| 1301 | MockWrite data_writes2[] = { |
| 1302 | MockWrite("GET / HTTP/1.1\r\n" |
| 1303 | "Host: www.google.com\r\n" |
| 1304 | "Connection: keep-alive\r\n" |
| 1305 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1306 | }; |
| 1307 | |
| 1308 | // Lastly, the server responds with the actual content. |
| 1309 | MockRead data_reads2[] = { |
| 1310 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1311 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1312 | MockRead("Content-Length: 100\r\n\r\n"), |
| 1313 | MockRead(false, OK), |
| 1314 | }; |
| 1315 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1316 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1317 | data_writes1, arraysize(data_writes1)); |
| 1318 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 1319 | data_writes2, arraysize(data_writes2)); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1320 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 1321 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 1322 | |
| 1323 | TestCompletionCallback callback1; |
| 1324 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1325 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1326 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1327 | |
| 1328 | rv = callback1.WaitForResult(); |
| 1329 | EXPECT_EQ(OK, rv); |
| 1330 | |
| 1331 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 1332 | EXPECT_FALSE(response == NULL); |
| 1333 | |
| 1334 | // The password prompt info should have been set in response->auth_challenge. |
| 1335 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 1336 | |
| 1337 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
| 1338 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 1339 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 1340 | |
| 1341 | TestCompletionCallback callback2; |
| 1342 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 1343 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1344 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1345 | |
| 1346 | rv = callback2.WaitForResult(); |
| 1347 | EXPECT_EQ(OK, rv); |
| 1348 | |
| 1349 | response = trans->GetResponseInfo(); |
| 1350 | ASSERT_FALSE(response == NULL); |
| 1351 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 1352 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 1353 | } |
| 1354 | |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1355 | // Test the request-challenge-retry sequence for basic auth, over a connection |
| 1356 | // that requires a restart when setting up an SSL tunnel. |
| 1357 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyNoKeepAlive) { |
| 1358 | // Configure against proxy server "myproxy:70". |
| 1359 | SessionDependencies session_deps(CreateFixedProxyService("myproxy:70")); |
| 1360 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 1361 | session_deps.net_log = log.bound().net_log(); |
| 1362 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 1363 | |
| 1364 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 1365 | |
| 1366 | HttpRequestInfo request; |
| 1367 | request.method = "GET"; |
| 1368 | request.url = GURL("https://www.google.com/"); |
| 1369 | // when the no authentication data flag is set. |
| 1370 | request.load_flags = net::LOAD_DO_NOT_SEND_AUTH_DATA; |
| 1371 | |
| 1372 | // Since we have proxy, should try to establish tunnel. |
| 1373 | MockWrite data_writes1[] = { |
| 1374 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 1375 | "Host: www.google.com\r\n" |
| 1376 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 1377 | |
| 1378 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1379 | // be issuing -- the final header line contains the credentials. |
| 1380 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 1381 | "Host: www.google.com\r\n" |
| 1382 | "Proxy-Connection: keep-alive\r\n" |
| 1383 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1384 | |
| 1385 | MockWrite("GET / HTTP/1.1\r\n" |
| 1386 | "Host: www.google.com\r\n" |
| 1387 | "Connection: keep-alive\r\n\r\n"), |
| 1388 | }; |
| 1389 | |
| 1390 | // The proxy responds to the connect with a 407, using a persistent |
| 1391 | // connection. |
| 1392 | MockRead data_reads1[] = { |
| 1393 | // No credentials. |
| 1394 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 1395 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1396 | MockRead("Proxy-Connection: close\r\n\r\n"), |
| 1397 | |
| 1398 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 1399 | |
| 1400 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1401 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1402 | MockRead("Content-Length: 100\r\n\r\n"), |
| 1403 | MockRead(false, OK), |
| 1404 | }; |
| 1405 | |
| 1406 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1407 | data_writes1, arraysize(data_writes1)); |
| 1408 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 1409 | SSLSocketDataProvider ssl(true, OK); |
| 1410 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 1411 | |
| 1412 | TestCompletionCallback callback1; |
| 1413 | |
| 1414 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 1415 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1416 | |
| 1417 | rv = callback1.WaitForResult(); |
| 1418 | EXPECT_EQ(OK, rv); |
| 1419 | size_t pos = ExpectLogContainsSomewhere( |
| 1420 | log.entries(), 0, NetLog::TYPE_HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
| 1421 | NetLog::PHASE_NONE); |
| 1422 | ExpectLogContainsSomewhere( |
| 1423 | log.entries(), pos, |
| 1424 | NetLog::TYPE_HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 1425 | NetLog::PHASE_NONE); |
| 1426 | |
| 1427 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 1428 | ASSERT_FALSE(response == NULL); |
| 1429 | |
| 1430 | EXPECT_EQ(407, response->headers->response_code()); |
| 1431 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 1432 | |
| 1433 | // The password prompt info should have been set in response->auth_challenge. |
| 1434 | ASSERT_FALSE(response->auth_challenge.get() == NULL); |
| 1435 | |
| 1436 | EXPECT_EQ(L"myproxy:70", response->auth_challenge->host_and_port); |
| 1437 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 1438 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 1439 | |
| 1440 | TestCompletionCallback callback2; |
| 1441 | |
| 1442 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
| 1443 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1444 | |
| 1445 | rv = callback2.WaitForResult(); |
| 1446 | EXPECT_EQ(OK, rv); |
| 1447 | |
| 1448 | response = trans->GetResponseInfo(); |
| 1449 | ASSERT_FALSE(response == NULL); |
| 1450 | |
| 1451 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 1452 | EXPECT_EQ(200, response->headers->response_code()); |
| 1453 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 1454 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 1455 | |
| 1456 | // The password prompt info should not be set. |
| 1457 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 1458 | } |
| 1459 | |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1460 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1461 | // proxy connection, when setting up an SSL tunnel. |
| 1462 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyKeepAlive) { |
| 1463 | // Configure against proxy server "myproxy:70". |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1464 | SessionDependencies session_deps(CreateFixedProxyService("myproxy:70")); |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 1465 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 1466 | session_deps.net_log = log.bound().net_log(); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1467 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1468 | |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 1469 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1470 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1471 | HttpRequestInfo request; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1472 | request.method = "GET"; |
| 1473 | request.url = GURL("https://www.google.com/"); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1474 | // Ensure that proxy authentication is attempted even |
| 1475 | // when the no authentication data flag is set. |
| 1476 | request.load_flags = net::LOAD_DO_NOT_SEND_AUTH_DATA; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1477 | |
| 1478 | // Since we have proxy, should try to establish tunnel. |
| 1479 | MockWrite data_writes1[] = { |
| 1480 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 1481 | "Host: www.google.com\r\n" |
| 1482 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1483 | |
| 1484 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1485 | // be issuing -- the final header line contains the credentials. |
| 1486 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 1487 | "Host: www.google.com\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 1488 | "Proxy-Connection: keep-alive\r\n" |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1489 | "Proxy-Authorization: Basic Zm9vOmJheg==\r\n\r\n"), |
| 1490 | }; |
| 1491 | |
| 1492 | // The proxy responds to the connect with a 407, using a persistent |
| 1493 | // connection. |
| 1494 | MockRead data_reads1[] = { |
| 1495 | // No credentials. |
| 1496 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 1497 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1498 | MockRead("Content-Length: 10\r\n\r\n"), |
| 1499 | MockRead("0123456789"), |
| 1500 | |
| 1501 | // Wrong credentials (wrong password). |
| 1502 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 1503 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1504 | MockRead("Content-Length: 10\r\n\r\n"), |
| 1505 | // No response body because the test stops reading here. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1506 | MockRead(false, ERR_UNEXPECTED), // Should not be reached. |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1507 | }; |
| 1508 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1509 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1510 | data_writes1, arraysize(data_writes1)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1511 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1512 | |
| 1513 | TestCompletionCallback callback1; |
| 1514 | |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 1515 | int rv = trans->Start(&request, &callback1, log.bound()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1516 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1517 | |
| 1518 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1519 | EXPECT_EQ(OK, rv); |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 1520 | size_t pos = ExpectLogContainsSomewhere( |
| 1521 | log.entries(), 0, NetLog::TYPE_HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
| 1522 | NetLog::PHASE_NONE); |
| 1523 | ExpectLogContainsSomewhere( |
| 1524 | log.entries(), pos, |
| 1525 | NetLog::TYPE_HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 1526 | NetLog::PHASE_NONE); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1527 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1528 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1529 | EXPECT_FALSE(response == NULL); |
| 1530 | |
| 1531 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 1532 | EXPECT_EQ(407, response->headers->response_code()); |
| 1533 | EXPECT_EQ(10, response->headers->GetContentLength()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1534 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1535 | |
| 1536 | // The password prompt info should have been set in response->auth_challenge. |
| 1537 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 1538 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 1539 | EXPECT_EQ(L"myproxy:70", response->auth_challenge->host_and_port); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1540 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 1541 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 1542 | |
| 1543 | TestCompletionCallback callback2; |
| 1544 | |
| 1545 | // Wrong password (should be "bar"). |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 1546 | rv = trans->RestartWithAuth(kFoo, kBaz, &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1547 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1548 | |
| 1549 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1550 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1551 | |
| 1552 | response = trans->GetResponseInfo(); |
| 1553 | EXPECT_FALSE(response == NULL); |
| 1554 | |
| 1555 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 1556 | EXPECT_EQ(407, response->headers->response_code()); |
| 1557 | EXPECT_EQ(10, response->headers->GetContentLength()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1558 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1559 | |
| 1560 | // The password prompt info should have been set in response->auth_challenge. |
| 1561 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 1562 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 1563 | EXPECT_EQ(L"myproxy:70", response->auth_challenge->host_and_port); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1564 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 1565 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 1566 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 1567 | // Flush the idle socket before the NetLog and HttpNetworkTransaction go |
| 1568 | // out of scope. |
| 1569 | session->FlushSocketPools(); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1570 | } |
| 1571 | |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1572 | // Test that we don't read the response body when we fail to establish a tunnel, |
| 1573 | // even if the user cancels the proxy's auth attempt. |
| 1574 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyCancelTunnel) { |
| 1575 | // Configure against proxy server "myproxy:70". |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1576 | SessionDependencies session_deps(CreateFixedProxyService("myproxy:70")); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1577 | |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 1578 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1579 | |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 1580 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1581 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1582 | HttpRequestInfo request; |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1583 | request.method = "GET"; |
| 1584 | request.url = GURL("https://www.google.com/"); |
| 1585 | request.load_flags = 0; |
| 1586 | |
| 1587 | // Since we have proxy, should try to establish tunnel. |
| 1588 | MockWrite data_writes[] = { |
| 1589 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 1590 | "Host: www.google.com\r\n" |
| 1591 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1592 | }; |
| 1593 | |
| 1594 | // The proxy responds to the connect with a 407. |
| 1595 | MockRead data_reads[] = { |
| 1596 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 1597 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1598 | MockRead("Content-Length: 10\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1599 | MockRead(false, ERR_UNEXPECTED), // Should not be reached. |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1600 | }; |
| 1601 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1602 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 1603 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1604 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1605 | |
| 1606 | TestCompletionCallback callback; |
| 1607 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1608 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1609 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1610 | |
| 1611 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1612 | EXPECT_EQ(OK, rv); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1613 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1614 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1615 | EXPECT_FALSE(response == NULL); |
| 1616 | |
| 1617 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 1618 | EXPECT_EQ(407, response->headers->response_code()); |
| 1619 | EXPECT_EQ(10, response->headers->GetContentLength()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1620 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1621 | |
| 1622 | std::string response_data; |
| 1623 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1624 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, rv); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 1625 | |
| 1626 | // Flush the idle socket before the HttpNetworkTransaction goes out of scope. |
| 1627 | session->FlushSocketPools(); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1628 | } |
| 1629 | |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 1630 | // Test when a server (non-proxy) returns a 407 (proxy-authenticate). |
| 1631 | // The request should fail with ERR_UNEXPECTED_PROXY_AUTH. |
| 1632 | TEST_F(HttpNetworkTransactionTest, UnexpectedProxyAuth) { |
| 1633 | // We are using a DIRECT connection (i.e. no proxy) for this session. |
| 1634 | SessionDependencies session_deps; |
| 1635 | scoped_ptr<HttpTransaction> trans( |
| 1636 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 1637 | |
| 1638 | HttpRequestInfo request; |
| 1639 | request.method = "GET"; |
| 1640 | request.url = GURL("http://www.google.com/"); |
| 1641 | request.load_flags = 0; |
| 1642 | |
| 1643 | MockWrite data_writes1[] = { |
| 1644 | MockWrite("GET / HTTP/1.1\r\n" |
| 1645 | "Host: www.google.com\r\n" |
| 1646 | "Connection: keep-alive\r\n\r\n"), |
| 1647 | }; |
| 1648 | |
| 1649 | MockRead data_reads1[] = { |
| 1650 | MockRead("HTTP/1.0 407 Proxy Auth required\r\n"), |
| 1651 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1652 | // Large content-length -- won't matter, as connection will be reset. |
| 1653 | MockRead("Content-Length: 10000\r\n\r\n"), |
| 1654 | MockRead(false, ERR_FAILED), |
| 1655 | }; |
| 1656 | |
| 1657 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1658 | data_writes1, arraysize(data_writes1)); |
| 1659 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 1660 | |
| 1661 | TestCompletionCallback callback; |
| 1662 | |
| 1663 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 1664 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1665 | |
| 1666 | rv = callback.WaitForResult(); |
| 1667 | EXPECT_EQ(ERR_UNEXPECTED_PROXY_AUTH, rv); |
| 1668 | } |
| 1669 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 1670 | |
| 1671 | // Test a simple get through an HTTPS Proxy. |
| 1672 | TEST_F(HttpNetworkTransactionTest, HttpsProxyGet) { |
| 1673 | // Configure against https proxy server "proxy:70". |
| 1674 | SessionDependencies session_deps(CreateFixedProxyService("https://proxy:70")); |
| 1675 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 1676 | session_deps.net_log = log.bound().net_log(); |
| 1677 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 1678 | |
| 1679 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 1680 | |
| 1681 | HttpRequestInfo request; |
| 1682 | request.method = "GET"; |
| 1683 | request.url = GURL("http://www.google.com/"); |
| 1684 | |
| 1685 | // Since we have proxy, should use full url |
| 1686 | MockWrite data_writes1[] = { |
| 1687 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 1688 | "Host: www.google.com\r\n" |
| 1689 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 1690 | }; |
| 1691 | |
| 1692 | MockRead data_reads1[] = { |
| 1693 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1694 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1695 | MockRead("Content-Length: 100\r\n\r\n"), |
| 1696 | MockRead(false, OK), |
| 1697 | }; |
| 1698 | |
| 1699 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1700 | data_writes1, arraysize(data_writes1)); |
| 1701 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 1702 | SSLSocketDataProvider ssl(true, OK); |
| 1703 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 1704 | |
| 1705 | TestCompletionCallback callback1; |
| 1706 | |
| 1707 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 1708 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1709 | |
| 1710 | rv = callback1.WaitForResult(); |
| 1711 | EXPECT_EQ(OK, rv); |
| 1712 | |
| 1713 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 1714 | ASSERT_FALSE(response == NULL); |
| 1715 | |
| 1716 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 1717 | EXPECT_EQ(200, response->headers->response_code()); |
| 1718 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 1719 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 1720 | |
| 1721 | // The password prompt info should not be set. |
| 1722 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 1723 | } |
| 1724 | |
| 1725 | // Test the challenge-response-retry sequence through an HTTPS Proxy |
| 1726 | TEST_F(HttpNetworkTransactionTest, HttpsProxyAuthRetry) { |
| 1727 | // Configure against https proxy server "proxy:70". |
| 1728 | SessionDependencies session_deps(CreateFixedProxyService("https://proxy:70")); |
| 1729 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 1730 | session_deps.net_log = log.bound().net_log(); |
| 1731 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 1732 | |
| 1733 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 1734 | |
| 1735 | HttpRequestInfo request; |
| 1736 | request.method = "GET"; |
| 1737 | request.url = GURL("http://www.google.com/"); |
| 1738 | // when the no authentication data flag is set. |
| 1739 | request.load_flags = net::LOAD_DO_NOT_SEND_AUTH_DATA; |
| 1740 | |
| 1741 | // Since we have proxy, should use full url |
| 1742 | MockWrite data_writes1[] = { |
| 1743 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 1744 | "Host: www.google.com\r\n" |
| 1745 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 1746 | |
| 1747 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1748 | // be issuing -- the final header line contains the credentials. |
| 1749 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 1750 | "Host: www.google.com\r\n" |
| 1751 | "Proxy-Connection: keep-alive\r\n" |
| 1752 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1753 | }; |
| 1754 | |
| 1755 | // The proxy responds to the GET with a 407, using a persistent |
| 1756 | // connection. |
| 1757 | MockRead data_reads1[] = { |
| 1758 | // No credentials. |
| 1759 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 1760 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1761 | MockRead("Proxy-Connection: keep-alive\r\n"), |
| 1762 | MockRead("Content-Length: 0\r\n\r\n"), |
| 1763 | |
| 1764 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1765 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1766 | MockRead("Content-Length: 100\r\n\r\n"), |
| 1767 | MockRead(false, OK), |
| 1768 | }; |
| 1769 | |
| 1770 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1771 | data_writes1, arraysize(data_writes1)); |
| 1772 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 1773 | SSLSocketDataProvider ssl(true, OK); |
| 1774 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 1775 | |
| 1776 | TestCompletionCallback callback1; |
| 1777 | |
| 1778 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 1779 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1780 | |
| 1781 | rv = callback1.WaitForResult(); |
| 1782 | EXPECT_EQ(OK, rv); |
| 1783 | |
| 1784 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 1785 | ASSERT_FALSE(response == NULL); |
| 1786 | |
| 1787 | EXPECT_EQ(407, response->headers->response_code()); |
| 1788 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 1789 | |
| 1790 | // The password prompt info should have been set in response->auth_challenge. |
| 1791 | ASSERT_FALSE(response->auth_challenge.get() == NULL); |
| 1792 | |
| 1793 | EXPECT_EQ(L"proxy:70", response->auth_challenge->host_and_port); |
| 1794 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 1795 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 1796 | |
| 1797 | TestCompletionCallback callback2; |
| 1798 | |
| 1799 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
| 1800 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1801 | |
| 1802 | rv = callback2.WaitForResult(); |
| 1803 | EXPECT_EQ(OK, rv); |
| 1804 | |
| 1805 | response = trans->GetResponseInfo(); |
| 1806 | ASSERT_FALSE(response == NULL); |
| 1807 | |
| 1808 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 1809 | EXPECT_EQ(200, response->headers->response_code()); |
| 1810 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 1811 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 1812 | |
| 1813 | // The password prompt info should not be set. |
| 1814 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 1815 | } |
| 1816 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 1817 | void HttpNetworkTransactionTest::ConnectStatusHelperWithExpectedStatus( |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 1818 | const MockRead& status, int expected_status) { |
| 1819 | // Configure against proxy server "myproxy:70". |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1820 | SessionDependencies session_deps(CreateFixedProxyService("myproxy:70")); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 1821 | |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1822 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 1823 | |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 1824 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 1825 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1826 | HttpRequestInfo request; |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 1827 | request.method = "GET"; |
| 1828 | request.url = GURL("https://www.google.com/"); |
| 1829 | request.load_flags = 0; |
| 1830 | |
| 1831 | // Since we have proxy, should try to establish tunnel. |
| 1832 | MockWrite data_writes[] = { |
| 1833 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 1834 | "Host: www.google.com\r\n" |
| 1835 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 1836 | }; |
| 1837 | |
| 1838 | MockRead data_reads[] = { |
| 1839 | status, |
| 1840 | MockRead("Content-Length: 10\r\n\r\n"), |
| 1841 | // No response body because the test stops reading here. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1842 | MockRead(false, ERR_UNEXPECTED), // Should not be reached. |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 1843 | }; |
| 1844 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1845 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 1846 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1847 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 1848 | |
| 1849 | TestCompletionCallback callback; |
| 1850 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1851 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1852 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 1853 | |
| 1854 | rv = callback.WaitForResult(); |
| 1855 | EXPECT_EQ(expected_status, rv); |
| 1856 | } |
| 1857 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 1858 | void HttpNetworkTransactionTest::ConnectStatusHelper(const MockRead& status) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 1859 | ConnectStatusHelperWithExpectedStatus( |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1860 | status, ERR_TUNNEL_CONNECTION_FAILED); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 1861 | } |
| 1862 | |
| 1863 | TEST_F(HttpNetworkTransactionTest, ConnectStatus100) { |
| 1864 | ConnectStatusHelper(MockRead("HTTP/1.1 100 Continue\r\n")); |
| 1865 | } |
| 1866 | |
| 1867 | TEST_F(HttpNetworkTransactionTest, ConnectStatus101) { |
| 1868 | ConnectStatusHelper(MockRead("HTTP/1.1 101 Switching Protocols\r\n")); |
| 1869 | } |
| 1870 | |
| 1871 | TEST_F(HttpNetworkTransactionTest, ConnectStatus201) { |
| 1872 | ConnectStatusHelper(MockRead("HTTP/1.1 201 Created\r\n")); |
| 1873 | } |
| 1874 | |
| 1875 | TEST_F(HttpNetworkTransactionTest, ConnectStatus202) { |
| 1876 | ConnectStatusHelper(MockRead("HTTP/1.1 202 Accepted\r\n")); |
| 1877 | } |
| 1878 | |
| 1879 | TEST_F(HttpNetworkTransactionTest, ConnectStatus203) { |
| 1880 | ConnectStatusHelper( |
| 1881 | MockRead("HTTP/1.1 203 Non-Authoritative Information\r\n")); |
| 1882 | } |
| 1883 | |
| 1884 | TEST_F(HttpNetworkTransactionTest, ConnectStatus204) { |
| 1885 | ConnectStatusHelper(MockRead("HTTP/1.1 204 No Content\r\n")); |
| 1886 | } |
| 1887 | |
| 1888 | TEST_F(HttpNetworkTransactionTest, ConnectStatus205) { |
| 1889 | ConnectStatusHelper(MockRead("HTTP/1.1 205 Reset Content\r\n")); |
| 1890 | } |
| 1891 | |
| 1892 | TEST_F(HttpNetworkTransactionTest, ConnectStatus206) { |
| 1893 | ConnectStatusHelper(MockRead("HTTP/1.1 206 Partial Content\r\n")); |
| 1894 | } |
| 1895 | |
| 1896 | TEST_F(HttpNetworkTransactionTest, ConnectStatus300) { |
| 1897 | ConnectStatusHelper(MockRead("HTTP/1.1 300 Multiple Choices\r\n")); |
| 1898 | } |
| 1899 | |
| 1900 | TEST_F(HttpNetworkTransactionTest, ConnectStatus301) { |
| 1901 | ConnectStatusHelper(MockRead("HTTP/1.1 301 Moved Permanently\r\n")); |
| 1902 | } |
| 1903 | |
| 1904 | TEST_F(HttpNetworkTransactionTest, ConnectStatus302) { |
| 1905 | ConnectStatusHelper(MockRead("HTTP/1.1 302 Found\r\n")); |
| 1906 | } |
| 1907 | |
| 1908 | TEST_F(HttpNetworkTransactionTest, ConnectStatus303) { |
| 1909 | ConnectStatusHelper(MockRead("HTTP/1.1 303 See Other\r\n")); |
| 1910 | } |
| 1911 | |
| 1912 | TEST_F(HttpNetworkTransactionTest, ConnectStatus304) { |
| 1913 | ConnectStatusHelper(MockRead("HTTP/1.1 304 Not Modified\r\n")); |
| 1914 | } |
| 1915 | |
| 1916 | TEST_F(HttpNetworkTransactionTest, ConnectStatus305) { |
| 1917 | ConnectStatusHelper(MockRead("HTTP/1.1 305 Use Proxy\r\n")); |
| 1918 | } |
| 1919 | |
| 1920 | TEST_F(HttpNetworkTransactionTest, ConnectStatus306) { |
| 1921 | ConnectStatusHelper(MockRead("HTTP/1.1 306\r\n")); |
| 1922 | } |
| 1923 | |
| 1924 | TEST_F(HttpNetworkTransactionTest, ConnectStatus307) { |
| 1925 | ConnectStatusHelper(MockRead("HTTP/1.1 307 Temporary Redirect\r\n")); |
| 1926 | } |
| 1927 | |
| 1928 | TEST_F(HttpNetworkTransactionTest, ConnectStatus400) { |
| 1929 | ConnectStatusHelper(MockRead("HTTP/1.1 400 Bad Request\r\n")); |
| 1930 | } |
| 1931 | |
| 1932 | TEST_F(HttpNetworkTransactionTest, ConnectStatus401) { |
| 1933 | ConnectStatusHelper(MockRead("HTTP/1.1 401 Unauthorized\r\n")); |
| 1934 | } |
| 1935 | |
| 1936 | TEST_F(HttpNetworkTransactionTest, ConnectStatus402) { |
| 1937 | ConnectStatusHelper(MockRead("HTTP/1.1 402 Payment Required\r\n")); |
| 1938 | } |
| 1939 | |
| 1940 | TEST_F(HttpNetworkTransactionTest, ConnectStatus403) { |
| 1941 | ConnectStatusHelper(MockRead("HTTP/1.1 403 Forbidden\r\n")); |
| 1942 | } |
| 1943 | |
| 1944 | TEST_F(HttpNetworkTransactionTest, ConnectStatus404) { |
| 1945 | ConnectStatusHelper(MockRead("HTTP/1.1 404 Not Found\r\n")); |
| 1946 | } |
| 1947 | |
| 1948 | TEST_F(HttpNetworkTransactionTest, ConnectStatus405) { |
| 1949 | ConnectStatusHelper(MockRead("HTTP/1.1 405 Method Not Allowed\r\n")); |
| 1950 | } |
| 1951 | |
| 1952 | TEST_F(HttpNetworkTransactionTest, ConnectStatus406) { |
| 1953 | ConnectStatusHelper(MockRead("HTTP/1.1 406 Not Acceptable\r\n")); |
| 1954 | } |
| 1955 | |
| 1956 | TEST_F(HttpNetworkTransactionTest, ConnectStatus407) { |
| 1957 | ConnectStatusHelperWithExpectedStatus( |
| 1958 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 1959 | ERR_PROXY_AUTH_UNSUPPORTED); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | TEST_F(HttpNetworkTransactionTest, ConnectStatus408) { |
| 1963 | ConnectStatusHelper(MockRead("HTTP/1.1 408 Request Timeout\r\n")); |
| 1964 | } |
| 1965 | |
| 1966 | TEST_F(HttpNetworkTransactionTest, ConnectStatus409) { |
| 1967 | ConnectStatusHelper(MockRead("HTTP/1.1 409 Conflict\r\n")); |
| 1968 | } |
| 1969 | |
| 1970 | TEST_F(HttpNetworkTransactionTest, ConnectStatus410) { |
| 1971 | ConnectStatusHelper(MockRead("HTTP/1.1 410 Gone\r\n")); |
| 1972 | } |
| 1973 | |
| 1974 | TEST_F(HttpNetworkTransactionTest, ConnectStatus411) { |
| 1975 | ConnectStatusHelper(MockRead("HTTP/1.1 411 Length Required\r\n")); |
| 1976 | } |
| 1977 | |
| 1978 | TEST_F(HttpNetworkTransactionTest, ConnectStatus412) { |
| 1979 | ConnectStatusHelper(MockRead("HTTP/1.1 412 Precondition Failed\r\n")); |
| 1980 | } |
| 1981 | |
| 1982 | TEST_F(HttpNetworkTransactionTest, ConnectStatus413) { |
| 1983 | ConnectStatusHelper(MockRead("HTTP/1.1 413 Request Entity Too Large\r\n")); |
| 1984 | } |
| 1985 | |
| 1986 | TEST_F(HttpNetworkTransactionTest, ConnectStatus414) { |
| 1987 | ConnectStatusHelper(MockRead("HTTP/1.1 414 Request-URI Too Long\r\n")); |
| 1988 | } |
| 1989 | |
| 1990 | TEST_F(HttpNetworkTransactionTest, ConnectStatus415) { |
| 1991 | ConnectStatusHelper(MockRead("HTTP/1.1 415 Unsupported Media Type\r\n")); |
| 1992 | } |
| 1993 | |
| 1994 | TEST_F(HttpNetworkTransactionTest, ConnectStatus416) { |
| 1995 | ConnectStatusHelper( |
| 1996 | MockRead("HTTP/1.1 416 Requested Range Not Satisfiable\r\n")); |
| 1997 | } |
| 1998 | |
| 1999 | TEST_F(HttpNetworkTransactionTest, ConnectStatus417) { |
| 2000 | ConnectStatusHelper(MockRead("HTTP/1.1 417 Expectation Failed\r\n")); |
| 2001 | } |
| 2002 | |
| 2003 | TEST_F(HttpNetworkTransactionTest, ConnectStatus500) { |
| 2004 | ConnectStatusHelper(MockRead("HTTP/1.1 500 Internal Server Error\r\n")); |
| 2005 | } |
| 2006 | |
| 2007 | TEST_F(HttpNetworkTransactionTest, ConnectStatus501) { |
| 2008 | ConnectStatusHelper(MockRead("HTTP/1.1 501 Not Implemented\r\n")); |
| 2009 | } |
| 2010 | |
| 2011 | TEST_F(HttpNetworkTransactionTest, ConnectStatus502) { |
| 2012 | ConnectStatusHelper(MockRead("HTTP/1.1 502 Bad Gateway\r\n")); |
| 2013 | } |
| 2014 | |
| 2015 | TEST_F(HttpNetworkTransactionTest, ConnectStatus503) { |
| 2016 | ConnectStatusHelper(MockRead("HTTP/1.1 503 Service Unavailable\r\n")); |
| 2017 | } |
| 2018 | |
| 2019 | TEST_F(HttpNetworkTransactionTest, ConnectStatus504) { |
| 2020 | ConnectStatusHelper(MockRead("HTTP/1.1 504 Gateway Timeout\r\n")); |
| 2021 | } |
| 2022 | |
| 2023 | TEST_F(HttpNetworkTransactionTest, ConnectStatus505) { |
| 2024 | ConnectStatusHelper(MockRead("HTTP/1.1 505 HTTP Version Not Supported\r\n")); |
| 2025 | } |
| 2026 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2027 | // Test the flow when both the proxy server AND origin server require |
| 2028 | // authentication. Again, this uses basic auth for both since that is |
| 2029 | // the simplest to mock. |
| 2030 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyThenServer) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2031 | SessionDependencies session_deps(CreateFixedProxyService("myproxy:70")); |
[email protected] | db8f44c | 2008-12-13 04:52:01 | [diff] [blame] | 2032 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2033 | // Configure against proxy server "myproxy:70". |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2034 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 2035 | CreateSession(&session_deps))); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2036 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2037 | HttpRequestInfo request; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2038 | request.method = "GET"; |
| 2039 | request.url = GURL("http://www.google.com/"); |
| 2040 | request.load_flags = 0; |
| 2041 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2042 | MockWrite data_writes1[] = { |
| 2043 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 2044 | "Host: www.google.com\r\n" |
| 2045 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 2046 | }; |
| 2047 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2048 | MockRead data_reads1[] = { |
| 2049 | MockRead("HTTP/1.0 407 Unauthorized\r\n"), |
| 2050 | // Give a couple authenticate options (only the middle one is actually |
| 2051 | // supported). |
[email protected] | 22927ad | 2009-09-21 19:56:19 | [diff] [blame] | 2052 | MockRead("Proxy-Authenticate: Basic invalid\r\n"), // Malformed. |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2053 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 2054 | MockRead("Proxy-Authenticate: UNSUPPORTED realm=\"FOO\"\r\n"), |
| 2055 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2056 | // Large content-length -- won't matter, as connection will be reset. |
| 2057 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2058 | MockRead(false, ERR_FAILED), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2059 | }; |
| 2060 | |
| 2061 | // After calling trans->RestartWithAuth() the first time, this is the |
| 2062 | // request we should be issuing -- the final header line contains the |
| 2063 | // proxy's credentials. |
| 2064 | MockWrite data_writes2[] = { |
| 2065 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 2066 | "Host: www.google.com\r\n" |
| 2067 | "Proxy-Connection: keep-alive\r\n" |
| 2068 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 2069 | }; |
| 2070 | |
| 2071 | // Now the proxy server lets the request pass through to origin server. |
| 2072 | // The origin server responds with a 401. |
| 2073 | MockRead data_reads2[] = { |
| 2074 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 2075 | // Note: We are using the same realm-name as the proxy server. This is |
| 2076 | // completely valid, as realms are unique across hosts. |
| 2077 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 2078 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2079 | MockRead("Content-Length: 2000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2080 | MockRead(false, ERR_FAILED), // Won't be reached. |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2081 | }; |
| 2082 | |
| 2083 | // After calling trans->RestartWithAuth() the second time, we should send |
| 2084 | // the credentials for both the proxy and origin server. |
| 2085 | MockWrite data_writes3[] = { |
| 2086 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 2087 | "Host: www.google.com\r\n" |
| 2088 | "Proxy-Connection: keep-alive\r\n" |
| 2089 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n" |
| 2090 | "Authorization: Basic Zm9vMjpiYXIy\r\n\r\n"), |
| 2091 | }; |
| 2092 | |
| 2093 | // Lastly we get the desired content. |
| 2094 | MockRead data_reads3[] = { |
| 2095 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 2096 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2097 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2098 | MockRead(false, OK), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2099 | }; |
| 2100 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 2101 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 2102 | data_writes1, arraysize(data_writes1)); |
| 2103 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 2104 | data_writes2, arraysize(data_writes2)); |
| 2105 | StaticSocketDataProvider data3(data_reads3, arraysize(data_reads3), |
| 2106 | data_writes3, arraysize(data_writes3)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 2107 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 2108 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 2109 | session_deps.socket_factory.AddSocketDataProvider(&data3); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2110 | |
| 2111 | TestCompletionCallback callback1; |
| 2112 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 2113 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2114 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2115 | |
| 2116 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2117 | EXPECT_EQ(OK, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2118 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2119 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2120 | EXPECT_FALSE(response == NULL); |
| 2121 | |
| 2122 | // The password prompt info should have been set in response->auth_challenge. |
| 2123 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 2124 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 2125 | EXPECT_EQ(L"myproxy:70", response->auth_challenge->host_and_port); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2126 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 2127 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 2128 | |
| 2129 | TestCompletionCallback callback2; |
| 2130 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 2131 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2132 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2133 | |
| 2134 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2135 | EXPECT_EQ(OK, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2136 | |
| 2137 | response = trans->GetResponseInfo(); |
| 2138 | EXPECT_FALSE(response == NULL); |
| 2139 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 2140 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 2141 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2142 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 2143 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 2144 | |
| 2145 | TestCompletionCallback callback3; |
| 2146 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 2147 | rv = trans->RestartWithAuth(kFoo2, kBar2, &callback3); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2148 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2149 | |
| 2150 | rv = callback3.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2151 | EXPECT_EQ(OK, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2152 | |
| 2153 | response = trans->GetResponseInfo(); |
| 2154 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 2155 | EXPECT_EQ(100, response->headers->GetContentLength()); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2156 | } |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 2157 | |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 2158 | // For the NTLM implementation using SSPI, we skip the NTLM tests since we |
| 2159 | // can't hook into its internals to cause it to generate predictable NTLM |
| 2160 | // authorization headers. |
| 2161 | #if defined(NTLM_PORTABLE) |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2162 | // The NTLM authentication unit tests were generated by capturing the HTTP |
| 2163 | // requests and responses using Fiddler 2 and inspecting the generated random |
| 2164 | // bytes in the debugger. |
| 2165 | |
| 2166 | // Enter the correct password and authenticate successfully. |
| 2167 | TEST_F(HttpNetworkTransactionTest, NTLMAuth1) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2168 | HttpAuthHandlerNTLM::ScopedProcSetter proc_setter(MockGenerateRandom1, |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 2169 | MockGetHostName); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2170 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 2171 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 2172 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2173 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2174 | HttpRequestInfo request; |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2175 | request.method = "GET"; |
| 2176 | request.url = GURL("http://172.22.68.17/kids/login.aspx"); |
| 2177 | request.load_flags = 0; |
| 2178 | |
| 2179 | MockWrite data_writes1[] = { |
| 2180 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 2181 | "Host: 172.22.68.17\r\n" |
| 2182 | "Connection: keep-alive\r\n\r\n"), |
| 2183 | }; |
| 2184 | |
| 2185 | MockRead data_reads1[] = { |
| 2186 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 2187 | // Negotiate and NTLM are often requested together. However, we only want |
| 2188 | // to test NTLM. Since Negotiate is preferred over NTLM, we have to skip |
| 2189 | // the header that requests Negotiate for this test. |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2190 | MockRead("WWW-Authenticate: NTLM\r\n"), |
| 2191 | MockRead("Connection: close\r\n"), |
| 2192 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 2193 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2194 | // Missing content -- won't matter, as connection will be reset. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2195 | MockRead(false, ERR_UNEXPECTED), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2196 | }; |
| 2197 | |
| 2198 | MockWrite data_writes2[] = { |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2199 | // After restarting with a null identity, this is the |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2200 | // request we should be issuing -- the final header line contains a Type |
| 2201 | // 1 message. |
| 2202 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 2203 | "Host: 172.22.68.17\r\n" |
| 2204 | "Connection: keep-alive\r\n" |
| 2205 | "Authorization: NTLM " |
| 2206 | "TlRMTVNTUAABAAAAB4IIAAAAAAAAAAAAAAAAAAAAAAA=\r\n\r\n"), |
| 2207 | |
| 2208 | // After calling trans->RestartWithAuth(), we should send a Type 3 message |
| 2209 | // (the credentials for the origin server). The second request continues |
| 2210 | // on the same connection. |
| 2211 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 2212 | "Host: 172.22.68.17\r\n" |
| 2213 | "Connection: keep-alive\r\n" |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2214 | "Authorization: NTLM TlRMTVNTUAADAAAAGAAYAGgAAAAYABgAgA" |
| 2215 | "AAAAAAAABAAAAAGAAYAEAAAAAQABAAWAAAAAAAAAAAAAAABYIIAHQA" |
| 2216 | "ZQBzAHQAaQBuAGcALQBuAHQAbABtAFcAVABDAC0AVwBJAE4ANwBVKW" |
| 2217 | "Yma5xzVAAAAAAAAAAAAAAAAAAAAACH+gWcm+YsP9Tqb9zCR3WAeZZX" |
| 2218 | "ahlhx5I=\r\n\r\n"), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2219 | }; |
| 2220 | |
| 2221 | MockRead data_reads2[] = { |
| 2222 | // The origin server responds with a Type 2 message. |
| 2223 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
| 2224 | MockRead("WWW-Authenticate: NTLM " |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2225 | "TlRMTVNTUAACAAAADAAMADgAAAAFgokCjGpMpPGlYKkAAAAAAAAAALo" |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2226 | "AugBEAAAABQEoCgAAAA9HAE8ATwBHAEwARQACAAwARwBPAE8ARwBMAE" |
| 2227 | "UAAQAaAEEASwBFAEUAUwBBAFIAQQAtAEMATwBSAFAABAAeAGMAbwByA" |
| 2228 | "HAALgBnAG8AbwBnAGwAZQAuAGMAbwBtAAMAQABhAGsAZQBlAHMAYQBy" |
| 2229 | "AGEALQBjAG8AcgBwAC4AYQBkAC4AYwBvAHIAcAAuAGcAbwBvAGcAbAB" |
| 2230 | "lAC4AYwBvAG0ABQAeAGMAbwByAHAALgBnAG8AbwBnAGwAZQAuAGMAbw" |
| 2231 | "BtAAAAAAA=\r\n"), |
| 2232 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 2233 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2234 | MockRead("You are not authorized to view this page\r\n"), |
| 2235 | |
| 2236 | // Lastly we get the desired content. |
| 2237 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 2238 | MockRead("Content-Type: text/html; charset=utf-8\r\n"), |
| 2239 | MockRead("Content-Length: 13\r\n\r\n"), |
| 2240 | MockRead("Please Login\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2241 | MockRead(false, OK), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2242 | }; |
| 2243 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 2244 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 2245 | data_writes1, arraysize(data_writes1)); |
| 2246 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 2247 | data_writes2, arraysize(data_writes2)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 2248 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 2249 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2250 | |
| 2251 | TestCompletionCallback callback1; |
| 2252 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 2253 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2254 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2255 | |
| 2256 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2257 | EXPECT_EQ(OK, rv); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2258 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2259 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
| 2260 | TestCompletionCallback callback2; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 2261 | rv = trans->RestartWithAuth(string16(), string16(), &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2262 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2263 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2264 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2265 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 2266 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2267 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 2268 | ASSERT_FALSE(response == NULL); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2269 | |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 2270 | // The password prompt info should have been set in |
| 2271 | // response->auth_challenge. |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2272 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 2273 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 2274 | 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] | 2275 | EXPECT_EQ(L"", response->auth_challenge->realm); |
| 2276 | EXPECT_EQ(L"ntlm", response->auth_challenge->scheme); |
| 2277 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2278 | TestCompletionCallback callback3; |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2279 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 2280 | rv = trans->RestartWithAuth(kTestingNTLM, kTestingNTLM, &callback3); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2281 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2282 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2283 | rv = callback3.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2284 | EXPECT_EQ(OK, rv); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2285 | |
| 2286 | response = trans->GetResponseInfo(); |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 2287 | ASSERT_FALSE(response == NULL); |
| 2288 | |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2289 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 2290 | EXPECT_EQ(13, response->headers->GetContentLength()); |
| 2291 | } |
| 2292 | |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2293 | // Enter a wrong password, and then the correct one. |
| 2294 | TEST_F(HttpNetworkTransactionTest, NTLMAuth2) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2295 | HttpAuthHandlerNTLM::ScopedProcSetter proc_setter(MockGenerateRandom2, |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 2296 | MockGetHostName); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2297 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 2298 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 2299 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2300 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2301 | HttpRequestInfo request; |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2302 | request.method = "GET"; |
| 2303 | request.url = GURL("http://172.22.68.17/kids/login.aspx"); |
| 2304 | request.load_flags = 0; |
| 2305 | |
| 2306 | MockWrite data_writes1[] = { |
| 2307 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 2308 | "Host: 172.22.68.17\r\n" |
| 2309 | "Connection: keep-alive\r\n\r\n"), |
| 2310 | }; |
| 2311 | |
| 2312 | MockRead data_reads1[] = { |
| 2313 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 2314 | // Negotiate and NTLM are often requested together. However, we only want |
| 2315 | // to test NTLM. Since Negotiate is preferred over NTLM, we have to skip |
| 2316 | // the header that requests Negotiate for this test. |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2317 | MockRead("WWW-Authenticate: NTLM\r\n"), |
| 2318 | MockRead("Connection: close\r\n"), |
| 2319 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 2320 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2321 | // Missing content -- won't matter, as connection will be reset. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2322 | MockRead(false, ERR_UNEXPECTED), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2323 | }; |
| 2324 | |
| 2325 | MockWrite data_writes2[] = { |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2326 | // After restarting with a null identity, this is the |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2327 | // request we should be issuing -- the final header line contains a Type |
| 2328 | // 1 message. |
| 2329 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 2330 | "Host: 172.22.68.17\r\n" |
| 2331 | "Connection: keep-alive\r\n" |
| 2332 | "Authorization: NTLM " |
| 2333 | "TlRMTVNTUAABAAAAB4IIAAAAAAAAAAAAAAAAAAAAAAA=\r\n\r\n"), |
| 2334 | |
| 2335 | // After calling trans->RestartWithAuth(), we should send a Type 3 message |
| 2336 | // (the credentials for the origin server). The second request continues |
| 2337 | // on the same connection. |
| 2338 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 2339 | "Host: 172.22.68.17\r\n" |
| 2340 | "Connection: keep-alive\r\n" |
| 2341 | "Authorization: NTLM TlRMTVNTUAADAAAAGAAYAGgAAAAYABgAgA" |
| 2342 | "AAAAAAAABAAAAAGAAYAEAAAAAQABAAWAAAAAAAAAAAAAAABYIIAHQA" |
| 2343 | "ZQBzAHQAaQBuAGcALQBuAHQAbABtAFcAVABDAC0AVwBJAE4ANwCWeY" |
| 2344 | "XnSZNwoQAAAAAAAAAAAAAAAAAAAADLa34/phTTKzNTWdub+uyFleOj" |
| 2345 | "4Ww7b7E=\r\n\r\n"), |
| 2346 | }; |
| 2347 | |
| 2348 | MockRead data_reads2[] = { |
| 2349 | // The origin server responds with a Type 2 message. |
| 2350 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
| 2351 | MockRead("WWW-Authenticate: NTLM " |
| 2352 | "TlRMTVNTUAACAAAADAAMADgAAAAFgokCbVWUZezVGpAAAAAAAAAAALo" |
| 2353 | "AugBEAAAABQEoCgAAAA9HAE8ATwBHAEwARQACAAwARwBPAE8ARwBMAE" |
| 2354 | "UAAQAaAEEASwBFAEUAUwBBAFIAQQAtAEMATwBSAFAABAAeAGMAbwByA" |
| 2355 | "HAALgBnAG8AbwBnAGwAZQAuAGMAbwBtAAMAQABhAGsAZQBlAHMAYQBy" |
| 2356 | "AGEALQBjAG8AcgBwAC4AYQBkAC4AYwBvAHIAcAAuAGcAbwBvAGcAbAB" |
| 2357 | "lAC4AYwBvAG0ABQAeAGMAbwByAHAALgBnAG8AbwBnAGwAZQAuAGMAbw" |
| 2358 | "BtAAAAAAA=\r\n"), |
| 2359 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 2360 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2361 | MockRead("You are not authorized to view this page\r\n"), |
| 2362 | |
| 2363 | // Wrong password. |
| 2364 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2365 | MockRead("WWW-Authenticate: NTLM\r\n"), |
| 2366 | MockRead("Connection: close\r\n"), |
| 2367 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 2368 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2369 | // Missing content -- won't matter, as connection will be reset. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2370 | MockRead(false, ERR_UNEXPECTED), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2371 | }; |
| 2372 | |
| 2373 | MockWrite data_writes3[] = { |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2374 | // After restarting with a null identity, this is the |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2375 | // request we should be issuing -- the final header line contains a Type |
| 2376 | // 1 message. |
| 2377 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 2378 | "Host: 172.22.68.17\r\n" |
| 2379 | "Connection: keep-alive\r\n" |
| 2380 | "Authorization: NTLM " |
| 2381 | "TlRMTVNTUAABAAAAB4IIAAAAAAAAAAAAAAAAAAAAAAA=\r\n\r\n"), |
| 2382 | |
| 2383 | // After calling trans->RestartWithAuth(), we should send a Type 3 message |
| 2384 | // (the credentials for the origin server). The second request continues |
| 2385 | // on the same connection. |
| 2386 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 2387 | "Host: 172.22.68.17\r\n" |
| 2388 | "Connection: keep-alive\r\n" |
| 2389 | "Authorization: NTLM TlRMTVNTUAADAAAAGAAYAGgAAAAYABgAgA" |
| 2390 | "AAAAAAAABAAAAAGAAYAEAAAAAQABAAWAAAAAAAAAAAAAAABYIIAHQA" |
| 2391 | "ZQBzAHQAaQBuAGcALQBuAHQAbABtAFcAVABDAC0AVwBJAE4ANwBO54" |
| 2392 | "dFMVvTHwAAAAAAAAAAAAAAAAAAAACS7sT6Uzw7L0L//WUqlIaVWpbI" |
| 2393 | "+4MUm7c=\r\n\r\n"), |
| 2394 | }; |
| 2395 | |
| 2396 | MockRead data_reads3[] = { |
| 2397 | // The origin server responds with a Type 2 message. |
| 2398 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
| 2399 | MockRead("WWW-Authenticate: NTLM " |
| 2400 | "TlRMTVNTUAACAAAADAAMADgAAAAFgokCL24VN8dgOR8AAAAAAAAAALo" |
| 2401 | "AugBEAAAABQEoCgAAAA9HAE8ATwBHAEwARQACAAwARwBPAE8ARwBMAE" |
| 2402 | "UAAQAaAEEASwBFAEUAUwBBAFIAQQAtAEMATwBSAFAABAAeAGMAbwByA" |
| 2403 | "HAALgBnAG8AbwBnAGwAZQAuAGMAbwBtAAMAQABhAGsAZQBlAHMAYQBy" |
| 2404 | "AGEALQBjAG8AcgBwAC4AYQBkAC4AYwBvAHIAcAAuAGcAbwBvAGcAbAB" |
| 2405 | "lAC4AYwBvAG0ABQAeAGMAbwByAHAALgBnAG8AbwBnAGwAZQAuAGMAbw" |
| 2406 | "BtAAAAAAA=\r\n"), |
| 2407 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 2408 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2409 | MockRead("You are not authorized to view this page\r\n"), |
| 2410 | |
| 2411 | // Lastly we get the desired content. |
| 2412 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 2413 | MockRead("Content-Type: text/html; charset=utf-8\r\n"), |
| 2414 | MockRead("Content-Length: 13\r\n\r\n"), |
| 2415 | MockRead("Please Login\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2416 | MockRead(false, OK), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2417 | }; |
| 2418 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 2419 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 2420 | data_writes1, arraysize(data_writes1)); |
| 2421 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 2422 | data_writes2, arraysize(data_writes2)); |
| 2423 | StaticSocketDataProvider data3(data_reads3, arraysize(data_reads3), |
| 2424 | data_writes3, arraysize(data_writes3)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 2425 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 2426 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 2427 | session_deps.socket_factory.AddSocketDataProvider(&data3); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2428 | |
| 2429 | TestCompletionCallback callback1; |
| 2430 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 2431 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2432 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2433 | |
| 2434 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2435 | EXPECT_EQ(OK, rv); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2436 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2437 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2438 | TestCompletionCallback callback2; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 2439 | rv = trans->RestartWithAuth(string16(), string16(), &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2440 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2441 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2442 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2443 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2444 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2445 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2446 | EXPECT_FALSE(response == NULL); |
| 2447 | |
| 2448 | // The password prompt info should have been set in response->auth_challenge. |
| 2449 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 2450 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 2451 | 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] | 2452 | EXPECT_EQ(L"", response->auth_challenge->realm); |
| 2453 | EXPECT_EQ(L"ntlm", response->auth_challenge->scheme); |
| 2454 | |
| 2455 | TestCompletionCallback callback3; |
| 2456 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2457 | // Enter the wrong password. |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 2458 | rv = trans->RestartWithAuth(kTestingNTLM, kWrongPassword, &callback3); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2459 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2460 | |
| 2461 | rv = callback3.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2462 | EXPECT_EQ(OK, rv); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2463 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2464 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
| 2465 | TestCompletionCallback callback4; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 2466 | rv = trans->RestartWithAuth(string16(), string16(), &callback4); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2467 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2468 | rv = callback4.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2469 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2470 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 2471 | |
| 2472 | response = trans->GetResponseInfo(); |
| 2473 | EXPECT_FALSE(response == NULL); |
| 2474 | |
| 2475 | // The password prompt info should have been set in response->auth_challenge. |
| 2476 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 2477 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 2478 | 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] | 2479 | EXPECT_EQ(L"", response->auth_challenge->realm); |
| 2480 | EXPECT_EQ(L"ntlm", response->auth_challenge->scheme); |
| 2481 | |
| 2482 | TestCompletionCallback callback5; |
| 2483 | |
| 2484 | // Now enter the right password. |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 2485 | rv = trans->RestartWithAuth(kTestingNTLM, kTestingNTLM, &callback5); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2486 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2487 | |
| 2488 | rv = callback5.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2489 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2490 | |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2491 | response = trans->GetResponseInfo(); |
| 2492 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 2493 | EXPECT_EQ(13, response->headers->GetContentLength()); |
| 2494 | } |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 2495 | #endif // NTLM_PORTABLE |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2496 | |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 2497 | // Test reading a server response which has only headers, and no body. |
| 2498 | // After some maximum number of bytes is consumed, the transaction should |
| 2499 | // fail with ERR_RESPONSE_HEADERS_TOO_BIG. |
| 2500 | TEST_F(HttpNetworkTransactionTest, LargeHeadersNoBody) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2501 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 2502 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 2503 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 2504 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2505 | HttpRequestInfo request; |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 2506 | request.method = "GET"; |
| 2507 | request.url = GURL("http://www.google.com/"); |
| 2508 | request.load_flags = 0; |
| 2509 | |
[email protected] | b75b7b2f | 2009-10-06 00:54:53 | [diff] [blame] | 2510 | // Respond with 300 kb of headers (we should fail after 256 kb). |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 2511 | std::string large_headers_string; |
[email protected] | b75b7b2f | 2009-10-06 00:54:53 | [diff] [blame] | 2512 | FillLargeHeadersString(&large_headers_string, 300 * 1024); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 2513 | |
| 2514 | MockRead data_reads[] = { |
| 2515 | MockRead("HTTP/1.0 200 OK\r\n"), |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 2516 | MockRead(true, large_headers_string.data(), large_headers_string.size()), |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 2517 | MockRead("\r\nBODY"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2518 | MockRead(false, OK), |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 2519 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 2520 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 2521 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 2522 | |
| 2523 | TestCompletionCallback callback; |
| 2524 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 2525 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2526 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 2527 | |
| 2528 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2529 | EXPECT_EQ(ERR_RESPONSE_HEADERS_TOO_BIG, rv); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 2530 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2531 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 2532 | EXPECT_TRUE(response == NULL); |
| 2533 | } |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 2534 | |
| 2535 | // Make sure that we don't try to reuse a TCPClientSocket when failing to |
| 2536 | // establish tunnel. |
| 2537 | // http://code.google.com/p/chromium/issues/detail?id=3772 |
| 2538 | TEST_F(HttpNetworkTransactionTest, DontRecycleTCPSocketForSSLTunnel) { |
| 2539 | // Configure against proxy server "myproxy:70". |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2540 | SessionDependencies session_deps(CreateFixedProxyService("myproxy:70")); |
[email protected] | db8f44c | 2008-12-13 04:52:01 | [diff] [blame] | 2541 | |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2542 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 2543 | |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 2544 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 2545 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2546 | HttpRequestInfo request; |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 2547 | request.method = "GET"; |
| 2548 | request.url = GURL("https://www.google.com/"); |
| 2549 | request.load_flags = 0; |
| 2550 | |
| 2551 | // Since we have proxy, should try to establish tunnel. |
| 2552 | MockWrite data_writes1[] = { |
| 2553 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 2554 | "Host: www.google.com\r\n" |
| 2555 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 2556 | }; |
| 2557 | |
[email protected] | 77848d1 | 2008-11-14 00:00:22 | [diff] [blame] | 2558 | // The proxy responds to the connect with a 404, using a persistent |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 2559 | // connection. Usually a proxy would return 501 (not implemented), |
| 2560 | // or 200 (tunnel established). |
| 2561 | MockRead data_reads1[] = { |
| 2562 | MockRead("HTTP/1.1 404 Not Found\r\n"), |
| 2563 | MockRead("Content-Length: 10\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2564 | MockRead(false, ERR_UNEXPECTED), // Should not be reached. |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 2565 | }; |
| 2566 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 2567 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 2568 | data_writes1, arraysize(data_writes1)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 2569 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 2570 | |
| 2571 | TestCompletionCallback callback1; |
| 2572 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 2573 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2574 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 2575 | |
| 2576 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2577 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, rv); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 2578 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2579 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2580 | EXPECT_TRUE(response == NULL); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 2581 | |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 2582 | // Empty the current queue. This is necessary because idle sockets are |
| 2583 | // added to the connection pool asynchronously with a PostTask. |
| 2584 | MessageLoop::current()->RunAllPending(); |
| 2585 | |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 2586 | // We now check to make sure the TCPClientSocket was not added back to |
| 2587 | // the pool. |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 2588 | EXPECT_EQ(0, session->tcp_socket_pool()->IdleSocketCount()); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 2589 | trans.reset(); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 2590 | MessageLoop::current()->RunAllPending(); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 2591 | // Make sure that the socket didn't get recycled after calling the destructor. |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 2592 | EXPECT_EQ(0, session->tcp_socket_pool()->IdleSocketCount()); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 2593 | } |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 2594 | |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 2595 | // Make sure that we recycle a socket after reading all of the response body. |
| 2596 | TEST_F(HttpNetworkTransactionTest, RecycleSocket) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2597 | SessionDependencies session_deps; |
| 2598 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 2599 | |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 2600 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 2601 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2602 | HttpRequestInfo request; |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 2603 | request.method = "GET"; |
| 2604 | request.url = GURL("http://www.google.com/"); |
| 2605 | request.load_flags = 0; |
| 2606 | |
| 2607 | MockRead data_reads[] = { |
| 2608 | // A part of the response body is received with the response headers. |
| 2609 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\nhel"), |
| 2610 | // The rest of the response body is received in two parts. |
| 2611 | MockRead("lo"), |
| 2612 | MockRead(" world"), |
| 2613 | MockRead("junk"), // Should not be read!! |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2614 | MockRead(false, OK), |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 2615 | }; |
| 2616 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 2617 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 2618 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 2619 | |
| 2620 | TestCompletionCallback callback; |
| 2621 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 2622 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2623 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 2624 | |
| 2625 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2626 | EXPECT_EQ(OK, rv); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 2627 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2628 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 2629 | EXPECT_TRUE(response != NULL); |
| 2630 | |
| 2631 | EXPECT_TRUE(response->headers != NULL); |
| 2632 | std::string status_line = response->headers->GetStatusLine(); |
| 2633 | EXPECT_EQ("HTTP/1.1 200 OK", status_line); |
| 2634 | |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 2635 | EXPECT_EQ(0, session->tcp_socket_pool()->IdleSocketCount()); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 2636 | |
| 2637 | std::string response_data; |
| 2638 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2639 | EXPECT_EQ(OK, rv); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 2640 | EXPECT_EQ("hello world", response_data); |
| 2641 | |
| 2642 | // Empty the current queue. This is necessary because idle sockets are |
| 2643 | // added to the connection pool asynchronously with a PostTask. |
| 2644 | MessageLoop::current()->RunAllPending(); |
| 2645 | |
| 2646 | // 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] | 2647 | EXPECT_EQ(1, session->tcp_socket_pool()->IdleSocketCount()); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 2648 | } |
| 2649 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 2650 | // Make sure that we recycle a SSL socket after reading all of the response |
| 2651 | // body. |
| 2652 | TEST_F(HttpNetworkTransactionTest, RecycleSSLSocket) { |
| 2653 | SessionDependencies session_deps; |
| 2654 | HttpRequestInfo request; |
| 2655 | request.method = "GET"; |
| 2656 | request.url = GURL("https://www.google.com/"); |
| 2657 | request.load_flags = 0; |
| 2658 | |
| 2659 | MockWrite data_writes[] = { |
| 2660 | MockWrite("GET / HTTP/1.1\r\n" |
| 2661 | "Host: www.google.com\r\n" |
| 2662 | "Connection: keep-alive\r\n\r\n"), |
| 2663 | }; |
| 2664 | |
| 2665 | MockRead data_reads[] = { |
| 2666 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 2667 | MockRead("Content-Length: 11\r\n\r\n"), |
| 2668 | MockRead("hello world"), |
| 2669 | MockRead(false, OK), |
| 2670 | }; |
| 2671 | |
| 2672 | SSLSocketDataProvider ssl(true, OK); |
| 2673 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 2674 | |
| 2675 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 2676 | data_writes, arraysize(data_writes)); |
| 2677 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 2678 | |
| 2679 | TestCompletionCallback callback; |
| 2680 | |
| 2681 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 2682 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 2683 | |
| 2684 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 2685 | |
| 2686 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2687 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 2688 | |
| 2689 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 2690 | ASSERT_TRUE(response != NULL); |
| 2691 | ASSERT_TRUE(response->headers != NULL); |
| 2692 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 2693 | |
| 2694 | EXPECT_EQ(0, session->tcp_socket_pool()->IdleSocketCount()); |
| 2695 | |
| 2696 | std::string response_data; |
| 2697 | rv = ReadTransaction(trans.get(), &response_data); |
| 2698 | EXPECT_EQ(OK, rv); |
| 2699 | EXPECT_EQ("hello world", response_data); |
| 2700 | |
| 2701 | // Empty the current queue. This is necessary because idle sockets are |
| 2702 | // added to the connection pool asynchronously with a PostTask. |
| 2703 | MessageLoop::current()->RunAllPending(); |
| 2704 | |
| 2705 | // We now check to make sure the socket was added back to the pool. |
| 2706 | EXPECT_EQ(1, session->ssl_socket_pool()->IdleSocketCount()); |
| 2707 | } |
| 2708 | |
| 2709 | // Grab a SSL socket, use it, and put it back into the pool. Then, reuse it |
| 2710 | // from the pool and make sure that we recover okay. |
| 2711 | TEST_F(HttpNetworkTransactionTest, RecycleDeadSSLSocket) { |
| 2712 | SessionDependencies session_deps; |
| 2713 | HttpRequestInfo request; |
| 2714 | request.method = "GET"; |
| 2715 | request.url = GURL("https://www.google.com/"); |
| 2716 | request.load_flags = 0; |
| 2717 | |
| 2718 | MockWrite data_writes[] = { |
| 2719 | MockWrite("GET / HTTP/1.1\r\n" |
| 2720 | "Host: www.google.com\r\n" |
| 2721 | "Connection: keep-alive\r\n\r\n"), |
| 2722 | MockWrite("GET / 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_reads[] = { |
| 2728 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 2729 | MockRead("Content-Length: 11\r\n\r\n"), |
| 2730 | MockRead(false, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| 2731 | MockRead("hello world"), |
| 2732 | MockRead(true, 0, 0) // EOF |
| 2733 | }; |
| 2734 | |
| 2735 | SSLSocketDataProvider ssl(true, OK); |
| 2736 | SSLSocketDataProvider ssl2(true, OK); |
| 2737 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 2738 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl2); |
| 2739 | |
| 2740 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 2741 | data_writes, arraysize(data_writes)); |
| 2742 | StaticSocketDataProvider data2(data_reads, arraysize(data_reads), |
| 2743 | data_writes, arraysize(data_writes)); |
| 2744 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 2745 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 2746 | |
| 2747 | TestCompletionCallback callback; |
| 2748 | |
| 2749 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 2750 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 2751 | |
| 2752 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 2753 | |
| 2754 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2755 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 2756 | |
| 2757 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 2758 | ASSERT_TRUE(response != NULL); |
| 2759 | ASSERT_TRUE(response->headers != NULL); |
| 2760 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 2761 | |
| 2762 | EXPECT_EQ(0, session->tcp_socket_pool()->IdleSocketCount()); |
| 2763 | |
| 2764 | std::string response_data; |
| 2765 | rv = ReadTransaction(trans.get(), &response_data); |
| 2766 | EXPECT_EQ(OK, rv); |
| 2767 | EXPECT_EQ("hello world", response_data); |
| 2768 | |
| 2769 | // Empty the current queue. This is necessary because idle sockets are |
| 2770 | // added to the connection pool asynchronously with a PostTask. |
| 2771 | MessageLoop::current()->RunAllPending(); |
| 2772 | |
| 2773 | // We now check to make sure the socket was added back to the pool. |
| 2774 | EXPECT_EQ(1, session->ssl_socket_pool()->IdleSocketCount()); |
| 2775 | |
| 2776 | // Now start the second transaction, which should reuse the previous socket. |
| 2777 | |
| 2778 | trans.reset(new HttpNetworkTransaction(session)); |
| 2779 | |
| 2780 | rv = trans->Start(&request, &callback, BoundNetLog()); |
| 2781 | |
| 2782 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2783 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 2784 | |
| 2785 | response = trans->GetResponseInfo(); |
| 2786 | ASSERT_TRUE(response != NULL); |
| 2787 | ASSERT_TRUE(response->headers != NULL); |
| 2788 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 2789 | |
| 2790 | EXPECT_EQ(0, session->tcp_socket_pool()->IdleSocketCount()); |
| 2791 | |
| 2792 | rv = ReadTransaction(trans.get(), &response_data); |
| 2793 | EXPECT_EQ(OK, rv); |
| 2794 | EXPECT_EQ("hello world", response_data); |
| 2795 | |
| 2796 | // Empty the current queue. This is necessary because idle sockets are |
| 2797 | // added to the connection pool asynchronously with a PostTask. |
| 2798 | MessageLoop::current()->RunAllPending(); |
| 2799 | |
| 2800 | // We now check to make sure the socket was added back to the pool. |
| 2801 | EXPECT_EQ(1, session->ssl_socket_pool()->IdleSocketCount()); |
| 2802 | } |
| 2803 | |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 2804 | // Make sure that we recycle a socket after a zero-length response. |
| 2805 | // http://crbug.com/9880 |
| 2806 | TEST_F(HttpNetworkTransactionTest, RecycleSocketAfterZeroContentLength) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2807 | SessionDependencies session_deps; |
| 2808 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 2809 | |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 2810 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 2811 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2812 | HttpRequestInfo request; |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 2813 | request.method = "GET"; |
| 2814 | request.url = GURL("http://www.google.com/csi?v=3&s=web&action=&" |
| 2815 | "tran=undefined&ei=mAXcSeegAo-SMurloeUN&" |
| 2816 | "e=17259,18167,19592,19773,19981,20133,20173,20233&" |
| 2817 | "rt=prt.2642,ol.2649,xjs.2951"); |
| 2818 | request.load_flags = 0; |
| 2819 | |
| 2820 | MockRead data_reads[] = { |
| 2821 | MockRead("HTTP/1.1 204 No Content\r\n" |
| 2822 | "Content-Length: 0\r\n" |
| 2823 | "Content-Type: text/html\r\n\r\n"), |
| 2824 | MockRead("junk"), // Should not be read!! |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2825 | MockRead(false, OK), |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 2826 | }; |
| 2827 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 2828 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 2829 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 2830 | |
| 2831 | TestCompletionCallback callback; |
| 2832 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 2833 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2834 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 2835 | |
| 2836 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2837 | EXPECT_EQ(OK, rv); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 2838 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2839 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 2840 | EXPECT_TRUE(response != NULL); |
| 2841 | |
| 2842 | EXPECT_TRUE(response->headers != NULL); |
| 2843 | std::string status_line = response->headers->GetStatusLine(); |
| 2844 | EXPECT_EQ("HTTP/1.1 204 No Content", status_line); |
| 2845 | |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 2846 | EXPECT_EQ(0, session->tcp_socket_pool()->IdleSocketCount()); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 2847 | |
| 2848 | std::string response_data; |
| 2849 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2850 | EXPECT_EQ(OK, rv); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 2851 | EXPECT_EQ("", response_data); |
| 2852 | |
| 2853 | // Empty the current queue. This is necessary because idle sockets are |
| 2854 | // added to the connection pool asynchronously with a PostTask. |
| 2855 | MessageLoop::current()->RunAllPending(); |
| 2856 | |
| 2857 | // 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] | 2858 | EXPECT_EQ(1, session->tcp_socket_pool()->IdleSocketCount()); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 2859 | } |
| 2860 | |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 2861 | TEST_F(HttpNetworkTransactionTest, ResendRequestOnWriteBodyError) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2862 | HttpRequestInfo request[2]; |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 2863 | // Transaction 1: a GET request that succeeds. The socket is recycled |
| 2864 | // after use. |
| 2865 | request[0].method = "GET"; |
| 2866 | request[0].url = GURL("http://www.google.com/"); |
| 2867 | request[0].load_flags = 0; |
| 2868 | // Transaction 2: a POST request. Reuses the socket kept alive from |
| 2869 | // transaction 1. The first attempts fails when writing the POST data. |
| 2870 | // This causes the transaction to retry with a new socket. The second |
| 2871 | // attempt succeeds. |
| 2872 | request[1].method = "POST"; |
| 2873 | request[1].url = GURL("http://www.google.com/login.cgi"); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2874 | request[1].upload_data = new UploadData; |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 2875 | request[1].upload_data->AppendBytes("foo", 3); |
| 2876 | request[1].load_flags = 0; |
| 2877 | |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2878 | SessionDependencies session_deps; |
| 2879 | scoped_refptr<HttpNetworkSession> session = CreateSession(&session_deps); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 2880 | |
| 2881 | // The first socket is used for transaction 1 and the first attempt of |
| 2882 | // transaction 2. |
| 2883 | |
| 2884 | // The response of transaction 1. |
| 2885 | MockRead data_reads1[] = { |
| 2886 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\n"), |
| 2887 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2888 | MockRead(false, OK), |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 2889 | }; |
| 2890 | // The mock write results of transaction 1 and the first attempt of |
| 2891 | // transaction 2. |
| 2892 | MockWrite data_writes1[] = { |
| 2893 | MockWrite(false, 64), // GET |
| 2894 | MockWrite(false, 93), // POST |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2895 | MockWrite(false, ERR_CONNECTION_ABORTED), // POST data |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 2896 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 2897 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 2898 | data_writes1, arraysize(data_writes1)); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 2899 | |
| 2900 | // The second socket is used for the second attempt of transaction 2. |
| 2901 | |
| 2902 | // The response of transaction 2. |
| 2903 | MockRead data_reads2[] = { |
| 2904 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 7\r\n\r\n"), |
| 2905 | MockRead("welcome"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2906 | MockRead(false, OK), |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 2907 | }; |
| 2908 | // The mock write results of the second attempt of transaction 2. |
| 2909 | MockWrite data_writes2[] = { |
| 2910 | MockWrite(false, 93), // POST |
| 2911 | MockWrite(false, 3), // POST data |
| 2912 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 2913 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 2914 | data_writes2, arraysize(data_writes2)); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 2915 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 2916 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 2917 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 2918 | |
| 2919 | const char* kExpectedResponseData[] = { |
| 2920 | "hello world", "welcome" |
| 2921 | }; |
| 2922 | |
| 2923 | for (int i = 0; i < 2; ++i) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2924 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 2925 | new HttpNetworkTransaction(session)); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 2926 | |
| 2927 | TestCompletionCallback callback; |
| 2928 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 2929 | int rv = trans->Start(&request[i], &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2930 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 2931 | |
| 2932 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2933 | EXPECT_EQ(OK, rv); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 2934 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2935 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 2936 | EXPECT_TRUE(response != NULL); |
| 2937 | |
| 2938 | EXPECT_TRUE(response->headers != NULL); |
| 2939 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 2940 | |
| 2941 | std::string response_data; |
| 2942 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2943 | EXPECT_EQ(OK, rv); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 2944 | EXPECT_EQ(kExpectedResponseData[i], response_data); |
| 2945 | } |
| 2946 | } |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2947 | |
| 2948 | // Test the request-challenge-retry sequence for basic auth when there is |
| 2949 | // an identity in the URL. The request should be sent as normal, but when |
| 2950 | // it fails the identity from the URL is used to answer the challenge. |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 2951 | TEST_F(HttpNetworkTransactionTest, AuthIdentityInURL) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2952 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 2953 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 2954 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2955 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2956 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2957 | request.method = "GET"; |
| 2958 | // Note: the URL has a username:password in it. |
[email protected] | a97cca4 | 2009-08-14 01:00:29 | [diff] [blame] | 2959 | request.url = GURL("http://foo:b@[email protected]/"); |
| 2960 | |
| 2961 | // The password contains an escaped character -- for this test to pass it |
| 2962 | // will need to be unescaped by HttpNetworkTransaction. |
| 2963 | EXPECT_EQ("b%40r", request.url.password()); |
| 2964 | |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 2965 | request.load_flags = LOAD_NORMAL; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2966 | |
| 2967 | MockWrite data_writes1[] = { |
| 2968 | MockWrite("GET / HTTP/1.1\r\n" |
| 2969 | "Host: www.google.com\r\n" |
| 2970 | "Connection: keep-alive\r\n\r\n"), |
| 2971 | }; |
| 2972 | |
| 2973 | MockRead data_reads1[] = { |
| 2974 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 2975 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 2976 | MockRead("Content-Length: 10\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2977 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2978 | }; |
| 2979 | |
| 2980 | // After the challenge above, the transaction will be restarted using the |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 2981 | // identity from the url (foo, b@r) to answer the challenge. |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2982 | MockWrite data_writes2[] = { |
| 2983 | MockWrite("GET / HTTP/1.1\r\n" |
| 2984 | "Host: www.google.com\r\n" |
| 2985 | "Connection: keep-alive\r\n" |
[email protected] | a97cca4 | 2009-08-14 01:00:29 | [diff] [blame] | 2986 | "Authorization: Basic Zm9vOmJAcg==\r\n\r\n"), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2987 | }; |
| 2988 | |
| 2989 | MockRead data_reads2[] = { |
| 2990 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 2991 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2992 | MockRead(false, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2993 | }; |
| 2994 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 2995 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 2996 | data_writes1, arraysize(data_writes1)); |
| 2997 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 2998 | data_writes2, arraysize(data_writes2)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 2999 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3000 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3001 | |
| 3002 | TestCompletionCallback callback1; |
| 3003 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3004 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3005 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3006 | |
| 3007 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3008 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3009 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3010 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
| 3011 | TestCompletionCallback callback2; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 3012 | rv = trans->RestartWithAuth(string16(), string16(), &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3013 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3014 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3015 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3016 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 3017 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3018 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3019 | EXPECT_FALSE(response == NULL); |
| 3020 | |
| 3021 | // There is no challenge info, since the identity in URL worked. |
| 3022 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3023 | |
| 3024 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 3025 | |
| 3026 | // Empty the current queue. |
| 3027 | MessageLoop::current()->RunAllPending(); |
| 3028 | } |
| 3029 | |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3030 | // Test the request-challenge-retry sequence for basic auth when there is |
| 3031 | // an incorrect identity in the URL. The identity from the URL should be used |
| 3032 | // only once. |
| 3033 | TEST_F(HttpNetworkTransactionTest, WrongAuthIdentityInURL) { |
| 3034 | SessionDependencies session_deps; |
| 3035 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 3036 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3037 | |
| 3038 | HttpRequestInfo request; |
| 3039 | request.method = "GET"; |
| 3040 | // Note: the URL has a username:password in it. The password "baz" is |
| 3041 | // wrong (should be "bar"). |
| 3042 | request.url = GURL("http://foo:[email protected]/"); |
| 3043 | |
| 3044 | request.load_flags = LOAD_NORMAL; |
| 3045 | |
| 3046 | MockWrite data_writes1[] = { |
| 3047 | MockWrite("GET / HTTP/1.1\r\n" |
| 3048 | "Host: www.google.com\r\n" |
| 3049 | "Connection: keep-alive\r\n\r\n"), |
| 3050 | }; |
| 3051 | |
| 3052 | MockRead data_reads1[] = { |
| 3053 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 3054 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3055 | MockRead("Content-Length: 10\r\n\r\n"), |
| 3056 | MockRead(false, ERR_FAILED), |
| 3057 | }; |
| 3058 | |
| 3059 | // After the challenge above, the transaction will be restarted using the |
| 3060 | // identity from the url (foo, baz) to answer the challenge. |
| 3061 | MockWrite data_writes2[] = { |
| 3062 | MockWrite("GET / HTTP/1.1\r\n" |
| 3063 | "Host: www.google.com\r\n" |
| 3064 | "Connection: keep-alive\r\n" |
| 3065 | "Authorization: Basic Zm9vOmJheg==\r\n\r\n"), |
| 3066 | }; |
| 3067 | |
| 3068 | MockRead data_reads2[] = { |
| 3069 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 3070 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3071 | MockRead("Content-Length: 10\r\n\r\n"), |
| 3072 | MockRead(false, ERR_FAILED), |
| 3073 | }; |
| 3074 | |
| 3075 | // After the challenge above, the transaction will be restarted using the |
| 3076 | // identity supplied by the user (foo, bar) to answer the challenge. |
| 3077 | MockWrite data_writes3[] = { |
| 3078 | MockWrite("GET / HTTP/1.1\r\n" |
| 3079 | "Host: www.google.com\r\n" |
| 3080 | "Connection: keep-alive\r\n" |
| 3081 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 3082 | }; |
| 3083 | |
| 3084 | MockRead data_reads3[] = { |
| 3085 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3086 | MockRead("Content-Length: 100\r\n\r\n"), |
| 3087 | MockRead(false, OK), |
| 3088 | }; |
| 3089 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3090 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3091 | data_writes1, arraysize(data_writes1)); |
| 3092 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 3093 | data_writes2, arraysize(data_writes2)); |
| 3094 | StaticSocketDataProvider data3(data_reads3, arraysize(data_reads3), |
| 3095 | data_writes3, arraysize(data_writes3)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3096 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3097 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 3098 | session_deps.socket_factory.AddSocketDataProvider(&data3); |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3099 | |
| 3100 | TestCompletionCallback callback1; |
| 3101 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3102 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3103 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3104 | |
| 3105 | rv = callback1.WaitForResult(); |
| 3106 | EXPECT_EQ(OK, rv); |
| 3107 | |
| 3108 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
| 3109 | TestCompletionCallback callback2; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 3110 | rv = trans->RestartWithAuth(string16(), string16(), &callback2); |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3111 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3112 | rv = callback2.WaitForResult(); |
| 3113 | EXPECT_EQ(OK, rv); |
| 3114 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 3115 | |
| 3116 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 3117 | EXPECT_FALSE(response == NULL); |
| 3118 | // The password prompt info should have been set in response->auth_challenge. |
| 3119 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 3120 | |
| 3121 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
| 3122 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 3123 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 3124 | |
| 3125 | TestCompletionCallback callback3; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 3126 | rv = trans->RestartWithAuth(kFoo, kBar, &callback3); |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3127 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3128 | rv = callback3.WaitForResult(); |
| 3129 | EXPECT_EQ(OK, rv); |
| 3130 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 3131 | |
| 3132 | response = trans->GetResponseInfo(); |
| 3133 | EXPECT_FALSE(response == NULL); |
| 3134 | |
| 3135 | // There is no challenge info, since the identity worked. |
| 3136 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3137 | |
| 3138 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 3139 | |
| 3140 | // Empty the current queue. |
| 3141 | MessageLoop::current()->RunAllPending(); |
| 3142 | } |
| 3143 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3144 | // Test that previously tried username/passwords for a realm get re-used. |
| 3145 | TEST_F(HttpNetworkTransactionTest, BasicAuthCacheAndPreauth) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 3146 | SessionDependencies session_deps; |
| 3147 | scoped_refptr<HttpNetworkSession> session = CreateSession(&session_deps); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3148 | |
| 3149 | // Transaction 1: authenticate (foo, bar) on MyRealm1 |
| 3150 | { |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 3151 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3152 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3153 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3154 | request.method = "GET"; |
| 3155 | request.url = GURL("http://www.google.com/x/y/z"); |
| 3156 | request.load_flags = 0; |
| 3157 | |
| 3158 | MockWrite data_writes1[] = { |
| 3159 | MockWrite("GET /x/y/z HTTP/1.1\r\n" |
| 3160 | "Host: www.google.com\r\n" |
| 3161 | "Connection: keep-alive\r\n\r\n"), |
| 3162 | }; |
| 3163 | |
| 3164 | MockRead data_reads1[] = { |
| 3165 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 3166 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3167 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3168 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3169 | }; |
| 3170 | |
| 3171 | // Resend with authorization (username=foo, password=bar) |
| 3172 | MockWrite data_writes2[] = { |
| 3173 | MockWrite("GET /x/y/z HTTP/1.1\r\n" |
| 3174 | "Host: www.google.com\r\n" |
| 3175 | "Connection: keep-alive\r\n" |
| 3176 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 3177 | }; |
| 3178 | |
| 3179 | // Sever accepts the authorization. |
| 3180 | MockRead data_reads2[] = { |
| 3181 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3182 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3183 | MockRead(false, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3184 | }; |
| 3185 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3186 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3187 | data_writes1, arraysize(data_writes1)); |
| 3188 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 3189 | data_writes2, arraysize(data_writes2)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3190 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3191 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3192 | |
| 3193 | TestCompletionCallback callback1; |
| 3194 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3195 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3196 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3197 | |
| 3198 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3199 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3200 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3201 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3202 | EXPECT_FALSE(response == NULL); |
| 3203 | |
| 3204 | // The password prompt info should have been set in |
| 3205 | // response->auth_challenge. |
| 3206 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 3207 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 3208 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3209 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 3210 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 3211 | |
| 3212 | TestCompletionCallback callback2; |
| 3213 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 3214 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3215 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3216 | |
| 3217 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3218 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3219 | |
| 3220 | response = trans->GetResponseInfo(); |
| 3221 | EXPECT_FALSE(response == NULL); |
| 3222 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3223 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 3224 | } |
| 3225 | |
| 3226 | // ------------------------------------------------------------------------ |
| 3227 | |
| 3228 | // Transaction 2: authenticate (foo2, bar2) on MyRealm2 |
| 3229 | { |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 3230 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3231 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3232 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3233 | request.method = "GET"; |
| 3234 | // Note that Transaction 1 was at /x/y/z, so this is in the same |
| 3235 | // protection space as MyRealm1. |
| 3236 | request.url = GURL("http://www.google.com/x/y/a/b"); |
| 3237 | request.load_flags = 0; |
| 3238 | |
| 3239 | MockWrite data_writes1[] = { |
| 3240 | MockWrite("GET /x/y/a/b HTTP/1.1\r\n" |
| 3241 | "Host: www.google.com\r\n" |
| 3242 | "Connection: keep-alive\r\n" |
| 3243 | // Send preemptive authorization for MyRealm1 |
| 3244 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 3245 | }; |
| 3246 | |
| 3247 | // The server didn't like the preemptive authorization, and |
| 3248 | // challenges us for a different realm (MyRealm2). |
| 3249 | MockRead data_reads1[] = { |
| 3250 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 3251 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm2\"\r\n"), |
| 3252 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3253 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3254 | }; |
| 3255 | |
| 3256 | // Resend with authorization for MyRealm2 (username=foo2, password=bar2) |
| 3257 | MockWrite data_writes2[] = { |
| 3258 | MockWrite("GET /x/y/a/b HTTP/1.1\r\n" |
| 3259 | "Host: www.google.com\r\n" |
| 3260 | "Connection: keep-alive\r\n" |
| 3261 | "Authorization: Basic Zm9vMjpiYXIy\r\n\r\n"), |
| 3262 | }; |
| 3263 | |
| 3264 | // Sever accepts the authorization. |
| 3265 | MockRead data_reads2[] = { |
| 3266 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3267 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3268 | MockRead(false, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3269 | }; |
| 3270 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3271 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3272 | data_writes1, arraysize(data_writes1)); |
| 3273 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 3274 | data_writes2, arraysize(data_writes2)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3275 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3276 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3277 | |
| 3278 | TestCompletionCallback callback1; |
| 3279 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3280 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3281 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3282 | |
| 3283 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3284 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3285 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3286 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3287 | EXPECT_FALSE(response == NULL); |
| 3288 | |
| 3289 | // The password prompt info should have been set in |
| 3290 | // response->auth_challenge. |
| 3291 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 3292 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 3293 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3294 | EXPECT_EQ(L"MyRealm2", response->auth_challenge->realm); |
| 3295 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 3296 | |
| 3297 | TestCompletionCallback callback2; |
| 3298 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 3299 | rv = trans->RestartWithAuth(kFoo2, kBar2, &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3300 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3301 | |
| 3302 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3303 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3304 | |
| 3305 | response = trans->GetResponseInfo(); |
| 3306 | EXPECT_FALSE(response == NULL); |
| 3307 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3308 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 3309 | } |
| 3310 | |
| 3311 | // ------------------------------------------------------------------------ |
| 3312 | |
| 3313 | // Transaction 3: Resend a request in MyRealm's protection space -- |
| 3314 | // succeed with preemptive authorization. |
| 3315 | { |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 3316 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3317 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3318 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3319 | request.method = "GET"; |
| 3320 | request.url = GURL("http://www.google.com/x/y/z2"); |
| 3321 | request.load_flags = 0; |
| 3322 | |
| 3323 | MockWrite data_writes1[] = { |
| 3324 | MockWrite("GET /x/y/z2 HTTP/1.1\r\n" |
| 3325 | "Host: www.google.com\r\n" |
| 3326 | "Connection: keep-alive\r\n" |
| 3327 | // The authorization for MyRealm1 gets sent preemptively |
| 3328 | // (since the url is in the same protection space) |
| 3329 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 3330 | }; |
| 3331 | |
| 3332 | // Sever accepts the preemptive authorization |
| 3333 | MockRead data_reads1[] = { |
| 3334 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3335 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3336 | MockRead(false, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3337 | }; |
| 3338 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3339 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3340 | data_writes1, arraysize(data_writes1)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3341 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3342 | |
| 3343 | TestCompletionCallback callback1; |
| 3344 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3345 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3346 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3347 | |
| 3348 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3349 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3350 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3351 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3352 | EXPECT_FALSE(response == NULL); |
| 3353 | |
| 3354 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3355 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 3356 | } |
| 3357 | |
| 3358 | // ------------------------------------------------------------------------ |
| 3359 | |
| 3360 | // Transaction 4: request another URL in MyRealm (however the |
| 3361 | // url is not known to belong to the protection space, so no pre-auth). |
| 3362 | { |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 3363 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3364 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3365 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3366 | request.method = "GET"; |
| 3367 | request.url = GURL("http://www.google.com/x/1"); |
| 3368 | request.load_flags = 0; |
| 3369 | |
| 3370 | MockWrite data_writes1[] = { |
| 3371 | MockWrite("GET /x/1 HTTP/1.1\r\n" |
| 3372 | "Host: www.google.com\r\n" |
| 3373 | "Connection: keep-alive\r\n\r\n"), |
| 3374 | }; |
| 3375 | |
| 3376 | MockRead data_reads1[] = { |
| 3377 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 3378 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3379 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3380 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3381 | }; |
| 3382 | |
| 3383 | // Resend with authorization from MyRealm's cache. |
| 3384 | MockWrite data_writes2[] = { |
| 3385 | MockWrite("GET /x/1 HTTP/1.1\r\n" |
| 3386 | "Host: www.google.com\r\n" |
| 3387 | "Connection: keep-alive\r\n" |
| 3388 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 3389 | }; |
| 3390 | |
| 3391 | // Sever accepts the authorization. |
| 3392 | MockRead data_reads2[] = { |
| 3393 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3394 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3395 | MockRead(false, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3396 | }; |
| 3397 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3398 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3399 | data_writes1, arraysize(data_writes1)); |
| 3400 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 3401 | data_writes2, arraysize(data_writes2)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3402 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3403 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3404 | |
| 3405 | TestCompletionCallback callback1; |
| 3406 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3407 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3408 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3409 | |
| 3410 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3411 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3412 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3413 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
| 3414 | TestCompletionCallback callback2; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 3415 | rv = trans->RestartWithAuth(string16(), string16(), &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3416 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3417 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3418 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3419 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 3420 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3421 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3422 | EXPECT_FALSE(response == NULL); |
| 3423 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3424 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 3425 | } |
| 3426 | |
| 3427 | // ------------------------------------------------------------------------ |
| 3428 | |
| 3429 | // Transaction 5: request a URL in MyRealm, but the server rejects the |
| 3430 | // cached identity. Should invalidate and re-prompt. |
| 3431 | { |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 3432 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3433 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3434 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3435 | request.method = "GET"; |
| 3436 | request.url = GURL("http://www.google.com/p/q/t"); |
| 3437 | request.load_flags = 0; |
| 3438 | |
| 3439 | MockWrite data_writes1[] = { |
| 3440 | MockWrite("GET /p/q/t HTTP/1.1\r\n" |
| 3441 | "Host: www.google.com\r\n" |
| 3442 | "Connection: keep-alive\r\n\r\n"), |
| 3443 | }; |
| 3444 | |
| 3445 | MockRead data_reads1[] = { |
| 3446 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 3447 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3448 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3449 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3450 | }; |
| 3451 | |
| 3452 | // Resend with authorization from cache for MyRealm. |
| 3453 | MockWrite data_writes2[] = { |
| 3454 | MockWrite("GET /p/q/t HTTP/1.1\r\n" |
| 3455 | "Host: www.google.com\r\n" |
| 3456 | "Connection: keep-alive\r\n" |
| 3457 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 3458 | }; |
| 3459 | |
| 3460 | // Sever rejects the authorization. |
| 3461 | MockRead data_reads2[] = { |
| 3462 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 3463 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3464 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3465 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3466 | }; |
| 3467 | |
| 3468 | // At this point we should prompt for new credentials for MyRealm. |
| 3469 | // Restart with username=foo3, password=foo4. |
| 3470 | MockWrite data_writes3[] = { |
| 3471 | MockWrite("GET /p/q/t HTTP/1.1\r\n" |
| 3472 | "Host: www.google.com\r\n" |
| 3473 | "Connection: keep-alive\r\n" |
| 3474 | "Authorization: Basic Zm9vMzpiYXIz\r\n\r\n"), |
| 3475 | }; |
| 3476 | |
| 3477 | // Sever accepts the authorization. |
| 3478 | MockRead data_reads3[] = { |
| 3479 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3480 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3481 | MockRead(false, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3482 | }; |
| 3483 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3484 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3485 | data_writes1, arraysize(data_writes1)); |
| 3486 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 3487 | data_writes2, arraysize(data_writes2)); |
| 3488 | StaticSocketDataProvider data3(data_reads3, arraysize(data_reads3), |
| 3489 | data_writes3, arraysize(data_writes3)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3490 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3491 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 3492 | session_deps.socket_factory.AddSocketDataProvider(&data3); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3493 | |
| 3494 | TestCompletionCallback callback1; |
| 3495 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3496 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3497 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3498 | |
| 3499 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3500 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3501 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3502 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
| 3503 | TestCompletionCallback callback2; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 3504 | rv = trans->RestartWithAuth(string16(), string16(), &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3505 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3506 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3507 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3508 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 3509 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3510 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3511 | EXPECT_FALSE(response == NULL); |
| 3512 | |
| 3513 | // The password prompt info should have been set in |
| 3514 | // response->auth_challenge. |
| 3515 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 3516 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 3517 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3518 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 3519 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 3520 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3521 | TestCompletionCallback callback3; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3522 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 3523 | rv = trans->RestartWithAuth(kFoo3, kBar3, &callback3); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3524 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3525 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3526 | rv = callback3.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3527 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3528 | |
| 3529 | response = trans->GetResponseInfo(); |
| 3530 | EXPECT_FALSE(response == NULL); |
| 3531 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3532 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 3533 | } |
| 3534 | } |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 3535 | |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 3536 | // Tests that nonce count increments when multiple auth attempts |
| 3537 | // are started with the same nonce. |
| 3538 | TEST_F(HttpNetworkTransactionTest, DigestPreAuthNonceCount) { |
| 3539 | SessionDependencies session_deps; |
| 3540 | scoped_refptr<HttpNetworkSession> session = CreateSession(&session_deps); |
| 3541 | HttpAuthHandlerDigest::SetFixedCnonce(true); |
| 3542 | |
| 3543 | // Transaction 1: authenticate (foo, bar) on MyRealm1 |
| 3544 | { |
| 3545 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 3546 | |
| 3547 | HttpRequestInfo request; |
| 3548 | request.method = "GET"; |
| 3549 | request.url = GURL("http://www.google.com/x/y/z"); |
| 3550 | request.load_flags = 0; |
| 3551 | |
| 3552 | MockWrite data_writes1[] = { |
| 3553 | MockWrite("GET /x/y/z HTTP/1.1\r\n" |
| 3554 | "Host: www.google.com\r\n" |
| 3555 | "Connection: keep-alive\r\n\r\n"), |
| 3556 | }; |
| 3557 | |
| 3558 | MockRead data_reads1[] = { |
| 3559 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 3560 | MockRead("WWW-Authenticate: Digest realm=\"digestive\", nonce=\"OU812\", " |
| 3561 | "algorithm=MD5, qop=\"auth\"\r\n\r\n"), |
| 3562 | MockRead(false, OK), |
| 3563 | }; |
| 3564 | |
| 3565 | // Resend with authorization (username=foo, password=bar) |
| 3566 | MockWrite data_writes2[] = { |
| 3567 | MockWrite("GET /x/y/z HTTP/1.1\r\n" |
| 3568 | "Host: www.google.com\r\n" |
| 3569 | "Connection: keep-alive\r\n" |
| 3570 | "Authorization: Digest username=\"foo\", realm=\"digestive\", " |
| 3571 | "nonce=\"OU812\", uri=\"/x/y/z\", algorithm=MD5, " |
| 3572 | "response=\"03ffbcd30add722589c1de345d7a927f\", qop=auth, " |
| 3573 | "nc=00000001, cnonce=\"0123456789abcdef\"\r\n\r\n"), |
| 3574 | }; |
| 3575 | |
| 3576 | // Sever accepts the authorization. |
| 3577 | MockRead data_reads2[] = { |
| 3578 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3579 | MockRead(false, OK), |
| 3580 | }; |
| 3581 | |
| 3582 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3583 | data_writes1, arraysize(data_writes1)); |
| 3584 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 3585 | data_writes2, arraysize(data_writes2)); |
| 3586 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3587 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 3588 | |
| 3589 | TestCompletionCallback callback1; |
| 3590 | |
| 3591 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
| 3592 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3593 | |
| 3594 | rv = callback1.WaitForResult(); |
| 3595 | EXPECT_EQ(OK, rv); |
| 3596 | |
| 3597 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 3598 | ASSERT_FALSE(response == NULL); |
| 3599 | |
| 3600 | // The password prompt info should have been set in |
| 3601 | // response->auth_challenge. |
| 3602 | ASSERT_FALSE(response->auth_challenge.get() == NULL); |
| 3603 | |
| 3604 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
| 3605 | EXPECT_EQ(L"digestive", response->auth_challenge->realm); |
| 3606 | EXPECT_EQ(L"digest", response->auth_challenge->scheme); |
| 3607 | |
| 3608 | TestCompletionCallback callback2; |
| 3609 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 3610 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 3611 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3612 | |
| 3613 | rv = callback2.WaitForResult(); |
| 3614 | EXPECT_EQ(OK, rv); |
| 3615 | |
| 3616 | response = trans->GetResponseInfo(); |
| 3617 | ASSERT_FALSE(response == NULL); |
| 3618 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3619 | } |
| 3620 | |
| 3621 | // ------------------------------------------------------------------------ |
| 3622 | |
| 3623 | // Transaction 2: Request another resource in digestive's protection space. |
| 3624 | // This will preemptively add an Authorization header which should have an |
| 3625 | // "nc" value of 2 (as compared to 1 in the first use. |
| 3626 | { |
| 3627 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 3628 | |
| 3629 | HttpRequestInfo request; |
| 3630 | request.method = "GET"; |
| 3631 | // Note that Transaction 1 was at /x/y/z, so this is in the same |
| 3632 | // protection space as digest. |
| 3633 | request.url = GURL("http://www.google.com/x/y/a/b"); |
| 3634 | request.load_flags = 0; |
| 3635 | |
| 3636 | MockWrite data_writes1[] = { |
| 3637 | MockWrite("GET /x/y/a/b HTTP/1.1\r\n" |
| 3638 | "Host: www.google.com\r\n" |
| 3639 | "Connection: keep-alive\r\n" |
| 3640 | "Authorization: Digest username=\"foo\", realm=\"digestive\", " |
| 3641 | "nonce=\"OU812\", uri=\"/x/y/a/b\", algorithm=MD5, " |
| 3642 | "response=\"d6f9a2c07d1c5df7b89379dca1269b35\", qop=auth, " |
| 3643 | "nc=00000002, cnonce=\"0123456789abcdef\"\r\n\r\n"), |
| 3644 | }; |
| 3645 | |
| 3646 | // Sever accepts the authorization. |
| 3647 | MockRead data_reads1[] = { |
| 3648 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3649 | MockRead("Content-Length: 100\r\n\r\n"), |
| 3650 | MockRead(false, OK), |
| 3651 | }; |
| 3652 | |
| 3653 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3654 | data_writes1, arraysize(data_writes1)); |
| 3655 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3656 | |
| 3657 | TestCompletionCallback callback1; |
| 3658 | |
| 3659 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
| 3660 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3661 | |
| 3662 | rv = callback1.WaitForResult(); |
| 3663 | EXPECT_EQ(OK, rv); |
| 3664 | |
| 3665 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 3666 | ASSERT_FALSE(response == NULL); |
| 3667 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3668 | } |
| 3669 | } |
| 3670 | |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 3671 | // Test the ResetStateForRestart() private method. |
| 3672 | TEST_F(HttpNetworkTransactionTest, ResetStateForRestart) { |
| 3673 | // Create a transaction (the dependencies aren't important). |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 3674 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 3675 | scoped_ptr<HttpNetworkTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 3676 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 3677 | |
| 3678 | // Setup some state (which we expect ResetStateForRestart() will clear). |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 3679 | trans->read_buf_ = new IOBuffer(15); |
| 3680 | trans->read_buf_len_ = 15; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 3681 | trans->request_headers_ = "Authorization: NTLM"; |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 3682 | |
| 3683 | // Setup state in response_ |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 3684 | HttpResponseInfo* response = &trans->response_; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 3685 | response->auth_challenge = new AuthChallengeInfo(); |
| 3686 | response->ssl_info.cert_status = -15; |
| 3687 | response->response_time = base::Time::Now(); |
| 3688 | response->was_cached = true; // (Wouldn't ever actually be true...) |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 3689 | |
| 3690 | { // Setup state for response_.vary_data |
| 3691 | HttpRequestInfo request; |
| 3692 | std::string temp("HTTP/1.1 200 OK\nVary: foo, bar\n\n"); |
| 3693 | std::replace(temp.begin(), temp.end(), '\n', '\0'); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 3694 | scoped_refptr<HttpResponseHeaders> headers = new HttpResponseHeaders(temp); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3695 | request.extra_headers.SetHeader("Foo", "1"); |
| 3696 | request.extra_headers.SetHeader("bar", "23"); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 3697 | EXPECT_TRUE(response->vary_data.Init(request, *headers)); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 3698 | } |
| 3699 | |
| 3700 | // Cause the above state to be reset. |
| 3701 | trans->ResetStateForRestart(); |
| 3702 | |
| 3703 | // Verify that the state that needed to be reset, has been reset. |
[email protected] | 9b6fee1 | 2009-09-29 18:13:07 | [diff] [blame] | 3704 | EXPECT_TRUE(trans->read_buf_.get() == NULL); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 3705 | EXPECT_EQ(0, trans->read_buf_len_); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 3706 | EXPECT_EQ(0U, trans->request_headers_.size()); |
| 3707 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3708 | EXPECT_TRUE(response->headers.get() == NULL); |
| 3709 | EXPECT_EQ(false, response->was_cached); |
| 3710 | EXPECT_EQ(0, response->ssl_info.cert_status); |
| 3711 | EXPECT_FALSE(response->vary_data.is_valid()); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 3712 | } |
| 3713 | |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 3714 | // Test HTTPS connections to a site with a bad certificate |
| 3715 | TEST_F(HttpNetworkTransactionTest, HTTPSBadCertificate) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 3716 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 3717 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 3718 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 3719 | |
| 3720 | HttpRequestInfo request; |
| 3721 | request.method = "GET"; |
| 3722 | request.url = GURL("https://www.google.com/"); |
| 3723 | request.load_flags = 0; |
| 3724 | |
| 3725 | MockWrite data_writes[] = { |
| 3726 | MockWrite("GET / HTTP/1.1\r\n" |
| 3727 | "Host: www.google.com\r\n" |
| 3728 | "Connection: keep-alive\r\n\r\n"), |
| 3729 | }; |
| 3730 | |
| 3731 | MockRead data_reads[] = { |
| 3732 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3733 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 3734 | MockRead("Content-Length: 100\r\n\r\n"), |
| 3735 | MockRead(false, OK), |
| 3736 | }; |
| 3737 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3738 | StaticSocketDataProvider ssl_bad_certificate; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3739 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 3740 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3741 | SSLSocketDataProvider ssl_bad(true, ERR_CERT_AUTHORITY_INVALID); |
| 3742 | SSLSocketDataProvider ssl(true, OK); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 3743 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3744 | session_deps.socket_factory.AddSocketDataProvider(&ssl_bad_certificate); |
| 3745 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 3746 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_bad); |
| 3747 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 3748 | |
| 3749 | TestCompletionCallback callback; |
| 3750 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3751 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 3752 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3753 | |
| 3754 | rv = callback.WaitForResult(); |
| 3755 | EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, rv); |
| 3756 | |
| 3757 | rv = trans->RestartIgnoringLastError(&callback); |
| 3758 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3759 | |
| 3760 | rv = callback.WaitForResult(); |
| 3761 | EXPECT_EQ(OK, rv); |
| 3762 | |
| 3763 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 3764 | |
| 3765 | EXPECT_FALSE(response == NULL); |
| 3766 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 3767 | } |
| 3768 | |
| 3769 | // Test HTTPS connections to a site with a bad certificate, going through a |
| 3770 | // proxy |
| 3771 | TEST_F(HttpNetworkTransactionTest, HTTPSBadCertificateViaProxy) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 3772 | SessionDependencies session_deps(CreateFixedProxyService("myproxy:70")); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 3773 | |
| 3774 | HttpRequestInfo request; |
| 3775 | request.method = "GET"; |
| 3776 | request.url = GURL("https://www.google.com/"); |
| 3777 | request.load_flags = 0; |
| 3778 | |
| 3779 | MockWrite proxy_writes[] = { |
| 3780 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 3781 | "Host: www.google.com\r\n" |
| 3782 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 3783 | }; |
| 3784 | |
| 3785 | MockRead proxy_reads[] = { |
| 3786 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3787 | MockRead(false, OK) |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 3788 | }; |
| 3789 | |
| 3790 | MockWrite data_writes[] = { |
| 3791 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 3792 | "Host: www.google.com\r\n" |
| 3793 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 3794 | MockWrite("GET / HTTP/1.1\r\n" |
| 3795 | "Host: www.google.com\r\n" |
| 3796 | "Connection: keep-alive\r\n\r\n"), |
| 3797 | }; |
| 3798 | |
| 3799 | MockRead data_reads[] = { |
| 3800 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
| 3801 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3802 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 3803 | MockRead("Content-Length: 100\r\n\r\n"), |
| 3804 | MockRead(false, OK), |
| 3805 | }; |
| 3806 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3807 | StaticSocketDataProvider ssl_bad_certificate( |
| 3808 | proxy_reads, arraysize(proxy_reads), |
| 3809 | proxy_writes, arraysize(proxy_writes)); |
| 3810 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 3811 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3812 | SSLSocketDataProvider ssl_bad(true, ERR_CERT_AUTHORITY_INVALID); |
| 3813 | SSLSocketDataProvider ssl(true, OK); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 3814 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3815 | session_deps.socket_factory.AddSocketDataProvider(&ssl_bad_certificate); |
| 3816 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 3817 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_bad); |
| 3818 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 3819 | |
| 3820 | TestCompletionCallback callback; |
| 3821 | |
| 3822 | for (int i = 0; i < 2; i++) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 3823 | session_deps.socket_factory.ResetNextMockIndexes(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 3824 | |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 3825 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 3826 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 3827 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3828 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 3829 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3830 | |
| 3831 | rv = callback.WaitForResult(); |
| 3832 | EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, rv); |
| 3833 | |
| 3834 | rv = trans->RestartIgnoringLastError(&callback); |
| 3835 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3836 | |
| 3837 | rv = callback.WaitForResult(); |
| 3838 | EXPECT_EQ(OK, rv); |
| 3839 | |
| 3840 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 3841 | |
| 3842 | EXPECT_FALSE(response == NULL); |
| 3843 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 3844 | } |
| 3845 | } |
| 3846 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 3847 | |
| 3848 | // Test HTTPS connections to a site, going through an HTTPS proxy |
| 3849 | TEST_F(HttpNetworkTransactionTest, HTTPSViaHttpsProxy) { |
| 3850 | SessionDependencies session_deps(CreateFixedProxyService("https://proxy:70")); |
| 3851 | |
| 3852 | HttpRequestInfo request; |
| 3853 | request.method = "GET"; |
| 3854 | request.url = GURL("https://www.google.com/"); |
| 3855 | request.load_flags = 0; |
| 3856 | |
| 3857 | MockWrite data_writes[] = { |
| 3858 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 3859 | "Host: www.google.com\r\n" |
| 3860 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 3861 | MockWrite("GET / HTTP/1.1\r\n" |
| 3862 | "Host: www.google.com\r\n" |
| 3863 | "Connection: keep-alive\r\n\r\n"), |
| 3864 | }; |
| 3865 | |
| 3866 | MockRead data_reads[] = { |
| 3867 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
| 3868 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 3869 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 3870 | MockRead("Content-Length: 100\r\n\r\n"), |
| 3871 | MockRead(false, OK), |
| 3872 | }; |
| 3873 | |
| 3874 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 3875 | data_writes, arraysize(data_writes)); |
| 3876 | SSLSocketDataProvider proxy_ssl(true, OK); // SSL to the proxy |
| 3877 | SSLSocketDataProvider tunnel_ssl(true, OK); // SSL through the tunnel |
| 3878 | |
| 3879 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 3880 | session_deps.socket_factory.AddSSLSocketDataProvider(&proxy_ssl); |
| 3881 | session_deps.socket_factory.AddSSLSocketDataProvider(&tunnel_ssl); |
| 3882 | |
| 3883 | TestCompletionCallback callback; |
| 3884 | |
| 3885 | scoped_ptr<HttpTransaction> trans( |
| 3886 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 3887 | |
| 3888 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 3889 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3890 | |
| 3891 | rv = callback.WaitForResult(); |
| 3892 | EXPECT_EQ(OK, rv); |
| 3893 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 3894 | |
| 3895 | ASSERT_FALSE(response == NULL); |
| 3896 | |
| 3897 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 3898 | EXPECT_EQ(200, response->headers->response_code()); |
| 3899 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 3900 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 3901 | } |
| 3902 | |
| 3903 | // Test HTTPS connections to a site with a bad certificate, going through an |
| 3904 | // HTTPS proxy |
| 3905 | TEST_F(HttpNetworkTransactionTest, HTTPSBadCertificateViaHttpsProxy) { |
| 3906 | SessionDependencies session_deps(CreateFixedProxyService("https://proxy:70")); |
| 3907 | |
| 3908 | HttpRequestInfo request; |
| 3909 | request.method = "GET"; |
| 3910 | request.url = GURL("https://www.google.com/"); |
| 3911 | request.load_flags = 0; |
| 3912 | |
| 3913 | // Attempt to fetch the URL from a server with a bad cert |
| 3914 | MockWrite bad_cert_writes[] = { |
| 3915 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 3916 | "Host: www.google.com\r\n" |
| 3917 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 3918 | }; |
| 3919 | |
| 3920 | MockRead bad_cert_reads[] = { |
| 3921 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
| 3922 | MockRead(false, OK) |
| 3923 | }; |
| 3924 | |
| 3925 | // Attempt to fetch the URL with a good cert |
| 3926 | MockWrite good_data_writes[] = { |
| 3927 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 3928 | "Host: www.google.com\r\n" |
| 3929 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 3930 | MockWrite("GET / HTTP/1.1\r\n" |
| 3931 | "Host: www.google.com\r\n" |
| 3932 | "Connection: keep-alive\r\n\r\n"), |
| 3933 | }; |
| 3934 | |
| 3935 | MockRead good_cert_reads[] = { |
| 3936 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
| 3937 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3938 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 3939 | MockRead("Content-Length: 100\r\n\r\n"), |
| 3940 | MockRead(false, OK), |
| 3941 | }; |
| 3942 | |
| 3943 | StaticSocketDataProvider ssl_bad_certificate( |
| 3944 | bad_cert_reads, arraysize(bad_cert_reads), |
| 3945 | bad_cert_writes, arraysize(bad_cert_writes)); |
| 3946 | StaticSocketDataProvider data(good_cert_reads, arraysize(good_cert_reads), |
| 3947 | good_data_writes, arraysize(good_data_writes)); |
| 3948 | SSLSocketDataProvider ssl_bad(true, ERR_CERT_AUTHORITY_INVALID); |
| 3949 | SSLSocketDataProvider ssl(true, OK); |
| 3950 | |
| 3951 | // SSL to the proxy, then CONNECT request, then SSL with bad certificate |
| 3952 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 3953 | session_deps.socket_factory.AddSocketDataProvider(&ssl_bad_certificate); |
| 3954 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_bad); |
| 3955 | |
| 3956 | // SSL to the proxy, then CONNECT request, then valid SSL certificate |
| 3957 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 3958 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 3959 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 3960 | |
| 3961 | TestCompletionCallback callback; |
| 3962 | |
| 3963 | scoped_ptr<HttpTransaction> trans( |
| 3964 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 3965 | |
| 3966 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 3967 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3968 | |
| 3969 | rv = callback.WaitForResult(); |
| 3970 | EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, rv); |
| 3971 | |
| 3972 | rv = trans->RestartIgnoringLastError(&callback); |
| 3973 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3974 | |
| 3975 | rv = callback.WaitForResult(); |
| 3976 | EXPECT_EQ(OK, rv); |
| 3977 | |
| 3978 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 3979 | |
| 3980 | EXPECT_FALSE(response == NULL); |
| 3981 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 3982 | } |
| 3983 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3984 | TEST_F(HttpNetworkTransactionTest, BuildRequest_UserAgent) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 3985 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 3986 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 3987 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3988 | |
| 3989 | HttpRequestInfo request; |
| 3990 | request.method = "GET"; |
| 3991 | request.url = GURL("http://www.google.com/"); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3992 | request.extra_headers.SetHeader(HttpRequestHeaders::kUserAgent, |
| 3993 | "Chromium Ultra Awesome X Edition"); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3994 | |
| 3995 | MockWrite data_writes[] = { |
| 3996 | MockWrite("GET / HTTP/1.1\r\n" |
| 3997 | "Host: www.google.com\r\n" |
| 3998 | "Connection: keep-alive\r\n" |
| 3999 | "User-Agent: Chromium Ultra Awesome X Edition\r\n\r\n"), |
| 4000 | }; |
| 4001 | |
| 4002 | // Lastly, the server responds with the actual content. |
| 4003 | MockRead data_reads[] = { |
| 4004 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4005 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4006 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4007 | MockRead(false, OK), |
| 4008 | }; |
| 4009 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4010 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4011 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4012 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4013 | |
| 4014 | TestCompletionCallback callback; |
| 4015 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4016 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4017 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4018 | |
| 4019 | rv = callback.WaitForResult(); |
| 4020 | EXPECT_EQ(OK, rv); |
| 4021 | } |
| 4022 | |
[email protected] | da81f13 | 2010-08-18 23:39:29 | [diff] [blame] | 4023 | TEST_F(HttpNetworkTransactionTest, BuildRequest_UserAgentOverTunnel) { |
| 4024 | SessionDependencies session_deps(CreateFixedProxyService("myproxy:70")); |
| 4025 | scoped_ptr<HttpTransaction> trans( |
| 4026 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4027 | |
| 4028 | HttpRequestInfo request; |
| 4029 | request.method = "GET"; |
| 4030 | request.url = GURL("https://www.google.com/"); |
| 4031 | request.extra_headers.SetHeader(HttpRequestHeaders::kUserAgent, |
| 4032 | "Chromium Ultra Awesome X Edition"); |
| 4033 | |
| 4034 | MockWrite data_writes[] = { |
| 4035 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 4036 | "Host: www.google.com\r\n" |
| 4037 | "Proxy-Connection: keep-alive\r\n" |
| 4038 | "User-Agent: Chromium Ultra Awesome X Edition\r\n\r\n"), |
| 4039 | }; |
| 4040 | MockRead data_reads[] = { |
| 4041 | // Return an error, so the transaction stops here (this test isn't |
| 4042 | // interested in the rest). |
| 4043 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 4044 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 4045 | MockRead("Proxy-Connection: close\r\n\r\n"), |
| 4046 | }; |
| 4047 | |
| 4048 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4049 | data_writes, arraysize(data_writes)); |
| 4050 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 4051 | |
| 4052 | TestCompletionCallback callback; |
| 4053 | |
| 4054 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 4055 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4056 | |
| 4057 | rv = callback.WaitForResult(); |
| 4058 | EXPECT_EQ(OK, rv); |
| 4059 | } |
| 4060 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4061 | TEST_F(HttpNetworkTransactionTest, BuildRequest_Referer) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 4062 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 4063 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 4064 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4065 | |
| 4066 | HttpRequestInfo request; |
| 4067 | request.method = "GET"; |
| 4068 | request.url = GURL("http://www.google.com/"); |
| 4069 | request.load_flags = 0; |
| 4070 | request.referrer = GURL("http://the.previous.site.com/"); |
| 4071 | |
| 4072 | MockWrite data_writes[] = { |
| 4073 | MockWrite("GET / HTTP/1.1\r\n" |
| 4074 | "Host: www.google.com\r\n" |
| 4075 | "Connection: keep-alive\r\n" |
| 4076 | "Referer: http://the.previous.site.com/\r\n\r\n"), |
| 4077 | }; |
| 4078 | |
| 4079 | // Lastly, the server responds with the actual content. |
| 4080 | MockRead data_reads[] = { |
| 4081 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4082 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4083 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4084 | MockRead(false, OK), |
| 4085 | }; |
| 4086 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4087 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4088 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4089 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4090 | |
| 4091 | TestCompletionCallback callback; |
| 4092 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4093 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4094 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4095 | |
| 4096 | rv = callback.WaitForResult(); |
| 4097 | EXPECT_EQ(OK, rv); |
| 4098 | } |
| 4099 | |
| 4100 | TEST_F(HttpNetworkTransactionTest, BuildRequest_PostContentLengthZero) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 4101 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 4102 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 4103 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4104 | |
| 4105 | HttpRequestInfo request; |
| 4106 | request.method = "POST"; |
| 4107 | request.url = GURL("http://www.google.com/"); |
| 4108 | |
| 4109 | MockWrite data_writes[] = { |
| 4110 | MockWrite("POST / HTTP/1.1\r\n" |
| 4111 | "Host: www.google.com\r\n" |
| 4112 | "Connection: keep-alive\r\n" |
| 4113 | "Content-Length: 0\r\n\r\n"), |
| 4114 | }; |
| 4115 | |
| 4116 | // Lastly, the server responds with the actual content. |
| 4117 | MockRead data_reads[] = { |
| 4118 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4119 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4120 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4121 | MockRead(false, OK), |
| 4122 | }; |
| 4123 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4124 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4125 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4126 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4127 | |
| 4128 | TestCompletionCallback callback; |
| 4129 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4130 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4131 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4132 | |
| 4133 | rv = callback.WaitForResult(); |
| 4134 | EXPECT_EQ(OK, rv); |
| 4135 | } |
| 4136 | |
| 4137 | TEST_F(HttpNetworkTransactionTest, BuildRequest_PutContentLengthZero) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 4138 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 4139 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 4140 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4141 | |
| 4142 | HttpRequestInfo request; |
| 4143 | request.method = "PUT"; |
| 4144 | request.url = GURL("http://www.google.com/"); |
| 4145 | |
| 4146 | MockWrite data_writes[] = { |
| 4147 | MockWrite("PUT / HTTP/1.1\r\n" |
| 4148 | "Host: www.google.com\r\n" |
| 4149 | "Connection: keep-alive\r\n" |
| 4150 | "Content-Length: 0\r\n\r\n"), |
| 4151 | }; |
| 4152 | |
| 4153 | // Lastly, the server responds with the actual content. |
| 4154 | MockRead data_reads[] = { |
| 4155 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4156 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4157 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4158 | MockRead(false, OK), |
| 4159 | }; |
| 4160 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4161 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4162 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4163 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4164 | |
| 4165 | TestCompletionCallback callback; |
| 4166 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4167 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4168 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4169 | |
| 4170 | rv = callback.WaitForResult(); |
| 4171 | EXPECT_EQ(OK, rv); |
| 4172 | } |
| 4173 | |
| 4174 | TEST_F(HttpNetworkTransactionTest, BuildRequest_HeadContentLengthZero) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 4175 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 4176 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 4177 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4178 | |
| 4179 | HttpRequestInfo request; |
| 4180 | request.method = "HEAD"; |
| 4181 | request.url = GURL("http://www.google.com/"); |
| 4182 | |
| 4183 | MockWrite data_writes[] = { |
| 4184 | MockWrite("HEAD / HTTP/1.1\r\n" |
| 4185 | "Host: www.google.com\r\n" |
| 4186 | "Connection: keep-alive\r\n" |
| 4187 | "Content-Length: 0\r\n\r\n"), |
| 4188 | }; |
| 4189 | |
| 4190 | // Lastly, the server responds with the actual content. |
| 4191 | MockRead data_reads[] = { |
| 4192 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4193 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4194 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4195 | MockRead(false, OK), |
| 4196 | }; |
| 4197 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4198 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4199 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4200 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4201 | |
| 4202 | TestCompletionCallback callback; |
| 4203 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4204 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4205 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4206 | |
| 4207 | rv = callback.WaitForResult(); |
| 4208 | EXPECT_EQ(OK, rv); |
| 4209 | } |
| 4210 | |
| 4211 | TEST_F(HttpNetworkTransactionTest, BuildRequest_CacheControlNoCache) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 4212 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 4213 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 4214 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4215 | |
| 4216 | HttpRequestInfo request; |
| 4217 | request.method = "GET"; |
| 4218 | request.url = GURL("http://www.google.com/"); |
| 4219 | request.load_flags = LOAD_BYPASS_CACHE; |
| 4220 | |
| 4221 | MockWrite data_writes[] = { |
| 4222 | MockWrite("GET / HTTP/1.1\r\n" |
| 4223 | "Host: www.google.com\r\n" |
| 4224 | "Connection: keep-alive\r\n" |
| 4225 | "Pragma: no-cache\r\n" |
| 4226 | "Cache-Control: no-cache\r\n\r\n"), |
| 4227 | }; |
| 4228 | |
| 4229 | // Lastly, the server responds with the actual content. |
| 4230 | MockRead data_reads[] = { |
| 4231 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4232 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4233 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4234 | MockRead(false, OK), |
| 4235 | }; |
| 4236 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4237 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4238 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4239 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4240 | |
| 4241 | TestCompletionCallback callback; |
| 4242 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4243 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4244 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4245 | |
| 4246 | rv = callback.WaitForResult(); |
| 4247 | EXPECT_EQ(OK, rv); |
| 4248 | } |
| 4249 | |
| 4250 | TEST_F(HttpNetworkTransactionTest, |
| 4251 | BuildRequest_CacheControlValidateCache) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 4252 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 4253 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 4254 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4255 | |
| 4256 | HttpRequestInfo request; |
| 4257 | request.method = "GET"; |
| 4258 | request.url = GURL("http://www.google.com/"); |
| 4259 | request.load_flags = LOAD_VALIDATE_CACHE; |
| 4260 | |
| 4261 | MockWrite data_writes[] = { |
| 4262 | MockWrite("GET / HTTP/1.1\r\n" |
| 4263 | "Host: www.google.com\r\n" |
| 4264 | "Connection: keep-alive\r\n" |
| 4265 | "Cache-Control: max-age=0\r\n\r\n"), |
| 4266 | }; |
| 4267 | |
| 4268 | // Lastly, the server responds with the actual content. |
| 4269 | MockRead data_reads[] = { |
| 4270 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4271 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4272 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4273 | MockRead(false, OK), |
| 4274 | }; |
| 4275 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4276 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4277 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4278 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4279 | |
| 4280 | TestCompletionCallback callback; |
| 4281 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4282 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4283 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4284 | |
| 4285 | rv = callback.WaitForResult(); |
| 4286 | EXPECT_EQ(OK, rv); |
| 4287 | } |
| 4288 | |
| 4289 | TEST_F(HttpNetworkTransactionTest, BuildRequest_ExtraHeaders) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 4290 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 4291 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 4292 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4293 | |
| 4294 | HttpRequestInfo request; |
| 4295 | request.method = "GET"; |
| 4296 | request.url = GURL("http://www.google.com/"); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4297 | request.extra_headers.SetHeader("FooHeader", "Bar"); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4298 | |
| 4299 | MockWrite data_writes[] = { |
| 4300 | MockWrite("GET / HTTP/1.1\r\n" |
| 4301 | "Host: www.google.com\r\n" |
| 4302 | "Connection: keep-alive\r\n" |
| 4303 | "FooHeader: Bar\r\n\r\n"), |
| 4304 | }; |
| 4305 | |
| 4306 | // Lastly, the server responds with the actual content. |
| 4307 | MockRead data_reads[] = { |
| 4308 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4309 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4310 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4311 | MockRead(false, OK), |
| 4312 | }; |
| 4313 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4314 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4315 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4316 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4317 | |
| 4318 | TestCompletionCallback callback; |
| 4319 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4320 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4321 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4322 | |
| 4323 | rv = callback.WaitForResult(); |
| 4324 | EXPECT_EQ(OK, rv); |
| 4325 | } |
| 4326 | |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 4327 | TEST_F(HttpNetworkTransactionTest, BuildRequest_ExtraHeadersStripped) { |
| 4328 | SessionDependencies session_deps; |
| 4329 | scoped_ptr<HttpTransaction> trans( |
| 4330 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4331 | |
| 4332 | HttpRequestInfo request; |
| 4333 | request.method = "GET"; |
| 4334 | request.url = GURL("http://www.google.com/"); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4335 | request.extra_headers.SetHeader("referer", "www.foo.com"); |
| 4336 | request.extra_headers.SetHeader("hEllo", "Kitty"); |
| 4337 | request.extra_headers.SetHeader("FoO", "bar"); |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 4338 | |
| 4339 | MockWrite data_writes[] = { |
| 4340 | MockWrite("GET / HTTP/1.1\r\n" |
| 4341 | "Host: www.google.com\r\n" |
| 4342 | "Connection: keep-alive\r\n" |
| 4343 | "hEllo: Kitty\r\n" |
| 4344 | "FoO: bar\r\n\r\n"), |
| 4345 | }; |
| 4346 | |
| 4347 | // Lastly, the server responds with the actual content. |
| 4348 | MockRead data_reads[] = { |
| 4349 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4350 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4351 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4352 | MockRead(false, OK), |
| 4353 | }; |
| 4354 | |
| 4355 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4356 | data_writes, arraysize(data_writes)); |
| 4357 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 4358 | |
| 4359 | TestCompletionCallback callback; |
| 4360 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4361 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 4362 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4363 | |
| 4364 | rv = callback.WaitForResult(); |
| 4365 | EXPECT_EQ(OK, rv); |
| 4366 | } |
| 4367 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 4368 | TEST_F(HttpNetworkTransactionTest, SOCKS4_HTTP_GET) { |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 4369 | SessionDependencies session_deps( |
| 4370 | CreateFixedProxyService("socks4://myproxy:1080")); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 4371 | |
| 4372 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 4373 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 4374 | |
| 4375 | HttpRequestInfo request; |
| 4376 | request.method = "GET"; |
| 4377 | request.url = GURL("http://www.google.com/"); |
| 4378 | request.load_flags = 0; |
| 4379 | |
| 4380 | char write_buffer[] = { 0x04, 0x01, 0x00, 0x50, 127, 0, 0, 1, 0 }; |
| 4381 | char read_buffer[] = { 0x00, 0x5A, 0x00, 0x00, 0, 0, 0, 0 }; |
| 4382 | |
| 4383 | MockWrite data_writes[] = { |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 4384 | MockWrite(true, write_buffer, arraysize(write_buffer)), |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 4385 | MockWrite("GET / HTTP/1.1\r\n" |
| 4386 | "Host: www.google.com\r\n" |
| 4387 | "Connection: keep-alive\r\n\r\n") |
| 4388 | }; |
| 4389 | |
| 4390 | MockRead data_reads[] = { |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4391 | MockRead(true, read_buffer, arraysize(read_buffer)), |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 4392 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4393 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), |
| 4394 | MockRead("Payload"), |
| 4395 | MockRead(false, OK) |
| 4396 | }; |
| 4397 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4398 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4399 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4400 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 4401 | |
| 4402 | TestCompletionCallback callback; |
| 4403 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4404 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 4405 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4406 | |
| 4407 | rv = callback.WaitForResult(); |
| 4408 | EXPECT_EQ(OK, rv); |
| 4409 | |
| 4410 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4411 | EXPECT_FALSE(response == NULL); |
| 4412 | |
| 4413 | std::string response_text; |
| 4414 | rv = ReadTransaction(trans.get(), &response_text); |
| 4415 | EXPECT_EQ(OK, rv); |
| 4416 | EXPECT_EQ("Payload", response_text); |
| 4417 | } |
| 4418 | |
| 4419 | TEST_F(HttpNetworkTransactionTest, SOCKS4_SSL_GET) { |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 4420 | SessionDependencies session_deps( |
| 4421 | CreateFixedProxyService("socks4://myproxy:1080")); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 4422 | |
| 4423 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 4424 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 4425 | |
| 4426 | HttpRequestInfo request; |
| 4427 | request.method = "GET"; |
| 4428 | request.url = GURL("https://www.google.com/"); |
| 4429 | request.load_flags = 0; |
| 4430 | |
| 4431 | unsigned char write_buffer[] = { 0x04, 0x01, 0x01, 0xBB, 127, 0, 0, 1, 0 }; |
| 4432 | unsigned char read_buffer[] = { 0x00, 0x5A, 0x00, 0x00, 0, 0, 0, 0 }; |
| 4433 | |
| 4434 | MockWrite data_writes[] = { |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 4435 | MockWrite(true, reinterpret_cast<char*>(write_buffer), |
| 4436 | arraysize(write_buffer)), |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 4437 | MockWrite("GET / HTTP/1.1\r\n" |
| 4438 | "Host: www.google.com\r\n" |
| 4439 | "Connection: keep-alive\r\n\r\n") |
| 4440 | }; |
| 4441 | |
| 4442 | MockRead data_reads[] = { |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 4443 | MockWrite(true, reinterpret_cast<char*>(read_buffer), |
| 4444 | arraysize(read_buffer)), |
| 4445 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4446 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), |
| 4447 | MockRead("Payload"), |
| 4448 | MockRead(false, OK) |
| 4449 | }; |
| 4450 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4451 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4452 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4453 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 4454 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4455 | SSLSocketDataProvider ssl(true, OK); |
| 4456 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 4457 | |
| 4458 | TestCompletionCallback callback; |
| 4459 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4460 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 4461 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4462 | |
| 4463 | rv = callback.WaitForResult(); |
| 4464 | EXPECT_EQ(OK, rv); |
| 4465 | |
| 4466 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4467 | EXPECT_FALSE(response == NULL); |
| 4468 | |
| 4469 | std::string response_text; |
| 4470 | rv = ReadTransaction(trans.get(), &response_text); |
| 4471 | EXPECT_EQ(OK, rv); |
| 4472 | EXPECT_EQ("Payload", response_text); |
| 4473 | } |
| 4474 | |
| 4475 | TEST_F(HttpNetworkTransactionTest, SOCKS5_HTTP_GET) { |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 4476 | SessionDependencies session_deps( |
| 4477 | CreateFixedProxyService("socks5://myproxy:1080")); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 4478 | |
| 4479 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 4480 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 4481 | |
| 4482 | HttpRequestInfo request; |
| 4483 | request.method = "GET"; |
| 4484 | request.url = GURL("http://www.google.com/"); |
| 4485 | request.load_flags = 0; |
| 4486 | |
| 4487 | const char kSOCKS5GreetRequest[] = { 0x05, 0x01, 0x00 }; |
| 4488 | const char kSOCKS5GreetResponse[] = { 0x05, 0x00 }; |
[email protected] | f209dba | 2009-12-18 00:24:37 | [diff] [blame] | 4489 | const char kSOCKS5OkRequest[] = { |
| 4490 | 0x05, // Version |
| 4491 | 0x01, // Command (CONNECT) |
| 4492 | 0x00, // Reserved. |
| 4493 | 0x03, // Address type (DOMAINNAME). |
| 4494 | 0x0E, // Length of domain (14) |
| 4495 | // Domain string: |
| 4496 | 'w', 'w', 'w', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'c', 'o', 'm', |
| 4497 | 0x00, 0x50, // 16-bit port (80) |
| 4498 | }; |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 4499 | const char kSOCKS5OkResponse[] = |
| 4500 | { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; |
| 4501 | |
| 4502 | MockWrite data_writes[] = { |
| 4503 | MockWrite(true, kSOCKS5GreetRequest, arraysize(kSOCKS5GreetRequest)), |
| 4504 | MockWrite(true, kSOCKS5OkRequest, arraysize(kSOCKS5OkRequest)), |
| 4505 | MockWrite("GET / HTTP/1.1\r\n" |
| 4506 | "Host: www.google.com\r\n" |
| 4507 | "Connection: keep-alive\r\n\r\n") |
| 4508 | }; |
| 4509 | |
| 4510 | MockRead data_reads[] = { |
| 4511 | MockWrite(true, kSOCKS5GreetResponse, arraysize(kSOCKS5GreetResponse)), |
| 4512 | MockWrite(true, kSOCKS5OkResponse, arraysize(kSOCKS5OkResponse)), |
| 4513 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4514 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), |
| 4515 | MockRead("Payload"), |
| 4516 | MockRead(false, OK) |
| 4517 | }; |
| 4518 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4519 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4520 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4521 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 4522 | |
| 4523 | TestCompletionCallback callback; |
| 4524 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4525 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 4526 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4527 | |
| 4528 | rv = callback.WaitForResult(); |
| 4529 | EXPECT_EQ(OK, rv); |
| 4530 | |
| 4531 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4532 | EXPECT_FALSE(response == NULL); |
| 4533 | |
| 4534 | std::string response_text; |
| 4535 | rv = ReadTransaction(trans.get(), &response_text); |
| 4536 | EXPECT_EQ(OK, rv); |
| 4537 | EXPECT_EQ("Payload", response_text); |
| 4538 | } |
| 4539 | |
| 4540 | TEST_F(HttpNetworkTransactionTest, SOCKS5_SSL_GET) { |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 4541 | SessionDependencies session_deps( |
| 4542 | CreateFixedProxyService("socks5://myproxy:1080")); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 4543 | |
| 4544 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 4545 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 4546 | |
| 4547 | HttpRequestInfo request; |
| 4548 | request.method = "GET"; |
| 4549 | request.url = GURL("https://www.google.com/"); |
| 4550 | request.load_flags = 0; |
| 4551 | |
| 4552 | const char kSOCKS5GreetRequest[] = { 0x05, 0x01, 0x00 }; |
| 4553 | const char kSOCKS5GreetResponse[] = { 0x05, 0x00 }; |
[email protected] | f209dba | 2009-12-18 00:24:37 | [diff] [blame] | 4554 | const unsigned char kSOCKS5OkRequest[] = { |
| 4555 | 0x05, // Version |
| 4556 | 0x01, // Command (CONNECT) |
| 4557 | 0x00, // Reserved. |
| 4558 | 0x03, // Address type (DOMAINNAME). |
| 4559 | 0x0E, // Length of domain (14) |
| 4560 | // Domain string: |
| 4561 | 'w', 'w', 'w', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'c', 'o', 'm', |
| 4562 | 0x01, 0xBB, // 16-bit port (443) |
| 4563 | }; |
| 4564 | |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 4565 | const char kSOCKS5OkResponse[] = |
| 4566 | { 0x05, 0x00, 0x00, 0x01, 0, 0, 0, 0, 0x00, 0x00 }; |
| 4567 | |
| 4568 | MockWrite data_writes[] = { |
| 4569 | MockWrite(true, kSOCKS5GreetRequest, arraysize(kSOCKS5GreetRequest)), |
| 4570 | MockWrite(true, reinterpret_cast<const char*>(kSOCKS5OkRequest), |
| 4571 | arraysize(kSOCKS5OkRequest)), |
| 4572 | MockWrite("GET / HTTP/1.1\r\n" |
| 4573 | "Host: www.google.com\r\n" |
| 4574 | "Connection: keep-alive\r\n\r\n") |
| 4575 | }; |
| 4576 | |
| 4577 | MockRead data_reads[] = { |
| 4578 | MockWrite(true, kSOCKS5GreetResponse, arraysize(kSOCKS5GreetResponse)), |
| 4579 | MockWrite(true, kSOCKS5OkResponse, arraysize(kSOCKS5OkResponse)), |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 4580 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4581 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), |
| 4582 | MockRead("Payload"), |
| 4583 | MockRead(false, OK) |
| 4584 | }; |
| 4585 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4586 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4587 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4588 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 4589 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4590 | SSLSocketDataProvider ssl(true, OK); |
| 4591 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 4592 | |
| 4593 | TestCompletionCallback callback; |
| 4594 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4595 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 4596 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4597 | |
| 4598 | rv = callback.WaitForResult(); |
| 4599 | EXPECT_EQ(OK, rv); |
| 4600 | |
| 4601 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4602 | EXPECT_FALSE(response == NULL); |
| 4603 | |
| 4604 | std::string response_text; |
| 4605 | rv = ReadTransaction(trans.get(), &response_text); |
| 4606 | EXPECT_EQ(OK, rv); |
| 4607 | EXPECT_EQ("Payload", response_text); |
| 4608 | } |
| 4609 | |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 4610 | // Tests that for connection endpoints the group names are correctly set. |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4611 | |
| 4612 | struct GroupNameTest { |
| 4613 | std::string proxy_server; |
| 4614 | std::string url; |
| 4615 | std::string expected_group_name; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4616 | bool ssl; |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4617 | }; |
| 4618 | |
| 4619 | scoped_refptr<HttpNetworkSession> SetupSessionForGroupNameTests( |
| 4620 | const std::string& proxy_server) { |
| 4621 | SessionDependencies session_deps(CreateFixedProxyService(proxy_server)); |
| 4622 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 4623 | |
| 4624 | HttpAlternateProtocols* alternate_protocols = |
| 4625 | session->mutable_alternate_protocols(); |
| 4626 | alternate_protocols->SetAlternateProtocolFor( |
| 4627 | HostPortPair("host.with.alternate", 80), 443, |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 4628 | HttpAlternateProtocols::NPN_SPDY_2); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4629 | |
| 4630 | return session; |
| 4631 | } |
| 4632 | |
| 4633 | int GroupNameTransactionHelper( |
| 4634 | const std::string& url, |
| 4635 | const scoped_refptr<HttpNetworkSession>& session) { |
| 4636 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 4637 | |
| 4638 | HttpRequestInfo request; |
| 4639 | request.method = "GET"; |
| 4640 | request.url = GURL(url); |
| 4641 | request.load_flags = 0; |
| 4642 | |
| 4643 | TestCompletionCallback callback; |
| 4644 | |
| 4645 | // We do not complete this request, the dtor will clean the transaction up. |
| 4646 | return trans->Start(&request, &callback, BoundNetLog()); |
| 4647 | } |
| 4648 | |
| 4649 | TEST_F(HttpNetworkTransactionTest, GroupNameForDirectConnections) { |
| 4650 | const GroupNameTest tests[] = { |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 4651 | { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4652 | "", // unused |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 4653 | "http://www.google.com/direct", |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 4654 | "www.google.com:80", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4655 | false, |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 4656 | }, |
| 4657 | { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4658 | "", // unused |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 4659 | "http://[2001:1418:13:1::25]/direct", |
| 4660 | "[2001:1418:13:1::25]:80", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4661 | false, |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 4662 | }, |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 4663 | |
| 4664 | // SSL Tests |
| 4665 | { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4666 | "", // unused |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 4667 | "https://www.google.com/direct_ssl", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 4668 | "ssl/www.google.com:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4669 | true, |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 4670 | }, |
| 4671 | { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4672 | "", // unused |
| 4673 | "https://[2001:1418:13:1::25]/direct", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 4674 | "ssl/[2001:1418:13:1::25]:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4675 | true, |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 4676 | }, |
| 4677 | { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4678 | "", // unused |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 4679 | "http://host.with.alternate/direct", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 4680 | "ssl/host.with.alternate:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4681 | true, |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 4682 | }, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4683 | }; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 4684 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 4685 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4686 | |
| 4687 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 4688 | scoped_refptr<HttpNetworkSession> session( |
| 4689 | SetupSessionForGroupNameTests(tests[i].proxy_server)); |
| 4690 | |
| 4691 | HttpNetworkSessionPeer peer(session); |
| 4692 | scoped_refptr<CaptureGroupNameTCPSocketPool> tcp_conn_pool( |
| 4693 | new CaptureGroupNameTCPSocketPool(session.get())); |
| 4694 | peer.SetTCPSocketPool(tcp_conn_pool); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4695 | scoped_refptr<CaptureGroupNameSSLSocketPool> ssl_conn_pool( |
| 4696 | new CaptureGroupNameSSLSocketPool(session.get())); |
| 4697 | peer.SetSSLSocketPool(ssl_conn_pool); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4698 | |
| 4699 | EXPECT_EQ(ERR_IO_PENDING, |
| 4700 | GroupNameTransactionHelper(tests[i].url, session)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4701 | if (tests[i].ssl) |
| 4702 | EXPECT_EQ(tests[i].expected_group_name, |
| 4703 | ssl_conn_pool->last_group_name_received()); |
| 4704 | else |
| 4705 | EXPECT_EQ(tests[i].expected_group_name, |
| 4706 | tcp_conn_pool->last_group_name_received()); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4707 | } |
| 4708 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 4709 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4710 | } |
| 4711 | |
| 4712 | TEST_F(HttpNetworkTransactionTest, GroupNameForHTTPProxyConnections) { |
| 4713 | const GroupNameTest tests[] = { |
| 4714 | { |
| 4715 | "http_proxy", |
| 4716 | "http://www.google.com/http_proxy_normal", |
| 4717 | "www.google.com:80", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4718 | false, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4719 | }, |
| 4720 | |
| 4721 | // SSL Tests |
| 4722 | { |
| 4723 | "http_proxy", |
| 4724 | "https://www.google.com/http_connect_ssl", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 4725 | "ssl/www.google.com:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4726 | true, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4727 | }, |
| 4728 | |
[email protected] | 9faeded9 | 2010-04-29 20:03:05 | [diff] [blame] | 4729 | { |
| 4730 | "http_proxy", |
| 4731 | "http://host.with.alternate/direct", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 4732 | "ssl/host.with.alternate:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4733 | true, |
[email protected] | 9faeded9 | 2010-04-29 20:03:05 | [diff] [blame] | 4734 | }, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4735 | }; |
| 4736 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 4737 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4738 | |
| 4739 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 4740 | scoped_refptr<HttpNetworkSession> session( |
| 4741 | SetupSessionForGroupNameTests(tests[i].proxy_server)); |
| 4742 | |
| 4743 | HttpNetworkSessionPeer peer(session); |
| 4744 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4745 | HostPortPair proxy_host("http_proxy", 80); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 4746 | scoped_refptr<CaptureGroupNameHttpProxySocketPool> http_proxy_pool( |
| 4747 | new CaptureGroupNameHttpProxySocketPool(session.get())); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4748 | peer.SetSocketPoolForHTTPProxy(proxy_host, http_proxy_pool); |
| 4749 | scoped_refptr<CaptureGroupNameSSLSocketPool> ssl_conn_pool( |
| 4750 | new CaptureGroupNameSSLSocketPool(session.get())); |
| 4751 | peer.SetSocketPoolForSSLWithProxy(proxy_host, ssl_conn_pool); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4752 | |
| 4753 | EXPECT_EQ(ERR_IO_PENDING, |
| 4754 | GroupNameTransactionHelper(tests[i].url, session)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4755 | if (tests[i].ssl) |
| 4756 | EXPECT_EQ(tests[i].expected_group_name, |
| 4757 | ssl_conn_pool->last_group_name_received()); |
| 4758 | else |
| 4759 | EXPECT_EQ(tests[i].expected_group_name, |
| 4760 | http_proxy_pool->last_group_name_received()); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4761 | } |
| 4762 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 4763 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4764 | } |
| 4765 | |
| 4766 | TEST_F(HttpNetworkTransactionTest, GroupNameForSOCKSConnections) { |
| 4767 | const GroupNameTest tests[] = { |
| 4768 | { |
| 4769 | "socks4://socks_proxy:1080", |
| 4770 | "http://www.google.com/socks4_direct", |
| 4771 | "socks4/www.google.com:80", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4772 | false, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4773 | }, |
| 4774 | { |
| 4775 | "socks5://socks_proxy:1080", |
| 4776 | "http://www.google.com/socks5_direct", |
| 4777 | "socks5/www.google.com:80", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4778 | false, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4779 | }, |
| 4780 | |
| 4781 | // SSL Tests |
| 4782 | { |
| 4783 | "socks4://socks_proxy:1080", |
| 4784 | "https://www.google.com/socks4_ssl", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 4785 | "socks4/ssl/www.google.com:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4786 | true, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4787 | }, |
| 4788 | { |
| 4789 | "socks5://socks_proxy:1080", |
| 4790 | "https://www.google.com/socks5_ssl", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 4791 | "socks5/ssl/www.google.com:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4792 | true, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4793 | }, |
| 4794 | |
[email protected] | 9faeded9 | 2010-04-29 20:03:05 | [diff] [blame] | 4795 | { |
| 4796 | "socks4://socks_proxy:1080", |
| 4797 | "http://host.with.alternate/direct", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 4798 | "socks4/ssl/host.with.alternate:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4799 | true, |
[email protected] | 9faeded9 | 2010-04-29 20:03:05 | [diff] [blame] | 4800 | }, |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 4801 | }; |
| 4802 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 4803 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 4804 | |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 4805 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4806 | scoped_refptr<HttpNetworkSession> session( |
| 4807 | SetupSessionForGroupNameTests(tests[i].proxy_server)); |
| 4808 | HttpNetworkSessionPeer peer(session); |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 4809 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4810 | HostPortPair proxy_host("socks_proxy", 1080); |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 4811 | scoped_refptr<CaptureGroupNameSOCKSSocketPool> socks_conn_pool( |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4812 | new CaptureGroupNameSOCKSSocketPool(session.get())); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4813 | peer.SetSocketPoolForSOCKSProxy(proxy_host, socks_conn_pool); |
| 4814 | scoped_refptr<CaptureGroupNameSSLSocketPool> ssl_conn_pool( |
| 4815 | new CaptureGroupNameSSLSocketPool(session.get())); |
| 4816 | peer.SetSocketPoolForSSLWithProxy(proxy_host, ssl_conn_pool); |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 4817 | |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 4818 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 4819 | |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4820 | EXPECT_EQ(ERR_IO_PENDING, |
| 4821 | GroupNameTransactionHelper(tests[i].url, session)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4822 | if (tests[i].ssl) |
| 4823 | EXPECT_EQ(tests[i].expected_group_name, |
| 4824 | ssl_conn_pool->last_group_name_received()); |
| 4825 | else |
| 4826 | EXPECT_EQ(tests[i].expected_group_name, |
| 4827 | socks_conn_pool->last_group_name_received()); |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 4828 | } |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 4829 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 4830 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 4831 | } |
| 4832 | |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 4833 | TEST_F(HttpNetworkTransactionTest, ReconsiderProxyAfterFailedConnection) { |
[email protected] | 5c6a17e | 2009-06-10 00:54:54 | [diff] [blame] | 4834 | SessionDependencies session_deps( |
| 4835 | CreateFixedProxyService("myproxy:70;foobar:80")); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 4836 | |
[email protected] | 6971906 | 2010-01-05 20:09:21 | [diff] [blame] | 4837 | // This simulates failure resolving all hostnames; that means we will fail |
| 4838 | // connecting to both proxies (myproxy:70 and foobar:80). |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 4839 | session_deps.host_resolver->rules()->AddSimulatedFailure("*"); |
| 4840 | |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 4841 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 4842 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 4843 | |
| 4844 | HttpRequestInfo request; |
| 4845 | request.method = "GET"; |
| 4846 | request.url = GURL("http://www.google.com/"); |
| 4847 | |
| 4848 | TestCompletionCallback callback; |
| 4849 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4850 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 4851 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4852 | |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 4853 | rv = callback.WaitForResult(); |
| 4854 | EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); |
| 4855 | } |
| 4856 | |
[email protected] | f3e6c1e | 2009-06-15 20:52:12 | [diff] [blame] | 4857 | // Host resolution observer used by |
| 4858 | // HttpNetworkTransactionTest.ResolveMadeWithReferrer to check that host |
| 4859 | // resovle requests are issued with a referrer of |expected_referrer|. |
| 4860 | class ResolutionReferrerObserver : public HostResolver::Observer { |
| 4861 | public: |
| 4862 | explicit ResolutionReferrerObserver(const GURL& expected_referrer) |
| 4863 | : expected_referrer_(expected_referrer), |
| 4864 | called_start_with_referrer_(false), |
| 4865 | called_finish_with_referrer_(false) { |
| 4866 | } |
| 4867 | |
| 4868 | virtual void OnStartResolution(int id, |
| 4869 | const HostResolver::RequestInfo& info) { |
| 4870 | if (info.referrer() == expected_referrer_) |
| 4871 | called_start_with_referrer_ = true; |
| 4872 | } |
| 4873 | |
| 4874 | virtual void OnFinishResolutionWithStatus( |
| 4875 | int id, bool was_resolved, const HostResolver::RequestInfo& info ) { |
| 4876 | if (info.referrer() == expected_referrer_) |
| 4877 | called_finish_with_referrer_ = true; |
| 4878 | } |
| 4879 | |
[email protected] | eb255d3 | 2009-06-17 02:11:03 | [diff] [blame] | 4880 | virtual void OnCancelResolution(int id, |
| 4881 | const HostResolver::RequestInfo& info ) { |
| 4882 | FAIL() << "Should not be cancelling any requests!"; |
| 4883 | } |
| 4884 | |
[email protected] | f3e6c1e | 2009-06-15 20:52:12 | [diff] [blame] | 4885 | bool did_complete_with_expected_referrer() const { |
| 4886 | return called_start_with_referrer_ && called_finish_with_referrer_; |
| 4887 | } |
| 4888 | |
| 4889 | private: |
| 4890 | GURL expected_referrer_; |
| 4891 | bool called_start_with_referrer_; |
| 4892 | bool called_finish_with_referrer_; |
| 4893 | |
| 4894 | DISALLOW_COPY_AND_ASSIGN(ResolutionReferrerObserver); |
| 4895 | }; |
| 4896 | |
| 4897 | // Make sure that when HostResolver::Resolve() is invoked, it passes through |
| 4898 | // the "referrer". This is depended on by the DNS prefetch observer. |
| 4899 | TEST_F(HttpNetworkTransactionTest, ResolveMadeWithReferrer) { |
| 4900 | GURL referrer = GURL("http://expected-referrer/"); |
| 4901 | EXPECT_TRUE(referrer.is_valid()); |
| 4902 | ResolutionReferrerObserver resolution_observer(referrer); |
| 4903 | |
| 4904 | SessionDependencies session_deps; |
| 4905 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 4906 | CreateSession(&session_deps))); |
[email protected] | f3e6c1e | 2009-06-15 20:52:12 | [diff] [blame] | 4907 | |
| 4908 | // Attach an observer to watch the host resolutions being made. |
[email protected] | 94a0d3d9 | 2009-06-27 01:50:14 | [diff] [blame] | 4909 | session_deps.host_resolver->AddObserver(&resolution_observer); |
[email protected] | f3e6c1e | 2009-06-15 20:52:12 | [diff] [blame] | 4910 | |
| 4911 | // Connect up a mock socket which will fail when reading. |
| 4912 | MockRead data_reads[] = { |
| 4913 | MockRead(false, ERR_FAILED), |
| 4914 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4915 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4916 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | f3e6c1e | 2009-06-15 20:52:12 | [diff] [blame] | 4917 | |
| 4918 | // Issue a request, containing an HTTP referrer. |
| 4919 | HttpRequestInfo request; |
| 4920 | request.method = "GET"; |
| 4921 | request.referrer = referrer; |
| 4922 | request.url = GURL("http://www.google.com/"); |
| 4923 | |
| 4924 | // Run the request until it fails reading from the socket. |
| 4925 | TestCompletionCallback callback; |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4926 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | f3e6c1e | 2009-06-15 20:52:12 | [diff] [blame] | 4927 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4928 | rv = callback.WaitForResult(); |
| 4929 | EXPECT_EQ(ERR_FAILED, rv); |
| 4930 | |
| 4931 | // Check that the host resolution observer saw |referrer|. |
| 4932 | EXPECT_TRUE(resolution_observer.did_complete_with_expected_referrer()); |
| 4933 | } |
| 4934 | |
[email protected] | 685af59 | 2010-05-11 19:31:24 | [diff] [blame] | 4935 | // Base test to make sure that when the load flags for a request specify to |
| 4936 | // bypass the cache, the DNS cache is not used. |
| 4937 | void BypassHostCacheOnRefreshHelper(int load_flags) { |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 4938 | SessionDependencies session_deps; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 4939 | |
[email protected] | a2c2fb9 | 2009-07-18 07:31:04 | [diff] [blame] | 4940 | // Select a host resolver that does caching. |
| 4941 | session_deps.host_resolver = new MockCachingHostResolver; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 4942 | |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 4943 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 4944 | CreateSession(&session_deps))); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 4945 | |
| 4946 | // Warm up the host cache so it has an entry for "www.google.com" (by doing |
| 4947 | // a synchronous lookup.) |
| 4948 | AddressList addrlist; |
[email protected] | 94a0d3d9 | 2009-06-27 01:50:14 | [diff] [blame] | 4949 | int rv = session_deps.host_resolver->Resolve( |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 4950 | HostResolver::RequestInfo("www.google.com", 80), &addrlist, |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4951 | NULL, NULL, BoundNetLog()); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 4952 | EXPECT_EQ(OK, rv); |
| 4953 | |
| 4954 | // Verify that it was added to host cache, by doing a subsequent async lookup |
| 4955 | // and confirming it completes synchronously. |
| 4956 | TestCompletionCallback resolve_callback; |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 4957 | rv = session_deps.host_resolver->Resolve( |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 4958 | HostResolver::RequestInfo("www.google.com", 80), &addrlist, |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4959 | &resolve_callback, NULL, BoundNetLog()); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 4960 | ASSERT_EQ(OK, rv); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 4961 | |
| 4962 | // Inject a failure the next time that "www.google.com" is resolved. This way |
| 4963 | // we can tell if the next lookup hit the cache, or the "network". |
| 4964 | // (cache --> success, "network" --> failure). |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 4965 | session_deps.host_resolver->rules()->AddSimulatedFailure("www.google.com"); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 4966 | |
| 4967 | // Connect up a mock socket which will fail with ERR_UNEXPECTED during the |
| 4968 | // first read -- this won't be reached as the host resolution will fail first. |
| 4969 | MockRead data_reads[] = { MockRead(false, ERR_UNEXPECTED) }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4970 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4971 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 4972 | |
| 4973 | // Issue a request, asking to bypass the cache(s). |
| 4974 | HttpRequestInfo request; |
| 4975 | request.method = "GET"; |
[email protected] | 685af59 | 2010-05-11 19:31:24 | [diff] [blame] | 4976 | request.load_flags = load_flags; |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 4977 | request.url = GURL("http://www.google.com/"); |
| 4978 | |
| 4979 | // Run the request. |
| 4980 | TestCompletionCallback callback; |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4981 | rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 4982 | ASSERT_EQ(ERR_IO_PENDING, rv); |
| 4983 | rv = callback.WaitForResult(); |
| 4984 | |
| 4985 | // If we bypassed the cache, we would have gotten a failure while resolving |
| 4986 | // "www.google.com". |
| 4987 | EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); |
| 4988 | } |
| 4989 | |
[email protected] | 685af59 | 2010-05-11 19:31:24 | [diff] [blame] | 4990 | // There are multiple load flags that should trigger the host cache bypass. |
| 4991 | // Test each in isolation: |
| 4992 | TEST_F(HttpNetworkTransactionTest, BypassHostCacheOnRefresh1) { |
| 4993 | BypassHostCacheOnRefreshHelper(LOAD_BYPASS_CACHE); |
| 4994 | } |
| 4995 | |
| 4996 | TEST_F(HttpNetworkTransactionTest, BypassHostCacheOnRefresh2) { |
| 4997 | BypassHostCacheOnRefreshHelper(LOAD_VALIDATE_CACHE); |
| 4998 | } |
| 4999 | |
| 5000 | TEST_F(HttpNetworkTransactionTest, BypassHostCacheOnRefresh3) { |
| 5001 | BypassHostCacheOnRefreshHelper(LOAD_DISABLE_CACHE); |
| 5002 | } |
| 5003 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5004 | // Make sure we can handle an error when writing the request. |
| 5005 | TEST_F(HttpNetworkTransactionTest, RequestWriteError) { |
| 5006 | SessionDependencies session_deps; |
| 5007 | scoped_refptr<HttpNetworkSession> session = CreateSession(&session_deps); |
| 5008 | |
| 5009 | HttpRequestInfo request; |
| 5010 | request.method = "GET"; |
| 5011 | request.url = GURL("http://www.foo.com/"); |
| 5012 | request.load_flags = 0; |
| 5013 | |
| 5014 | MockWrite write_failure[] = { |
| 5015 | MockWrite(true, ERR_CONNECTION_RESET), |
| 5016 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5017 | StaticSocketDataProvider data(NULL, 0, |
| 5018 | write_failure, arraysize(write_failure)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5019 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5020 | |
| 5021 | TestCompletionCallback callback; |
| 5022 | |
| 5023 | scoped_ptr<HttpTransaction> trans( |
| 5024 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5025 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5026 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5027 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5028 | |
| 5029 | rv = callback.WaitForResult(); |
| 5030 | EXPECT_EQ(ERR_CONNECTION_RESET, rv); |
| 5031 | } |
| 5032 | |
| 5033 | // Check that a connection closed after the start of the headers finishes ok. |
| 5034 | TEST_F(HttpNetworkTransactionTest, ConnectionClosedAfterStartOfHeaders) { |
| 5035 | SessionDependencies session_deps; |
| 5036 | scoped_refptr<HttpNetworkSession> session = CreateSession(&session_deps); |
| 5037 | |
| 5038 | HttpRequestInfo request; |
| 5039 | request.method = "GET"; |
| 5040 | request.url = GURL("http://www.foo.com/"); |
| 5041 | request.load_flags = 0; |
| 5042 | |
| 5043 | MockRead data_reads[] = { |
| 5044 | MockRead("HTTP/1."), |
| 5045 | MockRead(false, OK), |
| 5046 | }; |
| 5047 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5048 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5049 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5050 | |
| 5051 | TestCompletionCallback callback; |
| 5052 | |
| 5053 | scoped_ptr<HttpTransaction> trans( |
| 5054 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5055 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5056 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5057 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5058 | |
| 5059 | rv = callback.WaitForResult(); |
| 5060 | EXPECT_EQ(OK, rv); |
| 5061 | |
| 5062 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5063 | EXPECT_TRUE(response != NULL); |
| 5064 | |
| 5065 | EXPECT_TRUE(response->headers != NULL); |
| 5066 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
| 5067 | |
| 5068 | std::string response_data; |
| 5069 | rv = ReadTransaction(trans.get(), &response_data); |
| 5070 | EXPECT_EQ(OK, rv); |
| 5071 | EXPECT_EQ("", response_data); |
| 5072 | } |
| 5073 | |
| 5074 | // Make sure that a dropped connection while draining the body for auth |
| 5075 | // restart does the right thing. |
| 5076 | TEST_F(HttpNetworkTransactionTest, DrainResetOK) { |
| 5077 | SessionDependencies session_deps; |
| 5078 | scoped_ptr<HttpTransaction> trans( |
| 5079 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5080 | |
| 5081 | HttpRequestInfo request; |
| 5082 | request.method = "GET"; |
| 5083 | request.url = GURL("http://www.google.com/"); |
| 5084 | request.load_flags = 0; |
| 5085 | |
| 5086 | MockWrite data_writes1[] = { |
| 5087 | MockWrite("GET / HTTP/1.1\r\n" |
| 5088 | "Host: www.google.com\r\n" |
| 5089 | "Connection: keep-alive\r\n\r\n"), |
| 5090 | }; |
| 5091 | |
| 5092 | MockRead data_reads1[] = { |
| 5093 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 5094 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 5095 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 5096 | MockRead("Content-Length: 14\r\n\r\n"), |
| 5097 | MockRead("Unauth"), |
| 5098 | MockRead(true, ERR_CONNECTION_RESET), |
| 5099 | }; |
| 5100 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5101 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 5102 | data_writes1, arraysize(data_writes1)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5103 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5104 | |
| 5105 | // After calling trans->RestartWithAuth(), this is the request we should |
| 5106 | // be issuing -- the final header line contains the credentials. |
| 5107 | MockWrite data_writes2[] = { |
| 5108 | MockWrite("GET / HTTP/1.1\r\n" |
| 5109 | "Host: www.google.com\r\n" |
| 5110 | "Connection: keep-alive\r\n" |
| 5111 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 5112 | }; |
| 5113 | |
| 5114 | // Lastly, the server responds with the actual content. |
| 5115 | MockRead data_reads2[] = { |
| 5116 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 5117 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 5118 | MockRead("Content-Length: 100\r\n\r\n"), |
| 5119 | MockRead(false, OK), |
| 5120 | }; |
| 5121 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5122 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 5123 | data_writes2, arraysize(data_writes2)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5124 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5125 | |
| 5126 | TestCompletionCallback callback1; |
| 5127 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5128 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5129 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5130 | |
| 5131 | rv = callback1.WaitForResult(); |
| 5132 | EXPECT_EQ(OK, rv); |
| 5133 | |
| 5134 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5135 | EXPECT_FALSE(response == NULL); |
| 5136 | |
| 5137 | // The password prompt info should have been set in response->auth_challenge. |
| 5138 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 5139 | |
| 5140 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
| 5141 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 5142 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 5143 | |
| 5144 | TestCompletionCallback callback2; |
| 5145 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 5146 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5147 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5148 | |
| 5149 | rv = callback2.WaitForResult(); |
| 5150 | EXPECT_EQ(OK, rv); |
| 5151 | |
| 5152 | response = trans->GetResponseInfo(); |
| 5153 | EXPECT_FALSE(response == NULL); |
| 5154 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 5155 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 5156 | } |
| 5157 | |
| 5158 | // Test HTTPS connections going through a proxy that sends extra data. |
| 5159 | TEST_F(HttpNetworkTransactionTest, HTTPSViaProxyWithExtraData) { |
| 5160 | SessionDependencies session_deps(CreateFixedProxyService("myproxy:70")); |
| 5161 | |
| 5162 | HttpRequestInfo request; |
| 5163 | request.method = "GET"; |
| 5164 | request.url = GURL("https://www.google.com/"); |
| 5165 | request.load_flags = 0; |
| 5166 | |
| 5167 | MockRead proxy_reads[] = { |
| 5168 | MockRead("HTTP/1.0 200 Connected\r\n\r\nExtra data"), |
| 5169 | MockRead(false, OK) |
| 5170 | }; |
| 5171 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5172 | StaticSocketDataProvider data(proxy_reads, arraysize(proxy_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5173 | SSLSocketDataProvider ssl(true, OK); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5174 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5175 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 5176 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5177 | |
| 5178 | TestCompletionCallback callback; |
| 5179 | |
| 5180 | session_deps.socket_factory.ResetNextMockIndexes(); |
| 5181 | |
| 5182 | scoped_ptr<HttpTransaction> trans( |
| 5183 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5184 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5185 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5186 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5187 | |
| 5188 | rv = callback.WaitForResult(); |
| 5189 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, rv); |
| 5190 | } |
| 5191 | |
[email protected] | e22e136 | 2009-11-23 21:31:12 | [diff] [blame] | 5192 | TEST_F(HttpNetworkTransactionTest, LargeContentLengthThenClose) { |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 5193 | SessionDependencies session_deps; |
| 5194 | scoped_ptr<HttpTransaction> trans( |
| 5195 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5196 | |
| 5197 | HttpRequestInfo request; |
| 5198 | request.method = "GET"; |
| 5199 | request.url = GURL("http://www.google.com/"); |
| 5200 | request.load_flags = 0; |
| 5201 | |
[email protected] | e22e136 | 2009-11-23 21:31:12 | [diff] [blame] | 5202 | MockRead data_reads[] = { |
| 5203 | MockRead("HTTP/1.0 200 OK\r\nContent-Length:6719476739\r\n\r\n"), |
| 5204 | MockRead(false, OK), |
| 5205 | }; |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 5206 | |
| 5207 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
| 5208 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 5209 | |
| 5210 | TestCompletionCallback callback; |
| 5211 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5212 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 5213 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5214 | |
| 5215 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 5216 | |
| 5217 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5218 | EXPECT_TRUE(response != NULL); |
| 5219 | |
| 5220 | EXPECT_TRUE(response->headers != NULL); |
| 5221 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
| 5222 | |
| 5223 | std::string response_data; |
| 5224 | rv = ReadTransaction(trans.get(), &response_data); |
| 5225 | EXPECT_EQ(ERR_CONNECTION_CLOSED, rv); |
[email protected] | e22e136 | 2009-11-23 21:31:12 | [diff] [blame] | 5226 | } |
| 5227 | |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 5228 | TEST_F(HttpNetworkTransactionTest, UploadFileSmallerThanLength) { |
| 5229 | SessionDependencies session_deps; |
| 5230 | scoped_ptr<HttpTransaction> trans( |
| 5231 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5232 | |
| 5233 | HttpRequestInfo request; |
| 5234 | request.method = "POST"; |
| 5235 | request.url = GURL("http://www.google.com/upload"); |
| 5236 | request.upload_data = new UploadData; |
| 5237 | request.load_flags = 0; |
| 5238 | |
| 5239 | FilePath temp_file_path; |
| 5240 | ASSERT_TRUE(file_util::CreateTemporaryFile(&temp_file_path)); |
| 5241 | const uint64 kFakeSize = 100000; // file is actually blank |
| 5242 | |
| 5243 | std::vector<UploadData::Element> elements; |
| 5244 | UploadData::Element element; |
| 5245 | element.SetToFilePath(temp_file_path); |
| 5246 | element.SetContentLength(kFakeSize); |
| 5247 | elements.push_back(element); |
[email protected] | 9349cfb | 2010-08-31 18:00:53 | [diff] [blame^] | 5248 | request.upload_data->SetElements(elements); |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 5249 | EXPECT_EQ(kFakeSize, request.upload_data->GetContentLength()); |
| 5250 | |
| 5251 | MockRead data_reads[] = { |
| 5252 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 5253 | MockRead("hello world"), |
| 5254 | MockRead(false, OK), |
| 5255 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5256 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 5257 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 5258 | |
| 5259 | TestCompletionCallback callback; |
| 5260 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5261 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 5262 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5263 | |
| 5264 | rv = callback.WaitForResult(); |
| 5265 | EXPECT_EQ(OK, rv); |
| 5266 | |
| 5267 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5268 | EXPECT_TRUE(response != NULL); |
| 5269 | |
| 5270 | EXPECT_TRUE(response->headers != NULL); |
| 5271 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
| 5272 | |
| 5273 | std::string response_data; |
| 5274 | rv = ReadTransaction(trans.get(), &response_data); |
| 5275 | EXPECT_EQ(OK, rv); |
| 5276 | EXPECT_EQ("hello world", response_data); |
| 5277 | |
| 5278 | file_util::Delete(temp_file_path, false); |
| 5279 | } |
| 5280 | |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 5281 | TEST_F(HttpNetworkTransactionTest, UploadUnreadableFile) { |
| 5282 | // If we try to upload an unreadable file, the network stack should report |
| 5283 | // the file size as zero and upload zero bytes for that file. |
| 5284 | SessionDependencies session_deps; |
| 5285 | scoped_ptr<HttpTransaction> trans( |
| 5286 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5287 | |
| 5288 | FilePath temp_file; |
| 5289 | ASSERT_TRUE(file_util::CreateTemporaryFile(&temp_file)); |
| 5290 | std::string temp_file_content("Unreadable file."); |
| 5291 | ASSERT_TRUE(file_util::WriteFile(temp_file, temp_file_content.c_str(), |
| 5292 | temp_file_content.length())); |
| 5293 | ASSERT_TRUE(file_util::MakeFileUnreadable(temp_file)); |
| 5294 | |
| 5295 | HttpRequestInfo request; |
| 5296 | request.method = "POST"; |
| 5297 | request.url = GURL("http://www.google.com/upload"); |
| 5298 | request.upload_data = new UploadData; |
| 5299 | request.load_flags = 0; |
| 5300 | |
| 5301 | std::vector<UploadData::Element> elements; |
| 5302 | UploadData::Element element; |
| 5303 | element.SetToFilePath(temp_file); |
| 5304 | elements.push_back(element); |
[email protected] | 9349cfb | 2010-08-31 18:00:53 | [diff] [blame^] | 5305 | request.upload_data->SetElements(elements); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 5306 | |
| 5307 | MockRead data_reads[] = { |
| 5308 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 5309 | MockRead(false, OK), |
| 5310 | }; |
| 5311 | MockWrite data_writes[] = { |
| 5312 | MockWrite("POST /upload HTTP/1.1\r\n" |
| 5313 | "Host: www.google.com\r\n" |
| 5314 | "Connection: keep-alive\r\n" |
| 5315 | "Content-Length: 0\r\n\r\n"), |
| 5316 | MockWrite(false, OK), |
| 5317 | }; |
| 5318 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), data_writes, |
| 5319 | arraysize(data_writes)); |
| 5320 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 5321 | |
| 5322 | TestCompletionCallback callback; |
| 5323 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5324 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 5325 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5326 | |
| 5327 | rv = callback.WaitForResult(); |
| 5328 | EXPECT_EQ(OK, rv); |
| 5329 | |
| 5330 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5331 | EXPECT_TRUE(response != NULL); |
| 5332 | EXPECT_TRUE(response->headers != NULL); |
| 5333 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
| 5334 | |
| 5335 | file_util::Delete(temp_file, false); |
| 5336 | } |
| 5337 | |
| 5338 | TEST_F(HttpNetworkTransactionTest, UnreadableUploadFileAfterAuthRestart) { |
| 5339 | SessionDependencies session_deps; |
| 5340 | scoped_ptr<HttpTransaction> trans( |
| 5341 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5342 | |
| 5343 | FilePath temp_file; |
| 5344 | ASSERT_TRUE(file_util::CreateTemporaryFile(&temp_file)); |
| 5345 | std::string temp_file_contents("Unreadable file."); |
| 5346 | std::string unreadable_contents(temp_file_contents.length(), '\0'); |
| 5347 | ASSERT_TRUE(file_util::WriteFile(temp_file, temp_file_contents.c_str(), |
| 5348 | temp_file_contents.length())); |
| 5349 | |
| 5350 | HttpRequestInfo request; |
| 5351 | request.method = "POST"; |
| 5352 | request.url = GURL("http://www.google.com/upload"); |
| 5353 | request.upload_data = new UploadData; |
| 5354 | request.load_flags = 0; |
| 5355 | |
| 5356 | std::vector<UploadData::Element> elements; |
| 5357 | UploadData::Element element; |
| 5358 | element.SetToFilePath(temp_file); |
| 5359 | elements.push_back(element); |
[email protected] | 9349cfb | 2010-08-31 18:00:53 | [diff] [blame^] | 5360 | request.upload_data->SetElements(elements); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 5361 | |
| 5362 | MockRead data_reads[] = { |
| 5363 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 5364 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 5365 | MockRead("Content-Length: 0\r\n\r\n"), // No response body. |
| 5366 | |
| 5367 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 5368 | MockRead("Content-Length: 0\r\n\r\n"), |
| 5369 | MockRead(false, OK), |
| 5370 | }; |
| 5371 | MockWrite data_writes[] = { |
| 5372 | MockWrite("POST /upload HTTP/1.1\r\n" |
| 5373 | "Host: www.google.com\r\n" |
| 5374 | "Connection: keep-alive\r\n" |
| 5375 | "Content-Length: 16\r\n\r\n"), |
| 5376 | MockWrite(false, temp_file_contents.c_str()), |
| 5377 | |
| 5378 | MockWrite("POST /upload HTTP/1.1\r\n" |
| 5379 | "Host: www.google.com\r\n" |
| 5380 | "Connection: keep-alive\r\n" |
| 5381 | "Content-Length: 16\r\n" |
| 5382 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 5383 | MockWrite(false, unreadable_contents.c_str(), temp_file_contents.length()), |
| 5384 | MockWrite(false, OK), |
| 5385 | }; |
| 5386 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), data_writes, |
| 5387 | arraysize(data_writes)); |
| 5388 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 5389 | |
| 5390 | TestCompletionCallback callback1; |
| 5391 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5392 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 5393 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5394 | |
| 5395 | rv = callback1.WaitForResult(); |
| 5396 | EXPECT_EQ(OK, rv); |
| 5397 | |
| 5398 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5399 | EXPECT_TRUE(response != NULL); |
| 5400 | EXPECT_TRUE(response->headers != NULL); |
| 5401 | EXPECT_EQ("HTTP/1.1 401 Unauthorized", response->headers->GetStatusLine()); |
| 5402 | |
| 5403 | // The password prompt info should have been set in response->auth_challenge. |
| 5404 | EXPECT_TRUE(response->auth_challenge.get() != NULL); |
| 5405 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
| 5406 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 5407 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 5408 | |
| 5409 | // Now make the file unreadable and try again. |
| 5410 | ASSERT_TRUE(file_util::MakeFileUnreadable(temp_file)); |
| 5411 | |
| 5412 | TestCompletionCallback callback2; |
| 5413 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 5414 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 5415 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5416 | |
| 5417 | rv = callback2.WaitForResult(); |
| 5418 | EXPECT_EQ(OK, rv); |
| 5419 | |
| 5420 | response = trans->GetResponseInfo(); |
| 5421 | EXPECT_TRUE(response != NULL); |
| 5422 | EXPECT_TRUE(response->headers != NULL); |
| 5423 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 5424 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 5425 | |
| 5426 | file_util::Delete(temp_file, false); |
| 5427 | } |
| 5428 | |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 5429 | // Tests that changes to Auth realms are treated like auth rejections. |
| 5430 | TEST_F(HttpNetworkTransactionTest, ChangeAuthRealms) { |
| 5431 | SessionDependencies session_deps; |
| 5432 | scoped_ptr<HttpTransaction> trans( |
| 5433 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5434 | |
| 5435 | HttpRequestInfo request; |
| 5436 | request.method = "GET"; |
| 5437 | request.url = GURL("http://www.google.com/"); |
| 5438 | request.load_flags = 0; |
| 5439 | |
| 5440 | // First transaction will request a resource and receive a Basic challenge |
| 5441 | // with realm="first_realm". |
| 5442 | MockWrite data_writes1[] = { |
| 5443 | MockWrite("GET / HTTP/1.1\r\n" |
| 5444 | "Host: www.google.com\r\n" |
| 5445 | "Connection: keep-alive\r\n" |
| 5446 | "\r\n"), |
| 5447 | }; |
| 5448 | MockRead data_reads1[] = { |
| 5449 | MockRead("HTTP/1.1 401 Unauthorized\r\n" |
| 5450 | "WWW-Authenticate: Basic realm=\"first_realm\"\r\n" |
| 5451 | "\r\n"), |
| 5452 | }; |
| 5453 | |
| 5454 | // After calling trans->RestartWithAuth(), provide an Authentication header |
| 5455 | // for first_realm. The server will reject and provide a challenge with |
| 5456 | // second_realm. |
| 5457 | MockWrite data_writes2[] = { |
| 5458 | MockWrite("GET / HTTP/1.1\r\n" |
| 5459 | "Host: www.google.com\r\n" |
| 5460 | "Connection: keep-alive\r\n" |
| 5461 | "Authorization: Basic Zmlyc3Q6YmF6\r\n" |
| 5462 | "\r\n"), |
| 5463 | }; |
| 5464 | MockRead data_reads2[] = { |
| 5465 | MockRead("HTTP/1.1 401 Unauthorized\r\n" |
| 5466 | "WWW-Authenticate: Basic realm=\"second_realm\"\r\n" |
| 5467 | "\r\n"), |
| 5468 | }; |
| 5469 | |
| 5470 | // This again fails, and goes back to first_realm. Make sure that the |
| 5471 | // entry is removed from cache. |
| 5472 | MockWrite data_writes3[] = { |
| 5473 | MockWrite("GET / HTTP/1.1\r\n" |
| 5474 | "Host: www.google.com\r\n" |
| 5475 | "Connection: keep-alive\r\n" |
| 5476 | "Authorization: Basic c2Vjb25kOmZvdQ==\r\n" |
| 5477 | "\r\n"), |
| 5478 | }; |
| 5479 | MockRead data_reads3[] = { |
| 5480 | MockRead("HTTP/1.1 401 Unauthorized\r\n" |
| 5481 | "WWW-Authenticate: Basic realm=\"first_realm\"\r\n" |
| 5482 | "\r\n"), |
| 5483 | }; |
| 5484 | |
| 5485 | // Try one last time (with the correct password) and get the resource. |
| 5486 | MockWrite data_writes4[] = { |
| 5487 | MockWrite("GET / HTTP/1.1\r\n" |
| 5488 | "Host: www.google.com\r\n" |
| 5489 | "Connection: keep-alive\r\n" |
| 5490 | "Authorization: Basic Zmlyc3Q6YmFy\r\n" |
| 5491 | "\r\n"), |
| 5492 | }; |
| 5493 | MockRead data_reads4[] = { |
| 5494 | MockRead("HTTP/1.1 200 OK\r\n" |
| 5495 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 5496 | "Content-Length: 100\r\n" |
| 5497 | "\r\n"), |
| 5498 | }; |
| 5499 | |
| 5500 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 5501 | data_writes1, arraysize(data_writes1)); |
| 5502 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 5503 | data_writes2, arraysize(data_writes2)); |
| 5504 | StaticSocketDataProvider data3(data_reads3, arraysize(data_reads3), |
| 5505 | data_writes3, arraysize(data_writes3)); |
| 5506 | StaticSocketDataProvider data4(data_reads4, arraysize(data_reads4), |
| 5507 | data_writes4, arraysize(data_writes4)); |
| 5508 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 5509 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 5510 | session_deps.socket_factory.AddSocketDataProvider(&data3); |
| 5511 | session_deps.socket_factory.AddSocketDataProvider(&data4); |
| 5512 | |
| 5513 | TestCompletionCallback callback1; |
| 5514 | |
| 5515 | // Issue the first request with Authorize headers. There should be a |
| 5516 | // password prompt for first_realm waiting to be filled in after the |
| 5517 | // transaction completes. |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5518 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 5519 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5520 | rv = callback1.WaitForResult(); |
| 5521 | EXPECT_EQ(OK, rv); |
| 5522 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5523 | ASSERT_FALSE(response == NULL); |
| 5524 | ASSERT_FALSE(response->auth_challenge.get() == NULL); |
| 5525 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
| 5526 | EXPECT_EQ(L"first_realm", response->auth_challenge->realm); |
| 5527 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 5528 | |
| 5529 | // Issue the second request with an incorrect password. There should be a |
| 5530 | // password prompt for second_realm waiting to be filled in after the |
| 5531 | // transaction completes. |
| 5532 | TestCompletionCallback callback2; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 5533 | rv = trans->RestartWithAuth(kFirst, kBaz, &callback2); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 5534 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5535 | rv = callback2.WaitForResult(); |
| 5536 | EXPECT_EQ(OK, rv); |
| 5537 | response = trans->GetResponseInfo(); |
| 5538 | ASSERT_FALSE(response == NULL); |
| 5539 | ASSERT_FALSE(response->auth_challenge.get() == NULL); |
| 5540 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
| 5541 | EXPECT_EQ(L"second_realm", response->auth_challenge->realm); |
| 5542 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 5543 | |
| 5544 | // Issue the third request with another incorrect password. There should be |
| 5545 | // a password prompt for first_realm waiting to be filled in. If the password |
| 5546 | // prompt is not present, it indicates that the HttpAuthCacheEntry for |
| 5547 | // first_realm was not correctly removed. |
| 5548 | TestCompletionCallback callback3; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 5549 | rv = trans->RestartWithAuth(kSecond, kFou, &callback3); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 5550 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5551 | rv = callback3.WaitForResult(); |
| 5552 | EXPECT_EQ(OK, rv); |
| 5553 | response = trans->GetResponseInfo(); |
| 5554 | ASSERT_FALSE(response == NULL); |
| 5555 | ASSERT_FALSE(response->auth_challenge.get() == NULL); |
| 5556 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
| 5557 | EXPECT_EQ(L"first_realm", response->auth_challenge->realm); |
| 5558 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 5559 | |
| 5560 | // Issue the fourth request with the correct password and username. |
| 5561 | TestCompletionCallback callback4; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 5562 | rv = trans->RestartWithAuth(kFirst, kBar, &callback4); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 5563 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5564 | rv = callback4.WaitForResult(); |
| 5565 | EXPECT_EQ(OK, rv); |
| 5566 | response = trans->GetResponseInfo(); |
| 5567 | ASSERT_FALSE(response == NULL); |
| 5568 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 5569 | } |
| 5570 | |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5571 | TEST_F(HttpNetworkTransactionTest, HonorAlternateProtocolHeader) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 5572 | HttpStreamFactory::set_next_protos("needs_to_be_set_for_this_test"); |
| 5573 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 5574 | |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5575 | SessionDependencies session_deps; |
| 5576 | |
| 5577 | MockRead data_reads[] = { |
| 5578 | MockRead("HTTP/1.1 200 OK\r\n"), |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 5579 | MockRead(kAlternateProtocolHttpHeader), |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5580 | MockRead("hello world"), |
| 5581 | MockRead(false, OK), |
| 5582 | }; |
| 5583 | |
| 5584 | HttpRequestInfo request; |
| 5585 | request.method = "GET"; |
| 5586 | request.url = GURL("http://www.google.com/"); |
| 5587 | request.load_flags = 0; |
| 5588 | |
| 5589 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
| 5590 | |
| 5591 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 5592 | |
| 5593 | TestCompletionCallback callback; |
| 5594 | |
| 5595 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 5596 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 5597 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5598 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5599 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 5600 | |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame] | 5601 | HostPortPair http_host_port_pair("www.google.com", 80); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5602 | const HttpAlternateProtocols& alternate_protocols = |
| 5603 | session->alternate_protocols(); |
| 5604 | EXPECT_FALSE( |
| 5605 | alternate_protocols.HasAlternateProtocolFor(http_host_port_pair)); |
| 5606 | |
| 5607 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 5608 | |
| 5609 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5610 | ASSERT_TRUE(response != NULL); |
| 5611 | ASSERT_TRUE(response->headers != NULL); |
| 5612 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 5613 | EXPECT_FALSE(response->was_fetched_via_spdy); |
| 5614 | EXPECT_FALSE(response->was_npn_negotiated); |
[email protected] | 193b0b89 | 2010-06-26 03:57:57 | [diff] [blame] | 5615 | EXPECT_FALSE(response->was_alternate_protocol_available); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5616 | |
| 5617 | std::string response_data; |
| 5618 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 5619 | EXPECT_EQ("hello world", response_data); |
| 5620 | |
| 5621 | ASSERT_TRUE(alternate_protocols.HasAlternateProtocolFor(http_host_port_pair)); |
| 5622 | const HttpAlternateProtocols::PortProtocolPair alternate = |
| 5623 | alternate_protocols.GetAlternateProtocolFor(http_host_port_pair); |
| 5624 | HttpAlternateProtocols::PortProtocolPair expected_alternate; |
| 5625 | expected_alternate.port = 443; |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 5626 | expected_alternate.protocol = HttpAlternateProtocols::NPN_SPDY_2; |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5627 | EXPECT_TRUE(expected_alternate.Equals(alternate)); |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 5628 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 5629 | HttpStreamFactory::set_use_alternate_protocols(false); |
| 5630 | HttpStreamFactory::set_next_protos(""); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5631 | } |
| 5632 | |
| 5633 | TEST_F(HttpNetworkTransactionTest, MarkBrokenAlternateProtocol) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 5634 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5635 | SessionDependencies session_deps; |
| 5636 | |
| 5637 | HttpRequestInfo request; |
| 5638 | request.method = "GET"; |
| 5639 | request.url = GURL("http://www.google.com/"); |
| 5640 | request.load_flags = 0; |
| 5641 | |
| 5642 | MockConnect mock_connect(true, ERR_CONNECTION_REFUSED); |
| 5643 | StaticSocketDataProvider first_data; |
| 5644 | first_data.set_connect_data(mock_connect); |
| 5645 | session_deps.socket_factory.AddSocketDataProvider(&first_data); |
| 5646 | |
| 5647 | MockRead data_reads[] = { |
| 5648 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 5649 | MockRead("hello world"), |
| 5650 | MockRead(true, OK), |
| 5651 | }; |
| 5652 | StaticSocketDataProvider second_data( |
| 5653 | data_reads, arraysize(data_reads), NULL, 0); |
| 5654 | session_deps.socket_factory.AddSocketDataProvider(&second_data); |
| 5655 | |
| 5656 | // TODO(willchan): Delete this extra data provider. It's necessary due to a |
| 5657 | // ClientSocketPoolBaseHelper bug that starts up too many ConnectJobs: |
| 5658 | // http://crbug.com/37454. |
| 5659 | session_deps.socket_factory.AddSocketDataProvider(&second_data); |
| 5660 | |
| 5661 | TestCompletionCallback callback; |
| 5662 | |
| 5663 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 5664 | |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame] | 5665 | HostPortPair http_host_port_pair("www.google.com", 80); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5666 | HttpAlternateProtocols* alternate_protocols = |
| 5667 | session->mutable_alternate_protocols(); |
| 5668 | alternate_protocols->SetAlternateProtocolFor( |
| 5669 | http_host_port_pair, 1234 /* port is ignored by MockConnect anyway */, |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 5670 | HttpAlternateProtocols::NPN_SPDY_2); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5671 | |
| 5672 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 5673 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5674 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5675 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5676 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 5677 | |
| 5678 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5679 | ASSERT_TRUE(response != NULL); |
| 5680 | ASSERT_TRUE(response->headers != NULL); |
| 5681 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 5682 | |
| 5683 | std::string response_data; |
| 5684 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 5685 | EXPECT_EQ("hello world", response_data); |
| 5686 | |
| 5687 | ASSERT_TRUE( |
| 5688 | alternate_protocols->HasAlternateProtocolFor(http_host_port_pair)); |
| 5689 | const HttpAlternateProtocols::PortProtocolPair alternate = |
| 5690 | alternate_protocols->GetAlternateProtocolFor(http_host_port_pair); |
| 5691 | EXPECT_EQ(HttpAlternateProtocols::BROKEN, alternate.protocol); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 5692 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5693 | } |
| 5694 | |
| 5695 | // TODO(willchan): Redo this test to use TLS/NPN=>SPDY. Currently, the code |
| 5696 | // says that it does SPDY, but it just does the TLS handshake, but the NPN |
| 5697 | // response does not indicate SPDY, so we just do standard HTTPS over the port. |
| 5698 | // We should add code such that we don't fallback to HTTPS, but fallback to HTTP |
| 5699 | // on the original port. |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 5700 | // TEST_F(HttpNetworkTransactionTest, UseAlternateProtocol) { |
| 5701 | // SessionDependencies session_deps; |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 5702 | // |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 5703 | // HttpRequestInfo request; |
| 5704 | // request.method = "GET"; |
| 5705 | // request.url = GURL("http://www.google.com/"); |
| 5706 | // request.load_flags = 0; |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 5707 | // |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 5708 | // MockRead data_reads[] = { |
| 5709 | // MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 5710 | // MockRead("hello world"), |
| 5711 | // MockRead(true, OK), |
| 5712 | // }; |
| 5713 | // StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
| 5714 | // session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 5715 | // |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 5716 | // SSLSocketDataProvider ssl(true, OK); |
| 5717 | // session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 5718 | // |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 5719 | // TestCompletionCallback callback; |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 5720 | // |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 5721 | // scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 5722 | // |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 5723 | // HostPortPair http_host_port_pair; |
| 5724 | // http_host_port_pair.host = "www.google.com"; |
| 5725 | // http_host_port_pair.port = 80; |
| 5726 | // HttpAlternateProtocols* alternate_protocols = |
| 5727 | // session->mutable_alternate_protocols(); |
| 5728 | // alternate_protocols->SetAlternateProtocolFor( |
| 5729 | // http_host_port_pair, 1234 /* port is ignored */, |
[email protected] | 31e2c69e | 2010-04-15 18:06:06 | [diff] [blame] | 5730 | // HttpAlternateProtocols::NPN_SPDY_1); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 5731 | // |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 5732 | // scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 5733 | // |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5734 | // int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 5735 | // EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5736 | // EXPECT_EQ(OK, callback.WaitForResult()); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 5737 | // |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 5738 | // const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5739 | // ASSERT_TRUE(response != NULL); |
| 5740 | // ASSERT_TRUE(response->headers != NULL); |
| 5741 | // EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 5742 | // |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 5743 | // std::string response_data; |
| 5744 | // ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 5745 | // EXPECT_EQ("hello world", response_data); |
| 5746 | // } |
| 5747 | |
| 5748 | TEST_F(HttpNetworkTransactionTest, FailNpnSpdyAndFallback) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 5749 | HttpStreamFactory::set_use_alternate_protocols(true); |
| 5750 | HttpStreamFactory::set_next_protos(kExpectedNPNString); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5751 | SessionDependencies session_deps; |
| 5752 | |
| 5753 | HttpRequestInfo request; |
| 5754 | request.method = "GET"; |
| 5755 | request.url = GURL("http://www.google.com/"); |
| 5756 | request.load_flags = 0; |
| 5757 | |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 5758 | StaticSocketDataProvider first_tcp_connect; |
| 5759 | session_deps.socket_factory.AddSocketDataProvider(&first_tcp_connect); |
| 5760 | |
| 5761 | SSLSocketDataProvider ssl(true, OK); |
| 5762 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 5763 | |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5764 | MockRead data_reads[] = { |
| 5765 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 5766 | MockRead("hello world"), |
| 5767 | MockRead(true, OK), |
| 5768 | }; |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 5769 | StaticSocketDataProvider fallback_data( |
| 5770 | data_reads, arraysize(data_reads), NULL, 0); |
| 5771 | session_deps.socket_factory.AddSocketDataProvider(&fallback_data); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5772 | |
| 5773 | TestCompletionCallback callback; |
| 5774 | |
| 5775 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 5776 | |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame] | 5777 | HostPortPair http_host_port_pair("www.google.com", 80); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5778 | HttpAlternateProtocols* alternate_protocols = |
| 5779 | session->mutable_alternate_protocols(); |
| 5780 | alternate_protocols->SetAlternateProtocolFor( |
| 5781 | http_host_port_pair, 1234 /* port is ignored */, |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 5782 | HttpAlternateProtocols::NPN_SPDY_2); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5783 | |
| 5784 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 5785 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5786 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5787 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5788 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 5789 | |
| 5790 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5791 | ASSERT_TRUE(response != NULL); |
| 5792 | ASSERT_TRUE(response->headers != NULL); |
| 5793 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 5794 | |
| 5795 | std::string response_data; |
| 5796 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 5797 | EXPECT_EQ("hello world", response_data); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 5798 | HttpStreamFactory::set_next_protos(""); |
| 5799 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5800 | } |
| 5801 | |
| 5802 | TEST_F(HttpNetworkTransactionTest, UseAlternateProtocolForNpnSpdy) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 5803 | HttpStreamFactory::set_use_alternate_protocols(true); |
| 5804 | HttpStreamFactory::set_next_protos(kExpectedNPNString); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5805 | SessionDependencies session_deps; |
| 5806 | |
| 5807 | HttpRequestInfo request; |
| 5808 | request.method = "GET"; |
| 5809 | request.url = GURL("http://www.google.com/"); |
| 5810 | request.load_flags = 0; |
| 5811 | |
| 5812 | MockRead data_reads[] = { |
| 5813 | MockRead("HTTP/1.1 200 OK\r\n"), |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 5814 | MockRead(kAlternateProtocolHttpHeader), |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5815 | MockRead("hello world"), |
| 5816 | MockRead(true, OK), |
| 5817 | }; |
| 5818 | |
| 5819 | StaticSocketDataProvider first_transaction( |
| 5820 | data_reads, arraysize(data_reads), NULL, 0); |
| 5821 | session_deps.socket_factory.AddSocketDataProvider(&first_transaction); |
| 5822 | |
| 5823 | SSLSocketDataProvider ssl(true, OK); |
| 5824 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 5825 | ssl.next_proto = "spdy/2"; |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 5826 | ssl.was_npn_negotiated = true; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5827 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 5828 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 5829 | scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); |
[email protected] | e7f7509 | 2010-07-01 22:39:13 | [diff] [blame] | 5830 | MockWrite spdy_writes[] = { CreateMockWrite(*req) }; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5831 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 5832 | scoped_ptr<spdy::SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 5833 | scoped_ptr<spdy::SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5834 | MockRead spdy_reads[] = { |
[email protected] | e7f7509 | 2010-07-01 22:39:13 | [diff] [blame] | 5835 | CreateMockRead(*resp), |
| 5836 | CreateMockRead(*data), |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5837 | MockRead(true, 0, 0), |
| 5838 | }; |
| 5839 | |
| 5840 | scoped_refptr<DelayedSocketData> spdy_data( |
| 5841 | new DelayedSocketData( |
| 5842 | 1, // wait for one write to finish before reading. |
| 5843 | spdy_reads, arraysize(spdy_reads), |
| 5844 | spdy_writes, arraysize(spdy_writes))); |
| 5845 | session_deps.socket_factory.AddSocketDataProvider(spdy_data); |
| 5846 | |
| 5847 | TestCompletionCallback callback; |
| 5848 | |
| 5849 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 5850 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 5851 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5852 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5853 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5854 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 5855 | |
| 5856 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5857 | ASSERT_TRUE(response != NULL); |
| 5858 | ASSERT_TRUE(response->headers != NULL); |
| 5859 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 5860 | |
| 5861 | std::string response_data; |
| 5862 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 5863 | EXPECT_EQ("hello world", response_data); |
| 5864 | |
| 5865 | trans.reset(new HttpNetworkTransaction(session)); |
| 5866 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5867 | rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5868 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5869 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 5870 | |
| 5871 | response = trans->GetResponseInfo(); |
| 5872 | ASSERT_TRUE(response != NULL); |
| 5873 | ASSERT_TRUE(response->headers != NULL); |
| 5874 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 5875 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 5876 | EXPECT_TRUE(response->was_npn_negotiated); |
[email protected] | 193b0b89 | 2010-06-26 03:57:57 | [diff] [blame] | 5877 | EXPECT_TRUE(response->was_alternate_protocol_available); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5878 | |
| 5879 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 5880 | EXPECT_EQ("hello!", response_data); |
| 5881 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 5882 | HttpStreamFactory::set_next_protos(""); |
| 5883 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5884 | } |
| 5885 | |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 5886 | class CapturingProxyResolver : public ProxyResolver { |
| 5887 | public: |
| 5888 | CapturingProxyResolver() : ProxyResolver(false /* expects_pac_bytes */) {} |
| 5889 | virtual ~CapturingProxyResolver() {} |
| 5890 | |
| 5891 | virtual int GetProxyForURL(const GURL& url, |
| 5892 | ProxyInfo* results, |
| 5893 | CompletionCallback* callback, |
| 5894 | RequestHandle* request, |
| 5895 | const BoundNetLog& net_log) { |
[email protected] | fae7669f | 2010-08-02 21:49:40 | [diff] [blame] | 5896 | ProxyServer proxy_server(ProxyServer::SCHEME_HTTP, |
| 5897 | HostPortPair("myproxy", 80)); |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 5898 | results->UseProxyServer(proxy_server); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 5899 | resolved_.push_back(url); |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 5900 | return OK; |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 5901 | } |
| 5902 | |
| 5903 | virtual void CancelRequest(RequestHandle request) { |
| 5904 | NOTREACHED(); |
| 5905 | } |
| 5906 | |
[email protected] | 2447640 | 2010-07-20 20:55:17 | [diff] [blame] | 5907 | virtual int SetPacScript(const scoped_refptr<ProxyResolverScriptData>&, |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 5908 | CompletionCallback* /*callback*/) { |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 5909 | return OK; |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 5910 | } |
| 5911 | |
[email protected] | 2447640 | 2010-07-20 20:55:17 | [diff] [blame] | 5912 | const std::vector<GURL>& resolved() const { return resolved_; } |
| 5913 | |
| 5914 | private: |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 5915 | std::vector<GURL> resolved_; |
| 5916 | |
| 5917 | DISALLOW_COPY_AND_ASSIGN(CapturingProxyResolver); |
| 5918 | }; |
| 5919 | |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 5920 | TEST_F(HttpNetworkTransactionTest, UseAlternateProtocolForTunneledNpnSpdy) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 5921 | HttpStreamFactory::set_use_alternate_protocols(true); |
| 5922 | HttpStreamFactory::set_next_protos(kExpectedNPNString); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 5923 | |
| 5924 | ProxyConfig proxy_config; |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 5925 | proxy_config.set_auto_detect(true); |
| 5926 | proxy_config.set_pac_url(GURL("http://fooproxyurl")); |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 5927 | |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 5928 | CapturingProxyResolver* capturing_proxy_resolver = |
| 5929 | new CapturingProxyResolver(); |
[email protected] | 66761b95 | 2010-06-25 21:30:38 | [diff] [blame] | 5930 | SessionDependencies session_deps(new ProxyService( |
| 5931 | new ProxyConfigServiceFixed(proxy_config), capturing_proxy_resolver, |
| 5932 | NULL)); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 5933 | |
| 5934 | HttpRequestInfo request; |
| 5935 | request.method = "GET"; |
| 5936 | request.url = GURL("http://www.google.com/"); |
| 5937 | request.load_flags = 0; |
| 5938 | |
| 5939 | MockRead data_reads[] = { |
| 5940 | MockRead("HTTP/1.1 200 OK\r\n"), |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 5941 | MockRead(kAlternateProtocolHttpHeader), |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 5942 | MockRead("hello world"), |
| 5943 | MockRead(true, OK), |
| 5944 | }; |
| 5945 | |
| 5946 | StaticSocketDataProvider first_transaction( |
| 5947 | data_reads, arraysize(data_reads), NULL, 0); |
| 5948 | session_deps.socket_factory.AddSocketDataProvider(&first_transaction); |
| 5949 | |
| 5950 | SSLSocketDataProvider ssl(true, OK); |
| 5951 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 5952 | ssl.next_proto = "spdy/2"; |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 5953 | ssl.was_npn_negotiated = true; |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 5954 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 5955 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 5956 | scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 5957 | MockWrite spdy_writes[] = { |
| 5958 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 5959 | "Host: www.google.com\r\n" |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 5960 | "Proxy-Connection: keep-alive\r\n\r\n"), // 0 |
[email protected] | e7f7509 | 2010-07-01 22:39:13 | [diff] [blame] | 5961 | CreateMockWrite(*req) // 3 |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 5962 | }; |
| 5963 | |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 5964 | const char kCONNECTResponse[] = "HTTP/1.1 200 Connected\r\n\r\n"; |
| 5965 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 5966 | scoped_ptr<spdy::SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 5967 | scoped_ptr<spdy::SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 5968 | MockRead spdy_reads[] = { |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 5969 | MockRead(true, kCONNECTResponse, arraysize(kCONNECTResponse) - 1, 1), // 1 |
[email protected] | e7f7509 | 2010-07-01 22:39:13 | [diff] [blame] | 5970 | CreateMockRead(*resp.get(), 4), // 2, 4 |
| 5971 | CreateMockRead(*data.get(), 4), // 5 |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 5972 | MockRead(true, 0, 0, 4), // 6 |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 5973 | }; |
| 5974 | |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 5975 | scoped_refptr<OrderedSocketData> spdy_data( |
| 5976 | new OrderedSocketData( |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 5977 | spdy_reads, arraysize(spdy_reads), |
| 5978 | spdy_writes, arraysize(spdy_writes))); |
| 5979 | session_deps.socket_factory.AddSocketDataProvider(spdy_data); |
| 5980 | |
| 5981 | TestCompletionCallback callback; |
| 5982 | |
| 5983 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 5984 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 5985 | |
| 5986 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 5987 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5988 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 5989 | |
| 5990 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5991 | ASSERT_TRUE(response != NULL); |
| 5992 | ASSERT_TRUE(response->headers != NULL); |
| 5993 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 5994 | EXPECT_FALSE(response->was_fetched_via_spdy); |
| 5995 | EXPECT_FALSE(response->was_npn_negotiated); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 5996 | |
| 5997 | std::string response_data; |
| 5998 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 5999 | EXPECT_EQ("hello world", response_data); |
| 6000 | |
| 6001 | trans.reset(new HttpNetworkTransaction(session)); |
| 6002 | |
| 6003 | rv = trans->Start(&request, &callback, BoundNetLog()); |
| 6004 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6005 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6006 | |
| 6007 | response = trans->GetResponseInfo(); |
| 6008 | ASSERT_TRUE(response != NULL); |
| 6009 | ASSERT_TRUE(response->headers != NULL); |
| 6010 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 6011 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 6012 | EXPECT_TRUE(response->was_npn_negotiated); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6013 | |
| 6014 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 6015 | EXPECT_EQ("hello!", response_data); |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 6016 | ASSERT_EQ(2u, capturing_proxy_resolver->resolved().size()); |
| 6017 | EXPECT_EQ("http://www.google.com/", |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6018 | capturing_proxy_resolver->resolved()[0].spec()); |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 6019 | EXPECT_EQ("https://www.google.com/", |
| 6020 | capturing_proxy_resolver->resolved()[1].spec()); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6021 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6022 | HttpStreamFactory::set_next_protos(""); |
| 6023 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6024 | } |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6025 | |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6026 | TEST_F(HttpNetworkTransactionTest, |
| 6027 | UseAlternateProtocolForNpnSpdyWithExistingSpdySession) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6028 | HttpStreamFactory::set_use_alternate_protocols(true); |
| 6029 | HttpStreamFactory::set_next_protos(kExpectedNPNString); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6030 | SessionDependencies session_deps; |
| 6031 | |
| 6032 | HttpRequestInfo request; |
| 6033 | request.method = "GET"; |
| 6034 | request.url = GURL("http://www.google.com/"); |
| 6035 | request.load_flags = 0; |
| 6036 | |
| 6037 | MockRead data_reads[] = { |
| 6038 | MockRead("HTTP/1.1 200 OK\r\n"), |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 6039 | MockRead(kAlternateProtocolHttpHeader), |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6040 | MockRead("hello world"), |
| 6041 | MockRead(true, OK), |
| 6042 | }; |
| 6043 | |
| 6044 | StaticSocketDataProvider first_transaction( |
| 6045 | data_reads, arraysize(data_reads), NULL, 0); |
| 6046 | session_deps.socket_factory.AddSocketDataProvider(&first_transaction); |
| 6047 | |
| 6048 | SSLSocketDataProvider ssl(true, OK); |
| 6049 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 6050 | ssl.next_proto = "spdy/2"; |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 6051 | ssl.was_npn_negotiated = true; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6052 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 6053 | // Make sure we use ssl for spdy here. |
| 6054 | SpdySession::SetSSLMode(true); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6055 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 6056 | scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); |
[email protected] | e7f7509 | 2010-07-01 22:39:13 | [diff] [blame] | 6057 | MockWrite spdy_writes[] = { CreateMockWrite(*req) }; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6058 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 6059 | scoped_ptr<spdy::SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 6060 | scoped_ptr<spdy::SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6061 | MockRead spdy_reads[] = { |
[email protected] | e7f7509 | 2010-07-01 22:39:13 | [diff] [blame] | 6062 | CreateMockRead(*resp), |
| 6063 | CreateMockRead(*data), |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6064 | MockRead(true, 0, 0), |
| 6065 | }; |
| 6066 | |
| 6067 | scoped_refptr<DelayedSocketData> spdy_data( |
| 6068 | new DelayedSocketData( |
| 6069 | 1, // wait for one write to finish before reading. |
| 6070 | spdy_reads, arraysize(spdy_reads), |
| 6071 | spdy_writes, arraysize(spdy_writes))); |
| 6072 | session_deps.socket_factory.AddSocketDataProvider(spdy_data); |
| 6073 | |
| 6074 | TestCompletionCallback callback; |
| 6075 | |
| 6076 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 6077 | |
| 6078 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 6079 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 6080 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6081 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6082 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6083 | |
| 6084 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 6085 | ASSERT_TRUE(response != NULL); |
| 6086 | ASSERT_TRUE(response->headers != NULL); |
| 6087 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 6088 | |
| 6089 | std::string response_data; |
| 6090 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 6091 | EXPECT_EQ("hello world", response_data); |
| 6092 | |
| 6093 | // Set up an initial SpdySession in the pool to reuse. |
[email protected] | 31e68d7 | 2010-08-25 06:36:58 | [diff] [blame] | 6094 | HostPortProxyPair pair(HostPortPair("www.google.com", 443), |
| 6095 | ProxyServer::Direct()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6096 | scoped_refptr<SpdySession> spdy_session = |
[email protected] | b261d0e | 2010-08-02 19:13:24 | [diff] [blame] | 6097 | session->spdy_session_pool()->Get(pair, session, BoundNetLog()); |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 6098 | scoped_refptr<TCPSocketParams> tcp_params = |
| 6099 | new TCPSocketParams("www.google.com", 443, MEDIUM, GURL(), false); |
[email protected] | 635909f | 2010-05-12 18:19:36 | [diff] [blame] | 6100 | spdy_session->Connect("www.google.com:443", tcp_params, MEDIUM); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6101 | trans.reset(new HttpNetworkTransaction(session)); |
| 6102 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 6103 | rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6104 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6105 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6106 | |
| 6107 | response = trans->GetResponseInfo(); |
| 6108 | ASSERT_TRUE(response != NULL); |
| 6109 | ASSERT_TRUE(response->headers != NULL); |
| 6110 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 6111 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 6112 | EXPECT_TRUE(response->was_npn_negotiated); |
[email protected] | 193b0b89 | 2010-06-26 03:57:57 | [diff] [blame] | 6113 | EXPECT_TRUE(response->was_alternate_protocol_available); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6114 | |
| 6115 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 6116 | EXPECT_EQ("hello!", response_data); |
| 6117 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6118 | HttpStreamFactory::set_next_protos(""); |
| 6119 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6120 | } |
| 6121 | |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 6122 | // GenerateAuthToken is a mighty big test. |
| 6123 | // It tests all permutation of GenerateAuthToken behavior: |
| 6124 | // - Synchronous and Asynchronous completion. |
| 6125 | // - OK or error on completion. |
| 6126 | // - Direct connection, non-authenticating proxy, and authenticating proxy. |
| 6127 | // - HTTP or HTTPS backend (to include proxy tunneling). |
| 6128 | // - Non-authenticating and authenticating backend. |
| 6129 | // |
| 6130 | // In all, there are 44 reasonable permuations (for example, if there are |
| 6131 | // problems generating an auth token for an authenticating proxy, we don't |
| 6132 | // need to test all permutations of the backend server). |
| 6133 | // |
| 6134 | // The test proceeds by going over each of the configuration cases, and |
| 6135 | // potentially running up to three rounds in each of the tests. The TestConfig |
| 6136 | // specifies both the configuration for the test as well as the expectations |
| 6137 | // for the results. |
| 6138 | TEST_F(HttpNetworkTransactionTest, GenerateAuthToken) { |
| 6139 | const char* kServer = "http://www.example.com"; |
| 6140 | const char* kSecureServer = "https://www.example.com"; |
| 6141 | const char* kProxy = "myproxy:70"; |
| 6142 | const int kAuthErr = ERR_INVALID_AUTH_CREDENTIALS; |
| 6143 | |
| 6144 | enum AuthTiming { |
| 6145 | AUTH_NONE, |
| 6146 | AUTH_SYNC, |
| 6147 | AUTH_ASYNC, |
| 6148 | }; |
| 6149 | |
| 6150 | const MockWrite kGet( |
| 6151 | "GET / HTTP/1.1\r\n" |
| 6152 | "Host: www.example.com\r\n" |
| 6153 | "Connection: keep-alive\r\n\r\n"); |
| 6154 | const MockWrite kGetProxy( |
| 6155 | "GET http://www.example.com/ HTTP/1.1\r\n" |
| 6156 | "Host: www.example.com\r\n" |
| 6157 | "Proxy-Connection: keep-alive\r\n\r\n"); |
| 6158 | const MockWrite kGetAuth( |
| 6159 | "GET / HTTP/1.1\r\n" |
| 6160 | "Host: www.example.com\r\n" |
| 6161 | "Connection: keep-alive\r\n" |
| 6162 | "Authorization: auth_token\r\n\r\n"); |
| 6163 | const MockWrite kGetProxyAuth( |
| 6164 | "GET http://www.example.com/ HTTP/1.1\r\n" |
| 6165 | "Host: www.example.com\r\n" |
| 6166 | "Proxy-Connection: keep-alive\r\n" |
| 6167 | "Proxy-Authorization: auth_token\r\n\r\n"); |
| 6168 | const MockWrite kGetAuthThroughProxy( |
| 6169 | "GET http://www.example.com/ HTTP/1.1\r\n" |
| 6170 | "Host: www.example.com\r\n" |
| 6171 | "Proxy-Connection: keep-alive\r\n" |
| 6172 | "Authorization: auth_token\r\n\r\n"); |
| 6173 | const MockWrite kGetAuthWithProxyAuth( |
| 6174 | "GET http://www.example.com/ HTTP/1.1\r\n" |
| 6175 | "Host: www.example.com\r\n" |
| 6176 | "Proxy-Connection: keep-alive\r\n" |
| 6177 | "Proxy-Authorization: auth_token\r\n" |
| 6178 | "Authorization: auth_token\r\n\r\n"); |
| 6179 | const MockWrite kConnect( |
| 6180 | "CONNECT www.example.com:443 HTTP/1.1\r\n" |
| 6181 | "Host: www.example.com\r\n" |
| 6182 | "Proxy-Connection: keep-alive\r\n\r\n"); |
| 6183 | const MockWrite kConnectProxyAuth( |
| 6184 | "CONNECT www.example.com:443 HTTP/1.1\r\n" |
| 6185 | "Host: www.example.com\r\n" |
| 6186 | "Proxy-Connection: keep-alive\r\n" |
| 6187 | "Proxy-Authorization: auth_token\r\n\r\n"); |
| 6188 | |
| 6189 | const MockRead kSuccess( |
| 6190 | "HTTP/1.1 200 OK\r\n" |
| 6191 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 6192 | "Content-Length: 3\r\n\r\n" |
| 6193 | "Yes"); |
| 6194 | const MockRead kFailure( |
| 6195 | "Should not be called."); |
| 6196 | const MockRead kServerChallenge( |
| 6197 | "HTTP/1.1 401 Unauthorized\r\n" |
| 6198 | "WWW-Authenticate: Mock realm=server\r\n" |
| 6199 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 6200 | "Content-Length: 14\r\n\r\n" |
| 6201 | "Unauthorized\r\n"); |
| 6202 | const MockRead kProxyChallenge( |
| 6203 | "HTTP/1.1 407 Unauthorized\r\n" |
| 6204 | "Proxy-Authenticate: Mock realm=proxy\r\n" |
| 6205 | "Proxy-Connection: close\r\n" |
| 6206 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 6207 | "Content-Length: 14\r\n\r\n" |
| 6208 | "Unauthorized\r\n"); |
| 6209 | const MockRead kProxyConnected( |
| 6210 | "HTTP/1.1 200 Connection Established\r\n\r\n"); |
| 6211 | |
| 6212 | // NOTE(cbentzel): I wanted TestReadWriteRound to be a simple struct with |
| 6213 | // no constructors, but the C++ compiler on Windows warns about |
| 6214 | // unspecified data in compound literals. So, moved to using constructors, |
| 6215 | // and TestRound's created with the default constructor should not be used. |
| 6216 | struct TestRound { |
| 6217 | TestRound() |
| 6218 | : expected_rv(ERR_UNEXPECTED), |
| 6219 | extra_write(NULL), |
| 6220 | extra_read(NULL) { |
| 6221 | } |
| 6222 | TestRound(const MockWrite& write_arg, const MockRead& read_arg, |
| 6223 | int expected_rv_arg) |
| 6224 | : write(write_arg), |
| 6225 | read(read_arg), |
| 6226 | expected_rv(expected_rv_arg), |
| 6227 | extra_write(NULL), |
| 6228 | extra_read(NULL) { |
| 6229 | } |
| 6230 | TestRound(const MockWrite& write_arg, const MockRead& read_arg, |
| 6231 | int expected_rv_arg, const MockWrite* extra_write_arg, |
| 6232 | const MockWrite* extra_read_arg) |
| 6233 | : write(write_arg), |
| 6234 | read(read_arg), |
| 6235 | expected_rv(expected_rv_arg), |
| 6236 | extra_write(extra_write_arg), |
| 6237 | extra_read(extra_read_arg) { |
| 6238 | } |
| 6239 | MockWrite write; |
| 6240 | MockRead read; |
| 6241 | int expected_rv; |
| 6242 | const MockWrite* extra_write; |
| 6243 | const MockRead* extra_read; |
| 6244 | }; |
| 6245 | |
| 6246 | static const int kNoSSL = 500; |
| 6247 | |
| 6248 | struct TestConfig { |
| 6249 | const char* proxy_url; |
| 6250 | AuthTiming proxy_auth_timing; |
| 6251 | int proxy_auth_rv; |
| 6252 | const char* server_url; |
| 6253 | AuthTiming server_auth_timing; |
| 6254 | int server_auth_rv; |
| 6255 | int num_auth_rounds; |
| 6256 | int first_ssl_round; |
| 6257 | TestRound rounds[3]; |
| 6258 | } test_configs[] = { |
| 6259 | // Non-authenticating HTTP server with a direct connection. |
| 6260 | { NULL, AUTH_NONE, OK, kServer, AUTH_NONE, OK, 1, kNoSSL, |
| 6261 | { TestRound(kGet, kSuccess, OK)}}, |
| 6262 | // Authenticating HTTP server with a direct connection. |
| 6263 | { NULL, AUTH_NONE, OK, kServer, AUTH_SYNC, OK, 2, kNoSSL, |
| 6264 | { TestRound(kGet, kServerChallenge, OK), |
| 6265 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 6266 | { NULL, AUTH_NONE, OK, kServer, AUTH_SYNC, kAuthErr, 2, kNoSSL, |
| 6267 | { TestRound(kGet, kServerChallenge, OK), |
| 6268 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 6269 | { NULL, AUTH_NONE, OK, kServer, AUTH_ASYNC, OK, 2, kNoSSL, |
| 6270 | { TestRound(kGet, kServerChallenge, OK), |
| 6271 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 6272 | { NULL, AUTH_NONE, OK, kServer, AUTH_ASYNC, kAuthErr, 2, kNoSSL, |
| 6273 | { TestRound(kGet, kServerChallenge, OK), |
| 6274 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 6275 | // Non-authenticating HTTP server through a non-authenticating proxy. |
| 6276 | { kProxy, AUTH_NONE, OK, kServer, AUTH_NONE, OK, 1, kNoSSL, |
| 6277 | { TestRound(kGetProxy, kSuccess, OK)}}, |
| 6278 | // Authenticating HTTP server through a non-authenticating proxy. |
| 6279 | { kProxy, AUTH_NONE, OK, kServer, AUTH_SYNC, OK, 2, kNoSSL, |
| 6280 | { TestRound(kGetProxy, kServerChallenge, OK), |
| 6281 | TestRound(kGetAuthThroughProxy, kSuccess, OK)}}, |
| 6282 | { kProxy, AUTH_NONE, OK, kServer, AUTH_SYNC, kAuthErr, 2, kNoSSL, |
| 6283 | { TestRound(kGetProxy, kServerChallenge, OK), |
| 6284 | TestRound(kGetAuthThroughProxy, kFailure, kAuthErr)}}, |
| 6285 | { kProxy, AUTH_NONE, OK, kServer, AUTH_ASYNC, OK, 2, kNoSSL, |
| 6286 | { TestRound(kGetProxy, kServerChallenge, OK), |
| 6287 | TestRound(kGetAuthThroughProxy, kSuccess, OK)}}, |
| 6288 | { kProxy, AUTH_NONE, OK, kServer, AUTH_ASYNC, kAuthErr, 2, kNoSSL, |
| 6289 | { TestRound(kGetProxy, kServerChallenge, OK), |
| 6290 | TestRound(kGetAuthThroughProxy, kFailure, kAuthErr)}}, |
| 6291 | // Non-authenticating HTTP server through an authenticating proxy. |
| 6292 | { kProxy, AUTH_SYNC, OK, kServer, AUTH_NONE, OK, 2, kNoSSL, |
| 6293 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 6294 | TestRound(kGetProxyAuth, kSuccess, OK)}}, |
| 6295 | { kProxy, AUTH_SYNC, kAuthErr, kServer, AUTH_NONE, OK, 2, kNoSSL, |
| 6296 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 6297 | TestRound(kGetProxyAuth, kFailure, kAuthErr)}}, |
| 6298 | { kProxy, AUTH_ASYNC, OK, kServer, AUTH_NONE, OK, 2, kNoSSL, |
| 6299 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 6300 | TestRound(kGetProxyAuth, kSuccess, OK)}}, |
| 6301 | { kProxy, AUTH_ASYNC, kAuthErr, kServer, AUTH_NONE, OK, 2, kNoSSL, |
| 6302 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 6303 | TestRound(kGetProxyAuth, kFailure, kAuthErr)}}, |
| 6304 | // Authenticating HTTP server through an authenticating proxy. |
| 6305 | { kProxy, AUTH_SYNC, OK, kServer, AUTH_SYNC, OK, 3, kNoSSL, |
| 6306 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 6307 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 6308 | TestRound(kGetAuthWithProxyAuth, kSuccess, OK)}}, |
| 6309 | { kProxy, AUTH_SYNC, OK, kServer, AUTH_SYNC, kAuthErr, 3, kNoSSL, |
| 6310 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 6311 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 6312 | TestRound(kGetAuthWithProxyAuth, kFailure, kAuthErr)}}, |
| 6313 | { kProxy, AUTH_ASYNC, OK, kServer, AUTH_SYNC, OK, 3, kNoSSL, |
| 6314 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 6315 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 6316 | TestRound(kGetAuthWithProxyAuth, kSuccess, OK)}}, |
| 6317 | { kProxy, AUTH_ASYNC, OK, kServer, AUTH_SYNC, kAuthErr, 3, kNoSSL, |
| 6318 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 6319 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 6320 | TestRound(kGetAuthWithProxyAuth, kFailure, kAuthErr)}}, |
| 6321 | { kProxy, AUTH_SYNC, OK, kServer, AUTH_ASYNC, OK, 3, kNoSSL, |
| 6322 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 6323 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 6324 | TestRound(kGetAuthWithProxyAuth, kSuccess, OK)}}, |
| 6325 | { kProxy, AUTH_SYNC, OK, kServer, AUTH_ASYNC, kAuthErr, 3, kNoSSL, |
| 6326 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 6327 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 6328 | TestRound(kGetAuthWithProxyAuth, kFailure, kAuthErr)}}, |
| 6329 | { kProxy, AUTH_ASYNC, OK, kServer, AUTH_ASYNC, OK, 3, kNoSSL, |
| 6330 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 6331 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 6332 | TestRound(kGetAuthWithProxyAuth, kSuccess, OK)}}, |
| 6333 | { kProxy, AUTH_ASYNC, OK, kServer, AUTH_ASYNC, kAuthErr, 3, kNoSSL, |
| 6334 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 6335 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 6336 | TestRound(kGetAuthWithProxyAuth, kFailure, kAuthErr)}}, |
| 6337 | // Non-authenticating HTTPS server with a direct connection. |
| 6338 | { NULL, AUTH_NONE, OK, kSecureServer, AUTH_NONE, OK, 1, 0, |
| 6339 | { TestRound(kGet, kSuccess, OK)}}, |
| 6340 | // Authenticating HTTPS server with a direct connection. |
| 6341 | { NULL, AUTH_NONE, OK, kSecureServer, AUTH_SYNC, OK, 2, 0, |
| 6342 | { TestRound(kGet, kServerChallenge, OK), |
| 6343 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 6344 | { NULL, AUTH_NONE, OK, kSecureServer, AUTH_SYNC, kAuthErr, 2, 0, |
| 6345 | { TestRound(kGet, kServerChallenge, OK), |
| 6346 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 6347 | { NULL, AUTH_NONE, OK, kSecureServer, AUTH_ASYNC, OK, 2, 0, |
| 6348 | { TestRound(kGet, kServerChallenge, OK), |
| 6349 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 6350 | { NULL, AUTH_NONE, OK, kSecureServer, AUTH_ASYNC, kAuthErr, 2, 0, |
| 6351 | { TestRound(kGet, kServerChallenge, OK), |
| 6352 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 6353 | // Non-authenticating HTTPS server with a non-authenticating proxy. |
| 6354 | { kProxy, AUTH_NONE, OK, kSecureServer, AUTH_NONE, OK, 1, 0, |
| 6355 | { TestRound(kConnect, kProxyConnected, OK, &kGet, &kSuccess)}}, |
| 6356 | // Authenticating HTTPS server through a non-authenticating proxy. |
| 6357 | { kProxy, AUTH_NONE, OK, kSecureServer, AUTH_SYNC, OK, 2, 0, |
| 6358 | { TestRound(kConnect, kProxyConnected, OK, &kGet, &kServerChallenge), |
| 6359 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 6360 | { kProxy, AUTH_NONE, OK, kSecureServer, AUTH_SYNC, kAuthErr, 2, 0, |
| 6361 | { TestRound(kConnect, kProxyConnected, OK, &kGet, &kServerChallenge), |
| 6362 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 6363 | { kProxy, AUTH_NONE, OK, kSecureServer, AUTH_ASYNC, OK, 2, 0, |
| 6364 | { TestRound(kConnect, kProxyConnected, OK, &kGet, &kServerChallenge), |
| 6365 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 6366 | { kProxy, AUTH_NONE, OK, kSecureServer, AUTH_ASYNC, kAuthErr, 2, 0, |
| 6367 | { TestRound(kConnect, kProxyConnected, OK, &kGet, &kServerChallenge), |
| 6368 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 6369 | // Non-Authenticating HTTPS server through an authenticating proxy. |
| 6370 | { kProxy, AUTH_SYNC, OK, kSecureServer, AUTH_NONE, OK, 2, 1, |
| 6371 | { TestRound(kConnect, kProxyChallenge, OK), |
| 6372 | TestRound(kConnectProxyAuth, kProxyConnected, OK, &kGet, &kSuccess)}}, |
| 6373 | { kProxy, AUTH_SYNC, kAuthErr, kSecureServer, AUTH_NONE, OK, 2, kNoSSL, |
| 6374 | { TestRound(kConnect, kProxyChallenge, OK), |
| 6375 | TestRound(kConnectProxyAuth, kFailure, kAuthErr)}}, |
| 6376 | { kProxy, AUTH_ASYNC, OK, kSecureServer, AUTH_NONE, OK, 2, 1, |
| 6377 | { TestRound(kConnect, kProxyChallenge, OK), |
| 6378 | TestRound(kConnectProxyAuth, kProxyConnected, OK, &kGet, &kSuccess)}}, |
| 6379 | { kProxy, AUTH_ASYNC, kAuthErr, kSecureServer, AUTH_NONE, OK, 2, kNoSSL, |
| 6380 | { TestRound(kConnect, kProxyChallenge, OK), |
| 6381 | TestRound(kConnectProxyAuth, kFailure, kAuthErr)}}, |
| 6382 | // Authenticating HTTPS server through an authenticating proxy. |
| 6383 | { kProxy, AUTH_SYNC, OK, kSecureServer, AUTH_SYNC, OK, 3, 1, |
| 6384 | { TestRound(kConnect, kProxyChallenge, OK), |
| 6385 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 6386 | &kGet, &kServerChallenge), |
| 6387 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 6388 | { kProxy, AUTH_SYNC, OK, kSecureServer, AUTH_SYNC, kAuthErr, 3, 1, |
| 6389 | { TestRound(kConnect, kProxyChallenge, OK), |
| 6390 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 6391 | &kGet, &kServerChallenge), |
| 6392 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 6393 | { kProxy, AUTH_ASYNC, OK, kSecureServer, AUTH_SYNC, OK, 3, 1, |
| 6394 | { TestRound(kConnect, kProxyChallenge, OK), |
| 6395 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 6396 | &kGet, &kServerChallenge), |
| 6397 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 6398 | { kProxy, AUTH_ASYNC, OK, kSecureServer, AUTH_SYNC, kAuthErr, 3, 1, |
| 6399 | { TestRound(kConnect, kProxyChallenge, OK), |
| 6400 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 6401 | &kGet, &kServerChallenge), |
| 6402 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 6403 | { kProxy, AUTH_SYNC, OK, kSecureServer, AUTH_ASYNC, OK, 3, 1, |
| 6404 | { TestRound(kConnect, kProxyChallenge, OK), |
| 6405 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 6406 | &kGet, &kServerChallenge), |
| 6407 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 6408 | { kProxy, AUTH_SYNC, OK, kSecureServer, AUTH_ASYNC, kAuthErr, 3, 1, |
| 6409 | { TestRound(kConnect, kProxyChallenge, OK), |
| 6410 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 6411 | &kGet, &kServerChallenge), |
| 6412 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 6413 | { kProxy, AUTH_ASYNC, OK, kSecureServer, AUTH_ASYNC, OK, 3, 1, |
| 6414 | { TestRound(kConnect, kProxyChallenge, OK), |
| 6415 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 6416 | &kGet, &kServerChallenge), |
| 6417 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 6418 | { kProxy, AUTH_ASYNC, OK, kSecureServer, AUTH_ASYNC, kAuthErr, 3, 1, |
| 6419 | { TestRound(kConnect, kProxyChallenge, OK), |
| 6420 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 6421 | &kGet, &kServerChallenge), |
| 6422 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 6423 | }; |
| 6424 | |
| 6425 | SessionDependencies session_deps; |
| 6426 | scoped_refptr<HttpNetworkSession> session = CreateSession(&session_deps); |
[email protected] | 3fd9dae | 2010-06-21 11:39:00 | [diff] [blame] | 6427 | HttpAuthHandlerMock::Factory* auth_factory( |
| 6428 | new HttpAuthHandlerMock::Factory()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 6429 | session_deps.http_auth_handler_factory.reset(auth_factory); |
| 6430 | |
| 6431 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_configs); ++i) { |
| 6432 | const TestConfig& test_config = test_configs[i]; |
[email protected] | 65d3438 | 2010-07-01 18:12:26 | [diff] [blame] | 6433 | |
| 6434 | // Set up authentication handlers as necessary. |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 6435 | if (test_config.proxy_auth_timing != AUTH_NONE) { |
[email protected] | 3fd9dae | 2010-06-21 11:39:00 | [diff] [blame] | 6436 | HttpAuthHandlerMock* auth_handler(new HttpAuthHandlerMock()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 6437 | std::string auth_challenge = "Mock realm=proxy"; |
| 6438 | GURL origin(test_config.proxy_url); |
| 6439 | HttpAuth::ChallengeTokenizer tokenizer(auth_challenge.begin(), |
| 6440 | auth_challenge.end()); |
| 6441 | auth_handler->InitFromChallenge(&tokenizer, HttpAuth::AUTH_PROXY, |
| 6442 | origin, BoundNetLog()); |
| 6443 | auth_handler->SetGenerateExpectation( |
| 6444 | test_config.proxy_auth_timing == AUTH_ASYNC, |
| 6445 | test_config.proxy_auth_rv); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 6446 | auth_factory->set_mock_handler(auth_handler, HttpAuth::AUTH_PROXY); |
| 6447 | } |
| 6448 | if (test_config.server_auth_timing != AUTH_NONE) { |
[email protected] | 3fd9dae | 2010-06-21 11:39:00 | [diff] [blame] | 6449 | HttpAuthHandlerMock* auth_handler(new HttpAuthHandlerMock()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 6450 | std::string auth_challenge = "Mock realm=server"; |
| 6451 | GURL origin(test_config.server_url); |
| 6452 | HttpAuth::ChallengeTokenizer tokenizer(auth_challenge.begin(), |
| 6453 | auth_challenge.end()); |
| 6454 | auth_handler->InitFromChallenge(&tokenizer, HttpAuth::AUTH_SERVER, |
| 6455 | origin, BoundNetLog()); |
| 6456 | auth_handler->SetGenerateExpectation( |
| 6457 | test_config.server_auth_timing == AUTH_ASYNC, |
| 6458 | test_config.server_auth_rv); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 6459 | auth_factory->set_mock_handler(auth_handler, HttpAuth::AUTH_SERVER); |
| 6460 | } |
| 6461 | if (test_config.proxy_url) { |
| 6462 | session_deps.proxy_service = |
| 6463 | CreateFixedProxyService(test_config.proxy_url); |
| 6464 | } else { |
| 6465 | session_deps.proxy_service = ProxyService::CreateNull(); |
| 6466 | } |
| 6467 | |
| 6468 | HttpRequestInfo request; |
| 6469 | request.method = "GET"; |
| 6470 | request.url = GURL(test_config.server_url); |
| 6471 | request.load_flags = 0; |
| 6472 | |
| 6473 | scoped_ptr<HttpTransaction> trans( |
| 6474 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 6475 | |
| 6476 | for (int round = 0; round < test_config.num_auth_rounds; ++round) { |
| 6477 | const TestRound& read_write_round = test_config.rounds[round]; |
| 6478 | |
| 6479 | // Set up expected reads and writes. |
| 6480 | MockRead reads[2]; |
| 6481 | reads[0] = read_write_round.read; |
| 6482 | size_t length_reads = 1; |
| 6483 | if (read_write_round.extra_read) { |
| 6484 | reads[1] = *read_write_round.extra_read; |
| 6485 | length_reads = 2; |
| 6486 | } |
| 6487 | |
| 6488 | MockWrite writes[2]; |
| 6489 | writes[0] = read_write_round.write; |
| 6490 | size_t length_writes = 1; |
| 6491 | if (read_write_round.extra_write) { |
| 6492 | writes[1] = *read_write_round.extra_write; |
| 6493 | length_writes = 2; |
| 6494 | } |
| 6495 | StaticSocketDataProvider data_provider( |
| 6496 | reads, length_reads, writes, length_writes); |
| 6497 | session_deps.socket_factory.AddSocketDataProvider(&data_provider); |
| 6498 | |
| 6499 | // Add an SSL sequence if necessary. |
| 6500 | SSLSocketDataProvider ssl_socket_data_provider(false, OK); |
| 6501 | if (round >= test_config.first_ssl_round) |
| 6502 | session_deps.socket_factory.AddSSLSocketDataProvider( |
| 6503 | &ssl_socket_data_provider); |
| 6504 | |
| 6505 | // Start or restart the transaction. |
| 6506 | TestCompletionCallback callback; |
| 6507 | int rv; |
| 6508 | if (round == 0) { |
| 6509 | rv = trans->Start(&request, &callback, BoundNetLog()); |
| 6510 | } else { |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 6511 | rv = trans->RestartWithAuth(kFoo, kBar, &callback); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 6512 | } |
| 6513 | if (rv == ERR_IO_PENDING) |
| 6514 | rv = callback.WaitForResult(); |
| 6515 | |
| 6516 | // Compare results with expected data. |
| 6517 | EXPECT_EQ(read_write_round.expected_rv, rv); |
| 6518 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 6519 | if (read_write_round.expected_rv == OK) { |
| 6520 | EXPECT_FALSE(response == NULL); |
| 6521 | } else { |
| 6522 | EXPECT_TRUE(response == NULL); |
| 6523 | EXPECT_EQ(round + 1, test_config.num_auth_rounds); |
| 6524 | continue; |
| 6525 | } |
| 6526 | if (round + 1 < test_config.num_auth_rounds) { |
| 6527 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 6528 | } else { |
| 6529 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 6530 | } |
| 6531 | } |
[email protected] | e5ae96a | 2010-04-14 20:12:45 | [diff] [blame] | 6532 | } |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 6533 | |
| 6534 | // Flush the idle socket before the HttpNetworkTransaction goes out of scope. |
| 6535 | session->FlushSocketPools(); |
[email protected] | e5ae96a | 2010-04-14 20:12:45 | [diff] [blame] | 6536 | } |
| 6537 | |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 6538 | TEST_F(HttpNetworkTransactionTest, MultiRoundAuth) { |
| 6539 | // Do multi-round authentication and make sure it works correctly. |
| 6540 | SessionDependencies session_deps; |
| 6541 | HttpAuthHandlerMock::Factory* auth_factory( |
| 6542 | new HttpAuthHandlerMock::Factory()); |
| 6543 | session_deps.http_auth_handler_factory.reset(auth_factory); |
| 6544 | session_deps.proxy_service = ProxyService::CreateNull(); |
| 6545 | session_deps.host_resolver->rules()->AddRule("www.example.com", "10.0.0.1"); |
| 6546 | session_deps.host_resolver->set_synchronous_mode(true); |
| 6547 | |
| 6548 | HttpAuthHandlerMock* auth_handler(new HttpAuthHandlerMock()); |
| 6549 | auth_handler->set_connection_based(true); |
| 6550 | std::string auth_challenge = "Mock realm=server"; |
| 6551 | GURL origin("http://www.example.com"); |
| 6552 | HttpAuth::ChallengeTokenizer tokenizer(auth_challenge.begin(), |
| 6553 | auth_challenge.end()); |
| 6554 | auth_handler->InitFromChallenge(&tokenizer, HttpAuth::AUTH_SERVER, |
| 6555 | origin, BoundNetLog()); |
| 6556 | auth_factory->set_mock_handler(auth_handler, HttpAuth::AUTH_SERVER); |
| 6557 | |
| 6558 | scoped_refptr<HttpNetworkSession> session = CreateSession(&session_deps); |
| 6559 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 6560 | |
| 6561 | int rv = OK; |
| 6562 | const HttpResponseInfo* response = NULL; |
| 6563 | HttpRequestInfo request; |
| 6564 | request.method = "GET"; |
| 6565 | request.url = origin; |
| 6566 | request.load_flags = 0; |
| 6567 | TestCompletionCallback callback; |
| 6568 | |
| 6569 | const MockWrite kGet( |
| 6570 | "GET / HTTP/1.1\r\n" |
| 6571 | "Host: www.example.com\r\n" |
| 6572 | "Connection: keep-alive\r\n\r\n"); |
| 6573 | const MockWrite kGetAuth( |
| 6574 | "GET / HTTP/1.1\r\n" |
| 6575 | "Host: www.example.com\r\n" |
| 6576 | "Connection: keep-alive\r\n" |
| 6577 | "Authorization: auth_token\r\n\r\n"); |
| 6578 | |
| 6579 | const MockRead kServerChallenge( |
| 6580 | "HTTP/1.1 401 Unauthorized\r\n" |
| 6581 | "WWW-Authenticate: Mock realm=server\r\n" |
| 6582 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 6583 | "Content-Length: 14\r\n\r\n" |
| 6584 | "Unauthorized\r\n"); |
| 6585 | const MockRead kSuccess( |
| 6586 | "HTTP/1.1 200 OK\r\n" |
| 6587 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 6588 | "Content-Length: 3\r\n\r\n" |
| 6589 | "Yes"); |
| 6590 | |
| 6591 | MockWrite writes[] = { |
| 6592 | // First round |
| 6593 | kGet, |
| 6594 | // Second round |
| 6595 | kGetAuth, |
| 6596 | // Third round |
| 6597 | kGetAuth, |
| 6598 | }; |
| 6599 | MockRead reads[] = { |
| 6600 | // First round |
| 6601 | kServerChallenge, |
| 6602 | // Second round |
| 6603 | kServerChallenge, |
| 6604 | // Third round |
| 6605 | kSuccess, |
| 6606 | }; |
| 6607 | StaticSocketDataProvider data_provider(reads, arraysize(reads), |
| 6608 | writes, arraysize(writes)); |
| 6609 | session_deps.socket_factory.AddSocketDataProvider(&data_provider); |
| 6610 | |
| 6611 | // First round |
| 6612 | auth_handler->SetGenerateExpectation(false, OK); |
| 6613 | rv = trans->Start(&request, &callback, BoundNetLog()); |
| 6614 | if (rv == ERR_IO_PENDING) |
| 6615 | rv = callback.WaitForResult(); |
| 6616 | EXPECT_EQ(OK, rv); |
| 6617 | response = trans->GetResponseInfo(); |
| 6618 | ASSERT_FALSE(response == NULL); |
| 6619 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 6620 | |
| 6621 | // Second round |
| 6622 | auth_handler->SetGenerateExpectation(false, OK); |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 6623 | rv = trans->RestartWithAuth(kFoo, kBar, &callback); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 6624 | if (rv == ERR_IO_PENDING) |
| 6625 | rv = callback.WaitForResult(); |
| 6626 | EXPECT_EQ(OK, rv); |
| 6627 | response = trans->GetResponseInfo(); |
| 6628 | ASSERT_FALSE(response == NULL); |
| 6629 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 6630 | |
| 6631 | // Third round |
| 6632 | auth_handler->SetGenerateExpectation(false, OK); |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 6633 | rv = trans->RestartWithAuth(string16(), string16(), &callback); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 6634 | if (rv == ERR_IO_PENDING) |
| 6635 | rv = callback.WaitForResult(); |
| 6636 | EXPECT_EQ(OK, rv); |
| 6637 | response = trans->GetResponseInfo(); |
| 6638 | ASSERT_FALSE(response == NULL); |
| 6639 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 6640 | } |
| 6641 | |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 6642 | class TLSDecompressionFailureSocketDataProvider : public SocketDataProvider { |
| 6643 | public: |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 6644 | explicit TLSDecompressionFailureSocketDataProvider(bool fail_all) |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 6645 | : fail_all_(fail_all) { |
| 6646 | } |
| 6647 | |
| 6648 | virtual MockRead GetNextRead() { |
| 6649 | if (fail_all_) |
| 6650 | return MockRead(false /* async */, ERR_SSL_DECOMPRESSION_FAILURE_ALERT); |
| 6651 | |
| 6652 | return MockRead(false /* async */, |
| 6653 | "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nok.\r\n"); |
| 6654 | } |
| 6655 | |
| 6656 | virtual MockWriteResult OnWrite(const std::string& data) { |
| 6657 | return MockWriteResult(false /* async */, data.size()); |
| 6658 | } |
| 6659 | |
| 6660 | void Reset() { |
| 6661 | } |
| 6662 | |
| 6663 | private: |
| 6664 | const bool fail_all_; |
| 6665 | }; |
| 6666 | |
| 6667 | // Test that we restart a connection when we see a decompression failure from |
| 6668 | // the peer during the handshake. (In the real world we'll restart with SSLv3 |
| 6669 | // and we won't offer DEFLATE in that case.) |
| 6670 | TEST_F(HttpNetworkTransactionTest, RestartAfterTLSDecompressionFailure) { |
| 6671 | HttpRequestInfo request; |
| 6672 | request.method = "GET"; |
| 6673 | request.url = GURL("https://tlsdecompressionfailure.example.com/"); |
| 6674 | request.load_flags = 0; |
| 6675 | |
| 6676 | SessionDependencies session_deps; |
| 6677 | TLSDecompressionFailureSocketDataProvider socket_data_provider1( |
| 6678 | false /* fail all reads */); |
| 6679 | TLSDecompressionFailureSocketDataProvider socket_data_provider2(false); |
| 6680 | SSLSocketDataProvider ssl_socket_data_provider1( |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 6681 | false, ERR_SSL_DECOMPRESSION_FAILURE_ALERT); |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 6682 | SSLSocketDataProvider ssl_socket_data_provider2(false, OK); |
| 6683 | session_deps.socket_factory.AddSocketDataProvider(&socket_data_provider1); |
| 6684 | session_deps.socket_factory.AddSocketDataProvider(&socket_data_provider2); |
| 6685 | session_deps.socket_factory.AddSSLSocketDataProvider( |
| 6686 | &ssl_socket_data_provider1); |
| 6687 | session_deps.socket_factory.AddSSLSocketDataProvider( |
| 6688 | &ssl_socket_data_provider2); |
| 6689 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 6690 | // Work around http://crbug.com/37454 |
| 6691 | StaticSocketDataProvider bug37454_connection; |
| 6692 | bug37454_connection.set_connect_data(MockConnect(true, ERR_UNEXPECTED)); |
| 6693 | session_deps.socket_factory.AddSocketDataProvider(&bug37454_connection); |
| 6694 | |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 6695 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 6696 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 6697 | TestCompletionCallback callback; |
| 6698 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 6699 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 6700 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6701 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6702 | |
| 6703 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 6704 | ASSERT_TRUE(response != NULL); |
| 6705 | ASSERT_TRUE(response->headers != NULL); |
| 6706 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 6707 | |
| 6708 | std::string response_data; |
| 6709 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 6710 | EXPECT_EQ("ok.", response_data); |
| 6711 | } |
| 6712 | |
| 6713 | // Test that we restart a connection if we get a decompression failure from the |
| 6714 | // peer while reading the first bytes from the connection. This occurs when the |
| 6715 | // peer cannot handle DEFLATE but we're using False Start, so we don't notice |
| 6716 | // in the handshake. |
| 6717 | TEST_F(HttpNetworkTransactionTest, |
| 6718 | RestartAfterTLSDecompressionFailureWithFalseStart) { |
| 6719 | HttpRequestInfo request; |
| 6720 | request.method = "GET"; |
| 6721 | request.url = GURL("https://tlsdecompressionfailure2.example.com/"); |
| 6722 | request.load_flags = 0; |
| 6723 | |
| 6724 | SessionDependencies session_deps; |
| 6725 | TLSDecompressionFailureSocketDataProvider socket_data_provider1( |
| 6726 | true /* fail all reads */); |
| 6727 | TLSDecompressionFailureSocketDataProvider socket_data_provider2(false); |
| 6728 | SSLSocketDataProvider ssl_socket_data_provider1(false, OK); |
| 6729 | SSLSocketDataProvider ssl_socket_data_provider2(false, OK); |
| 6730 | session_deps.socket_factory.AddSocketDataProvider(&socket_data_provider1); |
| 6731 | session_deps.socket_factory.AddSocketDataProvider(&socket_data_provider2); |
| 6732 | session_deps.socket_factory.AddSSLSocketDataProvider( |
| 6733 | &ssl_socket_data_provider1); |
| 6734 | session_deps.socket_factory.AddSSLSocketDataProvider( |
| 6735 | &ssl_socket_data_provider2); |
| 6736 | |
| 6737 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 6738 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 6739 | TestCompletionCallback callback; |
| 6740 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 6741 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 6742 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6743 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6744 | |
| 6745 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 6746 | ASSERT_TRUE(response != NULL); |
| 6747 | ASSERT_TRUE(response->headers != NULL); |
| 6748 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 6749 | |
| 6750 | std::string response_data; |
| 6751 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 6752 | EXPECT_EQ("ok.", response_data); |
| 6753 | } |
| 6754 | |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 6755 | // This tests the case that a request is issued via http instead of spdy after |
| 6756 | // npn is negotiated. |
| 6757 | TEST_F(HttpNetworkTransactionTest, NpnWithHttpOverSSL) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6758 | HttpStreamFactory::set_use_alternate_protocols(true); |
| 6759 | HttpStreamFactory::set_next_protos("\x08http/1.1\x07http1.1"); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 6760 | SessionDependencies session_deps; |
| 6761 | HttpRequestInfo request; |
| 6762 | request.method = "GET"; |
| 6763 | request.url = GURL("https://www.google.com/"); |
| 6764 | request.load_flags = 0; |
| 6765 | |
| 6766 | MockWrite data_writes[] = { |
| 6767 | MockWrite("GET / HTTP/1.1\r\n" |
| 6768 | "Host: www.google.com\r\n" |
| 6769 | "Connection: keep-alive\r\n\r\n"), |
| 6770 | }; |
| 6771 | |
| 6772 | MockRead data_reads[] = { |
| 6773 | MockRead("HTTP/1.1 200 OK\r\n"), |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 6774 | MockRead(kAlternateProtocolHttpHeader), |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 6775 | MockRead("hello world"), |
| 6776 | MockRead(false, OK), |
| 6777 | }; |
| 6778 | |
| 6779 | SSLSocketDataProvider ssl(true, OK); |
| 6780 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
| 6781 | ssl.next_proto = "http/1.1"; |
| 6782 | |
| 6783 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 6784 | |
| 6785 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 6786 | data_writes, arraysize(data_writes)); |
| 6787 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 6788 | |
| 6789 | TestCompletionCallback callback; |
| 6790 | |
| 6791 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 6792 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 6793 | |
| 6794 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 6795 | |
| 6796 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6797 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6798 | |
| 6799 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 6800 | ASSERT_TRUE(response != NULL); |
| 6801 | ASSERT_TRUE(response->headers != NULL); |
| 6802 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 6803 | |
| 6804 | std::string response_data; |
| 6805 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 6806 | EXPECT_EQ("hello world", response_data); |
| 6807 | |
| 6808 | EXPECT_FALSE(response->was_fetched_via_spdy); |
| 6809 | EXPECT_TRUE(response->was_npn_negotiated); |
[email protected] | 193b0b89 | 2010-06-26 03:57:57 | [diff] [blame] | 6810 | EXPECT_FALSE(response->was_alternate_protocol_available); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 6811 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6812 | HttpStreamFactory::set_next_protos(""); |
| 6813 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 6814 | } |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 6815 | |
| 6816 | TEST_F(HttpNetworkTransactionTest, SpdyPostNPNServerHangup) { |
| 6817 | // Simulate the SSL handshake completing with an NPN negotiation |
| 6818 | // followed by an immediate server closing of the socket. |
| 6819 | // Fix crash: http://crbug.com/46369 |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6820 | HttpStreamFactory::set_use_alternate_protocols(true); |
| 6821 | HttpStreamFactory::set_next_protos(kExpectedNPNString); |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 6822 | SessionDependencies session_deps; |
| 6823 | |
| 6824 | HttpRequestInfo request; |
| 6825 | request.method = "GET"; |
| 6826 | request.url = GURL("https://www.google.com/"); |
| 6827 | request.load_flags = 0; |
| 6828 | |
| 6829 | SSLSocketDataProvider ssl(true, OK); |
| 6830 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 6831 | ssl.next_proto = "spdy/2"; |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 6832 | ssl.was_npn_negotiated = true; |
| 6833 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 6834 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 6835 | scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); |
[email protected] | e7f7509 | 2010-07-01 22:39:13 | [diff] [blame] | 6836 | MockWrite spdy_writes[] = { CreateMockWrite(*req) }; |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 6837 | |
| 6838 | MockRead spdy_reads[] = { |
| 6839 | MockRead(false, 0, 0) // Not async - return 0 immediately. |
| 6840 | }; |
| 6841 | |
| 6842 | scoped_refptr<DelayedSocketData> spdy_data( |
| 6843 | new DelayedSocketData( |
| 6844 | 0, // don't wait in this case, immediate hangup. |
| 6845 | spdy_reads, arraysize(spdy_reads), |
| 6846 | spdy_writes, arraysize(spdy_writes))); |
| 6847 | session_deps.socket_factory.AddSocketDataProvider(spdy_data); |
| 6848 | |
| 6849 | TestCompletionCallback callback; |
| 6850 | |
| 6851 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 6852 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 6853 | |
| 6854 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 6855 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6856 | EXPECT_EQ(ERR_CONNECTION_CLOSED, callback.WaitForResult()); |
| 6857 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6858 | HttpStreamFactory::set_next_protos(""); |
| 6859 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 6860 | } |
[email protected] | 65d3438 | 2010-07-01 18:12:26 | [diff] [blame] | 6861 | |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 6862 | TEST_F(HttpNetworkTransactionTest, SpdyAlternateProtocolThroughProxy) { |
| 6863 | // This test ensures that the URL passed into the proxy is upgraded |
| 6864 | // to https when doing an Alternate Protocol upgrade. |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6865 | HttpStreamFactory::set_use_alternate_protocols(true); |
| 6866 | HttpStreamFactory::set_next_protos( |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 6867 | "\x08http/1.1\x07http1.1\x06spdy/2\x04spdy"); |
| 6868 | |
| 6869 | SessionDependencies session_deps(CreateFixedProxyService("myproxy:70")); |
| 6870 | HttpAuthHandlerMock::Factory* auth_factory = |
| 6871 | new HttpAuthHandlerMock::Factory(); |
| 6872 | HttpAuthHandlerMock* auth_handler = new HttpAuthHandlerMock(); |
| 6873 | auth_factory->set_mock_handler(auth_handler, HttpAuth::AUTH_PROXY); |
| 6874 | auth_factory->set_do_init_from_challenge(true); |
| 6875 | session_deps.http_auth_handler_factory.reset(auth_factory); |
| 6876 | |
| 6877 | HttpRequestInfo request; |
| 6878 | request.method = "GET"; |
| 6879 | request.url = GURL("http://www.google.com"); |
| 6880 | request.load_flags = 0; |
| 6881 | |
| 6882 | // First round goes unauthenticated through the proxy. |
| 6883 | MockWrite data_writes_1[] = { |
| 6884 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 6885 | "Host: www.google.com\r\n" |
| 6886 | "Proxy-Connection: keep-alive\r\n" |
| 6887 | "\r\n"), |
| 6888 | }; |
| 6889 | MockRead data_reads_1[] = { |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 6890 | MockRead(false, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 6891 | MockRead("HTTP/1.1 200 OK\r\n" |
| 6892 | "Alternate-Protocol: 443:npn-spdy/2\r\n" |
| 6893 | "Proxy-Connection: close\r\n" |
| 6894 | "\r\n"), |
| 6895 | }; |
| 6896 | StaticSocketDataProvider data_1(data_reads_1, arraysize(data_reads_1), |
| 6897 | data_writes_1, arraysize(data_writes_1)); |
| 6898 | |
| 6899 | // Second round tries to tunnel to www.google.com due to the |
| 6900 | // Alternate-Protocol announcement in the first round. It fails due |
| 6901 | // to a proxy authentication challenge. |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 6902 | // After the failure, a tunnel is established to www.google.com using |
| 6903 | // Proxy-Authorization headers. There is then a SPDY request round. |
| 6904 | // |
| 6905 | // NOTE: Despite the "Proxy-Connection: Close", these are done on the |
| 6906 | // same MockTCPClientSocket since the underlying HttpNetworkClientSocket |
| 6907 | // does a Disconnect and Connect on the same socket, rather than trying |
| 6908 | // to obtain a new one. |
| 6909 | // |
| 6910 | // NOTE: Originally, the proxy response to the second CONNECT request |
| 6911 | // simply returned another 407 so the unit test could skip the SSL connection |
| 6912 | // establishment and SPDY framing issues. Alas, the |
| 6913 | // retry-http-when-alternate-protocol fails logic kicks in, which was more |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 6914 | // complicated to set up expectations for than the SPDY session. |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 6915 | |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 6916 | scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); |
| 6917 | scoped_ptr<spdy::SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 6918 | scoped_ptr<spdy::SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); |
| 6919 | |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 6920 | MockWrite data_writes_2[] = { |
| 6921 | // First connection attempt without Proxy-Authorization. |
| 6922 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 6923 | "Host: www.google.com\r\n" |
| 6924 | "Proxy-Connection: keep-alive\r\n" |
| 6925 | "\r\n"), |
| 6926 | |
| 6927 | // Second connection attempt with Proxy-Authorization. |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 6928 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 6929 | "Host: www.google.com\r\n" |
| 6930 | "Proxy-Connection: keep-alive\r\n" |
| 6931 | "Proxy-Authorization: auth_token\r\n" |
| 6932 | "\r\n"), |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 6933 | |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 6934 | // SPDY request |
| 6935 | CreateMockWrite(*req), |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 6936 | }; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 6937 | const char kRejectConnectResponse[] = ("HTTP/1.1 407 Unauthorized\r\n" |
| 6938 | "Proxy-Authenticate: Mock\r\n" |
| 6939 | "Proxy-Connection: close\r\n" |
| 6940 | "\r\n"); |
| 6941 | const char kAcceptConnectResponse[] = "HTTP/1.1 200 Connected\r\n\r\n"; |
| 6942 | MockRead data_reads_2[] = { |
| 6943 | // First connection attempt fails |
| 6944 | MockRead(false, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ, 1), |
| 6945 | MockRead(true, kRejectConnectResponse, |
| 6946 | arraysize(kRejectConnectResponse) - 1, 1), |
| 6947 | |
| 6948 | // Second connection attempt passes |
| 6949 | MockRead(true, kAcceptConnectResponse, |
| 6950 | arraysize(kAcceptConnectResponse) -1, 4), |
| 6951 | |
| 6952 | // SPDY response |
| 6953 | CreateMockRead(*resp.get(), 6), |
| 6954 | CreateMockRead(*data.get(), 6), |
| 6955 | MockRead(true, 0, 0, 6), |
| 6956 | }; |
| 6957 | scoped_refptr<OrderedSocketData> data_2( |
| 6958 | new OrderedSocketData(data_reads_2, arraysize(data_reads_2), |
| 6959 | data_writes_2, arraysize(data_writes_2))); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 6960 | |
| 6961 | SSLSocketDataProvider ssl(true, OK); |
| 6962 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
| 6963 | ssl.next_proto = "spdy/2"; |
| 6964 | ssl.was_npn_negotiated = true; |
| 6965 | |
| 6966 | session_deps.socket_factory.AddSocketDataProvider(&data_1); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 6967 | session_deps.socket_factory.AddSocketDataProvider(data_2.get()); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 6968 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 6969 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 6970 | |
| 6971 | // First round should work and provide the Alternate-Protocol state. |
| 6972 | TestCompletionCallback callback_1; |
| 6973 | scoped_ptr<HttpTransaction> trans_1(new HttpNetworkTransaction(session)); |
| 6974 | int rv = trans_1->Start(&request, &callback_1, BoundNetLog()); |
| 6975 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6976 | EXPECT_EQ(OK, callback_1.WaitForResult()); |
| 6977 | |
| 6978 | // Second round should attempt a tunnel connect and get an auth challenge. |
| 6979 | TestCompletionCallback callback_2; |
| 6980 | scoped_ptr<HttpTransaction> trans_2(new HttpNetworkTransaction(session)); |
| 6981 | rv = trans_2->Start(&request, &callback_2, BoundNetLog()); |
| 6982 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6983 | EXPECT_EQ(OK, callback_2.WaitForResult()); |
| 6984 | const HttpResponseInfo* response = trans_2->GetResponseInfo(); |
| 6985 | ASSERT_FALSE(response == NULL); |
| 6986 | ASSERT_FALSE(response->auth_challenge.get() == NULL); |
| 6987 | |
| 6988 | // Restart with auth. Tunnel should work and response received. |
| 6989 | TestCompletionCallback callback_3; |
| 6990 | rv = trans_2->RestartWithAuth(kFoo, kBar, &callback_3); |
| 6991 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6992 | EXPECT_EQ(OK, callback_3.WaitForResult()); |
| 6993 | |
| 6994 | // After all that work, these two lines (or actually, just the scheme) are |
| 6995 | // what this test is all about. Make sure it happens correctly. |
| 6996 | const GURL& request_url = auth_handler->request_url(); |
| 6997 | EXPECT_EQ("https", request_url.scheme()); |
| 6998 | EXPECT_EQ("www.google.com", request_url.host()); |
| 6999 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 7000 | HttpStreamFactory::set_next_protos(""); |
| 7001 | HttpStreamFactory::set_use_alternate_protocols(false); |
| 7002 | } |
| 7003 | |
| 7004 | // Test that if we cancel the transaction as the connection is completing, that |
| 7005 | // everything tears down correctly. |
| 7006 | TEST_F(HttpNetworkTransactionTest, SimpleCancel) { |
| 7007 | // Setup everything about the connection to complete synchronously, so that |
| 7008 | // after calling HttpNetworkTransaction::Start, the only thing we're waiting |
| 7009 | // for is the callback from the HttpStreamRequest. |
| 7010 | // Then cancel the transaction. |
| 7011 | // Verify that we don't crash. |
| 7012 | MockConnect mock_connect(false, OK); |
| 7013 | MockRead data_reads[] = { |
| 7014 | MockRead(false, "HTTP/1.0 200 OK\r\n\r\n"), |
| 7015 | MockRead(false, "hello world"), |
| 7016 | MockRead(false, OK), |
| 7017 | }; |
| 7018 | |
| 7019 | SessionDependencies session_deps; |
| 7020 | session_deps.host_resolver->set_synchronous_mode(true); |
| 7021 | scoped_ptr<HttpTransaction> trans( |
| 7022 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 7023 | |
| 7024 | HttpRequestInfo request; |
| 7025 | request.method = "GET"; |
| 7026 | request.url = GURL("http://www.google.com/"); |
| 7027 | request.load_flags = 0; |
| 7028 | |
| 7029 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
| 7030 | data.set_connect_data(mock_connect); |
| 7031 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 7032 | |
| 7033 | TestCompletionCallback callback; |
| 7034 | |
| 7035 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 7036 | int rv = trans->Start(&request, &callback, log.bound()); |
| 7037 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7038 | trans.reset(); // Cancel the transaction here. |
| 7039 | |
| 7040 | MessageLoop::current()->RunAllPending(); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 7041 | } |
| 7042 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 7043 | // Test a basic GET request through a proxy. |
| 7044 | TEST_F(HttpNetworkTransactionTest, ProxyGet) { |
| 7045 | SessionDependencies session_deps(CreateFixedProxyService("myproxy:70")); |
| 7046 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 7047 | session_deps.net_log = log.bound().net_log(); |
| 7048 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 7049 | |
| 7050 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 7051 | |
| 7052 | HttpRequestInfo request; |
| 7053 | request.method = "GET"; |
| 7054 | request.url = GURL("http://www.google.com/"); |
| 7055 | |
| 7056 | MockWrite data_writes1[] = { |
| 7057 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 7058 | "Host: www.google.com\r\n" |
| 7059 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 7060 | }; |
| 7061 | |
| 7062 | MockRead data_reads1[] = { |
| 7063 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 7064 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 7065 | MockRead("Content-Length: 100\r\n\r\n"), |
| 7066 | MockRead(false, OK), |
| 7067 | }; |
| 7068 | |
| 7069 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 7070 | data_writes1, arraysize(data_writes1)); |
| 7071 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 7072 | |
| 7073 | TestCompletionCallback callback1; |
| 7074 | |
| 7075 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 7076 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7077 | |
| 7078 | rv = callback1.WaitForResult(); |
| 7079 | EXPECT_EQ(OK, rv); |
| 7080 | |
| 7081 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 7082 | ASSERT_FALSE(response == NULL); |
| 7083 | |
| 7084 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 7085 | EXPECT_EQ(200, response->headers->response_code()); |
| 7086 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 7087 | EXPECT_TRUE(response->was_fetched_via_proxy); |
| 7088 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 7089 | } |
| 7090 | |
| 7091 | // Test a basic HTTPS GET request through a proxy. |
| 7092 | TEST_F(HttpNetworkTransactionTest, ProxyTunnelGet) { |
| 7093 | SessionDependencies session_deps(CreateFixedProxyService("myproxy:70")); |
| 7094 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 7095 | session_deps.net_log = log.bound().net_log(); |
| 7096 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 7097 | |
| 7098 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 7099 | |
| 7100 | HttpRequestInfo request; |
| 7101 | request.method = "GET"; |
| 7102 | request.url = GURL("https://www.google.com/"); |
| 7103 | |
| 7104 | // Since we have proxy, should try to establish tunnel. |
| 7105 | MockWrite data_writes1[] = { |
| 7106 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 7107 | "Host: www.google.com\r\n" |
| 7108 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 7109 | |
| 7110 | MockWrite("GET / HTTP/1.1\r\n" |
| 7111 | "Host: www.google.com\r\n" |
| 7112 | "Connection: keep-alive\r\n\r\n"), |
| 7113 | }; |
| 7114 | |
| 7115 | MockRead data_reads1[] = { |
| 7116 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 7117 | |
| 7118 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 7119 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 7120 | MockRead("Content-Length: 100\r\n\r\n"), |
| 7121 | MockRead(false, OK), |
| 7122 | }; |
| 7123 | |
| 7124 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 7125 | data_writes1, arraysize(data_writes1)); |
| 7126 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 7127 | SSLSocketDataProvider ssl(true, OK); |
| 7128 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 7129 | |
| 7130 | TestCompletionCallback callback1; |
| 7131 | |
| 7132 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 7133 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7134 | |
| 7135 | rv = callback1.WaitForResult(); |
| 7136 | EXPECT_EQ(OK, rv); |
| 7137 | size_t pos = ExpectLogContainsSomewhere( |
| 7138 | log.entries(), 0, NetLog::TYPE_HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
| 7139 | NetLog::PHASE_NONE); |
| 7140 | ExpectLogContainsSomewhere( |
| 7141 | log.entries(), pos, |
| 7142 | NetLog::TYPE_HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 7143 | NetLog::PHASE_NONE); |
| 7144 | |
| 7145 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 7146 | ASSERT_FALSE(response == NULL); |
| 7147 | |
| 7148 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 7149 | EXPECT_EQ(200, response->headers->response_code()); |
| 7150 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 7151 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 7152 | EXPECT_TRUE(response->was_fetched_via_proxy); |
| 7153 | } |
| 7154 | |
| 7155 | // Test a basic HTTPS GET request through a proxy, but the server hangs up |
| 7156 | // while establishing the tunnel. |
| 7157 | TEST_F(HttpNetworkTransactionTest, ProxyTunnelGetHangup) { |
| 7158 | SessionDependencies session_deps(CreateFixedProxyService("myproxy:70")); |
| 7159 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 7160 | session_deps.net_log = log.bound().net_log(); |
| 7161 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 7162 | |
| 7163 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 7164 | |
| 7165 | HttpRequestInfo request; |
| 7166 | request.method = "GET"; |
| 7167 | request.url = GURL("https://www.google.com/"); |
| 7168 | |
| 7169 | // Since we have proxy, should try to establish tunnel. |
| 7170 | MockWrite data_writes1[] = { |
| 7171 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 7172 | "Host: www.google.com\r\n" |
| 7173 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 7174 | |
| 7175 | MockWrite("GET / HTTP/1.1\r\n" |
| 7176 | "Host: www.google.com\r\n" |
| 7177 | "Connection: keep-alive\r\n\r\n"), |
| 7178 | }; |
| 7179 | |
| 7180 | MockRead data_reads1[] = { |
| 7181 | MockRead(false, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| 7182 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 7183 | MockRead(true, 0, 0), // EOF |
| 7184 | }; |
| 7185 | |
| 7186 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 7187 | data_writes1, arraysize(data_writes1)); |
| 7188 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 7189 | SSLSocketDataProvider ssl(true, OK); |
| 7190 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 7191 | |
| 7192 | TestCompletionCallback callback1; |
| 7193 | |
| 7194 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 7195 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7196 | |
| 7197 | rv = callback1.WaitForResult(); |
| 7198 | EXPECT_EQ(ERR_EMPTY_RESPONSE, rv); |
| 7199 | size_t pos = ExpectLogContainsSomewhere( |
| 7200 | log.entries(), 0, NetLog::TYPE_HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
| 7201 | NetLog::PHASE_NONE); |
| 7202 | ExpectLogContainsSomewhere( |
| 7203 | log.entries(), pos, |
| 7204 | NetLog::TYPE_HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 7205 | NetLog::PHASE_NONE); |
| 7206 | } |
| 7207 | |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 7208 | } // namespace net |