blob: caeaf71cbc6df57b4297df21e05f15b971668819 [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)
initial.commit586acc5fe2008-07-26 22:42:528#include <shlobj.h>
[email protected]d8eb84242010-09-25 02:25:069#include <windows.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"
[email protected]0757e7702009-03-27 04:00:2230#include "net/base/cookie_monster.h"
[email protected]54f4c9362011-07-25 21:54:4631#include "net/base/cookie_store_test_helpers.h"
initial.commit586acc5fe2008-07-26 22:42:5232#include "net/base/load_flags.h"
[email protected]8202d0c2011-02-23 08:31:1433#include "net/base/mock_host_resolver.h"
[email protected]d8eb84242010-09-25 02:25:0634#include "net/base/net_errors.h"
[email protected]9e743cd2010-03-16 07:03:5335#include "net/base/net_log.h"
36#include "net/base/net_log_unittest.h"
initial.commit586acc5fe2008-07-26 22:42:5237#include "net/base/net_module.h"
38#include "net/base/net_util.h"
[email protected]96adadb2010-08-28 01:16:1739#include "net/base/ssl_connection_status_flags.h"
[email protected]195e77d2009-07-23 19:10:2340#include "net/base/upload_data.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 {
2572 TestDelegate d;
2573 URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"), &d);
2574 req.set_context(context);
2575 req.Start();
2576 MessageLoop::current()->Run();
2577 EXPECT_EQ(0, d.blocked_get_cookies_count());
2578 EXPECT_EQ(0, d.blocked_set_cookie_count());
2579 EXPECT_EQ(1, d.set_cookie_count());
2580 }
2581
2582 // Verify that the cookie is set.
2583 {
2584 TestDelegate d;
2585 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
2586 req.set_context(context);
2587 req.Start();
2588 MessageLoop::current()->Run();
2589
2590 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2591 != std::string::npos);
2592 EXPECT_EQ(0, d.blocked_get_cookies_count());
2593 EXPECT_EQ(0, d.blocked_set_cookie_count());
2594 }
2595}
2596
[email protected]be6fca6c2010-01-30 21:48:572597TEST_F(URLRequestTest, DoNotSendCookies) {
[email protected]1700c6a2012-02-22 18:07:072598 LocalHttpTestServer test_server;
[email protected]95409e12010-08-17 20:07:112599 ASSERT_TRUE(test_server.Start());
2600
[email protected]861fcd52009-08-26 02:33:462601 // Set up a cookie.
2602 {
2603 TestDelegate d;
[email protected]7461a402011-03-24 23:19:512604 URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"), &d);
[email protected]87a09a92011-07-14 15:50:502605 req.set_context(default_context_);
[email protected]861fcd52009-08-26 02:33:462606 req.Start();
2607 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:202608 EXPECT_EQ(0, d.blocked_get_cookies_count());
2609 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]861fcd52009-08-26 02:33:462610 }
2611
2612 // Verify that the cookie is set.
2613 {
2614 TestDelegate d;
[email protected]95409e12010-08-17 20:07:112615 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]87a09a92011-07-14 15:50:502616 req.set_context(default_context_);
[email protected]861fcd52009-08-26 02:33:462617 req.Start();
2618 MessageLoop::current()->Run();
2619
2620 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2621 != std::string::npos);
[email protected]3dbb80b2010-02-09 22:41:202622 EXPECT_EQ(0, d.blocked_get_cookies_count());
2623 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]861fcd52009-08-26 02:33:462624 }
2625
2626 // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set.
2627 {
2628 TestDelegate d;
[email protected]95409e12010-08-17 20:07:112629 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]7461a402011-03-24 23:19:512630 req.set_load_flags(LOAD_DO_NOT_SEND_COOKIES);
[email protected]87a09a92011-07-14 15:50:502631 req.set_context(default_context_);
[email protected]861fcd52009-08-26 02:33:462632 req.Start();
2633 MessageLoop::current()->Run();
2634
2635 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
2636 == std::string::npos);
[email protected]abf03a02010-03-12 05:02:132637
[email protected]9fb83e82010-07-02 18:24:552638 // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies.
[email protected]abf03a02010-03-12 05:02:132639 EXPECT_EQ(0, d.blocked_get_cookies_count());
[email protected]3dbb80b2010-02-09 22:41:202640 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]861fcd52009-08-26 02:33:462641 }
2642}
2643
[email protected]be6fca6c2010-01-30 21:48:572644TEST_F(URLRequestTest, DoNotSaveCookies) {
[email protected]1700c6a2012-02-22 18:07:072645 LocalHttpTestServer test_server;
[email protected]95409e12010-08-17 20:07:112646 ASSERT_TRUE(test_server.Start());
2647
[email protected]861fcd52009-08-26 02:33:462648 // Set up a cookie.
2649 {
2650 TestDelegate d;
[email protected]87a09a92011-07-14 15:50:502651 URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"), &d);
2652 req.set_context(default_context_);
[email protected]861fcd52009-08-26 02:33:462653 req.Start();
2654 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:202655
2656 EXPECT_EQ(0, d.blocked_get_cookies_count());
2657 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]9fb83e82010-07-02 18:24:552658 EXPECT_EQ(1, d.set_cookie_count());
[email protected]861fcd52009-08-26 02:33:462659 }
2660
2661 // Try to set-up another cookie and update the previous cookie.
2662 {
[email protected]861fcd52009-08-26 02:33:462663 TestDelegate d;
[email protected]7461a402011-03-24 23:19:512664 URLRequest req(test_server.GetURL(
[email protected]861fcd52009-08-26 02:33:462665 "set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), &d);
[email protected]7461a402011-03-24 23:19:512666 req.set_load_flags(LOAD_DO_NOT_SAVE_COOKIES);
[email protected]87a09a92011-07-14 15:50:502667 req.set_context(default_context_);
[email protected]861fcd52009-08-26 02:33:462668 req.Start();
2669
2670 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:202671
[email protected]9fb83e82010-07-02 18:24:552672 // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie.
[email protected]3dbb80b2010-02-09 22:41:202673 EXPECT_EQ(0, d.blocked_get_cookies_count());
[email protected]abf03a02010-03-12 05:02:132674 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]9fb83e82010-07-02 18:24:552675 EXPECT_EQ(0, d.set_cookie_count());
[email protected]861fcd52009-08-26 02:33:462676 }
2677
2678 // Verify the cookies weren't saved or updated.
2679 {
2680 TestDelegate d;
[email protected]95409e12010-08-17 20:07:112681 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]87a09a92011-07-14 15:50:502682 req.set_context(default_context_);
[email protected]861fcd52009-08-26 02:33:462683 req.Start();
2684 MessageLoop::current()->Run();
2685
2686 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
2687 == std::string::npos);
2688 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
2689 != std::string::npos);
[email protected]3dbb80b2010-02-09 22:41:202690
2691 EXPECT_EQ(0, d.blocked_get_cookies_count());
2692 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]9fb83e82010-07-02 18:24:552693 EXPECT_EQ(0, d.set_cookie_count());
[email protected]861fcd52009-08-26 02:33:462694 }
2695}
2696
[email protected]34602282010-02-03 22:14:152697TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) {
[email protected]1700c6a2012-02-22 18:07:072698 LocalHttpTestServer test_server;
[email protected]95409e12010-08-17 20:07:112699 ASSERT_TRUE(test_server.Start());
2700
[email protected]34602282010-02-03 22:14:152701 // Set up a cookie.
2702 {
2703 TestDelegate d;
[email protected]7461a402011-03-24 23:19:512704 URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"), &d);
[email protected]87a09a92011-07-14 15:50:502705 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:152706 req.Start();
2707 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:202708
2709 EXPECT_EQ(0, d.blocked_get_cookies_count());
2710 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:152711 }
2712
2713 // Verify that the cookie is set.
2714 {
2715 TestDelegate d;
[email protected]95409e12010-08-17 20:07:112716 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]87a09a92011-07-14 15:50:502717 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:152718 req.Start();
2719 MessageLoop::current()->Run();
2720
2721 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2722 != std::string::npos);
[email protected]3dbb80b2010-02-09 22:41:202723
2724 EXPECT_EQ(0, d.blocked_get_cookies_count());
2725 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:152726 }
2727
2728 // Verify that the cookie isn't sent.
2729 {
[email protected]34602282010-02-03 22:14:152730 TestDelegate d;
[email protected]ed24fad2011-05-10 22:44:012731 d.set_cookie_options(TestDelegate::NO_GET_COOKIES);
[email protected]95409e12010-08-17 20:07:112732 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]87a09a92011-07-14 15:50:502733 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:152734 req.Start();
2735 MessageLoop::current()->Run();
2736
2737 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
2738 == std::string::npos);
2739
[email protected]3dbb80b2010-02-09 22:41:202740 EXPECT_EQ(1, d.blocked_get_cookies_count());
2741 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:152742 }
2743}
2744
2745TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) {
[email protected]1700c6a2012-02-22 18:07:072746 LocalHttpTestServer test_server;
[email protected]95409e12010-08-17 20:07:112747 ASSERT_TRUE(test_server.Start());
2748
[email protected]34602282010-02-03 22:14:152749 // Set up a cookie.
2750 {
2751 TestDelegate d;
[email protected]87a09a92011-07-14 15:50:502752 URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"), &d);
2753 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:152754 req.Start();
2755 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:202756
2757 EXPECT_EQ(0, d.blocked_get_cookies_count());
2758 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:152759 }
2760
2761 // Try to set-up another cookie and update the previous cookie.
2762 {
[email protected]34602282010-02-03 22:14:152763 TestDelegate d;
[email protected]ed24fad2011-05-10 22:44:012764 d.set_cookie_options(TestDelegate::NO_SET_COOKIE);
[email protected]7461a402011-03-24 23:19:512765 URLRequest req(test_server.GetURL(
[email protected]34602282010-02-03 22:14:152766 "set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), &d);
[email protected]87a09a92011-07-14 15:50:502767 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:152768 req.Start();
2769
2770 MessageLoop::current()->Run();
2771
[email protected]3dbb80b2010-02-09 22:41:202772 EXPECT_EQ(0, d.blocked_get_cookies_count());
2773 EXPECT_EQ(2, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:152774 }
2775
2776
2777 // Verify the cookies weren't saved or updated.
2778 {
2779 TestDelegate d;
[email protected]95409e12010-08-17 20:07:112780 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]87a09a92011-07-14 15:50:502781 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:152782 req.Start();
2783 MessageLoop::current()->Run();
2784
2785 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
2786 == std::string::npos);
2787 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
2788 != std::string::npos);
[email protected]3dbb80b2010-02-09 22:41:202789
2790 EXPECT_EQ(0, d.blocked_get_cookies_count());
2791 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:152792 }
2793}
2794
[email protected]2adf2882010-09-27 08:30:372795TEST_F(URLRequestTest, DoNotSaveEmptyCookies) {
[email protected]1700c6a2012-02-22 18:07:072796 LocalHttpTestServer test_server;
[email protected]2adf2882010-09-27 08:30:372797 ASSERT_TRUE(test_server.Start());
2798
[email protected]2adf2882010-09-27 08:30:372799 // Set up an empty cookie.
2800 {
2801 TestDelegate d;
[email protected]7461a402011-03-24 23:19:512802 URLRequest req(test_server.GetURL("set-cookie"), &d);
[email protected]87a09a92011-07-14 15:50:502803 req.set_context(default_context_);
[email protected]2adf2882010-09-27 08:30:372804 req.Start();
2805 MessageLoop::current()->Run();
2806
2807 EXPECT_EQ(0, d.blocked_get_cookies_count());
2808 EXPECT_EQ(0, d.blocked_set_cookie_count());
2809 EXPECT_EQ(0, d.set_cookie_count());
2810 }
2811}
2812
[email protected]34602282010-02-03 22:14:152813TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) {
[email protected]1700c6a2012-02-22 18:07:072814 LocalHttpTestServer test_server;
[email protected]95409e12010-08-17 20:07:112815 ASSERT_TRUE(test_server.Start());
2816
[email protected]34602282010-02-03 22:14:152817 // Set up a cookie.
2818 {
2819 TestDelegate d;
[email protected]7461a402011-03-24 23:19:512820 URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"), &d);
[email protected]87a09a92011-07-14 15:50:502821 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:152822 req.Start();
2823 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:202824
2825 EXPECT_EQ(0, d.blocked_get_cookies_count());
2826 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:152827 }
2828
2829 // Verify that the cookie is set.
2830 {
2831 TestDelegate d;
[email protected]95409e12010-08-17 20:07:112832 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]87a09a92011-07-14 15:50:502833 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:152834 req.Start();
2835 MessageLoop::current()->Run();
2836
2837 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2838 != std::string::npos);
[email protected]3dbb80b2010-02-09 22:41:202839
2840 EXPECT_EQ(0, d.blocked_get_cookies_count());
2841 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:152842 }
2843
2844 // Verify that the cookie isn't sent.
2845 {
[email protected]34602282010-02-03 22:14:152846 TestDelegate d;
[email protected]ed24fad2011-05-10 22:44:012847 d.set_cookie_options(TestDelegate::NO_GET_COOKIES);
[email protected]95409e12010-08-17 20:07:112848 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]87a09a92011-07-14 15:50:502849 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:152850 req.Start();
2851 MessageLoop::current()->Run();
2852
2853 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
2854 == std::string::npos);
2855
[email protected]3dbb80b2010-02-09 22:41:202856 EXPECT_EQ(1, d.blocked_get_cookies_count());
2857 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:152858 }
2859}
2860
2861TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) {
[email protected]1700c6a2012-02-22 18:07:072862 LocalHttpTestServer test_server;
[email protected]95409e12010-08-17 20:07:112863 ASSERT_TRUE(test_server.Start());
2864
[email protected]34602282010-02-03 22:14:152865 // Set up a cookie.
2866 {
2867 TestDelegate d;
[email protected]87a09a92011-07-14 15:50:502868 URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"), &d);
2869 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:152870 req.Start();
2871 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:202872
2873 EXPECT_EQ(0, d.blocked_get_cookies_count());
2874 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:152875 }
2876
2877 // Try to set-up another cookie and update the previous cookie.
2878 {
[email protected]34602282010-02-03 22:14:152879 TestDelegate d;
[email protected]ed24fad2011-05-10 22:44:012880 d.set_cookie_options(TestDelegate::NO_SET_COOKIE);
[email protected]7461a402011-03-24 23:19:512881 URLRequest req(test_server.GetURL(
[email protected]34602282010-02-03 22:14:152882 "set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), &d);
[email protected]87a09a92011-07-14 15:50:502883 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:152884 req.Start();
2885
2886 MessageLoop::current()->Run();
2887
[email protected]3dbb80b2010-02-09 22:41:202888 EXPECT_EQ(0, d.blocked_get_cookies_count());
2889 EXPECT_EQ(2, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:152890 }
2891
2892 // Verify the cookies weren't saved or updated.
2893 {
2894 TestDelegate d;
[email protected]95409e12010-08-17 20:07:112895 TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d);
[email protected]87a09a92011-07-14 15:50:502896 req.set_context(default_context_);
[email protected]34602282010-02-03 22:14:152897 req.Start();
2898 MessageLoop::current()->Run();
2899
2900 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
2901 == std::string::npos);
2902 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
2903 != std::string::npos);
[email protected]3dbb80b2010-02-09 22:41:202904
2905 EXPECT_EQ(0, d.blocked_get_cookies_count());
2906 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]34602282010-02-03 22:14:152907 }
2908}
2909
[email protected]218aa6a12011-09-13 17:38:382910void CheckCookiePolicyCallback(bool* was_run, const CookieList& cookies) {
2911 EXPECT_EQ(1U, cookies.size());
2912 EXPECT_FALSE(cookies[0].IsPersistent());
2913 *was_run = true;
[email protected]72d27ce2011-12-09 00:41:122914 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
[email protected]218aa6a12011-09-13 17:38:382915}
2916
[email protected]4f79b3f2010-02-05 04:27:472917TEST_F(URLRequestTest, CookiePolicy_ForceSession) {
[email protected]1700c6a2012-02-22 18:07:072918 LocalHttpTestServer test_server;
[email protected]95409e12010-08-17 20:07:112919 ASSERT_TRUE(test_server.Start());
2920
[email protected]4f79b3f2010-02-05 04:27:472921 // Set up a cookie.
2922 {
2923 TestDelegate d;
[email protected]ed24fad2011-05-10 22:44:012924 d.set_cookie_options(TestDelegate::FORCE_SESSION);
[email protected]7461a402011-03-24 23:19:512925 URLRequest req(test_server.GetURL(
[email protected]4f79b3f2010-02-05 04:27:472926 "set-cookie?A=1;expires=\"Fri, 05 Feb 2010 23:42:01 GMT\""), &d);
[email protected]87a09a92011-07-14 15:50:502927 req.set_context(default_context_);
[email protected]4f79b3f2010-02-05 04:27:472928 req.Start(); // Triggers an asynchronous cookie policy check.
2929
2930 MessageLoop::current()->Run();
[email protected]3dbb80b2010-02-09 22:41:202931
2932 EXPECT_EQ(0, d.blocked_get_cookies_count());
2933 EXPECT_EQ(0, d.blocked_set_cookie_count());
[email protected]4f79b3f2010-02-05 04:27:472934 }
2935
2936 // Now, check the cookie store.
[email protected]218aa6a12011-09-13 17:38:382937 bool was_run = false;
2938 default_context_->cookie_store()->GetCookieMonster()->GetAllCookiesAsync(
2939 base::Bind(&CheckCookiePolicyCallback, &was_run));
2940 MessageLoop::current()->RunAllPending();
2941 DCHECK(was_run);
[email protected]4f79b3f2010-02-05 04:27:472942}
2943
[email protected]71c64f62008-11-15 04:36:512944// In this test, we do a POST which the server will 302 redirect.
2945// The subsequent transaction should use GET, and should not send the
2946// Content-Type header.
2947// http://code.google.com/p/chromium/issues/detail?id=843
[email protected]37314622009-08-17 20:29:392948TEST_F(URLRequestTestHTTP, Post302RedirectGet) {
[email protected]95409e12010-08-17 20:07:112949 ASSERT_TRUE(test_server_.Start());
2950
[email protected]195e77d2009-07-23 19:10:232951 const char kData[] = "hello world";
[email protected]95409e12010-08-17 20:07:112952
[email protected]71c64f62008-11-15 04:36:512953 TestDelegate d;
[email protected]95409e12010-08-17 20:07:112954 TestURLRequest req(test_server_.GetURL("files/redirect-to-echoall"), &d);
[email protected]87a09a92011-07-14 15:50:502955 req.set_context(default_context_);
[email protected]71c64f62008-11-15 04:36:512956 req.set_method("POST");
[email protected]195e77d2009-07-23 19:10:232957 req.set_upload(CreateSimpleUploadData(kData));
[email protected]71c64f62008-11-15 04:36:512958
2959 // Set headers (some of which are specific to the POST).
[email protected]7461a402011-03-24 23:19:512960 HttpRequestHeaders headers;
[email protected]88e6b6f32010-05-07 23:14:252961 headers.AddHeadersFromString(
[email protected]dd265012009-01-08 20:45:272962 "Content-Type: multipart/form-data; "
2963 "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n"
2964 "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,"
2965 "text/plain;q=0.8,image/png,*/*;q=0.5\r\n"
[email protected]71c64f62008-11-15 04:36:512966 "Accept-Language: en-US,en\r\n"
2967 "Accept-Charset: ISO-8859-1,*,utf-8\r\n"
[email protected]195e77d2009-07-23 19:10:232968 "Content-Length: 11\r\n"
[email protected]a86c97cc2009-06-24 21:26:272969 "Origin: http://localhost:1337/");
[email protected]88e6b6f32010-05-07 23:14:252970 req.SetExtraRequestHeaders(headers);
[email protected]71c64f62008-11-15 04:36:512971 req.Start();
2972 MessageLoop::current()->Run();
2973
2974 std::string mime_type;
2975 req.GetMimeType(&mime_type);
2976 EXPECT_EQ("text/html", mime_type);
2977
2978 const std::string& data = d.data_received();
2979
2980 // Check that the post-specific headers were stripped:
2981 EXPECT_FALSE(ContainsString(data, "Content-Length:"));
2982 EXPECT_FALSE(ContainsString(data, "Content-Type:"));
2983 EXPECT_FALSE(ContainsString(data, "Origin:"));
2984
2985 // These extra request headers should not have been stripped.
2986 EXPECT_TRUE(ContainsString(data, "Accept:"));
2987 EXPECT_TRUE(ContainsString(data, "Accept-Language:"));
2988 EXPECT_TRUE(ContainsString(data, "Accept-Charset:"));
2989}
2990
[email protected]21184962011-10-26 00:50:302991// The following tests check that we handle mutating the request method for
[email protected]bbb011f62011-11-01 02:32:052992// HTTP redirects as expected.
2993// See http://crbug.com/56373 and http://crbug.com/102130.
[email protected]21184962011-10-26 00:50:302994
2995TEST_F(URLRequestTestHTTP, Redirect301Tests) {
[email protected]95409e12010-08-17 20:07:112996 ASSERT_TRUE(test_server_.Start());
2997
[email protected]21184962011-10-26 00:50:302998 const GURL url = test_server_.GetURL("files/redirect301-to-echo");
[email protected]95409e12010-08-17 20:07:112999
[email protected]21184962011-10-26 00:50:303000 HTTPRedirectMethodTest(url, "POST", "GET", true);
3001 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
[email protected]bbb011f62011-11-01 02:32:053002 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
[email protected]21184962011-10-26 00:50:303003}
3004
3005TEST_F(URLRequestTestHTTP, Redirect302Tests) {
3006 ASSERT_TRUE(test_server_.Start());
3007
3008 const GURL url = test_server_.GetURL("files/redirect302-to-echo");
3009
3010 HTTPRedirectMethodTest(url, "POST", "GET", true);
3011 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
[email protected]bbb011f62011-11-01 02:32:053012 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
[email protected]21184962011-10-26 00:50:303013}
3014
3015TEST_F(URLRequestTestHTTP, Redirect303Tests) {
3016 ASSERT_TRUE(test_server_.Start());
3017
3018 const GURL url = test_server_.GetURL("files/redirect303-to-echo");
3019
3020 HTTPRedirectMethodTest(url, "POST", "GET", true);
3021 HTTPRedirectMethodTest(url, "PUT", "GET", true);
[email protected]bbb011f62011-11-01 02:32:053022 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
[email protected]21184962011-10-26 00:50:303023}
3024
3025TEST_F(URLRequestTestHTTP, Redirect307Tests) {
3026 ASSERT_TRUE(test_server_.Start());
3027
3028 const GURL url = test_server_.GetURL("files/redirect307-to-echo");
3029
3030 HTTPRedirectMethodTest(url, "POST", "POST", true);
3031 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
[email protected]bbb011f62011-11-01 02:32:053032 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
[email protected]140932f2008-12-12 03:58:063033}
[email protected]dd265012009-01-08 20:45:273034
[email protected]3c5ca8c2011-09-29 01:14:513035TEST_F(URLRequestTestHTTP, InterceptPost302RedirectGet) {
3036 ASSERT_TRUE(test_server_.Start());
3037
3038 const char kData[] = "hello world";
3039
3040 TestDelegate d;
3041 TestURLRequest req(test_server_.GetURL("empty.html"), &d);
3042 req.set_context(default_context_);
3043 req.set_method("POST");
3044 req.set_upload(CreateSimpleUploadData(kData).get());
3045 HttpRequestHeaders headers;
3046 headers.SetHeader(HttpRequestHeaders::kContentLength,
3047 base::UintToString(arraysize(kData) - 1));
3048 req.SetExtraRequestHeaders(headers);
3049
3050 URLRequestRedirectJob* job =
3051 new URLRequestRedirectJob(&req, test_server_.GetURL("echo"));
3052 AddTestInterceptor()->set_main_intercept_job(job);
3053
3054 req.Start();
3055 MessageLoop::current()->Run();
3056 EXPECT_EQ("GET", req.method());
3057}
3058
3059TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) {
3060 ASSERT_TRUE(test_server_.Start());
3061
3062 const char kData[] = "hello world";
3063
3064 TestDelegate d;
3065 TestURLRequest req(test_server_.GetURL("empty.html"), &d);
3066 req.set_context(default_context_);
3067 req.set_method("POST");
3068 req.set_upload(CreateSimpleUploadData(kData).get());
3069 HttpRequestHeaders headers;
3070 headers.SetHeader(HttpRequestHeaders::kContentLength,
3071 base::UintToString(arraysize(kData) - 1));
3072 req.SetExtraRequestHeaders(headers);
3073
3074 URLRequestRedirectJob* job =
3075 new URLRequestRedirectJob(&req, test_server_.GetURL("echo"));
3076 job->set_redirect_code(
3077 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT);
3078 AddTestInterceptor()->set_main_intercept_job(job);
3079
3080 req.Start();
3081 MessageLoop::current()->Run();
3082 EXPECT_EQ("POST", req.method());
3083 EXPECT_EQ(kData, d.data_received());
3084}
3085
[email protected]a5c713f2009-04-16 21:05:473086// Custom URLRequestJobs for use with interceptor tests
[email protected]7461a402011-03-24 23:19:513087class RestartTestJob : public URLRequestTestJob {
[email protected]a5c713f2009-04-16 21:05:473088 public:
[email protected]7461a402011-03-24 23:19:513089 explicit RestartTestJob(URLRequest* request)
3090 : URLRequestTestJob(request, true) {}
[email protected]a5c713f2009-04-16 21:05:473091 protected:
3092 virtual void StartAsync() {
3093 this->NotifyRestartRequired();
3094 }
[email protected]5389bc72009-11-05 23:34:243095 private:
[email protected]13c8a092010-07-29 06:15:443096 ~RestartTestJob() {}
[email protected]a5c713f2009-04-16 21:05:473097};
3098
[email protected]7461a402011-03-24 23:19:513099class CancelTestJob : public URLRequestTestJob {
[email protected]a5c713f2009-04-16 21:05:473100 public:
[email protected]7461a402011-03-24 23:19:513101 explicit CancelTestJob(URLRequest* request)
3102 : URLRequestTestJob(request, true) {}
[email protected]a5c713f2009-04-16 21:05:473103 protected:
3104 virtual void StartAsync() {
3105 request_->Cancel();
3106 }
[email protected]5389bc72009-11-05 23:34:243107 private:
3108 ~CancelTestJob() {}
[email protected]a5c713f2009-04-16 21:05:473109};
3110
[email protected]7461a402011-03-24 23:19:513111class CancelThenRestartTestJob : public URLRequestTestJob {
[email protected]a5c713f2009-04-16 21:05:473112 public:
[email protected]7461a402011-03-24 23:19:513113 explicit CancelThenRestartTestJob(URLRequest* request)
3114 : URLRequestTestJob(request, true) {
[email protected]a5c713f2009-04-16 21:05:473115 }
3116 protected:
3117 virtual void StartAsync() {
3118 request_->Cancel();
3119 this->NotifyRestartRequired();
3120 }
[email protected]5389bc72009-11-05 23:34:243121 private:
3122 ~CancelThenRestartTestJob() {}
[email protected]a5c713f2009-04-16 21:05:473123};
3124
3125// An Interceptor for use with interceptor tests
[email protected]7461a402011-03-24 23:19:513126class TestInterceptor : URLRequest::Interceptor {
[email protected]a5c713f2009-04-16 21:05:473127 public:
3128 TestInterceptor()
3129 : intercept_main_request_(false), restart_main_request_(false),
3130 cancel_main_request_(false), cancel_then_restart_main_request_(false),
3131 simulate_main_network_error_(false),
3132 intercept_redirect_(false), cancel_redirect_request_(false),
3133 intercept_final_response_(false), cancel_final_request_(false),
3134 did_intercept_main_(false), did_restart_main_(false),
3135 did_cancel_main_(false), did_cancel_then_restart_main_(false),
3136 did_simulate_error_main_(false),
3137 did_intercept_redirect_(false), did_cancel_redirect_(false),
3138 did_intercept_final_(false), did_cancel_final_(false) {
[email protected]e3539402011-07-19 09:31:083139 URLRequest::Deprecated::RegisterRequestInterceptor(this);
[email protected]a5c713f2009-04-16 21:05:473140 }
3141
3142 ~TestInterceptor() {
[email protected]e3539402011-07-19 09:31:083143 URLRequest::Deprecated::UnregisterRequestInterceptor(this);
[email protected]a5c713f2009-04-16 21:05:473144 }
3145
[email protected]7461a402011-03-24 23:19:513146 virtual URLRequestJob* MaybeIntercept(URLRequest* request) {
[email protected]a5c713f2009-04-16 21:05:473147 if (restart_main_request_) {
3148 restart_main_request_ = false;
3149 did_restart_main_ = true;
3150 return new RestartTestJob(request);
3151 }
3152 if (cancel_main_request_) {
3153 cancel_main_request_ = false;
3154 did_cancel_main_ = true;
3155 return new CancelTestJob(request);
3156 }
3157 if (cancel_then_restart_main_request_) {
3158 cancel_then_restart_main_request_ = false;
3159 did_cancel_then_restart_main_ = true;
3160 return new CancelThenRestartTestJob(request);
3161 }
3162 if (simulate_main_network_error_) {
3163 simulate_main_network_error_ = false;
3164 did_simulate_error_main_ = true;
3165 // will error since the requeted url is not one of its canned urls
[email protected]7461a402011-03-24 23:19:513166 return new URLRequestTestJob(request, true);
[email protected]a5c713f2009-04-16 21:05:473167 }
3168 if (!intercept_main_request_)
3169 return NULL;
3170 intercept_main_request_ = false;
3171 did_intercept_main_ = true;
[email protected]7461a402011-03-24 23:19:513172 return new URLRequestTestJob(request,
[email protected]cbe04ef2011-01-11 00:13:243173 main_headers_,
3174 main_data_,
3175 true);
[email protected]a5c713f2009-04-16 21:05:473176 }
3177
[email protected]7461a402011-03-24 23:19:513178 virtual URLRequestJob* MaybeInterceptRedirect(URLRequest* request,
[email protected]9acd869e2010-12-11 10:18:593179 const GURL& location) {
[email protected]a5c713f2009-04-16 21:05:473180 if (cancel_redirect_request_) {
3181 cancel_redirect_request_ = false;
3182 did_cancel_redirect_ = true;
3183 return new CancelTestJob(request);
3184 }
3185 if (!intercept_redirect_)
3186 return NULL;
3187 intercept_redirect_ = false;
3188 did_intercept_redirect_ = true;
[email protected]7461a402011-03-24 23:19:513189 return new URLRequestTestJob(request,
[email protected]cbe04ef2011-01-11 00:13:243190 redirect_headers_,
3191 redirect_data_,
3192 true);
[email protected]a5c713f2009-04-16 21:05:473193 }
3194
[email protected]7461a402011-03-24 23:19:513195 virtual URLRequestJob* MaybeInterceptResponse(URLRequest* request) {
[email protected]a5c713f2009-04-16 21:05:473196 if (cancel_final_request_) {
3197 cancel_final_request_ = false;
3198 did_cancel_final_ = true;
3199 return new CancelTestJob(request);
3200 }
3201 if (!intercept_final_response_)
3202 return NULL;
3203 intercept_final_response_ = false;
3204 did_intercept_final_ = true;
[email protected]7461a402011-03-24 23:19:513205 return new URLRequestTestJob(request,
[email protected]cbe04ef2011-01-11 00:13:243206 final_headers_,
3207 final_data_,
3208 true);
[email protected]a5c713f2009-04-16 21:05:473209 }
3210
3211 // Whether to intercept the main request, and if so the response to return.
3212 bool intercept_main_request_;
3213 std::string main_headers_;
3214 std::string main_data_;
3215
3216 // Other actions we take at MaybeIntercept time
3217 bool restart_main_request_;
3218 bool cancel_main_request_;
3219 bool cancel_then_restart_main_request_;
3220 bool simulate_main_network_error_;
3221
3222 // Whether to intercept redirects, and if so the response to return.
3223 bool intercept_redirect_;
3224 std::string redirect_headers_;
3225 std::string redirect_data_;
3226
3227 // Other actions we can take at MaybeInterceptRedirect time
3228 bool cancel_redirect_request_;
3229
3230 // Whether to intercept final response, and if so the response to return.
3231 bool intercept_final_response_;
3232 std::string final_headers_;
3233 std::string final_data_;
3234
3235 // Other actions we can take at MaybeInterceptResponse time
3236 bool cancel_final_request_;
3237
3238 // If we did something or not
3239 bool did_intercept_main_;
3240 bool did_restart_main_;
3241 bool did_cancel_main_;
3242 bool did_cancel_then_restart_main_;
3243 bool did_simulate_error_main_;
3244 bool did_intercept_redirect_;
3245 bool did_cancel_redirect_;
3246 bool did_intercept_final_;
3247 bool did_cancel_final_;
3248
3249 // Static getters for canned response header and data strings
3250
3251 static std::string ok_data() {
[email protected]7461a402011-03-24 23:19:513252 return URLRequestTestJob::test_data_1();
[email protected]a5c713f2009-04-16 21:05:473253 }
3254
3255 static std::string ok_headers() {
[email protected]7461a402011-03-24 23:19:513256 return URLRequestTestJob::test_headers();
[email protected]a5c713f2009-04-16 21:05:473257 }
3258
3259 static std::string redirect_data() {
3260 return std::string();
3261 }
3262
3263 static std::string redirect_headers() {
[email protected]7461a402011-03-24 23:19:513264 return URLRequestTestJob::test_redirect_headers();
[email protected]a5c713f2009-04-16 21:05:473265 }
3266
3267 static std::string error_data() {
3268 return std::string("ohhh nooooo mr. bill!");
3269 }
3270
3271 static std::string error_headers() {
[email protected]7461a402011-03-24 23:19:513272 return URLRequestTestJob::test_error_headers();
[email protected]a5c713f2009-04-16 21:05:473273 }
3274};
3275
3276TEST_F(URLRequestTest, Intercept) {
3277 TestInterceptor interceptor;
3278
3279 // intercept the main request and respond with a simple response
3280 interceptor.intercept_main_request_ = true;
3281 interceptor.main_headers_ = TestInterceptor::ok_headers();
3282 interceptor.main_data_ = TestInterceptor::ok_data();
3283
3284 TestDelegate d;
3285 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
[email protected]87a09a92011-07-14 15:50:503286 req.set_context(default_context_);
[email protected]ea8e1812012-02-15 22:07:343287 base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data();
3288 base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data();
3289 base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data();
[email protected]a5c713f2009-04-16 21:05:473290 req.SetUserData(NULL, user_data0);
3291 req.SetUserData(&user_data1, user_data1);
3292 req.SetUserData(&user_data2, user_data2);
3293 req.set_method("GET");
3294 req.Start();
3295 MessageLoop::current()->Run();
3296
3297 // Make sure we can retrieve our specific user data
3298 EXPECT_EQ(user_data0, req.GetUserData(NULL));
3299 EXPECT_EQ(user_data1, req.GetUserData(&user_data1));
3300 EXPECT_EQ(user_data2, req.GetUserData(&user_data2));
3301
3302 // Check the interceptor got called as expected
3303 EXPECT_TRUE(interceptor.did_intercept_main_);
3304
3305 // Check we got one good response
3306 EXPECT_TRUE(req.status().is_success());
3307 EXPECT_EQ(200, req.response_headers()->response_code());
3308 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
3309 EXPECT_EQ(1, d.response_started_count());
3310 EXPECT_EQ(0, d.received_redirect_count());
3311}
3312
3313TEST_F(URLRequestTest, InterceptRedirect) {
3314 TestInterceptor interceptor;
3315
3316 // intercept the main request and respond with a redirect
3317 interceptor.intercept_main_request_ = true;
3318 interceptor.main_headers_ = TestInterceptor::redirect_headers();
3319 interceptor.main_data_ = TestInterceptor::redirect_data();
3320
3321 // intercept that redirect and respond a final OK response
3322 interceptor.intercept_redirect_ = true;
3323 interceptor.redirect_headers_ = TestInterceptor::ok_headers();
3324 interceptor.redirect_data_ = TestInterceptor::ok_data();
3325
3326 TestDelegate d;
3327 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
[email protected]87a09a92011-07-14 15:50:503328 req.set_context(default_context_);
[email protected]a5c713f2009-04-16 21:05:473329 req.set_method("GET");
3330 req.Start();
3331 MessageLoop::current()->Run();
3332
3333 // Check the interceptor got called as expected
3334 EXPECT_TRUE(interceptor.did_intercept_main_);
3335 EXPECT_TRUE(interceptor.did_intercept_redirect_);
3336
3337 // Check we got one good response
3338 EXPECT_TRUE(req.status().is_success());
[email protected]44637822009-08-27 17:01:113339 if (req.status().is_success()) {
3340 EXPECT_EQ(200, req.response_headers()->response_code());
3341 }
[email protected]a5c713f2009-04-16 21:05:473342 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
3343 EXPECT_EQ(1, d.response_started_count());
3344 EXPECT_EQ(0, d.received_redirect_count());
3345}
3346
3347TEST_F(URLRequestTest, InterceptServerError) {
3348 TestInterceptor interceptor;
3349
3350 // intercept the main request to generate a server error response
3351 interceptor.intercept_main_request_ = true;
3352 interceptor.main_headers_ = TestInterceptor::error_headers();
3353 interceptor.main_data_ = TestInterceptor::error_data();
3354
3355 // intercept that error and respond with an OK response
3356 interceptor.intercept_final_response_ = true;
3357 interceptor.final_headers_ = TestInterceptor::ok_headers();
3358 interceptor.final_data_ = TestInterceptor::ok_data();
3359
3360 TestDelegate d;
3361 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
[email protected]87a09a92011-07-14 15:50:503362 req.set_context(default_context_);
[email protected]a5c713f2009-04-16 21:05:473363 req.set_method("GET");
3364 req.Start();
3365 MessageLoop::current()->Run();
3366
3367 // Check the interceptor got called as expected
3368 EXPECT_TRUE(interceptor.did_intercept_main_);
3369 EXPECT_TRUE(interceptor.did_intercept_final_);
3370
3371 // Check we got one good response
3372 EXPECT_TRUE(req.status().is_success());
3373 EXPECT_EQ(200, req.response_headers()->response_code());
3374 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
3375 EXPECT_EQ(1, d.response_started_count());
3376 EXPECT_EQ(0, d.received_redirect_count());
3377}
3378
3379TEST_F(URLRequestTest, InterceptNetworkError) {
3380 TestInterceptor interceptor;
3381
3382 // intercept the main request to simulate a network error
3383 interceptor.simulate_main_network_error_ = true;
3384
3385 // intercept that error and respond with an OK response
3386 interceptor.intercept_final_response_ = true;
3387 interceptor.final_headers_ = TestInterceptor::ok_headers();
3388 interceptor.final_data_ = TestInterceptor::ok_data();
3389
3390 TestDelegate d;
3391 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
[email protected]87a09a92011-07-14 15:50:503392 req.set_context(default_context_);
[email protected]a5c713f2009-04-16 21:05:473393 req.set_method("GET");
3394 req.Start();
3395 MessageLoop::current()->Run();
3396
3397 // Check the interceptor got called as expected
3398 EXPECT_TRUE(interceptor.did_simulate_error_main_);
3399 EXPECT_TRUE(interceptor.did_intercept_final_);
3400
3401 // Check we received one good response
3402 EXPECT_TRUE(req.status().is_success());
3403 EXPECT_EQ(200, req.response_headers()->response_code());
3404 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
3405 EXPECT_EQ(1, d.response_started_count());
3406 EXPECT_EQ(0, d.received_redirect_count());
3407}
3408
3409TEST_F(URLRequestTest, InterceptRestartRequired) {
3410 TestInterceptor interceptor;
3411
3412 // restart the main request
3413 interceptor.restart_main_request_ = true;
3414
3415 // then intercept the new main request and respond with an OK response
3416 interceptor.intercept_main_request_ = true;
3417 interceptor.main_headers_ = TestInterceptor::ok_headers();
3418 interceptor.main_data_ = TestInterceptor::ok_data();
3419
3420 TestDelegate d;
3421 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
[email protected]87a09a92011-07-14 15:50:503422 req.set_context(default_context_);
[email protected]a5c713f2009-04-16 21:05:473423 req.set_method("GET");
3424 req.Start();
3425 MessageLoop::current()->Run();
3426
3427 // Check the interceptor got called as expected
3428 EXPECT_TRUE(interceptor.did_restart_main_);
3429 EXPECT_TRUE(interceptor.did_intercept_main_);
3430
3431 // Check we received one good response
3432 EXPECT_TRUE(req.status().is_success());
[email protected]44637822009-08-27 17:01:113433 if (req.status().is_success()) {
3434 EXPECT_EQ(200, req.response_headers()->response_code());
3435 }
[email protected]a5c713f2009-04-16 21:05:473436 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
3437 EXPECT_EQ(1, d.response_started_count());
3438 EXPECT_EQ(0, d.received_redirect_count());
3439}
3440
3441TEST_F(URLRequestTest, InterceptRespectsCancelMain) {
3442 TestInterceptor interceptor;
3443
3444 // intercept the main request and cancel from within the restarted job
3445 interceptor.cancel_main_request_ = true;
3446
3447 // setup to intercept final response and override it with an OK response
3448 interceptor.intercept_final_response_ = true;
3449 interceptor.final_headers_ = TestInterceptor::ok_headers();
3450 interceptor.final_data_ = TestInterceptor::ok_data();
3451
3452 TestDelegate d;
3453 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
[email protected]87a09a92011-07-14 15:50:503454 req.set_context(default_context_);
[email protected]a5c713f2009-04-16 21:05:473455 req.set_method("GET");
3456 req.Start();
3457 MessageLoop::current()->Run();
3458
3459 // Check the interceptor got called as expected
3460 EXPECT_TRUE(interceptor.did_cancel_main_);
3461 EXPECT_FALSE(interceptor.did_intercept_final_);
3462
3463 // Check we see a canceled request
3464 EXPECT_FALSE(req.status().is_success());
[email protected]7461a402011-03-24 23:19:513465 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
[email protected]a5c713f2009-04-16 21:05:473466}
3467
3468TEST_F(URLRequestTest, InterceptRespectsCancelRedirect) {
3469 TestInterceptor interceptor;
3470
3471 // intercept the main request and respond with a redirect
3472 interceptor.intercept_main_request_ = true;
3473 interceptor.main_headers_ = TestInterceptor::redirect_headers();
3474 interceptor.main_data_ = TestInterceptor::redirect_data();
3475
3476 // intercept the redirect and cancel from within that job
3477 interceptor.cancel_redirect_request_ = true;
3478
3479 // setup to intercept final response and override it with an OK response
3480 interceptor.intercept_final_response_ = true;
3481 interceptor.final_headers_ = TestInterceptor::ok_headers();
3482 interceptor.final_data_ = TestInterceptor::ok_data();
3483
3484 TestDelegate d;
3485 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
[email protected]87a09a92011-07-14 15:50:503486 req.set_context(default_context_);
[email protected]a5c713f2009-04-16 21:05:473487 req.set_method("GET");
3488 req.Start();
3489 MessageLoop::current()->Run();
3490
3491 // Check the interceptor got called as expected
3492 EXPECT_TRUE(interceptor.did_intercept_main_);
3493 EXPECT_TRUE(interceptor.did_cancel_redirect_);
3494 EXPECT_FALSE(interceptor.did_intercept_final_);
3495
3496 // Check we see a canceled request
3497 EXPECT_FALSE(req.status().is_success());
[email protected]7461a402011-03-24 23:19:513498 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
[email protected]a5c713f2009-04-16 21:05:473499}
3500
3501TEST_F(URLRequestTest, InterceptRespectsCancelFinal) {
3502 TestInterceptor interceptor;
3503
3504 // intercept the main request to simulate a network error
3505 interceptor.simulate_main_network_error_ = true;
3506
3507 // setup to intercept final response and cancel from within that job
3508 interceptor.cancel_final_request_ = true;
3509
3510 TestDelegate d;
3511 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
[email protected]87a09a92011-07-14 15:50:503512 req.set_context(default_context_);
[email protected]a5c713f2009-04-16 21:05:473513 req.set_method("GET");
3514 req.Start();
3515 MessageLoop::current()->Run();
3516
3517 // Check the interceptor got called as expected
3518 EXPECT_TRUE(interceptor.did_simulate_error_main_);
3519 EXPECT_TRUE(interceptor.did_cancel_final_);
3520
3521 // Check we see a canceled request
3522 EXPECT_FALSE(req.status().is_success());
[email protected]7461a402011-03-24 23:19:513523 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
[email protected]a5c713f2009-04-16 21:05:473524}
3525
3526TEST_F(URLRequestTest, InterceptRespectsCancelInRestart) {
3527 TestInterceptor interceptor;
3528
3529 // intercept the main request and cancel then restart from within that job
3530 interceptor.cancel_then_restart_main_request_ = true;
3531
3532 // setup to intercept final response and override it with an OK response
3533 interceptor.intercept_final_response_ = true;
3534 interceptor.final_headers_ = TestInterceptor::ok_headers();
3535 interceptor.final_data_ = TestInterceptor::ok_data();
3536
3537 TestDelegate d;
3538 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
[email protected]87a09a92011-07-14 15:50:503539 req.set_context(default_context_);
[email protected]a5c713f2009-04-16 21:05:473540 req.set_method("GET");
3541 req.Start();
3542 MessageLoop::current()->Run();
3543
3544 // Check the interceptor got called as expected
3545 EXPECT_TRUE(interceptor.did_cancel_then_restart_main_);
3546 EXPECT_FALSE(interceptor.did_intercept_final_);
3547
3548 // Check we see a canceled request
3549 EXPECT_FALSE(req.status().is_success());
[email protected]7461a402011-03-24 23:19:513550 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
[email protected]a5c713f2009-04-16 21:05:473551}
3552
[email protected]f4f2f512011-02-17 09:28:453553// Check that two different URL requests have different identifiers.
3554TEST_F(URLRequestTest, Identifiers) {
3555 TestDelegate d;
3556 TestURLRequest req(GURL("http://example.com"), &d);
3557 TestURLRequest other_req(GURL("http://example.com"), &d);
3558
3559 ASSERT_NE(req.identifier(), other_req.identifier());
3560}
3561
[email protected]8202d0c2011-02-23 08:31:143562// Check that a failure to connect to the proxy is reported to the network
3563// delegate.
3564TEST_F(URLRequestTest, NetworkDelegateProxyError) {
[email protected]e74aa632011-07-14 17:10:163565 MockHostResolver host_resolver;
3566 host_resolver.rules()->AddSimulatedFailure("*");
[email protected]87a09a92011-07-14 15:50:503567
3568 TestNetworkDelegate network_delegate; // must outlive URLRequests
3569 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
[email protected]8202d0c2011-02-23 08:31:143570 context->set_network_delegate(&network_delegate);
[email protected]87a09a92011-07-14 15:50:503571 context->SetProxyFromString("myproxy:70");
[email protected]e74aa632011-07-14 17:10:163572 context->set_host_resolver(&host_resolver);
[email protected]87a09a92011-07-14 15:50:503573 context->Init();
3574
3575 TestDelegate d;
3576 TestURLRequest req(GURL("http://example.com"), &d);
[email protected]8202d0c2011-02-23 08:31:143577 req.set_context(context);
[email protected]87a09a92011-07-14 15:50:503578 req.set_method("GET");
[email protected]8202d0c2011-02-23 08:31:143579
3580 req.Start();
3581 MessageLoop::current()->Run();
3582
3583 // Check we see a failed request.
3584 EXPECT_FALSE(req.status().is_success());
[email protected]7461a402011-03-24 23:19:513585 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
[email protected]d0cc35b2011-09-08 12:02:053586 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, req.status().error());
[email protected]8202d0c2011-02-23 08:31:143587
3588 EXPECT_EQ(1, network_delegate.error_count());
[email protected]d0cc35b2011-09-08 12:02:053589 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, network_delegate.last_error());
[email protected]a83dd332011-07-13 10:41:013590 EXPECT_EQ(1, network_delegate.completed_requests());
[email protected]8202d0c2011-02-23 08:31:143591}
3592
[email protected]c10450102011-06-27 09:06:163593// Check that it is impossible to change the referrer in the extra headers of
3594// an URLRequest.
3595TEST_F(URLRequestTest, DoNotOverrideReferrer) {
[email protected]1700c6a2012-02-22 18:07:073596 LocalHttpTestServer test_server;
[email protected]c10450102011-06-27 09:06:163597 ASSERT_TRUE(test_server.Start());
3598
[email protected]c10450102011-06-27 09:06:163599 // If extra headers contain referer and the request contains a referer,
3600 // only the latter shall be respected.
3601 {
3602 TestDelegate d;
3603 TestURLRequest req(test_server.GetURL("echoheader?Referer"), &d);
3604 req.set_referrer("http://foo.com/");
[email protected]87a09a92011-07-14 15:50:503605 req.set_context(default_context_);
[email protected]c10450102011-06-27 09:06:163606
3607 HttpRequestHeaders headers;
3608 headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/");
3609 req.SetExtraRequestHeaders(headers);
3610
3611 req.Start();
3612 MessageLoop::current()->Run();
3613
3614 EXPECT_EQ("http://foo.com/", d.data_received());
3615 }
3616
3617 // If extra headers contain a referer but the request does not, no referer
3618 // shall be sent in the header.
3619 {
3620 TestDelegate d;
3621 TestURLRequest req(test_server.GetURL("echoheader?Referer"), &d);
[email protected]87a09a92011-07-14 15:50:503622 req.set_context(default_context_);
[email protected]c10450102011-06-27 09:06:163623
3624 HttpRequestHeaders headers;
3625 headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/");
3626 req.SetExtraRequestHeaders(headers);
[email protected]52c287592011-06-28 17:39:103627 req.set_load_flags(LOAD_VALIDATE_CACHE);
[email protected]c10450102011-06-27 09:06:163628
3629 req.Start();
3630 MessageLoop::current()->Run();
3631
3632 EXPECT_EQ("None", d.data_received());
3633 }
3634}
3635
[email protected]a83dd332011-07-13 10:41:013636// Make sure that net::NetworkDelegate::NotifyCompleted is called if
3637// content is empty.
3638TEST_F(URLRequestTest, RequestCompletionForEmptyResponse) {
[email protected]a83dd332011-07-13 10:41:013639 TestDelegate d;
3640 TestURLRequest req(GURL("data:,"), &d);
[email protected]87a09a92011-07-14 15:50:503641 req.set_context(new TestURLRequestContext());
3642 req.set_context(default_context_);
[email protected]a83dd332011-07-13 10:41:013643 req.Start();
3644 MessageLoop::current()->Run();
3645 EXPECT_EQ("", d.data_received());
[email protected]87a09a92011-07-14 15:50:503646 EXPECT_EQ(1, default_network_delegate_.completed_requests());
[email protected]a83dd332011-07-13 10:41:013647}
3648
[email protected]b89ca032009-08-31 21:41:313649class URLRequestTestFTP : public URLRequestTest {
[email protected]95409e12010-08-17 20:07:113650 public:
[email protected]d9fca99a2012-02-24 16:16:203651 URLRequestTestFTP()
3652 : test_server_(TestServer::TYPE_FTP, TestServer::kLocalhost, FilePath()) {
[email protected]95409e12010-08-17 20:07:113653 }
3654
[email protected]b89ca032009-08-31 21:41:313655 protected:
[email protected]7461a402011-03-24 23:19:513656 TestServer test_server_;
[email protected]b89ca032009-08-31 21:41:313657};
3658
[email protected]5accf7332009-11-24 03:41:383659// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:123660TEST_F(URLRequestTestFTP, DISABLED_FTPDirectoryListing) {
[email protected]95409e12010-08-17 20:07:113661 ASSERT_TRUE(test_server_.Start());
3662
[email protected]a25e90e2009-09-09 17:05:373663 TestDelegate d;
3664 {
[email protected]95409e12010-08-17 20:07:113665 TestURLRequest r(test_server_.GetURL("/"), &d);
[email protected]87a09a92011-07-14 15:50:503666 r.set_context(default_context_);
[email protected]a25e90e2009-09-09 17:05:373667 r.Start();
3668 EXPECT_TRUE(r.is_pending());
3669
3670 MessageLoop::current()->Run();
3671
3672 EXPECT_FALSE(r.is_pending());
3673 EXPECT_EQ(1, d.response_started_count());
3674 EXPECT_FALSE(d.received_data_before_response());
3675 EXPECT_LT(0, d.bytes_received());
[email protected]6d81b482011-02-22 19:47:193676 EXPECT_EQ(test_server_.host_port_pair().host(),
3677 r.GetSocketAddress().host());
3678 EXPECT_EQ(test_server_.host_port_pair().port(),
3679 r.GetSocketAddress().port());
[email protected]a25e90e2009-09-09 17:05:373680 }
3681}
3682
[email protected]7df70012010-02-04 00:09:553683// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:123684TEST_F(URLRequestTestFTP, DISABLED_FTPGetTestAnonymous) {
[email protected]95409e12010-08-17 20:07:113685 ASSERT_TRUE(test_server_.Start());
3686
[email protected]399b8702009-05-01 20:34:023687 FilePath app_path;
[email protected]dd265012009-01-08 20:45:273688 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:023689 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:273690 TestDelegate d;
3691 {
[email protected]95409e12010-08-17 20:07:113692 TestURLRequest r(test_server_.GetURL("/LICENSE"), &d);
[email protected]87a09a92011-07-14 15:50:503693 r.set_context(default_context_);
[email protected]dd265012009-01-08 20:45:273694 r.Start();
3695 EXPECT_TRUE(r.is_pending());
3696
3697 MessageLoop::current()->Run();
3698
3699 int64 file_size = 0;
3700 file_util::GetFileSize(app_path, &file_size);
3701
[email protected]ba2f3342009-07-30 18:08:423702 EXPECT_FALSE(r.is_pending());
[email protected]dd265012009-01-08 20:45:273703 EXPECT_EQ(1, d.response_started_count());
3704 EXPECT_FALSE(d.received_data_before_response());
3705 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
[email protected]6d81b482011-02-22 19:47:193706 EXPECT_EQ(test_server_.host_port_pair().host(),
3707 r.GetSocketAddress().host());
3708 EXPECT_EQ(test_server_.host_port_pair().port(),
3709 r.GetSocketAddress().port());
[email protected]dd265012009-01-08 20:45:273710 }
3711}
3712
[email protected]e9ecbd12009-12-20 18:44:403713// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:123714TEST_F(URLRequestTestFTP, DISABLED_FTPGetTest) {
[email protected]95409e12010-08-17 20:07:113715 ASSERT_TRUE(test_server_.Start());
3716
[email protected]399b8702009-05-01 20:34:023717 FilePath app_path;
[email protected]dd265012009-01-08 20:45:273718 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:023719 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:273720 TestDelegate d;
3721 {
[email protected]95409e12010-08-17 20:07:113722 TestURLRequest r(
3723 test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", "chrome"),
3724 &d);
[email protected]87a09a92011-07-14 15:50:503725 r.set_context(default_context_);
[email protected]dd265012009-01-08 20:45:273726 r.Start();
3727 EXPECT_TRUE(r.is_pending());
3728
3729 MessageLoop::current()->Run();
3730
3731 int64 file_size = 0;
3732 file_util::GetFileSize(app_path, &file_size);
3733
[email protected]ba2f3342009-07-30 18:08:423734 EXPECT_FALSE(r.is_pending());
[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]dd265012009-01-08 20:45:273739 EXPECT_EQ(1, d.response_started_count());
3740 EXPECT_FALSE(d.received_data_before_response());
3741 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
3742 }
3743}
3744
[email protected]49abd652010-08-05 05:04:533745// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:123746TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPassword) {
[email protected]95409e12010-08-17 20:07:113747 ASSERT_TRUE(test_server_.Start());
3748
[email protected]399b8702009-05-01 20:34:023749 FilePath app_path;
[email protected]dd265012009-01-08 20:45:273750 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:023751 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:273752 TestDelegate d;
3753 {
[email protected]95409e12010-08-17 20:07:113754 TestURLRequest r(
3755 test_server_.GetURLWithUserAndPassword("/LICENSE",
3756 "chrome",
3757 "wrong_password"),
3758 &d);
[email protected]87a09a92011-07-14 15:50:503759 r.set_context(default_context_);
[email protected]dd265012009-01-08 20:45:273760 r.Start();
3761 EXPECT_TRUE(r.is_pending());
3762
3763 MessageLoop::current()->Run();
3764
3765 int64 file_size = 0;
3766 file_util::GetFileSize(app_path, &file_size);
3767
[email protected]ba2f3342009-07-30 18:08:423768 EXPECT_FALSE(r.is_pending());
[email protected]dd265012009-01-08 20:45:273769 EXPECT_EQ(1, d.response_started_count());
3770 EXPECT_FALSE(d.received_data_before_response());
3771 EXPECT_EQ(d.bytes_received(), 0);
3772 }
3773}
3774
[email protected]cde4e80d2009-10-16 19:58:153775// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:123776TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPasswordRestart) {
[email protected]95409e12010-08-17 20:07:113777 ASSERT_TRUE(test_server_.Start());
3778
[email protected]8b8a197d2009-08-26 15:57:583779 FilePath app_path;
3780 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
3781 app_path = app_path.AppendASCII("LICENSE");
3782 TestDelegate d;
3783 // Set correct login credentials. The delegate will be asked for them when
3784 // the initial login with wrong credentials will fail.
[email protected]f3cf9802011-10-28 18:44:583785 d.set_credentials(AuthCredentials(kChrome, kChrome));
[email protected]8b8a197d2009-08-26 15:57:583786 {
[email protected]95409e12010-08-17 20:07:113787 TestURLRequest r(
3788 test_server_.GetURLWithUserAndPassword("/LICENSE",
3789 "chrome",
3790 "wrong_password"),
3791 &d);
[email protected]87a09a92011-07-14 15:50:503792 r.set_context(default_context_);
[email protected]8b8a197d2009-08-26 15:57:583793 r.Start();
3794 EXPECT_TRUE(r.is_pending());
3795
3796 MessageLoop::current()->Run();
3797
3798 int64 file_size = 0;
3799 file_util::GetFileSize(app_path, &file_size);
3800
3801 EXPECT_FALSE(r.is_pending());
3802 EXPECT_EQ(1, d.response_started_count());
3803 EXPECT_FALSE(d.received_data_before_response());
3804 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
3805 }
3806}
3807
[email protected]49abd652010-08-05 05:04:533808// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:123809TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUser) {
[email protected]95409e12010-08-17 20:07:113810 ASSERT_TRUE(test_server_.Start());
3811
[email protected]399b8702009-05-01 20:34:023812 FilePath app_path;
[email protected]dd265012009-01-08 20:45:273813 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:023814 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:273815 TestDelegate d;
3816 {
[email protected]95409e12010-08-17 20:07:113817 TestURLRequest r(
3818 test_server_.GetURLWithUserAndPassword("/LICENSE",
3819 "wrong_user",
3820 "chrome"),
3821 &d);
[email protected]87a09a92011-07-14 15:50:503822 r.set_context(default_context_);
[email protected]dd265012009-01-08 20:45:273823 r.Start();
3824 EXPECT_TRUE(r.is_pending());
3825
3826 MessageLoop::current()->Run();
3827
3828 int64 file_size = 0;
3829 file_util::GetFileSize(app_path, &file_size);
3830
[email protected]ba2f3342009-07-30 18:08:423831 EXPECT_FALSE(r.is_pending());
[email protected]dd265012009-01-08 20:45:273832 EXPECT_EQ(1, d.response_started_count());
3833 EXPECT_FALSE(d.received_data_before_response());
3834 EXPECT_EQ(d.bytes_received(), 0);
3835 }
3836}
[email protected]8b8a197d2009-08-26 15:57:583837
[email protected]cde4e80d2009-10-16 19:58:153838// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:123839TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUserRestart) {
[email protected]95409e12010-08-17 20:07:113840 ASSERT_TRUE(test_server_.Start());
3841
[email protected]8b8a197d2009-08-26 15:57:583842 FilePath app_path;
3843 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
3844 app_path = app_path.AppendASCII("LICENSE");
3845 TestDelegate d;
3846 // Set correct login credentials. The delegate will be asked for them when
3847 // the initial login with wrong credentials will fail.
[email protected]f3cf9802011-10-28 18:44:583848 d.set_credentials(AuthCredentials(kChrome, kChrome));
[email protected]8b8a197d2009-08-26 15:57:583849 {
[email protected]95409e12010-08-17 20:07:113850 TestURLRequest r(
3851 test_server_.GetURLWithUserAndPassword("/LICENSE",
3852 "wrong_user",
3853 "chrome"),
3854 &d);
[email protected]87a09a92011-07-14 15:50:503855 r.set_context(default_context_);
[email protected]8b8a197d2009-08-26 15:57:583856 r.Start();
3857 EXPECT_TRUE(r.is_pending());
3858
3859 MessageLoop::current()->Run();
3860
3861 int64 file_size = 0;
3862 file_util::GetFileSize(app_path, &file_size);
3863
3864 EXPECT_FALSE(r.is_pending());
3865 EXPECT_EQ(1, d.response_started_count());
3866 EXPECT_FALSE(d.received_data_before_response());
3867 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
3868 }
3869}
[email protected]60a3df52009-09-22 16:13:243870
[email protected]cde4e80d2009-10-16 19:58:153871// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:123872TEST_F(URLRequestTestFTP, DISABLED_FTPCacheURLCredentials) {
[email protected]95409e12010-08-17 20:07:113873 ASSERT_TRUE(test_server_.Start());
3874
[email protected]60a3df52009-09-22 16:13:243875 FilePath app_path;
3876 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
3877 app_path = app_path.AppendASCII("LICENSE");
3878
3879 scoped_ptr<TestDelegate> d(new TestDelegate);
3880 {
3881 // Pass correct login identity in the URL.
[email protected]95409e12010-08-17 20:07:113882 TestURLRequest r(
3883 test_server_.GetURLWithUserAndPassword("/LICENSE",
3884 "chrome",
3885 "chrome"),
3886 d.get());
[email protected]87a09a92011-07-14 15:50:503887 r.set_context(default_context_);
[email protected]60a3df52009-09-22 16:13:243888 r.Start();
3889 EXPECT_TRUE(r.is_pending());
3890
3891 MessageLoop::current()->Run();
3892
3893 int64 file_size = 0;
3894 file_util::GetFileSize(app_path, &file_size);
3895
3896 EXPECT_FALSE(r.is_pending());
3897 EXPECT_EQ(1, d->response_started_count());
3898 EXPECT_FALSE(d->received_data_before_response());
3899 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
3900 }
3901
3902 d.reset(new TestDelegate);
3903 {
3904 // This request should use cached identity from previous request.
[email protected]95409e12010-08-17 20:07:113905 TestURLRequest r(test_server_.GetURL("/LICENSE"), d.get());
[email protected]87a09a92011-07-14 15:50:503906 r.set_context(default_context_);
[email protected]60a3df52009-09-22 16:13:243907 r.Start();
3908 EXPECT_TRUE(r.is_pending());
3909
3910 MessageLoop::current()->Run();
3911
3912 int64 file_size = 0;
3913 file_util::GetFileSize(app_path, &file_size);
3914
3915 EXPECT_FALSE(r.is_pending());
3916 EXPECT_EQ(1, d->response_started_count());
3917 EXPECT_FALSE(d->received_data_before_response());
3918 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
3919 }
3920}
3921
[email protected]cde4e80d2009-10-16 19:58:153922// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:123923TEST_F(URLRequestTestFTP, DISABLED_FTPCacheLoginBoxCredentials) {
[email protected]95409e12010-08-17 20:07:113924 ASSERT_TRUE(test_server_.Start());
3925
[email protected]60a3df52009-09-22 16:13:243926 FilePath app_path;
3927 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
3928 app_path = app_path.AppendASCII("LICENSE");
3929
3930 scoped_ptr<TestDelegate> d(new TestDelegate);
3931 // Set correct login credentials. The delegate will be asked for them when
3932 // the initial login with wrong credentials will fail.
[email protected]f3cf9802011-10-28 18:44:583933 d->set_credentials(AuthCredentials(kChrome, kChrome));
[email protected]60a3df52009-09-22 16:13:243934 {
[email protected]95409e12010-08-17 20:07:113935 TestURLRequest r(
3936 test_server_.GetURLWithUserAndPassword("/LICENSE",
3937 "chrome",
3938 "wrong_password"),
3939 d.get());
[email protected]87a09a92011-07-14 15:50:503940 r.set_context(default_context_);
[email protected]60a3df52009-09-22 16:13:243941 r.Start();
3942 EXPECT_TRUE(r.is_pending());
3943
3944 MessageLoop::current()->Run();
3945
3946 int64 file_size = 0;
3947 file_util::GetFileSize(app_path, &file_size);
3948
3949 EXPECT_FALSE(r.is_pending());
3950 EXPECT_EQ(1, d->response_started_count());
3951 EXPECT_FALSE(d->received_data_before_response());
3952 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
3953 }
3954
3955 // Use a new delegate without explicit credentials. The cached ones should be
3956 // used.
3957 d.reset(new TestDelegate);
3958 {
3959 // Don't pass wrong credentials in the URL, they would override valid cached
3960 // ones.
[email protected]95409e12010-08-17 20:07:113961 TestURLRequest r(test_server_.GetURL("/LICENSE"), d.get());
[email protected]87a09a92011-07-14 15:50:503962 r.set_context(default_context_);
[email protected]60a3df52009-09-22 16:13:243963 r.Start();
3964 EXPECT_TRUE(r.is_pending());
3965
3966 MessageLoop::current()->Run();
3967
3968 int64 file_size = 0;
3969 file_util::GetFileSize(app_path, &file_size);
3970
3971 EXPECT_FALSE(r.is_pending());
3972 EXPECT_EQ(1, d->response_started_count());
3973 EXPECT_FALSE(d->received_data_before_response());
3974 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
3975 }
3976}
[email protected]09a362d32009-09-24 18:01:333977
3978// Check that default A-L header is sent.
3979TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) {
[email protected]95409e12010-08-17 20:07:113980 ASSERT_TRUE(test_server_.Start());
3981
[email protected]87a09a92011-07-14 15:50:503982 TestNetworkDelegate network_delegate; // must outlive URLRequests
3983 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
3984 context->set_network_delegate(&network_delegate);
3985 context->set_accept_language("en");
3986 context->Init();
3987
[email protected]09a362d32009-09-24 18:01:333988 TestDelegate d;
[email protected]95409e12010-08-17 20:07:113989 TestURLRequest req(test_server_.GetURL("echoheader?Accept-Language"), &d);
[email protected]6dae6b82011-03-30 00:35:343990 req.set_context(context);
[email protected]09a362d32009-09-24 18:01:333991 req.Start();
3992 MessageLoop::current()->Run();
[email protected]6dae6b82011-03-30 00:35:343993 EXPECT_EQ("en", d.data_received());
3994}
3995
3996// Check that an empty A-L header is not sent. http://crbug.com/77365.
3997TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) {
3998 ASSERT_TRUE(test_server_.Start());
3999
[email protected]87a09a92011-07-14 15:50:504000 TestNetworkDelegate network_delegate; // must outlive URLRequests
4001 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
4002 context->set_network_delegate(&network_delegate);
4003 context->Init();
4004 // We override the language after initialization because empty entries
4005 // get overridden by Init().
[email protected]6dae6b82011-03-30 00:35:344006 context->set_accept_language("");
[email protected]87a09a92011-07-14 15:50:504007
4008 TestDelegate d;
4009 TestURLRequest req(test_server_.GetURL("echoheader?Accept-Language"), &d);
[email protected]6dae6b82011-03-30 00:35:344010 req.set_context(context);
4011 req.Start();
4012 MessageLoop::current()->Run();
4013 EXPECT_EQ("None", d.data_received());
[email protected]09a362d32009-09-24 18:01:334014}
4015
4016// Check that if request overrides the A-L header, the default is not appended.
4017// See http://crbug.com/20894
4018TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) {
[email protected]95409e12010-08-17 20:07:114019 ASSERT_TRUE(test_server_.Start());
4020
[email protected]09a362d32009-09-24 18:01:334021 TestDelegate d;
[email protected]87a09a92011-07-14 15:50:504022 TestURLRequest req(test_server_.GetURL("echoheader?Accept-Language"), &d);
4023 req.set_context(default_context_);
[email protected]7461a402011-03-24 23:19:514024 HttpRequestHeaders headers;
4025 headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru");
[email protected]88e6b6f32010-05-07 23:14:254026 req.SetExtraRequestHeaders(headers);
[email protected]09a362d32009-09-24 18:01:334027 req.Start();
4028 MessageLoop::current()->Run();
4029 EXPECT_EQ(std::string("ru"), d.data_received());
4030}
4031
[email protected]c7bef94c2011-06-21 18:05:514032// Check that default A-E header is sent.
4033TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) {
4034 ASSERT_TRUE(test_server_.Start());
4035
4036 TestDelegate d;
[email protected]87a09a92011-07-14 15:50:504037 TestURLRequest req(test_server_.GetURL("echoheader?Accept-Encoding"), &d);
4038 req.set_context(default_context_);
[email protected]c7bef94c2011-06-21 18:05:514039 HttpRequestHeaders headers;
4040 req.SetExtraRequestHeaders(headers);
4041 req.Start();
4042 MessageLoop::current()->Run();
4043 EXPECT_TRUE(ContainsString(d.data_received(), "gzip"));
4044}
4045
4046// Check that if request overrides the A-E header, the default is not appended.
4047// See http://crbug.com/47381
4048TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) {
4049 ASSERT_TRUE(test_server_.Start());
4050
4051 TestDelegate d;
[email protected]87a09a92011-07-14 15:50:504052 TestURLRequest req(test_server_.GetURL("echoheader?Accept-Encoding"), &d);
4053 req.set_context(default_context_);
[email protected]c7bef94c2011-06-21 18:05:514054 HttpRequestHeaders headers;
4055 headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, "identity");
4056 req.SetExtraRequestHeaders(headers);
4057 req.Start();
4058 MessageLoop::current()->Run();
4059 EXPECT_FALSE(ContainsString(d.data_received(), "gzip"));
4060 EXPECT_TRUE(ContainsString(d.data_received(), "identity"));
4061}
4062
[email protected]09a362d32009-09-24 18:01:334063// Check that default A-C header is sent.
4064TEST_F(URLRequestTestHTTP, DefaultAcceptCharset) {
[email protected]95409e12010-08-17 20:07:114065 ASSERT_TRUE(test_server_.Start());
4066
[email protected]87a09a92011-07-14 15:50:504067 TestNetworkDelegate network_delegate; // must outlive URLRequests
4068 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
4069 context->set_network_delegate(&network_delegate);
4070 context->set_accept_charset("en");
4071 context->Init();
4072
[email protected]09a362d32009-09-24 18:01:334073 TestDelegate d;
[email protected]95409e12010-08-17 20:07:114074 TestURLRequest req(test_server_.GetURL("echoheader?Accept-Charset"), &d);
[email protected]6dae6b82011-03-30 00:35:344075 req.set_context(context);
[email protected]09a362d32009-09-24 18:01:334076 req.Start();
4077 MessageLoop::current()->Run();
[email protected]6dae6b82011-03-30 00:35:344078 EXPECT_EQ("en", d.data_received());
4079}
4080
4081// Check that an empty A-C header is not sent. http://crbug.com/77365.
4082TEST_F(URLRequestTestHTTP, EmptyAcceptCharset) {
4083 ASSERT_TRUE(test_server_.Start());
4084
[email protected]87a09a92011-07-14 15:50:504085 TestNetworkDelegate network_delegate; // must outlive URLRequests
4086 scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true));
4087 context->set_network_delegate(&network_delegate);
4088 context->Init();
4089 // We override the accepted charset after initialization because empty
4090 // entries get overridden otherwise.
[email protected]6dae6b82011-03-30 00:35:344091 context->set_accept_charset("");
[email protected]87a09a92011-07-14 15:50:504092
4093 TestDelegate d;
4094 TestURLRequest req(test_server_.GetURL("echoheader?Accept-Charset"), &d);
[email protected]6dae6b82011-03-30 00:35:344095 req.set_context(context);
4096 req.Start();
4097 MessageLoop::current()->Run();
4098 EXPECT_EQ("None", d.data_received());
[email protected]09a362d32009-09-24 18:01:334099}
4100
4101// Check that if request overrides the A-C header, the default is not appended.
4102// See http://crbug.com/20894
4103TEST_F(URLRequestTestHTTP, OverrideAcceptCharset) {
[email protected]95409e12010-08-17 20:07:114104 ASSERT_TRUE(test_server_.Start());
4105
[email protected]09a362d32009-09-24 18:01:334106 TestDelegate d;
[email protected]87a09a92011-07-14 15:50:504107 TestURLRequest req(test_server_.GetURL("echoheader?Accept-Charset"), &d);
4108 req.set_context(default_context_);
[email protected]7461a402011-03-24 23:19:514109 HttpRequestHeaders headers;
4110 headers.SetHeader(HttpRequestHeaders::kAcceptCharset, "koi-8r");
[email protected]88e6b6f32010-05-07 23:14:254111 req.SetExtraRequestHeaders(headers);
[email protected]09a362d32009-09-24 18:01:334112 req.Start();
4113 MessageLoop::current()->Run();
4114 EXPECT_EQ(std::string("koi-8r"), d.data_received());
4115}
[email protected]50ba404b2011-01-21 13:38:454116
4117// Check that default User-Agent header is sent.
4118TEST_F(URLRequestTestHTTP, DefaultUserAgent) {
4119 ASSERT_TRUE(test_server_.Start());
4120
4121 TestDelegate d;
4122 TestURLRequest req(test_server_.GetURL("echoheader?User-Agent"), &d);
[email protected]87a09a92011-07-14 15:50:504123 req.set_context(default_context_);
[email protected]50ba404b2011-01-21 13:38:454124 req.Start();
4125 MessageLoop::current()->Run();
4126 EXPECT_EQ(req.context()->GetUserAgent(req.url()), d.data_received());
4127}
4128
4129// Check that if request overrides the User-Agent header,
4130// the default is not appended.
4131TEST_F(URLRequestTestHTTP, OverrideUserAgent) {
4132 ASSERT_TRUE(test_server_.Start());
4133
4134 TestDelegate d;
[email protected]87a09a92011-07-14 15:50:504135 TestURLRequest req(test_server_.GetURL("echoheader?User-Agent"), &d);
4136 req.set_context(default_context_);
[email protected]7461a402011-03-24 23:19:514137 HttpRequestHeaders headers;
4138 headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)");
[email protected]50ba404b2011-01-21 13:38:454139 req.SetExtraRequestHeaders(headers);
4140 req.Start();
4141 MessageLoop::current()->Run();
[email protected]ebbad602011-01-21 23:30:474142 // If the net tests are being run with ChromeFrame then we need to allow for
[email protected]0464e16a2011-02-04 22:56:574143 // the 'chromeframe' suffix which is added to the user agent before the
4144 // closing parentheses.
4145 EXPECT_TRUE(StartsWithASCII(d.data_received(), "Lynx (textmode", true));
[email protected]50ba404b2011-01-21 13:38:454146}
[email protected]7461a402011-03-24 23:19:514147
4148} // namespace net