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