blob: da8942239cae21813ff0e288273a9b1d88db402f [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// 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.commit586acc5fe2008-07-26 22:42:524
[email protected]77848d12008-11-14 00:00:225#include <math.h> // ceil
6
[email protected]68bf9152008-09-25 19:47:307#include "base/compiler_specific.h"
initial.commit586acc5fe2008-07-26 22:42:528#include "net/base/client_socket_factory.h"
[email protected]bacff652009-03-31 17:50:339#include "net/base/completion_callback.h"
[email protected]ff007e162009-05-23 09:13:1510#include "net/base/socket_test_util.h"
[email protected]bacff652009-03-31 17:50:3311#include "net/base/ssl_client_socket.h"
12#include "net/base/ssl_info.h"
initial.commit586acc5fe2008-07-26 22:42:5213#include "net/base/test_completion_callback.h"
14#include "net/base/upload_data.h"
[email protected]385a4672009-03-11 22:21:2915#include "net/http/http_auth_handler_ntlm.h"
initial.commit586acc5fe2008-07-26 22:42:5216#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]51fff29d2008-12-19 22:17:5319#include "net/proxy/proxy_config_service_fixed.h"
initial.commit586acc5fe2008-07-26 22:42:5220#include "testing/gtest/include/gtest/gtest.h"
[email protected]23887f04f2008-12-02 19:20:1521#include "testing/platform_test.h"
initial.commit586acc5fe2008-07-26 22:42:5222
23//-----------------------------------------------------------------------------
24
[email protected]89ceba9a2009-03-21 03:46:0625namespace net {
26
[email protected]db8f44c2008-12-13 04:52:0127// Create a proxy service which fails on all requests (falls back to direct).
[email protected]1c773ea12009-04-28 19:58:4228ProxyService* CreateNullProxyService() {
29 return ProxyService::CreateNull();
initial.commit586acc5fe2008-07-26 22:42:5230}
31
[email protected]1c773ea12009-04-28 19:58:4232ProxyService* CreateFixedProxyService(const std::string& proxy) {
[email protected]ab501a6a2009-05-12 15:07:5033 net::ProxyConfig proxy_config;
34 proxy_config.proxy_rules.ParseFromString(proxy);
[email protected]3e44697f2009-05-22 14:37:3935 return ProxyService::CreateFixed(proxy_config);
[email protected]51fff29d2008-12-19 22:17:5336}
37
38
[email protected]d207a5f2009-06-04 05:28:4039HttpNetworkSession* CreateSession(ProxyService* proxy_service,
40 ClientSocketFactory* client_socket_factory) {
41 return new HttpNetworkSession(proxy_service, client_socket_factory);
[email protected]e8d536192008-10-17 22:21:1442}
43
[email protected]89836e22008-09-25 20:33:4244class HttpNetworkTransactionTest : public PlatformTest {
initial.commit586acc5fe2008-07-26 22:42:5245 public:
[email protected]0e75a732008-10-16 20:36:0946 virtual void TearDown() {
47 // Empty the current queue.
48 MessageLoop::current()->RunAllPending();
49 PlatformTest::TearDown();
50 }
51
[email protected]3d2a59b2008-09-26 19:44:2552 protected:
53 void KeepAliveConnectionResendRequestTest(const MockRead& read_failure);
initial.commit586acc5fe2008-07-26 22:42:5254
[email protected]ff007e162009-05-23 09:13:1555 struct SimpleGetHelperResult {
56 int rv;
57 std::string status_line;
58 std::string response_data;
59 };
initial.commit586acc5fe2008-07-26 22:42:5260
[email protected]ff007e162009-05-23 09:13:1561 SimpleGetHelperResult SimpleGetHelper(MockRead data_reads[]) {
62 SimpleGetHelperResult out;
initial.commit586acc5fe2008-07-26 22:42:5263
[email protected]ff007e162009-05-23 09:13:1564 scoped_ptr<ProxyService> proxy_service(CreateNullProxyService());
[email protected]d207a5f2009-06-04 05:28:4065 scoped_ptr<HttpTransaction> trans(
66 new HttpNetworkTransaction(
67 CreateSession(proxy_service.get(), &mock_socket_factory_),
68 &mock_socket_factory_));
initial.commit586acc5fe2008-07-26 22:42:5269
[email protected]ff007e162009-05-23 09:13:1570 HttpRequestInfo request;
71 request.method = "GET";
72 request.url = GURL("http://www.google.com/");
73 request.load_flags = 0;
initial.commit586acc5fe2008-07-26 22:42:5274
[email protected]ff007e162009-05-23 09:13:1575 MockSocket data;
76 data.reads = data_reads;
77 mock_socket_factory_.AddMockSocket(&data);
initial.commit586acc5fe2008-07-26 22:42:5278
[email protected]ff007e162009-05-23 09:13:1579 TestCompletionCallback callback;
initial.commit586acc5fe2008-07-26 22:42:5280
[email protected]ff007e162009-05-23 09:13:1581 int rv = trans->Start(&request, &callback);
82 EXPECT_EQ(ERR_IO_PENDING, rv);
initial.commit586acc5fe2008-07-26 22:42:5283
[email protected]ff007e162009-05-23 09:13:1584 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]aecfbf22008-10-16 02:02:4797 return out;
[email protected]ff007e162009-05-23 09:13:1598 }
initial.commit586acc5fe2008-07-26 22:42:5299
[email protected]ff007e162009-05-23 09:13:15100 void ConnectStatusHelperWithExpectedStatus(const MockRead& status,
101 int expected_status);
initial.commit586acc5fe2008-07-26 22:42:52102
[email protected]ff007e162009-05-23 09:13:15103 void ConnectStatusHelper(const MockRead& status);
initial.commit586acc5fe2008-07-26 22:42:52104
[email protected]ff007e162009-05-23 09:13:15105 MockClientSocketFactory mock_socket_factory_;
106};
[email protected]231d5a32008-09-13 00:45:27107
[email protected]15a5ccf82008-10-23 19:57:43108// Fill |str| with a long header list that consumes >= |size| bytes.
109void FillLargeHeadersString(std::string* str, int size) {
[email protected]4ddaf2502008-10-23 18:26:19110 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]15a5ccf82008-10-23 19:57:43117 str->reserve(sizeof_data);
[email protected]372d34a2008-11-05 21:30:51118
[email protected]4ddaf2502008-10-23 18:26:19119 for (int i = 0; i < num_rows; ++i)
[email protected]15a5ccf82008-10-23 19:57:43120 str->append(row, sizeof_row);
[email protected]4ddaf2502008-10-23 18:26:19121}
122
[email protected]385a4672009-03-11 22:21:29123// Alternative functions that eliminate randomness and dependency on the local
124// host name so that the generated NTLM messages are reproducible.
[email protected]fe2bc6a2009-03-23 16:52:20125void MockGenerateRandom1(uint8* output, size_t n) {
[email protected]385a4672009-03-11 22:21:29126 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]fe2bc6a2009-03-23 16:52:20136void MockGenerateRandom2(uint8* output, size_t n) {
[email protected]385a4672009-03-11 22:21:29137 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]fe2bc6a2009-03-23 16:52:20148std::string MockGetHostName() {
149 return "WTC-WIN7";
[email protected]385a4672009-03-11 22:21:29150}
151
[email protected]231d5a32008-09-13 00:45:27152//-----------------------------------------------------------------------------
153
154TEST_F(HttpNetworkTransactionTest, Basic) {
[email protected]1c773ea12009-04-28 19:58:42155 scoped_ptr<ProxyService> proxy_service(CreateNullProxyService());
[email protected]d207a5f2009-06-04 05:28:40156 scoped_ptr<HttpTransaction> trans(
157 new HttpNetworkTransaction(
158 CreateSession(proxy_service.get(), &mock_socket_factory_),
159 &mock_socket_factory_));
[email protected]231d5a32008-09-13 00:45:27160}
161
162TEST_F(HttpNetworkTransactionTest, SimpleGET) {
163 MockRead data_reads[] = {
[email protected]217e6022008-09-29 18:18:35164 MockRead("HTTP/1.0 200 OK\r\n\r\n"),
165 MockRead("hello world"),
[email protected]1c773ea12009-04-28 19:58:42166 MockRead(false, OK),
[email protected]231d5a32008-09-13 00:45:27167 };
[email protected]231d5a32008-09-13 00:45:27168 SimpleGetHelperResult out = SimpleGetHelper(data_reads);
[email protected]1c773ea12009-04-28 19:58:42169 EXPECT_EQ(OK, out.rv);
[email protected]231d5a32008-09-13 00:45:27170 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.
175TEST_F(HttpNetworkTransactionTest, SimpleGETNoHeaders) {
176 MockRead data_reads[] = {
[email protected]217e6022008-09-29 18:18:35177 MockRead("hello world"),
[email protected]1c773ea12009-04-28 19:58:42178 MockRead(false, OK),
[email protected]231d5a32008-09-13 00:45:27179 };
[email protected]231d5a32008-09-13 00:45:27180 SimpleGetHelperResult out = SimpleGetHelper(data_reads);
[email protected]1c773ea12009-04-28 19:58:42181 EXPECT_EQ(OK, out.rv);
[email protected]231d5a32008-09-13 00:45:27182 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.
187TEST_F(HttpNetworkTransactionTest, StatusLineJunk2Bytes) {
188 MockRead data_reads[] = {
[email protected]217e6022008-09-29 18:18:35189 MockRead("xxxHTTP/1.0 404 Not Found\nServer: blah\n\nDATA"),
[email protected]1c773ea12009-04-28 19:58:42190 MockRead(false, OK),
[email protected]231d5a32008-09-13 00:45:27191 };
192 SimpleGetHelperResult out = SimpleGetHelper(data_reads);
[email protected]1c773ea12009-04-28 19:58:42193 EXPECT_EQ(OK, out.rv);
[email protected]231d5a32008-09-13 00:45:27194 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.
199TEST_F(HttpNetworkTransactionTest, StatusLineJunk4Bytes) {
200 MockRead data_reads[] = {
[email protected]217e6022008-09-29 18:18:35201 MockRead("\n\nQJHTTP/1.0 404 Not Found\nServer: blah\n\nDATA"),
[email protected]1c773ea12009-04-28 19:58:42202 MockRead(false, OK),
[email protected]231d5a32008-09-13 00:45:27203 };
204 SimpleGetHelperResult out = SimpleGetHelper(data_reads);
[email protected]1c773ea12009-04-28 19:58:42205 EXPECT_EQ(OK, out.rv);
[email protected]231d5a32008-09-13 00:45:27206 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.
211TEST_F(HttpNetworkTransactionTest, StatusLineJunk5Bytes) {
212 MockRead data_reads[] = {
[email protected]217e6022008-09-29 18:18:35213 MockRead("xxxxxHTTP/1.1 404 Not Found\nServer: blah"),
[email protected]1c773ea12009-04-28 19:58:42214 MockRead(false, OK),
[email protected]231d5a32008-09-13 00:45:27215 };
216 SimpleGetHelperResult out = SimpleGetHelper(data_reads);
[email protected]1c773ea12009-04-28 19:58:42217 EXPECT_EQ(OK, out.rv);
[email protected]3d2a59b2008-09-26 19:44:25218 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]231d5a32008-09-13 00:45:27220}
221
222// Same as StatusLineJunk4Bytes, except the read chunks are smaller.
223TEST_F(HttpNetworkTransactionTest, StatusLineJunk4Bytes_Slow) {
224 MockRead data_reads[] = {
[email protected]217e6022008-09-29 18:18:35225 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]1c773ea12009-04-28 19:58:42230 MockRead(false, OK),
[email protected]231d5a32008-09-13 00:45:27231 };
232 SimpleGetHelperResult out = SimpleGetHelper(data_reads);
[email protected]1c773ea12009-04-28 19:58:42233 EXPECT_EQ(OK, out.rv);
[email protected]231d5a32008-09-13 00:45:27234 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.
239TEST_F(HttpNetworkTransactionTest, StatusLinePartial) {
240 MockRead data_reads[] = {
[email protected]217e6022008-09-29 18:18:35241 MockRead("HTT"),
[email protected]1c773ea12009-04-28 19:58:42242 MockRead(false, OK),
[email protected]231d5a32008-09-13 00:45:27243 };
244 SimpleGetHelperResult out = SimpleGetHelper(data_reads);
[email protected]1c773ea12009-04-28 19:58:42245 EXPECT_EQ(OK, out.rv);
[email protected]231d5a32008-09-13 00:45:27246 EXPECT_EQ("HTTP/0.9 200 OK", out.status_line);
247 EXPECT_EQ("HTT", out.response_data);
initial.commit586acc5fe2008-07-26 22:42:52248}
249
[email protected]f9d44aa2008-09-23 23:57:17250// 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.
253TEST_F(HttpNetworkTransactionTest, StopsReading204) {
254 MockRead data_reads[] = {
[email protected]217e6022008-09-29 18:18:35255 MockRead("HTTP/1.1 204 No Content\r\n\r\n"),
256 MockRead("junk"), // Should not be read!!
[email protected]1c773ea12009-04-28 19:58:42257 MockRead(false, OK),
[email protected]f9d44aa2008-09-23 23:57:17258 };
259 SimpleGetHelperResult out = SimpleGetHelper(data_reads);
[email protected]1c773ea12009-04-28 19:58:42260 EXPECT_EQ(OK, out.rv);
[email protected]f9d44aa2008-09-23 23:57:17261 EXPECT_EQ("HTTP/1.1 204 No Content", out.status_line);
262 EXPECT_EQ("", out.response_data);
263}
264
[email protected]ef0faf2e72009-03-05 23:27:23265// Do a request using the HEAD method. Verify that we don't try to read the
266// message body (since HEAD has none).
267TEST_F(HttpNetworkTransactionTest, Head) {
[email protected]1c773ea12009-04-28 19:58:42268 scoped_ptr<ProxyService> proxy_service(CreateNullProxyService());
[email protected]d207a5f2009-06-04 05:28:40269 scoped_ptr<HttpTransaction> trans(
270 new HttpNetworkTransaction(
271 CreateSession(proxy_service.get(), &mock_socket_factory_),
272 &mock_socket_factory_));
[email protected]ef0faf2e72009-03-05 23:27:23273
[email protected]1c773ea12009-04-28 19:58:42274 HttpRequestInfo request;
[email protected]ef0faf2e72009-03-05 23:27:23275 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]1c773ea12009-04-28 19:58:42291 MockRead(false, ERR_UNEXPECTED), // Should not be reached.
[email protected]ef0faf2e72009-03-05 23:27:23292 };
293
294 MockSocket data1;
295 data1.reads = data_reads1;
296 data1.writes = data_writes1;
[email protected]ff007e162009-05-23 09:13:15297 mock_socket_factory_.AddMockSocket(&data1);
[email protected]ef0faf2e72009-03-05 23:27:23298
299 TestCompletionCallback callback1;
300
301 int rv = trans->Start(&request, &callback1);
[email protected]1c773ea12009-04-28 19:58:42302 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]ef0faf2e72009-03-05 23:27:23303
304 rv = callback1.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:42305 EXPECT_EQ(OK, rv);
[email protected]ef0faf2e72009-03-05 23:27:23306
[email protected]1c773ea12009-04-28 19:58:42307 const HttpResponseInfo* response = trans->GetResponseInfo();
[email protected]ef0faf2e72009-03-05 23:27:23308 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]1c773ea12009-04-28 19:58:42326 EXPECT_EQ(OK, rv);
[email protected]ef0faf2e72009-03-05 23:27:23327 EXPECT_EQ("", response_data);
328}
329
initial.commit586acc5fe2008-07-26 22:42:52330TEST_F(HttpNetworkTransactionTest, ReuseConnection) {
[email protected]1c773ea12009-04-28 19:58:42331 scoped_ptr<ProxyService> proxy_service(CreateNullProxyService());
332 scoped_refptr<HttpNetworkSession> session =
[email protected]d207a5f2009-06-04 05:28:40333 CreateSession(proxy_service.get(), &mock_socket_factory_);
initial.commit586acc5fe2008-07-26 22:42:52334
335 MockRead data_reads[] = {
[email protected]217e6022008-09-29 18:18:35336 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]1c773ea12009-04-28 19:58:42340 MockRead(false, OK),
initial.commit586acc5fe2008-07-26 22:42:52341 };
342 MockSocket data;
initial.commit586acc5fe2008-07-26 22:42:52343 data.reads = data_reads;
[email protected]ff007e162009-05-23 09:13:15344 mock_socket_factory_.AddMockSocket(&data);
initial.commit586acc5fe2008-07-26 22:42:52345
346 const char* kExpectedResponseData[] = {
347 "hello", "world"
348 };
349
350 for (int i = 0; i < 2; ++i) {
[email protected]1c773ea12009-04-28 19:58:42351 scoped_ptr<HttpTransaction> trans(
[email protected]ff007e162009-05-23 09:13:15352 new HttpNetworkTransaction(session, &mock_socket_factory_));
initial.commit586acc5fe2008-07-26 22:42:52353
[email protected]1c773ea12009-04-28 19:58:42354 HttpRequestInfo request;
initial.commit586acc5fe2008-07-26 22:42:52355 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]1c773ea12009-04-28 19:58:42362 EXPECT_EQ(ERR_IO_PENDING, rv);
initial.commit586acc5fe2008-07-26 22:42:52363
364 rv = callback.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:42365 EXPECT_EQ(OK, rv);
initial.commit586acc5fe2008-07-26 22:42:52366
[email protected]1c773ea12009-04-28 19:58:42367 const HttpResponseInfo* response = trans->GetResponseInfo();
initial.commit586acc5fe2008-07-26 22:42:52368 EXPECT_TRUE(response != NULL);
369
370 EXPECT_TRUE(response->headers != NULL);
[email protected]3d2a59b2008-09-26 19:44:25371 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine());
initial.commit586acc5fe2008-07-26 22:42:52372
373 std::string response_data;
[email protected]af4876d2008-10-21 23:10:57374 rv = ReadTransaction(trans.get(), &response_data);
[email protected]1c773ea12009-04-28 19:58:42375 EXPECT_EQ(OK, rv);
[email protected]3d2a59b2008-09-26 19:44:25376 EXPECT_EQ(kExpectedResponseData[i], response_data);
initial.commit586acc5fe2008-07-26 22:42:52377 }
378}
379
380TEST_F(HttpNetworkTransactionTest, Ignores100) {
[email protected]1c773ea12009-04-28 19:58:42381 scoped_ptr<ProxyService> proxy_service(CreateNullProxyService());
[email protected]d207a5f2009-06-04 05:28:40382 scoped_ptr<HttpTransaction> trans(
383 new HttpNetworkTransaction(
384 CreateSession(proxy_service.get(), &mock_socket_factory_),
385 &mock_socket_factory_));
initial.commit586acc5fe2008-07-26 22:42:52386
[email protected]1c773ea12009-04-28 19:58:42387 HttpRequestInfo request;
initial.commit586acc5fe2008-07-26 22:42:52388 request.method = "POST";
389 request.url = GURL("http://www.foo.com/");
[email protected]1c773ea12009-04-28 19:58:42390 request.upload_data = new UploadData;
initial.commit586acc5fe2008-07-26 22:42:52391 request.upload_data->AppendBytes("foo", 3);
392 request.load_flags = 0;
393
394 MockRead data_reads[] = {
[email protected]217e6022008-09-29 18:18:35395 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]1c773ea12009-04-28 19:58:42398 MockRead(false, OK),
initial.commit586acc5fe2008-07-26 22:42:52399 };
400 MockSocket data;
initial.commit586acc5fe2008-07-26 22:42:52401 data.reads = data_reads;
[email protected]ff007e162009-05-23 09:13:15402 mock_socket_factory_.AddMockSocket(&data);
initial.commit586acc5fe2008-07-26 22:42:52403
404 TestCompletionCallback callback;
405
406 int rv = trans->Start(&request, &callback);
[email protected]1c773ea12009-04-28 19:58:42407 EXPECT_EQ(ERR_IO_PENDING, rv);
initial.commit586acc5fe2008-07-26 22:42:52408
409 rv = callback.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:42410 EXPECT_EQ(OK, rv);
initial.commit586acc5fe2008-07-26 22:42:52411
[email protected]1c773ea12009-04-28 19:58:42412 const HttpResponseInfo* response = trans->GetResponseInfo();
initial.commit586acc5fe2008-07-26 22:42:52413 EXPECT_TRUE(response != NULL);
414
415 EXPECT_TRUE(response->headers != NULL);
[email protected]3d2a59b2008-09-26 19:44:25416 EXPECT_EQ("HTTP/1.0 200 OK", response->headers->GetStatusLine());
initial.commit586acc5fe2008-07-26 22:42:52417
418 std::string response_data;
[email protected]af4876d2008-10-21 23:10:57419 rv = ReadTransaction(trans.get(), &response_data);
[email protected]1c773ea12009-04-28 19:58:42420 EXPECT_EQ(OK, rv);
[email protected]3d2a59b2008-09-26 19:44:25421 EXPECT_EQ("hello world", response_data);
initial.commit586acc5fe2008-07-26 22:42:52422}
423
[email protected]3a2d3662009-03-27 03:49:14424// 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.
427TEST_F(HttpNetworkTransactionTest, Ignores1xx) {
[email protected]1c773ea12009-04-28 19:58:42428 scoped_ptr<ProxyService> proxy_service(CreateNullProxyService());
[email protected]d207a5f2009-06-04 05:28:40429 scoped_ptr<HttpTransaction> trans(
430 new HttpNetworkTransaction(
431 CreateSession(proxy_service.get(), &mock_socket_factory_),
432 &mock_socket_factory_));
[email protected]3a2d3662009-03-27 03:49:14433
[email protected]1c773ea12009-04-28 19:58:42434 HttpRequestInfo request;
[email protected]3a2d3662009-03-27 03:49:14435 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]1c773ea12009-04-28 19:58:42443 MockRead(false, OK),
[email protected]3a2d3662009-03-27 03:49:14444 };
445 MockSocket data;
446 data.reads = data_reads;
[email protected]ff007e162009-05-23 09:13:15447 mock_socket_factory_.AddMockSocket(&data);
[email protected]3a2d3662009-03-27 03:49:14448
449 TestCompletionCallback callback;
450
451 int rv = trans->Start(&request, &callback);
[email protected]1c773ea12009-04-28 19:58:42452 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]3a2d3662009-03-27 03:49:14453
454 rv = callback.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:42455 EXPECT_EQ(OK, rv);
[email protected]3a2d3662009-03-27 03:49:14456
[email protected]1c773ea12009-04-28 19:58:42457 const HttpResponseInfo* response = trans->GetResponseInfo();
[email protected]3a2d3662009-03-27 03:49:14458 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]1c773ea12009-04-28 19:58:42465 EXPECT_EQ(OK, rv);
[email protected]3a2d3662009-03-27 03:49:14466 EXPECT_EQ("hello world", response_data);
467}
468
[email protected]3d2a59b2008-09-26 19:44:25469// read_failure specifies a read failure that should cause the network
470// transaction to resend the request.
471void HttpNetworkTransactionTest::KeepAliveConnectionResendRequestTest(
472 const MockRead& read_failure) {
[email protected]1c773ea12009-04-28 19:58:42473 scoped_ptr<ProxyService> proxy_service(CreateNullProxyService());
474 scoped_refptr<HttpNetworkSession> session =
[email protected]d207a5f2009-06-04 05:28:40475 CreateSession(proxy_service.get(), &mock_socket_factory_);
initial.commit586acc5fe2008-07-26 22:42:52476
[email protected]1c773ea12009-04-28 19:58:42477 HttpRequestInfo request;
initial.commit586acc5fe2008-07-26 22:42:52478 request.method = "GET";
479 request.url = GURL("http://www.foo.com/");
480 request.load_flags = 0;
481
482 MockRead data1_reads[] = {
[email protected]217e6022008-09-29 18:18:35483 MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"),
484 MockRead("hello"),
[email protected]3d2a59b2008-09-26 19:44:25485 read_failure, // Now, we reuse the connection and fail the first read.
initial.commit586acc5fe2008-07-26 22:42:52486 };
487 MockSocket data1;
initial.commit586acc5fe2008-07-26 22:42:52488 data1.reads = data1_reads;
[email protected]ff007e162009-05-23 09:13:15489 mock_socket_factory_.AddMockSocket(&data1);
initial.commit586acc5fe2008-07-26 22:42:52490
491 MockRead data2_reads[] = {
[email protected]217e6022008-09-29 18:18:35492 MockRead("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\n"),
493 MockRead("world"),
[email protected]1c773ea12009-04-28 19:58:42494 MockRead(true, OK),
initial.commit586acc5fe2008-07-26 22:42:52495 };
496 MockSocket data2;
initial.commit586acc5fe2008-07-26 22:42:52497 data2.reads = data2_reads;
[email protected]ff007e162009-05-23 09:13:15498 mock_socket_factory_.AddMockSocket(&data2);
initial.commit586acc5fe2008-07-26 22:42:52499
500 const char* kExpectedResponseData[] = {
501 "hello", "world"
502 };
503
504 for (int i = 0; i < 2; ++i) {
505 TestCompletionCallback callback;
506
[email protected]1c773ea12009-04-28 19:58:42507 scoped_ptr<HttpTransaction> trans(
[email protected]ff007e162009-05-23 09:13:15508 new HttpNetworkTransaction(session, &mock_socket_factory_));
initial.commit586acc5fe2008-07-26 22:42:52509
510 int rv = trans->Start(&request, &callback);
[email protected]1c773ea12009-04-28 19:58:42511 EXPECT_EQ(ERR_IO_PENDING, rv);
initial.commit586acc5fe2008-07-26 22:42:52512
513 rv = callback.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:42514 EXPECT_EQ(OK, rv);
initial.commit586acc5fe2008-07-26 22:42:52515
[email protected]1c773ea12009-04-28 19:58:42516 const HttpResponseInfo* response = trans->GetResponseInfo();
initial.commit586acc5fe2008-07-26 22:42:52517 EXPECT_TRUE(response != NULL);
518
519 EXPECT_TRUE(response->headers != NULL);
[email protected]3d2a59b2008-09-26 19:44:25520 EXPECT_EQ("HTTP/1.1 200 OK", response->headers->GetStatusLine());
initial.commit586acc5fe2008-07-26 22:42:52521
522 std::string response_data;
[email protected]af4876d2008-10-21 23:10:57523 rv = ReadTransaction(trans.get(), &response_data);
[email protected]1c773ea12009-04-28 19:58:42524 EXPECT_EQ(OK, rv);
[email protected]3d2a59b2008-09-26 19:44:25525 EXPECT_EQ(kExpectedResponseData[i], response_data);
initial.commit586acc5fe2008-07-26 22:42:52526 }
527}
[email protected]3d2a59b2008-09-26 19:44:25528
529TEST_F(HttpNetworkTransactionTest, KeepAliveConnectionReset) {
[email protected]1c773ea12009-04-28 19:58:42530 MockRead read_failure(true, ERR_CONNECTION_RESET);
[email protected]3d2a59b2008-09-26 19:44:25531 KeepAliveConnectionResendRequestTest(read_failure);
532}
533
534TEST_F(HttpNetworkTransactionTest, KeepAliveConnectionEOF) {
[email protected]1c773ea12009-04-28 19:58:42535 MockRead read_failure(false, OK); // EOF
[email protected]3d2a59b2008-09-26 19:44:25536 KeepAliveConnectionResendRequestTest(read_failure);
537}
538
539TEST_F(HttpNetworkTransactionTest, NonKeepAliveConnectionReset) {
[email protected]1c773ea12009-04-28 19:58:42540 scoped_ptr<ProxyService> proxy_service(CreateNullProxyService());
[email protected]d207a5f2009-06-04 05:28:40541 scoped_ptr<HttpTransaction> trans(
542 new HttpNetworkTransaction(
543 CreateSession(proxy_service.get(), &mock_socket_factory_),
544 &mock_socket_factory_));
[email protected]3d2a59b2008-09-26 19:44:25545
[email protected]1c773ea12009-04-28 19:58:42546 HttpRequestInfo request;
[email protected]3d2a59b2008-09-26 19:44:25547 request.method = "GET";
548 request.url = GURL("http://www.google.com/");
549 request.load_flags = 0;
550
551 MockRead data_reads[] = {
[email protected]1c773ea12009-04-28 19:58:42552 MockRead(true, ERR_CONNECTION_RESET),
[email protected]217e6022008-09-29 18:18:35553 MockRead("HTTP/1.0 200 OK\r\n\r\n"), // Should not be used
554 MockRead("hello world"),
[email protected]1c773ea12009-04-28 19:58:42555 MockRead(false, OK),
[email protected]3d2a59b2008-09-26 19:44:25556 };
557 MockSocket data;
[email protected]3d2a59b2008-09-26 19:44:25558 data.reads = data_reads;
[email protected]ff007e162009-05-23 09:13:15559 mock_socket_factory_.AddMockSocket(&data);
[email protected]3d2a59b2008-09-26 19:44:25560
561 TestCompletionCallback callback;
562
563 int rv = trans->Start(&request, &callback);
[email protected]1c773ea12009-04-28 19:58:42564 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]3d2a59b2008-09-26 19:44:25565
566 rv = callback.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:42567 EXPECT_EQ(ERR_CONNECTION_RESET, rv);
[email protected]3d2a59b2008-09-26 19:44:25568
[email protected]1c773ea12009-04-28 19:58:42569 const HttpResponseInfo* response = trans->GetResponseInfo();
[email protected]3d2a59b2008-09-26 19:44:25570 EXPECT_TRUE(response == NULL);
[email protected]3d2a59b2008-09-26 19:44:25571}
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]1c773ea12009-04-28 19:58:42580// Us with WinHTTP: error page (ERR_INVALID_RESPONSE)
581// Us: error page (EMPTY_RESPONSE)
[email protected]3d2a59b2008-09-26 19:44:25582TEST_F(HttpNetworkTransactionTest, NonKeepAliveConnectionEOF) {
583 MockRead data_reads[] = {
[email protected]1c773ea12009-04-28 19:58:42584 MockRead(false, OK), // EOF
[email protected]217e6022008-09-29 18:18:35585 MockRead("HTTP/1.0 200 OK\r\n\r\n"), // Should not be used
586 MockRead("hello world"),
[email protected]1c773ea12009-04-28 19:58:42587 MockRead(false, OK),
[email protected]3d2a59b2008-09-26 19:44:25588 };
589 SimpleGetHelperResult out = SimpleGetHelper(data_reads);
[email protected]1c773ea12009-04-28 19:58:42590 EXPECT_EQ(ERR_EMPTY_RESPONSE, out.rv);
[email protected]3d2a59b2008-09-26 19:44:25591}
[email protected]038e9a32008-10-08 22:40:16592
593// Test the request-challenge-retry sequence for basic auth.
594// (basic auth is the easiest to mock, because it has no randomness).
595TEST_F(HttpNetworkTransactionTest, BasicAuth) {
[email protected]1c773ea12009-04-28 19:58:42596 scoped_ptr<ProxyService> proxy_service(CreateNullProxyService());
[email protected]d207a5f2009-06-04 05:28:40597 scoped_ptr<HttpTransaction> trans(
598 new HttpNetworkTransaction(
599 CreateSession(proxy_service.get(), &mock_socket_factory_),
600 &mock_socket_factory_));
[email protected]038e9a32008-10-08 22:40:16601
[email protected]1c773ea12009-04-28 19:58:42602 HttpRequestInfo request;
[email protected]038e9a32008-10-08 22:40:16603 request.method = "GET";
604 request.url = GURL("http://www.google.com/");
605 request.load_flags = 0;
606
[email protected]f9ee6b52008-11-08 06:46:23607 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]038e9a32008-10-08 22:40:16613 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]aaead502008-10-15 00:20:11617 MockRead("WWW-Authenticate: Basic\r\n"), // Malformed
[email protected]038e9a32008-10-08 22:40:16618 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]1c773ea12009-04-28 19:58:42623 MockRead(false, ERR_FAILED),
[email protected]038e9a32008-10-08 22:40:16624 };
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]1c773ea12009-04-28 19:58:42640 MockRead(false, OK),
[email protected]038e9a32008-10-08 22:40:16641 };
642
643 MockSocket data1;
644 data1.reads = data_reads1;
[email protected]f9ee6b52008-11-08 06:46:23645 data1.writes = data_writes1;
[email protected]038e9a32008-10-08 22:40:16646 MockSocket data2;
647 data2.reads = data_reads2;
648 data2.writes = data_writes2;
[email protected]ff007e162009-05-23 09:13:15649 mock_socket_factory_.AddMockSocket(&data1);
650 mock_socket_factory_.AddMockSocket(&data2);
[email protected]038e9a32008-10-08 22:40:16651
652 TestCompletionCallback callback1;
653
654 int rv = trans->Start(&request, &callback1);
[email protected]1c773ea12009-04-28 19:58:42655 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]038e9a32008-10-08 22:40:16656
657 rv = callback1.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:42658 EXPECT_EQ(OK, rv);
[email protected]038e9a32008-10-08 22:40:16659
[email protected]1c773ea12009-04-28 19:58:42660 const HttpResponseInfo* response = trans->GetResponseInfo();
[email protected]038e9a32008-10-08 22:40:16661 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]71e4573a2009-05-21 22:03:00666 EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port);
[email protected]038e9a32008-10-08 22:40:16667 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]1c773ea12009-04-28 19:58:42673 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]038e9a32008-10-08 22:40:16674
675 rv = callback2.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:42676 EXPECT_EQ(OK, rv);
[email protected]038e9a32008-10-08 22:40:16677
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]038e9a32008-10-08 22:40:16682}
683
[email protected]2d2697f92009-02-18 21:00:32684// Test the request-challenge-retry sequence for basic auth, over a keep-alive
685// connection.
686TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAlive) {
[email protected]1c773ea12009-04-28 19:58:42687 scoped_ptr<ProxyService> proxy_service(CreateNullProxyService());
[email protected]d207a5f2009-06-04 05:28:40688 scoped_ptr<HttpTransaction> trans(
689 new HttpNetworkTransaction(
690 CreateSession(proxy_service.get(), &mock_socket_factory_),
691 &mock_socket_factory_));
[email protected]2d2697f92009-02-18 21:00:32692
[email protected]1c773ea12009-04-28 19:58:42693 HttpRequestInfo request;
[email protected]2d2697f92009-02-18 21:00:32694 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]1c773ea12009-04-28 19:58:42722 MockRead(false, OK),
[email protected]2d2697f92009-02-18 21:00:32723 };
724
725 MockSocket data1;
726 data1.reads = data_reads1;
727 data1.writes = data_writes1;
[email protected]ff007e162009-05-23 09:13:15728 mock_socket_factory_.AddMockSocket(&data1);
[email protected]2d2697f92009-02-18 21:00:32729
730 TestCompletionCallback callback1;
731
732 int rv = trans->Start(&request, &callback1);
[email protected]1c773ea12009-04-28 19:58:42733 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]2d2697f92009-02-18 21:00:32734
735 rv = callback1.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:42736 EXPECT_EQ(OK, rv);
[email protected]2d2697f92009-02-18 21:00:32737
[email protected]1c773ea12009-04-28 19:58:42738 const HttpResponseInfo* response = trans->GetResponseInfo();
[email protected]2d2697f92009-02-18 21:00:32739 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]71e4573a2009-05-21 22:03:00744 EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port);
[email protected]2d2697f92009-02-18 21:00:32745 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]1c773ea12009-04-28 19:58:42751 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]2d2697f92009-02-18 21:00:32752
753 rv = callback2.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:42754 EXPECT_EQ(OK, rv);
[email protected]2d2697f92009-02-18 21:00:32755
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.
764TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAliveNoBody) {
[email protected]1c773ea12009-04-28 19:58:42765 scoped_ptr<ProxyService> proxy_service(CreateNullProxyService());
[email protected]d207a5f2009-06-04 05:28:40766 scoped_ptr<HttpTransaction> trans(
767 new HttpNetworkTransaction(
768 CreateSession(proxy_service.get(), &mock_socket_factory_),
769 &mock_socket_factory_));
[email protected]2d2697f92009-02-18 21:00:32770
[email protected]1c773ea12009-04-28 19:58:42771 HttpRequestInfo request;
[email protected]2d2697f92009-02-18 21:00:32772 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]1c773ea12009-04-28 19:58:42803 MockRead(false, OK),
[email protected]2d2697f92009-02-18 21:00:32804 };
805
806 MockSocket data1;
807 data1.reads = data_reads1;
808 data1.writes = data_writes1;
[email protected]ff007e162009-05-23 09:13:15809 mock_socket_factory_.AddMockSocket(&data1);
[email protected]2d2697f92009-02-18 21:00:32810
811 TestCompletionCallback callback1;
812
813 int rv = trans->Start(&request, &callback1);
[email protected]1c773ea12009-04-28 19:58:42814 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]2d2697f92009-02-18 21:00:32815
816 rv = callback1.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:42817 EXPECT_EQ(OK, rv);
[email protected]2d2697f92009-02-18 21:00:32818
[email protected]1c773ea12009-04-28 19:58:42819 const HttpResponseInfo* response = trans->GetResponseInfo();
[email protected]2d2697f92009-02-18 21:00:32820 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]71e4573a2009-05-21 22:03:00825 EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port);
[email protected]2d2697f92009-02-18 21:00:32826 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]1c773ea12009-04-28 19:58:42832 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]2d2697f92009-02-18 21:00:32833
834 rv = callback2.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:42835 EXPECT_EQ(OK, rv);
[email protected]2d2697f92009-02-18 21:00:32836
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.
845TEST_F(HttpNetworkTransactionTest, BasicAuthKeepAliveLargeBody) {
[email protected]1c773ea12009-04-28 19:58:42846 scoped_ptr<ProxyService> proxy_service(CreateNullProxyService());
[email protected]d207a5f2009-06-04 05:28:40847 scoped_ptr<HttpTransaction> trans(
848 new HttpNetworkTransaction(
849 CreateSession(proxy_service.get(), &mock_socket_factory_),
850 &mock_socket_factory_));
[email protected]2d2697f92009-02-18 21:00:32851
[email protected]1c773ea12009-04-28 19:58:42852 HttpRequestInfo request;
[email protected]2d2697f92009-02-18 21:00:32853 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]1c773ea12009-04-28 19:58:42887 MockRead(false, OK),
[email protected]2d2697f92009-02-18 21:00:32888 };
889
890 MockSocket data1;
891 data1.reads = data_reads1;
892 data1.writes = data_writes1;
[email protected]ff007e162009-05-23 09:13:15893 mock_socket_factory_.AddMockSocket(&data1);
[email protected]2d2697f92009-02-18 21:00:32894
895 TestCompletionCallback callback1;
896
897 int rv = trans->Start(&request, &callback1);
[email protected]1c773ea12009-04-28 19:58:42898 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]2d2697f92009-02-18 21:00:32899
900 rv = callback1.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:42901 EXPECT_EQ(OK, rv);
[email protected]2d2697f92009-02-18 21:00:32902
[email protected]1c773ea12009-04-28 19:58:42903 const HttpResponseInfo* response = trans->GetResponseInfo();
[email protected]2d2697f92009-02-18 21:00:32904 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]71e4573a2009-05-21 22:03:00909 EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port);
[email protected]2d2697f92009-02-18 21:00:32910 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]1c773ea12009-04-28 19:58:42916 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]2d2697f92009-02-18 21:00:32917
918 rv = callback2.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:42919 EXPECT_EQ(OK, rv);
[email protected]2d2697f92009-02-18 21:00:32920
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.
929TEST_F(HttpNetworkTransactionTest, BasicAuthProxyKeepAlive) {
930 // Configure against proxy server "myproxy:70".
[email protected]1c773ea12009-04-28 19:58:42931 scoped_ptr<ProxyService> proxy_service(
[email protected]2d2697f92009-02-18 21:00:32932 CreateFixedProxyService("myproxy:70"));
933
[email protected]1c773ea12009-04-28 19:58:42934 scoped_refptr<HttpNetworkSession> session(
[email protected]d207a5f2009-06-04 05:28:40935 CreateSession(proxy_service.get(), &mock_socket_factory_));
[email protected]2d2697f92009-02-18 21:00:32936
[email protected]1c773ea12009-04-28 19:58:42937 scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(
[email protected]ff007e162009-05-23 09:13:15938 session.get(), &mock_socket_factory_));
[email protected]2d2697f92009-02-18 21:00:32939
[email protected]1c773ea12009-04-28 19:58:42940 HttpRequestInfo request;
[email protected]2d2697f92009-02-18 21:00:32941 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]1c773ea12009-04-28 19:58:42971 MockRead(false, ERR_UNEXPECTED), // Should not be reached.
[email protected]2d2697f92009-02-18 21:00:32972 };
973
974 MockSocket data1;
975 data1.writes = data_writes1;
976 data1.reads = data_reads1;
[email protected]ff007e162009-05-23 09:13:15977 mock_socket_factory_.AddMockSocket(&data1);
[email protected]2d2697f92009-02-18 21:00:32978
979 TestCompletionCallback callback1;
980
981 int rv = trans->Start(&request, &callback1);
[email protected]1c773ea12009-04-28 19:58:42982 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]2d2697f92009-02-18 21:00:32983
984 rv = callback1.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:42985 EXPECT_EQ(OK, rv);
[email protected]2d2697f92009-02-18 21:00:32986
[email protected]1c773ea12009-04-28 19:58:42987 const HttpResponseInfo* response = trans->GetResponseInfo();
[email protected]2d2697f92009-02-18 21:00:32988 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]1c773ea12009-04-28 19:58:42993 EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion());
[email protected]2d2697f92009-02-18 21:00:32994
995 // The password prompt info should have been set in response->auth_challenge.
996 EXPECT_FALSE(response->auth_challenge.get() == NULL);
997
[email protected]71e4573a2009-05-21 22:03:00998 EXPECT_EQ(L"myproxy:70", response->auth_challenge->host_and_port);
[email protected]2d2697f92009-02-18 21:00:32999 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]1c773ea12009-04-28 19:58:421006 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]2d2697f92009-02-18 21:00:321007
1008 rv = callback2.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:421009 EXPECT_EQ(OK, rv);
[email protected]2d2697f92009-02-18 21:00:321010
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]1c773ea12009-04-28 19:58:421017 EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion());
[email protected]2d2697f92009-02-18 21:00:321018
1019 // The password prompt info should have been set in response->auth_challenge.
1020 EXPECT_FALSE(response->auth_challenge.get() == NULL);
1021
[email protected]71e4573a2009-05-21 22:03:001022 EXPECT_EQ(L"myproxy:70", response->auth_challenge->host_and_port);
[email protected]2d2697f92009-02-18 21:00:321023 EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm);
1024 EXPECT_EQ(L"basic", response->auth_challenge->scheme);
1025}
1026
[email protected]a8e9b162009-03-12 00:06:441027// 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.
1029TEST_F(HttpNetworkTransactionTest, BasicAuthProxyCancelTunnel) {
1030 // Configure against proxy server "myproxy:70".
[email protected]1c773ea12009-04-28 19:58:421031 scoped_ptr<ProxyService> proxy_service(
[email protected]a8e9b162009-03-12 00:06:441032 CreateFixedProxyService("myproxy:70"));
1033
[email protected]1c773ea12009-04-28 19:58:421034 scoped_refptr<HttpNetworkSession> session(
[email protected]d207a5f2009-06-04 05:28:401035 CreateSession(proxy_service.get(), &mock_socket_factory_));
[email protected]a8e9b162009-03-12 00:06:441036
[email protected]1c773ea12009-04-28 19:58:421037 scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(
[email protected]ff007e162009-05-23 09:13:151038 session.get(), &mock_socket_factory_));
[email protected]a8e9b162009-03-12 00:06:441039
[email protected]1c773ea12009-04-28 19:58:421040 HttpRequestInfo request;
[email protected]a8e9b162009-03-12 00:06:441041 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]1c773ea12009-04-28 19:58:421056 MockRead(false, ERR_UNEXPECTED), // Should not be reached.
[email protected]a8e9b162009-03-12 00:06:441057 };
1058
1059 MockSocket data;
1060 data.writes = data_writes;
1061 data.reads = data_reads;
[email protected]ff007e162009-05-23 09:13:151062 mock_socket_factory_.AddMockSocket(&data);
[email protected]a8e9b162009-03-12 00:06:441063
1064 TestCompletionCallback callback;
1065
1066 int rv = trans->Start(&request, &callback);
[email protected]1c773ea12009-04-28 19:58:421067 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]a8e9b162009-03-12 00:06:441068
1069 rv = callback.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:421070 EXPECT_EQ(OK, rv);
[email protected]a8e9b162009-03-12 00:06:441071
[email protected]1c773ea12009-04-28 19:58:421072 const HttpResponseInfo* response = trans->GetResponseInfo();
[email protected]a8e9b162009-03-12 00:06:441073 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]1c773ea12009-04-28 19:58:421078 EXPECT_TRUE(HttpVersion(1, 1) == response->headers->GetHttpVersion());
[email protected]a8e9b162009-03-12 00:06:441079
1080 std::string response_data;
1081 rv = ReadTransaction(trans.get(), &response_data);
[email protected]1c773ea12009-04-28 19:58:421082 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, rv);
[email protected]a8e9b162009-03-12 00:06:441083}
1084
[email protected]ff007e162009-05-23 09:13:151085void HttpNetworkTransactionTest::ConnectStatusHelperWithExpectedStatus(
[email protected]c744cf22009-02-27 07:28:081086 const MockRead& status, int expected_status) {
1087 // Configure against proxy server "myproxy:70".
[email protected]1c773ea12009-04-28 19:58:421088 scoped_ptr<ProxyService> proxy_service(
[email protected]c744cf22009-02-27 07:28:081089 CreateFixedProxyService("myproxy:70"));
1090
[email protected]1c773ea12009-04-28 19:58:421091 scoped_refptr<HttpNetworkSession> session(
[email protected]d207a5f2009-06-04 05:28:401092 CreateSession(proxy_service.get(), &mock_socket_factory_));
[email protected]c744cf22009-02-27 07:28:081093
[email protected]1c773ea12009-04-28 19:58:421094 scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(
[email protected]ff007e162009-05-23 09:13:151095 session.get(), &mock_socket_factory_));
[email protected]c744cf22009-02-27 07:28:081096
[email protected]1c773ea12009-04-28 19:58:421097 HttpRequestInfo request;
[email protected]c744cf22009-02-27 07:28:081098 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]1c773ea12009-04-28 19:58:421112 MockRead(false, ERR_UNEXPECTED), // Should not be reached.
[email protected]c744cf22009-02-27 07:28:081113 };
1114
1115 MockSocket data;
1116 data.writes = data_writes;
1117 data.reads = data_reads;
[email protected]ff007e162009-05-23 09:13:151118 mock_socket_factory_.AddMockSocket(&data);
[email protected]c744cf22009-02-27 07:28:081119
1120 TestCompletionCallback callback;
1121
1122 int rv = trans->Start(&request, &callback);
[email protected]1c773ea12009-04-28 19:58:421123 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]c744cf22009-02-27 07:28:081124
1125 rv = callback.WaitForResult();
1126 EXPECT_EQ(expected_status, rv);
1127}
1128
[email protected]ff007e162009-05-23 09:13:151129void HttpNetworkTransactionTest::ConnectStatusHelper(const MockRead& status) {
[email protected]c744cf22009-02-27 07:28:081130 ConnectStatusHelperWithExpectedStatus(
[email protected]1c773ea12009-04-28 19:58:421131 status, ERR_TUNNEL_CONNECTION_FAILED);
[email protected]c744cf22009-02-27 07:28:081132}
1133
1134TEST_F(HttpNetworkTransactionTest, ConnectStatus100) {
1135 ConnectStatusHelper(MockRead("HTTP/1.1 100 Continue\r\n"));
1136}
1137
1138TEST_F(HttpNetworkTransactionTest, ConnectStatus101) {
1139 ConnectStatusHelper(MockRead("HTTP/1.1 101 Switching Protocols\r\n"));
1140}
1141
1142TEST_F(HttpNetworkTransactionTest, ConnectStatus201) {
1143 ConnectStatusHelper(MockRead("HTTP/1.1 201 Created\r\n"));
1144}
1145
1146TEST_F(HttpNetworkTransactionTest, ConnectStatus202) {
1147 ConnectStatusHelper(MockRead("HTTP/1.1 202 Accepted\r\n"));
1148}
1149
1150TEST_F(HttpNetworkTransactionTest, ConnectStatus203) {
1151 ConnectStatusHelper(
1152 MockRead("HTTP/1.1 203 Non-Authoritative Information\r\n"));
1153}
1154
1155TEST_F(HttpNetworkTransactionTest, ConnectStatus204) {
1156 ConnectStatusHelper(MockRead("HTTP/1.1 204 No Content\r\n"));
1157}
1158
1159TEST_F(HttpNetworkTransactionTest, ConnectStatus205) {
1160 ConnectStatusHelper(MockRead("HTTP/1.1 205 Reset Content\r\n"));
1161}
1162
1163TEST_F(HttpNetworkTransactionTest, ConnectStatus206) {
1164 ConnectStatusHelper(MockRead("HTTP/1.1 206 Partial Content\r\n"));
1165}
1166
1167TEST_F(HttpNetworkTransactionTest, ConnectStatus300) {
1168 ConnectStatusHelper(MockRead("HTTP/1.1 300 Multiple Choices\r\n"));
1169}
1170
1171TEST_F(HttpNetworkTransactionTest, ConnectStatus301) {
1172 ConnectStatusHelper(MockRead("HTTP/1.1 301 Moved Permanently\r\n"));
1173}
1174
1175TEST_F(HttpNetworkTransactionTest, ConnectStatus302) {
1176 ConnectStatusHelper(MockRead("HTTP/1.1 302 Found\r\n"));
1177}
1178
1179TEST_F(HttpNetworkTransactionTest, ConnectStatus303) {
1180 ConnectStatusHelper(MockRead("HTTP/1.1 303 See Other\r\n"));
1181}
1182
1183TEST_F(HttpNetworkTransactionTest, ConnectStatus304) {
1184 ConnectStatusHelper(MockRead("HTTP/1.1 304 Not Modified\r\n"));
1185}
1186
1187TEST_F(HttpNetworkTransactionTest, ConnectStatus305) {
1188 ConnectStatusHelper(MockRead("HTTP/1.1 305 Use Proxy\r\n"));
1189}
1190
1191TEST_F(HttpNetworkTransactionTest, ConnectStatus306) {
1192 ConnectStatusHelper(MockRead("HTTP/1.1 306\r\n"));
1193}
1194
1195TEST_F(HttpNetworkTransactionTest, ConnectStatus307) {
1196 ConnectStatusHelper(MockRead("HTTP/1.1 307 Temporary Redirect\r\n"));
1197}
1198
1199TEST_F(HttpNetworkTransactionTest, ConnectStatus400) {
1200 ConnectStatusHelper(MockRead("HTTP/1.1 400 Bad Request\r\n"));
1201}
1202
1203TEST_F(HttpNetworkTransactionTest, ConnectStatus401) {
1204 ConnectStatusHelper(MockRead("HTTP/1.1 401 Unauthorized\r\n"));
1205}
1206
1207TEST_F(HttpNetworkTransactionTest, ConnectStatus402) {
1208 ConnectStatusHelper(MockRead("HTTP/1.1 402 Payment Required\r\n"));
1209}
1210
1211TEST_F(HttpNetworkTransactionTest, ConnectStatus403) {
1212 ConnectStatusHelper(MockRead("HTTP/1.1 403 Forbidden\r\n"));
1213}
1214
1215TEST_F(HttpNetworkTransactionTest, ConnectStatus404) {
1216 ConnectStatusHelper(MockRead("HTTP/1.1 404 Not Found\r\n"));
1217}
1218
1219TEST_F(HttpNetworkTransactionTest, ConnectStatus405) {
1220 ConnectStatusHelper(MockRead("HTTP/1.1 405 Method Not Allowed\r\n"));
1221}
1222
1223TEST_F(HttpNetworkTransactionTest, ConnectStatus406) {
1224 ConnectStatusHelper(MockRead("HTTP/1.1 406 Not Acceptable\r\n"));
1225}
1226
1227TEST_F(HttpNetworkTransactionTest, ConnectStatus407) {
1228 ConnectStatusHelperWithExpectedStatus(
1229 MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n"),
[email protected]1c773ea12009-04-28 19:58:421230 ERR_PROXY_AUTH_REQUESTED);
[email protected]c744cf22009-02-27 07:28:081231}
1232
1233TEST_F(HttpNetworkTransactionTest, ConnectStatus408) {
1234 ConnectStatusHelper(MockRead("HTTP/1.1 408 Request Timeout\r\n"));
1235}
1236
1237TEST_F(HttpNetworkTransactionTest, ConnectStatus409) {
1238 ConnectStatusHelper(MockRead("HTTP/1.1 409 Conflict\r\n"));
1239}
1240
1241TEST_F(HttpNetworkTransactionTest, ConnectStatus410) {
1242 ConnectStatusHelper(MockRead("HTTP/1.1 410 Gone\r\n"));
1243}
1244
1245TEST_F(HttpNetworkTransactionTest, ConnectStatus411) {
1246 ConnectStatusHelper(MockRead("HTTP/1.1 411 Length Required\r\n"));
1247}
1248
1249TEST_F(HttpNetworkTransactionTest, ConnectStatus412) {
1250 ConnectStatusHelper(MockRead("HTTP/1.1 412 Precondition Failed\r\n"));
1251}
1252
1253TEST_F(HttpNetworkTransactionTest, ConnectStatus413) {
1254 ConnectStatusHelper(MockRead("HTTP/1.1 413 Request Entity Too Large\r\n"));
1255}
1256
1257TEST_F(HttpNetworkTransactionTest, ConnectStatus414) {
1258 ConnectStatusHelper(MockRead("HTTP/1.1 414 Request-URI Too Long\r\n"));
1259}
1260
1261TEST_F(HttpNetworkTransactionTest, ConnectStatus415) {
1262 ConnectStatusHelper(MockRead("HTTP/1.1 415 Unsupported Media Type\r\n"));
1263}
1264
1265TEST_F(HttpNetworkTransactionTest, ConnectStatus416) {
1266 ConnectStatusHelper(
1267 MockRead("HTTP/1.1 416 Requested Range Not Satisfiable\r\n"));
1268}
1269
1270TEST_F(HttpNetworkTransactionTest, ConnectStatus417) {
1271 ConnectStatusHelper(MockRead("HTTP/1.1 417 Expectation Failed\r\n"));
1272}
1273
1274TEST_F(HttpNetworkTransactionTest, ConnectStatus500) {
1275 ConnectStatusHelper(MockRead("HTTP/1.1 500 Internal Server Error\r\n"));
1276}
1277
1278TEST_F(HttpNetworkTransactionTest, ConnectStatus501) {
1279 ConnectStatusHelper(MockRead("HTTP/1.1 501 Not Implemented\r\n"));
1280}
1281
1282TEST_F(HttpNetworkTransactionTest, ConnectStatus502) {
1283 ConnectStatusHelper(MockRead("HTTP/1.1 502 Bad Gateway\r\n"));
1284}
1285
1286TEST_F(HttpNetworkTransactionTest, ConnectStatus503) {
1287 ConnectStatusHelper(MockRead("HTTP/1.1 503 Service Unavailable\r\n"));
1288}
1289
1290TEST_F(HttpNetworkTransactionTest, ConnectStatus504) {
1291 ConnectStatusHelper(MockRead("HTTP/1.1 504 Gateway Timeout\r\n"));
1292}
1293
1294TEST_F(HttpNetworkTransactionTest, ConnectStatus505) {
1295 ConnectStatusHelper(MockRead("HTTP/1.1 505 HTTP Version Not Supported\r\n"));
1296}
1297
[email protected]038e9a32008-10-08 22:40:161298// 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.
1301TEST_F(HttpNetworkTransactionTest, BasicAuthProxyThenServer) {
[email protected]1c773ea12009-04-28 19:58:421302 scoped_ptr<ProxyService> proxy_service(
[email protected]51fff29d2008-12-19 22:17:531303 CreateFixedProxyService("myproxy:70"));
[email protected]db8f44c2008-12-13 04:52:011304
[email protected]038e9a32008-10-08 22:40:161305 // Configure against proxy server "myproxy:70".
[email protected]1c773ea12009-04-28 19:58:421306 scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(
[email protected]d207a5f2009-06-04 05:28:401307 CreateSession(proxy_service.get(), &mock_socket_factory_),
[email protected]ff007e162009-05-23 09:13:151308 &mock_socket_factory_));
[email protected]038e9a32008-10-08 22:40:161309
[email protected]1c773ea12009-04-28 19:58:421310 HttpRequestInfo request;
[email protected]038e9a32008-10-08 22:40:161311 request.method = "GET";
1312 request.url = GURL("http://www.google.com/");
1313 request.load_flags = 0;
1314
[email protected]f9ee6b52008-11-08 06:46:231315 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]038e9a32008-10-08 22:40:161321 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]aaead502008-10-15 00:20:111325 MockRead("Proxy-Authenticate: Basic\r\n"), // Malformed
[email protected]038e9a32008-10-08 22:40:161326 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]1c773ea12009-04-28 19:58:421331 MockRead(false, ERR_FAILED),
[email protected]038e9a32008-10-08 22:40:161332 };
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]1c773ea12009-04-28 19:58:421353 MockRead(false, ERR_FAILED), // Won't be reached.
[email protected]038e9a32008-10-08 22:40:161354 };
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]1c773ea12009-04-28 19:58:421371 MockRead(false, OK),
[email protected]038e9a32008-10-08 22:40:161372 };
1373
1374 MockSocket data1;
1375 data1.reads = data_reads1;
[email protected]f9ee6b52008-11-08 06:46:231376 data1.writes = data_writes1;
[email protected]038e9a32008-10-08 22:40:161377 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]ff007e162009-05-23 09:13:151383 mock_socket_factory_.AddMockSocket(&data1);
1384 mock_socket_factory_.AddMockSocket(&data2);
1385 mock_socket_factory_.AddMockSocket(&data3);
[email protected]038e9a32008-10-08 22:40:161386
1387 TestCompletionCallback callback1;
1388
1389 int rv = trans->Start(&request, &callback1);
[email protected]1c773ea12009-04-28 19:58:421390 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]038e9a32008-10-08 22:40:161391
1392 rv = callback1.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:421393 EXPECT_EQ(OK, rv);
[email protected]038e9a32008-10-08 22:40:161394
[email protected]1c773ea12009-04-28 19:58:421395 const HttpResponseInfo* response = trans->GetResponseInfo();
[email protected]038e9a32008-10-08 22:40:161396 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]71e4573a2009-05-21 22:03:001401 EXPECT_EQ(L"myproxy:70", response->auth_challenge->host_and_port);
[email protected]038e9a32008-10-08 22:40:161402 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]1c773ea12009-04-28 19:58:421408 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]038e9a32008-10-08 22:40:161409
1410 rv = callback2.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:421411 EXPECT_EQ(OK, rv);
[email protected]038e9a32008-10-08 22:40:161412
1413 response = trans->GetResponseInfo();
1414 EXPECT_FALSE(response == NULL);
1415 EXPECT_FALSE(response->auth_challenge.get() == NULL);
1416
[email protected]71e4573a2009-05-21 22:03:001417 EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port);
[email protected]038e9a32008-10-08 22:40:161418 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]1c773ea12009-04-28 19:58:421424 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]038e9a32008-10-08 22:40:161425
1426 rv = callback3.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:421427 EXPECT_EQ(OK, rv);
[email protected]038e9a32008-10-08 22:40:161428
1429 response = trans->GetResponseInfo();
1430 EXPECT_TRUE(response->auth_challenge.get() == NULL);
1431 EXPECT_EQ(100, response->headers->GetContentLength());
[email protected]038e9a32008-10-08 22:40:161432}
[email protected]4ddaf2502008-10-23 18:26:191433
[email protected]385a4672009-03-11 22:21:291434// 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.
1439TEST_F(HttpNetworkTransactionTest, NTLMAuth1) {
[email protected]1c773ea12009-04-28 19:58:421440 HttpAuthHandlerNTLM::ScopedProcSetter proc_setter(MockGenerateRandom1,
[email protected]fe2bc6a2009-03-23 16:52:201441 MockGetHostName);
[email protected]385a4672009-03-11 22:21:291442
[email protected]1c773ea12009-04-28 19:58:421443 scoped_ptr<ProxyService> proxy_service(CreateNullProxyService());
[email protected]d207a5f2009-06-04 05:28:401444 scoped_ptr<HttpTransaction> trans(
1445 new HttpNetworkTransaction(
1446 CreateSession(proxy_service.get(), &mock_socket_factory_),
1447 &mock_socket_factory_));
[email protected]3f918782009-02-28 01:29:241448
[email protected]1c773ea12009-04-28 19:58:421449 HttpRequestInfo request;
[email protected]3f918782009-02-28 01:29:241450 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]076c85082009-04-10 23:21:361467 MockRead("Content-Type: text/html\r\n\r\n"),
[email protected]3f918782009-02-28 01:29:241468 // Missing content -- won't matter, as connection will be reset.
[email protected]1c773ea12009-04-28 19:58:421469 MockRead(false, ERR_UNEXPECTED),
[email protected]3f918782009-02-28 01:29:241470 };
1471
1472 MockWrite data_writes2[] = {
[email protected]0757e7702009-03-27 04:00:221473 // After restarting with a null identity, this is the
[email protected]3f918782009-02-28 01:29:241474 // 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]385a4672009-03-11 22:21:291488 "Authorization: NTLM TlRMTVNTUAADAAAAGAAYAGgAAAAYABgAgA"
1489 "AAAAAAAABAAAAAGAAYAEAAAAAQABAAWAAAAAAAAAAAAAAABYIIAHQA"
1490 "ZQBzAHQAaQBuAGcALQBuAHQAbABtAFcAVABDAC0AVwBJAE4ANwBVKW"
1491 "Yma5xzVAAAAAAAAAAAAAAAAAAAAACH+gWcm+YsP9Tqb9zCR3WAeZZX"
1492 "ahlhx5I=\r\n\r\n"),
[email protected]3f918782009-02-28 01:29:241493 };
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]385a4672009-03-11 22:21:291499 "TlRMTVNTUAACAAAADAAMADgAAAAFgokCjGpMpPGlYKkAAAAAAAAAALo"
[email protected]3f918782009-02-28 01:29:241500 "AugBEAAAABQEoCgAAAA9HAE8ATwBHAEwARQACAAwARwBPAE8ARwBMAE"
1501 "UAAQAaAEEASwBFAEUAUwBBAFIAQQAtAEMATwBSAFAABAAeAGMAbwByA"
1502 "HAALgBnAG8AbwBnAGwAZQAuAGMAbwBtAAMAQABhAGsAZQBlAHMAYQBy"
1503 "AGEALQBjAG8AcgBwAC4AYQBkAC4AYwBvAHIAcAAuAGcAbwBvAGcAbAB"
1504 "lAC4AYwBvAG0ABQAeAGMAbwByAHAALgBnAG8AbwBnAGwAZQAuAGMAbw"
1505 "BtAAAAAAA=\r\n"),
1506 MockRead("Content-Length: 42\r\n"),
[email protected]076c85082009-04-10 23:21:361507 MockRead("Content-Type: text/html\r\n\r\n"),
[email protected]3f918782009-02-28 01:29:241508 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]1c773ea12009-04-28 19:58:421515 MockRead(false, OK),
[email protected]3f918782009-02-28 01:29:241516 };
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]ff007e162009-05-23 09:13:151524 mock_socket_factory_.AddMockSocket(&data1);
1525 mock_socket_factory_.AddMockSocket(&data2);
[email protected]3f918782009-02-28 01:29:241526
1527 TestCompletionCallback callback1;
1528
1529 int rv = trans->Start(&request, &callback1);
[email protected]1c773ea12009-04-28 19:58:421530 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]3f918782009-02-28 01:29:241531
1532 rv = callback1.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:421533 EXPECT_EQ(OK, rv);
[email protected]3f918782009-02-28 01:29:241534
[email protected]0757e7702009-03-27 04:00:221535 EXPECT_TRUE(trans->IsReadyToRestartForAuth());
1536 TestCompletionCallback callback2;
1537 rv = trans->RestartWithAuth(std::wstring(), std::wstring(), &callback2);
[email protected]1c773ea12009-04-28 19:58:421538 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]0757e7702009-03-27 04:00:221539 rv = callback2.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:421540 EXPECT_EQ(OK, rv);
[email protected]0757e7702009-03-27 04:00:221541 EXPECT_FALSE(trans->IsReadyToRestartForAuth());
1542
[email protected]1c773ea12009-04-28 19:58:421543 const HttpResponseInfo* response = trans->GetResponseInfo();
[email protected]3f918782009-02-28 01:29:241544 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]71e4573a2009-05-21 22:03:001549 EXPECT_EQ(L"172.22.68.17:80", response->auth_challenge->host_and_port);
[email protected]3f918782009-02-28 01:29:241550 EXPECT_EQ(L"", response->auth_challenge->realm);
1551 EXPECT_EQ(L"ntlm", response->auth_challenge->scheme);
1552
[email protected]0757e7702009-03-27 04:00:221553 TestCompletionCallback callback3;
[email protected]3f918782009-02-28 01:29:241554
[email protected]0757e7702009-03-27 04:00:221555 rv = trans->RestartWithAuth(L"testing-ntlm", L"testing-ntlm", &callback3);
[email protected]1c773ea12009-04-28 19:58:421556 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]3f918782009-02-28 01:29:241557
[email protected]0757e7702009-03-27 04:00:221558 rv = callback3.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:421559 EXPECT_EQ(OK, rv);
[email protected]3f918782009-02-28 01:29:241560
1561 response = trans->GetResponseInfo();
1562 EXPECT_TRUE(response->auth_challenge.get() == NULL);
1563 EXPECT_EQ(13, response->headers->GetContentLength());
1564}
1565
[email protected]385a4672009-03-11 22:21:291566// Enter a wrong password, and then the correct one.
1567TEST_F(HttpNetworkTransactionTest, NTLMAuth2) {
[email protected]1c773ea12009-04-28 19:58:421568 HttpAuthHandlerNTLM::ScopedProcSetter proc_setter(MockGenerateRandom2,
[email protected]fe2bc6a2009-03-23 16:52:201569 MockGetHostName);
[email protected]385a4672009-03-11 22:21:291570
[email protected]1c773ea12009-04-28 19:58:421571 scoped_ptr<ProxyService> proxy_service(CreateNullProxyService());
[email protected]d207a5f2009-06-04 05:28:401572 scoped_ptr<HttpTransaction> trans(
1573 new HttpNetworkTransaction(
1574 CreateSession(proxy_service.get(), &mock_socket_factory_),
1575 &mock_socket_factory_));
[email protected]385a4672009-03-11 22:21:291576
[email protected]1c773ea12009-04-28 19:58:421577 HttpRequestInfo request;
[email protected]385a4672009-03-11 22:21:291578 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]076c85082009-04-10 23:21:361595 MockRead("Content-Type: text/html\r\n\r\n"),
[email protected]385a4672009-03-11 22:21:291596 // Missing content -- won't matter, as connection will be reset.
[email protected]1c773ea12009-04-28 19:58:421597 MockRead(false, ERR_UNEXPECTED),
[email protected]385a4672009-03-11 22:21:291598 };
1599
1600 MockWrite data_writes2[] = {
[email protected]0757e7702009-03-27 04:00:221601 // After restarting with a null identity, this is the
[email protected]385a4672009-03-11 22:21:291602 // 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]076c85082009-04-10 23:21:361635 MockRead("Content-Type: text/html\r\n\r\n"),
[email protected]385a4672009-03-11 22:21:291636 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]076c85082009-04-10 23:21:361644 MockRead("Content-Type: text/html\r\n\r\n"),
[email protected]385a4672009-03-11 22:21:291645 // Missing content -- won't matter, as connection will be reset.
[email protected]1c773ea12009-04-28 19:58:421646 MockRead(false, ERR_UNEXPECTED),
[email protected]385a4672009-03-11 22:21:291647 };
1648
1649 MockWrite data_writes3[] = {
[email protected]0757e7702009-03-27 04:00:221650 // After restarting with a null identity, this is the
[email protected]385a4672009-03-11 22:21:291651 // 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]076c85082009-04-10 23:21:361684 MockRead("Content-Type: text/html\r\n\r\n"),
[email protected]385a4672009-03-11 22:21:291685 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]1c773ea12009-04-28 19:58:421692 MockRead(false, OK),
[email protected]385a4672009-03-11 22:21:291693 };
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]ff007e162009-05-23 09:13:151704 mock_socket_factory_.AddMockSocket(&data1);
1705 mock_socket_factory_.AddMockSocket(&data2);
1706 mock_socket_factory_.AddMockSocket(&data3);
[email protected]385a4672009-03-11 22:21:291707
1708 TestCompletionCallback callback1;
1709
1710 int rv = trans->Start(&request, &callback1);
[email protected]1c773ea12009-04-28 19:58:421711 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]385a4672009-03-11 22:21:291712
1713 rv = callback1.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:421714 EXPECT_EQ(OK, rv);
[email protected]385a4672009-03-11 22:21:291715
[email protected]0757e7702009-03-27 04:00:221716 EXPECT_TRUE(trans->IsReadyToRestartForAuth());
[email protected]385a4672009-03-11 22:21:291717 TestCompletionCallback callback2;
[email protected]0757e7702009-03-27 04:00:221718 rv = trans->RestartWithAuth(std::wstring(), std::wstring(), &callback2);
[email protected]1c773ea12009-04-28 19:58:421719 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]385a4672009-03-11 22:21:291720 rv = callback2.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:421721 EXPECT_EQ(OK, rv);
[email protected]0757e7702009-03-27 04:00:221722 EXPECT_FALSE(trans->IsReadyToRestartForAuth());
[email protected]385a4672009-03-11 22:21:291723
[email protected]1c773ea12009-04-28 19:58:421724 const HttpResponseInfo* response = trans->GetResponseInfo();
[email protected]385a4672009-03-11 22:21:291725 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]71e4573a2009-05-21 22:03:001730 EXPECT_EQ(L"172.22.68.17:80", response->auth_challenge->host_and_port);
[email protected]385a4672009-03-11 22:21:291731 EXPECT_EQ(L"", response->auth_challenge->realm);
1732 EXPECT_EQ(L"ntlm", response->auth_challenge->scheme);
1733
1734 TestCompletionCallback callback3;
1735
[email protected]0757e7702009-03-27 04:00:221736 // Enter the wrong password.
1737 rv = trans->RestartWithAuth(L"testing-ntlm", L"wrongpassword", &callback3);
[email protected]1c773ea12009-04-28 19:58:421738 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]385a4672009-03-11 22:21:291739
1740 rv = callback3.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:421741 EXPECT_EQ(OK, rv);
[email protected]385a4672009-03-11 22:21:291742
[email protected]0757e7702009-03-27 04:00:221743 EXPECT_TRUE(trans->IsReadyToRestartForAuth());
1744 TestCompletionCallback callback4;
1745 rv = trans->RestartWithAuth(std::wstring(), std::wstring(), &callback4);
[email protected]1c773ea12009-04-28 19:58:421746 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]0757e7702009-03-27 04:00:221747 rv = callback4.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:421748 EXPECT_EQ(OK, rv);
[email protected]0757e7702009-03-27 04:00:221749 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]71e4573a2009-05-21 22:03:001757 EXPECT_EQ(L"172.22.68.17:80", response->auth_challenge->host_and_port);
[email protected]0757e7702009-03-27 04:00:221758 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]1c773ea12009-04-28 19:58:421765 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]0757e7702009-03-27 04:00:221766
1767 rv = callback5.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:421768 EXPECT_EQ(OK, rv);
[email protected]0757e7702009-03-27 04:00:221769
[email protected]385a4672009-03-11 22:21:291770 response = trans->GetResponseInfo();
1771 EXPECT_TRUE(response->auth_challenge.get() == NULL);
1772 EXPECT_EQ(13, response->headers->GetContentLength());
1773}
1774
[email protected]4ddaf2502008-10-23 18:26:191775// 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.
1778TEST_F(HttpNetworkTransactionTest, LargeHeadersNoBody) {
[email protected]1c773ea12009-04-28 19:58:421779 scoped_ptr<ProxyService> proxy_service(CreateNullProxyService());
[email protected]d207a5f2009-06-04 05:28:401780 scoped_ptr<HttpTransaction> trans(
1781 new HttpNetworkTransaction(
1782 CreateSession(proxy_service.get(), &mock_socket_factory_),
1783 &mock_socket_factory_));
[email protected]4ddaf2502008-10-23 18:26:191784
[email protected]1c773ea12009-04-28 19:58:421785 HttpRequestInfo request;
[email protected]4ddaf2502008-10-23 18:26:191786 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]15a5ccf82008-10-23 19:57:431791 std::string large_headers_string;
1792 FillLargeHeadersString(&large_headers_string, 50 * 1024);
[email protected]4ddaf2502008-10-23 18:26:191793
1794 MockRead data_reads[] = {
1795 MockRead("HTTP/1.0 200 OK\r\n"),
[email protected]15a5ccf82008-10-23 19:57:431796 MockRead(true, large_headers_string.data(), large_headers_string.size()),
[email protected]4ddaf2502008-10-23 18:26:191797 MockRead("\r\nBODY"),
[email protected]1c773ea12009-04-28 19:58:421798 MockRead(false, OK),
[email protected]4ddaf2502008-10-23 18:26:191799 };
1800 MockSocket data;
1801 data.reads = data_reads;
[email protected]ff007e162009-05-23 09:13:151802 mock_socket_factory_.AddMockSocket(&data);
[email protected]4ddaf2502008-10-23 18:26:191803
1804 TestCompletionCallback callback;
1805
1806 int rv = trans->Start(&request, &callback);
[email protected]1c773ea12009-04-28 19:58:421807 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]4ddaf2502008-10-23 18:26:191808
1809 rv = callback.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:421810 EXPECT_EQ(ERR_RESPONSE_HEADERS_TOO_BIG, rv);
[email protected]4ddaf2502008-10-23 18:26:191811
[email protected]1c773ea12009-04-28 19:58:421812 const HttpResponseInfo* response = trans->GetResponseInfo();
[email protected]4ddaf2502008-10-23 18:26:191813 EXPECT_TRUE(response == NULL);
1814}
[email protected]f4e426b2008-11-05 00:24:491815
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
1819TEST_F(HttpNetworkTransactionTest, DontRecycleTCPSocketForSSLTunnel) {
1820 // Configure against proxy server "myproxy:70".
[email protected]1c773ea12009-04-28 19:58:421821 scoped_ptr<ProxyService> proxy_service(
[email protected]51fff29d2008-12-19 22:17:531822 CreateFixedProxyService("myproxy:70"));
[email protected]db8f44c2008-12-13 04:52:011823
[email protected]1c773ea12009-04-28 19:58:421824 scoped_refptr<HttpNetworkSession> session(
[email protected]d207a5f2009-06-04 05:28:401825 CreateSession(proxy_service.get(), &mock_socket_factory_));
[email protected]f4e426b2008-11-05 00:24:491826
[email protected]1c773ea12009-04-28 19:58:421827 scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(
[email protected]ff007e162009-05-23 09:13:151828 session.get(), &mock_socket_factory_));
[email protected]f4e426b2008-11-05 00:24:491829
[email protected]1c773ea12009-04-28 19:58:421830 HttpRequestInfo request;
[email protected]f4e426b2008-11-05 00:24:491831 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]77848d12008-11-14 00:00:221841 // The proxy responds to the connect with a 404, using a persistent
[email protected]f4e426b2008-11-05 00:24:491842 // 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]1c773ea12009-04-28 19:58:421847 MockRead(false, ERR_UNEXPECTED), // Should not be reached.
[email protected]f4e426b2008-11-05 00:24:491848 };
1849
1850 MockSocket data1;
1851 data1.writes = data_writes1;
1852 data1.reads = data_reads1;
[email protected]ff007e162009-05-23 09:13:151853 mock_socket_factory_.AddMockSocket(&data1);
[email protected]f4e426b2008-11-05 00:24:491854
1855 TestCompletionCallback callback1;
1856
1857 int rv = trans->Start(&request, &callback1);
[email protected]1c773ea12009-04-28 19:58:421858 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]f4e426b2008-11-05 00:24:491859
1860 rv = callback1.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:421861 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, rv);
[email protected]f4e426b2008-11-05 00:24:491862
[email protected]1c773ea12009-04-28 19:58:421863 const HttpResponseInfo* response = trans->GetResponseInfo();
[email protected]c744cf22009-02-27 07:28:081864 EXPECT_TRUE(response == NULL);
[email protected]f4e426b2008-11-05 00:24:491865
[email protected]b4404c02009-04-10 16:38:521866 // 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]f4e426b2008-11-05 00:24:491870 // 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]b4404c02009-04-10 16:38:521874 MessageLoop::current()->RunAllPending();
[email protected]f4e426b2008-11-05 00:24:491875 // 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]372d34a2008-11-05 21:30:511878
[email protected]1b157c02009-04-21 01:55:401879// Make sure that we recycle a socket after reading all of the response body.
1880TEST_F(HttpNetworkTransactionTest, RecycleSocket) {
[email protected]1c773ea12009-04-28 19:58:421881 scoped_ptr<ProxyService> proxy_service(CreateNullProxyService());
1882 scoped_refptr<HttpNetworkSession> session(
[email protected]d207a5f2009-06-04 05:28:401883 CreateSession(proxy_service.get(), &mock_socket_factory_));
[email protected]1b157c02009-04-21 01:55:401884
[email protected]1c773ea12009-04-28 19:58:421885 scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(
[email protected]ff007e162009-05-23 09:13:151886 session.get(), &mock_socket_factory_));
[email protected]1b157c02009-04-21 01:55:401887
[email protected]1c773ea12009-04-28 19:58:421888 HttpRequestInfo request;
[email protected]1b157c02009-04-21 01:55:401889 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]1c773ea12009-04-28 19:58:421900 MockRead(false, OK),
[email protected]1b157c02009-04-21 01:55:401901 };
1902
1903 MockSocket data;
1904 data.reads = data_reads;
[email protected]ff007e162009-05-23 09:13:151905 mock_socket_factory_.AddMockSocket(&data);
[email protected]1b157c02009-04-21 01:55:401906
1907 TestCompletionCallback callback;
1908
1909 int rv = trans->Start(&request, &callback);
[email protected]1c773ea12009-04-28 19:58:421910 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]1b157c02009-04-21 01:55:401911
1912 rv = callback.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:421913 EXPECT_EQ(OK, rv);
[email protected]1b157c02009-04-21 01:55:401914
[email protected]1c773ea12009-04-28 19:58:421915 const HttpResponseInfo* response = trans->GetResponseInfo();
[email protected]1b157c02009-04-21 01:55:401916 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]1c773ea12009-04-28 19:58:421926 EXPECT_EQ(OK, rv);
[email protected]1b157c02009-04-21 01:55:401927 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]b4404c02009-04-10 16:38:521937// Make sure that we recycle a socket after a zero-length response.
1938// http://crbug.com/9880
1939TEST_F(HttpNetworkTransactionTest, RecycleSocketAfterZeroContentLength) {
[email protected]1c773ea12009-04-28 19:58:421940 scoped_ptr<ProxyService> proxy_service(CreateNullProxyService());
1941 scoped_refptr<HttpNetworkSession> session(
[email protected]d207a5f2009-06-04 05:28:401942 CreateSession(proxy_service.get(), &mock_socket_factory_));
[email protected]b4404c02009-04-10 16:38:521943
[email protected]1c773ea12009-04-28 19:58:421944 scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(
[email protected]ff007e162009-05-23 09:13:151945 session.get(), &mock_socket_factory_));
[email protected]b4404c02009-04-10 16:38:521946
[email protected]1c773ea12009-04-28 19:58:421947 HttpRequestInfo request;
[email protected]b4404c02009-04-10 16:38:521948 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]1c773ea12009-04-28 19:58:421960 MockRead(false, OK),
[email protected]b4404c02009-04-10 16:38:521961 };
1962
1963 MockSocket data;
1964 data.reads = data_reads;
[email protected]ff007e162009-05-23 09:13:151965 mock_socket_factory_.AddMockSocket(&data);
[email protected]b4404c02009-04-10 16:38:521966
1967 TestCompletionCallback callback;
1968
1969 int rv = trans->Start(&request, &callback);
[email protected]1c773ea12009-04-28 19:58:421970 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]b4404c02009-04-10 16:38:521971
1972 rv = callback.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:421973 EXPECT_EQ(OK, rv);
[email protected]b4404c02009-04-10 16:38:521974
[email protected]1c773ea12009-04-28 19:58:421975 const HttpResponseInfo* response = trans->GetResponseInfo();
[email protected]b4404c02009-04-10 16:38:521976 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]1c773ea12009-04-28 19:58:421986 EXPECT_EQ(OK, rv);
[email protected]b4404c02009-04-10 16:38:521987 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]372d34a2008-11-05 21:30:511997TEST_F(HttpNetworkTransactionTest, ResendRequestOnWriteBodyError) {
[email protected]1c773ea12009-04-28 19:58:421998 HttpRequestInfo request[2];
[email protected]372d34a2008-11-05 21:30:511999 // 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]1c773ea12009-04-28 19:58:422010 request[1].upload_data = new UploadData;
[email protected]372d34a2008-11-05 21:30:512011 request[1].upload_data->AppendBytes("foo", 3);
2012 request[1].load_flags = 0;
2013
[email protected]1c773ea12009-04-28 19:58:422014 scoped_ptr<ProxyService> proxy_service(CreateNullProxyService());
2015 scoped_refptr<HttpNetworkSession> session =
[email protected]d207a5f2009-06-04 05:28:402016 CreateSession(proxy_service.get(), &mock_socket_factory_);
[email protected]372d34a2008-11-05 21:30:512017
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]1c773ea12009-04-28 19:58:422025 MockRead(false, OK),
[email protected]372d34a2008-11-05 21:30:512026 };
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]1c773ea12009-04-28 19:58:422032 MockWrite(false, ERR_CONNECTION_ABORTED), // POST data
[email protected]372d34a2008-11-05 21:30:512033 };
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]1c773ea12009-04-28 19:58:422044 MockRead(false, OK),
[email protected]372d34a2008-11-05 21:30:512045 };
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]ff007e162009-05-23 09:13:152055 mock_socket_factory_.AddMockSocket(&data1);
2056 mock_socket_factory_.AddMockSocket(&data2);
[email protected]372d34a2008-11-05 21:30:512057
2058 const char* kExpectedResponseData[] = {
2059 "hello world", "welcome"
2060 };
2061
2062 for (int i = 0; i < 2; ++i) {
[email protected]1c773ea12009-04-28 19:58:422063 scoped_ptr<HttpTransaction> trans(
[email protected]ff007e162009-05-23 09:13:152064 new HttpNetworkTransaction(session, &mock_socket_factory_));
[email protected]372d34a2008-11-05 21:30:512065
2066 TestCompletionCallback callback;
2067
2068 int rv = trans->Start(&request[i], &callback);
[email protected]1c773ea12009-04-28 19:58:422069 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]372d34a2008-11-05 21:30:512070
2071 rv = callback.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:422072 EXPECT_EQ(OK, rv);
[email protected]372d34a2008-11-05 21:30:512073
[email protected]1c773ea12009-04-28 19:58:422074 const HttpResponseInfo* response = trans->GetResponseInfo();
[email protected]372d34a2008-11-05 21:30:512075 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]1c773ea12009-04-28 19:58:422082 EXPECT_EQ(OK, rv);
[email protected]372d34a2008-11-05 21:30:512083 EXPECT_EQ(kExpectedResponseData[i], response_data);
2084 }
2085}
[email protected]f9ee6b52008-11-08 06:46:232086
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.
2090TEST_F(HttpNetworkTransactionTest, AuthIdentityInUrl) {
[email protected]1c773ea12009-04-28 19:58:422091 scoped_ptr<ProxyService> proxy_service(CreateNullProxyService());
[email protected]d207a5f2009-06-04 05:28:402092 scoped_ptr<HttpTransaction> trans(
2093 new HttpNetworkTransaction(
2094 CreateSession(proxy_service.get(), &mock_socket_factory_),
2095 &mock_socket_factory_));
[email protected]f9ee6b52008-11-08 06:46:232096
[email protected]1c773ea12009-04-28 19:58:422097 HttpRequestInfo request;
[email protected]f9ee6b52008-11-08 06:46:232098 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]1c773ea12009-04-28 19:58:422113 MockRead(false, ERR_FAILED),
[email protected]f9ee6b52008-11-08 06:46:232114 };
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]1c773ea12009-04-28 19:58:422128 MockRead(false, OK),
[email protected]f9ee6b52008-11-08 06:46:232129 };
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]ff007e162009-05-23 09:13:152137 mock_socket_factory_.AddMockSocket(&data1);
2138 mock_socket_factory_.AddMockSocket(&data2);
[email protected]f9ee6b52008-11-08 06:46:232139
2140 TestCompletionCallback callback1;
2141
2142 int rv = trans->Start(&request, &callback1);
[email protected]1c773ea12009-04-28 19:58:422143 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]f9ee6b52008-11-08 06:46:232144
2145 rv = callback1.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:422146 EXPECT_EQ(OK, rv);
[email protected]f9ee6b52008-11-08 06:46:232147
[email protected]0757e7702009-03-27 04:00:222148 EXPECT_TRUE(trans->IsReadyToRestartForAuth());
2149 TestCompletionCallback callback2;
2150 rv = trans->RestartWithAuth(std::wstring(), std::wstring(), &callback2);
[email protected]1c773ea12009-04-28 19:58:422151 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]0757e7702009-03-27 04:00:222152 rv = callback2.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:422153 EXPECT_EQ(OK, rv);
[email protected]0757e7702009-03-27 04:00:222154 EXPECT_FALSE(trans->IsReadyToRestartForAuth());
2155
[email protected]1c773ea12009-04-28 19:58:422156 const HttpResponseInfo* response = trans->GetResponseInfo();
[email protected]f9ee6b52008-11-08 06:46:232157 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.
2169TEST_F(HttpNetworkTransactionTest, BasicAuthCacheAndPreauth) {
[email protected]1c773ea12009-04-28 19:58:422170 scoped_ptr<ProxyService> proxy_service(CreateNullProxyService());
2171 scoped_refptr<HttpNetworkSession> session =
[email protected]d207a5f2009-06-04 05:28:402172 CreateSession(proxy_service.get(), &mock_socket_factory_);
[email protected]f9ee6b52008-11-08 06:46:232173
2174 // Transaction 1: authenticate (foo, bar) on MyRealm1
2175 {
[email protected]1c773ea12009-04-28 19:58:422176 scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(
[email protected]ff007e162009-05-23 09:13:152177 session, &mock_socket_factory_));
[email protected]f9ee6b52008-11-08 06:46:232178
[email protected]1c773ea12009-04-28 19:58:422179 HttpRequestInfo request;
[email protected]f9ee6b52008-11-08 06:46:232180 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]1c773ea12009-04-28 19:58:422194 MockRead(false, ERR_FAILED),
[email protected]f9ee6b52008-11-08 06:46:232195 };
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]1c773ea12009-04-28 19:58:422209 MockRead(false, OK),
[email protected]f9ee6b52008-11-08 06:46:232210 };
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]ff007e162009-05-23 09:13:152218 mock_socket_factory_.AddMockSocket(&data1);
2219 mock_socket_factory_.AddMockSocket(&data2);
[email protected]f9ee6b52008-11-08 06:46:232220
2221 TestCompletionCallback callback1;
2222
2223 int rv = trans->Start(&request, &callback1);
[email protected]1c773ea12009-04-28 19:58:422224 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]f9ee6b52008-11-08 06:46:232225
2226 rv = callback1.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:422227 EXPECT_EQ(OK, rv);
[email protected]f9ee6b52008-11-08 06:46:232228
[email protected]1c773ea12009-04-28 19:58:422229 const HttpResponseInfo* response = trans->GetResponseInfo();
[email protected]f9ee6b52008-11-08 06:46:232230 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]71e4573a2009-05-21 22:03:002236 EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port);
[email protected]f9ee6b52008-11-08 06:46:232237 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]1c773ea12009-04-28 19:58:422243 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]f9ee6b52008-11-08 06:46:232244
2245 rv = callback2.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:422246 EXPECT_EQ(OK, rv);
[email protected]f9ee6b52008-11-08 06:46:232247
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]1c773ea12009-04-28 19:58:422258 scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(
[email protected]ff007e162009-05-23 09:13:152259 session, &mock_socket_factory_));
[email protected]f9ee6b52008-11-08 06:46:232260
[email protected]1c773ea12009-04-28 19:58:422261 HttpRequestInfo request;
[email protected]f9ee6b52008-11-08 06:46:232262 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]1c773ea12009-04-28 19:58:422282 MockRead(false, ERR_FAILED),
[email protected]f9ee6b52008-11-08 06:46:232283 };
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]1c773ea12009-04-28 19:58:422297 MockRead(false, OK),
[email protected]f9ee6b52008-11-08 06:46:232298 };
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]ff007e162009-05-23 09:13:152306
2307 mock_socket_factory_.AddMockSocket(&data1);
2308 mock_socket_factory_.AddMockSocket(&data2);
[email protected]f9ee6b52008-11-08 06:46:232309
2310 TestCompletionCallback callback1;
2311
2312 int rv = trans->Start(&request, &callback1);
[email protected]1c773ea12009-04-28 19:58:422313 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]f9ee6b52008-11-08 06:46:232314
2315 rv = callback1.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:422316 EXPECT_EQ(OK, rv);
[email protected]f9ee6b52008-11-08 06:46:232317
[email protected]1c773ea12009-04-28 19:58:422318 const HttpResponseInfo* response = trans->GetResponseInfo();
[email protected]f9ee6b52008-11-08 06:46:232319 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]71e4573a2009-05-21 22:03:002325 EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port);
[email protected]f9ee6b52008-11-08 06:46:232326 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]1c773ea12009-04-28 19:58:422332 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]f9ee6b52008-11-08 06:46:232333
2334 rv = callback2.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:422335 EXPECT_EQ(OK, rv);
[email protected]f9ee6b52008-11-08 06:46:232336
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]1c773ea12009-04-28 19:58:422348 scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(
[email protected]ff007e162009-05-23 09:13:152349 session, &mock_socket_factory_));
[email protected]f9ee6b52008-11-08 06:46:232350
[email protected]1c773ea12009-04-28 19:58:422351 HttpRequestInfo request;
[email protected]f9ee6b52008-11-08 06:46:232352 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]1c773ea12009-04-28 19:58:422369 MockRead(false, OK),
[email protected]f9ee6b52008-11-08 06:46:232370 };
2371
2372 MockSocket data1;
2373 data1.reads = data_reads1;
2374 data1.writes = data_writes1;
[email protected]ff007e162009-05-23 09:13:152375 mock_socket_factory_.AddMockSocket(&data1);
[email protected]f9ee6b52008-11-08 06:46:232376
2377 TestCompletionCallback callback1;
2378
2379 int rv = trans->Start(&request, &callback1);
[email protected]1c773ea12009-04-28 19:58:422380 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]f9ee6b52008-11-08 06:46:232381
2382 rv = callback1.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:422383 EXPECT_EQ(OK, rv);
[email protected]f9ee6b52008-11-08 06:46:232384
[email protected]1c773ea12009-04-28 19:58:422385 const HttpResponseInfo* response = trans->GetResponseInfo();
[email protected]f9ee6b52008-11-08 06:46:232386 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]1c773ea12009-04-28 19:58:422397 scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(
[email protected]ff007e162009-05-23 09:13:152398 session, &mock_socket_factory_));
[email protected]f9ee6b52008-11-08 06:46:232399
[email protected]1c773ea12009-04-28 19:58:422400 HttpRequestInfo request;
[email protected]f9ee6b52008-11-08 06:46:232401 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]1c773ea12009-04-28 19:58:422415 MockRead(false, ERR_FAILED),
[email protected]f9ee6b52008-11-08 06:46:232416 };
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]1c773ea12009-04-28 19:58:422430 MockRead(false, OK),
[email protected]f9ee6b52008-11-08 06:46:232431 };
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]ff007e162009-05-23 09:13:152439 mock_socket_factory_.AddMockSocket(&data1);
2440 mock_socket_factory_.AddMockSocket(&data2);
[email protected]f9ee6b52008-11-08 06:46:232441
2442 TestCompletionCallback callback1;
2443
2444 int rv = trans->Start(&request, &callback1);
[email protected]1c773ea12009-04-28 19:58:422445 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]f9ee6b52008-11-08 06:46:232446
2447 rv = callback1.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:422448 EXPECT_EQ(OK, rv);
[email protected]f9ee6b52008-11-08 06:46:232449
[email protected]0757e7702009-03-27 04:00:222450 EXPECT_TRUE(trans->IsReadyToRestartForAuth());
2451 TestCompletionCallback callback2;
2452 rv = trans->RestartWithAuth(std::wstring(), std::wstring(), &callback2);
[email protected]1c773ea12009-04-28 19:58:422453 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]0757e7702009-03-27 04:00:222454 rv = callback2.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:422455 EXPECT_EQ(OK, rv);
[email protected]0757e7702009-03-27 04:00:222456 EXPECT_FALSE(trans->IsReadyToRestartForAuth());
2457
[email protected]1c773ea12009-04-28 19:58:422458 const HttpResponseInfo* response = trans->GetResponseInfo();
[email protected]f9ee6b52008-11-08 06:46:232459 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]1c773ea12009-04-28 19:58:422469 scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction(
[email protected]ff007e162009-05-23 09:13:152470 session, &mock_socket_factory_));
[email protected]f9ee6b52008-11-08 06:46:232471
[email protected]1c773ea12009-04-28 19:58:422472 HttpRequestInfo request;
[email protected]f9ee6b52008-11-08 06:46:232473 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]1c773ea12009-04-28 19:58:422487 MockRead(false, ERR_FAILED),
[email protected]f9ee6b52008-11-08 06:46:232488 };
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]1c773ea12009-04-28 19:58:422503 MockRead(false, ERR_FAILED),
[email protected]f9ee6b52008-11-08 06:46:232504 };
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]1c773ea12009-04-28 19:58:422519 MockRead(false, OK),
[email protected]f9ee6b52008-11-08 06:46:232520 };
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]ff007e162009-05-23 09:13:152531 mock_socket_factory_.AddMockSocket(&data1);
2532 mock_socket_factory_.AddMockSocket(&data2);
2533 mock_socket_factory_.AddMockSocket(&data3);
[email protected]f9ee6b52008-11-08 06:46:232534
2535 TestCompletionCallback callback1;
2536
2537 int rv = trans->Start(&request, &callback1);
[email protected]1c773ea12009-04-28 19:58:422538 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]f9ee6b52008-11-08 06:46:232539
2540 rv = callback1.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:422541 EXPECT_EQ(OK, rv);
[email protected]f9ee6b52008-11-08 06:46:232542
[email protected]0757e7702009-03-27 04:00:222543 EXPECT_TRUE(trans->IsReadyToRestartForAuth());
2544 TestCompletionCallback callback2;
2545 rv = trans->RestartWithAuth(std::wstring(), std::wstring(), &callback2);
[email protected]1c773ea12009-04-28 19:58:422546 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]0757e7702009-03-27 04:00:222547 rv = callback2.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:422548 EXPECT_EQ(OK, rv);
[email protected]0757e7702009-03-27 04:00:222549 EXPECT_FALSE(trans->IsReadyToRestartForAuth());
2550
[email protected]1c773ea12009-04-28 19:58:422551 const HttpResponseInfo* response = trans->GetResponseInfo();
[email protected]f9ee6b52008-11-08 06:46:232552 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]71e4573a2009-05-21 22:03:002558 EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port);
[email protected]f9ee6b52008-11-08 06:46:232559 EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm);
2560 EXPECT_EQ(L"basic", response->auth_challenge->scheme);
2561
[email protected]0757e7702009-03-27 04:00:222562 TestCompletionCallback callback3;
[email protected]f9ee6b52008-11-08 06:46:232563
[email protected]0757e7702009-03-27 04:00:222564 rv = trans->RestartWithAuth(L"foo3", L"bar3", &callback3);
[email protected]1c773ea12009-04-28 19:58:422565 EXPECT_EQ(ERR_IO_PENDING, rv);
[email protected]f9ee6b52008-11-08 06:46:232566
[email protected]0757e7702009-03-27 04:00:222567 rv = callback3.WaitForResult();
[email protected]1c773ea12009-04-28 19:58:422568 EXPECT_EQ(OK, rv);
[email protected]f9ee6b52008-11-08 06:46:232569
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]89ceba9a2009-03-21 03:46:062576
2577// Test the ResetStateForRestart() private method.
2578TEST_F(HttpNetworkTransactionTest, ResetStateForRestart) {
2579 // Create a transaction (the dependencies aren't important).
2580 scoped_ptr<ProxyService> proxy_service(CreateNullProxyService());
[email protected]d207a5f2009-06-04 05:28:402581 scoped_ptr<HttpNetworkTransaction> trans(
2582 new HttpNetworkTransaction(
2583 CreateSession(proxy_service.get(), &mock_socket_factory_),
2584 &mock_socket_factory_));
[email protected]89ceba9a2009-03-21 03:46:062585
2586 // Setup some state (which we expect ResetStateForRestart() will clear).
[email protected]ffeb0882009-04-30 21:51:252587 trans->header_buf_->Realloc(10);
[email protected]89ceba9a2009-03-21 03:46:062588 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]ffeb0882009-04-30 21:51:252596 trans->request_headers_->headers_ = "Authorization: NTLM";
[email protected]89ceba9a2009-03-21 03:46:062597 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]b4404c02009-04-10 16:38:522603 trans->response_.was_cached = true; // (Wouldn't ever actually be true...)
[email protected]89ceba9a2009-03-21 03:46:062604
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]ffeb0882009-04-30 21:51:252618 EXPECT_EQ(NULL, trans->header_buf_->headers());
[email protected]89ceba9a2009-03-21 03:46:062619 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]ffeb0882009-04-30 21:51:252627 EXPECT_EQ("", trans->request_headers_->headers_);
[email protected]89ceba9a2009-03-21 03:46:062628 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]89ceba9a2009-03-21 03:46:062632 EXPECT_EQ(0, trans->response_.ssl_info.cert_status);
2633 EXPECT_FALSE(trans->response_.vary_data.is_valid());
2634}
2635
[email protected]bacff652009-03-31 17:50:332636// Test HTTPS connections to a site with a bad certificate
2637TEST_F(HttpNetworkTransactionTest, HTTPSBadCertificate) {
2638 scoped_ptr<ProxyService> proxy_service(CreateNullProxyService());
[email protected]d207a5f2009-06-04 05:28:402639 scoped_ptr<HttpTransaction> trans(
2640 new HttpNetworkTransaction(
2641 CreateSession(proxy_service.get(), &mock_socket_factory_),
2642 &mock_socket_factory_));
[email protected]bacff652009-03-31 17:50:332643
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]ff007e162009-05-23 09:13:152667 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]bacff652009-03-31 17:50:332671
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
2694TEST_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]1c773ea12009-04-28 19:58:422710 MockRead(false, OK)
[email protected]bacff652009-03-31 17:50:332711 };
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]ff007e162009-05-23 09:13:152734 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]bacff652009-03-31 17:50:332738
2739 TestCompletionCallback callback;
2740
2741 for (int i = 0; i < 2; i++) {
[email protected]ff007e162009-05-23 09:13:152742 mock_socket_factory_.ResetNextMockIndexes();
[email protected]bacff652009-03-31 17:50:332743
[email protected]d207a5f2009-06-04 05:28:402744 scoped_ptr<HttpTransaction> trans(
2745 new HttpNetworkTransaction(
2746 CreateSession(proxy_service.get(), &mock_socket_factory_),
2747 &mock_socket_factory_));
[email protected]bacff652009-03-31 17:50:332748
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]1c773ea12009-04-28 19:58:422768TEST_F(HttpNetworkTransactionTest, BuildRequest_UserAgent) {
2769 scoped_ptr<ProxyService> proxy_service(CreateNullProxyService());
[email protected]d207a5f2009-06-04 05:28:402770 scoped_ptr<HttpTransaction> trans(
2771 new HttpNetworkTransaction(
2772 CreateSession(proxy_service.get(), &mock_socket_factory_),
2773 &mock_socket_factory_));
[email protected]1c773ea12009-04-28 19:58:422774
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]ff007e162009-05-23 09:13:152798 mock_socket_factory_.AddMockSocket(&data);
[email protected]1c773ea12009-04-28 19:58:422799
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
2809TEST_F(HttpNetworkTransactionTest, BuildRequest_Referer) {
2810 scoped_ptr<ProxyService> proxy_service(CreateNullProxyService());
[email protected]d207a5f2009-06-04 05:28:402811 scoped_ptr<HttpTransaction> trans(
2812 new HttpNetworkTransaction(
2813 CreateSession(proxy_service.get(), &mock_socket_factory_),
2814 &mock_socket_factory_));
[email protected]1c773ea12009-04-28 19:58:422815
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]ff007e162009-05-23 09:13:152840 mock_socket_factory_.AddMockSocket(&data);
[email protected]1c773ea12009-04-28 19:58:422841
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
2851TEST_F(HttpNetworkTransactionTest, BuildRequest_PostContentLengthZero) {
2852 scoped_ptr<ProxyService> proxy_service(CreateNullProxyService());
[email protected]d207a5f2009-06-04 05:28:402853 scoped_ptr<HttpTransaction> trans(
2854 new HttpNetworkTransaction(
2855 CreateSession(proxy_service.get(), &mock_socket_factory_),
2856 &mock_socket_factory_));
[email protected]1c773ea12009-04-28 19:58:422857
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]ff007e162009-05-23 09:13:152880 mock_socket_factory_.AddMockSocket(&data);
[email protected]1c773ea12009-04-28 19:58:422881
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
2891TEST_F(HttpNetworkTransactionTest, BuildRequest_PutContentLengthZero) {
2892 scoped_ptr<ProxyService> proxy_service(CreateNullProxyService());
[email protected]d207a5f2009-06-04 05:28:402893 scoped_ptr<HttpTransaction> trans(
2894 new HttpNetworkTransaction(
2895 CreateSession(proxy_service.get(), &mock_socket_factory_),
2896 &mock_socket_factory_));
[email protected]1c773ea12009-04-28 19:58:422897
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]ff007e162009-05-23 09:13:152920 mock_socket_factory_.AddMockSocket(&data);
[email protected]1c773ea12009-04-28 19:58:422921
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
2931TEST_F(HttpNetworkTransactionTest, BuildRequest_HeadContentLengthZero) {
2932 scoped_ptr<ProxyService> proxy_service(CreateNullProxyService());
[email protected]d207a5f2009-06-04 05:28:402933 scoped_ptr<HttpTransaction> trans(
2934 new HttpNetworkTransaction(
2935 CreateSession(proxy_service.get(), &mock_socket_factory_),
2936 &mock_socket_factory_));
[email protected]1c773ea12009-04-28 19:58:422937
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]ff007e162009-05-23 09:13:152960 mock_socket_factory_.AddMockSocket(&data);
[email protected]1c773ea12009-04-28 19:58:422961
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
2971TEST_F(HttpNetworkTransactionTest, BuildRequest_CacheControlNoCache) {
2972 scoped_ptr<ProxyService> proxy_service(CreateNullProxyService());
[email protected]d207a5f2009-06-04 05:28:402973 scoped_ptr<HttpTransaction> trans(
2974 new HttpNetworkTransaction(
2975 CreateSession(proxy_service.get(), &mock_socket_factory_),
2976 &mock_socket_factory_));
[email protected]1c773ea12009-04-28 19:58:422977
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]ff007e162009-05-23 09:13:153002 mock_socket_factory_.AddMockSocket(&data);
[email protected]1c773ea12009-04-28 19:58:423003
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
3013TEST_F(HttpNetworkTransactionTest,
3014 BuildRequest_CacheControlValidateCache) {
3015 scoped_ptr<ProxyService> proxy_service(CreateNullProxyService());
[email protected]d207a5f2009-06-04 05:28:403016 scoped_ptr<HttpTransaction> trans(
3017 new HttpNetworkTransaction(
3018 CreateSession(proxy_service.get(), &mock_socket_factory_),
3019 &mock_socket_factory_));
[email protected]1c773ea12009-04-28 19:58:423020
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]ff007e162009-05-23 09:13:153044 mock_socket_factory_.AddMockSocket(&data);
[email protected]1c773ea12009-04-28 19:58:423045
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
3055TEST_F(HttpNetworkTransactionTest, BuildRequest_ExtraHeaders) {
3056 scoped_ptr<ProxyService> proxy_service(CreateNullProxyService());
[email protected]d207a5f2009-06-04 05:28:403057 scoped_ptr<HttpTransaction> trans(
3058 new HttpNetworkTransaction(
3059 CreateSession(proxy_service.get(), &mock_socket_factory_),
3060 &mock_socket_factory_));
[email protected]1c773ea12009-04-28 19:58:423061
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]ff007e162009-05-23 09:13:153085 mock_socket_factory_.AddMockSocket(&data);
[email protected]1c773ea12009-04-28 19:58:423086
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]89ceba9a2009-03-21 03:46:063096} // namespace net