blob: 92e32ae30ed7eaba65131db087abed7090371527 [file] [log] [blame]
[email protected]9045b8822012-01-13 20:35:351// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// 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]ea224582008-12-07 20:25:465#include "build/build_config.h"
6
[email protected]9396b252008-09-29 17:29:387#if defined(OS_WIN)
[email protected]d8eb84242010-09-25 02:25:068#include <windows.h>
[email protected]aa84a7e2012-03-15 21:29:069#include <shlobj.h>
[email protected]9396b252008-09-29 17:29:3810#endif
11
initial.commit586acc5fe2008-07-26 22:42:5212#include <algorithm>
13#include <string>
14
[email protected]aad63572011-05-24 20:14:3915#include "base/basictypes.h"
[email protected]218aa6a12011-09-13 17:38:3816#include "base/bind.h"
[email protected]8523ba52011-05-22 19:00:5817#include "base/compiler_specific.h"
[email protected]399b8702009-05-01 20:34:0218#include "base/file_util.h"
[email protected]34b2b002009-11-20 06:53:2819#include "base/format_macros.h"
[email protected]084262c2011-12-01 21:12:4720#include "base/memory/weak_ptr.h"
initial.commit586acc5fe2008-07-26 22:42:5221#include "base/message_loop.h"
22#include "base/path_service.h"
23#include "base/process_util.h"
[email protected]528c56d2010-07-30 19:28:4424#include "base/string_number_conversions.h"
[email protected]d8eb84242010-09-25 02:25:0625#include "base/string_piece.h"
[email protected]c3456bb2011-12-12 22:22:1926#include "base/string_split.h"
[email protected]ebbad602011-01-21 23:30:4727#include "base/string_util.h"
[email protected]d8eb84242010-09-25 02:25:0628#include "base/stringprintf.h"
[email protected]be1ce6a72010-08-03 14:35:2229#include "base/utf_string_conversions.h"
initial.commit586acc5fe2008-07-26 22:42:5230#include "net/base/load_flags.h"
[email protected]8202d0c2011-02-23 08:31:1431#include "net/base/mock_host_resolver.h"
[email protected]d8eb84242010-09-25 02:25:0632#include "net/base/net_errors.h"
[email protected]9e743cd2010-03-16 07:03:5333#include "net/base/net_log.h"
34#include "net/base/net_log_unittest.h"
initial.commit586acc5fe2008-07-26 22:42:5235#include "net/base/net_module.h"
36#include "net/base/net_util.h"
[email protected]96adadb2010-08-28 01:16:1737#include "net/base/ssl_connection_status_flags.h"
[email protected]195e77d2009-07-23 19:10:2338#include "net/base/upload_data.h"
[email protected]aa84a7e2012-03-15 21:29:0639#include "net/cookies/cookie_monster.h"
40#include "net/cookies/cookie_store_test_helpers.h"
initial.commit586acc5fe2008-07-26 22:42:5241#include "net/disk_cache/disk_cache.h"
[email protected]ba2f3342009-07-30 18:08:4242#include "net/ftp/ftp_network_layer.h"
initial.commit586acc5fe2008-07-26 22:42:5243#include "net/http/http_cache.h"
44#include "net/http/http_network_layer.h"
[email protected]c3456bb2011-12-12 22:22:1945#include "net/http/http_network_session.h"
[email protected]88e6b6f32010-05-07 23:14:2546#include "net/http/http_request_headers.h"
[email protected]319d9e6f2009-02-18 19:47:2147#include "net/http/http_response_headers.h"
[email protected]63de95b2008-12-10 04:11:2748#include "net/proxy/proxy_service.h"
[email protected]c3456bb2011-12-12 22:22:1949#include "net/socket/ssl_client_socket.h"
[email protected]1b9565c2010-07-21 01:19:3150#include "net/test/test_server.h"
initial.commit586acc5fe2008-07-26 22:42:5251#include "net/url_request/url_request.h"
[email protected]7886a8c2009-08-21 04:11:0952#include "net/url_request/url_request_file_dir_job.h"
[email protected]bcb84f8b2009-08-31 16:20:1453#include "net/url_request/url_request_http_job.h"
[email protected]3c5ca8c2011-09-29 01:14:5154#include "net/url_request/url_request_job_factory.h"
55#include "net/url_request/url_request_redirect_job.h"
[email protected]a5c713f2009-04-16 21:05:4756#include "net/url_request/url_request_test_job.h"
[email protected]d2db0292011-01-26 20:23:4457#include "net/url_request/url_request_test_util.h"
initial.commit586acc5fe2008-07-26 22:42:5258#include "testing/gtest/include/gtest/gtest.h"
[email protected]23887f04f2008-12-02 19:20:1559#include "testing/platform_test.h"
initial.commit586acc5fe2008-07-26 22:42:5260
[email protected]e1acf6f2008-10-27 20:43:3361using base::Time;
62
[email protected]7461a402011-03-24 23:19:5163namespace net {
64
initial.commit586acc5fe2008-07-26 22:42:5265namespace {
66
[email protected]1700c6a2012-02-22 18:07:0767// A subclass of TestServer that uses a statically-configured hostname. This is
68// to work around mysterious failures in chrome_frame_net_tests. See:
69// http://crbug.com/114369
70class LocalHttpTestServer : public TestServer {
71 public:
72 explicit LocalHttpTestServer(const FilePath& document_root)
73 : TestServer(TestServer::TYPE_HTTP,
74 ScopedCustomUrlRequestTestHttpHost::value(),
75 document_root) {}
76 LocalHttpTestServer()
77 : TestServer(TestServer::TYPE_HTTP,
78 ScopedCustomUrlRequestTestHttpHost::value(),
79 FilePath()) {}
80};
81
[email protected]13c8a092010-07-29 06:15:4482const string16 kChrome(ASCIIToUTF16("chrome"));
83const string16 kSecret(ASCIIToUTF16("secret"));
84const string16 kUser(ASCIIToUTF16("user"));
85
[email protected]8a16266e2009-09-10 21:08:3986base::StringPiece TestNetResourceProvider(int key) {
initial.commit586acc5fe2008-07-26 22:42:5287 return "header";
88}
89
[email protected]71c64f62008-11-15 04:36:5190// Do a case-insensitive search through |haystack| for |needle|.
91bool ContainsString(const std::string& haystack, const char* needle) {
92 std::string::const_iterator it =
93 std::search(haystack.begin(),
94 haystack.end(),
95 needle,
96 needle + strlen(needle),
[email protected]07f1cee2010-11-03 03:53:3597 base::CaseInsensitiveCompare<char>());
[email protected]71c64f62008-11-15 04:36:5198 return it != haystack.end();
99}
100
[email protected]661376a2009-04-29 02:04:23101void FillBuffer(char* buffer, size_t len) {
102 static bool called = false;
103 if (!called) {
104 called = true;
105 int seed = static_cast<int>(Time::Now().ToInternalValue());
106 srand(seed);
107 }
108
109 for (size_t i = 0; i < len; i++) {
110 buffer[i] = static_cast<char>(rand());
111 if (!buffer[i])
112 buffer[i] = 'g';
113 }
114}
115
[email protected]7461a402011-03-24 23:19:51116scoped_refptr<UploadData> CreateSimpleUploadData(const char* data) {
117 scoped_refptr<UploadData> upload(new UploadData);
[email protected]195e77d2009-07-23 19:10:23118 upload->AppendBytes(data, strlen(data));
119 return upload;
120}
121
[email protected]96adadb2010-08-28 01:16:17122// Verify that the SSLInfo of a successful SSL connection has valid values.
[email protected]7461a402011-03-24 23:19:51123void CheckSSLInfo(const SSLInfo& ssl_info) {
[email protected]89af3d92010-10-15 20:31:37124 // Allow ChromeFrame fake SSLInfo to get through.
[email protected]c679be22010-08-29 15:20:05125 if (ssl_info.cert.get() &&
[email protected]89af3d92010-10-15 20:31:37126 ssl_info.cert.get()->issuer().GetDisplayName() == "Chrome Internal") {
127 // -1 means unknown.
128 EXPECT_EQ(ssl_info.security_bits, -1);
[email protected]c679be22010-08-29 15:20:05129 return;
[email protected]89af3d92010-10-15 20:31:37130 }
131
[email protected]96adadb2010-08-28 01:16:17132 // -1 means unknown. 0 means no encryption.
133 EXPECT_GT(ssl_info.security_bits, 0);
134
135 // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated.
[email protected]7461a402011-03-24 23:19:51136 int cipher_suite = SSLConnectionStatusToCipherSuite(
[email protected]96adadb2010-08-28 01:16:17137 ssl_info.connection_status);
138 EXPECT_NE(0, cipher_suite);
139}
140
[email protected]69d7ff442012-02-13 22:41:27141bool FingerprintsEqual(const FingerprintVector& a, const FingerprintVector& b) {
142 size_t size = a.size();
143
144 if (size != b.size())
145 return false;
146
147 for (size_t i = 0; i < size; ++i) {
148 if (!a[i].Equals(b[i]))
149 return false;
150 }
151
152 return true;
153}
154
[email protected]9396b252008-09-29 17:29:38155} // namespace
initial.commit586acc5fe2008-07-26 22:42:52156
[email protected]4c76d7c2011-04-15 19:14:12157// A network delegate that blocks requests, optionally cancelling or redirecting
158// them.
159class BlockingNetworkDelegate : public TestNetworkDelegate {
160 public:
[email protected]8523ba52011-05-22 19:00:58161 BlockingNetworkDelegate()
[email protected]9045b8822012-01-13 20:35:35162 : retval_(ERR_IO_PENDING),
163 callback_retval_(OK),
[email protected]c2911d72011-10-03 22:16:36164 auth_retval_(NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING),
165 auth_callback_retval_(
166 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION),
[email protected]084262c2011-12-01 21:12:47167 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {}
[email protected]4c76d7c2011-04-15 19:14:12168
[email protected]3cd384c602011-08-31 16:12:36169 void set_retval(int retval) { retval_ = retval; }
[email protected]4c76d7c2011-04-15 19:14:12170 void set_callback_retval(int retval) { callback_retval_ = retval; }
171 void set_redirect_url(const GURL& url) { redirect_url_ = url; }
[email protected]c2911d72011-10-03 22:16:36172 void set_auth_retval(NetworkDelegate::AuthRequiredResponse retval) {
173 auth_retval_ = retval; }
174 void set_auth_callback_retval(NetworkDelegate::AuthRequiredResponse retval) {
175 auth_callback_retval_ = retval; }
176 void set_auth_credentials(const AuthCredentials& auth_credentials) {
177 auth_credentials_ = auth_credentials;
178 }
[email protected]4c76d7c2011-04-15 19:14:12179
180 private:
[email protected]084262c2011-12-01 21:12:47181 // TestNetworkDelegate implementation.
[email protected]9045b8822012-01-13 20:35:35182 virtual int OnBeforeURLRequest(URLRequest* request,
183 const CompletionCallback& callback,
[email protected]084262c2011-12-01 21:12:47184 GURL* new_url) OVERRIDE {
[email protected]bb445ce2011-05-03 18:23:57185 if (redirect_url_ == request->url()) {
186 // We've already seen this request and redirected elsewhere.
[email protected]9045b8822012-01-13 20:35:35187 return OK;
[email protected]bb445ce2011-05-03 18:23:57188 }
189
[email protected]4c76d7c2011-04-15 19:14:12190 TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url);
191
192 if (!redirect_url_.is_empty())
193 *new_url = redirect_url_;
[email protected]3cd384c602011-08-31 16:12:36194
[email protected]9045b8822012-01-13 20:35:35195 if (retval_ != ERR_IO_PENDING)
[email protected]3cd384c602011-08-31 16:12:36196 return retval_;
197
[email protected]8523ba52011-05-22 19:00:58198 MessageLoop::current()->PostTask(
199 FROM_HERE,
[email protected]084262c2011-12-01 21:12:47200 base::Bind(&BlockingNetworkDelegate::DoCallback,
201 weak_factory_.GetWeakPtr(), callback));
[email protected]9045b8822012-01-13 20:35:35202 return ERR_IO_PENDING;
[email protected]4c76d7c2011-04-15 19:14:12203 }
204
[email protected]c2911d72011-10-03 22:16:36205 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired(
206 URLRequest* request,
207 const AuthChallengeInfo& auth_info,
208 const AuthCallback& callback,
209 AuthCredentials* credentials) OVERRIDE {
210 TestNetworkDelegate::OnAuthRequired(request, auth_info, callback,
211 credentials);
212 switch (auth_retval_) {
213 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION:
214 break;
215 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH:
216 *credentials = auth_credentials_;
217 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH:
218 break;
219 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING:
220 MessageLoop::current()->PostTask(
221 FROM_HERE,
[email protected]084262c2011-12-01 21:12:47222 base::Bind(&BlockingNetworkDelegate::DoAuthCallback,
223 weak_factory_.GetWeakPtr(), callback, credentials));
[email protected]c2911d72011-10-03 22:16:36224 break;
225 }
226 return auth_retval_;
227 }
228
[email protected]9045b8822012-01-13 20:35:35229 void DoCallback(const CompletionCallback& callback) {
[email protected]084262c2011-12-01 21:12:47230 callback.Run(callback_retval_);
[email protected]4c76d7c2011-04-15 19:14:12231 }
232
[email protected]c2911d72011-10-03 22:16:36233 void DoAuthCallback(const AuthCallback& callback,
234 AuthCredentials* credentials) {
235 if (auth_callback_retval_ ==
236 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH) {
237 *credentials = auth_credentials_;
238 }
239 callback.Run(auth_callback_retval_);
240 }
241
242
[email protected]3cd384c602011-08-31 16:12:36243 int retval_;
[email protected]4c76d7c2011-04-15 19:14:12244 int callback_retval_;
245 GURL redirect_url_;
[email protected]c2911d72011-10-03 22:16:36246 NetworkDelegate::AuthRequiredResponse auth_retval_;
247 NetworkDelegate::AuthRequiredResponse auth_callback_retval_;
248 AuthCredentials auth_credentials_;
[email protected]084262c2011-12-01 21:12:47249 base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_;
[email protected]4c76d7c2011-04-15 19:14:12250};
251
[email protected]9045b8822012-01-13 20:35:35252// A network delegate that allows blocking requests until a callback function is
253// called.
254class BlockingNetworkDelegateWithManualCallback : public TestNetworkDelegate {
255 public:
256 enum State {
257 NOT_BLOCKED = 0,
258 ON_BEFORE_URL_REQUEST = 1 << 0,
259 ON_BEFORE_SEND_HEADERS = 1 << 1,
260 ON_HEADERS_RECEIVED = 1 << 2,
261 ON_AUTH_REQUIRED = 1 << 3
262 };
263
264 BlockingNetworkDelegateWithManualCallback()
265 : block_on_(0),
266 state_(NOT_BLOCKED) {
267 }
268
269 // Activates blocking on |state|.
270 void BlockOn(State state) {
271 block_on_ |= state;
272 }
273
274 void DoCallback(int rv) {
275 ASSERT_NE(NOT_BLOCKED, state_);
276 CompletionCallback callback = callback_;
277 Reset();
278 callback.Run(rv);
279 }
280
281 void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response) {
282 ASSERT_EQ(ON_AUTH_REQUIRED, state_);
283 AuthCallback auth_callback = auth_callback_;
284 Reset();
285 auth_callback.Run(response);
286 }
287
288 // Runs the message loop until |state| is reached.
289 void WaitForState(State state) {
290 while (state_ != state)
291 MessageLoop::current()->RunAllPending();
292 }
293
294 private:
295 // TestNetworkDelegate implementation.
296 virtual int OnBeforeURLRequest(URLRequest* request,
297 const CompletionCallback& callback,
298 GURL* new_url) OVERRIDE {
299 TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url);
300 if ((block_on_ & ON_BEFORE_URL_REQUEST) == 0) {
301 return OK;
302 } else {
303 state_ = ON_BEFORE_URL_REQUEST;
304 callback_ = callback;
305 return ERR_IO_PENDING;
306 }
307 }
308
309 virtual int OnBeforeSendHeaders(URLRequest* request,
310 const CompletionCallback& callback,
311 HttpRequestHeaders* headers) OVERRIDE {
312 TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers);
313 if ((block_on_ & ON_BEFORE_SEND_HEADERS) == 0) {
314 return OK;
315 } else {
316 state_ = ON_BEFORE_SEND_HEADERS;
317 callback_ = callback;
318 return ERR_IO_PENDING;
319 }
320 }
321
322 virtual int OnHeadersReceived(
323 URLRequest* request,
324 const CompletionCallback& callback,
325 HttpResponseHeaders* original_response_headers,
326 scoped_refptr<HttpResponseHeaders>* override_response_headers)
327 OVERRIDE {
328 TestNetworkDelegate::OnHeadersReceived(
329 request, callback, original_response_headers,
330 override_response_headers);
331 if ((block_on_ & ON_HEADERS_RECEIVED) == 0) {
332 return OK;
333 } else {
334 state_ = ON_HEADERS_RECEIVED;
335 callback_ = callback;
336 return ERR_IO_PENDING;
337 }
338 }
339
340 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired(
341 URLRequest* request,
342 const AuthChallengeInfo& auth_info,
343 const AuthCallback& callback,
344 AuthCredentials* credentials) OVERRIDE {
345 if ((block_on_ & ON_AUTH_REQUIRED) == 0) {
346 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
347 } else {
348 state_ = ON_AUTH_REQUIRED;
349 auth_callback_ = callback;
350 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING;
351 }
352 }
353
354 void Reset() {
355 state_ = NOT_BLOCKED;
356 callback_.Reset();
357 auth_callback_.Reset();
358 }
359
360 int block_on_; // Bit mask on which states to block.
361 State state_;
362 CompletionCallback callback_;
363 AuthCallback auth_callback_;
364};
365
366
[email protected]3c5ca8c2011-09-29 01:14:51367// A simple Interceptor that returns a pre-built URLRequestJob one time.
368class TestJobInterceptor : public URLRequestJobFactory::Interceptor {
369 public:
370 TestJobInterceptor()
371 : main_intercept_job_(NULL) {
372 }
373
374 virtual URLRequestJob* MaybeIntercept(URLRequest* request) const OVERRIDE {
375 URLRequestJob* job = main_intercept_job_;
376 main_intercept_job_ = NULL;
377 return job;
378 }
379
380 virtual URLRequestJob* MaybeInterceptRedirect(
381 const GURL& location, URLRequest* request) const OVERRIDE {
382 return NULL;
383 }
384
385 virtual URLRequestJob* MaybeInterceptResponse(
386 URLRequest* request) const OVERRIDE {
387 return NULL;
388 }
389
390 void set_main_intercept_job(URLRequestJob* job) {
391 main_intercept_job_ = job;
392 }
393
394 private:
395 mutable URLRequestJob* main_intercept_job_;
396};
397
[email protected]7a0bb4bf2008-11-19 21:41:48398// Inherit PlatformTest since we require the autorelease pool on Mac OS X.f
399class URLRequestTest : public PlatformTest {
[email protected]abb26092010-11-11 22:19:00400 public:
[email protected]87a09a92011-07-14 15:50:50401 URLRequestTest() : default_context_(new TestURLRequestContext(true)) {
402 default_context_->set_network_delegate(&default_network_delegate_);
403 default_context_->Init();
404 }
405
[email protected]abb26092010-11-11 22:19:00406 static void SetUpTestCase() {
[email protected]7461a402011-03-24 23:19:51407 URLRequest::AllowFileAccess();
[email protected]abb26092010-11-11 22:19:00408 }
[email protected]87a09a92011-07-14 15:50:50409
[email protected]3c5ca8c2011-09-29 01:14:51410 // Adds the TestJobInterceptor to the default context.
411 TestJobInterceptor* AddTestInterceptor() {
412 TestJobInterceptor* interceptor = new TestJobInterceptor();
413 default_context_->set_job_factory(&job_factory_);
414 job_factory_.AddInterceptor(interceptor);
415 return interceptor;
416 }
417
[email protected]87a09a92011-07-14 15:50:50418 protected:
419 TestNetworkDelegate default_network_delegate_; // must outlive URLRequest
420 scoped_refptr<TestURLRequestContext> default_context_;
[email protected]3c5ca8c2011-09-29 01:14:51421 URLRequestJobFactory job_factory_;
[email protected]7a0bb4bf2008-11-19 21:41:48422};
423
[email protected]b89290212009-08-14 22:37:35424class URLRequestTestHTTP : public URLRequestTest {
[email protected]95409e12010-08-17 20:07:11425 public:
426 URLRequestTestHTTP()
[email protected]1700c6a2012-02-22 18:07:07427 : test_server_(FilePath(FILE_PATH_LITERAL(
[email protected]95409e12010-08-17 20:07:11428 "net/data/url_request_unittest"))) {
429 }
430
[email protected]b89290212009-08-14 22:37:35431 protected:
[email protected]21184962011-10-26 00:50:30432 // Requests |redirect_url|, which must return a HTTP 3xx redirect.
433 // |request_method| is the method to use for the initial request.
434 // |redirect_method| is the method that is expected to be used for the second
435 // request, after redirection.
436 // If |include_data| is true, data is uploaded with the request. The
437 // response body is expected to match it exactly, if and only if
438 // |request_method| == |redirect_method|.
439 void HTTPRedirectMethodTest(const GURL& redirect_url,
440 const std::string& request_method,
441 const std::string& redirect_method,
442 bool include_data) {
443 static const char kData[] = "hello world";
444 TestDelegate d;
445 TestURLRequest req(redirect_url, &d);
446 req.set_context(default_context_);
447 req.set_method(request_method);
448 if (include_data) {
449 req.set_upload(CreateSimpleUploadData(kData).get());
450 HttpRequestHeaders headers;
451 headers.SetHeader(HttpRequestHeaders::kContentLength,
452 base::UintToString(arraysize(kData) - 1));
453 req.SetExtraRequestHeaders(headers);
454 }
455 req.Start();
456 MessageLoop::current()->Run();
457 EXPECT_EQ(redirect_method, req.method());
458 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
459 EXPECT_EQ(OK, req.status().error());
460 if (include_data) {
461 if (request_method == redirect_method) {
462 EXPECT_EQ(kData, d.data_received());
463 } else {
464 EXPECT_NE(kData, d.data_received());
465 }
466 }
467 if (HasFailure())
468 LOG(WARNING) << "Request method was: " << request_method;
469 }
470
[email protected]762d2db2010-01-11 19:03:01471 void HTTPUploadDataOperationTest(const std::string& method) {
[email protected]762d2db2010-01-11 19:03:01472 const int kMsgSize = 20000; // multiple of 10
473 const int kIterations = 50;
474 char *uploadBytes = new char[kMsgSize+1];
475 char *ptr = uploadBytes;
476 char marker = 'a';
477 for (int idx = 0; idx < kMsgSize/10; idx++) {
478 memcpy(ptr, "----------", 10);
479 ptr += 10;
480 if (idx % 100 == 0) {
481 ptr--;
482 *ptr++ = marker;
483 if (++marker > 'z')
484 marker = 'a';
485 }
486 }
487 uploadBytes[kMsgSize] = '\0';
488
[email protected]762d2db2010-01-11 19:03:01489 for (int i = 0; i < kIterations; ++i) {
490 TestDelegate d;
[email protected]7461a402011-03-24 23:19:51491 URLRequest r(test_server_.GetURL("echo"), &d);
[email protected]87a09a92011-07-14 15:50:50492 r.set_context(default_context_);
[email protected]762d2db2010-01-11 19:03:01493 r.set_method(method.c_str());
494
495 r.AppendBytesToUpload(uploadBytes, kMsgSize);
496
497 r.Start();
498 EXPECT_TRUE(r.is_pending());
499
500 MessageLoop::current()->Run();
501
502 ASSERT_EQ(1, d.response_started_count()) << "request failed: " <<
[email protected]d0cc35b2011-09-08 12:02:05503 (int) r.status().status() << ", os error: " << r.status().error();
[email protected]762d2db2010-01-11 19:03:01504
505 EXPECT_FALSE(d.received_data_before_response());
506 EXPECT_EQ(uploadBytes, d.data_received());
507 EXPECT_EQ(memcmp(uploadBytes, d.data_received().c_str(), kMsgSize), 0);
508 EXPECT_EQ(d.data_received().compare(uploadBytes), 0);
509 }
510 delete[] uploadBytes;
511 }
512
[email protected]699efe602011-01-25 07:17:11513 void AddChunksToUpload(TestURLRequest* r) {
[email protected]0c9bf872011-03-04 17:53:22514 r->AppendChunkToUpload("a", 1, false);
515 r->AppendChunkToUpload("bcd", 3, false);
516 r->AppendChunkToUpload("this is a longer chunk than before.", 35, false);
517 r->AppendChunkToUpload("\r\n\r\n", 4, false);
518 r->AppendChunkToUpload("0", 1, false);
519 r->AppendChunkToUpload("2323", 4, true);
[email protected]699efe602011-01-25 07:17:11520 }
521
522 void VerifyReceivedDataMatchesChunks(TestURLRequest* r, TestDelegate* d) {
523 // This should match the chunks sent by AddChunksToUpload().
524 const char* expected_data =
525 "abcdthis is a longer chunk than before.\r\n\r\n02323";
526
527 ASSERT_EQ(1, d->response_started_count()) << "request failed: " <<
[email protected]d0cc35b2011-09-08 12:02:05528 (int) r->status().status() << ", os error: " << r->status().error();
[email protected]699efe602011-01-25 07:17:11529
530 EXPECT_FALSE(d->received_data_before_response());
531
532 ASSERT_EQ(strlen(expected_data), static_cast<size_t>(d->bytes_received()));
533 EXPECT_EQ(0, memcmp(d->data_received().c_str(), expected_data,
534 strlen(expected_data)));
535 }
536
[email protected]1700c6a2012-02-22 18:07:07537 LocalHttpTestServer test_server_;
[email protected]b89290212009-08-14 22:37:35538};
539
[email protected]95409e12010-08-17 20:07:11540// In this unit test, we're using the HTTPTestServer as a proxy server and
541// issuing a CONNECT request with the magic host name "www.redirect.com".
542// The HTTPTestServer will return a 302 response, which we should not
543// follow.
[email protected]8eb127e2011-09-16 10:47:47544TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) {
[email protected]95409e12010-08-17 20:07:11545 ASSERT_TRUE(test_server_.Start());
546
[email protected]87a09a92011-07-14 15:50:50547 TestNetworkDelegate network_delegate; // must outlive URLRequest
548 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
549 context->SetProxyFromString(test_server_.host_port_pair().ToString());
550 context->set_network_delegate(&network_delegate);
551 context->Init();
552
[email protected]d1ec59082009-02-11 02:48:15553 TestDelegate d;
554 {
[email protected]7461a402011-03-24 23:19:51555 URLRequest r(GURL("https://www.redirect.com/"), &d);
[email protected]87a09a92011-07-14 15:50:50556 r.set_context(context);
[email protected]d1ec59082009-02-11 02:48:15557 r.Start();
558 EXPECT_TRUE(r.is_pending());
559
560 MessageLoop::current()->Run();
561
[email protected]7461a402011-03-24 23:19:51562 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
[email protected]d0cc35b2011-09-08 12:02:05563 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().error());
[email protected]dc651782009-02-14 01:45:08564 EXPECT_EQ(1, d.response_started_count());
[email protected]d1ec59082009-02-11 02:48:15565 // We should not have followed the redirect.
566 EXPECT_EQ(0, d.received_redirect_count());
567 }
568}
569
[email protected]8202d0c2011-02-23 08:31:14570// This is the same as the previous test, but checks that the network delegate
571// registers the error.
[email protected]4841b772011-11-04 10:36:06572// This test was disabled because it made chrome_frame_net_tests hang
573// (see bug 102991).
574TEST_F(URLRequestTestHTTP, DISABLED_NetworkDelegateTunnelConnectionFailed) {
[email protected]8202d0c2011-02-23 08:31:14575 ASSERT_TRUE(test_server_.Start());
576
[email protected]87a09a92011-07-14 15:50:50577 TestNetworkDelegate network_delegate; // must outlive URLRequest
578 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
579 context->SetProxyFromString(test_server_.host_port_pair().ToString());
580 context->set_network_delegate(&network_delegate);
581 context->Init();
582
[email protected]8202d0c2011-02-23 08:31:14583 TestDelegate d;
584 {
[email protected]7461a402011-03-24 23:19:51585 URLRequest r(GURL("https://www.redirect.com/"), &d);
[email protected]8202d0c2011-02-23 08:31:14586 r.set_context(context);
[email protected]8202d0c2011-02-23 08:31:14587 r.Start();
588 EXPECT_TRUE(r.is_pending());
589
590 MessageLoop::current()->Run();
591
[email protected]7461a402011-03-24 23:19:51592 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
[email protected]d0cc35b2011-09-08 12:02:05593 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().error());
[email protected]8202d0c2011-02-23 08:31:14594 EXPECT_EQ(1, d.response_started_count());
595 // We should not have followed the redirect.
596 EXPECT_EQ(0, d.received_redirect_count());
597
598 EXPECT_EQ(1, network_delegate.error_count());
[email protected]d0cc35b2011-09-08 12:02:05599 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, network_delegate.last_error());
[email protected]8202d0c2011-02-23 08:31:14600 }
601}
602
[email protected]4c76d7c2011-04-15 19:14:12603// Tests that the network delegate can block and cancel a request.
604TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) {
605 ASSERT_TRUE(test_server_.Start());
606
607 TestDelegate d;
608 BlockingNetworkDelegate network_delegate;
609 network_delegate.set_callback_retval(ERR_EMPTY_RESPONSE);
610
[email protected]87a09a92011-07-14 15:50:50611 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
612 context->SetProxyFromString(test_server_.host_port_pair().ToString());
613 context->set_network_delegate(&network_delegate);
614 context->Init();
615
[email protected]4c76d7c2011-04-15 19:14:12616 {
617 TestURLRequest r(test_server_.GetURL(""), &d);
[email protected]4c76d7c2011-04-15 19:14:12618 r.set_context(context);
619
620 r.Start();
621 MessageLoop::current()->Run();
622
623 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
[email protected]d0cc35b2011-09-08 12:02:05624 EXPECT_EQ(ERR_EMPTY_RESPONSE, r.status().error());
[email protected]4c76d7c2011-04-15 19:14:12625 EXPECT_EQ(1, network_delegate.created_requests());
626 EXPECT_EQ(0, network_delegate.destroyed_requests());
627 }
628 EXPECT_EQ(1, network_delegate.destroyed_requests());
629}
630
[email protected]3cd384c602011-08-31 16:12:36631// Tests that the network delegate can cancel a request synchronously.
632TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously) {
633 ASSERT_TRUE(test_server_.Start());
634
635 TestDelegate d;
636 BlockingNetworkDelegate network_delegate;
637 network_delegate.set_retval(ERR_EMPTY_RESPONSE);
638
639 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
640 context->SetProxyFromString(test_server_.host_port_pair().ToString());
641 context->set_network_delegate(&network_delegate);
642 context->Init();
643
644 {
645 TestURLRequest r(test_server_.GetURL(""), &d);
646 r.set_context(context);
647
648 r.Start();
649 MessageLoop::current()->Run();
650
651 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
[email protected]d0cc35b2011-09-08 12:02:05652 EXPECT_EQ(ERR_EMPTY_RESPONSE, r.status().error());
[email protected]3cd384c602011-08-31 16:12:36653 EXPECT_EQ(1, network_delegate.created_requests());
654 EXPECT_EQ(0, network_delegate.destroyed_requests());
655 }
656 EXPECT_EQ(1, network_delegate.destroyed_requests());
657}
658
[email protected]4c76d7c2011-04-15 19:14:12659// Tests that the network delegate can block and redirect a request to a new
660// URL.
661TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) {
662 ASSERT_TRUE(test_server_.Start());
663
664 TestDelegate d;
665 BlockingNetworkDelegate network_delegate;
666 GURL redirect_url(test_server_.GetURL("simple.html"));
667 network_delegate.set_redirect_url(redirect_url);
668
[email protected]87a09a92011-07-14 15:50:50669 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
670 context->SetProxyFromString(test_server_.host_port_pair().ToString());
671 context->set_network_delegate(&network_delegate);
672 context->Init();
673
[email protected]4c76d7c2011-04-15 19:14:12674 {
[email protected]8f1ac082011-04-19 21:14:13675 GURL original_url(test_server_.GetURL("empty.html"));
676 TestURLRequest r(original_url, &d);
[email protected]4c76d7c2011-04-15 19:14:12677 r.set_context(context);
678
679 r.Start();
680 MessageLoop::current()->Run();
681
682 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
[email protected]d0cc35b2011-09-08 12:02:05683 EXPECT_EQ(0, r.status().error());
[email protected]4c76d7c2011-04-15 19:14:12684 EXPECT_EQ(redirect_url, r.url());
[email protected]8f1ac082011-04-19 21:14:13685 EXPECT_EQ(original_url, r.original_url());
686 EXPECT_EQ(2U, r.url_chain().size());
[email protected]4c76d7c2011-04-15 19:14:12687 EXPECT_EQ(1, network_delegate.created_requests());
688 EXPECT_EQ(0, network_delegate.destroyed_requests());
689 }
690 EXPECT_EQ(1, network_delegate.destroyed_requests());
691}
692
[email protected]3c5ca8c2011-09-29 01:14:51693// Tests that redirects caused by the network delegate preserve POST data.
694TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) {
695 ASSERT_TRUE(test_server_.Start());
696
697 const char kData[] = "hello world";
698
699 TestDelegate d;
700 BlockingNetworkDelegate network_delegate;
701 GURL redirect_url(test_server_.GetURL("echo"));
702 network_delegate.set_redirect_url(redirect_url);
703
704 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
705 context->set_network_delegate(&network_delegate);
706 context->Init();
707
708 {
709 GURL original_url(test_server_.GetURL("empty.html"));
710 TestURLRequest r(original_url, &d);
711 r.set_context(context);
712 r.set_method("POST");
713 r.set_upload(CreateSimpleUploadData(kData).get());
714 HttpRequestHeaders headers;
715 headers.SetHeader(HttpRequestHeaders::kContentLength,
716 base::UintToString(arraysize(kData) - 1));
717 r.SetExtraRequestHeaders(headers);
[email protected]3c5ca8c2011-09-29 01:14:51718 r.Start();
719 MessageLoop::current()->Run();
720
721 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
722 EXPECT_EQ(0, r.status().error());
723 EXPECT_EQ(redirect_url, r.url());
724 EXPECT_EQ(original_url, r.original_url());
725 EXPECT_EQ(2U, r.url_chain().size());
726 EXPECT_EQ(1, network_delegate.created_requests());
727 EXPECT_EQ(0, network_delegate.destroyed_requests());
728 EXPECT_EQ("POST", r.method());
729 EXPECT_EQ(kData, d.data_received());
730 }
731 EXPECT_EQ(1, network_delegate.destroyed_requests());
732}
733
[email protected]c2911d72011-10-03 22:16:36734// Tests that the network delegate can synchronously complete OnAuthRequired
735// by taking no action. This indicates that the NetworkDelegate does not want to
736// handle the challenge, and is passing the buck along to the
737// URLRequest::Delegate.
738TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) {
739 ASSERT_TRUE(test_server_.Start());
740
741 TestDelegate d;
742 BlockingNetworkDelegate network_delegate;
743 network_delegate.set_auth_retval(
744 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION);
745
746 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
747 context->set_network_delegate(&network_delegate);
748 context->Init();
749
[email protected]f3cf9802011-10-28 18:44:58750 d.set_credentials(AuthCredentials(kUser, kSecret));
[email protected]c2911d72011-10-03 22:16:36751
752 {
753 GURL url(test_server_.GetURL("auth-basic"));
754 TestURLRequest r(url, &d);
755 r.set_context(context);
756 r.Start();
757 MessageLoop::current()->Run();
758
759 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
760 EXPECT_EQ(0, r.status().error());
761 EXPECT_EQ(200, r.GetResponseCode());
762 EXPECT_TRUE(d.auth_required_called());
763 EXPECT_EQ(1, network_delegate.created_requests());
764 EXPECT_EQ(0, network_delegate.destroyed_requests());
765 }
766 EXPECT_EQ(1, network_delegate.destroyed_requests());
767}
768
769// Tests that the network delegate can synchronously complete OnAuthRequired
770// by setting credentials.
771TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) {
772 ASSERT_TRUE(test_server_.Start());
773
774 TestDelegate d;
775 BlockingNetworkDelegate network_delegate;
776 network_delegate.set_auth_retval(
777 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
778
[email protected]f3cf9802011-10-28 18:44:58779 network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret));
[email protected]c2911d72011-10-03 22:16:36780
781 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
782 context->set_network_delegate(&network_delegate);
783 context->Init();
784
785 {
786 GURL url(test_server_.GetURL("auth-basic"));
787 TestURLRequest r(url, &d);
788 r.set_context(context);
789 r.Start();
790 MessageLoop::current()->Run();
791
792 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
793 EXPECT_EQ(0, r.status().error());
794 EXPECT_EQ(200, r.GetResponseCode());
795 EXPECT_FALSE(d.auth_required_called());
796 EXPECT_EQ(1, network_delegate.created_requests());
797 EXPECT_EQ(0, network_delegate.destroyed_requests());
798 }
799 EXPECT_EQ(1, network_delegate.destroyed_requests());
800}
801
802// Tests that the network delegate can synchronously complete OnAuthRequired
803// by cancelling authentication.
804TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) {
805 ASSERT_TRUE(test_server_.Start());
806
807 TestDelegate d;
808 BlockingNetworkDelegate network_delegate;
809 network_delegate.set_auth_retval(
810 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH);
811
812 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
813 context->set_network_delegate(&network_delegate);
814 context->Init();
815
816 {
817 GURL url(test_server_.GetURL("auth-basic"));
818 TestURLRequest r(url, &d);
819 r.set_context(context);
820 r.Start();
821 MessageLoop::current()->Run();
822
823 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
824 EXPECT_EQ(OK, r.status().error());
825 EXPECT_EQ(401, r.GetResponseCode());
826 EXPECT_FALSE(d.auth_required_called());
827 EXPECT_EQ(1, network_delegate.created_requests());
828 EXPECT_EQ(0, network_delegate.destroyed_requests());
829 }
830 EXPECT_EQ(1, network_delegate.destroyed_requests());
831}
832
833// Tests that the network delegate can asynchronously complete OnAuthRequired
834// by taking no action. This indicates that the NetworkDelegate does not want
835// to handle the challenge, and is passing the buck along to the
836// URLRequest::Delegate.
837TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) {
838 ASSERT_TRUE(test_server_.Start());
839
840 TestDelegate d;
841 BlockingNetworkDelegate network_delegate;
842 network_delegate.set_auth_retval(
843 NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING);
844 network_delegate.set_auth_callback_retval(
845 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION);
846
847 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
848 context->set_network_delegate(&network_delegate);
849 context->Init();
850
[email protected]f3cf9802011-10-28 18:44:58851 d.set_credentials(AuthCredentials(kUser, kSecret));
[email protected]c2911d72011-10-03 22:16:36852
853 {
854 GURL url(test_server_.GetURL("auth-basic"));
855 TestURLRequest r(url, &d);
856 r.set_context(context);
857 r.Start();
858 MessageLoop::current()->Run();
859
860 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
861 EXPECT_EQ(0, r.status().error());
862 EXPECT_EQ(200, r.GetResponseCode());
863 EXPECT_TRUE(d.auth_required_called());
864 EXPECT_EQ(1, network_delegate.created_requests());
865 EXPECT_EQ(0, network_delegate.destroyed_requests());
866 }
867 EXPECT_EQ(1, network_delegate.destroyed_requests());
868}
869
870// Tests that the network delegate can asynchronously complete OnAuthRequired
871// by setting credentials.
872TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) {
873 ASSERT_TRUE(test_server_.Start());
874
875 TestDelegate d;
876 BlockingNetworkDelegate network_delegate;
877 network_delegate.set_auth_retval(
878 NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING);
879 network_delegate.set_auth_callback_retval(
880 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
881
[email protected]f3cf9802011-10-28 18:44:58882 AuthCredentials auth_credentials(kUser, kSecret);
[email protected]c2911d72011-10-03 22:16:36883 network_delegate.set_auth_credentials(auth_credentials);
884
885 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
886 context->set_network_delegate(&network_delegate);
887 context->Init();
888
889 {
890 GURL url(test_server_.GetURL("auth-basic"));
891 TestURLRequest r(url, &d);
892 r.set_context(context);
893 r.Start();
894 MessageLoop::current()->Run();
895
896 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
897 EXPECT_EQ(0, r.status().error());
898
899 EXPECT_EQ(200, r.GetResponseCode());
900 EXPECT_FALSE(d.auth_required_called());
901 EXPECT_EQ(1, network_delegate.created_requests());
902 EXPECT_EQ(0, network_delegate.destroyed_requests());
903 }
904 EXPECT_EQ(1, network_delegate.destroyed_requests());
905}
906
907// Tests that the network delegate can asynchronously complete OnAuthRequired
908// by cancelling authentication.
909TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) {
910 ASSERT_TRUE(test_server_.Start());
911
912 TestDelegate d;
913 BlockingNetworkDelegate network_delegate;
914 network_delegate.set_auth_retval(
915 NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING);
916 network_delegate.set_auth_callback_retval(
917 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH);
918
919 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
920 context->set_network_delegate(&network_delegate);
921 context->Init();
922
923 {
924 GURL url(test_server_.GetURL("auth-basic"));
925 TestURLRequest r(url, &d);
926 r.set_context(context);
927 r.Start();
928 MessageLoop::current()->Run();
929
930 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
931 EXPECT_EQ(OK, r.status().error());
932 EXPECT_EQ(401, r.GetResponseCode());
933 EXPECT_FALSE(d.auth_required_called());
934 EXPECT_EQ(1, network_delegate.created_requests());
935 EXPECT_EQ(0, network_delegate.destroyed_requests());
936 }
937 EXPECT_EQ(1, network_delegate.destroyed_requests());
938}
939
[email protected]9045b8822012-01-13 20:35:35940// Tests that we can handle when a network request was canceled while we were
941// waiting for the network delegate.
942// Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback.
943TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) {
944 ASSERT_TRUE(test_server_.Start());
945
946 TestDelegate d;
947 BlockingNetworkDelegateWithManualCallback network_delegate;
948 network_delegate.BlockOn(
949 BlockingNetworkDelegateWithManualCallback::ON_BEFORE_URL_REQUEST);
950
951 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
952 context->set_network_delegate(&network_delegate);
953 context->Init();
954
955 {
956 TestURLRequest r(test_server_.GetURL(""), &d);
957 r.set_context(context);
958
959 r.Start();
960 network_delegate.WaitForState(
961 BlockingNetworkDelegateWithManualCallback::ON_BEFORE_URL_REQUEST);
962 EXPECT_EQ(0, network_delegate.completed_requests());
963 // Cancel before callback.
964 r.Cancel();
965 // Ensure that network delegate is notified.
966 EXPECT_EQ(1, network_delegate.completed_requests());
967 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
968 EXPECT_EQ(ERR_ABORTED, r.status().error());
969 EXPECT_EQ(1, network_delegate.created_requests());
970 EXPECT_EQ(0, network_delegate.destroyed_requests());
971 }
972 EXPECT_EQ(1, network_delegate.destroyed_requests());
973}
974
975// Tests that we can handle when a network request was canceled while we were
976// waiting for the network delegate.
977// Part 2: Request is cancelled while waiting for OnBeforeSendHeaders callback.
978TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) {
979 ASSERT_TRUE(test_server_.Start());
980
981 TestDelegate d;
982 BlockingNetworkDelegateWithManualCallback network_delegate;
983 network_delegate.BlockOn(
984 BlockingNetworkDelegateWithManualCallback::ON_BEFORE_SEND_HEADERS);
985
986 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
987 context->set_network_delegate(&network_delegate);
988 context->Init();
989
990 {
991 TestURLRequest r(test_server_.GetURL(""), &d);
992 r.set_context(context);
993
994 r.Start();
995 network_delegate.WaitForState(
996 BlockingNetworkDelegateWithManualCallback::ON_BEFORE_SEND_HEADERS);
997 EXPECT_EQ(0, network_delegate.completed_requests());
998 // Cancel before callback.
999 r.Cancel();
1000 // Ensure that network delegate is notified.
1001 EXPECT_EQ(1, network_delegate.completed_requests());
1002 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
1003 EXPECT_EQ(ERR_ABORTED, r.status().error());
1004 EXPECT_EQ(1, network_delegate.created_requests());
1005 EXPECT_EQ(0, network_delegate.destroyed_requests());
1006 }
1007 EXPECT_EQ(1, network_delegate.destroyed_requests());
1008}
1009
1010// Tests that we can handle when a network request was canceled while we were
1011// waiting for the network delegate.
1012// Part 3: Request is cancelled while waiting for OnHeadersReceived callback.
1013TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) {
1014 ASSERT_TRUE(test_server_.Start());
1015
1016 TestDelegate d;
1017 BlockingNetworkDelegateWithManualCallback network_delegate;
1018 network_delegate.BlockOn(
1019 BlockingNetworkDelegateWithManualCallback::ON_HEADERS_RECEIVED);
1020
1021 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
1022 context->set_network_delegate(&network_delegate);
1023 context->Init();
1024
1025 {
1026 TestURLRequest r(test_server_.GetURL(""), &d);
1027 r.set_context(context);
1028
1029 r.Start();
1030 network_delegate.WaitForState(
1031 BlockingNetworkDelegateWithManualCallback::ON_HEADERS_RECEIVED);
1032 EXPECT_EQ(0, network_delegate.completed_requests());
1033 // Cancel before callback.
1034 r.Cancel();
1035 // Ensure that network delegate is notified.
1036 EXPECT_EQ(1, network_delegate.completed_requests());
1037 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
1038 EXPECT_EQ(ERR_ABORTED, r.status().error());
1039 EXPECT_EQ(1, network_delegate.created_requests());
1040 EXPECT_EQ(0, network_delegate.destroyed_requests());
1041 }
1042 EXPECT_EQ(1, network_delegate.destroyed_requests());
1043}
1044
1045// Tests that we can handle when a network request was canceled while we were
1046// waiting for the network delegate.
1047// Part 4: Request is cancelled while waiting for OnAuthRequired callback.
1048TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) {
1049 ASSERT_TRUE(test_server_.Start());
1050
1051 TestDelegate d;
1052 BlockingNetworkDelegateWithManualCallback network_delegate;
1053 network_delegate.BlockOn(
1054 BlockingNetworkDelegateWithManualCallback::ON_AUTH_REQUIRED);
1055
1056 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
1057 context->set_network_delegate(&network_delegate);
1058 context->Init();
1059
1060 {
1061 TestURLRequest r(test_server_.GetURL("auth-basic"), &d);
1062 r.set_context(context);
1063
1064 r.Start();
1065 network_delegate.WaitForState(
1066 BlockingNetworkDelegateWithManualCallback::ON_AUTH_REQUIRED);
1067 EXPECT_EQ(0, network_delegate.completed_requests());
1068 // Cancel before callback.
1069 r.Cancel();
1070 // Ensure that network delegate is notified.
1071 EXPECT_EQ(1, network_delegate.completed_requests());
1072 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
1073 EXPECT_EQ(ERR_ABORTED, r.status().error());
1074 EXPECT_EQ(1, network_delegate.created_requests());
1075 EXPECT_EQ(0, network_delegate.destroyed_requests());
1076 }
1077 EXPECT_EQ(1, network_delegate.destroyed_requests());
1078}
1079
[email protected]95409e12010-08-17 20:07:111080// In this unit test, we're using the HTTPTestServer as a proxy server and
1081// issuing a CONNECT request with the magic host name "www.server-auth.com".
1082// The HTTPTestServer will return a 401 response, which we should balk at.
[email protected]b89290212009-08-14 22:37:351083TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) {
[email protected]95409e12010-08-17 20:07:111084 ASSERT_TRUE(test_server_.Start());
1085
[email protected]87a09a92011-07-14 15:50:501086 TestNetworkDelegate network_delegate; // must outlive URLRequest
1087 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
1088 context->SetProxyFromString(test_server_.host_port_pair().ToString());
1089 context->set_network_delegate(&network_delegate);
1090 context->Init();
1091
[email protected]dc651782009-02-14 01:45:081092 TestDelegate d;
1093 {
[email protected]7461a402011-03-24 23:19:511094 URLRequest r(GURL("https://www.server-auth.com/"), &d);
[email protected]87a09a92011-07-14 15:50:501095 r.set_context(context);
[email protected]dc651782009-02-14 01:45:081096
1097 r.Start();
1098 EXPECT_TRUE(r.is_pending());
1099
1100 MessageLoop::current()->Run();
1101
[email protected]7461a402011-03-24 23:19:511102 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
[email protected]d0cc35b2011-09-08 12:02:051103 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().error());
[email protected]dc651782009-02-14 01:45:081104 }
1105}
1106
[email protected]b89290212009-08-14 22:37:351107TEST_F(URLRequestTestHTTP, GetTest_NoCache) {
[email protected]95409e12010-08-17 20:07:111108 ASSERT_TRUE(test_server_.Start());
1109
initial.commit586acc5fe2008-07-26 22:42:521110 TestDelegate d;
1111 {
[email protected]95409e12010-08-17 20:07:111112 TestURLRequest r(test_server_.GetURL(""), &d);
[email protected]87a09a92011-07-14 15:50:501113 r.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:521114
1115 r.Start();
1116 EXPECT_TRUE(r.is_pending());
1117
1118 MessageLoop::current()->Run();
1119
1120 EXPECT_EQ(1, d.response_started_count());
1121 EXPECT_FALSE(d.received_data_before_response());
1122 EXPECT_NE(0, d.bytes_received());
[email protected]6d81b482011-02-22 19:47:191123 EXPECT_EQ(test_server_.host_port_pair().host(),
1124 r.GetSocketAddress().host());
1125 EXPECT_EQ(test_server_.host_port_pair().port(),
1126 r.GetSocketAddress().port());
[email protected]c31a54592009-09-04 02:36:161127
[email protected]9e743cd2010-03-16 07:03:531128 // TODO(eroman): Add back the NetLog tests...
initial.commit586acc5fe2008-07-26 22:42:521129 }
initial.commit586acc5fe2008-07-26 22:42:521130}
1131
[email protected]b89290212009-08-14 22:37:351132TEST_F(URLRequestTestHTTP, GetTest) {
[email protected]95409e12010-08-17 20:07:111133 ASSERT_TRUE(test_server_.Start());
1134
initial.commit586acc5fe2008-07-26 22:42:521135 TestDelegate d;
1136 {
[email protected]95409e12010-08-17 20:07:111137 TestURLRequest r(test_server_.GetURL(""), &d);
[email protected]87a09a92011-07-14 15:50:501138 r.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:521139
1140 r.Start();
1141 EXPECT_TRUE(r.is_pending());
1142
1143 MessageLoop::current()->Run();
1144
1145 EXPECT_EQ(1, d.response_started_count());
1146 EXPECT_FALSE(d.received_data_before_response());
1147 EXPECT_NE(0, d.bytes_received());
[email protected]6d81b482011-02-22 19:47:191148 EXPECT_EQ(test_server_.host_port_pair().host(),
1149 r.GetSocketAddress().host());
1150 EXPECT_EQ(test_server_.host_port_pair().port(),
1151 r.GetSocketAddress().port());
initial.commit586acc5fe2008-07-26 22:42:521152 }
[email protected]5d7b373e2009-09-02 07:19:031153}
1154
[email protected]aad63572011-05-24 20:14:391155TEST_F(URLRequestTestHTTP, GetZippedTest) {
1156 ASSERT_TRUE(test_server_.Start());
1157
1158 // Parameter that specifies the Content-Length field in the response:
1159 // C - Compressed length.
1160 // U - Uncompressed length.
1161 // L - Large length (larger than both C & U).
1162 // M - Medium length (between C & U).
1163 // S - Small length (smaller than both C & U).
1164 const char test_parameters[] = "CULMS";
1165 const int num_tests = arraysize(test_parameters)- 1; // Skip NULL.
1166 // C & U should be OK.
[email protected]f0e2bf42011-07-22 21:21:441167 // L & M are larger than the data sent, and show an error.
[email protected]aad63572011-05-24 20:14:391168 // S has too little data, but we seem to accept it.
1169 const bool test_expect_success[num_tests] =
[email protected]f001bd6a2011-12-08 04:31:371170 { true, true, false, false, true };
[email protected]aad63572011-05-24 20:14:391171
1172 for (int i = 0; i < num_tests ; i++) {
1173 TestDelegate d;
1174 {
1175 std::string test_file =
1176 base::StringPrintf("compressedfiles/BullRunSpeech.txt?%c",
1177 test_parameters[i]);
[email protected]aad63572011-05-24 20:14:391178
[email protected]87a09a92011-07-14 15:50:501179 TestNetworkDelegate network_delegate; // must outlive URLRequest
1180 scoped_refptr<TestURLRequestContext> context(
1181 new TestURLRequestContext(true));
1182 context->set_network_delegate(&network_delegate);
1183 context->Init();
1184
1185 TestURLRequest r(test_server_.GetURL(test_file), &d);
1186 r.set_context(context);
[email protected]aad63572011-05-24 20:14:391187 r.Start();
1188 EXPECT_TRUE(r.is_pending());
1189
1190 MessageLoop::current()->Run();
1191
1192 EXPECT_EQ(1, d.response_started_count());
1193 EXPECT_FALSE(d.received_data_before_response());
1194 VLOG(1) << " Received " << d.bytes_received() << " bytes"
1195 << " status = " << r.status().status()
[email protected]d0cc35b2011-09-08 12:02:051196 << " error = " << r.status().error();
[email protected]aad63572011-05-24 20:14:391197 if (test_expect_success[i]) {
1198 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status())
1199 << " Parameter = \"" << test_file << "\"";
1200 } else {
1201 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
[email protected]f001bd6a2011-12-08 04:31:371202 EXPECT_EQ(-100, r.status().error())
[email protected]aad63572011-05-24 20:14:391203 << " Parameter = \"" << test_file << "\"";
1204 }
1205 }
1206 }
1207}
1208
[email protected]4841b772011-11-04 10:36:061209// This test was disabled because it made chrome_frame_net_tests hang
1210// (see bug 102991).
1211TEST_F(URLRequestTestHTTP, DISABLED_HTTPSToHTTPRedirectNoRefererTest) {
[email protected]95409e12010-08-17 20:07:111212 ASSERT_TRUE(test_server_.Start());
1213
[email protected]d9fca99a2012-02-24 16:16:201214 TestServer https_test_server(TestServer::TYPE_HTTPS,
1215 TestServer::kLocalhost,
1216 FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]95409e12010-08-17 20:07:111217 ASSERT_TRUE(https_test_server.Start());
[email protected]7844480a2009-12-16 21:18:581218
1219 // An https server is sent a request with an https referer,
1220 // and responds with a redirect to an http url. The http
1221 // server should not be sent the referer.
[email protected]95409e12010-08-17 20:07:111222 GURL http_destination = test_server_.GetURL("");
[email protected]7844480a2009-12-16 21:18:581223 TestDelegate d;
[email protected]95409e12010-08-17 20:07:111224 TestURLRequest req(https_test_server.GetURL(
[email protected]7844480a2009-12-16 21:18:581225 "server-redirect?" + http_destination.spec()), &d);
[email protected]87a09a92011-07-14 15:50:501226 req.set_context(default_context_);
[email protected]7844480a2009-12-16 21:18:581227 req.set_referrer("https://www.referrer.com/");
1228 req.Start();
1229 MessageLoop::current()->Run();
1230
1231 EXPECT_EQ(1, d.response_started_count());
1232 EXPECT_EQ(1, d.received_redirect_count());
1233 EXPECT_EQ(http_destination, req.url());
1234 EXPECT_EQ(std::string(), req.referrer());
1235}
1236
[email protected]8f1ac082011-04-19 21:14:131237TEST_F(URLRequestTestHTTP, MultipleRedirectTest) {
1238 ASSERT_TRUE(test_server_.Start());
1239
1240 GURL destination_url = test_server_.GetURL("");
1241 GURL middle_redirect_url = test_server_.GetURL(
1242 "server-redirect?" + destination_url.spec());
1243 GURL original_url = test_server_.GetURL(
1244 "server-redirect?" + middle_redirect_url.spec());
1245 TestDelegate d;
1246 TestURLRequest req(original_url, &d);
[email protected]87a09a92011-07-14 15:50:501247 req.set_context(default_context_);
[email protected]8f1ac082011-04-19 21:14:131248 req.Start();
1249 MessageLoop::current()->Run();
1250
1251 EXPECT_EQ(1, d.response_started_count());
1252 EXPECT_EQ(2, d.received_redirect_count());
1253 EXPECT_EQ(destination_url, req.url());
1254 EXPECT_EQ(original_url, req.original_url());
1255 ASSERT_EQ(3U, req.url_chain().size());
1256 EXPECT_EQ(original_url, req.url_chain()[0]);
1257 EXPECT_EQ(middle_redirect_url, req.url_chain()[1]);
1258 EXPECT_EQ(destination_url, req.url_chain()[2]);
1259}
1260
[email protected]73e0bba2009-02-19 22:57:091261class HTTPSRequestTest : public testing::Test {
[email protected]87a09a92011-07-14 15:50:501262 public:
1263 HTTPSRequestTest() : default_context_(new TestURLRequestContext(true)) {
1264 default_context_->set_network_delegate(&default_network_delegate_);
1265 default_context_->Init();
1266 }
1267 virtual ~HTTPSRequestTest() {}
1268
1269 protected:
1270 TestNetworkDelegate default_network_delegate_; // must outlive URLRequest
1271 scoped_refptr<TestURLRequestContext> default_context_;
[email protected]ea224582008-12-07 20:25:461272};
1273
[email protected]4841b772011-11-04 10:36:061274// This test was disabled because it made chrome_frame_net_tests hang
1275// (see bug 102991).
1276TEST_F(HTTPSRequestTest, DISABLED_HTTPSGetTest) {
[email protected]7461a402011-03-24 23:19:511277 TestServer test_server(TestServer::TYPE_HTTPS,
[email protected]d9fca99a2012-02-24 16:16:201278 TestServer::kLocalhost,
[email protected]87a09a92011-07-14 15:50:501279 FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]95409e12010-08-17 20:07:111280 ASSERT_TRUE(test_server.Start());
[email protected]ea224582008-12-07 20:25:461281
[email protected]ea224582008-12-07 20:25:461282 TestDelegate d;
1283 {
[email protected]95409e12010-08-17 20:07:111284 TestURLRequest r(test_server.GetURL(""), &d);
[email protected]87a09a92011-07-14 15:50:501285 r.set_context(default_context_);
[email protected]ea224582008-12-07 20:25:461286 r.Start();
1287 EXPECT_TRUE(r.is_pending());
1288
1289 MessageLoop::current()->Run();
1290
1291 EXPECT_EQ(1, d.response_started_count());
1292 EXPECT_FALSE(d.received_data_before_response());
1293 EXPECT_NE(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:171294 CheckSSLInfo(r.ssl_info());
[email protected]6d81b482011-02-22 19:47:191295 EXPECT_EQ(test_server.host_port_pair().host(),
1296 r.GetSocketAddress().host());
1297 EXPECT_EQ(test_server.host_port_pair().port(),
1298 r.GetSocketAddress().port());
[email protected]ea224582008-12-07 20:25:461299 }
[email protected]ea224582008-12-07 20:25:461300}
1301
[email protected]5774ada2010-07-15 06:30:541302TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) {
[email protected]7461a402011-03-24 23:19:511303 TestServer::HTTPSOptions https_options(
1304 TestServer::HTTPSOptions::CERT_MISMATCHED_NAME);
1305 TestServer test_server(https_options,
[email protected]87a09a92011-07-14 15:50:501306 FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]95409e12010-08-17 20:07:111307 ASSERT_TRUE(test_server.Start());
[email protected]bacff652009-03-31 17:50:331308
1309 bool err_allowed = true;
1310 for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) {
1311 TestDelegate d;
1312 {
1313 d.set_allow_certificate_errors(err_allowed);
[email protected]95409e12010-08-17 20:07:111314 TestURLRequest r(test_server.GetURL(""), &d);
[email protected]87a09a92011-07-14 15:50:501315 r.set_context(default_context_);
[email protected]bacff652009-03-31 17:50:331316
1317 r.Start();
1318 EXPECT_TRUE(r.is_pending());
1319
1320 MessageLoop::current()->Run();
1321
1322 EXPECT_EQ(1, d.response_started_count());
1323 EXPECT_FALSE(d.received_data_before_response());
1324 EXPECT_TRUE(d.have_certificate_errors());
[email protected]96adadb2010-08-28 01:16:171325 if (err_allowed) {
[email protected]bacff652009-03-31 17:50:331326 EXPECT_NE(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:171327 CheckSSLInfo(r.ssl_info());
1328 } else {
[email protected]bacff652009-03-31 17:50:331329 EXPECT_EQ(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:171330 }
[email protected]bacff652009-03-31 17:50:331331 }
1332 }
1333}
1334
[email protected]5774ada2010-07-15 06:30:541335TEST_F(HTTPSRequestTest, HTTPSExpiredTest) {
[email protected]7461a402011-03-24 23:19:511336 TestServer::HTTPSOptions https_options(
1337 TestServer::HTTPSOptions::CERT_EXPIRED);
1338 TestServer test_server(https_options,
[email protected]87a09a92011-07-14 15:50:501339 FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]95409e12010-08-17 20:07:111340 ASSERT_TRUE(test_server.Start());
[email protected]bacff652009-03-31 17:50:331341
1342 // Iterate from false to true, just so that we do the opposite of the
1343 // previous test in order to increase test coverage.
1344 bool err_allowed = false;
1345 for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) {
1346 TestDelegate d;
1347 {
1348 d.set_allow_certificate_errors(err_allowed);
[email protected]95409e12010-08-17 20:07:111349 TestURLRequest r(test_server.GetURL(""), &d);
[email protected]87a09a92011-07-14 15:50:501350 r.set_context(default_context_);
[email protected]bacff652009-03-31 17:50:331351
1352 r.Start();
1353 EXPECT_TRUE(r.is_pending());
1354
1355 MessageLoop::current()->Run();
1356
1357 EXPECT_EQ(1, d.response_started_count());
1358 EXPECT_FALSE(d.received_data_before_response());
1359 EXPECT_TRUE(d.have_certificate_errors());
[email protected]96adadb2010-08-28 01:16:171360 if (err_allowed) {
[email protected]bacff652009-03-31 17:50:331361 EXPECT_NE(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:171362 CheckSSLInfo(r.ssl_info());
1363 } else {
[email protected]bacff652009-03-31 17:50:331364 EXPECT_EQ(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:171365 }
[email protected]bacff652009-03-31 17:50:331366 }
1367 }
1368}
[email protected]73e0bba2009-02-19 22:57:091369
[email protected]46d117e2012-01-18 01:53:141370// This tests that a load of www.google.com with a certificate error sets
1371// the |certificate_errors_are_fatal| flag correctly. This flag will cause
1372// the interstitial to be fatal.
[email protected]dc694c32011-11-29 20:00:491373TEST_F(HTTPSRequestTest, HTTPSPreloadedHSTSTest) {
1374 TestServer::HTTPSOptions https_options(
1375 TestServer::HTTPSOptions::CERT_MISMATCHED_NAME);
1376 TestServer test_server(https_options,
1377 FilePath(FILE_PATH_LITERAL("net/data/ssl")));
1378 ASSERT_TRUE(test_server.Start());
1379
1380 // We require that the URL be www.google.com in order to pick up the
1381 // preloaded HSTS entries in the TransportSecurityState. This means that we
1382 // have to use a MockHostResolver in order to direct www.google.com to the
1383 // testserver.
1384
1385 MockHostResolver host_resolver;
1386 host_resolver.rules()->AddRule("www.google.com", "127.0.0.1");
1387 TestNetworkDelegate network_delegate; // must outlive URLRequest
1388 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
1389 context->set_network_delegate(&network_delegate);
1390 context->set_host_resolver(&host_resolver);
1391 TransportSecurityState transport_security_state("");
1392 context->set_transport_security_state(&transport_security_state);
1393 context->Init();
1394
1395 TestDelegate d;
1396 TestURLRequest r(GURL(StringPrintf("https://www.google.com:%d",
1397 test_server.host_port_pair().port())),
1398 &d);
1399 r.set_context(context);
1400
1401 r.Start();
1402 EXPECT_TRUE(r.is_pending());
1403
1404 MessageLoop::current()->Run();
1405
1406 EXPECT_EQ(1, d.response_started_count());
1407 EXPECT_FALSE(d.received_data_before_response());
1408 EXPECT_TRUE(d.have_certificate_errors());
[email protected]46d117e2012-01-18 01:53:141409 EXPECT_TRUE(d.certificate_errors_are_fatal());
[email protected]dc694c32011-11-29 20:00:491410}
1411
[email protected]69d7ff442012-02-13 22:41:271412// This tests that cached HTTPS page loads do not cause any updates to the
1413// TransportSecurityState.
[email protected]88056852012-02-15 05:18:201414TEST_F(HTTPSRequestTest, HTTPSErrorsNoClobberTSSTest) {
[email protected]69d7ff442012-02-13 22:41:271415 // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't
1416 // matter. It just has to be any error.
1417 TestServer::HTTPSOptions https_options(
1418 TestServer::HTTPSOptions::CERT_MISMATCHED_NAME);
1419 TestServer test_server(https_options,
1420 FilePath(FILE_PATH_LITERAL("net/data/ssl")));
1421 ASSERT_TRUE(test_server.Start());
1422
1423 // We require that the URL be www.google.com in order to pick up the
1424 // preloaded and dynamic HSTS and public key pin entries in the
1425 // TransportSecurityState. This means that we have to use a
1426 // MockHostResolver in order to direct www.google.com to the testserver.
1427
1428 MockHostResolver host_resolver;
1429 host_resolver.rules()->AddRule("www.google.com", "127.0.0.1");
1430 TestNetworkDelegate network_delegate; // must outlive URLRequest
1431 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
1432 context->set_network_delegate(&network_delegate);
1433 context->set_host_resolver(&host_resolver);
1434 TransportSecurityState transport_security_state("");
1435 TransportSecurityState::DomainState domain_state;
1436 EXPECT_TRUE(transport_security_state.HasMetadata(&domain_state,
1437 "www.google.com", true));
1438 context->set_transport_security_state(&transport_security_state);
1439 context->Init();
1440
1441 TestDelegate d;
1442 TestURLRequest r(GURL(StringPrintf("https://www.google.com:%d",
1443 test_server.host_port_pair().port())),
1444 &d);
1445 r.set_context(context);
1446
1447 r.Start();
1448 EXPECT_TRUE(r.is_pending());
1449
1450 MessageLoop::current()->Run();
1451
1452 EXPECT_EQ(1, d.response_started_count());
1453 EXPECT_FALSE(d.received_data_before_response());
1454 EXPECT_TRUE(d.have_certificate_errors());
1455 EXPECT_TRUE(d.certificate_errors_are_fatal());
1456
1457 // Get a fresh copy of the state, and check that it hasn't been updated.
1458 TransportSecurityState::DomainState new_domain_state;
1459 EXPECT_TRUE(transport_security_state.HasMetadata(&new_domain_state,
1460 "www.google.com", true));
1461 EXPECT_EQ(new_domain_state.mode, domain_state.mode);
[email protected]69d7ff442012-02-13 22:41:271462 EXPECT_EQ(new_domain_state.include_subdomains,
1463 domain_state.include_subdomains);
1464 EXPECT_TRUE(FingerprintsEqual(new_domain_state.preloaded_spki_hashes,
1465 domain_state.preloaded_spki_hashes));
1466 EXPECT_TRUE(FingerprintsEqual(new_domain_state.dynamic_spki_hashes,
1467 domain_state.dynamic_spki_hashes));
1468 EXPECT_TRUE(FingerprintsEqual(new_domain_state.bad_preloaded_spki_hashes,
1469 domain_state.bad_preloaded_spki_hashes));
1470}
1471
[email protected]8df162a2010-08-07 01:10:021472namespace {
1473
1474class SSLClientAuthTestDelegate : public TestDelegate {
1475 public:
1476 SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) {
1477 }
1478 virtual void OnCertificateRequested(
[email protected]7461a402011-03-24 23:19:511479 URLRequest* request,
1480 SSLCertRequestInfo* cert_request_info) {
[email protected]8df162a2010-08-07 01:10:021481 on_certificate_requested_count_++;
1482 MessageLoop::current()->Quit();
1483 }
1484 int on_certificate_requested_count() {
1485 return on_certificate_requested_count_;
1486 }
1487 private:
1488 int on_certificate_requested_count_;
1489};
1490
1491} // namespace
1492
1493// TODO(davidben): Test the rest of the code. Specifically,
1494// - Filtering which certificates to select.
1495// - Sending a certificate back.
1496// - Getting a certificate request in an SSL renegotiation sending the
1497// HTTP request.
1498TEST_F(HTTPSRequestTest, ClientAuthTest) {
[email protected]7461a402011-03-24 23:19:511499 TestServer::HTTPSOptions https_options;
[email protected]347599952010-10-28 11:57:361500 https_options.request_client_certificate = true;
[email protected]7461a402011-03-24 23:19:511501 TestServer test_server(https_options,
[email protected]87a09a92011-07-14 15:50:501502 FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]95409e12010-08-17 20:07:111503 ASSERT_TRUE(test_server.Start());
[email protected]8df162a2010-08-07 01:10:021504
1505 SSLClientAuthTestDelegate d;
1506 {
[email protected]95409e12010-08-17 20:07:111507 TestURLRequest r(test_server.GetURL(""), &d);
[email protected]87a09a92011-07-14 15:50:501508 r.set_context(default_context_);
[email protected]8df162a2010-08-07 01:10:021509
1510 r.Start();
1511 EXPECT_TRUE(r.is_pending());
1512
1513 MessageLoop::current()->Run();
1514
1515 EXPECT_EQ(1, d.on_certificate_requested_count());
1516 EXPECT_FALSE(d.received_data_before_response());
1517 EXPECT_EQ(0, d.bytes_received());
[email protected]65a3b912010-08-21 05:46:581518
1519 // Send no certificate.
1520 // TODO(davidben): Get temporary client cert import (with keys) working on
1521 // all platforms so we can test sending a cert as well.
1522 r.ContinueWithCertificate(NULL);
1523
1524 MessageLoop::current()->Run();
1525
1526 EXPECT_EQ(1, d.response_started_count());
1527 EXPECT_FALSE(d.received_data_before_response());
1528 EXPECT_NE(0, d.bytes_received());
[email protected]8df162a2010-08-07 01:10:021529 }
1530}
1531
[email protected]c3456bb2011-12-12 22:22:191532TEST_F(HTTPSRequestTest, ResumeTest) {
1533 // Test that we attempt a session resume when making two connections to the
1534 // same host.
1535 TestServer::HTTPSOptions https_options;
1536 https_options.record_resume = true;
1537 TestServer test_server(https_options,
1538 FilePath(FILE_PATH_LITERAL("net/data/ssl")));
1539 ASSERT_TRUE(test_server.Start());
1540
1541 SSLClientSocket::ClearSessionCache();
1542
1543 {
1544 TestDelegate d;
1545 TestURLRequest r(test_server.GetURL("ssl-session-cache"), &d);
1546 r.set_context(default_context_);
1547
1548 r.Start();
1549 EXPECT_TRUE(r.is_pending());
1550
1551 MessageLoop::current()->Run();
1552
1553 EXPECT_EQ(1, d.response_started_count());
1554 }
1555
1556 reinterpret_cast<HttpCache*>(default_context_->http_transaction_factory())->
1557 CloseAllConnections();
1558
1559 {
1560 TestDelegate d;
1561 TestURLRequest r(test_server.GetURL("ssl-session-cache"), &d);
1562 r.set_context(default_context_);
1563
1564 r.Start();
1565 EXPECT_TRUE(r.is_pending());
1566
1567 MessageLoop::current()->Run();
1568
1569 // The response will look like;
1570 // insert abc
1571 // lookup abc
1572 // insert xyz
1573 //
1574 // With a newline at the end which makes the split think that there are
1575 // four lines.
1576
1577 EXPECT_EQ(1, d.response_started_count());
1578 std::vector<std::string> lines;
1579 base::SplitString(d.data_received(), '\n', &lines);
1580 ASSERT_EQ(4u, lines.size()) << d.data_received();
1581
1582 std::string session_id;
1583
1584 for (size_t i = 0; i < 2; i++) {
1585 std::vector<std::string> parts;
1586 base::SplitString(lines[i], '\t', &parts);
1587 ASSERT_EQ(2u, parts.size());
1588 if (i == 0) {
1589 EXPECT_EQ("insert", parts[0]);
1590 session_id = parts[1];
1591 } else {
1592 EXPECT_EQ("lookup", parts[0]);
1593 EXPECT_EQ(session_id, parts[1]);
1594 }
1595 }
1596 }
1597}
1598
1599TEST_F(HTTPSRequestTest, SSLSessionCacheShardTest) {
1600 // Test that sessions aren't resumed when the value of ssl_session_cache_shard
1601 // differs.
1602 TestServer::HTTPSOptions https_options;
1603 https_options.record_resume = true;
1604 TestServer test_server(https_options,
1605 FilePath(FILE_PATH_LITERAL("net/data/ssl")));
1606 ASSERT_TRUE(test_server.Start());
1607
1608 SSLClientSocket::ClearSessionCache();
1609
1610 {
1611 TestDelegate d;
1612 TestURLRequest r(test_server.GetURL("ssl-session-cache"), &d);
1613 r.set_context(default_context_);
1614
1615 r.Start();
1616 EXPECT_TRUE(r.is_pending());
1617
1618 MessageLoop::current()->Run();
1619
1620 EXPECT_EQ(1, d.response_started_count());
1621 }
1622
1623 // Now create a new HttpCache with a different ssl_session_cache_shard value.
1624 HttpNetworkSession::Params params;
1625 params.host_resolver = default_context_->host_resolver();
1626 params.cert_verifier = default_context_->cert_verifier();
1627 params.proxy_service = default_context_->proxy_service();
1628 params.ssl_config_service = default_context_->ssl_config_service();
1629 params.http_auth_handler_factory =
1630 default_context_->http_auth_handler_factory();
1631 params.network_delegate = default_context_->network_delegate();
1632 params.http_server_properties = default_context_->http_server_properties();
1633 params.ssl_session_cache_shard = "alternate";
1634
1635 scoped_ptr<net::HttpCache> cache(new net::HttpCache(
1636 new net::HttpNetworkSession(params),
1637 net::HttpCache::DefaultBackend::InMemory(0)));
1638
1639 default_context_->set_http_transaction_factory(cache.get());
1640
1641 {
1642 TestDelegate d;
1643 TestURLRequest r(test_server.GetURL("ssl-session-cache"), &d);
1644 r.set_context(default_context_);
1645
1646 r.Start();
1647 EXPECT_TRUE(r.is_pending());
1648
1649 MessageLoop::current()->Run();
1650
1651 // The response will look like;
1652 // insert abc
1653 // insert xyz
1654 //
1655 // With a newline at the end which makes the split think that there are
1656 // three lines.
1657
1658 EXPECT_EQ(1, d.response_started_count());
1659 std::vector<std::string> lines;
1660 base::SplitString(d.data_received(), '\n', &lines);
1661 ASSERT_EQ(3u, lines.size());
1662
1663 std::string session_id;
1664 for (size_t i = 0; i < 2; i++) {
1665 std::vector<std::string> parts;
1666 base::SplitString(lines[i], '\t', &parts);
1667 ASSERT_EQ(2u, parts.size());
1668 EXPECT_EQ("insert", parts[0]);
1669 if (i == 0) {
1670 session_id = parts[1];
1671 } else {
1672 EXPECT_NE(session_id, parts[1]);
1673 }
1674 }
1675 }
1676}
1677
[email protected]37314622009-08-17 20:29:391678TEST_F(URLRequestTestHTTP, CancelTest) {
initial.commit586acc5fe2008-07-26 22:42:521679 TestDelegate d;
1680 {
1681 TestURLRequest r(GURL("http://www.google.com/"), &d);
[email protected]87a09a92011-07-14 15:50:501682 r.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:521683
1684 r.Start();
1685 EXPECT_TRUE(r.is_pending());
1686
1687 r.Cancel();
1688
1689 MessageLoop::current()->Run();
1690
1691 // We expect to receive OnResponseStarted even though the request has been
1692 // cancelled.
1693 EXPECT_EQ(1, d.response_started_count());
1694 EXPECT_EQ(0, d.bytes_received());
1695 EXPECT_FALSE(d.received_data_before_response());
1696 }
initial.commit586acc5fe2008-07-26 22:42:521697}
1698
[email protected]37314622009-08-17 20:29:391699TEST_F(URLRequestTestHTTP, CancelTest2) {
[email protected]95409e12010-08-17 20:07:111700 ASSERT_TRUE(test_server_.Start());
[email protected]dd265012009-01-08 20:45:271701
initial.commit586acc5fe2008-07-26 22:42:521702 TestDelegate d;
1703 {
[email protected]95409e12010-08-17 20:07:111704 TestURLRequest r(test_server_.GetURL(""), &d);
[email protected]87a09a92011-07-14 15:50:501705 r.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:521706
1707 d.set_cancel_in_response_started(true);
1708
1709 r.Start();
1710 EXPECT_TRUE(r.is_pending());
1711
1712 MessageLoop::current()->Run();
1713
1714 EXPECT_EQ(1, d.response_started_count());
1715 EXPECT_EQ(0, d.bytes_received());
1716 EXPECT_FALSE(d.received_data_before_response());
[email protected]7461a402011-03-24 23:19:511717 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
initial.commit586acc5fe2008-07-26 22:42:521718 }
initial.commit586acc5fe2008-07-26 22:42:521719}
1720
[email protected]37314622009-08-17 20:29:391721TEST_F(URLRequestTestHTTP, CancelTest3) {
[email protected]95409e12010-08-17 20:07:111722 ASSERT_TRUE(test_server_.Start());
1723
initial.commit586acc5fe2008-07-26 22:42:521724 TestDelegate d;
1725 {
[email protected]95409e12010-08-17 20:07:111726 TestURLRequest r(test_server_.GetURL(""), &d);
[email protected]87a09a92011-07-14 15:50:501727 r.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:521728
1729 d.set_cancel_in_received_data(true);
1730
1731 r.Start();
1732 EXPECT_TRUE(r.is_pending());
1733
1734 MessageLoop::current()->Run();
1735
1736 EXPECT_EQ(1, d.response_started_count());
1737 // There is no guarantee about how much data was received
1738 // before the cancel was issued. It could have been 0 bytes,
1739 // or it could have been all the bytes.
1740 // EXPECT_EQ(0, d.bytes_received());
1741 EXPECT_FALSE(d.received_data_before_response());
[email protected]7461a402011-03-24 23:19:511742 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
initial.commit586acc5fe2008-07-26 22:42:521743 }
initial.commit586acc5fe2008-07-26 22:42:521744}
1745
[email protected]37314622009-08-17 20:29:391746TEST_F(URLRequestTestHTTP, CancelTest4) {
[email protected]95409e12010-08-17 20:07:111747 ASSERT_TRUE(test_server_.Start());
1748
initial.commit586acc5fe2008-07-26 22:42:521749 TestDelegate d;
1750 {
[email protected]95409e12010-08-17 20:07:111751 TestURLRequest r(test_server_.GetURL(""), &d);
[email protected]87a09a92011-07-14 15:50:501752 r.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:521753
1754 r.Start();
1755 EXPECT_TRUE(r.is_pending());
1756
1757 // The request will be implicitly canceled when it is destroyed. The
1758 // test delegate must not post a quit message when this happens because
1759 // this test doesn't actually have a message loop. The quit message would
1760 // get put on this thread's message queue and the next test would exit
1761 // early, causing problems.
1762 d.set_quit_on_complete(false);
1763 }
1764 // expect things to just cleanup properly.
1765
1766 // we won't actually get a received reponse here because we've never run the
1767 // message loop
1768 EXPECT_FALSE(d.received_data_before_response());
1769 EXPECT_EQ(0, d.bytes_received());
1770}
1771
[email protected]37314622009-08-17 20:29:391772TEST_F(URLRequestTestHTTP, CancelTest5) {
[email protected]95409e12010-08-17 20:07:111773 ASSERT_TRUE(test_server_.Start());
1774
initial.commit586acc5fe2008-07-26 22:42:521775 // populate cache
1776 {
1777 TestDelegate d;
[email protected]7461a402011-03-24 23:19:511778 URLRequest r(test_server_.GetURL("cachetime"), &d);
[email protected]87a09a92011-07-14 15:50:501779 r.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:521780 r.Start();
1781 MessageLoop::current()->Run();
[email protected]7461a402011-03-24 23:19:511782 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
initial.commit586acc5fe2008-07-26 22:42:521783 }
1784
1785 // cancel read from cache (see bug 990242)
1786 {
1787 TestDelegate d;
[email protected]7461a402011-03-24 23:19:511788 URLRequest r(test_server_.GetURL("cachetime"), &d);
[email protected]87a09a92011-07-14 15:50:501789 r.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:521790 r.Start();
1791 r.Cancel();
1792 MessageLoop::current()->Run();
1793
[email protected]7461a402011-03-24 23:19:511794 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
initial.commit586acc5fe2008-07-26 22:42:521795 EXPECT_EQ(1, d.response_started_count());
1796 EXPECT_EQ(0, d.bytes_received());
1797 EXPECT_FALSE(d.received_data_before_response());
1798 }
initial.commit586acc5fe2008-07-26 22:42:521799}
1800
[email protected]37314622009-08-17 20:29:391801TEST_F(URLRequestTestHTTP, PostTest) {
[email protected]95409e12010-08-17 20:07:111802 ASSERT_TRUE(test_server_.Start());
[email protected]762d2db2010-01-11 19:03:011803 HTTPUploadDataOperationTest("POST");
1804}
initial.commit586acc5fe2008-07-26 22:42:521805
[email protected]762d2db2010-01-11 19:03:011806TEST_F(URLRequestTestHTTP, PutTest) {
[email protected]95409e12010-08-17 20:07:111807 ASSERT_TRUE(test_server_.Start());
[email protected]762d2db2010-01-11 19:03:011808 HTTPUploadDataOperationTest("PUT");
initial.commit586acc5fe2008-07-26 22:42:521809}
1810
[email protected]37314622009-08-17 20:29:391811TEST_F(URLRequestTestHTTP, PostEmptyTest) {
[email protected]95409e12010-08-17 20:07:111812 ASSERT_TRUE(test_server_.Start());
1813
initial.commit586acc5fe2008-07-26 22:42:521814 TestDelegate d;
1815 {
[email protected]95409e12010-08-17 20:07:111816 TestURLRequest r(test_server_.GetURL("echo"), &d);
[email protected]87a09a92011-07-14 15:50:501817 r.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:521818 r.set_method("POST");
1819
1820 r.Start();
1821 EXPECT_TRUE(r.is_pending());
1822
1823 MessageLoop::current()->Run();
1824
1825 ASSERT_EQ(1, d.response_started_count()) << "request failed: " <<
[email protected]d0cc35b2011-09-08 12:02:051826 (int) r.status().status() << ", error: " << r.status().error();
initial.commit586acc5fe2008-07-26 22:42:521827
1828 EXPECT_FALSE(d.received_data_before_response());
1829 EXPECT_TRUE(d.data_received().empty());
1830 }
initial.commit586acc5fe2008-07-26 22:42:521831}
1832
[email protected]37314622009-08-17 20:29:391833TEST_F(URLRequestTestHTTP, PostFileTest) {
[email protected]95409e12010-08-17 20:07:111834 ASSERT_TRUE(test_server_.Start());
1835
initial.commit586acc5fe2008-07-26 22:42:521836 TestDelegate d;
1837 {
[email protected]95409e12010-08-17 20:07:111838 TestURLRequest r(test_server_.GetURL("echo"), &d);
[email protected]87a09a92011-07-14 15:50:501839 r.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:521840 r.set_method("POST");
1841
[email protected]399b8702009-05-01 20:34:021842 FilePath dir;
initial.commit586acc5fe2008-07-26 22:42:521843 PathService::Get(base::DIR_EXE, &dir);
[email protected]9396b252008-09-29 17:29:381844 file_util::SetCurrentDirectory(dir);
initial.commit586acc5fe2008-07-26 22:42:521845
[email protected]2b2e84a2c2012-01-12 01:56:161846 scoped_refptr<UploadData> upload_data(new UploadData);
1847
[email protected]72cbd322009-04-07 10:17:121848 FilePath path;
initial.commit586acc5fe2008-07-26 22:42:521849 PathService::Get(base::DIR_SOURCE_ROOT, &path);
[email protected]72cbd322009-04-07 10:17:121850 path = path.Append(FILE_PATH_LITERAL("net"));
1851 path = path.Append(FILE_PATH_LITERAL("data"));
1852 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
1853 path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
[email protected]2b2e84a2c2012-01-12 01:56:161854 upload_data->AppendFileRange(path, 0, kuint64max, base::Time());
initial.commit586acc5fe2008-07-26 22:42:521855
1856 // This file should just be ignored in the upload stream.
[email protected]2b2e84a2c2012-01-12 01:56:161857 upload_data->AppendFileRange(
1858 FilePath(FILE_PATH_LITERAL(
1859 "c:\\path\\to\\non\\existant\\file.randomness.12345")),
1860 0, kuint64max, base::Time());
1861 r.set_upload(upload_data);
initial.commit586acc5fe2008-07-26 22:42:521862
1863 r.Start();
1864 EXPECT_TRUE(r.is_pending());
1865
1866 MessageLoop::current()->Run();
1867
[email protected]10a1fe92008-11-04 21:47:021868 int64 longsize;
1869 ASSERT_EQ(true, file_util::GetFileSize(path, &longsize));
1870 int size = static_cast<int>(longsize);
initial.commit586acc5fe2008-07-26 22:42:521871 scoped_array<char> buf(new char[size]);
1872
[email protected]dd265012009-01-08 20:45:271873 int size_read = static_cast<int>(file_util::ReadFile(path,
1874 buf.get(), size));
[email protected]eac0709a2008-11-04 21:00:461875 ASSERT_EQ(size, size_read);
initial.commit586acc5fe2008-07-26 22:42:521876
1877 ASSERT_EQ(1, d.response_started_count()) << "request failed: " <<
[email protected]d0cc35b2011-09-08 12:02:051878 (int) r.status().status() << ", error: " << r.status().error();
initial.commit586acc5fe2008-07-26 22:42:521879
1880 EXPECT_FALSE(d.received_data_before_response());
1881
1882 ASSERT_EQ(size, d.bytes_received());
1883 EXPECT_EQ(0, memcmp(d.data_received().c_str(), buf.get(), size));
1884 }
initial.commit586acc5fe2008-07-26 22:42:521885}
1886
[email protected]699efe602011-01-25 07:17:111887TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) {
1888 ASSERT_TRUE(test_server_.Start());
1889
1890 TestDelegate d;
1891 {
1892 TestURLRequest r(test_server_.GetURL("echo"), &d);
[email protected]87a09a92011-07-14 15:50:501893 r.set_context(default_context_);
[email protected]699efe602011-01-25 07:17:111894 r.EnableChunkedUpload();
1895 r.set_method("POST");
1896 AddChunksToUpload(&r);
1897 r.Start();
1898 EXPECT_TRUE(r.is_pending());
1899
1900 MessageLoop::current()->Run();
1901
1902 VerifyReceivedDataMatchesChunks(&r, &d);
1903 }
1904}
1905
1906TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) {
1907 ASSERT_TRUE(test_server_.Start());
1908
1909 TestDelegate d;
1910 {
1911 TestURLRequest r(test_server_.GetURL("echo"), &d);
[email protected]87a09a92011-07-14 15:50:501912 r.set_context(default_context_);
[email protected]699efe602011-01-25 07:17:111913 r.EnableChunkedUpload();
1914 r.set_method("POST");
1915 r.Start();
1916 EXPECT_TRUE(r.is_pending());
1917
1918 MessageLoop::current()->RunAllPending();
1919 AddChunksToUpload(&r);
1920 MessageLoop::current()->Run();
1921
1922 VerifyReceivedDataMatchesChunks(&r, &d);
1923 }
1924}
1925
[email protected]7a0bb4bf2008-11-19 21:41:481926TEST_F(URLRequestTest, AboutBlankTest) {
initial.commit586acc5fe2008-07-26 22:42:521927 TestDelegate d;
1928 {
1929 TestURLRequest r(GURL("about:blank"), &d);
[email protected]87a09a92011-07-14 15:50:501930 r.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:521931
1932 r.Start();
1933 EXPECT_TRUE(r.is_pending());
1934
1935 MessageLoop::current()->Run();
1936
1937 EXPECT_TRUE(!r.is_pending());
1938 EXPECT_FALSE(d.received_data_before_response());
1939 EXPECT_EQ(d.bytes_received(), 0);
[email protected]6d81b482011-02-22 19:47:191940 EXPECT_EQ("", r.GetSocketAddress().host());
1941 EXPECT_EQ(0, r.GetSocketAddress().port());
initial.commit586acc5fe2008-07-26 22:42:521942 }
initial.commit586acc5fe2008-07-26 22:42:521943}
1944
[email protected]fcb7fd92009-10-22 04:18:581945TEST_F(URLRequestTest, DataURLImageTest) {
1946 TestDelegate d;
1947 {
1948 // Use our nice little Chrome logo.
1949 TestURLRequest r(GURL(
1950 "data:image/png;base64,"
1951 "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUBBjG3"
1952 "w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQxIEVxitD"
1953 "5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI5MpCVdW0gO7t"
1954 "vNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKzMx1+fg9bAgK6zHq9"
1955 "cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7CkIMDxQpF7r/MWq12UctI1"
1956 "dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0WYUqt57yWf2McHTObYPbVD+Z"
1957 "wbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2HQQcm43XwmtoYM6vVKleh0uoWvnzW"
1958 "3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nkDFHF9ZS+uYVjRUasMeHUmyLYtcklTvzW"
1959 "GFZnNOXczThvpKIzjcahSqIzkvDLayDq6D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTb"
1960 "xXX1T5xsV9tm9r4TQwHLiZw/pdDZJea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5"
1961 "MrVvMzNaI3+ERHfrFzPKQukrQGI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gV"
1962 "Md6q5c8GdosynKmSeRuGzpjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq"
1963 "/uxxZKzNiZFGD0wRC3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7F"
1964 "Ux1qLnV7MGF40smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufB"
1965 "JbsysjjW4kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJM"
1966 "nlm2O34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfm"
1967 "moRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV5En"
1968 "hORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="),
1969 &d);
[email protected]87a09a92011-07-14 15:50:501970 r.set_context(default_context_);
[email protected]fcb7fd92009-10-22 04:18:581971
1972 r.Start();
1973 EXPECT_TRUE(r.is_pending());
1974
1975 MessageLoop::current()->Run();
1976
1977 EXPECT_TRUE(!r.is_pending());
1978 EXPECT_FALSE(d.received_data_before_response());
1979 EXPECT_EQ(d.bytes_received(), 911);
[email protected]6d81b482011-02-22 19:47:191980 EXPECT_EQ("", r.GetSocketAddress().host());
1981 EXPECT_EQ(0, r.GetSocketAddress().port());
[email protected]fcb7fd92009-10-22 04:18:581982 }
1983}
1984
[email protected]7a0bb4bf2008-11-19 21:41:481985TEST_F(URLRequestTest, FileTest) {
[email protected]b9e04f02008-11-27 04:03:571986 FilePath app_path;
initial.commit586acc5fe2008-07-26 22:42:521987 PathService::Get(base::FILE_EXE, &app_path);
[email protected]7461a402011-03-24 23:19:511988 GURL app_url = FilePathToFileURL(app_path);
initial.commit586acc5fe2008-07-26 22:42:521989
1990 TestDelegate d;
1991 {
[email protected]b9e04f02008-11-27 04:03:571992 TestURLRequest r(app_url, &d);
[email protected]87a09a92011-07-14 15:50:501993 r.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:521994
1995 r.Start();
1996 EXPECT_TRUE(r.is_pending());
1997
1998 MessageLoop::current()->Run();
1999
[email protected]b77280c2009-03-20 17:27:472000 int64 file_size = -1;
2001 EXPECT_TRUE(file_util::GetFileSize(app_path, &file_size));
initial.commit586acc5fe2008-07-26 22:42:522002
2003 EXPECT_TRUE(!r.is_pending());
2004 EXPECT_EQ(1, d.response_started_count());
2005 EXPECT_FALSE(d.received_data_before_response());
[email protected]9396b252008-09-29 17:29:382006 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
[email protected]6d81b482011-02-22 19:47:192007 EXPECT_EQ("", r.GetSocketAddress().host());
2008 EXPECT_EQ(0, r.GetSocketAddress().port());
initial.commit586acc5fe2008-07-26 22:42:522009 }
initial.commit586acc5fe2008-07-26 22:42:522010}
2011
[email protected]661376a2009-04-29 02:04:232012TEST_F(URLRequestTest, FileTestFullSpecifiedRange) {
2013 const size_t buffer_size = 4000;
2014 scoped_array<char> buffer(new char[buffer_size]);
2015 FillBuffer(buffer.get(), buffer_size);
2016
2017 FilePath temp_path;
[email protected]33edeab2009-08-18 16:07:552018 EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path));
[email protected]7461a402011-03-24 23:19:512019 GURL temp_url = FilePathToFileURL(temp_path);
[email protected]1e5ae862009-10-14 22:14:532020 EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size));
[email protected]661376a2009-04-29 02:04:232021
2022 int64 file_size;
2023 EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size));
2024
2025 const size_t first_byte_position = 500;
2026 const size_t last_byte_position = buffer_size - first_byte_position;
2027 const size_t content_length = last_byte_position - first_byte_position + 1;
2028 std::string partial_buffer_string(buffer.get() + first_byte_position,
2029 buffer.get() + last_byte_position + 1);
2030
2031 TestDelegate d;
2032 {
2033 TestURLRequest r(temp_url, &d);
[email protected]87a09a92011-07-14 15:50:502034 r.set_context(default_context_);
[email protected]661376a2009-04-29 02:04:232035
[email protected]7461a402011-03-24 23:19:512036 HttpRequestHeaders headers;
2037 headers.SetHeader(HttpRequestHeaders::kRange,
[email protected]d8eb84242010-09-25 02:25:062038 base::StringPrintf(
2039 "bytes=%" PRIuS "-%" PRIuS,
2040 first_byte_position, last_byte_position));
[email protected]88e6b6f32010-05-07 23:14:252041 r.SetExtraRequestHeaders(headers);
[email protected]661376a2009-04-29 02:04:232042 r.Start();
2043 EXPECT_TRUE(r.is_pending());
2044
2045 MessageLoop::current()->Run();
2046 EXPECT_TRUE(!r.is_pending());
2047 EXPECT_EQ(1, d.response_started_count());
2048 EXPECT_FALSE(d.received_data_before_response());
2049 EXPECT_EQ(static_cast<int>(content_length), d.bytes_received());
2050 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
2051 EXPECT_TRUE(partial_buffer_string == d.data_received());
2052 }
2053
2054 EXPECT_TRUE(file_util::Delete(temp_path, false));
[email protected]661376a2009-04-29 02:04:232055}
2056
2057TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) {
2058 const size_t buffer_size = 4000;
2059 scoped_array<char> buffer(new char[buffer_size]);
2060 FillBuffer(buffer.get(), buffer_size);
2061
2062 FilePath temp_path;
[email protected]33edeab2009-08-18 16:07:552063 EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path));
[email protected]7461a402011-03-24 23:19:512064 GURL temp_url = FilePathToFileURL(temp_path);
[email protected]1e5ae862009-10-14 22:14:532065 EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size));
[email protected]661376a2009-04-29 02:04:232066
2067 int64 file_size;
2068 EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size));
2069
2070 const size_t first_byte_position = 500;
2071 const size_t last_byte_position = buffer_size - 1;
2072 const size_t content_length = last_byte_position - first_byte_position + 1;
2073 std::string partial_buffer_string(buffer.get() + first_byte_position,
2074 buffer.get() + last_byte_position + 1);
2075
2076 TestDelegate d;
2077 {
2078 TestURLRequest r(temp_url, &d);
[email protected]87a09a92011-07-14 15:50:502079 r.set_context(default_context_);
[email protected]661376a2009-04-29 02:04:232080
[email protected]7461a402011-03-24 23:19:512081 HttpRequestHeaders headers;
2082 headers.SetHeader(HttpRequestHeaders::kRange,
[email protected]d8eb84242010-09-25 02:25:062083 base::StringPrintf("bytes=%" PRIuS "-",
2084 first_byte_position));
[email protected]88e6b6f32010-05-07 23:14:252085 r.SetExtraRequestHeaders(headers);
[email protected]661376a2009-04-29 02:04:232086 r.Start();
2087 EXPECT_TRUE(r.is_pending());
2088
2089 MessageLoop::current()->Run();
2090 EXPECT_TRUE(!r.is_pending());
2091 EXPECT_EQ(1, d.response_started_count());
2092 EXPECT_FALSE(d.received_data_before_response());
2093 EXPECT_EQ(static_cast<int>(content_length), d.bytes_received());
2094 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
2095 EXPECT_TRUE(partial_buffer_string == d.data_received());
2096 }
2097
2098 EXPECT_TRUE(file_util::Delete(temp_path, false));
[email protected]661376a2009-04-29 02:04:232099}
2100
2101TEST_F(URLRequestTest, FileTestMultipleRanges) {
2102 const size_t buffer_size = 400000;
2103 scoped_array<char> buffer(new char[buffer_size]);
2104 FillBuffer(buffer.get(), buffer_size);
2105
2106 FilePath temp_path;
[email protected]33edeab2009-08-18 16:07:552107 EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path));
[email protected]7461a402011-03-24 23:19:512108 GURL temp_url = FilePathToFileURL(temp_path);
[email protected]1e5ae862009-10-14 22:14:532109 EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size));
[email protected]661376a2009-04-29 02:04:232110
2111 int64 file_size;
2112 EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size));
2113
2114 TestDelegate d;
2115 {
2116 TestURLRequest r(temp_url, &d);
[email protected]87a09a92011-07-14 15:50:502117 r.set_context(default_context_);
[email protected]661376a2009-04-29 02:04:232118
[email protected]7461a402011-03-24 23:19:512119 HttpRequestHeaders headers;
2120 headers.SetHeader(HttpRequestHeaders::kRange,
[email protected]88e6b6f32010-05-07 23:14:252121 "bytes=0-0,10-200,200-300");
2122 r.SetExtraRequestHeaders(headers);
[email protected]661376a2009-04-29 02:04:232123 r.Start();
2124 EXPECT_TRUE(r.is_pending());
2125
2126 MessageLoop::current()->Run();
2127 EXPECT_TRUE(d.request_failed());
2128 }
2129
2130 EXPECT_TRUE(file_util::Delete(temp_path, false));
[email protected]661376a2009-04-29 02:04:232131}
2132
[email protected]7a0bb4bf2008-11-19 21:41:482133TEST_F(URLRequestTest, InvalidUrlTest) {
initial.commit586acc5fe2008-07-26 22:42:522134 TestDelegate d;
2135 {
2136 TestURLRequest r(GURL("invalid url"), &d);
[email protected]87a09a92011-07-14 15:50:502137 r.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:522138
2139 r.Start();
2140 EXPECT_TRUE(r.is_pending());
2141
2142 MessageLoop::current()->Run();
2143 EXPECT_TRUE(d.request_failed());
2144 }
initial.commit586acc5fe2008-07-26 22:42:522145}
2146
[email protected]37314622009-08-17 20:29:392147TEST_F(URLRequestTestHTTP, ResponseHeadersTest) {
[email protected]95409e12010-08-17 20:07:112148 ASSERT_TRUE(test_server_.Start());
2149
initial.commit586acc5fe2008-07-26 22:42:522150 TestDelegate d;
[email protected]95409e12010-08-17 20:07:112151 TestURLRequest req(test_server_.GetURL("files/with-headers.html"), &d);
[email protected]87a09a92011-07-14 15:50:502152 req.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:522153 req.Start();
2154 MessageLoop::current()->Run();
2155
[email protected]7461a402011-03-24 23:19:512156 const HttpResponseHeaders* headers = req.response_headers();
[email protected]589deddb2009-10-05 23:41:402157
2158 // Simple sanity check that response_info() accesses the same data.
2159 EXPECT_EQ(headers, req.response_info().headers.get());
2160
initial.commit586acc5fe2008-07-26 22:42:522161 std::string header;
2162 EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header));
2163 EXPECT_EQ("private", header);
2164
2165 header.clear();
2166 EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header));
2167 EXPECT_EQ("text/html; charset=ISO-8859-1", header);
2168
2169 // The response has two "X-Multiple-Entries" headers.
2170 // This verfies our output has them concatenated together.
2171 header.clear();
2172 EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header));
2173 EXPECT_EQ("a, b", header);
2174}
2175
[email protected]9396b252008-09-29 17:29:382176#if defined(OS_WIN)
[email protected]7a0bb4bf2008-11-19 21:41:482177TEST_F(URLRequestTest, ResolveShortcutTest) {
[email protected]399b8702009-05-01 20:34:022178 FilePath app_path;
initial.commit586acc5fe2008-07-26 22:42:522179 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:022180 app_path = app_path.AppendASCII("net");
2181 app_path = app_path.AppendASCII("data");
2182 app_path = app_path.AppendASCII("url_request_unittest");
2183 app_path = app_path.AppendASCII("with-headers.html");
initial.commit586acc5fe2008-07-26 22:42:522184
[email protected]399b8702009-05-01 20:34:022185 std::wstring lnk_path = app_path.value() + L".lnk";
initial.commit586acc5fe2008-07-26 22:42:522186
2187 HRESULT result;
2188 IShellLink *shell = NULL;
2189 IPersistFile *persist = NULL;
2190
2191 CoInitialize(NULL);
2192 // Temporarily create a shortcut for test
2193 result = CoCreateInstance(CLSID_ShellLink, NULL,
[email protected]5d7b373e2009-09-02 07:19:032194 CLSCTX_INPROC_SERVER, IID_IShellLink,
2195 reinterpret_cast<LPVOID*>(&shell));
[email protected]d149ce82009-07-01 23:57:022196 ASSERT_TRUE(SUCCEEDED(result));
initial.commit586acc5fe2008-07-26 22:42:522197 result = shell->QueryInterface(IID_IPersistFile,
[email protected]5d7b373e2009-09-02 07:19:032198 reinterpret_cast<LPVOID*>(&persist));
[email protected]d149ce82009-07-01 23:57:022199 ASSERT_TRUE(SUCCEEDED(result));
[email protected]399b8702009-05-01 20:34:022200 result = shell->SetPath(app_path.value().c_str());
initial.commit586acc5fe2008-07-26 22:42:522201 EXPECT_TRUE(SUCCEEDED(result));
2202 result = shell->SetDescription(L"ResolveShortcutTest");
2203 EXPECT_TRUE(SUCCEEDED(result));
2204 result = persist->Save(lnk_path.c_str(), TRUE);
2205 EXPECT_TRUE(SUCCEEDED(result));
2206 if (persist)
2207 persist->Release();
2208 if (shell)
2209 shell->Release();
2210
2211 TestDelegate d;
2212 {
[email protected]7461a402011-03-24 23:19:512213 TestURLRequest r(FilePathToFileURL(FilePath(lnk_path)), &d);
[email protected]87a09a92011-07-14 15:50:502214 r.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:522215
2216 r.Start();
2217 EXPECT_TRUE(r.is_pending());
2218
2219 MessageLoop::current()->Run();
2220
2221 WIN32_FILE_ATTRIBUTE_DATA data;
[email protected]399b8702009-05-01 20:34:022222 GetFileAttributesEx(app_path.value().c_str(),
2223 GetFileExInfoStandard, &data);
2224 HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ,
initial.commit586acc5fe2008-07-26 22:42:522225 FILE_SHARE_READ, NULL, OPEN_EXISTING,
2226 FILE_ATTRIBUTE_NORMAL, NULL);
2227 EXPECT_NE(INVALID_HANDLE_VALUE, file);
2228 scoped_array<char> buffer(new char[data.nFileSizeLow]);
2229 DWORD read_size;
2230 BOOL result;
2231 result = ReadFile(file, buffer.get(), data.nFileSizeLow,
2232 &read_size, NULL);
2233 std::string content(buffer.get(), read_size);
2234 CloseHandle(file);
2235
2236 EXPECT_TRUE(!r.is_pending());
2237 EXPECT_EQ(1, d.received_redirect_count());
2238 EXPECT_EQ(content, d.data_received());
2239 }
2240
2241 // Clean the shortcut
2242 DeleteFile(lnk_path.c_str());
2243 CoUninitialize();
initial.commit586acc5fe2008-07-26 22:42:522244}
[email protected]9396b252008-09-29 17:29:382245#endif // defined(OS_WIN)
initial.commit586acc5fe2008-07-26 22:42:522246
[email protected]37314622009-08-17 20:29:392247TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) {
[email protected]95409e12010-08-17 20:07:112248 ASSERT_TRUE(test_server_.Start());
[email protected]dd265012009-01-08 20:45:272249
initial.commit586acc5fe2008-07-26 22:42:522250 TestDelegate d;
[email protected]95409e12010-08-17 20:07:112251 TestURLRequest req(test_server_.GetURL(
initial.commit586acc5fe2008-07-26 22:42:522252 "files/content-type-normalization.html"), &d);
[email protected]87a09a92011-07-14 15:50:502253 req.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:522254 req.Start();
2255 MessageLoop::current()->Run();
2256
2257 std::string mime_type;
2258 req.GetMimeType(&mime_type);
2259 EXPECT_EQ("text/html", mime_type);
2260
2261 std::string charset;
2262 req.GetCharset(&charset);
2263 EXPECT_EQ("utf-8", charset);
2264 req.Cancel();
2265}
2266
[email protected]7a0bb4bf2008-11-19 21:41:482267TEST_F(URLRequestTest, FileDirCancelTest) {
initial.commit586acc5fe2008-07-26 22:42:522268 // Put in mock resource provider.
[email protected]7461a402011-03-24 23:19:512269 NetModule::SetResourceProvider(TestNetResourceProvider);
initial.commit586acc5fe2008-07-26 22:42:522270
2271 TestDelegate d;
2272 {
[email protected]72cbd322009-04-07 10:17:122273 FilePath file_path;
initial.commit586acc5fe2008-07-26 22:42:522274 PathService::Get(base::DIR_SOURCE_ROOT, &file_path);
[email protected]72cbd322009-04-07 10:17:122275 file_path = file_path.Append(FILE_PATH_LITERAL("net"));
2276 file_path = file_path.Append(FILE_PATH_LITERAL("data"));
initial.commit586acc5fe2008-07-26 22:42:522277
[email protected]7461a402011-03-24 23:19:512278 TestURLRequest req(FilePathToFileURL(file_path), &d);
[email protected]87a09a92011-07-14 15:50:502279 req.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:522280 req.Start();
2281 EXPECT_TRUE(req.is_pending());
2282
2283 d.set_cancel_in_received_data_pending(true);
2284
2285 MessageLoop::current()->Run();
2286 }
initial.commit586acc5fe2008-07-26 22:42:522287
2288 // Take out mock resource provider.
[email protected]7461a402011-03-24 23:19:512289 NetModule::SetResourceProvider(NULL);
initial.commit586acc5fe2008-07-26 22:42:522290}
2291
[email protected]7886a8c2009-08-21 04:11:092292TEST_F(URLRequestTest, FileDirRedirectNoCrash) {
2293 // There is an implicit redirect when loading a file path that matches a
2294 // directory and does not end with a slash. Ensure that following such
2295 // redirects does not crash. See http://crbug.com/18686.
2296
2297 FilePath path;
2298 PathService::Get(base::DIR_SOURCE_ROOT, &path);
2299 path = path.Append(FILE_PATH_LITERAL("net"));
2300 path = path.Append(FILE_PATH_LITERAL("data"));
2301 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
2302
2303 TestDelegate d;
[email protected]7461a402011-03-24 23:19:512304 TestURLRequest req(FilePathToFileURL(path), &d);
[email protected]87a09a92011-07-14 15:50:502305 req.set_context(default_context_);
[email protected]7886a8c2009-08-21 04:11:092306 req.Start();
2307 MessageLoop::current()->Run();
2308
[email protected]0db55772010-06-14 22:27:442309 ASSERT_EQ(1, d.received_redirect_count());
2310 ASSERT_LT(0, d.bytes_received());
2311 ASSERT_FALSE(d.request_failed());
2312 ASSERT_TRUE(req.status().is_success());
[email protected]7886a8c2009-08-21 04:11:092313}
2314
[email protected]0db55772010-06-14 22:27:442315#if defined(OS_WIN)
2316// Don't accept the url "file:///" on windows. See http://crbug.com/1474.
2317TEST_F(URLRequestTest, FileDirRedirectSingleSlash) {
2318 TestDelegate d;
2319 TestURLRequest req(GURL("file:///"), &d);
[email protected]87a09a92011-07-14 15:50:502320 req.set_context(default_context_);
[email protected]0db55772010-06-14 22:27:442321 req.Start();
2322 MessageLoop::current()->Run();
2323
2324 ASSERT_EQ(1, d.received_redirect_count());
2325 ASSERT_FALSE(req.status().is_success());
2326}
2327#endif
2328
[email protected]37314622009-08-17 20:29:392329TEST_F(URLRequestTestHTTP, RestrictRedirects) {
[email protected]95409e12010-08-17 20:07:112330 ASSERT_TRUE(test_server_.Start());
[email protected]dd265012009-01-08 20:45:272331
initial.commit586acc5fe2008-07-26 22:42:522332 TestDelegate d;
[email protected]95409e12010-08-17 20:07:112333 TestURLRequest req(test_server_.GetURL(
initial.commit586acc5fe2008-07-26 22:42:522334 "files/redirect-to-file.html"), &d);
[email protected]87a09a92011-07-14 15:50:502335 req.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:522336 req.Start();
2337 MessageLoop::current()->Run();
2338
[email protected]7461a402011-03-24 23:19:512339 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
[email protected]d0cc35b2011-09-08 12:02:052340 EXPECT_EQ(ERR_UNSAFE_REDIRECT, req.status().error());
initial.commit586acc5fe2008-07-26 22:42:522341}
2342
[email protected]37314622009-08-17 20:29:392343TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) {
[email protected]95409e12010-08-17 20:07:112344 ASSERT_TRUE(test_server_.Start());
[email protected]4e66ed12009-07-21 23:38:422345
2346 TestDelegate d;
[email protected]95409e12010-08-17 20:07:112347 TestURLRequest req(test_server_.GetURL(
[email protected]4e66ed12009-07-21 23:38:422348 "files/redirect-to-invalid-url.html"), &d);
[email protected]87a09a92011-07-14 15:50:502349 req.set_context(default_context_);
[email protected]4e66ed12009-07-21 23:38:422350 req.Start();
2351 MessageLoop::current()->Run();
2352
[email protected]7461a402011-03-24 23:19:512353 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
[email protected]d0cc35b2011-09-08 12:02:052354 EXPECT_EQ(ERR_INVALID_URL, req.status().error());
[email protected]4e66ed12009-07-21 23:38:422355}
2356
[email protected]37314622009-08-17 20:29:392357TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) {
[email protected]95409e12010-08-17 20:07:112358 ASSERT_TRUE(test_server_.Start());
2359
initial.commit586acc5fe2008-07-26 22:42:522360 TestDelegate d;
[email protected]87a09a92011-07-14 15:50:502361 TestURLRequest req(test_server_.GetURL("echoheader?Referer"), &d);
2362 req.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:522363 req.set_referrer("http://user:[email protected]/");
2364 req.Start();
2365 MessageLoop::current()->Run();
2366
2367 EXPECT_EQ(std::string("http://foo.com/"), d.data_received());
2368}
2369
[email protected]37314622009-08-17 20:29:392370TEST_F(URLRequestTestHTTP, CancelRedirect) {
[email protected]95409e12010-08-17 20:07:112371 ASSERT_TRUE(test_server_.Start());
2372
initial.commit586acc5fe2008-07-26 22:42:522373 TestDelegate d;
2374 {
2375 d.set_cancel_in_received_redirect(true);
[email protected]87a09a92011-07-14 15:50:502376 TestURLRequest req(test_server_.GetURL("files/redirect-test.html"), &d);
2377 req.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:522378 req.Start();
2379 MessageLoop::current()->Run();
2380
2381 EXPECT_EQ(1, d.response_started_count());
2382 EXPECT_EQ(0, d.bytes_received());
2383 EXPECT_FALSE(d.received_data_before_response());
[email protected]7461a402011-03-24 23:19:512384 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
initial.commit586acc5fe2008-07-26 22:42:522385 }
2386}
2387
[email protected]37314622009-08-17 20:29:392388TEST_F(URLRequestTestHTTP, DeferredRedirect) {
[email protected]95409e12010-08-17 20:07:112389 ASSERT_TRUE(test_server_.Start());
2390
[email protected]195e77d2009-07-23 19:10:232391 TestDelegate d;
2392 {
2393 d.set_quit_on_redirect(true);
[email protected]87a09a92011-07-14 15:50:502394 TestURLRequest req(test_server_.GetURL("files/redirect-test.html"), &d);
2395 req.set_context(default_context_);
[email protected]195e77d2009-07-23 19:10:232396 req.Start();
2397 MessageLoop::current()->Run();
2398
2399 EXPECT_EQ(1, d.received_redirect_count());
2400
2401 req.FollowDeferredRedirect();
2402 MessageLoop::current()->Run();
2403
2404 EXPECT_EQ(1, d.response_started_count());
2405 EXPECT_FALSE(d.received_data_before_response());
[email protected]7461a402011-03-24 23:19:512406 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
[email protected]195e77d2009-07-23 19:10:232407
2408 FilePath path;
2409 PathService::Get(base::DIR_SOURCE_ROOT, &path);
2410 path = path.Append(FILE_PATH_LITERAL("net"));
2411 path = path.Append(FILE_PATH_LITERAL("data"));
2412 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
2413 path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
2414
2415 std::string contents;
2416 EXPECT_TRUE(file_util::ReadFileToString(path, &contents));
2417 EXPECT_EQ(contents, d.data_received());
2418 }
2419}
2420
[email protected]37314622009-08-17 20:29:392421TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) {
[email protected]95409e12010-08-17 20:07:112422 ASSERT_TRUE(test_server_.Start());
2423
[email protected]195e77d2009-07-23 19:10:232424 TestDelegate d;
2425 {
2426 d.set_quit_on_redirect(true);
[email protected]87a09a92011-07-14 15:50:502427 TestURLRequest req(test_server_.GetURL("files/redirect-test.html"), &d);
2428 req.set_context(default_context_);
[email protected]195e77d2009-07-23 19:10:232429 req.Start();
2430 MessageLoop::current()->Run();
2431
2432 EXPECT_EQ(1, d.received_redirect_count());
2433
2434 req.Cancel();
2435 MessageLoop::current()->Run();
2436
2437 EXPECT_EQ(1, d.response_started_count());
2438 EXPECT_EQ(0, d.bytes_received());
2439 EXPECT_FALSE(d.received_data_before_response());
[email protected]7461a402011-03-24 23:19:512440 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
[email protected]195e77d2009-07-23 19:10:232441 }
2442}
2443
[email protected]37314622009-08-17 20:29:392444TEST_F(URLRequestTestHTTP, VaryHeader) {
[email protected]95409e12010-08-17 20:07:112445 ASSERT_TRUE(test_server_.Start());
initial.commit586acc5fe2008-07-26 22:42:522446
initial.commit586acc5fe2008-07-26 22:42:522447 // populate the cache
2448 {
2449 TestDelegate d;
[email protected]1bc869d52011-04-07 17:52:052450 URLRequest req(test_server_.GetURL("echoheadercache?foo"), &d);
[email protected]87a09a92011-07-14 15:50:502451 req.set_context(default_context_);
[email protected]7461a402011-03-24 23:19:512452 HttpRequestHeaders headers;
[email protected]88e6b6f32010-05-07 23:14:252453 headers.SetHeader("foo", "1");
2454 req.SetExtraRequestHeaders(headers);
initial.commit586acc5fe2008-07-26 22:42:522455 req.Start();
2456 MessageLoop::current()->Run();
initial.commit586acc5fe2008-07-26 22:42:522457 }
2458
initial.commit586acc5fe2008-07-26 22:42:522459 // expect a cache hit
2460 {
2461 TestDelegate d;
[email protected]1bc869d52011-04-07 17:52:052462 URLRequest req(test_server_.GetURL("echoheadercache?foo"), &d);
[email protected]87a09a92011-07-14 15:50:502463 req.set_context(default_context_);
[email protected]7461a402011-03-24 23:19:512464 HttpRequestHeaders headers;
[email protected]88e6b6f32010-05-07 23:14:252465 headers.SetHeader("foo", "1");
2466 req.SetExtraRequestHeaders(headers);
initial.commit586acc5fe2008-07-26 22:42:522467 req.Start();
2468 MessageLoop::current()->Run();
2469
[email protected]cb4ff9d2009-09-04 22:51:532470 EXPECT_TRUE(req.was_cached());
initial.commit586acc5fe2008-07-26 22:42:522471 }
2472
2473 // expect a cache miss
2474 {
2475 TestDelegate d;
[email protected]1bc869d52011-04-07 17:52:052476 URLRequest req(test_server_.GetURL("echoheadercache?foo"), &d);
[email protected]87a09a92011-07-14 15:50:502477 req.set_context(default_context_);
[email protected]7461a402011-03-24 23:19:512478 HttpRequestHeaders headers;
[email protected]88e6b6f32010-05-07 23:14:252479 headers.SetHeader("foo", "2");
2480 req.SetExtraRequestHeaders(headers);
initial.commit586acc5fe2008-07-26 22:42:522481 req.Start();
2482 MessageLoop::current()->Run();
2483
[email protected]cb4ff9d2009-09-04 22:51:532484 EXPECT_FALSE(req.was_cached());
initial.commit586acc5fe2008-07-26 22:42:522485 }
2486}
2487
[email protected]cb4ff9d2009-09-04 22:51:532488TEST_F(URLRequestTestHTTP, BasicAuth) {
[email protected]95409e12010-08-17 20:07:112489 ASSERT_TRUE(test_server_.Start());
2490
initial.commit586acc5fe2008-07-26 22:42:522491 // populate the cache
2492 {
2493 TestDelegate d;
[email protected]f3cf9802011-10-28 18:44:582494 d.set_credentials(AuthCredentials(kUser, kSecret));
initial.commit586acc5fe2008-07-26 22:42:522495
[email protected]7461a402011-03-24 23:19:512496 URLRequest r(test_server_.GetURL("auth-basic"), &d);
[email protected]87a09a92011-07-14 15:50:502497 r.set_context(default_context_);
initial.commit586acc5fe2008-07-26 22:42:522498 r.Start();
2499
2500 MessageLoop::current()->Run();
2501
2502 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
initial.commit586acc5fe2008-07-26 22:42:522503 }
2504
initial.commit586acc5fe2008-07-26 22:42:522505 // repeat request with end-to-end validation. since auth-basic results in a
2506 // cachable page, we expect this test to result in a 304. in which case, the
2507 // response should be fetched from the cache.
2508 {
2509 TestDelegate d;
[email protected]f3cf9802011-10-28 18:44:582510 d.set_credentials(AuthCredentials(kUser, kSecret));
initial.commit586acc5fe2008-07-26 22:42:522511
[email protected]7461a402011-03-24 23:19:512512 URLRequest r(test_server_.GetURL("auth-basic"), &d);
[email protected]87a09a92011-07-14 15:50:502513 r.set_context(default_context_);
[email protected]7461a402011-03-24 23:19:512514 r.set_load_flags(LOAD_VALIDATE_CACHE);
initial.commit586acc5fe2008-07-26 22:42:522515 r.Start();
2516
2517 MessageLoop::current()->Run();
2518
2519 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
2520
[email protected]cb4ff9d2009-09-04 22:51:532521 // Should be the same cached document.
2522 EXPECT_TRUE(r.was_cached());
initial.commit586acc5fe2008-07-26 22:42:522523 }
2524}
license.botbf09a502008-08-24 00:55:552525
[email protected]0757e7702009-03-27 04:00:222526// Check that Set-Cookie headers in 401 responses are respected.
2527// http://crbug.com/6450
[email protected]be6fca6c2010-01-30 21:48:572528TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) {
[email protected]95409e12010-08-17 20:07:112529 ASSERT_TRUE(test_server_.Start());
[email protected]0757e7702009-03-27 04:00:222530
2531 GURL url_requiring_auth =
[email protected]95409e12010-08-17 20:07:112532 test_server_.GetURL("auth-basic?set-cookie-if-challenged");
[email protected]0757e7702009-03-27 04:00:222533
2534 // Request a page that will give a 401 containing a Set-Cookie header.
2535 // Verify that when the transaction is restarted, it includes the new cookie.
2536 {
[email protected]87a09a92011-07-14 15:50:502537 TestNetworkDelegate network_delegate; // must outlive URLRequest
2538 scoped_refptr<TestURLRequestContext> context(
2539 new TestURLRequestContext(true));
2540 context->set_network_delegate(&network_delegate);
2541 context->Init();
2542
[email protected]0757e7702009-03-27 04:00:222543 TestDelegate d;
[email protected]f3cf9802011-10-28 18:44:582544 d.set_credentials(AuthCredentials(kUser, kSecret));
[email protected]0757e7702009-03-27 04:00:222545
[email protected]7461a402011-03-24 23:19:512546 URLRequest r(url_requiring_auth, &d);
[email protected]0757e7702009-03-27 04:00:222547 r.set_context(context);
2548 r.Start();
2549
2550 MessageLoop::current()->Run();
2551
2552 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
2553
2554 // Make sure we sent the cookie in the restarted transaction.
2555 EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true")
2556 != std::string::npos);
2557 }
[email protected]0757e7702009-03-27 04:00:222558}
2559
[email protected]54f4c9362011-07-25 21:54:462560TEST_F(URLRequestTest, DelayedCookieCallback) {
[email protected]1700c6a2012-02-22 18:07:072561 LocalHttpTestServer test_server;
[email protected]54f4c9362011-07-25 21:54:462562 ASSERT_TRUE(test_server.Start());
2563
2564 scoped_refptr<URLRequestContext> context(new TestURLRequestContext());
2565 scoped_refptr<DelayedCookieMonster> delayed_cm =
2566 new DelayedCookieMonster();
2567 scoped_refptr<CookieStore> cookie_store = delayed_cm;
2568 context->set_cookie_store(delayed_cm);
2569
2570 // Set up a cookie.
2571 {
[email protected]9c8ae8c2012-03-09 13:13:352572 TestNetworkDelegate network_delegate;
2573 context->set_network_delegate(&network_delegate);
[email protected]54f4c9362011-07-25 21:54:462574 TestDelegate d;
2575 URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"), &d);
2576 req.set_context(context);
2577 req.Start();
2578 MessageLoop::current()->Run();
[email protected]9c8ae8c2012-03-09 13:13:352579 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2580 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2581 EXPECT_EQ(1, network_delegate.set_cookie_count());
[email protected]54f4c9362011-07-25 21:54:462582 }
2583
2584 // Verify that the cookie is set.
2585 {
[email protected]9c8ae8c2012-03-09 13:13:352586 TestNetworkDelegate network_delegate;
2587 context->set_network_delegate(&network_delegate);
[email protected]54f4c9362011-07-25 21:54:462588 TestDelegate d;
2589 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
2590 req.set_context(context);
2591 req.Start();
2592 MessageLoop::current()->Run();
2593
2594 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2595 != std::string::npos);
[email protected]9c8ae8c2012-03-09 13:13:352596 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2597 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
[email protected]54f4c9362011-07-25 21:54:462598 }
2599}
2600
[email protected]be6fca6c2010-01-30 21:48:572601TEST_F(URLRequestTest, DoNotSendCookies) {
[email protected]1700c6a2012-02-22 18:07:072602 LocalHttpTestServer test_server;
[email protected]95409e12010-08-17 20:07:112603 ASSERT_TRUE(test_server.Start());
2604
[email protected]861fcd52009-08-26 02:33:462605 // Set up a cookie.
2606 {
[email protected]9c8ae8c2012-03-09 13:13:352607 TestNetworkDelegate network_delegate;
2608 default_context_->set_network_delegate(&network_delegate);
[email protected]861fcd52009-08-26 02:33:462609 TestDelegate d;
[email protected]7461a402011-03-24 23:19:512610 URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"), &d);
[email protected]87a09a92011-07-14 15:50:502611 req.set_context(default_context_);
[email protected]861fcd52009-08-26 02:33:462612 req.Start();
2613 MessageLoop::current()->Run();
[email protected]9c8ae8c2012-03-09 13:13:352614 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2615 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
[email protected]861fcd52009-08-26 02:33:462616 }
2617
2618 // Verify that the cookie is set.
2619 {
[email protected]9c8ae8c2012-03-09 13:13:352620 TestNetworkDelegate network_delegate;
2621 default_context_->set_network_delegate(&network_delegate);
[email protected]861fcd52009-08-26 02:33:462622 TestDelegate d;
[email protected]95409e12010-08-17 20:07:112623 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]87a09a92011-07-14 15:50:502624 req.set_context(default_context_);
[email protected]861fcd52009-08-26 02:33:462625 req.Start();
2626 MessageLoop::current()->Run();
2627
2628 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2629 != std::string::npos);
[email protected]9c8ae8c2012-03-09 13:13:352630 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2631 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
[email protected]861fcd52009-08-26 02:33:462632 }
2633
2634 // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set.
2635 {
[email protected]9c8ae8c2012-03-09 13:13:352636 TestNetworkDelegate network_delegate;
2637 default_context_->set_network_delegate(&network_delegate);
[email protected]861fcd52009-08-26 02:33:462638 TestDelegate d;
[email protected]95409e12010-08-17 20:07:112639 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]7461a402011-03-24 23:19:512640 req.set_load_flags(LOAD_DO_NOT_SEND_COOKIES);
[email protected]87a09a92011-07-14 15:50:502641 req.set_context(default_context_);
[email protected]861fcd52009-08-26 02:33:462642 req.Start();
2643 MessageLoop::current()->Run();
2644
2645 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
2646 == std::string::npos);
[email protected]abf03a02010-03-12 05:02:132647
[email protected]9fb83e82010-07-02 18:24:552648 // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies.
[email protected]9c8ae8c2012-03-09 13:13:352649 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2650 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
[email protected]861fcd52009-08-26 02:33:462651 }
[email protected]9c8ae8c2012-03-09 13:13:352652
2653 default_context_->set_network_delegate(&default_network_delegate_);
[email protected]861fcd52009-08-26 02:33:462654}
2655
[email protected]be6fca6c2010-01-30 21:48:572656TEST_F(URLRequestTest, DoNotSaveCookies) {
[email protected]1700c6a2012-02-22 18:07:072657 LocalHttpTestServer test_server;
[email protected]95409e12010-08-17 20:07:112658 ASSERT_TRUE(test_server.Start());
2659
[email protected]861fcd52009-08-26 02:33:462660 // Set up a cookie.
2661 {
[email protected]9c8ae8c2012-03-09 13:13:352662 TestNetworkDelegate network_delegate;
2663 default_context_->set_network_delegate(&network_delegate);
[email protected]861fcd52009-08-26 02:33:462664 TestDelegate d;
[email protected]87a09a92011-07-14 15:50:502665 URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"), &d);
2666 req.set_context(default_context_);
[email protected]861fcd52009-08-26 02:33:462667 req.Start();
2668 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:202669
[email protected]9c8ae8c2012-03-09 13:13:352670 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2671 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2672 EXPECT_EQ(1, network_delegate.set_cookie_count());
[email protected]861fcd52009-08-26 02:33:462673 }
2674
2675 // Try to set-up another cookie and update the previous cookie.
2676 {
[email protected]9c8ae8c2012-03-09 13:13:352677 TestNetworkDelegate network_delegate;
2678 default_context_->set_network_delegate(&network_delegate);
[email protected]861fcd52009-08-26 02:33:462679 TestDelegate d;
[email protected]7461a402011-03-24 23:19:512680 URLRequest req(test_server.GetURL(
[email protected]861fcd52009-08-26 02:33:462681 "set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), &d);
[email protected]7461a402011-03-24 23:19:512682 req.set_load_flags(LOAD_DO_NOT_SAVE_COOKIES);
[email protected]87a09a92011-07-14 15:50:502683 req.set_context(default_context_);
[email protected]861fcd52009-08-26 02:33:462684 req.Start();
2685
2686 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:202687
[email protected]9fb83e82010-07-02 18:24:552688 // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie.
[email protected]9c8ae8c2012-03-09 13:13:352689 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2690 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2691 EXPECT_EQ(0, network_delegate.set_cookie_count());
[email protected]861fcd52009-08-26 02:33:462692 }
2693
2694 // Verify the cookies weren't saved or updated.
2695 {
[email protected]9c8ae8c2012-03-09 13:13:352696 TestNetworkDelegate network_delegate;
2697 default_context_->set_network_delegate(&network_delegate);
[email protected]861fcd52009-08-26 02:33:462698 TestDelegate d;
[email protected]95409e12010-08-17 20:07:112699 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]87a09a92011-07-14 15:50:502700 req.set_context(default_context_);
[email protected]861fcd52009-08-26 02:33:462701 req.Start();
2702 MessageLoop::current()->Run();
2703
2704 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
2705 == std::string::npos);
2706 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
2707 != std::string::npos);
[email protected]3dbb80b2010-02-09 22:41:202708
[email protected]9c8ae8c2012-03-09 13:13:352709 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2710 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2711 EXPECT_EQ(0, network_delegate.set_cookie_count());
[email protected]861fcd52009-08-26 02:33:462712 }
[email protected]9c8ae8c2012-03-09 13:13:352713
2714 default_context_->set_network_delegate(&default_network_delegate_);
[email protected]861fcd52009-08-26 02:33:462715}
2716
[email protected]34602282010-02-03 22:14:152717TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) {
[email protected]1700c6a2012-02-22 18:07:072718 LocalHttpTestServer test_server;
[email protected]95409e12010-08-17 20:07:112719 ASSERT_TRUE(test_server.Start());
2720
[email protected]34602282010-02-03 22:14:152721 // Set up a cookie.
2722 {
[email protected]9c8ae8c2012-03-09 13:13:352723 TestNetworkDelegate network_delegate;
2724 default_context_->set_network_delegate(&network_delegate);
[email protected]34602282010-02-03 22:14:152725 TestDelegate d;
[email protected]7461a402011-03-24 23:19:512726 URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"), &d);
[email protected]87a09a92011-07-14 15:50:502727 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:152728 req.Start();
2729 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:202730
[email protected]9c8ae8c2012-03-09 13:13:352731 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2732 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:152733 }
2734
2735 // Verify that the cookie is set.
2736 {
[email protected]9c8ae8c2012-03-09 13:13:352737 TestNetworkDelegate network_delegate;
2738 default_context_->set_network_delegate(&network_delegate);
[email protected]34602282010-02-03 22:14:152739 TestDelegate d;
[email protected]95409e12010-08-17 20:07:112740 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]87a09a92011-07-14 15:50:502741 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:152742 req.Start();
2743 MessageLoop::current()->Run();
2744
2745 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2746 != std::string::npos);
[email protected]3dbb80b2010-02-09 22:41:202747
[email protected]9c8ae8c2012-03-09 13:13:352748 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2749 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:152750 }
2751
2752 // Verify that the cookie isn't sent.
2753 {
[email protected]9c8ae8c2012-03-09 13:13:352754 TestNetworkDelegate network_delegate;
2755 default_context_->set_network_delegate(&network_delegate);
[email protected]34602282010-02-03 22:14:152756 TestDelegate d;
[email protected]9c8ae8c2012-03-09 13:13:352757 network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES);
[email protected]95409e12010-08-17 20:07:112758 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]87a09a92011-07-14 15:50:502759 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:152760 req.Start();
2761 MessageLoop::current()->Run();
2762
2763 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
2764 == std::string::npos);
2765
[email protected]9c8ae8c2012-03-09 13:13:352766 EXPECT_EQ(1, network_delegate.blocked_get_cookies_count());
2767 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:152768 }
[email protected]9c8ae8c2012-03-09 13:13:352769
2770 default_context_->set_network_delegate(&default_network_delegate_);
[email protected]34602282010-02-03 22:14:152771}
2772
2773TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) {
[email protected]1700c6a2012-02-22 18:07:072774 LocalHttpTestServer test_server;
[email protected]95409e12010-08-17 20:07:112775 ASSERT_TRUE(test_server.Start());
2776
[email protected]34602282010-02-03 22:14:152777 // Set up a cookie.
2778 {
[email protected]9c8ae8c2012-03-09 13:13:352779 TestNetworkDelegate network_delegate;
2780 default_context_->set_network_delegate(&network_delegate);
[email protected]34602282010-02-03 22:14:152781 TestDelegate d;
[email protected]87a09a92011-07-14 15:50:502782 URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"), &d);
2783 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:152784 req.Start();
2785 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:202786
[email protected]9c8ae8c2012-03-09 13:13:352787 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2788 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:152789 }
2790
2791 // Try to set-up another cookie and update the previous cookie.
2792 {
[email protected]9c8ae8c2012-03-09 13:13:352793 TestNetworkDelegate network_delegate;
2794 default_context_->set_network_delegate(&network_delegate);
[email protected]34602282010-02-03 22:14:152795 TestDelegate d;
[email protected]9c8ae8c2012-03-09 13:13:352796 network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE);
[email protected]7461a402011-03-24 23:19:512797 URLRequest req(test_server.GetURL(
[email protected]34602282010-02-03 22:14:152798 "set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), &d);
[email protected]87a09a92011-07-14 15:50:502799 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:152800 req.Start();
2801
2802 MessageLoop::current()->Run();
2803
[email protected]9c8ae8c2012-03-09 13:13:352804 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2805 EXPECT_EQ(2, network_delegate.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:152806 }
2807
2808
2809 // Verify the cookies weren't saved or updated.
2810 {
[email protected]9c8ae8c2012-03-09 13:13:352811 TestNetworkDelegate network_delegate;
2812 default_context_->set_network_delegate(&network_delegate);
[email protected]34602282010-02-03 22:14:152813 TestDelegate d;
[email protected]95409e12010-08-17 20:07:112814 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]87a09a92011-07-14 15:50:502815 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:152816 req.Start();
2817 MessageLoop::current()->Run();
2818
2819 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
2820 == std::string::npos);
2821 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
2822 != std::string::npos);
[email protected]3dbb80b2010-02-09 22:41:202823
[email protected]9c8ae8c2012-03-09 13:13:352824 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2825 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:152826 }
[email protected]9c8ae8c2012-03-09 13:13:352827
2828 default_context_->set_network_delegate(&default_network_delegate_);
[email protected]34602282010-02-03 22:14:152829}
2830
[email protected]2adf2882010-09-27 08:30:372831TEST_F(URLRequestTest, DoNotSaveEmptyCookies) {
[email protected]1700c6a2012-02-22 18:07:072832 LocalHttpTestServer test_server;
[email protected]2adf2882010-09-27 08:30:372833 ASSERT_TRUE(test_server.Start());
2834
[email protected]2adf2882010-09-27 08:30:372835 // Set up an empty cookie.
2836 {
[email protected]9c8ae8c2012-03-09 13:13:352837 TestNetworkDelegate network_delegate;
2838 default_context_->set_network_delegate(&network_delegate);
[email protected]2adf2882010-09-27 08:30:372839 TestDelegate d;
[email protected]7461a402011-03-24 23:19:512840 URLRequest req(test_server.GetURL("set-cookie"), &d);
[email protected]87a09a92011-07-14 15:50:502841 req.set_context(default_context_);
[email protected]2adf2882010-09-27 08:30:372842 req.Start();
2843 MessageLoop::current()->Run();
2844
[email protected]9c8ae8c2012-03-09 13:13:352845 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2846 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2847 EXPECT_EQ(0, network_delegate.set_cookie_count());
[email protected]2adf2882010-09-27 08:30:372848 }
[email protected]9c8ae8c2012-03-09 13:13:352849
2850 default_context_->set_network_delegate(&default_network_delegate_);
[email protected]2adf2882010-09-27 08:30:372851}
2852
[email protected]34602282010-02-03 22:14:152853TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) {
[email protected]1700c6a2012-02-22 18:07:072854 LocalHttpTestServer test_server;
[email protected]95409e12010-08-17 20:07:112855 ASSERT_TRUE(test_server.Start());
2856
[email protected]34602282010-02-03 22:14:152857 // Set up a cookie.
2858 {
[email protected]9c8ae8c2012-03-09 13:13:352859 TestNetworkDelegate network_delegate;
2860 default_context_->set_network_delegate(&network_delegate);
[email protected]34602282010-02-03 22:14:152861 TestDelegate d;
[email protected]7461a402011-03-24 23:19:512862 URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"), &d);
[email protected]87a09a92011-07-14 15:50:502863 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:152864 req.Start();
2865 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:202866
[email protected]9c8ae8c2012-03-09 13:13:352867 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2868 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:152869 }
2870
2871 // Verify that the cookie is set.
2872 {
[email protected]9c8ae8c2012-03-09 13:13:352873 TestNetworkDelegate network_delegate;
2874 default_context_->set_network_delegate(&network_delegate);
[email protected]34602282010-02-03 22:14:152875 TestDelegate d;
[email protected]95409e12010-08-17 20:07:112876 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]87a09a92011-07-14 15:50:502877 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:152878 req.Start();
2879 MessageLoop::current()->Run();
2880
2881 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2882 != std::string::npos);
[email protected]3dbb80b2010-02-09 22:41:202883
[email protected]9c8ae8c2012-03-09 13:13:352884 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2885 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:152886 }
2887
2888 // Verify that the cookie isn't sent.
2889 {
[email protected]9c8ae8c2012-03-09 13:13:352890 TestNetworkDelegate network_delegate;
2891 default_context_->set_network_delegate(&network_delegate);
[email protected]34602282010-02-03 22:14:152892 TestDelegate d;
[email protected]9c8ae8c2012-03-09 13:13:352893 network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES);
[email protected]95409e12010-08-17 20:07:112894 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]87a09a92011-07-14 15:50:502895 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:152896 req.Start();
2897 MessageLoop::current()->Run();
2898
2899 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
2900 == std::string::npos);
2901
[email protected]9c8ae8c2012-03-09 13:13:352902 EXPECT_EQ(1, network_delegate.blocked_get_cookies_count());
2903 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:152904 }
[email protected]9c8ae8c2012-03-09 13:13:352905
2906 default_context_->set_network_delegate(&default_network_delegate_);
[email protected]34602282010-02-03 22:14:152907}
2908
2909TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) {
[email protected]1700c6a2012-02-22 18:07:072910 LocalHttpTestServer test_server;
[email protected]95409e12010-08-17 20:07:112911 ASSERT_TRUE(test_server.Start());
2912
[email protected]34602282010-02-03 22:14:152913 // Set up a cookie.
2914 {
[email protected]9c8ae8c2012-03-09 13:13:352915 TestNetworkDelegate network_delegate;
2916 default_context_->set_network_delegate(&network_delegate);
[email protected]34602282010-02-03 22:14:152917 TestDelegate d;
[email protected]87a09a92011-07-14 15:50:502918 URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"), &d);
2919 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:152920 req.Start();
2921 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:202922
[email protected]9c8ae8c2012-03-09 13:13:352923 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2924 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:152925 }
2926
2927 // Try to set-up another cookie and update the previous cookie.
2928 {
[email protected]9c8ae8c2012-03-09 13:13:352929 TestNetworkDelegate network_delegate;
2930 default_context_->set_network_delegate(&network_delegate);
[email protected]34602282010-02-03 22:14:152931 TestDelegate d;
[email protected]9c8ae8c2012-03-09 13:13:352932 network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE);
[email protected]7461a402011-03-24 23:19:512933 URLRequest req(test_server.GetURL(
[email protected]34602282010-02-03 22:14:152934 "set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), &d);
[email protected]87a09a92011-07-14 15:50:502935 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:152936 req.Start();
2937
2938 MessageLoop::current()->Run();
2939
[email protected]9c8ae8c2012-03-09 13:13:352940 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2941 EXPECT_EQ(2, network_delegate.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:152942 }
2943
2944 // Verify the cookies weren't saved or updated.
2945 {
[email protected]9c8ae8c2012-03-09 13:13:352946 TestNetworkDelegate network_delegate;
2947 default_context_->set_network_delegate(&network_delegate);
[email protected]34602282010-02-03 22:14:152948 TestDelegate d;
[email protected]95409e12010-08-17 20:07:112949 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]87a09a92011-07-14 15:50:502950 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:152951 req.Start();
2952 MessageLoop::current()->Run();
2953
2954 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
2955 == std::string::npos);
2956 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
2957 != std::string::npos);
[email protected]3dbb80b2010-02-09 22:41:202958
[email protected]9c8ae8c2012-03-09 13:13:352959 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2960 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:152961 }
[email protected]9c8ae8c2012-03-09 13:13:352962
2963 default_context_->set_network_delegate(&default_network_delegate_);
[email protected]34602282010-02-03 22:14:152964}
2965
[email protected]218aa6a12011-09-13 17:38:382966void CheckCookiePolicyCallback(bool* was_run, const CookieList& cookies) {
2967 EXPECT_EQ(1U, cookies.size());
2968 EXPECT_FALSE(cookies[0].IsPersistent());
2969 *was_run = true;
[email protected]72d27ce2011-12-09 00:41:122970 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
[email protected]218aa6a12011-09-13 17:38:382971}
2972
[email protected]4f79b3f2010-02-05 04:27:472973TEST_F(URLRequestTest, CookiePolicy_ForceSession) {
[email protected]1700c6a2012-02-22 18:07:072974 LocalHttpTestServer test_server;
[email protected]95409e12010-08-17 20:07:112975 ASSERT_TRUE(test_server.Start());
2976
[email protected]4f79b3f2010-02-05 04:27:472977 // Set up a cookie.
2978 {
[email protected]9c8ae8c2012-03-09 13:13:352979 TestNetworkDelegate network_delegate;
2980 default_context_->set_network_delegate(&network_delegate);
[email protected]4f79b3f2010-02-05 04:27:472981 TestDelegate d;
[email protected]9c8ae8c2012-03-09 13:13:352982 network_delegate.set_cookie_options(TestNetworkDelegate::FORCE_SESSION);
[email protected]7461a402011-03-24 23:19:512983 URLRequest req(test_server.GetURL(
[email protected]4f79b3f2010-02-05 04:27:472984 "set-cookie?A=1;expires=\"Fri, 05 Feb 2010 23:42:01 GMT\""), &d);
[email protected]87a09a92011-07-14 15:50:502985 req.set_context(default_context_);
[email protected]4f79b3f2010-02-05 04:27:472986 req.Start(); // Triggers an asynchronous cookie policy check.
2987
2988 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:202989
[email protected]9c8ae8c2012-03-09 13:13:352990 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2991 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
[email protected]4f79b3f2010-02-05 04:27:472992 }
[email protected]9c8ae8c2012-03-09 13:13:352993 default_context_->set_network_delegate(&default_network_delegate_);
[email protected]4f79b3f2010-02-05 04:27:472994
2995 // Now, check the cookie store.
[email protected]218aa6a12011-09-13 17:38:382996 bool was_run = false;
2997 default_context_->cookie_store()->GetCookieMonster()->GetAllCookiesAsync(
2998 base::Bind(&CheckCookiePolicyCallback, &was_run));
2999 MessageLoop::current()->RunAllPending();
3000 DCHECK(was_run);
[email protected]4f79b3f2010-02-05 04:27:473001}
3002
[email protected]71c64f62008-11-15 04:36:513003// In this test, we do a POST which the server will 302 redirect.
3004// The subsequent transaction should use GET, and should not send the
3005// Content-Type header.
3006// http://code.google.com/p/chromium/issues/detail?id=843
[email protected]37314622009-08-17 20:29:393007TEST_F(URLRequestTestHTTP, Post302RedirectGet) {
[email protected]95409e12010-08-17 20:07:113008 ASSERT_TRUE(test_server_.Start());
3009
[email protected]195e77d2009-07-23 19:10:233010 const char kData[] = "hello world";
[email protected]95409e12010-08-17 20:07:113011
[email protected]71c64f62008-11-15 04:36:513012 TestDelegate d;
[email protected]95409e12010-08-17 20:07:113013 TestURLRequest req(test_server_.GetURL("files/redirect-to-echoall"), &d);
[email protected]87a09a92011-07-14 15:50:503014 req.set_context(default_context_);
[email protected]71c64f62008-11-15 04:36:513015 req.set_method("POST");
[email protected]195e77d2009-07-23 19:10:233016 req.set_upload(CreateSimpleUploadData(kData));
[email protected]71c64f62008-11-15 04:36:513017
3018 // Set headers (some of which are specific to the POST).
[email protected]7461a402011-03-24 23:19:513019 HttpRequestHeaders headers;
[email protected]88e6b6f32010-05-07 23:14:253020 headers.AddHeadersFromString(
[email protected]dd265012009-01-08 20:45:273021 "Content-Type: multipart/form-data; "
3022 "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n"
3023 "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,"
3024 "text/plain;q=0.8,image/png,*/*;q=0.5\r\n"
[email protected]71c64f62008-11-15 04:36:513025 "Accept-Language: en-US,en\r\n"
3026 "Accept-Charset: ISO-8859-1,*,utf-8\r\n"
[email protected]195e77d2009-07-23 19:10:233027 "Content-Length: 11\r\n"
[email protected]a86c97cc2009-06-24 21:26:273028 "Origin: http://localhost:1337/");
[email protected]88e6b6f32010-05-07 23:14:253029 req.SetExtraRequestHeaders(headers);
[email protected]71c64f62008-11-15 04:36:513030 req.Start();
3031 MessageLoop::current()->Run();
3032
3033 std::string mime_type;
3034 req.GetMimeType(&mime_type);
3035 EXPECT_EQ("text/html", mime_type);
3036
3037 const std::string& data = d.data_received();
3038
3039 // Check that the post-specific headers were stripped:
3040 EXPECT_FALSE(ContainsString(data, "Content-Length:"));
3041 EXPECT_FALSE(ContainsString(data, "Content-Type:"));
3042 EXPECT_FALSE(ContainsString(data, "Origin:"));
3043
3044 // These extra request headers should not have been stripped.
3045 EXPECT_TRUE(ContainsString(data, "Accept:"));
3046 EXPECT_TRUE(ContainsString(data, "Accept-Language:"));
3047 EXPECT_TRUE(ContainsString(data, "Accept-Charset:"));
3048}
3049
[email protected]21184962011-10-26 00:50:303050// The following tests check that we handle mutating the request method for
[email protected]bbb011f62011-11-01 02:32:053051// HTTP redirects as expected.
3052// See http://crbug.com/56373 and http://crbug.com/102130.
[email protected]21184962011-10-26 00:50:303053
3054TEST_F(URLRequestTestHTTP, Redirect301Tests) {
[email protected]95409e12010-08-17 20:07:113055 ASSERT_TRUE(test_server_.Start());
3056
[email protected]21184962011-10-26 00:50:303057 const GURL url = test_server_.GetURL("files/redirect301-to-echo");
[email protected]95409e12010-08-17 20:07:113058
[email protected]21184962011-10-26 00:50:303059 HTTPRedirectMethodTest(url, "POST", "GET", true);
3060 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
[email protected]bbb011f62011-11-01 02:32:053061 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
[email protected]21184962011-10-26 00:50:303062}
3063
3064TEST_F(URLRequestTestHTTP, Redirect302Tests) {
3065 ASSERT_TRUE(test_server_.Start());
3066
3067 const GURL url = test_server_.GetURL("files/redirect302-to-echo");
3068
3069 HTTPRedirectMethodTest(url, "POST", "GET", true);
3070 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
[email protected]bbb011f62011-11-01 02:32:053071 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
[email protected]21184962011-10-26 00:50:303072}
3073
3074TEST_F(URLRequestTestHTTP, Redirect303Tests) {
3075 ASSERT_TRUE(test_server_.Start());
3076
3077 const GURL url = test_server_.GetURL("files/redirect303-to-echo");
3078
3079 HTTPRedirectMethodTest(url, "POST", "GET", true);
3080 HTTPRedirectMethodTest(url, "PUT", "GET", true);
[email protected]bbb011f62011-11-01 02:32:053081 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
[email protected]21184962011-10-26 00:50:303082}
3083
3084TEST_F(URLRequestTestHTTP, Redirect307Tests) {
3085 ASSERT_TRUE(test_server_.Start());
3086
3087 const GURL url = test_server_.GetURL("files/redirect307-to-echo");
3088
3089 HTTPRedirectMethodTest(url, "POST", "POST", true);
3090 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
[email protected]bbb011f62011-11-01 02:32:053091 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
[email protected]140932f2008-12-12 03:58:063092}
[email protected]dd265012009-01-08 20:45:273093
[email protected]3c5ca8c2011-09-29 01:14:513094TEST_F(URLRequestTestHTTP, InterceptPost302RedirectGet) {
3095 ASSERT_TRUE(test_server_.Start());
3096
3097 const char kData[] = "hello world";
3098
3099 TestDelegate d;
3100 TestURLRequest req(test_server_.GetURL("empty.html"), &d);
3101 req.set_context(default_context_);
3102 req.set_method("POST");
3103 req.set_upload(CreateSimpleUploadData(kData).get());
3104 HttpRequestHeaders headers;
3105 headers.SetHeader(HttpRequestHeaders::kContentLength,
3106 base::UintToString(arraysize(kData) - 1));
3107 req.SetExtraRequestHeaders(headers);
3108
3109 URLRequestRedirectJob* job =
3110 new URLRequestRedirectJob(&req, test_server_.GetURL("echo"));
3111 AddTestInterceptor()->set_main_intercept_job(job);
3112
3113 req.Start();
3114 MessageLoop::current()->Run();
3115 EXPECT_EQ("GET", req.method());
3116}
3117
3118TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) {
3119 ASSERT_TRUE(test_server_.Start());
3120
3121 const char kData[] = "hello world";
3122
3123 TestDelegate d;
3124 TestURLRequest req(test_server_.GetURL("empty.html"), &d);
3125 req.set_context(default_context_);
3126 req.set_method("POST");
3127 req.set_upload(CreateSimpleUploadData(kData).get());
3128 HttpRequestHeaders headers;
3129 headers.SetHeader(HttpRequestHeaders::kContentLength,
3130 base::UintToString(arraysize(kData) - 1));
3131 req.SetExtraRequestHeaders(headers);
3132
3133 URLRequestRedirectJob* job =
3134 new URLRequestRedirectJob(&req, test_server_.GetURL("echo"));
3135 job->set_redirect_code(
3136 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT);
3137 AddTestInterceptor()->set_main_intercept_job(job);
3138
3139 req.Start();
3140 MessageLoop::current()->Run();
3141 EXPECT_EQ("POST", req.method());
3142 EXPECT_EQ(kData, d.data_received());
3143}
3144
[email protected]a5c713f2009-04-16 21:05:473145// Custom URLRequestJobs for use with interceptor tests
[email protected]7461a402011-03-24 23:19:513146class RestartTestJob : public URLRequestTestJob {
[email protected]a5c713f2009-04-16 21:05:473147 public:
[email protected]7461a402011-03-24 23:19:513148 explicit RestartTestJob(URLRequest* request)
3149 : URLRequestTestJob(request, true) {}
[email protected]a5c713f2009-04-16 21:05:473150 protected:
3151 virtual void StartAsync() {
3152 this->NotifyRestartRequired();
3153 }
[email protected]5389bc72009-11-05 23:34:243154 private:
[email protected]13c8a092010-07-29 06:15:443155 ~RestartTestJob() {}
[email protected]a5c713f2009-04-16 21:05:473156};
3157
[email protected]7461a402011-03-24 23:19:513158class CancelTestJob : public URLRequestTestJob {
[email protected]a5c713f2009-04-16 21:05:473159 public:
[email protected]7461a402011-03-24 23:19:513160 explicit CancelTestJob(URLRequest* request)
3161 : URLRequestTestJob(request, true) {}
[email protected]a5c713f2009-04-16 21:05:473162 protected:
3163 virtual void StartAsync() {
3164 request_->Cancel();
3165 }
[email protected]5389bc72009-11-05 23:34:243166 private:
3167 ~CancelTestJob() {}
[email protected]a5c713f2009-04-16 21:05:473168};
3169
[email protected]7461a402011-03-24 23:19:513170class CancelThenRestartTestJob : public URLRequestTestJob {
[email protected]a5c713f2009-04-16 21:05:473171 public:
[email protected]7461a402011-03-24 23:19:513172 explicit CancelThenRestartTestJob(URLRequest* request)
3173 : URLRequestTestJob(request, true) {
[email protected]a5c713f2009-04-16 21:05:473174 }
3175 protected:
3176 virtual void StartAsync() {
3177 request_->Cancel();
3178 this->NotifyRestartRequired();
3179 }
[email protected]5389bc72009-11-05 23:34:243180 private:
3181 ~CancelThenRestartTestJob() {}
[email protected]a5c713f2009-04-16 21:05:473182};
3183
3184// An Interceptor for use with interceptor tests
[email protected]7461a402011-03-24 23:19:513185class TestInterceptor : URLRequest::Interceptor {
[email protected]a5c713f2009-04-16 21:05:473186 public:
3187 TestInterceptor()
3188 : intercept_main_request_(false), restart_main_request_(false),
3189 cancel_main_request_(false), cancel_then_restart_main_request_(false),
3190 simulate_main_network_error_(false),
3191 intercept_redirect_(false), cancel_redirect_request_(false),
3192 intercept_final_response_(false), cancel_final_request_(false),
3193 did_intercept_main_(false), did_restart_main_(false),
3194 did_cancel_main_(false), did_cancel_then_restart_main_(false),
3195 did_simulate_error_main_(false),
3196 did_intercept_redirect_(false), did_cancel_redirect_(false),
3197 did_intercept_final_(false), did_cancel_final_(false) {
[email protected]e3539402011-07-19 09:31:083198 URLRequest::Deprecated::RegisterRequestInterceptor(this);
[email protected]a5c713f2009-04-16 21:05:473199 }
3200
3201 ~TestInterceptor() {
[email protected]e3539402011-07-19 09:31:083202 URLRequest::Deprecated::UnregisterRequestInterceptor(this);
[email protected]a5c713f2009-04-16 21:05:473203 }
3204
[email protected]7461a402011-03-24 23:19:513205 virtual URLRequestJob* MaybeIntercept(URLRequest* request) {
[email protected]a5c713f2009-04-16 21:05:473206 if (restart_main_request_) {
3207 restart_main_request_ = false;
3208 did_restart_main_ = true;
3209 return new RestartTestJob(request);
3210 }
3211 if (cancel_main_request_) {
3212 cancel_main_request_ = false;
3213 did_cancel_main_ = true;
3214 return new CancelTestJob(request);
3215 }
3216 if (cancel_then_restart_main_request_) {
3217 cancel_then_restart_main_request_ = false;
3218 did_cancel_then_restart_main_ = true;
3219 return new CancelThenRestartTestJob(request);
3220 }
3221 if (simulate_main_network_error_) {
3222 simulate_main_network_error_ = false;
3223 did_simulate_error_main_ = true;
3224 // will error since the requeted url is not one of its canned urls
[email protected]7461a402011-03-24 23:19:513225 return new URLRequestTestJob(request, true);
[email protected]a5c713f2009-04-16 21:05:473226 }
3227 if (!intercept_main_request_)
3228 return NULL;
3229 intercept_main_request_ = false;
3230 did_intercept_main_ = true;
[email protected]7461a402011-03-24 23:19:513231 return new URLRequestTestJob(request,
[email protected]cbe04ef2011-01-11 00:13:243232 main_headers_,
3233 main_data_,
3234 true);
[email protected]a5c713f2009-04-16 21:05:473235 }
3236
[email protected]7461a402011-03-24 23:19:513237 virtual URLRequestJob* MaybeInterceptRedirect(URLRequest* request,
[email protected]9acd869e2010-12-11 10:18:593238 const GURL& location) {
[email protected]a5c713f2009-04-16 21:05:473239 if (cancel_redirect_request_) {
3240 cancel_redirect_request_ = false;
3241 did_cancel_redirect_ = true;
3242 return new CancelTestJob(request);
3243 }
3244 if (!intercept_redirect_)
3245 return NULL;
3246 intercept_redirect_ = false;
3247 did_intercept_redirect_ = true;
[email protected]7461a402011-03-24 23:19:513248 return new URLRequestTestJob(request,
[email protected]cbe04ef2011-01-11 00:13:243249 redirect_headers_,
3250 redirect_data_,
3251 true);
[email protected]a5c713f2009-04-16 21:05:473252 }
3253
[email protected]7461a402011-03-24 23:19:513254 virtual URLRequestJob* MaybeInterceptResponse(URLRequest* request) {
[email protected]a5c713f2009-04-16 21:05:473255 if (cancel_final_request_) {
3256 cancel_final_request_ = false;
3257 did_cancel_final_ = true;
3258 return new CancelTestJob(request);
3259 }
3260 if (!intercept_final_response_)
3261 return NULL;
3262 intercept_final_response_ = false;
3263 did_intercept_final_ = true;
[email protected]7461a402011-03-24 23:19:513264 return new URLRequestTestJob(request,
[email protected]cbe04ef2011-01-11 00:13:243265 final_headers_,
3266 final_data_,
3267 true);
[email protected]a5c713f2009-04-16 21:05:473268 }
3269
3270 // Whether to intercept the main request, and if so the response to return.
3271 bool intercept_main_request_;
3272 std::string main_headers_;
3273 std::string main_data_;
3274
3275 // Other actions we take at MaybeIntercept time
3276 bool restart_main_request_;
3277 bool cancel_main_request_;
3278 bool cancel_then_restart_main_request_;
3279 bool simulate_main_network_error_;
3280
3281 // Whether to intercept redirects, and if so the response to return.
3282 bool intercept_redirect_;
3283 std::string redirect_headers_;
3284 std::string redirect_data_;
3285
3286 // Other actions we can take at MaybeInterceptRedirect time
3287 bool cancel_redirect_request_;
3288
3289 // Whether to intercept final response, and if so the response to return.
3290 bool intercept_final_response_;
3291 std::string final_headers_;
3292 std::string final_data_;
3293
3294 // Other actions we can take at MaybeInterceptResponse time
3295 bool cancel_final_request_;
3296
3297 // If we did something or not
3298 bool did_intercept_main_;
3299 bool did_restart_main_;
3300 bool did_cancel_main_;
3301 bool did_cancel_then_restart_main_;
3302 bool did_simulate_error_main_;
3303 bool did_intercept_redirect_;
3304 bool did_cancel_redirect_;
3305 bool did_intercept_final_;
3306 bool did_cancel_final_;
3307
3308 // Static getters for canned response header and data strings
3309
3310 static std::string ok_data() {
[email protected]7461a402011-03-24 23:19:513311 return URLRequestTestJob::test_data_1();
[email protected]a5c713f2009-04-16 21:05:473312 }
3313
3314 static std::string ok_headers() {
[email protected]7461a402011-03-24 23:19:513315 return URLRequestTestJob::test_headers();
[email protected]a5c713f2009-04-16 21:05:473316 }
3317
3318 static std::string redirect_data() {
3319 return std::string();
3320 }
3321
3322 static std::string redirect_headers() {
[email protected]7461a402011-03-24 23:19:513323 return URLRequestTestJob::test_redirect_headers();
[email protected]a5c713f2009-04-16 21:05:473324 }
3325
3326 static std::string error_data() {
3327 return std::string("ohhh nooooo mr. bill!");
3328 }
3329
3330 static std::string error_headers() {
[email protected]7461a402011-03-24 23:19:513331 return URLRequestTestJob::test_error_headers();
[email protected]a5c713f2009-04-16 21:05:473332 }
3333};
3334
3335TEST_F(URLRequestTest, Intercept) {
3336 TestInterceptor interceptor;
3337
3338 // intercept the main request and respond with a simple response
3339 interceptor.intercept_main_request_ = true;
3340 interceptor.main_headers_ = TestInterceptor::ok_headers();
3341 interceptor.main_data_ = TestInterceptor::ok_data();
3342
3343 TestDelegate d;
3344 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
[email protected]87a09a92011-07-14 15:50:503345 req.set_context(default_context_);
[email protected]ea8e1812012-02-15 22:07:343346 base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data();
3347 base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data();
3348 base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data();
[email protected]a5c713f2009-04-16 21:05:473349 req.SetUserData(NULL, user_data0);
3350 req.SetUserData(&user_data1, user_data1);
3351 req.SetUserData(&user_data2, user_data2);
3352 req.set_method("GET");
3353 req.Start();
3354 MessageLoop::current()->Run();
3355
3356 // Make sure we can retrieve our specific user data
3357 EXPECT_EQ(user_data0, req.GetUserData(NULL));
3358 EXPECT_EQ(user_data1, req.GetUserData(&user_data1));
3359 EXPECT_EQ(user_data2, req.GetUserData(&user_data2));
3360
3361 // Check the interceptor got called as expected
3362 EXPECT_TRUE(interceptor.did_intercept_main_);
3363
3364 // Check we got one good response
3365 EXPECT_TRUE(req.status().is_success());
3366 EXPECT_EQ(200, req.response_headers()->response_code());
3367 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
3368 EXPECT_EQ(1, d.response_started_count());
3369 EXPECT_EQ(0, d.received_redirect_count());
3370}
3371
3372TEST_F(URLRequestTest, InterceptRedirect) {
3373 TestInterceptor interceptor;
3374
3375 // intercept the main request and respond with a redirect
3376 interceptor.intercept_main_request_ = true;
3377 interceptor.main_headers_ = TestInterceptor::redirect_headers();
3378 interceptor.main_data_ = TestInterceptor::redirect_data();
3379
3380 // intercept that redirect and respond a final OK response
3381 interceptor.intercept_redirect_ = true;
3382 interceptor.redirect_headers_ = TestInterceptor::ok_headers();
3383 interceptor.redirect_data_ = TestInterceptor::ok_data();
3384
3385 TestDelegate d;
3386 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
[email protected]87a09a92011-07-14 15:50:503387 req.set_context(default_context_);
[email protected]a5c713f2009-04-16 21:05:473388 req.set_method("GET");
3389 req.Start();
3390 MessageLoop::current()->Run();
3391
3392 // Check the interceptor got called as expected
3393 EXPECT_TRUE(interceptor.did_intercept_main_);
3394 EXPECT_TRUE(interceptor.did_intercept_redirect_);
3395
3396 // Check we got one good response
3397 EXPECT_TRUE(req.status().is_success());
[email protected]44637822009-08-27 17:01:113398 if (req.status().is_success()) {
3399 EXPECT_EQ(200, req.response_headers()->response_code());
3400 }
[email protected]a5c713f2009-04-16 21:05:473401 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
3402 EXPECT_EQ(1, d.response_started_count());
3403 EXPECT_EQ(0, d.received_redirect_count());
3404}
3405
3406TEST_F(URLRequestTest, InterceptServerError) {
3407 TestInterceptor interceptor;
3408
3409 // intercept the main request to generate a server error response
3410 interceptor.intercept_main_request_ = true;
3411 interceptor.main_headers_ = TestInterceptor::error_headers();
3412 interceptor.main_data_ = TestInterceptor::error_data();
3413
3414 // intercept that error and respond with an OK response
3415 interceptor.intercept_final_response_ = true;
3416 interceptor.final_headers_ = TestInterceptor::ok_headers();
3417 interceptor.final_data_ = TestInterceptor::ok_data();
3418
3419 TestDelegate d;
3420 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
[email protected]87a09a92011-07-14 15:50:503421 req.set_context(default_context_);
[email protected]a5c713f2009-04-16 21:05:473422 req.set_method("GET");
3423 req.Start();
3424 MessageLoop::current()->Run();
3425
3426 // Check the interceptor got called as expected
3427 EXPECT_TRUE(interceptor.did_intercept_main_);
3428 EXPECT_TRUE(interceptor.did_intercept_final_);
3429
3430 // Check we got one good response
3431 EXPECT_TRUE(req.status().is_success());
3432 EXPECT_EQ(200, req.response_headers()->response_code());
3433 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
3434 EXPECT_EQ(1, d.response_started_count());
3435 EXPECT_EQ(0, d.received_redirect_count());
3436}
3437
3438TEST_F(URLRequestTest, InterceptNetworkError) {
3439 TestInterceptor interceptor;
3440
3441 // intercept the main request to simulate a network error
3442 interceptor.simulate_main_network_error_ = true;
3443
3444 // intercept that error and respond with an OK response
3445 interceptor.intercept_final_response_ = true;
3446 interceptor.final_headers_ = TestInterceptor::ok_headers();
3447 interceptor.final_data_ = TestInterceptor::ok_data();
3448
3449 TestDelegate d;
3450 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
[email protected]87a09a92011-07-14 15:50:503451 req.set_context(default_context_);
[email protected]a5c713f2009-04-16 21:05:473452 req.set_method("GET");
3453 req.Start();
3454 MessageLoop::current()->Run();
3455
3456 // Check the interceptor got called as expected
3457 EXPECT_TRUE(interceptor.did_simulate_error_main_);
3458 EXPECT_TRUE(interceptor.did_intercept_final_);
3459
3460 // Check we received one good response
3461 EXPECT_TRUE(req.status().is_success());
3462 EXPECT_EQ(200, req.response_headers()->response_code());
3463 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
3464 EXPECT_EQ(1, d.response_started_count());
3465 EXPECT_EQ(0, d.received_redirect_count());
3466}
3467
3468TEST_F(URLRequestTest, InterceptRestartRequired) {
3469 TestInterceptor interceptor;
3470
3471 // restart the main request
3472 interceptor.restart_main_request_ = true;
3473
3474 // then intercept the new main request and respond with an OK response
3475 interceptor.intercept_main_request_ = true;
3476 interceptor.main_headers_ = TestInterceptor::ok_headers();
3477 interceptor.main_data_ = TestInterceptor::ok_data();
3478
3479 TestDelegate d;
3480 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
[email protected]87a09a92011-07-14 15:50:503481 req.set_context(default_context_);
[email protected]a5c713f2009-04-16 21:05:473482 req.set_method("GET");
3483 req.Start();
3484 MessageLoop::current()->Run();
3485
3486 // Check the interceptor got called as expected
3487 EXPECT_TRUE(interceptor.did_restart_main_);
3488 EXPECT_TRUE(interceptor.did_intercept_main_);
3489
3490 // Check we received one good response
3491 EXPECT_TRUE(req.status().is_success());
[email protected]44637822009-08-27 17:01:113492 if (req.status().is_success()) {
3493 EXPECT_EQ(200, req.response_headers()->response_code());
3494 }
[email protected]a5c713f2009-04-16 21:05:473495 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
3496 EXPECT_EQ(1, d.response_started_count());
3497 EXPECT_EQ(0, d.received_redirect_count());
3498}
3499
3500TEST_F(URLRequestTest, InterceptRespectsCancelMain) {
3501 TestInterceptor interceptor;
3502
3503 // intercept the main request and cancel from within the restarted job
3504 interceptor.cancel_main_request_ = true;
3505
3506 // setup to intercept final response and override it with an OK response
3507 interceptor.intercept_final_response_ = true;
3508 interceptor.final_headers_ = TestInterceptor::ok_headers();
3509 interceptor.final_data_ = TestInterceptor::ok_data();
3510
3511 TestDelegate d;
3512 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
[email protected]87a09a92011-07-14 15:50:503513 req.set_context(default_context_);
[email protected]a5c713f2009-04-16 21:05:473514 req.set_method("GET");
3515 req.Start();
3516 MessageLoop::current()->Run();
3517
3518 // Check the interceptor got called as expected
3519 EXPECT_TRUE(interceptor.did_cancel_main_);
3520 EXPECT_FALSE(interceptor.did_intercept_final_);
3521
3522 // Check we see a canceled request
3523 EXPECT_FALSE(req.status().is_success());
[email protected]7461a402011-03-24 23:19:513524 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
[email protected]a5c713f2009-04-16 21:05:473525}
3526
3527TEST_F(URLRequestTest, InterceptRespectsCancelRedirect) {
3528 TestInterceptor interceptor;
3529
3530 // intercept the main request and respond with a redirect
3531 interceptor.intercept_main_request_ = true;
3532 interceptor.main_headers_ = TestInterceptor::redirect_headers();
3533 interceptor.main_data_ = TestInterceptor::redirect_data();
3534
3535 // intercept the redirect and cancel from within that job
3536 interceptor.cancel_redirect_request_ = true;
3537
3538 // setup to intercept final response and override it with an OK response
3539 interceptor.intercept_final_response_ = true;
3540 interceptor.final_headers_ = TestInterceptor::ok_headers();
3541 interceptor.final_data_ = TestInterceptor::ok_data();
3542
3543 TestDelegate d;
3544 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
[email protected]87a09a92011-07-14 15:50:503545 req.set_context(default_context_);
[email protected]a5c713f2009-04-16 21:05:473546 req.set_method("GET");
3547 req.Start();
3548 MessageLoop::current()->Run();
3549
3550 // Check the interceptor got called as expected
3551 EXPECT_TRUE(interceptor.did_intercept_main_);
3552 EXPECT_TRUE(interceptor.did_cancel_redirect_);
3553 EXPECT_FALSE(interceptor.did_intercept_final_);
3554
3555 // Check we see a canceled request
3556 EXPECT_FALSE(req.status().is_success());
[email protected]7461a402011-03-24 23:19:513557 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
[email protected]a5c713f2009-04-16 21:05:473558}
3559
3560TEST_F(URLRequestTest, InterceptRespectsCancelFinal) {
3561 TestInterceptor interceptor;
3562
3563 // intercept the main request to simulate a network error
3564 interceptor.simulate_main_network_error_ = true;
3565
3566 // setup to intercept final response and cancel from within that job
3567 interceptor.cancel_final_request_ = true;
3568
3569 TestDelegate d;
3570 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
[email protected]87a09a92011-07-14 15:50:503571 req.set_context(default_context_);
[email protected]a5c713f2009-04-16 21:05:473572 req.set_method("GET");
3573 req.Start();
3574 MessageLoop::current()->Run();
3575
3576 // Check the interceptor got called as expected
3577 EXPECT_TRUE(interceptor.did_simulate_error_main_);
3578 EXPECT_TRUE(interceptor.did_cancel_final_);
3579
3580 // Check we see a canceled request
3581 EXPECT_FALSE(req.status().is_success());
[email protected]7461a402011-03-24 23:19:513582 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
[email protected]a5c713f2009-04-16 21:05:473583}
3584
3585TEST_F(URLRequestTest, InterceptRespectsCancelInRestart) {
3586 TestInterceptor interceptor;
3587
3588 // intercept the main request and cancel then restart from within that job
3589 interceptor.cancel_then_restart_main_request_ = true;
3590
3591 // setup to intercept final response and override it with an OK response
3592 interceptor.intercept_final_response_ = true;
3593 interceptor.final_headers_ = TestInterceptor::ok_headers();
3594 interceptor.final_data_ = TestInterceptor::ok_data();
3595
3596 TestDelegate d;
3597 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
[email protected]87a09a92011-07-14 15:50:503598 req.set_context(default_context_);
[email protected]a5c713f2009-04-16 21:05:473599 req.set_method("GET");
3600 req.Start();
3601 MessageLoop::current()->Run();
3602
3603 // Check the interceptor got called as expected
3604 EXPECT_TRUE(interceptor.did_cancel_then_restart_main_);
3605 EXPECT_FALSE(interceptor.did_intercept_final_);
3606
3607 // Check we see a canceled request
3608 EXPECT_FALSE(req.status().is_success());
[email protected]7461a402011-03-24 23:19:513609 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
[email protected]a5c713f2009-04-16 21:05:473610}
3611
[email protected]f4f2f512011-02-17 09:28:453612// Check that two different URL requests have different identifiers.
3613TEST_F(URLRequestTest, Identifiers) {
3614 TestDelegate d;
3615 TestURLRequest req(GURL("http://example.com"), &d);
3616 TestURLRequest other_req(GURL("http://example.com"), &d);
3617
3618 ASSERT_NE(req.identifier(), other_req.identifier());
3619}
3620
[email protected]8202d0c2011-02-23 08:31:143621// Check that a failure to connect to the proxy is reported to the network
3622// delegate.
3623TEST_F(URLRequestTest, NetworkDelegateProxyError) {
[email protected]e74aa632011-07-14 17:10:163624 MockHostResolver host_resolver;
3625 host_resolver.rules()->AddSimulatedFailure("*");
[email protected]87a09a92011-07-14 15:50:503626
3627 TestNetworkDelegate network_delegate; // must outlive URLRequests
3628 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
[email protected]8202d0c2011-02-23 08:31:143629 context->set_network_delegate(&network_delegate);
[email protected]87a09a92011-07-14 15:50:503630 context->SetProxyFromString("myproxy:70");
[email protected]e74aa632011-07-14 17:10:163631 context->set_host_resolver(&host_resolver);
[email protected]87a09a92011-07-14 15:50:503632 context->Init();
3633
3634 TestDelegate d;
3635 TestURLRequest req(GURL("http://example.com"), &d);
[email protected]8202d0c2011-02-23 08:31:143636 req.set_context(context);
[email protected]87a09a92011-07-14 15:50:503637 req.set_method("GET");
[email protected]8202d0c2011-02-23 08:31:143638
3639 req.Start();
3640 MessageLoop::current()->Run();
3641
3642 // Check we see a failed request.
3643 EXPECT_FALSE(req.status().is_success());
[email protected]7461a402011-03-24 23:19:513644 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
[email protected]d0cc35b2011-09-08 12:02:053645 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, req.status().error());
[email protected]8202d0c2011-02-23 08:31:143646
3647 EXPECT_EQ(1, network_delegate.error_count());
[email protected]d0cc35b2011-09-08 12:02:053648 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, network_delegate.last_error());
[email protected]a83dd332011-07-13 10:41:013649 EXPECT_EQ(1, network_delegate.completed_requests());
[email protected]8202d0c2011-02-23 08:31:143650}
3651
[email protected]c10450102011-06-27 09:06:163652// Check that it is impossible to change the referrer in the extra headers of
3653// an URLRequest.
3654TEST_F(URLRequestTest, DoNotOverrideReferrer) {
[email protected]1700c6a2012-02-22 18:07:073655 LocalHttpTestServer test_server;
[email protected]c10450102011-06-27 09:06:163656 ASSERT_TRUE(test_server.Start());
3657
[email protected]c10450102011-06-27 09:06:163658 // If extra headers contain referer and the request contains a referer,
3659 // only the latter shall be respected.
3660 {
3661 TestDelegate d;
3662 TestURLRequest req(test_server.GetURL("echoheader?Referer"), &d);
3663 req.set_referrer("http://foo.com/");
[email protected]87a09a92011-07-14 15:50:503664 req.set_context(default_context_);
[email protected]c10450102011-06-27 09:06:163665
3666 HttpRequestHeaders headers;
3667 headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/");
3668 req.SetExtraRequestHeaders(headers);
3669
3670 req.Start();
3671 MessageLoop::current()->Run();
3672
3673 EXPECT_EQ("http://foo.com/", d.data_received());
3674 }
3675
3676 // If extra headers contain a referer but the request does not, no referer
3677 // shall be sent in the header.
3678 {
3679 TestDelegate d;
3680 TestURLRequest req(test_server.GetURL("echoheader?Referer"), &d);
[email protected]87a09a92011-07-14 15:50:503681 req.set_context(default_context_);
[email protected]c10450102011-06-27 09:06:163682
3683 HttpRequestHeaders headers;
3684 headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/");
3685 req.SetExtraRequestHeaders(headers);
[email protected]52c287592011-06-28 17:39:103686 req.set_load_flags(LOAD_VALIDATE_CACHE);
[email protected]c10450102011-06-27 09:06:163687
3688 req.Start();
3689 MessageLoop::current()->Run();
3690
3691 EXPECT_EQ("None", d.data_received());
3692 }
3693}
3694
[email protected]a83dd332011-07-13 10:41:013695// Make sure that net::NetworkDelegate::NotifyCompleted is called if
3696// content is empty.
3697TEST_F(URLRequestTest, RequestCompletionForEmptyResponse) {
[email protected]a83dd332011-07-13 10:41:013698 TestDelegate d;
3699 TestURLRequest req(GURL("data:,"), &d);
[email protected]87a09a92011-07-14 15:50:503700 req.set_context(new TestURLRequestContext());
3701 req.set_context(default_context_);
[email protected]a83dd332011-07-13 10:41:013702 req.Start();
3703 MessageLoop::current()->Run();
3704 EXPECT_EQ("", d.data_received());
[email protected]87a09a92011-07-14 15:50:503705 EXPECT_EQ(1, default_network_delegate_.completed_requests());
[email protected]a83dd332011-07-13 10:41:013706}
3707
[email protected]b89ca032009-08-31 21:41:313708class URLRequestTestFTP : public URLRequestTest {
[email protected]95409e12010-08-17 20:07:113709 public:
[email protected]d9fca99a2012-02-24 16:16:203710 URLRequestTestFTP()
3711 : test_server_(TestServer::TYPE_FTP, TestServer::kLocalhost, FilePath()) {
[email protected]95409e12010-08-17 20:07:113712 }
3713
[email protected]b89ca032009-08-31 21:41:313714 protected:
[email protected]7461a402011-03-24 23:19:513715 TestServer test_server_;
[email protected]b89ca032009-08-31 21:41:313716};
3717
[email protected]5accf7332009-11-24 03:41:383718// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:123719TEST_F(URLRequestTestFTP, DISABLED_FTPDirectoryListing) {
[email protected]95409e12010-08-17 20:07:113720 ASSERT_TRUE(test_server_.Start());
3721
[email protected]a25e90e2009-09-09 17:05:373722 TestDelegate d;
3723 {
[email protected]95409e12010-08-17 20:07:113724 TestURLRequest r(test_server_.GetURL("/"), &d);
[email protected]87a09a92011-07-14 15:50:503725 r.set_context(default_context_);
[email protected]a25e90e2009-09-09 17:05:373726 r.Start();
3727 EXPECT_TRUE(r.is_pending());
3728
3729 MessageLoop::current()->Run();
3730
3731 EXPECT_FALSE(r.is_pending());
3732 EXPECT_EQ(1, d.response_started_count());
3733 EXPECT_FALSE(d.received_data_before_response());
3734 EXPECT_LT(0, d.bytes_received());
[email protected]6d81b482011-02-22 19:47:193735 EXPECT_EQ(test_server_.host_port_pair().host(),
3736 r.GetSocketAddress().host());
3737 EXPECT_EQ(test_server_.host_port_pair().port(),
3738 r.GetSocketAddress().port());
[email protected]a25e90e2009-09-09 17:05:373739 }
3740}
3741
[email protected]7df70012010-02-04 00:09:553742// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:123743TEST_F(URLRequestTestFTP, DISABLED_FTPGetTestAnonymous) {
[email protected]95409e12010-08-17 20:07:113744 ASSERT_TRUE(test_server_.Start());
3745
[email protected]399b8702009-05-01 20:34:023746 FilePath app_path;
[email protected]dd265012009-01-08 20:45:273747 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:023748 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:273749 TestDelegate d;
3750 {
[email protected]95409e12010-08-17 20:07:113751 TestURLRequest r(test_server_.GetURL("/LICENSE"), &d);
[email protected]87a09a92011-07-14 15:50:503752 r.set_context(default_context_);
[email protected]dd265012009-01-08 20:45:273753 r.Start();
3754 EXPECT_TRUE(r.is_pending());
3755
3756 MessageLoop::current()->Run();
3757
3758 int64 file_size = 0;
3759 file_util::GetFileSize(app_path, &file_size);
3760
[email protected]ba2f3342009-07-30 18:08:423761 EXPECT_FALSE(r.is_pending());
[email protected]dd265012009-01-08 20:45:273762 EXPECT_EQ(1, d.response_started_count());
3763 EXPECT_FALSE(d.received_data_before_response());
3764 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
[email protected]6d81b482011-02-22 19:47:193765 EXPECT_EQ(test_server_.host_port_pair().host(),
3766 r.GetSocketAddress().host());
3767 EXPECT_EQ(test_server_.host_port_pair().port(),
3768 r.GetSocketAddress().port());
[email protected]dd265012009-01-08 20:45:273769 }
3770}
3771
[email protected]e9ecbd12009-12-20 18:44:403772// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:123773TEST_F(URLRequestTestFTP, DISABLED_FTPGetTest) {
[email protected]95409e12010-08-17 20:07:113774 ASSERT_TRUE(test_server_.Start());
3775
[email protected]399b8702009-05-01 20:34:023776 FilePath app_path;
[email protected]dd265012009-01-08 20:45:273777 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:023778 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:273779 TestDelegate d;
3780 {
[email protected]95409e12010-08-17 20:07:113781 TestURLRequest r(
3782 test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", "chrome"),
3783 &d);
[email protected]87a09a92011-07-14 15:50:503784 r.set_context(default_context_);
[email protected]dd265012009-01-08 20:45:273785 r.Start();
3786 EXPECT_TRUE(r.is_pending());
3787
3788 MessageLoop::current()->Run();
3789
3790 int64 file_size = 0;
3791 file_util::GetFileSize(app_path, &file_size);
3792
[email protected]ba2f3342009-07-30 18:08:423793 EXPECT_FALSE(r.is_pending());
[email protected]6d81b482011-02-22 19:47:193794 EXPECT_EQ(test_server_.host_port_pair().host(),
3795 r.GetSocketAddress().host());
3796 EXPECT_EQ(test_server_.host_port_pair().port(),
3797 r.GetSocketAddress().port());
[email protected]dd265012009-01-08 20:45:273798 EXPECT_EQ(1, d.response_started_count());
3799 EXPECT_FALSE(d.received_data_before_response());
3800 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
3801 }
3802}
3803
[email protected]49abd652010-08-05 05:04:533804// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:123805TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPassword) {
[email protected]95409e12010-08-17 20:07:113806 ASSERT_TRUE(test_server_.Start());
3807
[email protected]399b8702009-05-01 20:34:023808 FilePath app_path;
[email protected]dd265012009-01-08 20:45:273809 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:023810 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:273811 TestDelegate d;
3812 {
[email protected]95409e12010-08-17 20:07:113813 TestURLRequest r(
3814 test_server_.GetURLWithUserAndPassword("/LICENSE",
3815 "chrome",
3816 "wrong_password"),
3817 &d);
[email protected]87a09a92011-07-14 15:50:503818 r.set_context(default_context_);
[email protected]dd265012009-01-08 20:45:273819 r.Start();
3820 EXPECT_TRUE(r.is_pending());
3821
3822 MessageLoop::current()->Run();
3823
3824 int64 file_size = 0;
3825 file_util::GetFileSize(app_path, &file_size);
3826
[email protected]ba2f3342009-07-30 18:08:423827 EXPECT_FALSE(r.is_pending());
[email protected]dd265012009-01-08 20:45:273828 EXPECT_EQ(1, d.response_started_count());
3829 EXPECT_FALSE(d.received_data_before_response());
3830 EXPECT_EQ(d.bytes_received(), 0);
3831 }
3832}
3833
[email protected]cde4e80d2009-10-16 19:58:153834// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:123835TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPasswordRestart) {
[email protected]95409e12010-08-17 20:07:113836 ASSERT_TRUE(test_server_.Start());
3837
[email protected]8b8a197d2009-08-26 15:57:583838 FilePath app_path;
3839 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
3840 app_path = app_path.AppendASCII("LICENSE");
3841 TestDelegate d;
3842 // Set correct login credentials. The delegate will be asked for them when
3843 // the initial login with wrong credentials will fail.
[email protected]f3cf9802011-10-28 18:44:583844 d.set_credentials(AuthCredentials(kChrome, kChrome));
[email protected]8b8a197d2009-08-26 15:57:583845 {
[email protected]95409e12010-08-17 20:07:113846 TestURLRequest r(
3847 test_server_.GetURLWithUserAndPassword("/LICENSE",
3848 "chrome",
3849 "wrong_password"),
3850 &d);
[email protected]87a09a92011-07-14 15:50:503851 r.set_context(default_context_);
[email protected]8b8a197d2009-08-26 15:57:583852 r.Start();
3853 EXPECT_TRUE(r.is_pending());
3854
3855 MessageLoop::current()->Run();
3856
3857 int64 file_size = 0;
3858 file_util::GetFileSize(app_path, &file_size);
3859
3860 EXPECT_FALSE(r.is_pending());
3861 EXPECT_EQ(1, d.response_started_count());
3862 EXPECT_FALSE(d.received_data_before_response());
3863 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
3864 }
3865}
3866
[email protected]49abd652010-08-05 05:04:533867// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:123868TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUser) {
[email protected]95409e12010-08-17 20:07:113869 ASSERT_TRUE(test_server_.Start());
3870
[email protected]399b8702009-05-01 20:34:023871 FilePath app_path;
[email protected]dd265012009-01-08 20:45:273872 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:023873 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:273874 TestDelegate d;
3875 {
[email protected]95409e12010-08-17 20:07:113876 TestURLRequest r(
3877 test_server_.GetURLWithUserAndPassword("/LICENSE",
3878 "wrong_user",
3879 "chrome"),
3880 &d);
[email protected]87a09a92011-07-14 15:50:503881 r.set_context(default_context_);
[email protected]dd265012009-01-08 20:45:273882 r.Start();
3883 EXPECT_TRUE(r.is_pending());
3884
3885 MessageLoop::current()->Run();
3886
3887 int64 file_size = 0;
3888 file_util::GetFileSize(app_path, &file_size);
3889
[email protected]ba2f3342009-07-30 18:08:423890 EXPECT_FALSE(r.is_pending());
[email protected]dd265012009-01-08 20:45:273891 EXPECT_EQ(1, d.response_started_count());
3892 EXPECT_FALSE(d.received_data_before_response());
3893 EXPECT_EQ(d.bytes_received(), 0);
3894 }
3895}
[email protected]8b8a197d2009-08-26 15:57:583896
[email protected]cde4e80d2009-10-16 19:58:153897// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:123898TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUserRestart) {
[email protected]95409e12010-08-17 20:07:113899 ASSERT_TRUE(test_server_.Start());
3900
[email protected]8b8a197d2009-08-26 15:57:583901 FilePath app_path;
3902 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
3903 app_path = app_path.AppendASCII("LICENSE");
3904 TestDelegate d;
3905 // Set correct login credentials. The delegate will be asked for them when
3906 // the initial login with wrong credentials will fail.
[email protected]f3cf9802011-10-28 18:44:583907 d.set_credentials(AuthCredentials(kChrome, kChrome));
[email protected]8b8a197d2009-08-26 15:57:583908 {
[email protected]95409e12010-08-17 20:07:113909 TestURLRequest r(
3910 test_server_.GetURLWithUserAndPassword("/LICENSE",
3911 "wrong_user",
3912 "chrome"),
3913 &d);
[email protected]87a09a92011-07-14 15:50:503914 r.set_context(default_context_);
[email protected]8b8a197d2009-08-26 15:57:583915 r.Start();
3916 EXPECT_TRUE(r.is_pending());
3917
3918 MessageLoop::current()->Run();
3919
3920 int64 file_size = 0;
3921 file_util::GetFileSize(app_path, &file_size);
3922
3923 EXPECT_FALSE(r.is_pending());
3924 EXPECT_EQ(1, d.response_started_count());
3925 EXPECT_FALSE(d.received_data_before_response());
3926 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
3927 }
3928}
[email protected]60a3df52009-09-22 16:13:243929
[email protected]cde4e80d2009-10-16 19:58:153930// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:123931TEST_F(URLRequestTestFTP, DISABLED_FTPCacheURLCredentials) {
[email protected]95409e12010-08-17 20:07:113932 ASSERT_TRUE(test_server_.Start());
3933
[email protected]60a3df52009-09-22 16:13:243934 FilePath app_path;
3935 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
3936 app_path = app_path.AppendASCII("LICENSE");
3937
3938 scoped_ptr<TestDelegate> d(new TestDelegate);
3939 {
3940 // Pass correct login identity in the URL.
[email protected]95409e12010-08-17 20:07:113941 TestURLRequest r(
3942 test_server_.GetURLWithUserAndPassword("/LICENSE",
3943 "chrome",
3944 "chrome"),
3945 d.get());
[email protected]87a09a92011-07-14 15:50:503946 r.set_context(default_context_);
[email protected]60a3df52009-09-22 16:13:243947 r.Start();
3948 EXPECT_TRUE(r.is_pending());
3949
3950 MessageLoop::current()->Run();
3951
3952 int64 file_size = 0;
3953 file_util::GetFileSize(app_path, &file_size);
3954
3955 EXPECT_FALSE(r.is_pending());
3956 EXPECT_EQ(1, d->response_started_count());
3957 EXPECT_FALSE(d->received_data_before_response());
3958 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
3959 }
3960
3961 d.reset(new TestDelegate);
3962 {
3963 // This request should use cached identity from previous request.
[email protected]95409e12010-08-17 20:07:113964 TestURLRequest r(test_server_.GetURL("/LICENSE"), d.get());
[email protected]87a09a92011-07-14 15:50:503965 r.set_context(default_context_);
[email protected]60a3df52009-09-22 16:13:243966 r.Start();
3967 EXPECT_TRUE(r.is_pending());
3968
3969 MessageLoop::current()->Run();
3970
3971 int64 file_size = 0;
3972 file_util::GetFileSize(app_path, &file_size);
3973
3974 EXPECT_FALSE(r.is_pending());
3975 EXPECT_EQ(1, d->response_started_count());
3976 EXPECT_FALSE(d->received_data_before_response());
3977 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
3978 }
3979}
3980
[email protected]cde4e80d2009-10-16 19:58:153981// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:123982TEST_F(URLRequestTestFTP, DISABLED_FTPCacheLoginBoxCredentials) {
[email protected]95409e12010-08-17 20:07:113983 ASSERT_TRUE(test_server_.Start());
3984
[email protected]60a3df52009-09-22 16:13:243985 FilePath app_path;
3986 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
3987 app_path = app_path.AppendASCII("LICENSE");
3988
3989 scoped_ptr<TestDelegate> d(new TestDelegate);
3990 // Set correct login credentials. The delegate will be asked for them when
3991 // the initial login with wrong credentials will fail.
[email protected]f3cf9802011-10-28 18:44:583992 d->set_credentials(AuthCredentials(kChrome, kChrome));
[email protected]60a3df52009-09-22 16:13:243993 {
[email protected]95409e12010-08-17 20:07:113994 TestURLRequest r(
3995 test_server_.GetURLWithUserAndPassword("/LICENSE",
3996 "chrome",
3997 "wrong_password"),
3998 d.get());
[email protected]87a09a92011-07-14 15:50:503999 r.set_context(default_context_);
[email protected]60a3df52009-09-22 16:13:244000 r.Start();
4001 EXPECT_TRUE(r.is_pending());
4002
4003 MessageLoop::current()->Run();
4004
4005 int64 file_size = 0;
4006 file_util::GetFileSize(app_path, &file_size);
4007
4008 EXPECT_FALSE(r.is_pending());
4009 EXPECT_EQ(1, d->response_started_count());
4010 EXPECT_FALSE(d->received_data_before_response());
4011 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
4012 }
4013
4014 // Use a new delegate without explicit credentials. The cached ones should be
4015 // used.
4016 d.reset(new TestDelegate);
4017 {
4018 // Don't pass wrong credentials in the URL, they would override valid cached
4019 // ones.
[email protected]95409e12010-08-17 20:07:114020 TestURLRequest r(test_server_.GetURL("/LICENSE"), d.get());
[email protected]87a09a92011-07-14 15:50:504021 r.set_context(default_context_);
[email protected]60a3df52009-09-22 16:13:244022 r.Start();
4023 EXPECT_TRUE(r.is_pending());
4024
4025 MessageLoop::current()->Run();
4026
4027 int64 file_size = 0;
4028 file_util::GetFileSize(app_path, &file_size);
4029
4030 EXPECT_FALSE(r.is_pending());
4031 EXPECT_EQ(1, d->response_started_count());
4032 EXPECT_FALSE(d->received_data_before_response());
4033 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
4034 }
4035}
[email protected]09a362d32009-09-24 18:01:334036
4037// Check that default A-L header is sent.
4038TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) {
[email protected]95409e12010-08-17 20:07:114039 ASSERT_TRUE(test_server_.Start());
4040
[email protected]87a09a92011-07-14 15:50:504041 TestNetworkDelegate network_delegate; // must outlive URLRequests
4042 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
4043 context->set_network_delegate(&network_delegate);
4044 context->set_accept_language("en");
4045 context->Init();
4046
[email protected]09a362d32009-09-24 18:01:334047 TestDelegate d;
[email protected]95409e12010-08-17 20:07:114048 TestURLRequest req(test_server_.GetURL("echoheader?Accept-Language"), &d);
[email protected]6dae6b82011-03-30 00:35:344049 req.set_context(context);
[email protected]09a362d32009-09-24 18:01:334050 req.Start();
4051 MessageLoop::current()->Run();
[email protected]6dae6b82011-03-30 00:35:344052 EXPECT_EQ("en", d.data_received());
4053}
4054
4055// Check that an empty A-L header is not sent. http://crbug.com/77365.
4056TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) {
4057 ASSERT_TRUE(test_server_.Start());
4058
[email protected]87a09a92011-07-14 15:50:504059 TestNetworkDelegate network_delegate; // must outlive URLRequests
4060 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
4061 context->set_network_delegate(&network_delegate);
4062 context->Init();
4063 // We override the language after initialization because empty entries
4064 // get overridden by Init().
[email protected]6dae6b82011-03-30 00:35:344065 context->set_accept_language("");
[email protected]87a09a92011-07-14 15:50:504066
4067 TestDelegate d;
4068 TestURLRequest req(test_server_.GetURL("echoheader?Accept-Language"), &d);
[email protected]6dae6b82011-03-30 00:35:344069 req.set_context(context);
4070 req.Start();
4071 MessageLoop::current()->Run();
4072 EXPECT_EQ("None", d.data_received());
[email protected]09a362d32009-09-24 18:01:334073}
4074
4075// Check that if request overrides the A-L header, the default is not appended.
4076// See http://crbug.com/20894
4077TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) {
[email protected]95409e12010-08-17 20:07:114078 ASSERT_TRUE(test_server_.Start());
4079
[email protected]09a362d32009-09-24 18:01:334080 TestDelegate d;
[email protected]87a09a92011-07-14 15:50:504081 TestURLRequest req(test_server_.GetURL("echoheader?Accept-Language"), &d);
4082 req.set_context(default_context_);
[email protected]7461a402011-03-24 23:19:514083 HttpRequestHeaders headers;
4084 headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru");
[email protected]88e6b6f32010-05-07 23:14:254085 req.SetExtraRequestHeaders(headers);
[email protected]09a362d32009-09-24 18:01:334086 req.Start();
4087 MessageLoop::current()->Run();
4088 EXPECT_EQ(std::string("ru"), d.data_received());
4089}
4090
[email protected]c7bef94c2011-06-21 18:05:514091// Check that default A-E header is sent.
4092TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) {
4093 ASSERT_TRUE(test_server_.Start());
4094
4095 TestDelegate d;
[email protected]87a09a92011-07-14 15:50:504096 TestURLRequest req(test_server_.GetURL("echoheader?Accept-Encoding"), &d);
4097 req.set_context(default_context_);
[email protected]c7bef94c2011-06-21 18:05:514098 HttpRequestHeaders headers;
4099 req.SetExtraRequestHeaders(headers);
4100 req.Start();
4101 MessageLoop::current()->Run();
4102 EXPECT_TRUE(ContainsString(d.data_received(), "gzip"));
4103}
4104
4105// Check that if request overrides the A-E header, the default is not appended.
4106// See http://crbug.com/47381
4107TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) {
4108 ASSERT_TRUE(test_server_.Start());
4109
4110 TestDelegate d;
[email protected]87a09a92011-07-14 15:50:504111 TestURLRequest req(test_server_.GetURL("echoheader?Accept-Encoding"), &d);
4112 req.set_context(default_context_);
[email protected]c7bef94c2011-06-21 18:05:514113 HttpRequestHeaders headers;
4114 headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, "identity");
4115 req.SetExtraRequestHeaders(headers);
4116 req.Start();
4117 MessageLoop::current()->Run();
4118 EXPECT_FALSE(ContainsString(d.data_received(), "gzip"));
4119 EXPECT_TRUE(ContainsString(d.data_received(), "identity"));
4120}
4121
[email protected]09a362d32009-09-24 18:01:334122// Check that default A-C header is sent.
4123TEST_F(URLRequestTestHTTP, DefaultAcceptCharset) {
[email protected]95409e12010-08-17 20:07:114124 ASSERT_TRUE(test_server_.Start());
4125
[email protected]87a09a92011-07-14 15:50:504126 TestNetworkDelegate network_delegate; // must outlive URLRequests
4127 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
4128 context->set_network_delegate(&network_delegate);
4129 context->set_accept_charset("en");
4130 context->Init();
4131
[email protected]09a362d32009-09-24 18:01:334132 TestDelegate d;
[email protected]95409e12010-08-17 20:07:114133 TestURLRequest req(test_server_.GetURL("echoheader?Accept-Charset"), &d);
[email protected]6dae6b82011-03-30 00:35:344134 req.set_context(context);
[email protected]09a362d32009-09-24 18:01:334135 req.Start();
4136 MessageLoop::current()->Run();
[email protected]6dae6b82011-03-30 00:35:344137 EXPECT_EQ("en", d.data_received());
4138}
4139
4140// Check that an empty A-C header is not sent. http://crbug.com/77365.
4141TEST_F(URLRequestTestHTTP, EmptyAcceptCharset) {
4142 ASSERT_TRUE(test_server_.Start());
4143
[email protected]87a09a92011-07-14 15:50:504144 TestNetworkDelegate network_delegate; // must outlive URLRequests
4145 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
4146 context->set_network_delegate(&network_delegate);
4147 context->Init();
4148 // We override the accepted charset after initialization because empty
4149 // entries get overridden otherwise.
[email protected]6dae6b82011-03-30 00:35:344150 context->set_accept_charset("");
[email protected]87a09a92011-07-14 15:50:504151
4152 TestDelegate d;
4153 TestURLRequest req(test_server_.GetURL("echoheader?Accept-Charset"), &d);
[email protected]6dae6b82011-03-30 00:35:344154 req.set_context(context);
4155 req.Start();
4156 MessageLoop::current()->Run();
4157 EXPECT_EQ("None", d.data_received());
[email protected]09a362d32009-09-24 18:01:334158}
4159
4160// Check that if request overrides the A-C header, the default is not appended.
4161// See http://crbug.com/20894
4162TEST_F(URLRequestTestHTTP, OverrideAcceptCharset) {
[email protected]95409e12010-08-17 20:07:114163 ASSERT_TRUE(test_server_.Start());
4164
[email protected]09a362d32009-09-24 18:01:334165 TestDelegate d;
[email protected]87a09a92011-07-14 15:50:504166 TestURLRequest req(test_server_.GetURL("echoheader?Accept-Charset"), &d);
4167 req.set_context(default_context_);
[email protected]7461a402011-03-24 23:19:514168 HttpRequestHeaders headers;
4169 headers.SetHeader(HttpRequestHeaders::kAcceptCharset, "koi-8r");
[email protected]88e6b6f32010-05-07 23:14:254170 req.SetExtraRequestHeaders(headers);
[email protected]09a362d32009-09-24 18:01:334171 req.Start();
4172 MessageLoop::current()->Run();
4173 EXPECT_EQ(std::string("koi-8r"), d.data_received());
4174}
[email protected]50ba404b2011-01-21 13:38:454175
4176// Check that default User-Agent header is sent.
4177TEST_F(URLRequestTestHTTP, DefaultUserAgent) {
4178 ASSERT_TRUE(test_server_.Start());
4179
4180 TestDelegate d;
4181 TestURLRequest req(test_server_.GetURL("echoheader?User-Agent"), &d);
[email protected]87a09a92011-07-14 15:50:504182 req.set_context(default_context_);
[email protected]50ba404b2011-01-21 13:38:454183 req.Start();
4184 MessageLoop::current()->Run();
4185 EXPECT_EQ(req.context()->GetUserAgent(req.url()), d.data_received());
4186}
4187
4188// Check that if request overrides the User-Agent header,
4189// the default is not appended.
4190TEST_F(URLRequestTestHTTP, OverrideUserAgent) {
4191 ASSERT_TRUE(test_server_.Start());
4192
4193 TestDelegate d;
[email protected]87a09a92011-07-14 15:50:504194 TestURLRequest req(test_server_.GetURL("echoheader?User-Agent"), &d);
4195 req.set_context(default_context_);
[email protected]7461a402011-03-24 23:19:514196 HttpRequestHeaders headers;
4197 headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)");
[email protected]50ba404b2011-01-21 13:38:454198 req.SetExtraRequestHeaders(headers);
4199 req.Start();
4200 MessageLoop::current()->Run();
[email protected]ebbad602011-01-21 23:30:474201 // If the net tests are being run with ChromeFrame then we need to allow for
[email protected]0464e16a2011-02-04 22:56:574202 // the 'chromeframe' suffix which is added to the user agent before the
4203 // closing parentheses.
4204 EXPECT_TRUE(StartsWithASCII(d.data_received(), "Lynx (textmode", true));
[email protected]50ba404b2011-01-21 13:38:454205}
[email protected]7461a402011-03-24 23:19:514206
4207} // namespace net