blob: eec3aa570808cd9297cba3c86340582cb14bbc58 [file] [log] [blame]
[email protected]9045b8822012-01-13 20:35:351// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
[email protected]ea224582008-12-07 20:25:465#include "build/build_config.h"
6
[email protected]9396b252008-09-29 17:29:387#if defined(OS_WIN)
[email protected]d8eb84242010-09-25 02:25:068#include <windows.h>
[email protected]aa84a7e2012-03-15 21:29:069#include <shlobj.h>
[email protected]9396b252008-09-29 17:29:3810#endif
11
initial.commit586acc5fe2008-07-26 22:42:5212#include <algorithm>
13#include <string>
14
[email protected]aad63572011-05-24 20:14:3915#include "base/basictypes.h"
[email protected]218aa6a12011-09-13 17:38:3816#include "base/bind.h"
[email protected]8523ba52011-05-22 19:00:5817#include "base/compiler_specific.h"
[email protected]399b8702009-05-01 20:34:0218#include "base/file_util.h"
[email protected]34b2b002009-11-20 06:53:2819#include "base/format_macros.h"
[email protected]084262c2011-12-01 21:12:4720#include "base/memory/weak_ptr.h"
initial.commit586acc5fe2008-07-26 22:42:5221#include "base/message_loop.h"
22#include "base/path_service.h"
23#include "base/process_util.h"
[email protected]528c56d2010-07-30 19:28:4424#include "base/string_number_conversions.h"
[email protected]d8eb84242010-09-25 02:25:0625#include "base/string_piece.h"
[email protected]c3456bb2011-12-12 22:22:1926#include "base/string_split.h"
[email protected]ebbad602011-01-21 23:30:4727#include "base/string_util.h"
[email protected]d8eb84242010-09-25 02:25:0628#include "base/stringprintf.h"
[email protected]be1ce6a72010-08-03 14:35:2229#include "base/utf_string_conversions.h"
[email protected]58e32bb2013-01-21 18:23:2530#include "net/base/capturing_net_log.h"
[email protected]dffe8242012-03-20 15:14:2731#include "net/base/cert_test_util.h"
32#include "net/base/ev_root_ca_metadata.h"
initial.commit586acc5fe2008-07-26 22:42:5233#include "net/base/load_flags.h"
[email protected]58e32bb2013-01-21 18:23:2534#include "net/base/load_timing_info.h"
35#include "net/base/load_timing_info_test_util.h"
[email protected]8202d0c2011-02-23 08:31:1436#include "net/base/mock_host_resolver.h"
[email protected]d8eb84242010-09-25 02:25:0637#include "net/base/net_errors.h"
[email protected]9e743cd2010-03-16 07:03:5338#include "net/base/net_log.h"
39#include "net/base/net_log_unittest.h"
initial.commit586acc5fe2008-07-26 22:42:5240#include "net/base/net_module.h"
41#include "net/base/net_util.h"
[email protected]96adadb2010-08-28 01:16:1742#include "net/base/ssl_connection_status_flags.h"
[email protected]42fdb452012-11-01 12:44:4043#include "net/base/test_data_directory.h"
[email protected]dffe8242012-03-20 15:14:2744#include "net/base/test_root_certs.h"
[email protected]f288ef02012-12-15 20:28:2845#include "net/base/upload_bytes_element_reader.h"
46#include "net/base/upload_data_stream.h"
47#include "net/base/upload_file_element_reader.h"
[email protected]aa84a7e2012-03-15 21:29:0648#include "net/cookies/cookie_monster.h"
49#include "net/cookies/cookie_store_test_helpers.h"
initial.commit586acc5fe2008-07-26 22:42:5250#include "net/disk_cache/disk_cache.h"
[email protected]ba2f3342009-07-30 18:08:4251#include "net/ftp/ftp_network_layer.h"
initial.commit586acc5fe2008-07-26 22:42:5252#include "net/http/http_cache.h"
53#include "net/http/http_network_layer.h"
[email protected]c3456bb2011-12-12 22:22:1954#include "net/http/http_network_session.h"
[email protected]88e6b6f32010-05-07 23:14:2555#include "net/http/http_request_headers.h"
[email protected]319d9e6f2009-02-18 19:47:2156#include "net/http/http_response_headers.h"
[email protected]dffe8242012-03-20 15:14:2757#include "net/ocsp/nss_ocsp.h"
[email protected]63de95b2008-12-10 04:11:2758#include "net/proxy/proxy_service.h"
[email protected]c3456bb2011-12-12 22:22:1959#include "net/socket/ssl_client_socket.h"
[email protected]1b9565c2010-07-21 01:19:3160#include "net/test/test_server.h"
[email protected]d2a133182012-08-05 16:44:0861#include "net/url_request/ftp_protocol_handler.h"
[email protected]ee4c30d2012-11-07 15:08:4362#include "net/url_request/static_http_user_agent_settings.h"
initial.commit586acc5fe2008-07-26 22:42:5263#include "net/url_request/url_request.h"
[email protected]7886a8c2009-08-21 04:11:0964#include "net/url_request/url_request_file_dir_job.h"
[email protected]bcb84f8b2009-08-31 16:20:1465#include "net/url_request/url_request_http_job.h"
[email protected]9d5730b2012-08-24 17:42:4966#include "net/url_request/url_request_job_factory_impl.h"
[email protected]3c5ca8c2011-09-29 01:14:5167#include "net/url_request/url_request_redirect_job.h"
[email protected]a5c713f2009-04-16 21:05:4768#include "net/url_request/url_request_test_job.h"
[email protected]d2db0292011-01-26 20:23:4469#include "net/url_request/url_request_test_util.h"
initial.commit586acc5fe2008-07-26 22:42:5270#include "testing/gtest/include/gtest/gtest.h"
[email protected]23887f04f2008-12-02 19:20:1571#include "testing/platform_test.h"
initial.commit586acc5fe2008-07-26 22:42:5272
[email protected]dffe8242012-03-20 15:14:2773#if defined(OS_WIN)
[email protected]451fd902012-10-03 17:14:4874#include "base/win/scoped_com_initializer.h"
[email protected]dffe8242012-03-20 15:14:2775#include "base/win/windows_version.h"
76#endif
77
[email protected]e1acf6f2008-10-27 20:43:3378using base::Time;
79
[email protected]7461a402011-03-24 23:19:5180namespace net {
81
initial.commit586acc5fe2008-07-26 22:42:5282namespace {
83
[email protected]13c8a092010-07-29 06:15:4484const string16 kChrome(ASCIIToUTF16("chrome"));
85const string16 kSecret(ASCIIToUTF16("secret"));
86const string16 kUser(ASCIIToUTF16("user"));
87
[email protected]58e32bb2013-01-21 18:23:2588// Tests load timing information in the case a fresh connection was used.
89// These tests use the TestServer, which doesn't support keep-alive sockets,
90// so there are no tests here that reuse sockets.
91void TestLoadTimingNotReused(const net::LoadTimingInfo& load_timing_info,
92 int connect_timing_flags) {
93 EXPECT_FALSE(load_timing_info.socket_reused);
94 EXPECT_NE(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
95
96 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
97 EXPECT_FALSE(load_timing_info.request_start.is_null());
98
99 EXPECT_LE(load_timing_info.request_start,
100 load_timing_info.connect_timing.connect_start);
101 ExpectConnectTimingHasTimes(load_timing_info.connect_timing,
102 connect_timing_flags);
103 EXPECT_LE(load_timing_info.connect_timing.connect_end,
104 load_timing_info.send_start);
105 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
106 EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
107
108 // Not set by these tests.
109 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null());
110 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null());
111}
112
113// Tests load timing in the case that there is no underlying connection. This
114// can be used to test in the case of cached responses, errors, or non-HTTP
115// requests.
116void TestLoadTimingNoHttpConnection(
117 const net::LoadTimingInfo& load_timing_info) {
118 EXPECT_FALSE(load_timing_info.socket_reused);
119 EXPECT_EQ(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
120
121 // Only the request times should be non-null.
122 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
123 EXPECT_FALSE(load_timing_info.request_start.is_null());
124
125 ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing);
126
127 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null());
128 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null());
129 EXPECT_TRUE(load_timing_info.send_start.is_null());
130 EXPECT_TRUE(load_timing_info.send_end.is_null());
131 EXPECT_TRUE(load_timing_info.receive_headers_end.is_null());
132}
133
[email protected]8a16266e2009-09-10 21:08:39134base::StringPiece TestNetResourceProvider(int key) {
initial.commit586acc5fe2008-07-26 22:42:52135 return "header";
136}
137
[email protected]71c64f62008-11-15 04:36:51138// Do a case-insensitive search through |haystack| for |needle|.
139bool ContainsString(const std::string& haystack, const char* needle) {
140 std::string::const_iterator it =
141 std::search(haystack.begin(),
142 haystack.end(),
143 needle,
144 needle + strlen(needle),
[email protected]07f1cee2010-11-03 03:53:35145 base::CaseInsensitiveCompare<char>());
[email protected]71c64f62008-11-15 04:36:51146 return it != haystack.end();
147}
148
[email protected]661376a2009-04-29 02:04:23149void FillBuffer(char* buffer, size_t len) {
150 static bool called = false;
151 if (!called) {
152 called = true;
153 int seed = static_cast<int>(Time::Now().ToInternalValue());
154 srand(seed);
155 }
156
157 for (size_t i = 0; i < len; i++) {
158 buffer[i] = static_cast<char>(rand());
159 if (!buffer[i])
160 buffer[i] = 'g';
161 }
162}
163
[email protected]f288ef02012-12-15 20:28:28164UploadDataStream* CreateSimpleUploadData(const char* data) {
165 scoped_ptr<UploadElementReader> reader(
166 new UploadBytesElementReader(data, strlen(data)));
167 return UploadDataStream::CreateWithReader(reader.Pass(), 0);
[email protected]195e77d2009-07-23 19:10:23168}
169
[email protected]96adadb2010-08-28 01:16:17170// Verify that the SSLInfo of a successful SSL connection has valid values.
[email protected]7461a402011-03-24 23:19:51171void CheckSSLInfo(const SSLInfo& ssl_info) {
[email protected]89af3d92010-10-15 20:31:37172 // Allow ChromeFrame fake SSLInfo to get through.
[email protected]c679be22010-08-29 15:20:05173 if (ssl_info.cert.get() &&
[email protected]89af3d92010-10-15 20:31:37174 ssl_info.cert.get()->issuer().GetDisplayName() == "Chrome Internal") {
175 // -1 means unknown.
176 EXPECT_EQ(ssl_info.security_bits, -1);
[email protected]c679be22010-08-29 15:20:05177 return;
[email protected]89af3d92010-10-15 20:31:37178 }
179
[email protected]96adadb2010-08-28 01:16:17180 // -1 means unknown. 0 means no encryption.
181 EXPECT_GT(ssl_info.security_bits, 0);
182
183 // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated.
[email protected]7461a402011-03-24 23:19:51184 int cipher_suite = SSLConnectionStatusToCipherSuite(
[email protected]96adadb2010-08-28 01:16:17185 ssl_info.connection_status);
186 EXPECT_NE(0, cipher_suite);
187}
188
[email protected]ede03212012-09-07 12:52:26189bool FingerprintsEqual(const HashValueVector& a, const HashValueVector& b) {
[email protected]69d7ff442012-02-13 22:41:27190 size_t size = a.size();
191
192 if (size != b.size())
193 return false;
194
195 for (size_t i = 0; i < size; ++i) {
196 if (!a[i].Equals(b[i]))
197 return false;
198 }
199
200 return true;
201}
202
[email protected]dc5a5cf2012-09-26 02:49:30203// A network delegate that allows the user to choose a subset of request stages
204// to block in. When blocking, the delegate can do one of the following:
205// * synchronously return a pre-specified error code, or
206// * asynchronously return that value via an automatically called callback,
207// or
208// * block and wait for the user to do a callback.
209// Additionally, the user may also specify a redirect URL -- then each request
210// with the current URL different from the redirect target will be redirected
211// to that target, in the on-before-URL-request stage, independent of whether
212// the delegate blocks in ON_BEFORE_URL_REQUEST or not.
[email protected]4c76d7c2011-04-15 19:14:12213class BlockingNetworkDelegate : public TestNetworkDelegate {
214 public:
[email protected]dc5a5cf2012-09-26 02:49:30215 // Stages in which the delegate can block.
216 enum Stage {
[email protected]9045b8822012-01-13 20:35:35217 NOT_BLOCKED = 0,
218 ON_BEFORE_URL_REQUEST = 1 << 0,
219 ON_BEFORE_SEND_HEADERS = 1 << 1,
220 ON_HEADERS_RECEIVED = 1 << 2,
221 ON_AUTH_REQUIRED = 1 << 3
222 };
223
[email protected]dc5a5cf2012-09-26 02:49:30224 // Behavior during blocked stages. During other stages, just
225 // returns net::OK or NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION.
226 enum BlockMode {
227 SYNCHRONOUS, // No callback, returns specified return values.
228 AUTO_CALLBACK, // |this| posts a task to run the callback using the
229 // specified return codes.
230 USER_CALLBACK, // User takes care of doing a callback. |retval_| and
231 // |auth_retval_| are ignored. In every blocking stage the
232 // message loop is quit.
233 };
234
235 // Creates a delegate which does not block at all.
236 explicit BlockingNetworkDelegate(BlockMode block_mode);
237
238 // For users to trigger a callback returning |response|.
239 // Side-effects: resets |stage_blocked_for_callback_| and stored callbacks.
240 // Only call if |block_mode_| == USER_CALLBACK.
241 void DoCallback(int response);
242 void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response);
243
244 // Setters.
245 void set_retval(int retval) {
246 ASSERT_NE(USER_CALLBACK, block_mode_);
247 ASSERT_NE(ERR_IO_PENDING, retval);
248 ASSERT_NE(OK, retval);
249 retval_ = retval;
[email protected]9045b8822012-01-13 20:35:35250 }
251
[email protected]dc5a5cf2012-09-26 02:49:30252 // If |auth_retval| == AUTH_REQUIRED_RESPONSE_SET_AUTH, then
253 // |auth_credentials_| will be passed with the response.
254 void set_auth_retval(AuthRequiredResponse auth_retval) {
255 ASSERT_NE(USER_CALLBACK, block_mode_);
256 ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, auth_retval);
257 auth_retval_ = auth_retval;
258 }
259 void set_auth_credentials(const AuthCredentials& auth_credentials) {
260 auth_credentials_ = auth_credentials;
[email protected]9045b8822012-01-13 20:35:35261 }
262
[email protected]dc5a5cf2012-09-26 02:49:30263 void set_redirect_url(const GURL& url) {
264 redirect_url_ = url;
[email protected]9045b8822012-01-13 20:35:35265 }
266
[email protected]dc5a5cf2012-09-26 02:49:30267 void set_block_on(int block_on) {
268 block_on_ = block_on;
[email protected]9045b8822012-01-13 20:35:35269 }
270
[email protected]dc5a5cf2012-09-26 02:49:30271 // Allows the user to check in which state did we block.
272 Stage stage_blocked_for_callback() const {
273 EXPECT_EQ(USER_CALLBACK, block_mode_);
274 return stage_blocked_for_callback_;
[email protected]9045b8822012-01-13 20:35:35275 }
276
277 private:
[email protected]dc5a5cf2012-09-26 02:49:30278 void RunCallback(int response, const CompletionCallback& callback);
279 void RunAuthCallback(AuthRequiredResponse response,
280 const AuthCallback& callback);
281
[email protected]9045b8822012-01-13 20:35:35282 // TestNetworkDelegate implementation.
283 virtual int OnBeforeURLRequest(URLRequest* request,
284 const CompletionCallback& callback,
[email protected]dc5a5cf2012-09-26 02:49:30285 GURL* new_url) OVERRIDE;
[email protected]9045b8822012-01-13 20:35:35286
287 virtual int OnBeforeSendHeaders(URLRequest* request,
288 const CompletionCallback& callback,
[email protected]dc5a5cf2012-09-26 02:49:30289 HttpRequestHeaders* headers) OVERRIDE;
[email protected]9045b8822012-01-13 20:35:35290
291 virtual int OnHeadersReceived(
292 URLRequest* request,
293 const CompletionCallback& callback,
[email protected]507af8f2012-10-20 00:42:32294 const HttpResponseHeaders* original_response_headers,
[email protected]dc5a5cf2012-09-26 02:49:30295 scoped_refptr<HttpResponseHeaders>* override_response_headers) OVERRIDE;
[email protected]9045b8822012-01-13 20:35:35296
297 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired(
298 URLRequest* request,
299 const AuthChallengeInfo& auth_info,
300 const AuthCallback& callback,
[email protected]dc5a5cf2012-09-26 02:49:30301 AuthCredentials* credentials) OVERRIDE;
[email protected]9045b8822012-01-13 20:35:35302
[email protected]dc5a5cf2012-09-26 02:49:30303 // Resets the callbacks and |stage_blocked_for_callback_|.
304 void Reset();
[email protected]9045b8822012-01-13 20:35:35305
[email protected]dc5a5cf2012-09-26 02:49:30306 // Checks whether we should block in |stage|. If yes, returns an error code
307 // and optionally sets up callback based on |block_mode_|. If no, returns OK.
308 int MaybeBlockStage(Stage stage, const CompletionCallback& callback);
309
310 // Configuration parameters, can be adjusted by public methods:
311 const BlockMode block_mode_;
312
313 // Values returned on blocking stages when mode is SYNCHRONOUS or
314 // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING.
315 int retval_; // To be returned in non-auth stages.
316 AuthRequiredResponse auth_retval_;
317
318 GURL redirect_url_; // Used if non-empty.
319 int block_on_; // Bit mask: in which stages to block.
320
321 // |auth_credentials_| will be copied to |*target_auth_credential_| on
322 // callback.
323 AuthCredentials auth_credentials_;
324 AuthCredentials* target_auth_credentials_;
325
326 // Internal variables, not set by not the user:
327 // Last blocked stage waiting for user callback (unused if |block_mode_| !=
328 // USER_CALLBACK).
329 Stage stage_blocked_for_callback_;
330
331 // Callback objects stored during blocking stages.
[email protected]9045b8822012-01-13 20:35:35332 CompletionCallback callback_;
333 AuthCallback auth_callback_;
[email protected]dc5a5cf2012-09-26 02:49:30334
335 base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_;
336
337 DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate);
[email protected]9045b8822012-01-13 20:35:35338};
339
[email protected]dc5a5cf2012-09-26 02:49:30340BlockingNetworkDelegate::BlockingNetworkDelegate(BlockMode block_mode)
341 : block_mode_(block_mode),
342 retval_(OK),
343 auth_retval_(AUTH_REQUIRED_RESPONSE_NO_ACTION),
344 block_on_(0),
345 target_auth_credentials_(NULL),
346 stage_blocked_for_callback_(NOT_BLOCKED),
347 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
348}
349
350void BlockingNetworkDelegate::DoCallback(int response) {
351 ASSERT_EQ(USER_CALLBACK, block_mode_);
352 ASSERT_NE(NOT_BLOCKED, stage_blocked_for_callback_);
353 ASSERT_NE(ON_AUTH_REQUIRED, stage_blocked_for_callback_);
354 CompletionCallback callback = callback_;
355 Reset();
356 RunCallback(response, callback);
357}
358
359void BlockingNetworkDelegate::DoAuthCallback(
360 NetworkDelegate::AuthRequiredResponse response) {
361 ASSERT_EQ(USER_CALLBACK, block_mode_);
362 ASSERT_EQ(ON_AUTH_REQUIRED, stage_blocked_for_callback_);
363 AuthCallback auth_callback = auth_callback_;
364 Reset();
365 RunAuthCallback(response, auth_callback);
366}
367
368void BlockingNetworkDelegate::RunCallback(int response,
369 const CompletionCallback& callback) {
370 callback.Run(response);
371}
372
373void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response,
374 const AuthCallback& callback) {
375 if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) {
376 ASSERT_TRUE(target_auth_credentials_ != NULL);
377 *target_auth_credentials_ = auth_credentials_;
378 }
379 callback.Run(response);
380}
381
382int BlockingNetworkDelegate::OnBeforeURLRequest(
383 URLRequest* request,
384 const CompletionCallback& callback,
385 GURL* new_url) {
386 if (redirect_url_ == request->url())
387 return OK; // We've already seen this request and redirected elsewhere.
388
389 TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url);
390
391 if (!redirect_url_.is_empty())
392 *new_url = redirect_url_;
393
394 return MaybeBlockStage(ON_BEFORE_URL_REQUEST, callback);
395}
396
397int BlockingNetworkDelegate::OnBeforeSendHeaders(
398 URLRequest* request,
399 const CompletionCallback& callback,
400 HttpRequestHeaders* headers) {
401 TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers);
402
403 return MaybeBlockStage(ON_BEFORE_SEND_HEADERS, callback);
404}
405
406int BlockingNetworkDelegate::OnHeadersReceived(
407 URLRequest* request,
408 const CompletionCallback& callback,
[email protected]507af8f2012-10-20 00:42:32409 const HttpResponseHeaders* original_response_headers,
[email protected]dc5a5cf2012-09-26 02:49:30410 scoped_refptr<HttpResponseHeaders>* override_response_headers) {
411 TestNetworkDelegate::OnHeadersReceived(
412 request, callback, original_response_headers,
413 override_response_headers);
414
415 return MaybeBlockStage(ON_HEADERS_RECEIVED, callback);
416}
417
418NetworkDelegate::AuthRequiredResponse BlockingNetworkDelegate::OnAuthRequired(
419 URLRequest* request,
420 const AuthChallengeInfo& auth_info,
421 const AuthCallback& callback,
422 AuthCredentials* credentials) {
423 TestNetworkDelegate::OnAuthRequired(request, auth_info, callback,
424 credentials);
425 // Check that the user has provided callback for the previous blocked stage.
426 EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_);
427
428 if ((block_on_ & ON_AUTH_REQUIRED) == 0) {
429 return AUTH_REQUIRED_RESPONSE_NO_ACTION;
430 }
431
432 target_auth_credentials_ = credentials;
433
434 switch (block_mode_) {
435 case SYNCHRONOUS:
436 if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH)
437 *target_auth_credentials_ = auth_credentials_;
438 return auth_retval_;
439
440 case AUTO_CALLBACK:
441 MessageLoop::current()->PostTask(
442 FROM_HERE,
443 base::Bind(&BlockingNetworkDelegate::RunAuthCallback,
444 weak_factory_.GetWeakPtr(), auth_retval_, callback));
445 return AUTH_REQUIRED_RESPONSE_IO_PENDING;
446
447 case USER_CALLBACK:
448 auth_callback_ = callback;
449 stage_blocked_for_callback_ = ON_AUTH_REQUIRED;
450 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
451 return AUTH_REQUIRED_RESPONSE_IO_PENDING;
452 }
453 NOTREACHED();
454 return AUTH_REQUIRED_RESPONSE_NO_ACTION; // Dummy value.
455}
456
457void BlockingNetworkDelegate::Reset() {
458 EXPECT_NE(NOT_BLOCKED, stage_blocked_for_callback_);
459 stage_blocked_for_callback_ = NOT_BLOCKED;
460 callback_.Reset();
461 auth_callback_.Reset();
462}
463
464int BlockingNetworkDelegate::MaybeBlockStage(
465 BlockingNetworkDelegate::Stage stage,
466 const CompletionCallback& callback) {
467 // Check that the user has provided callback for the previous blocked stage.
468 EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_);
469
470 if ((block_on_ & stage) == 0) {
471 return OK;
472 }
473
474 switch (block_mode_) {
475 case SYNCHRONOUS:
476 EXPECT_NE(OK, retval_);
477 return retval_;
478
479 case AUTO_CALLBACK:
480 MessageLoop::current()->PostTask(
481 FROM_HERE,
482 base::Bind(&BlockingNetworkDelegate::RunCallback,
483 weak_factory_.GetWeakPtr(), retval_, callback));
484 return ERR_IO_PENDING;
485
486 case USER_CALLBACK:
487 callback_ = callback;
488 stage_blocked_for_callback_ = stage;
489 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
490 return ERR_IO_PENDING;
491 }
492 NOTREACHED();
493 return 0;
494}
495
[email protected]d5a4dd62012-05-23 01:41:04496class TestURLRequestContextWithProxy : public TestURLRequestContext {
497 public:
498 // Does not own |delegate|.
499 TestURLRequestContextWithProxy(const std::string& proxy,
500 NetworkDelegate* delegate)
501 : TestURLRequestContext(true) {
502 context_storage_.set_proxy_service(ProxyService::CreateFixed(proxy));
503 set_network_delegate(delegate);
504 Init();
505 }
506 virtual ~TestURLRequestContextWithProxy() {}
507};
508
509} // namespace
510
[email protected]a592c0432012-12-01 18:10:29511// Inherit PlatformTest since we require the autorelease pool on Mac OS X.
[email protected]7a0bb4bf2008-11-19 21:41:48512class URLRequestTest : public PlatformTest {
[email protected]abb26092010-11-11 22:19:00513 public:
[email protected]ef2bf422012-05-11 03:27:09514 URLRequestTest() : default_context_(true) {
515 default_context_.set_network_delegate(&default_network_delegate_);
[email protected]58e32bb2013-01-21 18:23:25516 default_context_.set_net_log(&net_log_);
[email protected]ef2bf422012-05-11 03:27:09517 default_context_.Init();
[email protected]87a09a92011-07-14 15:50:50518 }
[email protected]f53b4802012-12-20 17:04:23519 virtual ~URLRequestTest() {}
[email protected]87a09a92011-07-14 15:50:50520
[email protected]3c5ca8c2011-09-29 01:14:51521 // Adds the TestJobInterceptor to the default context.
522 TestJobInterceptor* AddTestInterceptor() {
[email protected]f53b4802012-12-20 17:04:23523 TestJobInterceptor* protocol_handler_ = new TestJobInterceptor();
524 job_factory_.reset(new URLRequestJobFactoryImpl);
525 job_factory_->SetProtocolHandler("http", protocol_handler_);
526 default_context_.set_job_factory(job_factory_.get());
527 return protocol_handler_;
[email protected]3c5ca8c2011-09-29 01:14:51528 }
529
[email protected]87a09a92011-07-14 15:50:50530 protected:
[email protected]58e32bb2013-01-21 18:23:25531 CapturingNetLog net_log_;
[email protected]ceefd7fd2012-11-29 00:36:24532 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest.
[email protected]f53b4802012-12-20 17:04:23533 scoped_ptr<URLRequestJobFactoryImpl> job_factory_;
[email protected]ef2bf422012-05-11 03:27:09534 TestURLRequestContext default_context_;
[email protected]7a0bb4bf2008-11-19 21:41:48535};
536
[email protected]316c1e5e2012-09-12 15:17:44537TEST_F(URLRequestTest, AboutBlankTest) {
538 TestDelegate d;
539 {
540 URLRequest r(GURL("about:blank"), &d, &default_context_);
541
542 r.Start();
543 EXPECT_TRUE(r.is_pending());
544
545 MessageLoop::current()->Run();
546
547 EXPECT_TRUE(!r.is_pending());
548 EXPECT_FALSE(d.received_data_before_response());
549 EXPECT_EQ(d.bytes_received(), 0);
550 EXPECT_EQ("", r.GetSocketAddress().host());
551 EXPECT_EQ(0, r.GetSocketAddress().port());
552 }
553}
554
555TEST_F(URLRequestTest, DataURLImageTest) {
556 TestDelegate d;
557 {
558 // Use our nice little Chrome logo.
559 URLRequest r(GURL(
560 "data:image/png;base64,"
561 "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUBBjG3"
562 "w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQxIEVxitD"
563 "5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI5MpCVdW0gO7t"
564 "vNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKzMx1+fg9bAgK6zHq9"
565 "cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7CkIMDxQpF7r/MWq12UctI1"
566 "dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0WYUqt57yWf2McHTObYPbVD+Z"
567 "wbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2HQQcm43XwmtoYM6vVKleh0uoWvnzW"
568 "3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nkDFHF9ZS+uYVjRUasMeHUmyLYtcklTvzW"
569 "GFZnNOXczThvpKIzjcahSqIzkvDLayDq6D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTb"
570 "xXX1T5xsV9tm9r4TQwHLiZw/pdDZJea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5"
571 "MrVvMzNaI3+ERHfrFzPKQukrQGI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gV"
572 "Md6q5c8GdosynKmSeRuGzpjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq"
573 "/uxxZKzNiZFGD0wRC3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7F"
574 "Ux1qLnV7MGF40smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufB"
575 "JbsysjjW4kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJM"
576 "nlm2O34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfm"
577 "moRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV5En"
578 "hORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="),
579 &d,
580 &default_context_);
581
582 r.Start();
583 EXPECT_TRUE(r.is_pending());
584
585 MessageLoop::current()->Run();
586
587 EXPECT_TRUE(!r.is_pending());
588 EXPECT_FALSE(d.received_data_before_response());
589 EXPECT_EQ(d.bytes_received(), 911);
590 EXPECT_EQ("", r.GetSocketAddress().host());
591 EXPECT_EQ(0, r.GetSocketAddress().port());
592 }
593}
594
595TEST_F(URLRequestTest, FileTest) {
[email protected]6cdfd7f2013-02-08 20:40:15596 base::FilePath app_path;
[email protected]316c1e5e2012-09-12 15:17:44597 PathService::Get(base::FILE_EXE, &app_path);
598 GURL app_url = FilePathToFileURL(app_path);
599
600 TestDelegate d;
601 {
602 URLRequest r(app_url, &d, &default_context_);
603
604 r.Start();
605 EXPECT_TRUE(r.is_pending());
606
607 MessageLoop::current()->Run();
608
609 int64 file_size = -1;
610 EXPECT_TRUE(file_util::GetFileSize(app_path, &file_size));
611
612 EXPECT_TRUE(!r.is_pending());
613 EXPECT_EQ(1, d.response_started_count());
614 EXPECT_FALSE(d.received_data_before_response());
615 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
616 EXPECT_EQ("", r.GetSocketAddress().host());
617 EXPECT_EQ(0, r.GetSocketAddress().port());
618 }
619}
620
[email protected]ba40bb762012-12-17 07:11:04621TEST_F(URLRequestTest, FileTestCancel) {
[email protected]6cdfd7f2013-02-08 20:40:15622 base::FilePath app_path;
[email protected]ba40bb762012-12-17 07:11:04623 PathService::Get(base::FILE_EXE, &app_path);
624 GURL app_url = FilePathToFileURL(app_path);
625
626 TestDelegate d;
627 {
628 URLRequest r(app_url, &d, &default_context_);
629
630 r.Start();
631 EXPECT_TRUE(r.is_pending());
632 r.Cancel();
633 }
634 // Async cancelation should be safe even when URLRequest has been already
635 // destroyed.
636 MessageLoop::current()->RunUntilIdle();
637}
638
[email protected]316c1e5e2012-09-12 15:17:44639TEST_F(URLRequestTest, FileTestFullSpecifiedRange) {
640 const size_t buffer_size = 4000;
641 scoped_array<char> buffer(new char[buffer_size]);
642 FillBuffer(buffer.get(), buffer_size);
643
[email protected]6cdfd7f2013-02-08 20:40:15644 base::FilePath temp_path;
[email protected]316c1e5e2012-09-12 15:17:44645 EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path));
646 GURL temp_url = FilePathToFileURL(temp_path);
647 EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size));
648
649 int64 file_size;
650 EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size));
651
652 const size_t first_byte_position = 500;
653 const size_t last_byte_position = buffer_size - first_byte_position;
654 const size_t content_length = last_byte_position - first_byte_position + 1;
655 std::string partial_buffer_string(buffer.get() + first_byte_position,
656 buffer.get() + last_byte_position + 1);
657
658 TestDelegate d;
659 {
660 URLRequest r(temp_url, &d, &default_context_);
661
662 HttpRequestHeaders headers;
663 headers.SetHeader(HttpRequestHeaders::kRange,
664 base::StringPrintf(
665 "bytes=%" PRIuS "-%" PRIuS,
666 first_byte_position, last_byte_position));
667 r.SetExtraRequestHeaders(headers);
668 r.Start();
669 EXPECT_TRUE(r.is_pending());
670
671 MessageLoop::current()->Run();
672 EXPECT_TRUE(!r.is_pending());
673 EXPECT_EQ(1, d.response_started_count());
674 EXPECT_FALSE(d.received_data_before_response());
675 EXPECT_EQ(static_cast<int>(content_length), d.bytes_received());
676 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
677 EXPECT_TRUE(partial_buffer_string == d.data_received());
678 }
679
680 EXPECT_TRUE(file_util::Delete(temp_path, false));
681}
682
683TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) {
684 const size_t buffer_size = 4000;
685 scoped_array<char> buffer(new char[buffer_size]);
686 FillBuffer(buffer.get(), buffer_size);
687
[email protected]6cdfd7f2013-02-08 20:40:15688 base::FilePath temp_path;
[email protected]316c1e5e2012-09-12 15:17:44689 EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path));
690 GURL temp_url = FilePathToFileURL(temp_path);
691 EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size));
692
693 int64 file_size;
694 EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size));
695
696 const size_t first_byte_position = 500;
697 const size_t last_byte_position = buffer_size - 1;
698 const size_t content_length = last_byte_position - first_byte_position + 1;
699 std::string partial_buffer_string(buffer.get() + first_byte_position,
700 buffer.get() + last_byte_position + 1);
701
702 TestDelegate d;
703 {
704 URLRequest r(temp_url, &d, &default_context_);
705
706 HttpRequestHeaders headers;
707 headers.SetHeader(HttpRequestHeaders::kRange,
708 base::StringPrintf("bytes=%" PRIuS "-",
709 first_byte_position));
710 r.SetExtraRequestHeaders(headers);
711 r.Start();
712 EXPECT_TRUE(r.is_pending());
713
714 MessageLoop::current()->Run();
715 EXPECT_TRUE(!r.is_pending());
716 EXPECT_EQ(1, d.response_started_count());
717 EXPECT_FALSE(d.received_data_before_response());
718 EXPECT_EQ(static_cast<int>(content_length), d.bytes_received());
719 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
720 EXPECT_TRUE(partial_buffer_string == d.data_received());
721 }
722
723 EXPECT_TRUE(file_util::Delete(temp_path, false));
724}
725
726TEST_F(URLRequestTest, FileTestMultipleRanges) {
727 const size_t buffer_size = 400000;
728 scoped_array<char> buffer(new char[buffer_size]);
729 FillBuffer(buffer.get(), buffer_size);
730
[email protected]6cdfd7f2013-02-08 20:40:15731 base::FilePath temp_path;
[email protected]316c1e5e2012-09-12 15:17:44732 EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path));
733 GURL temp_url = FilePathToFileURL(temp_path);
734 EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size));
735
736 int64 file_size;
737 EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size));
738
739 TestDelegate d;
740 {
741 URLRequest r(temp_url, &d, &default_context_);
742
743 HttpRequestHeaders headers;
744 headers.SetHeader(HttpRequestHeaders::kRange,
745 "bytes=0-0,10-200,200-300");
746 r.SetExtraRequestHeaders(headers);
747 r.Start();
748 EXPECT_TRUE(r.is_pending());
749
750 MessageLoop::current()->Run();
751 EXPECT_TRUE(d.request_failed());
752 }
753
754 EXPECT_TRUE(file_util::Delete(temp_path, false));
755}
756
757TEST_F(URLRequestTest, InvalidUrlTest) {
758 TestDelegate d;
759 {
760 URLRequest r(GURL("invalid url"), &d, &default_context_);
761
762 r.Start();
763 EXPECT_TRUE(r.is_pending());
764
765 MessageLoop::current()->Run();
766 EXPECT_TRUE(d.request_failed());
767 }
768}
769
770#if defined(OS_WIN)
771TEST_F(URLRequestTest, ResolveShortcutTest) {
[email protected]6cdfd7f2013-02-08 20:40:15772 base::FilePath app_path;
[email protected]316c1e5e2012-09-12 15:17:44773 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
774 app_path = app_path.AppendASCII("net");
775 app_path = app_path.AppendASCII("data");
776 app_path = app_path.AppendASCII("url_request_unittest");
777 app_path = app_path.AppendASCII("with-headers.html");
778
779 std::wstring lnk_path = app_path.value() + L".lnk";
780
[email protected]451fd902012-10-03 17:14:48781 base::win::ScopedCOMInitializer com_initializer;
[email protected]00d60fa2012-10-01 18:20:17782
[email protected]316c1e5e2012-09-12 15:17:44783 // Temporarily create a shortcut for test
[email protected]00d60fa2012-10-01 18:20:17784 IShellLink* shell = NULL;
785 ASSERT_TRUE(SUCCEEDED(CoCreateInstance(
786 CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink,
787 reinterpret_cast<LPVOID*>(&shell))));
788 IPersistFile* persist = NULL;
789 ASSERT_TRUE(SUCCEEDED(shell->QueryInterface(
790 IID_IPersistFile, reinterpret_cast<LPVOID*>(&persist))));
791 EXPECT_TRUE(SUCCEEDED(shell->SetPath(app_path.value().c_str())));
792 EXPECT_TRUE(SUCCEEDED(shell->SetDescription(L"ResolveShortcutTest")));
793 EXPECT_TRUE(SUCCEEDED(persist->Save(lnk_path.c_str(), TRUE)));
[email protected]316c1e5e2012-09-12 15:17:44794 if (persist)
795 persist->Release();
796 if (shell)
797 shell->Release();
798
799 TestDelegate d;
800 {
[email protected]6cdfd7f2013-02-08 20:40:15801 URLRequest r(FilePathToFileURL(base::FilePath(lnk_path)), &d,
802 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:44803
804 r.Start();
805 EXPECT_TRUE(r.is_pending());
806
807 MessageLoop::current()->Run();
808
809 WIN32_FILE_ATTRIBUTE_DATA data;
810 GetFileAttributesEx(app_path.value().c_str(),
811 GetFileExInfoStandard, &data);
812 HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ,
813 FILE_SHARE_READ, NULL, OPEN_EXISTING,
814 FILE_ATTRIBUTE_NORMAL, NULL);
815 EXPECT_NE(INVALID_HANDLE_VALUE, file);
816 scoped_array<char> buffer(new char[data.nFileSizeLow]);
817 DWORD read_size;
818 BOOL result;
819 result = ReadFile(file, buffer.get(), data.nFileSizeLow,
820 &read_size, NULL);
821 std::string content(buffer.get(), read_size);
822 CloseHandle(file);
823
824 EXPECT_TRUE(!r.is_pending());
825 EXPECT_EQ(1, d.received_redirect_count());
826 EXPECT_EQ(content, d.data_received());
827 }
828
829 // Clean the shortcut
830 DeleteFile(lnk_path.c_str());
[email protected]316c1e5e2012-09-12 15:17:44831}
832#endif // defined(OS_WIN)
833
834TEST_F(URLRequestTest, FileDirCancelTest) {
835 // Put in mock resource provider.
836 NetModule::SetResourceProvider(TestNetResourceProvider);
837
838 TestDelegate d;
839 {
[email protected]6cdfd7f2013-02-08 20:40:15840 base::FilePath file_path;
[email protected]316c1e5e2012-09-12 15:17:44841 PathService::Get(base::DIR_SOURCE_ROOT, &file_path);
842 file_path = file_path.Append(FILE_PATH_LITERAL("net"));
843 file_path = file_path.Append(FILE_PATH_LITERAL("data"));
844
845 URLRequest req(FilePathToFileURL(file_path), &d, &default_context_);
846 req.Start();
847 EXPECT_TRUE(req.is_pending());
848
849 d.set_cancel_in_received_data_pending(true);
850
851 MessageLoop::current()->Run();
852 }
853
854 // Take out mock resource provider.
855 NetModule::SetResourceProvider(NULL);
856}
857
858TEST_F(URLRequestTest, FileDirRedirectNoCrash) {
859 // There is an implicit redirect when loading a file path that matches a
860 // directory and does not end with a slash. Ensure that following such
861 // redirects does not crash. See http://crbug.com/18686.
862
[email protected]6cdfd7f2013-02-08 20:40:15863 base::FilePath path;
[email protected]316c1e5e2012-09-12 15:17:44864 PathService::Get(base::DIR_SOURCE_ROOT, &path);
865 path = path.Append(FILE_PATH_LITERAL("net"));
866 path = path.Append(FILE_PATH_LITERAL("data"));
867 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
868
869 TestDelegate d;
870 URLRequest req(FilePathToFileURL(path), &d, &default_context_);
871 req.Start();
872 MessageLoop::current()->Run();
873
874 ASSERT_EQ(1, d.received_redirect_count());
875 ASSERT_LT(0, d.bytes_received());
876 ASSERT_FALSE(d.request_failed());
877 ASSERT_TRUE(req.status().is_success());
878}
879
880#if defined(OS_WIN)
881// Don't accept the url "file:///" on windows. See http://crbug.com/1474.
882TEST_F(URLRequestTest, FileDirRedirectSingleSlash) {
883 TestDelegate d;
884 URLRequest req(GURL("file:///"), &d, &default_context_);
885 req.Start();
886 MessageLoop::current()->Run();
887
888 ASSERT_EQ(1, d.received_redirect_count());
889 ASSERT_FALSE(req.status().is_success());
890}
891#endif
892
893// Custom URLRequestJobs for use with interceptor tests
894class RestartTestJob : public URLRequestTestJob {
895 public:
896 RestartTestJob(URLRequest* request, NetworkDelegate* network_delegate)
897 : URLRequestTestJob(request, network_delegate, true) {}
898 protected:
[email protected]46fadfd2013-02-06 09:40:16899 virtual void StartAsync() OVERRIDE {
[email protected]316c1e5e2012-09-12 15:17:44900 this->NotifyRestartRequired();
901 }
902 private:
[email protected]46fadfd2013-02-06 09:40:16903 virtual ~RestartTestJob() {}
[email protected]316c1e5e2012-09-12 15:17:44904};
905
906class CancelTestJob : public URLRequestTestJob {
907 public:
908 explicit CancelTestJob(URLRequest* request, NetworkDelegate* network_delegate)
909 : URLRequestTestJob(request, network_delegate, true) {}
910 protected:
[email protected]46fadfd2013-02-06 09:40:16911 virtual void StartAsync() OVERRIDE {
[email protected]316c1e5e2012-09-12 15:17:44912 request_->Cancel();
913 }
914 private:
[email protected]46fadfd2013-02-06 09:40:16915 virtual ~CancelTestJob() {}
[email protected]316c1e5e2012-09-12 15:17:44916};
917
918class CancelThenRestartTestJob : public URLRequestTestJob {
919 public:
920 explicit CancelThenRestartTestJob(URLRequest* request,
921 NetworkDelegate* network_delegate)
922 : URLRequestTestJob(request, network_delegate, true) {
923 }
924 protected:
[email protected]46fadfd2013-02-06 09:40:16925 virtual void StartAsync() OVERRIDE {
[email protected]316c1e5e2012-09-12 15:17:44926 request_->Cancel();
927 this->NotifyRestartRequired();
928 }
929 private:
[email protected]46fadfd2013-02-06 09:40:16930 virtual ~CancelThenRestartTestJob() {}
[email protected]316c1e5e2012-09-12 15:17:44931};
932
933// An Interceptor for use with interceptor tests
934class TestInterceptor : URLRequest::Interceptor {
935 public:
936 TestInterceptor()
937 : intercept_main_request_(false), restart_main_request_(false),
938 cancel_main_request_(false), cancel_then_restart_main_request_(false),
939 simulate_main_network_error_(false),
940 intercept_redirect_(false), cancel_redirect_request_(false),
941 intercept_final_response_(false), cancel_final_request_(false),
942 did_intercept_main_(false), did_restart_main_(false),
943 did_cancel_main_(false), did_cancel_then_restart_main_(false),
944 did_simulate_error_main_(false),
945 did_intercept_redirect_(false), did_cancel_redirect_(false),
946 did_intercept_final_(false), did_cancel_final_(false) {
947 URLRequest::Deprecated::RegisterRequestInterceptor(this);
948 }
949
[email protected]46fadfd2013-02-06 09:40:16950 virtual ~TestInterceptor() {
[email protected]316c1e5e2012-09-12 15:17:44951 URLRequest::Deprecated::UnregisterRequestInterceptor(this);
952 }
953
[email protected]46fadfd2013-02-06 09:40:16954 virtual URLRequestJob* MaybeIntercept(
955 URLRequest* request,
956 NetworkDelegate* network_delegate) OVERRIDE {
[email protected]316c1e5e2012-09-12 15:17:44957 if (restart_main_request_) {
958 restart_main_request_ = false;
959 did_restart_main_ = true;
960 return new RestartTestJob(request, network_delegate);
961 }
962 if (cancel_main_request_) {
963 cancel_main_request_ = false;
964 did_cancel_main_ = true;
965 return new CancelTestJob(request, network_delegate);
966 }
967 if (cancel_then_restart_main_request_) {
968 cancel_then_restart_main_request_ = false;
969 did_cancel_then_restart_main_ = true;
970 return new CancelThenRestartTestJob(request, network_delegate);
971 }
972 if (simulate_main_network_error_) {
973 simulate_main_network_error_ = false;
974 did_simulate_error_main_ = true;
975 // will error since the requeted url is not one of its canned urls
976 return new URLRequestTestJob(request, network_delegate, true);
977 }
978 if (!intercept_main_request_)
979 return NULL;
980 intercept_main_request_ = false;
981 did_intercept_main_ = true;
982 return new URLRequestTestJob(request,
983 network_delegate,
984 main_headers_,
985 main_data_,
986 true);
987 }
988
989 virtual URLRequestJob* MaybeInterceptRedirect(
990 URLRequest* request,
991 NetworkDelegate* network_delegate,
[email protected]46fadfd2013-02-06 09:40:16992 const GURL& location) OVERRIDE {
[email protected]316c1e5e2012-09-12 15:17:44993 if (cancel_redirect_request_) {
994 cancel_redirect_request_ = false;
995 did_cancel_redirect_ = true;
996 return new CancelTestJob(request, network_delegate);
997 }
998 if (!intercept_redirect_)
999 return NULL;
1000 intercept_redirect_ = false;
1001 did_intercept_redirect_ = true;
1002 return new URLRequestTestJob(request,
1003 network_delegate,
1004 redirect_headers_,
1005 redirect_data_,
1006 true);
1007 }
1008
1009 virtual URLRequestJob* MaybeInterceptResponse(
[email protected]46fadfd2013-02-06 09:40:161010 URLRequest* request, NetworkDelegate* network_delegate) OVERRIDE {
[email protected]316c1e5e2012-09-12 15:17:441011 if (cancel_final_request_) {
1012 cancel_final_request_ = false;
1013 did_cancel_final_ = true;
1014 return new CancelTestJob(request, network_delegate);
1015 }
1016 if (!intercept_final_response_)
1017 return NULL;
1018 intercept_final_response_ = false;
1019 did_intercept_final_ = true;
1020 return new URLRequestTestJob(request,
1021 network_delegate,
1022 final_headers_,
1023 final_data_,
1024 true);
1025 }
1026
1027 // Whether to intercept the main request, and if so the response to return.
1028 bool intercept_main_request_;
1029 std::string main_headers_;
1030 std::string main_data_;
1031
1032 // Other actions we take at MaybeIntercept time
1033 bool restart_main_request_;
1034 bool cancel_main_request_;
1035 bool cancel_then_restart_main_request_;
1036 bool simulate_main_network_error_;
1037
1038 // Whether to intercept redirects, and if so the response to return.
1039 bool intercept_redirect_;
1040 std::string redirect_headers_;
1041 std::string redirect_data_;
1042
1043 // Other actions we can take at MaybeInterceptRedirect time
1044 bool cancel_redirect_request_;
1045
1046 // Whether to intercept final response, and if so the response to return.
1047 bool intercept_final_response_;
1048 std::string final_headers_;
1049 std::string final_data_;
1050
1051 // Other actions we can take at MaybeInterceptResponse time
1052 bool cancel_final_request_;
1053
1054 // If we did something or not
1055 bool did_intercept_main_;
1056 bool did_restart_main_;
1057 bool did_cancel_main_;
1058 bool did_cancel_then_restart_main_;
1059 bool did_simulate_error_main_;
1060 bool did_intercept_redirect_;
1061 bool did_cancel_redirect_;
1062 bool did_intercept_final_;
1063 bool did_cancel_final_;
1064
1065 // Static getters for canned response header and data strings
1066
1067 static std::string ok_data() {
1068 return URLRequestTestJob::test_data_1();
1069 }
1070
1071 static std::string ok_headers() {
1072 return URLRequestTestJob::test_headers();
1073 }
1074
1075 static std::string redirect_data() {
1076 return std::string();
1077 }
1078
1079 static std::string redirect_headers() {
1080 return URLRequestTestJob::test_redirect_headers();
1081 }
1082
1083 static std::string error_data() {
1084 return std::string("ohhh nooooo mr. bill!");
1085 }
1086
1087 static std::string error_headers() {
1088 return URLRequestTestJob::test_error_headers();
1089 }
1090};
1091
1092TEST_F(URLRequestTest, Intercept) {
1093 TestInterceptor interceptor;
1094
1095 // intercept the main request and respond with a simple response
1096 interceptor.intercept_main_request_ = true;
1097 interceptor.main_headers_ = TestInterceptor::ok_headers();
1098 interceptor.main_data_ = TestInterceptor::ok_data();
1099
1100 TestDelegate d;
1101 URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_);
1102 base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data();
1103 base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data();
1104 base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data();
1105 req.SetUserData(NULL, user_data0);
1106 req.SetUserData(&user_data1, user_data1);
1107 req.SetUserData(&user_data2, user_data2);
1108 req.set_method("GET");
1109 req.Start();
1110 MessageLoop::current()->Run();
1111
1112 // Make sure we can retrieve our specific user data
1113 EXPECT_EQ(user_data0, req.GetUserData(NULL));
1114 EXPECT_EQ(user_data1, req.GetUserData(&user_data1));
1115 EXPECT_EQ(user_data2, req.GetUserData(&user_data2));
1116
1117 // Check the interceptor got called as expected
1118 EXPECT_TRUE(interceptor.did_intercept_main_);
1119
1120 // Check we got one good response
1121 EXPECT_TRUE(req.status().is_success());
1122 EXPECT_EQ(200, req.response_headers()->response_code());
1123 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1124 EXPECT_EQ(1, d.response_started_count());
1125 EXPECT_EQ(0, d.received_redirect_count());
1126}
1127
1128TEST_F(URLRequestTest, InterceptRedirect) {
1129 TestInterceptor interceptor;
1130
1131 // intercept the main request and respond with a redirect
1132 interceptor.intercept_main_request_ = true;
1133 interceptor.main_headers_ = TestInterceptor::redirect_headers();
1134 interceptor.main_data_ = TestInterceptor::redirect_data();
1135
1136 // intercept that redirect and respond a final OK response
1137 interceptor.intercept_redirect_ = true;
1138 interceptor.redirect_headers_ = TestInterceptor::ok_headers();
1139 interceptor.redirect_data_ = TestInterceptor::ok_data();
1140
1141 TestDelegate d;
1142 URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_);
1143 req.set_method("GET");
1144 req.Start();
1145 MessageLoop::current()->Run();
1146
1147 // Check the interceptor got called as expected
1148 EXPECT_TRUE(interceptor.did_intercept_main_);
1149 EXPECT_TRUE(interceptor.did_intercept_redirect_);
1150
1151 // Check we got one good response
1152 EXPECT_TRUE(req.status().is_success());
1153 if (req.status().is_success()) {
1154 EXPECT_EQ(200, req.response_headers()->response_code());
1155 }
1156 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1157 EXPECT_EQ(1, d.response_started_count());
1158 EXPECT_EQ(0, d.received_redirect_count());
1159}
1160
1161TEST_F(URLRequestTest, InterceptServerError) {
1162 TestInterceptor interceptor;
1163
1164 // intercept the main request to generate a server error response
1165 interceptor.intercept_main_request_ = true;
1166 interceptor.main_headers_ = TestInterceptor::error_headers();
1167 interceptor.main_data_ = TestInterceptor::error_data();
1168
1169 // intercept that error and respond with an OK response
1170 interceptor.intercept_final_response_ = true;
1171 interceptor.final_headers_ = TestInterceptor::ok_headers();
1172 interceptor.final_data_ = TestInterceptor::ok_data();
1173
1174 TestDelegate d;
1175 URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_);
1176 req.set_method("GET");
1177 req.Start();
1178 MessageLoop::current()->Run();
1179
1180 // Check the interceptor got called as expected
1181 EXPECT_TRUE(interceptor.did_intercept_main_);
1182 EXPECT_TRUE(interceptor.did_intercept_final_);
1183
1184 // Check we got one good response
1185 EXPECT_TRUE(req.status().is_success());
1186 EXPECT_EQ(200, req.response_headers()->response_code());
1187 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1188 EXPECT_EQ(1, d.response_started_count());
1189 EXPECT_EQ(0, d.received_redirect_count());
1190}
1191
1192TEST_F(URLRequestTest, InterceptNetworkError) {
1193 TestInterceptor interceptor;
1194
1195 // intercept the main request to simulate a network error
1196 interceptor.simulate_main_network_error_ = true;
1197
1198 // intercept that error and respond with an OK response
1199 interceptor.intercept_final_response_ = true;
1200 interceptor.final_headers_ = TestInterceptor::ok_headers();
1201 interceptor.final_data_ = TestInterceptor::ok_data();
1202
1203 TestDelegate d;
1204 URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_);
1205 req.set_method("GET");
1206 req.Start();
1207 MessageLoop::current()->Run();
1208
1209 // Check the interceptor got called as expected
1210 EXPECT_TRUE(interceptor.did_simulate_error_main_);
1211 EXPECT_TRUE(interceptor.did_intercept_final_);
1212
1213 // Check we received one good response
1214 EXPECT_TRUE(req.status().is_success());
1215 EXPECT_EQ(200, req.response_headers()->response_code());
1216 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1217 EXPECT_EQ(1, d.response_started_count());
1218 EXPECT_EQ(0, d.received_redirect_count());
1219}
1220
1221TEST_F(URLRequestTest, InterceptRestartRequired) {
1222 TestInterceptor interceptor;
1223
1224 // restart the main request
1225 interceptor.restart_main_request_ = true;
1226
1227 // then intercept the new main request and respond with an OK response
1228 interceptor.intercept_main_request_ = true;
1229 interceptor.main_headers_ = TestInterceptor::ok_headers();
1230 interceptor.main_data_ = TestInterceptor::ok_data();
1231
1232 TestDelegate d;
1233 URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_);
1234 req.set_method("GET");
1235 req.Start();
1236 MessageLoop::current()->Run();
1237
1238 // Check the interceptor got called as expected
1239 EXPECT_TRUE(interceptor.did_restart_main_);
1240 EXPECT_TRUE(interceptor.did_intercept_main_);
1241
1242 // Check we received one good response
1243 EXPECT_TRUE(req.status().is_success());
1244 if (req.status().is_success()) {
1245 EXPECT_EQ(200, req.response_headers()->response_code());
1246 }
1247 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1248 EXPECT_EQ(1, d.response_started_count());
1249 EXPECT_EQ(0, d.received_redirect_count());
1250}
1251
1252TEST_F(URLRequestTest, InterceptRespectsCancelMain) {
1253 TestInterceptor interceptor;
1254
1255 // intercept the main request and cancel from within the restarted job
1256 interceptor.cancel_main_request_ = true;
1257
1258 // setup to intercept final response and override it with an OK response
1259 interceptor.intercept_final_response_ = true;
1260 interceptor.final_headers_ = TestInterceptor::ok_headers();
1261 interceptor.final_data_ = TestInterceptor::ok_data();
1262
1263 TestDelegate d;
1264 URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_);
1265 req.set_method("GET");
1266 req.Start();
1267 MessageLoop::current()->Run();
1268
1269 // Check the interceptor got called as expected
1270 EXPECT_TRUE(interceptor.did_cancel_main_);
1271 EXPECT_FALSE(interceptor.did_intercept_final_);
1272
1273 // Check we see a canceled request
1274 EXPECT_FALSE(req.status().is_success());
1275 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1276}
1277
1278TEST_F(URLRequestTest, InterceptRespectsCancelRedirect) {
1279 TestInterceptor interceptor;
1280
1281 // intercept the main request and respond with a redirect
1282 interceptor.intercept_main_request_ = true;
1283 interceptor.main_headers_ = TestInterceptor::redirect_headers();
1284 interceptor.main_data_ = TestInterceptor::redirect_data();
1285
1286 // intercept the redirect and cancel from within that job
1287 interceptor.cancel_redirect_request_ = true;
1288
1289 // setup to intercept final response and override it with an OK response
1290 interceptor.intercept_final_response_ = true;
1291 interceptor.final_headers_ = TestInterceptor::ok_headers();
1292 interceptor.final_data_ = TestInterceptor::ok_data();
1293
1294 TestDelegate d;
1295 URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_);
1296 req.set_method("GET");
1297 req.Start();
1298 MessageLoop::current()->Run();
1299
1300 // Check the interceptor got called as expected
1301 EXPECT_TRUE(interceptor.did_intercept_main_);
1302 EXPECT_TRUE(interceptor.did_cancel_redirect_);
1303 EXPECT_FALSE(interceptor.did_intercept_final_);
1304
1305 // Check we see a canceled request
1306 EXPECT_FALSE(req.status().is_success());
1307 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1308}
1309
1310TEST_F(URLRequestTest, InterceptRespectsCancelFinal) {
1311 TestInterceptor interceptor;
1312
1313 // intercept the main request to simulate a network error
1314 interceptor.simulate_main_network_error_ = true;
1315
1316 // setup to intercept final response and cancel from within that job
1317 interceptor.cancel_final_request_ = true;
1318
1319 TestDelegate d;
1320 URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_);
1321 req.set_method("GET");
1322 req.Start();
1323 MessageLoop::current()->Run();
1324
1325 // Check the interceptor got called as expected
1326 EXPECT_TRUE(interceptor.did_simulate_error_main_);
1327 EXPECT_TRUE(interceptor.did_cancel_final_);
1328
1329 // Check we see a canceled request
1330 EXPECT_FALSE(req.status().is_success());
1331 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1332}
1333
1334TEST_F(URLRequestTest, InterceptRespectsCancelInRestart) {
1335 TestInterceptor interceptor;
1336
1337 // intercept the main request and cancel then restart from within that job
1338 interceptor.cancel_then_restart_main_request_ = true;
1339
1340 // setup to intercept final response and override it with an OK response
1341 interceptor.intercept_final_response_ = true;
1342 interceptor.final_headers_ = TestInterceptor::ok_headers();
1343 interceptor.final_data_ = TestInterceptor::ok_data();
1344
1345 TestDelegate d;
1346 URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_);
1347 req.set_method("GET");
1348 req.Start();
1349 MessageLoop::current()->Run();
1350
1351 // Check the interceptor got called as expected
1352 EXPECT_TRUE(interceptor.did_cancel_then_restart_main_);
1353 EXPECT_FALSE(interceptor.did_intercept_final_);
1354
1355 // Check we see a canceled request
1356 EXPECT_FALSE(req.status().is_success());
1357 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1358}
1359
1360// Check that two different URL requests have different identifiers.
1361TEST_F(URLRequestTest, Identifiers) {
1362 TestDelegate d;
1363 TestURLRequestContext context;
1364 TestURLRequest req(GURL("http://example.com"), &d, &context);
1365 TestURLRequest other_req(GURL("http://example.com"), &d, &context);
1366
1367 ASSERT_NE(req.identifier(), other_req.identifier());
1368}
1369
1370// Check that a failure to connect to the proxy is reported to the network
1371// delegate.
1372TEST_F(URLRequestTest, NetworkDelegateProxyError) {
1373 MockHostResolver host_resolver;
1374 host_resolver.rules()->AddSimulatedFailure("*");
1375
[email protected]ceefd7fd2012-11-29 00:36:241376 TestNetworkDelegate network_delegate; // Must outlive URLRequests.
[email protected]316c1e5e2012-09-12 15:17:441377 TestURLRequestContextWithProxy context("myproxy:70", &network_delegate);
1378
1379 TestDelegate d;
1380 URLRequest req(GURL("http://example.com"), &d, &context);
1381 req.set_method("GET");
1382
1383 req.Start();
1384 MessageLoop::current()->Run();
1385
1386 // Check we see a failed request.
1387 EXPECT_FALSE(req.status().is_success());
1388 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
1389 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, req.status().error());
1390
1391 EXPECT_EQ(1, network_delegate.error_count());
1392 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, network_delegate.last_error());
1393 EXPECT_EQ(1, network_delegate.completed_requests());
1394}
1395
1396// Make sure that net::NetworkDelegate::NotifyCompleted is called if
1397// content is empty.
1398TEST_F(URLRequestTest, RequestCompletionForEmptyResponse) {
1399 TestDelegate d;
1400 URLRequest req(GURL("data:,"), &d, &default_context_);
1401 req.Start();
1402 MessageLoop::current()->Run();
1403 EXPECT_EQ("", d.data_received());
1404 EXPECT_EQ(1, default_network_delegate_.completed_requests());
1405}
1406
1407// TODO(droger): Support TestServer on iOS (see http://crbug.com/148666).
1408#if !defined(OS_IOS)
1409// A subclass of TestServer that uses a statically-configured hostname. This is
1410// to work around mysterious failures in chrome_frame_net_tests. See:
1411// http://crbug.com/114369
1412class LocalHttpTestServer : public TestServer {
1413 public:
[email protected]6cdfd7f2013-02-08 20:40:151414 explicit LocalHttpTestServer(const base::FilePath& document_root)
[email protected]316c1e5e2012-09-12 15:17:441415 : TestServer(TestServer::TYPE_HTTP,
1416 ScopedCustomUrlRequestTestHttpHost::value(),
1417 document_root) {}
1418 LocalHttpTestServer()
1419 : TestServer(TestServer::TYPE_HTTP,
1420 ScopedCustomUrlRequestTestHttpHost::value(),
[email protected]6cdfd7f2013-02-08 20:40:151421 base::FilePath()) {}
[email protected]316c1e5e2012-09-12 15:17:441422};
1423
[email protected]f2f31b32013-01-16 23:24:091424TEST_F(URLRequestTest, DelayedCookieCallback) {
[email protected]316c1e5e2012-09-12 15:17:441425 LocalHttpTestServer test_server;
1426 ASSERT_TRUE(test_server.Start());
1427
1428 TestURLRequestContext context;
1429 scoped_refptr<DelayedCookieMonster> delayed_cm =
1430 new DelayedCookieMonster();
1431 scoped_refptr<CookieStore> cookie_store = delayed_cm;
1432 context.set_cookie_store(delayed_cm);
1433
1434 // Set up a cookie.
1435 {
1436 TestNetworkDelegate network_delegate;
1437 context.set_network_delegate(&network_delegate);
1438 TestDelegate d;
1439 URLRequest req(
1440 test_server.GetURL("set-cookie?CookieToNotSend=1"), &d, &context);
1441 req.Start();
1442 MessageLoop::current()->Run();
1443 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1444 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1445 EXPECT_EQ(1, network_delegate.set_cookie_count());
1446 }
1447
1448 // Verify that the cookie is set.
1449 {
1450 TestNetworkDelegate network_delegate;
1451 context.set_network_delegate(&network_delegate);
1452 TestDelegate d;
1453 URLRequest req(test_server.GetURL("echoheader?Cookie"), &d, &context);
1454 req.Start();
1455 MessageLoop::current()->Run();
1456
1457 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
1458 != std::string::npos);
1459 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1460 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1461 }
1462}
1463
[email protected]f2f31b32013-01-16 23:24:091464TEST_F(URLRequestTest, DoNotSendCookies) {
[email protected]316c1e5e2012-09-12 15:17:441465 LocalHttpTestServer test_server;
1466 ASSERT_TRUE(test_server.Start());
1467
1468 // Set up a cookie.
1469 {
1470 TestNetworkDelegate network_delegate;
1471 default_context_.set_network_delegate(&network_delegate);
1472 TestDelegate d;
1473 URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"),
1474 &d,
1475 &default_context_);
1476 req.Start();
1477 MessageLoop::current()->Run();
1478 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1479 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1480 }
1481
1482 // Verify that the cookie is set.
1483 {
1484 TestNetworkDelegate network_delegate;
1485 default_context_.set_network_delegate(&network_delegate);
1486 TestDelegate d;
1487 URLRequest req(
1488 test_server.GetURL("echoheader?Cookie"), &d, &default_context_);
1489 req.Start();
1490 MessageLoop::current()->Run();
1491
1492 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
1493 != std::string::npos);
1494 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1495 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1496 }
1497
1498 // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set.
1499 {
1500 TestNetworkDelegate network_delegate;
1501 default_context_.set_network_delegate(&network_delegate);
1502 TestDelegate d;
1503 URLRequest req(
1504 test_server.GetURL("echoheader?Cookie"), &d, &default_context_);
1505 req.set_load_flags(LOAD_DO_NOT_SEND_COOKIES);
1506 req.Start();
1507 MessageLoop::current()->Run();
1508
1509 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
1510 == std::string::npos);
1511
1512 // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies.
1513 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1514 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1515 }
1516}
1517
1518TEST_F(URLRequestTest, DoNotSaveCookies) {
1519 LocalHttpTestServer test_server;
1520 ASSERT_TRUE(test_server.Start());
1521
1522 // Set up a cookie.
1523 {
1524 TestNetworkDelegate network_delegate;
1525 default_context_.set_network_delegate(&network_delegate);
1526 TestDelegate d;
1527 URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"),
1528 &d,
1529 &default_context_);
1530 req.Start();
1531 MessageLoop::current()->Run();
1532
1533 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1534 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1535 EXPECT_EQ(1, network_delegate.set_cookie_count());
1536 }
1537
1538 // Try to set-up another cookie and update the previous cookie.
1539 {
1540 TestNetworkDelegate network_delegate;
1541 default_context_.set_network_delegate(&network_delegate);
1542 TestDelegate d;
1543 URLRequest req(
1544 test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
1545 &d,
1546 &default_context_);
1547 req.set_load_flags(LOAD_DO_NOT_SAVE_COOKIES);
1548 req.Start();
1549
1550 MessageLoop::current()->Run();
1551
1552 // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie.
1553 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1554 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1555 EXPECT_EQ(0, network_delegate.set_cookie_count());
1556 }
1557
1558 // Verify the cookies weren't saved or updated.
1559 {
1560 TestNetworkDelegate network_delegate;
1561 default_context_.set_network_delegate(&network_delegate);
1562 TestDelegate d;
1563 URLRequest req(
1564 test_server.GetURL("echoheader?Cookie"), &d, &default_context_);
1565 req.Start();
1566 MessageLoop::current()->Run();
1567
1568 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
1569 == std::string::npos);
1570 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
1571 != std::string::npos);
1572
1573 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1574 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1575 EXPECT_EQ(0, network_delegate.set_cookie_count());
1576 }
1577}
1578
1579TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) {
1580 LocalHttpTestServer test_server;
1581 ASSERT_TRUE(test_server.Start());
1582
1583 // Set up a cookie.
1584 {
1585 TestNetworkDelegate network_delegate;
1586 default_context_.set_network_delegate(&network_delegate);
1587 TestDelegate d;
1588 URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"),
1589 &d,
1590 &default_context_);
1591 req.Start();
1592 MessageLoop::current()->Run();
1593
1594 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1595 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1596 }
1597
1598 // Verify that the cookie is set.
1599 {
1600 TestNetworkDelegate network_delegate;
1601 default_context_.set_network_delegate(&network_delegate);
1602 TestDelegate d;
1603 URLRequest req(
1604 test_server.GetURL("echoheader?Cookie"), &d, &default_context_);
1605 req.Start();
1606 MessageLoop::current()->Run();
1607
1608 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
1609 != std::string::npos);
1610
1611 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1612 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1613 }
1614
1615 // Verify that the cookie isn't sent.
1616 {
1617 TestNetworkDelegate network_delegate;
1618 default_context_.set_network_delegate(&network_delegate);
1619 TestDelegate d;
1620 network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES);
1621 URLRequest req(
1622 test_server.GetURL("echoheader?Cookie"), &d, &default_context_);
1623 req.Start();
1624 MessageLoop::current()->Run();
1625
1626 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
1627 == std::string::npos);
1628
1629 EXPECT_EQ(1, network_delegate.blocked_get_cookies_count());
1630 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1631 }
1632}
1633
1634TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) {
1635 LocalHttpTestServer test_server;
1636 ASSERT_TRUE(test_server.Start());
1637
1638 // Set up a cookie.
1639 {
1640 TestNetworkDelegate network_delegate;
1641 default_context_.set_network_delegate(&network_delegate);
1642 TestDelegate d;
1643 URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"),
1644 &d,
1645 &default_context_);
1646 req.Start();
1647 MessageLoop::current()->Run();
1648
1649 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1650 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1651 }
1652
1653 // Try to set-up another cookie and update the previous cookie.
1654 {
1655 TestNetworkDelegate network_delegate;
1656 default_context_.set_network_delegate(&network_delegate);
1657 TestDelegate d;
1658 network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE);
1659 URLRequest req(
1660 test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
1661 &d,
1662 &default_context_);
1663 req.Start();
1664
1665 MessageLoop::current()->Run();
1666
1667 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1668 EXPECT_EQ(2, network_delegate.blocked_set_cookie_count());
1669 }
1670
1671 // Verify the cookies weren't saved or updated.
1672 {
1673 TestNetworkDelegate network_delegate;
1674 default_context_.set_network_delegate(&network_delegate);
1675 TestDelegate d;
1676 URLRequest req(
1677 test_server.GetURL("echoheader?Cookie"), &d, &default_context_);
1678 req.Start();
1679 MessageLoop::current()->Run();
1680
1681 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
1682 == std::string::npos);
1683 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
1684 != std::string::npos);
1685
1686 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1687 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1688 }
1689}
1690
1691TEST_F(URLRequestTest, DoNotSaveEmptyCookies) {
1692 LocalHttpTestServer test_server;
1693 ASSERT_TRUE(test_server.Start());
1694
1695 // Set up an empty cookie.
1696 {
1697 TestNetworkDelegate network_delegate;
1698 default_context_.set_network_delegate(&network_delegate);
1699 TestDelegate d;
1700 URLRequest req(test_server.GetURL("set-cookie"), &d, &default_context_);
1701 req.Start();
1702 MessageLoop::current()->Run();
1703
1704 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1705 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1706 EXPECT_EQ(0, network_delegate.set_cookie_count());
1707 }
1708}
1709
1710TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) {
1711 LocalHttpTestServer test_server;
1712 ASSERT_TRUE(test_server.Start());
1713
1714 // Set up a cookie.
1715 {
1716 TestNetworkDelegate network_delegate;
1717 default_context_.set_network_delegate(&network_delegate);
1718 TestDelegate d;
1719 URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"),
1720 &d,
1721 &default_context_);
1722 req.Start();
1723 MessageLoop::current()->Run();
1724
1725 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1726 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1727 }
1728
1729 // Verify that the cookie is set.
1730 {
1731 TestNetworkDelegate network_delegate;
1732 default_context_.set_network_delegate(&network_delegate);
1733 TestDelegate d;
1734 URLRequest req(
1735 test_server.GetURL("echoheader?Cookie"), &d, &default_context_);
1736 req.Start();
1737 MessageLoop::current()->Run();
1738
1739 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
1740 != std::string::npos);
1741
1742 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1743 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1744 }
1745
1746 // Verify that the cookie isn't sent.
1747 {
1748 TestNetworkDelegate network_delegate;
1749 default_context_.set_network_delegate(&network_delegate);
1750 TestDelegate d;
1751 network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES);
1752 URLRequest req(
1753 test_server.GetURL("echoheader?Cookie"), &d, &default_context_);
1754 req.Start();
1755 MessageLoop::current()->Run();
1756
1757 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
1758 == std::string::npos);
1759
1760 EXPECT_EQ(1, network_delegate.blocked_get_cookies_count());
1761 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1762 }
1763}
1764
1765TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) {
1766 LocalHttpTestServer test_server;
1767 ASSERT_TRUE(test_server.Start());
1768
1769 // Set up a cookie.
1770 {
1771 TestNetworkDelegate network_delegate;
1772 default_context_.set_network_delegate(&network_delegate);
1773 TestDelegate d;
1774 URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"),
1775 &d,
1776 &default_context_);
1777 req.Start();
1778 MessageLoop::current()->Run();
1779
1780 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1781 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1782 }
1783
1784 // Try to set-up another cookie and update the previous cookie.
1785 {
1786 TestNetworkDelegate network_delegate;
1787 default_context_.set_network_delegate(&network_delegate);
1788 TestDelegate d;
1789 network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE);
1790 URLRequest req(
1791 test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
1792 &d,
1793 &default_context_);
1794 req.Start();
1795
1796 MessageLoop::current()->Run();
1797
1798 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1799 EXPECT_EQ(2, network_delegate.blocked_set_cookie_count());
1800 }
1801
1802 // Verify the cookies weren't saved or updated.
1803 {
1804 TestNetworkDelegate network_delegate;
1805 default_context_.set_network_delegate(&network_delegate);
1806 TestDelegate d;
1807 URLRequest req(
1808 test_server.GetURL("echoheader?Cookie"), &d, &default_context_);
1809 req.Start();
1810 MessageLoop::current()->Run();
1811
1812 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
1813 == std::string::npos);
1814 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
1815 != std::string::npos);
1816
1817 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1818 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1819 }
1820}
1821
[email protected]5095cd72012-11-01 10:29:161822// FixedDateNetworkDelegate swaps out the server's HTTP Date response header
1823// value for the |fixed_date| argument given to the constructor.
1824class FixedDateNetworkDelegate : public TestNetworkDelegate {
1825 public:
1826 explicit FixedDateNetworkDelegate(const std::string& fixed_date)
1827 : fixed_date_(fixed_date) {}
1828 virtual ~FixedDateNetworkDelegate() {}
1829
1830 // net::NetworkDelegate implementation
1831 virtual int OnHeadersReceived(
1832 net::URLRequest* request,
1833 const net::CompletionCallback& callback,
1834 const net::HttpResponseHeaders* original_response_headers,
1835 scoped_refptr<net::HttpResponseHeaders>* override_response_headers)
1836 OVERRIDE;
1837
1838 private:
1839 std::string fixed_date_;
1840
1841 DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate);
1842};
1843
1844int FixedDateNetworkDelegate::OnHeadersReceived(
1845 net::URLRequest* request,
1846 const net::CompletionCallback& callback,
1847 const net::HttpResponseHeaders* original_response_headers,
1848 scoped_refptr<net::HttpResponseHeaders>* override_response_headers) {
1849 net::HttpResponseHeaders* new_response_headers =
1850 new net::HttpResponseHeaders(original_response_headers->raw_headers());
1851
1852 new_response_headers->RemoveHeader("Date");
1853 new_response_headers->AddHeader("Date: " + fixed_date_);
1854
1855 *override_response_headers = new_response_headers;
1856 return TestNetworkDelegate::OnHeadersReceived(request,
1857 callback,
1858 original_response_headers,
1859 override_response_headers);
1860}
1861
1862// Test that cookie expiration times are adjusted for server/client clock
1863// skew and that we handle incorrect timezone specifier "UTC" in HTTP Date
1864// headers by defaulting to GMT. (crbug.com/135131)
1865TEST_F(URLRequestTest, AcceptClockSkewCookieWithWrongDateTimezone) {
1866 LocalHttpTestServer test_server;
1867 ASSERT_TRUE(test_server.Start());
1868
1869 // Set up an expired cookie.
1870 {
1871 TestNetworkDelegate network_delegate;
1872 default_context_.set_network_delegate(&network_delegate);
1873 TestDelegate d;
1874 URLRequest req(test_server.GetURL(
1875 "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"),
1876 &d,
1877 &default_context_);
1878 req.Start();
1879 MessageLoop::current()->Run();
1880 }
1881 // Verify that the cookie is not set.
1882 {
1883 TestNetworkDelegate network_delegate;
1884 default_context_.set_network_delegate(&network_delegate);
1885 TestDelegate d;
1886 URLRequest req(
1887 test_server.GetURL("echoheader?Cookie"), &d, &default_context_);
1888 req.Start();
1889 MessageLoop::current()->Run();
1890
1891 EXPECT_TRUE(d.data_received().find("StillGood=1") == std::string::npos);
1892 }
1893 // Set up a cookie with clock skew and "UTC" HTTP Date timezone specifier.
1894 {
1895 FixedDateNetworkDelegate network_delegate("18-Apr-1977 22:49:13 UTC");
1896 default_context_.set_network_delegate(&network_delegate);
1897 TestDelegate d;
1898 URLRequest req(test_server.GetURL(
1899 "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"),
1900 &d,
1901 &default_context_);
1902 req.Start();
1903 MessageLoop::current()->Run();
1904 }
1905 // Verify that the cookie is set.
1906 {
1907 TestNetworkDelegate network_delegate;
1908 default_context_.set_network_delegate(&network_delegate);
1909 TestDelegate d;
1910 URLRequest req(
1911 test_server.GetURL("echoheader?Cookie"), &d, &default_context_);
1912 req.Start();
1913 MessageLoop::current()->Run();
1914
1915 EXPECT_TRUE(d.data_received().find("StillGood=1") != std::string::npos);
1916 }
1917}
1918
1919
[email protected]316c1e5e2012-09-12 15:17:441920// Check that it is impossible to change the referrer in the extra headers of
1921// an URLRequest.
1922TEST_F(URLRequestTest, DoNotOverrideReferrer) {
1923 LocalHttpTestServer test_server;
1924 ASSERT_TRUE(test_server.Start());
1925
1926 // If extra headers contain referer and the request contains a referer,
1927 // only the latter shall be respected.
1928 {
1929 TestDelegate d;
1930 URLRequest req(
1931 test_server.GetURL("echoheader?Referer"), &d, &default_context_);
1932 req.set_referrer("http://foo.com/");
1933
1934 HttpRequestHeaders headers;
1935 headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/");
1936 req.SetExtraRequestHeaders(headers);
1937
1938 req.Start();
1939 MessageLoop::current()->Run();
1940
1941 EXPECT_EQ("http://foo.com/", d.data_received());
1942 }
1943
1944 // If extra headers contain a referer but the request does not, no referer
1945 // shall be sent in the header.
1946 {
1947 TestDelegate d;
1948 URLRequest req(
1949 test_server.GetURL("echoheader?Referer"), &d, &default_context_);
1950
1951 HttpRequestHeaders headers;
1952 headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/");
1953 req.SetExtraRequestHeaders(headers);
1954 req.set_load_flags(LOAD_VALIDATE_CACHE);
1955
1956 req.Start();
1957 MessageLoop::current()->Run();
1958
1959 EXPECT_EQ("None", d.data_received());
1960 }
1961}
1962
[email protected]b89290212009-08-14 22:37:351963class URLRequestTestHTTP : public URLRequestTest {
[email protected]95409e12010-08-17 20:07:111964 public:
1965 URLRequestTestHTTP()
[email protected]6cdfd7f2013-02-08 20:40:151966 : test_server_(base::FilePath(FILE_PATH_LITERAL(
[email protected]95409e12010-08-17 20:07:111967 "net/data/url_request_unittest"))) {
1968 }
1969
[email protected]b89290212009-08-14 22:37:351970 protected:
[email protected]21184962011-10-26 00:50:301971 // Requests |redirect_url|, which must return a HTTP 3xx redirect.
1972 // |request_method| is the method to use for the initial request.
1973 // |redirect_method| is the method that is expected to be used for the second
1974 // request, after redirection.
1975 // If |include_data| is true, data is uploaded with the request. The
1976 // response body is expected to match it exactly, if and only if
1977 // |request_method| == |redirect_method|.
1978 void HTTPRedirectMethodTest(const GURL& redirect_url,
1979 const std::string& request_method,
1980 const std::string& redirect_method,
1981 bool include_data) {
1982 static const char kData[] = "hello world";
1983 TestDelegate d;
[email protected]94e2bbe2012-06-22 15:26:131984 URLRequest req(redirect_url, &d, &default_context_);
[email protected]21184962011-10-26 00:50:301985 req.set_method(request_method);
1986 if (include_data) {
[email protected]f288ef02012-12-15 20:28:281987 req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
[email protected]21184962011-10-26 00:50:301988 HttpRequestHeaders headers;
1989 headers.SetHeader(HttpRequestHeaders::kContentLength,
1990 base::UintToString(arraysize(kData) - 1));
1991 req.SetExtraRequestHeaders(headers);
1992 }
1993 req.Start();
1994 MessageLoop::current()->Run();
1995 EXPECT_EQ(redirect_method, req.method());
1996 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
1997 EXPECT_EQ(OK, req.status().error());
1998 if (include_data) {
1999 if (request_method == redirect_method) {
2000 EXPECT_EQ(kData, d.data_received());
2001 } else {
2002 EXPECT_NE(kData, d.data_received());
2003 }
2004 }
2005 if (HasFailure())
2006 LOG(WARNING) << "Request method was: " << request_method;
2007 }
2008
[email protected]762d2db2010-01-11 19:03:012009 void HTTPUploadDataOperationTest(const std::string& method) {
[email protected]762d2db2010-01-11 19:03:012010 const int kMsgSize = 20000; // multiple of 10
2011 const int kIterations = 50;
[email protected]f43b89f32012-05-01 19:39:482012 char* uploadBytes = new char[kMsgSize+1];
2013 char* ptr = uploadBytes;
[email protected]762d2db2010-01-11 19:03:012014 char marker = 'a';
2015 for (int idx = 0; idx < kMsgSize/10; idx++) {
2016 memcpy(ptr, "----------", 10);
2017 ptr += 10;
2018 if (idx % 100 == 0) {
2019 ptr--;
2020 *ptr++ = marker;
2021 if (++marker > 'z')
2022 marker = 'a';
2023 }
2024 }
2025 uploadBytes[kMsgSize] = '\0';
2026
[email protected]762d2db2010-01-11 19:03:012027 for (int i = 0; i < kIterations; ++i) {
2028 TestDelegate d;
[email protected]94e2bbe2012-06-22 15:26:132029 URLRequest r(test_server_.GetURL("echo"), &d, &default_context_);
[email protected]762d2db2010-01-11 19:03:012030 r.set_method(method.c_str());
2031
[email protected]f288ef02012-12-15 20:28:282032 r.set_upload(make_scoped_ptr(CreateSimpleUploadData(uploadBytes)));
[email protected]762d2db2010-01-11 19:03:012033
2034 r.Start();
2035 EXPECT_TRUE(r.is_pending());
2036
2037 MessageLoop::current()->Run();
2038
[email protected]329b68b2012-11-14 17:54:272039 ASSERT_EQ(1, d.response_started_count())
2040 << "request failed: " << r.status().status()
2041 << ", os error: " << r.status().error();
[email protected]762d2db2010-01-11 19:03:012042
2043 EXPECT_FALSE(d.received_data_before_response());
2044 EXPECT_EQ(uploadBytes, d.data_received());
[email protected]762d2db2010-01-11 19:03:012045 }
2046 delete[] uploadBytes;
2047 }
2048
[email protected]ef2bf422012-05-11 03:27:092049 void AddChunksToUpload(URLRequest* r) {
[email protected]0c9bf872011-03-04 17:53:222050 r->AppendChunkToUpload("a", 1, false);
2051 r->AppendChunkToUpload("bcd", 3, false);
2052 r->AppendChunkToUpload("this is a longer chunk than before.", 35, false);
2053 r->AppendChunkToUpload("\r\n\r\n", 4, false);
2054 r->AppendChunkToUpload("0", 1, false);
2055 r->AppendChunkToUpload("2323", 4, true);
[email protected]699efe602011-01-25 07:17:112056 }
2057
[email protected]ef2bf422012-05-11 03:27:092058 void VerifyReceivedDataMatchesChunks(URLRequest* r, TestDelegate* d) {
[email protected]699efe602011-01-25 07:17:112059 // This should match the chunks sent by AddChunksToUpload().
[email protected]329b68b2012-11-14 17:54:272060 const std::string expected_data =
[email protected]699efe602011-01-25 07:17:112061 "abcdthis is a longer chunk than before.\r\n\r\n02323";
2062
[email protected]329b68b2012-11-14 17:54:272063 ASSERT_EQ(1, d->response_started_count())
2064 << "request failed: " << r->status().status()
2065 << ", os error: " << r->status().error();
[email protected]699efe602011-01-25 07:17:112066
2067 EXPECT_FALSE(d->received_data_before_response());
2068
[email protected]329b68b2012-11-14 17:54:272069 EXPECT_EQ(expected_data.size(), static_cast<size_t>(d->bytes_received()));
2070 EXPECT_EQ(expected_data, d->data_received());
[email protected]699efe602011-01-25 07:17:112071 }
2072
[email protected]ede03212012-09-07 12:52:262073 bool DoManyCookiesRequest(int num_cookies) {
[email protected]263163f2012-06-14 22:40:342074 TestDelegate d;
2075 URLRequest r(test_server_.GetURL("set-many-cookies?" +
2076 base::IntToString(num_cookies)),
[email protected]94e2bbe2012-06-22 15:26:132077 &d,
2078 &default_context_);
[email protected]263163f2012-06-14 22:40:342079
2080 r.Start();
2081 EXPECT_TRUE(r.is_pending());
2082
2083 MessageLoop::current()->Run();
2084
2085 bool is_success = r.status().is_success();
2086
[email protected]ede03212012-09-07 12:52:262087 if (!is_success) {
[email protected]263163f2012-06-14 22:40:342088 // Requests handled by ChromeFrame send a less precise error message,
2089 // ERR_CONNECTION_ABORTED.
2090 EXPECT_TRUE(r.status().error() == ERR_RESPONSE_HEADERS_TOO_BIG ||
2091 r.status().error() == ERR_CONNECTION_ABORTED);
2092 // The test server appears to be unable to handle subsequent requests
2093 // after this error is triggered. Force it to restart.
2094 EXPECT_TRUE(test_server_.Stop());
2095 EXPECT_TRUE(test_server_.Start());
2096 }
2097
2098 return is_success;
2099 }
2100
[email protected]1700c6a2012-02-22 18:07:072101 LocalHttpTestServer test_server_;
[email protected]b89290212009-08-14 22:37:352102};
2103
[email protected]95409e12010-08-17 20:07:112104// In this unit test, we're using the HTTPTestServer as a proxy server and
2105// issuing a CONNECT request with the magic host name "www.redirect.com".
2106// The HTTPTestServer will return a 302 response, which we should not
2107// follow.
[email protected]f2f31b32013-01-16 23:24:092108TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) {
[email protected]95409e12010-08-17 20:07:112109 ASSERT_TRUE(test_server_.Start());
2110
[email protected]ceefd7fd2012-11-29 00:36:242111 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]d5a4dd62012-05-23 01:41:042112 TestURLRequestContextWithProxy context(
2113 test_server_.host_port_pair().ToString(),
2114 &network_delegate);
[email protected]87a09a92011-07-14 15:50:502115
[email protected]d1ec59082009-02-11 02:48:152116 TestDelegate d;
2117 {
[email protected]94e2bbe2012-06-22 15:26:132118 URLRequest r(GURL("https://www.redirect.com/"), &d, &context);
[email protected]d1ec59082009-02-11 02:48:152119 r.Start();
2120 EXPECT_TRUE(r.is_pending());
2121
2122 MessageLoop::current()->Run();
2123
[email protected]7461a402011-03-24 23:19:512124 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
[email protected]d0cc35b2011-09-08 12:02:052125 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().error());
[email protected]dc651782009-02-14 01:45:082126 EXPECT_EQ(1, d.response_started_count());
[email protected]d1ec59082009-02-11 02:48:152127 // We should not have followed the redirect.
2128 EXPECT_EQ(0, d.received_redirect_count());
2129 }
2130}
2131
[email protected]8202d0c2011-02-23 08:31:142132// This is the same as the previous test, but checks that the network delegate
2133// registers the error.
[email protected]c044616e2013-02-20 02:01:262134TEST_F(URLRequestTestHTTP, NetworkDelegateTunnelConnectionFailed) {
[email protected]8202d0c2011-02-23 08:31:142135 ASSERT_TRUE(test_server_.Start());
2136
[email protected]ceefd7fd2012-11-29 00:36:242137 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]d5a4dd62012-05-23 01:41:042138 TestURLRequestContextWithProxy context(
2139 test_server_.host_port_pair().ToString(),
2140 &network_delegate);
[email protected]87a09a92011-07-14 15:50:502141
[email protected]8202d0c2011-02-23 08:31:142142 TestDelegate d;
2143 {
[email protected]94e2bbe2012-06-22 15:26:132144 URLRequest r(GURL("https://www.redirect.com/"), &d, &context);
[email protected]8202d0c2011-02-23 08:31:142145 r.Start();
2146 EXPECT_TRUE(r.is_pending());
2147
2148 MessageLoop::current()->Run();
2149
[email protected]7461a402011-03-24 23:19:512150 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
[email protected]d0cc35b2011-09-08 12:02:052151 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().error());
[email protected]8202d0c2011-02-23 08:31:142152 EXPECT_EQ(1, d.response_started_count());
2153 // We should not have followed the redirect.
2154 EXPECT_EQ(0, d.received_redirect_count());
2155
2156 EXPECT_EQ(1, network_delegate.error_count());
[email protected]d0cc35b2011-09-08 12:02:052157 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, network_delegate.last_error());
[email protected]8202d0c2011-02-23 08:31:142158 }
2159}
2160
[email protected]dc5a5cf2012-09-26 02:49:302161// Tests that we can block and asynchronously return OK in various stages.
2162TEST_F(URLRequestTestHTTP, NetworkDelegateBlockAsynchronously) {
2163 static const BlockingNetworkDelegate::Stage blocking_stages[] = {
2164 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
2165 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
2166 BlockingNetworkDelegate::ON_HEADERS_RECEIVED
2167 };
2168 static const size_t blocking_stages_length = arraysize(blocking_stages);
2169
2170 ASSERT_TRUE(test_server_.Start());
2171
2172 TestDelegate d;
2173 BlockingNetworkDelegate network_delegate(
2174 BlockingNetworkDelegate::USER_CALLBACK);
2175 network_delegate.set_block_on(
2176 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST |
2177 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS |
2178 BlockingNetworkDelegate::ON_HEADERS_RECEIVED);
2179
2180 TestURLRequestContext context(true);
2181 context.set_network_delegate(&network_delegate);
2182 context.Init();
2183
2184 {
2185 URLRequest r(test_server_.GetURL("empty.html"), &d, &context);
2186
2187 r.Start();
2188 for (size_t i = 0; i < blocking_stages_length; ++i) {
2189 MessageLoop::current()->Run();
2190 EXPECT_EQ(blocking_stages[i],
2191 network_delegate.stage_blocked_for_callback());
2192 network_delegate.DoCallback(OK);
2193 }
2194 MessageLoop::current()->Run();
2195 EXPECT_EQ(200, r.GetResponseCode());
2196 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2197 EXPECT_EQ(1, network_delegate.created_requests());
2198 EXPECT_EQ(0, network_delegate.destroyed_requests());
2199 }
2200 EXPECT_EQ(1, network_delegate.destroyed_requests());
2201}
2202
[email protected]4c76d7c2011-04-15 19:14:122203// Tests that the network delegate can block and cancel a request.
2204TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) {
2205 ASSERT_TRUE(test_server_.Start());
2206
2207 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302208 BlockingNetworkDelegate network_delegate(
2209 BlockingNetworkDelegate::AUTO_CALLBACK);
2210 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
2211 network_delegate.set_retval(ERR_EMPTY_RESPONSE);
[email protected]4c76d7c2011-04-15 19:14:122212
[email protected]d5a4dd62012-05-23 01:41:042213 TestURLRequestContextWithProxy context(
2214 test_server_.host_port_pair().ToString(),
2215 &network_delegate);
[email protected]87a09a92011-07-14 15:50:502216
[email protected]4c76d7c2011-04-15 19:14:122217 {
[email protected]94e2bbe2012-06-22 15:26:132218 URLRequest r(test_server_.GetURL(""), &d, &context);
[email protected]4c76d7c2011-04-15 19:14:122219
2220 r.Start();
2221 MessageLoop::current()->Run();
2222
2223 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
[email protected]d0cc35b2011-09-08 12:02:052224 EXPECT_EQ(ERR_EMPTY_RESPONSE, r.status().error());
[email protected]4c76d7c2011-04-15 19:14:122225 EXPECT_EQ(1, network_delegate.created_requests());
2226 EXPECT_EQ(0, network_delegate.destroyed_requests());
2227 }
2228 EXPECT_EQ(1, network_delegate.destroyed_requests());
2229}
2230
[email protected]b4438d32012-09-27 06:15:302231// Helper function for NetworkDelegateCancelRequestAsynchronously and
2232// NetworkDelegateCancelRequestSynchronously. Sets up a blocking network
2233// delegate operating in |block_mode| and a request for |url|. It blocks the
2234// request in |stage| and cancels it with ERR_BLOCKED_BY_CLIENT.
2235void NetworkDelegateCancelRequest(BlockingNetworkDelegate::BlockMode block_mode,
2236 BlockingNetworkDelegate::Stage stage,
2237 const GURL& url) {
[email protected]3cd384c602011-08-31 16:12:362238 TestDelegate d;
[email protected]b4438d32012-09-27 06:15:302239 BlockingNetworkDelegate network_delegate(block_mode);
2240 network_delegate.set_retval(ERR_BLOCKED_BY_CLIENT);
2241 network_delegate.set_block_on(stage);
[email protected]3cd384c602011-08-31 16:12:362242
[email protected]b4438d32012-09-27 06:15:302243 TestURLRequestContext context(true);
2244 context.set_network_delegate(&network_delegate);
2245 context.Init();
[email protected]3cd384c602011-08-31 16:12:362246
2247 {
[email protected]b4438d32012-09-27 06:15:302248 URLRequest r(url, &d, &context);
[email protected]3cd384c602011-08-31 16:12:362249
2250 r.Start();
2251 MessageLoop::current()->Run();
2252
2253 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
[email protected]b4438d32012-09-27 06:15:302254 EXPECT_EQ(ERR_BLOCKED_BY_CLIENT, r.status().error());
[email protected]3cd384c602011-08-31 16:12:362255 EXPECT_EQ(1, network_delegate.created_requests());
2256 EXPECT_EQ(0, network_delegate.destroyed_requests());
2257 }
2258 EXPECT_EQ(1, network_delegate.destroyed_requests());
2259}
2260
[email protected]b4438d32012-09-27 06:15:302261// The following 3 tests check that the network delegate can cancel a request
2262// synchronously in various stages of the request.
2263TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously1) {
2264 ASSERT_TRUE(test_server_.Start());
2265 NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS,
2266 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
2267 test_server_.GetURL(""));
2268}
2269
2270TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously2) {
2271 ASSERT_TRUE(test_server_.Start());
2272 NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS,
2273 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
2274 test_server_.GetURL(""));
2275}
2276
2277TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously3) {
2278 ASSERT_TRUE(test_server_.Start());
2279 NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS,
2280 BlockingNetworkDelegate::ON_HEADERS_RECEIVED,
2281 test_server_.GetURL(""));
2282}
2283
2284// The following 3 tests check that the network delegate can cancel a request
2285// asynchronously in various stages of the request.
2286TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously1) {
2287 ASSERT_TRUE(test_server_.Start());
2288 NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK,
2289 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
2290 test_server_.GetURL(""));
2291}
2292
2293TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously2) {
2294 ASSERT_TRUE(test_server_.Start());
2295 NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK,
2296 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
2297 test_server_.GetURL(""));
2298}
2299
2300TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously3) {
2301 ASSERT_TRUE(test_server_.Start());
2302 NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK,
2303 BlockingNetworkDelegate::ON_HEADERS_RECEIVED,
2304 test_server_.GetURL(""));
2305}
2306
[email protected]4c76d7c2011-04-15 19:14:122307// Tests that the network delegate can block and redirect a request to a new
2308// URL.
2309TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) {
2310 ASSERT_TRUE(test_server_.Start());
2311
2312 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302313 BlockingNetworkDelegate network_delegate(
2314 BlockingNetworkDelegate::AUTO_CALLBACK);
2315 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
[email protected]4c76d7c2011-04-15 19:14:122316 GURL redirect_url(test_server_.GetURL("simple.html"));
2317 network_delegate.set_redirect_url(redirect_url);
2318
[email protected]d5a4dd62012-05-23 01:41:042319 TestURLRequestContextWithProxy context(
2320 test_server_.host_port_pair().ToString(),
2321 &network_delegate);
[email protected]87a09a92011-07-14 15:50:502322
[email protected]4c76d7c2011-04-15 19:14:122323 {
[email protected]8f1ac082011-04-19 21:14:132324 GURL original_url(test_server_.GetURL("empty.html"));
[email protected]94e2bbe2012-06-22 15:26:132325 URLRequest r(original_url, &d, &context);
[email protected]4c76d7c2011-04-15 19:14:122326
2327 r.Start();
2328 MessageLoop::current()->Run();
2329
2330 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
[email protected]d0cc35b2011-09-08 12:02:052331 EXPECT_EQ(0, r.status().error());
[email protected]4c76d7c2011-04-15 19:14:122332 EXPECT_EQ(redirect_url, r.url());
[email protected]8f1ac082011-04-19 21:14:132333 EXPECT_EQ(original_url, r.original_url());
2334 EXPECT_EQ(2U, r.url_chain().size());
[email protected]4c76d7c2011-04-15 19:14:122335 EXPECT_EQ(1, network_delegate.created_requests());
2336 EXPECT_EQ(0, network_delegate.destroyed_requests());
2337 }
2338 EXPECT_EQ(1, network_delegate.destroyed_requests());
2339}
2340
[email protected]b813ed72012-04-05 08:21:362341// Tests that the network delegate can block and redirect a request to a new
2342// URL by setting a redirect_url and returning in OnBeforeURLRequest directly.
2343TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) {
2344 ASSERT_TRUE(test_server_.Start());
2345
2346 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302347 BlockingNetworkDelegate network_delegate(
2348 BlockingNetworkDelegate::SYNCHRONOUS);
[email protected]b813ed72012-04-05 08:21:362349 GURL redirect_url(test_server_.GetURL("simple.html"));
2350 network_delegate.set_redirect_url(redirect_url);
[email protected]b813ed72012-04-05 08:21:362351
[email protected]d5a4dd62012-05-23 01:41:042352 TestURLRequestContextWithProxy context(
2353 test_server_.host_port_pair().ToString(),
2354 &network_delegate);
[email protected]b813ed72012-04-05 08:21:362355
2356 {
2357 GURL original_url(test_server_.GetURL("empty.html"));
[email protected]94e2bbe2012-06-22 15:26:132358 URLRequest r(original_url, &d, &context);
[email protected]b813ed72012-04-05 08:21:362359
2360 r.Start();
2361 MessageLoop::current()->Run();
2362
2363 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2364 EXPECT_EQ(0, r.status().error());
2365 EXPECT_EQ(redirect_url, r.url());
2366 EXPECT_EQ(original_url, r.original_url());
2367 EXPECT_EQ(2U, r.url_chain().size());
2368 EXPECT_EQ(1, network_delegate.created_requests());
2369 EXPECT_EQ(0, network_delegate.destroyed_requests());
2370 }
2371 EXPECT_EQ(1, network_delegate.destroyed_requests());
2372}
2373
[email protected]3c5ca8c2011-09-29 01:14:512374// Tests that redirects caused by the network delegate preserve POST data.
2375TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) {
2376 ASSERT_TRUE(test_server_.Start());
2377
2378 const char kData[] = "hello world";
2379
2380 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302381 BlockingNetworkDelegate network_delegate(
2382 BlockingNetworkDelegate::AUTO_CALLBACK);
2383 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
[email protected]3c5ca8c2011-09-29 01:14:512384 GURL redirect_url(test_server_.GetURL("echo"));
2385 network_delegate.set_redirect_url(redirect_url);
2386
[email protected]ef2bf422012-05-11 03:27:092387 TestURLRequestContext context(true);
2388 context.set_network_delegate(&network_delegate);
2389 context.Init();
[email protected]3c5ca8c2011-09-29 01:14:512390
2391 {
2392 GURL original_url(test_server_.GetURL("empty.html"));
[email protected]94e2bbe2012-06-22 15:26:132393 URLRequest r(original_url, &d, &context);
[email protected]3c5ca8c2011-09-29 01:14:512394 r.set_method("POST");
[email protected]f288ef02012-12-15 20:28:282395 r.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
[email protected]3c5ca8c2011-09-29 01:14:512396 HttpRequestHeaders headers;
2397 headers.SetHeader(HttpRequestHeaders::kContentLength,
2398 base::UintToString(arraysize(kData) - 1));
2399 r.SetExtraRequestHeaders(headers);
[email protected]3c5ca8c2011-09-29 01:14:512400 r.Start();
2401 MessageLoop::current()->Run();
2402
2403 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2404 EXPECT_EQ(0, r.status().error());
2405 EXPECT_EQ(redirect_url, r.url());
2406 EXPECT_EQ(original_url, r.original_url());
2407 EXPECT_EQ(2U, r.url_chain().size());
2408 EXPECT_EQ(1, network_delegate.created_requests());
2409 EXPECT_EQ(0, network_delegate.destroyed_requests());
2410 EXPECT_EQ("POST", r.method());
2411 EXPECT_EQ(kData, d.data_received());
2412 }
2413 EXPECT_EQ(1, network_delegate.destroyed_requests());
2414}
2415
[email protected]c2911d72011-10-03 22:16:362416// Tests that the network delegate can synchronously complete OnAuthRequired
2417// by taking no action. This indicates that the NetworkDelegate does not want to
2418// handle the challenge, and is passing the buck along to the
2419// URLRequest::Delegate.
2420TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) {
2421 ASSERT_TRUE(test_server_.Start());
2422
2423 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302424 BlockingNetworkDelegate network_delegate(
2425 BlockingNetworkDelegate::SYNCHRONOUS);
[email protected]c2911d72011-10-03 22:16:362426
[email protected]ef2bf422012-05-11 03:27:092427 TestURLRequestContext context(true);
2428 context.set_network_delegate(&network_delegate);
2429 context.Init();
[email protected]c2911d72011-10-03 22:16:362430
[email protected]f3cf9802011-10-28 18:44:582431 d.set_credentials(AuthCredentials(kUser, kSecret));
[email protected]c2911d72011-10-03 22:16:362432
2433 {
2434 GURL url(test_server_.GetURL("auth-basic"));
[email protected]94e2bbe2012-06-22 15:26:132435 URLRequest r(url, &d, &context);
[email protected]c2911d72011-10-03 22:16:362436 r.Start();
2437 MessageLoop::current()->Run();
2438
2439 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2440 EXPECT_EQ(0, r.status().error());
2441 EXPECT_EQ(200, r.GetResponseCode());
2442 EXPECT_TRUE(d.auth_required_called());
2443 EXPECT_EQ(1, network_delegate.created_requests());
2444 EXPECT_EQ(0, network_delegate.destroyed_requests());
2445 }
2446 EXPECT_EQ(1, network_delegate.destroyed_requests());
2447}
2448
2449// Tests that the network delegate can synchronously complete OnAuthRequired
2450// by setting credentials.
2451TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) {
2452 ASSERT_TRUE(test_server_.Start());
2453
2454 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302455 BlockingNetworkDelegate network_delegate(
2456 BlockingNetworkDelegate::SYNCHRONOUS);
2457 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]c2911d72011-10-03 22:16:362458 network_delegate.set_auth_retval(
2459 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
2460
[email protected]f3cf9802011-10-28 18:44:582461 network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret));
[email protected]c2911d72011-10-03 22:16:362462
[email protected]ef2bf422012-05-11 03:27:092463 TestURLRequestContext context(true);
2464 context.set_network_delegate(&network_delegate);
2465 context.Init();
[email protected]c2911d72011-10-03 22:16:362466
2467 {
2468 GURL url(test_server_.GetURL("auth-basic"));
[email protected]94e2bbe2012-06-22 15:26:132469 URLRequest r(url, &d, &context);
[email protected]c2911d72011-10-03 22:16:362470 r.Start();
2471 MessageLoop::current()->Run();
2472
2473 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2474 EXPECT_EQ(0, r.status().error());
2475 EXPECT_EQ(200, r.GetResponseCode());
2476 EXPECT_FALSE(d.auth_required_called());
2477 EXPECT_EQ(1, network_delegate.created_requests());
2478 EXPECT_EQ(0, network_delegate.destroyed_requests());
2479 }
2480 EXPECT_EQ(1, network_delegate.destroyed_requests());
2481}
2482
2483// Tests that the network delegate can synchronously complete OnAuthRequired
2484// by cancelling authentication.
2485TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) {
2486 ASSERT_TRUE(test_server_.Start());
2487
2488 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302489 BlockingNetworkDelegate network_delegate(
2490 BlockingNetworkDelegate::SYNCHRONOUS);
2491 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]c2911d72011-10-03 22:16:362492 network_delegate.set_auth_retval(
2493 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH);
2494
[email protected]ef2bf422012-05-11 03:27:092495 TestURLRequestContext context(true);
2496 context.set_network_delegate(&network_delegate);
2497 context.Init();
[email protected]c2911d72011-10-03 22:16:362498
2499 {
2500 GURL url(test_server_.GetURL("auth-basic"));
[email protected]94e2bbe2012-06-22 15:26:132501 URLRequest r(url, &d, &context);
[email protected]c2911d72011-10-03 22:16:362502 r.Start();
2503 MessageLoop::current()->Run();
2504
2505 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2506 EXPECT_EQ(OK, r.status().error());
2507 EXPECT_EQ(401, r.GetResponseCode());
2508 EXPECT_FALSE(d.auth_required_called());
2509 EXPECT_EQ(1, network_delegate.created_requests());
2510 EXPECT_EQ(0, network_delegate.destroyed_requests());
2511 }
2512 EXPECT_EQ(1, network_delegate.destroyed_requests());
2513}
2514
2515// Tests that the network delegate can asynchronously complete OnAuthRequired
2516// by taking no action. This indicates that the NetworkDelegate does not want
2517// to handle the challenge, and is passing the buck along to the
2518// URLRequest::Delegate.
2519TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) {
2520 ASSERT_TRUE(test_server_.Start());
2521
2522 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302523 BlockingNetworkDelegate network_delegate(
2524 BlockingNetworkDelegate::AUTO_CALLBACK);
2525 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]c2911d72011-10-03 22:16:362526
[email protected]ef2bf422012-05-11 03:27:092527 TestURLRequestContext context(true);
2528 context.set_network_delegate(&network_delegate);
2529 context.Init();
[email protected]c2911d72011-10-03 22:16:362530
[email protected]f3cf9802011-10-28 18:44:582531 d.set_credentials(AuthCredentials(kUser, kSecret));
[email protected]c2911d72011-10-03 22:16:362532
2533 {
2534 GURL url(test_server_.GetURL("auth-basic"));
[email protected]94e2bbe2012-06-22 15:26:132535 URLRequest r(url, &d, &context);
[email protected]c2911d72011-10-03 22:16:362536 r.Start();
2537 MessageLoop::current()->Run();
2538
2539 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2540 EXPECT_EQ(0, r.status().error());
2541 EXPECT_EQ(200, r.GetResponseCode());
2542 EXPECT_TRUE(d.auth_required_called());
2543 EXPECT_EQ(1, network_delegate.created_requests());
2544 EXPECT_EQ(0, network_delegate.destroyed_requests());
2545 }
2546 EXPECT_EQ(1, network_delegate.destroyed_requests());
2547}
2548
2549// Tests that the network delegate can asynchronously complete OnAuthRequired
2550// by setting credentials.
2551TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) {
2552 ASSERT_TRUE(test_server_.Start());
2553
2554 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302555 BlockingNetworkDelegate network_delegate(
2556 BlockingNetworkDelegate::AUTO_CALLBACK);
2557 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]c2911d72011-10-03 22:16:362558 network_delegate.set_auth_retval(
[email protected]c2911d72011-10-03 22:16:362559 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
2560
[email protected]f3cf9802011-10-28 18:44:582561 AuthCredentials auth_credentials(kUser, kSecret);
[email protected]c2911d72011-10-03 22:16:362562 network_delegate.set_auth_credentials(auth_credentials);
2563
[email protected]ef2bf422012-05-11 03:27:092564 TestURLRequestContext context(true);
2565 context.set_network_delegate(&network_delegate);
2566 context.Init();
[email protected]c2911d72011-10-03 22:16:362567
2568 {
2569 GURL url(test_server_.GetURL("auth-basic"));
[email protected]94e2bbe2012-06-22 15:26:132570 URLRequest r(url, &d, &context);
[email protected]c2911d72011-10-03 22:16:362571 r.Start();
2572 MessageLoop::current()->Run();
2573
2574 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2575 EXPECT_EQ(0, r.status().error());
2576
2577 EXPECT_EQ(200, r.GetResponseCode());
2578 EXPECT_FALSE(d.auth_required_called());
2579 EXPECT_EQ(1, network_delegate.created_requests());
2580 EXPECT_EQ(0, network_delegate.destroyed_requests());
2581 }
2582 EXPECT_EQ(1, network_delegate.destroyed_requests());
2583}
2584
2585// Tests that the network delegate can asynchronously complete OnAuthRequired
2586// by cancelling authentication.
2587TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) {
2588 ASSERT_TRUE(test_server_.Start());
2589
2590 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302591 BlockingNetworkDelegate network_delegate(
2592 BlockingNetworkDelegate::AUTO_CALLBACK);
2593 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]c2911d72011-10-03 22:16:362594 network_delegate.set_auth_retval(
[email protected]c2911d72011-10-03 22:16:362595 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH);
2596
[email protected]ef2bf422012-05-11 03:27:092597 TestURLRequestContext context(true);
2598 context.set_network_delegate(&network_delegate);
2599 context.Init();
[email protected]c2911d72011-10-03 22:16:362600
2601 {
2602 GURL url(test_server_.GetURL("auth-basic"));
[email protected]94e2bbe2012-06-22 15:26:132603 URLRequest r(url, &d, &context);
[email protected]c2911d72011-10-03 22:16:362604 r.Start();
2605 MessageLoop::current()->Run();
2606
2607 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2608 EXPECT_EQ(OK, r.status().error());
2609 EXPECT_EQ(401, r.GetResponseCode());
2610 EXPECT_FALSE(d.auth_required_called());
2611 EXPECT_EQ(1, network_delegate.created_requests());
2612 EXPECT_EQ(0, network_delegate.destroyed_requests());
2613 }
2614 EXPECT_EQ(1, network_delegate.destroyed_requests());
2615}
2616
[email protected]9045b8822012-01-13 20:35:352617// Tests that we can handle when a network request was canceled while we were
2618// waiting for the network delegate.
2619// Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback.
2620TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) {
2621 ASSERT_TRUE(test_server_.Start());
2622
2623 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302624 BlockingNetworkDelegate network_delegate(
2625 BlockingNetworkDelegate::USER_CALLBACK);
2626 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
[email protected]9045b8822012-01-13 20:35:352627
[email protected]ef2bf422012-05-11 03:27:092628 TestURLRequestContext context(true);
2629 context.set_network_delegate(&network_delegate);
2630 context.Init();
[email protected]9045b8822012-01-13 20:35:352631
2632 {
[email protected]94e2bbe2012-06-22 15:26:132633 URLRequest r(test_server_.GetURL(""), &d, &context);
[email protected]9045b8822012-01-13 20:35:352634
2635 r.Start();
[email protected]dc5a5cf2012-09-26 02:49:302636 MessageLoop::current()->Run();
2637 EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
2638 network_delegate.stage_blocked_for_callback());
[email protected]9045b8822012-01-13 20:35:352639 EXPECT_EQ(0, network_delegate.completed_requests());
2640 // Cancel before callback.
2641 r.Cancel();
2642 // Ensure that network delegate is notified.
2643 EXPECT_EQ(1, network_delegate.completed_requests());
2644 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
2645 EXPECT_EQ(ERR_ABORTED, r.status().error());
2646 EXPECT_EQ(1, network_delegate.created_requests());
2647 EXPECT_EQ(0, network_delegate.destroyed_requests());
2648 }
2649 EXPECT_EQ(1, network_delegate.destroyed_requests());
2650}
2651
2652// Tests that we can handle when a network request was canceled while we were
2653// waiting for the network delegate.
2654// Part 2: Request is cancelled while waiting for OnBeforeSendHeaders callback.
2655TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) {
2656 ASSERT_TRUE(test_server_.Start());
2657
2658 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302659 BlockingNetworkDelegate network_delegate(
2660 BlockingNetworkDelegate::USER_CALLBACK);
2661 network_delegate.set_block_on(
2662 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS);
[email protected]9045b8822012-01-13 20:35:352663
[email protected]ef2bf422012-05-11 03:27:092664 TestURLRequestContext context(true);
2665 context.set_network_delegate(&network_delegate);
2666 context.Init();
[email protected]9045b8822012-01-13 20:35:352667
2668 {
[email protected]94e2bbe2012-06-22 15:26:132669 URLRequest r(test_server_.GetURL(""), &d, &context);
[email protected]9045b8822012-01-13 20:35:352670
2671 r.Start();
[email protected]dc5a5cf2012-09-26 02:49:302672 MessageLoop::current()->Run();
2673 EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
2674 network_delegate.stage_blocked_for_callback());
[email protected]9045b8822012-01-13 20:35:352675 EXPECT_EQ(0, network_delegate.completed_requests());
2676 // Cancel before callback.
2677 r.Cancel();
2678 // Ensure that network delegate is notified.
2679 EXPECT_EQ(1, network_delegate.completed_requests());
2680 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
2681 EXPECT_EQ(ERR_ABORTED, r.status().error());
2682 EXPECT_EQ(1, network_delegate.created_requests());
2683 EXPECT_EQ(0, network_delegate.destroyed_requests());
2684 }
2685 EXPECT_EQ(1, network_delegate.destroyed_requests());
2686}
2687
2688// Tests that we can handle when a network request was canceled while we were
2689// waiting for the network delegate.
2690// Part 3: Request is cancelled while waiting for OnHeadersReceived callback.
2691TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) {
2692 ASSERT_TRUE(test_server_.Start());
2693
2694 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302695 BlockingNetworkDelegate network_delegate(
2696 BlockingNetworkDelegate::USER_CALLBACK);
2697 network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED);
[email protected]9045b8822012-01-13 20:35:352698
[email protected]ef2bf422012-05-11 03:27:092699 TestURLRequestContext context(true);
2700 context.set_network_delegate(&network_delegate);
2701 context.Init();
[email protected]9045b8822012-01-13 20:35:352702
2703 {
[email protected]94e2bbe2012-06-22 15:26:132704 URLRequest r(test_server_.GetURL(""), &d, &context);
[email protected]9045b8822012-01-13 20:35:352705
2706 r.Start();
[email protected]dc5a5cf2012-09-26 02:49:302707 MessageLoop::current()->Run();
2708 EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED,
2709 network_delegate.stage_blocked_for_callback());
[email protected]9045b8822012-01-13 20:35:352710 EXPECT_EQ(0, network_delegate.completed_requests());
2711 // Cancel before callback.
2712 r.Cancel();
2713 // Ensure that network delegate is notified.
2714 EXPECT_EQ(1, network_delegate.completed_requests());
2715 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
2716 EXPECT_EQ(ERR_ABORTED, r.status().error());
2717 EXPECT_EQ(1, network_delegate.created_requests());
2718 EXPECT_EQ(0, network_delegate.destroyed_requests());
2719 }
2720 EXPECT_EQ(1, network_delegate.destroyed_requests());
2721}
2722
2723// Tests that we can handle when a network request was canceled while we were
2724// waiting for the network delegate.
2725// Part 4: Request is cancelled while waiting for OnAuthRequired callback.
[email protected]bfe8b302013-02-15 12:16:022726TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) {
[email protected]9045b8822012-01-13 20:35:352727 ASSERT_TRUE(test_server_.Start());
2728
2729 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302730 BlockingNetworkDelegate network_delegate(
2731 BlockingNetworkDelegate::USER_CALLBACK);
2732 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]9045b8822012-01-13 20:35:352733
[email protected]ef2bf422012-05-11 03:27:092734 TestURLRequestContext context(true);
2735 context.set_network_delegate(&network_delegate);
2736 context.Init();
[email protected]9045b8822012-01-13 20:35:352737
2738 {
[email protected]94e2bbe2012-06-22 15:26:132739 URLRequest r(test_server_.GetURL("auth-basic"), &d, &context);
[email protected]9045b8822012-01-13 20:35:352740
2741 r.Start();
[email protected]dc5a5cf2012-09-26 02:49:302742 MessageLoop::current()->Run();
2743 EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED,
2744 network_delegate.stage_blocked_for_callback());
[email protected]9045b8822012-01-13 20:35:352745 EXPECT_EQ(0, network_delegate.completed_requests());
2746 // Cancel before callback.
2747 r.Cancel();
2748 // Ensure that network delegate is notified.
2749 EXPECT_EQ(1, network_delegate.completed_requests());
2750 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
2751 EXPECT_EQ(ERR_ABORTED, r.status().error());
2752 EXPECT_EQ(1, network_delegate.created_requests());
2753 EXPECT_EQ(0, network_delegate.destroyed_requests());
2754 }
2755 EXPECT_EQ(1, network_delegate.destroyed_requests());
2756}
2757
[email protected]95409e12010-08-17 20:07:112758// In this unit test, we're using the HTTPTestServer as a proxy server and
2759// issuing a CONNECT request with the magic host name "www.server-auth.com".
2760// The HTTPTestServer will return a 401 response, which we should balk at.
[email protected]b89290212009-08-14 22:37:352761TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) {
[email protected]95409e12010-08-17 20:07:112762 ASSERT_TRUE(test_server_.Start());
2763
[email protected]ceefd7fd2012-11-29 00:36:242764 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]d5a4dd62012-05-23 01:41:042765 TestURLRequestContextWithProxy context(
2766 test_server_.host_port_pair().ToString(),
2767 &network_delegate);
[email protected]87a09a92011-07-14 15:50:502768
[email protected]dc651782009-02-14 01:45:082769 TestDelegate d;
2770 {
[email protected]94e2bbe2012-06-22 15:26:132771 URLRequest r(GURL("https://www.server-auth.com/"), &d, &context);
[email protected]dc651782009-02-14 01:45:082772
2773 r.Start();
2774 EXPECT_TRUE(r.is_pending());
2775
2776 MessageLoop::current()->Run();
2777
[email protected]7461a402011-03-24 23:19:512778 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
[email protected]d0cc35b2011-09-08 12:02:052779 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().error());
[email protected]dc651782009-02-14 01:45:082780 }
2781}
2782
[email protected]b89290212009-08-14 22:37:352783TEST_F(URLRequestTestHTTP, GetTest_NoCache) {
[email protected]95409e12010-08-17 20:07:112784 ASSERT_TRUE(test_server_.Start());
2785
initial.commit586acc5fe2008-07-26 22:42:522786 TestDelegate d;
2787 {
[email protected]94e2bbe2012-06-22 15:26:132788 URLRequest r(test_server_.GetURL(""), &d, &default_context_);
initial.commit586acc5fe2008-07-26 22:42:522789
2790 r.Start();
2791 EXPECT_TRUE(r.is_pending());
2792
2793 MessageLoop::current()->Run();
2794
2795 EXPECT_EQ(1, d.response_started_count());
2796 EXPECT_FALSE(d.received_data_before_response());
2797 EXPECT_NE(0, d.bytes_received());
[email protected]6d81b482011-02-22 19:47:192798 EXPECT_EQ(test_server_.host_port_pair().host(),
2799 r.GetSocketAddress().host());
2800 EXPECT_EQ(test_server_.host_port_pair().port(),
2801 r.GetSocketAddress().port());
[email protected]c31a54592009-09-04 02:36:162802
[email protected]9e743cd2010-03-16 07:03:532803 // TODO(eroman): Add back the NetLog tests...
initial.commit586acc5fe2008-07-26 22:42:522804 }
initial.commit586acc5fe2008-07-26 22:42:522805}
2806
[email protected]263163f2012-06-14 22:40:342807// This test has the server send a large number of cookies to the client.
2808// To ensure that no number of cookies causes a crash, a galloping binary
2809// search is used to estimate that maximum number of cookies that are accepted
2810// by the browser. Beyond the maximum number, the request will fail with
2811// ERR_RESPONSE_HEADERS_TOO_BIG.
2812TEST_F(URLRequestTestHTTP, GetTest_ManyCookies) {
2813 ASSERT_TRUE(test_server_.Start());
2814
2815 int lower_bound = 0;
2816 int upper_bound = 1;
2817
2818 // Double the number of cookies until the response header limits are
2819 // exceeded.
2820 while (DoManyCookiesRequest(upper_bound)) {
2821 lower_bound = upper_bound;
2822 upper_bound *= 2;
2823 ASSERT_LT(upper_bound, 1000000);
2824 }
2825
2826 int tolerance = upper_bound * 0.005;
2827 if (tolerance < 2)
2828 tolerance = 2;
2829
2830 // Perform a binary search to find the highest possible number of cookies,
2831 // within the desired tolerance.
2832 while (upper_bound - lower_bound >= tolerance) {
2833 int num_cookies = (lower_bound + upper_bound) / 2;
2834
2835 if (DoManyCookiesRequest(num_cookies))
2836 lower_bound = num_cookies;
2837 else
2838 upper_bound = num_cookies;
2839 }
2840 // Success: the test did not crash.
2841}
2842
[email protected]b89290212009-08-14 22:37:352843TEST_F(URLRequestTestHTTP, GetTest) {
[email protected]95409e12010-08-17 20:07:112844 ASSERT_TRUE(test_server_.Start());
2845
initial.commit586acc5fe2008-07-26 22:42:522846 TestDelegate d;
2847 {
[email protected]94e2bbe2012-06-22 15:26:132848 URLRequest r(test_server_.GetURL(""), &d, &default_context_);
initial.commit586acc5fe2008-07-26 22:42:522849
2850 r.Start();
2851 EXPECT_TRUE(r.is_pending());
2852
2853 MessageLoop::current()->Run();
2854
2855 EXPECT_EQ(1, d.response_started_count());
2856 EXPECT_FALSE(d.received_data_before_response());
2857 EXPECT_NE(0, d.bytes_received());
[email protected]6d81b482011-02-22 19:47:192858 EXPECT_EQ(test_server_.host_port_pair().host(),
2859 r.GetSocketAddress().host());
2860 EXPECT_EQ(test_server_.host_port_pair().port(),
2861 r.GetSocketAddress().port());
initial.commit586acc5fe2008-07-26 22:42:522862 }
[email protected]5d7b373e2009-09-02 07:19:032863}
2864
[email protected]58e32bb2013-01-21 18:23:252865TEST_F(URLRequestTestHTTP, GetTestLoadTiming) {
2866 ASSERT_TRUE(test_server_.Start());
2867
2868 TestDelegate d;
2869 {
2870 URLRequest r(test_server_.GetURL(""), &d, &default_context_);
2871
2872 r.Start();
2873 EXPECT_TRUE(r.is_pending());
2874
2875 MessageLoop::current()->Run();
2876
2877 LoadTimingInfo load_timing_info;
[email protected]0a703912013-02-02 04:25:172878 r.GetLoadTimingInfo(&load_timing_info);
[email protected]58e32bb2013-01-21 18:23:252879 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
2880
2881 EXPECT_EQ(1, d.response_started_count());
2882 EXPECT_FALSE(d.received_data_before_response());
2883 EXPECT_NE(0, d.bytes_received());
2884 EXPECT_EQ(test_server_.host_port_pair().host(),
2885 r.GetSocketAddress().host());
2886 EXPECT_EQ(test_server_.host_port_pair().port(),
2887 r.GetSocketAddress().port());
2888 }
2889}
2890
[email protected]aad63572011-05-24 20:14:392891TEST_F(URLRequestTestHTTP, GetZippedTest) {
2892 ASSERT_TRUE(test_server_.Start());
2893
2894 // Parameter that specifies the Content-Length field in the response:
2895 // C - Compressed length.
2896 // U - Uncompressed length.
2897 // L - Large length (larger than both C & U).
2898 // M - Medium length (between C & U).
2899 // S - Small length (smaller than both C & U).
2900 const char test_parameters[] = "CULMS";
2901 const int num_tests = arraysize(test_parameters)- 1; // Skip NULL.
2902 // C & U should be OK.
[email protected]f0e2bf42011-07-22 21:21:442903 // L & M are larger than the data sent, and show an error.
[email protected]aad63572011-05-24 20:14:392904 // S has too little data, but we seem to accept it.
2905 const bool test_expect_success[num_tests] =
[email protected]f001bd6a2011-12-08 04:31:372906 { true, true, false, false, true };
[email protected]aad63572011-05-24 20:14:392907
2908 for (int i = 0; i < num_tests ; i++) {
2909 TestDelegate d;
2910 {
2911 std::string test_file =
2912 base::StringPrintf("compressedfiles/BullRunSpeech.txt?%c",
2913 test_parameters[i]);
[email protected]aad63572011-05-24 20:14:392914
[email protected]ceefd7fd2012-11-29 00:36:242915 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]ef2bf422012-05-11 03:27:092916 TestURLRequestContext context(true);
2917 context.set_network_delegate(&network_delegate);
2918 context.Init();
[email protected]87a09a92011-07-14 15:50:502919
[email protected]94e2bbe2012-06-22 15:26:132920 URLRequest r(test_server_.GetURL(test_file), &d, &context);
[email protected]aad63572011-05-24 20:14:392921 r.Start();
2922 EXPECT_TRUE(r.is_pending());
2923
2924 MessageLoop::current()->Run();
2925
2926 EXPECT_EQ(1, d.response_started_count());
2927 EXPECT_FALSE(d.received_data_before_response());
2928 VLOG(1) << " Received " << d.bytes_received() << " bytes"
2929 << " status = " << r.status().status()
[email protected]d0cc35b2011-09-08 12:02:052930 << " error = " << r.status().error();
[email protected]aad63572011-05-24 20:14:392931 if (test_expect_success[i]) {
2932 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status())
2933 << " Parameter = \"" << test_file << "\"";
2934 } else {
2935 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
[email protected]5543cbb2012-04-20 16:35:232936 EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, r.status().error())
[email protected]aad63572011-05-24 20:14:392937 << " Parameter = \"" << test_file << "\"";
2938 }
2939 }
2940 }
2941}
2942
[email protected]c044616e2013-02-20 02:01:262943TEST_F(URLRequestTestHTTP, HTTPSToHTTPRedirectNoRefererTest) {
[email protected]95409e12010-08-17 20:07:112944 ASSERT_TRUE(test_server_.Start());
2945
[email protected]6cdfd7f2013-02-08 20:40:152946 TestServer https_test_server(
2947 TestServer::TYPE_HTTPS, TestServer::kLocalhost,
2948 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]95409e12010-08-17 20:07:112949 ASSERT_TRUE(https_test_server.Start());
[email protected]7844480a2009-12-16 21:18:582950
2951 // An https server is sent a request with an https referer,
2952 // and responds with a redirect to an http url. The http
2953 // server should not be sent the referer.
[email protected]95409e12010-08-17 20:07:112954 GURL http_destination = test_server_.GetURL("");
[email protected]7844480a2009-12-16 21:18:582955 TestDelegate d;
[email protected]ef2bf422012-05-11 03:27:092956 URLRequest req(https_test_server.GetURL(
[email protected]94e2bbe2012-06-22 15:26:132957 "server-redirect?" + http_destination.spec()), &d, &default_context_);
[email protected]7844480a2009-12-16 21:18:582958 req.set_referrer("https://www.referrer.com/");
2959 req.Start();
2960 MessageLoop::current()->Run();
2961
2962 EXPECT_EQ(1, d.response_started_count());
2963 EXPECT_EQ(1, d.received_redirect_count());
2964 EXPECT_EQ(http_destination, req.url());
2965 EXPECT_EQ(std::string(), req.referrer());
2966}
2967
[email protected]58e32bb2013-01-21 18:23:252968TEST_F(URLRequestTestHTTP, RedirectLoadTiming) {
2969 ASSERT_TRUE(test_server_.Start());
2970
2971 GURL destination_url = test_server_.GetURL("");
2972 GURL original_url = test_server_.GetURL(
2973 "server-redirect?" + destination_url.spec());
2974 TestDelegate d;
2975 URLRequest req(original_url, &d, &default_context_);
2976 req.Start();
2977 MessageLoop::current()->Run();
2978
2979 EXPECT_EQ(1, d.response_started_count());
2980 EXPECT_EQ(1, d.received_redirect_count());
2981 EXPECT_EQ(destination_url, req.url());
2982 EXPECT_EQ(original_url, req.original_url());
2983 ASSERT_EQ(2U, req.url_chain().size());
2984 EXPECT_EQ(original_url, req.url_chain()[0]);
2985 EXPECT_EQ(destination_url, req.url_chain()[1]);
2986
2987 LoadTimingInfo load_timing_info_before_redirect;
2988 EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeRedirect(
2989 &load_timing_info_before_redirect));
2990 TestLoadTimingNotReused(load_timing_info_before_redirect,
2991 CONNECT_TIMING_HAS_DNS_TIMES);
2992
2993 LoadTimingInfo load_timing_info;
[email protected]0a703912013-02-02 04:25:172994 req.GetLoadTimingInfo(&load_timing_info);
[email protected]58e32bb2013-01-21 18:23:252995 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
2996
2997 // Check that a new socket was used on redirect, since the server does not
2998 // supposed keep-alive sockets, and that the times before the redirect are
2999 // before the ones recorded for the second request.
3000 EXPECT_NE(load_timing_info_before_redirect.socket_log_id,
3001 load_timing_info.socket_log_id);
3002 EXPECT_LE(load_timing_info_before_redirect.receive_headers_end,
3003 load_timing_info.connect_timing.connect_start);
3004}
3005
[email protected]8f1ac082011-04-19 21:14:133006TEST_F(URLRequestTestHTTP, MultipleRedirectTest) {
3007 ASSERT_TRUE(test_server_.Start());
3008
3009 GURL destination_url = test_server_.GetURL("");
3010 GURL middle_redirect_url = test_server_.GetURL(
3011 "server-redirect?" + destination_url.spec());
3012 GURL original_url = test_server_.GetURL(
3013 "server-redirect?" + middle_redirect_url.spec());
3014 TestDelegate d;
[email protected]94e2bbe2012-06-22 15:26:133015 URLRequest req(original_url, &d, &default_context_);
[email protected]8f1ac082011-04-19 21:14:133016 req.Start();
3017 MessageLoop::current()->Run();
3018
3019 EXPECT_EQ(1, d.response_started_count());
3020 EXPECT_EQ(2, d.received_redirect_count());
3021 EXPECT_EQ(destination_url, req.url());
3022 EXPECT_EQ(original_url, req.original_url());
3023 ASSERT_EQ(3U, req.url_chain().size());
3024 EXPECT_EQ(original_url, req.url_chain()[0]);
3025 EXPECT_EQ(middle_redirect_url, req.url_chain()[1]);
3026 EXPECT_EQ(destination_url, req.url_chain()[2]);
3027}
3028
[email protected]847c0fa92012-11-06 16:37:423029namespace {
3030
3031const char kExtraHeader[] = "Allow-Snafu";
3032const char kExtraValue[] = "fubar";
3033
3034class RedirectWithAdditionalHeadersDelegate : public TestDelegate {
[email protected]46fadfd2013-02-06 09:40:163035 virtual void OnReceivedRedirect(net::URLRequest* request,
3036 const GURL& new_url,
3037 bool* defer_redirect) OVERRIDE {
[email protected]847c0fa92012-11-06 16:37:423038 TestDelegate::OnReceivedRedirect(request, new_url, defer_redirect);
3039 request->SetExtraRequestHeaderByName(kExtraHeader, kExtraValue, false);
3040 }
3041};
3042
3043} // namespace
3044
3045TEST_F(URLRequestTestHTTP, RedirectWithAdditionalHeadersTest) {
3046 ASSERT_TRUE(test_server_.Start());
3047
3048 GURL destination_url = test_server_.GetURL(
3049 "echoheader?" + std::string(kExtraHeader));
3050 GURL original_url = test_server_.GetURL(
3051 "server-redirect?" + destination_url.spec());
3052 RedirectWithAdditionalHeadersDelegate d;
3053 URLRequest req(original_url, &d, &default_context_);
3054 req.Start();
3055 MessageLoop::current()->Run();
3056
3057 std::string value;
3058 const HttpRequestHeaders& headers = req.extra_request_headers();
3059 EXPECT_TRUE(headers.GetHeader(kExtraHeader, &value));
3060 EXPECT_EQ(kExtraValue, value);
3061 EXPECT_FALSE(req.is_pending());
3062 EXPECT_FALSE(req.is_redirecting());
3063 EXPECT_EQ(kExtraValue, d.data_received());
3064}
3065
[email protected]251a1b92012-11-13 11:01:093066namespace {
3067
3068const char kExtraHeaderToRemove[] = "To-Be-Removed";
3069
3070class RedirectWithHeaderRemovalDelegate : public TestDelegate {
[email protected]46fadfd2013-02-06 09:40:163071 virtual void OnReceivedRedirect(net::URLRequest* request,
[email protected]251a1b92012-11-13 11:01:093072 const GURL& new_url,
[email protected]46fadfd2013-02-06 09:40:163073 bool* defer_redirect) OVERRIDE {
[email protected]251a1b92012-11-13 11:01:093074 TestDelegate::OnReceivedRedirect(request, new_url, defer_redirect);
3075 request->RemoveRequestHeaderByName(kExtraHeaderToRemove);
3076 }
3077};
3078
3079} // namespace
3080
3081TEST_F(URLRequestTestHTTP, RedirectWithHeaderRemovalTest) {
3082 ASSERT_TRUE(test_server_.Start());
3083
3084 GURL destination_url = test_server_.GetURL(
3085 "echoheader?" + std::string(kExtraHeaderToRemove));
3086 GURL original_url = test_server_.GetURL(
3087 "server-redirect?" + destination_url.spec());
3088 RedirectWithHeaderRemovalDelegate d;
3089 URLRequest req(original_url, &d, &default_context_);
3090 req.SetExtraRequestHeaderByName(kExtraHeaderToRemove, "dummy", false);
3091 req.Start();
3092 MessageLoop::current()->Run();
3093
3094 std::string value;
3095 const HttpRequestHeaders& headers = req.extra_request_headers();
3096 EXPECT_FALSE(headers.GetHeader(kExtraHeaderToRemove, &value));
3097 EXPECT_FALSE(req.is_pending());
3098 EXPECT_FALSE(req.is_redirecting());
3099 EXPECT_EQ("None", d.data_received());
3100}
3101
[email protected]316c1e5e2012-09-12 15:17:443102TEST_F(URLRequestTestHTTP, CancelTest) {
3103 TestDelegate d;
3104 {
3105 URLRequest r(GURL("http://www.google.com/"), &d, &default_context_);
3106
3107 r.Start();
3108 EXPECT_TRUE(r.is_pending());
3109
3110 r.Cancel();
3111
3112 MessageLoop::current()->Run();
3113
3114 // We expect to receive OnResponseStarted even though the request has been
3115 // cancelled.
3116 EXPECT_EQ(1, d.response_started_count());
3117 EXPECT_EQ(0, d.bytes_received());
3118 EXPECT_FALSE(d.received_data_before_response());
3119 }
3120}
3121
3122TEST_F(URLRequestTestHTTP, CancelTest2) {
3123 ASSERT_TRUE(test_server_.Start());
3124
3125 TestDelegate d;
3126 {
3127 URLRequest r(test_server_.GetURL(""), &d, &default_context_);
3128
3129 d.set_cancel_in_response_started(true);
3130
3131 r.Start();
3132 EXPECT_TRUE(r.is_pending());
3133
3134 MessageLoop::current()->Run();
3135
3136 EXPECT_EQ(1, d.response_started_count());
3137 EXPECT_EQ(0, d.bytes_received());
3138 EXPECT_FALSE(d.received_data_before_response());
3139 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
3140 }
3141}
3142
3143TEST_F(URLRequestTestHTTP, CancelTest3) {
3144 ASSERT_TRUE(test_server_.Start());
3145
3146 TestDelegate d;
3147 {
3148 URLRequest r(test_server_.GetURL(""), &d, &default_context_);
3149
3150 d.set_cancel_in_received_data(true);
3151
3152 r.Start();
3153 EXPECT_TRUE(r.is_pending());
3154
3155 MessageLoop::current()->Run();
3156
3157 EXPECT_EQ(1, d.response_started_count());
3158 // There is no guarantee about how much data was received
3159 // before the cancel was issued. It could have been 0 bytes,
3160 // or it could have been all the bytes.
3161 // EXPECT_EQ(0, d.bytes_received());
3162 EXPECT_FALSE(d.received_data_before_response());
3163 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
3164 }
3165}
3166
3167TEST_F(URLRequestTestHTTP, CancelTest4) {
3168 ASSERT_TRUE(test_server_.Start());
3169
3170 TestDelegate d;
3171 {
3172 URLRequest r(test_server_.GetURL(""), &d, &default_context_);
3173
3174 r.Start();
3175 EXPECT_TRUE(r.is_pending());
3176
3177 // The request will be implicitly canceled when it is destroyed. The
3178 // test delegate must not post a quit message when this happens because
3179 // this test doesn't actually have a message loop. The quit message would
3180 // get put on this thread's message queue and the next test would exit
3181 // early, causing problems.
3182 d.set_quit_on_complete(false);
3183 }
3184 // expect things to just cleanup properly.
3185
3186 // we won't actually get a received reponse here because we've never run the
3187 // message loop
3188 EXPECT_FALSE(d.received_data_before_response());
3189 EXPECT_EQ(0, d.bytes_received());
3190}
3191
3192TEST_F(URLRequestTestHTTP, CancelTest5) {
3193 ASSERT_TRUE(test_server_.Start());
3194
3195 // populate cache
3196 {
3197 TestDelegate d;
3198 URLRequest r(test_server_.GetURL("cachetime"), &d, &default_context_);
3199 r.Start();
3200 MessageLoop::current()->Run();
3201 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3202 }
3203
3204 // cancel read from cache (see bug 990242)
3205 {
3206 TestDelegate d;
3207 URLRequest r(test_server_.GetURL("cachetime"), &d, &default_context_);
3208 r.Start();
3209 r.Cancel();
3210 MessageLoop::current()->Run();
3211
3212 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
3213 EXPECT_EQ(1, d.response_started_count());
3214 EXPECT_EQ(0, d.bytes_received());
3215 EXPECT_FALSE(d.received_data_before_response());
3216 }
3217}
3218
3219TEST_F(URLRequestTestHTTP, PostTest) {
3220 ASSERT_TRUE(test_server_.Start());
3221 HTTPUploadDataOperationTest("POST");
3222}
3223
3224TEST_F(URLRequestTestHTTP, PutTest) {
3225 ASSERT_TRUE(test_server_.Start());
3226 HTTPUploadDataOperationTest("PUT");
3227}
3228
3229TEST_F(URLRequestTestHTTP, PostEmptyTest) {
3230 ASSERT_TRUE(test_server_.Start());
3231
3232 TestDelegate d;
3233 {
3234 URLRequest r(test_server_.GetURL("echo"), &d, &default_context_);
3235 r.set_method("POST");
3236
3237 r.Start();
3238 EXPECT_TRUE(r.is_pending());
3239
3240 MessageLoop::current()->Run();
3241
[email protected]329b68b2012-11-14 17:54:273242 ASSERT_EQ(1, d.response_started_count())
3243 << "request failed: " << r.status().status()
3244 << ", error: " << r.status().error();
[email protected]316c1e5e2012-09-12 15:17:443245
3246 EXPECT_FALSE(d.received_data_before_response());
3247 EXPECT_TRUE(d.data_received().empty());
3248 }
3249}
3250
3251TEST_F(URLRequestTestHTTP, PostFileTest) {
3252 ASSERT_TRUE(test_server_.Start());
3253
3254 TestDelegate d;
3255 {
3256 URLRequest r(test_server_.GetURL("echo"), &d, &default_context_);
3257 r.set_method("POST");
3258
[email protected]6cdfd7f2013-02-08 20:40:153259 base::FilePath dir;
[email protected]316c1e5e2012-09-12 15:17:443260 PathService::Get(base::DIR_EXE, &dir);
3261 file_util::SetCurrentDirectory(dir);
3262
[email protected]f288ef02012-12-15 20:28:283263 ScopedVector<UploadElementReader> element_readers;
[email protected]316c1e5e2012-09-12 15:17:443264
[email protected]6cdfd7f2013-02-08 20:40:153265 base::FilePath path;
[email protected]316c1e5e2012-09-12 15:17:443266 PathService::Get(base::DIR_SOURCE_ROOT, &path);
3267 path = path.Append(FILE_PATH_LITERAL("net"));
3268 path = path.Append(FILE_PATH_LITERAL("data"));
3269 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
3270 path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
[email protected]f288ef02012-12-15 20:28:283271 element_readers.push_back(new UploadFileElementReader(
[email protected]671a7142013-01-10 14:08:073272 base::MessageLoopProxy::current(), path, 0, kuint64max, base::Time()));
[email protected]316c1e5e2012-09-12 15:17:443273
3274 // This file should just be ignored in the upload stream.
[email protected]f288ef02012-12-15 20:28:283275 element_readers.push_back(new UploadFileElementReader(
[email protected]671a7142013-01-10 14:08:073276 base::MessageLoopProxy::current(),
[email protected]6cdfd7f2013-02-08 20:40:153277 base::FilePath(FILE_PATH_LITERAL(
[email protected]316c1e5e2012-09-12 15:17:443278 "c:\\path\\to\\non\\existant\\file.randomness.12345")),
[email protected]f288ef02012-12-15 20:28:283279 0, kuint64max, base::Time()));
3280 r.set_upload(make_scoped_ptr(new UploadDataStream(&element_readers, 0)));
[email protected]316c1e5e2012-09-12 15:17:443281
3282 r.Start();
3283 EXPECT_TRUE(r.is_pending());
3284
3285 MessageLoop::current()->Run();
3286
[email protected]329b68b2012-11-14 17:54:273287 int64 size = 0;
3288 ASSERT_EQ(true, file_util::GetFileSize(path, &size));
[email protected]316c1e5e2012-09-12 15:17:443289 scoped_array<char> buf(new char[size]);
3290
[email protected]329b68b2012-11-14 17:54:273291 ASSERT_EQ(size, file_util::ReadFile(path, buf.get(), size));
[email protected]316c1e5e2012-09-12 15:17:443292
[email protected]329b68b2012-11-14 17:54:273293 ASSERT_EQ(1, d.response_started_count())
3294 << "request failed: " << r.status().status()
3295 << ", error: " << r.status().error();
[email protected]316c1e5e2012-09-12 15:17:443296
3297 EXPECT_FALSE(d.received_data_before_response());
3298
[email protected]329b68b2012-11-14 17:54:273299 EXPECT_EQ(size, d.bytes_received());
3300 EXPECT_EQ(std::string(&buf[0], size), d.data_received());
[email protected]316c1e5e2012-09-12 15:17:443301 }
3302}
3303
3304TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) {
3305 ASSERT_TRUE(test_server_.Start());
3306
3307 TestDelegate d;
3308 {
3309 URLRequest r(test_server_.GetURL("echo"), &d, &default_context_);
3310 r.EnableChunkedUpload();
3311 r.set_method("POST");
3312 AddChunksToUpload(&r);
3313 r.Start();
3314 EXPECT_TRUE(r.is_pending());
3315
3316 MessageLoop::current()->Run();
3317
3318 VerifyReceivedDataMatchesChunks(&r, &d);
3319 }
3320}
3321
[email protected]329b68b2012-11-14 17:54:273322TEST_F(URLRequestTestHTTP, TestPostChunkedDataJustAfterStart) {
3323 ASSERT_TRUE(test_server_.Start());
3324
3325 TestDelegate d;
3326 {
3327 URLRequest r(test_server_.GetURL("echo"), &d, &default_context_);
3328 r.EnableChunkedUpload();
3329 r.set_method("POST");
3330 r.Start();
3331 EXPECT_TRUE(r.is_pending());
3332 AddChunksToUpload(&r);
3333 MessageLoop::current()->Run();
3334
3335 VerifyReceivedDataMatchesChunks(&r, &d);
3336 }
3337}
3338
[email protected]316c1e5e2012-09-12 15:17:443339TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) {
3340 ASSERT_TRUE(test_server_.Start());
3341
3342 TestDelegate d;
3343 {
3344 URLRequest r(test_server_.GetURL("echo"), &d, &default_context_);
3345 r.EnableChunkedUpload();
3346 r.set_method("POST");
3347 r.Start();
3348 EXPECT_TRUE(r.is_pending());
3349
[email protected]b4c62eb2012-11-14 18:36:513350 MessageLoop::current()->RunUntilIdle();
[email protected]316c1e5e2012-09-12 15:17:443351 AddChunksToUpload(&r);
3352 MessageLoop::current()->Run();
3353
3354 VerifyReceivedDataMatchesChunks(&r, &d);
3355 }
3356}
3357
3358TEST_F(URLRequestTestHTTP, ResponseHeadersTest) {
3359 ASSERT_TRUE(test_server_.Start());
3360
3361 TestDelegate d;
3362 URLRequest req(
3363 test_server_.GetURL("files/with-headers.html"), &d, &default_context_);
3364 req.Start();
3365 MessageLoop::current()->Run();
3366
3367 const HttpResponseHeaders* headers = req.response_headers();
3368
3369 // Simple sanity check that response_info() accesses the same data.
3370 EXPECT_EQ(headers, req.response_info().headers.get());
3371
3372 std::string header;
3373 EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header));
3374 EXPECT_EQ("private", header);
3375
3376 header.clear();
3377 EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header));
3378 EXPECT_EQ("text/html; charset=ISO-8859-1", header);
3379
3380 // The response has two "X-Multiple-Entries" headers.
3381 // This verfies our output has them concatenated together.
3382 header.clear();
3383 EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header));
3384 EXPECT_EQ("a, b", header);
3385}
3386
[email protected]242d8562012-10-30 21:20:463387TEST_F(URLRequestTestHTTP, ProcessSTS) {
3388 TestServer::SSLOptions ssl_options;
3389 TestServer https_test_server(
3390 TestServer::TYPE_HTTPS,
3391 ssl_options,
[email protected]6cdfd7f2013-02-08 20:40:153392 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
[email protected]242d8562012-10-30 21:20:463393 ASSERT_TRUE(https_test_server.Start());
3394
3395 TestDelegate d;
3396 URLRequest request(
3397 https_test_server.GetURL("files/hsts-headers.html"),
3398 &d,
3399 &default_context_);
3400 request.Start();
3401 MessageLoop::current()->Run();
3402
3403 TransportSecurityState* security_state =
3404 default_context_.transport_security_state();
3405 bool sni_available = true;
3406 TransportSecurityState::DomainState domain_state;
3407 EXPECT_TRUE(security_state->GetDomainState(
3408 TestServer::kLocalhost, sni_available, &domain_state));
3409 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
3410 domain_state.upgrade_mode);
3411 EXPECT_TRUE(domain_state.include_subdomains);
3412}
3413
3414TEST_F(URLRequestTestHTTP, ProcessSTSOnce) {
3415 TestServer::SSLOptions ssl_options;
3416 TestServer https_test_server(
3417 TestServer::TYPE_HTTPS,
3418 ssl_options,
[email protected]6cdfd7f2013-02-08 20:40:153419 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
[email protected]242d8562012-10-30 21:20:463420 ASSERT_TRUE(https_test_server.Start());
3421
3422 TestDelegate d;
3423 URLRequest request(
3424 https_test_server.GetURL("files/hsts-multiple-headers.html"),
3425 &d,
3426 &default_context_);
3427 request.Start();
3428 MessageLoop::current()->Run();
3429
3430 // We should have set parameters from the first header, not the second.
3431 TransportSecurityState* security_state =
3432 default_context_.transport_security_state();
3433 bool sni_available = true;
3434 TransportSecurityState::DomainState domain_state;
3435 EXPECT_TRUE(security_state->GetDomainState(
3436 TestServer::kLocalhost, sni_available, &domain_state));
3437 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
3438 domain_state.upgrade_mode);
3439 EXPECT_FALSE(domain_state.include_subdomains);
3440}
3441
[email protected]316c1e5e2012-09-12 15:17:443442TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) {
3443 ASSERT_TRUE(test_server_.Start());
3444
3445 TestDelegate d;
3446 URLRequest req(test_server_.GetURL(
3447 "files/content-type-normalization.html"), &d, &default_context_);
3448 req.Start();
3449 MessageLoop::current()->Run();
3450
3451 std::string mime_type;
3452 req.GetMimeType(&mime_type);
3453 EXPECT_EQ("text/html", mime_type);
3454
3455 std::string charset;
3456 req.GetCharset(&charset);
3457 EXPECT_EQ("utf-8", charset);
3458 req.Cancel();
3459}
3460
3461TEST_F(URLRequestTestHTTP, RestrictRedirects) {
3462 ASSERT_TRUE(test_server_.Start());
3463
3464 TestDelegate d;
3465 URLRequest req(test_server_.GetURL(
3466 "files/redirect-to-file.html"), &d, &default_context_);
3467 req.Start();
3468 MessageLoop::current()->Run();
3469
3470 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
3471 EXPECT_EQ(ERR_UNSAFE_REDIRECT, req.status().error());
3472}
3473
3474TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) {
3475 ASSERT_TRUE(test_server_.Start());
3476
3477 TestDelegate d;
3478 URLRequest req(test_server_.GetURL(
3479 "files/redirect-to-invalid-url.html"), &d, &default_context_);
3480 req.Start();
3481 MessageLoop::current()->Run();
3482
3483 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
3484 EXPECT_EQ(ERR_INVALID_URL, req.status().error());
3485}
3486
3487TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) {
3488 ASSERT_TRUE(test_server_.Start());
3489
3490 TestDelegate d;
3491 URLRequest req(
3492 test_server_.GetURL("echoheader?Referer"), &d, &default_context_);
3493 req.set_referrer("http://user:[email protected]/");
3494 req.Start();
3495 MessageLoop::current()->Run();
3496
3497 EXPECT_EQ(std::string("http://foo.com/"), d.data_received());
3498}
3499
3500TEST_F(URLRequestTestHTTP, CancelRedirect) {
3501 ASSERT_TRUE(test_server_.Start());
3502
3503 TestDelegate d;
3504 {
3505 d.set_cancel_in_received_redirect(true);
3506 URLRequest req(
3507 test_server_.GetURL("files/redirect-test.html"), &d, &default_context_);
3508 req.Start();
3509 MessageLoop::current()->Run();
3510
3511 EXPECT_EQ(1, d.response_started_count());
3512 EXPECT_EQ(0, d.bytes_received());
3513 EXPECT_FALSE(d.received_data_before_response());
3514 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
3515 }
3516}
3517
3518TEST_F(URLRequestTestHTTP, DeferredRedirect) {
3519 ASSERT_TRUE(test_server_.Start());
3520
3521 TestDelegate d;
3522 {
3523 d.set_quit_on_redirect(true);
3524 URLRequest req(
3525 test_server_.GetURL("files/redirect-test.html"), &d, &default_context_);
3526 req.Start();
3527 MessageLoop::current()->Run();
3528
3529 EXPECT_EQ(1, d.received_redirect_count());
3530
3531 req.FollowDeferredRedirect();
3532 MessageLoop::current()->Run();
3533
3534 EXPECT_EQ(1, d.response_started_count());
3535 EXPECT_FALSE(d.received_data_before_response());
3536 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
3537
[email protected]6cdfd7f2013-02-08 20:40:153538 base::FilePath path;
[email protected]316c1e5e2012-09-12 15:17:443539 PathService::Get(base::DIR_SOURCE_ROOT, &path);
3540 path = path.Append(FILE_PATH_LITERAL("net"));
3541 path = path.Append(FILE_PATH_LITERAL("data"));
3542 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
3543 path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
3544
3545 std::string contents;
3546 EXPECT_TRUE(file_util::ReadFileToString(path, &contents));
3547 EXPECT_EQ(contents, d.data_received());
3548 }
3549}
3550
3551TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) {
3552 ASSERT_TRUE(test_server_.Start());
3553
3554 TestDelegate d;
3555 {
3556 d.set_quit_on_redirect(true);
3557 URLRequest req(
3558 test_server_.GetURL("files/redirect-test.html"), &d, &default_context_);
3559 req.Start();
3560 MessageLoop::current()->Run();
3561
3562 EXPECT_EQ(1, d.received_redirect_count());
3563
3564 req.Cancel();
3565 MessageLoop::current()->Run();
3566
3567 EXPECT_EQ(1, d.response_started_count());
3568 EXPECT_EQ(0, d.bytes_received());
3569 EXPECT_FALSE(d.received_data_before_response());
3570 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
3571 }
3572}
3573
3574TEST_F(URLRequestTestHTTP, VaryHeader) {
3575 ASSERT_TRUE(test_server_.Start());
3576
3577 // populate the cache
3578 {
3579 TestDelegate d;
3580 URLRequest req(
3581 test_server_.GetURL("echoheadercache?foo"), &d, &default_context_);
3582 HttpRequestHeaders headers;
3583 headers.SetHeader("foo", "1");
3584 req.SetExtraRequestHeaders(headers);
3585 req.Start();
3586 MessageLoop::current()->Run();
3587 }
3588
3589 // expect a cache hit
3590 {
3591 TestDelegate d;
3592 URLRequest req(
3593 test_server_.GetURL("echoheadercache?foo"), &d, &default_context_);
3594 HttpRequestHeaders headers;
3595 headers.SetHeader("foo", "1");
3596 req.SetExtraRequestHeaders(headers);
3597 req.Start();
3598 MessageLoop::current()->Run();
3599
3600 EXPECT_TRUE(req.was_cached());
3601 }
3602
3603 // expect a cache miss
3604 {
3605 TestDelegate d;
3606 URLRequest req(
3607 test_server_.GetURL("echoheadercache?foo"), &d, &default_context_);
3608 HttpRequestHeaders headers;
3609 headers.SetHeader("foo", "2");
3610 req.SetExtraRequestHeaders(headers);
3611 req.Start();
3612 MessageLoop::current()->Run();
3613
3614 EXPECT_FALSE(req.was_cached());
3615 }
3616}
3617
3618TEST_F(URLRequestTestHTTP, BasicAuth) {
3619 ASSERT_TRUE(test_server_.Start());
3620
3621 // populate the cache
3622 {
3623 TestDelegate d;
3624 d.set_credentials(AuthCredentials(kUser, kSecret));
3625
3626 URLRequest r(test_server_.GetURL("auth-basic"), &d, &default_context_);
3627 r.Start();
3628
3629 MessageLoop::current()->Run();
3630
3631 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
3632 }
3633
3634 // repeat request with end-to-end validation. since auth-basic results in a
3635 // cachable page, we expect this test to result in a 304. in which case, the
3636 // response should be fetched from the cache.
3637 {
3638 TestDelegate d;
3639 d.set_credentials(AuthCredentials(kUser, kSecret));
3640
3641 URLRequest r(test_server_.GetURL("auth-basic"), &d, &default_context_);
3642 r.set_load_flags(LOAD_VALIDATE_CACHE);
3643 r.Start();
3644
3645 MessageLoop::current()->Run();
3646
3647 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
3648
3649 // Should be the same cached document.
3650 EXPECT_TRUE(r.was_cached());
3651 }
3652}
3653
3654// Check that Set-Cookie headers in 401 responses are respected.
3655// http://crbug.com/6450
3656TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) {
3657 ASSERT_TRUE(test_server_.Start());
3658
3659 GURL url_requiring_auth =
3660 test_server_.GetURL("auth-basic?set-cookie-if-challenged");
3661
3662 // Request a page that will give a 401 containing a Set-Cookie header.
3663 // Verify that when the transaction is restarted, it includes the new cookie.
3664 {
[email protected]ceefd7fd2012-11-29 00:36:243665 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]316c1e5e2012-09-12 15:17:443666 TestURLRequestContext context(true);
3667 context.set_network_delegate(&network_delegate);
3668 context.Init();
3669
3670 TestDelegate d;
3671 d.set_credentials(AuthCredentials(kUser, kSecret));
3672
3673 URLRequest r(url_requiring_auth, &d, &context);
3674 r.Start();
3675
3676 MessageLoop::current()->Run();
3677
3678 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
3679
3680 // Make sure we sent the cookie in the restarted transaction.
3681 EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true")
3682 != std::string::npos);
3683 }
3684
3685 // Same test as above, except this time the restart is initiated earlier
3686 // (without user intervention since identity is embedded in the URL).
3687 {
[email protected]ceefd7fd2012-11-29 00:36:243688 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]316c1e5e2012-09-12 15:17:443689 TestURLRequestContext context(true);
3690 context.set_network_delegate(&network_delegate);
3691 context.Init();
3692
3693 TestDelegate d;
3694
3695 GURL::Replacements replacements;
3696 std::string username("user2");
3697 std::string password("secret");
3698 replacements.SetUsernameStr(username);
3699 replacements.SetPasswordStr(password);
3700 GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements);
3701
3702 URLRequest r(url_with_identity, &d, &context);
3703 r.Start();
3704
3705 MessageLoop::current()->Run();
3706
3707 EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos);
3708
3709 // Make sure we sent the cookie in the restarted transaction.
3710 EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true")
3711 != std::string::npos);
3712 }
3713}
3714
[email protected]58e32bb2013-01-21 18:23:253715// Tests that load timing works as expected with auth and the cache.
3716TEST_F(URLRequestTestHTTP, BasicAuthLoadTiming) {
3717 ASSERT_TRUE(test_server_.Start());
3718
3719 // populate the cache
3720 {
3721 TestDelegate d;
3722 d.set_credentials(AuthCredentials(kUser, kSecret));
3723
3724 URLRequest r(test_server_.GetURL("auth-basic"), &d, &default_context_);
3725 r.Start();
3726
3727 MessageLoop::current()->Run();
3728
3729 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
3730
3731 LoadTimingInfo load_timing_info_before_auth;
3732 EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeAuth(
3733 &load_timing_info_before_auth));
3734 TestLoadTimingNotReused(load_timing_info_before_auth,
3735 CONNECT_TIMING_HAS_DNS_TIMES);
3736
3737 LoadTimingInfo load_timing_info;
[email protected]0a703912013-02-02 04:25:173738 r.GetLoadTimingInfo(&load_timing_info);
[email protected]58e32bb2013-01-21 18:23:253739 // The test server does not support keep alive sockets, so the second
3740 // request with auth should use a new socket.
3741 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
3742 EXPECT_NE(load_timing_info_before_auth.socket_log_id,
3743 load_timing_info.socket_log_id);
3744 EXPECT_LE(load_timing_info_before_auth.receive_headers_end,
3745 load_timing_info.connect_timing.connect_start);
3746 }
3747
3748 // repeat request with end-to-end validation. since auth-basic results in a
3749 // cachable page, we expect this test to result in a 304. in which case, the
3750 // response should be fetched from the cache.
3751 {
3752 TestDelegate d;
3753 d.set_credentials(AuthCredentials(kUser, kSecret));
3754
3755 URLRequest r(test_server_.GetURL("auth-basic"), &d, &default_context_);
3756 r.set_load_flags(LOAD_VALIDATE_CACHE);
3757 r.Start();
3758
3759 MessageLoop::current()->Run();
3760
3761 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
3762
3763 // Should be the same cached document.
3764 EXPECT_TRUE(r.was_cached());
3765
3766 LoadTimingInfo load_timing_info;
[email protected]0a703912013-02-02 04:25:173767 r.GetLoadTimingInfo(&load_timing_info);
[email protected]58e32bb2013-01-21 18:23:253768 TestLoadTimingNoHttpConnection(load_timing_info);
3769 }
3770}
3771
[email protected]316c1e5e2012-09-12 15:17:443772// In this test, we do a POST which the server will 302 redirect.
3773// The subsequent transaction should use GET, and should not send the
3774// Content-Type header.
3775// http://code.google.com/p/chromium/issues/detail?id=843
3776TEST_F(URLRequestTestHTTP, Post302RedirectGet) {
3777 ASSERT_TRUE(test_server_.Start());
3778
3779 const char kData[] = "hello world";
3780
3781 TestDelegate d;
3782 URLRequest req(
3783 test_server_.GetURL("files/redirect-to-echoall"), &d, &default_context_);
3784 req.set_method("POST");
[email protected]f288ef02012-12-15 20:28:283785 req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
[email protected]316c1e5e2012-09-12 15:17:443786
3787 // Set headers (some of which are specific to the POST).
3788 HttpRequestHeaders headers;
3789 headers.AddHeadersFromString(
3790 "Content-Type: multipart/form-data; "
3791 "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n"
3792 "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,"
3793 "text/plain;q=0.8,image/png,*/*;q=0.5\r\n"
3794 "Accept-Language: en-US,en\r\n"
3795 "Accept-Charset: ISO-8859-1,*,utf-8\r\n"
3796 "Content-Length: 11\r\n"
3797 "Origin: http://localhost:1337/");
3798 req.SetExtraRequestHeaders(headers);
3799 req.Start();
3800 MessageLoop::current()->Run();
3801
3802 std::string mime_type;
3803 req.GetMimeType(&mime_type);
3804 EXPECT_EQ("text/html", mime_type);
3805
3806 const std::string& data = d.data_received();
3807
3808 // Check that the post-specific headers were stripped:
3809 EXPECT_FALSE(ContainsString(data, "Content-Length:"));
3810 EXPECT_FALSE(ContainsString(data, "Content-Type:"));
3811 EXPECT_FALSE(ContainsString(data, "Origin:"));
3812
3813 // These extra request headers should not have been stripped.
3814 EXPECT_TRUE(ContainsString(data, "Accept:"));
3815 EXPECT_TRUE(ContainsString(data, "Accept-Language:"));
3816 EXPECT_TRUE(ContainsString(data, "Accept-Charset:"));
3817}
3818
3819// The following tests check that we handle mutating the request method for
3820// HTTP redirects as expected.
3821// See http://crbug.com/56373 and http://crbug.com/102130.
3822
3823TEST_F(URLRequestTestHTTP, Redirect301Tests) {
3824 ASSERT_TRUE(test_server_.Start());
3825
3826 const GURL url = test_server_.GetURL("files/redirect301-to-echo");
3827
3828 HTTPRedirectMethodTest(url, "POST", "GET", true);
3829 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
3830 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
3831}
3832
3833TEST_F(URLRequestTestHTTP, Redirect302Tests) {
3834 ASSERT_TRUE(test_server_.Start());
3835
3836 const GURL url = test_server_.GetURL("files/redirect302-to-echo");
3837
3838 HTTPRedirectMethodTest(url, "POST", "GET", true);
3839 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
3840 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
3841}
3842
3843TEST_F(URLRequestTestHTTP, Redirect303Tests) {
3844 ASSERT_TRUE(test_server_.Start());
3845
3846 const GURL url = test_server_.GetURL("files/redirect303-to-echo");
3847
3848 HTTPRedirectMethodTest(url, "POST", "GET", true);
3849 HTTPRedirectMethodTest(url, "PUT", "GET", true);
3850 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
3851}
3852
3853TEST_F(URLRequestTestHTTP, Redirect307Tests) {
3854 ASSERT_TRUE(test_server_.Start());
3855
3856 const GURL url = test_server_.GetURL("files/redirect307-to-echo");
3857
3858 HTTPRedirectMethodTest(url, "POST", "POST", true);
3859 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
3860 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
3861}
3862
3863TEST_F(URLRequestTestHTTP, InterceptPost302RedirectGet) {
3864 ASSERT_TRUE(test_server_.Start());
3865
3866 const char kData[] = "hello world";
3867
3868 TestDelegate d;
3869 URLRequest req(test_server_.GetURL("empty.html"), &d, &default_context_);
3870 req.set_method("POST");
[email protected]f288ef02012-12-15 20:28:283871 req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
[email protected]316c1e5e2012-09-12 15:17:443872 HttpRequestHeaders headers;
3873 headers.SetHeader(HttpRequestHeaders::kContentLength,
3874 base::UintToString(arraysize(kData) - 1));
3875 req.SetExtraRequestHeaders(headers);
3876
3877 URLRequestRedirectJob* job = new URLRequestRedirectJob(
[email protected]8ccc69f2012-11-28 19:52:143878 &req, default_context_.network_delegate(), test_server_.GetURL("echo"),
3879 URLRequestRedirectJob::REDIRECT_302_FOUND);
[email protected]316c1e5e2012-09-12 15:17:443880 AddTestInterceptor()->set_main_intercept_job(job);
3881
3882 req.Start();
3883 MessageLoop::current()->Run();
3884 EXPECT_EQ("GET", req.method());
3885}
3886
3887TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) {
3888 ASSERT_TRUE(test_server_.Start());
3889
3890 const char kData[] = "hello world";
3891
3892 TestDelegate d;
3893 URLRequest req(test_server_.GetURL("empty.html"), &d, &default_context_);
3894 req.set_method("POST");
[email protected]f288ef02012-12-15 20:28:283895 req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
[email protected]316c1e5e2012-09-12 15:17:443896 HttpRequestHeaders headers;
3897 headers.SetHeader(HttpRequestHeaders::kContentLength,
3898 base::UintToString(arraysize(kData) - 1));
3899 req.SetExtraRequestHeaders(headers);
3900
3901 URLRequestRedirectJob* job = new URLRequestRedirectJob(
[email protected]8ccc69f2012-11-28 19:52:143902 &req, default_context_.network_delegate(), test_server_.GetURL("echo"),
[email protected]316c1e5e2012-09-12 15:17:443903 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT);
3904 AddTestInterceptor()->set_main_intercept_job(job);
3905
3906 req.Start();
3907 MessageLoop::current()->Run();
3908 EXPECT_EQ("POST", req.method());
3909 EXPECT_EQ(kData, d.data_received());
3910}
3911
3912// Check that default A-L header is sent.
3913TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) {
3914 ASSERT_TRUE(test_server_.Start());
3915
[email protected]ee4c30d2012-11-07 15:08:433916 StaticHttpUserAgentSettings settings("en", EmptyString(), EmptyString());
[email protected]ceefd7fd2012-11-29 00:36:243917 TestNetworkDelegate network_delegate; // Must outlive URLRequests.
[email protected]316c1e5e2012-09-12 15:17:443918 TestURLRequestContext context(true);
3919 context.set_network_delegate(&network_delegate);
[email protected]ee4c30d2012-11-07 15:08:433920 context.set_http_user_agent_settings(&settings);
[email protected]316c1e5e2012-09-12 15:17:443921 context.Init();
3922
3923 TestDelegate d;
3924 URLRequest req(
3925 test_server_.GetURL("echoheader?Accept-Language"), &d, &context);
3926 req.Start();
3927 MessageLoop::current()->Run();
3928 EXPECT_EQ("en", d.data_received());
3929}
3930
3931// Check that an empty A-L header is not sent. http://crbug.com/77365.
3932TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) {
3933 ASSERT_TRUE(test_server_.Start());
3934
[email protected]ee4c30d2012-11-07 15:08:433935 StaticHttpUserAgentSettings settings(
3936 EmptyString(), EmptyString(), EmptyString());
[email protected]ceefd7fd2012-11-29 00:36:243937 TestNetworkDelegate network_delegate; // Must outlive URLRequests.
[email protected]316c1e5e2012-09-12 15:17:443938 TestURLRequestContext context(true);
3939 context.set_network_delegate(&network_delegate);
3940 context.Init();
3941 // We override the language after initialization because empty entries
3942 // get overridden by Init().
[email protected]ee4c30d2012-11-07 15:08:433943 context.set_http_user_agent_settings(&settings);
[email protected]316c1e5e2012-09-12 15:17:443944
3945 TestDelegate d;
3946 URLRequest req(
3947 test_server_.GetURL("echoheader?Accept-Language"), &d, &context);
3948 req.Start();
3949 MessageLoop::current()->Run();
3950 EXPECT_EQ("None", d.data_received());
3951}
3952
3953// Check that if request overrides the A-L header, the default is not appended.
3954// See http://crbug.com/20894
3955TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) {
3956 ASSERT_TRUE(test_server_.Start());
3957
3958 TestDelegate d;
3959 URLRequest req(test_server_.GetURL("echoheader?Accept-Language"),
3960 &d,
3961 &default_context_);
3962 HttpRequestHeaders headers;
3963 headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru");
3964 req.SetExtraRequestHeaders(headers);
3965 req.Start();
3966 MessageLoop::current()->Run();
3967 EXPECT_EQ(std::string("ru"), d.data_received());
3968}
3969
3970// Check that default A-E header is sent.
3971TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) {
3972 ASSERT_TRUE(test_server_.Start());
3973
3974 TestDelegate d;
3975 URLRequest req(test_server_.GetURL("echoheader?Accept-Encoding"),
3976 &d,
3977 &default_context_);
3978 HttpRequestHeaders headers;
3979 req.SetExtraRequestHeaders(headers);
3980 req.Start();
3981 MessageLoop::current()->Run();
3982 EXPECT_TRUE(ContainsString(d.data_received(), "gzip"));
3983}
3984
3985// Check that if request overrides the A-E header, the default is not appended.
3986// See http://crbug.com/47381
3987TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) {
3988 ASSERT_TRUE(test_server_.Start());
3989
3990 TestDelegate d;
3991 URLRequest req(test_server_.GetURL("echoheader?Accept-Encoding"),
3992 &d,
3993 &default_context_);
3994 HttpRequestHeaders headers;
3995 headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, "identity");
3996 req.SetExtraRequestHeaders(headers);
3997 req.Start();
3998 MessageLoop::current()->Run();
3999 EXPECT_FALSE(ContainsString(d.data_received(), "gzip"));
4000 EXPECT_TRUE(ContainsString(d.data_received(), "identity"));
4001}
4002
4003// Check that default A-C header is sent.
4004TEST_F(URLRequestTestHTTP, DefaultAcceptCharset) {
4005 ASSERT_TRUE(test_server_.Start());
4006
[email protected]ee4c30d2012-11-07 15:08:434007 StaticHttpUserAgentSettings settings(EmptyString(), "en", EmptyString());
[email protected]ceefd7fd2012-11-29 00:36:244008 TestNetworkDelegate network_delegate; // Must outlive URLRequests.
[email protected]316c1e5e2012-09-12 15:17:444009 TestURLRequestContext context(true);
4010 context.set_network_delegate(&network_delegate);
[email protected]ee4c30d2012-11-07 15:08:434011 context.set_http_user_agent_settings(&settings);
[email protected]316c1e5e2012-09-12 15:17:444012 context.Init();
4013
4014 TestDelegate d;
4015 URLRequest req(test_server_.GetURL("echoheader?Accept-Charset"),
4016 &d,
4017 &context);
4018 req.Start();
4019 MessageLoop::current()->Run();
4020 EXPECT_EQ("en", d.data_received());
4021}
4022
4023// Check that an empty A-C header is not sent. http://crbug.com/77365.
4024TEST_F(URLRequestTestHTTP, EmptyAcceptCharset) {
4025 ASSERT_TRUE(test_server_.Start());
4026
[email protected]ee4c30d2012-11-07 15:08:434027 StaticHttpUserAgentSettings settings(
4028 EmptyString(), EmptyString(), EmptyString());
[email protected]ceefd7fd2012-11-29 00:36:244029 TestNetworkDelegate network_delegate; // Must outlive URLRequests.
[email protected]316c1e5e2012-09-12 15:17:444030 TestURLRequestContext context(true);
4031 context.set_network_delegate(&network_delegate);
4032 context.Init();
4033 // We override the accepted charset after initialization because empty
4034 // entries get overridden otherwise.
[email protected]ee4c30d2012-11-07 15:08:434035 context.set_http_user_agent_settings(&settings);
[email protected]316c1e5e2012-09-12 15:17:444036
4037 TestDelegate d;
4038 URLRequest req(test_server_.GetURL("echoheader?Accept-Charset"),
4039 &d,
4040 &context);
4041 req.Start();
4042 MessageLoop::current()->Run();
4043 EXPECT_EQ("None", d.data_received());
4044}
4045
4046// Check that if request overrides the A-C header, the default is not appended.
4047// See http://crbug.com/20894
4048TEST_F(URLRequestTestHTTP, OverrideAcceptCharset) {
4049 ASSERT_TRUE(test_server_.Start());
4050
4051 TestDelegate d;
4052 URLRequest req(test_server_.GetURL("echoheader?Accept-Charset"),
4053 &d,
4054 &default_context_);
4055 HttpRequestHeaders headers;
4056 headers.SetHeader(HttpRequestHeaders::kAcceptCharset, "koi-8r");
4057 req.SetExtraRequestHeaders(headers);
4058 req.Start();
4059 MessageLoop::current()->Run();
4060 EXPECT_EQ(std::string("koi-8r"), d.data_received());
4061}
4062
4063// Check that default User-Agent header is sent.
4064TEST_F(URLRequestTestHTTP, DefaultUserAgent) {
4065 ASSERT_TRUE(test_server_.Start());
4066
4067 TestDelegate d;
4068 URLRequest req(test_server_.GetURL("echoheader?User-Agent"),
4069 &d,
4070 &default_context_);
4071 req.Start();
4072 MessageLoop::current()->Run();
4073 EXPECT_EQ(req.context()->GetUserAgent(req.url()), d.data_received());
4074}
4075
4076// Check that if request overrides the User-Agent header,
4077// the default is not appended.
4078TEST_F(URLRequestTestHTTP, OverrideUserAgent) {
4079 ASSERT_TRUE(test_server_.Start());
4080
4081 TestDelegate d;
4082 URLRequest req(test_server_.GetURL("echoheader?User-Agent"),
4083 &d,
4084 &default_context_);
4085 HttpRequestHeaders headers;
4086 headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)");
4087 req.SetExtraRequestHeaders(headers);
4088 req.Start();
4089 MessageLoop::current()->Run();
4090 // If the net tests are being run with ChromeFrame then we need to allow for
4091 // the 'chromeframe' suffix which is added to the user agent before the
4092 // closing parentheses.
4093 EXPECT_TRUE(StartsWithASCII(d.data_received(), "Lynx (textmode", true));
4094}
4095
[email protected]ee4c30d2012-11-07 15:08:434096// Check that a NULL HttpUserAgentSettings causes the corresponding empty
4097// User-Agent header to be sent but does not send the Accept-Language and
4098// Accept-Charset headers.
4099TEST_F(URLRequestTestHTTP, EmptyHttpUserAgentSettings) {
4100 ASSERT_TRUE(test_server_.Start());
4101
[email protected]ceefd7fd2012-11-29 00:36:244102 TestNetworkDelegate network_delegate; // Must outlive URLRequests.
[email protected]ee4c30d2012-11-07 15:08:434103 TestURLRequestContext context(true);
4104 context.set_network_delegate(&network_delegate);
4105 context.Init();
4106 // We override the HttpUserAgentSettings after initialization because empty
4107 // entries get overridden by Init().
4108 context.set_http_user_agent_settings(NULL);
4109
4110 struct {
4111 const char* request;
4112 const char* expected_response;
4113 } tests[] = { { "echoheader?Accept-Language", "None" },
4114 { "echoheader?Accept-Charset", "None" },
4115 { "echoheader?User-Agent", "" } };
4116
4117 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); i++) {
4118 TestDelegate d;
4119 URLRequest req(test_server_.GetURL(tests[i].request), &d, &context);
4120 req.Start();
4121 MessageLoop::current()->Run();
4122 EXPECT_EQ(tests[i].expected_response, d.data_received())
4123 << " Request = \"" << tests[i].request << "\"";
4124 }
4125}
4126
[email protected]73e0bba2009-02-19 22:57:094127class HTTPSRequestTest : public testing::Test {
[email protected]87a09a92011-07-14 15:50:504128 public:
[email protected]ef2bf422012-05-11 03:27:094129 HTTPSRequestTest() : default_context_(true) {
4130 default_context_.set_network_delegate(&default_network_delegate_);
4131 default_context_.Init();
[email protected]87a09a92011-07-14 15:50:504132 }
4133 virtual ~HTTPSRequestTest() {}
4134
4135 protected:
[email protected]ceefd7fd2012-11-29 00:36:244136 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest.
[email protected]ef2bf422012-05-11 03:27:094137 TestURLRequestContext default_context_;
[email protected]ea224582008-12-07 20:25:464138};
4139
[email protected]c044616e2013-02-20 02:01:264140TEST_F(HTTPSRequestTest, HTTPSGetTest) {
[email protected]7461a402011-03-24 23:19:514141 TestServer test_server(TestServer::TYPE_HTTPS,
[email protected]d9fca99a2012-02-24 16:16:204142 TestServer::kLocalhost,
[email protected]6cdfd7f2013-02-08 20:40:154143 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]95409e12010-08-17 20:07:114144 ASSERT_TRUE(test_server.Start());
[email protected]ea224582008-12-07 20:25:464145
[email protected]ea224582008-12-07 20:25:464146 TestDelegate d;
4147 {
[email protected]94e2bbe2012-06-22 15:26:134148 URLRequest r(test_server.GetURL(""), &d, &default_context_);
[email protected]ea224582008-12-07 20:25:464149 r.Start();
4150 EXPECT_TRUE(r.is_pending());
4151
4152 MessageLoop::current()->Run();
4153
4154 EXPECT_EQ(1, d.response_started_count());
4155 EXPECT_FALSE(d.received_data_before_response());
4156 EXPECT_NE(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:174157 CheckSSLInfo(r.ssl_info());
[email protected]6d81b482011-02-22 19:47:194158 EXPECT_EQ(test_server.host_port_pair().host(),
4159 r.GetSocketAddress().host());
4160 EXPECT_EQ(test_server.host_port_pair().port(),
4161 r.GetSocketAddress().port());
[email protected]ea224582008-12-07 20:25:464162 }
[email protected]ea224582008-12-07 20:25:464163}
4164
[email protected]5774ada2010-07-15 06:30:544165TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) {
[email protected]295e5cd2012-08-23 01:05:124166 TestServer::SSLOptions ssl_options(
4167 TestServer::SSLOptions::CERT_MISMATCHED_NAME);
4168 TestServer test_server(TestServer::TYPE_HTTPS,
4169 ssl_options,
[email protected]6cdfd7f2013-02-08 20:40:154170 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]95409e12010-08-17 20:07:114171 ASSERT_TRUE(test_server.Start());
[email protected]bacff652009-03-31 17:50:334172
4173 bool err_allowed = true;
4174 for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) {
4175 TestDelegate d;
4176 {
4177 d.set_allow_certificate_errors(err_allowed);
[email protected]94e2bbe2012-06-22 15:26:134178 URLRequest r(test_server.GetURL(""), &d, &default_context_);
[email protected]bacff652009-03-31 17:50:334179
4180 r.Start();
4181 EXPECT_TRUE(r.is_pending());
4182
4183 MessageLoop::current()->Run();
4184
4185 EXPECT_EQ(1, d.response_started_count());
4186 EXPECT_FALSE(d.received_data_before_response());
4187 EXPECT_TRUE(d.have_certificate_errors());
[email protected]96adadb2010-08-28 01:16:174188 if (err_allowed) {
[email protected]bacff652009-03-31 17:50:334189 EXPECT_NE(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:174190 CheckSSLInfo(r.ssl_info());
4191 } else {
[email protected]bacff652009-03-31 17:50:334192 EXPECT_EQ(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:174193 }
[email protected]bacff652009-03-31 17:50:334194 }
4195 }
4196}
4197
[email protected]5774ada2010-07-15 06:30:544198TEST_F(HTTPSRequestTest, HTTPSExpiredTest) {
[email protected]295e5cd2012-08-23 01:05:124199 TestServer::SSLOptions ssl_options(
4200 TestServer::SSLOptions::CERT_EXPIRED);
4201 TestServer test_server(TestServer::TYPE_HTTPS,
4202 ssl_options,
[email protected]6cdfd7f2013-02-08 20:40:154203 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]95409e12010-08-17 20:07:114204 ASSERT_TRUE(test_server.Start());
[email protected]bacff652009-03-31 17:50:334205
4206 // Iterate from false to true, just so that we do the opposite of the
4207 // previous test in order to increase test coverage.
4208 bool err_allowed = false;
4209 for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) {
4210 TestDelegate d;
4211 {
4212 d.set_allow_certificate_errors(err_allowed);
[email protected]94e2bbe2012-06-22 15:26:134213 URLRequest r(test_server.GetURL(""), &d, &default_context_);
[email protected]bacff652009-03-31 17:50:334214
4215 r.Start();
4216 EXPECT_TRUE(r.is_pending());
4217
4218 MessageLoop::current()->Run();
4219
4220 EXPECT_EQ(1, d.response_started_count());
4221 EXPECT_FALSE(d.received_data_before_response());
4222 EXPECT_TRUE(d.have_certificate_errors());
[email protected]96adadb2010-08-28 01:16:174223 if (err_allowed) {
[email protected]bacff652009-03-31 17:50:334224 EXPECT_NE(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:174225 CheckSSLInfo(r.ssl_info());
4226 } else {
[email protected]bacff652009-03-31 17:50:334227 EXPECT_EQ(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:174228 }
[email protected]bacff652009-03-31 17:50:334229 }
4230 }
4231}
[email protected]73e0bba2009-02-19 22:57:094232
[email protected]316c1e5e2012-09-12 15:17:444233// Tests TLSv1.1 -> TLSv1 fallback. Verifies that we don't fall back more
4234// than necessary.
4235TEST_F(HTTPSRequestTest, TLSv1Fallback) {
4236 uint16 default_version_max = SSLConfigService::default_version_max();
4237 // The OpenSSL library in use may not support TLS 1.1.
4238#if !defined(USE_OPENSSL)
4239 EXPECT_GT(default_version_max, SSL_PROTOCOL_VERSION_TLS1);
4240#endif
4241 if (default_version_max <= SSL_PROTOCOL_VERSION_TLS1)
4242 return;
4243
4244 TestServer::SSLOptions ssl_options(
4245 TestServer::SSLOptions::CERT_OK);
4246 ssl_options.tls_intolerant =
4247 TestServer::SSLOptions::TLS_INTOLERANT_TLS1_1;
4248 TestServer test_server(TestServer::TYPE_HTTPS,
4249 ssl_options,
[email protected]6cdfd7f2013-02-08 20:40:154250 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:444251 ASSERT_TRUE(test_server.Start());
4252
4253 TestDelegate d;
4254 TestURLRequestContext context(true);
4255 context.Init();
4256 d.set_allow_certificate_errors(true);
4257 URLRequest r(test_server.GetURL(""), &d, &context);
4258 r.Start();
4259
4260 MessageLoop::current()->Run();
4261
4262 EXPECT_EQ(1, d.response_started_count());
4263 EXPECT_NE(0, d.bytes_received());
4264 EXPECT_EQ(static_cast<int>(SSL_CONNECTION_VERSION_TLS1),
4265 SSLConnectionStatusToVersion(r.ssl_info().connection_status));
4266 EXPECT_TRUE(r.ssl_info().connection_status & SSL_CONNECTION_VERSION_FALLBACK);
4267}
4268
4269// This tests that a load of www.google.com with a certificate error sets
4270// the |certificate_errors_are_fatal| flag correctly. This flag will cause
4271// the interstitial to be fatal.
4272TEST_F(HTTPSRequestTest, HTTPSPreloadedHSTSTest) {
4273 TestServer::SSLOptions ssl_options(
4274 TestServer::SSLOptions::CERT_MISMATCHED_NAME);
4275 TestServer test_server(TestServer::TYPE_HTTPS,
4276 ssl_options,
[email protected]6cdfd7f2013-02-08 20:40:154277 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:444278 ASSERT_TRUE(test_server.Start());
4279
4280 // We require that the URL be www.google.com in order to pick up the
4281 // preloaded HSTS entries in the TransportSecurityState. This means that we
4282 // have to use a MockHostResolver in order to direct www.google.com to the
[email protected]ceefd7fd2012-11-29 00:36:244283 // testserver. By default, MockHostResolver maps all hosts to 127.0.0.1.
[email protected]316c1e5e2012-09-12 15:17:444284
4285 MockHostResolver host_resolver;
[email protected]ceefd7fd2012-11-29 00:36:244286 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]316c1e5e2012-09-12 15:17:444287 TestURLRequestContext context(true);
4288 context.set_network_delegate(&network_delegate);
4289 context.set_host_resolver(&host_resolver);
4290 TransportSecurityState transport_security_state;
4291 context.set_transport_security_state(&transport_security_state);
4292 context.Init();
4293
4294 TestDelegate d;
4295 URLRequest r(GURL(StringPrintf("https://www.google.com:%d",
4296 test_server.host_port_pair().port())),
4297 &d,
4298 &context);
4299
4300 r.Start();
4301 EXPECT_TRUE(r.is_pending());
4302
4303 MessageLoop::current()->Run();
4304
4305 EXPECT_EQ(1, d.response_started_count());
4306 EXPECT_FALSE(d.received_data_before_response());
4307 EXPECT_TRUE(d.have_certificate_errors());
4308 EXPECT_TRUE(d.certificate_errors_are_fatal());
4309}
4310
4311// This tests that cached HTTPS page loads do not cause any updates to the
4312// TransportSecurityState.
4313TEST_F(HTTPSRequestTest, HTTPSErrorsNoClobberTSSTest) {
4314 // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't
4315 // matter. It just has to be any error.
4316 TestServer::SSLOptions ssl_options(
4317 TestServer::SSLOptions::CERT_MISMATCHED_NAME);
4318 TestServer test_server(TestServer::TYPE_HTTPS,
4319 ssl_options,
[email protected]6cdfd7f2013-02-08 20:40:154320 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:444321 ASSERT_TRUE(test_server.Start());
4322
4323 // We require that the URL be www.google.com in order to pick up the
4324 // preloaded and dynamic HSTS and public key pin entries in the
4325 // TransportSecurityState. This means that we have to use a
4326 // MockHostResolver in order to direct www.google.com to the testserver.
[email protected]ceefd7fd2012-11-29 00:36:244327 // By default, MockHostResolver maps all hosts to 127.0.0.1.
[email protected]316c1e5e2012-09-12 15:17:444328
4329 MockHostResolver host_resolver;
[email protected]ceefd7fd2012-11-29 00:36:244330 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]316c1e5e2012-09-12 15:17:444331 TestURLRequestContext context(true);
4332 context.set_network_delegate(&network_delegate);
4333 context.set_host_resolver(&host_resolver);
4334 TransportSecurityState transport_security_state;
4335 TransportSecurityState::DomainState domain_state;
4336 EXPECT_TRUE(transport_security_state.GetDomainState("www.google.com", true,
4337 &domain_state));
4338 context.set_transport_security_state(&transport_security_state);
4339 context.Init();
4340
4341 TestDelegate d;
4342 URLRequest r(GURL(StringPrintf("https://www.google.com:%d",
4343 test_server.host_port_pair().port())),
4344 &d,
4345 &context);
4346
4347 r.Start();
4348 EXPECT_TRUE(r.is_pending());
4349
4350 MessageLoop::current()->Run();
4351
4352 EXPECT_EQ(1, d.response_started_count());
4353 EXPECT_FALSE(d.received_data_before_response());
4354 EXPECT_TRUE(d.have_certificate_errors());
4355 EXPECT_TRUE(d.certificate_errors_are_fatal());
4356
4357 // Get a fresh copy of the state, and check that it hasn't been updated.
4358 TransportSecurityState::DomainState new_domain_state;
4359 EXPECT_TRUE(transport_security_state.GetDomainState("www.google.com", true,
4360 &new_domain_state));
4361 EXPECT_EQ(new_domain_state.upgrade_mode, domain_state.upgrade_mode);
4362 EXPECT_EQ(new_domain_state.include_subdomains,
4363 domain_state.include_subdomains);
4364 EXPECT_TRUE(FingerprintsEqual(new_domain_state.static_spki_hashes,
4365 domain_state.static_spki_hashes));
4366 EXPECT_TRUE(FingerprintsEqual(new_domain_state.dynamic_spki_hashes,
4367 domain_state.dynamic_spki_hashes));
4368 EXPECT_TRUE(FingerprintsEqual(new_domain_state.bad_static_spki_hashes,
4369 domain_state.bad_static_spki_hashes));
4370}
4371
[email protected]8ccc69f2012-11-28 19:52:144372// Make sure HSTS preserves a POST request's method and body.
4373TEST_F(HTTPSRequestTest, HSTSPreservesPosts) {
4374 static const char kData[] = "hello world";
4375
4376 TestServer::SSLOptions ssl_options(TestServer::SSLOptions::CERT_OK);
4377 TestServer test_server(TestServer::TYPE_HTTPS,
4378 ssl_options,
[email protected]6cdfd7f2013-02-08 20:40:154379 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]8ccc69f2012-11-28 19:52:144380 ASSERT_TRUE(test_server.Start());
4381
4382
4383 // Per spec, TransportSecurityState expects a domain name, rather than an IP
4384 // address, so a MockHostResolver is needed to redirect www.somewhere.com to
[email protected]ceefd7fd2012-11-29 00:36:244385 // the TestServer. By default, MockHostResolver maps all hosts to 127.0.0.1.
[email protected]8ccc69f2012-11-28 19:52:144386 MockHostResolver host_resolver;
[email protected]8ccc69f2012-11-28 19:52:144387
4388 // Force https for www.somewhere.com.
4389 TransportSecurityState transport_security_state;
4390 net::TransportSecurityState::DomainState domain_state;
4391 domain_state.upgrade_expiry =
4392 domain_state.created + base::TimeDelta::FromDays(1000);
4393 transport_security_state.EnableHost("www.somewhere.com", domain_state);
4394
4395 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
4396
4397 TestURLRequestContext context(true);
4398 context.set_host_resolver(&host_resolver);
4399 context.set_transport_security_state(&transport_security_state);
4400 context.set_network_delegate(&network_delegate);
4401 context.Init();
4402
4403 TestDelegate d;
4404 // Navigating to https://www.somewhere.com instead of https://127.0.0.1 will
4405 // cause a certificate error. Ignore the error.
4406 d.set_allow_certificate_errors(true);
4407
4408 URLRequest req(GURL(StringPrintf("http://www.somewhere.com:%d/echo",
4409 test_server.host_port_pair().port())),
4410 &d,
4411 &context);
4412 req.set_method("POST");
[email protected]f288ef02012-12-15 20:28:284413 req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
[email protected]8ccc69f2012-11-28 19:52:144414
4415 req.Start();
4416 MessageLoop::current()->Run();
4417
4418 EXPECT_EQ("https", req.url().scheme());
4419 EXPECT_EQ("POST", req.method());
4420 EXPECT_EQ(kData, d.data_received());
4421}
4422
[email protected]316c1e5e2012-09-12 15:17:444423TEST_F(HTTPSRequestTest, SSLv3Fallback) {
4424 TestServer::SSLOptions ssl_options(
4425 TestServer::SSLOptions::CERT_OK);
4426 ssl_options.tls_intolerant = TestServer::SSLOptions::TLS_INTOLERANT_ALL;
4427 TestServer test_server(TestServer::TYPE_HTTPS,
4428 ssl_options,
[email protected]6cdfd7f2013-02-08 20:40:154429 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:444430 ASSERT_TRUE(test_server.Start());
4431
4432 TestDelegate d;
4433 TestURLRequestContext context(true);
4434 context.Init();
4435 d.set_allow_certificate_errors(true);
4436 URLRequest r(test_server.GetURL(""), &d, &context);
4437 r.Start();
4438
4439 MessageLoop::current()->Run();
4440
4441 EXPECT_EQ(1, d.response_started_count());
4442 EXPECT_NE(0, d.bytes_received());
4443 EXPECT_EQ(static_cast<int>(SSL_CONNECTION_VERSION_SSL3),
4444 SSLConnectionStatusToVersion(r.ssl_info().connection_status));
4445 EXPECT_TRUE(r.ssl_info().connection_status & SSL_CONNECTION_VERSION_FALLBACK);
4446}
4447
4448namespace {
4449
4450class SSLClientAuthTestDelegate : public TestDelegate {
4451 public:
4452 SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) {
4453 }
4454 virtual void OnCertificateRequested(
4455 URLRequest* request,
[email protected]46fadfd2013-02-06 09:40:164456 SSLCertRequestInfo* cert_request_info) OVERRIDE {
[email protected]316c1e5e2012-09-12 15:17:444457 on_certificate_requested_count_++;
4458 MessageLoop::current()->Quit();
4459 }
4460 int on_certificate_requested_count() {
4461 return on_certificate_requested_count_;
4462 }
4463 private:
4464 int on_certificate_requested_count_;
4465};
4466
4467} // namespace
4468
4469// TODO(davidben): Test the rest of the code. Specifically,
4470// - Filtering which certificates to select.
4471// - Sending a certificate back.
4472// - Getting a certificate request in an SSL renegotiation sending the
4473// HTTP request.
4474TEST_F(HTTPSRequestTest, ClientAuthTest) {
4475 TestServer::SSLOptions ssl_options;
4476 ssl_options.request_client_certificate = true;
4477 TestServer test_server(TestServer::TYPE_HTTPS,
4478 ssl_options,
[email protected]6cdfd7f2013-02-08 20:40:154479 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:444480 ASSERT_TRUE(test_server.Start());
4481
4482 SSLClientAuthTestDelegate d;
4483 {
4484 URLRequest r(test_server.GetURL(""), &d, &default_context_);
4485
4486 r.Start();
4487 EXPECT_TRUE(r.is_pending());
4488
4489 MessageLoop::current()->Run();
4490
4491 EXPECT_EQ(1, d.on_certificate_requested_count());
4492 EXPECT_FALSE(d.received_data_before_response());
4493 EXPECT_EQ(0, d.bytes_received());
4494
4495 // Send no certificate.
4496 // TODO(davidben): Get temporary client cert import (with keys) working on
4497 // all platforms so we can test sending a cert as well.
4498 r.ContinueWithCertificate(NULL);
4499
4500 MessageLoop::current()->Run();
4501
4502 EXPECT_EQ(1, d.response_started_count());
4503 EXPECT_FALSE(d.received_data_before_response());
4504 EXPECT_NE(0, d.bytes_received());
4505 }
4506}
4507
4508TEST_F(HTTPSRequestTest, ResumeTest) {
4509 // Test that we attempt a session resume when making two connections to the
4510 // same host.
4511 TestServer::SSLOptions ssl_options;
4512 ssl_options.record_resume = true;
4513 TestServer test_server(TestServer::TYPE_HTTPS,
4514 ssl_options,
[email protected]6cdfd7f2013-02-08 20:40:154515 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:444516 ASSERT_TRUE(test_server.Start());
4517
4518 SSLClientSocket::ClearSessionCache();
4519
4520 {
4521 TestDelegate d;
4522 URLRequest r(
4523 test_server.GetURL("ssl-session-cache"), &d, &default_context_);
4524
4525 r.Start();
4526 EXPECT_TRUE(r.is_pending());
4527
4528 MessageLoop::current()->Run();
4529
4530 EXPECT_EQ(1, d.response_started_count());
4531 }
4532
4533 reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())->
4534 CloseAllConnections();
4535
4536 {
4537 TestDelegate d;
4538 URLRequest r(
4539 test_server.GetURL("ssl-session-cache"), &d, &default_context_);
4540
4541 r.Start();
4542 EXPECT_TRUE(r.is_pending());
4543
4544 MessageLoop::current()->Run();
4545
4546 // The response will look like;
4547 // insert abc
4548 // lookup abc
4549 // insert xyz
4550 //
4551 // With a newline at the end which makes the split think that there are
4552 // four lines.
4553
4554 EXPECT_EQ(1, d.response_started_count());
4555 std::vector<std::string> lines;
4556 base::SplitString(d.data_received(), '\n', &lines);
4557 ASSERT_EQ(4u, lines.size()) << d.data_received();
4558
4559 std::string session_id;
4560
4561 for (size_t i = 0; i < 2; i++) {
4562 std::vector<std::string> parts;
4563 base::SplitString(lines[i], '\t', &parts);
4564 ASSERT_EQ(2u, parts.size());
4565 if (i == 0) {
4566 EXPECT_EQ("insert", parts[0]);
4567 session_id = parts[1];
4568 } else {
4569 EXPECT_EQ("lookup", parts[0]);
4570 EXPECT_EQ(session_id, parts[1]);
4571 }
4572 }
4573 }
4574}
4575
4576TEST_F(HTTPSRequestTest, SSLSessionCacheShardTest) {
4577 // Test that sessions aren't resumed when the value of ssl_session_cache_shard
4578 // differs.
4579 TestServer::SSLOptions ssl_options;
4580 ssl_options.record_resume = true;
4581 TestServer test_server(TestServer::TYPE_HTTPS,
4582 ssl_options,
[email protected]6cdfd7f2013-02-08 20:40:154583 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:444584 ASSERT_TRUE(test_server.Start());
4585
4586 SSLClientSocket::ClearSessionCache();
4587
4588 {
4589 TestDelegate d;
4590 URLRequest r(
4591 test_server.GetURL("ssl-session-cache"), &d, &default_context_);
4592
4593 r.Start();
4594 EXPECT_TRUE(r.is_pending());
4595
4596 MessageLoop::current()->Run();
4597
4598 EXPECT_EQ(1, d.response_started_count());
4599 }
4600
4601 // Now create a new HttpCache with a different ssl_session_cache_shard value.
4602 HttpNetworkSession::Params params;
4603 params.host_resolver = default_context_.host_resolver();
4604 params.cert_verifier = default_context_.cert_verifier();
4605 params.proxy_service = default_context_.proxy_service();
4606 params.ssl_config_service = default_context_.ssl_config_service();
4607 params.http_auth_handler_factory =
4608 default_context_.http_auth_handler_factory();
4609 params.network_delegate = default_context_.network_delegate();
4610 params.http_server_properties = default_context_.http_server_properties();
4611 params.ssl_session_cache_shard = "alternate";
4612
4613 scoped_ptr<net::HttpCache> cache(new net::HttpCache(
4614 new net::HttpNetworkSession(params),
4615 net::HttpCache::DefaultBackend::InMemory(0)));
4616
4617 default_context_.set_http_transaction_factory(cache.get());
4618
4619 {
4620 TestDelegate d;
4621 URLRequest r(
4622 test_server.GetURL("ssl-session-cache"), &d, &default_context_);
4623
4624 r.Start();
4625 EXPECT_TRUE(r.is_pending());
4626
4627 MessageLoop::current()->Run();
4628
4629 // The response will look like;
4630 // insert abc
4631 // insert xyz
4632 //
4633 // With a newline at the end which makes the split think that there are
4634 // three lines.
4635
4636 EXPECT_EQ(1, d.response_started_count());
4637 std::vector<std::string> lines;
4638 base::SplitString(d.data_received(), '\n', &lines);
4639 ASSERT_EQ(3u, lines.size());
4640
4641 std::string session_id;
4642 for (size_t i = 0; i < 2; i++) {
4643 std::vector<std::string> parts;
4644 base::SplitString(lines[i], '\t', &parts);
4645 ASSERT_EQ(2u, parts.size());
4646 EXPECT_EQ("insert", parts[0]);
4647 if (i == 0) {
4648 session_id = parts[1];
4649 } else {
4650 EXPECT_NE(session_id, parts[1]);
4651 }
4652 }
4653 }
4654}
4655
[email protected]a13234c2012-03-20 21:45:024656class TestSSLConfigService : public SSLConfigService {
[email protected]dffe8242012-03-20 15:14:274657 public:
[email protected]a13234c2012-03-20 21:45:024658 TestSSLConfigService(bool ev_enabled, bool online_rev_checking)
4659 : ev_enabled_(ev_enabled),
4660 online_rev_checking_(online_rev_checking) {
4661 }
4662
[email protected]a9813302012-04-28 09:29:284663 // SSLConfigService:
4664 virtual void GetSSLConfig(SSLConfig* config) OVERRIDE {
[email protected]dffe8242012-03-20 15:14:274665 *config = SSLConfig();
[email protected]a13234c2012-03-20 21:45:024666 config->rev_checking_enabled = online_rev_checking_;
4667 config->verify_ev_cert = ev_enabled_;
[email protected]dffe8242012-03-20 15:14:274668 }
[email protected]a13234c2012-03-20 21:45:024669
[email protected]a9813302012-04-28 09:29:284670 protected:
4671 virtual ~TestSSLConfigService() {}
4672
[email protected]a13234c2012-03-20 21:45:024673 private:
4674 const bool ev_enabled_;
4675 const bool online_rev_checking_;
[email protected]dffe8242012-03-20 15:14:274676};
4677
4678// This the fingerprint of the "Testing CA" certificate used by the testserver.
4679// See net/data/ssl/certificates/ocsp-test-root.pem.
[email protected]ede03212012-09-07 12:52:264680static const SHA1HashValue kOCSPTestCertFingerprint =
[email protected]dffe8242012-03-20 15:14:274681 { { 0xf1, 0xad, 0xf6, 0xce, 0x42, 0xac, 0xe7, 0xb4, 0xf4, 0x24,
4682 0xdb, 0x1a, 0xf7, 0xa0, 0x9f, 0x09, 0xa1, 0xea, 0xf1, 0x5c } };
4683
4684// This is the policy OID contained in the certificates that testserver
4685// generates.
4686static const char kOCSPTestCertPolicy[] = "1.3.6.1.4.1.11129.2.4.1";
4687
4688class HTTPSOCSPTest : public HTTPSRequestTest {
4689 public:
4690 HTTPSOCSPTest()
[email protected]ef2bf422012-05-11 03:27:094691 : context_(true),
[email protected]b6f2de32012-08-17 04:35:084692 ev_test_policy_(
4693 new ScopedTestEVPolicy(EVRootCAMetadata::GetInstance(),
4694 kOCSPTestCertFingerprint,
4695 kOCSPTestCertPolicy)) {
[email protected]a13234c2012-03-20 21:45:024696 }
4697
4698 virtual void SetUp() OVERRIDE {
[email protected]ef2bf422012-05-11 03:27:094699 SetupContext(&context_);
4700 context_.Init();
[email protected]dffe8242012-03-20 15:14:274701
4702 scoped_refptr<net::X509Certificate> root_cert =
4703 ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem");
4704 CHECK_NE(static_cast<X509Certificate*>(NULL), root_cert);
4705 test_root_.reset(new ScopedTestRoot(root_cert));
4706
[email protected]a592c0432012-12-01 18:10:294707#if defined(USE_NSS) || defined(OS_IOS)
[email protected]ef2bf422012-05-11 03:27:094708 SetURLRequestContextForNSSHttpIO(&context_);
[email protected]dffe8242012-03-20 15:14:274709 EnsureNSSHttpIOInit();
4710#endif
4711 }
4712
[email protected]295e5cd2012-08-23 01:05:124713 void DoConnection(const TestServer::SSLOptions& ssl_options,
[email protected]dffe8242012-03-20 15:14:274714 CertStatus* out_cert_status) {
[email protected]924e9f92012-12-16 22:00:534715 // We always overwrite out_cert_status.
4716 *out_cert_status = 0;
[email protected]295e5cd2012-08-23 01:05:124717 TestServer test_server(TestServer::TYPE_HTTPS,
4718 ssl_options,
[email protected]6cdfd7f2013-02-08 20:40:154719 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]dffe8242012-03-20 15:14:274720 ASSERT_TRUE(test_server.Start());
4721
4722 TestDelegate d;
4723 d.set_allow_certificate_errors(true);
[email protected]94e2bbe2012-06-22 15:26:134724 URLRequest r(test_server.GetURL(""), &d, &context_);
[email protected]dffe8242012-03-20 15:14:274725 r.Start();
4726
4727 MessageLoop::current()->Run();
4728
4729 EXPECT_EQ(1, d.response_started_count());
4730 *out_cert_status = r.ssl_info().cert_status;
4731 }
4732
[email protected]46fadfd2013-02-06 09:40:164733 virtual ~HTTPSOCSPTest() {
[email protected]a592c0432012-12-01 18:10:294734#if defined(USE_NSS) || defined(OS_IOS)
[email protected]dffe8242012-03-20 15:14:274735 ShutdownNSSHttpIO();
4736#endif
4737 }
4738
[email protected]a13234c2012-03-20 21:45:024739 protected:
4740 // SetupContext configures the URLRequestContext that will be used for making
4741 // connetions to testserver. This can be overridden in test subclasses for
4742 // different behaviour.
4743 virtual void SetupContext(URLRequestContext* context) {
4744 context->set_ssl_config_service(
4745 new TestSSLConfigService(true /* check for EV */,
4746 true /* online revocation checking */));
4747 }
4748
[email protected]dffe8242012-03-20 15:14:274749 scoped_ptr<ScopedTestRoot> test_root_;
[email protected]ef2bf422012-05-11 03:27:094750 TestURLRequestContext context_;
[email protected]b6f2de32012-08-17 04:35:084751 scoped_ptr<ScopedTestEVPolicy> ev_test_policy_;
[email protected]dffe8242012-03-20 15:14:274752};
4753
[email protected]a13234c2012-03-20 21:45:024754static CertStatus ExpectedCertStatusForFailedOnlineRevocationCheck() {
[email protected]05454a432012-03-20 20:04:014755#if defined(OS_WIN)
[email protected]a13234c2012-03-20 21:45:024756 // Windows can return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION but we don't
4757 // have that ability on other platforms.
4758 return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION;
4759#else
4760 return 0;
4761#endif
4762}
4763
4764// SystemUsesChromiumEVMetadata returns true iff the current operating system
4765// uses Chromium's EV metadata (i.e. EVRootCAMetadata). If it does not, then
4766// several tests are effected because our testing EV certificate won't be
4767// recognised as EV.
4768static bool SystemUsesChromiumEVMetadata() {
[email protected]5c504192012-03-27 19:00:574769#if defined(USE_OPENSSL)
4770 // http://crbug.com/117478 - OpenSSL does not support EV validation.
4771 return false;
[email protected]a592c0432012-12-01 18:10:294772#elif defined(OS_MACOSX) && !defined(OS_IOS)
[email protected]a13234c2012-03-20 21:45:024773 // On OS X, we use the system to tell us whether a certificate is EV or not
4774 // and the system won't recognise our testing root.
[email protected]05454a432012-03-20 20:04:014775 return false;
4776#else
4777 return true;
4778#endif
4779}
4780
[email protected]b6f2de32012-08-17 04:35:084781static bool SystemSupportsOCSP() {
[email protected]5c504192012-03-27 19:00:574782#if defined(USE_OPENSSL)
4783 // http://crbug.com/117478 - OpenSSL does not support OCSP.
4784 return false;
4785#elif defined(OS_WIN)
[email protected]a13234c2012-03-20 21:45:024786 return base::win::GetVersion() >= base::win::VERSION_VISTA;
4787#elif defined(OS_ANDROID)
4788 // TODO(jnd): http://crbug.com/117478 - EV verification is not yet supported.
4789 return false;
4790#else
4791 return true;
4792#endif
4793}
4794
[email protected]dffe8242012-03-20 15:14:274795TEST_F(HTTPSOCSPTest, Valid) {
4796 if (!SystemSupportsOCSP()) {
4797 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
4798 return;
4799 }
4800
[email protected]295e5cd2012-08-23 01:05:124801 TestServer::SSLOptions ssl_options(TestServer::SSLOptions::CERT_AUTO);
4802 ssl_options.ocsp_status = TestServer::SSLOptions::OCSP_OK;
[email protected]dffe8242012-03-20 15:14:274803
[email protected]924e9f92012-12-16 22:00:534804 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:124805 DoConnection(ssl_options, &cert_status);
[email protected]dffe8242012-03-20 15:14:274806
4807 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
4808
[email protected]a13234c2012-03-20 21:45:024809 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
4810 static_cast<bool>(cert_status & CERT_STATUS_IS_EV));
[email protected]dffe8242012-03-20 15:14:274811
4812 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
4813}
4814
4815TEST_F(HTTPSOCSPTest, Revoked) {
4816 if (!SystemSupportsOCSP()) {
4817 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
4818 return;
4819 }
4820
[email protected]295e5cd2012-08-23 01:05:124821 TestServer::SSLOptions ssl_options(
4822 TestServer::SSLOptions::CERT_AUTO);
4823 ssl_options.ocsp_status = TestServer::SSLOptions::OCSP_REVOKED;
[email protected]dffe8242012-03-20 15:14:274824
4825 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:124826 DoConnection(ssl_options, &cert_status);
[email protected]dffe8242012-03-20 15:14:274827
[email protected]a592c0432012-12-01 18:10:294828#if !(defined(OS_MACOSX) && !defined(OS_IOS))
[email protected]dffe8242012-03-20 15:14:274829 // Doesn't pass on OS X yet for reasons that need to be investigated.
4830 EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS);
4831#endif
4832 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
4833 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
4834}
4835
4836TEST_F(HTTPSOCSPTest, Invalid) {
4837 if (!SystemSupportsOCSP()) {
4838 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
4839 return;
4840 }
4841
[email protected]295e5cd2012-08-23 01:05:124842 TestServer::SSLOptions ssl_options(
4843 TestServer::SSLOptions::CERT_AUTO);
4844 ssl_options.ocsp_status = TestServer::SSLOptions::OCSP_INVALID;
[email protected]dffe8242012-03-20 15:14:274845
[email protected]924e9f92012-12-16 22:00:534846 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:124847 DoConnection(ssl_options, &cert_status);
[email protected]dffe8242012-03-20 15:14:274848
[email protected]a13234c2012-03-20 21:45:024849 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
[email protected]dffe8242012-03-20 15:14:274850 cert_status & CERT_STATUS_ALL_ERRORS);
[email protected]dffe8242012-03-20 15:14:274851
4852 // Without a positive OCSP response, we shouldn't show the EV status.
4853 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
4854 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
4855}
[email protected]a13234c2012-03-20 21:45:024856
4857class HTTPSEVCRLSetTest : public HTTPSOCSPTest {
4858 protected:
4859 virtual void SetupContext(URLRequestContext* context) OVERRIDE {
4860 context->set_ssl_config_service(
4861 new TestSSLConfigService(true /* check for EV */,
4862 false /* online revocation checking */));
4863 }
4864};
4865
4866TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndInvalidOCSP) {
4867 if (!SystemSupportsOCSP()) {
4868 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
4869 return;
4870 }
4871
[email protected]295e5cd2012-08-23 01:05:124872 TestServer::SSLOptions ssl_options(
4873 TestServer::SSLOptions::CERT_AUTO);
4874 ssl_options.ocsp_status = TestServer::SSLOptions::OCSP_INVALID;
[email protected]a13234c2012-03-20 21:45:024875 SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>());
4876
[email protected]924e9f92012-12-16 22:00:534877 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:124878 DoConnection(ssl_options, &cert_status);
[email protected]a13234c2012-03-20 21:45:024879
4880 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
4881 cert_status & CERT_STATUS_ALL_ERRORS);
4882
4883 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
[email protected]b6f2de32012-08-17 04:35:084884 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
4885 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
[email protected]a13234c2012-03-20 21:45:024886}
4887
4888TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndGoodOCSP) {
4889 if (!SystemSupportsOCSP()) {
4890 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
4891 return;
4892 }
4893
[email protected]295e5cd2012-08-23 01:05:124894 TestServer::SSLOptions ssl_options(
4895 TestServer::SSLOptions::CERT_AUTO);
4896 ssl_options.ocsp_status = TestServer::SSLOptions::OCSP_OK;
[email protected]a13234c2012-03-20 21:45:024897 SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>());
4898
4899 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:124900 DoConnection(ssl_options, &cert_status);
[email protected]a13234c2012-03-20 21:45:024901
4902 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
4903
4904 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
4905 static_cast<bool>(cert_status & CERT_STATUS_IS_EV));
[email protected]b6f2de32012-08-17 04:35:084906 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
4907 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
[email protected]a13234c2012-03-20 21:45:024908}
4909
4910TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSet) {
4911 if (!SystemSupportsOCSP()) {
4912 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
4913 return;
4914 }
4915
[email protected]295e5cd2012-08-23 01:05:124916 TestServer::SSLOptions ssl_options(
4917 TestServer::SSLOptions::CERT_AUTO);
4918 ssl_options.ocsp_status = TestServer::SSLOptions::OCSP_INVALID;
[email protected]a13234c2012-03-20 21:45:024919 SSLConfigService::SetCRLSet(
4920 scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting()));
4921
4922 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:124923 DoConnection(ssl_options, &cert_status);
[email protected]a13234c2012-03-20 21:45:024924
4925 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
4926 cert_status & CERT_STATUS_ALL_ERRORS);
4927
4928 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
[email protected]b6f2de32012-08-17 04:35:084929 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
4930 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
[email protected]a13234c2012-03-20 21:45:024931}
4932
4933TEST_F(HTTPSEVCRLSetTest, FreshCRLSet) {
[email protected]295e5cd2012-08-23 01:05:124934 TestServer::SSLOptions ssl_options(
4935 TestServer::SSLOptions::CERT_AUTO);
4936 ssl_options.ocsp_status = TestServer::SSLOptions::OCSP_INVALID;
[email protected]a13234c2012-03-20 21:45:024937 SSLConfigService::SetCRLSet(
4938 scoped_refptr<CRLSet>(CRLSet::EmptyCRLSetForTesting()));
4939
[email protected]924e9f92012-12-16 22:00:534940 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:124941 DoConnection(ssl_options, &cert_status);
[email protected]a13234c2012-03-20 21:45:024942
4943 // With a valid, fresh CRLSet the bad OCSP response shouldn't matter because
4944 // we wont check it.
4945 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
4946
4947 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
4948 static_cast<bool>(cert_status & CERT_STATUS_IS_EV));
4949
4950 EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
4951}
4952
[email protected]b6f2de32012-08-17 04:35:084953TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSetAndRevokedNonEVCert) {
4954 // Test that when EV verification is requested, but online revocation
4955 // checking is disabled, and the leaf certificate is not in fact EV, that
4956 // no revocation checking actually happens.
4957 if (!SystemSupportsOCSP()) {
4958 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
4959 return;
4960 }
4961
4962 // Unmark the certificate's OID as EV, which should disable revocation
4963 // checking (as per the user preference)
4964 ev_test_policy_.reset();
4965
[email protected]295e5cd2012-08-23 01:05:124966 TestServer::SSLOptions ssl_options(
4967 TestServer::SSLOptions::CERT_AUTO);
4968 ssl_options.ocsp_status = TestServer::SSLOptions::OCSP_REVOKED;
[email protected]b6f2de32012-08-17 04:35:084969 SSLConfigService::SetCRLSet(
4970 scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting()));
4971
4972 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:124973 DoConnection(ssl_options, &cert_status);
[email protected]b6f2de32012-08-17 04:35:084974
4975 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
4976
4977 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
4978 EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
4979}
4980
[email protected]a13234c2012-03-20 21:45:024981class HTTPSCRLSetTest : public HTTPSOCSPTest {
4982 protected:
4983 virtual void SetupContext(URLRequestContext* context) OVERRIDE {
4984 context->set_ssl_config_service(
4985 new TestSSLConfigService(false /* check for EV */,
4986 false /* online revocation checking */));
4987 }
4988};
4989
4990TEST_F(HTTPSCRLSetTest, ExpiredCRLSet) {
[email protected]295e5cd2012-08-23 01:05:124991 TestServer::SSLOptions ssl_options(
4992 TestServer::SSLOptions::CERT_AUTO);
4993 ssl_options.ocsp_status = TestServer::SSLOptions::OCSP_INVALID;
[email protected]a13234c2012-03-20 21:45:024994 SSLConfigService::SetCRLSet(
4995 scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting()));
4996
[email protected]924e9f92012-12-16 22:00:534997 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:124998 DoConnection(ssl_options, &cert_status);
[email protected]a13234c2012-03-20 21:45:024999
5000 // If we're not trying EV verification then, even if the CRLSet has expired,
5001 // we don't fall back to online revocation checks.
5002 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
5003 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
5004 EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
5005}
[email protected]316c1e5e2012-09-12 15:17:445006#endif // !defined(OS_IOS)
[email protected]dffe8242012-03-20 15:14:275007
[email protected]316c1e5e2012-09-12 15:17:445008#if !defined(DISABLE_FTP_SUPPORT)
[email protected]b89ca032009-08-31 21:41:315009class URLRequestTestFTP : public URLRequestTest {
[email protected]95409e12010-08-17 20:07:115010 public:
[email protected]d9fca99a2012-02-24 16:16:205011 URLRequestTestFTP()
[email protected]6cdfd7f2013-02-08 20:40:155012 : test_server_(TestServer::TYPE_FTP, TestServer::kLocalhost,
5013 base::FilePath()) {
[email protected]95409e12010-08-17 20:07:115014 }
5015
[email protected]b89ca032009-08-31 21:41:315016 protected:
[email protected]7461a402011-03-24 23:19:515017 TestServer test_server_;
[email protected]b89ca032009-08-31 21:41:315018};
5019
[email protected]d2a133182012-08-05 16:44:085020// Make sure an FTP request using an unsafe ports fails.
[email protected]f2f31b32013-01-16 23:24:095021TEST_F(URLRequestTestFTP, UnsafePort) {
[email protected]d2a133182012-08-05 16:44:085022 ASSERT_TRUE(test_server_.Start());
5023
[email protected]9d5730b2012-08-24 17:42:495024 URLRequestJobFactoryImpl job_factory;
[email protected]d2a133182012-08-05 16:44:085025
5026 GURL url("ftp://127.0.0.1:7");
5027 FtpProtocolHandler ftp_protocol_handler(
[email protected]d2a133182012-08-05 16:44:085028 default_context_.ftp_transaction_factory(),
5029 default_context_.ftp_auth_cache());
5030 job_factory.SetProtocolHandler(
5031 "ftp",
[email protected]9f170462012-08-24 01:06:585032 new FtpProtocolHandler(default_context_.ftp_transaction_factory(),
[email protected]d2a133182012-08-05 16:44:085033 default_context_.ftp_auth_cache()));
5034 default_context_.set_job_factory(&job_factory);
5035
5036 TestDelegate d;
5037 {
5038 URLRequest r(url, &d, &default_context_);
5039 r.Start();
5040 EXPECT_TRUE(r.is_pending());
5041
5042 MessageLoop::current()->Run();
5043
5044 EXPECT_FALSE(r.is_pending());
5045 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
5046 EXPECT_EQ(ERR_UNSAFE_PORT, r.status().error());
5047 }
5048}
5049
[email protected]5accf7332009-11-24 03:41:385050// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:125051TEST_F(URLRequestTestFTP, DISABLED_FTPDirectoryListing) {
[email protected]95409e12010-08-17 20:07:115052 ASSERT_TRUE(test_server_.Start());
5053
[email protected]a25e90e2009-09-09 17:05:375054 TestDelegate d;
5055 {
[email protected]94e2bbe2012-06-22 15:26:135056 URLRequest r(test_server_.GetURL("/"), &d, &default_context_);
[email protected]a25e90e2009-09-09 17:05:375057 r.Start();
5058 EXPECT_TRUE(r.is_pending());
5059
5060 MessageLoop::current()->Run();
5061
5062 EXPECT_FALSE(r.is_pending());
5063 EXPECT_EQ(1, d.response_started_count());
5064 EXPECT_FALSE(d.received_data_before_response());
5065 EXPECT_LT(0, d.bytes_received());
[email protected]6d81b482011-02-22 19:47:195066 EXPECT_EQ(test_server_.host_port_pair().host(),
5067 r.GetSocketAddress().host());
5068 EXPECT_EQ(test_server_.host_port_pair().port(),
5069 r.GetSocketAddress().port());
[email protected]a25e90e2009-09-09 17:05:375070 }
5071}
5072
[email protected]7df70012010-02-04 00:09:555073// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:125074TEST_F(URLRequestTestFTP, DISABLED_FTPGetTestAnonymous) {
[email protected]95409e12010-08-17 20:07:115075 ASSERT_TRUE(test_server_.Start());
5076
[email protected]6cdfd7f2013-02-08 20:40:155077 base::FilePath app_path;
[email protected]dd265012009-01-08 20:45:275078 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:025079 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:275080 TestDelegate d;
5081 {
[email protected]94e2bbe2012-06-22 15:26:135082 URLRequest r(test_server_.GetURL("/LICENSE"), &d, &default_context_);
[email protected]dd265012009-01-08 20:45:275083 r.Start();
5084 EXPECT_TRUE(r.is_pending());
5085
5086 MessageLoop::current()->Run();
5087
5088 int64 file_size = 0;
5089 file_util::GetFileSize(app_path, &file_size);
5090
[email protected]ba2f3342009-07-30 18:08:425091 EXPECT_FALSE(r.is_pending());
[email protected]dd265012009-01-08 20:45:275092 EXPECT_EQ(1, d.response_started_count());
5093 EXPECT_FALSE(d.received_data_before_response());
5094 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
[email protected]6d81b482011-02-22 19:47:195095 EXPECT_EQ(test_server_.host_port_pair().host(),
5096 r.GetSocketAddress().host());
5097 EXPECT_EQ(test_server_.host_port_pair().port(),
5098 r.GetSocketAddress().port());
[email protected]dd265012009-01-08 20:45:275099 }
5100}
5101
[email protected]e9ecbd12009-12-20 18:44:405102// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:125103TEST_F(URLRequestTestFTP, DISABLED_FTPGetTest) {
[email protected]95409e12010-08-17 20:07:115104 ASSERT_TRUE(test_server_.Start());
5105
[email protected]6cdfd7f2013-02-08 20:40:155106 base::FilePath app_path;
[email protected]dd265012009-01-08 20:45:275107 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:025108 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:275109 TestDelegate d;
5110 {
[email protected]ef2bf422012-05-11 03:27:095111 URLRequest r(
[email protected]95409e12010-08-17 20:07:115112 test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", "chrome"),
[email protected]94e2bbe2012-06-22 15:26:135113 &d,
5114 &default_context_);
[email protected]dd265012009-01-08 20:45:275115 r.Start();
5116 EXPECT_TRUE(r.is_pending());
5117
5118 MessageLoop::current()->Run();
5119
5120 int64 file_size = 0;
5121 file_util::GetFileSize(app_path, &file_size);
5122
[email protected]ba2f3342009-07-30 18:08:425123 EXPECT_FALSE(r.is_pending());
[email protected]6d81b482011-02-22 19:47:195124 EXPECT_EQ(test_server_.host_port_pair().host(),
5125 r.GetSocketAddress().host());
5126 EXPECT_EQ(test_server_.host_port_pair().port(),
5127 r.GetSocketAddress().port());
[email protected]dd265012009-01-08 20:45:275128 EXPECT_EQ(1, d.response_started_count());
5129 EXPECT_FALSE(d.received_data_before_response());
5130 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
[email protected]58e32bb2013-01-21 18:23:255131
5132 LoadTimingInfo load_timing_info;
[email protected]0a703912013-02-02 04:25:175133 r.GetLoadTimingInfo(&load_timing_info);
[email protected]58e32bb2013-01-21 18:23:255134 TestLoadTimingNoHttpConnection(load_timing_info);
[email protected]dd265012009-01-08 20:45:275135 }
5136}
5137
[email protected]49abd652010-08-05 05:04:535138// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:125139TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPassword) {
[email protected]95409e12010-08-17 20:07:115140 ASSERT_TRUE(test_server_.Start());
5141
[email protected]6cdfd7f2013-02-08 20:40:155142 base::FilePath app_path;
[email protected]dd265012009-01-08 20:45:275143 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:025144 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:275145 TestDelegate d;
5146 {
[email protected]ef2bf422012-05-11 03:27:095147 URLRequest r(
[email protected]95409e12010-08-17 20:07:115148 test_server_.GetURLWithUserAndPassword("/LICENSE",
5149 "chrome",
5150 "wrong_password"),
[email protected]94e2bbe2012-06-22 15:26:135151 &d,
5152 &default_context_);
[email protected]dd265012009-01-08 20:45:275153 r.Start();
5154 EXPECT_TRUE(r.is_pending());
5155
5156 MessageLoop::current()->Run();
5157
5158 int64 file_size = 0;
5159 file_util::GetFileSize(app_path, &file_size);
5160
[email protected]ba2f3342009-07-30 18:08:425161 EXPECT_FALSE(r.is_pending());
[email protected]dd265012009-01-08 20:45:275162 EXPECT_EQ(1, d.response_started_count());
5163 EXPECT_FALSE(d.received_data_before_response());
5164 EXPECT_EQ(d.bytes_received(), 0);
5165 }
5166}
5167
[email protected]cde4e80d2009-10-16 19:58:155168// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:125169TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPasswordRestart) {
[email protected]95409e12010-08-17 20:07:115170 ASSERT_TRUE(test_server_.Start());
5171
[email protected]6cdfd7f2013-02-08 20:40:155172 base::FilePath app_path;
[email protected]8b8a197d2009-08-26 15:57:585173 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
5174 app_path = app_path.AppendASCII("LICENSE");
5175 TestDelegate d;
5176 // Set correct login credentials. The delegate will be asked for them when
5177 // the initial login with wrong credentials will fail.
[email protected]f3cf9802011-10-28 18:44:585178 d.set_credentials(AuthCredentials(kChrome, kChrome));
[email protected]8b8a197d2009-08-26 15:57:585179 {
[email protected]ef2bf422012-05-11 03:27:095180 URLRequest r(
[email protected]95409e12010-08-17 20:07:115181 test_server_.GetURLWithUserAndPassword("/LICENSE",
5182 "chrome",
5183 "wrong_password"),
[email protected]94e2bbe2012-06-22 15:26:135184 &d,
5185 &default_context_);
[email protected]8b8a197d2009-08-26 15:57:585186 r.Start();
5187 EXPECT_TRUE(r.is_pending());
5188
5189 MessageLoop::current()->Run();
5190
5191 int64 file_size = 0;
5192 file_util::GetFileSize(app_path, &file_size);
5193
5194 EXPECT_FALSE(r.is_pending());
5195 EXPECT_EQ(1, d.response_started_count());
5196 EXPECT_FALSE(d.received_data_before_response());
5197 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
5198 }
5199}
5200
[email protected]49abd652010-08-05 05:04:535201// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:125202TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUser) {
[email protected]95409e12010-08-17 20:07:115203 ASSERT_TRUE(test_server_.Start());
5204
[email protected]6cdfd7f2013-02-08 20:40:155205 base::FilePath app_path;
[email protected]dd265012009-01-08 20:45:275206 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:025207 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:275208 TestDelegate d;
5209 {
[email protected]ef2bf422012-05-11 03:27:095210 URLRequest r(
[email protected]95409e12010-08-17 20:07:115211 test_server_.GetURLWithUserAndPassword("/LICENSE",
5212 "wrong_user",
5213 "chrome"),
[email protected]94e2bbe2012-06-22 15:26:135214 &d,
5215 &default_context_);
[email protected]dd265012009-01-08 20:45:275216 r.Start();
5217 EXPECT_TRUE(r.is_pending());
5218
5219 MessageLoop::current()->Run();
5220
5221 int64 file_size = 0;
5222 file_util::GetFileSize(app_path, &file_size);
5223
[email protected]ba2f3342009-07-30 18:08:425224 EXPECT_FALSE(r.is_pending());
[email protected]dd265012009-01-08 20:45:275225 EXPECT_EQ(1, d.response_started_count());
5226 EXPECT_FALSE(d.received_data_before_response());
5227 EXPECT_EQ(d.bytes_received(), 0);
5228 }
5229}
[email protected]8b8a197d2009-08-26 15:57:585230
[email protected]cde4e80d2009-10-16 19:58:155231// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:125232TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUserRestart) {
[email protected]95409e12010-08-17 20:07:115233 ASSERT_TRUE(test_server_.Start());
5234
[email protected]6cdfd7f2013-02-08 20:40:155235 base::FilePath app_path;
[email protected]8b8a197d2009-08-26 15:57:585236 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
5237 app_path = app_path.AppendASCII("LICENSE");
5238 TestDelegate d;
5239 // Set correct login credentials. The delegate will be asked for them when
5240 // the initial login with wrong credentials will fail.
[email protected]f3cf9802011-10-28 18:44:585241 d.set_credentials(AuthCredentials(kChrome, kChrome));
[email protected]8b8a197d2009-08-26 15:57:585242 {
[email protected]ef2bf422012-05-11 03:27:095243 URLRequest r(
[email protected]95409e12010-08-17 20:07:115244 test_server_.GetURLWithUserAndPassword("/LICENSE",
5245 "wrong_user",
5246 "chrome"),
[email protected]94e2bbe2012-06-22 15:26:135247 &d,
5248 &default_context_);
[email protected]8b8a197d2009-08-26 15:57:585249 r.Start();
5250 EXPECT_TRUE(r.is_pending());
5251
5252 MessageLoop::current()->Run();
5253
5254 int64 file_size = 0;
5255 file_util::GetFileSize(app_path, &file_size);
5256
5257 EXPECT_FALSE(r.is_pending());
5258 EXPECT_EQ(1, d.response_started_count());
5259 EXPECT_FALSE(d.received_data_before_response());
5260 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
5261 }
5262}
[email protected]60a3df52009-09-22 16:13:245263
[email protected]cde4e80d2009-10-16 19:58:155264// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:125265TEST_F(URLRequestTestFTP, DISABLED_FTPCacheURLCredentials) {
[email protected]95409e12010-08-17 20:07:115266 ASSERT_TRUE(test_server_.Start());
5267
[email protected]6cdfd7f2013-02-08 20:40:155268 base::FilePath app_path;
[email protected]60a3df52009-09-22 16:13:245269 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
5270 app_path = app_path.AppendASCII("LICENSE");
5271
5272 scoped_ptr<TestDelegate> d(new TestDelegate);
5273 {
5274 // Pass correct login identity in the URL.
[email protected]ef2bf422012-05-11 03:27:095275 URLRequest r(
[email protected]95409e12010-08-17 20:07:115276 test_server_.GetURLWithUserAndPassword("/LICENSE",
5277 "chrome",
5278 "chrome"),
[email protected]94e2bbe2012-06-22 15:26:135279 d.get(),
5280 &default_context_);
[email protected]60a3df52009-09-22 16:13:245281 r.Start();
5282 EXPECT_TRUE(r.is_pending());
5283
5284 MessageLoop::current()->Run();
5285
5286 int64 file_size = 0;
5287 file_util::GetFileSize(app_path, &file_size);
5288
5289 EXPECT_FALSE(r.is_pending());
5290 EXPECT_EQ(1, d->response_started_count());
5291 EXPECT_FALSE(d->received_data_before_response());
5292 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
5293 }
5294
5295 d.reset(new TestDelegate);
5296 {
5297 // This request should use cached identity from previous request.
[email protected]94e2bbe2012-06-22 15:26:135298 URLRequest r(test_server_.GetURL("/LICENSE"), d.get(), &default_context_);
[email protected]60a3df52009-09-22 16:13:245299 r.Start();
5300 EXPECT_TRUE(r.is_pending());
5301
5302 MessageLoop::current()->Run();
5303
5304 int64 file_size = 0;
5305 file_util::GetFileSize(app_path, &file_size);
5306
5307 EXPECT_FALSE(r.is_pending());
5308 EXPECT_EQ(1, d->response_started_count());
5309 EXPECT_FALSE(d->received_data_before_response());
5310 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
5311 }
5312}
5313
[email protected]cde4e80d2009-10-16 19:58:155314// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:125315TEST_F(URLRequestTestFTP, DISABLED_FTPCacheLoginBoxCredentials) {
[email protected]95409e12010-08-17 20:07:115316 ASSERT_TRUE(test_server_.Start());
5317
[email protected]6cdfd7f2013-02-08 20:40:155318 base::FilePath app_path;
[email protected]60a3df52009-09-22 16:13:245319 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
5320 app_path = app_path.AppendASCII("LICENSE");
5321
5322 scoped_ptr<TestDelegate> d(new TestDelegate);
5323 // Set correct login credentials. The delegate will be asked for them when
5324 // the initial login with wrong credentials will fail.
[email protected]f3cf9802011-10-28 18:44:585325 d->set_credentials(AuthCredentials(kChrome, kChrome));
[email protected]60a3df52009-09-22 16:13:245326 {
[email protected]ef2bf422012-05-11 03:27:095327 URLRequest r(
[email protected]95409e12010-08-17 20:07:115328 test_server_.GetURLWithUserAndPassword("/LICENSE",
5329 "chrome",
5330 "wrong_password"),
[email protected]94e2bbe2012-06-22 15:26:135331 d.get(),
5332 &default_context_);
[email protected]60a3df52009-09-22 16:13:245333 r.Start();
5334 EXPECT_TRUE(r.is_pending());
5335
5336 MessageLoop::current()->Run();
5337
5338 int64 file_size = 0;
5339 file_util::GetFileSize(app_path, &file_size);
5340
5341 EXPECT_FALSE(r.is_pending());
5342 EXPECT_EQ(1, d->response_started_count());
5343 EXPECT_FALSE(d->received_data_before_response());
5344 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
5345 }
5346
5347 // Use a new delegate without explicit credentials. The cached ones should be
5348 // used.
5349 d.reset(new TestDelegate);
5350 {
5351 // Don't pass wrong credentials in the URL, they would override valid cached
5352 // ones.
[email protected]94e2bbe2012-06-22 15:26:135353 URLRequest r(test_server_.GetURL("/LICENSE"), d.get(), &default_context_);
[email protected]60a3df52009-09-22 16:13:245354 r.Start();
5355 EXPECT_TRUE(r.is_pending());
5356
5357 MessageLoop::current()->Run();
5358
5359 int64 file_size = 0;
5360 file_util::GetFileSize(app_path, &file_size);
5361
5362 EXPECT_FALSE(r.is_pending());
5363 EXPECT_EQ(1, d->response_started_count());
5364 EXPECT_FALSE(d->received_data_before_response());
5365 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
5366 }
5367}
[email protected]316c1e5e2012-09-12 15:17:445368#endif // !defined(DISABLE_FTP_SUPPORT)
[email protected]7461a402011-03-24 23:19:515369
5370} // namespace net