[email protected] | eb6234e | 2012-01-19 01:50:02 | [diff] [blame] | 1 | // Copyright (c) 2012 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] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 8 | #include <stdarg.h> |
| 9 | #include <string> |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 10 | #include <vector> |
[email protected] | 77848d1 | 2008-11-14 00:00:22 | [diff] [blame] | 11 | |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 12 | #include "base/basictypes.h" |
[email protected] | 68bf915 | 2008-09-25 19:47:30 | [diff] [blame] | 13 | #include "base/compiler_specific.h" |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 14 | #include "base/file_path.h" |
| 15 | #include "base/file_util.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 16 | #include "base/memory/scoped_ptr.h" |
[email protected] | 5e6efa5 | 2011-06-27 17:26:41 | [diff] [blame] | 17 | #include "base/metrics/histogram.h" |
[email protected] | f36a813 | 2011-09-02 18:36:33 | [diff] [blame] | 18 | #include "base/test/test_file_util.h" |
[email protected] | be1ce6a7 | 2010-08-03 14:35:22 | [diff] [blame] | 19 | #include "base/utf_string_conversions.h" |
[email protected] | 277d594 | 2010-08-11 21:02:35 | [diff] [blame] | 20 | #include "net/base/auth.h" |
[email protected] | 169d001 | 2010-05-10 23:20:12 | [diff] [blame] | 21 | #include "net/base/capturing_net_log.h" |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 22 | #include "net/base/completion_callback.h" |
[email protected] | 98e1cd01 | 2011-11-08 15:33:09 | [diff] [blame] | 23 | #include "net/base/host_cache.h" |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 24 | #include "net/base/mock_host_resolver.h" |
[email protected] | 169d001 | 2010-05-10 23:20:12 | [diff] [blame] | 25 | #include "net/base/net_log.h" |
| 26 | #include "net/base/net_log_unittest.h" |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 27 | #include "net/base/request_priority.h" |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 28 | #include "net/base/ssl_cert_request_info.h" |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 29 | #include "net/base/ssl_config_service_defaults.h" |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 30 | #include "net/base/ssl_info.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 31 | #include "net/base/test_completion_callback.h" |
| 32 | #include "net/base/upload_data.h" |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 33 | #include "net/http/http_auth_handler_digest.h" |
[email protected] | 3fd9dae | 2010-06-21 11:39:00 | [diff] [blame] | 34 | #include "net/http/http_auth_handler_mock.h" |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 35 | #include "net/http/http_auth_handler_ntlm.h" |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 36 | #include "net/http/http_basic_stream.h" |
[email protected] | 3deb9a5 | 2010-11-11 00:24:40 | [diff] [blame] | 37 | #include "net/http/http_net_log_params.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 38 | #include "net/http/http_network_session.h" |
[email protected] | 87bfa3f | 2010-09-30 14:54:56 | [diff] [blame] | 39 | #include "net/http/http_network_session_peer.h" |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 40 | #include "net/http/http_server_properties_impl.h" |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 41 | #include "net/http/http_stream.h" |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 42 | #include "net/http/http_stream_factory.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 43 | #include "net/http/http_transaction_unittest.h" |
[email protected] | 51fff29d | 2008-12-19 22:17:53 | [diff] [blame] | 44 | #include "net/proxy/proxy_config_service_fixed.h" |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 45 | #include "net/proxy/proxy_resolver.h" |
| 46 | #include "net/proxy/proxy_service.h" |
[email protected] | f7984fc6 | 2009-06-22 23:26:44 | [diff] [blame] | 47 | #include "net/socket/client_socket_factory.h" |
[email protected] | a42dbd14 | 2011-11-17 16:42:02 | [diff] [blame] | 48 | #include "net/socket/mock_client_socket_pool_manager.h" |
[email protected] | f7984fc6 | 2009-06-22 23:26:44 | [diff] [blame] | 49 | #include "net/socket/socket_test_util.h" |
| 50 | #include "net/socket/ssl_client_socket.h" |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 51 | #include "net/spdy/spdy_framer.h" |
| 52 | #include "net/spdy/spdy_session.h" |
| 53 | #include "net/spdy/spdy_session_pool.h" |
| 54 | #include "net/spdy/spdy_test_util.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 55 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 56 | #include "testing/platform_test.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 57 | |
| 58 | //----------------------------------------------------------------------------- |
| 59 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 60 | namespace { |
| 61 | |
| 62 | const string16 kBar(ASCIIToUTF16("bar")); |
| 63 | const string16 kBar2(ASCIIToUTF16("bar2")); |
| 64 | const string16 kBar3(ASCIIToUTF16("bar3")); |
| 65 | const string16 kBaz(ASCIIToUTF16("baz")); |
| 66 | const string16 kFirst(ASCIIToUTF16("first")); |
| 67 | const string16 kFoo(ASCIIToUTF16("foo")); |
| 68 | const string16 kFoo2(ASCIIToUTF16("foo2")); |
| 69 | const string16 kFoo3(ASCIIToUTF16("foo3")); |
| 70 | const string16 kFou(ASCIIToUTF16("fou")); |
| 71 | const string16 kSecond(ASCIIToUTF16("second")); |
| 72 | const string16 kTestingNTLM(ASCIIToUTF16("testing-ntlm")); |
| 73 | const string16 kWrongPassword(ASCIIToUTF16("wrongpassword")); |
| 74 | |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 75 | // MakeNextProtos is a utility function that returns a vector of std::strings |
| 76 | // from its arguments. Don't forget to terminate the argument list with a NULL. |
| 77 | std::vector<std::string> MakeNextProtos(const char* a, ...) { |
| 78 | std::vector<std::string> ret; |
| 79 | ret.push_back(a); |
| 80 | |
| 81 | va_list args; |
| 82 | va_start(args, a); |
| 83 | |
| 84 | for (;;) { |
| 85 | const char* value = va_arg(args, const char*); |
| 86 | if (value == NULL) |
| 87 | break; |
| 88 | ret.push_back(value); |
| 89 | } |
| 90 | va_end(args); |
| 91 | |
| 92 | return ret; |
| 93 | } |
| 94 | |
| 95 | // SpdyNextProtos returns a vector of NPN protocol strings for negotiating |
| 96 | // SPDY. |
| 97 | std::vector<std::string> SpdyNextProtos() { |
[email protected] | 42baef7a | 2011-12-10 04:52:10 | [diff] [blame] | 98 | return MakeNextProtos("http/1.1", "spdy/2", "spdy/2.1", NULL); |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 99 | } |
| 100 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 101 | } // namespace |
| 102 | |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 103 | namespace net { |
| 104 | |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 105 | // Helper to manage the lifetimes of the dependencies for a |
| 106 | // HttpNetworkTransaction. |
[email protected] | ac03952 | 2010-06-15 16:39:44 | [diff] [blame] | 107 | struct SessionDependencies { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 108 | // Default set of dependencies -- "null" proxy service. |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 109 | SessionDependencies() |
| 110 | : host_resolver(new MockHostResolver), |
[email protected] | 822581d | 2010-12-16 17:27:15 | [diff] [blame] | 111 | cert_verifier(new CertVerifier), |
[email protected] | ebeefff3 | 2010-09-15 05:10:02 | [diff] [blame] | 112 | proxy_service(ProxyService::CreateDirect()), |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 113 | ssl_config_service(new SSLConfigServiceDefaults), |
[email protected] | f660e4b | 2010-09-29 14:20:08 | [diff] [blame] | 114 | http_auth_handler_factory( |
[email protected] | 73c4532 | 2010-10-01 23:57:54 | [diff] [blame] | 115 | HttpAuthHandlerFactory::CreateDefault(host_resolver.get())), |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 116 | net_log(NULL) {} |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 117 | |
| 118 | // Custom proxy service dependency. |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 119 | explicit SessionDependencies(ProxyService* proxy_service) |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 120 | : host_resolver(new MockHostResolver), |
[email protected] | 822581d | 2010-12-16 17:27:15 | [diff] [blame] | 121 | cert_verifier(new CertVerifier), |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 122 | proxy_service(proxy_service), |
[email protected] | d1eda93 | 2009-11-04 01:03:10 | [diff] [blame] | 123 | ssl_config_service(new SSLConfigServiceDefaults), |
[email protected] | f660e4b | 2010-09-29 14:20:08 | [diff] [blame] | 124 | http_auth_handler_factory( |
[email protected] | 73c4532 | 2010-10-01 23:57:54 | [diff] [blame] | 125 | HttpAuthHandlerFactory::CreateDefault(host_resolver.get())), |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 126 | net_log(NULL) {} |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 127 | |
[email protected] | 73c4532 | 2010-10-01 23:57:54 | [diff] [blame] | 128 | scoped_ptr<MockHostResolverBase> host_resolver; |
[email protected] | 822581d | 2010-12-16 17:27:15 | [diff] [blame] | 129 | scoped_ptr<CertVerifier> cert_verifier; |
[email protected] | 6104ea5d | 2011-04-27 21:37:12 | [diff] [blame] | 130 | scoped_ptr<ProxyService> proxy_service; |
[email protected] | db36938c | 2009-08-19 21:48:42 | [diff] [blame] | 131 | scoped_refptr<SSLConfigService> ssl_config_service; |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 132 | MockClientSocketFactory socket_factory; |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 133 | scoped_ptr<HttpAuthHandlerFactory> http_auth_handler_factory; |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 134 | HttpServerPropertiesImpl http_server_properties; |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 135 | NetLog* net_log; |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 136 | }; |
| 137 | |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 138 | HttpNetworkSession* CreateSession(SessionDependencies* session_deps) { |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 139 | net::HttpNetworkSession::Params params; |
| 140 | params.client_socket_factory = &session_deps->socket_factory; |
| 141 | params.host_resolver = session_deps->host_resolver.get(); |
| 142 | params.cert_verifier = session_deps->cert_verifier.get(); |
[email protected] | 6104ea5d | 2011-04-27 21:37:12 | [diff] [blame] | 143 | params.proxy_service = session_deps->proxy_service.get(); |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 144 | params.ssl_config_service = session_deps->ssl_config_service; |
| 145 | params.http_auth_handler_factory = |
| 146 | session_deps->http_auth_handler_factory.get(); |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 147 | params.http_server_properties = &session_deps->http_server_properties; |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 148 | params.net_log = session_deps->net_log; |
| 149 | return new HttpNetworkSession(params); |
[email protected] | e8d53619 | 2008-10-17 22:21:14 | [diff] [blame] | 150 | } |
| 151 | |
[email protected] | 89836e2 | 2008-09-25 20:33:42 | [diff] [blame] | 152 | class HttpNetworkTransactionTest : public PlatformTest { |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 153 | protected: |
| 154 | struct SimpleGetHelperResult { |
| 155 | int rv; |
| 156 | std::string status_line; |
| 157 | std::string response_data; |
| 158 | }; |
| 159 | |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 160 | virtual void SetUp() { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 161 | NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
| 162 | MessageLoop::current()->RunAllPending(); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 163 | spdy::SpdyFramer::set_enable_compression_default(false); |
| 164 | } |
| 165 | |
[email protected] | 0e75a73 | 2008-10-16 20:36:09 | [diff] [blame] | 166 | virtual void TearDown() { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 167 | NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
| 168 | MessageLoop::current()->RunAllPending(); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 169 | spdy::SpdyFramer::set_enable_compression_default(true); |
[email protected] | 0e75a73 | 2008-10-16 20:36:09 | [diff] [blame] | 170 | // Empty the current queue. |
| 171 | MessageLoop::current()->RunAllPending(); |
| 172 | PlatformTest::TearDown(); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 173 | NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
| 174 | MessageLoop::current()->RunAllPending(); |
[email protected] | 0e75a73 | 2008-10-16 20:36:09 | [diff] [blame] | 175 | } |
| 176 | |
[email protected] | 20296599 | 2011-12-07 23:04:51 | [diff] [blame] | 177 | // Either |write_failure| specifies a write failure or |read_failure| |
| 178 | // specifies a read failure when using a reused socket. In either case, the |
| 179 | // failure should cause the network transaction to resend the request, and the |
| 180 | // other argument should be NULL. |
| 181 | void KeepAliveConnectionResendRequestTest(const MockWrite* write_failure, |
| 182 | const MockRead* read_failure); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 183 | |
[email protected] | 5a60c8b | 2011-10-19 20:14:29 | [diff] [blame] | 184 | SimpleGetHelperResult SimpleGetHelperForData(StaticSocketDataProvider* data[], |
| 185 | size_t data_count) { |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 186 | SimpleGetHelperResult out; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 187 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 188 | HttpRequestInfo request; |
| 189 | request.method = "GET"; |
| 190 | request.url = GURL("http://www.google.com/"); |
| 191 | request.load_flags = 0; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 192 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 193 | SessionDependencies session_deps; |
| 194 | scoped_ptr<HttpTransaction> trans( |
| 195 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 196 | |
[email protected] | 5a60c8b | 2011-10-19 20:14:29 | [diff] [blame] | 197 | for (size_t i = 0; i < data_count; ++i) { |
| 198 | session_deps.socket_factory.AddSocketDataProvider(data[i]); |
| 199 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 200 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 201 | TestCompletionCallback callback; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 202 | |
[email protected] | 169d001 | 2010-05-10 23:20:12 | [diff] [blame] | 203 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
[email protected] | 3deb9a5 | 2010-11-11 00:24:40 | [diff] [blame] | 204 | EXPECT_TRUE(log.bound().IsLoggingAllEvents()); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 205 | int rv = trans->Start(&request, callback.callback(), log.bound()); |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 206 | EXPECT_EQ(ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 207 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 208 | out.rv = callback.WaitForResult(); |
| 209 | if (out.rv != OK) |
| 210 | return out; |
| 211 | |
| 212 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 213 | // Can't use ASSERT_* inside helper functions like this, so |
| 214 | // return an error. |
| 215 | if (response == NULL || response->headers == NULL) { |
| 216 | out.rv = ERR_UNEXPECTED; |
| 217 | return out; |
| 218 | } |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 219 | out.status_line = response->headers->GetStatusLine(); |
| 220 | |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 221 | EXPECT_EQ("192.0.2.33", response->socket_address.host()); |
| 222 | EXPECT_EQ(0, response->socket_address.port()); |
| 223 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 224 | rv = ReadTransaction(trans.get(), &out.response_data); |
| 225 | EXPECT_EQ(OK, rv); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 226 | |
| 227 | net::CapturingNetLog::EntryList entries; |
| 228 | log.GetEntries(&entries); |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 229 | size_t pos = ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 230 | entries, 0, NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST_HEADERS, |
[email protected] | 169d001 | 2010-05-10 23:20:12 | [diff] [blame] | 231 | NetLog::PHASE_NONE); |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 232 | ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 233 | entries, pos, |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 234 | NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS, |
| 235 | NetLog::PHASE_NONE); |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 236 | |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 237 | CapturingNetLog::Entry entry = entries[pos]; |
[email protected] | 3deb9a5 | 2010-11-11 00:24:40 | [diff] [blame] | 238 | NetLogHttpRequestParameter* request_params = |
| 239 | static_cast<NetLogHttpRequestParameter*>(entry.extra_parameters.get()); |
| 240 | EXPECT_EQ("GET / HTTP/1.1\r\n", request_params->GetLine()); |
| 241 | EXPECT_EQ("Host: www.google.com\r\n" |
| 242 | "Connection: keep-alive\r\n\r\n", |
| 243 | request_params->GetHeaders().ToString()); |
| 244 | |
[email protected] | aecfbf2 | 2008-10-16 02:02:47 | [diff] [blame] | 245 | return out; |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 246 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 247 | |
[email protected] | 5a60c8b | 2011-10-19 20:14:29 | [diff] [blame] | 248 | SimpleGetHelperResult SimpleGetHelper(MockRead data_reads[], |
| 249 | size_t reads_count) { |
| 250 | StaticSocketDataProvider reads(data_reads, reads_count, NULL, 0); |
| 251 | StaticSocketDataProvider* data[] = { &reads }; |
| 252 | return SimpleGetHelperForData(data, 1); |
| 253 | } |
| 254 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 255 | void ConnectStatusHelperWithExpectedStatus(const MockRead& status, |
| 256 | int expected_status); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 257 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 258 | void ConnectStatusHelper(const MockRead& status); |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 259 | }; |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 260 | |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 261 | // Fill |str| with a long header list that consumes >= |size| bytes. |
| 262 | void FillLargeHeadersString(std::string* str, int size) { |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 263 | const char* row = |
| 264 | "SomeHeaderName: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n"; |
| 265 | const int sizeof_row = strlen(row); |
| 266 | const int num_rows = static_cast<int>( |
| 267 | ceil(static_cast<float>(size) / sizeof_row)); |
| 268 | const int sizeof_data = num_rows * sizeof_row; |
| 269 | DCHECK(sizeof_data >= size); |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 270 | str->reserve(sizeof_data); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 271 | |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 272 | for (int i = 0; i < num_rows; ++i) |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 273 | str->append(row, sizeof_row); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 274 | } |
| 275 | |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 276 | // Alternative functions that eliminate randomness and dependency on the local |
| 277 | // host name so that the generated NTLM messages are reproducible. |
[email protected] | fe2bc6a | 2009-03-23 16:52:20 | [diff] [blame] | 278 | void MockGenerateRandom1(uint8* output, size_t n) { |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 279 | static const uint8 bytes[] = { |
| 280 | 0x55, 0x29, 0x66, 0x26, 0x6b, 0x9c, 0x73, 0x54 |
| 281 | }; |
| 282 | static size_t current_byte = 0; |
| 283 | for (size_t i = 0; i < n; ++i) { |
| 284 | output[i] = bytes[current_byte++]; |
| 285 | current_byte %= arraysize(bytes); |
| 286 | } |
| 287 | } |
| 288 | |
[email protected] | fe2bc6a | 2009-03-23 16:52:20 | [diff] [blame] | 289 | void MockGenerateRandom2(uint8* output, size_t n) { |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 290 | static const uint8 bytes[] = { |
| 291 | 0x96, 0x79, 0x85, 0xe7, 0x49, 0x93, 0x70, 0xa1, |
| 292 | 0x4e, 0xe7, 0x87, 0x45, 0x31, 0x5b, 0xd3, 0x1f |
| 293 | }; |
| 294 | static size_t current_byte = 0; |
| 295 | for (size_t i = 0; i < n; ++i) { |
| 296 | output[i] = bytes[current_byte++]; |
| 297 | current_byte %= arraysize(bytes); |
| 298 | } |
| 299 | } |
| 300 | |
[email protected] | fe2bc6a | 2009-03-23 16:52:20 | [diff] [blame] | 301 | std::string MockGetHostName() { |
| 302 | return "WTC-WIN7"; |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 303 | } |
| 304 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 305 | template<typename ParentPool> |
| 306 | class CaptureGroupNameSocketPool : public ParentPool { |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 307 | public: |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 308 | CaptureGroupNameSocketPool(HostResolver* host_resolver, |
| 309 | CertVerifier* cert_verifier); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 310 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 311 | const std::string last_group_name_received() const { |
| 312 | return last_group_name_; |
| 313 | } |
| 314 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 315 | virtual int RequestSocket(const std::string& group_name, |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 316 | const void* socket_params, |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 317 | RequestPriority priority, |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 318 | ClientSocketHandle* handle, |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 319 | const CompletionCallback& callback, |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 320 | const BoundNetLog& net_log) { |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 321 | last_group_name_ = group_name; |
| 322 | return ERR_IO_PENDING; |
| 323 | } |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 324 | virtual void CancelRequest(const std::string& group_name, |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 325 | ClientSocketHandle* handle) {} |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 326 | virtual void ReleaseSocket(const std::string& group_name, |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 327 | StreamSocket* socket, |
[email protected] | 9f95c69 | 2011-02-11 19:20:19 | [diff] [blame] | 328 | int id) {} |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 329 | virtual void CloseIdleSockets() {} |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 330 | virtual int IdleSocketCount() const { |
| 331 | return 0; |
| 332 | } |
| 333 | virtual int IdleSocketCountInGroup(const std::string& group_name) const { |
| 334 | return 0; |
| 335 | } |
| 336 | virtual LoadState GetLoadState(const std::string& group_name, |
| 337 | const ClientSocketHandle* handle) const { |
| 338 | return LOAD_STATE_IDLE; |
| 339 | } |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 340 | virtual base::TimeDelta ConnectionTimeout() const { |
| 341 | return base::TimeDelta(); |
| 342 | } |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 343 | |
| 344 | private: |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 345 | std::string last_group_name_; |
| 346 | }; |
| 347 | |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 348 | typedef CaptureGroupNameSocketPool<TransportClientSocketPool> |
| 349 | CaptureGroupNameTransportSocketPool; |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 350 | typedef CaptureGroupNameSocketPool<HttpProxyClientSocketPool> |
| 351 | CaptureGroupNameHttpProxySocketPool; |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 352 | typedef CaptureGroupNameSocketPool<SOCKSClientSocketPool> |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 353 | CaptureGroupNameSOCKSSocketPool; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 354 | typedef CaptureGroupNameSocketPool<SSLClientSocketPool> |
| 355 | CaptureGroupNameSSLSocketPool; |
| 356 | |
| 357 | template<typename ParentPool> |
| 358 | CaptureGroupNameSocketPool<ParentPool>::CaptureGroupNameSocketPool( |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 359 | HostResolver* host_resolver, |
| 360 | CertVerifier* /* cert_verifier */) |
| 361 | : ParentPool(0, 0, NULL, host_resolver, NULL, NULL) {} |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 362 | |
| 363 | template<> |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 364 | CaptureGroupNameHttpProxySocketPool::CaptureGroupNameSocketPool( |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 365 | HostResolver* host_resolver, |
| 366 | CertVerifier* /* cert_verifier */) |
| 367 | : HttpProxyClientSocketPool(0, 0, NULL, host_resolver, NULL, NULL, NULL) {} |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 368 | |
| 369 | template<> |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 370 | CaptureGroupNameSSLSocketPool::CaptureGroupNameSocketPool( |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 371 | HostResolver* host_resolver, |
| 372 | CertVerifier* cert_verifier) |
[email protected] | 3ed7496f | 2011-12-15 18:27:40 | [diff] [blame] | 373 | : SSLClientSocketPool(0, 0, NULL, host_resolver, cert_verifier, NULL, |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 374 | NULL, NULL, "", NULL, NULL, NULL, NULL, NULL, NULL) {} |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 375 | |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 376 | //----------------------------------------------------------------------------- |
| 377 | |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 378 | // This is the expected return from a current server advertising SPDY. |
| 379 | static const char kAlternateProtocolHttpHeader[] = |
[email protected] | 8d2f701 | 2012-02-16 00:08:04 | [diff] [blame] | 380 | "Alternate-Protocol: 443:npn-spdy/2.1\r\n\r\n"; |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 381 | |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 382 | // Helper functions for validating that AuthChallengeInfo's are correctly |
| 383 | // configured for common cases. |
| 384 | bool CheckBasicServerAuth(const AuthChallengeInfo* auth_challenge) { |
| 385 | if (!auth_challenge) |
| 386 | return false; |
| 387 | EXPECT_FALSE(auth_challenge->is_proxy); |
| 388 | EXPECT_EQ("www.google.com:80", auth_challenge->challenger.ToString()); |
| 389 | EXPECT_EQ("MyRealm1", auth_challenge->realm); |
| 390 | EXPECT_EQ("basic", auth_challenge->scheme); |
| 391 | return true; |
| 392 | } |
| 393 | |
| 394 | bool CheckBasicProxyAuth(const AuthChallengeInfo* auth_challenge) { |
| 395 | if (!auth_challenge) |
| 396 | return false; |
| 397 | EXPECT_TRUE(auth_challenge->is_proxy); |
| 398 | EXPECT_EQ("myproxy:70", auth_challenge->challenger.ToString()); |
| 399 | EXPECT_EQ("MyRealm1", auth_challenge->realm); |
| 400 | EXPECT_EQ("basic", auth_challenge->scheme); |
| 401 | return true; |
| 402 | } |
| 403 | |
| 404 | bool CheckDigestServerAuth(const AuthChallengeInfo* auth_challenge) { |
| 405 | if (!auth_challenge) |
| 406 | return false; |
| 407 | EXPECT_FALSE(auth_challenge->is_proxy); |
| 408 | EXPECT_EQ("www.google.com:80", auth_challenge->challenger.ToString()); |
| 409 | EXPECT_EQ("digestive", auth_challenge->realm); |
| 410 | EXPECT_EQ("digest", auth_challenge->scheme); |
| 411 | return true; |
| 412 | } |
| 413 | |
| 414 | bool CheckNTLMServerAuth(const AuthChallengeInfo* auth_challenge) { |
| 415 | if (!auth_challenge) |
| 416 | return false; |
| 417 | EXPECT_FALSE(auth_challenge->is_proxy); |
| 418 | EXPECT_EQ("172.22.68.17:80", auth_challenge->challenger.ToString()); |
| 419 | EXPECT_EQ(std::string(), auth_challenge->realm); |
| 420 | EXPECT_EQ("ntlm", auth_challenge->scheme); |
| 421 | return true; |
| 422 | } |
| 423 | |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 424 | TEST_F(HttpNetworkTransactionTest, Basic) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 425 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 426 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 427 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | TEST_F(HttpNetworkTransactionTest, SimpleGET) { |
| 431 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 432 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 433 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 434 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 435 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 436 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 437 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 438 | EXPECT_EQ(OK, out.rv); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 439 | EXPECT_EQ("HTTP/1.0 200 OK", out.status_line); |
| 440 | EXPECT_EQ("hello world", out.response_data); |
| 441 | } |
| 442 | |
| 443 | // Response with no status line. |
| 444 | TEST_F(HttpNetworkTransactionTest, SimpleGETNoHeaders) { |
| 445 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 446 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 447 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 448 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 449 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 450 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 451 | EXPECT_EQ(OK, out.rv); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 452 | EXPECT_EQ("HTTP/0.9 200 OK", out.status_line); |
| 453 | EXPECT_EQ("hello world", out.response_data); |
| 454 | } |
| 455 | |
| 456 | // Allow up to 4 bytes of junk to precede status line. |
| 457 | TEST_F(HttpNetworkTransactionTest, StatusLineJunk2Bytes) { |
| 458 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 459 | MockRead("xxxHTTP/1.0 404 Not Found\nServer: blah\n\nDATA"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 460 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 461 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 462 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 463 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 464 | EXPECT_EQ(OK, out.rv); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 465 | EXPECT_EQ("HTTP/1.0 404 Not Found", out.status_line); |
| 466 | EXPECT_EQ("DATA", out.response_data); |
| 467 | } |
| 468 | |
| 469 | // Allow up to 4 bytes of junk to precede status line. |
| 470 | TEST_F(HttpNetworkTransactionTest, StatusLineJunk4Bytes) { |
| 471 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 472 | MockRead("\n\nQJHTTP/1.0 404 Not Found\nServer: blah\n\nDATA"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 473 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 474 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 475 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 476 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 477 | EXPECT_EQ(OK, out.rv); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 478 | EXPECT_EQ("HTTP/1.0 404 Not Found", out.status_line); |
| 479 | EXPECT_EQ("DATA", out.response_data); |
| 480 | } |
| 481 | |
| 482 | // Beyond 4 bytes of slop and it should fail to find a status line. |
| 483 | TEST_F(HttpNetworkTransactionTest, StatusLineJunk5Bytes) { |
| 484 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 485 | MockRead("xxxxxHTTP/1.1 404 Not Found\nServer: blah"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 486 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 487 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 488 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 489 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 490 | EXPECT_EQ(OK, out.rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 491 | EXPECT_EQ("HTTP/0.9 200 OK", out.status_line); |
| 492 | EXPECT_EQ("xxxxxHTTP/1.1 404 Not Found\nServer: blah", out.response_data); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | // Same as StatusLineJunk4Bytes, except the read chunks are smaller. |
| 496 | TEST_F(HttpNetworkTransactionTest, StatusLineJunk4Bytes_Slow) { |
| 497 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 498 | MockRead("\n"), |
| 499 | MockRead("\n"), |
| 500 | MockRead("Q"), |
| 501 | MockRead("J"), |
| 502 | MockRead("HTTP/1.0 404 Not Found\nServer: blah\n\nDATA"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 503 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 504 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 505 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 506 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 507 | EXPECT_EQ(OK, out.rv); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 508 | EXPECT_EQ("HTTP/1.0 404 Not Found", out.status_line); |
| 509 | EXPECT_EQ("DATA", out.response_data); |
| 510 | } |
| 511 | |
| 512 | // Close the connection before enough bytes to have a status line. |
| 513 | TEST_F(HttpNetworkTransactionTest, StatusLinePartial) { |
| 514 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 515 | MockRead("HTT"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 516 | MockRead(false, OK), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 517 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 518 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 519 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 520 | EXPECT_EQ(OK, out.rv); |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 521 | EXPECT_EQ("HTTP/0.9 200 OK", out.status_line); |
| 522 | EXPECT_EQ("HTT", out.response_data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 523 | } |
| 524 | |
[email protected] | f9d44aa | 2008-09-23 23:57:17 | [diff] [blame] | 525 | // Simulate a 204 response, lacking a Content-Length header, sent over a |
| 526 | // persistent connection. The response should still terminate since a 204 |
| 527 | // cannot have a response body. |
| 528 | TEST_F(HttpNetworkTransactionTest, StopsReading204) { |
| 529 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 530 | MockRead("HTTP/1.1 204 No Content\r\n\r\n"), |
| 531 | MockRead("junk"), // Should not be read!! |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 532 | MockRead(false, OK), |
[email protected] | f9d44aa | 2008-09-23 23:57:17 | [diff] [blame] | 533 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 534 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 535 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 536 | EXPECT_EQ(OK, out.rv); |
[email protected] | f9d44aa | 2008-09-23 23:57:17 | [diff] [blame] | 537 | EXPECT_EQ("HTTP/1.1 204 No Content", out.status_line); |
| 538 | EXPECT_EQ("", out.response_data); |
| 539 | } |
| 540 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 541 | // A simple request using chunked encoding with some extra data after. |
| 542 | // (Like might be seen in a pipelined response.) |
| 543 | TEST_F(HttpNetworkTransactionTest, ChunkedEncoding) { |
| 544 | MockRead data_reads[] = { |
| 545 | MockRead("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"), |
| 546 | MockRead("5\r\nHello\r\n"), |
| 547 | MockRead("1\r\n"), |
| 548 | MockRead(" \r\n"), |
| 549 | MockRead("5\r\nworld\r\n"), |
| 550 | MockRead("0\r\n\r\nHTTP/1.1 200 OK\r\n"), |
| 551 | MockRead(false, OK), |
| 552 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 553 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 554 | arraysize(data_reads)); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 555 | EXPECT_EQ(OK, out.rv); |
| 556 | EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); |
| 557 | EXPECT_EQ("Hello world", out.response_data); |
| 558 | } |
| 559 | |
[email protected] | 9fe44f5 | 2010-09-23 18:36:00 | [diff] [blame] | 560 | // Next tests deal with http://crbug.com/56344. |
| 561 | |
| 562 | TEST_F(HttpNetworkTransactionTest, |
| 563 | MultipleContentLengthHeadersNoTransferEncoding) { |
| 564 | MockRead data_reads[] = { |
| 565 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 566 | MockRead("Content-Length: 10\r\n"), |
| 567 | MockRead("Content-Length: 5\r\n\r\n"), |
| 568 | }; |
| 569 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 570 | arraysize(data_reads)); |
| 571 | EXPECT_EQ(ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_LENGTH, out.rv); |
| 572 | } |
| 573 | |
| 574 | TEST_F(HttpNetworkTransactionTest, |
[email protected] | 44b5204 | 2010-10-29 22:48:04 | [diff] [blame] | 575 | DuplicateContentLengthHeadersNoTransferEncoding) { |
| 576 | MockRead data_reads[] = { |
| 577 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 578 | MockRead("Content-Length: 5\r\n"), |
| 579 | MockRead("Content-Length: 5\r\n\r\n"), |
| 580 | MockRead("Hello"), |
| 581 | }; |
| 582 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 583 | arraysize(data_reads)); |
| 584 | EXPECT_EQ(OK, out.rv); |
| 585 | EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); |
| 586 | EXPECT_EQ("Hello", out.response_data); |
| 587 | } |
| 588 | |
| 589 | TEST_F(HttpNetworkTransactionTest, |
| 590 | ComplexContentLengthHeadersNoTransferEncoding) { |
| 591 | // More than 2 dupes. |
| 592 | { |
| 593 | MockRead data_reads[] = { |
| 594 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 595 | MockRead("Content-Length: 5\r\n"), |
| 596 | MockRead("Content-Length: 5\r\n"), |
| 597 | MockRead("Content-Length: 5\r\n\r\n"), |
| 598 | MockRead("Hello"), |
| 599 | }; |
| 600 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 601 | arraysize(data_reads)); |
| 602 | EXPECT_EQ(OK, out.rv); |
| 603 | EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); |
| 604 | EXPECT_EQ("Hello", out.response_data); |
| 605 | } |
| 606 | // HTTP/1.0 |
| 607 | { |
| 608 | MockRead data_reads[] = { |
| 609 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 610 | MockRead("Content-Length: 5\r\n"), |
| 611 | MockRead("Content-Length: 5\r\n"), |
| 612 | MockRead("Content-Length: 5\r\n\r\n"), |
| 613 | MockRead("Hello"), |
| 614 | }; |
| 615 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 616 | arraysize(data_reads)); |
| 617 | EXPECT_EQ(OK, out.rv); |
| 618 | EXPECT_EQ("HTTP/1.0 200 OK", out.status_line); |
| 619 | EXPECT_EQ("Hello", out.response_data); |
| 620 | } |
| 621 | // 2 dupes and one mismatched. |
| 622 | { |
| 623 | MockRead data_reads[] = { |
| 624 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 625 | MockRead("Content-Length: 10\r\n"), |
| 626 | MockRead("Content-Length: 10\r\n"), |
| 627 | MockRead("Content-Length: 5\r\n\r\n"), |
| 628 | }; |
| 629 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 630 | arraysize(data_reads)); |
| 631 | EXPECT_EQ(ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_LENGTH, out.rv); |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | TEST_F(HttpNetworkTransactionTest, |
[email protected] | 9fe44f5 | 2010-09-23 18:36:00 | [diff] [blame] | 636 | MultipleContentLengthHeadersTransferEncoding) { |
| 637 | MockRead data_reads[] = { |
| 638 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 639 | MockRead("Content-Length: 666\r\n"), |
| 640 | MockRead("Content-Length: 1337\r\n"), |
| 641 | MockRead("Transfer-Encoding: chunked\r\n\r\n"), |
| 642 | MockRead("5\r\nHello\r\n"), |
| 643 | MockRead("1\r\n"), |
| 644 | MockRead(" \r\n"), |
| 645 | MockRead("5\r\nworld\r\n"), |
| 646 | MockRead("0\r\n\r\nHTTP/1.1 200 OK\r\n"), |
| 647 | MockRead(false, OK), |
| 648 | }; |
| 649 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 650 | arraysize(data_reads)); |
| 651 | EXPECT_EQ(OK, out.rv); |
| 652 | EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); |
| 653 | EXPECT_EQ("Hello world", out.response_data); |
| 654 | } |
| 655 | |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 656 | // Next tests deal with http://crbug.com/98895. |
| 657 | |
| 658 | // Checks that a single Content-Disposition header results in no error. |
| 659 | TEST_F(HttpNetworkTransactionTest, SingleContentDispositionHeader) { |
| 660 | MockRead data_reads[] = { |
| 661 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 662 | MockRead("Content-Disposition: attachment;filename=\"salutations.txt\"r\n"), |
| 663 | MockRead("Content-Length: 5\r\n\r\n"), |
| 664 | MockRead("Hello"), |
| 665 | }; |
| 666 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 667 | arraysize(data_reads)); |
| 668 | EXPECT_EQ(OK, out.rv); |
| 669 | EXPECT_EQ("HTTP/1.1 200 OK", out.status_line); |
| 670 | EXPECT_EQ("Hello", out.response_data); |
| 671 | } |
| 672 | |
| 673 | // Checks that two identical Content-Disposition headers result in an error. |
| 674 | TEST_F(HttpNetworkTransactionTest, |
| 675 | DuplicateIdenticalContentDispositionHeaders) { |
| 676 | MockRead data_reads[] = { |
| 677 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 678 | MockRead("Content-Disposition: attachment;filename=\"greetings.txt\"r\n"), |
| 679 | MockRead("Content-Disposition: attachment;filename=\"greetings.txt\"r\n"), |
| 680 | MockRead("Content-Length: 5\r\n\r\n"), |
| 681 | MockRead("Hello"), |
| 682 | }; |
| 683 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 684 | arraysize(data_reads)); |
| 685 | EXPECT_EQ(ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION, out.rv); |
| 686 | } |
| 687 | |
| 688 | // Checks that two distinct Content-Disposition headers result in an error. |
| 689 | TEST_F(HttpNetworkTransactionTest, DuplicateDistinctContentDispositionHeaders) { |
| 690 | MockRead data_reads[] = { |
| 691 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 692 | MockRead("Content-Disposition: attachment;filename=\"greetings.txt\"r\n"), |
| 693 | MockRead("Content-Disposition: attachment;filename=\"hi.txt\"r\n"), |
| 694 | MockRead("Content-Length: 5\r\n\r\n"), |
| 695 | MockRead("Hello"), |
| 696 | }; |
| 697 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 698 | arraysize(data_reads)); |
| 699 | EXPECT_EQ(ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION, out.rv); |
| 700 | } |
| 701 | |
| 702 | // Checks the behavior of a single Location header. |
| 703 | TEST_F(HttpNetworkTransactionTest, SingleLocationHeader) { |
| 704 | MockRead data_reads[] = { |
| 705 | MockRead("HTTP/1.1 302 Redirect\r\n"), |
| 706 | MockRead("Location: http://good.com/\r\n"), |
| 707 | MockRead("Content-Length: 0\r\n\r\n"), |
| 708 | MockRead(false, OK), |
| 709 | }; |
| 710 | |
| 711 | HttpRequestInfo request; |
| 712 | request.method = "GET"; |
| 713 | request.url = GURL("http://redirect.com/"); |
| 714 | request.load_flags = 0; |
| 715 | |
| 716 | SessionDependencies session_deps; |
| 717 | scoped_ptr<HttpTransaction> trans( |
| 718 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 719 | |
| 720 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
| 721 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 722 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 723 | TestCompletionCallback callback; |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 724 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 725 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 1628fe9 | 2011-10-04 23:04:55 | [diff] [blame] | 726 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 727 | |
| 728 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 729 | |
| 730 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 731 | ASSERT_TRUE(response != NULL && response->headers != NULL); |
| 732 | EXPECT_EQ("HTTP/1.1 302 Redirect", response->headers->GetStatusLine()); |
| 733 | std::string url; |
| 734 | EXPECT_TRUE(response->headers->IsRedirect(&url)); |
| 735 | EXPECT_EQ("http://good.com/", url); |
| 736 | } |
| 737 | |
| 738 | // Checks that two identical Location headers result in an error. |
| 739 | TEST_F(HttpNetworkTransactionTest, DuplicateIdenticalLocationHeaders) { |
| 740 | MockRead data_reads[] = { |
| 741 | MockRead("HTTP/1.1 302 Redirect\r\n"), |
| 742 | MockRead("Location: http://good.com/\r\n"), |
| 743 | MockRead("Location: http://good.com/\r\n"), |
| 744 | MockRead("Content-Length: 0\r\n\r\n"), |
| 745 | MockRead(false, OK), |
| 746 | }; |
| 747 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 748 | arraysize(data_reads)); |
| 749 | EXPECT_EQ(ERR_RESPONSE_HEADERS_MULTIPLE_LOCATION, out.rv); |
| 750 | } |
| 751 | |
| 752 | // Checks that two distinct Location headers result in an error. |
| 753 | TEST_F(HttpNetworkTransactionTest, DuplicateDistinctLocationHeaders) { |
| 754 | MockRead data_reads[] = { |
| 755 | MockRead("HTTP/1.1 302 Redirect\r\n"), |
| 756 | MockRead("Location: http://good.com/\r\n"), |
| 757 | MockRead("Location: http://evil.com/\r\n"), |
| 758 | MockRead("Content-Length: 0\r\n\r\n"), |
| 759 | MockRead(false, OK), |
| 760 | }; |
| 761 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 762 | arraysize(data_reads)); |
| 763 | EXPECT_EQ(ERR_RESPONSE_HEADERS_MULTIPLE_LOCATION, out.rv); |
| 764 | } |
| 765 | |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 766 | // Do a request using the HEAD method. Verify that we don't try to read the |
| 767 | // message body (since HEAD has none). |
| 768 | TEST_F(HttpNetworkTransactionTest, Head) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 769 | HttpRequestInfo request; |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 770 | request.method = "HEAD"; |
| 771 | request.url = GURL("http://www.google.com/"); |
| 772 | request.load_flags = 0; |
| 773 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 774 | SessionDependencies session_deps; |
| 775 | scoped_ptr<HttpTransaction> trans( |
| 776 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 777 | |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 778 | MockWrite data_writes1[] = { |
| 779 | MockWrite("HEAD / HTTP/1.1\r\n" |
| 780 | "Host: www.google.com\r\n" |
| 781 | "Connection: keep-alive\r\n" |
| 782 | "Content-Length: 0\r\n\r\n"), |
| 783 | }; |
| 784 | MockRead data_reads1[] = { |
| 785 | MockRead("HTTP/1.1 404 Not Found\r\n"), |
| 786 | MockRead("Server: Blah\r\n"), |
| 787 | MockRead("Content-Length: 1234\r\n\r\n"), |
| 788 | |
| 789 | // No response body because the test stops reading here. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 790 | MockRead(false, ERR_UNEXPECTED), // Should not be reached. |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 791 | }; |
| 792 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 793 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 794 | data_writes1, arraysize(data_writes1)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 795 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 796 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 797 | TestCompletionCallback callback1; |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 798 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 799 | int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 800 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 801 | |
| 802 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 803 | EXPECT_EQ(OK, rv); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 804 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 805 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 806 | ASSERT_TRUE(response != NULL); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 807 | |
| 808 | // Check that the headers got parsed. |
| 809 | EXPECT_TRUE(response->headers != NULL); |
| 810 | EXPECT_EQ(1234, response->headers->GetContentLength()); |
| 811 | EXPECT_EQ("HTTP/1.1 404 Not Found", response->headers->GetStatusLine()); |
| 812 | |
| 813 | std::string server_header; |
| 814 | void* iter = NULL; |
| 815 | bool has_server_header = response->headers->EnumerateHeader( |
| 816 | &iter, "Server", &server_header); |
| 817 | EXPECT_TRUE(has_server_header); |
| 818 | EXPECT_EQ("Blah", server_header); |
| 819 | |
| 820 | // Reading should give EOF right away, since there is no message body |
| 821 | // (despite non-zero content-length). |
| 822 | std::string response_data; |
| 823 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 824 | EXPECT_EQ(OK, rv); |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 825 | EXPECT_EQ("", response_data); |
| 826 | } |
| 827 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 828 | TEST_F(HttpNetworkTransactionTest, ReuseConnection) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 829 | SessionDependencies session_deps; |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 830 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 831 | |
| 832 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 833 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 834 | MockRead("hello"), |
| 835 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 836 | MockRead("world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 837 | MockRead(false, OK), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 838 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 839 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 840 | session_deps.socket_factory.AddSocketDataProvider(&data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 841 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 842 | const char* const kExpectedResponseData[] = { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 843 | "hello", "world" |
| 844 | }; |
| 845 | |
| 846 | for (int i = 0; i < 2; ++i) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 847 | HttpRequestInfo request; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 848 | request.method = "GET"; |
| 849 | request.url = GURL("http://www.google.com/"); |
| 850 | request.load_flags = 0; |
| 851 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 852 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 853 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 854 | TestCompletionCallback callback; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 855 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 856 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 857 | EXPECT_EQ(ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 858 | |
| 859 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 860 | EXPECT_EQ(OK, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 861 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 862 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 863 | ASSERT_TRUE(response != NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 864 | |
| 865 | EXPECT_TRUE(response->headers != NULL); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 866 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 867 | |
| 868 | std::string response_data; |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 869 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 870 | EXPECT_EQ(OK, rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 871 | EXPECT_EQ(kExpectedResponseData[i], response_data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 872 | } |
| 873 | } |
| 874 | |
| 875 | TEST_F(HttpNetworkTransactionTest, Ignores100) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 876 | HttpRequestInfo request; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 877 | request.method = "POST"; |
| 878 | request.url = GURL("http://www.foo.com/"); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 879 | request.upload_data = new UploadData; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 880 | request.upload_data->AppendBytes("foo", 3); |
| 881 | request.load_flags = 0; |
| 882 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 883 | SessionDependencies session_deps; |
| 884 | scoped_ptr<HttpTransaction> trans( |
| 885 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 886 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 887 | MockRead data_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 888 | MockRead("HTTP/1.0 100 Continue\r\n\r\n"), |
| 889 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 890 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 891 | MockRead(false, OK), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 892 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 893 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 894 | session_deps.socket_factory.AddSocketDataProvider(&data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 895 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 896 | TestCompletionCallback callback; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 897 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 898 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 899 | EXPECT_EQ(ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 900 | |
| 901 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 902 | EXPECT_EQ(OK, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 903 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 904 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 905 | ASSERT_TRUE(response != NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 906 | |
| 907 | EXPECT_TRUE(response->headers != NULL); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 908 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 909 | |
| 910 | std::string response_data; |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 911 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 912 | EXPECT_EQ(OK, rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 913 | EXPECT_EQ("hello world", response_data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 914 | } |
| 915 | |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 916 | // This test is almost the same as Ignores100 above, but the response contains |
| 917 | // 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] | 918 | // HTTP/1.1 and the two status headers are read in one read. |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 919 | TEST_F(HttpNetworkTransactionTest, Ignores1xx) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 920 | HttpRequestInfo request; |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 921 | request.method = "GET"; |
| 922 | request.url = GURL("http://www.foo.com/"); |
| 923 | request.load_flags = 0; |
| 924 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 925 | SessionDependencies session_deps; |
| 926 | scoped_ptr<HttpTransaction> trans( |
| 927 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 928 | |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 929 | MockRead data_reads[] = { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 930 | MockRead("HTTP/1.1 102 Unspecified status code\r\n\r\n" |
| 931 | "HTTP/1.1 200 OK\r\n\r\n"), |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 932 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 933 | MockRead(false, OK), |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 934 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 935 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 936 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 937 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 938 | TestCompletionCallback callback; |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 939 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 940 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 941 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 942 | |
| 943 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 944 | EXPECT_EQ(OK, rv); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 945 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 946 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 947 | ASSERT_TRUE(response != NULL); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 948 | |
| 949 | EXPECT_TRUE(response->headers != NULL); |
| 950 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 951 | |
| 952 | std::string response_data; |
| 953 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 954 | EXPECT_EQ(OK, rv); |
[email protected] | 3a2d366 | 2009-03-27 03:49:14 | [diff] [blame] | 955 | EXPECT_EQ("hello world", response_data); |
| 956 | } |
| 957 | |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 958 | TEST_F(HttpNetworkTransactionTest, Incomplete100ThenEOF) { |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 959 | HttpRequestInfo request; |
| 960 | request.method = "POST"; |
| 961 | request.url = GURL("http://www.foo.com/"); |
| 962 | request.load_flags = 0; |
| 963 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 964 | SessionDependencies session_deps; |
| 965 | scoped_ptr<HttpTransaction> trans( |
| 966 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 967 | |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 968 | MockRead data_reads[] = { |
| 969 | MockRead(false, "HTTP/1.0 100 Continue\r\n"), |
| 970 | MockRead(true, 0), |
| 971 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 972 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 973 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 974 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 975 | TestCompletionCallback callback; |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 976 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 977 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 978 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 979 | |
| 980 | rv = callback.WaitForResult(); |
| 981 | EXPECT_EQ(OK, rv); |
| 982 | |
| 983 | std::string response_data; |
| 984 | rv = ReadTransaction(trans.get(), &response_data); |
| 985 | EXPECT_EQ(OK, rv); |
| 986 | EXPECT_EQ("", response_data); |
| 987 | } |
| 988 | |
| 989 | TEST_F(HttpNetworkTransactionTest, EmptyResponse) { |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 990 | HttpRequestInfo request; |
| 991 | request.method = "POST"; |
| 992 | request.url = GURL("http://www.foo.com/"); |
| 993 | request.load_flags = 0; |
| 994 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 995 | SessionDependencies session_deps; |
| 996 | scoped_ptr<HttpTransaction> trans( |
| 997 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 998 | |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 999 | MockRead data_reads[] = { |
| 1000 | MockRead(true, 0), |
| 1001 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1002 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 1003 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 1004 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1005 | TestCompletionCallback callback; |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 1006 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1007 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | ee9410e7 | 2010-01-07 01:42:38 | [diff] [blame] | 1008 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1009 | |
| 1010 | rv = callback.WaitForResult(); |
| 1011 | EXPECT_EQ(ERR_EMPTY_RESPONSE, rv); |
| 1012 | } |
| 1013 | |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1014 | void HttpNetworkTransactionTest::KeepAliveConnectionResendRequestTest( |
[email protected] | 20296599 | 2011-12-07 23:04:51 | [diff] [blame] | 1015 | const MockWrite* write_failure, |
| 1016 | const MockRead* read_failure) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1017 | HttpRequestInfo request; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1018 | request.method = "GET"; |
| 1019 | request.url = GURL("http://www.foo.com/"); |
| 1020 | request.load_flags = 0; |
| 1021 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1022 | SessionDependencies session_deps; |
| 1023 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 1024 | |
[email protected] | 20296599 | 2011-12-07 23:04:51 | [diff] [blame] | 1025 | // Written data for successfully sending both requests. |
| 1026 | MockWrite data1_writes[] = { |
| 1027 | MockWrite("GET / HTTP/1.1\r\n" |
| 1028 | "Host: www.foo.com\r\n" |
| 1029 | "Connection: keep-alive\r\n\r\n"), |
| 1030 | MockWrite("GET / HTTP/1.1\r\n" |
| 1031 | "Host: www.foo.com\r\n" |
| 1032 | "Connection: keep-alive\r\n\r\n") |
| 1033 | }; |
| 1034 | |
| 1035 | // Read results for the first request. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1036 | MockRead data1_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 1037 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 1038 | MockRead("hello"), |
[email protected] | 20296599 | 2011-12-07 23:04:51 | [diff] [blame] | 1039 | MockRead(true, OK), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1040 | }; |
[email protected] | 20296599 | 2011-12-07 23:04:51 | [diff] [blame] | 1041 | |
| 1042 | if (write_failure) { |
| 1043 | ASSERT_TRUE(!read_failure); |
| 1044 | data1_writes[1] = *write_failure; |
| 1045 | } else { |
| 1046 | ASSERT_TRUE(read_failure); |
| 1047 | data1_reads[2] = *read_failure; |
| 1048 | } |
| 1049 | |
| 1050 | StaticSocketDataProvider data1(data1_reads, arraysize(data1_reads), |
| 1051 | data1_writes, arraysize(data1_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1052 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1053 | |
| 1054 | MockRead data2_reads[] = { |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 1055 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 1056 | MockRead("world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1057 | MockRead(true, OK), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1058 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1059 | StaticSocketDataProvider data2(data2_reads, arraysize(data2_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1060 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1061 | |
| 1062 | const char* kExpectedResponseData[] = { |
| 1063 | "hello", "world" |
| 1064 | }; |
| 1065 | |
| 1066 | for (int i = 0; i < 2; ++i) { |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1067 | TestCompletionCallback callback; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1068 | |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 1069 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1070 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1071 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1072 | EXPECT_EQ(ERR_IO_PENDING, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1073 | |
| 1074 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1075 | EXPECT_EQ(OK, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1076 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1077 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 1078 | ASSERT_TRUE(response != NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1079 | |
| 1080 | EXPECT_TRUE(response->headers != NULL); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1081 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1082 | |
| 1083 | std::string response_data; |
[email protected] | af4876d | 2008-10-21 23:10:57 | [diff] [blame] | 1084 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1085 | EXPECT_EQ(OK, rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1086 | EXPECT_EQ(kExpectedResponseData[i], response_data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1087 | } |
| 1088 | } |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1089 | |
[email protected] | 20296599 | 2011-12-07 23:04:51 | [diff] [blame] | 1090 | TEST_F(HttpNetworkTransactionTest, KeepAliveConnectionNotConnectedOnWrite) { |
| 1091 | MockWrite write_failure(true, ERR_SOCKET_NOT_CONNECTED); |
| 1092 | KeepAliveConnectionResendRequestTest(&write_failure, NULL); |
| 1093 | } |
| 1094 | |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1095 | TEST_F(HttpNetworkTransactionTest, KeepAliveConnectionReset) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1096 | MockRead read_failure(true, ERR_CONNECTION_RESET); |
[email protected] | 20296599 | 2011-12-07 23:04:51 | [diff] [blame] | 1097 | KeepAliveConnectionResendRequestTest(NULL, &read_failure); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | TEST_F(HttpNetworkTransactionTest, KeepAliveConnectionEOF) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1101 | MockRead read_failure(false, OK); // EOF |
[email protected] | 20296599 | 2011-12-07 23:04:51 | [diff] [blame] | 1102 | KeepAliveConnectionResendRequestTest(NULL, &read_failure); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | TEST_F(HttpNetworkTransactionTest, NonKeepAliveConnectionReset) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1106 | HttpRequestInfo request; |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1107 | request.method = "GET"; |
| 1108 | request.url = GURL("http://www.google.com/"); |
| 1109 | request.load_flags = 0; |
| 1110 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1111 | SessionDependencies session_deps; |
| 1112 | scoped_ptr<HttpTransaction> trans( |
| 1113 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 1114 | |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1115 | MockRead data_reads[] = { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1116 | MockRead(true, ERR_CONNECTION_RESET), |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 1117 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), // Should not be used |
| 1118 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1119 | MockRead(false, OK), |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1120 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1121 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1122 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1123 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1124 | TestCompletionCallback callback; |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1125 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1126 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1127 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1128 | |
| 1129 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1130 | EXPECT_EQ(ERR_CONNECTION_RESET, rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1131 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1132 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1133 | EXPECT_TRUE(response == NULL); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | // What do various browsers do when the server closes a non-keepalive |
| 1137 | // connection without sending any response header or body? |
| 1138 | // |
| 1139 | // IE7: error page |
| 1140 | // Safari 3.1.2 (Windows): error page |
| 1141 | // Firefox 3.0.1: blank page |
| 1142 | // Opera 9.52: after five attempts, blank page |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1143 | // Us with WinHTTP: error page (ERR_INVALID_RESPONSE) |
| 1144 | // Us: error page (EMPTY_RESPONSE) |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1145 | TEST_F(HttpNetworkTransactionTest, NonKeepAliveConnectionEOF) { |
| 1146 | MockRead data_reads[] = { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1147 | MockRead(false, OK), // EOF |
[email protected] | 217e602 | 2008-09-29 18:18:35 | [diff] [blame] | 1148 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), // Should not be used |
| 1149 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1150 | MockRead(false, OK), |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1151 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1152 | SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
| 1153 | arraysize(data_reads)); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1154 | EXPECT_EQ(ERR_EMPTY_RESPONSE, out.rv); |
[email protected] | 3d2a59b | 2008-09-26 19:44:25 | [diff] [blame] | 1155 | } |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1156 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1157 | // Test that we correctly reuse a keep-alive connection after not explicitly |
| 1158 | // reading the body. |
| 1159 | TEST_F(HttpNetworkTransactionTest, KeepAliveAfterUnreadBody) { |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 1160 | HttpRequestInfo request; |
| 1161 | request.method = "GET"; |
| 1162 | request.url = GURL("http://www.foo.com/"); |
| 1163 | request.load_flags = 0; |
| 1164 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1165 | SessionDependencies session_deps; |
| 1166 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 1167 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1168 | // Note that because all these reads happen in the same |
| 1169 | // StaticSocketDataProvider, it shows that the same socket is being reused for |
| 1170 | // all transactions. |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 1171 | MockRead data1_reads[] = { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1172 | MockRead("HTTP/1.1 204 No Content\r\n\r\n"), |
| 1173 | MockRead("HTTP/1.1 205 Reset Content\r\n\r\n"), |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 1174 | MockRead("HTTP/1.1 304 Not Modified\r\n\r\n"), |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1175 | MockRead("HTTP/1.1 302 Found\r\n" |
| 1176 | "Content-Length: 0\r\n\r\n"), |
| 1177 | MockRead("HTTP/1.1 302 Found\r\n" |
| 1178 | "Content-Length: 5\r\n\r\n" |
| 1179 | "hello"), |
| 1180 | MockRead("HTTP/1.1 301 Moved Permanently\r\n" |
| 1181 | "Content-Length: 0\r\n\r\n"), |
| 1182 | MockRead("HTTP/1.1 301 Moved Permanently\r\n" |
| 1183 | "Content-Length: 5\r\n\r\n" |
| 1184 | "hello"), |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 1185 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"), |
| 1186 | MockRead("hello"), |
| 1187 | }; |
| 1188 | StaticSocketDataProvider data1(data1_reads, arraysize(data1_reads), NULL, 0); |
| 1189 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 1190 | |
| 1191 | MockRead data2_reads[] = { |
| 1192 | MockRead(false, ERR_UNEXPECTED), // Should not be reached. |
| 1193 | }; |
| 1194 | StaticSocketDataProvider data2(data2_reads, arraysize(data2_reads), NULL, 0); |
| 1195 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 1196 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1197 | const int kNumUnreadBodies = arraysize(data1_reads) - 2; |
| 1198 | std::string response_lines[kNumUnreadBodies]; |
| 1199 | |
| 1200 | for (size_t i = 0; i < arraysize(data1_reads) - 2; ++i) { |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1201 | TestCompletionCallback callback; |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 1202 | |
| 1203 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 1204 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1205 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 1206 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1207 | |
| 1208 | rv = callback.WaitForResult(); |
| 1209 | EXPECT_EQ(OK, rv); |
| 1210 | |
| 1211 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1212 | ASSERT_TRUE(response != NULL); |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 1213 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1214 | ASSERT_TRUE(response->headers != NULL); |
| 1215 | response_lines[i] = response->headers->GetStatusLine(); |
| 1216 | |
| 1217 | // We intentionally don't read the response bodies. |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 1218 | } |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1219 | |
| 1220 | const char* const kStatusLines[] = { |
| 1221 | "HTTP/1.1 204 No Content", |
| 1222 | "HTTP/1.1 205 Reset Content", |
| 1223 | "HTTP/1.1 304 Not Modified", |
| 1224 | "HTTP/1.1 302 Found", |
| 1225 | "HTTP/1.1 302 Found", |
| 1226 | "HTTP/1.1 301 Moved Permanently", |
| 1227 | "HTTP/1.1 301 Moved Permanently", |
| 1228 | }; |
| 1229 | |
| 1230 | COMPILE_ASSERT(kNumUnreadBodies == arraysize(kStatusLines), |
| 1231 | forgot_to_update_kStatusLines); |
| 1232 | |
| 1233 | for (int i = 0; i < kNumUnreadBodies; ++i) |
| 1234 | EXPECT_EQ(kStatusLines[i], response_lines[i]); |
| 1235 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1236 | TestCompletionCallback callback; |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1237 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1238 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1239 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1240 | rv = callback.WaitForResult(); |
| 1241 | EXPECT_EQ(OK, rv); |
| 1242 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 1243 | ASSERT_TRUE(response != NULL); |
| 1244 | ASSERT_TRUE(response->headers != NULL); |
| 1245 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 1246 | std::string response_data; |
| 1247 | rv = ReadTransaction(trans.get(), &response_data); |
| 1248 | EXPECT_EQ(OK, rv); |
| 1249 | EXPECT_EQ("hello", response_data); |
[email protected] | fc31d6a4 | 2010-06-24 18:05:13 | [diff] [blame] | 1250 | } |
| 1251 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1252 | // Test the request-challenge-retry sequence for basic auth. |
| 1253 | // (basic auth is the easiest to mock, because it has no randomness). |
| 1254 | TEST_F(HttpNetworkTransactionTest, BasicAuth) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1255 | HttpRequestInfo request; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1256 | request.method = "GET"; |
| 1257 | request.url = GURL("http://www.google.com/"); |
| 1258 | request.load_flags = 0; |
| 1259 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1260 | SessionDependencies session_deps; |
| 1261 | scoped_ptr<HttpTransaction> trans( |
| 1262 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 1263 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1264 | MockWrite data_writes1[] = { |
| 1265 | MockWrite("GET / HTTP/1.1\r\n" |
| 1266 | "Host: www.google.com\r\n" |
| 1267 | "Connection: keep-alive\r\n\r\n"), |
| 1268 | }; |
| 1269 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1270 | MockRead data_reads1[] = { |
| 1271 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 1272 | // Give a couple authenticate options (only the middle one is actually |
| 1273 | // supported). |
[email protected] | 22927ad | 2009-09-21 19:56:19 | [diff] [blame] | 1274 | MockRead("WWW-Authenticate: Basic invalid\r\n"), // Malformed. |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1275 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1276 | MockRead("WWW-Authenticate: UNSUPPORTED realm=\"FOO\"\r\n"), |
| 1277 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1278 | // Large content-length -- won't matter, as connection will be reset. |
| 1279 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1280 | MockRead(false, ERR_FAILED), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1281 | }; |
| 1282 | |
| 1283 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1284 | // be issuing -- the final header line contains the credentials. |
| 1285 | MockWrite data_writes2[] = { |
| 1286 | MockWrite("GET / HTTP/1.1\r\n" |
| 1287 | "Host: www.google.com\r\n" |
| 1288 | "Connection: keep-alive\r\n" |
| 1289 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1290 | }; |
| 1291 | |
| 1292 | // Lastly, the server responds with the actual content. |
| 1293 | MockRead data_reads2[] = { |
| 1294 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 1295 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1296 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1297 | MockRead(false, OK), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1298 | }; |
| 1299 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1300 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1301 | data_writes1, arraysize(data_writes1)); |
| 1302 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 1303 | data_writes2, arraysize(data_writes2)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1304 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 1305 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1306 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1307 | TestCompletionCallback callback1; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1308 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1309 | int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1310 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1311 | |
| 1312 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1313 | EXPECT_EQ(OK, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1314 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1315 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 1316 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 1317 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge.get())); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1318 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1319 | TestCompletionCallback callback2; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1320 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1321 | rv = trans->RestartWithAuth( |
| 1322 | AuthCredentials(kFoo, kBar), callback2.callback()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1323 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1324 | |
| 1325 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1326 | EXPECT_EQ(OK, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1327 | |
| 1328 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 1329 | ASSERT_TRUE(response != NULL); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1330 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 1331 | EXPECT_EQ(100, response->headers->GetContentLength()); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1332 | } |
| 1333 | |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1334 | TEST_F(HttpNetworkTransactionTest, DoNotSendAuth) { |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1335 | HttpRequestInfo request; |
| 1336 | request.method = "GET"; |
| 1337 | request.url = GURL("http://www.google.com/"); |
| 1338 | request.load_flags = net::LOAD_DO_NOT_SEND_AUTH_DATA; |
| 1339 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1340 | SessionDependencies session_deps; |
| 1341 | scoped_ptr<HttpTransaction> trans( |
| 1342 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 1343 | |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1344 | MockWrite data_writes[] = { |
| 1345 | MockWrite("GET / HTTP/1.1\r\n" |
| 1346 | "Host: www.google.com\r\n" |
| 1347 | "Connection: keep-alive\r\n\r\n"), |
| 1348 | }; |
| 1349 | |
| 1350 | MockRead data_reads[] = { |
| 1351 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 1352 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1353 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1354 | // Large content-length -- won't matter, as connection will be reset. |
| 1355 | MockRead("Content-Length: 10000\r\n\r\n"), |
| 1356 | MockRead(false, ERR_FAILED), |
| 1357 | }; |
| 1358 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1359 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 1360 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1361 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1362 | TestCompletionCallback callback; |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1363 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1364 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1365 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1366 | |
| 1367 | rv = callback.WaitForResult(); |
| 1368 | EXPECT_EQ(0, rv); |
| 1369 | |
| 1370 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 1371 | ASSERT_TRUE(response != NULL); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 1372 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 1373 | } |
| 1374 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1375 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
| 1376 | // connection. |
| 1377 | TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAlive) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1378 | HttpRequestInfo request; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1379 | request.method = "GET"; |
| 1380 | request.url = GURL("http://www.google.com/"); |
| 1381 | request.load_flags = 0; |
| 1382 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1383 | SessionDependencies session_deps; |
| 1384 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 1385 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1386 | MockWrite data_writes1[] = { |
| 1387 | MockWrite("GET / HTTP/1.1\r\n" |
| 1388 | "Host: www.google.com\r\n" |
| 1389 | "Connection: keep-alive\r\n\r\n"), |
| 1390 | |
| 1391 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1392 | // be issuing -- the final header line contains the credentials. |
| 1393 | MockWrite("GET / HTTP/1.1\r\n" |
| 1394 | "Host: www.google.com\r\n" |
| 1395 | "Connection: keep-alive\r\n" |
| 1396 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1397 | }; |
| 1398 | |
| 1399 | MockRead data_reads1[] = { |
| 1400 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 1401 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1402 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1403 | MockRead("Content-Length: 14\r\n\r\n"), |
| 1404 | MockRead("Unauthorized\r\n"), |
| 1405 | |
| 1406 | // Lastly, the server responds with the actual content. |
| 1407 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1408 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1409 | MockRead("Content-Length: 5\r\n\r\n"), |
| 1410 | MockRead("Hello"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1411 | }; |
| 1412 | |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 1413 | // If there is a regression where we disconnect a Keep-Alive |
| 1414 | // connection during an auth roundtrip, we'll end up reading this. |
| 1415 | MockRead data_reads2[] = { |
| 1416 | MockRead(false, ERR_FAILED), |
| 1417 | }; |
| 1418 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1419 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1420 | data_writes1, arraysize(data_writes1)); |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 1421 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 1422 | NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1423 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 1424 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1425 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1426 | TestCompletionCallback callback1; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1427 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1428 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1429 | int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1430 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1431 | |
| 1432 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1433 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1434 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1435 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 1436 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 1437 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge.get())); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1438 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1439 | TestCompletionCallback callback2; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1440 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1441 | rv = trans->RestartWithAuth( |
| 1442 | AuthCredentials(kFoo, kBar), callback2.callback()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1443 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1444 | |
| 1445 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1446 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1447 | |
| 1448 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 1449 | ASSERT_TRUE(response != NULL); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1450 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1451 | EXPECT_EQ(5, response->headers->GetContentLength()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
| 1455 | // connection and with no response body to drain. |
| 1456 | TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAliveNoBody) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1457 | HttpRequestInfo request; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1458 | request.method = "GET"; |
| 1459 | request.url = GURL("http://www.google.com/"); |
| 1460 | request.load_flags = 0; |
| 1461 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1462 | SessionDependencies session_deps; |
| 1463 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 1464 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1465 | MockWrite data_writes1[] = { |
| 1466 | MockWrite("GET / HTTP/1.1\r\n" |
| 1467 | "Host: www.google.com\r\n" |
| 1468 | "Connection: keep-alive\r\n\r\n"), |
| 1469 | |
| 1470 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1471 | // be issuing -- the final header line contains the credentials. |
| 1472 | MockWrite("GET / HTTP/1.1\r\n" |
| 1473 | "Host: www.google.com\r\n" |
| 1474 | "Connection: keep-alive\r\n" |
| 1475 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1476 | }; |
| 1477 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1478 | MockRead data_reads1[] = { |
| 1479 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 1480 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1481 | MockRead("Content-Length: 0\r\n\r\n"), // No response body. |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1482 | |
| 1483 | // Lastly, the server responds with the actual content. |
| 1484 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1485 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1486 | MockRead("Content-Length: 5\r\n\r\n"), |
| 1487 | MockRead("hello"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1488 | }; |
| 1489 | |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 1490 | // An incorrect reconnect would cause this to be read. |
| 1491 | MockRead data_reads2[] = { |
| 1492 | MockRead(false, ERR_FAILED), |
| 1493 | }; |
| 1494 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1495 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1496 | data_writes1, arraysize(data_writes1)); |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 1497 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 1498 | NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1499 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 1500 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1501 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1502 | TestCompletionCallback callback1; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1503 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1504 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1505 | int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1506 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1507 | |
| 1508 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1509 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1510 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1511 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 1512 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 1513 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge.get())); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1514 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1515 | TestCompletionCallback callback2; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1516 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1517 | rv = trans->RestartWithAuth( |
| 1518 | AuthCredentials(kFoo, kBar), callback2.callback()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1519 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1520 | |
| 1521 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1522 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1523 | |
| 1524 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 1525 | ASSERT_TRUE(response != NULL); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1526 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1527 | EXPECT_EQ(5, response->headers->GetContentLength()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
| 1531 | // connection and with a large response body to drain. |
| 1532 | TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAliveLargeBody) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1533 | HttpRequestInfo request; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1534 | request.method = "GET"; |
| 1535 | request.url = GURL("http://www.google.com/"); |
| 1536 | request.load_flags = 0; |
| 1537 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1538 | SessionDependencies session_deps; |
| 1539 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 1540 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1541 | MockWrite data_writes1[] = { |
| 1542 | MockWrite("GET / HTTP/1.1\r\n" |
| 1543 | "Host: www.google.com\r\n" |
| 1544 | "Connection: keep-alive\r\n\r\n"), |
| 1545 | |
| 1546 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1547 | // be issuing -- the final header line contains the credentials. |
| 1548 | MockWrite("GET / HTTP/1.1\r\n" |
| 1549 | "Host: www.google.com\r\n" |
| 1550 | "Connection: keep-alive\r\n" |
| 1551 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1552 | }; |
| 1553 | |
| 1554 | // Respond with 5 kb of response body. |
| 1555 | std::string large_body_string("Unauthorized"); |
| 1556 | large_body_string.append(5 * 1024, ' '); |
| 1557 | large_body_string.append("\r\n"); |
| 1558 | |
| 1559 | MockRead data_reads1[] = { |
| 1560 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 1561 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1562 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1563 | // 5134 = 12 + 5 * 1024 + 2 |
| 1564 | MockRead("Content-Length: 5134\r\n\r\n"), |
| 1565 | MockRead(true, large_body_string.data(), large_body_string.size()), |
| 1566 | |
| 1567 | // Lastly, the server responds with the actual content. |
| 1568 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1569 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1570 | MockRead("Content-Length: 5\r\n\r\n"), |
| 1571 | MockRead("hello"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1572 | }; |
| 1573 | |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 1574 | // An incorrect reconnect would cause this to be read. |
| 1575 | MockRead data_reads2[] = { |
| 1576 | MockRead(false, ERR_FAILED), |
| 1577 | }; |
| 1578 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1579 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1580 | data_writes1, arraysize(data_writes1)); |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 1581 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 1582 | NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1583 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 1584 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1585 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1586 | TestCompletionCallback callback1; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1587 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1588 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1589 | int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1590 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1591 | |
| 1592 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1593 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1594 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1595 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 1596 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 1597 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge.get())); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1598 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1599 | TestCompletionCallback callback2; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1600 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1601 | rv = trans->RestartWithAuth( |
| 1602 | AuthCredentials(kFoo, kBar), callback2.callback()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1603 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1604 | |
| 1605 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1606 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1607 | |
| 1608 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 1609 | ASSERT_TRUE(response != NULL); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1610 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1611 | EXPECT_EQ(5, response->headers->GetContentLength()); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1612 | } |
| 1613 | |
| 1614 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1615 | // connection, but the server gets impatient and closes the connection. |
| 1616 | TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAliveImpatientServer) { |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1617 | HttpRequestInfo request; |
| 1618 | request.method = "GET"; |
| 1619 | request.url = GURL("http://www.google.com/"); |
| 1620 | request.load_flags = 0; |
| 1621 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1622 | SessionDependencies session_deps; |
| 1623 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 1624 | |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1625 | MockWrite data_writes1[] = { |
| 1626 | MockWrite("GET / HTTP/1.1\r\n" |
| 1627 | "Host: www.google.com\r\n" |
| 1628 | "Connection: keep-alive\r\n\r\n"), |
| 1629 | // This simulates the seemingly successful write to a closed connection |
| 1630 | // if the bug is not fixed. |
| 1631 | MockWrite("GET / HTTP/1.1\r\n" |
| 1632 | "Host: www.google.com\r\n" |
| 1633 | "Connection: keep-alive\r\n" |
| 1634 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1635 | }; |
| 1636 | |
| 1637 | MockRead data_reads1[] = { |
| 1638 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 1639 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1640 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 1641 | MockRead("Content-Length: 14\r\n\r\n"), |
| 1642 | // Tell MockTCPClientSocket to simulate the server closing the connection. |
| 1643 | MockRead(false, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| 1644 | MockRead("Unauthorized\r\n"), |
| 1645 | MockRead(false, OK), // The server closes the connection. |
| 1646 | }; |
| 1647 | |
| 1648 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1649 | // be issuing -- the final header line contains the credentials. |
| 1650 | MockWrite data_writes2[] = { |
| 1651 | MockWrite("GET / HTTP/1.1\r\n" |
| 1652 | "Host: www.google.com\r\n" |
| 1653 | "Connection: keep-alive\r\n" |
| 1654 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1655 | }; |
| 1656 | |
| 1657 | // Lastly, the server responds with the actual content. |
| 1658 | MockRead data_reads2[] = { |
| 1659 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1660 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1661 | MockRead("Content-Length: 5\r\n\r\n"), |
| 1662 | MockRead("hello"), |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1663 | }; |
| 1664 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1665 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1666 | data_writes1, arraysize(data_writes1)); |
| 1667 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 1668 | data_writes2, arraysize(data_writes2)); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1669 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 1670 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 1671 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1672 | TestCompletionCallback callback1; |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1673 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1674 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1675 | int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1676 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1677 | |
| 1678 | rv = callback1.WaitForResult(); |
| 1679 | EXPECT_EQ(OK, rv); |
| 1680 | |
| 1681 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 1682 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 1683 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge.get())); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1684 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1685 | TestCompletionCallback callback2; |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1686 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1687 | rv = trans->RestartWithAuth( |
| 1688 | AuthCredentials(kFoo, kBar), callback2.callback()); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1689 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1690 | |
| 1691 | rv = callback2.WaitForResult(); |
| 1692 | EXPECT_EQ(OK, rv); |
| 1693 | |
| 1694 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 1695 | ASSERT_TRUE(response != NULL); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1696 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1697 | EXPECT_EQ(5, response->headers->GetContentLength()); |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1698 | } |
| 1699 | |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1700 | // Test the request-challenge-retry sequence for basic auth, over a connection |
| 1701 | // that requires a restart when setting up an SSL tunnel. |
| 1702 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyNoKeepAlive) { |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1703 | HttpRequestInfo request; |
| 1704 | request.method = "GET"; |
| 1705 | request.url = GURL("https://www.google.com/"); |
| 1706 | // when the no authentication data flag is set. |
| 1707 | request.load_flags = net::LOAD_DO_NOT_SEND_AUTH_DATA; |
| 1708 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1709 | // Configure against proxy server "myproxy:70". |
| 1710 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
| 1711 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 1712 | session_deps.net_log = log.bound().net_log(); |
| 1713 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 1714 | |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1715 | // Since we have proxy, should try to establish tunnel. |
| 1716 | MockWrite data_writes1[] = { |
| 1717 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 1718 | "Host: www.google.com\r\n" |
| 1719 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 1720 | |
| 1721 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1722 | // be issuing -- the final header line contains the credentials. |
| 1723 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 1724 | "Host: www.google.com\r\n" |
| 1725 | "Proxy-Connection: keep-alive\r\n" |
| 1726 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 1727 | |
| 1728 | MockWrite("GET / HTTP/1.1\r\n" |
| 1729 | "Host: www.google.com\r\n" |
| 1730 | "Connection: keep-alive\r\n\r\n"), |
| 1731 | }; |
| 1732 | |
| 1733 | // The proxy responds to the connect with a 407, using a persistent |
| 1734 | // connection. |
| 1735 | MockRead data_reads1[] = { |
| 1736 | // No credentials. |
| 1737 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 1738 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1739 | MockRead("Proxy-Connection: close\r\n\r\n"), |
| 1740 | |
| 1741 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 1742 | |
| 1743 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 1744 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1745 | MockRead("Content-Length: 5\r\n\r\n"), |
| 1746 | MockRead(false, "hello"), |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1747 | }; |
| 1748 | |
| 1749 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1750 | data_writes1, arraysize(data_writes1)); |
| 1751 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 1752 | SSLSocketDataProvider ssl(true, OK); |
| 1753 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 1754 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1755 | TestCompletionCallback callback1; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1756 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1757 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 1758 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1759 | int rv = trans->Start(&request, callback1.callback(), log.bound()); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1760 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1761 | |
| 1762 | rv = callback1.WaitForResult(); |
| 1763 | EXPECT_EQ(OK, rv); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1764 | net::CapturingNetLog::EntryList entries; |
| 1765 | log.GetEntries(&entries); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1766 | size_t pos = ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1767 | entries, 0, NetLog::TYPE_HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1768 | NetLog::PHASE_NONE); |
| 1769 | ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1770 | entries, pos, |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1771 | NetLog::TYPE_HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 1772 | NetLog::PHASE_NONE); |
| 1773 | |
| 1774 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 1775 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 1776 | ASSERT_FALSE(response->headers == NULL); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1777 | EXPECT_EQ(407, response->headers->response_code()); |
| 1778 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 1779 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge.get())); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1780 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1781 | TestCompletionCallback callback2; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1782 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1783 | rv = trans->RestartWithAuth( |
| 1784 | AuthCredentials(kFoo, kBar), callback2.callback()); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1785 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1786 | |
| 1787 | rv = callback2.WaitForResult(); |
| 1788 | EXPECT_EQ(OK, rv); |
| 1789 | |
| 1790 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 1791 | ASSERT_TRUE(response != NULL); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1792 | |
| 1793 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 1794 | EXPECT_EQ(200, response->headers->response_code()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1795 | EXPECT_EQ(5, response->headers->GetContentLength()); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1796 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 1797 | |
| 1798 | // The password prompt info should not be set. |
| 1799 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 1800 | |
| 1801 | trans.reset(); |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 1802 | session->CloseAllConnections(); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 1803 | } |
| 1804 | |
[email protected] | 11203f01 | 2009-11-12 23:02:31 | [diff] [blame] | 1805 | // Test the request-challenge-retry sequence for basic auth, over a keep-alive |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1806 | // proxy connection, when setting up an SSL tunnel. |
| 1807 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyKeepAlive) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1808 | HttpRequestInfo request; |
| 1809 | request.method = "GET"; |
| 1810 | request.url = GURL("https://www.google.com/"); |
| 1811 | // Ensure that proxy authentication is attempted even |
| 1812 | // when the no authentication data flag is set. |
| 1813 | request.load_flags = net::LOAD_DO_NOT_SEND_AUTH_DATA; |
| 1814 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1815 | // Configure against proxy server "myproxy:70". |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 1816 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 1817 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 1818 | session_deps.net_log = log.bound().net_log(); |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 1819 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1820 | |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 1821 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1822 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1823 | // Since we have proxy, should try to establish tunnel. |
| 1824 | MockWrite data_writes1[] = { |
| 1825 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 1826 | "Host: www.google.com\r\n" |
| 1827 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1828 | |
| 1829 | // After calling trans->RestartWithAuth(), this is the request we should |
| 1830 | // be issuing -- the final header line contains the credentials. |
| 1831 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 1832 | "Host: www.google.com\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 1833 | "Proxy-Connection: keep-alive\r\n" |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1834 | "Proxy-Authorization: Basic Zm9vOmJheg==\r\n\r\n"), |
| 1835 | }; |
| 1836 | |
| 1837 | // The proxy responds to the connect with a 407, using a persistent |
| 1838 | // connection. |
| 1839 | MockRead data_reads1[] = { |
| 1840 | // No credentials. |
| 1841 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 1842 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1843 | MockRead("Content-Length: 10\r\n\r\n"), |
| 1844 | MockRead("0123456789"), |
| 1845 | |
| 1846 | // Wrong credentials (wrong password). |
| 1847 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 1848 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1849 | MockRead("Content-Length: 10\r\n\r\n"), |
| 1850 | // No response body because the test stops reading here. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1851 | MockRead(false, ERR_UNEXPECTED), // Should not be reached. |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1852 | }; |
| 1853 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1854 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1855 | data_writes1, arraysize(data_writes1)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1856 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1857 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1858 | TestCompletionCallback callback1; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1859 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1860 | int rv = trans->Start(&request, callback1.callback(), log.bound()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1861 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1862 | |
| 1863 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1864 | EXPECT_EQ(OK, rv); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1865 | net::CapturingNetLog::EntryList entries; |
| 1866 | log.GetEntries(&entries); |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 1867 | size_t pos = ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1868 | entries, 0, NetLog::TYPE_HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 1869 | NetLog::PHASE_NONE); |
| 1870 | ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1871 | entries, pos, |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 1872 | NetLog::TYPE_HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 1873 | NetLog::PHASE_NONE); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1874 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1875 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 1876 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 1877 | ASSERT_FALSE(response->headers == NULL); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1878 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 1879 | EXPECT_EQ(407, response->headers->response_code()); |
| 1880 | EXPECT_EQ(10, response->headers->GetContentLength()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1881 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 1882 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge.get())); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1883 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1884 | TestCompletionCallback callback2; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1885 | |
| 1886 | // Wrong password (should be "bar"). |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1887 | rv = trans->RestartWithAuth( |
| 1888 | AuthCredentials(kFoo, kBaz), callback2.callback()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1889 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1890 | |
| 1891 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1892 | EXPECT_EQ(OK, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1893 | |
| 1894 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 1895 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 1896 | ASSERT_FALSE(response->headers == NULL); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1897 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 1898 | EXPECT_EQ(407, response->headers->response_code()); |
| 1899 | EXPECT_EQ(10, response->headers->GetContentLength()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1900 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 1901 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge.get())); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 1902 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 1903 | // Flush the idle socket before the NetLog and HttpNetworkTransaction go |
| 1904 | // out of scope. |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 1905 | session->CloseAllConnections(); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1906 | } |
| 1907 | |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1908 | // Test that we don't read the response body when we fail to establish a tunnel, |
| 1909 | // even if the user cancels the proxy's auth attempt. |
| 1910 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyCancelTunnel) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1911 | HttpRequestInfo request; |
| 1912 | request.method = "GET"; |
| 1913 | request.url = GURL("https://www.google.com/"); |
| 1914 | request.load_flags = 0; |
| 1915 | |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1916 | // Configure against proxy server "myproxy:70". |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 1917 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1918 | |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 1919 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1920 | |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 1921 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1922 | |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1923 | // Since we have proxy, should try to establish tunnel. |
| 1924 | MockWrite data_writes[] = { |
| 1925 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 1926 | "Host: www.google.com\r\n" |
| 1927 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1928 | }; |
| 1929 | |
| 1930 | // The proxy responds to the connect with a 407. |
| 1931 | MockRead data_reads[] = { |
| 1932 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 1933 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1934 | MockRead("Content-Length: 10\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1935 | MockRead(false, ERR_UNEXPECTED), // Should not be reached. |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1936 | }; |
| 1937 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 1938 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 1939 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 1940 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1941 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1942 | TestCompletionCallback callback; |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1943 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1944 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1945 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1946 | |
| 1947 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1948 | EXPECT_EQ(OK, rv); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1949 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1950 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 1951 | ASSERT_TRUE(response != NULL); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1952 | |
| 1953 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 1954 | EXPECT_EQ(407, response->headers->response_code()); |
| 1955 | EXPECT_EQ(10, response->headers->GetContentLength()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1956 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1957 | |
| 1958 | std::string response_data; |
| 1959 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1960 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, rv); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 1961 | |
| 1962 | // Flush the idle socket before the HttpNetworkTransaction goes out of scope. |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 1963 | session->CloseAllConnections(); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 1964 | } |
| 1965 | |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 1966 | // Test when a server (non-proxy) returns a 407 (proxy-authenticate). |
| 1967 | // The request should fail with ERR_UNEXPECTED_PROXY_AUTH. |
| 1968 | TEST_F(HttpNetworkTransactionTest, UnexpectedProxyAuth) { |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 1969 | HttpRequestInfo request; |
| 1970 | request.method = "GET"; |
| 1971 | request.url = GURL("http://www.google.com/"); |
| 1972 | request.load_flags = 0; |
| 1973 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 1974 | // We are using a DIRECT connection (i.e. no proxy) for this session. |
| 1975 | SessionDependencies session_deps; |
| 1976 | scoped_ptr<HttpTransaction> trans( |
| 1977 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 1978 | |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 1979 | MockWrite data_writes1[] = { |
| 1980 | MockWrite("GET / HTTP/1.1\r\n" |
| 1981 | "Host: www.google.com\r\n" |
| 1982 | "Connection: keep-alive\r\n\r\n"), |
| 1983 | }; |
| 1984 | |
| 1985 | MockRead data_reads1[] = { |
| 1986 | MockRead("HTTP/1.0 407 Proxy Auth required\r\n"), |
| 1987 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 1988 | // Large content-length -- won't matter, as connection will be reset. |
| 1989 | MockRead("Content-Length: 10000\r\n\r\n"), |
| 1990 | MockRead(false, ERR_FAILED), |
| 1991 | }; |
| 1992 | |
| 1993 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 1994 | data_writes1, arraysize(data_writes1)); |
| 1995 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 1996 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1997 | TestCompletionCallback callback; |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 1998 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1999 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 8fdbcd2 | 2010-05-05 02:54:52 | [diff] [blame] | 2000 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2001 | |
| 2002 | rv = callback.WaitForResult(); |
| 2003 | EXPECT_EQ(ERR_UNEXPECTED_PROXY_AUTH, rv); |
| 2004 | } |
| 2005 | |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 2006 | // Tests when an HTTPS server (non-proxy) returns a 407 (proxy-authentication) |
| 2007 | // through a non-authenticating proxy. The request should fail with |
| 2008 | // ERR_UNEXPECTED_PROXY_AUTH. |
| 2009 | // Note that it is impossible to detect if an HTTP server returns a 407 through |
| 2010 | // a non-authenticating proxy - there is nothing to indicate whether the |
| 2011 | // response came from the proxy or the server, so it is treated as if the proxy |
| 2012 | // issued the challenge. |
| 2013 | TEST_F(HttpNetworkTransactionTest, HttpsServerRequestsProxyAuthThroughProxy) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 2014 | HttpRequestInfo request; |
| 2015 | request.method = "GET"; |
| 2016 | request.url = GURL("https://www.google.com/"); |
| 2017 | |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 2018 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
| 2019 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 2020 | session_deps.net_log = log.bound().net_log(); |
| 2021 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 2022 | |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 2023 | // Since we have proxy, should try to establish tunnel. |
| 2024 | MockWrite data_writes1[] = { |
| 2025 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 2026 | "Host: www.google.com\r\n" |
| 2027 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 2028 | |
| 2029 | MockWrite("GET / HTTP/1.1\r\n" |
| 2030 | "Host: www.google.com\r\n" |
| 2031 | "Connection: keep-alive\r\n\r\n"), |
| 2032 | }; |
| 2033 | |
| 2034 | MockRead data_reads1[] = { |
| 2035 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 2036 | |
| 2037 | MockRead("HTTP/1.1 407 Unauthorized\r\n"), |
| 2038 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 2039 | MockRead("\r\n"), |
| 2040 | MockRead(false, OK), |
| 2041 | }; |
| 2042 | |
| 2043 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 2044 | data_writes1, arraysize(data_writes1)); |
| 2045 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 2046 | SSLSocketDataProvider ssl(true, OK); |
| 2047 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 2048 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2049 | TestCompletionCallback callback1; |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 2050 | |
| 2051 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 2052 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2053 | int rv = trans->Start(&request, callback1.callback(), log.bound()); |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 2054 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2055 | |
| 2056 | rv = callback1.WaitForResult(); |
| 2057 | EXPECT_EQ(ERR_UNEXPECTED_PROXY_AUTH, rv); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 2058 | net::CapturingNetLog::EntryList entries; |
| 2059 | log.GetEntries(&entries); |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 2060 | size_t pos = ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 2061 | entries, 0, NetLog::TYPE_HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 2062 | NetLog::PHASE_NONE); |
| 2063 | ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 2064 | entries, pos, |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 2065 | NetLog::TYPE_HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 2066 | NetLog::PHASE_NONE); |
| 2067 | } |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2068 | |
| 2069 | // Test a simple get through an HTTPS Proxy. |
| 2070 | TEST_F(HttpNetworkTransactionTest, HttpsProxyGet) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 2071 | HttpRequestInfo request; |
| 2072 | request.method = "GET"; |
| 2073 | request.url = GURL("http://www.google.com/"); |
| 2074 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2075 | // Configure against https proxy server "proxy:70". |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 2076 | SessionDependencies session_deps(ProxyService::CreateFixed( |
| 2077 | "https://proxy:70")); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2078 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 2079 | session_deps.net_log = log.bound().net_log(); |
| 2080 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 2081 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2082 | // Since we have proxy, should use full url |
| 2083 | MockWrite data_writes1[] = { |
| 2084 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 2085 | "Host: www.google.com\r\n" |
| 2086 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 2087 | }; |
| 2088 | |
| 2089 | MockRead data_reads1[] = { |
| 2090 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 2091 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2092 | MockRead("Content-Length: 100\r\n\r\n"), |
| 2093 | MockRead(false, OK), |
| 2094 | }; |
| 2095 | |
| 2096 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 2097 | data_writes1, arraysize(data_writes1)); |
| 2098 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 2099 | SSLSocketDataProvider ssl(true, OK); |
| 2100 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 2101 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2102 | TestCompletionCallback callback1; |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2103 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2104 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 2105 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2106 | int rv = trans->Start(&request, callback1.callback(), log.bound()); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2107 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2108 | |
| 2109 | rv = callback1.WaitForResult(); |
| 2110 | EXPECT_EQ(OK, rv); |
| 2111 | |
| 2112 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 2113 | ASSERT_TRUE(response != NULL); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2114 | |
| 2115 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 2116 | EXPECT_EQ(200, response->headers->response_code()); |
| 2117 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 2118 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 2119 | |
| 2120 | // The password prompt info should not be set. |
| 2121 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 2122 | } |
| 2123 | |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 2124 | // Test a SPDY get through an HTTPS Proxy. |
| 2125 | TEST_F(HttpNetworkTransactionTest, HttpsProxySpdyGet) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 2126 | HttpRequestInfo request; |
| 2127 | request.method = "GET"; |
| 2128 | request.url = GURL("http://www.google.com/"); |
| 2129 | request.load_flags = 0; |
| 2130 | |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 2131 | // Configure against https proxy server "proxy:70". |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 2132 | SessionDependencies session_deps(ProxyService::CreateFixed( |
| 2133 | "https://proxy:70")); |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 2134 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 2135 | session_deps.net_log = log.bound().net_log(); |
| 2136 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 2137 | |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 2138 | // fetch http://www.google.com/ via SPDY |
| 2139 | scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST, |
| 2140 | false)); |
| 2141 | MockWrite spdy_writes[] = { CreateMockWrite(*req) }; |
| 2142 | |
| 2143 | scoped_ptr<spdy::SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 2144 | scoped_ptr<spdy::SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); |
| 2145 | MockRead spdy_reads[] = { |
| 2146 | CreateMockRead(*resp), |
| 2147 | CreateMockRead(*data), |
| 2148 | MockRead(true, 0, 0), |
| 2149 | }; |
| 2150 | |
[email protected] | a159531 | 2012-01-22 03:25:04 | [diff] [blame] | 2151 | scoped_ptr<DelayedSocketData> spdy_data( |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 2152 | new DelayedSocketData( |
| 2153 | 1, // wait for one write to finish before reading. |
| 2154 | spdy_reads, arraysize(spdy_reads), |
| 2155 | spdy_writes, arraysize(spdy_writes))); |
[email protected] | a159531 | 2012-01-22 03:25:04 | [diff] [blame] | 2156 | session_deps.socket_factory.AddSocketDataProvider(spdy_data.get()); |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 2157 | |
| 2158 | SSLSocketDataProvider ssl(true, OK); |
| 2159 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 2160 | ssl.next_proto = "spdy/2.1"; |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 2161 | ssl.was_npn_negotiated = true; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 2162 | ssl.protocol_negotiated = SSLClientSocket::kProtoSPDY21; |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 2163 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 2164 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2165 | TestCompletionCallback callback1; |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 2166 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2167 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 2168 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2169 | int rv = trans->Start(&request, callback1.callback(), log.bound()); |
[email protected] | 7642b5ae | 2010-09-01 20:55:17 | [diff] [blame] | 2170 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2171 | |
| 2172 | rv = callback1.WaitForResult(); |
| 2173 | EXPECT_EQ(OK, rv); |
| 2174 | |
| 2175 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 2176 | ASSERT_TRUE(response != NULL); |
| 2177 | ASSERT_TRUE(response->headers != NULL); |
| 2178 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 2179 | |
| 2180 | std::string response_data; |
| 2181 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 2182 | EXPECT_EQ(net::kUploadData, response_data); |
| 2183 | } |
| 2184 | |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 2185 | // Test a SPDY get through an HTTPS Proxy. |
| 2186 | TEST_F(HttpNetworkTransactionTest, HttpsProxySpdyGetWithProxyAuth) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 2187 | HttpRequestInfo request; |
| 2188 | request.method = "GET"; |
| 2189 | request.url = GURL("http://www.google.com/"); |
| 2190 | request.load_flags = 0; |
| 2191 | |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 2192 | // Configure against https proxy server "myproxy:70". |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 2193 | SessionDependencies session_deps( |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 2194 | ProxyService::CreateFixed("https://myproxy:70")); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 2195 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 2196 | session_deps.net_log = log.bound().net_log(); |
| 2197 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 2198 | |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 2199 | // The first request will be a bare GET, the second request will be a |
| 2200 | // GET with a Proxy-Authorization header. |
| 2201 | scoped_ptr<spdy::SpdyFrame> req_get( |
| 2202 | ConstructSpdyGet(NULL, 0, false, 1, LOWEST, false)); |
| 2203 | const char* const kExtraAuthorizationHeaders[] = { |
| 2204 | "proxy-authorization", |
| 2205 | "Basic Zm9vOmJhcg==", |
| 2206 | }; |
| 2207 | scoped_ptr<spdy::SpdyFrame> req_get_authorization( |
| 2208 | ConstructSpdyGet( |
| 2209 | kExtraAuthorizationHeaders, arraysize(kExtraAuthorizationHeaders)/2, |
| 2210 | false, 3, LOWEST, false)); |
| 2211 | MockWrite spdy_writes[] = { |
| 2212 | CreateMockWrite(*req_get, 1), |
| 2213 | CreateMockWrite(*req_get_authorization, 4), |
| 2214 | }; |
| 2215 | |
| 2216 | // The first response is a 407 proxy authentication challenge, and the second |
| 2217 | // response will be a 200 response since the second request includes a valid |
| 2218 | // Authorization header. |
| 2219 | const char* const kExtraAuthenticationHeaders[] = { |
[email protected] | fb9e431 | 2012-02-17 06:27:26 | [diff] [blame^] | 2220 | "proxy-authenticate", |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 2221 | "Basic realm=\"MyRealm1\"" |
| 2222 | }; |
| 2223 | scoped_ptr<spdy::SpdyFrame> resp_authentication( |
| 2224 | ConstructSpdySynReplyError( |
| 2225 | "407 Proxy Authentication Required", |
| 2226 | kExtraAuthenticationHeaders, arraysize(kExtraAuthenticationHeaders)/2, |
| 2227 | 1)); |
| 2228 | scoped_ptr<spdy::SpdyFrame> body_authentication( |
| 2229 | ConstructSpdyBodyFrame(1, true)); |
| 2230 | scoped_ptr<spdy::SpdyFrame> resp_data(ConstructSpdyGetSynReply(NULL, 0, 3)); |
| 2231 | scoped_ptr<spdy::SpdyFrame> body_data(ConstructSpdyBodyFrame(3, true)); |
| 2232 | MockRead spdy_reads[] = { |
| 2233 | CreateMockRead(*resp_authentication, 2), |
| 2234 | CreateMockRead(*body_authentication, 3), |
| 2235 | CreateMockRead(*resp_data, 5), |
| 2236 | CreateMockRead(*body_data, 6), |
| 2237 | MockRead(true, 0, 7), |
| 2238 | }; |
| 2239 | |
[email protected] | a159531 | 2012-01-22 03:25:04 | [diff] [blame] | 2240 | scoped_ptr<OrderedSocketData> data( |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 2241 | new OrderedSocketData(spdy_reads, arraysize(spdy_reads), |
| 2242 | spdy_writes, arraysize(spdy_writes))); |
[email protected] | a159531 | 2012-01-22 03:25:04 | [diff] [blame] | 2243 | session_deps.socket_factory.AddSocketDataProvider(data.get()); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 2244 | |
| 2245 | SSLSocketDataProvider ssl(true, OK); |
| 2246 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 2247 | ssl.next_proto = "spdy/2.1"; |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 2248 | ssl.was_npn_negotiated = true; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 2249 | ssl.protocol_negotiated = SSLClientSocket::kProtoSPDY21; |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 2250 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 2251 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2252 | TestCompletionCallback callback1; |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 2253 | |
| 2254 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 2255 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2256 | int rv = trans->Start(&request, callback1.callback(), log.bound()); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 2257 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2258 | |
| 2259 | rv = callback1.WaitForResult(); |
| 2260 | EXPECT_EQ(OK, rv); |
| 2261 | |
| 2262 | const HttpResponseInfo* const response = trans->GetResponseInfo(); |
| 2263 | |
| 2264 | ASSERT_TRUE(response != NULL); |
| 2265 | ASSERT_TRUE(response->headers != NULL); |
| 2266 | EXPECT_EQ(407, response->headers->response_code()); |
| 2267 | EXPECT_TRUE(response->was_fetched_via_spdy); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 2268 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge.get())); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 2269 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2270 | TestCompletionCallback callback2; |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 2271 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2272 | rv = trans->RestartWithAuth( |
| 2273 | AuthCredentials(kFoo, kBar), callback2.callback()); |
[email protected] | dc7bd1c5 | 2010-11-12 00:01:13 | [diff] [blame] | 2274 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2275 | |
| 2276 | rv = callback2.WaitForResult(); |
| 2277 | EXPECT_EQ(OK, rv); |
| 2278 | |
| 2279 | const HttpResponseInfo* const response_restart = trans->GetResponseInfo(); |
| 2280 | |
| 2281 | ASSERT_TRUE(response_restart != NULL); |
| 2282 | ASSERT_TRUE(response_restart->headers != NULL); |
| 2283 | EXPECT_EQ(200, response_restart->headers->response_code()); |
| 2284 | // The password prompt info should not be set. |
| 2285 | EXPECT_TRUE(response_restart->auth_challenge.get() == NULL); |
| 2286 | } |
| 2287 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2288 | // Test a SPDY CONNECT through an HTTPS Proxy to an HTTPS (non-SPDY) Server. |
| 2289 | TEST_F(HttpNetworkTransactionTest, HttpsProxySpdyConnectHttps) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 2290 | HttpRequestInfo request; |
| 2291 | request.method = "GET"; |
| 2292 | request.url = GURL("https://www.google.com/"); |
| 2293 | request.load_flags = 0; |
| 2294 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2295 | // Configure against https proxy server "proxy:70". |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 2296 | SessionDependencies session_deps(ProxyService::CreateFixed( |
| 2297 | "https://proxy:70")); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2298 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 2299 | session_deps.net_log = log.bound().net_log(); |
| 2300 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 2301 | |
| 2302 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 2303 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2304 | // CONNECT to www.google.com:443 via SPDY |
| 2305 | scoped_ptr<spdy::SpdyFrame> connect(ConstructSpdyConnect(NULL, 0, 1)); |
| 2306 | // fetch https://www.google.com/ via HTTP |
| 2307 | |
| 2308 | const char get[] = "GET / HTTP/1.1\r\n" |
| 2309 | "Host: www.google.com\r\n" |
| 2310 | "Connection: keep-alive\r\n\r\n"; |
| 2311 | scoped_ptr<spdy::SpdyFrame> wrapped_get( |
| 2312 | ConstructSpdyBodyFrame(1, get, strlen(get), false)); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2313 | scoped_ptr<spdy::SpdyFrame> conn_resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 2314 | const char resp[] = "HTTP/1.1 200 OK\r\n" |
| 2315 | "Content-Length: 10\r\n\r\n"; |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2316 | scoped_ptr<spdy::SpdyFrame> wrapped_get_resp( |
| 2317 | ConstructSpdyBodyFrame(1, resp, strlen(resp), false)); |
| 2318 | scoped_ptr<spdy::SpdyFrame> wrapped_body( |
| 2319 | ConstructSpdyBodyFrame(1, "1234567890", 10, false)); |
[email protected] | 8d2f701 | 2012-02-16 00:08:04 | [diff] [blame] | 2320 | scoped_ptr<spdy::SpdyFrame> window_update( |
| 2321 | ConstructSpdyWindowUpdate(1, wrapped_get_resp->length())); |
| 2322 | |
| 2323 | MockWrite spdy_writes[] = { |
| 2324 | CreateMockWrite(*connect, 1), |
| 2325 | CreateMockWrite(*wrapped_get, 3), |
| 2326 | CreateMockWrite(*window_update, 5) |
| 2327 | }; |
| 2328 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2329 | MockRead spdy_reads[] = { |
| 2330 | CreateMockRead(*conn_resp, 2, true), |
| 2331 | CreateMockRead(*wrapped_get_resp, 4, true), |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2332 | CreateMockRead(*wrapped_body, 6, true), |
[email protected] | 8d2f701 | 2012-02-16 00:08:04 | [diff] [blame] | 2333 | CreateMockRead(*wrapped_body, 7, true), |
| 2334 | MockRead(true, 0, 8), |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2335 | }; |
| 2336 | |
[email protected] | a159531 | 2012-01-22 03:25:04 | [diff] [blame] | 2337 | scoped_ptr<OrderedSocketData> spdy_data( |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2338 | new OrderedSocketData( |
| 2339 | spdy_reads, arraysize(spdy_reads), |
| 2340 | spdy_writes, arraysize(spdy_writes))); |
[email protected] | a159531 | 2012-01-22 03:25:04 | [diff] [blame] | 2341 | session_deps.socket_factory.AddSocketDataProvider(spdy_data.get()); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2342 | |
| 2343 | SSLSocketDataProvider ssl(true, OK); |
| 2344 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 2345 | ssl.next_proto = "spdy/2.1"; |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2346 | ssl.was_npn_negotiated = true; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 2347 | ssl.protocol_negotiated = SSLClientSocket::kProtoSPDY21; |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2348 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 2349 | SSLSocketDataProvider ssl2(true, OK); |
| 2350 | ssl2.was_npn_negotiated = false; |
[email protected] | c30bcce | 2011-12-20 17:50:51 | [diff] [blame] | 2351 | ssl2.protocol_negotiated = SSLClientSocket::kProtoUnknown; |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2352 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl2); |
| 2353 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2354 | TestCompletionCallback callback1; |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2355 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2356 | int rv = trans->Start(&request, callback1.callback(), log.bound()); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2357 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2358 | |
| 2359 | rv = callback1.WaitForResult(); |
| 2360 | EXPECT_EQ(OK, rv); |
| 2361 | |
| 2362 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 2363 | ASSERT_TRUE(response != NULL); |
| 2364 | ASSERT_TRUE(response->headers != NULL); |
| 2365 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 2366 | |
| 2367 | std::string response_data; |
| 2368 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 2369 | EXPECT_EQ("1234567890", response_data); |
| 2370 | } |
| 2371 | |
| 2372 | // Test a SPDY CONNECT through an HTTPS Proxy to a SPDY server. |
| 2373 | TEST_F(HttpNetworkTransactionTest, HttpsProxySpdyConnectSpdy) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 2374 | HttpRequestInfo request; |
| 2375 | request.method = "GET"; |
| 2376 | request.url = GURL("https://www.google.com/"); |
| 2377 | request.load_flags = 0; |
| 2378 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2379 | // Configure against https proxy server "proxy:70". |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 2380 | SessionDependencies session_deps(ProxyService::CreateFixed( |
| 2381 | "https://proxy:70")); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2382 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 2383 | session_deps.net_log = log.bound().net_log(); |
| 2384 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 2385 | |
| 2386 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 2387 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2388 | // CONNECT to www.google.com:443 via SPDY |
| 2389 | scoped_ptr<spdy::SpdyFrame> connect(ConstructSpdyConnect(NULL, 0, 1)); |
| 2390 | // fetch https://www.google.com/ via SPDY |
| 2391 | const char* const kMyUrl = "https://www.google.com/"; |
| 2392 | scoped_ptr<spdy::SpdyFrame> get(ConstructSpdyGet(kMyUrl, false, 1, LOWEST)); |
| 2393 | scoped_ptr<spdy::SpdyFrame> wrapped_get(ConstructWrappedSpdyFrame(get, 1)); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2394 | scoped_ptr<spdy::SpdyFrame> conn_resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 2395 | scoped_ptr<spdy::SpdyFrame> get_resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 2396 | scoped_ptr<spdy::SpdyFrame> wrapped_get_resp( |
| 2397 | ConstructWrappedSpdyFrame(get_resp, 1)); |
| 2398 | scoped_ptr<spdy::SpdyFrame> body(ConstructSpdyBodyFrame(1, true)); |
| 2399 | scoped_ptr<spdy::SpdyFrame> wrapped_body(ConstructWrappedSpdyFrame(body, 1)); |
[email protected] | 8d2f701 | 2012-02-16 00:08:04 | [diff] [blame] | 2400 | scoped_ptr<spdy::SpdyFrame> window_update_get_resp( |
| 2401 | ConstructSpdyWindowUpdate(1, wrapped_get_resp->length())); |
| 2402 | scoped_ptr<spdy::SpdyFrame> window_update_body( |
| 2403 | ConstructSpdyWindowUpdate(1, wrapped_body->length())); |
| 2404 | |
| 2405 | MockWrite spdy_writes[] = { |
| 2406 | CreateMockWrite(*connect, 1), |
| 2407 | CreateMockWrite(*wrapped_get, 3), |
| 2408 | CreateMockWrite(*window_update_get_resp, 5), |
| 2409 | CreateMockWrite(*window_update_body, 7), |
| 2410 | }; |
| 2411 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2412 | MockRead spdy_reads[] = { |
| 2413 | CreateMockRead(*conn_resp, 2, true), |
| 2414 | CreateMockRead(*wrapped_get_resp, 4, true), |
[email protected] | 8d2f701 | 2012-02-16 00:08:04 | [diff] [blame] | 2415 | CreateMockRead(*wrapped_body, 6, true), |
| 2416 | MockRead(true, 0, 8), |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2417 | }; |
| 2418 | |
[email protected] | a159531 | 2012-01-22 03:25:04 | [diff] [blame] | 2419 | scoped_ptr<OrderedSocketData> spdy_data( |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2420 | new OrderedSocketData( |
| 2421 | spdy_reads, arraysize(spdy_reads), |
| 2422 | spdy_writes, arraysize(spdy_writes))); |
[email protected] | a159531 | 2012-01-22 03:25:04 | [diff] [blame] | 2423 | session_deps.socket_factory.AddSocketDataProvider(spdy_data.get()); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2424 | |
| 2425 | SSLSocketDataProvider ssl(true, OK); |
| 2426 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 2427 | ssl.next_proto = "spdy/2.1"; |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2428 | ssl.was_npn_negotiated = true; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 2429 | ssl.protocol_negotiated = SSLClientSocket::kProtoSPDY21; |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2430 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 2431 | SSLSocketDataProvider ssl2(true, OK); |
| 2432 | ssl2.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 2433 | ssl2.next_proto = "spdy/2.1"; |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2434 | ssl2.was_npn_negotiated = true; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 2435 | ssl2.protocol_negotiated = SSLClientSocket::kProtoSPDY21; |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2436 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl2); |
| 2437 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2438 | TestCompletionCallback callback1; |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2439 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2440 | int rv = trans->Start(&request, callback1.callback(), log.bound()); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2441 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2442 | |
| 2443 | rv = callback1.WaitForResult(); |
| 2444 | EXPECT_EQ(OK, rv); |
| 2445 | |
| 2446 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 2447 | ASSERT_TRUE(response != NULL); |
| 2448 | ASSERT_TRUE(response->headers != NULL); |
| 2449 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 2450 | |
| 2451 | std::string response_data; |
| 2452 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 2453 | EXPECT_EQ(net::kUploadData, response_data); |
| 2454 | } |
| 2455 | |
| 2456 | // Test a SPDY CONNECT failure through an HTTPS Proxy. |
| 2457 | TEST_F(HttpNetworkTransactionTest, HttpsProxySpdyConnectFailure) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 2458 | HttpRequestInfo request; |
| 2459 | request.method = "GET"; |
| 2460 | request.url = GURL("https://www.google.com/"); |
| 2461 | request.load_flags = 0; |
| 2462 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2463 | // Configure against https proxy server "proxy:70". |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 2464 | SessionDependencies session_deps(ProxyService::CreateFixed( |
| 2465 | "https://proxy:70")); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2466 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 2467 | session_deps.net_log = log.bound().net_log(); |
| 2468 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 2469 | |
| 2470 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 2471 | |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2472 | // CONNECT to www.google.com:443 via SPDY |
| 2473 | scoped_ptr<spdy::SpdyFrame> connect(ConstructSpdyConnect(NULL, 0, 1)); |
| 2474 | scoped_ptr<spdy::SpdyFrame> get(ConstructSpdyRstStream(1, spdy::CANCEL)); |
| 2475 | |
| 2476 | MockWrite spdy_writes[] = { |
| 2477 | CreateMockWrite(*connect, 1), |
| 2478 | CreateMockWrite(*get, 3), |
| 2479 | }; |
| 2480 | |
| 2481 | scoped_ptr<spdy::SpdyFrame> resp(ConstructSpdySynReplyError(1)); |
| 2482 | scoped_ptr<spdy::SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); |
| 2483 | MockRead spdy_reads[] = { |
| 2484 | CreateMockRead(*resp, 2, true), |
| 2485 | MockRead(true, 0, 4), |
| 2486 | }; |
| 2487 | |
[email protected] | a159531 | 2012-01-22 03:25:04 | [diff] [blame] | 2488 | scoped_ptr<OrderedSocketData> spdy_data( |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2489 | new OrderedSocketData( |
| 2490 | spdy_reads, arraysize(spdy_reads), |
| 2491 | spdy_writes, arraysize(spdy_writes))); |
[email protected] | a159531 | 2012-01-22 03:25:04 | [diff] [blame] | 2492 | session_deps.socket_factory.AddSocketDataProvider(spdy_data.get()); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2493 | |
| 2494 | SSLSocketDataProvider ssl(true, OK); |
| 2495 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 2496 | ssl.next_proto = "spdy/2.1"; |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2497 | ssl.was_npn_negotiated = true; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 2498 | ssl.protocol_negotiated = SSLClientSocket::kProtoSPDY21; |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2499 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 2500 | SSLSocketDataProvider ssl2(true, OK); |
| 2501 | ssl2.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 2502 | ssl2.next_proto = "spdy/2.1"; |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2503 | ssl2.was_npn_negotiated = true; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 2504 | ssl.protocol_negotiated = SSLClientSocket::kProtoSPDY21; |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2505 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl2); |
| 2506 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2507 | TestCompletionCallback callback1; |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2508 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2509 | int rv = trans->Start(&request, callback1.callback(), log.bound()); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2510 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2511 | |
| 2512 | rv = callback1.WaitForResult(); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 2513 | EXPECT_EQ(OK, rv); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2514 | |
| 2515 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 2516 | ASSERT_TRUE(response != NULL); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 2517 | EXPECT_EQ(500, response->headers->response_code()); |
[email protected] | d9da5fe | 2010-10-13 22:37:16 | [diff] [blame] | 2518 | } |
| 2519 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2520 | // Test the challenge-response-retry sequence through an HTTPS Proxy |
| 2521 | TEST_F(HttpNetworkTransactionTest, HttpsProxyAuthRetry) { |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2522 | HttpRequestInfo request; |
| 2523 | request.method = "GET"; |
| 2524 | request.url = GURL("http://www.google.com/"); |
| 2525 | // when the no authentication data flag is set. |
| 2526 | request.load_flags = net::LOAD_DO_NOT_SEND_AUTH_DATA; |
| 2527 | |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 2528 | // Configure against https proxy server "myproxy:70". |
| 2529 | SessionDependencies session_deps( |
| 2530 | ProxyService::CreateFixed("https://myproxy:70")); |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 2531 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 2532 | session_deps.net_log = log.bound().net_log(); |
| 2533 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 2534 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2535 | // Since we have proxy, should use full url |
| 2536 | MockWrite data_writes1[] = { |
| 2537 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 2538 | "Host: www.google.com\r\n" |
| 2539 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 2540 | |
| 2541 | // After calling trans->RestartWithAuth(), this is the request we should |
| 2542 | // be issuing -- the final header line contains the credentials. |
| 2543 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 2544 | "Host: www.google.com\r\n" |
| 2545 | "Proxy-Connection: keep-alive\r\n" |
| 2546 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 2547 | }; |
| 2548 | |
| 2549 | // The proxy responds to the GET with a 407, using a persistent |
| 2550 | // connection. |
| 2551 | MockRead data_reads1[] = { |
| 2552 | // No credentials. |
| 2553 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 2554 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 2555 | MockRead("Proxy-Connection: keep-alive\r\n"), |
| 2556 | MockRead("Content-Length: 0\r\n\r\n"), |
| 2557 | |
| 2558 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 2559 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2560 | MockRead("Content-Length: 100\r\n\r\n"), |
| 2561 | MockRead(false, OK), |
| 2562 | }; |
| 2563 | |
| 2564 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 2565 | data_writes1, arraysize(data_writes1)); |
| 2566 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 2567 | SSLSocketDataProvider ssl(true, OK); |
| 2568 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 2569 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2570 | TestCompletionCallback callback1; |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2571 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2572 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 2573 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2574 | int rv = trans->Start(&request, callback1.callback(), log.bound()); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2575 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2576 | |
| 2577 | rv = callback1.WaitForResult(); |
| 2578 | EXPECT_EQ(OK, rv); |
| 2579 | |
| 2580 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 2581 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 2582 | ASSERT_FALSE(response->headers == NULL); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2583 | EXPECT_EQ(407, response->headers->response_code()); |
| 2584 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 2585 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge.get())); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2586 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2587 | TestCompletionCallback callback2; |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2588 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2589 | rv = trans->RestartWithAuth( |
| 2590 | AuthCredentials(kFoo, kBar), callback2.callback()); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2591 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2592 | |
| 2593 | rv = callback2.WaitForResult(); |
| 2594 | EXPECT_EQ(OK, rv); |
| 2595 | |
| 2596 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 2597 | ASSERT_TRUE(response != NULL); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 2598 | |
| 2599 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 2600 | EXPECT_EQ(200, response->headers->response_code()); |
| 2601 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 2602 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 2603 | |
| 2604 | // The password prompt info should not be set. |
| 2605 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 2606 | } |
| 2607 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 2608 | void HttpNetworkTransactionTest::ConnectStatusHelperWithExpectedStatus( |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2609 | const MockRead& status, int expected_status) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2610 | HttpRequestInfo request; |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2611 | request.method = "GET"; |
| 2612 | request.url = GURL("https://www.google.com/"); |
| 2613 | request.load_flags = 0; |
| 2614 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 2615 | // Configure against proxy server "myproxy:70". |
| 2616 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
| 2617 | |
| 2618 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 2619 | |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2620 | // Since we have proxy, should try to establish tunnel. |
| 2621 | MockWrite data_writes[] = { |
| 2622 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 2623 | "Host: www.google.com\r\n" |
| 2624 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2625 | }; |
| 2626 | |
| 2627 | MockRead data_reads[] = { |
| 2628 | status, |
| 2629 | MockRead("Content-Length: 10\r\n\r\n"), |
| 2630 | // No response body because the test stops reading here. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2631 | MockRead(false, ERR_UNEXPECTED), // Should not be reached. |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2632 | }; |
| 2633 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 2634 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 2635 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 2636 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2637 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2638 | TestCompletionCallback callback; |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2639 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 2640 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 2641 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2642 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2643 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2644 | |
| 2645 | rv = callback.WaitForResult(); |
| 2646 | EXPECT_EQ(expected_status, rv); |
| 2647 | } |
| 2648 | |
[email protected] | ff007e16 | 2009-05-23 09:13:15 | [diff] [blame] | 2649 | void HttpNetworkTransactionTest::ConnectStatusHelper(const MockRead& status) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2650 | ConnectStatusHelperWithExpectedStatus( |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2651 | status, ERR_TUNNEL_CONNECTION_FAILED); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2652 | } |
| 2653 | |
| 2654 | TEST_F(HttpNetworkTransactionTest, ConnectStatus100) { |
| 2655 | ConnectStatusHelper(MockRead("HTTP/1.1 100 Continue\r\n")); |
| 2656 | } |
| 2657 | |
| 2658 | TEST_F(HttpNetworkTransactionTest, ConnectStatus101) { |
| 2659 | ConnectStatusHelper(MockRead("HTTP/1.1 101 Switching Protocols\r\n")); |
| 2660 | } |
| 2661 | |
| 2662 | TEST_F(HttpNetworkTransactionTest, ConnectStatus201) { |
| 2663 | ConnectStatusHelper(MockRead("HTTP/1.1 201 Created\r\n")); |
| 2664 | } |
| 2665 | |
| 2666 | TEST_F(HttpNetworkTransactionTest, ConnectStatus202) { |
| 2667 | ConnectStatusHelper(MockRead("HTTP/1.1 202 Accepted\r\n")); |
| 2668 | } |
| 2669 | |
| 2670 | TEST_F(HttpNetworkTransactionTest, ConnectStatus203) { |
| 2671 | ConnectStatusHelper( |
| 2672 | MockRead("HTTP/1.1 203 Non-Authoritative Information\r\n")); |
| 2673 | } |
| 2674 | |
| 2675 | TEST_F(HttpNetworkTransactionTest, ConnectStatus204) { |
| 2676 | ConnectStatusHelper(MockRead("HTTP/1.1 204 No Content\r\n")); |
| 2677 | } |
| 2678 | |
| 2679 | TEST_F(HttpNetworkTransactionTest, ConnectStatus205) { |
| 2680 | ConnectStatusHelper(MockRead("HTTP/1.1 205 Reset Content\r\n")); |
| 2681 | } |
| 2682 | |
| 2683 | TEST_F(HttpNetworkTransactionTest, ConnectStatus206) { |
| 2684 | ConnectStatusHelper(MockRead("HTTP/1.1 206 Partial Content\r\n")); |
| 2685 | } |
| 2686 | |
| 2687 | TEST_F(HttpNetworkTransactionTest, ConnectStatus300) { |
| 2688 | ConnectStatusHelper(MockRead("HTTP/1.1 300 Multiple Choices\r\n")); |
| 2689 | } |
| 2690 | |
| 2691 | TEST_F(HttpNetworkTransactionTest, ConnectStatus301) { |
| 2692 | ConnectStatusHelper(MockRead("HTTP/1.1 301 Moved Permanently\r\n")); |
| 2693 | } |
| 2694 | |
| 2695 | TEST_F(HttpNetworkTransactionTest, ConnectStatus302) { |
| 2696 | ConnectStatusHelper(MockRead("HTTP/1.1 302 Found\r\n")); |
| 2697 | } |
| 2698 | |
| 2699 | TEST_F(HttpNetworkTransactionTest, ConnectStatus303) { |
| 2700 | ConnectStatusHelper(MockRead("HTTP/1.1 303 See Other\r\n")); |
| 2701 | } |
| 2702 | |
| 2703 | TEST_F(HttpNetworkTransactionTest, ConnectStatus304) { |
| 2704 | ConnectStatusHelper(MockRead("HTTP/1.1 304 Not Modified\r\n")); |
| 2705 | } |
| 2706 | |
| 2707 | TEST_F(HttpNetworkTransactionTest, ConnectStatus305) { |
| 2708 | ConnectStatusHelper(MockRead("HTTP/1.1 305 Use Proxy\r\n")); |
| 2709 | } |
| 2710 | |
| 2711 | TEST_F(HttpNetworkTransactionTest, ConnectStatus306) { |
| 2712 | ConnectStatusHelper(MockRead("HTTP/1.1 306\r\n")); |
| 2713 | } |
| 2714 | |
| 2715 | TEST_F(HttpNetworkTransactionTest, ConnectStatus307) { |
| 2716 | ConnectStatusHelper(MockRead("HTTP/1.1 307 Temporary Redirect\r\n")); |
| 2717 | } |
| 2718 | |
| 2719 | TEST_F(HttpNetworkTransactionTest, ConnectStatus400) { |
| 2720 | ConnectStatusHelper(MockRead("HTTP/1.1 400 Bad Request\r\n")); |
| 2721 | } |
| 2722 | |
| 2723 | TEST_F(HttpNetworkTransactionTest, ConnectStatus401) { |
| 2724 | ConnectStatusHelper(MockRead("HTTP/1.1 401 Unauthorized\r\n")); |
| 2725 | } |
| 2726 | |
| 2727 | TEST_F(HttpNetworkTransactionTest, ConnectStatus402) { |
| 2728 | ConnectStatusHelper(MockRead("HTTP/1.1 402 Payment Required\r\n")); |
| 2729 | } |
| 2730 | |
| 2731 | TEST_F(HttpNetworkTransactionTest, ConnectStatus403) { |
| 2732 | ConnectStatusHelper(MockRead("HTTP/1.1 403 Forbidden\r\n")); |
| 2733 | } |
| 2734 | |
| 2735 | TEST_F(HttpNetworkTransactionTest, ConnectStatus404) { |
| 2736 | ConnectStatusHelper(MockRead("HTTP/1.1 404 Not Found\r\n")); |
| 2737 | } |
| 2738 | |
| 2739 | TEST_F(HttpNetworkTransactionTest, ConnectStatus405) { |
| 2740 | ConnectStatusHelper(MockRead("HTTP/1.1 405 Method Not Allowed\r\n")); |
| 2741 | } |
| 2742 | |
| 2743 | TEST_F(HttpNetworkTransactionTest, ConnectStatus406) { |
| 2744 | ConnectStatusHelper(MockRead("HTTP/1.1 406 Not Acceptable\r\n")); |
| 2745 | } |
| 2746 | |
| 2747 | TEST_F(HttpNetworkTransactionTest, ConnectStatus407) { |
| 2748 | ConnectStatusHelperWithExpectedStatus( |
| 2749 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 2750 | ERR_PROXY_AUTH_UNSUPPORTED); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 2751 | } |
| 2752 | |
| 2753 | TEST_F(HttpNetworkTransactionTest, ConnectStatus408) { |
| 2754 | ConnectStatusHelper(MockRead("HTTP/1.1 408 Request Timeout\r\n")); |
| 2755 | } |
| 2756 | |
| 2757 | TEST_F(HttpNetworkTransactionTest, ConnectStatus409) { |
| 2758 | ConnectStatusHelper(MockRead("HTTP/1.1 409 Conflict\r\n")); |
| 2759 | } |
| 2760 | |
| 2761 | TEST_F(HttpNetworkTransactionTest, ConnectStatus410) { |
| 2762 | ConnectStatusHelper(MockRead("HTTP/1.1 410 Gone\r\n")); |
| 2763 | } |
| 2764 | |
| 2765 | TEST_F(HttpNetworkTransactionTest, ConnectStatus411) { |
| 2766 | ConnectStatusHelper(MockRead("HTTP/1.1 411 Length Required\r\n")); |
| 2767 | } |
| 2768 | |
| 2769 | TEST_F(HttpNetworkTransactionTest, ConnectStatus412) { |
| 2770 | ConnectStatusHelper(MockRead("HTTP/1.1 412 Precondition Failed\r\n")); |
| 2771 | } |
| 2772 | |
| 2773 | TEST_F(HttpNetworkTransactionTest, ConnectStatus413) { |
| 2774 | ConnectStatusHelper(MockRead("HTTP/1.1 413 Request Entity Too Large\r\n")); |
| 2775 | } |
| 2776 | |
| 2777 | TEST_F(HttpNetworkTransactionTest, ConnectStatus414) { |
| 2778 | ConnectStatusHelper(MockRead("HTTP/1.1 414 Request-URI Too Long\r\n")); |
| 2779 | } |
| 2780 | |
| 2781 | TEST_F(HttpNetworkTransactionTest, ConnectStatus415) { |
| 2782 | ConnectStatusHelper(MockRead("HTTP/1.1 415 Unsupported Media Type\r\n")); |
| 2783 | } |
| 2784 | |
| 2785 | TEST_F(HttpNetworkTransactionTest, ConnectStatus416) { |
| 2786 | ConnectStatusHelper( |
| 2787 | MockRead("HTTP/1.1 416 Requested Range Not Satisfiable\r\n")); |
| 2788 | } |
| 2789 | |
| 2790 | TEST_F(HttpNetworkTransactionTest, ConnectStatus417) { |
| 2791 | ConnectStatusHelper(MockRead("HTTP/1.1 417 Expectation Failed\r\n")); |
| 2792 | } |
| 2793 | |
| 2794 | TEST_F(HttpNetworkTransactionTest, ConnectStatus500) { |
| 2795 | ConnectStatusHelper(MockRead("HTTP/1.1 500 Internal Server Error\r\n")); |
| 2796 | } |
| 2797 | |
| 2798 | TEST_F(HttpNetworkTransactionTest, ConnectStatus501) { |
| 2799 | ConnectStatusHelper(MockRead("HTTP/1.1 501 Not Implemented\r\n")); |
| 2800 | } |
| 2801 | |
| 2802 | TEST_F(HttpNetworkTransactionTest, ConnectStatus502) { |
| 2803 | ConnectStatusHelper(MockRead("HTTP/1.1 502 Bad Gateway\r\n")); |
| 2804 | } |
| 2805 | |
| 2806 | TEST_F(HttpNetworkTransactionTest, ConnectStatus503) { |
| 2807 | ConnectStatusHelper(MockRead("HTTP/1.1 503 Service Unavailable\r\n")); |
| 2808 | } |
| 2809 | |
| 2810 | TEST_F(HttpNetworkTransactionTest, ConnectStatus504) { |
| 2811 | ConnectStatusHelper(MockRead("HTTP/1.1 504 Gateway Timeout\r\n")); |
| 2812 | } |
| 2813 | |
| 2814 | TEST_F(HttpNetworkTransactionTest, ConnectStatus505) { |
| 2815 | ConnectStatusHelper(MockRead("HTTP/1.1 505 HTTP Version Not Supported\r\n")); |
| 2816 | } |
| 2817 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2818 | // Test the flow when both the proxy server AND origin server require |
| 2819 | // authentication. Again, this uses basic auth for both since that is |
| 2820 | // the simplest to mock. |
| 2821 | TEST_F(HttpNetworkTransactionTest, BasicAuthProxyThenServer) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 2822 | HttpRequestInfo request; |
| 2823 | request.method = "GET"; |
| 2824 | request.url = GURL("http://www.google.com/"); |
| 2825 | request.load_flags = 0; |
| 2826 | |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 2827 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
[email protected] | db8f44c | 2008-12-13 04:52:01 | [diff] [blame] | 2828 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2829 | // Configure against proxy server "myproxy:70". |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2830 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 2831 | CreateSession(&session_deps))); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2832 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 2833 | MockWrite data_writes1[] = { |
| 2834 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 2835 | "Host: www.google.com\r\n" |
| 2836 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 2837 | }; |
| 2838 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2839 | MockRead data_reads1[] = { |
| 2840 | MockRead("HTTP/1.0 407 Unauthorized\r\n"), |
| 2841 | // Give a couple authenticate options (only the middle one is actually |
| 2842 | // supported). |
[email protected] | 22927ad | 2009-09-21 19:56:19 | [diff] [blame] | 2843 | MockRead("Proxy-Authenticate: Basic invalid\r\n"), // Malformed. |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2844 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 2845 | MockRead("Proxy-Authenticate: UNSUPPORTED realm=\"FOO\"\r\n"), |
| 2846 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2847 | // Large content-length -- won't matter, as connection will be reset. |
| 2848 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2849 | MockRead(false, ERR_FAILED), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2850 | }; |
| 2851 | |
| 2852 | // After calling trans->RestartWithAuth() the first time, this is the |
| 2853 | // request we should be issuing -- the final header line contains the |
| 2854 | // proxy's credentials. |
| 2855 | MockWrite data_writes2[] = { |
| 2856 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 2857 | "Host: www.google.com\r\n" |
| 2858 | "Proxy-Connection: keep-alive\r\n" |
| 2859 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 2860 | }; |
| 2861 | |
| 2862 | // Now the proxy server lets the request pass through to origin server. |
| 2863 | // The origin server responds with a 401. |
| 2864 | MockRead data_reads2[] = { |
| 2865 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 2866 | // Note: We are using the same realm-name as the proxy server. This is |
| 2867 | // completely valid, as realms are unique across hosts. |
| 2868 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 2869 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2870 | MockRead("Content-Length: 2000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2871 | MockRead(false, ERR_FAILED), // Won't be reached. |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2872 | }; |
| 2873 | |
| 2874 | // After calling trans->RestartWithAuth() the second time, we should send |
| 2875 | // the credentials for both the proxy and origin server. |
| 2876 | MockWrite data_writes3[] = { |
| 2877 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 2878 | "Host: www.google.com\r\n" |
| 2879 | "Proxy-Connection: keep-alive\r\n" |
| 2880 | "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n" |
| 2881 | "Authorization: Basic Zm9vMjpiYXIy\r\n\r\n"), |
| 2882 | }; |
| 2883 | |
| 2884 | // Lastly we get the desired content. |
| 2885 | MockRead data_reads3[] = { |
| 2886 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 2887 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 2888 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2889 | MockRead(false, OK), |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2890 | }; |
| 2891 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 2892 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 2893 | data_writes1, arraysize(data_writes1)); |
| 2894 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 2895 | data_writes2, arraysize(data_writes2)); |
| 2896 | StaticSocketDataProvider data3(data_reads3, arraysize(data_reads3), |
| 2897 | data_writes3, arraysize(data_writes3)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 2898 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 2899 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 2900 | session_deps.socket_factory.AddSocketDataProvider(&data3); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2901 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2902 | TestCompletionCallback callback1; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2903 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2904 | int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2905 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2906 | |
| 2907 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2908 | EXPECT_EQ(OK, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2909 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2910 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 2911 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 2912 | EXPECT_TRUE(CheckBasicProxyAuth(response->auth_challenge.get())); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2913 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2914 | TestCompletionCallback callback2; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2915 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2916 | rv = trans->RestartWithAuth( |
| 2917 | AuthCredentials(kFoo, kBar), callback2.callback()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2918 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2919 | |
| 2920 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2921 | EXPECT_EQ(OK, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2922 | |
| 2923 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 2924 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 2925 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge.get())); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2926 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2927 | TestCompletionCallback callback3; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2928 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 2929 | rv = trans->RestartWithAuth( |
| 2930 | AuthCredentials(kFoo2, kBar2), callback3.callback()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2931 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2932 | |
| 2933 | rv = callback3.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2934 | EXPECT_EQ(OK, rv); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2935 | |
| 2936 | response = trans->GetResponseInfo(); |
| 2937 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 2938 | EXPECT_EQ(100, response->headers->GetContentLength()); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 2939 | } |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 2940 | |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 2941 | // For the NTLM implementation using SSPI, we skip the NTLM tests since we |
| 2942 | // can't hook into its internals to cause it to generate predictable NTLM |
| 2943 | // authorization headers. |
| 2944 | #if defined(NTLM_PORTABLE) |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2945 | // The NTLM authentication unit tests were generated by capturing the HTTP |
| 2946 | // requests and responses using Fiddler 2 and inspecting the generated random |
| 2947 | // bytes in the debugger. |
| 2948 | |
| 2949 | // Enter the correct password and authenticate successfully. |
| 2950 | TEST_F(HttpNetworkTransactionTest, NTLMAuth1) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2951 | HttpRequestInfo request; |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2952 | request.method = "GET"; |
| 2953 | request.url = GURL("http://172.22.68.17/kids/login.aspx"); |
| 2954 | request.load_flags = 0; |
| 2955 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 2956 | HttpAuthHandlerNTLM::ScopedProcSetter proc_setter(MockGenerateRandom1, |
| 2957 | MockGetHostName); |
| 2958 | SessionDependencies session_deps; |
| 2959 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 2960 | |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2961 | MockWrite data_writes1[] = { |
| 2962 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 2963 | "Host: 172.22.68.17\r\n" |
| 2964 | "Connection: keep-alive\r\n\r\n"), |
| 2965 | }; |
| 2966 | |
| 2967 | MockRead data_reads1[] = { |
| 2968 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 2969 | // Negotiate and NTLM are often requested together. However, we only want |
| 2970 | // to test NTLM. Since Negotiate is preferred over NTLM, we have to skip |
| 2971 | // the header that requests Negotiate for this test. |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2972 | MockRead("WWW-Authenticate: NTLM\r\n"), |
| 2973 | MockRead("Connection: close\r\n"), |
| 2974 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 2975 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2976 | // Missing content -- won't matter, as connection will be reset. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 2977 | MockRead(false, ERR_UNEXPECTED), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2978 | }; |
| 2979 | |
| 2980 | MockWrite data_writes2[] = { |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2981 | // After restarting with a null identity, this is the |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 2982 | // request we should be issuing -- the final header line contains a Type |
| 2983 | // 1 message. |
| 2984 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 2985 | "Host: 172.22.68.17\r\n" |
| 2986 | "Connection: keep-alive\r\n" |
| 2987 | "Authorization: NTLM " |
| 2988 | "TlRMTVNTUAABAAAAB4IIAAAAAAAAAAAAAAAAAAAAAAA=\r\n\r\n"), |
| 2989 | |
| 2990 | // After calling trans->RestartWithAuth(), we should send a Type 3 message |
| 2991 | // (the credentials for the origin server). The second request continues |
| 2992 | // on the same connection. |
| 2993 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 2994 | "Host: 172.22.68.17\r\n" |
| 2995 | "Connection: keep-alive\r\n" |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 2996 | "Authorization: NTLM TlRMTVNTUAADAAAAGAAYAGgAAAAYABgAgA" |
| 2997 | "AAAAAAAABAAAAAGAAYAEAAAAAQABAAWAAAAAAAAAAAAAAABYIIAHQA" |
| 2998 | "ZQBzAHQAaQBuAGcALQBuAHQAbABtAFcAVABDAC0AVwBJAE4ANwBVKW" |
| 2999 | "Yma5xzVAAAAAAAAAAAAAAAAAAAAACH+gWcm+YsP9Tqb9zCR3WAeZZX" |
| 3000 | "ahlhx5I=\r\n\r\n"), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 3001 | }; |
| 3002 | |
| 3003 | MockRead data_reads2[] = { |
| 3004 | // The origin server responds with a Type 2 message. |
| 3005 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
| 3006 | MockRead("WWW-Authenticate: NTLM " |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3007 | "TlRMTVNTUAACAAAADAAMADgAAAAFgokCjGpMpPGlYKkAAAAAAAAAALo" |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 3008 | "AugBEAAAABQEoCgAAAA9HAE8ATwBHAEwARQACAAwARwBPAE8ARwBMAE" |
| 3009 | "UAAQAaAEEASwBFAEUAUwBBAFIAQQAtAEMATwBSAFAABAAeAGMAbwByA" |
| 3010 | "HAALgBnAG8AbwBnAGwAZQAuAGMAbwBtAAMAQABhAGsAZQBlAHMAYQBy" |
| 3011 | "AGEALQBjAG8AcgBwAC4AYQBkAC4AYwBvAHIAcAAuAGcAbwBvAGcAbAB" |
| 3012 | "lAC4AYwBvAG0ABQAeAGMAbwByAHAALgBnAG8AbwBnAGwAZQAuAGMAbw" |
| 3013 | "BtAAAAAAA=\r\n"), |
| 3014 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 3015 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 3016 | MockRead("You are not authorized to view this page\r\n"), |
| 3017 | |
| 3018 | // Lastly we get the desired content. |
| 3019 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 3020 | MockRead("Content-Type: text/html; charset=utf-8\r\n"), |
| 3021 | MockRead("Content-Length: 13\r\n\r\n"), |
| 3022 | MockRead("Please Login\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3023 | MockRead(false, OK), |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 3024 | }; |
| 3025 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3026 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3027 | data_writes1, arraysize(data_writes1)); |
| 3028 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 3029 | data_writes2, arraysize(data_writes2)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3030 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3031 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 3032 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3033 | TestCompletionCallback callback1; |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 3034 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 3035 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 3036 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3037 | int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3038 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 3039 | |
| 3040 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3041 | EXPECT_EQ(OK, rv); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 3042 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3043 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 3044 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3045 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 3046 | ASSERT_FALSE(response == NULL); |
| 3047 | EXPECT_TRUE(CheckNTLMServerAuth(response->auth_challenge.get())); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 3048 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3049 | TestCompletionCallback callback2; |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 3050 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3051 | rv = trans->RestartWithAuth(AuthCredentials(kTestingNTLM, kTestingNTLM), |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3052 | callback2.callback()); |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 3053 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3054 | |
| 3055 | rv = callback2.WaitForResult(); |
| 3056 | EXPECT_EQ(OK, rv); |
| 3057 | |
| 3058 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
| 3059 | |
| 3060 | response = trans->GetResponseInfo(); |
| 3061 | ASSERT_TRUE(response != NULL); |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 3062 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3063 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3064 | TestCompletionCallback callback3; |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 3065 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3066 | rv = trans->RestartWithAuth(AuthCredentials(), callback3.callback()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3067 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 3068 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3069 | rv = callback3.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3070 | EXPECT_EQ(OK, rv); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 3071 | |
| 3072 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 3073 | ASSERT_TRUE(response != NULL); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 3074 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3075 | EXPECT_EQ(13, response->headers->GetContentLength()); |
| 3076 | } |
| 3077 | |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3078 | // Enter a wrong password, and then the correct one. |
| 3079 | TEST_F(HttpNetworkTransactionTest, NTLMAuth2) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3080 | HttpRequestInfo request; |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3081 | request.method = "GET"; |
| 3082 | request.url = GURL("http://172.22.68.17/kids/login.aspx"); |
| 3083 | request.load_flags = 0; |
| 3084 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3085 | HttpAuthHandlerNTLM::ScopedProcSetter proc_setter(MockGenerateRandom2, |
| 3086 | MockGetHostName); |
| 3087 | SessionDependencies session_deps; |
| 3088 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 3089 | |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3090 | MockWrite data_writes1[] = { |
| 3091 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 3092 | "Host: 172.22.68.17\r\n" |
| 3093 | "Connection: keep-alive\r\n\r\n"), |
| 3094 | }; |
| 3095 | |
| 3096 | MockRead data_reads1[] = { |
| 3097 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 3098 | // Negotiate and NTLM are often requested together. However, we only want |
| 3099 | // to test NTLM. Since Negotiate is preferred over NTLM, we have to skip |
| 3100 | // the header that requests Negotiate for this test. |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3101 | MockRead("WWW-Authenticate: NTLM\r\n"), |
| 3102 | MockRead("Connection: close\r\n"), |
| 3103 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 3104 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3105 | // Missing content -- won't matter, as connection will be reset. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3106 | MockRead(false, ERR_UNEXPECTED), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3107 | }; |
| 3108 | |
| 3109 | MockWrite data_writes2[] = { |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3110 | // After restarting with a null identity, this is the |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3111 | // request we should be issuing -- the final header line contains a Type |
| 3112 | // 1 message. |
| 3113 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 3114 | "Host: 172.22.68.17\r\n" |
| 3115 | "Connection: keep-alive\r\n" |
| 3116 | "Authorization: NTLM " |
| 3117 | "TlRMTVNTUAABAAAAB4IIAAAAAAAAAAAAAAAAAAAAAAA=\r\n\r\n"), |
| 3118 | |
| 3119 | // After calling trans->RestartWithAuth(), we should send a Type 3 message |
| 3120 | // (the credentials for the origin server). The second request continues |
| 3121 | // on the same connection. |
| 3122 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 3123 | "Host: 172.22.68.17\r\n" |
| 3124 | "Connection: keep-alive\r\n" |
| 3125 | "Authorization: NTLM TlRMTVNTUAADAAAAGAAYAGgAAAAYABgAgA" |
| 3126 | "AAAAAAAABAAAAAGAAYAEAAAAAQABAAWAAAAAAAAAAAAAAABYIIAHQA" |
| 3127 | "ZQBzAHQAaQBuAGcALQBuAHQAbABtAFcAVABDAC0AVwBJAE4ANwCWeY" |
| 3128 | "XnSZNwoQAAAAAAAAAAAAAAAAAAAADLa34/phTTKzNTWdub+uyFleOj" |
| 3129 | "4Ww7b7E=\r\n\r\n"), |
| 3130 | }; |
| 3131 | |
| 3132 | MockRead data_reads2[] = { |
| 3133 | // The origin server responds with a Type 2 message. |
| 3134 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
| 3135 | MockRead("WWW-Authenticate: NTLM " |
| 3136 | "TlRMTVNTUAACAAAADAAMADgAAAAFgokCbVWUZezVGpAAAAAAAAAAALo" |
| 3137 | "AugBEAAAABQEoCgAAAA9HAE8ATwBHAEwARQACAAwARwBPAE8ARwBMAE" |
| 3138 | "UAAQAaAEEASwBFAEUAUwBBAFIAQQAtAEMATwBSAFAABAAeAGMAbwByA" |
| 3139 | "HAALgBnAG8AbwBnAGwAZQAuAGMAbwBtAAMAQABhAGsAZQBlAHMAYQBy" |
| 3140 | "AGEALQBjAG8AcgBwAC4AYQBkAC4AYwBvAHIAcAAuAGcAbwBvAGcAbAB" |
| 3141 | "lAC4AYwBvAG0ABQAeAGMAbwByAHAALgBnAG8AbwBnAGwAZQAuAGMAbw" |
| 3142 | "BtAAAAAAA=\r\n"), |
| 3143 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 3144 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3145 | MockRead("You are not authorized to view this page\r\n"), |
| 3146 | |
| 3147 | // Wrong password. |
| 3148 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3149 | MockRead("WWW-Authenticate: NTLM\r\n"), |
| 3150 | MockRead("Connection: close\r\n"), |
| 3151 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 3152 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3153 | // Missing content -- won't matter, as connection will be reset. |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3154 | MockRead(false, ERR_UNEXPECTED), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3155 | }; |
| 3156 | |
| 3157 | MockWrite data_writes3[] = { |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3158 | // After restarting with a null identity, this is the |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3159 | // request we should be issuing -- the final header line contains a Type |
| 3160 | // 1 message. |
| 3161 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 3162 | "Host: 172.22.68.17\r\n" |
| 3163 | "Connection: keep-alive\r\n" |
| 3164 | "Authorization: NTLM " |
| 3165 | "TlRMTVNTUAABAAAAB4IIAAAAAAAAAAAAAAAAAAAAAAA=\r\n\r\n"), |
| 3166 | |
| 3167 | // After calling trans->RestartWithAuth(), we should send a Type 3 message |
| 3168 | // (the credentials for the origin server). The second request continues |
| 3169 | // on the same connection. |
| 3170 | MockWrite("GET /kids/login.aspx HTTP/1.1\r\n" |
| 3171 | "Host: 172.22.68.17\r\n" |
| 3172 | "Connection: keep-alive\r\n" |
| 3173 | "Authorization: NTLM TlRMTVNTUAADAAAAGAAYAGgAAAAYABgAgA" |
| 3174 | "AAAAAAAABAAAAAGAAYAEAAAAAQABAAWAAAAAAAAAAAAAAABYIIAHQA" |
| 3175 | "ZQBzAHQAaQBuAGcALQBuAHQAbABtAFcAVABDAC0AVwBJAE4ANwBO54" |
| 3176 | "dFMVvTHwAAAAAAAAAAAAAAAAAAAACS7sT6Uzw7L0L//WUqlIaVWpbI" |
| 3177 | "+4MUm7c=\r\n\r\n"), |
| 3178 | }; |
| 3179 | |
| 3180 | MockRead data_reads3[] = { |
| 3181 | // The origin server responds with a Type 2 message. |
| 3182 | MockRead("HTTP/1.1 401 Access Denied\r\n"), |
| 3183 | MockRead("WWW-Authenticate: NTLM " |
| 3184 | "TlRMTVNTUAACAAAADAAMADgAAAAFgokCL24VN8dgOR8AAAAAAAAAALo" |
| 3185 | "AugBEAAAABQEoCgAAAA9HAE8ATwBHAEwARQACAAwARwBPAE8ARwBMAE" |
| 3186 | "UAAQAaAEEASwBFAEUAUwBBAFIAQQAtAEMATwBSAFAABAAeAGMAbwByA" |
| 3187 | "HAALgBnAG8AbwBnAGwAZQAuAGMAbwBtAAMAQABhAGsAZQBlAHMAYQBy" |
| 3188 | "AGEALQBjAG8AcgBwAC4AYQBkAC4AYwBvAHIAcAAuAGcAbwBvAGcAbAB" |
| 3189 | "lAC4AYwBvAG0ABQAeAGMAbwByAHAALgBnAG8AbwBnAGwAZQAuAGMAbw" |
| 3190 | "BtAAAAAAA=\r\n"), |
| 3191 | MockRead("Content-Length: 42\r\n"), |
[email protected] | 076c8508 | 2009-04-10 23:21:36 | [diff] [blame] | 3192 | MockRead("Content-Type: text/html\r\n\r\n"), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3193 | MockRead("You are not authorized to view this page\r\n"), |
| 3194 | |
| 3195 | // Lastly we get the desired content. |
| 3196 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 3197 | MockRead("Content-Type: text/html; charset=utf-8\r\n"), |
| 3198 | MockRead("Content-Length: 13\r\n\r\n"), |
| 3199 | MockRead("Please Login\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3200 | MockRead(false, OK), |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3201 | }; |
| 3202 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3203 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3204 | data_writes1, arraysize(data_writes1)); |
| 3205 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 3206 | data_writes2, arraysize(data_writes2)); |
| 3207 | StaticSocketDataProvider data3(data_reads3, arraysize(data_reads3), |
| 3208 | data_writes3, arraysize(data_writes3)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3209 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3210 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 3211 | session_deps.socket_factory.AddSocketDataProvider(&data3); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3212 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3213 | TestCompletionCallback callback1; |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3214 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 3215 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 3216 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3217 | int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3218 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3219 | |
| 3220 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3221 | EXPECT_EQ(OK, rv); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3222 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3223 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3224 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3225 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 3226 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 3227 | EXPECT_TRUE(CheckNTLMServerAuth(response->auth_challenge.get())); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3228 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3229 | TestCompletionCallback callback2; |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3230 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3231 | // Enter the wrong password. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3232 | rv = trans->RestartWithAuth(AuthCredentials(kTestingNTLM, kWrongPassword), |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3233 | callback2.callback()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3234 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3235 | |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 3236 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3237 | EXPECT_EQ(OK, rv); |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3238 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3239 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3240 | TestCompletionCallback callback3; |
| 3241 | rv = trans->RestartWithAuth(AuthCredentials(), callback3.callback()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3242 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 3243 | rv = callback3.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3244 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3245 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 3246 | |
| 3247 | response = trans->GetResponseInfo(); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 3248 | ASSERT_FALSE(response == NULL); |
| 3249 | EXPECT_TRUE(CheckNTLMServerAuth(response->auth_challenge.get())); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3250 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3251 | TestCompletionCallback callback4; |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3252 | |
| 3253 | // Now enter the right password. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3254 | rv = trans->RestartWithAuth(AuthCredentials(kTestingNTLM, kTestingNTLM), |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3255 | callback4.callback()); |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 3256 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3257 | |
| 3258 | rv = callback4.WaitForResult(); |
| 3259 | EXPECT_EQ(OK, rv); |
| 3260 | |
| 3261 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
| 3262 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3263 | TestCompletionCallback callback5; |
[email protected] | 10af5fe7 | 2011-01-31 16:17:25 | [diff] [blame] | 3264 | |
| 3265 | // One more roundtrip |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3266 | rv = trans->RestartWithAuth(AuthCredentials(), callback5.callback()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3267 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3268 | |
| 3269 | rv = callback5.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3270 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3271 | |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3272 | response = trans->GetResponseInfo(); |
| 3273 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3274 | EXPECT_EQ(13, response->headers->GetContentLength()); |
| 3275 | } |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3276 | #endif // NTLM_PORTABLE |
[email protected] | 385a467 | 2009-03-11 22:21:29 | [diff] [blame] | 3277 | |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 3278 | // Test reading a server response which has only headers, and no body. |
| 3279 | // After some maximum number of bytes is consumed, the transaction should |
| 3280 | // fail with ERR_RESPONSE_HEADERS_TOO_BIG. |
| 3281 | TEST_F(HttpNetworkTransactionTest, LargeHeadersNoBody) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3282 | HttpRequestInfo request; |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 3283 | request.method = "GET"; |
| 3284 | request.url = GURL("http://www.google.com/"); |
| 3285 | request.load_flags = 0; |
| 3286 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3287 | SessionDependencies session_deps; |
| 3288 | scoped_ptr<HttpTransaction> trans( |
| 3289 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 3290 | |
[email protected] | b75b7b2f | 2009-10-06 00:54:53 | [diff] [blame] | 3291 | // Respond with 300 kb of headers (we should fail after 256 kb). |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 3292 | std::string large_headers_string; |
[email protected] | b75b7b2f | 2009-10-06 00:54:53 | [diff] [blame] | 3293 | FillLargeHeadersString(&large_headers_string, 300 * 1024); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 3294 | |
| 3295 | MockRead data_reads[] = { |
| 3296 | MockRead("HTTP/1.0 200 OK\r\n"), |
[email protected] | 15a5ccf8 | 2008-10-23 19:57:43 | [diff] [blame] | 3297 | MockRead(true, large_headers_string.data(), large_headers_string.size()), |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 3298 | MockRead("\r\nBODY"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3299 | MockRead(false, OK), |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 3300 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3301 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3302 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 3303 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3304 | TestCompletionCallback callback; |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 3305 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3306 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3307 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 3308 | |
| 3309 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3310 | EXPECT_EQ(ERR_RESPONSE_HEADERS_TOO_BIG, rv); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 3311 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3312 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 3313 | EXPECT_TRUE(response == NULL); |
| 3314 | } |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3315 | |
| 3316 | // Make sure that we don't try to reuse a TCPClientSocket when failing to |
| 3317 | // establish tunnel. |
| 3318 | // http://code.google.com/p/chromium/issues/detail?id=3772 |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 3319 | TEST_F(HttpNetworkTransactionTest, DontRecycleTransportSocketForSSLTunnel) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3320 | HttpRequestInfo request; |
| 3321 | request.method = "GET"; |
| 3322 | request.url = GURL("https://www.google.com/"); |
| 3323 | request.load_flags = 0; |
| 3324 | |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3325 | // Configure against proxy server "myproxy:70". |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 3326 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
[email protected] | db8f44c | 2008-12-13 04:52:01 | [diff] [blame] | 3327 | |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 3328 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3329 | |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 3330 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3331 | |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3332 | // Since we have proxy, should try to establish tunnel. |
| 3333 | MockWrite data_writes1[] = { |
| 3334 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 3335 | "Host: www.google.com\r\n" |
| 3336 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3337 | }; |
| 3338 | |
[email protected] | 77848d1 | 2008-11-14 00:00:22 | [diff] [blame] | 3339 | // The proxy responds to the connect with a 404, using a persistent |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3340 | // connection. Usually a proxy would return 501 (not implemented), |
| 3341 | // or 200 (tunnel established). |
| 3342 | MockRead data_reads1[] = { |
| 3343 | MockRead("HTTP/1.1 404 Not Found\r\n"), |
| 3344 | MockRead("Content-Length: 10\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3345 | MockRead(false, ERR_UNEXPECTED), // Should not be reached. |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3346 | }; |
| 3347 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3348 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3349 | data_writes1, arraysize(data_writes1)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3350 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3351 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3352 | TestCompletionCallback callback1; |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3353 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3354 | int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3355 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3356 | |
| 3357 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3358 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, rv); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3359 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3360 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 3361 | EXPECT_TRUE(response == NULL); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3362 | |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3363 | // Empty the current queue. This is necessary because idle sockets are |
| 3364 | // added to the connection pool asynchronously with a PostTask. |
| 3365 | MessageLoop::current()->RunAllPending(); |
| 3366 | |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3367 | // We now check to make sure the TCPClientSocket was not added back to |
| 3368 | // the pool. |
[email protected] | a42dbd14 | 2011-11-17 16:42:02 | [diff] [blame] | 3369 | EXPECT_EQ(0, session->GetTransportSocketPool()->IdleSocketCount()); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3370 | trans.reset(); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3371 | MessageLoop::current()->RunAllPending(); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3372 | // Make sure that the socket didn't get recycled after calling the destructor. |
[email protected] | a42dbd14 | 2011-11-17 16:42:02 | [diff] [blame] | 3373 | EXPECT_EQ(0, session->GetTransportSocketPool()->IdleSocketCount()); |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 3374 | } |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3375 | |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 3376 | // Make sure that we recycle a socket after reading all of the response body. |
| 3377 | TEST_F(HttpNetworkTransactionTest, RecycleSocket) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3378 | HttpRequestInfo request; |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 3379 | request.method = "GET"; |
| 3380 | request.url = GURL("http://www.google.com/"); |
| 3381 | request.load_flags = 0; |
| 3382 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3383 | SessionDependencies session_deps; |
| 3384 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 3385 | |
| 3386 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 3387 | |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 3388 | MockRead data_reads[] = { |
| 3389 | // A part of the response body is received with the response headers. |
| 3390 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\nhel"), |
| 3391 | // The rest of the response body is received in two parts. |
| 3392 | MockRead("lo"), |
| 3393 | MockRead(" world"), |
| 3394 | MockRead("junk"), // Should not be read!! |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3395 | MockRead(false, OK), |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 3396 | }; |
| 3397 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3398 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3399 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 3400 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3401 | TestCompletionCallback callback; |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 3402 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3403 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3404 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 3405 | |
| 3406 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3407 | EXPECT_EQ(OK, rv); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 3408 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3409 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 3410 | ASSERT_TRUE(response != NULL); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 3411 | |
| 3412 | EXPECT_TRUE(response->headers != NULL); |
| 3413 | std::string status_line = response->headers->GetStatusLine(); |
| 3414 | EXPECT_EQ("HTTP/1.1 200 OK", status_line); |
| 3415 | |
[email protected] | a42dbd14 | 2011-11-17 16:42:02 | [diff] [blame] | 3416 | EXPECT_EQ(0, session->GetTransportSocketPool()->IdleSocketCount()); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 3417 | |
| 3418 | std::string response_data; |
| 3419 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3420 | EXPECT_EQ(OK, rv); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 3421 | EXPECT_EQ("hello world", response_data); |
| 3422 | |
| 3423 | // Empty the current queue. This is necessary because idle sockets are |
| 3424 | // added to the connection pool asynchronously with a PostTask. |
| 3425 | MessageLoop::current()->RunAllPending(); |
| 3426 | |
| 3427 | // We now check to make sure the socket was added back to the pool. |
[email protected] | a42dbd14 | 2011-11-17 16:42:02 | [diff] [blame] | 3428 | EXPECT_EQ(1, session->GetTransportSocketPool()->IdleSocketCount()); |
[email protected] | 1b157c0 | 2009-04-21 01:55:40 | [diff] [blame] | 3429 | } |
| 3430 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 3431 | // Make sure that we recycle a SSL socket after reading all of the response |
| 3432 | // body. |
| 3433 | TEST_F(HttpNetworkTransactionTest, RecycleSSLSocket) { |
| 3434 | SessionDependencies session_deps; |
| 3435 | HttpRequestInfo request; |
| 3436 | request.method = "GET"; |
| 3437 | request.url = GURL("https://www.google.com/"); |
| 3438 | request.load_flags = 0; |
| 3439 | |
| 3440 | MockWrite data_writes[] = { |
| 3441 | MockWrite("GET / HTTP/1.1\r\n" |
| 3442 | "Host: www.google.com\r\n" |
| 3443 | "Connection: keep-alive\r\n\r\n"), |
| 3444 | }; |
| 3445 | |
| 3446 | MockRead data_reads[] = { |
| 3447 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 3448 | MockRead("Content-Length: 11\r\n\r\n"), |
| 3449 | MockRead("hello world"), |
| 3450 | MockRead(false, OK), |
| 3451 | }; |
| 3452 | |
| 3453 | SSLSocketDataProvider ssl(true, OK); |
| 3454 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 3455 | |
| 3456 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 3457 | data_writes, arraysize(data_writes)); |
| 3458 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 3459 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3460 | TestCompletionCallback callback; |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 3461 | |
| 3462 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 3463 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 3464 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3465 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 3466 | |
| 3467 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3468 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 3469 | |
| 3470 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 3471 | ASSERT_TRUE(response != NULL); |
| 3472 | ASSERT_TRUE(response->headers != NULL); |
| 3473 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 3474 | |
[email protected] | a42dbd14 | 2011-11-17 16:42:02 | [diff] [blame] | 3475 | EXPECT_EQ(0, session->GetTransportSocketPool()->IdleSocketCount()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 3476 | |
| 3477 | std::string response_data; |
| 3478 | rv = ReadTransaction(trans.get(), &response_data); |
| 3479 | EXPECT_EQ(OK, rv); |
| 3480 | EXPECT_EQ("hello world", response_data); |
| 3481 | |
| 3482 | // Empty the current queue. This is necessary because idle sockets are |
| 3483 | // added to the connection pool asynchronously with a PostTask. |
| 3484 | MessageLoop::current()->RunAllPending(); |
| 3485 | |
| 3486 | // We now check to make sure the socket was added back to the pool. |
[email protected] | a42dbd14 | 2011-11-17 16:42:02 | [diff] [blame] | 3487 | EXPECT_EQ(1, session->GetSSLSocketPool()->IdleSocketCount()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 3488 | } |
| 3489 | |
| 3490 | // Grab a SSL socket, use it, and put it back into the pool. Then, reuse it |
| 3491 | // from the pool and make sure that we recover okay. |
| 3492 | TEST_F(HttpNetworkTransactionTest, RecycleDeadSSLSocket) { |
| 3493 | SessionDependencies session_deps; |
| 3494 | HttpRequestInfo request; |
| 3495 | request.method = "GET"; |
| 3496 | request.url = GURL("https://www.google.com/"); |
| 3497 | request.load_flags = 0; |
| 3498 | |
| 3499 | MockWrite data_writes[] = { |
| 3500 | MockWrite("GET / HTTP/1.1\r\n" |
| 3501 | "Host: www.google.com\r\n" |
| 3502 | "Connection: keep-alive\r\n\r\n"), |
| 3503 | MockWrite("GET / HTTP/1.1\r\n" |
| 3504 | "Host: www.google.com\r\n" |
| 3505 | "Connection: keep-alive\r\n\r\n"), |
| 3506 | }; |
| 3507 | |
| 3508 | MockRead data_reads[] = { |
| 3509 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 3510 | MockRead("Content-Length: 11\r\n\r\n"), |
| 3511 | MockRead(false, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| 3512 | MockRead("hello world"), |
| 3513 | MockRead(true, 0, 0) // EOF |
| 3514 | }; |
| 3515 | |
| 3516 | SSLSocketDataProvider ssl(true, OK); |
| 3517 | SSLSocketDataProvider ssl2(true, OK); |
| 3518 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 3519 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl2); |
| 3520 | |
| 3521 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 3522 | data_writes, arraysize(data_writes)); |
| 3523 | StaticSocketDataProvider data2(data_reads, arraysize(data_reads), |
| 3524 | data_writes, arraysize(data_writes)); |
| 3525 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 3526 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 3527 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3528 | TestCompletionCallback callback; |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 3529 | |
| 3530 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 3531 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 3532 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3533 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 3534 | |
| 3535 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3536 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 3537 | |
| 3538 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 3539 | ASSERT_TRUE(response != NULL); |
| 3540 | ASSERT_TRUE(response->headers != NULL); |
| 3541 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 3542 | |
[email protected] | a42dbd14 | 2011-11-17 16:42:02 | [diff] [blame] | 3543 | EXPECT_EQ(0, session->GetTransportSocketPool()->IdleSocketCount()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 3544 | |
| 3545 | std::string response_data; |
| 3546 | rv = ReadTransaction(trans.get(), &response_data); |
| 3547 | EXPECT_EQ(OK, rv); |
| 3548 | EXPECT_EQ("hello world", response_data); |
| 3549 | |
| 3550 | // Empty the current queue. This is necessary because idle sockets are |
| 3551 | // added to the connection pool asynchronously with a PostTask. |
| 3552 | MessageLoop::current()->RunAllPending(); |
| 3553 | |
| 3554 | // We now check to make sure the socket was added back to the pool. |
[email protected] | a42dbd14 | 2011-11-17 16:42:02 | [diff] [blame] | 3555 | EXPECT_EQ(1, session->GetSSLSocketPool()->IdleSocketCount()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 3556 | |
| 3557 | // Now start the second transaction, which should reuse the previous socket. |
| 3558 | |
| 3559 | trans.reset(new HttpNetworkTransaction(session)); |
| 3560 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3561 | rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 3562 | |
| 3563 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3564 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 3565 | |
| 3566 | response = trans->GetResponseInfo(); |
| 3567 | ASSERT_TRUE(response != NULL); |
| 3568 | ASSERT_TRUE(response->headers != NULL); |
| 3569 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 3570 | |
[email protected] | a42dbd14 | 2011-11-17 16:42:02 | [diff] [blame] | 3571 | EXPECT_EQ(0, session->GetTransportSocketPool()->IdleSocketCount()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 3572 | |
| 3573 | rv = ReadTransaction(trans.get(), &response_data); |
| 3574 | EXPECT_EQ(OK, rv); |
| 3575 | EXPECT_EQ("hello world", response_data); |
| 3576 | |
| 3577 | // Empty the current queue. This is necessary because idle sockets are |
| 3578 | // added to the connection pool asynchronously with a PostTask. |
| 3579 | MessageLoop::current()->RunAllPending(); |
| 3580 | |
| 3581 | // We now check to make sure the socket was added back to the pool. |
[email protected] | a42dbd14 | 2011-11-17 16:42:02 | [diff] [blame] | 3582 | EXPECT_EQ(1, session->GetSSLSocketPool()->IdleSocketCount()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 3583 | } |
| 3584 | |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3585 | // Make sure that we recycle a socket after a zero-length response. |
| 3586 | // http://crbug.com/9880 |
| 3587 | TEST_F(HttpNetworkTransactionTest, RecycleSocketAfterZeroContentLength) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3588 | HttpRequestInfo request; |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3589 | request.method = "GET"; |
| 3590 | request.url = GURL("http://www.google.com/csi?v=3&s=web&action=&" |
| 3591 | "tran=undefined&ei=mAXcSeegAo-SMurloeUN&" |
| 3592 | "e=17259,18167,19592,19773,19981,20133,20173,20233&" |
| 3593 | "rt=prt.2642,ol.2649,xjs.2951"); |
| 3594 | request.load_flags = 0; |
| 3595 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3596 | SessionDependencies session_deps; |
| 3597 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 3598 | |
| 3599 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 3600 | |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3601 | MockRead data_reads[] = { |
| 3602 | MockRead("HTTP/1.1 204 No Content\r\n" |
| 3603 | "Content-Length: 0\r\n" |
| 3604 | "Content-Type: text/html\r\n\r\n"), |
| 3605 | MockRead("junk"), // Should not be read!! |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3606 | MockRead(false, OK), |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3607 | }; |
| 3608 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3609 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3610 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3611 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3612 | TestCompletionCallback callback; |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3613 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3614 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3615 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3616 | |
| 3617 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3618 | EXPECT_EQ(OK, rv); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3619 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3620 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 3621 | ASSERT_TRUE(response != NULL); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3622 | |
| 3623 | EXPECT_TRUE(response->headers != NULL); |
| 3624 | std::string status_line = response->headers->GetStatusLine(); |
| 3625 | EXPECT_EQ("HTTP/1.1 204 No Content", status_line); |
| 3626 | |
[email protected] | a42dbd14 | 2011-11-17 16:42:02 | [diff] [blame] | 3627 | EXPECT_EQ(0, session->GetTransportSocketPool()->IdleSocketCount()); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3628 | |
| 3629 | std::string response_data; |
| 3630 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3631 | EXPECT_EQ(OK, rv); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3632 | EXPECT_EQ("", response_data); |
| 3633 | |
| 3634 | // Empty the current queue. This is necessary because idle sockets are |
| 3635 | // added to the connection pool asynchronously with a PostTask. |
| 3636 | MessageLoop::current()->RunAllPending(); |
| 3637 | |
| 3638 | // We now check to make sure the socket was added back to the pool. |
[email protected] | a42dbd14 | 2011-11-17 16:42:02 | [diff] [blame] | 3639 | EXPECT_EQ(1, session->GetTransportSocketPool()->IdleSocketCount()); |
[email protected] | b4404c0 | 2009-04-10 16:38:52 | [diff] [blame] | 3640 | } |
| 3641 | |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3642 | TEST_F(HttpNetworkTransactionTest, ResendRequestOnWriteBodyError) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3643 | HttpRequestInfo request[2]; |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3644 | // Transaction 1: a GET request that succeeds. The socket is recycled |
| 3645 | // after use. |
| 3646 | request[0].method = "GET"; |
| 3647 | request[0].url = GURL("http://www.google.com/"); |
| 3648 | request[0].load_flags = 0; |
| 3649 | // Transaction 2: a POST request. Reuses the socket kept alive from |
| 3650 | // transaction 1. The first attempts fails when writing the POST data. |
| 3651 | // This causes the transaction to retry with a new socket. The second |
| 3652 | // attempt succeeds. |
| 3653 | request[1].method = "POST"; |
| 3654 | request[1].url = GURL("http://www.google.com/login.cgi"); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3655 | request[1].upload_data = new UploadData; |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3656 | request[1].upload_data->AppendBytes("foo", 3); |
| 3657 | request[1].load_flags = 0; |
| 3658 | |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 3659 | SessionDependencies session_deps; |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 3660 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3661 | |
| 3662 | // The first socket is used for transaction 1 and the first attempt of |
| 3663 | // transaction 2. |
| 3664 | |
| 3665 | // The response of transaction 1. |
| 3666 | MockRead data_reads1[] = { |
| 3667 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\n"), |
| 3668 | MockRead("hello world"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3669 | MockRead(false, OK), |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3670 | }; |
| 3671 | // The mock write results of transaction 1 and the first attempt of |
| 3672 | // transaction 2. |
| 3673 | MockWrite data_writes1[] = { |
| 3674 | MockWrite(false, 64), // GET |
| 3675 | MockWrite(false, 93), // POST |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3676 | MockWrite(false, ERR_CONNECTION_ABORTED), // POST data |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3677 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3678 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3679 | data_writes1, arraysize(data_writes1)); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3680 | |
| 3681 | // The second socket is used for the second attempt of transaction 2. |
| 3682 | |
| 3683 | // The response of transaction 2. |
| 3684 | MockRead data_reads2[] = { |
| 3685 | MockRead("HTTP/1.1 200 OK\r\nContent-Length: 7\r\n\r\n"), |
| 3686 | MockRead("welcome"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3687 | MockRead(false, OK), |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3688 | }; |
| 3689 | // The mock write results of the second attempt of transaction 2. |
| 3690 | MockWrite data_writes2[] = { |
| 3691 | MockWrite(false, 93), // POST |
| 3692 | MockWrite(false, 3), // POST data |
| 3693 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3694 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 3695 | data_writes2, arraysize(data_writes2)); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3696 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3697 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3698 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3699 | |
| 3700 | const char* kExpectedResponseData[] = { |
| 3701 | "hello world", "welcome" |
| 3702 | }; |
| 3703 | |
| 3704 | for (int i = 0; i < 2; ++i) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3705 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 3706 | new HttpNetworkTransaction(session)); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3707 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3708 | TestCompletionCallback callback; |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3709 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3710 | int rv = trans->Start(&request[i], callback.callback(), BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3711 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3712 | |
| 3713 | rv = callback.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3714 | EXPECT_EQ(OK, rv); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3715 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3716 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 3717 | ASSERT_TRUE(response != NULL); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3718 | |
| 3719 | EXPECT_TRUE(response->headers != NULL); |
| 3720 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 3721 | |
| 3722 | std::string response_data; |
| 3723 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3724 | EXPECT_EQ(OK, rv); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 3725 | EXPECT_EQ(kExpectedResponseData[i], response_data); |
| 3726 | } |
| 3727 | } |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3728 | |
| 3729 | // Test the request-challenge-retry sequence for basic auth when there is |
| 3730 | // an identity in the URL. The request should be sent as normal, but when |
[email protected] | 7b08ba6 | 2012-02-10 20:19:41 | [diff] [blame] | 3731 | // it fails the identity from the URL is no longer used. |
| 3732 | TEST_F(HttpNetworkTransactionTest, IgnoreAuthIdentityInURL) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3733 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3734 | request.method = "GET"; |
[email protected] | a97cca4 | 2009-08-14 01:00:29 | [diff] [blame] | 3735 | request.url = GURL("http://foo:b@[email protected]/"); |
[email protected] | 7b08ba6 | 2012-02-10 20:19:41 | [diff] [blame] | 3736 | request.load_flags = LOAD_NORMAL; |
[email protected] | a97cca4 | 2009-08-14 01:00:29 | [diff] [blame] | 3737 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3738 | SessionDependencies session_deps; |
| 3739 | scoped_ptr<HttpTransaction> trans( |
| 3740 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 3741 | |
[email protected] | a97cca4 | 2009-08-14 01:00:29 | [diff] [blame] | 3742 | // The password contains an escaped character -- for this test to pass it |
| 3743 | // will need to be unescaped by HttpNetworkTransaction. |
| 3744 | EXPECT_EQ("b%40r", request.url.password()); |
| 3745 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3746 | MockWrite data_writes1[] = { |
| 3747 | MockWrite("GET / HTTP/1.1\r\n" |
| 3748 | "Host: www.google.com\r\n" |
| 3749 | "Connection: keep-alive\r\n\r\n"), |
| 3750 | }; |
| 3751 | |
| 3752 | MockRead data_reads1[] = { |
| 3753 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 3754 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3755 | MockRead("Content-Length: 10\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3756 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3757 | }; |
| 3758 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3759 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3760 | data_writes1, arraysize(data_writes1)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3761 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3762 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3763 | TestCompletionCallback callback1; |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3764 | int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3765 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3766 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3767 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 3768 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 3769 | |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 3770 | // Empty the current queue. |
| 3771 | MessageLoop::current()->RunAllPending(); |
| 3772 | } |
| 3773 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3774 | // Test that previously tried username/passwords for a realm get re-used. |
| 3775 | TEST_F(HttpNetworkTransactionTest, BasicAuthCacheAndPreauth) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 3776 | SessionDependencies session_deps; |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 3777 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3778 | |
| 3779 | // Transaction 1: authenticate (foo, bar) on MyRealm1 |
| 3780 | { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3781 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3782 | request.method = "GET"; |
| 3783 | request.url = GURL("http://www.google.com/x/y/z"); |
| 3784 | request.load_flags = 0; |
| 3785 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3786 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 3787 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3788 | MockWrite data_writes1[] = { |
| 3789 | MockWrite("GET /x/y/z HTTP/1.1\r\n" |
| 3790 | "Host: www.google.com\r\n" |
| 3791 | "Connection: keep-alive\r\n\r\n"), |
| 3792 | }; |
| 3793 | |
| 3794 | MockRead data_reads1[] = { |
| 3795 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 3796 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 3797 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3798 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3799 | }; |
| 3800 | |
| 3801 | // Resend with authorization (username=foo, password=bar) |
| 3802 | MockWrite data_writes2[] = { |
| 3803 | MockWrite("GET /x/y/z HTTP/1.1\r\n" |
| 3804 | "Host: www.google.com\r\n" |
| 3805 | "Connection: keep-alive\r\n" |
| 3806 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 3807 | }; |
| 3808 | |
| 3809 | // Sever accepts the authorization. |
| 3810 | MockRead data_reads2[] = { |
| 3811 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3812 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3813 | MockRead(false, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3814 | }; |
| 3815 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3816 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3817 | data_writes1, arraysize(data_writes1)); |
| 3818 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 3819 | data_writes2, arraysize(data_writes2)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3820 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3821 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3822 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3823 | TestCompletionCallback callback1; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3824 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3825 | int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3826 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3827 | |
| 3828 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3829 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3830 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3831 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 3832 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 3833 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge.get())); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3834 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3835 | TestCompletionCallback callback2; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3836 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3837 | rv = trans->RestartWithAuth( |
| 3838 | AuthCredentials(kFoo, kBar), callback2.callback()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3839 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3840 | |
| 3841 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3842 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3843 | |
| 3844 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 3845 | ASSERT_TRUE(response != NULL); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3846 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3847 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 3848 | } |
| 3849 | |
| 3850 | // ------------------------------------------------------------------------ |
| 3851 | |
| 3852 | // Transaction 2: authenticate (foo2, bar2) on MyRealm2 |
| 3853 | { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3854 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3855 | request.method = "GET"; |
| 3856 | // Note that Transaction 1 was at /x/y/z, so this is in the same |
| 3857 | // protection space as MyRealm1. |
| 3858 | request.url = GURL("http://www.google.com/x/y/a/b"); |
| 3859 | request.load_flags = 0; |
| 3860 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3861 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 3862 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3863 | MockWrite data_writes1[] = { |
| 3864 | MockWrite("GET /x/y/a/b HTTP/1.1\r\n" |
| 3865 | "Host: www.google.com\r\n" |
| 3866 | "Connection: keep-alive\r\n" |
| 3867 | // Send preemptive authorization for MyRealm1 |
| 3868 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 3869 | }; |
| 3870 | |
| 3871 | // The server didn't like the preemptive authorization, and |
| 3872 | // challenges us for a different realm (MyRealm2). |
| 3873 | MockRead data_reads1[] = { |
| 3874 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 3875 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm2\"\r\n"), |
| 3876 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3877 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3878 | }; |
| 3879 | |
| 3880 | // Resend with authorization for MyRealm2 (username=foo2, password=bar2) |
| 3881 | MockWrite data_writes2[] = { |
| 3882 | MockWrite("GET /x/y/a/b HTTP/1.1\r\n" |
| 3883 | "Host: www.google.com\r\n" |
| 3884 | "Connection: keep-alive\r\n" |
| 3885 | "Authorization: Basic Zm9vMjpiYXIy\r\n\r\n"), |
| 3886 | }; |
| 3887 | |
| 3888 | // Sever accepts the authorization. |
| 3889 | MockRead data_reads2[] = { |
| 3890 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3891 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3892 | MockRead(false, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3893 | }; |
| 3894 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3895 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3896 | data_writes1, arraysize(data_writes1)); |
| 3897 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 3898 | data_writes2, arraysize(data_writes2)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3899 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 3900 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3901 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3902 | TestCompletionCallback callback1; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3903 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3904 | int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3905 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3906 | |
| 3907 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3908 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3909 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3910 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 3911 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 3912 | ASSERT_TRUE(response->auth_challenge.get()); |
| 3913 | EXPECT_FALSE(response->auth_challenge->is_proxy); |
| 3914 | EXPECT_EQ("www.google.com:80", |
| 3915 | response->auth_challenge->challenger.ToString()); |
| 3916 | EXPECT_EQ("MyRealm2", response->auth_challenge->realm); |
| 3917 | EXPECT_EQ("basic", response->auth_challenge->scheme); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3918 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3919 | TestCompletionCallback callback2; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3920 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3921 | rv = trans->RestartWithAuth( |
| 3922 | AuthCredentials(kFoo2, kBar2), callback2.callback()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3923 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3924 | |
| 3925 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3926 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3927 | |
| 3928 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 3929 | ASSERT_TRUE(response != NULL); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3930 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3931 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 3932 | } |
| 3933 | |
| 3934 | // ------------------------------------------------------------------------ |
| 3935 | |
| 3936 | // Transaction 3: Resend a request in MyRealm's protection space -- |
| 3937 | // succeed with preemptive authorization. |
| 3938 | { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3939 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3940 | request.method = "GET"; |
| 3941 | request.url = GURL("http://www.google.com/x/y/z2"); |
| 3942 | request.load_flags = 0; |
| 3943 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3944 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 3945 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3946 | MockWrite data_writes1[] = { |
| 3947 | MockWrite("GET /x/y/z2 HTTP/1.1\r\n" |
| 3948 | "Host: www.google.com\r\n" |
| 3949 | "Connection: keep-alive\r\n" |
| 3950 | // The authorization for MyRealm1 gets sent preemptively |
| 3951 | // (since the url is in the same protection space) |
| 3952 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 3953 | }; |
| 3954 | |
| 3955 | // Sever accepts the preemptive authorization |
| 3956 | MockRead data_reads1[] = { |
| 3957 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 3958 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3959 | MockRead(false, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3960 | }; |
| 3961 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 3962 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 3963 | data_writes1, arraysize(data_writes1)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 3964 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3965 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3966 | TestCompletionCallback callback1; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3967 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 3968 | int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3969 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3970 | |
| 3971 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3972 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3973 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3974 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 3975 | ASSERT_TRUE(response != NULL); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3976 | |
| 3977 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 3978 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 3979 | } |
| 3980 | |
| 3981 | // ------------------------------------------------------------------------ |
| 3982 | |
| 3983 | // Transaction 4: request another URL in MyRealm (however the |
| 3984 | // url is not known to belong to the protection space, so no pre-auth). |
| 3985 | { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 3986 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3987 | request.method = "GET"; |
| 3988 | request.url = GURL("http://www.google.com/x/1"); |
| 3989 | request.load_flags = 0; |
| 3990 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 3991 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 3992 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 3993 | MockWrite data_writes1[] = { |
| 3994 | MockWrite("GET /x/1 HTTP/1.1\r\n" |
| 3995 | "Host: www.google.com\r\n" |
| 3996 | "Connection: keep-alive\r\n\r\n"), |
| 3997 | }; |
| 3998 | |
| 3999 | MockRead data_reads1[] = { |
| 4000 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 4001 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 4002 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4003 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4004 | }; |
| 4005 | |
| 4006 | // Resend with authorization from MyRealm's cache. |
| 4007 | MockWrite data_writes2[] = { |
| 4008 | MockWrite("GET /x/1 HTTP/1.1\r\n" |
| 4009 | "Host: www.google.com\r\n" |
| 4010 | "Connection: keep-alive\r\n" |
| 4011 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 4012 | }; |
| 4013 | |
| 4014 | // Sever accepts the authorization. |
| 4015 | MockRead data_reads2[] = { |
| 4016 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4017 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4018 | MockRead(false, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4019 | }; |
| 4020 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4021 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 4022 | data_writes1, arraysize(data_writes1)); |
| 4023 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 4024 | data_writes2, arraysize(data_writes2)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4025 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 4026 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4027 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4028 | TestCompletionCallback callback1; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4029 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4030 | int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4031 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4032 | |
| 4033 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4034 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4035 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 4036 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4037 | TestCompletionCallback callback2; |
| 4038 | rv = trans->RestartWithAuth(AuthCredentials(), callback2.callback()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4039 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 4040 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4041 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 4042 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 4043 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4044 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 4045 | ASSERT_TRUE(response != NULL); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4046 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 4047 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 4048 | } |
| 4049 | |
| 4050 | // ------------------------------------------------------------------------ |
| 4051 | |
| 4052 | // Transaction 5: request a URL in MyRealm, but the server rejects the |
| 4053 | // cached identity. Should invalidate and re-prompt. |
| 4054 | { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4055 | HttpRequestInfo request; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4056 | request.method = "GET"; |
| 4057 | request.url = GURL("http://www.google.com/p/q/t"); |
| 4058 | request.load_flags = 0; |
| 4059 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 4060 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 4061 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4062 | MockWrite data_writes1[] = { |
| 4063 | MockWrite("GET /p/q/t HTTP/1.1\r\n" |
| 4064 | "Host: www.google.com\r\n" |
| 4065 | "Connection: keep-alive\r\n\r\n"), |
| 4066 | }; |
| 4067 | |
| 4068 | MockRead data_reads1[] = { |
| 4069 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 4070 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 4071 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4072 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4073 | }; |
| 4074 | |
| 4075 | // Resend with authorization from cache for MyRealm. |
| 4076 | MockWrite data_writes2[] = { |
| 4077 | MockWrite("GET /p/q/t HTTP/1.1\r\n" |
| 4078 | "Host: www.google.com\r\n" |
| 4079 | "Connection: keep-alive\r\n" |
| 4080 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 4081 | }; |
| 4082 | |
| 4083 | // Sever rejects the authorization. |
| 4084 | MockRead data_reads2[] = { |
| 4085 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 4086 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 4087 | MockRead("Content-Length: 10000\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4088 | MockRead(false, ERR_FAILED), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4089 | }; |
| 4090 | |
| 4091 | // At this point we should prompt for new credentials for MyRealm. |
| 4092 | // Restart with username=foo3, password=foo4. |
| 4093 | MockWrite data_writes3[] = { |
| 4094 | MockWrite("GET /p/q/t HTTP/1.1\r\n" |
| 4095 | "Host: www.google.com\r\n" |
| 4096 | "Connection: keep-alive\r\n" |
| 4097 | "Authorization: Basic Zm9vMzpiYXIz\r\n\r\n"), |
| 4098 | }; |
| 4099 | |
| 4100 | // Sever accepts the authorization. |
| 4101 | MockRead data_reads3[] = { |
| 4102 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4103 | MockRead("Content-Length: 100\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4104 | MockRead(false, OK), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4105 | }; |
| 4106 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4107 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 4108 | data_writes1, arraysize(data_writes1)); |
| 4109 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 4110 | data_writes2, arraysize(data_writes2)); |
| 4111 | StaticSocketDataProvider data3(data_reads3, arraysize(data_reads3), |
| 4112 | data_writes3, arraysize(data_writes3)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4113 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 4114 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 4115 | session_deps.socket_factory.AddSocketDataProvider(&data3); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4116 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4117 | TestCompletionCallback callback1; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4118 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4119 | int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4120 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4121 | |
| 4122 | rv = callback1.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4123 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4124 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 4125 | EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4126 | TestCompletionCallback callback2; |
| 4127 | rv = trans->RestartWithAuth(AuthCredentials(), callback2.callback()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4128 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 4129 | rv = callback2.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4130 | EXPECT_EQ(OK, rv); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 4131 | EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 4132 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4133 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 4134 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 4135 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge.get())); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4136 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4137 | TestCompletionCallback callback3; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4138 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4139 | rv = trans->RestartWithAuth( |
| 4140 | AuthCredentials(kFoo3, kBar3), callback3.callback()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4141 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4142 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 4143 | rv = callback3.WaitForResult(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4144 | EXPECT_EQ(OK, rv); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4145 | |
| 4146 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 4147 | ASSERT_TRUE(response != NULL); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 4148 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 4149 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 4150 | } |
| 4151 | } |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 4152 | |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 4153 | // Tests that nonce count increments when multiple auth attempts |
| 4154 | // are started with the same nonce. |
| 4155 | TEST_F(HttpNetworkTransactionTest, DigestPreAuthNonceCount) { |
| 4156 | SessionDependencies session_deps; |
[email protected] | 54fea256 | 2010-11-17 14:40:44 | [diff] [blame] | 4157 | HttpAuthHandlerDigest::Factory* digest_factory = |
| 4158 | new HttpAuthHandlerDigest::Factory(); |
| 4159 | HttpAuthHandlerDigest::FixedNonceGenerator* nonce_generator = |
| 4160 | new HttpAuthHandlerDigest::FixedNonceGenerator("0123456789abcdef"); |
| 4161 | digest_factory->set_nonce_generator(nonce_generator); |
| 4162 | session_deps.http_auth_handler_factory.reset(digest_factory); |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 4163 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 4164 | |
| 4165 | // Transaction 1: authenticate (foo, bar) on MyRealm1 |
| 4166 | { |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 4167 | HttpRequestInfo request; |
| 4168 | request.method = "GET"; |
| 4169 | request.url = GURL("http://www.google.com/x/y/z"); |
| 4170 | request.load_flags = 0; |
| 4171 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 4172 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 4173 | |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 4174 | MockWrite data_writes1[] = { |
| 4175 | MockWrite("GET /x/y/z HTTP/1.1\r\n" |
| 4176 | "Host: www.google.com\r\n" |
| 4177 | "Connection: keep-alive\r\n\r\n"), |
| 4178 | }; |
| 4179 | |
| 4180 | MockRead data_reads1[] = { |
| 4181 | MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 4182 | MockRead("WWW-Authenticate: Digest realm=\"digestive\", nonce=\"OU812\", " |
| 4183 | "algorithm=MD5, qop=\"auth\"\r\n\r\n"), |
| 4184 | MockRead(false, OK), |
| 4185 | }; |
| 4186 | |
| 4187 | // Resend with authorization (username=foo, password=bar) |
| 4188 | MockWrite data_writes2[] = { |
| 4189 | MockWrite("GET /x/y/z HTTP/1.1\r\n" |
| 4190 | "Host: www.google.com\r\n" |
| 4191 | "Connection: keep-alive\r\n" |
| 4192 | "Authorization: Digest username=\"foo\", realm=\"digestive\", " |
| 4193 | "nonce=\"OU812\", uri=\"/x/y/z\", algorithm=MD5, " |
| 4194 | "response=\"03ffbcd30add722589c1de345d7a927f\", qop=auth, " |
| 4195 | "nc=00000001, cnonce=\"0123456789abcdef\"\r\n\r\n"), |
| 4196 | }; |
| 4197 | |
| 4198 | // Sever accepts the authorization. |
| 4199 | MockRead data_reads2[] = { |
| 4200 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4201 | MockRead(false, OK), |
| 4202 | }; |
| 4203 | |
| 4204 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 4205 | data_writes1, arraysize(data_writes1)); |
| 4206 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 4207 | data_writes2, arraysize(data_writes2)); |
| 4208 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 4209 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 4210 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4211 | TestCompletionCallback callback1; |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 4212 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4213 | int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 4214 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4215 | |
| 4216 | rv = callback1.WaitForResult(); |
| 4217 | EXPECT_EQ(OK, rv); |
| 4218 | |
| 4219 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 4220 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 4221 | EXPECT_TRUE(CheckDigestServerAuth(response->auth_challenge.get())); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 4222 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4223 | TestCompletionCallback callback2; |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 4224 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4225 | rv = trans->RestartWithAuth( |
| 4226 | AuthCredentials(kFoo, kBar), callback2.callback()); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 4227 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4228 | |
| 4229 | rv = callback2.WaitForResult(); |
| 4230 | EXPECT_EQ(OK, rv); |
| 4231 | |
| 4232 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 4233 | ASSERT_TRUE(response != NULL); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 4234 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 4235 | } |
| 4236 | |
| 4237 | // ------------------------------------------------------------------------ |
| 4238 | |
| 4239 | // Transaction 2: Request another resource in digestive's protection space. |
| 4240 | // This will preemptively add an Authorization header which should have an |
| 4241 | // "nc" value of 2 (as compared to 1 in the first use. |
| 4242 | { |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 4243 | HttpRequestInfo request; |
| 4244 | request.method = "GET"; |
| 4245 | // Note that Transaction 1 was at /x/y/z, so this is in the same |
| 4246 | // protection space as digest. |
| 4247 | request.url = GURL("http://www.google.com/x/y/a/b"); |
| 4248 | request.load_flags = 0; |
| 4249 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 4250 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 4251 | |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 4252 | MockWrite data_writes1[] = { |
| 4253 | MockWrite("GET /x/y/a/b HTTP/1.1\r\n" |
| 4254 | "Host: www.google.com\r\n" |
| 4255 | "Connection: keep-alive\r\n" |
| 4256 | "Authorization: Digest username=\"foo\", realm=\"digestive\", " |
| 4257 | "nonce=\"OU812\", uri=\"/x/y/a/b\", algorithm=MD5, " |
| 4258 | "response=\"d6f9a2c07d1c5df7b89379dca1269b35\", qop=auth, " |
| 4259 | "nc=00000002, cnonce=\"0123456789abcdef\"\r\n\r\n"), |
| 4260 | }; |
| 4261 | |
| 4262 | // Sever accepts the authorization. |
| 4263 | MockRead data_reads1[] = { |
| 4264 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4265 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4266 | MockRead(false, OK), |
| 4267 | }; |
| 4268 | |
| 4269 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 4270 | data_writes1, arraysize(data_writes1)); |
| 4271 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 4272 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4273 | TestCompletionCallback callback1; |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 4274 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4275 | int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 4276 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4277 | |
| 4278 | rv = callback1.WaitForResult(); |
| 4279 | EXPECT_EQ(OK, rv); |
| 4280 | |
| 4281 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 4282 | ASSERT_TRUE(response != NULL); |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 4283 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 4284 | } |
| 4285 | } |
| 4286 | |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 4287 | // Test the ResetStateForRestart() private method. |
| 4288 | TEST_F(HttpNetworkTransactionTest, ResetStateForRestart) { |
| 4289 | // Create a transaction (the dependencies aren't important). |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 4290 | SessionDependencies session_deps; |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 4291 | scoped_ptr<HttpNetworkTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 4292 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 4293 | |
| 4294 | // Setup some state (which we expect ResetStateForRestart() will clear). |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 4295 | trans->read_buf_ = new IOBuffer(15); |
| 4296 | trans->read_buf_len_ = 15; |
[email protected] | b94f92d | 2010-10-27 16:45:20 | [diff] [blame] | 4297 | trans->request_headers_.SetHeader("Authorization", "NTLM"); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 4298 | |
| 4299 | // Setup state in response_ |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 4300 | HttpResponseInfo* response = &trans->response_; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 4301 | response->auth_challenge = new AuthChallengeInfo(); |
[email protected] | 70d6650 | 2011-09-23 00:55:08 | [diff] [blame] | 4302 | response->ssl_info.cert_status = static_cast<CertStatus>(-1); // Nonsensical. |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 4303 | response->response_time = base::Time::Now(); |
| 4304 | response->was_cached = true; // (Wouldn't ever actually be true...) |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 4305 | |
| 4306 | { // Setup state for response_.vary_data |
| 4307 | HttpRequestInfo request; |
| 4308 | std::string temp("HTTP/1.1 200 OK\nVary: foo, bar\n\n"); |
| 4309 | std::replace(temp.begin(), temp.end(), '\n', '\0'); |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 4310 | scoped_refptr<HttpResponseHeaders> headers(new HttpResponseHeaders(temp)); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4311 | request.extra_headers.SetHeader("Foo", "1"); |
| 4312 | request.extra_headers.SetHeader("bar", "23"); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 4313 | EXPECT_TRUE(response->vary_data.Init(request, *headers)); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 4314 | } |
| 4315 | |
| 4316 | // Cause the above state to be reset. |
| 4317 | trans->ResetStateForRestart(); |
| 4318 | |
| 4319 | // Verify that the state that needed to be reset, has been reset. |
[email protected] | 9b6fee1 | 2009-09-29 18:13:07 | [diff] [blame] | 4320 | EXPECT_TRUE(trans->read_buf_.get() == NULL); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 4321 | EXPECT_EQ(0, trans->read_buf_len_); |
[email protected] | b94f92d | 2010-10-27 16:45:20 | [diff] [blame] | 4322 | EXPECT_TRUE(trans->request_headers_.IsEmpty()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 4323 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 4324 | EXPECT_TRUE(response->headers.get() == NULL); |
[email protected] | 34f4094 | 2010-10-04 00:34:04 | [diff] [blame] | 4325 | EXPECT_FALSE(response->was_cached); |
[email protected] | 70d6650 | 2011-09-23 00:55:08 | [diff] [blame] | 4326 | EXPECT_EQ(0U, response->ssl_info.cert_status); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 4327 | EXPECT_FALSE(response->vary_data.is_valid()); |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 4328 | } |
| 4329 | |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4330 | // Test HTTPS connections to a site with a bad certificate |
| 4331 | TEST_F(HttpNetworkTransactionTest, HTTPSBadCertificate) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4332 | HttpRequestInfo request; |
| 4333 | request.method = "GET"; |
| 4334 | request.url = GURL("https://www.google.com/"); |
| 4335 | request.load_flags = 0; |
| 4336 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 4337 | SessionDependencies session_deps; |
| 4338 | scoped_ptr<HttpTransaction> trans( |
| 4339 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4340 | |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4341 | MockWrite data_writes[] = { |
| 4342 | MockWrite("GET / HTTP/1.1\r\n" |
| 4343 | "Host: www.google.com\r\n" |
| 4344 | "Connection: keep-alive\r\n\r\n"), |
| 4345 | }; |
| 4346 | |
| 4347 | MockRead data_reads[] = { |
| 4348 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4349 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4350 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4351 | MockRead(false, OK), |
| 4352 | }; |
| 4353 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4354 | StaticSocketDataProvider ssl_bad_certificate; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4355 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4356 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4357 | SSLSocketDataProvider ssl_bad(true, ERR_CERT_AUTHORITY_INVALID); |
| 4358 | SSLSocketDataProvider ssl(true, OK); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4359 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4360 | session_deps.socket_factory.AddSocketDataProvider(&ssl_bad_certificate); |
| 4361 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 4362 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_bad); |
| 4363 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4364 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4365 | TestCompletionCallback callback; |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4366 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4367 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4368 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4369 | |
| 4370 | rv = callback.WaitForResult(); |
| 4371 | EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, rv); |
| 4372 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4373 | rv = trans->RestartIgnoringLastError(callback.callback()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4374 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4375 | |
| 4376 | rv = callback.WaitForResult(); |
| 4377 | EXPECT_EQ(OK, rv); |
| 4378 | |
| 4379 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4380 | |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 4381 | ASSERT_TRUE(response != NULL); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4382 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 4383 | } |
| 4384 | |
| 4385 | // Test HTTPS connections to a site with a bad certificate, going through a |
| 4386 | // proxy |
| 4387 | TEST_F(HttpNetworkTransactionTest, HTTPSBadCertificateViaProxy) { |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 4388 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4389 | |
| 4390 | HttpRequestInfo request; |
| 4391 | request.method = "GET"; |
| 4392 | request.url = GURL("https://www.google.com/"); |
| 4393 | request.load_flags = 0; |
| 4394 | |
| 4395 | MockWrite proxy_writes[] = { |
| 4396 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 4397 | "Host: www.google.com\r\n" |
| 4398 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4399 | }; |
| 4400 | |
| 4401 | MockRead proxy_reads[] = { |
| 4402 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4403 | MockRead(false, OK) |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4404 | }; |
| 4405 | |
| 4406 | MockWrite data_writes[] = { |
| 4407 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
[email protected] | e44de5d | 2009-06-05 20:12:45 | [diff] [blame] | 4408 | "Host: www.google.com\r\n" |
| 4409 | "Proxy-Connection: keep-alive\r\n\r\n"), |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4410 | MockWrite("GET / HTTP/1.1\r\n" |
| 4411 | "Host: www.google.com\r\n" |
| 4412 | "Connection: keep-alive\r\n\r\n"), |
| 4413 | }; |
| 4414 | |
| 4415 | MockRead data_reads[] = { |
| 4416 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
| 4417 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4418 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4419 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4420 | MockRead(false, OK), |
| 4421 | }; |
| 4422 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4423 | StaticSocketDataProvider ssl_bad_certificate( |
| 4424 | proxy_reads, arraysize(proxy_reads), |
| 4425 | proxy_writes, arraysize(proxy_writes)); |
| 4426 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4427 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4428 | SSLSocketDataProvider ssl_bad(true, ERR_CERT_AUTHORITY_INVALID); |
| 4429 | SSLSocketDataProvider ssl(true, OK); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4430 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4431 | session_deps.socket_factory.AddSocketDataProvider(&ssl_bad_certificate); |
| 4432 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 4433 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_bad); |
| 4434 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4435 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4436 | TestCompletionCallback callback; |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4437 | |
| 4438 | for (int i = 0; i < 2; i++) { |
[email protected] | 228ff74 | 2009-06-05 01:19:59 | [diff] [blame] | 4439 | session_deps.socket_factory.ResetNextMockIndexes(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4440 | |
[email protected] | d207a5f | 2009-06-04 05:28:40 | [diff] [blame] | 4441 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 4442 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4443 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4444 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4445 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4446 | |
| 4447 | rv = callback.WaitForResult(); |
| 4448 | EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, rv); |
| 4449 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4450 | rv = trans->RestartIgnoringLastError(callback.callback()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4451 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4452 | |
| 4453 | rv = callback.WaitForResult(); |
| 4454 | EXPECT_EQ(OK, rv); |
| 4455 | |
| 4456 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4457 | |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 4458 | ASSERT_TRUE(response != NULL); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4459 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 4460 | } |
| 4461 | } |
| 4462 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 4463 | |
| 4464 | // Test HTTPS connections to a site, going through an HTTPS proxy |
| 4465 | TEST_F(HttpNetworkTransactionTest, HTTPSViaHttpsProxy) { |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 4466 | SessionDependencies session_deps(ProxyService::CreateFixed( |
| 4467 | "https://proxy:70")); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 4468 | |
| 4469 | HttpRequestInfo request; |
| 4470 | request.method = "GET"; |
| 4471 | request.url = GURL("https://www.google.com/"); |
| 4472 | request.load_flags = 0; |
| 4473 | |
| 4474 | MockWrite data_writes[] = { |
| 4475 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 4476 | "Host: www.google.com\r\n" |
| 4477 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 4478 | MockWrite("GET / HTTP/1.1\r\n" |
| 4479 | "Host: www.google.com\r\n" |
| 4480 | "Connection: keep-alive\r\n\r\n"), |
| 4481 | }; |
| 4482 | |
| 4483 | MockRead data_reads[] = { |
| 4484 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
| 4485 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 4486 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4487 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4488 | MockRead(false, OK), |
| 4489 | }; |
| 4490 | |
| 4491 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4492 | data_writes, arraysize(data_writes)); |
| 4493 | SSLSocketDataProvider proxy_ssl(true, OK); // SSL to the proxy |
| 4494 | SSLSocketDataProvider tunnel_ssl(true, OK); // SSL through the tunnel |
| 4495 | |
| 4496 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 4497 | session_deps.socket_factory.AddSSLSocketDataProvider(&proxy_ssl); |
| 4498 | session_deps.socket_factory.AddSSLSocketDataProvider(&tunnel_ssl); |
| 4499 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4500 | TestCompletionCallback callback; |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 4501 | |
| 4502 | scoped_ptr<HttpTransaction> trans( |
| 4503 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4504 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4505 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 4506 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4507 | |
| 4508 | rv = callback.WaitForResult(); |
| 4509 | EXPECT_EQ(OK, rv); |
| 4510 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4511 | |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 4512 | ASSERT_TRUE(response != NULL); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 4513 | |
| 4514 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 4515 | EXPECT_EQ(200, response->headers->response_code()); |
| 4516 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 4517 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 4518 | } |
| 4519 | |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 4520 | // Test an HTTPS Proxy's ability to redirect a CONNECT request |
| 4521 | TEST_F(HttpNetworkTransactionTest, RedirectOfHttpsConnectViaHttpsProxy) { |
| 4522 | SessionDependencies session_deps( |
| 4523 | ProxyService::CreateFixed("https://proxy:70")); |
| 4524 | |
| 4525 | HttpRequestInfo request; |
| 4526 | request.method = "GET"; |
| 4527 | request.url = GURL("https://www.google.com/"); |
| 4528 | request.load_flags = 0; |
| 4529 | |
| 4530 | MockWrite data_writes[] = { |
| 4531 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 4532 | "Host: www.google.com\r\n" |
| 4533 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 4534 | }; |
| 4535 | |
| 4536 | MockRead data_reads[] = { |
| 4537 | MockRead("HTTP/1.1 302 Redirect\r\n"), |
| 4538 | MockRead("Location: http://login.example.com/\r\n"), |
| 4539 | MockRead("Content-Length: 0\r\n\r\n"), |
| 4540 | MockRead(false, OK), |
| 4541 | }; |
| 4542 | |
| 4543 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4544 | data_writes, arraysize(data_writes)); |
| 4545 | SSLSocketDataProvider proxy_ssl(true, OK); // SSL to the proxy |
| 4546 | |
| 4547 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 4548 | session_deps.socket_factory.AddSSLSocketDataProvider(&proxy_ssl); |
| 4549 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4550 | TestCompletionCallback callback; |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 4551 | |
| 4552 | scoped_ptr<HttpTransaction> trans( |
| 4553 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4554 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4555 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 4556 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4557 | |
| 4558 | rv = callback.WaitForResult(); |
| 4559 | EXPECT_EQ(OK, rv); |
| 4560 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4561 | |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 4562 | ASSERT_TRUE(response != NULL); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 4563 | |
| 4564 | EXPECT_EQ(302, response->headers->response_code()); |
| 4565 | std::string url; |
| 4566 | EXPECT_TRUE(response->headers->IsRedirect(&url)); |
| 4567 | EXPECT_EQ("http://login.example.com/", url); |
| 4568 | } |
| 4569 | |
| 4570 | // Test an HTTPS (SPDY) Proxy's ability to redirect a CONNECT request |
| 4571 | TEST_F(HttpNetworkTransactionTest, RedirectOfHttpsConnectViaSpdyProxy) { |
| 4572 | SessionDependencies session_deps( |
| 4573 | ProxyService::CreateFixed("https://proxy:70")); |
| 4574 | |
| 4575 | HttpRequestInfo request; |
| 4576 | request.method = "GET"; |
| 4577 | request.url = GURL("https://www.google.com/"); |
| 4578 | request.load_flags = 0; |
| 4579 | |
| 4580 | scoped_ptr<spdy::SpdyFrame> conn(ConstructSpdyConnect(NULL, 0, 1)); |
| 4581 | scoped_ptr<spdy::SpdyFrame> goaway(ConstructSpdyRstStream(1, spdy::CANCEL)); |
| 4582 | MockWrite data_writes[] = { |
| 4583 | CreateMockWrite(*conn.get(), 0, false), |
| 4584 | }; |
| 4585 | |
| 4586 | static const char* const kExtraHeaders[] = { |
| 4587 | "location", |
| 4588 | "http://login.example.com/", |
| 4589 | }; |
| 4590 | scoped_ptr<spdy::SpdyFrame> resp( |
| 4591 | ConstructSpdySynReplyError("302 Redirect", kExtraHeaders, |
| 4592 | arraysize(kExtraHeaders)/2, 1)); |
| 4593 | MockRead data_reads[] = { |
| 4594 | CreateMockRead(*resp.get(), 1, false), |
| 4595 | MockRead(true, 0, 2), // EOF |
| 4596 | }; |
| 4597 | |
[email protected] | a159531 | 2012-01-22 03:25:04 | [diff] [blame] | 4598 | scoped_ptr<DelayedSocketData> data( |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 4599 | new DelayedSocketData( |
| 4600 | 1, // wait for one write to finish before reading. |
| 4601 | data_reads, arraysize(data_reads), |
| 4602 | data_writes, arraysize(data_writes))); |
| 4603 | SSLSocketDataProvider proxy_ssl(true, OK); // SSL to the proxy |
| 4604 | proxy_ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 4605 | proxy_ssl.next_proto = "spdy/2.1"; |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 4606 | proxy_ssl.was_npn_negotiated = true; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 4607 | proxy_ssl.protocol_negotiated = SSLClientSocket::kProtoSPDY21; |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 4608 | |
| 4609 | session_deps.socket_factory.AddSocketDataProvider(data.get()); |
| 4610 | session_deps.socket_factory.AddSSLSocketDataProvider(&proxy_ssl); |
| 4611 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4612 | TestCompletionCallback callback; |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 4613 | |
| 4614 | scoped_ptr<HttpTransaction> trans( |
| 4615 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4616 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4617 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 4618 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4619 | |
| 4620 | rv = callback.WaitForResult(); |
| 4621 | EXPECT_EQ(OK, rv); |
| 4622 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4623 | |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 4624 | ASSERT_TRUE(response != NULL); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 4625 | |
| 4626 | EXPECT_EQ(302, response->headers->response_code()); |
| 4627 | std::string url; |
| 4628 | EXPECT_TRUE(response->headers->IsRedirect(&url)); |
| 4629 | EXPECT_EQ("http://login.example.com/", url); |
| 4630 | } |
| 4631 | |
| 4632 | // Test an HTTPS Proxy's ability to provide a response to a CONNECT request |
| 4633 | TEST_F(HttpNetworkTransactionTest, ErrorResponseTofHttpsConnectViaHttpsProxy) { |
| 4634 | SessionDependencies session_deps( |
| 4635 | ProxyService::CreateFixed("https://proxy:70")); |
| 4636 | |
| 4637 | HttpRequestInfo request; |
| 4638 | request.method = "GET"; |
| 4639 | request.url = GURL("https://www.google.com/"); |
| 4640 | request.load_flags = 0; |
| 4641 | |
| 4642 | MockWrite data_writes[] = { |
| 4643 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 4644 | "Host: www.google.com\r\n" |
| 4645 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 4646 | }; |
| 4647 | |
| 4648 | MockRead data_reads[] = { |
| 4649 | MockRead("HTTP/1.1 404 Not Found\r\n"), |
| 4650 | MockRead("Content-Length: 23\r\n\r\n"), |
| 4651 | MockRead("The host does not exist"), |
| 4652 | MockRead(false, OK), |
| 4653 | }; |
| 4654 | |
| 4655 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4656 | data_writes, arraysize(data_writes)); |
| 4657 | SSLSocketDataProvider proxy_ssl(true, OK); // SSL to the proxy |
| 4658 | |
| 4659 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 4660 | session_deps.socket_factory.AddSSLSocketDataProvider(&proxy_ssl); |
| 4661 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4662 | TestCompletionCallback callback; |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 4663 | |
| 4664 | scoped_ptr<HttpTransaction> trans( |
| 4665 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4666 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4667 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 4668 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4669 | |
| 4670 | rv = callback.WaitForResult(); |
| 4671 | EXPECT_EQ(OK, rv); |
| 4672 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4673 | |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 4674 | ASSERT_TRUE(response != NULL); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 4675 | |
| 4676 | EXPECT_EQ(404, response->headers->response_code()); |
| 4677 | EXPECT_EQ(23, response->headers->GetContentLength()); |
| 4678 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 4679 | EXPECT_FALSE(response->ssl_info.is_valid()); |
| 4680 | |
| 4681 | std::string response_data; |
| 4682 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 4683 | EXPECT_EQ("The host does not exist", response_data); |
| 4684 | } |
| 4685 | |
| 4686 | // Test an HTTPS (SPDY) Proxy's ability to provide a response to a CONNECT |
| 4687 | // request |
| 4688 | TEST_F(HttpNetworkTransactionTest, ErrorResponseTofHttpsConnectViaSpdyProxy) { |
| 4689 | SessionDependencies session_deps( |
| 4690 | ProxyService::CreateFixed("https://proxy:70")); |
| 4691 | |
| 4692 | HttpRequestInfo request; |
| 4693 | request.method = "GET"; |
| 4694 | request.url = GURL("https://www.google.com/"); |
| 4695 | request.load_flags = 0; |
| 4696 | |
| 4697 | scoped_ptr<spdy::SpdyFrame> conn(ConstructSpdyConnect(NULL, 0, 1)); |
| 4698 | scoped_ptr<spdy::SpdyFrame> goaway(ConstructSpdyRstStream(1, spdy::CANCEL)); |
| 4699 | MockWrite data_writes[] = { |
| 4700 | CreateMockWrite(*conn.get(), 0, false), |
| 4701 | }; |
| 4702 | |
| 4703 | static const char* const kExtraHeaders[] = { |
| 4704 | "location", |
| 4705 | "http://login.example.com/", |
| 4706 | }; |
| 4707 | scoped_ptr<spdy::SpdyFrame> resp( |
| 4708 | ConstructSpdySynReplyError("404 Not Found", kExtraHeaders, |
| 4709 | arraysize(kExtraHeaders)/2, 1)); |
| 4710 | scoped_ptr<spdy::SpdyFrame> body( |
| 4711 | ConstructSpdyBodyFrame(1, "The host does not exist", 23, true)); |
| 4712 | MockRead data_reads[] = { |
| 4713 | CreateMockRead(*resp.get(), 1, false), |
| 4714 | CreateMockRead(*body.get(), 2, false), |
| 4715 | MockRead(true, 0, 3), // EOF |
| 4716 | }; |
| 4717 | |
[email protected] | a159531 | 2012-01-22 03:25:04 | [diff] [blame] | 4718 | scoped_ptr<DelayedSocketData> data( |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 4719 | new DelayedSocketData( |
| 4720 | 1, // wait for one write to finish before reading. |
| 4721 | data_reads, arraysize(data_reads), |
| 4722 | data_writes, arraysize(data_writes))); |
| 4723 | SSLSocketDataProvider proxy_ssl(true, OK); // SSL to the proxy |
| 4724 | proxy_ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 4725 | proxy_ssl.next_proto = "spdy/2.1"; |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 4726 | proxy_ssl.was_npn_negotiated = true; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 4727 | proxy_ssl.protocol_negotiated = SSLClientSocket::kProtoSPDY21; |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 4728 | |
| 4729 | session_deps.socket_factory.AddSocketDataProvider(data.get()); |
| 4730 | session_deps.socket_factory.AddSSLSocketDataProvider(&proxy_ssl); |
| 4731 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4732 | TestCompletionCallback callback; |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 4733 | |
| 4734 | scoped_ptr<HttpTransaction> trans( |
| 4735 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4736 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4737 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 4738 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4739 | |
| 4740 | rv = callback.WaitForResult(); |
| 4741 | EXPECT_EQ(OK, rv); |
| 4742 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4743 | |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 4744 | ASSERT_TRUE(response != NULL); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 4745 | |
| 4746 | EXPECT_EQ(404, response->headers->response_code()); |
| 4747 | EXPECT_FALSE(response->ssl_info.is_valid()); |
| 4748 | |
| 4749 | std::string response_data; |
| 4750 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 4751 | EXPECT_EQ("The host does not exist", response_data); |
| 4752 | } |
| 4753 | |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 4754 | // Test HTTPS connections to a site with a bad certificate, going through an |
| 4755 | // HTTPS proxy |
| 4756 | TEST_F(HttpNetworkTransactionTest, HTTPSBadCertificateViaHttpsProxy) { |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 4757 | SessionDependencies session_deps(ProxyService::CreateFixed( |
| 4758 | "https://proxy:70")); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 4759 | |
| 4760 | HttpRequestInfo request; |
| 4761 | request.method = "GET"; |
| 4762 | request.url = GURL("https://www.google.com/"); |
| 4763 | request.load_flags = 0; |
| 4764 | |
| 4765 | // Attempt to fetch the URL from a server with a bad cert |
| 4766 | MockWrite bad_cert_writes[] = { |
| 4767 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 4768 | "Host: www.google.com\r\n" |
| 4769 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 4770 | }; |
| 4771 | |
| 4772 | MockRead bad_cert_reads[] = { |
| 4773 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
| 4774 | MockRead(false, OK) |
| 4775 | }; |
| 4776 | |
| 4777 | // Attempt to fetch the URL with a good cert |
| 4778 | MockWrite good_data_writes[] = { |
| 4779 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 4780 | "Host: www.google.com\r\n" |
| 4781 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 4782 | MockWrite("GET / HTTP/1.1\r\n" |
| 4783 | "Host: www.google.com\r\n" |
| 4784 | "Connection: keep-alive\r\n\r\n"), |
| 4785 | }; |
| 4786 | |
| 4787 | MockRead good_cert_reads[] = { |
| 4788 | MockRead("HTTP/1.0 200 Connected\r\n\r\n"), |
| 4789 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4790 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4791 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4792 | MockRead(false, OK), |
| 4793 | }; |
| 4794 | |
| 4795 | StaticSocketDataProvider ssl_bad_certificate( |
| 4796 | bad_cert_reads, arraysize(bad_cert_reads), |
| 4797 | bad_cert_writes, arraysize(bad_cert_writes)); |
| 4798 | StaticSocketDataProvider data(good_cert_reads, arraysize(good_cert_reads), |
| 4799 | good_data_writes, arraysize(good_data_writes)); |
| 4800 | SSLSocketDataProvider ssl_bad(true, ERR_CERT_AUTHORITY_INVALID); |
| 4801 | SSLSocketDataProvider ssl(true, OK); |
| 4802 | |
| 4803 | // SSL to the proxy, then CONNECT request, then SSL with bad certificate |
| 4804 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 4805 | session_deps.socket_factory.AddSocketDataProvider(&ssl_bad_certificate); |
| 4806 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_bad); |
| 4807 | |
| 4808 | // SSL to the proxy, then CONNECT request, then valid SSL certificate |
| 4809 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 4810 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 4811 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 4812 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4813 | TestCompletionCallback callback; |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 4814 | |
| 4815 | scoped_ptr<HttpTransaction> trans( |
| 4816 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4817 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4818 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 4819 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4820 | |
| 4821 | rv = callback.WaitForResult(); |
| 4822 | EXPECT_EQ(ERR_CERT_AUTHORITY_INVALID, rv); |
| 4823 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4824 | rv = trans->RestartIgnoringLastError(callback.callback()); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 4825 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4826 | |
| 4827 | rv = callback.WaitForResult(); |
| 4828 | EXPECT_EQ(OK, rv); |
| 4829 | |
| 4830 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 4831 | |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 4832 | ASSERT_TRUE(response != NULL); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 4833 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 4834 | } |
| 4835 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4836 | TEST_F(HttpNetworkTransactionTest, BuildRequest_UserAgent) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4837 | HttpRequestInfo request; |
| 4838 | request.method = "GET"; |
| 4839 | request.url = GURL("http://www.google.com/"); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 4840 | request.extra_headers.SetHeader(HttpRequestHeaders::kUserAgent, |
| 4841 | "Chromium Ultra Awesome X Edition"); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4842 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 4843 | SessionDependencies session_deps; |
| 4844 | scoped_ptr<HttpTransaction> trans( |
| 4845 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4846 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4847 | MockWrite data_writes[] = { |
| 4848 | MockWrite("GET / HTTP/1.1\r\n" |
| 4849 | "Host: www.google.com\r\n" |
| 4850 | "Connection: keep-alive\r\n" |
| 4851 | "User-Agent: Chromium Ultra Awesome X Edition\r\n\r\n"), |
| 4852 | }; |
| 4853 | |
| 4854 | // Lastly, the server responds with the actual content. |
| 4855 | MockRead data_reads[] = { |
| 4856 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4857 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4858 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4859 | MockRead(false, OK), |
| 4860 | }; |
| 4861 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4862 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4863 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4864 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4865 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4866 | TestCompletionCallback callback; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4867 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4868 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4869 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4870 | |
| 4871 | rv = callback.WaitForResult(); |
| 4872 | EXPECT_EQ(OK, rv); |
| 4873 | } |
| 4874 | |
[email protected] | da81f13 | 2010-08-18 23:39:29 | [diff] [blame] | 4875 | TEST_F(HttpNetworkTransactionTest, BuildRequest_UserAgentOverTunnel) { |
[email protected] | da81f13 | 2010-08-18 23:39:29 | [diff] [blame] | 4876 | HttpRequestInfo request; |
| 4877 | request.method = "GET"; |
| 4878 | request.url = GURL("https://www.google.com/"); |
| 4879 | request.extra_headers.SetHeader(HttpRequestHeaders::kUserAgent, |
| 4880 | "Chromium Ultra Awesome X Edition"); |
| 4881 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 4882 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
| 4883 | scoped_ptr<HttpTransaction> trans( |
| 4884 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4885 | |
[email protected] | da81f13 | 2010-08-18 23:39:29 | [diff] [blame] | 4886 | MockWrite data_writes[] = { |
| 4887 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 4888 | "Host: www.google.com\r\n" |
| 4889 | "Proxy-Connection: keep-alive\r\n" |
| 4890 | "User-Agent: Chromium Ultra Awesome X Edition\r\n\r\n"), |
| 4891 | }; |
| 4892 | MockRead data_reads[] = { |
| 4893 | // Return an error, so the transaction stops here (this test isn't |
| 4894 | // interested in the rest). |
| 4895 | MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"), |
| 4896 | MockRead("Proxy-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 4897 | MockRead("Proxy-Connection: close\r\n\r\n"), |
| 4898 | }; |
| 4899 | |
| 4900 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4901 | data_writes, arraysize(data_writes)); |
| 4902 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 4903 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4904 | TestCompletionCallback callback; |
[email protected] | da81f13 | 2010-08-18 23:39:29 | [diff] [blame] | 4905 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4906 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | da81f13 | 2010-08-18 23:39:29 | [diff] [blame] | 4907 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4908 | |
| 4909 | rv = callback.WaitForResult(); |
| 4910 | EXPECT_EQ(OK, rv); |
| 4911 | } |
| 4912 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4913 | TEST_F(HttpNetworkTransactionTest, BuildRequest_Referer) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4914 | HttpRequestInfo request; |
| 4915 | request.method = "GET"; |
| 4916 | request.url = GURL("http://www.google.com/"); |
| 4917 | request.load_flags = 0; |
[email protected] | c1045010 | 2011-06-27 09:06:16 | [diff] [blame] | 4918 | request.extra_headers.SetHeader(HttpRequestHeaders::kReferer, |
| 4919 | "http://the.previous.site.com/"); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4920 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 4921 | SessionDependencies session_deps; |
| 4922 | scoped_ptr<HttpTransaction> trans( |
| 4923 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4924 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4925 | MockWrite data_writes[] = { |
| 4926 | MockWrite("GET / HTTP/1.1\r\n" |
| 4927 | "Host: www.google.com\r\n" |
| 4928 | "Connection: keep-alive\r\n" |
| 4929 | "Referer: http://the.previous.site.com/\r\n\r\n"), |
| 4930 | }; |
| 4931 | |
| 4932 | // Lastly, the server responds with the actual content. |
| 4933 | MockRead data_reads[] = { |
| 4934 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4935 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4936 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4937 | MockRead(false, OK), |
| 4938 | }; |
| 4939 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4940 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4941 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4942 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4943 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4944 | TestCompletionCallback callback; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4945 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4946 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4947 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4948 | |
| 4949 | rv = callback.WaitForResult(); |
| 4950 | EXPECT_EQ(OK, rv); |
| 4951 | } |
| 4952 | |
| 4953 | TEST_F(HttpNetworkTransactionTest, BuildRequest_PostContentLengthZero) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4954 | HttpRequestInfo request; |
| 4955 | request.method = "POST"; |
| 4956 | request.url = GURL("http://www.google.com/"); |
| 4957 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 4958 | SessionDependencies session_deps; |
| 4959 | scoped_ptr<HttpTransaction> trans( |
| 4960 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4961 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4962 | MockWrite data_writes[] = { |
| 4963 | MockWrite("POST / HTTP/1.1\r\n" |
| 4964 | "Host: www.google.com\r\n" |
| 4965 | "Connection: keep-alive\r\n" |
| 4966 | "Content-Length: 0\r\n\r\n"), |
| 4967 | }; |
| 4968 | |
| 4969 | // Lastly, the server responds with the actual content. |
| 4970 | MockRead data_reads[] = { |
| 4971 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 4972 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 4973 | MockRead("Content-Length: 100\r\n\r\n"), |
| 4974 | MockRead(false, OK), |
| 4975 | }; |
| 4976 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 4977 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 4978 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 4979 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4980 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4981 | TestCompletionCallback callback; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4982 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 4983 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4984 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 4985 | |
| 4986 | rv = callback.WaitForResult(); |
| 4987 | EXPECT_EQ(OK, rv); |
| 4988 | } |
| 4989 | |
| 4990 | TEST_F(HttpNetworkTransactionTest, BuildRequest_PutContentLengthZero) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4991 | HttpRequestInfo request; |
| 4992 | request.method = "PUT"; |
| 4993 | request.url = GURL("http://www.google.com/"); |
| 4994 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 4995 | SessionDependencies session_deps; |
| 4996 | scoped_ptr<HttpTransaction> trans( |
| 4997 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 4998 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 4999 | MockWrite data_writes[] = { |
| 5000 | MockWrite("PUT / HTTP/1.1\r\n" |
| 5001 | "Host: www.google.com\r\n" |
| 5002 | "Connection: keep-alive\r\n" |
| 5003 | "Content-Length: 0\r\n\r\n"), |
| 5004 | }; |
| 5005 | |
| 5006 | // Lastly, the server responds with the actual content. |
| 5007 | MockRead data_reads[] = { |
| 5008 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 5009 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 5010 | MockRead("Content-Length: 100\r\n\r\n"), |
| 5011 | MockRead(false, OK), |
| 5012 | }; |
| 5013 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5014 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 5015 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5016 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5017 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5018 | TestCompletionCallback callback; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5019 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5020 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5021 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5022 | |
| 5023 | rv = callback.WaitForResult(); |
| 5024 | EXPECT_EQ(OK, rv); |
| 5025 | } |
| 5026 | |
| 5027 | TEST_F(HttpNetworkTransactionTest, BuildRequest_HeadContentLengthZero) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5028 | HttpRequestInfo request; |
| 5029 | request.method = "HEAD"; |
| 5030 | request.url = GURL("http://www.google.com/"); |
| 5031 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 5032 | SessionDependencies session_deps; |
| 5033 | scoped_ptr<HttpTransaction> trans( |
| 5034 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5035 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5036 | MockWrite data_writes[] = { |
| 5037 | MockWrite("HEAD / HTTP/1.1\r\n" |
| 5038 | "Host: www.google.com\r\n" |
| 5039 | "Connection: keep-alive\r\n" |
| 5040 | "Content-Length: 0\r\n\r\n"), |
| 5041 | }; |
| 5042 | |
| 5043 | // Lastly, the server responds with the actual content. |
| 5044 | MockRead data_reads[] = { |
| 5045 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 5046 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 5047 | MockRead("Content-Length: 100\r\n\r\n"), |
| 5048 | MockRead(false, OK), |
| 5049 | }; |
| 5050 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5051 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 5052 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5053 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5054 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5055 | TestCompletionCallback callback; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5056 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5057 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5058 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5059 | |
| 5060 | rv = callback.WaitForResult(); |
| 5061 | EXPECT_EQ(OK, rv); |
| 5062 | } |
| 5063 | |
| 5064 | TEST_F(HttpNetworkTransactionTest, BuildRequest_CacheControlNoCache) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5065 | HttpRequestInfo request; |
| 5066 | request.method = "GET"; |
| 5067 | request.url = GURL("http://www.google.com/"); |
| 5068 | request.load_flags = LOAD_BYPASS_CACHE; |
| 5069 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 5070 | SessionDependencies session_deps; |
| 5071 | scoped_ptr<HttpTransaction> trans( |
| 5072 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5073 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5074 | MockWrite data_writes[] = { |
| 5075 | MockWrite("GET / HTTP/1.1\r\n" |
| 5076 | "Host: www.google.com\r\n" |
| 5077 | "Connection: keep-alive\r\n" |
| 5078 | "Pragma: no-cache\r\n" |
| 5079 | "Cache-Control: no-cache\r\n\r\n"), |
| 5080 | }; |
| 5081 | |
| 5082 | // Lastly, the server responds with the actual content. |
| 5083 | MockRead data_reads[] = { |
| 5084 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 5085 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 5086 | MockRead("Content-Length: 100\r\n\r\n"), |
| 5087 | MockRead(false, OK), |
| 5088 | }; |
| 5089 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5090 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 5091 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5092 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5093 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5094 | TestCompletionCallback callback; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5095 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5096 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5097 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5098 | |
| 5099 | rv = callback.WaitForResult(); |
| 5100 | EXPECT_EQ(OK, rv); |
| 5101 | } |
| 5102 | |
| 5103 | TEST_F(HttpNetworkTransactionTest, |
| 5104 | BuildRequest_CacheControlValidateCache) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5105 | HttpRequestInfo request; |
| 5106 | request.method = "GET"; |
| 5107 | request.url = GURL("http://www.google.com/"); |
| 5108 | request.load_flags = LOAD_VALIDATE_CACHE; |
| 5109 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 5110 | SessionDependencies session_deps; |
| 5111 | scoped_ptr<HttpTransaction> trans( |
| 5112 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5113 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5114 | MockWrite data_writes[] = { |
| 5115 | MockWrite("GET / HTTP/1.1\r\n" |
| 5116 | "Host: www.google.com\r\n" |
| 5117 | "Connection: keep-alive\r\n" |
| 5118 | "Cache-Control: max-age=0\r\n\r\n"), |
| 5119 | }; |
| 5120 | |
| 5121 | // Lastly, the server responds with the actual content. |
| 5122 | MockRead data_reads[] = { |
| 5123 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 5124 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 5125 | MockRead("Content-Length: 100\r\n\r\n"), |
| 5126 | MockRead(false, OK), |
| 5127 | }; |
| 5128 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5129 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 5130 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5131 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5132 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5133 | TestCompletionCallback callback; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5134 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5135 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5136 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5137 | |
| 5138 | rv = callback.WaitForResult(); |
| 5139 | EXPECT_EQ(OK, rv); |
| 5140 | } |
| 5141 | |
| 5142 | TEST_F(HttpNetworkTransactionTest, BuildRequest_ExtraHeaders) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5143 | HttpRequestInfo request; |
| 5144 | request.method = "GET"; |
| 5145 | request.url = GURL("http://www.google.com/"); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 5146 | request.extra_headers.SetHeader("FooHeader", "Bar"); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5147 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 5148 | SessionDependencies session_deps; |
| 5149 | scoped_ptr<HttpTransaction> trans( |
| 5150 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5151 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5152 | MockWrite data_writes[] = { |
| 5153 | MockWrite("GET / HTTP/1.1\r\n" |
| 5154 | "Host: www.google.com\r\n" |
| 5155 | "Connection: keep-alive\r\n" |
| 5156 | "FooHeader: Bar\r\n\r\n"), |
| 5157 | }; |
| 5158 | |
| 5159 | // Lastly, the server responds with the actual content. |
| 5160 | MockRead data_reads[] = { |
| 5161 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 5162 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 5163 | MockRead("Content-Length: 100\r\n\r\n"), |
| 5164 | MockRead(false, OK), |
| 5165 | }; |
| 5166 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5167 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 5168 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5169 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5170 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5171 | TestCompletionCallback callback; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5172 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5173 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 5174 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5175 | |
| 5176 | rv = callback.WaitForResult(); |
| 5177 | EXPECT_EQ(OK, rv); |
| 5178 | } |
| 5179 | |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 5180 | TEST_F(HttpNetworkTransactionTest, BuildRequest_ExtraHeadersStripped) { |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 5181 | HttpRequestInfo request; |
| 5182 | request.method = "GET"; |
| 5183 | request.url = GURL("http://www.google.com/"); |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 5184 | request.extra_headers.SetHeader("referer", "www.foo.com"); |
| 5185 | request.extra_headers.SetHeader("hEllo", "Kitty"); |
| 5186 | request.extra_headers.SetHeader("FoO", "bar"); |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 5187 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 5188 | SessionDependencies session_deps; |
| 5189 | scoped_ptr<HttpTransaction> trans( |
| 5190 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5191 | |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 5192 | MockWrite data_writes[] = { |
| 5193 | MockWrite("GET / HTTP/1.1\r\n" |
| 5194 | "Host: www.google.com\r\n" |
| 5195 | "Connection: keep-alive\r\n" |
[email protected] | c1045010 | 2011-06-27 09:06:16 | [diff] [blame] | 5196 | "referer: www.foo.com\r\n" |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 5197 | "hEllo: Kitty\r\n" |
| 5198 | "FoO: bar\r\n\r\n"), |
| 5199 | }; |
| 5200 | |
| 5201 | // Lastly, the server responds with the actual content. |
| 5202 | MockRead data_reads[] = { |
| 5203 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 5204 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 5205 | MockRead("Content-Length: 100\r\n\r\n"), |
| 5206 | MockRead(false, OK), |
| 5207 | }; |
| 5208 | |
| 5209 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 5210 | data_writes, arraysize(data_writes)); |
| 5211 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 5212 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5213 | TestCompletionCallback callback; |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 5214 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5215 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 5216 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5217 | |
| 5218 | rv = callback.WaitForResult(); |
| 5219 | EXPECT_EQ(OK, rv); |
| 5220 | } |
| 5221 | |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 5222 | // http://crbug.com/112682 |
[email protected] | ee0b101 | 2012-02-04 04:30:25 | [diff] [blame] | 5223 | #if defined(OS_MACOSX) |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 5224 | #define MAYBE_SOCKS4_HTTP_GET DISABLED_SOCKS4_HTTP_GET |
[email protected] | ee0b101 | 2012-02-04 04:30:25 | [diff] [blame] | 5225 | #else |
| 5226 | #define MAYBE_SOCKS4_HTTP_GET SOCKS4_HTTP_GET |
| 5227 | #endif |
| 5228 | |
| 5229 | TEST_F(HttpNetworkTransactionTest, MAYBE_SOCKS4_HTTP_GET) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 5230 | HttpRequestInfo request; |
| 5231 | request.method = "GET"; |
| 5232 | request.url = GURL("http://www.google.com/"); |
| 5233 | request.load_flags = 0; |
| 5234 | |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 5235 | SessionDependencies session_deps( |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 5236 | ProxyService::CreateFixed("socks4://myproxy:1080")); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5237 | |
| 5238 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 5239 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5240 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5241 | char write_buffer[] = { 0x04, 0x01, 0x00, 0x50, 127, 0, 0, 1, 0 }; |
| 5242 | char read_buffer[] = { 0x00, 0x5A, 0x00, 0x00, 0, 0, 0, 0 }; |
| 5243 | |
| 5244 | MockWrite data_writes[] = { |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5245 | MockWrite(true, write_buffer, arraysize(write_buffer)), |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5246 | MockWrite("GET / HTTP/1.1\r\n" |
| 5247 | "Host: www.google.com\r\n" |
| 5248 | "Connection: keep-alive\r\n\r\n") |
| 5249 | }; |
| 5250 | |
| 5251 | MockRead data_reads[] = { |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5252 | MockRead(true, read_buffer, arraysize(read_buffer)), |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5253 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 5254 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), |
| 5255 | MockRead("Payload"), |
| 5256 | MockRead(false, OK) |
| 5257 | }; |
| 5258 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5259 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 5260 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5261 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5262 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5263 | TestCompletionCallback callback; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5264 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5265 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5266 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5267 | |
| 5268 | rv = callback.WaitForResult(); |
| 5269 | EXPECT_EQ(OK, rv); |
| 5270 | |
| 5271 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 5272 | ASSERT_TRUE(response != NULL); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5273 | |
| 5274 | std::string response_text; |
| 5275 | rv = ReadTransaction(trans.get(), &response_text); |
| 5276 | EXPECT_EQ(OK, rv); |
| 5277 | EXPECT_EQ("Payload", response_text); |
| 5278 | } |
| 5279 | |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 5280 | // http://crbug.com/112682 |
[email protected] | ee0b101 | 2012-02-04 04:30:25 | [diff] [blame] | 5281 | #if defined(OS_MACOSX) |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 5282 | #define MAYBE_SOCKS4_SSL_GET DISABLED_SOCKS4_SSL_GET |
[email protected] | ee0b101 | 2012-02-04 04:30:25 | [diff] [blame] | 5283 | #else |
| 5284 | #define MAYBE_SOCKS4_SSL_GET SOCKS4_SSL_GET |
| 5285 | #endif |
| 5286 | |
| 5287 | TEST_F(HttpNetworkTransactionTest, MAYBE_SOCKS4_SSL_GET) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 5288 | HttpRequestInfo request; |
| 5289 | request.method = "GET"; |
| 5290 | request.url = GURL("https://www.google.com/"); |
| 5291 | request.load_flags = 0; |
| 5292 | |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 5293 | SessionDependencies session_deps( |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 5294 | ProxyService::CreateFixed("socks4://myproxy:1080")); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5295 | |
| 5296 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 5297 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5298 | |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5299 | unsigned char write_buffer[] = { 0x04, 0x01, 0x01, 0xBB, 127, 0, 0, 1, 0 }; |
| 5300 | unsigned char read_buffer[] = { 0x00, 0x5A, 0x00, 0x00, 0, 0, 0, 0 }; |
| 5301 | |
| 5302 | MockWrite data_writes[] = { |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5303 | MockWrite(true, reinterpret_cast<char*>(write_buffer), |
| 5304 | arraysize(write_buffer)), |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5305 | MockWrite("GET / HTTP/1.1\r\n" |
| 5306 | "Host: www.google.com\r\n" |
| 5307 | "Connection: keep-alive\r\n\r\n") |
| 5308 | }; |
| 5309 | |
| 5310 | MockRead data_reads[] = { |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5311 | MockWrite(true, reinterpret_cast<char*>(read_buffer), |
| 5312 | arraysize(read_buffer)), |
| 5313 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 5314 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), |
| 5315 | MockRead("Payload"), |
| 5316 | MockRead(false, OK) |
| 5317 | }; |
| 5318 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5319 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 5320 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5321 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5322 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5323 | SSLSocketDataProvider ssl(true, OK); |
| 5324 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5325 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5326 | TestCompletionCallback callback; |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5327 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5328 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5329 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5330 | |
| 5331 | rv = callback.WaitForResult(); |
| 5332 | EXPECT_EQ(OK, rv); |
| 5333 | |
| 5334 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 5335 | ASSERT_TRUE(response != NULL); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5336 | |
| 5337 | std::string response_text; |
| 5338 | rv = ReadTransaction(trans.get(), &response_text); |
| 5339 | EXPECT_EQ(OK, rv); |
| 5340 | EXPECT_EQ("Payload", response_text); |
| 5341 | } |
| 5342 | |
| 5343 | TEST_F(HttpNetworkTransactionTest, SOCKS5_HTTP_GET) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 5344 | HttpRequestInfo request; |
| 5345 | request.method = "GET"; |
| 5346 | request.url = GURL("http://www.google.com/"); |
| 5347 | request.load_flags = 0; |
| 5348 | |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 5349 | SessionDependencies session_deps( |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 5350 | ProxyService::CreateFixed("socks5://myproxy:1080")); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5351 | |
| 5352 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 5353 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5354 | |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5355 | const char kSOCKS5GreetRequest[] = { 0x05, 0x01, 0x00 }; |
| 5356 | const char kSOCKS5GreetResponse[] = { 0x05, 0x00 }; |
[email protected] | f209dba | 2009-12-18 00:24:37 | [diff] [blame] | 5357 | const char kSOCKS5OkRequest[] = { |
| 5358 | 0x05, // Version |
| 5359 | 0x01, // Command (CONNECT) |
| 5360 | 0x00, // Reserved. |
| 5361 | 0x03, // Address type (DOMAINNAME). |
| 5362 | 0x0E, // Length of domain (14) |
| 5363 | // Domain string: |
| 5364 | 'w', 'w', 'w', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'c', 'o', 'm', |
| 5365 | 0x00, 0x50, // 16-bit port (80) |
| 5366 | }; |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5367 | const char kSOCKS5OkResponse[] = |
| 5368 | { 0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50 }; |
| 5369 | |
| 5370 | MockWrite data_writes[] = { |
| 5371 | MockWrite(true, kSOCKS5GreetRequest, arraysize(kSOCKS5GreetRequest)), |
| 5372 | MockWrite(true, kSOCKS5OkRequest, arraysize(kSOCKS5OkRequest)), |
| 5373 | MockWrite("GET / HTTP/1.1\r\n" |
| 5374 | "Host: www.google.com\r\n" |
| 5375 | "Connection: keep-alive\r\n\r\n") |
| 5376 | }; |
| 5377 | |
| 5378 | MockRead data_reads[] = { |
| 5379 | MockWrite(true, kSOCKS5GreetResponse, arraysize(kSOCKS5GreetResponse)), |
| 5380 | MockWrite(true, kSOCKS5OkResponse, arraysize(kSOCKS5OkResponse)), |
| 5381 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 5382 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), |
| 5383 | MockRead("Payload"), |
| 5384 | MockRead(false, OK) |
| 5385 | }; |
| 5386 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5387 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 5388 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5389 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5390 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5391 | TestCompletionCallback callback; |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5392 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5393 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5394 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5395 | |
| 5396 | rv = callback.WaitForResult(); |
| 5397 | EXPECT_EQ(OK, rv); |
| 5398 | |
| 5399 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 5400 | ASSERT_TRUE(response != NULL); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5401 | |
| 5402 | std::string response_text; |
| 5403 | rv = ReadTransaction(trans.get(), &response_text); |
| 5404 | EXPECT_EQ(OK, rv); |
| 5405 | EXPECT_EQ("Payload", response_text); |
| 5406 | } |
| 5407 | |
| 5408 | TEST_F(HttpNetworkTransactionTest, SOCKS5_SSL_GET) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 5409 | HttpRequestInfo request; |
| 5410 | request.method = "GET"; |
| 5411 | request.url = GURL("https://www.google.com/"); |
| 5412 | request.load_flags = 0; |
| 5413 | |
[email protected] | 80d6524d | 2009-08-18 03:58:09 | [diff] [blame] | 5414 | SessionDependencies session_deps( |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 5415 | ProxyService::CreateFixed("socks5://myproxy:1080")); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5416 | |
| 5417 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 5418 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5419 | |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5420 | const char kSOCKS5GreetRequest[] = { 0x05, 0x01, 0x00 }; |
| 5421 | const char kSOCKS5GreetResponse[] = { 0x05, 0x00 }; |
[email protected] | f209dba | 2009-12-18 00:24:37 | [diff] [blame] | 5422 | const unsigned char kSOCKS5OkRequest[] = { |
| 5423 | 0x05, // Version |
| 5424 | 0x01, // Command (CONNECT) |
| 5425 | 0x00, // Reserved. |
| 5426 | 0x03, // Address type (DOMAINNAME). |
| 5427 | 0x0E, // Length of domain (14) |
| 5428 | // Domain string: |
| 5429 | 'w', 'w', 'w', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'c', 'o', 'm', |
| 5430 | 0x01, 0xBB, // 16-bit port (443) |
| 5431 | }; |
| 5432 | |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 5433 | const char kSOCKS5OkResponse[] = |
| 5434 | { 0x05, 0x00, 0x00, 0x01, 0, 0, 0, 0, 0x00, 0x00 }; |
| 5435 | |
| 5436 | MockWrite data_writes[] = { |
| 5437 | MockWrite(true, kSOCKS5GreetRequest, arraysize(kSOCKS5GreetRequest)), |
| 5438 | MockWrite(true, reinterpret_cast<const char*>(kSOCKS5OkRequest), |
| 5439 | arraysize(kSOCKS5OkRequest)), |
| 5440 | MockWrite("GET / HTTP/1.1\r\n" |
| 5441 | "Host: www.google.com\r\n" |
| 5442 | "Connection: keep-alive\r\n\r\n") |
| 5443 | }; |
| 5444 | |
| 5445 | MockRead data_reads[] = { |
| 5446 | MockWrite(true, kSOCKS5GreetResponse, arraysize(kSOCKS5GreetResponse)), |
| 5447 | MockWrite(true, kSOCKS5OkResponse, arraysize(kSOCKS5OkResponse)), |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5448 | MockRead("HTTP/1.0 200 OK\r\n"), |
| 5449 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n\r\n"), |
| 5450 | MockRead("Payload"), |
| 5451 | MockRead(false, OK) |
| 5452 | }; |
| 5453 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5454 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 5455 | data_writes, arraysize(data_writes)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5456 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5457 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5458 | SSLSocketDataProvider ssl(true, OK); |
| 5459 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5460 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5461 | TestCompletionCallback callback; |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5462 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5463 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5464 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5465 | |
| 5466 | rv = callback.WaitForResult(); |
| 5467 | EXPECT_EQ(OK, rv); |
| 5468 | |
| 5469 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 5470 | ASSERT_TRUE(response != NULL); |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 5471 | |
| 5472 | std::string response_text; |
| 5473 | rv = ReadTransaction(trans.get(), &response_text); |
| 5474 | EXPECT_EQ(OK, rv); |
| 5475 | EXPECT_EQ("Payload", response_text); |
| 5476 | } |
| 5477 | |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5478 | // Tests that for connection endpoints the group names are correctly set. |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5479 | |
| 5480 | struct GroupNameTest { |
| 5481 | std::string proxy_server; |
| 5482 | std::string url; |
| 5483 | std::string expected_group_name; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5484 | bool ssl; |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5485 | }; |
| 5486 | |
| 5487 | scoped_refptr<HttpNetworkSession> SetupSessionForGroupNameTests( |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 5488 | SessionDependencies* session_deps) { |
| 5489 | scoped_refptr<HttpNetworkSession> session(CreateSession(session_deps)); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5490 | |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 5491 | HttpServerProperties* http_server_properties = |
| 5492 | session->http_server_properties(); |
| 5493 | http_server_properties->SetAlternateProtocol( |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5494 | HostPortPair("host.with.alternate", 80), 443, |
[email protected] | 8d2f701 | 2012-02-16 00:08:04 | [diff] [blame] | 5495 | NPN_SPDY_21); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5496 | |
| 5497 | return session; |
| 5498 | } |
| 5499 | |
| 5500 | int GroupNameTransactionHelper( |
| 5501 | const std::string& url, |
| 5502 | const scoped_refptr<HttpNetworkSession>& session) { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5503 | HttpRequestInfo request; |
| 5504 | request.method = "GET"; |
| 5505 | request.url = GURL(url); |
| 5506 | request.load_flags = 0; |
| 5507 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 5508 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 5509 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5510 | TestCompletionCallback callback; |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5511 | |
| 5512 | // We do not complete this request, the dtor will clean the transaction up. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5513 | return trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5514 | } |
| 5515 | |
| 5516 | TEST_F(HttpNetworkTransactionTest, GroupNameForDirectConnections) { |
| 5517 | const GroupNameTest tests[] = { |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5518 | { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5519 | "", // unused |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5520 | "http://www.google.com/direct", |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5521 | "www.google.com:80", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5522 | false, |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5523 | }, |
| 5524 | { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5525 | "", // unused |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5526 | "http://[2001:1418:13:1::25]/direct", |
| 5527 | "[2001:1418:13:1::25]:80", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5528 | false, |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5529 | }, |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5530 | |
| 5531 | // SSL Tests |
| 5532 | { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5533 | "", // unused |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5534 | "https://www.google.com/direct_ssl", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 5535 | "ssl/www.google.com:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5536 | true, |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5537 | }, |
| 5538 | { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5539 | "", // unused |
| 5540 | "https://[2001:1418:13:1::25]/direct", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 5541 | "ssl/[2001:1418:13:1::25]:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5542 | true, |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5543 | }, |
| 5544 | { |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5545 | "", // unused |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5546 | "http://host.with.alternate/direct", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 5547 | "ssl/host.with.alternate:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5548 | true, |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5549 | }, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5550 | }; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5551 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 5552 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5553 | |
| 5554 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 5555 | SessionDependencies session_deps( |
| 5556 | ProxyService::CreateFixed(tests[i].proxy_server)); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5557 | scoped_refptr<HttpNetworkSession> session( |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 5558 | SetupSessionForGroupNameTests(&session_deps)); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5559 | |
| 5560 | HttpNetworkSessionPeer peer(session); |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 5561 | CaptureGroupNameTransportSocketPool* transport_conn_pool = |
| 5562 | new CaptureGroupNameTransportSocketPool(NULL, NULL); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 5563 | CaptureGroupNameSSLSocketPool* ssl_conn_pool = |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 5564 | new CaptureGroupNameSSLSocketPool(NULL, NULL); |
[email protected] | a42dbd14 | 2011-11-17 16:42:02 | [diff] [blame] | 5565 | MockClientSocketPoolManager* mock_pool_manager = |
| 5566 | new MockClientSocketPoolManager; |
| 5567 | mock_pool_manager->SetTransportSocketPool(transport_conn_pool); |
| 5568 | mock_pool_manager->SetSSLSocketPool(ssl_conn_pool); |
| 5569 | peer.SetClientSocketPoolManager(mock_pool_manager); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5570 | |
| 5571 | EXPECT_EQ(ERR_IO_PENDING, |
| 5572 | GroupNameTransactionHelper(tests[i].url, session)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5573 | if (tests[i].ssl) |
| 5574 | EXPECT_EQ(tests[i].expected_group_name, |
| 5575 | ssl_conn_pool->last_group_name_received()); |
| 5576 | else |
| 5577 | EXPECT_EQ(tests[i].expected_group_name, |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 5578 | transport_conn_pool->last_group_name_received()); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5579 | } |
| 5580 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 5581 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5582 | } |
| 5583 | |
| 5584 | TEST_F(HttpNetworkTransactionTest, GroupNameForHTTPProxyConnections) { |
| 5585 | const GroupNameTest tests[] = { |
| 5586 | { |
| 5587 | "http_proxy", |
| 5588 | "http://www.google.com/http_proxy_normal", |
| 5589 | "www.google.com:80", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5590 | false, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5591 | }, |
| 5592 | |
| 5593 | // SSL Tests |
| 5594 | { |
| 5595 | "http_proxy", |
| 5596 | "https://www.google.com/http_connect_ssl", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 5597 | "ssl/www.google.com:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5598 | true, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5599 | }, |
[email protected] | af3490e | 2010-10-16 21:02:29 | [diff] [blame] | 5600 | |
[email protected] | 9faeded9 | 2010-04-29 20:03:05 | [diff] [blame] | 5601 | { |
| 5602 | "http_proxy", |
| 5603 | "http://host.with.alternate/direct", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 5604 | "ssl/host.with.alternate:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5605 | true, |
[email protected] | 9faeded9 | 2010-04-29 20:03:05 | [diff] [blame] | 5606 | }, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5607 | }; |
| 5608 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 5609 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5610 | |
| 5611 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 5612 | SessionDependencies session_deps( |
| 5613 | ProxyService::CreateFixed(tests[i].proxy_server)); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5614 | scoped_refptr<HttpNetworkSession> session( |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 5615 | SetupSessionForGroupNameTests(&session_deps)); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5616 | |
| 5617 | HttpNetworkSessionPeer peer(session); |
| 5618 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5619 | HostPortPair proxy_host("http_proxy", 80); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 5620 | CaptureGroupNameHttpProxySocketPool* http_proxy_pool = |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 5621 | new CaptureGroupNameHttpProxySocketPool(NULL, NULL); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 5622 | CaptureGroupNameSSLSocketPool* ssl_conn_pool = |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 5623 | new CaptureGroupNameSSLSocketPool(NULL, NULL); |
[email protected] | a42dbd14 | 2011-11-17 16:42:02 | [diff] [blame] | 5624 | |
| 5625 | MockClientSocketPoolManager* mock_pool_manager = |
| 5626 | new MockClientSocketPoolManager; |
| 5627 | mock_pool_manager->SetSocketPoolForHTTPProxy(proxy_host, http_proxy_pool); |
| 5628 | mock_pool_manager->SetSocketPoolForSSLWithProxy(proxy_host, ssl_conn_pool); |
| 5629 | peer.SetClientSocketPoolManager(mock_pool_manager); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5630 | |
| 5631 | EXPECT_EQ(ERR_IO_PENDING, |
| 5632 | GroupNameTransactionHelper(tests[i].url, session)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5633 | if (tests[i].ssl) |
| 5634 | EXPECT_EQ(tests[i].expected_group_name, |
| 5635 | ssl_conn_pool->last_group_name_received()); |
| 5636 | else |
| 5637 | EXPECT_EQ(tests[i].expected_group_name, |
| 5638 | http_proxy_pool->last_group_name_received()); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5639 | } |
| 5640 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 5641 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5642 | } |
| 5643 | |
| 5644 | TEST_F(HttpNetworkTransactionTest, GroupNameForSOCKSConnections) { |
| 5645 | const GroupNameTest tests[] = { |
| 5646 | { |
| 5647 | "socks4://socks_proxy:1080", |
| 5648 | "http://www.google.com/socks4_direct", |
| 5649 | "socks4/www.google.com:80", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5650 | false, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5651 | }, |
| 5652 | { |
| 5653 | "socks5://socks_proxy:1080", |
| 5654 | "http://www.google.com/socks5_direct", |
| 5655 | "socks5/www.google.com:80", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5656 | false, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5657 | }, |
| 5658 | |
| 5659 | // SSL Tests |
| 5660 | { |
| 5661 | "socks4://socks_proxy:1080", |
| 5662 | "https://www.google.com/socks4_ssl", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 5663 | "socks4/ssl/www.google.com:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5664 | true, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5665 | }, |
| 5666 | { |
| 5667 | "socks5://socks_proxy:1080", |
| 5668 | "https://www.google.com/socks5_ssl", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 5669 | "socks5/ssl/www.google.com:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5670 | true, |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5671 | }, |
[email protected] | af3490e | 2010-10-16 21:02:29 | [diff] [blame] | 5672 | |
[email protected] | 9faeded9 | 2010-04-29 20:03:05 | [diff] [blame] | 5673 | { |
| 5674 | "socks4://socks_proxy:1080", |
| 5675 | "http://host.with.alternate/direct", |
[email protected] | 0e88ad60 | 2010-05-04 23:47:02 | [diff] [blame] | 5676 | "socks4/ssl/host.with.alternate:443", |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5677 | true, |
[email protected] | 9faeded9 | 2010-04-29 20:03:05 | [diff] [blame] | 5678 | }, |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5679 | }; |
| 5680 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 5681 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5682 | |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5683 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 5684 | SessionDependencies session_deps( |
| 5685 | ProxyService::CreateFixed(tests[i].proxy_server)); |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5686 | scoped_refptr<HttpNetworkSession> session( |
[email protected] | 8b114dd7 | 2011-03-25 05:33:02 | [diff] [blame] | 5687 | SetupSessionForGroupNameTests(&session_deps)); |
| 5688 | |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5689 | HttpNetworkSessionPeer peer(session); |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5690 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5691 | HostPortPair proxy_host("socks_proxy", 1080); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 5692 | CaptureGroupNameSOCKSSocketPool* socks_conn_pool = |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 5693 | new CaptureGroupNameSOCKSSocketPool(NULL, NULL); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 5694 | CaptureGroupNameSSLSocketPool* ssl_conn_pool = |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 5695 | new CaptureGroupNameSSLSocketPool(NULL, NULL); |
[email protected] | a42dbd14 | 2011-11-17 16:42:02 | [diff] [blame] | 5696 | |
| 5697 | MockClientSocketPoolManager* mock_pool_manager = |
| 5698 | new MockClientSocketPoolManager; |
| 5699 | mock_pool_manager->SetSocketPoolForSOCKSProxy(proxy_host, socks_conn_pool); |
| 5700 | mock_pool_manager->SetSocketPoolForSSLWithProxy(proxy_host, ssl_conn_pool); |
| 5701 | peer.SetClientSocketPoolManager(mock_pool_manager); |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5702 | |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 5703 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5704 | |
[email protected] | 2d731a3 | 2010-04-29 01:04:06 | [diff] [blame] | 5705 | EXPECT_EQ(ERR_IO_PENDING, |
| 5706 | GroupNameTransactionHelper(tests[i].url, session)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 5707 | if (tests[i].ssl) |
| 5708 | EXPECT_EQ(tests[i].expected_group_name, |
| 5709 | ssl_conn_pool->last_group_name_received()); |
| 5710 | else |
| 5711 | EXPECT_EQ(tests[i].expected_group_name, |
| 5712 | socks_conn_pool->last_group_name_received()); |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5713 | } |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 5714 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 5715 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 04e5be3 | 2009-06-26 20:00:31 | [diff] [blame] | 5716 | } |
| 5717 | |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 5718 | TEST_F(HttpNetworkTransactionTest, ReconsiderProxyAfterFailedConnection) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 5719 | HttpRequestInfo request; |
| 5720 | request.method = "GET"; |
| 5721 | request.url = GURL("http://www.google.com/"); |
| 5722 | |
[email protected] | 5c6a17e | 2009-06-10 00:54:54 | [diff] [blame] | 5723 | SessionDependencies session_deps( |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 5724 | ProxyService::CreateFixed("myproxy:70;foobar:80")); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 5725 | |
[email protected] | 6971906 | 2010-01-05 20:09:21 | [diff] [blame] | 5726 | // This simulates failure resolving all hostnames; that means we will fail |
| 5727 | // connecting to both proxies (myproxy:70 and foobar:80). |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 5728 | session_deps.host_resolver->rules()->AddSimulatedFailure("*"); |
| 5729 | |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 5730 | scoped_ptr<HttpTransaction> trans( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 5731 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 5732 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5733 | TestCompletionCallback callback; |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 5734 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5735 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 5736 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5737 | |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 5738 | rv = callback.WaitForResult(); |
[email protected] | f7fccee | 2010-09-16 20:53:01 | [diff] [blame] | 5739 | EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, rv); |
[email protected] | 9172a98 | 2009-06-06 00:30:25 | [diff] [blame] | 5740 | } |
| 5741 | |
[email protected] | 685af59 | 2010-05-11 19:31:24 | [diff] [blame] | 5742 | // Base test to make sure that when the load flags for a request specify to |
| 5743 | // bypass the cache, the DNS cache is not used. |
| 5744 | void BypassHostCacheOnRefreshHelper(int load_flags) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 5745 | // Issue a request, asking to bypass the cache(s). |
| 5746 | HttpRequestInfo request; |
| 5747 | request.method = "GET"; |
| 5748 | request.load_flags = load_flags; |
| 5749 | request.url = GURL("http://www.google.com/"); |
| 5750 | |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5751 | SessionDependencies session_deps; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 5752 | |
[email protected] | a2c2fb9 | 2009-07-18 07:31:04 | [diff] [blame] | 5753 | // Select a host resolver that does caching. |
[email protected] | 73c4532 | 2010-10-01 23:57:54 | [diff] [blame] | 5754 | session_deps.host_resolver.reset(new MockCachingHostResolver); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 5755 | |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5756 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction( |
[email protected] | 5695b8c | 2009-09-30 21:36:43 | [diff] [blame] | 5757 | CreateSession(&session_deps))); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5758 | |
[email protected] | 6e78dfb | 2011-07-28 21:34:47 | [diff] [blame] | 5759 | // Warm up the host cache so it has an entry for "www.google.com". |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5760 | AddressList addrlist; |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 5761 | TestCompletionCallback callback; |
[email protected] | 94a0d3d9 | 2009-06-27 01:50:14 | [diff] [blame] | 5762 | int rv = session_deps.host_resolver->Resolve( |
[email protected] | 930cc74 | 2010-09-15 22:54:10 | [diff] [blame] | 5763 | HostResolver::RequestInfo(HostPortPair("www.google.com", 80)), &addrlist, |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 5764 | callback.callback(), NULL, BoundNetLog()); |
[email protected] | 6e78dfb | 2011-07-28 21:34:47 | [diff] [blame] | 5765 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5766 | rv = callback.WaitForResult(); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5767 | EXPECT_EQ(OK, rv); |
| 5768 | |
| 5769 | // Verify that it was added to host cache, by doing a subsequent async lookup |
| 5770 | // and confirming it completes synchronously. |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 5771 | rv = session_deps.host_resolver->Resolve( |
[email protected] | 930cc74 | 2010-09-15 22:54:10 | [diff] [blame] | 5772 | HostResolver::RequestInfo(HostPortPair("www.google.com", 80)), &addrlist, |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 5773 | callback.callback(), NULL, BoundNetLog()); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 5774 | ASSERT_EQ(OK, rv); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5775 | |
| 5776 | // Inject a failure the next time that "www.google.com" is resolved. This way |
| 5777 | // we can tell if the next lookup hit the cache, or the "network". |
| 5778 | // (cache --> success, "network" --> failure). |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 5779 | session_deps.host_resolver->rules()->AddSimulatedFailure("www.google.com"); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5780 | |
| 5781 | // Connect up a mock socket which will fail with ERR_UNEXPECTED during the |
| 5782 | // first read -- this won't be reached as the host resolution will fail first. |
| 5783 | MockRead data_reads[] = { MockRead(false, ERR_UNEXPECTED) }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5784 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5785 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5786 | |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5787 | // Run the request. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5788 | rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5789 | ASSERT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5790 | rv = callback.WaitForResult(); |
[email protected] | 3b9cca4 | 2009-06-16 01:08:28 | [diff] [blame] | 5791 | |
| 5792 | // If we bypassed the cache, we would have gotten a failure while resolving |
| 5793 | // "www.google.com". |
| 5794 | EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); |
| 5795 | } |
| 5796 | |
[email protected] | 685af59 | 2010-05-11 19:31:24 | [diff] [blame] | 5797 | // There are multiple load flags that should trigger the host cache bypass. |
| 5798 | // Test each in isolation: |
| 5799 | TEST_F(HttpNetworkTransactionTest, BypassHostCacheOnRefresh1) { |
| 5800 | BypassHostCacheOnRefreshHelper(LOAD_BYPASS_CACHE); |
| 5801 | } |
| 5802 | |
| 5803 | TEST_F(HttpNetworkTransactionTest, BypassHostCacheOnRefresh2) { |
| 5804 | BypassHostCacheOnRefreshHelper(LOAD_VALIDATE_CACHE); |
| 5805 | } |
| 5806 | |
| 5807 | TEST_F(HttpNetworkTransactionTest, BypassHostCacheOnRefresh3) { |
| 5808 | BypassHostCacheOnRefreshHelper(LOAD_DISABLE_CACHE); |
| 5809 | } |
| 5810 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5811 | // Make sure we can handle an error when writing the request. |
| 5812 | TEST_F(HttpNetworkTransactionTest, RequestWriteError) { |
| 5813 | SessionDependencies session_deps; |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 5814 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5815 | |
| 5816 | HttpRequestInfo request; |
| 5817 | request.method = "GET"; |
| 5818 | request.url = GURL("http://www.foo.com/"); |
| 5819 | request.load_flags = 0; |
| 5820 | |
| 5821 | MockWrite write_failure[] = { |
| 5822 | MockWrite(true, ERR_CONNECTION_RESET), |
| 5823 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5824 | StaticSocketDataProvider data(NULL, 0, |
| 5825 | write_failure, arraysize(write_failure)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5826 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5827 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5828 | TestCompletionCallback callback; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5829 | |
| 5830 | scoped_ptr<HttpTransaction> trans( |
| 5831 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5832 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5833 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5834 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5835 | |
| 5836 | rv = callback.WaitForResult(); |
| 5837 | EXPECT_EQ(ERR_CONNECTION_RESET, rv); |
| 5838 | } |
| 5839 | |
| 5840 | // Check that a connection closed after the start of the headers finishes ok. |
| 5841 | TEST_F(HttpNetworkTransactionTest, ConnectionClosedAfterStartOfHeaders) { |
| 5842 | SessionDependencies session_deps; |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 5843 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5844 | |
| 5845 | HttpRequestInfo request; |
| 5846 | request.method = "GET"; |
| 5847 | request.url = GURL("http://www.foo.com/"); |
| 5848 | request.load_flags = 0; |
| 5849 | |
| 5850 | MockRead data_reads[] = { |
| 5851 | MockRead("HTTP/1."), |
| 5852 | MockRead(false, OK), |
| 5853 | }; |
| 5854 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5855 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5856 | session_deps.socket_factory.AddSocketDataProvider(&data); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5857 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5858 | TestCompletionCallback callback; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5859 | |
| 5860 | scoped_ptr<HttpTransaction> trans( |
| 5861 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5862 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5863 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5864 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5865 | |
| 5866 | rv = callback.WaitForResult(); |
| 5867 | EXPECT_EQ(OK, rv); |
| 5868 | |
| 5869 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 5870 | ASSERT_TRUE(response != NULL); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5871 | |
| 5872 | EXPECT_TRUE(response->headers != NULL); |
| 5873 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
| 5874 | |
| 5875 | std::string response_data; |
| 5876 | rv = ReadTransaction(trans.get(), &response_data); |
| 5877 | EXPECT_EQ(OK, rv); |
| 5878 | EXPECT_EQ("", response_data); |
| 5879 | } |
| 5880 | |
| 5881 | // Make sure that a dropped connection while draining the body for auth |
| 5882 | // restart does the right thing. |
| 5883 | TEST_F(HttpNetworkTransactionTest, DrainResetOK) { |
| 5884 | SessionDependencies session_deps; |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 5885 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5886 | |
| 5887 | HttpRequestInfo request; |
| 5888 | request.method = "GET"; |
| 5889 | request.url = GURL("http://www.google.com/"); |
| 5890 | request.load_flags = 0; |
| 5891 | |
| 5892 | MockWrite data_writes1[] = { |
| 5893 | MockWrite("GET / HTTP/1.1\r\n" |
| 5894 | "Host: www.google.com\r\n" |
| 5895 | "Connection: keep-alive\r\n\r\n"), |
| 5896 | }; |
| 5897 | |
| 5898 | MockRead data_reads1[] = { |
| 5899 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 5900 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 5901 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 5902 | MockRead("Content-Length: 14\r\n\r\n"), |
| 5903 | MockRead("Unauth"), |
| 5904 | MockRead(true, ERR_CONNECTION_RESET), |
| 5905 | }; |
| 5906 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5907 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 5908 | data_writes1, arraysize(data_writes1)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5909 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5910 | |
| 5911 | // After calling trans->RestartWithAuth(), this is the request we should |
| 5912 | // be issuing -- the final header line contains the credentials. |
| 5913 | MockWrite data_writes2[] = { |
| 5914 | MockWrite("GET / HTTP/1.1\r\n" |
| 5915 | "Host: www.google.com\r\n" |
| 5916 | "Connection: keep-alive\r\n" |
| 5917 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 5918 | }; |
| 5919 | |
| 5920 | // Lastly, the server responds with the actual content. |
| 5921 | MockRead data_reads2[] = { |
| 5922 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 5923 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 5924 | MockRead("Content-Length: 100\r\n\r\n"), |
| 5925 | MockRead(false, OK), |
| 5926 | }; |
| 5927 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5928 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 5929 | data_writes2, arraysize(data_writes2)); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5930 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5931 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5932 | TestCompletionCallback callback1; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5933 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 5934 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 5935 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5936 | int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5937 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5938 | |
| 5939 | rv = callback1.WaitForResult(); |
| 5940 | EXPECT_EQ(OK, rv); |
| 5941 | |
| 5942 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 5943 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 5944 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge.get())); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5945 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5946 | TestCompletionCallback callback2; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5947 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5948 | rv = trans->RestartWithAuth( |
| 5949 | AuthCredentials(kFoo, kBar), callback2.callback()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5950 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5951 | |
| 5952 | rv = callback2.WaitForResult(); |
| 5953 | EXPECT_EQ(OK, rv); |
| 5954 | |
| 5955 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 5956 | ASSERT_TRUE(response != NULL); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5957 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 5958 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 5959 | } |
| 5960 | |
| 5961 | // Test HTTPS connections going through a proxy that sends extra data. |
| 5962 | TEST_F(HttpNetworkTransactionTest, HTTPSViaProxyWithExtraData) { |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 5963 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5964 | |
| 5965 | HttpRequestInfo request; |
| 5966 | request.method = "GET"; |
| 5967 | request.url = GURL("https://www.google.com/"); |
| 5968 | request.load_flags = 0; |
| 5969 | |
| 5970 | MockRead proxy_reads[] = { |
| 5971 | MockRead("HTTP/1.0 200 Connected\r\n\r\nExtra data"), |
| 5972 | MockRead(false, OK) |
| 5973 | }; |
| 5974 | |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 5975 | StaticSocketDataProvider data(proxy_reads, arraysize(proxy_reads), NULL, 0); |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5976 | SSLSocketDataProvider ssl(true, OK); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5977 | |
[email protected] | 5ecc992a4 | 2009-11-11 01:41:59 | [diff] [blame] | 5978 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 5979 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5980 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5981 | TestCompletionCallback callback; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5982 | |
| 5983 | session_deps.socket_factory.ResetNextMockIndexes(); |
| 5984 | |
| 5985 | scoped_ptr<HttpTransaction> trans( |
| 5986 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 5987 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 5988 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 5989 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 5990 | |
| 5991 | rv = callback.WaitForResult(); |
| 5992 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, rv); |
| 5993 | } |
| 5994 | |
[email protected] | e22e136 | 2009-11-23 21:31:12 | [diff] [blame] | 5995 | TEST_F(HttpNetworkTransactionTest, LargeContentLengthThenClose) { |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 5996 | HttpRequestInfo request; |
| 5997 | request.method = "GET"; |
| 5998 | request.url = GURL("http://www.google.com/"); |
| 5999 | request.load_flags = 0; |
| 6000 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 6001 | SessionDependencies session_deps; |
| 6002 | scoped_ptr<HttpTransaction> trans( |
| 6003 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 6004 | |
[email protected] | e22e136 | 2009-11-23 21:31:12 | [diff] [blame] | 6005 | MockRead data_reads[] = { |
| 6006 | MockRead("HTTP/1.0 200 OK\r\nContent-Length:6719476739\r\n\r\n"), |
| 6007 | MockRead(false, OK), |
| 6008 | }; |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 6009 | |
| 6010 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
| 6011 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 6012 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6013 | TestCompletionCallback callback; |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 6014 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6015 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 6016 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6017 | |
| 6018 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6019 | |
| 6020 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 6021 | ASSERT_TRUE(response != NULL); |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 6022 | |
| 6023 | EXPECT_TRUE(response->headers != NULL); |
| 6024 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
| 6025 | |
| 6026 | std::string response_data; |
| 6027 | rv = ReadTransaction(trans.get(), &response_data); |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 6028 | EXPECT_EQ(ERR_CONNECTION_CLOSED, rv); |
[email protected] | e22e136 | 2009-11-23 21:31:12 | [diff] [blame] | 6029 | } |
| 6030 | |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 6031 | TEST_F(HttpNetworkTransactionTest, UploadFileSmallerThanLength) { |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 6032 | HttpRequestInfo request; |
| 6033 | request.method = "POST"; |
| 6034 | request.url = GURL("http://www.google.com/upload"); |
| 6035 | request.upload_data = new UploadData; |
| 6036 | request.load_flags = 0; |
| 6037 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 6038 | SessionDependencies session_deps; |
| 6039 | scoped_ptr<HttpTransaction> trans( |
| 6040 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 6041 | |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 6042 | FilePath temp_file_path; |
| 6043 | ASSERT_TRUE(file_util::CreateTemporaryFile(&temp_file_path)); |
| 6044 | const uint64 kFakeSize = 100000; // file is actually blank |
| 6045 | |
| 6046 | std::vector<UploadData::Element> elements; |
| 6047 | UploadData::Element element; |
| 6048 | element.SetToFilePath(temp_file_path); |
| 6049 | element.SetContentLength(kFakeSize); |
| 6050 | elements.push_back(element); |
[email protected] | 9349cfb | 2010-08-31 18:00:53 | [diff] [blame] | 6051 | request.upload_data->SetElements(elements); |
[email protected] | 1dce708 | 2012-02-10 07:39:11 | [diff] [blame] | 6052 | EXPECT_EQ(kFakeSize, request.upload_data->GetContentLengthSync()); |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 6053 | |
| 6054 | MockRead data_reads[] = { |
| 6055 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 6056 | MockRead("hello world"), |
| 6057 | MockRead(false, OK), |
| 6058 | }; |
[email protected] | 31a2bfe | 2010-02-09 08:03:39 | [diff] [blame] | 6059 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 6060 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 6061 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6062 | TestCompletionCallback callback; |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 6063 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6064 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 6065 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6066 | |
| 6067 | rv = callback.WaitForResult(); |
| 6068 | EXPECT_EQ(OK, rv); |
| 6069 | |
| 6070 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 6071 | ASSERT_TRUE(response != NULL); |
[email protected] | 95d88ffe | 2010-02-04 21:25:33 | [diff] [blame] | 6072 | |
| 6073 | EXPECT_TRUE(response->headers != NULL); |
| 6074 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
| 6075 | |
| 6076 | std::string response_data; |
| 6077 | rv = ReadTransaction(trans.get(), &response_data); |
| 6078 | EXPECT_EQ(OK, rv); |
| 6079 | EXPECT_EQ("hello world", response_data); |
| 6080 | |
| 6081 | file_util::Delete(temp_file_path, false); |
| 6082 | } |
| 6083 | |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6084 | TEST_F(HttpNetworkTransactionTest, UploadUnreadableFile) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 6085 | HttpRequestInfo request; |
| 6086 | request.method = "POST"; |
| 6087 | request.url = GURL("http://www.google.com/upload"); |
| 6088 | request.upload_data = new UploadData; |
| 6089 | request.load_flags = 0; |
| 6090 | |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6091 | // If we try to upload an unreadable file, the network stack should report |
| 6092 | // the file size as zero and upload zero bytes for that file. |
| 6093 | SessionDependencies session_deps; |
| 6094 | scoped_ptr<HttpTransaction> trans( |
| 6095 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 6096 | |
| 6097 | FilePath temp_file; |
| 6098 | ASSERT_TRUE(file_util::CreateTemporaryFile(&temp_file)); |
| 6099 | std::string temp_file_content("Unreadable file."); |
| 6100 | ASSERT_TRUE(file_util::WriteFile(temp_file, temp_file_content.c_str(), |
| 6101 | temp_file_content.length())); |
| 6102 | ASSERT_TRUE(file_util::MakeFileUnreadable(temp_file)); |
| 6103 | |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6104 | std::vector<UploadData::Element> elements; |
| 6105 | UploadData::Element element; |
| 6106 | element.SetToFilePath(temp_file); |
| 6107 | elements.push_back(element); |
[email protected] | 9349cfb | 2010-08-31 18:00:53 | [diff] [blame] | 6108 | request.upload_data->SetElements(elements); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6109 | |
| 6110 | MockRead data_reads[] = { |
| 6111 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 6112 | MockRead(false, OK), |
| 6113 | }; |
| 6114 | MockWrite data_writes[] = { |
| 6115 | MockWrite("POST /upload HTTP/1.1\r\n" |
| 6116 | "Host: www.google.com\r\n" |
| 6117 | "Connection: keep-alive\r\n" |
| 6118 | "Content-Length: 0\r\n\r\n"), |
| 6119 | MockWrite(false, OK), |
| 6120 | }; |
| 6121 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), data_writes, |
| 6122 | arraysize(data_writes)); |
| 6123 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 6124 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6125 | TestCompletionCallback callback; |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6126 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6127 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6128 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6129 | |
| 6130 | rv = callback.WaitForResult(); |
| 6131 | EXPECT_EQ(OK, rv); |
| 6132 | |
| 6133 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 6134 | ASSERT_TRUE(response != NULL); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6135 | EXPECT_TRUE(response->headers != NULL); |
| 6136 | EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine()); |
| 6137 | |
| 6138 | file_util::Delete(temp_file, false); |
| 6139 | } |
| 6140 | |
| 6141 | TEST_F(HttpNetworkTransactionTest, UnreadableUploadFileAfterAuthRestart) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 6142 | HttpRequestInfo request; |
| 6143 | request.method = "POST"; |
| 6144 | request.url = GURL("http://www.google.com/upload"); |
| 6145 | request.upload_data = new UploadData; |
| 6146 | request.load_flags = 0; |
| 6147 | |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6148 | SessionDependencies session_deps; |
| 6149 | scoped_ptr<HttpTransaction> trans( |
| 6150 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 6151 | |
| 6152 | FilePath temp_file; |
| 6153 | ASSERT_TRUE(file_util::CreateTemporaryFile(&temp_file)); |
| 6154 | std::string temp_file_contents("Unreadable file."); |
| 6155 | std::string unreadable_contents(temp_file_contents.length(), '\0'); |
| 6156 | ASSERT_TRUE(file_util::WriteFile(temp_file, temp_file_contents.c_str(), |
| 6157 | temp_file_contents.length())); |
| 6158 | |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6159 | std::vector<UploadData::Element> elements; |
| 6160 | UploadData::Element element; |
| 6161 | element.SetToFilePath(temp_file); |
| 6162 | elements.push_back(element); |
[email protected] | 9349cfb | 2010-08-31 18:00:53 | [diff] [blame] | 6163 | request.upload_data->SetElements(elements); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6164 | |
| 6165 | MockRead data_reads[] = { |
| 6166 | MockRead("HTTP/1.1 401 Unauthorized\r\n"), |
| 6167 | MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 6168 | MockRead("Content-Length: 0\r\n\r\n"), // No response body. |
| 6169 | |
| 6170 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 6171 | MockRead("Content-Length: 0\r\n\r\n"), |
| 6172 | MockRead(false, OK), |
| 6173 | }; |
| 6174 | MockWrite data_writes[] = { |
| 6175 | MockWrite("POST /upload HTTP/1.1\r\n" |
| 6176 | "Host: www.google.com\r\n" |
| 6177 | "Connection: keep-alive\r\n" |
| 6178 | "Content-Length: 16\r\n\r\n"), |
| 6179 | MockWrite(false, temp_file_contents.c_str()), |
| 6180 | |
| 6181 | MockWrite("POST /upload HTTP/1.1\r\n" |
| 6182 | "Host: www.google.com\r\n" |
| 6183 | "Connection: keep-alive\r\n" |
| 6184 | "Content-Length: 16\r\n" |
| 6185 | "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 6186 | MockWrite(false, unreadable_contents.c_str(), temp_file_contents.length()), |
| 6187 | MockWrite(false, OK), |
| 6188 | }; |
| 6189 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), data_writes, |
| 6190 | arraysize(data_writes)); |
| 6191 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 6192 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6193 | TestCompletionCallback callback1; |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6194 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6195 | int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6196 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6197 | |
| 6198 | rv = callback1.WaitForResult(); |
| 6199 | EXPECT_EQ(OK, rv); |
| 6200 | |
| 6201 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 6202 | ASSERT_TRUE(response != NULL); |
| 6203 | ASSERT_TRUE(response->headers != NULL); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6204 | EXPECT_EQ("HTTP/1.1 401 Unauthorized", response->headers->GetStatusLine()); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 6205 | EXPECT_TRUE(CheckBasicServerAuth(response->auth_challenge.get())); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6206 | |
| 6207 | // Now make the file unreadable and try again. |
| 6208 | ASSERT_TRUE(file_util::MakeFileUnreadable(temp_file)); |
| 6209 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6210 | TestCompletionCallback callback2; |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6211 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6212 | rv = trans->RestartWithAuth( |
| 6213 | AuthCredentials(kFoo, kBar), callback2.callback()); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6214 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6215 | |
| 6216 | rv = callback2.WaitForResult(); |
| 6217 | EXPECT_EQ(OK, rv); |
| 6218 | |
| 6219 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 6220 | ASSERT_TRUE(response != NULL); |
[email protected] | 6624b462 | 2010-03-29 19:58:36 | [diff] [blame] | 6221 | EXPECT_TRUE(response->headers != NULL); |
| 6222 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 6223 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 6224 | |
| 6225 | file_util::Delete(temp_file, false); |
| 6226 | } |
| 6227 | |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 6228 | // Tests that changes to Auth realms are treated like auth rejections. |
| 6229 | TEST_F(HttpNetworkTransactionTest, ChangeAuthRealms) { |
| 6230 | SessionDependencies session_deps; |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 6231 | |
| 6232 | HttpRequestInfo request; |
| 6233 | request.method = "GET"; |
| 6234 | request.url = GURL("http://www.google.com/"); |
| 6235 | request.load_flags = 0; |
| 6236 | |
| 6237 | // First transaction will request a resource and receive a Basic challenge |
| 6238 | // with realm="first_realm". |
| 6239 | MockWrite data_writes1[] = { |
| 6240 | MockWrite("GET / HTTP/1.1\r\n" |
| 6241 | "Host: www.google.com\r\n" |
| 6242 | "Connection: keep-alive\r\n" |
| 6243 | "\r\n"), |
| 6244 | }; |
| 6245 | MockRead data_reads1[] = { |
| 6246 | MockRead("HTTP/1.1 401 Unauthorized\r\n" |
| 6247 | "WWW-Authenticate: Basic realm=\"first_realm\"\r\n" |
| 6248 | "\r\n"), |
| 6249 | }; |
| 6250 | |
| 6251 | // After calling trans->RestartWithAuth(), provide an Authentication header |
| 6252 | // for first_realm. The server will reject and provide a challenge with |
| 6253 | // second_realm. |
| 6254 | MockWrite data_writes2[] = { |
| 6255 | MockWrite("GET / HTTP/1.1\r\n" |
| 6256 | "Host: www.google.com\r\n" |
| 6257 | "Connection: keep-alive\r\n" |
| 6258 | "Authorization: Basic Zmlyc3Q6YmF6\r\n" |
| 6259 | "\r\n"), |
| 6260 | }; |
| 6261 | MockRead data_reads2[] = { |
| 6262 | MockRead("HTTP/1.1 401 Unauthorized\r\n" |
| 6263 | "WWW-Authenticate: Basic realm=\"second_realm\"\r\n" |
| 6264 | "\r\n"), |
| 6265 | }; |
| 6266 | |
| 6267 | // This again fails, and goes back to first_realm. Make sure that the |
| 6268 | // entry is removed from cache. |
| 6269 | MockWrite data_writes3[] = { |
| 6270 | MockWrite("GET / HTTP/1.1\r\n" |
| 6271 | "Host: www.google.com\r\n" |
| 6272 | "Connection: keep-alive\r\n" |
| 6273 | "Authorization: Basic c2Vjb25kOmZvdQ==\r\n" |
| 6274 | "\r\n"), |
| 6275 | }; |
| 6276 | MockRead data_reads3[] = { |
| 6277 | MockRead("HTTP/1.1 401 Unauthorized\r\n" |
| 6278 | "WWW-Authenticate: Basic realm=\"first_realm\"\r\n" |
| 6279 | "\r\n"), |
| 6280 | }; |
| 6281 | |
| 6282 | // Try one last time (with the correct password) and get the resource. |
| 6283 | MockWrite data_writes4[] = { |
| 6284 | MockWrite("GET / HTTP/1.1\r\n" |
| 6285 | "Host: www.google.com\r\n" |
| 6286 | "Connection: keep-alive\r\n" |
| 6287 | "Authorization: Basic Zmlyc3Q6YmFy\r\n" |
| 6288 | "\r\n"), |
| 6289 | }; |
| 6290 | MockRead data_reads4[] = { |
| 6291 | MockRead("HTTP/1.1 200 OK\r\n" |
| 6292 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 6293 | "Content-Length: 5\r\n" |
| 6294 | "\r\n" |
| 6295 | "hello"), |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 6296 | }; |
| 6297 | |
| 6298 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 6299 | data_writes1, arraysize(data_writes1)); |
| 6300 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 6301 | data_writes2, arraysize(data_writes2)); |
| 6302 | StaticSocketDataProvider data3(data_reads3, arraysize(data_reads3), |
| 6303 | data_writes3, arraysize(data_writes3)); |
| 6304 | StaticSocketDataProvider data4(data_reads4, arraysize(data_reads4), |
| 6305 | data_writes4, arraysize(data_writes4)); |
| 6306 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 6307 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 6308 | session_deps.socket_factory.AddSocketDataProvider(&data3); |
| 6309 | session_deps.socket_factory.AddSocketDataProvider(&data4); |
| 6310 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6311 | TestCompletionCallback callback1; |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 6312 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 6313 | scoped_ptr<HttpTransaction> trans( |
| 6314 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 6315 | |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 6316 | // Issue the first request with Authorize headers. There should be a |
| 6317 | // password prompt for first_realm waiting to be filled in after the |
| 6318 | // transaction completes. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6319 | int rv = trans->Start(&request, callback1.callback(), BoundNetLog()); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 6320 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6321 | rv = callback1.WaitForResult(); |
| 6322 | EXPECT_EQ(OK, rv); |
| 6323 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 6324 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 6325 | const AuthChallengeInfo* challenge = response->auth_challenge.get(); |
| 6326 | ASSERT_FALSE(challenge == NULL); |
| 6327 | EXPECT_FALSE(challenge->is_proxy); |
| 6328 | EXPECT_EQ("www.google.com:80", challenge->challenger.ToString()); |
| 6329 | EXPECT_EQ("first_realm", challenge->realm); |
| 6330 | EXPECT_EQ("basic", challenge->scheme); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 6331 | |
| 6332 | // Issue the second request with an incorrect password. There should be a |
| 6333 | // password prompt for second_realm waiting to be filled in after the |
| 6334 | // transaction completes. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6335 | TestCompletionCallback callback2; |
| 6336 | rv = trans->RestartWithAuth( |
| 6337 | AuthCredentials(kFirst, kBaz), callback2.callback()); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 6338 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6339 | rv = callback2.WaitForResult(); |
| 6340 | EXPECT_EQ(OK, rv); |
| 6341 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 6342 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 6343 | challenge = response->auth_challenge.get(); |
| 6344 | ASSERT_FALSE(challenge == NULL); |
| 6345 | EXPECT_FALSE(challenge->is_proxy); |
| 6346 | EXPECT_EQ("www.google.com:80", challenge->challenger.ToString()); |
| 6347 | EXPECT_EQ("second_realm", challenge->realm); |
| 6348 | EXPECT_EQ("basic", challenge->scheme); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 6349 | |
| 6350 | // Issue the third request with another incorrect password. There should be |
| 6351 | // a password prompt for first_realm waiting to be filled in. If the password |
| 6352 | // prompt is not present, it indicates that the HttpAuthCacheEntry for |
| 6353 | // first_realm was not correctly removed. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6354 | TestCompletionCallback callback3; |
| 6355 | rv = trans->RestartWithAuth( |
| 6356 | AuthCredentials(kSecond, kFou), callback3.callback()); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 6357 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6358 | rv = callback3.WaitForResult(); |
| 6359 | EXPECT_EQ(OK, rv); |
| 6360 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 6361 | ASSERT_TRUE(response != NULL); |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 6362 | challenge = response->auth_challenge.get(); |
| 6363 | ASSERT_FALSE(challenge == NULL); |
| 6364 | EXPECT_FALSE(challenge->is_proxy); |
| 6365 | EXPECT_EQ("www.google.com:80", challenge->challenger.ToString()); |
| 6366 | EXPECT_EQ("first_realm", challenge->realm); |
| 6367 | EXPECT_EQ("basic", challenge->scheme); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 6368 | |
| 6369 | // Issue the fourth request with the correct password and username. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6370 | TestCompletionCallback callback4; |
| 6371 | rv = trans->RestartWithAuth( |
| 6372 | AuthCredentials(kFirst, kBar), callback4.callback()); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 6373 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6374 | rv = callback4.WaitForResult(); |
| 6375 | EXPECT_EQ(OK, rv); |
| 6376 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 6377 | ASSERT_TRUE(response != NULL); |
[email protected] | aeefc9e8 | 2010-02-19 16:18:27 | [diff] [blame] | 6378 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 6379 | } |
| 6380 | |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6381 | TEST_F(HttpNetworkTransactionTest, HonorAlternateProtocolHeader) { |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 6382 | HttpStreamFactory::set_next_protos(MakeNextProtos("foo", "bar", NULL)); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6383 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 6384 | |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6385 | SessionDependencies session_deps; |
| 6386 | |
| 6387 | MockRead data_reads[] = { |
| 6388 | MockRead("HTTP/1.1 200 OK\r\n"), |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 6389 | MockRead(kAlternateProtocolHttpHeader), |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6390 | MockRead("hello world"), |
| 6391 | MockRead(false, OK), |
| 6392 | }; |
| 6393 | |
| 6394 | HttpRequestInfo request; |
| 6395 | request.method = "GET"; |
| 6396 | request.url = GURL("http://www.google.com/"); |
| 6397 | request.load_flags = 0; |
| 6398 | |
| 6399 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
| 6400 | |
| 6401 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 6402 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6403 | TestCompletionCallback callback; |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6404 | |
| 6405 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 6406 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 6407 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6408 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6409 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 6410 | |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame] | 6411 | HostPortPair http_host_port_pair("www.google.com", 80); |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6412 | const HttpServerProperties& http_server_properties = |
| 6413 | *session->http_server_properties(); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6414 | EXPECT_FALSE( |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6415 | http_server_properties.HasAlternateProtocol(http_host_port_pair)); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6416 | |
| 6417 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6418 | |
| 6419 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 6420 | ASSERT_TRUE(response != NULL); |
| 6421 | ASSERT_TRUE(response->headers != NULL); |
| 6422 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 6423 | EXPECT_FALSE(response->was_fetched_via_spdy); |
| 6424 | EXPECT_FALSE(response->was_npn_negotiated); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6425 | |
| 6426 | std::string response_data; |
| 6427 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 6428 | EXPECT_EQ("hello world", response_data); |
| 6429 | |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6430 | ASSERT_TRUE(http_server_properties.HasAlternateProtocol(http_host_port_pair)); |
| 6431 | const PortAlternateProtocolPair alternate = |
| 6432 | http_server_properties.GetAlternateProtocol(http_host_port_pair); |
| 6433 | PortAlternateProtocolPair expected_alternate; |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6434 | expected_alternate.port = 443; |
[email protected] | 8d2f701 | 2012-02-16 00:08:04 | [diff] [blame] | 6435 | expected_alternate.protocol = NPN_SPDY_21; |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6436 | EXPECT_TRUE(expected_alternate.Equals(alternate)); |
[email protected] | a2cb812 | 2010-03-10 17:22:42 | [diff] [blame] | 6437 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6438 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 6439 | HttpStreamFactory::set_next_protos(std::vector<std::string>()); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6440 | } |
| 6441 | |
[email protected] | 8b95ed6 | 2011-03-18 18:12:03 | [diff] [blame] | 6442 | TEST_F(HttpNetworkTransactionTest, MarkBrokenAlternateProtocolAndFallback) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6443 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6444 | SessionDependencies session_deps; |
| 6445 | |
| 6446 | HttpRequestInfo request; |
| 6447 | request.method = "GET"; |
| 6448 | request.url = GURL("http://www.google.com/"); |
| 6449 | request.load_flags = 0; |
| 6450 | |
| 6451 | MockConnect mock_connect(true, ERR_CONNECTION_REFUSED); |
| 6452 | StaticSocketDataProvider first_data; |
| 6453 | first_data.set_connect_data(mock_connect); |
| 6454 | session_deps.socket_factory.AddSocketDataProvider(&first_data); |
| 6455 | |
| 6456 | MockRead data_reads[] = { |
| 6457 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 6458 | MockRead("hello world"), |
| 6459 | MockRead(true, OK), |
| 6460 | }; |
| 6461 | StaticSocketDataProvider second_data( |
| 6462 | data_reads, arraysize(data_reads), NULL, 0); |
| 6463 | session_deps.socket_factory.AddSocketDataProvider(&second_data); |
| 6464 | |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6465 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 6466 | |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6467 | HttpServerProperties* http_server_properties = |
| 6468 | session->http_server_properties(); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6469 | // Port must be < 1024, or the header will be ignored (since initial port was |
| 6470 | // port 80 (another restricted port). |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6471 | http_server_properties->SetAlternateProtocol( |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6472 | HostPortPair::FromURL(request.url), |
| 6473 | 666 /* port is ignored by MockConnect anyway */, |
[email protected] | 8d2f701 | 2012-02-16 00:08:04 | [diff] [blame] | 6474 | NPN_SPDY_21); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6475 | |
| 6476 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6477 | TestCompletionCallback callback; |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6478 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6479 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6480 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6481 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6482 | |
| 6483 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 6484 | ASSERT_TRUE(response != NULL); |
| 6485 | ASSERT_TRUE(response->headers != NULL); |
| 6486 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 6487 | |
| 6488 | std::string response_data; |
| 6489 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 6490 | EXPECT_EQ("hello world", response_data); |
| 6491 | |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6492 | ASSERT_TRUE(http_server_properties->HasAlternateProtocol( |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6493 | HostPortPair::FromURL(request.url))); |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6494 | const PortAlternateProtocolPair alternate = |
| 6495 | http_server_properties->GetAlternateProtocol( |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6496 | HostPortPair::FromURL(request.url)); |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6497 | EXPECT_EQ(ALTERNATE_PROTOCOL_BROKEN, alternate.protocol); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6498 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 6499 | } |
| 6500 | |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6501 | TEST_F(HttpNetworkTransactionTest, AlternateProtocolPortRestrictedBlocked) { |
| 6502 | // Ensure that we're not allowed to redirect traffic via an alternate |
| 6503 | // protocol to an unrestricted (port >= 1024) when the original traffic was |
| 6504 | // on a restricted port (port < 1024). Ensure that we can redirect in all |
| 6505 | // other cases. |
| 6506 | HttpStreamFactory::set_use_alternate_protocols(true); |
| 6507 | SessionDependencies session_deps; |
| 6508 | |
| 6509 | HttpRequestInfo restricted_port_request; |
| 6510 | restricted_port_request.method = "GET"; |
| 6511 | restricted_port_request.url = GURL("http://www.google.com:1023/"); |
| 6512 | restricted_port_request.load_flags = 0; |
| 6513 | |
| 6514 | MockConnect mock_connect(true, ERR_CONNECTION_REFUSED); |
| 6515 | StaticSocketDataProvider first_data; |
| 6516 | first_data.set_connect_data(mock_connect); |
| 6517 | session_deps.socket_factory.AddSocketDataProvider(&first_data); |
| 6518 | |
| 6519 | MockRead data_reads[] = { |
| 6520 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 6521 | MockRead("hello world"), |
| 6522 | MockRead(true, OK), |
| 6523 | }; |
| 6524 | StaticSocketDataProvider second_data( |
| 6525 | data_reads, arraysize(data_reads), NULL, 0); |
| 6526 | session_deps.socket_factory.AddSocketDataProvider(&second_data); |
| 6527 | |
| 6528 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 6529 | |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6530 | HttpServerProperties* http_server_properties = |
| 6531 | session->http_server_properties(); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6532 | const int kUnrestrictedAlternatePort = 1024; |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6533 | http_server_properties->SetAlternateProtocol( |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6534 | HostPortPair::FromURL(restricted_port_request.url), |
| 6535 | kUnrestrictedAlternatePort, |
[email protected] | 8d2f701 | 2012-02-16 00:08:04 | [diff] [blame] | 6536 | NPN_SPDY_21); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6537 | |
| 6538 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6539 | TestCompletionCallback callback; |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6540 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6541 | int rv = trans->Start( |
| 6542 | &restricted_port_request, callback.callback(), BoundNetLog()); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6543 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6544 | // Invalid change to unrestricted port should fail. |
| 6545 | EXPECT_EQ(ERR_CONNECTION_REFUSED, callback.WaitForResult()); |
| 6546 | |
| 6547 | HttpStreamFactory::set_use_alternate_protocols(false); |
| 6548 | } |
| 6549 | |
| 6550 | TEST_F(HttpNetworkTransactionTest, AlternateProtocolPortRestrictedAllowed) { |
| 6551 | // Ensure that we're not allowed to redirect traffic via an alternate |
| 6552 | // protocol to an unrestricted (port >= 1024) when the original traffic was |
| 6553 | // on a restricted port (port < 1024). Ensure that we can redirect in all |
| 6554 | // other cases. |
| 6555 | HttpStreamFactory::set_use_alternate_protocols(true); |
| 6556 | SessionDependencies session_deps; |
| 6557 | |
| 6558 | HttpRequestInfo restricted_port_request; |
| 6559 | restricted_port_request.method = "GET"; |
| 6560 | restricted_port_request.url = GURL("http://www.google.com:1023/"); |
| 6561 | restricted_port_request.load_flags = 0; |
| 6562 | |
| 6563 | MockConnect mock_connect(true, ERR_CONNECTION_REFUSED); |
| 6564 | StaticSocketDataProvider first_data; |
| 6565 | first_data.set_connect_data(mock_connect); |
| 6566 | session_deps.socket_factory.AddSocketDataProvider(&first_data); |
| 6567 | |
| 6568 | MockRead data_reads[] = { |
| 6569 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 6570 | MockRead("hello world"), |
| 6571 | MockRead(true, OK), |
| 6572 | }; |
| 6573 | StaticSocketDataProvider second_data( |
| 6574 | data_reads, arraysize(data_reads), NULL, 0); |
| 6575 | session_deps.socket_factory.AddSocketDataProvider(&second_data); |
| 6576 | |
| 6577 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 6578 | |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6579 | HttpServerProperties* http_server_properties = |
| 6580 | session->http_server_properties(); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6581 | const int kRestrictedAlternatePort = 80; |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6582 | http_server_properties->SetAlternateProtocol( |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6583 | HostPortPair::FromURL(restricted_port_request.url), |
| 6584 | kRestrictedAlternatePort, |
[email protected] | 8d2f701 | 2012-02-16 00:08:04 | [diff] [blame] | 6585 | NPN_SPDY_21); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6586 | |
| 6587 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6588 | TestCompletionCallback callback; |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6589 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6590 | int rv = trans->Start( |
| 6591 | &restricted_port_request, callback.callback(), BoundNetLog()); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6592 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6593 | // Valid change to restricted port should pass. |
| 6594 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6595 | |
| 6596 | HttpStreamFactory::set_use_alternate_protocols(false); |
| 6597 | } |
| 6598 | |
| 6599 | TEST_F(HttpNetworkTransactionTest, AlternateProtocolPortUnrestrictedAllowed1) { |
| 6600 | // Ensure that we're not allowed to redirect traffic via an alternate |
| 6601 | // protocol to an unrestricted (port >= 1024) when the original traffic was |
| 6602 | // on a restricted port (port < 1024). Ensure that we can redirect in all |
| 6603 | // other cases. |
| 6604 | HttpStreamFactory::set_use_alternate_protocols(true); |
| 6605 | SessionDependencies session_deps; |
| 6606 | |
| 6607 | HttpRequestInfo unrestricted_port_request; |
| 6608 | unrestricted_port_request.method = "GET"; |
| 6609 | unrestricted_port_request.url = GURL("http://www.google.com:1024/"); |
| 6610 | unrestricted_port_request.load_flags = 0; |
| 6611 | |
| 6612 | MockConnect mock_connect(true, ERR_CONNECTION_REFUSED); |
| 6613 | StaticSocketDataProvider first_data; |
| 6614 | first_data.set_connect_data(mock_connect); |
| 6615 | session_deps.socket_factory.AddSocketDataProvider(&first_data); |
| 6616 | |
| 6617 | MockRead data_reads[] = { |
| 6618 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 6619 | MockRead("hello world"), |
| 6620 | MockRead(true, OK), |
| 6621 | }; |
| 6622 | StaticSocketDataProvider second_data( |
| 6623 | data_reads, arraysize(data_reads), NULL, 0); |
| 6624 | session_deps.socket_factory.AddSocketDataProvider(&second_data); |
| 6625 | |
| 6626 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 6627 | |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6628 | HttpServerProperties* http_server_properties = |
| 6629 | session->http_server_properties(); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6630 | const int kRestrictedAlternatePort = 80; |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6631 | http_server_properties->SetAlternateProtocol( |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6632 | HostPortPair::FromURL(unrestricted_port_request.url), |
| 6633 | kRestrictedAlternatePort, |
[email protected] | 8d2f701 | 2012-02-16 00:08:04 | [diff] [blame] | 6634 | NPN_SPDY_21); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6635 | |
| 6636 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6637 | TestCompletionCallback callback; |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6638 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6639 | int rv = trans->Start( |
| 6640 | &unrestricted_port_request, callback.callback(), BoundNetLog()); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6641 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6642 | // Valid change to restricted port should pass. |
| 6643 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6644 | |
| 6645 | HttpStreamFactory::set_use_alternate_protocols(false); |
| 6646 | } |
| 6647 | |
| 6648 | TEST_F(HttpNetworkTransactionTest, AlternateProtocolPortUnrestrictedAllowed2) { |
| 6649 | // Ensure that we're not allowed to redirect traffic via an alternate |
| 6650 | // protocol to an unrestricted (port >= 1024) when the original traffic was |
| 6651 | // on a restricted port (port < 1024). Ensure that we can redirect in all |
| 6652 | // other cases. |
| 6653 | HttpStreamFactory::set_use_alternate_protocols(true); |
| 6654 | SessionDependencies session_deps; |
| 6655 | |
| 6656 | HttpRequestInfo unrestricted_port_request; |
| 6657 | unrestricted_port_request.method = "GET"; |
| 6658 | unrestricted_port_request.url = GURL("http://www.google.com:1024/"); |
| 6659 | unrestricted_port_request.load_flags = 0; |
| 6660 | |
| 6661 | MockConnect mock_connect(true, ERR_CONNECTION_REFUSED); |
| 6662 | StaticSocketDataProvider first_data; |
| 6663 | first_data.set_connect_data(mock_connect); |
| 6664 | session_deps.socket_factory.AddSocketDataProvider(&first_data); |
| 6665 | |
| 6666 | MockRead data_reads[] = { |
| 6667 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 6668 | MockRead("hello world"), |
| 6669 | MockRead(true, OK), |
| 6670 | }; |
| 6671 | StaticSocketDataProvider second_data( |
| 6672 | data_reads, arraysize(data_reads), NULL, 0); |
| 6673 | session_deps.socket_factory.AddSocketDataProvider(&second_data); |
| 6674 | |
| 6675 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 6676 | |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6677 | HttpServerProperties* http_server_properties = |
| 6678 | session->http_server_properties(); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6679 | const int kUnrestrictedAlternatePort = 1024; |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 6680 | http_server_properties->SetAlternateProtocol( |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6681 | HostPortPair::FromURL(unrestricted_port_request.url), |
| 6682 | kUnrestrictedAlternatePort, |
[email protected] | 8d2f701 | 2012-02-16 00:08:04 | [diff] [blame] | 6683 | NPN_SPDY_21); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6684 | |
| 6685 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6686 | TestCompletionCallback callback; |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6687 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6688 | int rv = trans->Start( |
| 6689 | &unrestricted_port_request, callback.callback(), BoundNetLog()); |
[email protected] | 3912662a3 | 2011-10-04 00:51:11 | [diff] [blame] | 6690 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6691 | // Valid change to an unrestricted port should pass. |
| 6692 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6693 | |
| 6694 | HttpStreamFactory::set_use_alternate_protocols(false); |
| 6695 | } |
| 6696 | |
[email protected] | eb6234e | 2012-01-19 01:50:02 | [diff] [blame] | 6697 | TEST_F(HttpNetworkTransactionTest, AlternateProtocolUnsafeBlocked) { |
| 6698 | // Ensure that we're not allowed to redirect traffic via an alternate |
| 6699 | // protocol to an unsafe port, and that we resume the second |
| 6700 | // HttpStreamFactoryImpl::Job once the alternate protocol request fails. |
| 6701 | HttpStreamFactory::set_use_alternate_protocols(true); |
| 6702 | SessionDependencies session_deps; |
| 6703 | |
| 6704 | HttpRequestInfo request; |
| 6705 | request.method = "GET"; |
| 6706 | request.url = GURL("http://www.google.com/"); |
| 6707 | request.load_flags = 0; |
| 6708 | |
| 6709 | // The alternate protocol request will error out before we attempt to connect, |
| 6710 | // so only the standard HTTP request will try to connect. |
| 6711 | MockRead data_reads[] = { |
| 6712 | MockRead("HTTP/1.1 200 OK\r\n\r\n"), |
| 6713 | MockRead("hello world"), |
| 6714 | MockRead(true, OK), |
| 6715 | }; |
| 6716 | StaticSocketDataProvider data( |
| 6717 | data_reads, arraysize(data_reads), NULL, 0); |
| 6718 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 6719 | |
| 6720 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 6721 | |
| 6722 | HttpServerProperties* http_server_properties = |
| 6723 | session->http_server_properties(); |
| 6724 | const int kUnsafePort = 7; |
| 6725 | http_server_properties->SetAlternateProtocol( |
| 6726 | HostPortPair::FromURL(request.url), |
| 6727 | kUnsafePort, |
| 6728 | NPN_SPDY_2); |
| 6729 | |
| 6730 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 6731 | TestCompletionCallback callback; |
| 6732 | |
| 6733 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
| 6734 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6735 | // The HTTP request should succeed. |
| 6736 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6737 | |
| 6738 | // Disable alternate protocol before the asserts. |
| 6739 | HttpStreamFactory::set_use_alternate_protocols(false); |
| 6740 | |
| 6741 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 6742 | ASSERT_TRUE(response != NULL); |
| 6743 | ASSERT_TRUE(response->headers != NULL); |
| 6744 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 6745 | |
| 6746 | std::string response_data; |
| 6747 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 6748 | EXPECT_EQ("hello world", response_data); |
| 6749 | } |
| 6750 | |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6751 | TEST_F(HttpNetworkTransactionTest, UseAlternateProtocolForNpnSpdy) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6752 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 6753 | HttpStreamFactory::set_next_protos(SpdyNextProtos()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6754 | SessionDependencies session_deps; |
| 6755 | |
| 6756 | HttpRequestInfo request; |
| 6757 | request.method = "GET"; |
| 6758 | request.url = GURL("http://www.google.com/"); |
| 6759 | request.load_flags = 0; |
| 6760 | |
| 6761 | MockRead data_reads[] = { |
| 6762 | MockRead("HTTP/1.1 200 OK\r\n"), |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 6763 | MockRead(kAlternateProtocolHttpHeader), |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6764 | MockRead("hello world"), |
| 6765 | MockRead(true, OK), |
| 6766 | }; |
| 6767 | |
| 6768 | StaticSocketDataProvider first_transaction( |
| 6769 | data_reads, arraysize(data_reads), NULL, 0); |
| 6770 | session_deps.socket_factory.AddSocketDataProvider(&first_transaction); |
| 6771 | |
| 6772 | SSLSocketDataProvider ssl(true, OK); |
| 6773 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 6774 | ssl.next_proto = "spdy/2.1"; |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 6775 | ssl.was_npn_negotiated = true; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 6776 | ssl.protocol_negotiated = SSLClientSocket::kProtoSPDY21; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6777 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 6778 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 6779 | scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); |
[email protected] | e7f7509 | 2010-07-01 22:39:13 | [diff] [blame] | 6780 | MockWrite spdy_writes[] = { CreateMockWrite(*req) }; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6781 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 6782 | scoped_ptr<spdy::SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 6783 | scoped_ptr<spdy::SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6784 | MockRead spdy_reads[] = { |
[email protected] | e7f7509 | 2010-07-01 22:39:13 | [diff] [blame] | 6785 | CreateMockRead(*resp), |
| 6786 | CreateMockRead(*data), |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6787 | MockRead(true, 0, 0), |
| 6788 | }; |
| 6789 | |
[email protected] | a159531 | 2012-01-22 03:25:04 | [diff] [blame] | 6790 | scoped_ptr<DelayedSocketData> spdy_data( |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6791 | new DelayedSocketData( |
| 6792 | 1, // wait for one write to finish before reading. |
| 6793 | spdy_reads, arraysize(spdy_reads), |
| 6794 | spdy_writes, arraysize(spdy_writes))); |
[email protected] | a159531 | 2012-01-22 03:25:04 | [diff] [blame] | 6795 | session_deps.socket_factory.AddSocketDataProvider(spdy_data.get()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6796 | |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 6797 | MockConnect never_finishing_connect(false, ERR_IO_PENDING); |
| 6798 | StaticSocketDataProvider hanging_non_alternate_protocol_socket( |
| 6799 | NULL, 0, NULL, 0); |
| 6800 | hanging_non_alternate_protocol_socket.set_connect_data( |
| 6801 | never_finishing_connect); |
| 6802 | session_deps.socket_factory.AddSocketDataProvider( |
| 6803 | &hanging_non_alternate_protocol_socket); |
| 6804 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6805 | TestCompletionCallback callback; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6806 | |
| 6807 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 6808 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 6809 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6810 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6811 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6812 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6813 | |
| 6814 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 6815 | ASSERT_TRUE(response != NULL); |
| 6816 | ASSERT_TRUE(response->headers != NULL); |
| 6817 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 6818 | |
| 6819 | std::string response_data; |
| 6820 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 6821 | EXPECT_EQ("hello world", response_data); |
| 6822 | |
| 6823 | trans.reset(new HttpNetworkTransaction(session)); |
| 6824 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6825 | rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6826 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6827 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 6828 | |
| 6829 | response = trans->GetResponseInfo(); |
| 6830 | ASSERT_TRUE(response != NULL); |
| 6831 | ASSERT_TRUE(response->headers != NULL); |
| 6832 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 6833 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 6834 | EXPECT_TRUE(response->was_npn_negotiated); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6835 | |
| 6836 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 6837 | EXPECT_EQ("hello!", response_data); |
| 6838 | |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 6839 | HttpStreamFactory::set_next_protos(std::vector<std::string>()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 6840 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 6841 | } |
| 6842 | |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 6843 | TEST_F(HttpNetworkTransactionTest, AlternateProtocolWithSpdyLateBinding) { |
| 6844 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 6845 | HttpStreamFactory::set_next_protos(SpdyNextProtos()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 6846 | SessionDependencies session_deps; |
| 6847 | |
| 6848 | HttpRequestInfo request; |
| 6849 | request.method = "GET"; |
| 6850 | request.url = GURL("http://www.google.com/"); |
| 6851 | request.load_flags = 0; |
| 6852 | |
| 6853 | MockRead data_reads[] = { |
| 6854 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 6855 | MockRead(kAlternateProtocolHttpHeader), |
| 6856 | MockRead("hello world"), |
| 6857 | MockRead(true, OK), |
| 6858 | }; |
| 6859 | |
| 6860 | StaticSocketDataProvider first_transaction( |
| 6861 | data_reads, arraysize(data_reads), NULL, 0); |
| 6862 | // Socket 1 is the HTTP transaction with the Alternate-Protocol header. |
| 6863 | session_deps.socket_factory.AddSocketDataProvider(&first_transaction); |
| 6864 | |
| 6865 | MockConnect never_finishing_connect(false, ERR_IO_PENDING); |
| 6866 | StaticSocketDataProvider hanging_socket( |
| 6867 | NULL, 0, NULL, 0); |
| 6868 | hanging_socket.set_connect_data(never_finishing_connect); |
| 6869 | // Socket 2 and 3 are the hanging Alternate-Protocol and |
| 6870 | // non-Alternate-Protocol jobs from the 2nd transaction. |
| 6871 | session_deps.socket_factory.AddSocketDataProvider(&hanging_socket); |
| 6872 | session_deps.socket_factory.AddSocketDataProvider(&hanging_socket); |
| 6873 | |
| 6874 | SSLSocketDataProvider ssl(true, OK); |
| 6875 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 6876 | ssl.next_proto = "spdy/2.1"; |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 6877 | ssl.was_npn_negotiated = true; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 6878 | ssl.protocol_negotiated = SSLClientSocket::kProtoSPDY21; |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 6879 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 6880 | |
| 6881 | scoped_ptr<spdy::SpdyFrame> req1(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); |
| 6882 | scoped_ptr<spdy::SpdyFrame> req2(ConstructSpdyGet(NULL, 0, false, 3, LOWEST)); |
| 6883 | MockWrite spdy_writes[] = { |
| 6884 | CreateMockWrite(*req1), |
| 6885 | CreateMockWrite(*req2), |
| 6886 | }; |
| 6887 | scoped_ptr<spdy::SpdyFrame> resp1(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 6888 | scoped_ptr<spdy::SpdyFrame> data1(ConstructSpdyBodyFrame(1, true)); |
| 6889 | scoped_ptr<spdy::SpdyFrame> resp2(ConstructSpdyGetSynReply(NULL, 0, 3)); |
| 6890 | scoped_ptr<spdy::SpdyFrame> data2(ConstructSpdyBodyFrame(3, true)); |
| 6891 | MockRead spdy_reads[] = { |
| 6892 | CreateMockRead(*resp1), |
| 6893 | CreateMockRead(*data1), |
| 6894 | CreateMockRead(*resp2), |
| 6895 | CreateMockRead(*data2), |
| 6896 | MockRead(true, 0, 0), |
| 6897 | }; |
| 6898 | |
[email protected] | a159531 | 2012-01-22 03:25:04 | [diff] [blame] | 6899 | scoped_ptr<DelayedSocketData> spdy_data( |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 6900 | new DelayedSocketData( |
| 6901 | 2, // wait for writes to finish before reading. |
| 6902 | spdy_reads, arraysize(spdy_reads), |
| 6903 | spdy_writes, arraysize(spdy_writes))); |
| 6904 | // Socket 4 is the successful Alternate-Protocol for transaction 3. |
[email protected] | a159531 | 2012-01-22 03:25:04 | [diff] [blame] | 6905 | session_deps.socket_factory.AddSocketDataProvider(spdy_data.get()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 6906 | |
| 6907 | // Socket 5 is the unsuccessful non-Alternate-Protocol for transaction 3. |
| 6908 | session_deps.socket_factory.AddSocketDataProvider(&hanging_socket); |
| 6909 | |
| 6910 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6911 | TestCompletionCallback callback1; |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 6912 | HttpNetworkTransaction trans1(session); |
| 6913 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6914 | int rv = trans1.Start(&request, callback1.callback(), BoundNetLog()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 6915 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6916 | EXPECT_EQ(OK, callback1.WaitForResult()); |
| 6917 | |
| 6918 | const HttpResponseInfo* response = trans1.GetResponseInfo(); |
| 6919 | ASSERT_TRUE(response != NULL); |
| 6920 | ASSERT_TRUE(response->headers != NULL); |
| 6921 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 6922 | |
| 6923 | std::string response_data; |
| 6924 | ASSERT_EQ(OK, ReadTransaction(&trans1, &response_data)); |
| 6925 | EXPECT_EQ("hello world", response_data); |
| 6926 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6927 | TestCompletionCallback callback2; |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 6928 | HttpNetworkTransaction trans2(session); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6929 | rv = trans2.Start(&request, callback2.callback(), BoundNetLog()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 6930 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6931 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6932 | TestCompletionCallback callback3; |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 6933 | HttpNetworkTransaction trans3(session); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 6934 | rv = trans3.Start(&request, callback3.callback(), BoundNetLog()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 6935 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 6936 | |
| 6937 | EXPECT_EQ(OK, callback2.WaitForResult()); |
| 6938 | EXPECT_EQ(OK, callback3.WaitForResult()); |
| 6939 | |
| 6940 | response = trans2.GetResponseInfo(); |
| 6941 | ASSERT_TRUE(response != NULL); |
| 6942 | ASSERT_TRUE(response->headers != NULL); |
| 6943 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 6944 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 6945 | EXPECT_TRUE(response->was_npn_negotiated); |
| 6946 | ASSERT_EQ(OK, ReadTransaction(&trans2, &response_data)); |
| 6947 | EXPECT_EQ("hello!", response_data); |
| 6948 | |
| 6949 | response = trans3.GetResponseInfo(); |
| 6950 | ASSERT_TRUE(response != NULL); |
| 6951 | ASSERT_TRUE(response->headers != NULL); |
| 6952 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 6953 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 6954 | EXPECT_TRUE(response->was_npn_negotiated); |
| 6955 | ASSERT_EQ(OK, ReadTransaction(&trans3, &response_data)); |
| 6956 | EXPECT_EQ("hello!", response_data); |
| 6957 | |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 6958 | HttpStreamFactory::set_next_protos(std::vector<std::string>()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 6959 | HttpStreamFactory::set_use_alternate_protocols(false); |
| 6960 | } |
| 6961 | |
| 6962 | TEST_F(HttpNetworkTransactionTest, StallAlternateProtocolForNpnSpdy) { |
| 6963 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 6964 | HttpStreamFactory::set_next_protos(SpdyNextProtos()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 6965 | SessionDependencies session_deps; |
| 6966 | |
| 6967 | HttpRequestInfo request; |
| 6968 | request.method = "GET"; |
| 6969 | request.url = GURL("http://www.google.com/"); |
| 6970 | request.load_flags = 0; |
| 6971 | |
| 6972 | MockRead data_reads[] = { |
| 6973 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 6974 | MockRead(kAlternateProtocolHttpHeader), |
| 6975 | MockRead("hello world"), |
| 6976 | MockRead(true, OK), |
| 6977 | }; |
| 6978 | |
| 6979 | StaticSocketDataProvider first_transaction( |
| 6980 | data_reads, arraysize(data_reads), NULL, 0); |
| 6981 | session_deps.socket_factory.AddSocketDataProvider(&first_transaction); |
| 6982 | |
| 6983 | SSLSocketDataProvider ssl(true, OK); |
| 6984 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 6985 | ssl.next_proto = "spdy/2.1"; |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 6986 | ssl.was_npn_negotiated = true; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 6987 | ssl.protocol_negotiated = SSLClientSocket::kProtoSPDY21; |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 6988 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 6989 | |
| 6990 | MockConnect never_finishing_connect(false, ERR_IO_PENDING); |
| 6991 | StaticSocketDataProvider hanging_alternate_protocol_socket( |
| 6992 | NULL, 0, NULL, 0); |
| 6993 | hanging_alternate_protocol_socket.set_connect_data( |
| 6994 | never_finishing_connect); |
| 6995 | session_deps.socket_factory.AddSocketDataProvider( |
| 6996 | &hanging_alternate_protocol_socket); |
| 6997 | |
| 6998 | // 2nd request is just a copy of the first one, over HTTP again. |
| 6999 | session_deps.socket_factory.AddSocketDataProvider(&first_transaction); |
| 7000 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7001 | TestCompletionCallback callback; |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 7002 | |
| 7003 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 7004 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 7005 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7006 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 7007 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7008 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 7009 | |
| 7010 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 7011 | ASSERT_TRUE(response != NULL); |
| 7012 | ASSERT_TRUE(response->headers != NULL); |
| 7013 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 7014 | |
| 7015 | std::string response_data; |
| 7016 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 7017 | EXPECT_EQ("hello world", response_data); |
| 7018 | |
| 7019 | trans.reset(new HttpNetworkTransaction(session)); |
| 7020 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7021 | rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 7022 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7023 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 7024 | |
| 7025 | response = trans->GetResponseInfo(); |
| 7026 | ASSERT_TRUE(response != NULL); |
| 7027 | ASSERT_TRUE(response->headers != NULL); |
| 7028 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 7029 | EXPECT_FALSE(response->was_fetched_via_spdy); |
| 7030 | EXPECT_FALSE(response->was_npn_negotiated); |
| 7031 | |
| 7032 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 7033 | EXPECT_EQ("hello world", response_data); |
| 7034 | |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 7035 | HttpStreamFactory::set_next_protos(std::vector<std::string>()); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 7036 | HttpStreamFactory::set_use_alternate_protocols(false); |
| 7037 | } |
| 7038 | |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7039 | class CapturingProxyResolver : public ProxyResolver { |
| 7040 | public: |
| 7041 | CapturingProxyResolver() : ProxyResolver(false /* expects_pac_bytes */) {} |
| 7042 | virtual ~CapturingProxyResolver() {} |
| 7043 | |
| 7044 | virtual int GetProxyForURL(const GURL& url, |
| 7045 | ProxyInfo* results, |
[email protected] | 23578681 | 2011-12-20 02:15:31 | [diff] [blame] | 7046 | const CompletionCallback& callback, |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7047 | RequestHandle* request, |
| 7048 | const BoundNetLog& net_log) { |
[email protected] | fae7669f | 2010-08-02 21:49:40 | [diff] [blame] | 7049 | ProxyServer proxy_server(ProxyServer::SCHEME_HTTP, |
| 7050 | HostPortPair("myproxy", 80)); |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 7051 | results->UseProxyServer(proxy_server); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7052 | resolved_.push_back(url); |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 7053 | return OK; |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7054 | } |
| 7055 | |
| 7056 | virtual void CancelRequest(RequestHandle request) { |
| 7057 | NOTREACHED(); |
| 7058 | } |
| 7059 | |
[email protected] | f2c971f | 2011-11-08 00:33:17 | [diff] [blame] | 7060 | virtual LoadState GetLoadState(RequestHandle request) const OVERRIDE { |
| 7061 | NOTREACHED(); |
| 7062 | return LOAD_STATE_IDLE; |
| 7063 | } |
| 7064 | |
| 7065 | virtual LoadState GetLoadStateThreadSafe( |
| 7066 | RequestHandle request) const OVERRIDE { |
| 7067 | NOTREACHED(); |
| 7068 | return LOAD_STATE_IDLE; |
| 7069 | } |
| 7070 | |
[email protected] | 1e60547 | 2010-12-16 21:41:40 | [diff] [blame] | 7071 | virtual void CancelSetPacScript() { |
| 7072 | NOTREACHED(); |
| 7073 | } |
| 7074 | |
[email protected] | 2447640 | 2010-07-20 20:55:17 | [diff] [blame] | 7075 | virtual int SetPacScript(const scoped_refptr<ProxyResolverScriptData>&, |
[email protected] | 23578681 | 2011-12-20 02:15:31 | [diff] [blame] | 7076 | const CompletionCallback& /*callback*/) { |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 7077 | return OK; |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7078 | } |
| 7079 | |
[email protected] | 2447640 | 2010-07-20 20:55:17 | [diff] [blame] | 7080 | const std::vector<GURL>& resolved() const { return resolved_; } |
| 7081 | |
| 7082 | private: |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7083 | std::vector<GURL> resolved_; |
| 7084 | |
| 7085 | DISALLOW_COPY_AND_ASSIGN(CapturingProxyResolver); |
| 7086 | }; |
| 7087 | |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7088 | TEST_F(HttpNetworkTransactionTest, UseAlternateProtocolForTunneledNpnSpdy) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 7089 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 7090 | HttpStreamFactory::set_next_protos(SpdyNextProtos()); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7091 | |
| 7092 | ProxyConfig proxy_config; |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 7093 | proxy_config.set_auto_detect(true); |
| 7094 | proxy_config.set_pac_url(GURL("http://fooproxyurl")); |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 7095 | |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7096 | CapturingProxyResolver* capturing_proxy_resolver = |
| 7097 | new CapturingProxyResolver(); |
[email protected] | 66761b95 | 2010-06-25 21:30:38 | [diff] [blame] | 7098 | SessionDependencies session_deps(new ProxyService( |
| 7099 | new ProxyConfigServiceFixed(proxy_config), capturing_proxy_resolver, |
| 7100 | NULL)); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7101 | |
| 7102 | HttpRequestInfo request; |
| 7103 | request.method = "GET"; |
| 7104 | request.url = GURL("http://www.google.com/"); |
| 7105 | request.load_flags = 0; |
| 7106 | |
| 7107 | MockRead data_reads[] = { |
| 7108 | MockRead("HTTP/1.1 200 OK\r\n"), |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 7109 | MockRead(kAlternateProtocolHttpHeader), |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7110 | MockRead("hello world"), |
| 7111 | MockRead(true, OK), |
| 7112 | }; |
| 7113 | |
| 7114 | StaticSocketDataProvider first_transaction( |
| 7115 | data_reads, arraysize(data_reads), NULL, 0); |
| 7116 | session_deps.socket_factory.AddSocketDataProvider(&first_transaction); |
| 7117 | |
| 7118 | SSLSocketDataProvider ssl(true, OK); |
| 7119 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 7120 | ssl.next_proto = "spdy/2.1"; |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 7121 | ssl.was_npn_negotiated = true; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 7122 | ssl.protocol_negotiated = SSLClientSocket::kProtoSPDY21; |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7123 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 7124 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 7125 | scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7126 | MockWrite spdy_writes[] = { |
| 7127 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 7128 | "Host: www.google.com\r\n" |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 7129 | "Proxy-Connection: keep-alive\r\n\r\n"), // 0 |
[email protected] | e7f7509 | 2010-07-01 22:39:13 | [diff] [blame] | 7130 | CreateMockWrite(*req) // 3 |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7131 | }; |
| 7132 | |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 7133 | const char kCONNECTResponse[] = "HTTP/1.1 200 Connected\r\n\r\n"; |
| 7134 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 7135 | scoped_ptr<spdy::SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 7136 | scoped_ptr<spdy::SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7137 | MockRead spdy_reads[] = { |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 7138 | MockRead(true, kCONNECTResponse, arraysize(kCONNECTResponse) - 1, 1), // 1 |
[email protected] | e7f7509 | 2010-07-01 22:39:13 | [diff] [blame] | 7139 | CreateMockRead(*resp.get(), 4), // 2, 4 |
| 7140 | CreateMockRead(*data.get(), 4), // 5 |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 7141 | MockRead(true, 0, 0, 4), // 6 |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7142 | }; |
| 7143 | |
[email protected] | a159531 | 2012-01-22 03:25:04 | [diff] [blame] | 7144 | scoped_ptr<OrderedSocketData> spdy_data( |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 7145 | new OrderedSocketData( |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7146 | spdy_reads, arraysize(spdy_reads), |
| 7147 | spdy_writes, arraysize(spdy_writes))); |
[email protected] | a159531 | 2012-01-22 03:25:04 | [diff] [blame] | 7148 | session_deps.socket_factory.AddSocketDataProvider(spdy_data.get()); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7149 | |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 7150 | MockConnect never_finishing_connect(false, ERR_IO_PENDING); |
| 7151 | StaticSocketDataProvider hanging_non_alternate_protocol_socket( |
| 7152 | NULL, 0, NULL, 0); |
| 7153 | hanging_non_alternate_protocol_socket.set_connect_data( |
| 7154 | never_finishing_connect); |
| 7155 | session_deps.socket_factory.AddSocketDataProvider( |
| 7156 | &hanging_non_alternate_protocol_socket); |
| 7157 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7158 | TestCompletionCallback callback; |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7159 | |
| 7160 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 7161 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 7162 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7163 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7164 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7165 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 7166 | |
| 7167 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 7168 | ASSERT_TRUE(response != NULL); |
| 7169 | ASSERT_TRUE(response->headers != NULL); |
| 7170 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 7171 | EXPECT_FALSE(response->was_fetched_via_spdy); |
| 7172 | EXPECT_FALSE(response->was_npn_negotiated); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7173 | |
| 7174 | std::string response_data; |
| 7175 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 7176 | EXPECT_EQ("hello world", response_data); |
| 7177 | |
| 7178 | trans.reset(new HttpNetworkTransaction(session)); |
| 7179 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7180 | rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7181 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7182 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 7183 | |
| 7184 | response = trans->GetResponseInfo(); |
| 7185 | ASSERT_TRUE(response != NULL); |
| 7186 | ASSERT_TRUE(response->headers != NULL); |
| 7187 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 7188 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 7189 | EXPECT_TRUE(response->was_npn_negotiated); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7190 | |
| 7191 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 7192 | EXPECT_EQ("hello!", response_data); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 7193 | ASSERT_EQ(3u, capturing_proxy_resolver->resolved().size()); |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 7194 | EXPECT_EQ("http://www.google.com/", |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7195 | capturing_proxy_resolver->resolved()[0].spec()); |
[email protected] | d911f1b | 2010-05-05 22:39:42 | [diff] [blame] | 7196 | EXPECT_EQ("https://www.google.com/", |
| 7197 | capturing_proxy_resolver->resolved()[1].spec()); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7198 | |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 7199 | HttpStreamFactory::set_next_protos(std::vector<std::string>()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 7200 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7201 | } |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 7202 | |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 7203 | TEST_F(HttpNetworkTransactionTest, |
| 7204 | UseAlternateProtocolForNpnSpdyWithExistingSpdySession) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 7205 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 7206 | HttpStreamFactory::set_next_protos(SpdyNextProtos()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 7207 | SessionDependencies session_deps; |
| 7208 | |
| 7209 | HttpRequestInfo request; |
| 7210 | request.method = "GET"; |
| 7211 | request.url = GURL("http://www.google.com/"); |
| 7212 | request.load_flags = 0; |
| 7213 | |
| 7214 | MockRead data_reads[] = { |
| 7215 | MockRead("HTTP/1.1 200 OK\r\n"), |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 7216 | MockRead(kAlternateProtocolHttpHeader), |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 7217 | MockRead("hello world"), |
| 7218 | MockRead(true, OK), |
| 7219 | }; |
| 7220 | |
| 7221 | StaticSocketDataProvider first_transaction( |
| 7222 | data_reads, arraysize(data_reads), NULL, 0); |
| 7223 | session_deps.socket_factory.AddSocketDataProvider(&first_transaction); |
| 7224 | |
| 7225 | SSLSocketDataProvider ssl(true, OK); |
| 7226 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 7227 | ssl.next_proto = "spdy/2.1"; |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 7228 | ssl.was_npn_negotiated = true; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 7229 | ssl.protocol_negotiated = SSLClientSocket::kProtoSPDY21; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 7230 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 7231 | // Make sure we use ssl for spdy here. |
| 7232 | SpdySession::SetSSLMode(true); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 7233 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 7234 | scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); |
[email protected] | e7f7509 | 2010-07-01 22:39:13 | [diff] [blame] | 7235 | MockWrite spdy_writes[] = { CreateMockWrite(*req) }; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 7236 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 7237 | scoped_ptr<spdy::SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 7238 | scoped_ptr<spdy::SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 7239 | MockRead spdy_reads[] = { |
[email protected] | e7f7509 | 2010-07-01 22:39:13 | [diff] [blame] | 7240 | CreateMockRead(*resp), |
| 7241 | CreateMockRead(*data), |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 7242 | MockRead(true, 0, 0), |
| 7243 | }; |
| 7244 | |
[email protected] | a159531 | 2012-01-22 03:25:04 | [diff] [blame] | 7245 | scoped_ptr<DelayedSocketData> spdy_data( |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 7246 | new DelayedSocketData( |
| 7247 | 1, // wait for one write to finish before reading. |
| 7248 | spdy_reads, arraysize(spdy_reads), |
| 7249 | spdy_writes, arraysize(spdy_writes))); |
[email protected] | a159531 | 2012-01-22 03:25:04 | [diff] [blame] | 7250 | session_deps.socket_factory.AddSocketDataProvider(spdy_data.get()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 7251 | |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 7252 | TestCompletionCallback callback; |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 7253 | |
| 7254 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 7255 | |
| 7256 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 7257 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7258 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 7259 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7260 | EXPECT_EQ(OK, callback.WaitForResult()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 7261 | |
| 7262 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 7263 | ASSERT_TRUE(response != NULL); |
| 7264 | ASSERT_TRUE(response->headers != NULL); |
| 7265 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 7266 | |
| 7267 | std::string response_data; |
| 7268 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 7269 | EXPECT_EQ("hello world", response_data); |
| 7270 | |
| 7271 | // Set up an initial SpdySession in the pool to reuse. |
[email protected] | 02b0c34 | 2010-09-25 21:09:38 | [diff] [blame] | 7272 | HostPortPair host_port_pair("www.google.com", 443); |
| 7273 | HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 7274 | scoped_refptr<SpdySession> spdy_session = |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 7275 | session->spdy_session_pool()->Get(pair, BoundNetLog()); |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 7276 | scoped_refptr<TransportSocketParams> transport_params( |
[email protected] | acdda41 | 2011-11-15 21:21:29 | [diff] [blame] | 7277 | new TransportSocketParams(host_port_pair, MEDIUM, false, false)); |
[email protected] | 02b0c34 | 2010-09-25 21:09:38 | [diff] [blame] | 7278 | |
| 7279 | scoped_ptr<ClientSocketHandle> connection(new ClientSocketHandle); |
| 7280 | EXPECT_EQ(ERR_IO_PENDING, |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 7281 | connection->Init(host_port_pair.ToString(), |
| 7282 | transport_params, |
| 7283 | LOWEST, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 7284 | callback.callback(), |
[email protected] | a42dbd14 | 2011-11-17 16:42:02 | [diff] [blame] | 7285 | session->GetTransportSocketPool(), |
[email protected] | 02b0c34 | 2010-09-25 21:09:38 | [diff] [blame] | 7286 | BoundNetLog())); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 7287 | EXPECT_EQ(OK, callback.WaitForResult()); |
[email protected] | 02b0c34 | 2010-09-25 21:09:38 | [diff] [blame] | 7288 | |
| 7289 | SSLConfig ssl_config; |
| 7290 | session->ssl_config_service()->GetSSLConfig(&ssl_config); |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 7291 | scoped_ptr<ClientSocketHandle> ssl_connection(new ClientSocketHandle); |
[email protected] | feb79bcd | 2011-07-21 16:55:17 | [diff] [blame] | 7292 | SSLClientSocketContext context; |
| 7293 | context.cert_verifier = session_deps.cert_verifier.get(); |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 7294 | ssl_connection->set_socket(session_deps.socket_factory.CreateSSLClientSocket( |
| 7295 | connection.release(), HostPortPair("" , 443), ssl_config, |
[email protected] | feb79bcd | 2011-07-21 16:55:17 | [diff] [blame] | 7296 | NULL /* ssl_host_info */, context)); |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 7297 | EXPECT_EQ(ERR_IO_PENDING, |
| 7298 | ssl_connection->socket()->Connect(callback.callback())); |
[email protected] | 02b0c34 | 2010-09-25 21:09:38 | [diff] [blame] | 7299 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 7300 | |
[email protected] | 9e1bdd3 | 2011-02-03 21:48:34 | [diff] [blame] | 7301 | EXPECT_EQ(OK, spdy_session->InitializeWithSocket(ssl_connection.release(), |
[email protected] | 02b0c34 | 2010-09-25 21:09:38 | [diff] [blame] | 7302 | true, OK)); |
| 7303 | |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 7304 | trans.reset(new HttpNetworkTransaction(session)); |
| 7305 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7306 | rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 7307 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7308 | EXPECT_EQ(OK, callback.WaitForResult()); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 7309 | |
| 7310 | response = trans->GetResponseInfo(); |
| 7311 | ASSERT_TRUE(response != NULL); |
| 7312 | ASSERT_TRUE(response->headers != NULL); |
| 7313 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 7314 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 7315 | EXPECT_TRUE(response->was_npn_negotiated); |
[email protected] | 2ff8b31 | 2010-04-26 22:20:54 | [diff] [blame] | 7316 | |
| 7317 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 7318 | EXPECT_EQ("hello!", response_data); |
| 7319 | |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 7320 | HttpStreamFactory::set_next_protos(std::vector<std::string>()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 7321 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 7322 | } |
| 7323 | |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7324 | // GenerateAuthToken is a mighty big test. |
| 7325 | // It tests all permutation of GenerateAuthToken behavior: |
| 7326 | // - Synchronous and Asynchronous completion. |
| 7327 | // - OK or error on completion. |
| 7328 | // - Direct connection, non-authenticating proxy, and authenticating proxy. |
| 7329 | // - HTTP or HTTPS backend (to include proxy tunneling). |
| 7330 | // - Non-authenticating and authenticating backend. |
| 7331 | // |
[email protected] | fe3b7dc | 2012-02-03 19:52:09 | [diff] [blame] | 7332 | // In all, there are 44 reasonable permuations (for example, if there are |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7333 | // problems generating an auth token for an authenticating proxy, we don't |
| 7334 | // need to test all permutations of the backend server). |
| 7335 | // |
| 7336 | // The test proceeds by going over each of the configuration cases, and |
| 7337 | // potentially running up to three rounds in each of the tests. The TestConfig |
| 7338 | // specifies both the configuration for the test as well as the expectations |
| 7339 | // for the results. |
| 7340 | TEST_F(HttpNetworkTransactionTest, GenerateAuthToken) { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 7341 | static const char kServer[] = "http://www.example.com"; |
| 7342 | static const char kSecureServer[] = "https://www.example.com"; |
| 7343 | static const char kProxy[] = "myproxy:70"; |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7344 | const int kAuthErr = ERR_INVALID_AUTH_CREDENTIALS; |
| 7345 | |
| 7346 | enum AuthTiming { |
| 7347 | AUTH_NONE, |
| 7348 | AUTH_SYNC, |
| 7349 | AUTH_ASYNC, |
| 7350 | }; |
| 7351 | |
| 7352 | const MockWrite kGet( |
| 7353 | "GET / HTTP/1.1\r\n" |
| 7354 | "Host: www.example.com\r\n" |
| 7355 | "Connection: keep-alive\r\n\r\n"); |
| 7356 | const MockWrite kGetProxy( |
| 7357 | "GET http://www.example.com/ HTTP/1.1\r\n" |
| 7358 | "Host: www.example.com\r\n" |
| 7359 | "Proxy-Connection: keep-alive\r\n\r\n"); |
| 7360 | const MockWrite kGetAuth( |
| 7361 | "GET / HTTP/1.1\r\n" |
| 7362 | "Host: www.example.com\r\n" |
| 7363 | "Connection: keep-alive\r\n" |
| 7364 | "Authorization: auth_token\r\n\r\n"); |
| 7365 | const MockWrite kGetProxyAuth( |
| 7366 | "GET http://www.example.com/ HTTP/1.1\r\n" |
| 7367 | "Host: www.example.com\r\n" |
| 7368 | "Proxy-Connection: keep-alive\r\n" |
| 7369 | "Proxy-Authorization: auth_token\r\n\r\n"); |
| 7370 | const MockWrite kGetAuthThroughProxy( |
| 7371 | "GET http://www.example.com/ HTTP/1.1\r\n" |
| 7372 | "Host: www.example.com\r\n" |
| 7373 | "Proxy-Connection: keep-alive\r\n" |
| 7374 | "Authorization: auth_token\r\n\r\n"); |
| 7375 | const MockWrite kGetAuthWithProxyAuth( |
| 7376 | "GET http://www.example.com/ HTTP/1.1\r\n" |
| 7377 | "Host: www.example.com\r\n" |
| 7378 | "Proxy-Connection: keep-alive\r\n" |
| 7379 | "Proxy-Authorization: auth_token\r\n" |
| 7380 | "Authorization: auth_token\r\n\r\n"); |
| 7381 | const MockWrite kConnect( |
| 7382 | "CONNECT www.example.com:443 HTTP/1.1\r\n" |
| 7383 | "Host: www.example.com\r\n" |
| 7384 | "Proxy-Connection: keep-alive\r\n\r\n"); |
| 7385 | const MockWrite kConnectProxyAuth( |
| 7386 | "CONNECT www.example.com:443 HTTP/1.1\r\n" |
| 7387 | "Host: www.example.com\r\n" |
| 7388 | "Proxy-Connection: keep-alive\r\n" |
| 7389 | "Proxy-Authorization: auth_token\r\n\r\n"); |
| 7390 | |
| 7391 | const MockRead kSuccess( |
| 7392 | "HTTP/1.1 200 OK\r\n" |
| 7393 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 7394 | "Content-Length: 3\r\n\r\n" |
| 7395 | "Yes"); |
| 7396 | const MockRead kFailure( |
| 7397 | "Should not be called."); |
| 7398 | const MockRead kServerChallenge( |
| 7399 | "HTTP/1.1 401 Unauthorized\r\n" |
| 7400 | "WWW-Authenticate: Mock realm=server\r\n" |
| 7401 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 7402 | "Content-Length: 14\r\n\r\n" |
| 7403 | "Unauthorized\r\n"); |
| 7404 | const MockRead kProxyChallenge( |
| 7405 | "HTTP/1.1 407 Unauthorized\r\n" |
| 7406 | "Proxy-Authenticate: Mock realm=proxy\r\n" |
| 7407 | "Proxy-Connection: close\r\n" |
| 7408 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 7409 | "Content-Length: 14\r\n\r\n" |
| 7410 | "Unauthorized\r\n"); |
| 7411 | const MockRead kProxyConnected( |
| 7412 | "HTTP/1.1 200 Connection Established\r\n\r\n"); |
| 7413 | |
| 7414 | // NOTE(cbentzel): I wanted TestReadWriteRound to be a simple struct with |
| 7415 | // no constructors, but the C++ compiler on Windows warns about |
| 7416 | // unspecified data in compound literals. So, moved to using constructors, |
| 7417 | // and TestRound's created with the default constructor should not be used. |
| 7418 | struct TestRound { |
| 7419 | TestRound() |
| 7420 | : expected_rv(ERR_UNEXPECTED), |
| 7421 | extra_write(NULL), |
| 7422 | extra_read(NULL) { |
| 7423 | } |
| 7424 | TestRound(const MockWrite& write_arg, const MockRead& read_arg, |
| 7425 | int expected_rv_arg) |
| 7426 | : write(write_arg), |
| 7427 | read(read_arg), |
| 7428 | expected_rv(expected_rv_arg), |
| 7429 | extra_write(NULL), |
| 7430 | extra_read(NULL) { |
| 7431 | } |
| 7432 | TestRound(const MockWrite& write_arg, const MockRead& read_arg, |
| 7433 | int expected_rv_arg, const MockWrite* extra_write_arg, |
| 7434 | const MockWrite* extra_read_arg) |
| 7435 | : write(write_arg), |
| 7436 | read(read_arg), |
| 7437 | expected_rv(expected_rv_arg), |
| 7438 | extra_write(extra_write_arg), |
| 7439 | extra_read(extra_read_arg) { |
| 7440 | } |
| 7441 | MockWrite write; |
| 7442 | MockRead read; |
| 7443 | int expected_rv; |
| 7444 | const MockWrite* extra_write; |
| 7445 | const MockRead* extra_read; |
| 7446 | }; |
| 7447 | |
| 7448 | static const int kNoSSL = 500; |
| 7449 | |
| 7450 | struct TestConfig { |
| 7451 | const char* proxy_url; |
| 7452 | AuthTiming proxy_auth_timing; |
| 7453 | int proxy_auth_rv; |
| 7454 | const char* server_url; |
| 7455 | AuthTiming server_auth_timing; |
| 7456 | int server_auth_rv; |
| 7457 | int num_auth_rounds; |
| 7458 | int first_ssl_round; |
| 7459 | TestRound rounds[3]; |
| 7460 | } test_configs[] = { |
| 7461 | // Non-authenticating HTTP server with a direct connection. |
| 7462 | { NULL, AUTH_NONE, OK, kServer, AUTH_NONE, OK, 1, kNoSSL, |
| 7463 | { TestRound(kGet, kSuccess, OK)}}, |
| 7464 | // Authenticating HTTP server with a direct connection. |
| 7465 | { NULL, AUTH_NONE, OK, kServer, AUTH_SYNC, OK, 2, kNoSSL, |
| 7466 | { TestRound(kGet, kServerChallenge, OK), |
| 7467 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 7468 | { NULL, AUTH_NONE, OK, kServer, AUTH_SYNC, kAuthErr, 2, kNoSSL, |
| 7469 | { TestRound(kGet, kServerChallenge, OK), |
| 7470 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 7471 | { NULL, AUTH_NONE, OK, kServer, AUTH_ASYNC, OK, 2, kNoSSL, |
| 7472 | { TestRound(kGet, kServerChallenge, OK), |
| 7473 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 7474 | { NULL, AUTH_NONE, OK, kServer, AUTH_ASYNC, kAuthErr, 2, kNoSSL, |
| 7475 | { TestRound(kGet, kServerChallenge, OK), |
| 7476 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 7477 | // Non-authenticating HTTP server through a non-authenticating proxy. |
| 7478 | { kProxy, AUTH_NONE, OK, kServer, AUTH_NONE, OK, 1, kNoSSL, |
| 7479 | { TestRound(kGetProxy, kSuccess, OK)}}, |
| 7480 | // Authenticating HTTP server through a non-authenticating proxy. |
| 7481 | { kProxy, AUTH_NONE, OK, kServer, AUTH_SYNC, OK, 2, kNoSSL, |
| 7482 | { TestRound(kGetProxy, kServerChallenge, OK), |
| 7483 | TestRound(kGetAuthThroughProxy, kSuccess, OK)}}, |
| 7484 | { kProxy, AUTH_NONE, OK, kServer, AUTH_SYNC, kAuthErr, 2, kNoSSL, |
| 7485 | { TestRound(kGetProxy, kServerChallenge, OK), |
| 7486 | TestRound(kGetAuthThroughProxy, kFailure, kAuthErr)}}, |
| 7487 | { kProxy, AUTH_NONE, OK, kServer, AUTH_ASYNC, OK, 2, kNoSSL, |
| 7488 | { TestRound(kGetProxy, kServerChallenge, OK), |
| 7489 | TestRound(kGetAuthThroughProxy, kSuccess, OK)}}, |
| 7490 | { kProxy, AUTH_NONE, OK, kServer, AUTH_ASYNC, kAuthErr, 2, kNoSSL, |
| 7491 | { TestRound(kGetProxy, kServerChallenge, OK), |
| 7492 | TestRound(kGetAuthThroughProxy, kFailure, kAuthErr)}}, |
| 7493 | // Non-authenticating HTTP server through an authenticating proxy. |
| 7494 | { kProxy, AUTH_SYNC, OK, kServer, AUTH_NONE, OK, 2, kNoSSL, |
| 7495 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7496 | TestRound(kGetProxyAuth, kSuccess, OK)}}, |
| 7497 | { kProxy, AUTH_SYNC, kAuthErr, kServer, AUTH_NONE, OK, 2, kNoSSL, |
| 7498 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7499 | TestRound(kGetProxyAuth, kFailure, kAuthErr)}}, |
| 7500 | { kProxy, AUTH_ASYNC, OK, kServer, AUTH_NONE, OK, 2, kNoSSL, |
| 7501 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7502 | TestRound(kGetProxyAuth, kSuccess, OK)}}, |
| 7503 | { kProxy, AUTH_ASYNC, kAuthErr, kServer, AUTH_NONE, OK, 2, kNoSSL, |
| 7504 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7505 | TestRound(kGetProxyAuth, kFailure, kAuthErr)}}, |
| 7506 | // Authenticating HTTP server through an authenticating proxy. |
| 7507 | { kProxy, AUTH_SYNC, OK, kServer, AUTH_SYNC, OK, 3, kNoSSL, |
| 7508 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7509 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 7510 | TestRound(kGetAuthWithProxyAuth, kSuccess, OK)}}, |
| 7511 | { kProxy, AUTH_SYNC, OK, kServer, AUTH_SYNC, kAuthErr, 3, kNoSSL, |
| 7512 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7513 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 7514 | TestRound(kGetAuthWithProxyAuth, kFailure, kAuthErr)}}, |
| 7515 | { kProxy, AUTH_ASYNC, OK, kServer, AUTH_SYNC, OK, 3, kNoSSL, |
| 7516 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7517 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 7518 | TestRound(kGetAuthWithProxyAuth, kSuccess, OK)}}, |
| 7519 | { kProxy, AUTH_ASYNC, OK, kServer, AUTH_SYNC, kAuthErr, 3, kNoSSL, |
| 7520 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7521 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 7522 | TestRound(kGetAuthWithProxyAuth, kFailure, kAuthErr)}}, |
| 7523 | { kProxy, AUTH_SYNC, OK, kServer, AUTH_ASYNC, OK, 3, kNoSSL, |
| 7524 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7525 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 7526 | TestRound(kGetAuthWithProxyAuth, kSuccess, OK)}}, |
| 7527 | { kProxy, AUTH_SYNC, OK, kServer, AUTH_ASYNC, kAuthErr, 3, kNoSSL, |
| 7528 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7529 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 7530 | TestRound(kGetAuthWithProxyAuth, kFailure, kAuthErr)}}, |
| 7531 | { kProxy, AUTH_ASYNC, OK, kServer, AUTH_ASYNC, OK, 3, kNoSSL, |
| 7532 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7533 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 7534 | TestRound(kGetAuthWithProxyAuth, kSuccess, OK)}}, |
| 7535 | { kProxy, AUTH_ASYNC, OK, kServer, AUTH_ASYNC, kAuthErr, 3, kNoSSL, |
| 7536 | { TestRound(kGetProxy, kProxyChallenge, OK), |
| 7537 | TestRound(kGetProxyAuth, kServerChallenge, OK), |
| 7538 | TestRound(kGetAuthWithProxyAuth, kFailure, kAuthErr)}}, |
| 7539 | // Non-authenticating HTTPS server with a direct connection. |
| 7540 | { NULL, AUTH_NONE, OK, kSecureServer, AUTH_NONE, OK, 1, 0, |
| 7541 | { TestRound(kGet, kSuccess, OK)}}, |
| 7542 | // Authenticating HTTPS server with a direct connection. |
| 7543 | { NULL, AUTH_NONE, OK, kSecureServer, AUTH_SYNC, OK, 2, 0, |
| 7544 | { TestRound(kGet, kServerChallenge, OK), |
| 7545 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 7546 | { NULL, AUTH_NONE, OK, kSecureServer, AUTH_SYNC, kAuthErr, 2, 0, |
| 7547 | { TestRound(kGet, kServerChallenge, OK), |
| 7548 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 7549 | { NULL, AUTH_NONE, OK, kSecureServer, AUTH_ASYNC, OK, 2, 0, |
| 7550 | { TestRound(kGet, kServerChallenge, OK), |
| 7551 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 7552 | { NULL, AUTH_NONE, OK, kSecureServer, AUTH_ASYNC, kAuthErr, 2, 0, |
| 7553 | { TestRound(kGet, kServerChallenge, OK), |
| 7554 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 7555 | // Non-authenticating HTTPS server with a non-authenticating proxy. |
| 7556 | { kProxy, AUTH_NONE, OK, kSecureServer, AUTH_NONE, OK, 1, 0, |
| 7557 | { TestRound(kConnect, kProxyConnected, OK, &kGet, &kSuccess)}}, |
| 7558 | // Authenticating HTTPS server through a non-authenticating proxy. |
| 7559 | { kProxy, AUTH_NONE, OK, kSecureServer, AUTH_SYNC, OK, 2, 0, |
| 7560 | { TestRound(kConnect, kProxyConnected, OK, &kGet, &kServerChallenge), |
| 7561 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 7562 | { kProxy, AUTH_NONE, OK, kSecureServer, AUTH_SYNC, kAuthErr, 2, 0, |
| 7563 | { TestRound(kConnect, kProxyConnected, OK, &kGet, &kServerChallenge), |
| 7564 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 7565 | { kProxy, AUTH_NONE, OK, kSecureServer, AUTH_ASYNC, OK, 2, 0, |
| 7566 | { TestRound(kConnect, kProxyConnected, OK, &kGet, &kServerChallenge), |
| 7567 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 7568 | { kProxy, AUTH_NONE, OK, kSecureServer, AUTH_ASYNC, kAuthErr, 2, 0, |
| 7569 | { TestRound(kConnect, kProxyConnected, OK, &kGet, &kServerChallenge), |
| 7570 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 7571 | // Non-Authenticating HTTPS server through an authenticating proxy. |
| 7572 | { kProxy, AUTH_SYNC, OK, kSecureServer, AUTH_NONE, OK, 2, 1, |
| 7573 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7574 | TestRound(kConnectProxyAuth, kProxyConnected, OK, &kGet, &kSuccess)}}, |
| 7575 | { kProxy, AUTH_SYNC, kAuthErr, kSecureServer, AUTH_NONE, OK, 2, kNoSSL, |
| 7576 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7577 | TestRound(kConnectProxyAuth, kFailure, kAuthErr)}}, |
| 7578 | { kProxy, AUTH_ASYNC, OK, kSecureServer, AUTH_NONE, OK, 2, 1, |
| 7579 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7580 | TestRound(kConnectProxyAuth, kProxyConnected, OK, &kGet, &kSuccess)}}, |
| 7581 | { kProxy, AUTH_ASYNC, kAuthErr, kSecureServer, AUTH_NONE, OK, 2, kNoSSL, |
| 7582 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7583 | TestRound(kConnectProxyAuth, kFailure, kAuthErr)}}, |
| 7584 | // Authenticating HTTPS server through an authenticating proxy. |
| 7585 | { kProxy, AUTH_SYNC, OK, kSecureServer, AUTH_SYNC, OK, 3, 1, |
| 7586 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7587 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 7588 | &kGet, &kServerChallenge), |
| 7589 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 7590 | { kProxy, AUTH_SYNC, OK, kSecureServer, AUTH_SYNC, kAuthErr, 3, 1, |
| 7591 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7592 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 7593 | &kGet, &kServerChallenge), |
| 7594 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 7595 | { kProxy, AUTH_ASYNC, OK, kSecureServer, AUTH_SYNC, OK, 3, 1, |
| 7596 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7597 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 7598 | &kGet, &kServerChallenge), |
| 7599 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 7600 | { kProxy, AUTH_ASYNC, OK, kSecureServer, AUTH_SYNC, kAuthErr, 3, 1, |
| 7601 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7602 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 7603 | &kGet, &kServerChallenge), |
| 7604 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 7605 | { kProxy, AUTH_SYNC, OK, kSecureServer, AUTH_ASYNC, OK, 3, 1, |
| 7606 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7607 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 7608 | &kGet, &kServerChallenge), |
| 7609 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 7610 | { kProxy, AUTH_SYNC, OK, kSecureServer, AUTH_ASYNC, kAuthErr, 3, 1, |
| 7611 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7612 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 7613 | &kGet, &kServerChallenge), |
| 7614 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 7615 | { kProxy, AUTH_ASYNC, OK, kSecureServer, AUTH_ASYNC, OK, 3, 1, |
| 7616 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7617 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 7618 | &kGet, &kServerChallenge), |
| 7619 | TestRound(kGetAuth, kSuccess, OK)}}, |
| 7620 | { kProxy, AUTH_ASYNC, OK, kSecureServer, AUTH_ASYNC, kAuthErr, 3, 1, |
| 7621 | { TestRound(kConnect, kProxyChallenge, OK), |
| 7622 | TestRound(kConnectProxyAuth, kProxyConnected, OK, |
| 7623 | &kGet, &kServerChallenge), |
| 7624 | TestRound(kGetAuth, kFailure, kAuthErr)}}, |
| 7625 | }; |
| 7626 | |
| 7627 | SessionDependencies session_deps; |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7628 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_configs); ++i) { |
[email protected] | 2d01c26 | 2011-08-11 23:07:08 | [diff] [blame] | 7629 | HttpAuthHandlerMock::Factory* auth_factory( |
| 7630 | new HttpAuthHandlerMock::Factory()); |
| 7631 | session_deps.http_auth_handler_factory.reset(auth_factory); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7632 | const TestConfig& test_config = test_configs[i]; |
[email protected] | 65d3438 | 2010-07-01 18:12:26 | [diff] [blame] | 7633 | |
| 7634 | // Set up authentication handlers as necessary. |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7635 | if (test_config.proxy_auth_timing != AUTH_NONE) { |
[email protected] | 2d01c26 | 2011-08-11 23:07:08 | [diff] [blame] | 7636 | for (int n = 0; n < 2; n++) { |
| 7637 | HttpAuthHandlerMock* auth_handler(new HttpAuthHandlerMock()); |
| 7638 | std::string auth_challenge = "Mock realm=proxy"; |
| 7639 | GURL origin(test_config.proxy_url); |
| 7640 | HttpAuth::ChallengeTokenizer tokenizer(auth_challenge.begin(), |
| 7641 | auth_challenge.end()); |
| 7642 | auth_handler->InitFromChallenge(&tokenizer, HttpAuth::AUTH_PROXY, |
| 7643 | origin, BoundNetLog()); |
| 7644 | auth_handler->SetGenerateExpectation( |
| 7645 | test_config.proxy_auth_timing == AUTH_ASYNC, |
| 7646 | test_config.proxy_auth_rv); |
| 7647 | auth_factory->AddMockHandler(auth_handler, HttpAuth::AUTH_PROXY); |
| 7648 | } |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7649 | } |
| 7650 | if (test_config.server_auth_timing != AUTH_NONE) { |
[email protected] | 3fd9dae | 2010-06-21 11:39:00 | [diff] [blame] | 7651 | HttpAuthHandlerMock* auth_handler(new HttpAuthHandlerMock()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7652 | std::string auth_challenge = "Mock realm=server"; |
| 7653 | GURL origin(test_config.server_url); |
| 7654 | HttpAuth::ChallengeTokenizer tokenizer(auth_challenge.begin(), |
| 7655 | auth_challenge.end()); |
| 7656 | auth_handler->InitFromChallenge(&tokenizer, HttpAuth::AUTH_SERVER, |
| 7657 | origin, BoundNetLog()); |
| 7658 | auth_handler->SetGenerateExpectation( |
| 7659 | test_config.server_auth_timing == AUTH_ASYNC, |
| 7660 | test_config.server_auth_rv); |
[email protected] | 2d01c26 | 2011-08-11 23:07:08 | [diff] [blame] | 7661 | auth_factory->AddMockHandler(auth_handler, HttpAuth::AUTH_SERVER); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7662 | } |
| 7663 | if (test_config.proxy_url) { |
[email protected] | 6104ea5d | 2011-04-27 21:37:12 | [diff] [blame] | 7664 | session_deps.proxy_service.reset( |
| 7665 | ProxyService::CreateFixed(test_config.proxy_url)); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7666 | } else { |
[email protected] | 6104ea5d | 2011-04-27 21:37:12 | [diff] [blame] | 7667 | session_deps.proxy_service.reset(ProxyService::CreateDirect()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7668 | } |
| 7669 | |
| 7670 | HttpRequestInfo request; |
| 7671 | request.method = "GET"; |
| 7672 | request.url = GURL(test_config.server_url); |
| 7673 | request.load_flags = 0; |
| 7674 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 7675 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 7676 | HttpNetworkTransaction trans(CreateSession(&session_deps)); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7677 | |
| 7678 | for (int round = 0; round < test_config.num_auth_rounds; ++round) { |
| 7679 | const TestRound& read_write_round = test_config.rounds[round]; |
| 7680 | |
| 7681 | // Set up expected reads and writes. |
| 7682 | MockRead reads[2]; |
| 7683 | reads[0] = read_write_round.read; |
| 7684 | size_t length_reads = 1; |
| 7685 | if (read_write_round.extra_read) { |
| 7686 | reads[1] = *read_write_round.extra_read; |
| 7687 | length_reads = 2; |
| 7688 | } |
| 7689 | |
| 7690 | MockWrite writes[2]; |
| 7691 | writes[0] = read_write_round.write; |
| 7692 | size_t length_writes = 1; |
| 7693 | if (read_write_round.extra_write) { |
| 7694 | writes[1] = *read_write_round.extra_write; |
| 7695 | length_writes = 2; |
| 7696 | } |
| 7697 | StaticSocketDataProvider data_provider( |
| 7698 | reads, length_reads, writes, length_writes); |
| 7699 | session_deps.socket_factory.AddSocketDataProvider(&data_provider); |
| 7700 | |
| 7701 | // Add an SSL sequence if necessary. |
| 7702 | SSLSocketDataProvider ssl_socket_data_provider(false, OK); |
| 7703 | if (round >= test_config.first_ssl_round) |
| 7704 | session_deps.socket_factory.AddSSLSocketDataProvider( |
| 7705 | &ssl_socket_data_provider); |
| 7706 | |
| 7707 | // Start or restart the transaction. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7708 | TestCompletionCallback callback; |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7709 | int rv; |
| 7710 | if (round == 0) { |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7711 | rv = trans.Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7712 | } else { |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7713 | rv = trans.RestartWithAuth( |
| 7714 | AuthCredentials(kFoo, kBar), callback.callback()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7715 | } |
| 7716 | if (rv == ERR_IO_PENDING) |
| 7717 | rv = callback.WaitForResult(); |
| 7718 | |
| 7719 | // Compare results with expected data. |
| 7720 | EXPECT_EQ(read_write_round.expected_rv, rv); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 7721 | const HttpResponseInfo* response = trans.GetResponseInfo(); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7722 | if (read_write_round.expected_rv == OK) { |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 7723 | ASSERT_TRUE(response != NULL); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 7724 | } else { |
| 7725 | EXPECT_TRUE(response == NULL); |
| 7726 | EXPECT_EQ(round + 1, test_config.num_auth_rounds); |
| 7727 | continue; |
| 7728 | } |
| 7729 | if (round + 1 < test_config.num_auth_rounds) { |
| 7730 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 7731 | } else { |
| 7732 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 7733 | } |
| 7734 | } |
[email protected] | e5ae96a | 2010-04-14 20:12:45 | [diff] [blame] | 7735 | } |
| 7736 | } |
| 7737 | |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7738 | TEST_F(HttpNetworkTransactionTest, MultiRoundAuth) { |
| 7739 | // Do multi-round authentication and make sure it works correctly. |
| 7740 | SessionDependencies session_deps; |
| 7741 | HttpAuthHandlerMock::Factory* auth_factory( |
| 7742 | new HttpAuthHandlerMock::Factory()); |
| 7743 | session_deps.http_auth_handler_factory.reset(auth_factory); |
[email protected] | 6104ea5d | 2011-04-27 21:37:12 | [diff] [blame] | 7744 | session_deps.proxy_service.reset(ProxyService::CreateDirect()); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7745 | session_deps.host_resolver->rules()->AddRule("www.example.com", "10.0.0.1"); |
| 7746 | session_deps.host_resolver->set_synchronous_mode(true); |
| 7747 | |
| 7748 | HttpAuthHandlerMock* auth_handler(new HttpAuthHandlerMock()); |
| 7749 | auth_handler->set_connection_based(true); |
| 7750 | std::string auth_challenge = "Mock realm=server"; |
| 7751 | GURL origin("http://www.example.com"); |
| 7752 | HttpAuth::ChallengeTokenizer tokenizer(auth_challenge.begin(), |
| 7753 | auth_challenge.end()); |
| 7754 | auth_handler->InitFromChallenge(&tokenizer, HttpAuth::AUTH_SERVER, |
| 7755 | origin, BoundNetLog()); |
[email protected] | 2d01c26 | 2011-08-11 23:07:08 | [diff] [blame] | 7756 | auth_factory->AddMockHandler(auth_handler, HttpAuth::AUTH_SERVER); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7757 | |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7758 | int rv = OK; |
| 7759 | const HttpResponseInfo* response = NULL; |
| 7760 | HttpRequestInfo request; |
| 7761 | request.method = "GET"; |
| 7762 | request.url = origin; |
| 7763 | request.load_flags = 0; |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 7764 | |
| 7765 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 7766 | |
| 7767 | // Use a TCP Socket Pool with only one connection per group. This is used |
| 7768 | // to validate that the TCP socket is not released to the pool between |
| 7769 | // each round of multi-round authentication. |
| 7770 | HttpNetworkSessionPeer session_peer(session); |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 7771 | ClientSocketPoolHistograms transport_pool_histograms("SmallTCP"); |
| 7772 | TransportClientSocketPool* transport_pool = new TransportClientSocketPool( |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 7773 | 50, // Max sockets for pool |
| 7774 | 1, // Max sockets per group |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 7775 | &transport_pool_histograms, |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 7776 | session_deps.host_resolver.get(), |
| 7777 | &session_deps.socket_factory, |
| 7778 | session_deps.net_log); |
[email protected] | a42dbd14 | 2011-11-17 16:42:02 | [diff] [blame] | 7779 | MockClientSocketPoolManager* mock_pool_manager = |
| 7780 | new MockClientSocketPoolManager; |
| 7781 | mock_pool_manager->SetTransportSocketPool(transport_pool); |
| 7782 | session_peer.SetClientSocketPoolManager(mock_pool_manager); |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 7783 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 7784 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7785 | TestCompletionCallback callback; |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7786 | |
| 7787 | const MockWrite kGet( |
| 7788 | "GET / HTTP/1.1\r\n" |
| 7789 | "Host: www.example.com\r\n" |
| 7790 | "Connection: keep-alive\r\n\r\n"); |
| 7791 | const MockWrite kGetAuth( |
| 7792 | "GET / HTTP/1.1\r\n" |
| 7793 | "Host: www.example.com\r\n" |
| 7794 | "Connection: keep-alive\r\n" |
| 7795 | "Authorization: auth_token\r\n\r\n"); |
| 7796 | |
| 7797 | const MockRead kServerChallenge( |
| 7798 | "HTTP/1.1 401 Unauthorized\r\n" |
| 7799 | "WWW-Authenticate: Mock realm=server\r\n" |
| 7800 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 7801 | "Content-Length: 14\r\n\r\n" |
| 7802 | "Unauthorized\r\n"); |
| 7803 | const MockRead kSuccess( |
| 7804 | "HTTP/1.1 200 OK\r\n" |
| 7805 | "Content-Type: text/html; charset=iso-8859-1\r\n" |
| 7806 | "Content-Length: 3\r\n\r\n" |
| 7807 | "Yes"); |
| 7808 | |
| 7809 | MockWrite writes[] = { |
| 7810 | // First round |
| 7811 | kGet, |
| 7812 | // Second round |
| 7813 | kGetAuth, |
| 7814 | // Third round |
| 7815 | kGetAuth, |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 7816 | // Fourth round |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 7817 | kGetAuth, |
| 7818 | // Competing request |
| 7819 | kGet, |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7820 | }; |
| 7821 | MockRead reads[] = { |
| 7822 | // First round |
| 7823 | kServerChallenge, |
| 7824 | // Second round |
| 7825 | kServerChallenge, |
| 7826 | // Third round |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 7827 | kServerChallenge, |
| 7828 | // Fourth round |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7829 | kSuccess, |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 7830 | // Competing response |
| 7831 | kSuccess, |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7832 | }; |
| 7833 | StaticSocketDataProvider data_provider(reads, arraysize(reads), |
| 7834 | writes, arraysize(writes)); |
| 7835 | session_deps.socket_factory.AddSocketDataProvider(&data_provider); |
| 7836 | |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 7837 | const char* const kSocketGroup = "www.example.com:80"; |
| 7838 | |
| 7839 | // First round of authentication. |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7840 | auth_handler->SetGenerateExpectation(false, OK); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7841 | rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7842 | if (rv == ERR_IO_PENDING) |
| 7843 | rv = callback.WaitForResult(); |
| 7844 | EXPECT_EQ(OK, rv); |
| 7845 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 7846 | ASSERT_TRUE(response != NULL); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7847 | EXPECT_FALSE(response->auth_challenge.get() == NULL); |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 7848 | EXPECT_EQ(0, transport_pool->IdleSocketCountInGroup(kSocketGroup)); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7849 | |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 7850 | // In between rounds, another request comes in for the same domain. |
| 7851 | // It should not be able to grab the TCP socket that trans has already |
| 7852 | // claimed. |
| 7853 | scoped_ptr<HttpTransaction> trans_compete( |
| 7854 | new HttpNetworkTransaction(session)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7855 | TestCompletionCallback callback_compete; |
| 7856 | rv = trans_compete->Start( |
| 7857 | &request, callback_compete.callback(), BoundNetLog()); |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 7858 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7859 | // callback_compete.WaitForResult at this point would stall forever, |
| 7860 | // since the HttpNetworkTransaction does not release the request back to |
| 7861 | // the pool until after authentication completes. |
| 7862 | |
| 7863 | // Second round of authentication. |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7864 | auth_handler->SetGenerateExpectation(false, OK); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7865 | rv = trans->RestartWithAuth(AuthCredentials(kFoo, kBar), callback.callback()); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7866 | if (rv == ERR_IO_PENDING) |
| 7867 | rv = callback.WaitForResult(); |
| 7868 | EXPECT_EQ(OK, rv); |
| 7869 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 7870 | ASSERT_TRUE(response != NULL); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7871 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 7872 | EXPECT_EQ(0, transport_pool->IdleSocketCountInGroup(kSocketGroup)); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7873 | |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 7874 | // Third round of authentication. |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7875 | auth_handler->SetGenerateExpectation(false, OK); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7876 | rv = trans->RestartWithAuth(AuthCredentials(), callback.callback()); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7877 | if (rv == ERR_IO_PENDING) |
| 7878 | rv = callback.WaitForResult(); |
| 7879 | EXPECT_EQ(OK, rv); |
| 7880 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 7881 | ASSERT_TRUE(response != NULL); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7882 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 7883 | EXPECT_EQ(0, transport_pool->IdleSocketCountInGroup(kSocketGroup)); |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 7884 | |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 7885 | // Fourth round of authentication, which completes successfully. |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 7886 | auth_handler->SetGenerateExpectation(false, OK); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7887 | rv = trans->RestartWithAuth(AuthCredentials(), callback.callback()); |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 7888 | if (rv == ERR_IO_PENDING) |
| 7889 | rv = callback.WaitForResult(); |
| 7890 | EXPECT_EQ(OK, rv); |
| 7891 | response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 7892 | ASSERT_TRUE(response != NULL); |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 7893 | EXPECT_TRUE(response->auth_challenge.get() == NULL); |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 7894 | EXPECT_EQ(0, transport_pool->IdleSocketCountInGroup(kSocketGroup)); |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 7895 | |
| 7896 | // Read the body since the fourth round was successful. This will also |
| 7897 | // release the socket back to the pool. |
| 7898 | scoped_refptr<IOBufferWithSize> io_buf(new IOBufferWithSize(50)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7899 | rv = trans->Read(io_buf, io_buf->size(), callback.callback()); |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 7900 | if (rv == ERR_IO_PENDING) |
| 7901 | rv = callback.WaitForResult(); |
| 7902 | EXPECT_EQ(3, rv); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7903 | rv = trans->Read(io_buf, io_buf->size(), callback.callback()); |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 7904 | EXPECT_EQ(0, rv); |
| 7905 | // There are still 0 idle sockets, since the trans_compete transaction |
| 7906 | // will be handed it immediately after trans releases it to the group. |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 7907 | EXPECT_EQ(0, transport_pool->IdleSocketCountInGroup(kSocketGroup)); |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 7908 | |
| 7909 | // The competing request can now finish. Wait for the headers and then |
| 7910 | // read the body. |
| 7911 | rv = callback_compete.WaitForResult(); |
| 7912 | EXPECT_EQ(OK, rv); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7913 | rv = trans_compete->Read(io_buf, io_buf->size(), callback.callback()); |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 7914 | if (rv == ERR_IO_PENDING) |
| 7915 | rv = callback.WaitForResult(); |
| 7916 | EXPECT_EQ(3, rv); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7917 | rv = trans_compete->Read(io_buf, io_buf->size(), callback.callback()); |
[email protected] | 7ef4cbbb | 2011-02-06 11:19:10 | [diff] [blame] | 7918 | EXPECT_EQ(0, rv); |
| 7919 | |
| 7920 | // Finally, the socket is released to the group. |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 7921 | EXPECT_EQ(1, transport_pool->IdleSocketCountInGroup(kSocketGroup)); |
[email protected] | c871bce9 | 2010-07-15 21:51:14 | [diff] [blame] | 7922 | } |
| 7923 | |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 7924 | class TLSDecompressionFailureSocketDataProvider : public SocketDataProvider { |
| 7925 | public: |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 7926 | explicit TLSDecompressionFailureSocketDataProvider(bool fail_all) |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 7927 | : fail_all_(fail_all) { |
| 7928 | } |
| 7929 | |
| 7930 | virtual MockRead GetNextRead() { |
| 7931 | if (fail_all_) |
| 7932 | return MockRead(false /* async */, ERR_SSL_DECOMPRESSION_FAILURE_ALERT); |
| 7933 | |
| 7934 | return MockRead(false /* async */, |
| 7935 | "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nok.\r\n"); |
| 7936 | } |
| 7937 | |
| 7938 | virtual MockWriteResult OnWrite(const std::string& data) { |
| 7939 | return MockWriteResult(false /* async */, data.size()); |
| 7940 | } |
| 7941 | |
| 7942 | void Reset() { |
| 7943 | } |
| 7944 | |
| 7945 | private: |
| 7946 | const bool fail_all_; |
| 7947 | }; |
| 7948 | |
| 7949 | // Test that we restart a connection when we see a decompression failure from |
| 7950 | // the peer during the handshake. (In the real world we'll restart with SSLv3 |
| 7951 | // and we won't offer DEFLATE in that case.) |
| 7952 | TEST_F(HttpNetworkTransactionTest, RestartAfterTLSDecompressionFailure) { |
| 7953 | HttpRequestInfo request; |
| 7954 | request.method = "GET"; |
| 7955 | request.url = GURL("https://tlsdecompressionfailure.example.com/"); |
| 7956 | request.load_flags = 0; |
| 7957 | |
| 7958 | SessionDependencies session_deps; |
| 7959 | TLSDecompressionFailureSocketDataProvider socket_data_provider1( |
| 7960 | false /* fail all reads */); |
| 7961 | TLSDecompressionFailureSocketDataProvider socket_data_provider2(false); |
| 7962 | SSLSocketDataProvider ssl_socket_data_provider1( |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 7963 | false, ERR_SSL_DECOMPRESSION_FAILURE_ALERT); |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 7964 | SSLSocketDataProvider ssl_socket_data_provider2(false, OK); |
| 7965 | session_deps.socket_factory.AddSocketDataProvider(&socket_data_provider1); |
| 7966 | session_deps.socket_factory.AddSocketDataProvider(&socket_data_provider2); |
| 7967 | session_deps.socket_factory.AddSSLSocketDataProvider( |
| 7968 | &ssl_socket_data_provider1); |
| 7969 | session_deps.socket_factory.AddSSLSocketDataProvider( |
| 7970 | &ssl_socket_data_provider2); |
| 7971 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 7972 | // Work around http://crbug.com/37454 |
| 7973 | StaticSocketDataProvider bug37454_connection; |
| 7974 | bug37454_connection.set_connect_data(MockConnect(true, ERR_UNEXPECTED)); |
| 7975 | session_deps.socket_factory.AddSocketDataProvider(&bug37454_connection); |
| 7976 | |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 7977 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 7978 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7979 | TestCompletionCallback callback; |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 7980 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 7981 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 7982 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 7983 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 7984 | |
| 7985 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 7986 | ASSERT_TRUE(response != NULL); |
| 7987 | ASSERT_TRUE(response->headers != NULL); |
| 7988 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 7989 | |
| 7990 | std::string response_data; |
| 7991 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 7992 | EXPECT_EQ("ok.", response_data); |
| 7993 | } |
| 7994 | |
| 7995 | // Test that we restart a connection if we get a decompression failure from the |
| 7996 | // peer while reading the first bytes from the connection. This occurs when the |
| 7997 | // peer cannot handle DEFLATE but we're using False Start, so we don't notice |
| 7998 | // in the handshake. |
| 7999 | TEST_F(HttpNetworkTransactionTest, |
| 8000 | RestartAfterTLSDecompressionFailureWithFalseStart) { |
| 8001 | HttpRequestInfo request; |
| 8002 | request.method = "GET"; |
| 8003 | request.url = GURL("https://tlsdecompressionfailure2.example.com/"); |
| 8004 | request.load_flags = 0; |
| 8005 | |
| 8006 | SessionDependencies session_deps; |
| 8007 | TLSDecompressionFailureSocketDataProvider socket_data_provider1( |
| 8008 | true /* fail all reads */); |
| 8009 | TLSDecompressionFailureSocketDataProvider socket_data_provider2(false); |
| 8010 | SSLSocketDataProvider ssl_socket_data_provider1(false, OK); |
| 8011 | SSLSocketDataProvider ssl_socket_data_provider2(false, OK); |
| 8012 | session_deps.socket_factory.AddSocketDataProvider(&socket_data_provider1); |
| 8013 | session_deps.socket_factory.AddSocketDataProvider(&socket_data_provider2); |
| 8014 | session_deps.socket_factory.AddSSLSocketDataProvider( |
| 8015 | &ssl_socket_data_provider1); |
| 8016 | session_deps.socket_factory.AddSSLSocketDataProvider( |
| 8017 | &ssl_socket_data_provider2); |
| 8018 | |
| 8019 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 8020 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8021 | TestCompletionCallback callback; |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 8022 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8023 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | aeaca1f | 2010-04-20 22:05:21 | [diff] [blame] | 8024 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 8025 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 8026 | |
| 8027 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 8028 | ASSERT_TRUE(response != NULL); |
| 8029 | ASSERT_TRUE(response->headers != NULL); |
| 8030 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 8031 | |
| 8032 | std::string response_data; |
| 8033 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 8034 | EXPECT_EQ("ok.", response_data); |
| 8035 | } |
| 8036 | |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 8037 | // This tests the case that a request is issued via http instead of spdy after |
| 8038 | // npn is negotiated. |
| 8039 | TEST_F(HttpNetworkTransactionTest, NpnWithHttpOverSSL) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 8040 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 8041 | HttpStreamFactory::set_next_protos( |
| 8042 | MakeNextProtos("http/1.1", "http1.1", NULL)); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 8043 | SessionDependencies session_deps; |
| 8044 | HttpRequestInfo request; |
| 8045 | request.method = "GET"; |
| 8046 | request.url = GURL("https://www.google.com/"); |
| 8047 | request.load_flags = 0; |
| 8048 | |
| 8049 | MockWrite data_writes[] = { |
| 8050 | MockWrite("GET / HTTP/1.1\r\n" |
| 8051 | "Host: www.google.com\r\n" |
| 8052 | "Connection: keep-alive\r\n\r\n"), |
| 8053 | }; |
| 8054 | |
| 8055 | MockRead data_reads[] = { |
| 8056 | MockRead("HTTP/1.1 200 OK\r\n"), |
[email protected] | dae22c5 | 2010-07-30 02:16:35 | [diff] [blame] | 8057 | MockRead(kAlternateProtocolHttpHeader), |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 8058 | MockRead("hello world"), |
| 8059 | MockRead(false, OK), |
| 8060 | }; |
| 8061 | |
| 8062 | SSLSocketDataProvider ssl(true, OK); |
| 8063 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
| 8064 | ssl.next_proto = "http/1.1"; |
[email protected] | c30bcce | 2011-12-20 17:50:51 | [diff] [blame] | 8065 | ssl.protocol_negotiated = SSLClientSocket::kProtoHTTP11; |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 8066 | |
| 8067 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 8068 | |
| 8069 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), |
| 8070 | data_writes, arraysize(data_writes)); |
| 8071 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 8072 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8073 | TestCompletionCallback callback; |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 8074 | |
| 8075 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 8076 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 8077 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8078 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 8079 | |
| 8080 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 8081 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 8082 | |
| 8083 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 8084 | ASSERT_TRUE(response != NULL); |
| 8085 | ASSERT_TRUE(response->headers != NULL); |
| 8086 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 8087 | |
| 8088 | std::string response_data; |
| 8089 | ASSERT_EQ(OK, ReadTransaction(trans.get(), &response_data)); |
| 8090 | EXPECT_EQ("hello world", response_data); |
| 8091 | |
| 8092 | EXPECT_FALSE(response->was_fetched_via_spdy); |
| 8093 | EXPECT_TRUE(response->was_npn_negotiated); |
| 8094 | |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 8095 | HttpStreamFactory::set_next_protos(std::vector<std::string>()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 8096 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 65041fa | 2010-05-21 06:56:53 | [diff] [blame] | 8097 | } |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 8098 | |
| 8099 | TEST_F(HttpNetworkTransactionTest, SpdyPostNPNServerHangup) { |
| 8100 | // Simulate the SSL handshake completing with an NPN negotiation |
| 8101 | // followed by an immediate server closing of the socket. |
| 8102 | // Fix crash: http://crbug.com/46369 |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 8103 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 8104 | HttpStreamFactory::set_next_protos(SpdyNextProtos()); |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 8105 | SessionDependencies session_deps; |
| 8106 | |
| 8107 | HttpRequestInfo request; |
| 8108 | request.method = "GET"; |
| 8109 | request.url = GURL("https://www.google.com/"); |
| 8110 | request.load_flags = 0; |
| 8111 | |
| 8112 | SSLSocketDataProvider ssl(true, OK); |
| 8113 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 8114 | ssl.next_proto = "spdy/2.1"; |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 8115 | ssl.was_npn_negotiated = true; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 8116 | ssl.protocol_negotiated = SSLClientSocket::kProtoSPDY21; |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 8117 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 8118 | |
[email protected] | 2bd9302 | 2010-07-17 00:58:44 | [diff] [blame] | 8119 | scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); |
[email protected] | e7f7509 | 2010-07-01 22:39:13 | [diff] [blame] | 8120 | MockWrite spdy_writes[] = { CreateMockWrite(*req) }; |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 8121 | |
| 8122 | MockRead spdy_reads[] = { |
| 8123 | MockRead(false, 0, 0) // Not async - return 0 immediately. |
| 8124 | }; |
| 8125 | |
[email protected] | a159531 | 2012-01-22 03:25:04 | [diff] [blame] | 8126 | scoped_ptr<DelayedSocketData> spdy_data( |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 8127 | new DelayedSocketData( |
| 8128 | 0, // don't wait in this case, immediate hangup. |
| 8129 | spdy_reads, arraysize(spdy_reads), |
| 8130 | spdy_writes, arraysize(spdy_writes))); |
[email protected] | a159531 | 2012-01-22 03:25:04 | [diff] [blame] | 8131 | session_deps.socket_factory.AddSocketDataProvider(spdy_data.get()); |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 8132 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8133 | TestCompletionCallback callback; |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 8134 | |
| 8135 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 8136 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 8137 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8138 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 8139 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 8140 | EXPECT_EQ(ERR_CONNECTION_CLOSED, callback.WaitForResult()); |
| 8141 | |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 8142 | HttpStreamFactory::set_next_protos(std::vector<std::string>()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 8143 | HttpStreamFactory::set_use_alternate_protocols(false); |
[email protected] | 26ef658 | 2010-06-24 02:30:47 | [diff] [blame] | 8144 | } |
[email protected] | 65d3438 | 2010-07-01 18:12:26 | [diff] [blame] | 8145 | |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8146 | TEST_F(HttpNetworkTransactionTest, SpdyAlternateProtocolThroughProxy) { |
| 8147 | // This test ensures that the URL passed into the proxy is upgraded |
| 8148 | // to https when doing an Alternate Protocol upgrade. |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 8149 | HttpStreamFactory::set_use_alternate_protocols(true); |
| 8150 | HttpStreamFactory::set_next_protos( |
[email protected] | 42baef7a | 2011-12-10 04:52:10 | [diff] [blame] | 8151 | MakeNextProtos( |
| 8152 | "http/1.1", "http1.1", "spdy/2.1", "spdy/2", "spdy", NULL)); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8153 | |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 8154 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8155 | HttpAuthHandlerMock::Factory* auth_factory = |
| 8156 | new HttpAuthHandlerMock::Factory(); |
[email protected] | 76725780 | 2011-12-14 17:33:18 | [diff] [blame] | 8157 | HttpAuthHandlerMock* auth_handler = new HttpAuthHandlerMock(); |
| 8158 | auth_factory->AddMockHandler(auth_handler, HttpAuth::AUTH_PROXY); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8159 | auth_factory->set_do_init_from_challenge(true); |
| 8160 | session_deps.http_auth_handler_factory.reset(auth_factory); |
| 8161 | |
| 8162 | HttpRequestInfo request; |
| 8163 | request.method = "GET"; |
| 8164 | request.url = GURL("http://www.google.com"); |
| 8165 | request.load_flags = 0; |
| 8166 | |
| 8167 | // First round goes unauthenticated through the proxy. |
| 8168 | MockWrite data_writes_1[] = { |
| 8169 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 8170 | "Host: www.google.com\r\n" |
| 8171 | "Proxy-Connection: keep-alive\r\n" |
| 8172 | "\r\n"), |
| 8173 | }; |
| 8174 | MockRead data_reads_1[] = { |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 8175 | MockRead(false, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8176 | MockRead("HTTP/1.1 200 OK\r\n" |
[email protected] | 8d2f701 | 2012-02-16 00:08:04 | [diff] [blame] | 8177 | "Alternate-Protocol: 443:npn-spdy/2.1\r\n" |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8178 | "Proxy-Connection: close\r\n" |
| 8179 | "\r\n"), |
| 8180 | }; |
| 8181 | StaticSocketDataProvider data_1(data_reads_1, arraysize(data_reads_1), |
| 8182 | data_writes_1, arraysize(data_writes_1)); |
| 8183 | |
| 8184 | // Second round tries to tunnel to www.google.com due to the |
| 8185 | // Alternate-Protocol announcement in the first round. It fails due |
| 8186 | // to a proxy authentication challenge. |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 8187 | // After the failure, a tunnel is established to www.google.com using |
| 8188 | // Proxy-Authorization headers. There is then a SPDY request round. |
| 8189 | // |
[email protected] | fe3b7dc | 2012-02-03 19:52:09 | [diff] [blame] | 8190 | // NOTE: Despite the "Proxy-Connection: Close", these are done on the |
| 8191 | // same MockTCPClientSocket since the underlying HttpNetworkClientSocket |
| 8192 | // does a Disconnect and Connect on the same socket, rather than trying |
| 8193 | // to obtain a new one. |
| 8194 | // |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 8195 | // NOTE: Originally, the proxy response to the second CONNECT request |
| 8196 | // simply returned another 407 so the unit test could skip the SSL connection |
| 8197 | // establishment and SPDY framing issues. Alas, the |
| 8198 | // retry-http-when-alternate-protocol fails logic kicks in, which was more |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8199 | // complicated to set up expectations for than the SPDY session. |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 8200 | |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8201 | scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); |
| 8202 | scoped_ptr<spdy::SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 8203 | scoped_ptr<spdy::SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); |
| 8204 | |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 8205 | MockWrite data_writes_2[] = { |
| 8206 | // First connection attempt without Proxy-Authorization. |
| 8207 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 8208 | "Host: www.google.com\r\n" |
| 8209 | "Proxy-Connection: keep-alive\r\n" |
| 8210 | "\r\n"), |
| 8211 | |
| 8212 | // Second connection attempt with Proxy-Authorization. |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8213 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 8214 | "Host: www.google.com\r\n" |
| 8215 | "Proxy-Connection: keep-alive\r\n" |
| 8216 | "Proxy-Authorization: auth_token\r\n" |
| 8217 | "\r\n"), |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8218 | |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 8219 | // SPDY request |
| 8220 | CreateMockWrite(*req), |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8221 | }; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 8222 | const char kRejectConnectResponse[] = ("HTTP/1.1 407 Unauthorized\r\n" |
| 8223 | "Proxy-Authenticate: Mock\r\n" |
| 8224 | "Proxy-Connection: close\r\n" |
| 8225 | "\r\n"); |
| 8226 | const char kAcceptConnectResponse[] = "HTTP/1.1 200 Connected\r\n\r\n"; |
| 8227 | MockRead data_reads_2[] = { |
| 8228 | // First connection attempt fails |
| 8229 | MockRead(false, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ, 1), |
| 8230 | MockRead(true, kRejectConnectResponse, |
| 8231 | arraysize(kRejectConnectResponse) - 1, 1), |
| 8232 | |
| 8233 | // Second connection attempt passes |
| 8234 | MockRead(true, kAcceptConnectResponse, |
[email protected] | fe3b7dc | 2012-02-03 19:52:09 | [diff] [blame] | 8235 | arraysize(kAcceptConnectResponse) -1, 4), |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 8236 | |
| 8237 | // SPDY response |
[email protected] | fe3b7dc | 2012-02-03 19:52:09 | [diff] [blame] | 8238 | CreateMockRead(*resp.get(), 6), |
| 8239 | CreateMockRead(*data.get(), 6), |
| 8240 | MockRead(true, 0, 0, 6), |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 8241 | }; |
[email protected] | a159531 | 2012-01-22 03:25:04 | [diff] [blame] | 8242 | scoped_ptr<OrderedSocketData> data_2( |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 8243 | new OrderedSocketData(data_reads_2, arraysize(data_reads_2), |
| 8244 | data_writes_2, arraysize(data_writes_2))); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8245 | |
| 8246 | SSLSocketDataProvider ssl(true, OK); |
| 8247 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 8248 | ssl.next_proto = "spdy/2.1"; |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8249 | ssl.was_npn_negotiated = true; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 8250 | ssl.protocol_negotiated = SSLClientSocket::kProtoSPDY21; |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8251 | |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 8252 | MockConnect never_finishing_connect(false, ERR_IO_PENDING); |
| 8253 | StaticSocketDataProvider hanging_non_alternate_protocol_socket( |
| 8254 | NULL, 0, NULL, 0); |
| 8255 | hanging_non_alternate_protocol_socket.set_connect_data( |
| 8256 | never_finishing_connect); |
| 8257 | |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8258 | session_deps.socket_factory.AddSocketDataProvider(&data_1); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 8259 | session_deps.socket_factory.AddSocketDataProvider(data_2.get()); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8260 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 8261 | session_deps.socket_factory.AddSocketDataProvider( |
| 8262 | &hanging_non_alternate_protocol_socket); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8263 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 8264 | |
| 8265 | // First round should work and provide the Alternate-Protocol state. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8266 | TestCompletionCallback callback_1; |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8267 | scoped_ptr<HttpTransaction> trans_1(new HttpNetworkTransaction(session)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8268 | int rv = trans_1->Start(&request, callback_1.callback(), BoundNetLog()); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8269 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 8270 | EXPECT_EQ(OK, callback_1.WaitForResult()); |
| 8271 | |
| 8272 | // Second round should attempt a tunnel connect and get an auth challenge. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8273 | TestCompletionCallback callback_2; |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8274 | scoped_ptr<HttpTransaction> trans_2(new HttpNetworkTransaction(session)); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8275 | rv = trans_2->Start(&request, callback_2.callback(), BoundNetLog()); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8276 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 8277 | EXPECT_EQ(OK, callback_2.WaitForResult()); |
| 8278 | const HttpResponseInfo* response = trans_2->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 8279 | ASSERT_TRUE(response != NULL); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8280 | ASSERT_FALSE(response->auth_challenge.get() == NULL); |
| 8281 | |
| 8282 | // Restart with auth. Tunnel should work and response received. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8283 | TestCompletionCallback callback_3; |
| 8284 | rv = trans_2->RestartWithAuth( |
| 8285 | AuthCredentials(kFoo, kBar), callback_3.callback()); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8286 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 8287 | EXPECT_EQ(OK, callback_3.WaitForResult()); |
| 8288 | |
| 8289 | // After all that work, these two lines (or actually, just the scheme) are |
| 8290 | // what this test is all about. Make sure it happens correctly. |
[email protected] | 76725780 | 2011-12-14 17:33:18 | [diff] [blame] | 8291 | const GURL& request_url = auth_handler->request_url(); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8292 | EXPECT_EQ("https", request_url.scheme()); |
| 8293 | EXPECT_EQ("www.google.com", request_url.host()); |
| 8294 | |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 8295 | HttpStreamFactory::set_next_protos(std::vector<std::string>()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 8296 | HttpStreamFactory::set_use_alternate_protocols(false); |
| 8297 | } |
| 8298 | |
| 8299 | // Test that if we cancel the transaction as the connection is completing, that |
| 8300 | // everything tears down correctly. |
| 8301 | TEST_F(HttpNetworkTransactionTest, SimpleCancel) { |
| 8302 | // Setup everything about the connection to complete synchronously, so that |
| 8303 | // after calling HttpNetworkTransaction::Start, the only thing we're waiting |
| 8304 | // for is the callback from the HttpStreamRequest. |
| 8305 | // Then cancel the transaction. |
| 8306 | // Verify that we don't crash. |
| 8307 | MockConnect mock_connect(false, OK); |
| 8308 | MockRead data_reads[] = { |
| 8309 | MockRead(false, "HTTP/1.0 200 OK\r\n\r\n"), |
| 8310 | MockRead(false, "hello world"), |
| 8311 | MockRead(false, OK), |
| 8312 | }; |
| 8313 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 8314 | HttpRequestInfo request; |
| 8315 | request.method = "GET"; |
| 8316 | request.url = GURL("http://www.google.com/"); |
| 8317 | request.load_flags = 0; |
| 8318 | |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 8319 | SessionDependencies session_deps; |
| 8320 | session_deps.host_resolver->set_synchronous_mode(true); |
| 8321 | scoped_ptr<HttpTransaction> trans( |
| 8322 | new HttpNetworkTransaction(CreateSession(&session_deps))); |
| 8323 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 8324 | StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
| 8325 | data.set_connect_data(mock_connect); |
| 8326 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 8327 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8328 | TestCompletionCallback callback; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 8329 | |
| 8330 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8331 | int rv = trans->Start(&request, callback.callback(), log.bound()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 8332 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 8333 | trans.reset(); // Cancel the transaction here. |
| 8334 | |
| 8335 | MessageLoop::current()->RunAllPending(); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 8336 | } |
| 8337 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8338 | // Test a basic GET request through a proxy. |
| 8339 | TEST_F(HttpNetworkTransactionTest, ProxyGet) { |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 8340 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8341 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 8342 | session_deps.net_log = log.bound().net_log(); |
| 8343 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 8344 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8345 | HttpRequestInfo request; |
| 8346 | request.method = "GET"; |
| 8347 | request.url = GURL("http://www.google.com/"); |
| 8348 | |
| 8349 | MockWrite data_writes1[] = { |
| 8350 | MockWrite("GET http://www.google.com/ HTTP/1.1\r\n" |
| 8351 | "Host: www.google.com\r\n" |
| 8352 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 8353 | }; |
| 8354 | |
| 8355 | MockRead data_reads1[] = { |
| 8356 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 8357 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 8358 | MockRead("Content-Length: 100\r\n\r\n"), |
| 8359 | MockRead(false, OK), |
| 8360 | }; |
| 8361 | |
| 8362 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 8363 | data_writes1, arraysize(data_writes1)); |
| 8364 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 8365 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8366 | TestCompletionCallback callback1; |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8367 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 8368 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 8369 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8370 | int rv = trans->Start(&request, callback1.callback(), log.bound()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8371 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 8372 | |
| 8373 | rv = callback1.WaitForResult(); |
| 8374 | EXPECT_EQ(OK, rv); |
| 8375 | |
| 8376 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 8377 | ASSERT_TRUE(response != NULL); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8378 | |
| 8379 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 8380 | EXPECT_EQ(200, response->headers->response_code()); |
| 8381 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 8382 | EXPECT_TRUE(response->was_fetched_via_proxy); |
| 8383 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 8384 | } |
| 8385 | |
| 8386 | // Test a basic HTTPS GET request through a proxy. |
| 8387 | TEST_F(HttpNetworkTransactionTest, ProxyTunnelGet) { |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 8388 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8389 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 8390 | session_deps.net_log = log.bound().net_log(); |
| 8391 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 8392 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8393 | HttpRequestInfo request; |
| 8394 | request.method = "GET"; |
| 8395 | request.url = GURL("https://www.google.com/"); |
| 8396 | |
| 8397 | // Since we have proxy, should try to establish tunnel. |
| 8398 | MockWrite data_writes1[] = { |
| 8399 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 8400 | "Host: www.google.com\r\n" |
| 8401 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 8402 | |
| 8403 | MockWrite("GET / HTTP/1.1\r\n" |
| 8404 | "Host: www.google.com\r\n" |
| 8405 | "Connection: keep-alive\r\n\r\n"), |
| 8406 | }; |
| 8407 | |
| 8408 | MockRead data_reads1[] = { |
| 8409 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 8410 | |
| 8411 | MockRead("HTTP/1.1 200 OK\r\n"), |
| 8412 | MockRead("Content-Type: text/html; charset=iso-8859-1\r\n"), |
| 8413 | MockRead("Content-Length: 100\r\n\r\n"), |
| 8414 | MockRead(false, OK), |
| 8415 | }; |
| 8416 | |
| 8417 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 8418 | data_writes1, arraysize(data_writes1)); |
| 8419 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 8420 | SSLSocketDataProvider ssl(true, OK); |
| 8421 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 8422 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8423 | TestCompletionCallback callback1; |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8424 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 8425 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 8426 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8427 | int rv = trans->Start(&request, callback1.callback(), log.bound()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8428 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 8429 | |
| 8430 | rv = callback1.WaitForResult(); |
| 8431 | EXPECT_EQ(OK, rv); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 8432 | net::CapturingNetLog::EntryList entries; |
| 8433 | log.GetEntries(&entries); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8434 | size_t pos = ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 8435 | entries, 0, NetLog::TYPE_HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8436 | NetLog::PHASE_NONE); |
| 8437 | ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 8438 | entries, pos, |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8439 | NetLog::TYPE_HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 8440 | NetLog::PHASE_NONE); |
| 8441 | |
| 8442 | const HttpResponseInfo* response = trans->GetResponseInfo(); |
[email protected] | fe2255a | 2011-09-20 19:37:50 | [diff] [blame] | 8443 | ASSERT_TRUE(response != NULL); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8444 | |
| 8445 | EXPECT_TRUE(response->headers->IsKeepAlive()); |
| 8446 | EXPECT_EQ(200, response->headers->response_code()); |
| 8447 | EXPECT_EQ(100, response->headers->GetContentLength()); |
| 8448 | EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion()); |
| 8449 | EXPECT_TRUE(response->was_fetched_via_proxy); |
| 8450 | } |
| 8451 | |
| 8452 | // Test a basic HTTPS GET request through a proxy, but the server hangs up |
| 8453 | // while establishing the tunnel. |
| 8454 | TEST_F(HttpNetworkTransactionTest, ProxyTunnelGetHangup) { |
[email protected] | 81cdfcd | 2010-10-16 00:49:00 | [diff] [blame] | 8455 | SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:70")); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8456 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 8457 | session_deps.net_log = log.bound().net_log(); |
| 8458 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 8459 | |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8460 | HttpRequestInfo request; |
| 8461 | request.method = "GET"; |
| 8462 | request.url = GURL("https://www.google.com/"); |
| 8463 | |
| 8464 | // Since we have proxy, should try to establish tunnel. |
| 8465 | MockWrite data_writes1[] = { |
| 8466 | MockWrite("CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 8467 | "Host: www.google.com\r\n" |
| 8468 | "Proxy-Connection: keep-alive\r\n\r\n"), |
| 8469 | |
| 8470 | MockWrite("GET / HTTP/1.1\r\n" |
| 8471 | "Host: www.google.com\r\n" |
| 8472 | "Connection: keep-alive\r\n\r\n"), |
| 8473 | }; |
| 8474 | |
| 8475 | MockRead data_reads1[] = { |
| 8476 | MockRead(false, ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ), |
| 8477 | MockRead("HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 8478 | MockRead(true, 0, 0), // EOF |
| 8479 | }; |
| 8480 | |
| 8481 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), |
| 8482 | data_writes1, arraysize(data_writes1)); |
| 8483 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 8484 | SSLSocketDataProvider ssl(true, OK); |
| 8485 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 8486 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8487 | TestCompletionCallback callback1; |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8488 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 8489 | scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(session)); |
| 8490 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8491 | int rv = trans->Start(&request, callback1.callback(), log.bound()); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8492 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 8493 | |
| 8494 | rv = callback1.WaitForResult(); |
| 8495 | EXPECT_EQ(ERR_EMPTY_RESPONSE, rv); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 8496 | net::CapturingNetLog::EntryList entries; |
| 8497 | log.GetEntries(&entries); |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8498 | size_t pos = ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 8499 | entries, 0, NetLog::TYPE_HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8500 | NetLog::PHASE_NONE); |
| 8501 | ExpectLogContainsSomewhere( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 8502 | entries, pos, |
[email protected] | 76a505b | 2010-08-25 06:23:00 | [diff] [blame] | 8503 | NetLog::TYPE_HTTP_TRANSACTION_READ_TUNNEL_RESPONSE_HEADERS, |
| 8504 | NetLog::PHASE_NONE); |
| 8505 | } |
| 8506 | |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 8507 | // Test for crbug.com/55424. |
| 8508 | TEST_F(HttpNetworkTransactionTest, PreconnectWithExistingSpdySession) { |
| 8509 | SessionDependencies session_deps; |
| 8510 | |
| 8511 | scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet( |
| 8512 | "https://www.google.com", false, 1, LOWEST)); |
| 8513 | MockWrite spdy_writes[] = { CreateMockWrite(*req) }; |
| 8514 | |
| 8515 | scoped_ptr<spdy::SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 8516 | scoped_ptr<spdy::SpdyFrame> data(ConstructSpdyBodyFrame(1, true)); |
| 8517 | MockRead spdy_reads[] = { |
| 8518 | CreateMockRead(*resp), |
| 8519 | CreateMockRead(*data), |
| 8520 | MockRead(true, 0, 0), |
| 8521 | }; |
| 8522 | |
[email protected] | a159531 | 2012-01-22 03:25:04 | [diff] [blame] | 8523 | scoped_ptr<DelayedSocketData> spdy_data( |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 8524 | new DelayedSocketData( |
| 8525 | 1, // wait for one write to finish before reading. |
| 8526 | spdy_reads, arraysize(spdy_reads), |
| 8527 | spdy_writes, arraysize(spdy_writes))); |
[email protected] | a159531 | 2012-01-22 03:25:04 | [diff] [blame] | 8528 | session_deps.socket_factory.AddSocketDataProvider(spdy_data.get()); |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 8529 | |
| 8530 | SSLSocketDataProvider ssl(true, OK); |
| 8531 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 8532 | ssl.next_proto = "spdy/2.1"; |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 8533 | ssl.was_npn_negotiated = true; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 8534 | ssl.protocol_negotiated = SSLClientSocket::kProtoSPDY21; |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 8535 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 8536 | |
| 8537 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 8538 | |
| 8539 | // Set up an initial SpdySession in the pool to reuse. |
[email protected] | 02b0c34 | 2010-09-25 21:09:38 | [diff] [blame] | 8540 | HostPortPair host_port_pair("www.google.com", 443); |
| 8541 | HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 8542 | scoped_refptr<SpdySession> spdy_session = |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 8543 | session->spdy_session_pool()->Get(pair, BoundNetLog()); |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 8544 | scoped_refptr<TransportSocketParams> transport_params( |
[email protected] | acdda41 | 2011-11-15 21:21:29 | [diff] [blame] | 8545 | new TransportSocketParams(host_port_pair, MEDIUM, false, false)); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 8546 | TestCompletionCallback callback; |
[email protected] | 02b0c34 | 2010-09-25 21:09:38 | [diff] [blame] | 8547 | |
| 8548 | scoped_ptr<ClientSocketHandle> connection(new ClientSocketHandle); |
| 8549 | EXPECT_EQ(ERR_IO_PENDING, |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 8550 | connection->Init(host_port_pair.ToString(), transport_params, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 8551 | LOWEST, callback.callback(), |
[email protected] | a42dbd14 | 2011-11-17 16:42:02 | [diff] [blame] | 8552 | session->GetTransportSocketPool(), BoundNetLog())); |
[email protected] | 02b0c34 | 2010-09-25 21:09:38 | [diff] [blame] | 8553 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 8554 | spdy_session->InitializeWithSocket(connection.release(), false, OK); |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 8555 | |
| 8556 | HttpRequestInfo request; |
| 8557 | request.method = "GET"; |
| 8558 | request.url = GURL("https://www.google.com/"); |
| 8559 | request.load_flags = 0; |
| 8560 | |
| 8561 | // This is the important line that marks this as a preconnect. |
| 8562 | request.motivation = HttpRequestInfo::PRECONNECT_MOTIVATED; |
| 8563 | |
| 8564 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 8565 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8566 | int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 8567 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8568 | EXPECT_EQ(OK, callback.WaitForResult()); |
[email protected] | 749eefa8 | 2010-09-13 22:14:03 | [diff] [blame] | 8569 | } |
| 8570 | |
[email protected] | 73b8dd22 | 2010-11-11 19:55:24 | [diff] [blame] | 8571 | // Given a net error, cause that error to be returned from the first Write() |
| 8572 | // call and verify that the HttpTransaction fails with that error. |
| 8573 | static void CheckErrorIsPassedBack(int error, bool async) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 8574 | net::HttpRequestInfo request_info; |
| 8575 | request_info.url = GURL("https://www.example.com/"); |
| 8576 | request_info.method = "GET"; |
| 8577 | request_info.load_flags = net::LOAD_NORMAL; |
| 8578 | |
[email protected] | 73b8dd22 | 2010-11-11 19:55:24 | [diff] [blame] | 8579 | SessionDependencies session_deps; |
| 8580 | |
| 8581 | SSLSocketDataProvider ssl_data(async, OK); |
| 8582 | net::MockWrite data_writes[] = { |
| 8583 | net::MockWrite(async, error), |
| 8584 | }; |
| 8585 | net::StaticSocketDataProvider data(NULL, 0, |
| 8586 | data_writes, arraysize(data_writes)); |
| 8587 | session_deps.socket_factory.AddSocketDataProvider(&data); |
| 8588 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_data); |
| 8589 | |
| 8590 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 8591 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 8592 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8593 | TestCompletionCallback callback; |
| 8594 | int rv = trans->Start(&request_info, callback.callback(), net::BoundNetLog()); |
[email protected] | 73b8dd22 | 2010-11-11 19:55:24 | [diff] [blame] | 8595 | if (rv == net::ERR_IO_PENDING) |
| 8596 | rv = callback.WaitForResult(); |
| 8597 | ASSERT_EQ(error, rv); |
| 8598 | } |
| 8599 | |
| 8600 | TEST_F(HttpNetworkTransactionTest, SSLWriteCertError) { |
| 8601 | // Just check a grab bag of cert errors. |
| 8602 | static const int kErrors[] = { |
| 8603 | ERR_CERT_COMMON_NAME_INVALID, |
| 8604 | ERR_CERT_AUTHORITY_INVALID, |
| 8605 | ERR_CERT_DATE_INVALID, |
| 8606 | }; |
| 8607 | for (size_t i = 0; i < arraysize(kErrors); i++) { |
| 8608 | CheckErrorIsPassedBack(kErrors[i], false /* not async */); |
| 8609 | CheckErrorIsPassedBack(kErrors[i], true /* async */); |
| 8610 | } |
| 8611 | } |
| 8612 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 8613 | // Ensure that a client certificate is removed from the SSL client auth |
| 8614 | // cache when: |
| 8615 | // 1) No proxy is involved. |
| 8616 | // 2) TLS False Start is disabled. |
| 8617 | // 3) The initial TLS handshake requests a client certificate. |
| 8618 | // 4) The client supplies an invalid/unacceptable certificate. |
| 8619 | TEST_F(HttpNetworkTransactionTest, ClientAuthCertCache_Direct_NoFalseStart) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 8620 | net::HttpRequestInfo request_info; |
| 8621 | request_info.url = GURL("https://www.example.com/"); |
| 8622 | request_info.method = "GET"; |
| 8623 | request_info.load_flags = net::LOAD_NORMAL; |
| 8624 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 8625 | SessionDependencies session_deps; |
| 8626 | |
| 8627 | scoped_refptr<SSLCertRequestInfo> cert_request(new SSLCertRequestInfo()); |
| 8628 | cert_request->host_and_port = "www.example.com:443"; |
| 8629 | |
| 8630 | // [ssl_]data1 contains the data for the first SSL handshake. When a |
| 8631 | // CertificateRequest is received for the first time, the handshake will |
| 8632 | // be aborted to allow the caller to provide a certificate. |
| 8633 | SSLSocketDataProvider ssl_data1(true /* async */, |
| 8634 | net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
| 8635 | ssl_data1.cert_request_info = cert_request.get(); |
| 8636 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_data1); |
| 8637 | net::StaticSocketDataProvider data1(NULL, 0, NULL, 0); |
| 8638 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 8639 | |
| 8640 | // [ssl_]data2 contains the data for the second SSL handshake. When TLS |
| 8641 | // False Start is not being used, the result of the SSL handshake will be |
| 8642 | // returned as part of the SSLClientSocket::Connect() call. This test |
| 8643 | // matches the result of a server sending a handshake_failure alert, |
| 8644 | // rather than a Finished message, because it requires a client |
| 8645 | // certificate and none was supplied. |
| 8646 | SSLSocketDataProvider ssl_data2(true /* async */, |
| 8647 | net::ERR_SSL_PROTOCOL_ERROR); |
| 8648 | ssl_data2.cert_request_info = cert_request.get(); |
| 8649 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_data2); |
| 8650 | net::StaticSocketDataProvider data2(NULL, 0, NULL, 0); |
| 8651 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 8652 | |
| 8653 | // [ssl_]data3 contains the data for the third SSL handshake. When a |
| 8654 | // connection to a server fails during an SSL handshake, |
| 8655 | // HttpNetworkTransaction will attempt to fallback to SSLv3 if the initial |
| 8656 | // connection was attempted with TLSv1. This is transparent to the caller |
| 8657 | // of the HttpNetworkTransaction. Because this test failure is due to |
| 8658 | // requiring a client certificate, this fallback handshake should also |
| 8659 | // fail. |
| 8660 | SSLSocketDataProvider ssl_data3(true /* async */, |
| 8661 | net::ERR_SSL_PROTOCOL_ERROR); |
| 8662 | ssl_data3.cert_request_info = cert_request.get(); |
| 8663 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_data3); |
| 8664 | net::StaticSocketDataProvider data3(NULL, 0, NULL, 0); |
| 8665 | session_deps.socket_factory.AddSocketDataProvider(&data3); |
| 8666 | |
| 8667 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 8668 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 8669 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 8670 | // Begin the SSL handshake with the peer. This consumes ssl_data1. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8671 | TestCompletionCallback callback; |
| 8672 | int rv = trans->Start(&request_info, callback.callback(), net::BoundNetLog()); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 8673 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 8674 | |
| 8675 | // Complete the SSL handshake, which should abort due to requiring a |
| 8676 | // client certificate. |
| 8677 | rv = callback.WaitForResult(); |
| 8678 | ASSERT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED, rv); |
| 8679 | |
| 8680 | // Indicate that no certificate should be supplied. From the perspective |
| 8681 | // of SSLClientCertCache, NULL is just as meaningful as a real |
| 8682 | // certificate, so this is the same as supply a |
| 8683 | // legitimate-but-unacceptable certificate. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8684 | rv = trans->RestartWithCertificate(NULL, callback.callback()); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 8685 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 8686 | |
| 8687 | // Ensure the certificate was added to the client auth cache before |
| 8688 | // allowing the connection to continue restarting. |
| 8689 | scoped_refptr<X509Certificate> client_cert; |
| 8690 | ASSERT_TRUE(session->ssl_client_auth_cache()->Lookup("www.example.com:443", |
| 8691 | &client_cert)); |
| 8692 | ASSERT_EQ(NULL, client_cert.get()); |
| 8693 | |
| 8694 | // Restart the handshake. This will consume ssl_data2, which fails, and |
| 8695 | // then consume ssl_data3, which should also fail. The result code is |
| 8696 | // checked against what ssl_data3 should return. |
| 8697 | rv = callback.WaitForResult(); |
| 8698 | ASSERT_EQ(net::ERR_SSL_PROTOCOL_ERROR, rv); |
| 8699 | |
| 8700 | // Ensure that the client certificate is removed from the cache on a |
| 8701 | // handshake failure. |
| 8702 | ASSERT_FALSE(session->ssl_client_auth_cache()->Lookup("www.example.com:443", |
| 8703 | &client_cert)); |
| 8704 | } |
| 8705 | |
| 8706 | // Ensure that a client certificate is removed from the SSL client auth |
| 8707 | // cache when: |
| 8708 | // 1) No proxy is involved. |
| 8709 | // 2) TLS False Start is enabled. |
| 8710 | // 3) The initial TLS handshake requests a client certificate. |
| 8711 | // 4) The client supplies an invalid/unacceptable certificate. |
| 8712 | TEST_F(HttpNetworkTransactionTest, ClientAuthCertCache_Direct_FalseStart) { |
[email protected] | cb9bf6ca | 2011-01-28 13:15:27 | [diff] [blame] | 8713 | net::HttpRequestInfo request_info; |
| 8714 | request_info.url = GURL("https://www.example.com/"); |
| 8715 | request_info.method = "GET"; |
| 8716 | request_info.load_flags = net::LOAD_NORMAL; |
| 8717 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 8718 | SessionDependencies session_deps; |
| 8719 | |
| 8720 | scoped_refptr<SSLCertRequestInfo> cert_request(new SSLCertRequestInfo()); |
| 8721 | cert_request->host_and_port = "www.example.com:443"; |
| 8722 | |
| 8723 | // When TLS False Start is used, SSLClientSocket::Connect() calls will |
| 8724 | // return successfully after reading up to the peer's Certificate message. |
| 8725 | // This is to allow the caller to call SSLClientSocket::Write(), which can |
| 8726 | // enqueue application data to be sent in the same packet as the |
| 8727 | // ChangeCipherSpec and Finished messages. |
| 8728 | // The actual handshake will be finished when SSLClientSocket::Read() is |
| 8729 | // called, which expects to process the peer's ChangeCipherSpec and |
| 8730 | // Finished messages. If there was an error negotiating with the peer, |
| 8731 | // such as due to the peer requiring a client certificate when none was |
| 8732 | // supplied, the alert sent by the peer won't be processed until Read() is |
| 8733 | // called. |
| 8734 | |
| 8735 | // Like the non-False Start case, when a client certificate is requested by |
| 8736 | // the peer, the handshake is aborted during the Connect() call. |
| 8737 | // [ssl_]data1 represents the initial SSL handshake with the peer. |
| 8738 | SSLSocketDataProvider ssl_data1(true /* async */, |
| 8739 | net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
| 8740 | ssl_data1.cert_request_info = cert_request.get(); |
| 8741 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_data1); |
| 8742 | net::StaticSocketDataProvider data1(NULL, 0, NULL, 0); |
| 8743 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 8744 | |
| 8745 | // When a client certificate is supplied, Connect() will not be aborted |
| 8746 | // when the peer requests the certificate. Instead, the handshake will |
| 8747 | // artificially succeed, allowing the caller to write the HTTP request to |
| 8748 | // the socket. The handshake messages are not processed until Read() is |
| 8749 | // called, which then detects that the handshake was aborted, due to the |
| 8750 | // peer sending a handshake_failure because it requires a client |
| 8751 | // certificate. |
| 8752 | SSLSocketDataProvider ssl_data2(true /* async */, net::OK); |
| 8753 | ssl_data2.cert_request_info = cert_request.get(); |
| 8754 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_data2); |
| 8755 | net::MockRead data2_reads[] = { |
| 8756 | net::MockRead(true /* async */, net::ERR_SSL_PROTOCOL_ERROR), |
| 8757 | }; |
| 8758 | net::StaticSocketDataProvider data2( |
| 8759 | data2_reads, arraysize(data2_reads), NULL, 0); |
| 8760 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 8761 | |
| 8762 | // As described in ClientAuthCertCache_Direct_NoFalseStart, [ssl_]data3 is |
| 8763 | // the data for the SSL handshake once the TLSv1 connection falls back to |
| 8764 | // SSLv3. It has the same behaviour as [ssl_]data2. |
| 8765 | SSLSocketDataProvider ssl_data3(true /* async */, net::OK); |
| 8766 | ssl_data3.cert_request_info = cert_request.get(); |
| 8767 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_data3); |
| 8768 | net::StaticSocketDataProvider data3( |
| 8769 | data2_reads, arraysize(data2_reads), NULL, 0); |
| 8770 | session_deps.socket_factory.AddSocketDataProvider(&data3); |
| 8771 | |
| 8772 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 8773 | scoped_ptr<HttpNetworkTransaction> trans(new HttpNetworkTransaction(session)); |
| 8774 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 8775 | // Begin the initial SSL handshake. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8776 | TestCompletionCallback callback; |
| 8777 | int rv = trans->Start(&request_info, callback.callback(), net::BoundNetLog()); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 8778 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 8779 | |
| 8780 | // Complete the SSL handshake, which should abort due to requiring a |
| 8781 | // client certificate. |
| 8782 | rv = callback.WaitForResult(); |
| 8783 | ASSERT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED, rv); |
| 8784 | |
| 8785 | // Indicate that no certificate should be supplied. From the perspective |
| 8786 | // of SSLClientCertCache, NULL is just as meaningful as a real |
| 8787 | // certificate, so this is the same as supply a |
| 8788 | // legitimate-but-unacceptable certificate. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8789 | rv = trans->RestartWithCertificate(NULL, callback.callback()); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 8790 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 8791 | |
| 8792 | // Ensure the certificate was added to the client auth cache before |
| 8793 | // allowing the connection to continue restarting. |
| 8794 | scoped_refptr<X509Certificate> client_cert; |
| 8795 | ASSERT_TRUE(session->ssl_client_auth_cache()->Lookup("www.example.com:443", |
| 8796 | &client_cert)); |
| 8797 | ASSERT_EQ(NULL, client_cert.get()); |
| 8798 | |
| 8799 | |
| 8800 | // Restart the handshake. This will consume ssl_data2, which fails, and |
| 8801 | // then consume ssl_data3, which should also fail. The result code is |
| 8802 | // checked against what ssl_data3 should return. |
| 8803 | rv = callback.WaitForResult(); |
| 8804 | ASSERT_EQ(net::ERR_SSL_PROTOCOL_ERROR, rv); |
| 8805 | |
| 8806 | // Ensure that the client certificate is removed from the cache on a |
| 8807 | // handshake failure. |
| 8808 | ASSERT_FALSE(session->ssl_client_auth_cache()->Lookup("www.example.com:443", |
| 8809 | &client_cert)); |
| 8810 | } |
| 8811 | |
[email protected] | 8c40513 | 2011-01-11 22:03:18 | [diff] [blame] | 8812 | // Ensure that a client certificate is removed from the SSL client auth |
| 8813 | // cache when: |
| 8814 | // 1) An HTTPS proxy is involved. |
| 8815 | // 3) The HTTPS proxy requests a client certificate. |
| 8816 | // 4) The client supplies an invalid/unacceptable certificate for the |
| 8817 | // proxy. |
| 8818 | // The test is repeated twice, first for connecting to an HTTPS endpoint, |
| 8819 | // then for connecting to an HTTP endpoint. |
| 8820 | TEST_F(HttpNetworkTransactionTest, ClientAuthCertCache_Proxy_Fail) { |
| 8821 | SessionDependencies session_deps( |
| 8822 | ProxyService::CreateFixed("https://proxy:70")); |
| 8823 | CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 8824 | session_deps.net_log = log.bound().net_log(); |
| 8825 | |
| 8826 | scoped_refptr<SSLCertRequestInfo> cert_request(new SSLCertRequestInfo()); |
| 8827 | cert_request->host_and_port = "proxy:70"; |
| 8828 | |
| 8829 | // See ClientAuthCertCache_Direct_NoFalseStart for the explanation of |
| 8830 | // [ssl_]data[1-3]. Rather than represending the endpoint |
| 8831 | // (www.example.com:443), they represent failures with the HTTPS proxy |
| 8832 | // (proxy:70). |
| 8833 | SSLSocketDataProvider ssl_data1(true /* async */, |
| 8834 | net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
| 8835 | ssl_data1.cert_request_info = cert_request.get(); |
| 8836 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_data1); |
| 8837 | net::StaticSocketDataProvider data1(NULL, 0, NULL, 0); |
| 8838 | session_deps.socket_factory.AddSocketDataProvider(&data1); |
| 8839 | |
| 8840 | SSLSocketDataProvider ssl_data2(true /* async */, |
| 8841 | net::ERR_SSL_PROTOCOL_ERROR); |
| 8842 | ssl_data2.cert_request_info = cert_request.get(); |
| 8843 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_data2); |
| 8844 | net::StaticSocketDataProvider data2(NULL, 0, NULL, 0); |
| 8845 | session_deps.socket_factory.AddSocketDataProvider(&data2); |
| 8846 | |
| 8847 | SSLSocketDataProvider ssl_data3(true /* async */, |
| 8848 | net::ERR_SSL_PROTOCOL_ERROR); |
| 8849 | ssl_data3.cert_request_info = cert_request.get(); |
| 8850 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_data3); |
| 8851 | net::StaticSocketDataProvider data3(NULL, 0, NULL, 0); |
| 8852 | session_deps.socket_factory.AddSocketDataProvider(&data3); |
| 8853 | |
| 8854 | net::HttpRequestInfo requests[2]; |
| 8855 | requests[0].url = GURL("https://www.example.com/"); |
| 8856 | requests[0].method = "GET"; |
| 8857 | requests[0].load_flags = net::LOAD_NORMAL; |
| 8858 | |
| 8859 | requests[1].url = GURL("http://www.example.com/"); |
| 8860 | requests[1].method = "GET"; |
| 8861 | requests[1].load_flags = net::LOAD_NORMAL; |
| 8862 | |
| 8863 | for (size_t i = 0; i < arraysize(requests); ++i) { |
| 8864 | session_deps.socket_factory.ResetNextMockIndexes(); |
| 8865 | scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 8866 | scoped_ptr<HttpNetworkTransaction> trans( |
| 8867 | new HttpNetworkTransaction(session)); |
| 8868 | |
| 8869 | // Begin the SSL handshake with the proxy. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8870 | TestCompletionCallback callback; |
| 8871 | int rv = trans->Start( |
| 8872 | &requests[i], callback.callback(), net::BoundNetLog()); |
[email protected] | 8c40513 | 2011-01-11 22:03:18 | [diff] [blame] | 8873 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 8874 | |
| 8875 | // Complete the SSL handshake, which should abort due to requiring a |
| 8876 | // client certificate. |
| 8877 | rv = callback.WaitForResult(); |
| 8878 | ASSERT_EQ(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED, rv); |
| 8879 | |
| 8880 | // Indicate that no certificate should be supplied. From the perspective |
| 8881 | // of SSLClientCertCache, NULL is just as meaningful as a real |
| 8882 | // certificate, so this is the same as supply a |
| 8883 | // legitimate-but-unacceptable certificate. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 8884 | rv = trans->RestartWithCertificate(NULL, callback.callback()); |
[email protected] | 8c40513 | 2011-01-11 22:03:18 | [diff] [blame] | 8885 | ASSERT_EQ(net::ERR_IO_PENDING, rv); |
| 8886 | |
| 8887 | // Ensure the certificate was added to the client auth cache before |
| 8888 | // allowing the connection to continue restarting. |
| 8889 | scoped_refptr<X509Certificate> client_cert; |
| 8890 | ASSERT_TRUE(session->ssl_client_auth_cache()->Lookup("proxy:70", |
| 8891 | &client_cert)); |
| 8892 | ASSERT_EQ(NULL, client_cert.get()); |
| 8893 | // Ensure the certificate was NOT cached for the endpoint. This only |
| 8894 | // applies to HTTPS requests, but is fine to check for HTTP requests. |
| 8895 | ASSERT_FALSE(session->ssl_client_auth_cache()->Lookup("www.example.com:443", |
| 8896 | &client_cert)); |
| 8897 | |
| 8898 | // Restart the handshake. This will consume ssl_data2, which fails, and |
| 8899 | // then consume ssl_data3, which should also fail. The result code is |
| 8900 | // checked against what ssl_data3 should return. |
| 8901 | rv = callback.WaitForResult(); |
| 8902 | ASSERT_EQ(net::ERR_PROXY_CONNECTION_FAILED, rv); |
| 8903 | |
| 8904 | // Now that the new handshake has failed, ensure that the client |
| 8905 | // certificate was removed from the client auth cache. |
| 8906 | ASSERT_FALSE(session->ssl_client_auth_cache()->Lookup("proxy:70", |
| 8907 | &client_cert)); |
| 8908 | ASSERT_FALSE(session->ssl_client_auth_cache()->Lookup("www.example.com:443", |
| 8909 | &client_cert)); |
| 8910 | } |
| 8911 | } |
| 8912 | |
[email protected] | 5c288bc | 2011-07-26 15:12:05 | [diff] [blame] | 8913 | void IPPoolingAddAlias(MockCachingHostResolver* host_resolver, |
| 8914 | SpdySessionPoolPeer* pool_peer, |
| 8915 | std::string host, |
| 8916 | int port, |
| 8917 | std::string iplist) { |
| 8918 | // Create a host resolver dependency that returns address |iplist| for |
| 8919 | // resolutions of |host|. |
| 8920 | host_resolver->rules()->AddIPLiteralRule(host, iplist, ""); |
[email protected] | 46da33be | 2011-07-19 21:58:04 | [diff] [blame] | 8921 | |
[email protected] | 5c288bc | 2011-07-26 15:12:05 | [diff] [blame] | 8922 | // Setup a HostPortProxyPair. |
| 8923 | HostPortPair host_port_pair(host, port); |
| 8924 | HostPortProxyPair pair = HostPortProxyPair(host_port_pair, |
| 8925 | ProxyServer::Direct()); |
[email protected] | 46da33be | 2011-07-19 21:58:04 | [diff] [blame] | 8926 | |
[email protected] | 5c288bc | 2011-07-26 15:12:05 | [diff] [blame] | 8927 | // Resolve the host and port. |
| 8928 | AddressList addresses; |
| 8929 | HostResolver::RequestInfo info(host_port_pair); |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 8930 | TestCompletionCallback callback; |
| 8931 | int rv = host_resolver->Resolve(info, &addresses, callback.callback(), NULL, |
[email protected] | 6e78dfb | 2011-07-28 21:34:47 | [diff] [blame] | 8932 | BoundNetLog()); |
| 8933 | if (rv == ERR_IO_PENDING) |
| 8934 | rv = callback.WaitForResult(); |
| 8935 | DCHECK_EQ(OK, rv); |
[email protected] | 46da33be | 2011-07-19 21:58:04 | [diff] [blame] | 8936 | |
[email protected] | 5c288bc | 2011-07-26 15:12:05 | [diff] [blame] | 8937 | // Add the first address as an alias. It would have been better to call |
| 8938 | // MockClientSocket::GetPeerAddress but that returns 192.0.2.33 whereas |
| 8939 | // MockHostResolver returns 127.0.0.1 (MockHostResolverBase::Reset). So we use |
| 8940 | // the first address (127.0.0.1) returned by MockHostResolver as an alias for |
| 8941 | // the |pair|. |
| 8942 | const addrinfo* address = addresses.head(); |
| 8943 | pool_peer->AddAlias(address, pair); |
[email protected] | 46da33be | 2011-07-19 21:58:04 | [diff] [blame] | 8944 | } |
| 8945 | |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 8946 | TEST_F(HttpNetworkTransactionTest, UseIPConnectionPooling) { |
| 8947 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 8948 | HttpStreamFactory::set_next_protos(SpdyNextProtos()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 8949 | |
| 8950 | // Set up a special HttpNetworkSession with a MockCachingHostResolver. |
| 8951 | SessionDependencies session_deps; |
| 8952 | MockCachingHostResolver host_resolver; |
| 8953 | net::HttpNetworkSession::Params params; |
| 8954 | params.client_socket_factory = &session_deps.socket_factory; |
| 8955 | params.host_resolver = &host_resolver; |
| 8956 | params.cert_verifier = session_deps.cert_verifier.get(); |
| 8957 | params.proxy_service = session_deps.proxy_service.get(); |
| 8958 | params.ssl_config_service = session_deps.ssl_config_service; |
| 8959 | params.http_auth_handler_factory = |
| 8960 | session_deps.http_auth_handler_factory.get(); |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 8961 | params.http_server_properties = &session_deps.http_server_properties; |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 8962 | params.net_log = session_deps.net_log; |
| 8963 | scoped_refptr<HttpNetworkSession> session(new HttpNetworkSession(params)); |
[email protected] | b9ec688 | 2011-07-01 07:40:26 | [diff] [blame] | 8964 | SpdySessionPoolPeer pool_peer(session->spdy_session_pool()); |
| 8965 | pool_peer.DisableDomainAuthenticationVerification(); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 8966 | |
| 8967 | SSLSocketDataProvider ssl(true, OK); |
| 8968 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 8969 | ssl.next_proto = "spdy/2.1"; |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 8970 | ssl.was_npn_negotiated = true; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 8971 | ssl.protocol_negotiated = SSLClientSocket::kProtoSPDY21; |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 8972 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 8973 | |
| 8974 | scoped_ptr<spdy::SpdyFrame> host1_req(ConstructSpdyGet( |
| 8975 | "https://www.google.com", false, 1, LOWEST)); |
| 8976 | scoped_ptr<spdy::SpdyFrame> host2_req(ConstructSpdyGet( |
| 8977 | "https://www.gmail.com", false, 3, LOWEST)); |
| 8978 | MockWrite spdy_writes[] = { |
| 8979 | CreateMockWrite(*host1_req, 1), |
| 8980 | CreateMockWrite(*host2_req, 4), |
| 8981 | }; |
| 8982 | scoped_ptr<spdy::SpdyFrame> host1_resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 8983 | scoped_ptr<spdy::SpdyFrame> host1_resp_body(ConstructSpdyBodyFrame(1, true)); |
| 8984 | scoped_ptr<spdy::SpdyFrame> host2_resp(ConstructSpdyGetSynReply(NULL, 0, 3)); |
| 8985 | scoped_ptr<spdy::SpdyFrame> host2_resp_body(ConstructSpdyBodyFrame(3, true)); |
| 8986 | MockRead spdy_reads[] = { |
| 8987 | CreateMockRead(*host1_resp, 2), |
| 8988 | CreateMockRead(*host1_resp_body, 3), |
| 8989 | CreateMockRead(*host2_resp, 5), |
| 8990 | CreateMockRead(*host2_resp_body, 6), |
| 8991 | MockRead(true, 0, 7), |
| 8992 | }; |
| 8993 | |
[email protected] | a159531 | 2012-01-22 03:25:04 | [diff] [blame] | 8994 | scoped_ptr<OrderedSocketData> spdy_data( |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 8995 | new OrderedSocketData( |
| 8996 | spdy_reads, arraysize(spdy_reads), |
| 8997 | spdy_writes, arraysize(spdy_writes))); |
[email protected] | a159531 | 2012-01-22 03:25:04 | [diff] [blame] | 8998 | session_deps.socket_factory.AddSocketDataProvider(spdy_data.get()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 8999 | |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 9000 | TestCompletionCallback callback; |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9001 | HttpRequestInfo request1; |
| 9002 | request1.method = "GET"; |
| 9003 | request1.url = GURL("https://www.google.com/"); |
| 9004 | request1.load_flags = 0; |
| 9005 | HttpNetworkTransaction trans1(session); |
| 9006 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 9007 | int rv = trans1.Start(&request1, callback.callback(), BoundNetLog()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9008 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 9009 | EXPECT_EQ(OK, callback.WaitForResult()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9010 | |
| 9011 | const HttpResponseInfo* response = trans1.GetResponseInfo(); |
| 9012 | ASSERT_TRUE(response != NULL); |
| 9013 | ASSERT_TRUE(response->headers != NULL); |
| 9014 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 9015 | |
| 9016 | std::string response_data; |
| 9017 | ASSERT_EQ(OK, ReadTransaction(&trans1, &response_data)); |
| 9018 | EXPECT_EQ("hello!", response_data); |
| 9019 | |
| 9020 | // Preload www.gmail.com into HostCache. |
| 9021 | HostPortPair host_port("www.gmail.com", 443); |
| 9022 | HostResolver::RequestInfo resolve_info(host_port); |
| 9023 | AddressList ignored; |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 9024 | rv = host_resolver.Resolve(resolve_info, &ignored, callback.callback(), NULL, |
[email protected] | 6e78dfb | 2011-07-28 21:34:47 | [diff] [blame] | 9025 | BoundNetLog()); |
| 9026 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 9027 | rv = callback.WaitForResult(); |
| 9028 | EXPECT_EQ(OK, rv); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9029 | |
[email protected] | 5c288bc | 2011-07-26 15:12:05 | [diff] [blame] | 9030 | // MockHostResolver returns 127.0.0.1, port 443 for https://www.google.com/ |
| 9031 | // and https://www.gmail.com/. Add 127.0.0.1 as alias for host_port_pair: |
| 9032 | // (www.google.com, 443). |
| 9033 | IPPoolingAddAlias(&host_resolver, &pool_peer, "www.google.com", 443, |
| 9034 | "127.0.0.1"); |
[email protected] | 46da33be | 2011-07-19 21:58:04 | [diff] [blame] | 9035 | |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9036 | HttpRequestInfo request2; |
| 9037 | request2.method = "GET"; |
| 9038 | request2.url = GURL("https://www.gmail.com/"); |
| 9039 | request2.load_flags = 0; |
| 9040 | HttpNetworkTransaction trans2(session); |
| 9041 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 9042 | rv = trans2.Start(&request2, callback.callback(), BoundNetLog()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9043 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 9044 | EXPECT_EQ(OK, callback.WaitForResult()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9045 | |
| 9046 | response = trans2.GetResponseInfo(); |
| 9047 | ASSERT_TRUE(response != NULL); |
| 9048 | ASSERT_TRUE(response->headers != NULL); |
| 9049 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 9050 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 9051 | EXPECT_TRUE(response->was_npn_negotiated); |
| 9052 | ASSERT_EQ(OK, ReadTransaction(&trans2, &response_data)); |
| 9053 | EXPECT_EQ("hello!", response_data); |
| 9054 | |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 9055 | HttpStreamFactory::set_next_protos(std::vector<std::string>()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9056 | HttpStreamFactory::set_use_alternate_protocols(false); |
| 9057 | } |
| 9058 | |
| 9059 | class OneTimeCachingHostResolver : public net::HostResolver { |
| 9060 | public: |
| 9061 | explicit OneTimeCachingHostResolver(const HostPortPair& host_port) |
| 9062 | : host_port_(host_port) {} |
| 9063 | virtual ~OneTimeCachingHostResolver() {} |
| 9064 | |
| 9065 | RuleBasedHostResolverProc* rules() { return host_resolver_.rules(); } |
| 9066 | |
| 9067 | // HostResolver methods: |
| 9068 | virtual int Resolve(const RequestInfo& info, |
| 9069 | AddressList* addresses, |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 9070 | const CompletionCallback& callback, |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9071 | RequestHandle* out_req, |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 9072 | const BoundNetLog& net_log) OVERRIDE { |
| 9073 | return host_resolver_.Resolve( |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9074 | info, addresses, callback, out_req, net_log); |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 9075 | } |
| 9076 | |
| 9077 | virtual int ResolveFromCache(const RequestInfo& info, |
| 9078 | AddressList* addresses, |
| 9079 | const BoundNetLog& net_log) OVERRIDE { |
| 9080 | int rv = host_resolver_.ResolveFromCache(info, addresses, net_log); |
| 9081 | if (rv == OK && info.host_port_pair().Equals(host_port_)) |
[email protected] | 98e1cd01 | 2011-11-08 15:33:09 | [diff] [blame] | 9082 | host_resolver_.GetHostCache()->clear(); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9083 | return rv; |
| 9084 | } |
| 9085 | |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 9086 | virtual void CancelRequest(RequestHandle req) OVERRIDE { |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9087 | host_resolver_.CancelRequest(req); |
| 9088 | } |
| 9089 | |
[email protected] | 46da33be | 2011-07-19 21:58:04 | [diff] [blame] | 9090 | MockCachingHostResolver* GetMockHostResolver() { |
| 9091 | return &host_resolver_; |
| 9092 | } |
| 9093 | |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9094 | private: |
| 9095 | MockCachingHostResolver host_resolver_; |
| 9096 | const HostPortPair host_port_; |
| 9097 | }; |
| 9098 | |
| 9099 | TEST_F(HttpNetworkTransactionTest, |
| 9100 | UseIPConnectionPoolingWithHostCacheExpiration) { |
| 9101 | HttpStreamFactory::set_use_alternate_protocols(true); |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 9102 | HttpStreamFactory::set_next_protos(SpdyNextProtos()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9103 | |
| 9104 | // Set up a special HttpNetworkSession with a OneTimeCachingHostResolver. |
| 9105 | SessionDependencies session_deps; |
| 9106 | OneTimeCachingHostResolver host_resolver(HostPortPair("www.gmail.com", 443)); |
| 9107 | net::HttpNetworkSession::Params params; |
| 9108 | params.client_socket_factory = &session_deps.socket_factory; |
| 9109 | params.host_resolver = &host_resolver; |
| 9110 | params.cert_verifier = session_deps.cert_verifier.get(); |
| 9111 | params.proxy_service = session_deps.proxy_service.get(); |
| 9112 | params.ssl_config_service = session_deps.ssl_config_service; |
| 9113 | params.http_auth_handler_factory = |
| 9114 | session_deps.http_auth_handler_factory.get(); |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 9115 | params.http_server_properties = &session_deps.http_server_properties; |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9116 | params.net_log = session_deps.net_log; |
| 9117 | scoped_refptr<HttpNetworkSession> session(new HttpNetworkSession(params)); |
[email protected] | b9ec688 | 2011-07-01 07:40:26 | [diff] [blame] | 9118 | SpdySessionPoolPeer pool_peer(session->spdy_session_pool()); |
| 9119 | pool_peer.DisableDomainAuthenticationVerification(); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9120 | |
| 9121 | SSLSocketDataProvider ssl(true, OK); |
| 9122 | ssl.next_proto_status = SSLClientSocket::kNextProtoNegotiated; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 9123 | ssl.next_proto = "spdy/2.1"; |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9124 | ssl.was_npn_negotiated = true; |
[email protected] | 1ad77dc | 2012-01-27 03:51:20 | [diff] [blame] | 9125 | ssl.protocol_negotiated = SSLClientSocket::kProtoSPDY21; |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9126 | session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
| 9127 | |
| 9128 | scoped_ptr<spdy::SpdyFrame> host1_req(ConstructSpdyGet( |
| 9129 | "https://www.google.com", false, 1, LOWEST)); |
| 9130 | scoped_ptr<spdy::SpdyFrame> host2_req(ConstructSpdyGet( |
| 9131 | "https://www.gmail.com", false, 3, LOWEST)); |
| 9132 | MockWrite spdy_writes[] = { |
| 9133 | CreateMockWrite(*host1_req, 1), |
| 9134 | CreateMockWrite(*host2_req, 4), |
| 9135 | }; |
| 9136 | scoped_ptr<spdy::SpdyFrame> host1_resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 9137 | scoped_ptr<spdy::SpdyFrame> host1_resp_body(ConstructSpdyBodyFrame(1, true)); |
| 9138 | scoped_ptr<spdy::SpdyFrame> host2_resp(ConstructSpdyGetSynReply(NULL, 0, 3)); |
| 9139 | scoped_ptr<spdy::SpdyFrame> host2_resp_body(ConstructSpdyBodyFrame(3, true)); |
| 9140 | MockRead spdy_reads[] = { |
| 9141 | CreateMockRead(*host1_resp, 2), |
| 9142 | CreateMockRead(*host1_resp_body, 3), |
| 9143 | CreateMockRead(*host2_resp, 5), |
| 9144 | CreateMockRead(*host2_resp_body, 6), |
| 9145 | MockRead(true, 0, 7), |
| 9146 | }; |
| 9147 | |
[email protected] | a159531 | 2012-01-22 03:25:04 | [diff] [blame] | 9148 | scoped_ptr<OrderedSocketData> spdy_data( |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9149 | new OrderedSocketData( |
| 9150 | spdy_reads, arraysize(spdy_reads), |
| 9151 | spdy_writes, arraysize(spdy_writes))); |
[email protected] | a159531 | 2012-01-22 03:25:04 | [diff] [blame] | 9152 | session_deps.socket_factory.AddSocketDataProvider(spdy_data.get()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9153 | |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 9154 | TestCompletionCallback callback; |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9155 | HttpRequestInfo request1; |
| 9156 | request1.method = "GET"; |
| 9157 | request1.url = GURL("https://www.google.com/"); |
| 9158 | request1.load_flags = 0; |
| 9159 | HttpNetworkTransaction trans1(session); |
| 9160 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 9161 | int rv = trans1.Start(&request1, callback.callback(), BoundNetLog()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9162 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 9163 | EXPECT_EQ(OK, callback.WaitForResult()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9164 | |
| 9165 | const HttpResponseInfo* response = trans1.GetResponseInfo(); |
| 9166 | ASSERT_TRUE(response != NULL); |
| 9167 | ASSERT_TRUE(response->headers != NULL); |
| 9168 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 9169 | |
| 9170 | std::string response_data; |
| 9171 | ASSERT_EQ(OK, ReadTransaction(&trans1, &response_data)); |
| 9172 | EXPECT_EQ("hello!", response_data); |
| 9173 | |
| 9174 | // Preload cache entries into HostCache. |
| 9175 | HostResolver::RequestInfo resolve_info(HostPortPair("www.gmail.com", 443)); |
| 9176 | AddressList ignored; |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 9177 | rv = host_resolver.Resolve(resolve_info, &ignored, callback.callback(), NULL, |
[email protected] | 6e78dfb | 2011-07-28 21:34:47 | [diff] [blame] | 9178 | BoundNetLog()); |
| 9179 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 9180 | rv = callback.WaitForResult(); |
| 9181 | EXPECT_EQ(OK, rv); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9182 | |
| 9183 | HttpRequestInfo request2; |
| 9184 | request2.method = "GET"; |
| 9185 | request2.url = GURL("https://www.gmail.com/"); |
| 9186 | request2.load_flags = 0; |
| 9187 | HttpNetworkTransaction trans2(session); |
| 9188 | |
[email protected] | 5c288bc | 2011-07-26 15:12:05 | [diff] [blame] | 9189 | // MockHostResolver returns 127.0.0.1, port 443 for https://www.google.com/ |
| 9190 | // and https://www.gmail.com/. Add 127.0.0.1 as alias for host_port_pair: |
| 9191 | // (www.google.com, 443). |
| 9192 | IPPoolingAddAlias(host_resolver.GetMockHostResolver(), &pool_peer, |
| 9193 | "www.google.com", 443, "127.0.0.1"); |
[email protected] | 46da33be | 2011-07-19 21:58:04 | [diff] [blame] | 9194 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 9195 | rv = trans2.Start(&request2, callback.callback(), BoundNetLog()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9196 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 9197 | EXPECT_EQ(OK, callback.WaitForResult()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9198 | |
| 9199 | response = trans2.GetResponseInfo(); |
| 9200 | ASSERT_TRUE(response != NULL); |
| 9201 | ASSERT_TRUE(response->headers != NULL); |
| 9202 | EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine()); |
| 9203 | EXPECT_TRUE(response->was_fetched_via_spdy); |
| 9204 | EXPECT_TRUE(response->was_npn_negotiated); |
| 9205 | ASSERT_EQ(OK, ReadTransaction(&trans2, &response_data)); |
| 9206 | EXPECT_EQ("hello!", response_data); |
| 9207 | |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 9208 | HttpStreamFactory::set_next_protos(std::vector<std::string>()); |
[email protected] | e3ceb68 | 2011-06-28 23:55:46 | [diff] [blame] | 9209 | HttpStreamFactory::set_use_alternate_protocols(false); |
| 9210 | } |
| 9211 | |
[email protected] | 5a60c8b | 2011-10-19 20:14:29 | [diff] [blame] | 9212 | TEST_F(HttpNetworkTransactionTest, ReadPipelineEvictionFallback) { |
| 9213 | MockRead data_reads1[] = { |
| 9214 | MockRead(false, ERR_PIPELINE_EVICTION), |
| 9215 | }; |
| 9216 | MockRead data_reads2[] = { |
| 9217 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 9218 | MockRead("hello world"), |
| 9219 | MockRead(false, OK), |
| 9220 | }; |
| 9221 | StaticSocketDataProvider data1(data_reads1, arraysize(data_reads1), NULL, 0); |
| 9222 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), NULL, 0); |
| 9223 | StaticSocketDataProvider* data[] = { &data1, &data2 }; |
| 9224 | |
| 9225 | SimpleGetHelperResult out = SimpleGetHelperForData(data, arraysize(data)); |
| 9226 | |
| 9227 | EXPECT_EQ(OK, out.rv); |
| 9228 | EXPECT_EQ("HTTP/1.0 200 OK", out.status_line); |
| 9229 | EXPECT_EQ("hello world", out.response_data); |
| 9230 | } |
| 9231 | |
| 9232 | TEST_F(HttpNetworkTransactionTest, SendPipelineEvictionFallback) { |
| 9233 | MockWrite data_writes1[] = { |
| 9234 | MockWrite(false, ERR_PIPELINE_EVICTION), |
| 9235 | }; |
| 9236 | MockWrite data_writes2[] = { |
| 9237 | MockWrite("GET / HTTP/1.1\r\n" |
| 9238 | "Host: www.google.com\r\n" |
| 9239 | "Connection: keep-alive\r\n\r\n"), |
| 9240 | }; |
| 9241 | MockRead data_reads2[] = { |
| 9242 | MockRead("HTTP/1.0 200 OK\r\n\r\n"), |
| 9243 | MockRead("hello world"), |
| 9244 | MockRead(false, OK), |
| 9245 | }; |
| 9246 | StaticSocketDataProvider data1(NULL, 0, |
| 9247 | data_writes1, arraysize(data_writes1)); |
| 9248 | StaticSocketDataProvider data2(data_reads2, arraysize(data_reads2), |
| 9249 | data_writes2, arraysize(data_writes2)); |
| 9250 | StaticSocketDataProvider* data[] = { &data1, &data2 }; |
| 9251 | |
| 9252 | SimpleGetHelperResult out = SimpleGetHelperForData(data, arraysize(data)); |
| 9253 | |
| 9254 | EXPECT_EQ(OK, out.rv); |
| 9255 | EXPECT_EQ("HTTP/1.0 200 OK", out.status_line); |
| 9256 | EXPECT_EQ("hello world", out.response_data); |
| 9257 | } |
| 9258 | |
[email protected] | 89ceba9a | 2009-03-21 03:46:06 | [diff] [blame] | 9259 | } // namespace net |