[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 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame^] | 75 | void SetTCPSocketPool(TCPClientSocketPool* pool) { |
| 76 | session_->socket_pool_manager_.tcp_socket_pool_.reset(pool); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void SetSocketPoolForSOCKSProxy( |
| 80 | const HostPortPair& socks_proxy, |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame^] | 81 | SOCKSClientSocketPool* pool) { |
| 82 | ClientSocketPoolManager* socket_pool_manager = |
| 83 | &session_->socket_pool_manager_; |
| 84 | |
| 85 | // Call through the public interface to force initialization of the |
| 86 | // wrapped socket pools. |
| 87 | delete socket_pool_manager->GetSocketPoolForSOCKSProxy(socks_proxy); |
| 88 | socket_pool_manager->socks_socket_pools_[socks_proxy] = pool; |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void SetSocketPoolForHTTPProxy( |
| 92 | const HostPortPair& http_proxy, |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame^] | 93 | HttpProxyClientSocketPool* pool) { |
| 94 | ClientSocketPoolManager* socket_pool_manager = |
| 95 | &session_->socket_pool_manager_; |
| 96 | |
| 97 | // Call through the public interface to force initialization of the |
| 98 | // wrapped socket pools. |
| 99 | delete socket_pool_manager->GetSocketPoolForHTTPProxy(http_proxy); |
| 100 | socket_pool_manager->http_proxy_socket_pools_[http_proxy] = pool; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 101 | } |
| 102 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame^] | 103 | void SetSSLSocketPool(SSLClientSocketPool* pool) { |
| 104 | session_->socket_pool_manager_.ssl_socket_pool_.reset(pool); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void SetSocketPoolForSSLWithProxy( |
| 108 | const HostPortPair& proxy_host, |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame^] | 109 | SSLClientSocketPool* pool) { |
| 110 | ClientSocketPoolManager* socket_pool_manager = |
| 111 | &session_->socket_pool_manager_; |
| 112 | |
| 113 | // Call through the public interface to force initialization of the |
| 114 | // wrapped socket pools. |
| 115 | delete socket_pool_manager->GetSocketPoolForSSLWithProxy(proxy_host); |
| 116 | socket_pool_manager->ssl_socket_pools_for_proxies_[proxy_host] = pool; |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | private: |
| 120 | const scoped_refptr<HttpNetworkSession> session_; |
| 121 | |
| 122 | DISALLOW_COPY_AND_ASSIGN(HttpNetworkSessionPeer); |
| 123 | }; |
| 124 | |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 125 | // Helper to manage the lifetimes of the dependencies for a |
| 126 | // HttpNetworkTransaction. |
[email protected] | ac03952 | 2010-06-15 16:39:44 | [diff] [blame] | 127 | struct SessionDependencies { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 128 | // Default set of dependencies -- "null" proxy service. |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 129 | SessionDependencies() |
| 130 | : host_resolver(new MockHostResolver), |
[email protected] | ebeefff3 | 2010-09-15 05:10:02 | [diff] [blame] | 131 | proxy_service(ProxyService::CreateDirect()), |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 132 | ssl_config_service(new SSLConfigServiceDefaults), |
[email protected] | f660e4b | 2010-09-29 14:20:08 | [diff] [blame] | 133 | http_auth_handler_factory( |
| 134 | HttpAuthHandlerFactory::CreateDefault(host_resolver)), |
[email protected] | 7abf7d2 | 2010-09-04 01:41:59 | [diff] [blame] | 135 | spdy_session_pool(new SpdySessionPool(NULL)), |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 136 | net_log(NULL) {} |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 137 | |
| 138 | // Custom proxy service dependency. |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 139 | explicit SessionDependencies(ProxyService* proxy_service) |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 140 | : host_resolver(new MockHostResolver), |
| 141 | proxy_service(proxy_service), |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 142 | ssl_config_service(new SSLConfigServiceDefaults), |
[email protected] | f660e4b | 2010-09-29 14:20:08 | [diff] [blame] | 143 | http_auth_handler_factory( |
| 144 | HttpAuthHandlerFactory::CreateDefault(host_resolver)), |
[email protected] | 7abf7d2 | 2010-09-04 01:41:59 | [diff] [blame] | 145 | spdy_session_pool(new SpdySessionPool(NULL)), |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 146 | net_log(NULL) {} |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 147 | |
[email protected] | a2c2fb9 | 2009-07-18 07:31:04 | [diff] [blame] | 148 | scoped_refptr<MockHostResolverBase> host_resolver; |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 149 | scoped_refptr<ProxyService> proxy_service; |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 150 | scoped_refptr<SSLConfigService> ssl_config_service; |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 151 | MockClientSocketFactory socket_factory; |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 152 | scoped_ptr<HttpAuthHandlerFactory> http_auth_handler_factory; |
[email protected] | a554a826 | 2010-05-20 00:13:52 | [diff] [blame] | 153 | scoped_refptr<SpdySessionPool> spdy_session_pool; |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 154 | NetLog* net_log; |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 155 | }; |
| 156 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 157 | ProxyService* CreateFixedProxyService(const std::string& proxy) { |
[email protected] | ab501a6a | 2009-05-12 15:07:50 | [diff] [blame] | 158 | net::ProxyConfig proxy_config; |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 159 | proxy_config.proxy_rules().ParseFromString(proxy); |
[email protected] | 3e44697f | 2009-05-22 14:37:39 | [diff] [blame] | 160 | return ProxyService::CreateFixed(proxy_config); |
[email protected] | 51fff29d | 2008-12-19 22:17:53 | [diff] [blame] | 161 | } |
| 162 | |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 163 | HttpNetworkSession* CreateSession(SessionDependencies* session_deps) { |
[email protected] | 66761b95 | 2010-06-25 21:30:38 | [diff] [blame] | 164 | return new HttpNetworkSession(session_deps->host_resolver, |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 165 | session_deps->proxy_service, |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 166 | &session_deps->socket_factory, |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 167 | session_deps->ssl_config_service, |
[email protected] | a554a826 | 2010-05-20 00:13:52 | [diff] [blame] | 168 | session_deps->spdy_session_pool, |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 169 | session_deps->http_auth_handler_factory.get(), |
[email protected] | ac03952 | 2010-06-15 16:39:44 | [diff] [blame] | 170 | NULL, |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 171 | session_deps->net_log); |
[email protected] | e8d53619 | 2008-10-17 22:21:14 | [diff] [blame] | 172 | } |
| 173 | |
[email protected] | 89836e2 | 2008-09-25 20:33:42 | [diff] [blame] | 174 | class HttpNetworkTransactionTest : public PlatformTest { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 175 | public: |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 176 | virtual void SetUp() { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 177 | NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
| 178 | MessageLoop::current()->RunAllPending(); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 179 | spdy::SpdyFramer::set_enable_compression_default(false); |
| 180 | } |
| 181 | |
[email protected] | 0e75a73 | 2008-10-16 20:36:09 | [diff] [blame] | 182 | virtual void TearDown() { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 183 | NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
| 184 | MessageLoop::current()->RunAllPending(); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 185 | spdy::SpdyFramer::set_enable_compression_default(true); |
[email protected] | 0e75a73 | 2008-10-16 20:36:09 | [diff] [blame] | 186 | // Empty the current queue. |
| 187 | MessageLoop::current()->RunAllPending(); |
| 188 | PlatformTest::TearDown(); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 189 | NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
| 190 | MessageLoop::current()->RunAllPending(); |
[email protected] | 0e75a73 | 2008-10-16 20:36:09 | [diff] [blame] | 191 | } |
| 192 | |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 193 | protected: |
| 194 | void KeepAliveConnectionResendRequestTest(const MockRead& read_failure); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 195 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 196 | struct SimpleGetHelperResult { |
| 197 | int rv; |
| 198 | std::string status_line; |
| 199 | std::string response_data; |
| 200 | }; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 201 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 202 | SimpleGetHelperResult SimpleGetHelper(MockRead data_reads[], |
| 203 | size_t reads_count) { |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 204 | SimpleGetHelperResult out; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 205 | |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 206 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 207 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 208 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 209 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 210 | HttpRequestInfo request; |
| 211 | request.method = "GET"; |
| 212 | request.url = GURL("http://www.google.com/"); |
| 213 | request.load_flags = 0; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 214 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 215 | StaticSocketDataProvider data(data_reads, reads_count, NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 216 | session_deps.socket_factory.AddSocketDataProvider(&data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 217 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 218 | TestCompletionCallback callback; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 219 | |
[email protected] | 169d001 | 2010-05-10 23:20:12 | [diff] [blame] | 220 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 221 | int rv = trans->Start(&request, &callback, log.bound()); |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 222 | EXPECT_EQ(ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 223 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 224 | out.rv = callback.WaitForResult(); |
| 225 | if (out.rv != OK) |
| 226 | return out; |
| 227 | |
| 228 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 229 | EXPECT_TRUE(response != NULL); |
| 230 | |
| 231 | EXPECT_TRUE(response->headers != NULL); |
| 232 | out.status_line = response->headers->GetStatusLine(); |
| 233 | |
| 234 | rv = ReadTransaction(trans.get(), &out.response_data); |
| 235 | EXPECT_EQ(OK, rv); |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 236 | size_t pos = ExpectLogContainsSomewhere( |
[email protected] | 169d001 | 2010-05-10 23:20:12 | [diff] [blame] | 237 | log.entries(), 0, NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_HEADERS, |
| 238 | NetLog::PHASE_NONE); |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 239 | ExpectLogContainsSomewhere( |
| 240 | log.entries(), pos, |
| 241 | NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS, |
| 242 | NetLog::PHASE_NONE); |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 243 | |
[email protected] | aecfbf2 | 2008-10-16 02:02:47 | [diff] [blame] | 244 | return out; |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 245 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 246 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 247 | void ConnectStatusHelperWithExpectedStatus(const MockRead& status, |
| 248 | int expected_status); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 249 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 250 | void ConnectStatusHelper(const MockRead& status); |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 251 | }; |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 252 | |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 253 | // Fill |str| with a long header list that consumes >= |size| bytes. |
| 254 | void FillLargeHeadersString(std::string* str, int size) { |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 255 | const char* row = |
| 256 | "SomeHeaderName: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n"; |
| 257 | const int sizeof_row = strlen(row); |
| 258 | const int num_rows = static_cast<int>( |
| 259 | ceil(static_cast<float>(size) / sizeof_row)); |
| 260 | const int sizeof_data = num_rows * sizeof_row; |
| 261 | DCHECK(sizeof_data >= size); |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 262 | str->reserve(sizeof_data); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 263 | |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 264 | for (int i = 0; i < num_rows; ++i) |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 265 | str->append(row, sizeof_row); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 266 | } |
| 267 | |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 268 | // Alternative functions that eliminate randomness and dependency on the local |
| 269 | // host name so that the generated NTLM messages are reproducible. |
[email protected] | fe2bc6a | 2009-03-23 16:52:20 | [diff] [blame] | 270 | void MockGenerateRandom1(uint8* output, size_t n) { |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 271 | static const uint8 bytes[] = { |
| 272 | 0x55, 0x29, 0x66, 0x26, 0x6b, 0x9c, 0x73, 0x54 |
| 273 | }; |
| 274 | static size_t current_byte = 0; |
| 275 | for (size_t i = 0; i < n; ++i) { |
| 276 | output[i] = bytes[current_byte++]; |
| 277 | current_byte %= arraysize(bytes); |
| 278 | } |
| 279 | } |
| 280 | |
[email protected] | fe2bc6a | 2009-03-23 16:52:20 | [diff] [blame] | 281 | void MockGenerateRandom2(uint8* output, size_t n) { |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 282 | static const uint8 bytes[] = { |
| 283 | 0x96, 0x79, 0x85, 0xe7, 0x49, 0x93, 0x70, 0xa1, |
| 284 | 0x4e, 0xe7, 0x87, 0x45, 0x31, 0x5b, 0xd3, 0x1f |
| 285 | }; |
| 286 | static size_t current_byte = 0; |
| 287 | for (size_t i = 0; i < n; ++i) { |
| 288 | output[i] = bytes[current_byte++]; |
| 289 | current_byte %= arraysize(bytes); |
| 290 | } |
| 291 | } |
| 292 | |
[email protected] | fe2bc6a | 2009-03-23 16:52:20 | [diff] [blame] | 293 | std::string MockGetHostName() { |
| 294 | return "WTC-WIN7"; |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 295 | } |
| 296 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 297 | template<typename ParentPool> |
| 298 | class CaptureGroupNameSocketPool : public ParentPool { |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 299 | public: |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 300 | explicit CaptureGroupNameSocketPool(HttpNetworkSession* session); |
| 301 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 302 | const std::string last_group_name_received() const { |
| 303 | return last_group_name_; |
| 304 | } |
| 305 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 306 | virtual int RequestSocket(const std::string& group_name, |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 307 | const void* socket_params, |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 308 | RequestPriority priority, |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 309 | ClientSocketHandle* handle, |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 310 | CompletionCallback* callback, |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 311 | const BoundNetLog& net_log) { |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 312 | last_group_name_ = group_name; |
| 313 | return ERR_IO_PENDING; |
| 314 | } |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 315 | virtual void CancelRequest(const std::string& group_name, |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 316 | ClientSocketHandle* handle) {} |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 317 | virtual void ReleaseSocket(const std::string& group_name, |
| 318 | ClientSocket* socket) {} |
| 319 | virtual void CloseIdleSockets() {} |
| 320 | virtual HostResolver* GetHostResolver() const { |
| 321 | return NULL; |
| 322 | } |
| 323 | virtual int IdleSocketCount() const { |
| 324 | return 0; |
| 325 | } |
| 326 | virtual int IdleSocketCountInGroup(const std::string& group_name) const { |
| 327 | return 0; |
| 328 | } |
| 329 | virtual LoadState GetLoadState(const std::string& group_name, |
| 330 | const ClientSocketHandle* handle) const { |
| 331 | return LOAD_STATE_IDLE; |
| 332 | } |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 333 | virtual base::TimeDelta ConnectionTimeout() const { |
| 334 | return base::TimeDelta(); |
| 335 | } |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 336 | |
| 337 | private: |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 338 | std::string last_group_name_; |
| 339 | }; |
| 340 | |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 341 | typedef CaptureGroupNameSocketPool<TCPClientSocketPool> |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 342 | CaptureGroupNameTCPSocketPool; |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 343 | typedef CaptureGroupNameSocketPool<HttpProxyClientSocketPool> |
| 344 | CaptureGroupNameHttpProxySocketPool; |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 345 | typedef CaptureGroupNameSocketPool<SOCKSClientSocketPool> |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 346 | CaptureGroupNameSOCKSSocketPool; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 347 | typedef CaptureGroupNameSocketPool<SSLClientSocketPool> |
| 348 | CaptureGroupNameSSLSocketPool; |
| 349 | |
| 350 | template<typename ParentPool> |
| 351 | CaptureGroupNameSocketPool<ParentPool>::CaptureGroupNameSocketPool( |
| 352 | HttpNetworkSession* session) |
| 353 | : ParentPool(0, 0, NULL, session->host_resolver(), NULL, NULL) {} |
| 354 | |
| 355 | template<> |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 356 | CaptureGroupNameHttpProxySocketPool::CaptureGroupNameSocketPool( |
| 357 | HttpNetworkSession* session) |
| 358 | : HttpProxyClientSocketPool(0, 0, NULL, session->host_resolver(), NULL, |
| 359 | NULL, NULL) {} |
| 360 | |
| 361 | template<> |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 362 | CaptureGroupNameSSLSocketPool::CaptureGroupNameSocketPool( |
| 363 | HttpNetworkSession* session) |
| 364 | : SSLClientSocketPool(0, 0, NULL, session->host_resolver(), NULL, NULL, |
[email protected] | 7abf7d2 | 2010-09-04 01:41:59 | [diff] [blame] | 365 | NULL, NULL, NULL, NULL) {} |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 366 | |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 367 | //----------------------------------------------------------------------------- |
| 368 | |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 369 | // This is the expected list of advertised protocols from the browser's NPN |
| 370 | // list. |
| 371 | static const char kExpectedNPNString[] = "\x08http/1.1\x06spdy/2"; |
| 372 | |
| 373 | // This is the expected return from a current server advertising SPDY. |
| 374 | static const char kAlternateProtocolHttpHeader[] = |
| 375 | "Alternate-Protocol: 443:npn-spdy/2\r\n\r\n"; |
| 376 | |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 377 | TEST_F(HttpNetworkTransactionTest, Basic) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 378 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 379 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 380 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | TEST_F(HttpNetworkTransactionTest, SimpleGET) { |
| 384 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 385 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 386 | MockRead("hello world"), |
[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 200 OK", out.status_line); |
| 393 | EXPECT_EQ("hello world", out.response_data); |
| 394 | } |
| 395 | |
| 396 | // Response with no status line. |
| 397 | TEST_F(HttpNetworkTransactionTest, SimpleGETNoHeaders) { |
| 398 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 399 | MockRead("hello world"), |
[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/0.9 200 OK", out.status_line); |
| 406 | EXPECT_EQ("hello world", out.response_data); |
| 407 | } |
| 408 | |
| 409 | // Allow up to 4 bytes of junk to precede status line. |
| 410 | TEST_F(HttpNetworkTransactionTest, StatusLineJunk2Bytes) { |
| 411 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 412 | MockRead("xxxHTTP/1.0 404 Not Found\nServer: blah\n\nDATA"), |
[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] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 418 | EXPECT_EQ("HTTP/1.0 404 Not Found", out.status_line); |
| 419 | EXPECT_EQ("DATA", out.response_data); |
| 420 | } |
| 421 | |
| 422 | // Allow up to 4 bytes of junk to precede status line. |
| 423 | TEST_F(HttpNetworkTransactionTest, StatusLineJunk4Bytes) { |
| 424 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 425 | MockRead("\n\nQJHTTP/1.0 404 Not Found\nServer: blah\n\nDATA"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 426 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 427 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 428 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 429 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 430 | EXPECT_EQ(OK, out.rv); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 431 | EXPECT_EQ("HTTP/1.0 404 Not Found", out.status_line); |
| 432 | EXPECT_EQ("DATA", out.response_data); |
| 433 | } |
| 434 | |
| 435 | // Beyond 4 bytes of slop and it should fail to find a status line. |
| 436 | TEST_F(HttpNetworkTransactionTest, StatusLineJunk5Bytes) { |
| 437 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 438 | MockRead("xxxxxHTTP/1.1 404 Not Found\nServer: blah"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 439 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 440 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 441 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 442 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 443 | EXPECT_EQ(OK, out.rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 444 | EXPECT_EQ("HTTP/0.9 200 OK", out.status_line); |
| 445 | EXPECT_EQ("xxxxxHTTP/1.1 404 Not Found\nServer: blah", out.response_data); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | // Same as StatusLineJunk4Bytes, except the read chunks are smaller. |
| 449 | TEST_F(HttpNetworkTransactionTest, StatusLineJunk4Bytes_Slow) { |
| 450 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 451 | MockRead("\n"), |
| 452 | MockRead("\n"), |
| 453 | MockRead("Q"), |
| 454 | MockRead("J"), |
| 455 | MockRead("HTTP/1.0 404 Not Found\nServer: blah\n\nDATA"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 456 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 457 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 458 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 459 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 460 | EXPECT_EQ(OK, out.rv); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 461 | EXPECT_EQ("HTTP/1.0 404 Not Found", out.status_line); |
| 462 | EXPECT_EQ("DATA", out.response_data); |
| 463 | } |
| 464 | |
| 465 | // Close the connection before enough bytes to have a status line. |
| 466 | TEST_F(HttpNetworkTransactionTest, StatusLinePartial) { |
| 467 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 468 | MockRead("HTT"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 469 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 470 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 471 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 472 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 473 | EXPECT_EQ(OK, out.rv); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 474 | EXPECT_EQ("HTTP/0.9 200 OK", out.status_line); |
| 475 | EXPECT_EQ("HTT", out.response_data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 476 | } |
| 477 | |
[email protected] | f9d44aa | 2008-09-23 23:57:17 | [diff] [blame] | 478 | // Simulate a 204 response, lacking a Content-Length header, sent over a |
| 479 | // persistent connection. The response should still terminate since a 204 |
| 480 | // cannot have a response body. |
| 481 | TEST_F(HttpNetworkTransactionTest, StopsReading204) { |
| 482 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 483 | MockRead("HTTP/1.1 204 No Content\r\n\r\n"), |
| 484 | MockRead("junk"), // Should not be read!! |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 485 | MockRead(false, OK), |
[email protected] | f9d44aa | 2008-09-23 23:57:17 | [diff] [blame] | 486 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 487 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 488 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 489 | EXPECT_EQ(OK, out.rv); |
[email protected] | f9d44aa | 2008-09-23 23:57:17 | [diff] [blame] | 490 | EXPECT_EQ("HTTP/1.1 204 No Content", out.status_line); |
| 491 | EXPECT_EQ("", out.response_data); |
| 492 | } |
| 493 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 494 | // A simple request using chunked encoding with some extra data after. |
| 495 | // (Like might be seen in a pipelined response.) |
| 496 | TEST_F(HttpNetworkTransactionTest, ChunkedEncoding) { |
| 497 | MockRead data_reads[] = { |
| 498 | MockRead("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"), |
| 499 | MockRead("5\r\nHello\r\n"), |
| 500 | MockRead("1\r\n"), |
| 501 | MockRead(" \r\n"), |
| 502 | MockRead("5\r\nworld\r\n"), |
| 503 | MockRead("0\r\n\r\nHTTP/1.1 200 OK\r\n"), |
| 504 | MockRead(false, OK), |
| 505 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 506 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 507 | arraysize(data_reads)); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 508 | EXPECT_EQ(OK, out.rv); |
| 509 | EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); |
| 510 | EXPECT_EQ("Hello world", out.response_data); |
| 511 | } |
| 512 | |
[email protected] | 9fe44f5 | 2010-09-23 18:36:00 | [diff] [blame] | 513 | // Next tests deal with http://crbug.com/56344. |
| 514 | |
| 515 | TEST_F(HttpNetworkTransactionTest, |
| 516 | MultipleContentLengthHeadersNoTransferEncoding) { |
| 517 | MockRead data_reads[] = { |
| 518 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 519 | MockRead("Content-Length: 10\r\n"), |
| 520 | MockRead("Content-Length: 5\r\n\r\n"), |
| 521 | }; |
| 522 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 523 | arraysize(data_reads)); |
| 524 | EXPECT_EQ(ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_LENGTH, out.rv); |
| 525 | } |
| 526 | |
| 527 | TEST_F(HttpNetworkTransactionTest, |
| 528 | MultipleContentLengthHeadersTransferEncoding) { |
| 529 | MockRead data_reads[] = { |
| 530 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 531 | MockRead("Content-Length: 666\r\n"), |
| 532 | MockRead("Content-Length: 1337\r\n"), |
| 533 | MockRead("Transfer-Encoding: chunked\r\n\r\n"), |
| 534 | MockRead("5\r\nHello\r\n"), |
| 535 | MockRead("1\r\n"), |
| 536 | MockRead(" \r\n"), |
| 537 | MockRead("5\r\nworld\r\n"), |
| 538 | MockRead("0\r\n\r\nHTTP/1.1 200 OK\r\n"), |
| 539 | MockRead(false, OK), |
| 540 | }; |
| 541 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 542 | arraysize(data_reads)); |
| 543 | EXPECT_EQ(OK, out.rv); |
| 544 | EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); |
| 545 | EXPECT_EQ("Hello world", out.response_data); |
| 546 | } |
| 547 | |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 548 | // Do a request using the HEAD method. Verify that we don't try to read the |
| 549 | // message body (since HEAD has none). |
| 550 | TEST_F(HttpNetworkTransactionTest, Head) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 551 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 552 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 553 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 554 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 555 | HttpRequestInfo request; |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 556 | request.method = "HEAD"; |
| 557 | request.url = GURL("http://www.google.com/"); |
| 558 | request.load_flags = 0; |
| 559 | |
| 560 | MockWrite data_writes1[] = { |
| 561 | MockWrite("HEAD / HTTP/1.1\r\n" |
| 562 | "Host: www.google.com\r\n" |
| 563 | "Connection: keep-alive\r\n" |
| 564 | "Content-Length: 0\r\n\r\n"), |
| 565 | }; |
| 566 | MockRead data_reads1[] = { |
| 567 | MockRead("HTTP/1.1 404 Not Found\r\n"), |
| 568 | MockRead("Server: Blah\r\n"), |
| 569 | MockRead("Content-Length: 1234\r\n\r\n"), |
| 570 | |
| 571 | // No response body because the test stops reading here. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 572 | MockRead(false, ERR_UNEXPECTED), // Should not be reached. |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 573 | }; |
| 574 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 575 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 576 | data_writes1, arraysize(data_writes1)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 577 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 578 | |
| 579 | TestCompletionCallback callback1; |
| 580 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 581 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 582 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 583 | |
| 584 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 585 | EXPECT_EQ(OK, rv); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 586 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 587 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 588 | EXPECT_FALSE(response == NULL); |
| 589 | |
| 590 | // Check that the headers got parsed. |
| 591 | EXPECT_TRUE(response->headers != NULL); |
| 592 | EXPECT_EQ(1234, response->headers->GetContentLength()); |
| 593 | EXPECT_EQ("HTTP/1.1 404 Not Found", response->headers->GetStatusLine()); |
| 594 | |
| 595 | std::string server_header; |
| 596 | void* iter = NULL; |
| 597 | bool has_server_header = response->headers->EnumerateHeader( |
| 598 | &iter, "Server", &server_header); |
| 599 | EXPECT_TRUE(has_server_header); |
| 600 | EXPECT_EQ("Blah", server_header); |
| 601 | |
| 602 | // Reading should give EOF right away, since there is no message body |
| 603 | // (despite non-zero content-length). |
| 604 | std::string response_data; |
| 605 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 606 | EXPECT_EQ(OK, rv); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 607 | EXPECT_EQ("", response_data); |
| 608 | } |
| 609 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 610 | TEST_F(HttpNetworkTransactionTest, ReuseConnection) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 611 | SessionDependencies session_deps; |
| 612 | scoped_refptr<HttpNetworkSession> session = CreateSession(&session_deps); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 613 | |
| 614 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 615 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 616 | MockRead("hello"), |
| 617 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 618 | MockRead("world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 619 | MockRead(false, OK), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 620 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 621 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 622 | session_deps.socket_factory.AddSocketDataProvider(&data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 623 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 624 | const char* const kExpectedResponseData[] = { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 625 | "hello", "world" |
| 626 | }; |
| 627 | |
| 628 | for (int i = 0; i < 2; ++i) { |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 629 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 630 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 631 | HttpRequestInfo request; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 632 | request.method = "GET"; |
| 633 | request.url = GURL("http://www.google.com/"); |
| 634 | request.load_flags = 0; |
| 635 | |
| 636 | TestCompletionCallback callback; |
| 637 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 638 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 639 | EXPECT_EQ(ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 640 | |
| 641 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 642 | EXPECT_EQ(OK, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 643 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 644 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 645 | EXPECT_TRUE(response != NULL); |
| 646 | |
| 647 | EXPECT_TRUE(response->headers != NULL); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 648 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 649 | |
| 650 | std::string response_data; |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 651 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 652 | EXPECT_EQ(OK, rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 653 | EXPECT_EQ(kExpectedResponseData[i], response_data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 654 | } |
| 655 | } |
| 656 | |
| 657 | TEST_F(HttpNetworkTransactionTest, Ignores100) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 658 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 659 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 660 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 661 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 662 | HttpRequestInfo request; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 663 | request.method = "POST"; |
| 664 | request.url = GURL("http://www.foo.com/"); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 665 | request.upload_data = new UploadData; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 666 | request.upload_data->AppendBytes("foo", 3); |
| 667 | request.load_flags = 0; |
| 668 | |
| 669 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 670 | MockRead("HTTP/1.0 100 Continue\r\n\r\n"), |
| 671 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 672 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 673 | MockRead(false, OK), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 674 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 675 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 676 | session_deps.socket_factory.AddSocketDataProvider(&data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 677 | |
| 678 | TestCompletionCallback callback; |
| 679 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 680 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 681 | EXPECT_EQ(ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 682 | |
| 683 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 684 | EXPECT_EQ(OK, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 685 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 686 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 687 | EXPECT_TRUE(response != NULL); |
| 688 | |
| 689 | EXPECT_TRUE(response->headers != NULL); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 690 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 691 | |
| 692 | std::string response_data; |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 693 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 694 | EXPECT_EQ(OK, rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 695 | EXPECT_EQ("hello world", response_data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 696 | } |
| 697 | |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 698 | // This test is almost the same as Ignores100 above, but the response contains |
| 699 | // 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] | 700 | // HTTP/1.1 and the two status headers are read in one read. |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 701 | TEST_F(HttpNetworkTransactionTest, Ignores1xx) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 702 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 703 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 704 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 705 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 706 | HttpRequestInfo request; |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 707 | request.method = "GET"; |
| 708 | request.url = GURL("http://www.foo.com/"); |
| 709 | request.load_flags = 0; |
| 710 | |
| 711 | MockRead data_reads[] = { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 712 | MockRead("HTTP/1.1 102 Unspecified status code\r\n\r\n" |
| 713 | "HTTP/1.1 200 OK\r\n\r\n"), |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 714 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 715 | MockRead(false, OK), |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 716 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 717 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 718 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 719 | |
| 720 | TestCompletionCallback callback; |
| 721 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 722 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 723 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 724 | |
| 725 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 726 | EXPECT_EQ(OK, rv); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 727 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 728 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 729 | EXPECT_TRUE(response != NULL); |
| 730 | |
| 731 | EXPECT_TRUE(response->headers != NULL); |
| 732 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 733 | |
| 734 | std::string response_data; |
| 735 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 736 | EXPECT_EQ(OK, rv); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 737 | EXPECT_EQ("hello world", response_data); |
| 738 | } |
| 739 | |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 740 | TEST_F(HttpNetworkTransactionTest, Incomplete100ThenEOF) { |
| 741 | SessionDependencies session_deps; |
| 742 | scoped_ptr<HttpTransaction> trans( |
| 743 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 744 | |
| 745 | HttpRequestInfo request; |
| 746 | request.method = "POST"; |
| 747 | request.url = GURL("http://www.foo.com/"); |
| 748 | request.load_flags = 0; |
| 749 | |
| 750 | MockRead data_reads[] = { |
| 751 | MockRead(false, "HTTP/1.0 100 Continue\r\n"), |
| 752 | MockRead(true, 0), |
| 753 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 754 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 755 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 756 | |
| 757 | TestCompletionCallback callback; |
| 758 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 759 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 760 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 761 | |
| 762 | rv = callback.WaitForResult(); |
| 763 | EXPECT_EQ(OK, rv); |
| 764 | |
| 765 | std::string response_data; |
| 766 | rv = ReadTransaction(trans.get(), &response_data); |
| 767 | EXPECT_EQ(OK, rv); |
| 768 | EXPECT_EQ("", response_data); |
| 769 | } |
| 770 | |
| 771 | TEST_F(HttpNetworkTransactionTest, EmptyResponse) { |
| 772 | SessionDependencies session_deps; |
| 773 | scoped_ptr<HttpTransaction> trans( |
| 774 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 775 | |
| 776 | HttpRequestInfo request; |
| 777 | request.method = "POST"; |
| 778 | request.url = GURL("http://www.foo.com/"); |
| 779 | request.load_flags = 0; |
| 780 | |
| 781 | MockRead data_reads[] = { |
| 782 | MockRead(true, 0), |
| 783 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 784 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 785 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 786 | |
| 787 | TestCompletionCallback callback; |
| 788 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 789 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 790 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 791 | |
| 792 | rv = callback.WaitForResult(); |
| 793 | EXPECT_EQ(ERR_EMPTY_RESPONSE, rv); |
| 794 | } |
| 795 | |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 796 | // read_failure specifies a read failure that should cause the network |
| 797 | // transaction to resend the request. |
| 798 | void HttpNetworkTransactionTest::KeepAliveConnectionResendRequestTest( |
| 799 | const MockRead& read_failure) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 800 | SessionDependencies session_deps; |
| 801 | scoped_refptr<HttpNetworkSession> session = CreateSession(&session_deps); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 802 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 803 | HttpRequestInfo request; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 804 | request.method = "GET"; |
| 805 | request.url = GURL("http://www.foo.com/"); |
| 806 | request.load_flags = 0; |
| 807 | |
| 808 | MockRead data1_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 809 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 810 | MockRead("hello"), |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 811 | read_failure, // Now, we reuse the connection and fail the first read. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 812 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 813 | StaticSocketDataProvider data1(data1_reads, arraysize(data1_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 814 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 815 | |
| 816 | MockRead data2_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 817 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 818 | MockRead("world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 819 | MockRead(true, OK), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 820 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 821 | StaticSocketDataProvider data2(data2_reads, arraysize(data2_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 822 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 823 | |
| 824 | const char* kExpectedResponseData[] = { |
| 825 | "hello", "world" |
| 826 | }; |
| 827 | |
| 828 | for (int i = 0; i < 2; ++i) { |
| 829 | TestCompletionCallback callback; |
| 830 | |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 831 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 832 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 833 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 834 | EXPECT_EQ(ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 835 | |
| 836 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 837 | EXPECT_EQ(OK, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 838 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 839 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 840 | EXPECT_TRUE(response != NULL); |
| 841 | |
| 842 | EXPECT_TRUE(response->headers != NULL); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 843 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 844 | |
| 845 | std::string response_data; |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 846 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 847 | EXPECT_EQ(OK, rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 848 | EXPECT_EQ(kExpectedResponseData[i], response_data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 849 | } |
| 850 | } |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 851 | |
| 852 | TEST_F(HttpNetworkTransactionTest, KeepAliveConnectionReset) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 853 | MockRead read_failure(true, ERR_CONNECTION_RESET); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 854 | KeepAliveConnectionResendRequestTest(read_failure); |
| 855 | } |
| 856 | |
| 857 | TEST_F(HttpNetworkTransactionTest, KeepAliveConnectionEOF) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 858 | MockRead read_failure(false, OK); // EOF |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 859 | KeepAliveConnectionResendRequestTest(read_failure); |
| 860 | } |
| 861 | |
| 862 | TEST_F(HttpNetworkTransactionTest, NonKeepAliveConnectionReset) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 863 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 864 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 865 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 866 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 867 | HttpRequestInfo request; |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 868 | request.method = "GET"; |
| 869 | request.url = GURL("http://www.google.com/"); |
| 870 | request.load_flags = 0; |
| 871 | |
| 872 | MockRead data_reads[] = { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 873 | MockRead(true, ERR_CONNECTION_RESET), |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 874 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), // Should not be used |
| 875 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 876 | MockRead(false, OK), |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 877 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 878 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 879 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 880 | |
| 881 | TestCompletionCallback callback; |
| 882 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 883 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 884 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 885 | |
| 886 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 887 | EXPECT_EQ(ERR_CONNECTION_RESET, rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 888 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 889 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 890 | EXPECT_TRUE(response == NULL); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | // What do various browsers do when the server closes a non-keepalive |
| 894 | // connection without sending any response header or body? |
| 895 | // |
| 896 | // IE7: error page |
| 897 | // Safari 3.1.2 (Windows): error page |
| 898 | // Firefox 3.0.1: blank page |
| 899 | // Opera 9.52: after five attempts, blank page |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 900 | // Us with WinHTTP: error page (ERR_INVALID_RESPONSE) |
| 901 | // Us: error page (EMPTY_RESPONSE) |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 902 | TEST_F(HttpNetworkTransactionTest, NonKeepAliveConnectionEOF) { |
| 903 | MockRead data_reads[] = { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 904 | MockRead(false, OK), // EOF |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 905 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), // Should not be used |
| 906 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 907 | MockRead(false, OK), |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 908 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 909 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 910 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 911 | EXPECT_EQ(ERR_EMPTY_RESPONSE, out.rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 912 | } |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 913 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 914 | // Test that we correctly reuse a keep-alive connection after not explicitly |
| 915 | // reading the body. |
| 916 | TEST_F(HttpNetworkTransactionTest, KeepAliveAfterUnreadBody) { |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 917 | SessionDependencies session_deps; |
| 918 | scoped_refptr<HttpNetworkSession> session = CreateSession(&session_deps); |
| 919 | |
| 920 | HttpRequestInfo request; |
| 921 | request.method = "GET"; |
| 922 | request.url = GURL("http://www.foo.com/"); |
| 923 | request.load_flags = 0; |
| 924 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 925 | // Note that because all these reads happen in the same |
| 926 | // StaticSocketDataProvider, it shows that the same socket is being reused for |
| 927 | // all transactions. |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 928 | MockRead data1_reads[] = { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 929 | MockRead("HTTP/1.1 204 No Content\r\n\r\n"), |
| 930 | MockRead("HTTP/1.1 205 Reset Content\r\n\r\n"), |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 931 | MockRead("HTTP/1.1 304 Not Modified\r\n\r\n"), |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 932 | MockRead("HTTP/1.1 302 Found\r\n" |
| 933 | "Content-Length: 0\r\n\r\n"), |
| 934 | MockRead("HTTP/1.1 302 Found\r\n" |
| 935 | "Content-Length: 5\r\n\r\n" |
| 936 | "hello"), |
| 937 | MockRead("HTTP/1.1 301 Moved Permanently\r\n" |
| 938 | "Content-Length: 0\r\n\r\n"), |
| 939 | MockRead("HTTP/1.1 301 Moved Permanently\r\n" |
| 940 | "Content-Length: 5\r\n\r\n" |
| 941 | "hello"), |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 942 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 943 | MockRead("hello"), |
| 944 | }; |
| 945 | StaticSocketDataProvider data1(data1_reads, arraysize(data1_reads), NULL, 0); |
| 946 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 947 | |
| 948 | MockRead data2_reads[] = { |
| 949 | MockRead(false, ERR_UNEXPECTED), // Should not be reached. |
| 950 | }; |
| 951 | StaticSocketDataProvider data2(data2_reads, arraysize(data2_reads), NULL, 0); |
| 952 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 953 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 954 | const int kNumUnreadBodies = arraysize(data1_reads) - 2; |
| 955 | std::string response_lines[kNumUnreadBodies]; |
| 956 | |
| 957 | for (size_t i = 0; i < arraysize(data1_reads) - 2; ++i) { |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 958 | TestCompletionCallback callback; |
| 959 | |
| 960 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 961 | |
| 962 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 963 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 964 | |
| 965 | rv = callback.WaitForResult(); |
| 966 | EXPECT_EQ(OK, rv); |
| 967 | |
| 968 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 969 | ASSERT_TRUE(response != NULL); |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 970 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 971 | ASSERT_TRUE(response->headers != NULL); |
| 972 | response_lines[i] = response->headers->GetStatusLine(); |
| 973 | |
| 974 | // We intentionally don't read the response bodies. |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 975 | } |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 976 | |
| 977 | const char* const kStatusLines[] = { |
| 978 | "HTTP/1.1 204 No Content", |
| 979 | "HTTP/1.1 205 Reset Content", |
| 980 | "HTTP/1.1 304 Not Modified", |
| 981 | "HTTP/1.1 302 Found", |
| 982 | "HTTP/1.1 302 Found", |
| 983 | "HTTP/1.1 301 Moved Permanently", |
| 984 | "HTTP/1.1 301 Moved Permanently", |
| 985 | }; |
| 986 | |
| 987 | COMPILE_ASSERT(kNumUnreadBodies == arraysize(kStatusLines), |
| 988 | forgot_to_update_kStatusLines); |
| 989 | |
| 990 | for (int i = 0; i < kNumUnreadBodies; ++i) |
| 991 | EXPECT_EQ(kStatusLines[i], response_lines[i]); |
| 992 | |
| 993 | TestCompletionCallback callback; |
| 994 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 995 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 996 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 997 | rv = callback.WaitForResult(); |
| 998 | EXPECT_EQ(OK, rv); |
| 999 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 1000 | ASSERT_TRUE(response != NULL); |
| 1001 | ASSERT_TRUE(response->headers != NULL); |
| 1002 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 1003 | std::string response_data; |
| 1004 | rv = ReadTransaction(trans.get(), &response_data); |
| 1005 | EXPECT_EQ(OK, rv); |
| 1006 | EXPECT_EQ("hello", response_data); |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 1007 | } |
| 1008 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1009 | // Test the request-challenge-retry sequence for basic auth. |
| 1010 | // (basic auth is the easiest to mock, because it has no randomness). |
| 1011 | TEST_F(HttpNetworkTransactionTest, BasicAuth) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1012 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 1013 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 1014 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1015 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1016 | HttpRequestInfo request; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1017 | request.method = "GET"; |
| 1018 | request.url = GURL("http://www.google.com/"); |
| 1019 | request.load_flags = 0; |
| 1020 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1021 | MockWrite data_writes1[] = { |
| 1022 | MockWrite("GET / HTTP/1.1\r\n" |
| 1023 | "Host: www.google.com\r\n" |
| 1024 | "Connection: keep-alive\r\n\r\n"), |
| 1025 | }; |
| 1026 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1027 | MockRead data_reads1[] = { |
| 1028 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 1029 | // Give a couple authenticate options (only the middle one is actually |
| 1030 | // supported). |
[email protected] | 22927ad | 2009-09-21 19:56:19 | [diff] [blame] | 1031 | MockRead("WWW-Authenticate: Basic invalid\r\n"), // Malformed. |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1032 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1033 | MockRead("WWW-Authenticate: UNSUPPORTED realm=\"FOO\"\r\n"), |
| 1034 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1035 | // Large content-length -- won't matter, as connection will be reset. |
| 1036 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1037 | MockRead(false, ERR_FAILED), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1038 | }; |
| 1039 | |
| 1040 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1041 | // be issuing -- the final header line contains the credentials. |
| 1042 | MockWrite data_writes2[] = { |
| 1043 | MockWrite("GET / HTTP/1.1\r\n" |
| 1044 | "Host: www.google.com\r\n" |
| 1045 | "Connection: keep-alive\r\n" |
| 1046 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1047 | }; |
| 1048 | |
| 1049 | // Lastly, the server responds with the actual content. |
| 1050 | MockRead data_reads2[] = { |
| 1051 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 1052 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1053 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1054 | MockRead(false, OK), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1055 | }; |
| 1056 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1057 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1058 | data_writes1, arraysize(data_writes1)); |
| 1059 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 1060 | data_writes2, arraysize(data_writes2)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1061 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 1062 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1063 | |
| 1064 | TestCompletionCallback callback1; |
| 1065 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1066 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1067 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1068 | |
| 1069 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1070 | EXPECT_EQ(OK, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1071 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1072 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1073 | EXPECT_FALSE(response == NULL); |
| 1074 | |
| 1075 | // The password prompt info should have been set in response->auth_challenge. |
| 1076 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 1077 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 1078 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1079 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 1080 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 1081 | |
| 1082 | TestCompletionCallback callback2; |
| 1083 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 1084 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1085 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1086 | |
| 1087 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1088 | EXPECT_EQ(OK, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1089 | |
| 1090 | response = trans->GetResponseInfo(); |
| 1091 | EXPECT_FALSE(response == NULL); |
| 1092 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 1093 | EXPECT_EQ(100, response->headers->GetContentLength()); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1094 | } |
| 1095 | |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1096 | TEST_F(HttpNetworkTransactionTest, DoNotSendAuth) { |
| 1097 | SessionDependencies session_deps; |
| 1098 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 1099 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1100 | |
| 1101 | HttpRequestInfo request; |
| 1102 | request.method = "GET"; |
| 1103 | request.url = GURL("http://www.google.com/"); |
| 1104 | request.load_flags = net::LOAD_DO_NOT_SEND_AUTH_DATA; |
| 1105 | |
| 1106 | MockWrite data_writes[] = { |
| 1107 | MockWrite("GET / HTTP/1.1\r\n" |
| 1108 | "Host: www.google.com\r\n" |
| 1109 | "Connection: keep-alive\r\n\r\n"), |
| 1110 | }; |
| 1111 | |
| 1112 | MockRead data_reads[] = { |
| 1113 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 1114 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1115 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1116 | // Large content-length -- won't matter, as connection will be reset. |
| 1117 | MockRead("Content-Length: 10000\r\n\r\n"), |
| 1118 | MockRead(false, ERR_FAILED), |
| 1119 | }; |
| 1120 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1121 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 1122 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1123 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1124 | TestCompletionCallback callback; |
| 1125 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1126 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1127 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1128 | |
| 1129 | rv = callback.WaitForResult(); |
| 1130 | EXPECT_EQ(0, rv); |
| 1131 | |
| 1132 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 1133 | ASSERT_FALSE(response == NULL); |
| 1134 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 1135 | } |
| 1136 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1137 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
| 1138 | // connection. |
| 1139 | TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAlive) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1140 | SessionDependencies session_deps; |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1141 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1142 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1143 | HttpRequestInfo request; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1144 | request.method = "GET"; |
| 1145 | request.url = GURL("http://www.google.com/"); |
| 1146 | request.load_flags = 0; |
| 1147 | |
| 1148 | MockWrite data_writes1[] = { |
| 1149 | MockWrite("GET / HTTP/1.1\r\n" |
| 1150 | "Host: www.google.com\r\n" |
| 1151 | "Connection: keep-alive\r\n\r\n"), |
| 1152 | |
| 1153 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1154 | // be issuing -- the final header line contains the credentials. |
| 1155 | MockWrite("GET / HTTP/1.1\r\n" |
| 1156 | "Host: www.google.com\r\n" |
| 1157 | "Connection: keep-alive\r\n" |
| 1158 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1159 | }; |
| 1160 | |
| 1161 | MockRead data_reads1[] = { |
| 1162 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 1163 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1164 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1165 | MockRead("Content-Length: 14\r\n\r\n"), |
| 1166 | MockRead("Unauthorized\r\n"), |
| 1167 | |
| 1168 | // Lastly, the server responds with the actual content. |
| 1169 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1170 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1171 | MockRead("Content-Length: 5\r\n\r\n"), |
| 1172 | MockRead("Hello"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1173 | }; |
| 1174 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1175 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1176 | data_writes1, arraysize(data_writes1)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1177 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1178 | |
| 1179 | TestCompletionCallback callback1; |
| 1180 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1181 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1182 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1183 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1184 | |
| 1185 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1186 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1187 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1188 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1189 | EXPECT_FALSE(response == NULL); |
| 1190 | |
| 1191 | // The password prompt info should have been set in response->auth_challenge. |
| 1192 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 1193 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 1194 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1195 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 1196 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 1197 | |
| 1198 | TestCompletionCallback callback2; |
| 1199 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 1200 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1201 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1202 | |
| 1203 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1204 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1205 | |
| 1206 | response = trans->GetResponseInfo(); |
| 1207 | EXPECT_FALSE(response == NULL); |
| 1208 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1209 | EXPECT_EQ(5, response->headers->GetContentLength()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1210 | } |
| 1211 | |
| 1212 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
| 1213 | // connection and with no response body to drain. |
| 1214 | TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAliveNoBody) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1215 | SessionDependencies session_deps; |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1216 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1217 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1218 | HttpRequestInfo request; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1219 | request.method = "GET"; |
| 1220 | request.url = GURL("http://www.google.com/"); |
| 1221 | request.load_flags = 0; |
| 1222 | |
| 1223 | MockWrite data_writes1[] = { |
| 1224 | MockWrite("GET / HTTP/1.1\r\n" |
| 1225 | "Host: www.google.com\r\n" |
| 1226 | "Connection: keep-alive\r\n\r\n"), |
| 1227 | |
| 1228 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1229 | // be issuing -- the final header line contains the credentials. |
| 1230 | MockWrite("GET / HTTP/1.1\r\n" |
| 1231 | "Host: www.google.com\r\n" |
| 1232 | "Connection: keep-alive\r\n" |
| 1233 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1234 | }; |
| 1235 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1236 | MockRead data_reads1[] = { |
| 1237 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 1238 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1239 | MockRead("Content-Length: 0\r\n\r\n"), // No response body. |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1240 | |
| 1241 | // Lastly, the server responds with the actual content. |
| 1242 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1243 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1244 | MockRead("Content-Length: 5\r\n\r\n"), |
| 1245 | MockRead("hello"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1246 | }; |
| 1247 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1248 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1249 | data_writes1, arraysize(data_writes1)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1250 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1251 | |
| 1252 | TestCompletionCallback callback1; |
| 1253 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1254 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1255 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1256 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1257 | |
| 1258 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1259 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1260 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1261 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1262 | EXPECT_FALSE(response == NULL); |
| 1263 | |
| 1264 | // The password prompt info should have been set in response->auth_challenge. |
| 1265 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 1266 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 1267 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1268 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 1269 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 1270 | |
| 1271 | TestCompletionCallback callback2; |
| 1272 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 1273 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1274 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1275 | |
| 1276 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1277 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1278 | |
| 1279 | response = trans->GetResponseInfo(); |
| 1280 | EXPECT_FALSE(response == NULL); |
| 1281 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1282 | EXPECT_EQ(5, response->headers->GetContentLength()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
| 1286 | // connection and with a large response body to drain. |
| 1287 | TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAliveLargeBody) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1288 | SessionDependencies session_deps; |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1289 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1290 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1291 | HttpRequestInfo request; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1292 | request.method = "GET"; |
| 1293 | request.url = GURL("http://www.google.com/"); |
| 1294 | request.load_flags = 0; |
| 1295 | |
| 1296 | MockWrite data_writes1[] = { |
| 1297 | MockWrite("GET / HTTP/1.1\r\n" |
| 1298 | "Host: www.google.com\r\n" |
| 1299 | "Connection: keep-alive\r\n\r\n"), |
| 1300 | |
| 1301 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1302 | // be issuing -- the final header line contains the credentials. |
| 1303 | MockWrite("GET / HTTP/1.1\r\n" |
| 1304 | "Host: www.google.com\r\n" |
| 1305 | "Connection: keep-alive\r\n" |
| 1306 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1307 | }; |
| 1308 | |
| 1309 | // Respond with 5 kb of response body. |
| 1310 | std::string large_body_string("Unauthorized"); |
| 1311 | large_body_string.append(5 * 1024, ' '); |
| 1312 | large_body_string.append("\r\n"); |
| 1313 | |
| 1314 | MockRead data_reads1[] = { |
| 1315 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 1316 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1317 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1318 | // 5134 = 12 + 5 * 1024 + 2 |
| 1319 | MockRead("Content-Length: 5134\r\n\r\n"), |
| 1320 | MockRead(true, large_body_string.data(), large_body_string.size()), |
| 1321 | |
| 1322 | // Lastly, the server responds with the actual content. |
| 1323 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1324 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1325 | MockRead("Content-Length: 5\r\n\r\n"), |
| 1326 | MockRead("hello"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1327 | }; |
| 1328 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1329 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1330 | data_writes1, arraysize(data_writes1)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1331 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1332 | |
| 1333 | TestCompletionCallback callback1; |
| 1334 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1335 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1336 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1337 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1338 | |
| 1339 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1340 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1341 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1342 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1343 | EXPECT_FALSE(response == NULL); |
| 1344 | |
| 1345 | // The password prompt info should have been set in response->auth_challenge. |
| 1346 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 1347 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 1348 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1349 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 1350 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 1351 | |
| 1352 | TestCompletionCallback callback2; |
| 1353 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 1354 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1355 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1356 | |
| 1357 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1358 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1359 | |
| 1360 | response = trans->GetResponseInfo(); |
| 1361 | EXPECT_FALSE(response == NULL); |
| 1362 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1363 | EXPECT_EQ(5, response->headers->GetContentLength()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1364 | } |
| 1365 | |
| 1366 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1367 | // connection, but the server gets impatient and closes the connection. |
| 1368 | TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAliveImpatientServer) { |
| 1369 | SessionDependencies session_deps; |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1370 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1371 | |
| 1372 | HttpRequestInfo request; |
| 1373 | request.method = "GET"; |
| 1374 | request.url = GURL("http://www.google.com/"); |
| 1375 | request.load_flags = 0; |
| 1376 | |
| 1377 | MockWrite data_writes1[] = { |
| 1378 | MockWrite("GET / HTTP/1.1\r\n" |
| 1379 | "Host: www.google.com\r\n" |
| 1380 | "Connection: keep-alive\r\n\r\n"), |
| 1381 | // This simulates the seemingly successful write to a closed connection |
| 1382 | // if the bug is not fixed. |
| 1383 | MockWrite("GET / HTTP/1.1\r\n" |
| 1384 | "Host: www.google.com\r\n" |
| 1385 | "Connection: keep-alive\r\n" |
| 1386 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1387 | }; |
| 1388 | |
| 1389 | MockRead data_reads1[] = { |
| 1390 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 1391 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1392 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1393 | MockRead("Content-Length: 14\r\n\r\n"), |
| 1394 | // Tell MockTCPClientSocket to simulate the server closing the connection. |
| 1395 | MockRead(false, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| 1396 | MockRead("Unauthorized\r\n"), |
| 1397 | MockRead(false, OK), // The server closes the connection. |
| 1398 | }; |
| 1399 | |
| 1400 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1401 | // be issuing -- the final header line contains the credentials. |
| 1402 | MockWrite data_writes2[] = { |
| 1403 | MockWrite("GET / HTTP/1.1\r\n" |
| 1404 | "Host: www.google.com\r\n" |
| 1405 | "Connection: keep-alive\r\n" |
| 1406 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1407 | }; |
| 1408 | |
| 1409 | // Lastly, the server responds with the actual content. |
| 1410 | MockRead data_reads2[] = { |
| 1411 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1412 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1413 | MockRead("Content-Length: 5\r\n\r\n"), |
| 1414 | MockRead("hello"), |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1415 | }; |
| 1416 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1417 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1418 | data_writes1, arraysize(data_writes1)); |
| 1419 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 1420 | data_writes2, arraysize(data_writes2)); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1421 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 1422 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 1423 | |
| 1424 | TestCompletionCallback callback1; |
| 1425 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1426 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1427 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1428 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1429 | |
| 1430 | rv = callback1.WaitForResult(); |
| 1431 | EXPECT_EQ(OK, rv); |
| 1432 | |
| 1433 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 1434 | EXPECT_FALSE(response == NULL); |
| 1435 | |
| 1436 | // The password prompt info should have been set in response->auth_challenge. |
| 1437 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 1438 | |
| 1439 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
| 1440 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 1441 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 1442 | |
| 1443 | TestCompletionCallback callback2; |
| 1444 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 1445 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1446 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1447 | |
| 1448 | rv = callback2.WaitForResult(); |
| 1449 | EXPECT_EQ(OK, rv); |
| 1450 | |
| 1451 | response = trans->GetResponseInfo(); |
| 1452 | ASSERT_FALSE(response == NULL); |
| 1453 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1454 | EXPECT_EQ(5, response->headers->GetContentLength()); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1455 | } |
| 1456 | |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1457 | // Test the request-challenge-retry sequence for basic auth, over a connection |
| 1458 | // that requires a restart when setting up an SSL tunnel. |
| 1459 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyNoKeepAlive) { |
| 1460 | // Configure against proxy server "myproxy:70". |
| 1461 | SessionDependencies session_deps(CreateFixedProxyService("myproxy:70")); |
| 1462 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 1463 | session_deps.net_log = log.bound().net_log(); |
| 1464 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 1465 | |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1466 | HttpRequestInfo request; |
| 1467 | request.method = "GET"; |
| 1468 | request.url = GURL("https://www.google.com/"); |
| 1469 | // when the no authentication data flag is set. |
| 1470 | request.load_flags = net::LOAD_DO_NOT_SEND_AUTH_DATA; |
| 1471 | |
| 1472 | // Since we have proxy, should try to establish tunnel. |
| 1473 | MockWrite data_writes1[] = { |
| 1474 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 1475 | "Host: www.google.com\r\n" |
| 1476 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 1477 | |
| 1478 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1479 | // be issuing -- the final header line contains the credentials. |
| 1480 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 1481 | "Host: www.google.com\r\n" |
| 1482 | "Proxy-Connection: keep-alive\r\n" |
| 1483 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1484 | |
| 1485 | MockWrite("GET / HTTP/1.1\r\n" |
| 1486 | "Host: www.google.com\r\n" |
| 1487 | "Connection: keep-alive\r\n\r\n"), |
| 1488 | }; |
| 1489 | |
| 1490 | // The proxy responds to the connect with a 407, using a persistent |
| 1491 | // connection. |
| 1492 | MockRead data_reads1[] = { |
| 1493 | // No credentials. |
| 1494 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 1495 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1496 | MockRead("Proxy-Connection: close\r\n\r\n"), |
| 1497 | |
| 1498 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 1499 | |
| 1500 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1501 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1502 | MockRead("Content-Length: 5\r\n\r\n"), |
| 1503 | MockRead(false, "hello"), |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1504 | }; |
| 1505 | |
| 1506 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1507 | data_writes1, arraysize(data_writes1)); |
| 1508 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 1509 | SSLSocketDataProvider ssl(true, OK); |
| 1510 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 1511 | |
| 1512 | TestCompletionCallback callback1; |
| 1513 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1514 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 1515 | |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1516 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 1517 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1518 | |
| 1519 | rv = callback1.WaitForResult(); |
| 1520 | EXPECT_EQ(OK, rv); |
| 1521 | size_t pos = ExpectLogContainsSomewhere( |
| 1522 | log.entries(), 0, NetLog::TYPE_HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
| 1523 | NetLog::PHASE_NONE); |
| 1524 | ExpectLogContainsSomewhere( |
| 1525 | log.entries(), pos, |
| 1526 | NetLog::TYPE_HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 1527 | NetLog::PHASE_NONE); |
| 1528 | |
| 1529 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 1530 | ASSERT_FALSE(response == NULL); |
| 1531 | |
| 1532 | EXPECT_EQ(407, response->headers->response_code()); |
| 1533 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 1534 | |
| 1535 | // The password prompt info should have been set in response->auth_challenge. |
| 1536 | ASSERT_FALSE(response->auth_challenge.get() == NULL); |
| 1537 | |
| 1538 | EXPECT_EQ(L"myproxy:70", response->auth_challenge->host_and_port); |
| 1539 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 1540 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 1541 | |
| 1542 | TestCompletionCallback callback2; |
| 1543 | |
| 1544 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
| 1545 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1546 | |
| 1547 | rv = callback2.WaitForResult(); |
| 1548 | EXPECT_EQ(OK, rv); |
| 1549 | |
| 1550 | response = trans->GetResponseInfo(); |
| 1551 | ASSERT_FALSE(response == NULL); |
| 1552 | |
| 1553 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 1554 | EXPECT_EQ(200, response->headers->response_code()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1555 | EXPECT_EQ(5, response->headers->GetContentLength()); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1556 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 1557 | |
| 1558 | // The password prompt info should not be set. |
| 1559 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1560 | |
| 1561 | trans.reset(); |
| 1562 | session->FlushSocketPools(); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1563 | } |
| 1564 | |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1565 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1566 | // proxy connection, when setting up an SSL tunnel. |
| 1567 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyKeepAlive) { |
| 1568 | // Configure against proxy server "myproxy:70". |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1569 | SessionDependencies session_deps(CreateFixedProxyService("myproxy:70")); |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 1570 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 1571 | session_deps.net_log = log.bound().net_log(); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1572 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1573 | |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 1574 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1575 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1576 | HttpRequestInfo request; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1577 | request.method = "GET"; |
| 1578 | request.url = GURL("https://www.google.com/"); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1579 | // Ensure that proxy authentication is attempted even |
| 1580 | // when the no authentication data flag is set. |
| 1581 | request.load_flags = net::LOAD_DO_NOT_SEND_AUTH_DATA; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1582 | |
| 1583 | // Since we have proxy, should try to establish tunnel. |
| 1584 | MockWrite data_writes1[] = { |
| 1585 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 1586 | "Host: www.google.com\r\n" |
| 1587 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1588 | |
| 1589 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1590 | // be issuing -- the final header line contains the credentials. |
| 1591 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 1592 | "Host: www.google.com\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 1593 | "Proxy-Connection: keep-alive\r\n" |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1594 | "Proxy-Authorization: Basic Zm9vOmJheg==\r\n\r\n"), |
| 1595 | }; |
| 1596 | |
| 1597 | // The proxy responds to the connect with a 407, using a persistent |
| 1598 | // connection. |
| 1599 | MockRead data_reads1[] = { |
| 1600 | // No credentials. |
| 1601 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 1602 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1603 | MockRead("Content-Length: 10\r\n\r\n"), |
| 1604 | MockRead("0123456789"), |
| 1605 | |
| 1606 | // Wrong credentials (wrong password). |
| 1607 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 1608 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1609 | MockRead("Content-Length: 10\r\n\r\n"), |
| 1610 | // No response body because the test stops reading here. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1611 | MockRead(false, ERR_UNEXPECTED), // Should not be reached. |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1612 | }; |
| 1613 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1614 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1615 | data_writes1, arraysize(data_writes1)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1616 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1617 | |
| 1618 | TestCompletionCallback callback1; |
| 1619 | |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 1620 | int rv = trans->Start(&request, &callback1, log.bound()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1621 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1622 | |
| 1623 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1624 | EXPECT_EQ(OK, rv); |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 1625 | size_t pos = ExpectLogContainsSomewhere( |
| 1626 | log.entries(), 0, NetLog::TYPE_HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
| 1627 | NetLog::PHASE_NONE); |
| 1628 | ExpectLogContainsSomewhere( |
| 1629 | log.entries(), pos, |
| 1630 | NetLog::TYPE_HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 1631 | NetLog::PHASE_NONE); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1632 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1633 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1634 | EXPECT_FALSE(response == NULL); |
| 1635 | |
| 1636 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 1637 | EXPECT_EQ(407, response->headers->response_code()); |
| 1638 | EXPECT_EQ(10, response->headers->GetContentLength()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1639 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1640 | |
| 1641 | // The password prompt info should have been set in response->auth_challenge. |
| 1642 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 1643 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 1644 | EXPECT_EQ(L"myproxy:70", response->auth_challenge->host_and_port); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1645 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 1646 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 1647 | |
| 1648 | TestCompletionCallback callback2; |
| 1649 | |
| 1650 | // Wrong password (should be "bar"). |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 1651 | rv = trans->RestartWithAuth(kFoo, kBaz, &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1652 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1653 | |
| 1654 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1655 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1656 | |
| 1657 | response = trans->GetResponseInfo(); |
| 1658 | EXPECT_FALSE(response == NULL); |
| 1659 | |
| 1660 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 1661 | EXPECT_EQ(407, response->headers->response_code()); |
| 1662 | EXPECT_EQ(10, response->headers->GetContentLength()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1663 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1664 | |
| 1665 | // The password prompt info should have been set in response->auth_challenge. |
| 1666 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 1667 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 1668 | EXPECT_EQ(L"myproxy:70", response->auth_challenge->host_and_port); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1669 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 1670 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 1671 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 1672 | // Flush the idle socket before the NetLog and HttpNetworkTransaction go |
| 1673 | // out of scope. |
| 1674 | session->FlushSocketPools(); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1675 | } |
| 1676 | |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1677 | // Test that we don't read the response body when we fail to establish a tunnel, |
| 1678 | // even if the user cancels the proxy's auth attempt. |
| 1679 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyCancelTunnel) { |
| 1680 | // Configure against proxy server "myproxy:70". |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1681 | SessionDependencies session_deps(CreateFixedProxyService("myproxy:70")); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1682 | |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 1683 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1684 | |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 1685 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1686 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1687 | HttpRequestInfo request; |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1688 | request.method = "GET"; |
| 1689 | request.url = GURL("https://www.google.com/"); |
| 1690 | request.load_flags = 0; |
| 1691 | |
| 1692 | // Since we have proxy, should try to establish tunnel. |
| 1693 | MockWrite data_writes[] = { |
| 1694 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 1695 | "Host: www.google.com\r\n" |
| 1696 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1697 | }; |
| 1698 | |
| 1699 | // The proxy responds to the connect with a 407. |
| 1700 | MockRead data_reads[] = { |
| 1701 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 1702 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1703 | MockRead("Content-Length: 10\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1704 | MockRead(false, ERR_UNEXPECTED), // Should not be reached. |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1705 | }; |
| 1706 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1707 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 1708 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1709 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1710 | |
| 1711 | TestCompletionCallback callback; |
| 1712 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 1713 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1714 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1715 | |
| 1716 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1717 | EXPECT_EQ(OK, rv); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1718 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1719 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1720 | EXPECT_FALSE(response == NULL); |
| 1721 | |
| 1722 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 1723 | EXPECT_EQ(407, response->headers->response_code()); |
| 1724 | EXPECT_EQ(10, response->headers->GetContentLength()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1725 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1726 | |
| 1727 | std::string response_data; |
| 1728 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1729 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, rv); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 1730 | |
| 1731 | // Flush the idle socket before the HttpNetworkTransaction goes out of scope. |
| 1732 | session->FlushSocketPools(); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1733 | } |
| 1734 | |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 1735 | // Test when a server (non-proxy) returns a 407 (proxy-authenticate). |
| 1736 | // The request should fail with ERR_UNEXPECTED_PROXY_AUTH. |
| 1737 | TEST_F(HttpNetworkTransactionTest, UnexpectedProxyAuth) { |
| 1738 | // We are using a DIRECT connection (i.e. no proxy) for this session. |
| 1739 | SessionDependencies session_deps; |
| 1740 | scoped_ptr<HttpTransaction> trans( |
| 1741 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 1742 | |
| 1743 | HttpRequestInfo request; |
| 1744 | request.method = "GET"; |
| 1745 | request.url = GURL("http://www.google.com/"); |
| 1746 | request.load_flags = 0; |
| 1747 | |
| 1748 | MockWrite data_writes1[] = { |
| 1749 | MockWrite("GET / HTTP/1.1\r\n" |
| 1750 | "Host: www.google.com\r\n" |
| 1751 | "Connection: keep-alive\r\n\r\n"), |
| 1752 | }; |
| 1753 | |
| 1754 | MockRead data_reads1[] = { |
| 1755 | MockRead("HTTP/1.0 407 Proxy Auth required\r\n"), |
| 1756 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1757 | // Large content-length -- won't matter, as connection will be reset. |
| 1758 | MockRead("Content-Length: 10000\r\n\r\n"), |
| 1759 | MockRead(false, ERR_FAILED), |
| 1760 | }; |
| 1761 | |
| 1762 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1763 | data_writes1, arraysize(data_writes1)); |
| 1764 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 1765 | |
| 1766 | TestCompletionCallback callback; |
| 1767 | |
| 1768 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 1769 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1770 | |
| 1771 | rv = callback.WaitForResult(); |
| 1772 | EXPECT_EQ(ERR_UNEXPECTED_PROXY_AUTH, rv); |
| 1773 | } |
| 1774 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 1775 | |
| 1776 | // Test a simple get through an HTTPS Proxy. |
| 1777 | TEST_F(HttpNetworkTransactionTest, HttpsProxyGet) { |
| 1778 | // Configure against https proxy server "proxy:70". |
| 1779 | SessionDependencies session_deps(CreateFixedProxyService("https://proxy:70")); |
| 1780 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 1781 | session_deps.net_log = log.bound().net_log(); |
| 1782 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 1783 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 1784 | HttpRequestInfo request; |
| 1785 | request.method = "GET"; |
| 1786 | request.url = GURL("http://www.google.com/"); |
| 1787 | |
| 1788 | // Since we have proxy, should use full url |
| 1789 | MockWrite data_writes1[] = { |
| 1790 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 1791 | "Host: www.google.com\r\n" |
| 1792 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 1793 | }; |
| 1794 | |
| 1795 | MockRead data_reads1[] = { |
| 1796 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1797 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1798 | MockRead("Content-Length: 100\r\n\r\n"), |
| 1799 | MockRead(false, OK), |
| 1800 | }; |
| 1801 | |
| 1802 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1803 | data_writes1, arraysize(data_writes1)); |
| 1804 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 1805 | SSLSocketDataProvider ssl(true, OK); |
| 1806 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 1807 | |
| 1808 | TestCompletionCallback callback1; |
| 1809 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1810 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 1811 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 1812 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 1813 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1814 | |
| 1815 | rv = callback1.WaitForResult(); |
| 1816 | EXPECT_EQ(OK, rv); |
| 1817 | |
| 1818 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 1819 | ASSERT_FALSE(response == NULL); |
| 1820 | |
| 1821 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 1822 | EXPECT_EQ(200, response->headers->response_code()); |
| 1823 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 1824 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 1825 | |
| 1826 | // The password prompt info should not be set. |
| 1827 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 1828 | } |
| 1829 | |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 1830 | // Test a SPDY get through an HTTPS Proxy. |
| 1831 | TEST_F(HttpNetworkTransactionTest, HttpsProxySpdyGet) { |
| 1832 | // Configure against https proxy server "proxy:70". |
| 1833 | SessionDependencies session_deps(CreateFixedProxyService("https://proxy:70")); |
| 1834 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 1835 | session_deps.net_log = log.bound().net_log(); |
| 1836 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 1837 | |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 1838 | HttpRequestInfo request; |
| 1839 | request.method = "GET"; |
| 1840 | request.url = GURL("http://www.google.com/"); |
| 1841 | request.load_flags = 0; |
| 1842 | |
| 1843 | // fetch http://www.google.com/ via SPDY |
| 1844 | scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST, |
| 1845 | false)); |
| 1846 | MockWrite spdy_writes[] = { CreateMockWrite(*req) }; |
| 1847 | |
| 1848 | scoped_ptr<spdy::SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 1849 | scoped_ptr<spdy::SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); |
| 1850 | MockRead spdy_reads[] = { |
| 1851 | CreateMockRead(*resp), |
| 1852 | CreateMockRead(*data), |
| 1853 | MockRead(true, 0, 0), |
| 1854 | }; |
| 1855 | |
| 1856 | scoped_refptr<DelayedSocketData> spdy_data( |
| 1857 | new DelayedSocketData( |
| 1858 | 1, // wait for one write to finish before reading. |
| 1859 | spdy_reads, arraysize(spdy_reads), |
| 1860 | spdy_writes, arraysize(spdy_writes))); |
| 1861 | session_deps.socket_factory.AddSocketDataProvider(spdy_data); |
| 1862 | |
| 1863 | SSLSocketDataProvider ssl(true, OK); |
| 1864 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
| 1865 | ssl.next_proto = "spdy/2"; |
| 1866 | ssl.was_npn_negotiated = true; |
| 1867 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 1868 | |
| 1869 | TestCompletionCallback callback1; |
| 1870 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1871 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 1872 | |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 1873 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 1874 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1875 | |
| 1876 | rv = callback1.WaitForResult(); |
| 1877 | EXPECT_EQ(OK, rv); |
| 1878 | |
| 1879 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 1880 | ASSERT_TRUE(response != NULL); |
| 1881 | ASSERT_TRUE(response->headers != NULL); |
| 1882 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 1883 | |
| 1884 | std::string response_data; |
| 1885 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 1886 | EXPECT_EQ(net::kUploadData, response_data); |
| 1887 | } |
| 1888 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 1889 | // Test the challenge-response-retry sequence through an HTTPS Proxy |
| 1890 | TEST_F(HttpNetworkTransactionTest, HttpsProxyAuthRetry) { |
| 1891 | // Configure against https proxy server "proxy:70". |
| 1892 | SessionDependencies session_deps(CreateFixedProxyService("https://proxy:70")); |
| 1893 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 1894 | session_deps.net_log = log.bound().net_log(); |
| 1895 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 1896 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 1897 | HttpRequestInfo request; |
| 1898 | request.method = "GET"; |
| 1899 | request.url = GURL("http://www.google.com/"); |
| 1900 | // when the no authentication data flag is set. |
| 1901 | request.load_flags = net::LOAD_DO_NOT_SEND_AUTH_DATA; |
| 1902 | |
| 1903 | // Since we have proxy, should use full url |
| 1904 | MockWrite data_writes1[] = { |
| 1905 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 1906 | "Host: www.google.com\r\n" |
| 1907 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 1908 | |
| 1909 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1910 | // be issuing -- the final header line contains the credentials. |
| 1911 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 1912 | "Host: www.google.com\r\n" |
| 1913 | "Proxy-Connection: keep-alive\r\n" |
| 1914 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1915 | }; |
| 1916 | |
| 1917 | // The proxy responds to the GET with a 407, using a persistent |
| 1918 | // connection. |
| 1919 | MockRead data_reads1[] = { |
| 1920 | // No credentials. |
| 1921 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 1922 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1923 | MockRead("Proxy-Connection: keep-alive\r\n"), |
| 1924 | MockRead("Content-Length: 0\r\n\r\n"), |
| 1925 | |
| 1926 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1927 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1928 | MockRead("Content-Length: 100\r\n\r\n"), |
| 1929 | MockRead(false, OK), |
| 1930 | }; |
| 1931 | |
| 1932 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1933 | data_writes1, arraysize(data_writes1)); |
| 1934 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 1935 | SSLSocketDataProvider ssl(true, OK); |
| 1936 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 1937 | |
| 1938 | TestCompletionCallback callback1; |
| 1939 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1940 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 1941 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 1942 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 1943 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1944 | |
| 1945 | rv = callback1.WaitForResult(); |
| 1946 | EXPECT_EQ(OK, rv); |
| 1947 | |
| 1948 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 1949 | ASSERT_FALSE(response == NULL); |
| 1950 | |
| 1951 | EXPECT_EQ(407, response->headers->response_code()); |
| 1952 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 1953 | |
| 1954 | // The password prompt info should have been set in response->auth_challenge. |
| 1955 | ASSERT_FALSE(response->auth_challenge.get() == NULL); |
| 1956 | |
| 1957 | EXPECT_EQ(L"proxy:70", response->auth_challenge->host_and_port); |
| 1958 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 1959 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 1960 | |
| 1961 | TestCompletionCallback callback2; |
| 1962 | |
| 1963 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
| 1964 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1965 | |
| 1966 | rv = callback2.WaitForResult(); |
| 1967 | EXPECT_EQ(OK, rv); |
| 1968 | |
| 1969 | response = trans->GetResponseInfo(); |
| 1970 | ASSERT_FALSE(response == NULL); |
| 1971 | |
| 1972 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 1973 | EXPECT_EQ(200, response->headers->response_code()); |
| 1974 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 1975 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 1976 | |
| 1977 | // The password prompt info should not be set. |
| 1978 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 1979 | } |
| 1980 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 1981 | void HttpNetworkTransactionTest::ConnectStatusHelperWithExpectedStatus( |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 1982 | const MockRead& status, int expected_status) { |
| 1983 | // Configure against proxy server "myproxy:70". |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1984 | SessionDependencies session_deps(CreateFixedProxyService("myproxy:70")); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 1985 | |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1986 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 1987 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1988 | HttpRequestInfo request; |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 1989 | request.method = "GET"; |
| 1990 | request.url = GURL("https://www.google.com/"); |
| 1991 | request.load_flags = 0; |
| 1992 | |
| 1993 | // Since we have proxy, should try to establish tunnel. |
| 1994 | MockWrite data_writes[] = { |
| 1995 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 1996 | "Host: www.google.com\r\n" |
| 1997 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 1998 | }; |
| 1999 | |
| 2000 | MockRead data_reads[] = { |
| 2001 | status, |
| 2002 | MockRead("Content-Length: 10\r\n\r\n"), |
| 2003 | // No response body because the test stops reading here. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2004 | MockRead(false, ERR_UNEXPECTED), // Should not be reached. |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2005 | }; |
| 2006 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 2007 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 2008 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 2009 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2010 | |
| 2011 | TestCompletionCallback callback; |
| 2012 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2013 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 2014 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 2015 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2016 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2017 | |
| 2018 | rv = callback.WaitForResult(); |
| 2019 | EXPECT_EQ(expected_status, rv); |
| 2020 | } |
| 2021 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 2022 | void HttpNetworkTransactionTest::ConnectStatusHelper(const MockRead& status) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2023 | ConnectStatusHelperWithExpectedStatus( |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2024 | status, ERR_TUNNEL_CONNECTION_FAILED); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2025 | } |
| 2026 | |
| 2027 | TEST_F(HttpNetworkTransactionTest, ConnectStatus100) { |
| 2028 | ConnectStatusHelper(MockRead("HTTP/1.1 100 Continue\r\n")); |
| 2029 | } |
| 2030 | |
| 2031 | TEST_F(HttpNetworkTransactionTest, ConnectStatus101) { |
| 2032 | ConnectStatusHelper(MockRead("HTTP/1.1 101 Switching Protocols\r\n")); |
| 2033 | } |
| 2034 | |
| 2035 | TEST_F(HttpNetworkTransactionTest, ConnectStatus201) { |
| 2036 | ConnectStatusHelper(MockRead("HTTP/1.1 201 Created\r\n")); |
| 2037 | } |
| 2038 | |
| 2039 | TEST_F(HttpNetworkTransactionTest, ConnectStatus202) { |
| 2040 | ConnectStatusHelper(MockRead("HTTP/1.1 202 Accepted\r\n")); |
| 2041 | } |
| 2042 | |
| 2043 | TEST_F(HttpNetworkTransactionTest, ConnectStatus203) { |
| 2044 | ConnectStatusHelper( |
| 2045 | MockRead("HTTP/1.1 203 Non-Authoritative Information\r\n")); |
| 2046 | } |
| 2047 | |
| 2048 | TEST_F(HttpNetworkTransactionTest, ConnectStatus204) { |
| 2049 | ConnectStatusHelper(MockRead("HTTP/1.1 204 No Content\r\n")); |
| 2050 | } |
| 2051 | |
| 2052 | TEST_F(HttpNetworkTransactionTest, ConnectStatus205) { |
| 2053 | ConnectStatusHelper(MockRead("HTTP/1.1 205 Reset Content\r\n")); |
| 2054 | } |
| 2055 | |
| 2056 | TEST_F(HttpNetworkTransactionTest, ConnectStatus206) { |
| 2057 | ConnectStatusHelper(MockRead("HTTP/1.1 206 Partial Content\r\n")); |
| 2058 | } |
| 2059 | |
| 2060 | TEST_F(HttpNetworkTransactionTest, ConnectStatus300) { |
| 2061 | ConnectStatusHelper(MockRead("HTTP/1.1 300 Multiple Choices\r\n")); |
| 2062 | } |
| 2063 | |
| 2064 | TEST_F(HttpNetworkTransactionTest, ConnectStatus301) { |
| 2065 | ConnectStatusHelper(MockRead("HTTP/1.1 301 Moved Permanently\r\n")); |
| 2066 | } |
| 2067 | |
| 2068 | TEST_F(HttpNetworkTransactionTest, ConnectStatus302) { |
| 2069 | ConnectStatusHelper(MockRead("HTTP/1.1 302 Found\r\n")); |
| 2070 | } |
| 2071 | |
| 2072 | TEST_F(HttpNetworkTransactionTest, ConnectStatus303) { |
| 2073 | ConnectStatusHelper(MockRead("HTTP/1.1 303 See Other\r\n")); |
| 2074 | } |
| 2075 | |
| 2076 | TEST_F(HttpNetworkTransactionTest, ConnectStatus304) { |
| 2077 | ConnectStatusHelper(MockRead("HTTP/1.1 304 Not Modified\r\n")); |
| 2078 | } |
| 2079 | |
| 2080 | TEST_F(HttpNetworkTransactionTest, ConnectStatus305) { |
| 2081 | ConnectStatusHelper(MockRead("HTTP/1.1 305 Use Proxy\r\n")); |
| 2082 | } |
| 2083 | |
| 2084 | TEST_F(HttpNetworkTransactionTest, ConnectStatus306) { |
| 2085 | ConnectStatusHelper(MockRead("HTTP/1.1 306\r\n")); |
| 2086 | } |
| 2087 | |
| 2088 | TEST_F(HttpNetworkTransactionTest, ConnectStatus307) { |
| 2089 | ConnectStatusHelper(MockRead("HTTP/1.1 307 Temporary Redirect\r\n")); |
| 2090 | } |
| 2091 | |
| 2092 | TEST_F(HttpNetworkTransactionTest, ConnectStatus400) { |
| 2093 | ConnectStatusHelper(MockRead("HTTP/1.1 400 Bad Request\r\n")); |
| 2094 | } |
| 2095 | |
| 2096 | TEST_F(HttpNetworkTransactionTest, ConnectStatus401) { |
| 2097 | ConnectStatusHelper(MockRead("HTTP/1.1 401 Unauthorized\r\n")); |
| 2098 | } |
| 2099 | |
| 2100 | TEST_F(HttpNetworkTransactionTest, ConnectStatus402) { |
| 2101 | ConnectStatusHelper(MockRead("HTTP/1.1 402 Payment Required\r\n")); |
| 2102 | } |
| 2103 | |
| 2104 | TEST_F(HttpNetworkTransactionTest, ConnectStatus403) { |
| 2105 | ConnectStatusHelper(MockRead("HTTP/1.1 403 Forbidden\r\n")); |
| 2106 | } |
| 2107 | |
| 2108 | TEST_F(HttpNetworkTransactionTest, ConnectStatus404) { |
| 2109 | ConnectStatusHelper(MockRead("HTTP/1.1 404 Not Found\r\n")); |
| 2110 | } |
| 2111 | |
| 2112 | TEST_F(HttpNetworkTransactionTest, ConnectStatus405) { |
| 2113 | ConnectStatusHelper(MockRead("HTTP/1.1 405 Method Not Allowed\r\n")); |
| 2114 | } |
| 2115 | |
| 2116 | TEST_F(HttpNetworkTransactionTest, ConnectStatus406) { |
| 2117 | ConnectStatusHelper(MockRead("HTTP/1.1 406 Not Acceptable\r\n")); |
| 2118 | } |
| 2119 | |
| 2120 | TEST_F(HttpNetworkTransactionTest, ConnectStatus407) { |
| 2121 | ConnectStatusHelperWithExpectedStatus( |
| 2122 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 2123 | ERR_PROXY_AUTH_UNSUPPORTED); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2124 | } |
| 2125 | |
| 2126 | TEST_F(HttpNetworkTransactionTest, ConnectStatus408) { |
| 2127 | ConnectStatusHelper(MockRead("HTTP/1.1 408 Request Timeout\r\n")); |
| 2128 | } |
| 2129 | |
| 2130 | TEST_F(HttpNetworkTransactionTest, ConnectStatus409) { |
| 2131 | ConnectStatusHelper(MockRead("HTTP/1.1 409 Conflict\r\n")); |
| 2132 | } |
| 2133 | |
| 2134 | TEST_F(HttpNetworkTransactionTest, ConnectStatus410) { |
| 2135 | ConnectStatusHelper(MockRead("HTTP/1.1 410 Gone\r\n")); |
| 2136 | } |
| 2137 | |
| 2138 | TEST_F(HttpNetworkTransactionTest, ConnectStatus411) { |
| 2139 | ConnectStatusHelper(MockRead("HTTP/1.1 411 Length Required\r\n")); |
| 2140 | } |
| 2141 | |
| 2142 | TEST_F(HttpNetworkTransactionTest, ConnectStatus412) { |
| 2143 | ConnectStatusHelper(MockRead("HTTP/1.1 412 Precondition Failed\r\n")); |
| 2144 | } |
| 2145 | |
| 2146 | TEST_F(HttpNetworkTransactionTest, ConnectStatus413) { |
| 2147 | ConnectStatusHelper(MockRead("HTTP/1.1 413 Request Entity Too Large\r\n")); |
| 2148 | } |
| 2149 | |
| 2150 | TEST_F(HttpNetworkTransactionTest, ConnectStatus414) { |
| 2151 | ConnectStatusHelper(MockRead("HTTP/1.1 414 Request-URI Too Long\r\n")); |
| 2152 | } |
| 2153 | |
| 2154 | TEST_F(HttpNetworkTransactionTest, ConnectStatus415) { |
| 2155 | ConnectStatusHelper(MockRead("HTTP/1.1 415 Unsupported Media Type\r\n")); |
| 2156 | } |
| 2157 | |
| 2158 | TEST_F(HttpNetworkTransactionTest, ConnectStatus416) { |
| 2159 | ConnectStatusHelper( |
| 2160 | MockRead("HTTP/1.1 416 Requested Range Not Satisfiable\r\n")); |
| 2161 | } |
| 2162 | |
| 2163 | TEST_F(HttpNetworkTransactionTest, ConnectStatus417) { |
| 2164 | ConnectStatusHelper(MockRead("HTTP/1.1 417 Expectation Failed\r\n")); |
| 2165 | } |
| 2166 | |
| 2167 | TEST_F(HttpNetworkTransactionTest, ConnectStatus500) { |
| 2168 | ConnectStatusHelper(MockRead("HTTP/1.1 500 Internal Server Error\r\n")); |
| 2169 | } |
| 2170 | |
| 2171 | TEST_F(HttpNetworkTransactionTest, ConnectStatus501) { |
| 2172 | ConnectStatusHelper(MockRead("HTTP/1.1 501 Not Implemented\r\n")); |
| 2173 | } |
| 2174 | |
| 2175 | TEST_F(HttpNetworkTransactionTest, ConnectStatus502) { |
| 2176 | ConnectStatusHelper(MockRead("HTTP/1.1 502 Bad Gateway\r\n")); |
| 2177 | } |
| 2178 | |
| 2179 | TEST_F(HttpNetworkTransactionTest, ConnectStatus503) { |
| 2180 | ConnectStatusHelper(MockRead("HTTP/1.1 503 Service Unavailable\r\n")); |
| 2181 | } |
| 2182 | |
| 2183 | TEST_F(HttpNetworkTransactionTest, ConnectStatus504) { |
| 2184 | ConnectStatusHelper(MockRead("HTTP/1.1 504 Gateway Timeout\r\n")); |
| 2185 | } |
| 2186 | |
| 2187 | TEST_F(HttpNetworkTransactionTest, ConnectStatus505) { |
| 2188 | ConnectStatusHelper(MockRead("HTTP/1.1 505 HTTP Version Not Supported\r\n")); |
| 2189 | } |
| 2190 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2191 | // Test the flow when both the proxy server AND origin server require |
| 2192 | // authentication. Again, this uses basic auth for both since that is |
| 2193 | // the simplest to mock. |
| 2194 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyThenServer) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2195 | SessionDependencies session_deps(CreateFixedProxyService("myproxy:70")); |
[email protected] | db8f44c | 2008-12-13 04:52:01 | [diff] [blame] | 2196 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2197 | // Configure against proxy server "myproxy:70". |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2198 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 2199 | CreateSession(&session_deps))); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2200 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2201 | HttpRequestInfo request; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2202 | request.method = "GET"; |
| 2203 | request.url = GURL("http://www.google.com/"); |
| 2204 | request.load_flags = 0; |
| 2205 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2206 | MockWrite data_writes1[] = { |
| 2207 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 2208 | "Host: www.google.com\r\n" |
| 2209 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 2210 | }; |
| 2211 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2212 | MockRead data_reads1[] = { |
| 2213 | MockRead("HTTP/1.0 407 Unauthorized\r\n"), |
| 2214 | // Give a couple authenticate options (only the middle one is actually |
| 2215 | // supported). |
[email protected] | 22927ad | 2009-09-21 19:56:19 | [diff] [blame] | 2216 | MockRead("Proxy-Authenticate: Basic invalid\r\n"), // Malformed. |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2217 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 2218 | MockRead("Proxy-Authenticate: UNSUPPORTED realm=\"FOO\"\r\n"), |
| 2219 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2220 | // Large content-length -- won't matter, as connection will be reset. |
| 2221 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2222 | MockRead(false, ERR_FAILED), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2223 | }; |
| 2224 | |
| 2225 | // After calling trans->RestartWithAuth() the first time, this is the |
| 2226 | // request we should be issuing -- the final header line contains the |
| 2227 | // proxy's credentials. |
| 2228 | MockWrite data_writes2[] = { |
| 2229 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 2230 | "Host: www.google.com\r\n" |
| 2231 | "Proxy-Connection: keep-alive\r\n" |
| 2232 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 2233 | }; |
| 2234 | |
| 2235 | // Now the proxy server lets the request pass through to origin server. |
| 2236 | // The origin server responds with a 401. |
| 2237 | MockRead data_reads2[] = { |
| 2238 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 2239 | // Note: We are using the same realm-name as the proxy server. This is |
| 2240 | // completely valid, as realms are unique across hosts. |
| 2241 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 2242 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2243 | MockRead("Content-Length: 2000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2244 | MockRead(false, ERR_FAILED), // Won't be reached. |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2245 | }; |
| 2246 | |
| 2247 | // After calling trans->RestartWithAuth() the second time, we should send |
| 2248 | // the credentials for both the proxy and origin server. |
| 2249 | MockWrite data_writes3[] = { |
| 2250 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 2251 | "Host: www.google.com\r\n" |
| 2252 | "Proxy-Connection: keep-alive\r\n" |
| 2253 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n" |
| 2254 | "Authorization: Basic Zm9vMjpiYXIy\r\n\r\n"), |
| 2255 | }; |
| 2256 | |
| 2257 | // Lastly we get the desired content. |
| 2258 | MockRead data_reads3[] = { |
| 2259 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 2260 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2261 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2262 | MockRead(false, OK), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2263 | }; |
| 2264 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 2265 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 2266 | data_writes1, arraysize(data_writes1)); |
| 2267 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 2268 | data_writes2, arraysize(data_writes2)); |
| 2269 | StaticSocketDataProvider data3(data_reads3, arraysize(data_reads3), |
| 2270 | data_writes3, arraysize(data_writes3)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 2271 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 2272 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 2273 | session_deps.socket_factory.AddSocketDataProvider(&data3); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2274 | |
| 2275 | TestCompletionCallback callback1; |
| 2276 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 2277 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2278 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2279 | |
| 2280 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2281 | EXPECT_EQ(OK, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2282 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2283 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2284 | EXPECT_FALSE(response == NULL); |
| 2285 | |
| 2286 | // The password prompt info should have been set in response->auth_challenge. |
| 2287 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 2288 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 2289 | EXPECT_EQ(L"myproxy:70", response->auth_challenge->host_and_port); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2290 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 2291 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 2292 | |
| 2293 | TestCompletionCallback callback2; |
| 2294 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 2295 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2296 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2297 | |
| 2298 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2299 | EXPECT_EQ(OK, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2300 | |
| 2301 | response = trans->GetResponseInfo(); |
| 2302 | EXPECT_FALSE(response == NULL); |
| 2303 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 2304 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 2305 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2306 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 2307 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 2308 | |
| 2309 | TestCompletionCallback callback3; |
| 2310 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 2311 | rv = trans->RestartWithAuth(kFoo2, kBar2, &callback3); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2312 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2313 | |
| 2314 | rv = callback3.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2315 | EXPECT_EQ(OK, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2316 | |
| 2317 | response = trans->GetResponseInfo(); |
| 2318 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 2319 | EXPECT_EQ(100, response->headers->GetContentLength()); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2320 | } |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 2321 | |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 2322 | // For the NTLM implementation using SSPI, we skip the NTLM tests since we |
| 2323 | // can't hook into its internals to cause it to generate predictable NTLM |
| 2324 | // authorization headers. |
| 2325 | #if defined(NTLM_PORTABLE) |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2326 | // The NTLM authentication unit tests were generated by capturing the HTTP |
| 2327 | // requests and responses using Fiddler 2 and inspecting the generated random |
| 2328 | // bytes in the debugger. |
| 2329 | |
| 2330 | // Enter the correct password and authenticate successfully. |
| 2331 | TEST_F(HttpNetworkTransactionTest, NTLMAuth1) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2332 | HttpAuthHandlerNTLM::ScopedProcSetter proc_setter(MockGenerateRandom1, |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 2333 | MockGetHostName); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2334 | SessionDependencies session_deps; |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2335 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2336 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2337 | HttpRequestInfo request; |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2338 | request.method = "GET"; |
| 2339 | request.url = GURL("http://172.22.68.17/kids/login.aspx"); |
| 2340 | request.load_flags = 0; |
| 2341 | |
| 2342 | MockWrite data_writes1[] = { |
| 2343 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 2344 | "Host: 172.22.68.17\r\n" |
| 2345 | "Connection: keep-alive\r\n\r\n"), |
| 2346 | }; |
| 2347 | |
| 2348 | MockRead data_reads1[] = { |
| 2349 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 2350 | // Negotiate and NTLM are often requested together. However, we only want |
| 2351 | // to test NTLM. Since Negotiate is preferred over NTLM, we have to skip |
| 2352 | // the header that requests Negotiate for this test. |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2353 | MockRead("WWW-Authenticate: NTLM\r\n"), |
| 2354 | MockRead("Connection: close\r\n"), |
| 2355 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 2356 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2357 | // Missing content -- won't matter, as connection will be reset. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2358 | MockRead(false, ERR_UNEXPECTED), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2359 | }; |
| 2360 | |
| 2361 | MockWrite data_writes2[] = { |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2362 | // After restarting with a null identity, this is the |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2363 | // request we should be issuing -- the final header line contains a Type |
| 2364 | // 1 message. |
| 2365 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 2366 | "Host: 172.22.68.17\r\n" |
| 2367 | "Connection: keep-alive\r\n" |
| 2368 | "Authorization: NTLM " |
| 2369 | "TlRMTVNTUAABAAAAB4IIAAAAAAAAAAAAAAAAAAAAAAA=\r\n\r\n"), |
| 2370 | |
| 2371 | // After calling trans->RestartWithAuth(), we should send a Type 3 message |
| 2372 | // (the credentials for the origin server). The second request continues |
| 2373 | // on the same connection. |
| 2374 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 2375 | "Host: 172.22.68.17\r\n" |
| 2376 | "Connection: keep-alive\r\n" |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2377 | "Authorization: NTLM TlRMTVNTUAADAAAAGAAYAGgAAAAYABgAgA" |
| 2378 | "AAAAAAAABAAAAAGAAYAEAAAAAQABAAWAAAAAAAAAAAAAAABYIIAHQA" |
| 2379 | "ZQBzAHQAaQBuAGcALQBuAHQAbABtAFcAVABDAC0AVwBJAE4ANwBVKW" |
| 2380 | "Yma5xzVAAAAAAAAAAAAAAAAAAAAACH+gWcm+YsP9Tqb9zCR3WAeZZX" |
| 2381 | "ahlhx5I=\r\n\r\n"), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2382 | }; |
| 2383 | |
| 2384 | MockRead data_reads2[] = { |
| 2385 | // The origin server responds with a Type 2 message. |
| 2386 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
| 2387 | MockRead("WWW-Authenticate: NTLM " |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2388 | "TlRMTVNTUAACAAAADAAMADgAAAAFgokCjGpMpPGlYKkAAAAAAAAAALo" |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2389 | "AugBEAAAABQEoCgAAAA9HAE8ATwBHAEwARQACAAwARwBPAE8ARwBMAE" |
| 2390 | "UAAQAaAEEASwBFAEUAUwBBAFIAQQAtAEMATwBSAFAABAAeAGMAbwByA" |
| 2391 | "HAALgBnAG8AbwBnAGwAZQAuAGMAbwBtAAMAQABhAGsAZQBlAHMAYQBy" |
| 2392 | "AGEALQBjAG8AcgBwAC4AYQBkAC4AYwBvAHIAcAAuAGcAbwBvAGcAbAB" |
| 2393 | "lAC4AYwBvAG0ABQAeAGMAbwByAHAALgBnAG8AbwBnAGwAZQAuAGMAbw" |
| 2394 | "BtAAAAAAA=\r\n"), |
| 2395 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 2396 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2397 | MockRead("You are not authorized to view this page\r\n"), |
| 2398 | |
| 2399 | // Lastly we get the desired content. |
| 2400 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 2401 | MockRead("Content-Type: text/html; charset=utf-8\r\n"), |
| 2402 | MockRead("Content-Length: 13\r\n\r\n"), |
| 2403 | MockRead("Please Login\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2404 | MockRead(false, OK), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2405 | }; |
| 2406 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 2407 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 2408 | data_writes1, arraysize(data_writes1)); |
| 2409 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 2410 | data_writes2, arraysize(data_writes2)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 2411 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 2412 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2413 | |
| 2414 | TestCompletionCallback callback1; |
| 2415 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2416 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 2417 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 2418 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2419 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2420 | |
| 2421 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2422 | EXPECT_EQ(OK, rv); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2423 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2424 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
| 2425 | TestCompletionCallback callback2; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 2426 | rv = trans->RestartWithAuth(string16(), string16(), &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2427 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2428 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2429 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2430 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 2431 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2432 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 2433 | ASSERT_FALSE(response == NULL); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2434 | |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 2435 | // The password prompt info should have been set in |
| 2436 | // response->auth_challenge. |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2437 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 2438 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 2439 | 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] | 2440 | EXPECT_EQ(L"", response->auth_challenge->realm); |
| 2441 | EXPECT_EQ(L"ntlm", response->auth_challenge->scheme); |
| 2442 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2443 | TestCompletionCallback callback3; |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2444 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 2445 | rv = trans->RestartWithAuth(kTestingNTLM, kTestingNTLM, &callback3); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2446 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2447 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2448 | rv = callback3.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2449 | EXPECT_EQ(OK, rv); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2450 | |
| 2451 | response = trans->GetResponseInfo(); |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 2452 | ASSERT_FALSE(response == NULL); |
| 2453 | |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2454 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 2455 | EXPECT_EQ(13, response->headers->GetContentLength()); |
| 2456 | } |
| 2457 | |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2458 | // Enter a wrong password, and then the correct one. |
| 2459 | TEST_F(HttpNetworkTransactionTest, NTLMAuth2) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2460 | HttpAuthHandlerNTLM::ScopedProcSetter proc_setter(MockGenerateRandom2, |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 2461 | MockGetHostName); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2462 | SessionDependencies session_deps; |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2463 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2464 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2465 | HttpRequestInfo request; |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2466 | request.method = "GET"; |
| 2467 | request.url = GURL("http://172.22.68.17/kids/login.aspx"); |
| 2468 | request.load_flags = 0; |
| 2469 | |
| 2470 | MockWrite data_writes1[] = { |
| 2471 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 2472 | "Host: 172.22.68.17\r\n" |
| 2473 | "Connection: keep-alive\r\n\r\n"), |
| 2474 | }; |
| 2475 | |
| 2476 | MockRead data_reads1[] = { |
| 2477 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 2478 | // Negotiate and NTLM are often requested together. However, we only want |
| 2479 | // to test NTLM. Since Negotiate is preferred over NTLM, we have to skip |
| 2480 | // the header that requests Negotiate for this test. |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2481 | MockRead("WWW-Authenticate: NTLM\r\n"), |
| 2482 | MockRead("Connection: close\r\n"), |
| 2483 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 2484 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2485 | // Missing content -- won't matter, as connection will be reset. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2486 | MockRead(false, ERR_UNEXPECTED), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2487 | }; |
| 2488 | |
| 2489 | MockWrite data_writes2[] = { |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2490 | // After restarting with a null identity, this is the |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2491 | // request we should be issuing -- the final header line contains a Type |
| 2492 | // 1 message. |
| 2493 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 2494 | "Host: 172.22.68.17\r\n" |
| 2495 | "Connection: keep-alive\r\n" |
| 2496 | "Authorization: NTLM " |
| 2497 | "TlRMTVNTUAABAAAAB4IIAAAAAAAAAAAAAAAAAAAAAAA=\r\n\r\n"), |
| 2498 | |
| 2499 | // After calling trans->RestartWithAuth(), we should send a Type 3 message |
| 2500 | // (the credentials for the origin server). The second request continues |
| 2501 | // on the same connection. |
| 2502 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 2503 | "Host: 172.22.68.17\r\n" |
| 2504 | "Connection: keep-alive\r\n" |
| 2505 | "Authorization: NTLM TlRMTVNTUAADAAAAGAAYAGgAAAAYABgAgA" |
| 2506 | "AAAAAAAABAAAAAGAAYAEAAAAAQABAAWAAAAAAAAAAAAAAABYIIAHQA" |
| 2507 | "ZQBzAHQAaQBuAGcALQBuAHQAbABtAFcAVABDAC0AVwBJAE4ANwCWeY" |
| 2508 | "XnSZNwoQAAAAAAAAAAAAAAAAAAAADLa34/phTTKzNTWdub+uyFleOj" |
| 2509 | "4Ww7b7E=\r\n\r\n"), |
| 2510 | }; |
| 2511 | |
| 2512 | MockRead data_reads2[] = { |
| 2513 | // The origin server responds with a Type 2 message. |
| 2514 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
| 2515 | MockRead("WWW-Authenticate: NTLM " |
| 2516 | "TlRMTVNTUAACAAAADAAMADgAAAAFgokCbVWUZezVGpAAAAAAAAAAALo" |
| 2517 | "AugBEAAAABQEoCgAAAA9HAE8ATwBHAEwARQACAAwARwBPAE8ARwBMAE" |
| 2518 | "UAAQAaAEEASwBFAEUAUwBBAFIAQQAtAEMATwBSAFAABAAeAGMAbwByA" |
| 2519 | "HAALgBnAG8AbwBnAGwAZQAuAGMAbwBtAAMAQABhAGsAZQBlAHMAYQBy" |
| 2520 | "AGEALQBjAG8AcgBwAC4AYQBkAC4AYwBvAHIAcAAuAGcAbwBvAGcAbAB" |
| 2521 | "lAC4AYwBvAG0ABQAeAGMAbwByAHAALgBnAG8AbwBnAGwAZQAuAGMAbw" |
| 2522 | "BtAAAAAAA=\r\n"), |
| 2523 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 2524 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2525 | MockRead("You are not authorized to view this page\r\n"), |
| 2526 | |
| 2527 | // Wrong password. |
| 2528 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2529 | MockRead("WWW-Authenticate: NTLM\r\n"), |
| 2530 | MockRead("Connection: close\r\n"), |
| 2531 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 2532 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2533 | // Missing content -- won't matter, as connection will be reset. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2534 | MockRead(false, ERR_UNEXPECTED), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2535 | }; |
| 2536 | |
| 2537 | MockWrite data_writes3[] = { |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2538 | // After restarting with a null identity, this is the |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2539 | // request we should be issuing -- the final header line contains a Type |
| 2540 | // 1 message. |
| 2541 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 2542 | "Host: 172.22.68.17\r\n" |
| 2543 | "Connection: keep-alive\r\n" |
| 2544 | "Authorization: NTLM " |
| 2545 | "TlRMTVNTUAABAAAAB4IIAAAAAAAAAAAAAAAAAAAAAAA=\r\n\r\n"), |
| 2546 | |
| 2547 | // After calling trans->RestartWithAuth(), we should send a Type 3 message |
| 2548 | // (the credentials for the origin server). The second request continues |
| 2549 | // on the same connection. |
| 2550 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 2551 | "Host: 172.22.68.17\r\n" |
| 2552 | "Connection: keep-alive\r\n" |
| 2553 | "Authorization: NTLM TlRMTVNTUAADAAAAGAAYAGgAAAAYABgAgA" |
| 2554 | "AAAAAAAABAAAAAGAAYAEAAAAAQABAAWAAAAAAAAAAAAAAABYIIAHQA" |
| 2555 | "ZQBzAHQAaQBuAGcALQBuAHQAbABtAFcAVABDAC0AVwBJAE4ANwBO54" |
| 2556 | "dFMVvTHwAAAAAAAAAAAAAAAAAAAACS7sT6Uzw7L0L//WUqlIaVWpbI" |
| 2557 | "+4MUm7c=\r\n\r\n"), |
| 2558 | }; |
| 2559 | |
| 2560 | MockRead data_reads3[] = { |
| 2561 | // The origin server responds with a Type 2 message. |
| 2562 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
| 2563 | MockRead("WWW-Authenticate: NTLM " |
| 2564 | "TlRMTVNTUAACAAAADAAMADgAAAAFgokCL24VN8dgOR8AAAAAAAAAALo" |
| 2565 | "AugBEAAAABQEoCgAAAA9HAE8ATwBHAEwARQACAAwARwBPAE8ARwBMAE" |
| 2566 | "UAAQAaAEEASwBFAEUAUwBBAFIAQQAtAEMATwBSAFAABAAeAGMAbwByA" |
| 2567 | "HAALgBnAG8AbwBnAGwAZQAuAGMAbwBtAAMAQABhAGsAZQBlAHMAYQBy" |
| 2568 | "AGEALQBjAG8AcgBwAC4AYQBkAC4AYwBvAHIAcAAuAGcAbwBvAGcAbAB" |
| 2569 | "lAC4AYwBvAG0ABQAeAGMAbwByAHAALgBnAG8AbwBnAGwAZQAuAGMAbw" |
| 2570 | "BtAAAAAAA=\r\n"), |
| 2571 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 2572 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2573 | MockRead("You are not authorized to view this page\r\n"), |
| 2574 | |
| 2575 | // Lastly we get the desired content. |
| 2576 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 2577 | MockRead("Content-Type: text/html; charset=utf-8\r\n"), |
| 2578 | MockRead("Content-Length: 13\r\n\r\n"), |
| 2579 | MockRead("Please Login\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2580 | MockRead(false, OK), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2581 | }; |
| 2582 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 2583 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 2584 | data_writes1, arraysize(data_writes1)); |
| 2585 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 2586 | data_writes2, arraysize(data_writes2)); |
| 2587 | StaticSocketDataProvider data3(data_reads3, arraysize(data_reads3), |
| 2588 | data_writes3, arraysize(data_writes3)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 2589 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 2590 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 2591 | session_deps.socket_factory.AddSocketDataProvider(&data3); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2592 | |
| 2593 | TestCompletionCallback callback1; |
| 2594 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2595 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 2596 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 2597 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2598 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2599 | |
| 2600 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2601 | EXPECT_EQ(OK, rv); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2602 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2603 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2604 | TestCompletionCallback callback2; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 2605 | rv = trans->RestartWithAuth(string16(), string16(), &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2606 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2607 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2608 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2609 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2610 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2611 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2612 | EXPECT_FALSE(response == NULL); |
| 2613 | |
| 2614 | // The password prompt info should have been set in response->auth_challenge. |
| 2615 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 2616 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 2617 | 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] | 2618 | EXPECT_EQ(L"", response->auth_challenge->realm); |
| 2619 | EXPECT_EQ(L"ntlm", response->auth_challenge->scheme); |
| 2620 | |
| 2621 | TestCompletionCallback callback3; |
| 2622 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2623 | // Enter the wrong password. |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 2624 | rv = trans->RestartWithAuth(kTestingNTLM, kWrongPassword, &callback3); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2625 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2626 | |
| 2627 | rv = callback3.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2628 | EXPECT_EQ(OK, rv); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2629 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2630 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
| 2631 | TestCompletionCallback callback4; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 2632 | rv = trans->RestartWithAuth(string16(), string16(), &callback4); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2633 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2634 | rv = callback4.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2635 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2636 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 2637 | |
| 2638 | response = trans->GetResponseInfo(); |
| 2639 | EXPECT_FALSE(response == NULL); |
| 2640 | |
| 2641 | // The password prompt info should have been set in response->auth_challenge. |
| 2642 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 2643 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 2644 | EXPECT_EQ(L"172.22.68.17:80", response->auth_challenge->host_and_port); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2645 | EXPECT_EQ(L"", response->auth_challenge->realm); |
| 2646 | EXPECT_EQ(L"ntlm", response->auth_challenge->scheme); |
| 2647 | |
| 2648 | TestCompletionCallback callback5; |
| 2649 | |
| 2650 | // Now enter the right password. |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 2651 | rv = trans->RestartWithAuth(kTestingNTLM, kTestingNTLM, &callback5); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2652 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2653 | |
| 2654 | rv = callback5.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2655 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2656 | |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2657 | response = trans->GetResponseInfo(); |
| 2658 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 2659 | EXPECT_EQ(13, response->headers->GetContentLength()); |
| 2660 | } |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 2661 | #endif // NTLM_PORTABLE |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2662 | |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 2663 | // Test reading a server response which has only headers, and no body. |
| 2664 | // After some maximum number of bytes is consumed, the transaction should |
| 2665 | // fail with ERR_RESPONSE_HEADERS_TOO_BIG. |
| 2666 | TEST_F(HttpNetworkTransactionTest, LargeHeadersNoBody) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2667 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 2668 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 2669 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 2670 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2671 | HttpRequestInfo request; |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 2672 | request.method = "GET"; |
| 2673 | request.url = GURL("http://www.google.com/"); |
| 2674 | request.load_flags = 0; |
| 2675 | |
[email protected] | b75b7b2f | 2009-10-06 00:54:53 | [diff] [blame] | 2676 | // Respond with 300 kb of headers (we should fail after 256 kb). |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 2677 | std::string large_headers_string; |
[email protected] | b75b7b2f | 2009-10-06 00:54:53 | [diff] [blame] | 2678 | FillLargeHeadersString(&large_headers_string, 300 * 1024); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 2679 | |
| 2680 | MockRead data_reads[] = { |
| 2681 | MockRead("HTTP/1.0 200 OK\r\n"), |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 2682 | MockRead(true, large_headers_string.data(), large_headers_string.size()), |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 2683 | MockRead("\r\nBODY"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2684 | MockRead(false, OK), |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 2685 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 2686 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 2687 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 2688 | |
| 2689 | TestCompletionCallback callback; |
| 2690 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 2691 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2692 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 2693 | |
| 2694 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2695 | EXPECT_EQ(ERR_RESPONSE_HEADERS_TOO_BIG, rv); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 2696 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2697 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 2698 | EXPECT_TRUE(response == NULL); |
| 2699 | } |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 2700 | |
| 2701 | // Make sure that we don't try to reuse a TCPClientSocket when failing to |
| 2702 | // establish tunnel. |
| 2703 | // http://code.google.com/p/chromium/issues/detail?id=3772 |
| 2704 | TEST_F(HttpNetworkTransactionTest, DontRecycleTCPSocketForSSLTunnel) { |
| 2705 | // Configure against proxy server "myproxy:70". |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2706 | SessionDependencies session_deps(CreateFixedProxyService("myproxy:70")); |
[email protected] | db8f44c | 2008-12-13 04:52:01 | [diff] [blame] | 2707 | |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2708 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 2709 | |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 2710 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 2711 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2712 | HttpRequestInfo request; |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 2713 | request.method = "GET"; |
| 2714 | request.url = GURL("https://www.google.com/"); |
| 2715 | request.load_flags = 0; |
| 2716 | |
| 2717 | // Since we have proxy, should try to establish tunnel. |
| 2718 | MockWrite data_writes1[] = { |
| 2719 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 2720 | "Host: www.google.com\r\n" |
| 2721 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 2722 | }; |
| 2723 | |
[email protected] | 77848d1 | 2008-11-14 00:00:22 | [diff] [blame] | 2724 | // The proxy responds to the connect with a 404, using a persistent |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 2725 | // connection. Usually a proxy would return 501 (not implemented), |
| 2726 | // or 200 (tunnel established). |
| 2727 | MockRead data_reads1[] = { |
| 2728 | MockRead("HTTP/1.1 404 Not Found\r\n"), |
| 2729 | MockRead("Content-Length: 10\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2730 | MockRead(false, ERR_UNEXPECTED), // Should not be reached. |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 2731 | }; |
| 2732 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 2733 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 2734 | data_writes1, arraysize(data_writes1)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 2735 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 2736 | |
| 2737 | TestCompletionCallback callback1; |
| 2738 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 2739 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2740 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 2741 | |
| 2742 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2743 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, rv); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 2744 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2745 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2746 | EXPECT_TRUE(response == NULL); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 2747 | |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 2748 | // Empty the current queue. This is necessary because idle sockets are |
| 2749 | // added to the connection pool asynchronously with a PostTask. |
| 2750 | MessageLoop::current()->RunAllPending(); |
| 2751 | |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 2752 | // We now check to make sure the TCPClientSocket was not added back to |
| 2753 | // the pool. |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 2754 | EXPECT_EQ(0, session->tcp_socket_pool()->IdleSocketCount()); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 2755 | trans.reset(); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 2756 | MessageLoop::current()->RunAllPending(); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 2757 | // Make sure that the socket didn't get recycled after calling the destructor. |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 2758 | EXPECT_EQ(0, session->tcp_socket_pool()->IdleSocketCount()); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 2759 | } |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 2760 | |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 2761 | // Make sure that we recycle a socket after reading all of the response body. |
| 2762 | TEST_F(HttpNetworkTransactionTest, RecycleSocket) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2763 | SessionDependencies session_deps; |
| 2764 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 2765 | |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 2766 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 2767 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2768 | HttpRequestInfo request; |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 2769 | request.method = "GET"; |
| 2770 | request.url = GURL("http://www.google.com/"); |
| 2771 | request.load_flags = 0; |
| 2772 | |
| 2773 | MockRead data_reads[] = { |
| 2774 | // A part of the response body is received with the response headers. |
| 2775 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\nhel"), |
| 2776 | // The rest of the response body is received in two parts. |
| 2777 | MockRead("lo"), |
| 2778 | MockRead(" world"), |
| 2779 | MockRead("junk"), // Should not be read!! |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2780 | MockRead(false, OK), |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 2781 | }; |
| 2782 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 2783 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 2784 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 2785 | |
| 2786 | TestCompletionCallback callback; |
| 2787 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 2788 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2789 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 2790 | |
| 2791 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2792 | EXPECT_EQ(OK, rv); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 2793 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2794 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 2795 | EXPECT_TRUE(response != NULL); |
| 2796 | |
| 2797 | EXPECT_TRUE(response->headers != NULL); |
| 2798 | std::string status_line = response->headers->GetStatusLine(); |
| 2799 | EXPECT_EQ("HTTP/1.1 200 OK", status_line); |
| 2800 | |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 2801 | EXPECT_EQ(0, session->tcp_socket_pool()->IdleSocketCount()); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 2802 | |
| 2803 | std::string response_data; |
| 2804 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2805 | EXPECT_EQ(OK, rv); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 2806 | EXPECT_EQ("hello world", response_data); |
| 2807 | |
| 2808 | // Empty the current queue. This is necessary because idle sockets are |
| 2809 | // added to the connection pool asynchronously with a PostTask. |
| 2810 | MessageLoop::current()->RunAllPending(); |
| 2811 | |
| 2812 | // 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] | 2813 | EXPECT_EQ(1, session->tcp_socket_pool()->IdleSocketCount()); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 2814 | } |
| 2815 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 2816 | // Make sure that we recycle a SSL socket after reading all of the response |
| 2817 | // body. |
| 2818 | TEST_F(HttpNetworkTransactionTest, RecycleSSLSocket) { |
| 2819 | SessionDependencies session_deps; |
| 2820 | HttpRequestInfo request; |
| 2821 | request.method = "GET"; |
| 2822 | request.url = GURL("https://www.google.com/"); |
| 2823 | request.load_flags = 0; |
| 2824 | |
| 2825 | MockWrite data_writes[] = { |
| 2826 | MockWrite("GET / HTTP/1.1\r\n" |
| 2827 | "Host: www.google.com\r\n" |
| 2828 | "Connection: keep-alive\r\n\r\n"), |
| 2829 | }; |
| 2830 | |
| 2831 | MockRead data_reads[] = { |
| 2832 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 2833 | MockRead("Content-Length: 11\r\n\r\n"), |
| 2834 | MockRead("hello world"), |
| 2835 | MockRead(false, OK), |
| 2836 | }; |
| 2837 | |
| 2838 | SSLSocketDataProvider ssl(true, OK); |
| 2839 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 2840 | |
| 2841 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 2842 | data_writes, arraysize(data_writes)); |
| 2843 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 2844 | |
| 2845 | TestCompletionCallback callback; |
| 2846 | |
| 2847 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 2848 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 2849 | |
| 2850 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 2851 | |
| 2852 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2853 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 2854 | |
| 2855 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 2856 | ASSERT_TRUE(response != NULL); |
| 2857 | ASSERT_TRUE(response->headers != NULL); |
| 2858 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 2859 | |
| 2860 | EXPECT_EQ(0, session->tcp_socket_pool()->IdleSocketCount()); |
| 2861 | |
| 2862 | std::string response_data; |
| 2863 | rv = ReadTransaction(trans.get(), &response_data); |
| 2864 | EXPECT_EQ(OK, rv); |
| 2865 | EXPECT_EQ("hello world", response_data); |
| 2866 | |
| 2867 | // Empty the current queue. This is necessary because idle sockets are |
| 2868 | // added to the connection pool asynchronously with a PostTask. |
| 2869 | MessageLoop::current()->RunAllPending(); |
| 2870 | |
| 2871 | // We now check to make sure the socket was added back to the pool. |
| 2872 | EXPECT_EQ(1, session->ssl_socket_pool()->IdleSocketCount()); |
| 2873 | } |
| 2874 | |
| 2875 | // Grab a SSL socket, use it, and put it back into the pool. Then, reuse it |
| 2876 | // from the pool and make sure that we recover okay. |
| 2877 | TEST_F(HttpNetworkTransactionTest, RecycleDeadSSLSocket) { |
| 2878 | SessionDependencies session_deps; |
| 2879 | HttpRequestInfo request; |
| 2880 | request.method = "GET"; |
| 2881 | request.url = GURL("https://www.google.com/"); |
| 2882 | request.load_flags = 0; |
| 2883 | |
| 2884 | MockWrite data_writes[] = { |
| 2885 | MockWrite("GET / HTTP/1.1\r\n" |
| 2886 | "Host: www.google.com\r\n" |
| 2887 | "Connection: keep-alive\r\n\r\n"), |
| 2888 | MockWrite("GET / HTTP/1.1\r\n" |
| 2889 | "Host: www.google.com\r\n" |
| 2890 | "Connection: keep-alive\r\n\r\n"), |
| 2891 | }; |
| 2892 | |
| 2893 | MockRead data_reads[] = { |
| 2894 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 2895 | MockRead("Content-Length: 11\r\n\r\n"), |
| 2896 | MockRead(false, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| 2897 | MockRead("hello world"), |
| 2898 | MockRead(true, 0, 0) // EOF |
| 2899 | }; |
| 2900 | |
| 2901 | SSLSocketDataProvider ssl(true, OK); |
| 2902 | SSLSocketDataProvider ssl2(true, OK); |
| 2903 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 2904 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl2); |
| 2905 | |
| 2906 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 2907 | data_writes, arraysize(data_writes)); |
| 2908 | StaticSocketDataProvider data2(data_reads, arraysize(data_reads), |
| 2909 | data_writes, arraysize(data_writes)); |
| 2910 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 2911 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 2912 | |
| 2913 | TestCompletionCallback callback; |
| 2914 | |
| 2915 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 2916 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 2917 | |
| 2918 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 2919 | |
| 2920 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2921 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 2922 | |
| 2923 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 2924 | ASSERT_TRUE(response != NULL); |
| 2925 | ASSERT_TRUE(response->headers != NULL); |
| 2926 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 2927 | |
| 2928 | EXPECT_EQ(0, session->tcp_socket_pool()->IdleSocketCount()); |
| 2929 | |
| 2930 | std::string response_data; |
| 2931 | rv = ReadTransaction(trans.get(), &response_data); |
| 2932 | EXPECT_EQ(OK, rv); |
| 2933 | EXPECT_EQ("hello world", response_data); |
| 2934 | |
| 2935 | // Empty the current queue. This is necessary because idle sockets are |
| 2936 | // added to the connection pool asynchronously with a PostTask. |
| 2937 | MessageLoop::current()->RunAllPending(); |
| 2938 | |
| 2939 | // We now check to make sure the socket was added back to the pool. |
| 2940 | EXPECT_EQ(1, session->ssl_socket_pool()->IdleSocketCount()); |
| 2941 | |
| 2942 | // Now start the second transaction, which should reuse the previous socket. |
| 2943 | |
| 2944 | trans.reset(new HttpNetworkTransaction(session)); |
| 2945 | |
| 2946 | rv = trans->Start(&request, &callback, BoundNetLog()); |
| 2947 | |
| 2948 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2949 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 2950 | |
| 2951 | response = trans->GetResponseInfo(); |
| 2952 | ASSERT_TRUE(response != NULL); |
| 2953 | ASSERT_TRUE(response->headers != NULL); |
| 2954 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 2955 | |
| 2956 | EXPECT_EQ(0, session->tcp_socket_pool()->IdleSocketCount()); |
| 2957 | |
| 2958 | rv = ReadTransaction(trans.get(), &response_data); |
| 2959 | EXPECT_EQ(OK, rv); |
| 2960 | EXPECT_EQ("hello world", response_data); |
| 2961 | |
| 2962 | // Empty the current queue. This is necessary because idle sockets are |
| 2963 | // added to the connection pool asynchronously with a PostTask. |
| 2964 | MessageLoop::current()->RunAllPending(); |
| 2965 | |
| 2966 | // We now check to make sure the socket was added back to the pool. |
| 2967 | EXPECT_EQ(1, session->ssl_socket_pool()->IdleSocketCount()); |
| 2968 | } |
| 2969 | |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 2970 | // Make sure that we recycle a socket after a zero-length response. |
| 2971 | // http://crbug.com/9880 |
| 2972 | TEST_F(HttpNetworkTransactionTest, RecycleSocketAfterZeroContentLength) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 2973 | SessionDependencies session_deps; |
| 2974 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 2975 | |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 2976 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 2977 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2978 | HttpRequestInfo request; |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 2979 | request.method = "GET"; |
| 2980 | request.url = GURL("http://www.google.com/csi?v=3&s=web&action=&" |
| 2981 | "tran=undefined&ei=mAXcSeegAo-SMurloeUN&" |
| 2982 | "e=17259,18167,19592,19773,19981,20133,20173,20233&" |
| 2983 | "rt=prt.2642,ol.2649,xjs.2951"); |
| 2984 | request.load_flags = 0; |
| 2985 | |
| 2986 | MockRead data_reads[] = { |
| 2987 | MockRead("HTTP/1.1 204 No Content\r\n" |
| 2988 | "Content-Length: 0\r\n" |
| 2989 | "Content-Type: text/html\r\n\r\n"), |
| 2990 | MockRead("junk"), // Should not be read!! |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2991 | MockRead(false, OK), |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 2992 | }; |
| 2993 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 2994 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 2995 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 2996 | |
| 2997 | TestCompletionCallback callback; |
| 2998 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 2999 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3000 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3001 | |
| 3002 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3003 | EXPECT_EQ(OK, rv); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3004 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3005 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3006 | EXPECT_TRUE(response != NULL); |
| 3007 | |
| 3008 | EXPECT_TRUE(response->headers != NULL); |
| 3009 | std::string status_line = response->headers->GetStatusLine(); |
| 3010 | EXPECT_EQ("HTTP/1.1 204 No Content", status_line); |
| 3011 | |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 3012 | EXPECT_EQ(0, session->tcp_socket_pool()->IdleSocketCount()); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3013 | |
| 3014 | std::string response_data; |
| 3015 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3016 | EXPECT_EQ(OK, rv); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3017 | EXPECT_EQ("", response_data); |
| 3018 | |
| 3019 | // Empty the current queue. This is necessary because idle sockets are |
| 3020 | // added to the connection pool asynchronously with a PostTask. |
| 3021 | MessageLoop::current()->RunAllPending(); |
| 3022 | |
| 3023 | // 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] | 3024 | EXPECT_EQ(1, session->tcp_socket_pool()->IdleSocketCount()); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3025 | } |
| 3026 | |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3027 | TEST_F(HttpNetworkTransactionTest, ResendRequestOnWriteBodyError) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3028 | HttpRequestInfo request[2]; |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3029 | // Transaction 1: a GET request that succeeds. The socket is recycled |
| 3030 | // after use. |
| 3031 | request[0].method = "GET"; |
| 3032 | request[0].url = GURL("http://www.google.com/"); |
| 3033 | request[0].load_flags = 0; |
| 3034 | // Transaction 2: a POST request. Reuses the socket kept alive from |
| 3035 | // transaction 1. The first attempts fails when writing the POST data. |
| 3036 | // This causes the transaction to retry with a new socket. The second |
| 3037 | // attempt succeeds. |
| 3038 | request[1].method = "POST"; |
| 3039 | request[1].url = GURL("http://www.google.com/login.cgi"); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3040 | request[1].upload_data = new UploadData; |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3041 | request[1].upload_data->AppendBytes("foo", 3); |
| 3042 | request[1].load_flags = 0; |
| 3043 | |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 3044 | SessionDependencies session_deps; |
| 3045 | scoped_refptr<HttpNetworkSession> session = CreateSession(&session_deps); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3046 | |
| 3047 | // The first socket is used for transaction 1 and the first attempt of |
| 3048 | // transaction 2. |
| 3049 | |
| 3050 | // The response of transaction 1. |
| 3051 | MockRead data_reads1[] = { |
| 3052 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\n"), |
| 3053 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3054 | MockRead(false, OK), |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3055 | }; |
| 3056 | // The mock write results of transaction 1 and the first attempt of |
| 3057 | // transaction 2. |
| 3058 | MockWrite data_writes1[] = { |
| 3059 | MockWrite(false, 64), // GET |
| 3060 | MockWrite(false, 93), // POST |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3061 | MockWrite(false, ERR_CONNECTION_ABORTED), // POST data |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3062 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3063 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3064 | data_writes1, arraysize(data_writes1)); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3065 | |
| 3066 | // The second socket is used for the second attempt of transaction 2. |
| 3067 | |
| 3068 | // The response of transaction 2. |
| 3069 | MockRead data_reads2[] = { |
| 3070 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 7\r\n\r\n"), |
| 3071 | MockRead("welcome"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3072 | MockRead(false, OK), |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3073 | }; |
| 3074 | // The mock write results of the second attempt of transaction 2. |
| 3075 | MockWrite data_writes2[] = { |
| 3076 | MockWrite(false, 93), // POST |
| 3077 | MockWrite(false, 3), // POST data |
| 3078 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3079 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 3080 | data_writes2, arraysize(data_writes2)); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3081 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3082 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3083 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3084 | |
| 3085 | const char* kExpectedResponseData[] = { |
| 3086 | "hello world", "welcome" |
| 3087 | }; |
| 3088 | |
| 3089 | for (int i = 0; i < 2; ++i) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3090 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 3091 | new HttpNetworkTransaction(session)); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3092 | |
| 3093 | TestCompletionCallback callback; |
| 3094 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3095 | int rv = trans->Start(&request[i], &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3096 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3097 | |
| 3098 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3099 | EXPECT_EQ(OK, rv); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3100 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3101 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3102 | EXPECT_TRUE(response != NULL); |
| 3103 | |
| 3104 | EXPECT_TRUE(response->headers != NULL); |
| 3105 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 3106 | |
| 3107 | std::string response_data; |
| 3108 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3109 | EXPECT_EQ(OK, rv); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3110 | EXPECT_EQ(kExpectedResponseData[i], response_data); |
| 3111 | } |
| 3112 | } |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3113 | |
| 3114 | // Test the request-challenge-retry sequence for basic auth when there is |
| 3115 | // an identity in the URL. The request should be sent as normal, but when |
| 3116 | // it fails the identity from the URL is used to answer the challenge. |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3117 | TEST_F(HttpNetworkTransactionTest, AuthIdentityInURL) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 3118 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 3119 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 3120 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3121 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3122 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3123 | request.method = "GET"; |
| 3124 | // Note: the URL has a username:password in it. |
[email protected] | a97cca4 | 2009-08-14 01:00:29 | [diff] [blame] | 3125 | request.url = GURL("http://foo:b@[email protected]/"); |
| 3126 | |
| 3127 | // The password contains an escaped character -- for this test to pass it |
| 3128 | // will need to be unescaped by HttpNetworkTransaction. |
| 3129 | EXPECT_EQ("b%40r", request.url.password()); |
| 3130 | |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3131 | request.load_flags = LOAD_NORMAL; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3132 | |
| 3133 | MockWrite data_writes1[] = { |
| 3134 | MockWrite("GET / HTTP/1.1\r\n" |
| 3135 | "Host: www.google.com\r\n" |
| 3136 | "Connection: keep-alive\r\n\r\n"), |
| 3137 | }; |
| 3138 | |
| 3139 | MockRead data_reads1[] = { |
| 3140 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 3141 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3142 | MockRead("Content-Length: 10\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3143 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3144 | }; |
| 3145 | |
| 3146 | // After the challenge above, the transaction will be restarted using the |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3147 | // identity from the url (foo, b@r) to answer the challenge. |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3148 | MockWrite data_writes2[] = { |
| 3149 | MockWrite("GET / HTTP/1.1\r\n" |
| 3150 | "Host: www.google.com\r\n" |
| 3151 | "Connection: keep-alive\r\n" |
[email protected] | a97cca4 | 2009-08-14 01:00:29 | [diff] [blame] | 3152 | "Authorization: Basic Zm9vOmJAcg==\r\n\r\n"), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3153 | }; |
| 3154 | |
| 3155 | MockRead data_reads2[] = { |
| 3156 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3157 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3158 | MockRead(false, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3159 | }; |
| 3160 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3161 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3162 | data_writes1, arraysize(data_writes1)); |
| 3163 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 3164 | data_writes2, arraysize(data_writes2)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3165 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3166 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3167 | |
| 3168 | TestCompletionCallback callback1; |
| 3169 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3170 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3171 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3172 | |
| 3173 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3174 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3175 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3176 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
| 3177 | TestCompletionCallback callback2; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 3178 | rv = trans->RestartWithAuth(string16(), string16(), &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3179 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3180 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3181 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3182 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 3183 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3184 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3185 | EXPECT_FALSE(response == NULL); |
| 3186 | |
| 3187 | // There is no challenge info, since the identity in URL worked. |
| 3188 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3189 | |
| 3190 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 3191 | |
| 3192 | // Empty the current queue. |
| 3193 | MessageLoop::current()->RunAllPending(); |
| 3194 | } |
| 3195 | |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3196 | // Test the request-challenge-retry sequence for basic auth when there is |
| 3197 | // an incorrect identity in the URL. The identity from the URL should be used |
| 3198 | // only once. |
| 3199 | TEST_F(HttpNetworkTransactionTest, WrongAuthIdentityInURL) { |
| 3200 | SessionDependencies session_deps; |
| 3201 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 3202 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3203 | |
| 3204 | HttpRequestInfo request; |
| 3205 | request.method = "GET"; |
| 3206 | // Note: the URL has a username:password in it. The password "baz" is |
| 3207 | // wrong (should be "bar"). |
| 3208 | request.url = GURL("http://foo:[email protected]/"); |
| 3209 | |
| 3210 | request.load_flags = LOAD_NORMAL; |
| 3211 | |
| 3212 | MockWrite data_writes1[] = { |
| 3213 | MockWrite("GET / HTTP/1.1\r\n" |
| 3214 | "Host: www.google.com\r\n" |
| 3215 | "Connection: keep-alive\r\n\r\n"), |
| 3216 | }; |
| 3217 | |
| 3218 | MockRead data_reads1[] = { |
| 3219 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 3220 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3221 | MockRead("Content-Length: 10\r\n\r\n"), |
| 3222 | MockRead(false, ERR_FAILED), |
| 3223 | }; |
| 3224 | |
| 3225 | // After the challenge above, the transaction will be restarted using the |
| 3226 | // identity from the url (foo, baz) to answer the challenge. |
| 3227 | MockWrite data_writes2[] = { |
| 3228 | MockWrite("GET / HTTP/1.1\r\n" |
| 3229 | "Host: www.google.com\r\n" |
| 3230 | "Connection: keep-alive\r\n" |
| 3231 | "Authorization: Basic Zm9vOmJheg==\r\n\r\n"), |
| 3232 | }; |
| 3233 | |
| 3234 | MockRead data_reads2[] = { |
| 3235 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 3236 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3237 | MockRead("Content-Length: 10\r\n\r\n"), |
| 3238 | MockRead(false, ERR_FAILED), |
| 3239 | }; |
| 3240 | |
| 3241 | // After the challenge above, the transaction will be restarted using the |
| 3242 | // identity supplied by the user (foo, bar) to answer the challenge. |
| 3243 | MockWrite data_writes3[] = { |
| 3244 | MockWrite("GET / HTTP/1.1\r\n" |
| 3245 | "Host: www.google.com\r\n" |
| 3246 | "Connection: keep-alive\r\n" |
| 3247 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 3248 | }; |
| 3249 | |
| 3250 | MockRead data_reads3[] = { |
| 3251 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3252 | MockRead("Content-Length: 100\r\n\r\n"), |
| 3253 | MockRead(false, OK), |
| 3254 | }; |
| 3255 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3256 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3257 | data_writes1, arraysize(data_writes1)); |
| 3258 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 3259 | data_writes2, arraysize(data_writes2)); |
| 3260 | StaticSocketDataProvider data3(data_reads3, arraysize(data_reads3), |
| 3261 | data_writes3, arraysize(data_writes3)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3262 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3263 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 3264 | session_deps.socket_factory.AddSocketDataProvider(&data3); |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3265 | |
| 3266 | TestCompletionCallback callback1; |
| 3267 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3268 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3269 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3270 | |
| 3271 | rv = callback1.WaitForResult(); |
| 3272 | EXPECT_EQ(OK, rv); |
| 3273 | |
| 3274 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
| 3275 | TestCompletionCallback callback2; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 3276 | rv = trans->RestartWithAuth(string16(), string16(), &callback2); |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3277 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3278 | rv = callback2.WaitForResult(); |
| 3279 | EXPECT_EQ(OK, rv); |
| 3280 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 3281 | |
| 3282 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 3283 | EXPECT_FALSE(response == NULL); |
| 3284 | // The password prompt info should have been set in response->auth_challenge. |
| 3285 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 3286 | |
| 3287 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
| 3288 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 3289 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 3290 | |
| 3291 | TestCompletionCallback callback3; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 3292 | rv = trans->RestartWithAuth(kFoo, kBar, &callback3); |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3293 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3294 | rv = callback3.WaitForResult(); |
| 3295 | EXPECT_EQ(OK, rv); |
| 3296 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 3297 | |
| 3298 | response = trans->GetResponseInfo(); |
| 3299 | EXPECT_FALSE(response == NULL); |
| 3300 | |
| 3301 | // There is no challenge info, since the identity worked. |
| 3302 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3303 | |
| 3304 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 3305 | |
| 3306 | // Empty the current queue. |
| 3307 | MessageLoop::current()->RunAllPending(); |
| 3308 | } |
| 3309 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3310 | // Test that previously tried username/passwords for a realm get re-used. |
| 3311 | TEST_F(HttpNetworkTransactionTest, BasicAuthCacheAndPreauth) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 3312 | SessionDependencies session_deps; |
| 3313 | scoped_refptr<HttpNetworkSession> session = CreateSession(&session_deps); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3314 | |
| 3315 | // Transaction 1: authenticate (foo, bar) on MyRealm1 |
| 3316 | { |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 3317 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3318 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3319 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3320 | request.method = "GET"; |
| 3321 | request.url = GURL("http://www.google.com/x/y/z"); |
| 3322 | request.load_flags = 0; |
| 3323 | |
| 3324 | MockWrite data_writes1[] = { |
| 3325 | MockWrite("GET /x/y/z HTTP/1.1\r\n" |
| 3326 | "Host: www.google.com\r\n" |
| 3327 | "Connection: keep-alive\r\n\r\n"), |
| 3328 | }; |
| 3329 | |
| 3330 | MockRead data_reads1[] = { |
| 3331 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 3332 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3333 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3334 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3335 | }; |
| 3336 | |
| 3337 | // Resend with authorization (username=foo, password=bar) |
| 3338 | MockWrite data_writes2[] = { |
| 3339 | MockWrite("GET /x/y/z HTTP/1.1\r\n" |
| 3340 | "Host: www.google.com\r\n" |
| 3341 | "Connection: keep-alive\r\n" |
| 3342 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 3343 | }; |
| 3344 | |
| 3345 | // Sever accepts the authorization. |
| 3346 | MockRead data_reads2[] = { |
| 3347 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3348 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3349 | MockRead(false, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3350 | }; |
| 3351 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3352 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3353 | data_writes1, arraysize(data_writes1)); |
| 3354 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 3355 | data_writes2, arraysize(data_writes2)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3356 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3357 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3358 | |
| 3359 | TestCompletionCallback callback1; |
| 3360 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3361 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3362 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3363 | |
| 3364 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3365 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3366 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3367 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3368 | EXPECT_FALSE(response == NULL); |
| 3369 | |
| 3370 | // The password prompt info should have been set in |
| 3371 | // response->auth_challenge. |
| 3372 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 3373 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 3374 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3375 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 3376 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 3377 | |
| 3378 | TestCompletionCallback callback2; |
| 3379 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 3380 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3381 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3382 | |
| 3383 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3384 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3385 | |
| 3386 | response = trans->GetResponseInfo(); |
| 3387 | EXPECT_FALSE(response == NULL); |
| 3388 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3389 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 3390 | } |
| 3391 | |
| 3392 | // ------------------------------------------------------------------------ |
| 3393 | |
| 3394 | // Transaction 2: authenticate (foo2, bar2) on MyRealm2 |
| 3395 | { |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 3396 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3397 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3398 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3399 | request.method = "GET"; |
| 3400 | // Note that Transaction 1 was at /x/y/z, so this is in the same |
| 3401 | // protection space as MyRealm1. |
| 3402 | request.url = GURL("http://www.google.com/x/y/a/b"); |
| 3403 | request.load_flags = 0; |
| 3404 | |
| 3405 | MockWrite data_writes1[] = { |
| 3406 | MockWrite("GET /x/y/a/b HTTP/1.1\r\n" |
| 3407 | "Host: www.google.com\r\n" |
| 3408 | "Connection: keep-alive\r\n" |
| 3409 | // Send preemptive authorization for MyRealm1 |
| 3410 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 3411 | }; |
| 3412 | |
| 3413 | // The server didn't like the preemptive authorization, and |
| 3414 | // challenges us for a different realm (MyRealm2). |
| 3415 | MockRead data_reads1[] = { |
| 3416 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 3417 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm2\"\r\n"), |
| 3418 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3419 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3420 | }; |
| 3421 | |
| 3422 | // Resend with authorization for MyRealm2 (username=foo2, password=bar2) |
| 3423 | MockWrite data_writes2[] = { |
| 3424 | MockWrite("GET /x/y/a/b HTTP/1.1\r\n" |
| 3425 | "Host: www.google.com\r\n" |
| 3426 | "Connection: keep-alive\r\n" |
| 3427 | "Authorization: Basic Zm9vMjpiYXIy\r\n\r\n"), |
| 3428 | }; |
| 3429 | |
| 3430 | // Sever accepts the authorization. |
| 3431 | MockRead data_reads2[] = { |
| 3432 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3433 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3434 | MockRead(false, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3435 | }; |
| 3436 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3437 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3438 | data_writes1, arraysize(data_writes1)); |
| 3439 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 3440 | data_writes2, arraysize(data_writes2)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3441 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3442 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3443 | |
| 3444 | TestCompletionCallback callback1; |
| 3445 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3446 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3447 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3448 | |
| 3449 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3450 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3451 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3452 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3453 | EXPECT_FALSE(response == NULL); |
| 3454 | |
| 3455 | // The password prompt info should have been set in |
| 3456 | // response->auth_challenge. |
| 3457 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 3458 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 3459 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3460 | EXPECT_EQ(L"MyRealm2", response->auth_challenge->realm); |
| 3461 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 3462 | |
| 3463 | TestCompletionCallback callback2; |
| 3464 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 3465 | rv = trans->RestartWithAuth(kFoo2, kBar2, &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3466 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3467 | |
| 3468 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3469 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3470 | |
| 3471 | response = trans->GetResponseInfo(); |
| 3472 | EXPECT_FALSE(response == NULL); |
| 3473 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3474 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 3475 | } |
| 3476 | |
| 3477 | // ------------------------------------------------------------------------ |
| 3478 | |
| 3479 | // Transaction 3: Resend a request in MyRealm's protection space -- |
| 3480 | // succeed with preemptive authorization. |
| 3481 | { |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 3482 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3483 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3484 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3485 | request.method = "GET"; |
| 3486 | request.url = GURL("http://www.google.com/x/y/z2"); |
| 3487 | request.load_flags = 0; |
| 3488 | |
| 3489 | MockWrite data_writes1[] = { |
| 3490 | MockWrite("GET /x/y/z2 HTTP/1.1\r\n" |
| 3491 | "Host: www.google.com\r\n" |
| 3492 | "Connection: keep-alive\r\n" |
| 3493 | // The authorization for MyRealm1 gets sent preemptively |
| 3494 | // (since the url is in the same protection space) |
| 3495 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 3496 | }; |
| 3497 | |
| 3498 | // Sever accepts the preemptive authorization |
| 3499 | MockRead data_reads1[] = { |
| 3500 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3501 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3502 | MockRead(false, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3503 | }; |
| 3504 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3505 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3506 | data_writes1, arraysize(data_writes1)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3507 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3508 | |
| 3509 | TestCompletionCallback callback1; |
| 3510 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3511 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3512 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3513 | |
| 3514 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3515 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3516 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3517 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3518 | EXPECT_FALSE(response == NULL); |
| 3519 | |
| 3520 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3521 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 3522 | } |
| 3523 | |
| 3524 | // ------------------------------------------------------------------------ |
| 3525 | |
| 3526 | // Transaction 4: request another URL in MyRealm (however the |
| 3527 | // url is not known to belong to the protection space, so no pre-auth). |
| 3528 | { |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 3529 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3530 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3531 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3532 | request.method = "GET"; |
| 3533 | request.url = GURL("http://www.google.com/x/1"); |
| 3534 | request.load_flags = 0; |
| 3535 | |
| 3536 | MockWrite data_writes1[] = { |
| 3537 | MockWrite("GET /x/1 HTTP/1.1\r\n" |
| 3538 | "Host: www.google.com\r\n" |
| 3539 | "Connection: keep-alive\r\n\r\n"), |
| 3540 | }; |
| 3541 | |
| 3542 | MockRead data_reads1[] = { |
| 3543 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 3544 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3545 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3546 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3547 | }; |
| 3548 | |
| 3549 | // Resend with authorization from MyRealm's cache. |
| 3550 | MockWrite data_writes2[] = { |
| 3551 | MockWrite("GET /x/1 HTTP/1.1\r\n" |
| 3552 | "Host: www.google.com\r\n" |
| 3553 | "Connection: keep-alive\r\n" |
| 3554 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 3555 | }; |
| 3556 | |
| 3557 | // Sever accepts the authorization. |
| 3558 | MockRead data_reads2[] = { |
| 3559 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3560 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3561 | MockRead(false, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3562 | }; |
| 3563 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3564 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3565 | data_writes1, arraysize(data_writes1)); |
| 3566 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 3567 | data_writes2, arraysize(data_writes2)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3568 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3569 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3570 | |
| 3571 | TestCompletionCallback callback1; |
| 3572 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3573 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3574 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3575 | |
| 3576 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3577 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3578 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3579 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
| 3580 | TestCompletionCallback callback2; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 3581 | rv = trans->RestartWithAuth(string16(), string16(), &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3582 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3583 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3584 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3585 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 3586 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3587 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3588 | EXPECT_FALSE(response == NULL); |
| 3589 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3590 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 3591 | } |
| 3592 | |
| 3593 | // ------------------------------------------------------------------------ |
| 3594 | |
| 3595 | // Transaction 5: request a URL in MyRealm, but the server rejects the |
| 3596 | // cached identity. Should invalidate and re-prompt. |
| 3597 | { |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 3598 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3599 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3600 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3601 | request.method = "GET"; |
| 3602 | request.url = GURL("http://www.google.com/p/q/t"); |
| 3603 | request.load_flags = 0; |
| 3604 | |
| 3605 | MockWrite data_writes1[] = { |
| 3606 | MockWrite("GET /p/q/t HTTP/1.1\r\n" |
| 3607 | "Host: www.google.com\r\n" |
| 3608 | "Connection: keep-alive\r\n\r\n"), |
| 3609 | }; |
| 3610 | |
| 3611 | MockRead data_reads1[] = { |
| 3612 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 3613 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3614 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3615 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3616 | }; |
| 3617 | |
| 3618 | // Resend with authorization from cache for MyRealm. |
| 3619 | MockWrite data_writes2[] = { |
| 3620 | MockWrite("GET /p/q/t HTTP/1.1\r\n" |
| 3621 | "Host: www.google.com\r\n" |
| 3622 | "Connection: keep-alive\r\n" |
| 3623 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 3624 | }; |
| 3625 | |
| 3626 | // Sever rejects the authorization. |
| 3627 | MockRead data_reads2[] = { |
| 3628 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 3629 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3630 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3631 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3632 | }; |
| 3633 | |
| 3634 | // At this point we should prompt for new credentials for MyRealm. |
| 3635 | // Restart with username=foo3, password=foo4. |
| 3636 | MockWrite data_writes3[] = { |
| 3637 | MockWrite("GET /p/q/t HTTP/1.1\r\n" |
| 3638 | "Host: www.google.com\r\n" |
| 3639 | "Connection: keep-alive\r\n" |
| 3640 | "Authorization: Basic Zm9vMzpiYXIz\r\n\r\n"), |
| 3641 | }; |
| 3642 | |
| 3643 | // Sever accepts the authorization. |
| 3644 | MockRead data_reads3[] = { |
| 3645 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3646 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3647 | MockRead(false, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3648 | }; |
| 3649 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3650 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3651 | data_writes1, arraysize(data_writes1)); |
| 3652 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 3653 | data_writes2, arraysize(data_writes2)); |
| 3654 | StaticSocketDataProvider data3(data_reads3, arraysize(data_reads3), |
| 3655 | data_writes3, arraysize(data_writes3)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3656 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3657 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 3658 | session_deps.socket_factory.AddSocketDataProvider(&data3); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3659 | |
| 3660 | TestCompletionCallback callback1; |
| 3661 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3662 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3663 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3664 | |
| 3665 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3666 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3667 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3668 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
| 3669 | TestCompletionCallback callback2; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 3670 | rv = trans->RestartWithAuth(string16(), string16(), &callback2); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3671 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3672 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3673 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3674 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 3675 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3676 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3677 | EXPECT_FALSE(response == NULL); |
| 3678 | |
| 3679 | // The password prompt info should have been set in |
| 3680 | // response->auth_challenge. |
| 3681 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 3682 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 3683 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3684 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 3685 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 3686 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3687 | TestCompletionCallback callback3; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3688 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 3689 | rv = trans->RestartWithAuth(kFoo3, kBar3, &callback3); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3690 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3691 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3692 | rv = callback3.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3693 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3694 | |
| 3695 | response = trans->GetResponseInfo(); |
| 3696 | EXPECT_FALSE(response == NULL); |
| 3697 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3698 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 3699 | } |
| 3700 | } |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 3701 | |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 3702 | // Tests that nonce count increments when multiple auth attempts |
| 3703 | // are started with the same nonce. |
| 3704 | TEST_F(HttpNetworkTransactionTest, DigestPreAuthNonceCount) { |
| 3705 | SessionDependencies session_deps; |
| 3706 | scoped_refptr<HttpNetworkSession> session = CreateSession(&session_deps); |
| 3707 | HttpAuthHandlerDigest::SetFixedCnonce(true); |
| 3708 | |
| 3709 | // Transaction 1: authenticate (foo, bar) on MyRealm1 |
| 3710 | { |
| 3711 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 3712 | |
| 3713 | HttpRequestInfo request; |
| 3714 | request.method = "GET"; |
| 3715 | request.url = GURL("http://www.google.com/x/y/z"); |
| 3716 | request.load_flags = 0; |
| 3717 | |
| 3718 | MockWrite data_writes1[] = { |
| 3719 | MockWrite("GET /x/y/z HTTP/1.1\r\n" |
| 3720 | "Host: www.google.com\r\n" |
| 3721 | "Connection: keep-alive\r\n\r\n"), |
| 3722 | }; |
| 3723 | |
| 3724 | MockRead data_reads1[] = { |
| 3725 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 3726 | MockRead("WWW-Authenticate: Digest realm=\"digestive\", nonce=\"OU812\", " |
| 3727 | "algorithm=MD5, qop=\"auth\"\r\n\r\n"), |
| 3728 | MockRead(false, OK), |
| 3729 | }; |
| 3730 | |
| 3731 | // Resend with authorization (username=foo, password=bar) |
| 3732 | MockWrite data_writes2[] = { |
| 3733 | MockWrite("GET /x/y/z HTTP/1.1\r\n" |
| 3734 | "Host: www.google.com\r\n" |
| 3735 | "Connection: keep-alive\r\n" |
| 3736 | "Authorization: Digest username=\"foo\", realm=\"digestive\", " |
| 3737 | "nonce=\"OU812\", uri=\"/x/y/z\", algorithm=MD5, " |
| 3738 | "response=\"03ffbcd30add722589c1de345d7a927f\", qop=auth, " |
| 3739 | "nc=00000001, cnonce=\"0123456789abcdef\"\r\n\r\n"), |
| 3740 | }; |
| 3741 | |
| 3742 | // Sever accepts the authorization. |
| 3743 | MockRead data_reads2[] = { |
| 3744 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3745 | MockRead(false, OK), |
| 3746 | }; |
| 3747 | |
| 3748 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3749 | data_writes1, arraysize(data_writes1)); |
| 3750 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 3751 | data_writes2, arraysize(data_writes2)); |
| 3752 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3753 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 3754 | |
| 3755 | TestCompletionCallback callback1; |
| 3756 | |
| 3757 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
| 3758 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3759 | |
| 3760 | rv = callback1.WaitForResult(); |
| 3761 | EXPECT_EQ(OK, rv); |
| 3762 | |
| 3763 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 3764 | ASSERT_FALSE(response == NULL); |
| 3765 | |
| 3766 | // The password prompt info should have been set in |
| 3767 | // response->auth_challenge. |
| 3768 | ASSERT_FALSE(response->auth_challenge.get() == NULL); |
| 3769 | |
| 3770 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
| 3771 | EXPECT_EQ(L"digestive", response->auth_challenge->realm); |
| 3772 | EXPECT_EQ(L"digest", response->auth_challenge->scheme); |
| 3773 | |
| 3774 | TestCompletionCallback callback2; |
| 3775 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 3776 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 3777 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3778 | |
| 3779 | rv = callback2.WaitForResult(); |
| 3780 | EXPECT_EQ(OK, rv); |
| 3781 | |
| 3782 | response = trans->GetResponseInfo(); |
| 3783 | ASSERT_FALSE(response == NULL); |
| 3784 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3785 | } |
| 3786 | |
| 3787 | // ------------------------------------------------------------------------ |
| 3788 | |
| 3789 | // Transaction 2: Request another resource in digestive's protection space. |
| 3790 | // This will preemptively add an Authorization header which should have an |
| 3791 | // "nc" value of 2 (as compared to 1 in the first use. |
| 3792 | { |
| 3793 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 3794 | |
| 3795 | HttpRequestInfo request; |
| 3796 | request.method = "GET"; |
| 3797 | // Note that Transaction 1 was at /x/y/z, so this is in the same |
| 3798 | // protection space as digest. |
| 3799 | request.url = GURL("http://www.google.com/x/y/a/b"); |
| 3800 | request.load_flags = 0; |
| 3801 | |
| 3802 | MockWrite data_writes1[] = { |
| 3803 | MockWrite("GET /x/y/a/b HTTP/1.1\r\n" |
| 3804 | "Host: www.google.com\r\n" |
| 3805 | "Connection: keep-alive\r\n" |
| 3806 | "Authorization: Digest username=\"foo\", realm=\"digestive\", " |
| 3807 | "nonce=\"OU812\", uri=\"/x/y/a/b\", algorithm=MD5, " |
| 3808 | "response=\"d6f9a2c07d1c5df7b89379dca1269b35\", qop=auth, " |
| 3809 | "nc=00000002, cnonce=\"0123456789abcdef\"\r\n\r\n"), |
| 3810 | }; |
| 3811 | |
| 3812 | // Sever accepts the authorization. |
| 3813 | MockRead data_reads1[] = { |
| 3814 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3815 | MockRead("Content-Length: 100\r\n\r\n"), |
| 3816 | MockRead(false, OK), |
| 3817 | }; |
| 3818 | |
| 3819 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3820 | data_writes1, arraysize(data_writes1)); |
| 3821 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3822 | |
| 3823 | TestCompletionCallback callback1; |
| 3824 | |
| 3825 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
| 3826 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3827 | |
| 3828 | rv = callback1.WaitForResult(); |
| 3829 | EXPECT_EQ(OK, rv); |
| 3830 | |
| 3831 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 3832 | ASSERT_FALSE(response == NULL); |
| 3833 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3834 | } |
| 3835 | } |
| 3836 | |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 3837 | // Test the ResetStateForRestart() private method. |
| 3838 | TEST_F(HttpNetworkTransactionTest, ResetStateForRestart) { |
| 3839 | // Create a transaction (the dependencies aren't important). |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 3840 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 3841 | scoped_ptr<HttpNetworkTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 3842 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 3843 | |
| 3844 | // Setup some state (which we expect ResetStateForRestart() will clear). |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 3845 | trans->read_buf_ = new IOBuffer(15); |
| 3846 | trans->read_buf_len_ = 15; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 3847 | trans->request_headers_ = "Authorization: NTLM"; |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 3848 | |
| 3849 | // Setup state in response_ |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 3850 | HttpResponseInfo* response = &trans->response_; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 3851 | response->auth_challenge = new AuthChallengeInfo(); |
| 3852 | response->ssl_info.cert_status = -15; |
| 3853 | response->response_time = base::Time::Now(); |
| 3854 | response->was_cached = true; // (Wouldn't ever actually be true...) |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 3855 | |
| 3856 | { // Setup state for response_.vary_data |
| 3857 | HttpRequestInfo request; |
| 3858 | std::string temp("HTTP/1.1 200 OK\nVary: foo, bar\n\n"); |
| 3859 | std::replace(temp.begin(), temp.end(), '\n', '\0'); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 3860 | scoped_refptr<HttpResponseHeaders> headers = new HttpResponseHeaders(temp); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 3861 | request.extra_headers.SetHeader("Foo", "1"); |
| 3862 | request.extra_headers.SetHeader("bar", "23"); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 3863 | EXPECT_TRUE(response->vary_data.Init(request, *headers)); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 3864 | } |
| 3865 | |
| 3866 | // Cause the above state to be reset. |
| 3867 | trans->ResetStateForRestart(); |
| 3868 | |
| 3869 | // Verify that the state that needed to be reset, has been reset. |
[email protected] | 9b6fee1 | 2009-09-29 18:13:07 | [diff] [blame] | 3870 | EXPECT_TRUE(trans->read_buf_.get() == NULL); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 3871 | EXPECT_EQ(0, trans->read_buf_len_); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 3872 | EXPECT_EQ(0U, trans->request_headers_.size()); |
| 3873 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3874 | EXPECT_TRUE(response->headers.get() == NULL); |
| 3875 | EXPECT_EQ(false, response->was_cached); |
| 3876 | EXPECT_EQ(0, response->ssl_info.cert_status); |
| 3877 | EXPECT_FALSE(response->vary_data.is_valid()); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 3878 | } |
| 3879 | |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 3880 | // Test HTTPS connections to a site with a bad certificate |
| 3881 | TEST_F(HttpNetworkTransactionTest, HTTPSBadCertificate) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 3882 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 3883 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 3884 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 3885 | |
| 3886 | HttpRequestInfo request; |
| 3887 | request.method = "GET"; |
| 3888 | request.url = GURL("https://www.google.com/"); |
| 3889 | request.load_flags = 0; |
| 3890 | |
| 3891 | MockWrite data_writes[] = { |
| 3892 | MockWrite("GET / HTTP/1.1\r\n" |
| 3893 | "Host: www.google.com\r\n" |
| 3894 | "Connection: keep-alive\r\n\r\n"), |
| 3895 | }; |
| 3896 | |
| 3897 | MockRead data_reads[] = { |
| 3898 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3899 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 3900 | MockRead("Content-Length: 100\r\n\r\n"), |
| 3901 | MockRead(false, OK), |
| 3902 | }; |
| 3903 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3904 | StaticSocketDataProvider ssl_bad_certificate; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3905 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 3906 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3907 | SSLSocketDataProvider ssl_bad(true, ERR_CERT_AUTHORITY_INVALID); |
| 3908 | SSLSocketDataProvider ssl(true, OK); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 3909 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3910 | session_deps.socket_factory.AddSocketDataProvider(&ssl_bad_certificate); |
| 3911 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 3912 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_bad); |
| 3913 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 3914 | |
| 3915 | TestCompletionCallback callback; |
| 3916 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3917 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 3918 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3919 | |
| 3920 | rv = callback.WaitForResult(); |
| 3921 | EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, rv); |
| 3922 | |
| 3923 | rv = trans->RestartIgnoringLastError(&callback); |
| 3924 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3925 | |
| 3926 | rv = callback.WaitForResult(); |
| 3927 | EXPECT_EQ(OK, rv); |
| 3928 | |
| 3929 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 3930 | |
| 3931 | EXPECT_FALSE(response == NULL); |
| 3932 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 3933 | } |
| 3934 | |
| 3935 | // Test HTTPS connections to a site with a bad certificate, going through a |
| 3936 | // proxy |
| 3937 | TEST_F(HttpNetworkTransactionTest, HTTPSBadCertificateViaProxy) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 3938 | SessionDependencies session_deps(CreateFixedProxyService("myproxy:70")); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 3939 | |
| 3940 | HttpRequestInfo request; |
| 3941 | request.method = "GET"; |
| 3942 | request.url = GURL("https://www.google.com/"); |
| 3943 | request.load_flags = 0; |
| 3944 | |
| 3945 | MockWrite proxy_writes[] = { |
| 3946 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 3947 | "Host: www.google.com\r\n" |
| 3948 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 3949 | }; |
| 3950 | |
| 3951 | MockRead proxy_reads[] = { |
| 3952 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3953 | MockRead(false, OK) |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 3954 | }; |
| 3955 | |
| 3956 | MockWrite data_writes[] = { |
| 3957 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 3958 | "Host: www.google.com\r\n" |
| 3959 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 3960 | MockWrite("GET / HTTP/1.1\r\n" |
| 3961 | "Host: www.google.com\r\n" |
| 3962 | "Connection: keep-alive\r\n\r\n"), |
| 3963 | }; |
| 3964 | |
| 3965 | MockRead data_reads[] = { |
| 3966 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
| 3967 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3968 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 3969 | MockRead("Content-Length: 100\r\n\r\n"), |
| 3970 | MockRead(false, OK), |
| 3971 | }; |
| 3972 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3973 | StaticSocketDataProvider ssl_bad_certificate( |
| 3974 | proxy_reads, arraysize(proxy_reads), |
| 3975 | proxy_writes, arraysize(proxy_writes)); |
| 3976 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 3977 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3978 | SSLSocketDataProvider ssl_bad(true, ERR_CERT_AUTHORITY_INVALID); |
| 3979 | SSLSocketDataProvider ssl(true, OK); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 3980 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3981 | session_deps.socket_factory.AddSocketDataProvider(&ssl_bad_certificate); |
| 3982 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 3983 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_bad); |
| 3984 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 3985 | |
| 3986 | TestCompletionCallback callback; |
| 3987 | |
| 3988 | for (int i = 0; i < 2; i++) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 3989 | session_deps.socket_factory.ResetNextMockIndexes(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 3990 | |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 3991 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 3992 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 3993 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 3994 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 3995 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3996 | |
| 3997 | rv = callback.WaitForResult(); |
| 3998 | EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, rv); |
| 3999 | |
| 4000 | rv = trans->RestartIgnoringLastError(&callback); |
| 4001 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4002 | |
| 4003 | rv = callback.WaitForResult(); |
| 4004 | EXPECT_EQ(OK, rv); |
| 4005 | |
| 4006 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4007 | |
| 4008 | EXPECT_FALSE(response == NULL); |
| 4009 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 4010 | } |
| 4011 | } |
| 4012 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 4013 | |
| 4014 | // Test HTTPS connections to a site, going through an HTTPS proxy |
| 4015 | TEST_F(HttpNetworkTransactionTest, HTTPSViaHttpsProxy) { |
| 4016 | SessionDependencies session_deps(CreateFixedProxyService("https://proxy:70")); |
| 4017 | |
| 4018 | HttpRequestInfo request; |
| 4019 | request.method = "GET"; |
| 4020 | request.url = GURL("https://www.google.com/"); |
| 4021 | request.load_flags = 0; |
| 4022 | |
| 4023 | MockWrite data_writes[] = { |
| 4024 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 4025 | "Host: www.google.com\r\n" |
| 4026 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 4027 | MockWrite("GET / HTTP/1.1\r\n" |
| 4028 | "Host: www.google.com\r\n" |
| 4029 | "Connection: keep-alive\r\n\r\n"), |
| 4030 | }; |
| 4031 | |
| 4032 | MockRead data_reads[] = { |
| 4033 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
| 4034 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 4035 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4036 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4037 | MockRead(false, OK), |
| 4038 | }; |
| 4039 | |
| 4040 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4041 | data_writes, arraysize(data_writes)); |
| 4042 | SSLSocketDataProvider proxy_ssl(true, OK); // SSL to the proxy |
| 4043 | SSLSocketDataProvider tunnel_ssl(true, OK); // SSL through the tunnel |
| 4044 | |
| 4045 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 4046 | session_deps.socket_factory.AddSSLSocketDataProvider(&proxy_ssl); |
| 4047 | session_deps.socket_factory.AddSSLSocketDataProvider(&tunnel_ssl); |
| 4048 | |
| 4049 | TestCompletionCallback callback; |
| 4050 | |
| 4051 | scoped_ptr<HttpTransaction> trans( |
| 4052 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 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 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4060 | |
| 4061 | ASSERT_FALSE(response == NULL); |
| 4062 | |
| 4063 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 4064 | EXPECT_EQ(200, response->headers->response_code()); |
| 4065 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 4066 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 4067 | } |
| 4068 | |
| 4069 | // Test HTTPS connections to a site with a bad certificate, going through an |
| 4070 | // HTTPS proxy |
| 4071 | TEST_F(HttpNetworkTransactionTest, HTTPSBadCertificateViaHttpsProxy) { |
| 4072 | SessionDependencies session_deps(CreateFixedProxyService("https://proxy:70")); |
| 4073 | |
| 4074 | HttpRequestInfo request; |
| 4075 | request.method = "GET"; |
| 4076 | request.url = GURL("https://www.google.com/"); |
| 4077 | request.load_flags = 0; |
| 4078 | |
| 4079 | // Attempt to fetch the URL from a server with a bad cert |
| 4080 | MockWrite bad_cert_writes[] = { |
| 4081 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 4082 | "Host: www.google.com\r\n" |
| 4083 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 4084 | }; |
| 4085 | |
| 4086 | MockRead bad_cert_reads[] = { |
| 4087 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
| 4088 | MockRead(false, OK) |
| 4089 | }; |
| 4090 | |
| 4091 | // Attempt to fetch the URL with a good cert |
| 4092 | MockWrite good_data_writes[] = { |
| 4093 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 4094 | "Host: www.google.com\r\n" |
| 4095 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 4096 | MockWrite("GET / HTTP/1.1\r\n" |
| 4097 | "Host: www.google.com\r\n" |
| 4098 | "Connection: keep-alive\r\n\r\n"), |
| 4099 | }; |
| 4100 | |
| 4101 | MockRead good_cert_reads[] = { |
| 4102 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
| 4103 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4104 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4105 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4106 | MockRead(false, OK), |
| 4107 | }; |
| 4108 | |
| 4109 | StaticSocketDataProvider ssl_bad_certificate( |
| 4110 | bad_cert_reads, arraysize(bad_cert_reads), |
| 4111 | bad_cert_writes, arraysize(bad_cert_writes)); |
| 4112 | StaticSocketDataProvider data(good_cert_reads, arraysize(good_cert_reads), |
| 4113 | good_data_writes, arraysize(good_data_writes)); |
| 4114 | SSLSocketDataProvider ssl_bad(true, ERR_CERT_AUTHORITY_INVALID); |
| 4115 | SSLSocketDataProvider ssl(true, OK); |
| 4116 | |
| 4117 | // SSL to the proxy, then CONNECT request, then SSL with bad certificate |
| 4118 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 4119 | session_deps.socket_factory.AddSocketDataProvider(&ssl_bad_certificate); |
| 4120 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_bad); |
| 4121 | |
| 4122 | // SSL to the proxy, then CONNECT request, then valid SSL certificate |
| 4123 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 4124 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 4125 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 4126 | |
| 4127 | TestCompletionCallback callback; |
| 4128 | |
| 4129 | scoped_ptr<HttpTransaction> trans( |
| 4130 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4131 | |
| 4132 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 4133 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4134 | |
| 4135 | rv = callback.WaitForResult(); |
| 4136 | EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, rv); |
| 4137 | |
| 4138 | rv = trans->RestartIgnoringLastError(&callback); |
| 4139 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4140 | |
| 4141 | rv = callback.WaitForResult(); |
| 4142 | EXPECT_EQ(OK, rv); |
| 4143 | |
| 4144 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4145 | |
| 4146 | EXPECT_FALSE(response == NULL); |
| 4147 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 4148 | } |
| 4149 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4150 | TEST_F(HttpNetworkTransactionTest, BuildRequest_UserAgent) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 4151 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 4152 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 4153 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4154 | |
| 4155 | HttpRequestInfo request; |
| 4156 | request.method = "GET"; |
| 4157 | request.url = GURL("http://www.google.com/"); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4158 | request.extra_headers.SetHeader(HttpRequestHeaders::kUserAgent, |
| 4159 | "Chromium Ultra Awesome X Edition"); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4160 | |
| 4161 | MockWrite data_writes[] = { |
| 4162 | MockWrite("GET / HTTP/1.1\r\n" |
| 4163 | "Host: www.google.com\r\n" |
| 4164 | "Connection: keep-alive\r\n" |
| 4165 | "User-Agent: Chromium Ultra Awesome X Edition\r\n\r\n"), |
| 4166 | }; |
| 4167 | |
| 4168 | // Lastly, the server responds with the actual content. |
| 4169 | MockRead data_reads[] = { |
| 4170 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4171 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4172 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4173 | MockRead(false, OK), |
| 4174 | }; |
| 4175 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4176 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4177 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4178 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4179 | |
| 4180 | TestCompletionCallback callback; |
| 4181 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4182 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4183 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4184 | |
| 4185 | rv = callback.WaitForResult(); |
| 4186 | EXPECT_EQ(OK, rv); |
| 4187 | } |
| 4188 | |
[email protected] | da81f13 | 2010-08-18 23:39:29 | [diff] [blame] | 4189 | TEST_F(HttpNetworkTransactionTest, BuildRequest_UserAgentOverTunnel) { |
| 4190 | SessionDependencies session_deps(CreateFixedProxyService("myproxy:70")); |
| 4191 | scoped_ptr<HttpTransaction> trans( |
| 4192 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4193 | |
| 4194 | HttpRequestInfo request; |
| 4195 | request.method = "GET"; |
| 4196 | request.url = GURL("https://www.google.com/"); |
| 4197 | request.extra_headers.SetHeader(HttpRequestHeaders::kUserAgent, |
| 4198 | "Chromium Ultra Awesome X Edition"); |
| 4199 | |
| 4200 | MockWrite data_writes[] = { |
| 4201 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 4202 | "Host: www.google.com\r\n" |
| 4203 | "Proxy-Connection: keep-alive\r\n" |
| 4204 | "User-Agent: Chromium Ultra Awesome X Edition\r\n\r\n"), |
| 4205 | }; |
| 4206 | MockRead data_reads[] = { |
| 4207 | // Return an error, so the transaction stops here (this test isn't |
| 4208 | // interested in the rest). |
| 4209 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 4210 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 4211 | MockRead("Proxy-Connection: close\r\n\r\n"), |
| 4212 | }; |
| 4213 | |
| 4214 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4215 | data_writes, arraysize(data_writes)); |
| 4216 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 4217 | |
| 4218 | TestCompletionCallback callback; |
| 4219 | |
| 4220 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 4221 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4222 | |
| 4223 | rv = callback.WaitForResult(); |
| 4224 | EXPECT_EQ(OK, rv); |
| 4225 | } |
| 4226 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4227 | TEST_F(HttpNetworkTransactionTest, BuildRequest_Referer) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 4228 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 4229 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 4230 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4231 | |
| 4232 | HttpRequestInfo request; |
| 4233 | request.method = "GET"; |
| 4234 | request.url = GURL("http://www.google.com/"); |
| 4235 | request.load_flags = 0; |
| 4236 | request.referrer = GURL("http://the.previous.site.com/"); |
| 4237 | |
| 4238 | MockWrite data_writes[] = { |
| 4239 | MockWrite("GET / HTTP/1.1\r\n" |
| 4240 | "Host: www.google.com\r\n" |
| 4241 | "Connection: keep-alive\r\n" |
| 4242 | "Referer: http://the.previous.site.com/\r\n\r\n"), |
| 4243 | }; |
| 4244 | |
| 4245 | // Lastly, the server responds with the actual content. |
| 4246 | MockRead data_reads[] = { |
| 4247 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4248 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4249 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4250 | MockRead(false, OK), |
| 4251 | }; |
| 4252 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4253 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4254 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4255 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4256 | |
| 4257 | TestCompletionCallback callback; |
| 4258 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4259 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4260 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4261 | |
| 4262 | rv = callback.WaitForResult(); |
| 4263 | EXPECT_EQ(OK, rv); |
| 4264 | } |
| 4265 | |
| 4266 | TEST_F(HttpNetworkTransactionTest, BuildRequest_PostContentLengthZero) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 4267 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 4268 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 4269 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4270 | |
| 4271 | HttpRequestInfo request; |
| 4272 | request.method = "POST"; |
| 4273 | request.url = GURL("http://www.google.com/"); |
| 4274 | |
| 4275 | MockWrite data_writes[] = { |
| 4276 | MockWrite("POST / HTTP/1.1\r\n" |
| 4277 | "Host: www.google.com\r\n" |
| 4278 | "Connection: keep-alive\r\n" |
| 4279 | "Content-Length: 0\r\n\r\n"), |
| 4280 | }; |
| 4281 | |
| 4282 | // Lastly, the server responds with the actual content. |
| 4283 | MockRead data_reads[] = { |
| 4284 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4285 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4286 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4287 | MockRead(false, OK), |
| 4288 | }; |
| 4289 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4290 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4291 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4292 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4293 | |
| 4294 | TestCompletionCallback callback; |
| 4295 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4296 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4297 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4298 | |
| 4299 | rv = callback.WaitForResult(); |
| 4300 | EXPECT_EQ(OK, rv); |
| 4301 | } |
| 4302 | |
| 4303 | TEST_F(HttpNetworkTransactionTest, BuildRequest_PutContentLengthZero) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 4304 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 4305 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 4306 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4307 | |
| 4308 | HttpRequestInfo request; |
| 4309 | request.method = "PUT"; |
| 4310 | request.url = GURL("http://www.google.com/"); |
| 4311 | |
| 4312 | MockWrite data_writes[] = { |
| 4313 | MockWrite("PUT / HTTP/1.1\r\n" |
| 4314 | "Host: www.google.com\r\n" |
| 4315 | "Connection: keep-alive\r\n" |
| 4316 | "Content-Length: 0\r\n\r\n"), |
| 4317 | }; |
| 4318 | |
| 4319 | // Lastly, the server responds with the actual content. |
| 4320 | MockRead data_reads[] = { |
| 4321 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4322 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4323 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4324 | MockRead(false, OK), |
| 4325 | }; |
| 4326 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4327 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4328 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4329 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4330 | |
| 4331 | TestCompletionCallback callback; |
| 4332 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4333 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4334 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4335 | |
| 4336 | rv = callback.WaitForResult(); |
| 4337 | EXPECT_EQ(OK, rv); |
| 4338 | } |
| 4339 | |
| 4340 | TEST_F(HttpNetworkTransactionTest, BuildRequest_HeadContentLengthZero) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 4341 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 4342 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 4343 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4344 | |
| 4345 | HttpRequestInfo request; |
| 4346 | request.method = "HEAD"; |
| 4347 | request.url = GURL("http://www.google.com/"); |
| 4348 | |
| 4349 | MockWrite data_writes[] = { |
| 4350 | MockWrite("HEAD / HTTP/1.1\r\n" |
| 4351 | "Host: www.google.com\r\n" |
| 4352 | "Connection: keep-alive\r\n" |
| 4353 | "Content-Length: 0\r\n\r\n"), |
| 4354 | }; |
| 4355 | |
| 4356 | // Lastly, the server responds with the actual content. |
| 4357 | MockRead data_reads[] = { |
| 4358 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4359 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4360 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4361 | MockRead(false, OK), |
| 4362 | }; |
| 4363 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4364 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4365 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4366 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4367 | |
| 4368 | TestCompletionCallback callback; |
| 4369 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4370 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4371 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4372 | |
| 4373 | rv = callback.WaitForResult(); |
| 4374 | EXPECT_EQ(OK, rv); |
| 4375 | } |
| 4376 | |
| 4377 | TEST_F(HttpNetworkTransactionTest, BuildRequest_CacheControlNoCache) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 4378 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 4379 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 4380 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4381 | |
| 4382 | HttpRequestInfo request; |
| 4383 | request.method = "GET"; |
| 4384 | request.url = GURL("http://www.google.com/"); |
| 4385 | request.load_flags = LOAD_BYPASS_CACHE; |
| 4386 | |
| 4387 | MockWrite data_writes[] = { |
| 4388 | MockWrite("GET / HTTP/1.1\r\n" |
| 4389 | "Host: www.google.com\r\n" |
| 4390 | "Connection: keep-alive\r\n" |
| 4391 | "Pragma: no-cache\r\n" |
| 4392 | "Cache-Control: no-cache\r\n\r\n"), |
| 4393 | }; |
| 4394 | |
| 4395 | // Lastly, the server responds with the actual content. |
| 4396 | MockRead data_reads[] = { |
| 4397 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4398 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4399 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4400 | MockRead(false, OK), |
| 4401 | }; |
| 4402 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4403 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4404 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4405 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4406 | |
| 4407 | TestCompletionCallback callback; |
| 4408 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4409 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4410 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4411 | |
| 4412 | rv = callback.WaitForResult(); |
| 4413 | EXPECT_EQ(OK, rv); |
| 4414 | } |
| 4415 | |
| 4416 | TEST_F(HttpNetworkTransactionTest, |
| 4417 | BuildRequest_CacheControlValidateCache) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 4418 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 4419 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 4420 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4421 | |
| 4422 | HttpRequestInfo request; |
| 4423 | request.method = "GET"; |
| 4424 | request.url = GURL("http://www.google.com/"); |
| 4425 | request.load_flags = LOAD_VALIDATE_CACHE; |
| 4426 | |
| 4427 | MockWrite data_writes[] = { |
| 4428 | MockWrite("GET / HTTP/1.1\r\n" |
| 4429 | "Host: www.google.com\r\n" |
| 4430 | "Connection: keep-alive\r\n" |
| 4431 | "Cache-Control: max-age=0\r\n\r\n"), |
| 4432 | }; |
| 4433 | |
| 4434 | // Lastly, the server responds with the actual content. |
| 4435 | MockRead data_reads[] = { |
| 4436 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4437 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4438 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4439 | MockRead(false, OK), |
| 4440 | }; |
| 4441 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4442 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4443 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4444 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4445 | |
| 4446 | TestCompletionCallback callback; |
| 4447 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4448 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4449 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4450 | |
| 4451 | rv = callback.WaitForResult(); |
| 4452 | EXPECT_EQ(OK, rv); |
| 4453 | } |
| 4454 | |
| 4455 | TEST_F(HttpNetworkTransactionTest, BuildRequest_ExtraHeaders) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 4456 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 4457 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 4458 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4459 | |
| 4460 | HttpRequestInfo request; |
| 4461 | request.method = "GET"; |
| 4462 | request.url = GURL("http://www.google.com/"); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4463 | request.extra_headers.SetHeader("FooHeader", "Bar"); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4464 | |
| 4465 | MockWrite data_writes[] = { |
| 4466 | MockWrite("GET / HTTP/1.1\r\n" |
| 4467 | "Host: www.google.com\r\n" |
| 4468 | "Connection: keep-alive\r\n" |
| 4469 | "FooHeader: Bar\r\n\r\n"), |
| 4470 | }; |
| 4471 | |
| 4472 | // Lastly, the server responds with the actual content. |
| 4473 | MockRead data_reads[] = { |
| 4474 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4475 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4476 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4477 | MockRead(false, OK), |
| 4478 | }; |
| 4479 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4480 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4481 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4482 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4483 | |
| 4484 | TestCompletionCallback callback; |
| 4485 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4486 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4487 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4488 | |
| 4489 | rv = callback.WaitForResult(); |
| 4490 | EXPECT_EQ(OK, rv); |
| 4491 | } |
| 4492 | |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 4493 | TEST_F(HttpNetworkTransactionTest, BuildRequest_ExtraHeadersStripped) { |
| 4494 | SessionDependencies session_deps; |
| 4495 | scoped_ptr<HttpTransaction> trans( |
| 4496 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4497 | |
| 4498 | HttpRequestInfo request; |
| 4499 | request.method = "GET"; |
| 4500 | request.url = GURL("http://www.google.com/"); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4501 | request.extra_headers.SetHeader("referer", "www.foo.com"); |
| 4502 | request.extra_headers.SetHeader("hEllo", "Kitty"); |
| 4503 | request.extra_headers.SetHeader("FoO", "bar"); |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 4504 | |
| 4505 | MockWrite data_writes[] = { |
| 4506 | MockWrite("GET / HTTP/1.1\r\n" |
| 4507 | "Host: www.google.com\r\n" |
| 4508 | "Connection: keep-alive\r\n" |
| 4509 | "hEllo: Kitty\r\n" |
| 4510 | "FoO: bar\r\n\r\n"), |
| 4511 | }; |
| 4512 | |
| 4513 | // Lastly, the server responds with the actual content. |
| 4514 | MockRead data_reads[] = { |
| 4515 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4516 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4517 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4518 | MockRead(false, OK), |
| 4519 | }; |
| 4520 | |
| 4521 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4522 | data_writes, arraysize(data_writes)); |
| 4523 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 4524 | |
| 4525 | TestCompletionCallback callback; |
| 4526 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4527 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 4528 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4529 | |
| 4530 | rv = callback.WaitForResult(); |
| 4531 | EXPECT_EQ(OK, rv); |
| 4532 | } |
| 4533 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 4534 | TEST_F(HttpNetworkTransactionTest, SOCKS4_HTTP_GET) { |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 4535 | SessionDependencies session_deps( |
| 4536 | CreateFixedProxyService("socks4://myproxy:1080")); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 4537 | |
| 4538 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 4539 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 4540 | |
| 4541 | HttpRequestInfo request; |
| 4542 | request.method = "GET"; |
| 4543 | request.url = GURL("http://www.google.com/"); |
| 4544 | request.load_flags = 0; |
| 4545 | |
| 4546 | char write_buffer[] = { 0x04, 0x01, 0x00, 0x50, 127, 0, 0, 1, 0 }; |
| 4547 | char read_buffer[] = { 0x00, 0x5A, 0x00, 0x00, 0, 0, 0, 0 }; |
| 4548 | |
| 4549 | MockWrite data_writes[] = { |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 4550 | MockWrite(true, write_buffer, arraysize(write_buffer)), |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 4551 | MockWrite("GET / HTTP/1.1\r\n" |
| 4552 | "Host: www.google.com\r\n" |
| 4553 | "Connection: keep-alive\r\n\r\n") |
| 4554 | }; |
| 4555 | |
| 4556 | MockRead data_reads[] = { |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4557 | MockRead(true, read_buffer, arraysize(read_buffer)), |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 4558 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4559 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), |
| 4560 | MockRead("Payload"), |
| 4561 | MockRead(false, OK) |
| 4562 | }; |
| 4563 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4564 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4565 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4566 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 4567 | |
| 4568 | TestCompletionCallback callback; |
| 4569 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4570 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 4571 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4572 | |
| 4573 | rv = callback.WaitForResult(); |
| 4574 | EXPECT_EQ(OK, rv); |
| 4575 | |
| 4576 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4577 | EXPECT_FALSE(response == NULL); |
| 4578 | |
| 4579 | std::string response_text; |
| 4580 | rv = ReadTransaction(trans.get(), &response_text); |
| 4581 | EXPECT_EQ(OK, rv); |
| 4582 | EXPECT_EQ("Payload", response_text); |
| 4583 | } |
| 4584 | |
| 4585 | TEST_F(HttpNetworkTransactionTest, SOCKS4_SSL_GET) { |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 4586 | SessionDependencies session_deps( |
| 4587 | CreateFixedProxyService("socks4://myproxy:1080")); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 4588 | |
| 4589 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 4590 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 4591 | |
| 4592 | HttpRequestInfo request; |
| 4593 | request.method = "GET"; |
| 4594 | request.url = GURL("https://www.google.com/"); |
| 4595 | request.load_flags = 0; |
| 4596 | |
| 4597 | unsigned char write_buffer[] = { 0x04, 0x01, 0x01, 0xBB, 127, 0, 0, 1, 0 }; |
| 4598 | unsigned char read_buffer[] = { 0x00, 0x5A, 0x00, 0x00, 0, 0, 0, 0 }; |
| 4599 | |
| 4600 | MockWrite data_writes[] = { |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 4601 | MockWrite(true, reinterpret_cast<char*>(write_buffer), |
| 4602 | arraysize(write_buffer)), |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 4603 | MockWrite("GET / HTTP/1.1\r\n" |
| 4604 | "Host: www.google.com\r\n" |
| 4605 | "Connection: keep-alive\r\n\r\n") |
| 4606 | }; |
| 4607 | |
| 4608 | MockRead data_reads[] = { |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 4609 | MockWrite(true, reinterpret_cast<char*>(read_buffer), |
| 4610 | arraysize(read_buffer)), |
| 4611 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4612 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), |
| 4613 | MockRead("Payload"), |
| 4614 | MockRead(false, OK) |
| 4615 | }; |
| 4616 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4617 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4618 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4619 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 4620 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4621 | SSLSocketDataProvider ssl(true, OK); |
| 4622 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 4623 | |
| 4624 | TestCompletionCallback callback; |
| 4625 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4626 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 4627 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4628 | |
| 4629 | rv = callback.WaitForResult(); |
| 4630 | EXPECT_EQ(OK, rv); |
| 4631 | |
| 4632 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4633 | EXPECT_FALSE(response == NULL); |
| 4634 | |
| 4635 | std::string response_text; |
| 4636 | rv = ReadTransaction(trans.get(), &response_text); |
| 4637 | EXPECT_EQ(OK, rv); |
| 4638 | EXPECT_EQ("Payload", response_text); |
| 4639 | } |
| 4640 | |
| 4641 | TEST_F(HttpNetworkTransactionTest, SOCKS5_HTTP_GET) { |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 4642 | SessionDependencies session_deps( |
| 4643 | CreateFixedProxyService("socks5://myproxy:1080")); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 4644 | |
| 4645 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 4646 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 4647 | |
| 4648 | HttpRequestInfo request; |
| 4649 | request.method = "GET"; |
| 4650 | request.url = GURL("http://www.google.com/"); |
| 4651 | request.load_flags = 0; |
| 4652 | |
| 4653 | const char kSOCKS5GreetRequest[] = { 0x05, 0x01, 0x00 }; |
| 4654 | const char kSOCKS5GreetResponse[] = { 0x05, 0x00 }; |
[email protected] | f209dba | 2009-12-18 00:24:37 | [diff] [blame] | 4655 | const char kSOCKS5OkRequest[] = { |
| 4656 | 0x05, // Version |
| 4657 | 0x01, // Command (CONNECT) |
| 4658 | 0x00, // Reserved. |
| 4659 | 0x03, // Address type (DOMAINNAME). |
| 4660 | 0x0E, // Length of domain (14) |
| 4661 | // Domain string: |
| 4662 | 'w', 'w', 'w', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'c', 'o', 'm', |
| 4663 | 0x00, 0x50, // 16-bit port (80) |
| 4664 | }; |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 4665 | const char kSOCKS5OkResponse[] = |
| 4666 | { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; |
| 4667 | |
| 4668 | MockWrite data_writes[] = { |
| 4669 | MockWrite(true, kSOCKS5GreetRequest, arraysize(kSOCKS5GreetRequest)), |
| 4670 | MockWrite(true, kSOCKS5OkRequest, arraysize(kSOCKS5OkRequest)), |
| 4671 | MockWrite("GET / HTTP/1.1\r\n" |
| 4672 | "Host: www.google.com\r\n" |
| 4673 | "Connection: keep-alive\r\n\r\n") |
| 4674 | }; |
| 4675 | |
| 4676 | MockRead data_reads[] = { |
| 4677 | MockWrite(true, kSOCKS5GreetResponse, arraysize(kSOCKS5GreetResponse)), |
| 4678 | MockWrite(true, kSOCKS5OkResponse, arraysize(kSOCKS5OkResponse)), |
| 4679 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4680 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), |
| 4681 | MockRead("Payload"), |
| 4682 | MockRead(false, OK) |
| 4683 | }; |
| 4684 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4685 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4686 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4687 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 4688 | |
| 4689 | TestCompletionCallback callback; |
| 4690 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4691 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 4692 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4693 | |
| 4694 | rv = callback.WaitForResult(); |
| 4695 | EXPECT_EQ(OK, rv); |
| 4696 | |
| 4697 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4698 | EXPECT_FALSE(response == NULL); |
| 4699 | |
| 4700 | std::string response_text; |
| 4701 | rv = ReadTransaction(trans.get(), &response_text); |
| 4702 | EXPECT_EQ(OK, rv); |
| 4703 | EXPECT_EQ("Payload", response_text); |
| 4704 | } |
| 4705 | |
| 4706 | TEST_F(HttpNetworkTransactionTest, SOCKS5_SSL_GET) { |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 4707 | SessionDependencies session_deps( |
| 4708 | CreateFixedProxyService("socks5://myproxy:1080")); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 4709 | |
| 4710 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 4711 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 4712 | |
| 4713 | HttpRequestInfo request; |
| 4714 | request.method = "GET"; |
| 4715 | request.url = GURL("https://www.google.com/"); |
| 4716 | request.load_flags = 0; |
| 4717 | |
| 4718 | const char kSOCKS5GreetRequest[] = { 0x05, 0x01, 0x00 }; |
| 4719 | const char kSOCKS5GreetResponse[] = { 0x05, 0x00 }; |
[email protected] | f209dba | 2009-12-18 00:24:37 | [diff] [blame] | 4720 | const unsigned char kSOCKS5OkRequest[] = { |
| 4721 | 0x05, // Version |
| 4722 | 0x01, // Command (CONNECT) |
| 4723 | 0x00, // Reserved. |
| 4724 | 0x03, // Address type (DOMAINNAME). |
| 4725 | 0x0E, // Length of domain (14) |
| 4726 | // Domain string: |
| 4727 | 'w', 'w', 'w', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'c', 'o', 'm', |
| 4728 | 0x01, 0xBB, // 16-bit port (443) |
| 4729 | }; |
| 4730 | |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 4731 | const char kSOCKS5OkResponse[] = |
| 4732 | { 0x05, 0x00, 0x00, 0x01, 0, 0, 0, 0, 0x00, 0x00 }; |
| 4733 | |
| 4734 | MockWrite data_writes[] = { |
| 4735 | MockWrite(true, kSOCKS5GreetRequest, arraysize(kSOCKS5GreetRequest)), |
| 4736 | MockWrite(true, reinterpret_cast<const char*>(kSOCKS5OkRequest), |
| 4737 | arraysize(kSOCKS5OkRequest)), |
| 4738 | MockWrite("GET / HTTP/1.1\r\n" |
| 4739 | "Host: www.google.com\r\n" |
| 4740 | "Connection: keep-alive\r\n\r\n") |
| 4741 | }; |
| 4742 | |
| 4743 | MockRead data_reads[] = { |
| 4744 | MockWrite(true, kSOCKS5GreetResponse, arraysize(kSOCKS5GreetResponse)), |
| 4745 | MockWrite(true, kSOCKS5OkResponse, arraysize(kSOCKS5OkResponse)), |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 4746 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4747 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), |
| 4748 | MockRead("Payload"), |
| 4749 | MockRead(false, OK) |
| 4750 | }; |
| 4751 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4752 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4753 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4754 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 4755 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4756 | SSLSocketDataProvider ssl(true, OK); |
| 4757 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 4758 | |
| 4759 | TestCompletionCallback callback; |
| 4760 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 4761 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 4762 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4763 | |
| 4764 | rv = callback.WaitForResult(); |
| 4765 | EXPECT_EQ(OK, rv); |
| 4766 | |
| 4767 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4768 | EXPECT_FALSE(response == NULL); |
| 4769 | |
| 4770 | std::string response_text; |
| 4771 | rv = ReadTransaction(trans.get(), &response_text); |
| 4772 | EXPECT_EQ(OK, rv); |
| 4773 | EXPECT_EQ("Payload", response_text); |
| 4774 | } |
| 4775 | |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 4776 | // Tests that for connection endpoints the group names are correctly set. |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4777 | |
| 4778 | struct GroupNameTest { |
| 4779 | std::string proxy_server; |
| 4780 | std::string url; |
| 4781 | std::string expected_group_name; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4782 | bool ssl; |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4783 | }; |
| 4784 | |
| 4785 | scoped_refptr<HttpNetworkSession> SetupSessionForGroupNameTests( |
| 4786 | const std::string& proxy_server) { |
| 4787 | SessionDependencies session_deps(CreateFixedProxyService(proxy_server)); |
| 4788 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 4789 | |
| 4790 | HttpAlternateProtocols* alternate_protocols = |
| 4791 | session->mutable_alternate_protocols(); |
| 4792 | alternate_protocols->SetAlternateProtocolFor( |
| 4793 | HostPortPair("host.with.alternate", 80), 443, |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 4794 | HttpAlternateProtocols::NPN_SPDY_2); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4795 | |
| 4796 | return session; |
| 4797 | } |
| 4798 | |
| 4799 | int GroupNameTransactionHelper( |
| 4800 | const std::string& url, |
| 4801 | const scoped_refptr<HttpNetworkSession>& session) { |
| 4802 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 4803 | |
| 4804 | HttpRequestInfo request; |
| 4805 | request.method = "GET"; |
| 4806 | request.url = GURL(url); |
| 4807 | request.load_flags = 0; |
| 4808 | |
| 4809 | TestCompletionCallback callback; |
| 4810 | |
| 4811 | // We do not complete this request, the dtor will clean the transaction up. |
| 4812 | return trans->Start(&request, &callback, BoundNetLog()); |
| 4813 | } |
| 4814 | |
| 4815 | TEST_F(HttpNetworkTransactionTest, GroupNameForDirectConnections) { |
| 4816 | const GroupNameTest tests[] = { |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 4817 | { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4818 | "", // unused |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 4819 | "http://www.google.com/direct", |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 4820 | "www.google.com:80", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4821 | false, |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 4822 | }, |
| 4823 | { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4824 | "", // unused |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 4825 | "http://[2001:1418:13:1::25]/direct", |
| 4826 | "[2001:1418:13:1::25]:80", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4827 | false, |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 4828 | }, |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 4829 | |
| 4830 | // SSL Tests |
| 4831 | { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4832 | "", // unused |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 4833 | "https://www.google.com/direct_ssl", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 4834 | "ssl/www.google.com:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4835 | true, |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 4836 | }, |
| 4837 | { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4838 | "", // unused |
| 4839 | "https://[2001:1418:13:1::25]/direct", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 4840 | "ssl/[2001:1418:13:1::25]:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4841 | true, |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 4842 | }, |
| 4843 | { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4844 | "", // unused |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 4845 | "http://host.with.alternate/direct", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 4846 | "ssl/host.with.alternate:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4847 | true, |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 4848 | }, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4849 | }; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 4850 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 4851 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4852 | |
| 4853 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 4854 | scoped_refptr<HttpNetworkSession> session( |
| 4855 | SetupSessionForGroupNameTests(tests[i].proxy_server)); |
| 4856 | |
| 4857 | HttpNetworkSessionPeer peer(session); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame^] | 4858 | CaptureGroupNameTCPSocketPool* tcp_conn_pool = |
| 4859 | new CaptureGroupNameTCPSocketPool(session); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4860 | peer.SetTCPSocketPool(tcp_conn_pool); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame^] | 4861 | CaptureGroupNameSSLSocketPool* ssl_conn_pool = |
| 4862 | new CaptureGroupNameSSLSocketPool(session.get()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4863 | peer.SetSSLSocketPool(ssl_conn_pool); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4864 | |
| 4865 | EXPECT_EQ(ERR_IO_PENDING, |
| 4866 | GroupNameTransactionHelper(tests[i].url, session)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4867 | if (tests[i].ssl) |
| 4868 | EXPECT_EQ(tests[i].expected_group_name, |
| 4869 | ssl_conn_pool->last_group_name_received()); |
| 4870 | else |
| 4871 | EXPECT_EQ(tests[i].expected_group_name, |
| 4872 | tcp_conn_pool->last_group_name_received()); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4873 | } |
| 4874 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 4875 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4876 | } |
| 4877 | |
| 4878 | TEST_F(HttpNetworkTransactionTest, GroupNameForHTTPProxyConnections) { |
| 4879 | const GroupNameTest tests[] = { |
| 4880 | { |
| 4881 | "http_proxy", |
| 4882 | "http://www.google.com/http_proxy_normal", |
| 4883 | "www.google.com:80", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4884 | false, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4885 | }, |
| 4886 | |
| 4887 | // SSL Tests |
| 4888 | { |
| 4889 | "http_proxy", |
| 4890 | "https://www.google.com/http_connect_ssl", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 4891 | "ssl/www.google.com:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4892 | true, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4893 | }, |
| 4894 | |
[email protected] | 9faeded9 | 2010-04-29 20:03:05 | [diff] [blame] | 4895 | { |
| 4896 | "http_proxy", |
| 4897 | "http://host.with.alternate/direct", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 4898 | "ssl/host.with.alternate:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4899 | true, |
[email protected] | 9faeded9 | 2010-04-29 20:03:05 | [diff] [blame] | 4900 | }, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4901 | }; |
| 4902 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 4903 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4904 | |
| 4905 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 4906 | scoped_refptr<HttpNetworkSession> session( |
| 4907 | SetupSessionForGroupNameTests(tests[i].proxy_server)); |
| 4908 | |
| 4909 | HttpNetworkSessionPeer peer(session); |
| 4910 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4911 | HostPortPair proxy_host("http_proxy", 80); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame^] | 4912 | CaptureGroupNameHttpProxySocketPool* http_proxy_pool = |
| 4913 | new CaptureGroupNameHttpProxySocketPool(session); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4914 | peer.SetSocketPoolForHTTPProxy(proxy_host, http_proxy_pool); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame^] | 4915 | CaptureGroupNameSSLSocketPool* ssl_conn_pool = |
| 4916 | new CaptureGroupNameSSLSocketPool(session); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4917 | peer.SetSocketPoolForSSLWithProxy(proxy_host, ssl_conn_pool); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4918 | |
| 4919 | EXPECT_EQ(ERR_IO_PENDING, |
| 4920 | GroupNameTransactionHelper(tests[i].url, session)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4921 | if (tests[i].ssl) |
| 4922 | EXPECT_EQ(tests[i].expected_group_name, |
| 4923 | ssl_conn_pool->last_group_name_received()); |
| 4924 | else |
| 4925 | EXPECT_EQ(tests[i].expected_group_name, |
| 4926 | http_proxy_pool->last_group_name_received()); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4927 | } |
| 4928 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 4929 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4930 | } |
| 4931 | |
| 4932 | TEST_F(HttpNetworkTransactionTest, GroupNameForSOCKSConnections) { |
| 4933 | const GroupNameTest tests[] = { |
| 4934 | { |
| 4935 | "socks4://socks_proxy:1080", |
| 4936 | "http://www.google.com/socks4_direct", |
| 4937 | "socks4/www.google.com:80", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4938 | false, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4939 | }, |
| 4940 | { |
| 4941 | "socks5://socks_proxy:1080", |
| 4942 | "http://www.google.com/socks5_direct", |
| 4943 | "socks5/www.google.com:80", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4944 | false, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4945 | }, |
| 4946 | |
| 4947 | // SSL Tests |
| 4948 | { |
| 4949 | "socks4://socks_proxy:1080", |
| 4950 | "https://www.google.com/socks4_ssl", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 4951 | "socks4/ssl/www.google.com:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4952 | true, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4953 | }, |
| 4954 | { |
| 4955 | "socks5://socks_proxy:1080", |
| 4956 | "https://www.google.com/socks5_ssl", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 4957 | "socks5/ssl/www.google.com:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4958 | true, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4959 | }, |
| 4960 | |
[email protected] | 9faeded9 | 2010-04-29 20:03:05 | [diff] [blame] | 4961 | { |
| 4962 | "socks4://socks_proxy:1080", |
| 4963 | "http://host.with.alternate/direct", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 4964 | "socks4/ssl/host.with.alternate:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4965 | true, |
[email protected] | 9faeded9 | 2010-04-29 20:03:05 | [diff] [blame] | 4966 | }, |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 4967 | }; |
| 4968 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 4969 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 4970 | |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 4971 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4972 | scoped_refptr<HttpNetworkSession> session( |
| 4973 | SetupSessionForGroupNameTests(tests[i].proxy_server)); |
| 4974 | HttpNetworkSessionPeer peer(session); |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 4975 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4976 | HostPortPair proxy_host("socks_proxy", 1080); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame^] | 4977 | CaptureGroupNameSOCKSSocketPool* socks_conn_pool = |
| 4978 | new CaptureGroupNameSOCKSSocketPool(session); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4979 | peer.SetSocketPoolForSOCKSProxy(proxy_host, socks_conn_pool); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame^] | 4980 | CaptureGroupNameSSLSocketPool* ssl_conn_pool = |
| 4981 | new CaptureGroupNameSSLSocketPool(session); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4982 | peer.SetSocketPoolForSSLWithProxy(proxy_host, ssl_conn_pool); |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 4983 | |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 4984 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 4985 | |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 4986 | EXPECT_EQ(ERR_IO_PENDING, |
| 4987 | GroupNameTransactionHelper(tests[i].url, session)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 4988 | if (tests[i].ssl) |
| 4989 | EXPECT_EQ(tests[i].expected_group_name, |
| 4990 | ssl_conn_pool->last_group_name_received()); |
| 4991 | else |
| 4992 | EXPECT_EQ(tests[i].expected_group_name, |
| 4993 | socks_conn_pool->last_group_name_received()); |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 4994 | } |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 4995 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 4996 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 4997 | } |
| 4998 | |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 4999 | TEST_F(HttpNetworkTransactionTest, ReconsiderProxyAfterFailedConnection) { |
[email protected] | 5c6a17e | 2009-06-10 00:54:54 | [diff] [blame] | 5000 | SessionDependencies session_deps( |
| 5001 | CreateFixedProxyService("myproxy:70;foobar:80")); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 5002 | |
[email protected] | 6971906 | 2010-01-05 20:09:21 | [diff] [blame] | 5003 | // This simulates failure resolving all hostnames; that means we will fail |
| 5004 | // connecting to both proxies (myproxy:70 and foobar:80). |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 5005 | session_deps.host_resolver->rules()->AddSimulatedFailure("*"); |
| 5006 | |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 5007 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 5008 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 5009 | |
| 5010 | HttpRequestInfo request; |
| 5011 | request.method = "GET"; |
| 5012 | request.url = GURL("http://www.google.com/"); |
| 5013 | |
| 5014 | TestCompletionCallback callback; |
| 5015 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5016 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 5017 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5018 | |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 5019 | rv = callback.WaitForResult(); |
[email protected] | f7fccee | 2010-09-16 20:53:01 | [diff] [blame] | 5020 | EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, rv); |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 5021 | } |
| 5022 | |
[email protected] | f3e6c1e | 2009-06-15 20:52:12 | [diff] [blame] | 5023 | // Host resolution observer used by |
| 5024 | // HttpNetworkTransactionTest.ResolveMadeWithReferrer to check that host |
| 5025 | // resovle requests are issued with a referrer of |expected_referrer|. |
| 5026 | class ResolutionReferrerObserver : public HostResolver::Observer { |
| 5027 | public: |
| 5028 | explicit ResolutionReferrerObserver(const GURL& expected_referrer) |
| 5029 | : expected_referrer_(expected_referrer), |
| 5030 | called_start_with_referrer_(false), |
| 5031 | called_finish_with_referrer_(false) { |
| 5032 | } |
| 5033 | |
| 5034 | virtual void OnStartResolution(int id, |
| 5035 | const HostResolver::RequestInfo& info) { |
| 5036 | if (info.referrer() == expected_referrer_) |
| 5037 | called_start_with_referrer_ = true; |
| 5038 | } |
| 5039 | |
| 5040 | virtual void OnFinishResolutionWithStatus( |
| 5041 | int id, bool was_resolved, const HostResolver::RequestInfo& info ) { |
| 5042 | if (info.referrer() == expected_referrer_) |
| 5043 | called_finish_with_referrer_ = true; |
| 5044 | } |
| 5045 | |
[email protected] | eb255d3 | 2009-06-17 02:11:03 | [diff] [blame] | 5046 | virtual void OnCancelResolution(int id, |
| 5047 | const HostResolver::RequestInfo& info ) { |
| 5048 | FAIL() << "Should not be cancelling any requests!"; |
| 5049 | } |
| 5050 | |
[email protected] | f3e6c1e | 2009-06-15 20:52:12 | [diff] [blame] | 5051 | bool did_complete_with_expected_referrer() const { |
| 5052 | return called_start_with_referrer_ && called_finish_with_referrer_; |
| 5053 | } |
| 5054 | |
| 5055 | private: |
| 5056 | GURL expected_referrer_; |
| 5057 | bool called_start_with_referrer_; |
| 5058 | bool called_finish_with_referrer_; |
| 5059 | |
| 5060 | DISALLOW_COPY_AND_ASSIGN(ResolutionReferrerObserver); |
| 5061 | }; |
| 5062 | |
| 5063 | // Make sure that when HostResolver::Resolve() is invoked, it passes through |
| 5064 | // the "referrer". This is depended on by the DNS prefetch observer. |
| 5065 | TEST_F(HttpNetworkTransactionTest, ResolveMadeWithReferrer) { |
| 5066 | GURL referrer = GURL("http://expected-referrer/"); |
| 5067 | EXPECT_TRUE(referrer.is_valid()); |
| 5068 | ResolutionReferrerObserver resolution_observer(referrer); |
| 5069 | |
| 5070 | SessionDependencies session_deps; |
| 5071 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 5072 | CreateSession(&session_deps))); |
[email protected] | f3e6c1e | 2009-06-15 20:52:12 | [diff] [blame] | 5073 | |
| 5074 | // Attach an observer to watch the host resolutions being made. |
[email protected] | 94a0d3d9 | 2009-06-27 01:50:14 | [diff] [blame] | 5075 | session_deps.host_resolver->AddObserver(&resolution_observer); |
[email protected] | f3e6c1e | 2009-06-15 20:52:12 | [diff] [blame] | 5076 | |
| 5077 | // Connect up a mock socket which will fail when reading. |
| 5078 | MockRead data_reads[] = { |
| 5079 | MockRead(false, ERR_FAILED), |
| 5080 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5081 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5082 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | f3e6c1e | 2009-06-15 20:52:12 | [diff] [blame] | 5083 | |
| 5084 | // Issue a request, containing an HTTP referrer. |
| 5085 | HttpRequestInfo request; |
| 5086 | request.method = "GET"; |
| 5087 | request.referrer = referrer; |
| 5088 | request.url = GURL("http://www.google.com/"); |
| 5089 | |
| 5090 | // Run the request until it fails reading from the socket. |
| 5091 | TestCompletionCallback callback; |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5092 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | f3e6c1e | 2009-06-15 20:52:12 | [diff] [blame] | 5093 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5094 | rv = callback.WaitForResult(); |
| 5095 | EXPECT_EQ(ERR_FAILED, rv); |
| 5096 | |
| 5097 | // Check that the host resolution observer saw |referrer|. |
| 5098 | EXPECT_TRUE(resolution_observer.did_complete_with_expected_referrer()); |
| 5099 | } |
| 5100 | |
[email protected] | 685af59 | 2010-05-11 19:31:24 | [diff] [blame] | 5101 | // Base test to make sure that when the load flags for a request specify to |
| 5102 | // bypass the cache, the DNS cache is not used. |
| 5103 | void BypassHostCacheOnRefreshHelper(int load_flags) { |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5104 | SessionDependencies session_deps; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 5105 | |
[email protected] | a2c2fb9 | 2009-07-18 07:31:04 | [diff] [blame] | 5106 | // Select a host resolver that does caching. |
| 5107 | session_deps.host_resolver = new MockCachingHostResolver; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 5108 | |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5109 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 5110 | CreateSession(&session_deps))); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5111 | |
| 5112 | // Warm up the host cache so it has an entry for "www.google.com" (by doing |
| 5113 | // a synchronous lookup.) |
| 5114 | AddressList addrlist; |
[email protected] | 94a0d3d9 | 2009-06-27 01:50:14 | [diff] [blame] | 5115 | int rv = session_deps.host_resolver->Resolve( |
[email protected] | 930cc74 | 2010-09-15 22:54:10 | [diff] [blame] | 5116 | HostResolver::RequestInfo(HostPortPair("www.google.com", 80)), &addrlist, |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5117 | NULL, NULL, BoundNetLog()); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5118 | EXPECT_EQ(OK, rv); |
| 5119 | |
| 5120 | // Verify that it was added to host cache, by doing a subsequent async lookup |
| 5121 | // and confirming it completes synchronously. |
| 5122 | TestCompletionCallback resolve_callback; |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 5123 | rv = session_deps.host_resolver->Resolve( |
[email protected] | 930cc74 | 2010-09-15 22:54:10 | [diff] [blame] | 5124 | HostResolver::RequestInfo(HostPortPair("www.google.com", 80)), &addrlist, |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5125 | &resolve_callback, NULL, BoundNetLog()); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 5126 | ASSERT_EQ(OK, rv); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5127 | |
| 5128 | // Inject a failure the next time that "www.google.com" is resolved. This way |
| 5129 | // we can tell if the next lookup hit the cache, or the "network". |
| 5130 | // (cache --> success, "network" --> failure). |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 5131 | session_deps.host_resolver->rules()->AddSimulatedFailure("www.google.com"); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5132 | |
| 5133 | // Connect up a mock socket which will fail with ERR_UNEXPECTED during the |
| 5134 | // first read -- this won't be reached as the host resolution will fail first. |
| 5135 | MockRead data_reads[] = { MockRead(false, ERR_UNEXPECTED) }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5136 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5137 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5138 | |
| 5139 | // Issue a request, asking to bypass the cache(s). |
| 5140 | HttpRequestInfo request; |
| 5141 | request.method = "GET"; |
[email protected] | 685af59 | 2010-05-11 19:31:24 | [diff] [blame] | 5142 | request.load_flags = load_flags; |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5143 | request.url = GURL("http://www.google.com/"); |
| 5144 | |
| 5145 | // Run the request. |
| 5146 | TestCompletionCallback callback; |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5147 | rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5148 | ASSERT_EQ(ERR_IO_PENDING, rv); |
| 5149 | rv = callback.WaitForResult(); |
| 5150 | |
| 5151 | // If we bypassed the cache, we would have gotten a failure while resolving |
| 5152 | // "www.google.com". |
| 5153 | EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); |
| 5154 | } |
| 5155 | |
[email protected] | 685af59 | 2010-05-11 19:31:24 | [diff] [blame] | 5156 | // There are multiple load flags that should trigger the host cache bypass. |
| 5157 | // Test each in isolation: |
| 5158 | TEST_F(HttpNetworkTransactionTest, BypassHostCacheOnRefresh1) { |
| 5159 | BypassHostCacheOnRefreshHelper(LOAD_BYPASS_CACHE); |
| 5160 | } |
| 5161 | |
| 5162 | TEST_F(HttpNetworkTransactionTest, BypassHostCacheOnRefresh2) { |
| 5163 | BypassHostCacheOnRefreshHelper(LOAD_VALIDATE_CACHE); |
| 5164 | } |
| 5165 | |
| 5166 | TEST_F(HttpNetworkTransactionTest, BypassHostCacheOnRefresh3) { |
| 5167 | BypassHostCacheOnRefreshHelper(LOAD_DISABLE_CACHE); |
| 5168 | } |
| 5169 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5170 | // Make sure we can handle an error when writing the request. |
| 5171 | TEST_F(HttpNetworkTransactionTest, RequestWriteError) { |
| 5172 | SessionDependencies session_deps; |
| 5173 | scoped_refptr<HttpNetworkSession> session = CreateSession(&session_deps); |
| 5174 | |
| 5175 | HttpRequestInfo request; |
| 5176 | request.method = "GET"; |
| 5177 | request.url = GURL("http://www.foo.com/"); |
| 5178 | request.load_flags = 0; |
| 5179 | |
| 5180 | MockWrite write_failure[] = { |
| 5181 | MockWrite(true, ERR_CONNECTION_RESET), |
| 5182 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5183 | StaticSocketDataProvider data(NULL, 0, |
| 5184 | write_failure, arraysize(write_failure)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5185 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5186 | |
| 5187 | TestCompletionCallback callback; |
| 5188 | |
| 5189 | scoped_ptr<HttpTransaction> trans( |
| 5190 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5191 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5192 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5193 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5194 | |
| 5195 | rv = callback.WaitForResult(); |
| 5196 | EXPECT_EQ(ERR_CONNECTION_RESET, rv); |
| 5197 | } |
| 5198 | |
| 5199 | // Check that a connection closed after the start of the headers finishes ok. |
| 5200 | TEST_F(HttpNetworkTransactionTest, ConnectionClosedAfterStartOfHeaders) { |
| 5201 | SessionDependencies session_deps; |
| 5202 | scoped_refptr<HttpNetworkSession> session = CreateSession(&session_deps); |
| 5203 | |
| 5204 | HttpRequestInfo request; |
| 5205 | request.method = "GET"; |
| 5206 | request.url = GURL("http://www.foo.com/"); |
| 5207 | request.load_flags = 0; |
| 5208 | |
| 5209 | MockRead data_reads[] = { |
| 5210 | MockRead("HTTP/1."), |
| 5211 | MockRead(false, OK), |
| 5212 | }; |
| 5213 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5214 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5215 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5216 | |
| 5217 | TestCompletionCallback callback; |
| 5218 | |
| 5219 | scoped_ptr<HttpTransaction> trans( |
| 5220 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5221 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5222 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5223 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5224 | |
| 5225 | rv = callback.WaitForResult(); |
| 5226 | EXPECT_EQ(OK, rv); |
| 5227 | |
| 5228 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5229 | EXPECT_TRUE(response != NULL); |
| 5230 | |
| 5231 | EXPECT_TRUE(response->headers != NULL); |
| 5232 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
| 5233 | |
| 5234 | std::string response_data; |
| 5235 | rv = ReadTransaction(trans.get(), &response_data); |
| 5236 | EXPECT_EQ(OK, rv); |
| 5237 | EXPECT_EQ("", response_data); |
| 5238 | } |
| 5239 | |
| 5240 | // Make sure that a dropped connection while draining the body for auth |
| 5241 | // restart does the right thing. |
| 5242 | TEST_F(HttpNetworkTransactionTest, DrainResetOK) { |
| 5243 | SessionDependencies session_deps; |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 5244 | scoped_refptr<HttpNetworkSession> session = CreateSession(&session_deps); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5245 | |
| 5246 | HttpRequestInfo request; |
| 5247 | request.method = "GET"; |
| 5248 | request.url = GURL("http://www.google.com/"); |
| 5249 | request.load_flags = 0; |
| 5250 | |
| 5251 | MockWrite data_writes1[] = { |
| 5252 | MockWrite("GET / HTTP/1.1\r\n" |
| 5253 | "Host: www.google.com\r\n" |
| 5254 | "Connection: keep-alive\r\n\r\n"), |
| 5255 | }; |
| 5256 | |
| 5257 | MockRead data_reads1[] = { |
| 5258 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 5259 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 5260 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 5261 | MockRead("Content-Length: 14\r\n\r\n"), |
| 5262 | MockRead("Unauth"), |
| 5263 | MockRead(true, ERR_CONNECTION_RESET), |
| 5264 | }; |
| 5265 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5266 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 5267 | data_writes1, arraysize(data_writes1)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5268 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5269 | |
| 5270 | // After calling trans->RestartWithAuth(), this is the request we should |
| 5271 | // be issuing -- the final header line contains the credentials. |
| 5272 | MockWrite data_writes2[] = { |
| 5273 | MockWrite("GET / HTTP/1.1\r\n" |
| 5274 | "Host: www.google.com\r\n" |
| 5275 | "Connection: keep-alive\r\n" |
| 5276 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 5277 | }; |
| 5278 | |
| 5279 | // Lastly, the server responds with the actual content. |
| 5280 | MockRead data_reads2[] = { |
| 5281 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 5282 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 5283 | MockRead("Content-Length: 100\r\n\r\n"), |
| 5284 | MockRead(false, OK), |
| 5285 | }; |
| 5286 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5287 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 5288 | data_writes2, arraysize(data_writes2)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5289 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5290 | |
| 5291 | TestCompletionCallback callback1; |
| 5292 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 5293 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 5294 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5295 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5296 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5297 | |
| 5298 | rv = callback1.WaitForResult(); |
| 5299 | EXPECT_EQ(OK, rv); |
| 5300 | |
| 5301 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5302 | EXPECT_FALSE(response == NULL); |
| 5303 | |
| 5304 | // The password prompt info should have been set in response->auth_challenge. |
| 5305 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 5306 | |
| 5307 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
| 5308 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 5309 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 5310 | |
| 5311 | TestCompletionCallback callback2; |
| 5312 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 5313 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5314 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5315 | |
| 5316 | rv = callback2.WaitForResult(); |
| 5317 | EXPECT_EQ(OK, rv); |
| 5318 | |
| 5319 | response = trans->GetResponseInfo(); |
| 5320 | EXPECT_FALSE(response == NULL); |
| 5321 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 5322 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 5323 | } |
| 5324 | |
| 5325 | // Test HTTPS connections going through a proxy that sends extra data. |
| 5326 | TEST_F(HttpNetworkTransactionTest, HTTPSViaProxyWithExtraData) { |
| 5327 | SessionDependencies session_deps(CreateFixedProxyService("myproxy:70")); |
| 5328 | |
| 5329 | HttpRequestInfo request; |
| 5330 | request.method = "GET"; |
| 5331 | request.url = GURL("https://www.google.com/"); |
| 5332 | request.load_flags = 0; |
| 5333 | |
| 5334 | MockRead proxy_reads[] = { |
| 5335 | MockRead("HTTP/1.0 200 Connected\r\n\r\nExtra data"), |
| 5336 | MockRead(false, OK) |
| 5337 | }; |
| 5338 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5339 | StaticSocketDataProvider data(proxy_reads, arraysize(proxy_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5340 | SSLSocketDataProvider ssl(true, OK); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5341 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5342 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 5343 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5344 | |
| 5345 | TestCompletionCallback callback; |
| 5346 | |
| 5347 | session_deps.socket_factory.ResetNextMockIndexes(); |
| 5348 | |
| 5349 | scoped_ptr<HttpTransaction> trans( |
| 5350 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5351 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5352 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5353 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5354 | |
| 5355 | rv = callback.WaitForResult(); |
| 5356 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, rv); |
| 5357 | } |
| 5358 | |
[email protected] | e22e136 | 2009-11-23 21:31:12 | [diff] [blame] | 5359 | TEST_F(HttpNetworkTransactionTest, LargeContentLengthThenClose) { |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 5360 | SessionDependencies session_deps; |
| 5361 | scoped_ptr<HttpTransaction> trans( |
| 5362 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5363 | |
| 5364 | HttpRequestInfo request; |
| 5365 | request.method = "GET"; |
| 5366 | request.url = GURL("http://www.google.com/"); |
| 5367 | request.load_flags = 0; |
| 5368 | |
[email protected] | e22e136 | 2009-11-23 21:31:12 | [diff] [blame] | 5369 | MockRead data_reads[] = { |
| 5370 | MockRead("HTTP/1.0 200 OK\r\nContent-Length:6719476739\r\n\r\n"), |
| 5371 | MockRead(false, OK), |
| 5372 | }; |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 5373 | |
| 5374 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
| 5375 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 5376 | |
| 5377 | TestCompletionCallback callback; |
| 5378 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5379 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 5380 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5381 | |
| 5382 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 5383 | |
| 5384 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5385 | EXPECT_TRUE(response != NULL); |
| 5386 | |
| 5387 | EXPECT_TRUE(response->headers != NULL); |
| 5388 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
| 5389 | |
| 5390 | std::string response_data; |
| 5391 | rv = ReadTransaction(trans.get(), &response_data); |
| 5392 | EXPECT_EQ(ERR_CONNECTION_CLOSED, rv); |
[email protected] | e22e136 | 2009-11-23 21:31:12 | [diff] [blame] | 5393 | } |
| 5394 | |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 5395 | TEST_F(HttpNetworkTransactionTest, UploadFileSmallerThanLength) { |
| 5396 | SessionDependencies session_deps; |
| 5397 | scoped_ptr<HttpTransaction> trans( |
| 5398 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5399 | |
| 5400 | HttpRequestInfo request; |
| 5401 | request.method = "POST"; |
| 5402 | request.url = GURL("http://www.google.com/upload"); |
| 5403 | request.upload_data = new UploadData; |
| 5404 | request.load_flags = 0; |
| 5405 | |
| 5406 | FilePath temp_file_path; |
| 5407 | ASSERT_TRUE(file_util::CreateTemporaryFile(&temp_file_path)); |
| 5408 | const uint64 kFakeSize = 100000; // file is actually blank |
| 5409 | |
| 5410 | std::vector<UploadData::Element> elements; |
| 5411 | UploadData::Element element; |
| 5412 | element.SetToFilePath(temp_file_path); |
| 5413 | element.SetContentLength(kFakeSize); |
| 5414 | elements.push_back(element); |
[email protected] | 9349cfb | 2010-08-31 18:00:53 | [diff] [blame] | 5415 | request.upload_data->SetElements(elements); |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 5416 | EXPECT_EQ(kFakeSize, request.upload_data->GetContentLength()); |
| 5417 | |
| 5418 | MockRead data_reads[] = { |
| 5419 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 5420 | MockRead("hello world"), |
| 5421 | MockRead(false, OK), |
| 5422 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5423 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 5424 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 5425 | |
| 5426 | TestCompletionCallback callback; |
| 5427 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5428 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 5429 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5430 | |
| 5431 | rv = callback.WaitForResult(); |
| 5432 | EXPECT_EQ(OK, rv); |
| 5433 | |
| 5434 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5435 | EXPECT_TRUE(response != NULL); |
| 5436 | |
| 5437 | EXPECT_TRUE(response->headers != NULL); |
| 5438 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
| 5439 | |
| 5440 | std::string response_data; |
| 5441 | rv = ReadTransaction(trans.get(), &response_data); |
| 5442 | EXPECT_EQ(OK, rv); |
| 5443 | EXPECT_EQ("hello world", response_data); |
| 5444 | |
| 5445 | file_util::Delete(temp_file_path, false); |
| 5446 | } |
| 5447 | |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 5448 | TEST_F(HttpNetworkTransactionTest, UploadUnreadableFile) { |
| 5449 | // If we try to upload an unreadable file, the network stack should report |
| 5450 | // the file size as zero and upload zero bytes for that file. |
| 5451 | SessionDependencies session_deps; |
| 5452 | scoped_ptr<HttpTransaction> trans( |
| 5453 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5454 | |
| 5455 | FilePath temp_file; |
| 5456 | ASSERT_TRUE(file_util::CreateTemporaryFile(&temp_file)); |
| 5457 | std::string temp_file_content("Unreadable file."); |
| 5458 | ASSERT_TRUE(file_util::WriteFile(temp_file, temp_file_content.c_str(), |
| 5459 | temp_file_content.length())); |
| 5460 | ASSERT_TRUE(file_util::MakeFileUnreadable(temp_file)); |
| 5461 | |
| 5462 | HttpRequestInfo request; |
| 5463 | request.method = "POST"; |
| 5464 | request.url = GURL("http://www.google.com/upload"); |
| 5465 | request.upload_data = new UploadData; |
| 5466 | request.load_flags = 0; |
| 5467 | |
| 5468 | std::vector<UploadData::Element> elements; |
| 5469 | UploadData::Element element; |
| 5470 | element.SetToFilePath(temp_file); |
| 5471 | elements.push_back(element); |
[email protected] | 9349cfb | 2010-08-31 18:00:53 | [diff] [blame] | 5472 | request.upload_data->SetElements(elements); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 5473 | |
| 5474 | MockRead data_reads[] = { |
| 5475 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 5476 | MockRead(false, OK), |
| 5477 | }; |
| 5478 | MockWrite data_writes[] = { |
| 5479 | MockWrite("POST /upload HTTP/1.1\r\n" |
| 5480 | "Host: www.google.com\r\n" |
| 5481 | "Connection: keep-alive\r\n" |
| 5482 | "Content-Length: 0\r\n\r\n"), |
| 5483 | MockWrite(false, OK), |
| 5484 | }; |
| 5485 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), data_writes, |
| 5486 | arraysize(data_writes)); |
| 5487 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 5488 | |
| 5489 | TestCompletionCallback callback; |
| 5490 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5491 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 5492 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5493 | |
| 5494 | rv = callback.WaitForResult(); |
| 5495 | EXPECT_EQ(OK, rv); |
| 5496 | |
| 5497 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5498 | EXPECT_TRUE(response != NULL); |
| 5499 | EXPECT_TRUE(response->headers != NULL); |
| 5500 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
| 5501 | |
| 5502 | file_util::Delete(temp_file, false); |
| 5503 | } |
| 5504 | |
| 5505 | TEST_F(HttpNetworkTransactionTest, UnreadableUploadFileAfterAuthRestart) { |
| 5506 | SessionDependencies session_deps; |
| 5507 | scoped_ptr<HttpTransaction> trans( |
| 5508 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5509 | |
| 5510 | FilePath temp_file; |
| 5511 | ASSERT_TRUE(file_util::CreateTemporaryFile(&temp_file)); |
| 5512 | std::string temp_file_contents("Unreadable file."); |
| 5513 | std::string unreadable_contents(temp_file_contents.length(), '\0'); |
| 5514 | ASSERT_TRUE(file_util::WriteFile(temp_file, temp_file_contents.c_str(), |
| 5515 | temp_file_contents.length())); |
| 5516 | |
| 5517 | HttpRequestInfo request; |
| 5518 | request.method = "POST"; |
| 5519 | request.url = GURL("http://www.google.com/upload"); |
| 5520 | request.upload_data = new UploadData; |
| 5521 | request.load_flags = 0; |
| 5522 | |
| 5523 | std::vector<UploadData::Element> elements; |
| 5524 | UploadData::Element element; |
| 5525 | element.SetToFilePath(temp_file); |
| 5526 | elements.push_back(element); |
[email protected] | 9349cfb | 2010-08-31 18:00:53 | [diff] [blame] | 5527 | request.upload_data->SetElements(elements); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 5528 | |
| 5529 | MockRead data_reads[] = { |
| 5530 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 5531 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 5532 | MockRead("Content-Length: 0\r\n\r\n"), // No response body. |
| 5533 | |
| 5534 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 5535 | MockRead("Content-Length: 0\r\n\r\n"), |
| 5536 | MockRead(false, OK), |
| 5537 | }; |
| 5538 | MockWrite data_writes[] = { |
| 5539 | MockWrite("POST /upload HTTP/1.1\r\n" |
| 5540 | "Host: www.google.com\r\n" |
| 5541 | "Connection: keep-alive\r\n" |
| 5542 | "Content-Length: 16\r\n\r\n"), |
| 5543 | MockWrite(false, temp_file_contents.c_str()), |
| 5544 | |
| 5545 | MockWrite("POST /upload HTTP/1.1\r\n" |
| 5546 | "Host: www.google.com\r\n" |
| 5547 | "Connection: keep-alive\r\n" |
| 5548 | "Content-Length: 16\r\n" |
| 5549 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 5550 | MockWrite(false, unreadable_contents.c_str(), temp_file_contents.length()), |
| 5551 | MockWrite(false, OK), |
| 5552 | }; |
| 5553 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), data_writes, |
| 5554 | arraysize(data_writes)); |
| 5555 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 5556 | |
| 5557 | TestCompletionCallback callback1; |
| 5558 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5559 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 5560 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5561 | |
| 5562 | rv = callback1.WaitForResult(); |
| 5563 | EXPECT_EQ(OK, rv); |
| 5564 | |
| 5565 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5566 | EXPECT_TRUE(response != NULL); |
| 5567 | EXPECT_TRUE(response->headers != NULL); |
| 5568 | EXPECT_EQ("HTTP/1.1 401 Unauthorized", response->headers->GetStatusLine()); |
| 5569 | |
| 5570 | // The password prompt info should have been set in response->auth_challenge. |
| 5571 | EXPECT_TRUE(response->auth_challenge.get() != NULL); |
| 5572 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
| 5573 | EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 5574 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 5575 | |
| 5576 | // Now make the file unreadable and try again. |
| 5577 | ASSERT_TRUE(file_util::MakeFileUnreadable(temp_file)); |
| 5578 | |
| 5579 | TestCompletionCallback callback2; |
| 5580 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 5581 | rv = trans->RestartWithAuth(kFoo, kBar, &callback2); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 5582 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5583 | |
| 5584 | rv = callback2.WaitForResult(); |
| 5585 | EXPECT_EQ(OK, rv); |
| 5586 | |
| 5587 | response = trans->GetResponseInfo(); |
| 5588 | EXPECT_TRUE(response != NULL); |
| 5589 | EXPECT_TRUE(response->headers != NULL); |
| 5590 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 5591 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 5592 | |
| 5593 | file_util::Delete(temp_file, false); |
| 5594 | } |
| 5595 | |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 5596 | // Tests that changes to Auth realms are treated like auth rejections. |
| 5597 | TEST_F(HttpNetworkTransactionTest, ChangeAuthRealms) { |
| 5598 | SessionDependencies session_deps; |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 5599 | |
| 5600 | HttpRequestInfo request; |
| 5601 | request.method = "GET"; |
| 5602 | request.url = GURL("http://www.google.com/"); |
| 5603 | request.load_flags = 0; |
| 5604 | |
| 5605 | // First transaction will request a resource and receive a Basic challenge |
| 5606 | // with realm="first_realm". |
| 5607 | MockWrite data_writes1[] = { |
| 5608 | MockWrite("GET / HTTP/1.1\r\n" |
| 5609 | "Host: www.google.com\r\n" |
| 5610 | "Connection: keep-alive\r\n" |
| 5611 | "\r\n"), |
| 5612 | }; |
| 5613 | MockRead data_reads1[] = { |
| 5614 | MockRead("HTTP/1.1 401 Unauthorized\r\n" |
| 5615 | "WWW-Authenticate: Basic realm=\"first_realm\"\r\n" |
| 5616 | "\r\n"), |
| 5617 | }; |
| 5618 | |
| 5619 | // After calling trans->RestartWithAuth(), provide an Authentication header |
| 5620 | // for first_realm. The server will reject and provide a challenge with |
| 5621 | // second_realm. |
| 5622 | MockWrite data_writes2[] = { |
| 5623 | MockWrite("GET / HTTP/1.1\r\n" |
| 5624 | "Host: www.google.com\r\n" |
| 5625 | "Connection: keep-alive\r\n" |
| 5626 | "Authorization: Basic Zmlyc3Q6YmF6\r\n" |
| 5627 | "\r\n"), |
| 5628 | }; |
| 5629 | MockRead data_reads2[] = { |
| 5630 | MockRead("HTTP/1.1 401 Unauthorized\r\n" |
| 5631 | "WWW-Authenticate: Basic realm=\"second_realm\"\r\n" |
| 5632 | "\r\n"), |
| 5633 | }; |
| 5634 | |
| 5635 | // This again fails, and goes back to first_realm. Make sure that the |
| 5636 | // entry is removed from cache. |
| 5637 | MockWrite data_writes3[] = { |
| 5638 | MockWrite("GET / HTTP/1.1\r\n" |
| 5639 | "Host: www.google.com\r\n" |
| 5640 | "Connection: keep-alive\r\n" |
| 5641 | "Authorization: Basic c2Vjb25kOmZvdQ==\r\n" |
| 5642 | "\r\n"), |
| 5643 | }; |
| 5644 | MockRead data_reads3[] = { |
| 5645 | MockRead("HTTP/1.1 401 Unauthorized\r\n" |
| 5646 | "WWW-Authenticate: Basic realm=\"first_realm\"\r\n" |
| 5647 | "\r\n"), |
| 5648 | }; |
| 5649 | |
| 5650 | // Try one last time (with the correct password) and get the resource. |
| 5651 | MockWrite data_writes4[] = { |
| 5652 | MockWrite("GET / HTTP/1.1\r\n" |
| 5653 | "Host: www.google.com\r\n" |
| 5654 | "Connection: keep-alive\r\n" |
| 5655 | "Authorization: Basic Zmlyc3Q6YmFy\r\n" |
| 5656 | "\r\n"), |
| 5657 | }; |
| 5658 | MockRead data_reads4[] = { |
| 5659 | MockRead("HTTP/1.1 200 OK\r\n" |
| 5660 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 5661 | "Content-Length: 5\r\n" |
| 5662 | "\r\n" |
| 5663 | "hello"), |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 5664 | }; |
| 5665 | |
| 5666 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 5667 | data_writes1, arraysize(data_writes1)); |
| 5668 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 5669 | data_writes2, arraysize(data_writes2)); |
| 5670 | StaticSocketDataProvider data3(data_reads3, arraysize(data_reads3), |
| 5671 | data_writes3, arraysize(data_writes3)); |
| 5672 | StaticSocketDataProvider data4(data_reads4, arraysize(data_reads4), |
| 5673 | data_writes4, arraysize(data_writes4)); |
| 5674 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 5675 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 5676 | session_deps.socket_factory.AddSocketDataProvider(&data3); |
| 5677 | session_deps.socket_factory.AddSocketDataProvider(&data4); |
| 5678 | |
| 5679 | TestCompletionCallback callback1; |
| 5680 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 5681 | scoped_ptr<HttpTransaction> trans( |
| 5682 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5683 | |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 5684 | // Issue the first request with Authorize headers. There should be a |
| 5685 | // password prompt for first_realm waiting to be filled in after the |
| 5686 | // transaction completes. |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5687 | int rv = trans->Start(&request, &callback1, BoundNetLog()); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 5688 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5689 | rv = callback1.WaitForResult(); |
| 5690 | EXPECT_EQ(OK, rv); |
| 5691 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5692 | ASSERT_FALSE(response == NULL); |
| 5693 | ASSERT_FALSE(response->auth_challenge.get() == NULL); |
| 5694 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
| 5695 | EXPECT_EQ(L"first_realm", response->auth_challenge->realm); |
| 5696 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 5697 | |
| 5698 | // Issue the second request with an incorrect password. There should be a |
| 5699 | // password prompt for second_realm waiting to be filled in after the |
| 5700 | // transaction completes. |
| 5701 | TestCompletionCallback callback2; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 5702 | rv = trans->RestartWithAuth(kFirst, kBaz, &callback2); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 5703 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5704 | rv = callback2.WaitForResult(); |
| 5705 | EXPECT_EQ(OK, rv); |
| 5706 | response = trans->GetResponseInfo(); |
| 5707 | ASSERT_FALSE(response == NULL); |
| 5708 | ASSERT_FALSE(response->auth_challenge.get() == NULL); |
| 5709 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
| 5710 | EXPECT_EQ(L"second_realm", response->auth_challenge->realm); |
| 5711 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 5712 | |
| 5713 | // Issue the third request with another incorrect password. There should be |
| 5714 | // a password prompt for first_realm waiting to be filled in. If the password |
| 5715 | // prompt is not present, it indicates that the HttpAuthCacheEntry for |
| 5716 | // first_realm was not correctly removed. |
| 5717 | TestCompletionCallback callback3; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 5718 | rv = trans->RestartWithAuth(kSecond, kFou, &callback3); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 5719 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5720 | rv = callback3.WaitForResult(); |
| 5721 | EXPECT_EQ(OK, rv); |
| 5722 | response = trans->GetResponseInfo(); |
| 5723 | ASSERT_FALSE(response == NULL); |
| 5724 | ASSERT_FALSE(response->auth_challenge.get() == NULL); |
| 5725 | EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
| 5726 | EXPECT_EQ(L"first_realm", response->auth_challenge->realm); |
| 5727 | EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 5728 | |
| 5729 | // Issue the fourth request with the correct password and username. |
| 5730 | TestCompletionCallback callback4; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 5731 | rv = trans->RestartWithAuth(kFirst, kBar, &callback4); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 5732 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5733 | rv = callback4.WaitForResult(); |
| 5734 | EXPECT_EQ(OK, rv); |
| 5735 | response = trans->GetResponseInfo(); |
| 5736 | ASSERT_FALSE(response == NULL); |
| 5737 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 5738 | } |
| 5739 | |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5740 | TEST_F(HttpNetworkTransactionTest, HonorAlternateProtocolHeader) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 5741 | HttpStreamFactory::set_next_protos("needs_to_be_set_for_this_test"); |
| 5742 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 5743 | |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5744 | SessionDependencies session_deps; |
| 5745 | |
| 5746 | MockRead data_reads[] = { |
| 5747 | MockRead("HTTP/1.1 200 OK\r\n"), |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 5748 | MockRead(kAlternateProtocolHttpHeader), |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5749 | MockRead("hello world"), |
| 5750 | MockRead(false, OK), |
| 5751 | }; |
| 5752 | |
| 5753 | HttpRequestInfo request; |
| 5754 | request.method = "GET"; |
| 5755 | request.url = GURL("http://www.google.com/"); |
| 5756 | request.load_flags = 0; |
| 5757 | |
| 5758 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
| 5759 | |
| 5760 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 5761 | |
| 5762 | TestCompletionCallback callback; |
| 5763 | |
| 5764 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 5765 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 5766 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5767 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5768 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 5769 | |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame] | 5770 | HostPortPair http_host_port_pair("www.google.com", 80); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5771 | const HttpAlternateProtocols& alternate_protocols = |
| 5772 | session->alternate_protocols(); |
| 5773 | EXPECT_FALSE( |
| 5774 | alternate_protocols.HasAlternateProtocolFor(http_host_port_pair)); |
| 5775 | |
| 5776 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 5777 | |
| 5778 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5779 | ASSERT_TRUE(response != NULL); |
| 5780 | ASSERT_TRUE(response->headers != NULL); |
| 5781 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 5782 | EXPECT_FALSE(response->was_fetched_via_spdy); |
| 5783 | EXPECT_FALSE(response->was_npn_negotiated); |
[email protected] | 193b0b89 | 2010-06-26 03:57:57 | [diff] [blame] | 5784 | EXPECT_FALSE(response->was_alternate_protocol_available); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5785 | |
| 5786 | std::string response_data; |
| 5787 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 5788 | EXPECT_EQ("hello world", response_data); |
| 5789 | |
| 5790 | ASSERT_TRUE(alternate_protocols.HasAlternateProtocolFor(http_host_port_pair)); |
| 5791 | const HttpAlternateProtocols::PortProtocolPair alternate = |
| 5792 | alternate_protocols.GetAlternateProtocolFor(http_host_port_pair); |
| 5793 | HttpAlternateProtocols::PortProtocolPair expected_alternate; |
| 5794 | expected_alternate.port = 443; |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 5795 | expected_alternate.protocol = HttpAlternateProtocols::NPN_SPDY_2; |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5796 | EXPECT_TRUE(expected_alternate.Equals(alternate)); |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 5797 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 5798 | HttpStreamFactory::set_use_alternate_protocols(false); |
| 5799 | HttpStreamFactory::set_next_protos(""); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5800 | } |
| 5801 | |
| 5802 | TEST_F(HttpNetworkTransactionTest, MarkBrokenAlternateProtocol) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 5803 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5804 | SessionDependencies session_deps; |
| 5805 | |
| 5806 | HttpRequestInfo request; |
| 5807 | request.method = "GET"; |
| 5808 | request.url = GURL("http://www.google.com/"); |
| 5809 | request.load_flags = 0; |
| 5810 | |
| 5811 | MockConnect mock_connect(true, ERR_CONNECTION_REFUSED); |
| 5812 | StaticSocketDataProvider first_data; |
| 5813 | first_data.set_connect_data(mock_connect); |
| 5814 | session_deps.socket_factory.AddSocketDataProvider(&first_data); |
| 5815 | |
| 5816 | MockRead data_reads[] = { |
| 5817 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 5818 | MockRead("hello world"), |
| 5819 | MockRead(true, OK), |
| 5820 | }; |
| 5821 | StaticSocketDataProvider second_data( |
| 5822 | data_reads, arraysize(data_reads), NULL, 0); |
| 5823 | session_deps.socket_factory.AddSocketDataProvider(&second_data); |
| 5824 | |
| 5825 | // TODO(willchan): Delete this extra data provider. It's necessary due to a |
| 5826 | // ClientSocketPoolBaseHelper bug that starts up too many ConnectJobs: |
| 5827 | // http://crbug.com/37454. |
| 5828 | session_deps.socket_factory.AddSocketDataProvider(&second_data); |
| 5829 | |
| 5830 | TestCompletionCallback callback; |
| 5831 | |
| 5832 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 5833 | |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame] | 5834 | HostPortPair http_host_port_pair("www.google.com", 80); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5835 | HttpAlternateProtocols* alternate_protocols = |
| 5836 | session->mutable_alternate_protocols(); |
| 5837 | alternate_protocols->SetAlternateProtocolFor( |
| 5838 | http_host_port_pair, 1234 /* port is ignored by MockConnect anyway */, |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 5839 | HttpAlternateProtocols::NPN_SPDY_2); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5840 | |
| 5841 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 5842 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5843 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5844 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5845 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 5846 | |
| 5847 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5848 | ASSERT_TRUE(response != NULL); |
| 5849 | ASSERT_TRUE(response->headers != NULL); |
| 5850 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 5851 | |
| 5852 | std::string response_data; |
| 5853 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 5854 | EXPECT_EQ("hello world", response_data); |
| 5855 | |
| 5856 | ASSERT_TRUE( |
| 5857 | alternate_protocols->HasAlternateProtocolFor(http_host_port_pair)); |
| 5858 | const HttpAlternateProtocols::PortProtocolPair alternate = |
| 5859 | alternate_protocols->GetAlternateProtocolFor(http_host_port_pair); |
| 5860 | EXPECT_EQ(HttpAlternateProtocols::BROKEN, alternate.protocol); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 5861 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5862 | } |
| 5863 | |
| 5864 | // TODO(willchan): Redo this test to use TLS/NPN=>SPDY. Currently, the code |
| 5865 | // says that it does SPDY, but it just does the TLS handshake, but the NPN |
| 5866 | // response does not indicate SPDY, so we just do standard HTTPS over the port. |
| 5867 | // We should add code such that we don't fallback to HTTPS, but fallback to HTTP |
| 5868 | // on the original port. |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 5869 | // TEST_F(HttpNetworkTransactionTest, UseAlternateProtocol) { |
| 5870 | // SessionDependencies session_deps; |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 5871 | // |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 5872 | // HttpRequestInfo request; |
| 5873 | // request.method = "GET"; |
| 5874 | // request.url = GURL("http://www.google.com/"); |
| 5875 | // request.load_flags = 0; |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 5876 | // |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 5877 | // MockRead data_reads[] = { |
| 5878 | // MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 5879 | // MockRead("hello world"), |
| 5880 | // MockRead(true, OK), |
| 5881 | // }; |
| 5882 | // StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
| 5883 | // session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 5884 | // |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 5885 | // SSLSocketDataProvider ssl(true, OK); |
| 5886 | // session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 5887 | // |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 5888 | // TestCompletionCallback callback; |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 5889 | // |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 5890 | // scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 5891 | // |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 5892 | // HostPortPair http_host_port_pair; |
| 5893 | // http_host_port_pair.host = "www.google.com"; |
| 5894 | // http_host_port_pair.port = 80; |
| 5895 | // HttpAlternateProtocols* alternate_protocols = |
| 5896 | // session->mutable_alternate_protocols(); |
| 5897 | // alternate_protocols->SetAlternateProtocolFor( |
| 5898 | // http_host_port_pair, 1234 /* port is ignored */, |
[email protected] | 31e2c69e | 2010-04-15 18:06:06 | [diff] [blame] | 5899 | // HttpAlternateProtocols::NPN_SPDY_1); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 5900 | // |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 5901 | // scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 5902 | // |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5903 | // int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 5904 | // EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5905 | // EXPECT_EQ(OK, callback.WaitForResult()); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 5906 | // |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 5907 | // const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5908 | // ASSERT_TRUE(response != NULL); |
| 5909 | // ASSERT_TRUE(response->headers != NULL); |
| 5910 | // EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 5911 | // |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 5912 | // std::string response_data; |
| 5913 | // ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 5914 | // EXPECT_EQ("hello world", response_data); |
| 5915 | // } |
| 5916 | |
| 5917 | TEST_F(HttpNetworkTransactionTest, FailNpnSpdyAndFallback) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 5918 | HttpStreamFactory::set_use_alternate_protocols(true); |
| 5919 | HttpStreamFactory::set_next_protos(kExpectedNPNString); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5920 | SessionDependencies session_deps; |
| 5921 | |
| 5922 | HttpRequestInfo request; |
| 5923 | request.method = "GET"; |
| 5924 | request.url = GURL("http://www.google.com/"); |
| 5925 | request.load_flags = 0; |
| 5926 | |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 5927 | StaticSocketDataProvider first_tcp_connect; |
| 5928 | session_deps.socket_factory.AddSocketDataProvider(&first_tcp_connect); |
| 5929 | |
| 5930 | SSLSocketDataProvider ssl(true, OK); |
| 5931 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 5932 | |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5933 | MockRead data_reads[] = { |
| 5934 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 5935 | MockRead("hello world"), |
| 5936 | MockRead(true, OK), |
| 5937 | }; |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 5938 | StaticSocketDataProvider fallback_data( |
| 5939 | data_reads, arraysize(data_reads), NULL, 0); |
| 5940 | session_deps.socket_factory.AddSocketDataProvider(&fallback_data); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5941 | |
| 5942 | TestCompletionCallback callback; |
| 5943 | |
| 5944 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 5945 | |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame] | 5946 | HostPortPair http_host_port_pair("www.google.com", 80); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5947 | HttpAlternateProtocols* alternate_protocols = |
| 5948 | session->mutable_alternate_protocols(); |
| 5949 | alternate_protocols->SetAlternateProtocolFor( |
| 5950 | http_host_port_pair, 1234 /* port is ignored */, |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 5951 | HttpAlternateProtocols::NPN_SPDY_2); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5952 | |
| 5953 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 5954 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 5955 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 5956 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5957 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 5958 | |
| 5959 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 5960 | ASSERT_TRUE(response != NULL); |
| 5961 | ASSERT_TRUE(response->headers != NULL); |
| 5962 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 5963 | |
| 5964 | std::string response_data; |
| 5965 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 5966 | EXPECT_EQ("hello world", response_data); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 5967 | HttpStreamFactory::set_next_protos(""); |
| 5968 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5969 | } |
| 5970 | |
| 5971 | TEST_F(HttpNetworkTransactionTest, UseAlternateProtocolForNpnSpdy) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 5972 | HttpStreamFactory::set_use_alternate_protocols(true); |
| 5973 | HttpStreamFactory::set_next_protos(kExpectedNPNString); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5974 | SessionDependencies session_deps; |
| 5975 | |
| 5976 | HttpRequestInfo request; |
| 5977 | request.method = "GET"; |
| 5978 | request.url = GURL("http://www.google.com/"); |
| 5979 | request.load_flags = 0; |
| 5980 | |
| 5981 | MockRead data_reads[] = { |
| 5982 | MockRead("HTTP/1.1 200 OK\r\n"), |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 5983 | MockRead(kAlternateProtocolHttpHeader), |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5984 | MockRead("hello world"), |
| 5985 | MockRead(true, OK), |
| 5986 | }; |
| 5987 | |
| 5988 | StaticSocketDataProvider first_transaction( |
| 5989 | data_reads, arraysize(data_reads), NULL, 0); |
| 5990 | session_deps.socket_factory.AddSocketDataProvider(&first_transaction); |
| 5991 | |
| 5992 | SSLSocketDataProvider ssl(true, OK); |
| 5993 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 5994 | ssl.next_proto = "spdy/2"; |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 5995 | ssl.was_npn_negotiated = true; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5996 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 5997 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 5998 | scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); |
[email protected] | e7f7509 | 2010-07-01 22:39:13 | [diff] [blame] | 5999 | MockWrite spdy_writes[] = { CreateMockWrite(*req) }; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6000 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 6001 | scoped_ptr<spdy::SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 6002 | scoped_ptr<spdy::SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6003 | MockRead spdy_reads[] = { |
[email protected] | e7f7509 | 2010-07-01 22:39:13 | [diff] [blame] | 6004 | CreateMockRead(*resp), |
| 6005 | CreateMockRead(*data), |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6006 | MockRead(true, 0, 0), |
| 6007 | }; |
| 6008 | |
| 6009 | scoped_refptr<DelayedSocketData> spdy_data( |
| 6010 | new DelayedSocketData( |
| 6011 | 1, // wait for one write to finish before reading. |
| 6012 | spdy_reads, arraysize(spdy_reads), |
| 6013 | spdy_writes, arraysize(spdy_writes))); |
| 6014 | session_deps.socket_factory.AddSocketDataProvider(spdy_data); |
| 6015 | |
| 6016 | TestCompletionCallback callback; |
| 6017 | |
| 6018 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 6019 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 6020 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 6021 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6022 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6023 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6024 | |
| 6025 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 6026 | ASSERT_TRUE(response != NULL); |
| 6027 | ASSERT_TRUE(response->headers != NULL); |
| 6028 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 6029 | |
| 6030 | std::string response_data; |
| 6031 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 6032 | EXPECT_EQ("hello world", response_data); |
| 6033 | |
| 6034 | trans.reset(new HttpNetworkTransaction(session)); |
| 6035 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 6036 | rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6037 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6038 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6039 | |
| 6040 | response = trans->GetResponseInfo(); |
| 6041 | ASSERT_TRUE(response != NULL); |
| 6042 | ASSERT_TRUE(response->headers != NULL); |
| 6043 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 6044 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 6045 | EXPECT_TRUE(response->was_npn_negotiated); |
[email protected] | 193b0b89 | 2010-06-26 03:57:57 | [diff] [blame] | 6046 | EXPECT_TRUE(response->was_alternate_protocol_available); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6047 | |
| 6048 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 6049 | EXPECT_EQ("hello!", response_data); |
| 6050 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6051 | HttpStreamFactory::set_next_protos(""); |
| 6052 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6053 | } |
| 6054 | |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6055 | class CapturingProxyResolver : public ProxyResolver { |
| 6056 | public: |
| 6057 | CapturingProxyResolver() : ProxyResolver(false /* expects_pac_bytes */) {} |
| 6058 | virtual ~CapturingProxyResolver() {} |
| 6059 | |
| 6060 | virtual int GetProxyForURL(const GURL& url, |
| 6061 | ProxyInfo* results, |
| 6062 | CompletionCallback* callback, |
| 6063 | RequestHandle* request, |
| 6064 | const BoundNetLog& net_log) { |
[email protected] | fae7669f | 2010-08-02 21:49:40 | [diff] [blame] | 6065 | ProxyServer proxy_server(ProxyServer::SCHEME_HTTP, |
| 6066 | HostPortPair("myproxy", 80)); |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 6067 | results->UseProxyServer(proxy_server); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6068 | resolved_.push_back(url); |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 6069 | return OK; |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6070 | } |
| 6071 | |
| 6072 | virtual void CancelRequest(RequestHandle request) { |
| 6073 | NOTREACHED(); |
| 6074 | } |
| 6075 | |
[email protected] | 2447640 | 2010-07-20 20:55:17 | [diff] [blame] | 6076 | virtual int SetPacScript(const scoped_refptr<ProxyResolverScriptData>&, |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6077 | CompletionCallback* /*callback*/) { |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 6078 | return OK; |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6079 | } |
| 6080 | |
[email protected] | 2447640 | 2010-07-20 20:55:17 | [diff] [blame] | 6081 | const std::vector<GURL>& resolved() const { return resolved_; } |
| 6082 | |
| 6083 | private: |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6084 | std::vector<GURL> resolved_; |
| 6085 | |
| 6086 | DISALLOW_COPY_AND_ASSIGN(CapturingProxyResolver); |
| 6087 | }; |
| 6088 | |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6089 | TEST_F(HttpNetworkTransactionTest, UseAlternateProtocolForTunneledNpnSpdy) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6090 | HttpStreamFactory::set_use_alternate_protocols(true); |
| 6091 | HttpStreamFactory::set_next_protos(kExpectedNPNString); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6092 | |
| 6093 | ProxyConfig proxy_config; |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 6094 | proxy_config.set_auto_detect(true); |
| 6095 | proxy_config.set_pac_url(GURL("http://fooproxyurl")); |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 6096 | |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6097 | CapturingProxyResolver* capturing_proxy_resolver = |
| 6098 | new CapturingProxyResolver(); |
[email protected] | 66761b95 | 2010-06-25 21:30:38 | [diff] [blame] | 6099 | SessionDependencies session_deps(new ProxyService( |
| 6100 | new ProxyConfigServiceFixed(proxy_config), capturing_proxy_resolver, |
| 6101 | NULL)); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6102 | |
| 6103 | HttpRequestInfo request; |
| 6104 | request.method = "GET"; |
| 6105 | request.url = GURL("http://www.google.com/"); |
| 6106 | request.load_flags = 0; |
| 6107 | |
| 6108 | MockRead data_reads[] = { |
| 6109 | MockRead("HTTP/1.1 200 OK\r\n"), |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 6110 | MockRead(kAlternateProtocolHttpHeader), |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6111 | MockRead("hello world"), |
| 6112 | MockRead(true, OK), |
| 6113 | }; |
| 6114 | |
| 6115 | StaticSocketDataProvider first_transaction( |
| 6116 | data_reads, arraysize(data_reads), NULL, 0); |
| 6117 | session_deps.socket_factory.AddSocketDataProvider(&first_transaction); |
| 6118 | |
| 6119 | SSLSocketDataProvider ssl(true, OK); |
| 6120 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 6121 | ssl.next_proto = "spdy/2"; |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 6122 | ssl.was_npn_negotiated = true; |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6123 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 6124 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 6125 | scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6126 | MockWrite spdy_writes[] = { |
| 6127 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 6128 | "Host: www.google.com\r\n" |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 6129 | "Proxy-Connection: keep-alive\r\n\r\n"), // 0 |
[email protected] | e7f7509 | 2010-07-01 22:39:13 | [diff] [blame] | 6130 | CreateMockWrite(*req) // 3 |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6131 | }; |
| 6132 | |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 6133 | const char kCONNECTResponse[] = "HTTP/1.1 200 Connected\r\n\r\n"; |
| 6134 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 6135 | scoped_ptr<spdy::SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 6136 | scoped_ptr<spdy::SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6137 | MockRead spdy_reads[] = { |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 6138 | MockRead(true, kCONNECTResponse, arraysize(kCONNECTResponse) - 1, 1), // 1 |
[email protected] | e7f7509 | 2010-07-01 22:39:13 | [diff] [blame] | 6139 | CreateMockRead(*resp.get(), 4), // 2, 4 |
| 6140 | CreateMockRead(*data.get(), 4), // 5 |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 6141 | MockRead(true, 0, 0, 4), // 6 |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6142 | }; |
| 6143 | |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 6144 | scoped_refptr<OrderedSocketData> spdy_data( |
| 6145 | new OrderedSocketData( |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6146 | spdy_reads, arraysize(spdy_reads), |
| 6147 | spdy_writes, arraysize(spdy_writes))); |
| 6148 | session_deps.socket_factory.AddSocketDataProvider(spdy_data); |
| 6149 | |
| 6150 | TestCompletionCallback callback; |
| 6151 | |
| 6152 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 6153 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 6154 | |
| 6155 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 6156 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6157 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6158 | |
| 6159 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 6160 | ASSERT_TRUE(response != NULL); |
| 6161 | ASSERT_TRUE(response->headers != NULL); |
| 6162 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 6163 | EXPECT_FALSE(response->was_fetched_via_spdy); |
| 6164 | EXPECT_FALSE(response->was_npn_negotiated); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6165 | |
| 6166 | std::string response_data; |
| 6167 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 6168 | EXPECT_EQ("hello world", response_data); |
| 6169 | |
| 6170 | trans.reset(new HttpNetworkTransaction(session)); |
| 6171 | |
| 6172 | rv = trans->Start(&request, &callback, BoundNetLog()); |
| 6173 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6174 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6175 | |
| 6176 | response = trans->GetResponseInfo(); |
| 6177 | ASSERT_TRUE(response != NULL); |
| 6178 | ASSERT_TRUE(response->headers != NULL); |
| 6179 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 6180 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 6181 | EXPECT_TRUE(response->was_npn_negotiated); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6182 | |
| 6183 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 6184 | EXPECT_EQ("hello!", response_data); |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 6185 | ASSERT_EQ(2u, capturing_proxy_resolver->resolved().size()); |
| 6186 | EXPECT_EQ("http://www.google.com/", |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6187 | capturing_proxy_resolver->resolved()[0].spec()); |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 6188 | EXPECT_EQ("https://www.google.com/", |
| 6189 | capturing_proxy_resolver->resolved()[1].spec()); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6190 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6191 | HttpStreamFactory::set_next_protos(""); |
| 6192 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6193 | } |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 6194 | |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6195 | TEST_F(HttpNetworkTransactionTest, |
| 6196 | UseAlternateProtocolForNpnSpdyWithExistingSpdySession) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6197 | HttpStreamFactory::set_use_alternate_protocols(true); |
| 6198 | HttpStreamFactory::set_next_protos(kExpectedNPNString); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6199 | SessionDependencies session_deps; |
| 6200 | |
| 6201 | HttpRequestInfo request; |
| 6202 | request.method = "GET"; |
| 6203 | request.url = GURL("http://www.google.com/"); |
| 6204 | request.load_flags = 0; |
| 6205 | |
| 6206 | MockRead data_reads[] = { |
| 6207 | MockRead("HTTP/1.1 200 OK\r\n"), |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 6208 | MockRead(kAlternateProtocolHttpHeader), |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6209 | MockRead("hello world"), |
| 6210 | MockRead(true, OK), |
| 6211 | }; |
| 6212 | |
| 6213 | StaticSocketDataProvider first_transaction( |
| 6214 | data_reads, arraysize(data_reads), NULL, 0); |
| 6215 | session_deps.socket_factory.AddSocketDataProvider(&first_transaction); |
| 6216 | |
| 6217 | SSLSocketDataProvider ssl(true, OK); |
| 6218 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 6219 | ssl.next_proto = "spdy/2"; |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 6220 | ssl.was_npn_negotiated = true; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6221 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 6222 | // Make sure we use ssl for spdy here. |
| 6223 | SpdySession::SetSSLMode(true); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6224 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 6225 | scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); |
[email protected] | e7f7509 | 2010-07-01 22:39:13 | [diff] [blame] | 6226 | MockWrite spdy_writes[] = { CreateMockWrite(*req) }; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6227 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 6228 | scoped_ptr<spdy::SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 6229 | scoped_ptr<spdy::SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6230 | MockRead spdy_reads[] = { |
[email protected] | e7f7509 | 2010-07-01 22:39:13 | [diff] [blame] | 6231 | CreateMockRead(*resp), |
| 6232 | CreateMockRead(*data), |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6233 | MockRead(true, 0, 0), |
| 6234 | }; |
| 6235 | |
| 6236 | scoped_refptr<DelayedSocketData> spdy_data( |
| 6237 | new DelayedSocketData( |
| 6238 | 1, // wait for one write to finish before reading. |
| 6239 | spdy_reads, arraysize(spdy_reads), |
| 6240 | spdy_writes, arraysize(spdy_writes))); |
| 6241 | session_deps.socket_factory.AddSocketDataProvider(spdy_data); |
| 6242 | |
| 6243 | TestCompletionCallback callback; |
| 6244 | |
| 6245 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 6246 | |
| 6247 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 6248 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 6249 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6250 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6251 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6252 | |
| 6253 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 6254 | ASSERT_TRUE(response != NULL); |
| 6255 | ASSERT_TRUE(response->headers != NULL); |
| 6256 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 6257 | |
| 6258 | std::string response_data; |
| 6259 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 6260 | EXPECT_EQ("hello world", response_data); |
| 6261 | |
| 6262 | // Set up an initial SpdySession in the pool to reuse. |
[email protected] | 02b0c34 | 2010-09-25 21:09:38 | [diff] [blame] | 6263 | HostPortPair host_port_pair("www.google.com", 443); |
| 6264 | HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6265 | scoped_refptr<SpdySession> spdy_session = |
[email protected] | f458033 | 2010-09-25 21:20:27 | [diff] [blame] | 6266 | session->spdy_session_pool()->Get(pair, session->mutable_spdy_settings(), |
| 6267 | BoundNetLog()); |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 6268 | scoped_refptr<TCPSocketParams> tcp_params = |
| 6269 | new TCPSocketParams("www.google.com", 443, MEDIUM, GURL(), false); |
[email protected] | 02b0c34 | 2010-09-25 21:09:38 | [diff] [blame] | 6270 | |
| 6271 | scoped_ptr<ClientSocketHandle> connection(new ClientSocketHandle); |
| 6272 | EXPECT_EQ(ERR_IO_PENDING, |
| 6273 | connection->Init(host_port_pair.ToString(),tcp_params, LOWEST, |
| 6274 | &callback, session->tcp_socket_pool(), |
| 6275 | BoundNetLog())); |
| 6276 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6277 | |
| 6278 | SSLConfig ssl_config; |
| 6279 | session->ssl_config_service()->GetSSLConfig(&ssl_config); |
| 6280 | ClientSocket* socket = connection->release_socket(); |
[email protected] | 563ee90 | 2010-09-27 16:16:17 | [diff] [blame] | 6281 | socket = session->socket_factory()->CreateSSLClientSocket(socket, "" , |
| 6282 | ssl_config); |
[email protected] | 02b0c34 | 2010-09-25 21:09:38 | [diff] [blame] | 6283 | connection->set_socket(socket); |
| 6284 | EXPECT_EQ(ERR_IO_PENDING, socket->Connect(&callback)); |
| 6285 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6286 | |
| 6287 | EXPECT_EQ(OK, spdy_session->InitializeWithSocket(connection.release(), |
| 6288 | true, OK)); |
| 6289 | |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6290 | trans.reset(new HttpNetworkTransaction(session)); |
| 6291 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 6292 | rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6293 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6294 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6295 | |
| 6296 | response = trans->GetResponseInfo(); |
| 6297 | ASSERT_TRUE(response != NULL); |
| 6298 | ASSERT_TRUE(response->headers != NULL); |
| 6299 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 6300 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 6301 | EXPECT_TRUE(response->was_npn_negotiated); |
[email protected] | 193b0b89 | 2010-06-26 03:57:57 | [diff] [blame] | 6302 | EXPECT_TRUE(response->was_alternate_protocol_available); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6303 | |
| 6304 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 6305 | EXPECT_EQ("hello!", response_data); |
| 6306 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6307 | HttpStreamFactory::set_next_protos(""); |
| 6308 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6309 | } |
| 6310 | |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 6311 | // GenerateAuthToken is a mighty big test. |
| 6312 | // It tests all permutation of GenerateAuthToken behavior: |
| 6313 | // - Synchronous and Asynchronous completion. |
| 6314 | // - OK or error on completion. |
| 6315 | // - Direct connection, non-authenticating proxy, and authenticating proxy. |
| 6316 | // - HTTP or HTTPS backend (to include proxy tunneling). |
| 6317 | // - Non-authenticating and authenticating backend. |
| 6318 | // |
| 6319 | // In all, there are 44 reasonable permuations (for example, if there are |
| 6320 | // problems generating an auth token for an authenticating proxy, we don't |
| 6321 | // need to test all permutations of the backend server). |
| 6322 | // |
| 6323 | // The test proceeds by going over each of the configuration cases, and |
| 6324 | // potentially running up to three rounds in each of the tests. The TestConfig |
| 6325 | // specifies both the configuration for the test as well as the expectations |
| 6326 | // for the results. |
| 6327 | TEST_F(HttpNetworkTransactionTest, GenerateAuthToken) { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 6328 | static const char kServer[] = "http://www.example.com"; |
| 6329 | static const char kSecureServer[] = "https://www.example.com"; |
| 6330 | static const char kProxy[] = "myproxy:70"; |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 6331 | const int kAuthErr = ERR_INVALID_AUTH_CREDENTIALS; |
| 6332 | |
| 6333 | enum AuthTiming { |
| 6334 | AUTH_NONE, |
| 6335 | AUTH_SYNC, |
| 6336 | AUTH_ASYNC, |
| 6337 | }; |
| 6338 | |
| 6339 | const MockWrite kGet( |
| 6340 | "GET / HTTP/1.1\r\n" |
| 6341 | "Host: www.example.com\r\n" |
| 6342 | "Connection: keep-alive\r\n\r\n"); |
| 6343 | const MockWrite kGetProxy( |
| 6344 | "GET http://www.example.com/ HTTP/1.1\r\n" |
| 6345 | "Host: www.example.com\r\n" |
| 6346 | "Proxy-Connection: keep-alive\r\n\r\n"); |
| 6347 | const MockWrite kGetAuth( |
| 6348 | "GET / HTTP/1.1\r\n" |
| 6349 | "Host: www.example.com\r\n" |
| 6350 | "Connection: keep-alive\r\n" |
| 6351 | "Authorization: auth_token\r\n\r\n"); |
| 6352 | const MockWrite kGetProxyAuth( |
| 6353 | "GET http://www.example.com/ HTTP/1.1\r\n" |
| 6354 | "Host: www.example.com\r\n" |
| 6355 | "Proxy-Connection: keep-alive\r\n" |
| 6356 | "Proxy-Authorization: auth_token\r\n\r\n"); |
| 6357 | const MockWrite kGetAuthThroughProxy( |
| 6358 | "GET http://www.example.com/ HTTP/1.1\r\n" |
| 6359 | "Host: www.example.com\r\n" |
| 6360 | "Proxy-Connection: keep-alive\r\n" |
| 6361 | "Authorization: auth_token\r\n\r\n"); |
| 6362 | const MockWrite kGetAuthWithProxyAuth( |
| 6363 | "GET http://www.example.com/ HTTP/1.1\r\n" |
| 6364 | "Host: www.example.com\r\n" |
| 6365 | "Proxy-Connection: keep-alive\r\n" |
| 6366 | "Proxy-Authorization: auth_token\r\n" |
| 6367 | "Authorization: auth_token\r\n\r\n"); |
| 6368 | const MockWrite kConnect( |
| 6369 | "CONNECT www.example.com:443 HTTP/1.1\r\n" |
| 6370 | "Host: www.example.com\r\n" |
| 6371 | "Proxy-Connection: keep-alive\r\n\r\n"); |
| 6372 | const MockWrite kConnectProxyAuth( |
| 6373 | "CONNECT www.example.com:443 HTTP/1.1\r\n" |
| 6374 | "Host: www.example.com\r\n" |
| 6375 | "Proxy-Connection: keep-alive\r\n" |
| 6376 | "Proxy-Authorization: auth_token\r\n\r\n"); |
| 6377 | |
| 6378 | const MockRead kSuccess( |
| 6379 | "HTTP/1.1 200 OK\r\n" |
| 6380 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 6381 | "Content-Length: 3\r\n\r\n" |
| 6382 | "Yes"); |
| 6383 | const MockRead kFailure( |
| 6384 | "Should not be called."); |
| 6385 | const MockRead kServerChallenge( |
| 6386 | "HTTP/1.1 401 Unauthorized\r\n" |
| 6387 | "WWW-Authenticate: Mock realm=server\r\n" |
| 6388 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 6389 | "Content-Length: 14\r\n\r\n" |
| 6390 | "Unauthorized\r\n"); |
| 6391 | const MockRead kProxyChallenge( |
| 6392 | "HTTP/1.1 407 Unauthorized\r\n" |
| 6393 | "Proxy-Authenticate: Mock realm=proxy\r\n" |
| 6394 | "Proxy-Connection: close\r\n" |
| 6395 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 6396 | "Content-Length: 14\r\n\r\n" |
| 6397 | "Unauthorized\r\n"); |
| 6398 | const MockRead kProxyConnected( |
| 6399 | "HTTP/1.1 200 Connection Established\r\n\r\n"); |
| 6400 | |
| 6401 | // NOTE(cbentzel): I wanted TestReadWriteRound to be a simple struct with |
| 6402 | // no constructors, but the C++ compiler on Windows warns about |
| 6403 | // unspecified data in compound literals. So, moved to using constructors, |
| 6404 | // and TestRound's created with the default constructor should not be used. |
| 6405 | struct TestRound { |
| 6406 | TestRound() |
| 6407 | : expected_rv(ERR_UNEXPECTED), |
| 6408 | extra_write(NULL), |
| 6409 | extra_read(NULL) { |
| 6410 | } |
| 6411 | TestRound(const MockWrite& write_arg, const MockRead& read_arg, |
| 6412 | int expected_rv_arg) |
| 6413 | : write(write_arg), |
| 6414 | read(read_arg), |
| 6415 | expected_rv(expected_rv_arg), |
| 6416 | extra_write(NULL), |
| 6417 | extra_read(NULL) { |
| 6418 | } |
| 6419 | TestRound(const MockWrite& write_arg, const MockRead& read_arg, |
| 6420 | int expected_rv_arg, const MockWrite* extra_write_arg, |
| 6421 | const MockWrite* extra_read_arg) |
| 6422 | : write(write_arg), |
| 6423 | read(read_arg), |
| 6424 | expected_rv(expected_rv_arg), |
| 6425 | extra_write(extra_write_arg), |
| 6426 | extra_read(extra_read_arg) { |
| 6427 | } |
| 6428 | MockWrite write; |
| 6429 | MockRead read; |
| 6430 | int expected_rv; |
| 6431 | const MockWrite* extra_write; |
| 6432 | const MockRead* extra_read; |
| 6433 | }; |
| 6434 | |
| 6435 | static const int kNoSSL = 500; |
| 6436 | |
| 6437 | struct TestConfig { |
| 6438 | const char* proxy_url; |
| 6439 | AuthTiming proxy_auth_timing; |
| 6440 | int proxy_auth_rv; |
| 6441 | const char* server_url; |
| 6442 | AuthTiming server_auth_timing; |
| 6443 | int server_auth_rv; |
| 6444 | int num_auth_rounds; |
| 6445 | int first_ssl_round; |
| 6446 | TestRound rounds[3]; |
| 6447 | } test_configs[] = { |
| 6448 | // Non-authenticating HTTP server with a direct connection. |
| 6449 | { NULL, AUTH_NONE, OK, kServer, AUTH_NONE, OK, 1, kNoSSL, |
| 6450 | { TestRound(kGet, kSuccess, OK)}}, |
| 6451 | // Authenticating HTTP server with a direct connection. |
| 6452 | { NULL, AUTH_NONE, OK, kServer, AUTH_SYNC, OK, 2, kNoSSL, |
| 6453 | { TestRound(kGet, kServerChallenge, OK), |
| 6454 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 6455 | { NULL, AUTH_NONE, OK, kServer, AUTH_SYNC, kAuthErr, 2, kNoSSL, |
| 6456 | { TestRound(kGet, kServerChallenge, OK), |
| 6457 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 6458 | { NULL, AUTH_NONE, OK, kServer, AUTH_ASYNC, OK, 2, kNoSSL, |
| 6459 | { TestRound(kGet, kServerChallenge, OK), |
| 6460 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 6461 | { NULL, AUTH_NONE, OK, kServer, AUTH_ASYNC, kAuthErr, 2, kNoSSL, |
| 6462 | { TestRound(kGet, kServerChallenge, OK), |
| 6463 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 6464 | // Non-authenticating HTTP server through a non-authenticating proxy. |
| 6465 | { kProxy, AUTH_NONE, OK, kServer, AUTH_NONE, OK, 1, kNoSSL, |
| 6466 | { TestRound(kGetProxy, kSuccess, OK)}}, |
| 6467 | // Authenticating HTTP server through a non-authenticating proxy. |
| 6468 | { kProxy, AUTH_NONE, OK, kServer, AUTH_SYNC, OK, 2, kNoSSL, |
| 6469 | { TestRound(kGetProxy, kServerChallenge, OK), |
| 6470 | TestRound(kGetAuthThroughProxy, kSuccess, OK)}}, |
| 6471 | { kProxy, AUTH_NONE, OK, kServer, AUTH_SYNC, kAuthErr, 2, kNoSSL, |
| 6472 | { TestRound(kGetProxy, kServerChallenge, OK), |
| 6473 | TestRound(kGetAuthThroughProxy, kFailure, kAuthErr)}}, |
| 6474 | { kProxy, AUTH_NONE, OK, kServer, AUTH_ASYNC, OK, 2, kNoSSL, |
| 6475 | { TestRound(kGetProxy, kServerChallenge, OK), |
| 6476 | TestRound(kGetAuthThroughProxy, kSuccess, OK)}}, |
| 6477 | { kProxy, AUTH_NONE, OK, kServer, AUTH_ASYNC, kAuthErr, 2, kNoSSL, |
| 6478 | { TestRound(kGetProxy, kServerChallenge, OK), |
| 6479 | TestRound(kGetAuthThroughProxy, kFailure, kAuthErr)}}, |
| 6480 | // Non-authenticating HTTP server through an authenticating proxy. |
| 6481 | { kProxy, AUTH_SYNC, OK, kServer, AUTH_NONE, OK, 2, kNoSSL, |
| 6482 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 6483 | TestRound(kGetProxyAuth, kSuccess, OK)}}, |
| 6484 | { kProxy, AUTH_SYNC, kAuthErr, kServer, AUTH_NONE, OK, 2, kNoSSL, |
| 6485 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 6486 | TestRound(kGetProxyAuth, kFailure, kAuthErr)}}, |
| 6487 | { kProxy, AUTH_ASYNC, OK, kServer, AUTH_NONE, OK, 2, kNoSSL, |
| 6488 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 6489 | TestRound(kGetProxyAuth, kSuccess, OK)}}, |
| 6490 | { kProxy, AUTH_ASYNC, kAuthErr, kServer, AUTH_NONE, OK, 2, kNoSSL, |
| 6491 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 6492 | TestRound(kGetProxyAuth, kFailure, kAuthErr)}}, |
| 6493 | // Authenticating HTTP server through an authenticating proxy. |
| 6494 | { kProxy, AUTH_SYNC, OK, kServer, AUTH_SYNC, OK, 3, kNoSSL, |
| 6495 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 6496 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 6497 | TestRound(kGetAuthWithProxyAuth, kSuccess, OK)}}, |
| 6498 | { kProxy, AUTH_SYNC, OK, kServer, AUTH_SYNC, kAuthErr, 3, kNoSSL, |
| 6499 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 6500 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 6501 | TestRound(kGetAuthWithProxyAuth, kFailure, kAuthErr)}}, |
| 6502 | { kProxy, AUTH_ASYNC, OK, kServer, AUTH_SYNC, OK, 3, kNoSSL, |
| 6503 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 6504 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 6505 | TestRound(kGetAuthWithProxyAuth, kSuccess, OK)}}, |
| 6506 | { kProxy, AUTH_ASYNC, OK, kServer, AUTH_SYNC, kAuthErr, 3, kNoSSL, |
| 6507 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 6508 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 6509 | TestRound(kGetAuthWithProxyAuth, kFailure, kAuthErr)}}, |
| 6510 | { kProxy, AUTH_SYNC, OK, kServer, AUTH_ASYNC, OK, 3, kNoSSL, |
| 6511 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 6512 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 6513 | TestRound(kGetAuthWithProxyAuth, kSuccess, OK)}}, |
| 6514 | { kProxy, AUTH_SYNC, OK, kServer, AUTH_ASYNC, kAuthErr, 3, kNoSSL, |
| 6515 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 6516 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 6517 | TestRound(kGetAuthWithProxyAuth, kFailure, kAuthErr)}}, |
| 6518 | { kProxy, AUTH_ASYNC, OK, kServer, AUTH_ASYNC, OK, 3, kNoSSL, |
| 6519 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 6520 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 6521 | TestRound(kGetAuthWithProxyAuth, kSuccess, OK)}}, |
| 6522 | { kProxy, AUTH_ASYNC, OK, kServer, AUTH_ASYNC, kAuthErr, 3, kNoSSL, |
| 6523 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 6524 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 6525 | TestRound(kGetAuthWithProxyAuth, kFailure, kAuthErr)}}, |
| 6526 | // Non-authenticating HTTPS server with a direct connection. |
| 6527 | { NULL, AUTH_NONE, OK, kSecureServer, AUTH_NONE, OK, 1, 0, |
| 6528 | { TestRound(kGet, kSuccess, OK)}}, |
| 6529 | // Authenticating HTTPS server with a direct connection. |
| 6530 | { NULL, AUTH_NONE, OK, kSecureServer, AUTH_SYNC, OK, 2, 0, |
| 6531 | { TestRound(kGet, kServerChallenge, OK), |
| 6532 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 6533 | { NULL, AUTH_NONE, OK, kSecureServer, AUTH_SYNC, kAuthErr, 2, 0, |
| 6534 | { TestRound(kGet, kServerChallenge, OK), |
| 6535 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 6536 | { NULL, AUTH_NONE, OK, kSecureServer, AUTH_ASYNC, OK, 2, 0, |
| 6537 | { TestRound(kGet, kServerChallenge, OK), |
| 6538 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 6539 | { NULL, AUTH_NONE, OK, kSecureServer, AUTH_ASYNC, kAuthErr, 2, 0, |
| 6540 | { TestRound(kGet, kServerChallenge, OK), |
| 6541 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 6542 | // Non-authenticating HTTPS server with a non-authenticating proxy. |
| 6543 | { kProxy, AUTH_NONE, OK, kSecureServer, AUTH_NONE, OK, 1, 0, |
| 6544 | { TestRound(kConnect, kProxyConnected, OK, &kGet, &kSuccess)}}, |
| 6545 | // Authenticating HTTPS server through a non-authenticating proxy. |
| 6546 | { kProxy, AUTH_NONE, OK, kSecureServer, AUTH_SYNC, OK, 2, 0, |
| 6547 | { TestRound(kConnect, kProxyConnected, OK, &kGet, &kServerChallenge), |
| 6548 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 6549 | { kProxy, AUTH_NONE, OK, kSecureServer, AUTH_SYNC, kAuthErr, 2, 0, |
| 6550 | { TestRound(kConnect, kProxyConnected, OK, &kGet, &kServerChallenge), |
| 6551 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 6552 | { kProxy, AUTH_NONE, OK, kSecureServer, AUTH_ASYNC, OK, 2, 0, |
| 6553 | { TestRound(kConnect, kProxyConnected, OK, &kGet, &kServerChallenge), |
| 6554 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 6555 | { kProxy, AUTH_NONE, OK, kSecureServer, AUTH_ASYNC, kAuthErr, 2, 0, |
| 6556 | { TestRound(kConnect, kProxyConnected, OK, &kGet, &kServerChallenge), |
| 6557 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 6558 | // Non-Authenticating HTTPS server through an authenticating proxy. |
| 6559 | { kProxy, AUTH_SYNC, OK, kSecureServer, AUTH_NONE, OK, 2, 1, |
| 6560 | { TestRound(kConnect, kProxyChallenge, OK), |
| 6561 | TestRound(kConnectProxyAuth, kProxyConnected, OK, &kGet, &kSuccess)}}, |
| 6562 | { kProxy, AUTH_SYNC, kAuthErr, kSecureServer, AUTH_NONE, OK, 2, kNoSSL, |
| 6563 | { TestRound(kConnect, kProxyChallenge, OK), |
| 6564 | TestRound(kConnectProxyAuth, kFailure, kAuthErr)}}, |
| 6565 | { kProxy, AUTH_ASYNC, OK, kSecureServer, AUTH_NONE, OK, 2, 1, |
| 6566 | { TestRound(kConnect, kProxyChallenge, OK), |
| 6567 | TestRound(kConnectProxyAuth, kProxyConnected, OK, &kGet, &kSuccess)}}, |
| 6568 | { kProxy, AUTH_ASYNC, kAuthErr, kSecureServer, AUTH_NONE, OK, 2, kNoSSL, |
| 6569 | { TestRound(kConnect, kProxyChallenge, OK), |
| 6570 | TestRound(kConnectProxyAuth, kFailure, kAuthErr)}}, |
| 6571 | // Authenticating HTTPS server through an authenticating proxy. |
| 6572 | { kProxy, AUTH_SYNC, OK, kSecureServer, AUTH_SYNC, OK, 3, 1, |
| 6573 | { TestRound(kConnect, kProxyChallenge, OK), |
| 6574 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 6575 | &kGet, &kServerChallenge), |
| 6576 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 6577 | { kProxy, AUTH_SYNC, OK, kSecureServer, AUTH_SYNC, kAuthErr, 3, 1, |
| 6578 | { TestRound(kConnect, kProxyChallenge, OK), |
| 6579 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 6580 | &kGet, &kServerChallenge), |
| 6581 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 6582 | { kProxy, AUTH_ASYNC, OK, kSecureServer, AUTH_SYNC, OK, 3, 1, |
| 6583 | { TestRound(kConnect, kProxyChallenge, OK), |
| 6584 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 6585 | &kGet, &kServerChallenge), |
| 6586 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 6587 | { kProxy, AUTH_ASYNC, OK, kSecureServer, AUTH_SYNC, kAuthErr, 3, 1, |
| 6588 | { TestRound(kConnect, kProxyChallenge, OK), |
| 6589 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 6590 | &kGet, &kServerChallenge), |
| 6591 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 6592 | { kProxy, AUTH_SYNC, OK, kSecureServer, AUTH_ASYNC, OK, 3, 1, |
| 6593 | { TestRound(kConnect, kProxyChallenge, OK), |
| 6594 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 6595 | &kGet, &kServerChallenge), |
| 6596 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 6597 | { kProxy, AUTH_SYNC, OK, kSecureServer, AUTH_ASYNC, kAuthErr, 3, 1, |
| 6598 | { TestRound(kConnect, kProxyChallenge, OK), |
| 6599 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 6600 | &kGet, &kServerChallenge), |
| 6601 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 6602 | { kProxy, AUTH_ASYNC, OK, kSecureServer, AUTH_ASYNC, OK, 3, 1, |
| 6603 | { TestRound(kConnect, kProxyChallenge, OK), |
| 6604 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 6605 | &kGet, &kServerChallenge), |
| 6606 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 6607 | { kProxy, AUTH_ASYNC, OK, kSecureServer, AUTH_ASYNC, kAuthErr, 3, 1, |
| 6608 | { TestRound(kConnect, kProxyChallenge, OK), |
| 6609 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 6610 | &kGet, &kServerChallenge), |
| 6611 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 6612 | }; |
| 6613 | |
| 6614 | SessionDependencies session_deps; |
[email protected] | 3fd9dae | 2010-06-21 11:39:00 | [diff] [blame] | 6615 | HttpAuthHandlerMock::Factory* auth_factory( |
| 6616 | new HttpAuthHandlerMock::Factory()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 6617 | session_deps.http_auth_handler_factory.reset(auth_factory); |
| 6618 | |
| 6619 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_configs); ++i) { |
| 6620 | const TestConfig& test_config = test_configs[i]; |
[email protected] | 65d3438 | 2010-07-01 18:12:26 | [diff] [blame] | 6621 | |
| 6622 | // Set up authentication handlers as necessary. |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 6623 | if (test_config.proxy_auth_timing != AUTH_NONE) { |
[email protected] | 3fd9dae | 2010-06-21 11:39:00 | [diff] [blame] | 6624 | HttpAuthHandlerMock* auth_handler(new HttpAuthHandlerMock()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 6625 | std::string auth_challenge = "Mock realm=proxy"; |
| 6626 | GURL origin(test_config.proxy_url); |
| 6627 | HttpAuth::ChallengeTokenizer tokenizer(auth_challenge.begin(), |
| 6628 | auth_challenge.end()); |
| 6629 | auth_handler->InitFromChallenge(&tokenizer, HttpAuth::AUTH_PROXY, |
| 6630 | origin, BoundNetLog()); |
| 6631 | auth_handler->SetGenerateExpectation( |
| 6632 | test_config.proxy_auth_timing == AUTH_ASYNC, |
| 6633 | test_config.proxy_auth_rv); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 6634 | auth_factory->set_mock_handler(auth_handler, HttpAuth::AUTH_PROXY); |
| 6635 | } |
| 6636 | if (test_config.server_auth_timing != AUTH_NONE) { |
[email protected] | 3fd9dae | 2010-06-21 11:39:00 | [diff] [blame] | 6637 | HttpAuthHandlerMock* auth_handler(new HttpAuthHandlerMock()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 6638 | std::string auth_challenge = "Mock realm=server"; |
| 6639 | GURL origin(test_config.server_url); |
| 6640 | HttpAuth::ChallengeTokenizer tokenizer(auth_challenge.begin(), |
| 6641 | auth_challenge.end()); |
| 6642 | auth_handler->InitFromChallenge(&tokenizer, HttpAuth::AUTH_SERVER, |
| 6643 | origin, BoundNetLog()); |
| 6644 | auth_handler->SetGenerateExpectation( |
| 6645 | test_config.server_auth_timing == AUTH_ASYNC, |
| 6646 | test_config.server_auth_rv); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 6647 | auth_factory->set_mock_handler(auth_handler, HttpAuth::AUTH_SERVER); |
| 6648 | } |
| 6649 | if (test_config.proxy_url) { |
| 6650 | session_deps.proxy_service = |
| 6651 | CreateFixedProxyService(test_config.proxy_url); |
| 6652 | } else { |
[email protected] | ebeefff3 | 2010-09-15 05:10:02 | [diff] [blame] | 6653 | session_deps.proxy_service = ProxyService::CreateDirect(); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 6654 | } |
| 6655 | |
| 6656 | HttpRequestInfo request; |
| 6657 | request.method = "GET"; |
| 6658 | request.url = GURL(test_config.server_url); |
| 6659 | request.load_flags = 0; |
| 6660 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 6661 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 6662 | HttpNetworkTransaction trans(CreateSession(&session_deps)); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 6663 | |
| 6664 | for (int round = 0; round < test_config.num_auth_rounds; ++round) { |
| 6665 | const TestRound& read_write_round = test_config.rounds[round]; |
| 6666 | |
| 6667 | // Set up expected reads and writes. |
| 6668 | MockRead reads[2]; |
| 6669 | reads[0] = read_write_round.read; |
| 6670 | size_t length_reads = 1; |
| 6671 | if (read_write_round.extra_read) { |
| 6672 | reads[1] = *read_write_round.extra_read; |
| 6673 | length_reads = 2; |
| 6674 | } |
| 6675 | |
| 6676 | MockWrite writes[2]; |
| 6677 | writes[0] = read_write_round.write; |
| 6678 | size_t length_writes = 1; |
| 6679 | if (read_write_round.extra_write) { |
| 6680 | writes[1] = *read_write_round.extra_write; |
| 6681 | length_writes = 2; |
| 6682 | } |
| 6683 | StaticSocketDataProvider data_provider( |
| 6684 | reads, length_reads, writes, length_writes); |
| 6685 | session_deps.socket_factory.AddSocketDataProvider(&data_provider); |
| 6686 | |
| 6687 | // Add an SSL sequence if necessary. |
| 6688 | SSLSocketDataProvider ssl_socket_data_provider(false, OK); |
| 6689 | if (round >= test_config.first_ssl_round) |
| 6690 | session_deps.socket_factory.AddSSLSocketDataProvider( |
| 6691 | &ssl_socket_data_provider); |
| 6692 | |
| 6693 | // Start or restart the transaction. |
| 6694 | TestCompletionCallback callback; |
| 6695 | int rv; |
| 6696 | if (round == 0) { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 6697 | rv = trans.Start(&request, &callback, BoundNetLog()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 6698 | } else { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 6699 | rv = trans.RestartWithAuth(kFoo, kBar, &callback); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 6700 | } |
| 6701 | if (rv == ERR_IO_PENDING) |
| 6702 | rv = callback.WaitForResult(); |
| 6703 | |
| 6704 | // Compare results with expected data. |
| 6705 | EXPECT_EQ(read_write_round.expected_rv, rv); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 6706 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 6707 | if (read_write_round.expected_rv == OK) { |
| 6708 | EXPECT_FALSE(response == NULL); |
| 6709 | } else { |
| 6710 | EXPECT_TRUE(response == NULL); |
| 6711 | EXPECT_EQ(round + 1, test_config.num_auth_rounds); |
| 6712 | continue; |
| 6713 | } |
| 6714 | if (round + 1 < test_config.num_auth_rounds) { |
| 6715 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 6716 | } else { |
| 6717 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 6718 | } |
| 6719 | } |
[email protected] | e5ae96a | 2010-04-14 20:12:45 | [diff] [blame] | 6720 | } |
| 6721 | } |
| 6722 | |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 6723 | TEST_F(HttpNetworkTransactionTest, MultiRoundAuth) { |
| 6724 | // Do multi-round authentication and make sure it works correctly. |
| 6725 | SessionDependencies session_deps; |
| 6726 | HttpAuthHandlerMock::Factory* auth_factory( |
| 6727 | new HttpAuthHandlerMock::Factory()); |
| 6728 | session_deps.http_auth_handler_factory.reset(auth_factory); |
[email protected] | ebeefff3 | 2010-09-15 05:10:02 | [diff] [blame] | 6729 | session_deps.proxy_service = ProxyService::CreateDirect(); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 6730 | session_deps.host_resolver->rules()->AddRule("www.example.com", "10.0.0.1"); |
| 6731 | session_deps.host_resolver->set_synchronous_mode(true); |
| 6732 | |
| 6733 | HttpAuthHandlerMock* auth_handler(new HttpAuthHandlerMock()); |
| 6734 | auth_handler->set_connection_based(true); |
| 6735 | std::string auth_challenge = "Mock realm=server"; |
| 6736 | GURL origin("http://www.example.com"); |
| 6737 | HttpAuth::ChallengeTokenizer tokenizer(auth_challenge.begin(), |
| 6738 | auth_challenge.end()); |
| 6739 | auth_handler->InitFromChallenge(&tokenizer, HttpAuth::AUTH_SERVER, |
| 6740 | origin, BoundNetLog()); |
| 6741 | auth_factory->set_mock_handler(auth_handler, HttpAuth::AUTH_SERVER); |
| 6742 | |
| 6743 | scoped_refptr<HttpNetworkSession> session = CreateSession(&session_deps); |
| 6744 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 6745 | |
| 6746 | int rv = OK; |
| 6747 | const HttpResponseInfo* response = NULL; |
| 6748 | HttpRequestInfo request; |
| 6749 | request.method = "GET"; |
| 6750 | request.url = origin; |
| 6751 | request.load_flags = 0; |
| 6752 | TestCompletionCallback callback; |
| 6753 | |
| 6754 | const MockWrite kGet( |
| 6755 | "GET / HTTP/1.1\r\n" |
| 6756 | "Host: www.example.com\r\n" |
| 6757 | "Connection: keep-alive\r\n\r\n"); |
| 6758 | const MockWrite kGetAuth( |
| 6759 | "GET / HTTP/1.1\r\n" |
| 6760 | "Host: www.example.com\r\n" |
| 6761 | "Connection: keep-alive\r\n" |
| 6762 | "Authorization: auth_token\r\n\r\n"); |
| 6763 | |
| 6764 | const MockRead kServerChallenge( |
| 6765 | "HTTP/1.1 401 Unauthorized\r\n" |
| 6766 | "WWW-Authenticate: Mock realm=server\r\n" |
| 6767 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 6768 | "Content-Length: 14\r\n\r\n" |
| 6769 | "Unauthorized\r\n"); |
| 6770 | const MockRead kSuccess( |
| 6771 | "HTTP/1.1 200 OK\r\n" |
| 6772 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 6773 | "Content-Length: 3\r\n\r\n" |
| 6774 | "Yes"); |
| 6775 | |
| 6776 | MockWrite writes[] = { |
| 6777 | // First round |
| 6778 | kGet, |
| 6779 | // Second round |
| 6780 | kGetAuth, |
| 6781 | // Third round |
| 6782 | kGetAuth, |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 6783 | // Fourth round |
| 6784 | kGetAuth |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 6785 | }; |
| 6786 | MockRead reads[] = { |
| 6787 | // First round |
| 6788 | kServerChallenge, |
| 6789 | // Second round |
| 6790 | kServerChallenge, |
| 6791 | // Third round |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 6792 | kServerChallenge, |
| 6793 | // Fourth round |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 6794 | kSuccess, |
| 6795 | }; |
| 6796 | StaticSocketDataProvider data_provider(reads, arraysize(reads), |
| 6797 | writes, arraysize(writes)); |
| 6798 | session_deps.socket_factory.AddSocketDataProvider(&data_provider); |
| 6799 | |
| 6800 | // First round |
| 6801 | auth_handler->SetGenerateExpectation(false, OK); |
| 6802 | rv = trans->Start(&request, &callback, BoundNetLog()); |
| 6803 | if (rv == ERR_IO_PENDING) |
| 6804 | rv = callback.WaitForResult(); |
| 6805 | EXPECT_EQ(OK, rv); |
| 6806 | response = trans->GetResponseInfo(); |
| 6807 | ASSERT_FALSE(response == NULL); |
| 6808 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 6809 | |
| 6810 | // Second round |
| 6811 | auth_handler->SetGenerateExpectation(false, OK); |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 6812 | rv = trans->RestartWithAuth(kFoo, kBar, &callback); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 6813 | if (rv == ERR_IO_PENDING) |
| 6814 | rv = callback.WaitForResult(); |
| 6815 | EXPECT_EQ(OK, rv); |
| 6816 | response = trans->GetResponseInfo(); |
| 6817 | ASSERT_FALSE(response == NULL); |
| 6818 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 6819 | |
| 6820 | // Third round |
| 6821 | auth_handler->SetGenerateExpectation(false, OK); |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 6822 | rv = trans->RestartWithAuth(string16(), string16(), &callback); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 6823 | if (rv == ERR_IO_PENDING) |
| 6824 | rv = callback.WaitForResult(); |
| 6825 | EXPECT_EQ(OK, rv); |
| 6826 | response = trans->GetResponseInfo(); |
| 6827 | ASSERT_FALSE(response == NULL); |
| 6828 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 6829 | |
| 6830 | // Fourth round |
| 6831 | auth_handler->SetGenerateExpectation(false, OK); |
| 6832 | rv = trans->RestartWithAuth(string16(), string16(), &callback); |
| 6833 | if (rv == ERR_IO_PENDING) |
| 6834 | rv = callback.WaitForResult(); |
| 6835 | EXPECT_EQ(OK, rv); |
| 6836 | response = trans->GetResponseInfo(); |
| 6837 | ASSERT_FALSE(response == NULL); |
| 6838 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 6839 | } |
| 6840 | |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 6841 | class TLSDecompressionFailureSocketDataProvider : public SocketDataProvider { |
| 6842 | public: |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 6843 | explicit TLSDecompressionFailureSocketDataProvider(bool fail_all) |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 6844 | : fail_all_(fail_all) { |
| 6845 | } |
| 6846 | |
| 6847 | virtual MockRead GetNextRead() { |
| 6848 | if (fail_all_) |
| 6849 | return MockRead(false /* async */, ERR_SSL_DECOMPRESSION_FAILURE_ALERT); |
| 6850 | |
| 6851 | return MockRead(false /* async */, |
| 6852 | "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nok.\r\n"); |
| 6853 | } |
| 6854 | |
| 6855 | virtual MockWriteResult OnWrite(const std::string& data) { |
| 6856 | return MockWriteResult(false /* async */, data.size()); |
| 6857 | } |
| 6858 | |
| 6859 | void Reset() { |
| 6860 | } |
| 6861 | |
| 6862 | private: |
| 6863 | const bool fail_all_; |
| 6864 | }; |
| 6865 | |
| 6866 | // Test that we restart a connection when we see a decompression failure from |
| 6867 | // the peer during the handshake. (In the real world we'll restart with SSLv3 |
| 6868 | // and we won't offer DEFLATE in that case.) |
| 6869 | TEST_F(HttpNetworkTransactionTest, RestartAfterTLSDecompressionFailure) { |
| 6870 | HttpRequestInfo request; |
| 6871 | request.method = "GET"; |
| 6872 | request.url = GURL("https://tlsdecompressionfailure.example.com/"); |
| 6873 | request.load_flags = 0; |
| 6874 | |
| 6875 | SessionDependencies session_deps; |
| 6876 | TLSDecompressionFailureSocketDataProvider socket_data_provider1( |
| 6877 | false /* fail all reads */); |
| 6878 | TLSDecompressionFailureSocketDataProvider socket_data_provider2(false); |
| 6879 | SSLSocketDataProvider ssl_socket_data_provider1( |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 6880 | false, ERR_SSL_DECOMPRESSION_FAILURE_ALERT); |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 6881 | SSLSocketDataProvider ssl_socket_data_provider2(false, OK); |
| 6882 | session_deps.socket_factory.AddSocketDataProvider(&socket_data_provider1); |
| 6883 | session_deps.socket_factory.AddSocketDataProvider(&socket_data_provider2); |
| 6884 | session_deps.socket_factory.AddSSLSocketDataProvider( |
| 6885 | &ssl_socket_data_provider1); |
| 6886 | session_deps.socket_factory.AddSSLSocketDataProvider( |
| 6887 | &ssl_socket_data_provider2); |
| 6888 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 6889 | // Work around http://crbug.com/37454 |
| 6890 | StaticSocketDataProvider bug37454_connection; |
| 6891 | bug37454_connection.set_connect_data(MockConnect(true, ERR_UNEXPECTED)); |
| 6892 | session_deps.socket_factory.AddSocketDataProvider(&bug37454_connection); |
| 6893 | |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 6894 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 6895 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 6896 | TestCompletionCallback callback; |
| 6897 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 6898 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 6899 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6900 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6901 | |
| 6902 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 6903 | ASSERT_TRUE(response != NULL); |
| 6904 | ASSERT_TRUE(response->headers != NULL); |
| 6905 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 6906 | |
| 6907 | std::string response_data; |
| 6908 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 6909 | EXPECT_EQ("ok.", response_data); |
| 6910 | } |
| 6911 | |
| 6912 | // Test that we restart a connection if we get a decompression failure from the |
| 6913 | // peer while reading the first bytes from the connection. This occurs when the |
| 6914 | // peer cannot handle DEFLATE but we're using False Start, so we don't notice |
| 6915 | // in the handshake. |
| 6916 | TEST_F(HttpNetworkTransactionTest, |
| 6917 | RestartAfterTLSDecompressionFailureWithFalseStart) { |
| 6918 | HttpRequestInfo request; |
| 6919 | request.method = "GET"; |
| 6920 | request.url = GURL("https://tlsdecompressionfailure2.example.com/"); |
| 6921 | request.load_flags = 0; |
| 6922 | |
| 6923 | SessionDependencies session_deps; |
| 6924 | TLSDecompressionFailureSocketDataProvider socket_data_provider1( |
| 6925 | true /* fail all reads */); |
| 6926 | TLSDecompressionFailureSocketDataProvider socket_data_provider2(false); |
| 6927 | SSLSocketDataProvider ssl_socket_data_provider1(false, OK); |
| 6928 | SSLSocketDataProvider ssl_socket_data_provider2(false, OK); |
| 6929 | session_deps.socket_factory.AddSocketDataProvider(&socket_data_provider1); |
| 6930 | session_deps.socket_factory.AddSocketDataProvider(&socket_data_provider2); |
| 6931 | session_deps.socket_factory.AddSSLSocketDataProvider( |
| 6932 | &ssl_socket_data_provider1); |
| 6933 | session_deps.socket_factory.AddSSLSocketDataProvider( |
| 6934 | &ssl_socket_data_provider2); |
| 6935 | |
| 6936 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 6937 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 6938 | TestCompletionCallback callback; |
| 6939 | |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 6940 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 6941 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6942 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6943 | |
| 6944 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 6945 | ASSERT_TRUE(response != NULL); |
| 6946 | ASSERT_TRUE(response->headers != NULL); |
| 6947 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 6948 | |
| 6949 | std::string response_data; |
| 6950 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 6951 | EXPECT_EQ("ok.", response_data); |
| 6952 | } |
| 6953 | |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 6954 | // This tests the case that a request is issued via http instead of spdy after |
| 6955 | // npn is negotiated. |
| 6956 | TEST_F(HttpNetworkTransactionTest, NpnWithHttpOverSSL) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6957 | HttpStreamFactory::set_use_alternate_protocols(true); |
| 6958 | HttpStreamFactory::set_next_protos("\x08http/1.1\x07http1.1"); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 6959 | SessionDependencies session_deps; |
| 6960 | HttpRequestInfo request; |
| 6961 | request.method = "GET"; |
| 6962 | request.url = GURL("https://www.google.com/"); |
| 6963 | request.load_flags = 0; |
| 6964 | |
| 6965 | MockWrite data_writes[] = { |
| 6966 | MockWrite("GET / HTTP/1.1\r\n" |
| 6967 | "Host: www.google.com\r\n" |
| 6968 | "Connection: keep-alive\r\n\r\n"), |
| 6969 | }; |
| 6970 | |
| 6971 | MockRead data_reads[] = { |
| 6972 | MockRead("HTTP/1.1 200 OK\r\n"), |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 6973 | MockRead(kAlternateProtocolHttpHeader), |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 6974 | MockRead("hello world"), |
| 6975 | MockRead(false, OK), |
| 6976 | }; |
| 6977 | |
| 6978 | SSLSocketDataProvider ssl(true, OK); |
| 6979 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
| 6980 | ssl.next_proto = "http/1.1"; |
| 6981 | |
| 6982 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 6983 | |
| 6984 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 6985 | data_writes, arraysize(data_writes)); |
| 6986 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 6987 | |
| 6988 | TestCompletionCallback callback; |
| 6989 | |
| 6990 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 6991 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 6992 | |
| 6993 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 6994 | |
| 6995 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6996 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6997 | |
| 6998 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 6999 | ASSERT_TRUE(response != NULL); |
| 7000 | ASSERT_TRUE(response->headers != NULL); |
| 7001 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 7002 | |
| 7003 | std::string response_data; |
| 7004 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 7005 | EXPECT_EQ("hello world", response_data); |
| 7006 | |
| 7007 | EXPECT_FALSE(response->was_fetched_via_spdy); |
| 7008 | EXPECT_TRUE(response->was_npn_negotiated); |
[email protected] | 193b0b89 | 2010-06-26 03:57:57 | [diff] [blame] | 7009 | EXPECT_FALSE(response->was_alternate_protocol_available); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 7010 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 7011 | HttpStreamFactory::set_next_protos(""); |
| 7012 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 7013 | } |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 7014 | |
| 7015 | TEST_F(HttpNetworkTransactionTest, SpdyPostNPNServerHangup) { |
| 7016 | // Simulate the SSL handshake completing with an NPN negotiation |
| 7017 | // followed by an immediate server closing of the socket. |
| 7018 | // Fix crash: http://crbug.com/46369 |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 7019 | HttpStreamFactory::set_use_alternate_protocols(true); |
| 7020 | HttpStreamFactory::set_next_protos(kExpectedNPNString); |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 7021 | SessionDependencies session_deps; |
| 7022 | |
| 7023 | HttpRequestInfo request; |
| 7024 | request.method = "GET"; |
| 7025 | request.url = GURL("https://www.google.com/"); |
| 7026 | request.load_flags = 0; |
| 7027 | |
| 7028 | SSLSocketDataProvider ssl(true, OK); |
| 7029 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 7030 | ssl.next_proto = "spdy/2"; |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 7031 | ssl.was_npn_negotiated = true; |
| 7032 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 7033 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 7034 | scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); |
[email protected] | e7f7509 | 2010-07-01 22:39:13 | [diff] [blame] | 7035 | MockWrite spdy_writes[] = { CreateMockWrite(*req) }; |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 7036 | |
| 7037 | MockRead spdy_reads[] = { |
| 7038 | MockRead(false, 0, 0) // Not async - return 0 immediately. |
| 7039 | }; |
| 7040 | |
| 7041 | scoped_refptr<DelayedSocketData> spdy_data( |
| 7042 | new DelayedSocketData( |
| 7043 | 0, // don't wait in this case, immediate hangup. |
| 7044 | spdy_reads, arraysize(spdy_reads), |
| 7045 | spdy_writes, arraysize(spdy_writes))); |
| 7046 | session_deps.socket_factory.AddSocketDataProvider(spdy_data); |
| 7047 | |
| 7048 | TestCompletionCallback callback; |
| 7049 | |
| 7050 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 7051 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 7052 | |
| 7053 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 7054 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7055 | EXPECT_EQ(ERR_CONNECTION_CLOSED, callback.WaitForResult()); |
| 7056 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 7057 | HttpStreamFactory::set_next_protos(""); |
| 7058 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 7059 | } |
[email protected] | 65d3438 | 2010-07-01 18:12:26 | [diff] [blame] | 7060 | |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 7061 | TEST_F(HttpNetworkTransactionTest, SpdyAlternateProtocolThroughProxy) { |
| 7062 | // This test ensures that the URL passed into the proxy is upgraded |
| 7063 | // to https when doing an Alternate Protocol upgrade. |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 7064 | HttpStreamFactory::set_use_alternate_protocols(true); |
| 7065 | HttpStreamFactory::set_next_protos( |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 7066 | "\x08http/1.1\x07http1.1\x06spdy/2\x04spdy"); |
| 7067 | |
| 7068 | SessionDependencies session_deps(CreateFixedProxyService("myproxy:70")); |
| 7069 | HttpAuthHandlerMock::Factory* auth_factory = |
| 7070 | new HttpAuthHandlerMock::Factory(); |
| 7071 | HttpAuthHandlerMock* auth_handler = new HttpAuthHandlerMock(); |
| 7072 | auth_factory->set_mock_handler(auth_handler, HttpAuth::AUTH_PROXY); |
| 7073 | auth_factory->set_do_init_from_challenge(true); |
| 7074 | session_deps.http_auth_handler_factory.reset(auth_factory); |
| 7075 | |
| 7076 | HttpRequestInfo request; |
| 7077 | request.method = "GET"; |
| 7078 | request.url = GURL("http://www.google.com"); |
| 7079 | request.load_flags = 0; |
| 7080 | |
| 7081 | // First round goes unauthenticated through the proxy. |
| 7082 | MockWrite data_writes_1[] = { |
| 7083 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 7084 | "Host: www.google.com\r\n" |
| 7085 | "Proxy-Connection: keep-alive\r\n" |
| 7086 | "\r\n"), |
| 7087 | }; |
| 7088 | MockRead data_reads_1[] = { |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 7089 | MockRead(false, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 7090 | MockRead("HTTP/1.1 200 OK\r\n" |
| 7091 | "Alternate-Protocol: 443:npn-spdy/2\r\n" |
| 7092 | "Proxy-Connection: close\r\n" |
| 7093 | "\r\n"), |
| 7094 | }; |
| 7095 | StaticSocketDataProvider data_1(data_reads_1, arraysize(data_reads_1), |
| 7096 | data_writes_1, arraysize(data_writes_1)); |
| 7097 | |
| 7098 | // Second round tries to tunnel to www.google.com due to the |
| 7099 | // Alternate-Protocol announcement in the first round. It fails due |
| 7100 | // to a proxy authentication challenge. |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 7101 | // After the failure, a tunnel is established to www.google.com using |
| 7102 | // Proxy-Authorization headers. There is then a SPDY request round. |
| 7103 | // |
| 7104 | // NOTE: Despite the "Proxy-Connection: Close", these are done on the |
| 7105 | // same MockTCPClientSocket since the underlying HttpNetworkClientSocket |
| 7106 | // does a Disconnect and Connect on the same socket, rather than trying |
| 7107 | // to obtain a new one. |
| 7108 | // |
| 7109 | // NOTE: Originally, the proxy response to the second CONNECT request |
| 7110 | // simply returned another 407 so the unit test could skip the SSL connection |
| 7111 | // establishment and SPDY framing issues. Alas, the |
| 7112 | // retry-http-when-alternate-protocol fails logic kicks in, which was more |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 7113 | // complicated to set up expectations for than the SPDY session. |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 7114 | |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 7115 | scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); |
| 7116 | scoped_ptr<spdy::SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 7117 | scoped_ptr<spdy::SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); |
| 7118 | |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 7119 | MockWrite data_writes_2[] = { |
| 7120 | // First connection attempt without Proxy-Authorization. |
| 7121 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 7122 | "Host: www.google.com\r\n" |
| 7123 | "Proxy-Connection: keep-alive\r\n" |
| 7124 | "\r\n"), |
| 7125 | |
| 7126 | // Second connection attempt with Proxy-Authorization. |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 7127 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 7128 | "Host: www.google.com\r\n" |
| 7129 | "Proxy-Connection: keep-alive\r\n" |
| 7130 | "Proxy-Authorization: auth_token\r\n" |
| 7131 | "\r\n"), |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 7132 | |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 7133 | // SPDY request |
| 7134 | CreateMockWrite(*req), |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 7135 | }; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 7136 | const char kRejectConnectResponse[] = ("HTTP/1.1 407 Unauthorized\r\n" |
| 7137 | "Proxy-Authenticate: Mock\r\n" |
| 7138 | "Proxy-Connection: close\r\n" |
| 7139 | "\r\n"); |
| 7140 | const char kAcceptConnectResponse[] = "HTTP/1.1 200 Connected\r\n\r\n"; |
| 7141 | MockRead data_reads_2[] = { |
| 7142 | // First connection attempt fails |
| 7143 | MockRead(false, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ, 1), |
| 7144 | MockRead(true, kRejectConnectResponse, |
| 7145 | arraysize(kRejectConnectResponse) - 1, 1), |
| 7146 | |
| 7147 | // Second connection attempt passes |
| 7148 | MockRead(true, kAcceptConnectResponse, |
| 7149 | arraysize(kAcceptConnectResponse) -1, 4), |
| 7150 | |
| 7151 | // SPDY response |
| 7152 | CreateMockRead(*resp.get(), 6), |
| 7153 | CreateMockRead(*data.get(), 6), |
| 7154 | MockRead(true, 0, 0, 6), |
| 7155 | }; |
| 7156 | scoped_refptr<OrderedSocketData> data_2( |
| 7157 | new OrderedSocketData(data_reads_2, arraysize(data_reads_2), |
| 7158 | data_writes_2, arraysize(data_writes_2))); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 7159 | |
| 7160 | SSLSocketDataProvider ssl(true, OK); |
| 7161 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
| 7162 | ssl.next_proto = "spdy/2"; |
| 7163 | ssl.was_npn_negotiated = true; |
| 7164 | |
| 7165 | session_deps.socket_factory.AddSocketDataProvider(&data_1); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 7166 | session_deps.socket_factory.AddSocketDataProvider(data_2.get()); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 7167 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 7168 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 7169 | |
| 7170 | // First round should work and provide the Alternate-Protocol state. |
| 7171 | TestCompletionCallback callback_1; |
| 7172 | scoped_ptr<HttpTransaction> trans_1(new HttpNetworkTransaction(session)); |
| 7173 | int rv = trans_1->Start(&request, &callback_1, BoundNetLog()); |
| 7174 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7175 | EXPECT_EQ(OK, callback_1.WaitForResult()); |
| 7176 | |
| 7177 | // Second round should attempt a tunnel connect and get an auth challenge. |
| 7178 | TestCompletionCallback callback_2; |
| 7179 | scoped_ptr<HttpTransaction> trans_2(new HttpNetworkTransaction(session)); |
| 7180 | rv = trans_2->Start(&request, &callback_2, BoundNetLog()); |
| 7181 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7182 | EXPECT_EQ(OK, callback_2.WaitForResult()); |
| 7183 | const HttpResponseInfo* response = trans_2->GetResponseInfo(); |
| 7184 | ASSERT_FALSE(response == NULL); |
| 7185 | ASSERT_FALSE(response->auth_challenge.get() == NULL); |
| 7186 | |
| 7187 | // Restart with auth. Tunnel should work and response received. |
| 7188 | TestCompletionCallback callback_3; |
| 7189 | rv = trans_2->RestartWithAuth(kFoo, kBar, &callback_3); |
| 7190 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7191 | EXPECT_EQ(OK, callback_3.WaitForResult()); |
| 7192 | |
| 7193 | // After all that work, these two lines (or actually, just the scheme) are |
| 7194 | // what this test is all about. Make sure it happens correctly. |
| 7195 | const GURL& request_url = auth_handler->request_url(); |
| 7196 | EXPECT_EQ("https", request_url.scheme()); |
| 7197 | EXPECT_EQ("www.google.com", request_url.host()); |
| 7198 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 7199 | HttpStreamFactory::set_next_protos(""); |
| 7200 | HttpStreamFactory::set_use_alternate_protocols(false); |
| 7201 | } |
| 7202 | |
| 7203 | // Test that if we cancel the transaction as the connection is completing, that |
| 7204 | // everything tears down correctly. |
| 7205 | TEST_F(HttpNetworkTransactionTest, SimpleCancel) { |
| 7206 | // Setup everything about the connection to complete synchronously, so that |
| 7207 | // after calling HttpNetworkTransaction::Start, the only thing we're waiting |
| 7208 | // for is the callback from the HttpStreamRequest. |
| 7209 | // Then cancel the transaction. |
| 7210 | // Verify that we don't crash. |
| 7211 | MockConnect mock_connect(false, OK); |
| 7212 | MockRead data_reads[] = { |
| 7213 | MockRead(false, "HTTP/1.0 200 OK\r\n\r\n"), |
| 7214 | MockRead(false, "hello world"), |
| 7215 | MockRead(false, OK), |
| 7216 | }; |
| 7217 | |
| 7218 | SessionDependencies session_deps; |
| 7219 | session_deps.host_resolver->set_synchronous_mode(true); |
| 7220 | scoped_ptr<HttpTransaction> trans( |
| 7221 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 7222 | |
| 7223 | HttpRequestInfo request; |
| 7224 | request.method = "GET"; |
| 7225 | request.url = GURL("http://www.google.com/"); |
| 7226 | request.load_flags = 0; |
| 7227 | |
| 7228 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
| 7229 | data.set_connect_data(mock_connect); |
| 7230 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 7231 | |
| 7232 | TestCompletionCallback callback; |
| 7233 | |
| 7234 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 7235 | int rv = trans->Start(&request, &callback, log.bound()); |
| 7236 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7237 | trans.reset(); // Cancel the transaction here. |
| 7238 | |
| 7239 | MessageLoop::current()->RunAllPending(); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 7240 | } |
| 7241 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 7242 | // Test a basic GET request through a proxy. |
| 7243 | TEST_F(HttpNetworkTransactionTest, ProxyGet) { |
| 7244 | SessionDependencies session_deps(CreateFixedProxyService("myproxy:70")); |
| 7245 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 7246 | session_deps.net_log = log.bound().net_log(); |
| 7247 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 7248 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 7249 | HttpRequestInfo request; |
| 7250 | request.method = "GET"; |
| 7251 | request.url = GURL("http://www.google.com/"); |
| 7252 | |
| 7253 | MockWrite data_writes1[] = { |
| 7254 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 7255 | "Host: www.google.com\r\n" |
| 7256 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 7257 | }; |
| 7258 | |
| 7259 | MockRead data_reads1[] = { |
| 7260 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 7261 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 7262 | MockRead("Content-Length: 100\r\n\r\n"), |
| 7263 | MockRead(false, OK), |
| 7264 | }; |
| 7265 | |
| 7266 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 7267 | data_writes1, arraysize(data_writes1)); |
| 7268 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 7269 | |
| 7270 | TestCompletionCallback callback1; |
| 7271 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 7272 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 7273 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 7274 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 7275 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7276 | |
| 7277 | rv = callback1.WaitForResult(); |
| 7278 | EXPECT_EQ(OK, rv); |
| 7279 | |
| 7280 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 7281 | ASSERT_FALSE(response == NULL); |
| 7282 | |
| 7283 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 7284 | EXPECT_EQ(200, response->headers->response_code()); |
| 7285 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 7286 | EXPECT_TRUE(response->was_fetched_via_proxy); |
| 7287 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 7288 | } |
| 7289 | |
| 7290 | // Test a basic HTTPS GET request through a proxy. |
| 7291 | TEST_F(HttpNetworkTransactionTest, ProxyTunnelGet) { |
| 7292 | SessionDependencies session_deps(CreateFixedProxyService("myproxy:70")); |
| 7293 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 7294 | session_deps.net_log = log.bound().net_log(); |
| 7295 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 7296 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 7297 | HttpRequestInfo request; |
| 7298 | request.method = "GET"; |
| 7299 | request.url = GURL("https://www.google.com/"); |
| 7300 | |
| 7301 | // Since we have proxy, should try to establish tunnel. |
| 7302 | MockWrite data_writes1[] = { |
| 7303 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 7304 | "Host: www.google.com\r\n" |
| 7305 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 7306 | |
| 7307 | MockWrite("GET / HTTP/1.1\r\n" |
| 7308 | "Host: www.google.com\r\n" |
| 7309 | "Connection: keep-alive\r\n\r\n"), |
| 7310 | }; |
| 7311 | |
| 7312 | MockRead data_reads1[] = { |
| 7313 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 7314 | |
| 7315 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 7316 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 7317 | MockRead("Content-Length: 100\r\n\r\n"), |
| 7318 | MockRead(false, OK), |
| 7319 | }; |
| 7320 | |
| 7321 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 7322 | data_writes1, arraysize(data_writes1)); |
| 7323 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 7324 | SSLSocketDataProvider ssl(true, OK); |
| 7325 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 7326 | |
| 7327 | TestCompletionCallback callback1; |
| 7328 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 7329 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 7330 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 7331 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 7332 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7333 | |
| 7334 | rv = callback1.WaitForResult(); |
| 7335 | EXPECT_EQ(OK, rv); |
| 7336 | size_t pos = ExpectLogContainsSomewhere( |
| 7337 | log.entries(), 0, NetLog::TYPE_HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
| 7338 | NetLog::PHASE_NONE); |
| 7339 | ExpectLogContainsSomewhere( |
| 7340 | log.entries(), pos, |
| 7341 | NetLog::TYPE_HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 7342 | NetLog::PHASE_NONE); |
| 7343 | |
| 7344 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 7345 | ASSERT_FALSE(response == NULL); |
| 7346 | |
| 7347 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 7348 | EXPECT_EQ(200, response->headers->response_code()); |
| 7349 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 7350 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 7351 | EXPECT_TRUE(response->was_fetched_via_proxy); |
| 7352 | } |
| 7353 | |
| 7354 | // Test a basic HTTPS GET request through a proxy, but the server hangs up |
| 7355 | // while establishing the tunnel. |
| 7356 | TEST_F(HttpNetworkTransactionTest, ProxyTunnelGetHangup) { |
| 7357 | SessionDependencies session_deps(CreateFixedProxyService("myproxy:70")); |
| 7358 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 7359 | session_deps.net_log = log.bound().net_log(); |
| 7360 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 7361 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 7362 | HttpRequestInfo request; |
| 7363 | request.method = "GET"; |
| 7364 | request.url = GURL("https://www.google.com/"); |
| 7365 | |
| 7366 | // Since we have proxy, should try to establish tunnel. |
| 7367 | MockWrite data_writes1[] = { |
| 7368 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 7369 | "Host: www.google.com\r\n" |
| 7370 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 7371 | |
| 7372 | MockWrite("GET / HTTP/1.1\r\n" |
| 7373 | "Host: www.google.com\r\n" |
| 7374 | "Connection: keep-alive\r\n\r\n"), |
| 7375 | }; |
| 7376 | |
| 7377 | MockRead data_reads1[] = { |
| 7378 | MockRead(false, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| 7379 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 7380 | MockRead(true, 0, 0), // EOF |
| 7381 | }; |
| 7382 | |
| 7383 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 7384 | data_writes1, arraysize(data_writes1)); |
| 7385 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 7386 | SSLSocketDataProvider ssl(true, OK); |
| 7387 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 7388 | |
| 7389 | TestCompletionCallback callback1; |
| 7390 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 7391 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 7392 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 7393 | int rv = trans->Start(&request, &callback1, log.bound()); |
| 7394 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7395 | |
| 7396 | rv = callback1.WaitForResult(); |
| 7397 | EXPECT_EQ(ERR_EMPTY_RESPONSE, rv); |
| 7398 | size_t pos = ExpectLogContainsSomewhere( |
| 7399 | log.entries(), 0, NetLog::TYPE_HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
| 7400 | NetLog::PHASE_NONE); |
| 7401 | ExpectLogContainsSomewhere( |
| 7402 | log.entries(), pos, |
| 7403 | NetLog::TYPE_HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 7404 | NetLog::PHASE_NONE); |
| 7405 | } |
| 7406 | |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 7407 | // Test for crbug.com/55424. |
| 7408 | TEST_F(HttpNetworkTransactionTest, PreconnectWithExistingSpdySession) { |
| 7409 | SessionDependencies session_deps; |
| 7410 | |
| 7411 | scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet( |
| 7412 | "https://www.google.com", false, 1, LOWEST)); |
| 7413 | MockWrite spdy_writes[] = { CreateMockWrite(*req) }; |
| 7414 | |
| 7415 | scoped_ptr<spdy::SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 7416 | scoped_ptr<spdy::SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); |
| 7417 | MockRead spdy_reads[] = { |
| 7418 | CreateMockRead(*resp), |
| 7419 | CreateMockRead(*data), |
| 7420 | MockRead(true, 0, 0), |
| 7421 | }; |
| 7422 | |
| 7423 | scoped_refptr<DelayedSocketData> spdy_data( |
| 7424 | new DelayedSocketData( |
| 7425 | 1, // wait for one write to finish before reading. |
| 7426 | spdy_reads, arraysize(spdy_reads), |
| 7427 | spdy_writes, arraysize(spdy_writes))); |
| 7428 | session_deps.socket_factory.AddSocketDataProvider(spdy_data); |
| 7429 | |
| 7430 | SSLSocketDataProvider ssl(true, OK); |
| 7431 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
| 7432 | ssl.next_proto = "spdy/2"; |
| 7433 | ssl.was_npn_negotiated = true; |
| 7434 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 7435 | |
| 7436 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 7437 | |
| 7438 | // Set up an initial SpdySession in the pool to reuse. |
[email protected] | 02b0c34 | 2010-09-25 21:09:38 | [diff] [blame] | 7439 | HostPortPair host_port_pair("www.google.com", 443); |
| 7440 | HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 7441 | scoped_refptr<SpdySession> spdy_session = |
[email protected] | f458033 | 2010-09-25 21:20:27 | [diff] [blame] | 7442 | session->spdy_session_pool()->Get(pair, session->mutable_spdy_settings(), |
| 7443 | BoundNetLog()); |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 7444 | scoped_refptr<TCPSocketParams> tcp_params = |
| 7445 | new TCPSocketParams("www.google.com", 443, MEDIUM, GURL(), false); |
[email protected] | 02b0c34 | 2010-09-25 21:09:38 | [diff] [blame] | 7446 | TestCompletionCallback callback; |
| 7447 | |
| 7448 | scoped_ptr<ClientSocketHandle> connection(new ClientSocketHandle); |
| 7449 | EXPECT_EQ(ERR_IO_PENDING, |
| 7450 | connection->Init(host_port_pair.ToString(), tcp_params, LOWEST, |
| 7451 | &callback, session->tcp_socket_pool(), |
| 7452 | BoundNetLog())); |
| 7453 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 7454 | spdy_session->InitializeWithSocket(connection.release(), false, OK); |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 7455 | |
| 7456 | HttpRequestInfo request; |
| 7457 | request.method = "GET"; |
| 7458 | request.url = GURL("https://www.google.com/"); |
| 7459 | request.load_flags = 0; |
| 7460 | |
| 7461 | // This is the important line that marks this as a preconnect. |
| 7462 | request.motivation = HttpRequestInfo::PRECONNECT_MOTIVATED; |
| 7463 | |
| 7464 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 7465 | |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 7466 | int rv = trans->Start(&request, &callback, BoundNetLog()); |
| 7467 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7468 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 7469 | } |
| 7470 | |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 7471 | } // namespace net |