blob: 0d7c06615a72867e9461aeb6ad0724f66314d07b [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]ebbad602011-01-21 23:30:4725#include "base/string_util.h"
[email protected]d8eb84242010-09-25 02:25:0626#include "base/stringprintf.h"
[email protected]d069c11a2013-04-13 00:01:5527#include "base/strings/string_piece.h"
[email protected]d778e0422013-03-06 18:10:2228#include "base/strings/string_split.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"
initial.commit586acc5fe2008-07-26 22:42:5231#include "net/base/load_flags.h"
[email protected]58e32bb2013-01-21 18:23:2532#include "net/base/load_timing_info.h"
33#include "net/base/load_timing_info_test_util.h"
[email protected]d8eb84242010-09-25 02:25:0634#include "net/base/net_errors.h"
[email protected]9e743cd2010-03-16 07:03:5335#include "net/base/net_log.h"
36#include "net/base/net_log_unittest.h"
initial.commit586acc5fe2008-07-26 22:42:5237#include "net/base/net_module.h"
38#include "net/base/net_util.h"
[email protected]42fdb452012-11-01 12:44:4039#include "net/base/test_data_directory.h"
[email protected]f288ef02012-12-15 20:28:2840#include "net/base/upload_bytes_element_reader.h"
41#include "net/base/upload_data_stream.h"
42#include "net/base/upload_file_element_reader.h"
[email protected]6e7845ae2013-03-29 21:48:1143#include "net/cert/ev_root_ca_metadata.h"
44#include "net/cert/test_root_certs.h"
[email protected]aa84a7e2012-03-15 21:29:0645#include "net/cookies/cookie_monster.h"
46#include "net/cookies/cookie_store_test_helpers.h"
initial.commit586acc5fe2008-07-26 22:42:5247#include "net/disk_cache/disk_cache.h"
[email protected]f2cb3cf2013-03-21 01:40:5348#include "net/dns/mock_host_resolver.h"
[email protected]ba2f3342009-07-30 18:08:4249#include "net/ftp/ftp_network_layer.h"
initial.commit586acc5fe2008-07-26 22:42:5250#include "net/http/http_cache.h"
51#include "net/http/http_network_layer.h"
[email protected]c3456bb2011-12-12 22:22:1952#include "net/http/http_network_session.h"
[email protected]88e6b6f32010-05-07 23:14:2553#include "net/http/http_request_headers.h"
[email protected]319d9e6f2009-02-18 19:47:2154#include "net/http/http_response_headers.h"
[email protected]dffe8242012-03-20 15:14:2755#include "net/ocsp/nss_ocsp.h"
[email protected]63de95b2008-12-10 04:11:2756#include "net/proxy/proxy_service.h"
[email protected]c3456bb2011-12-12 22:22:1957#include "net/socket/ssl_client_socket.h"
[email protected]536fd0b2013-03-14 17:41:5758#include "net/ssl/ssl_connection_status_flags.h"
[email protected]6e7845ae2013-03-29 21:48:1159#include "net/test/cert_test_util.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]42cba2fb2013-03-29 19:58:5784const base::string16 kChrome(ASCIIToUTF16("chrome"));
85const base::string16 kSecret(ASCIIToUTF16("secret"));
86const base::string16 kUser(ASCIIToUTF16("user"));
[email protected]13c8a092010-07-29 06:15:4487
[email protected]2bba3252013-04-08 19:50:5988// Tests load timing information in the case a fresh connection was used, with
89// no proxy.
[email protected]58e32bb2013-01-21 18:23:2590void TestLoadTimingNotReused(const net::LoadTimingInfo& load_timing_info,
91 int connect_timing_flags) {
92 EXPECT_FALSE(load_timing_info.socket_reused);
93 EXPECT_NE(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
94
95 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
96 EXPECT_FALSE(load_timing_info.request_start.is_null());
97
98 EXPECT_LE(load_timing_info.request_start,
99 load_timing_info.connect_timing.connect_start);
100 ExpectConnectTimingHasTimes(load_timing_info.connect_timing,
101 connect_timing_flags);
102 EXPECT_LE(load_timing_info.connect_timing.connect_end,
103 load_timing_info.send_start);
104 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
105 EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
106
[email protected]58e32bb2013-01-21 18:23:25107 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null());
108 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null());
109}
110
[email protected]2bba3252013-04-08 19:50:59111// Same as above, but with proxy times.
112void TestLoadTimingNotReusedWithProxy(
113 const net::LoadTimingInfo& load_timing_info,
114 int connect_timing_flags) {
115 EXPECT_FALSE(load_timing_info.socket_reused);
116 EXPECT_NE(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
117
118 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
119 EXPECT_FALSE(load_timing_info.request_start.is_null());
120
121 EXPECT_LE(load_timing_info.request_start,
122 load_timing_info.proxy_resolve_start);
123 EXPECT_LE(load_timing_info.proxy_resolve_start,
124 load_timing_info.proxy_resolve_end);
125 EXPECT_LE(load_timing_info.proxy_resolve_end,
126 load_timing_info.connect_timing.connect_start);
127 ExpectConnectTimingHasTimes(load_timing_info.connect_timing,
128 connect_timing_flags);
129 EXPECT_LE(load_timing_info.connect_timing.connect_end,
130 load_timing_info.send_start);
131 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
132 EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
133}
134
135// Same as above, but with a reused socket and proxy times.
136void TestLoadTimingReusedWithProxy(
137 const net::LoadTimingInfo& load_timing_info) {
138 EXPECT_TRUE(load_timing_info.socket_reused);
139 EXPECT_NE(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
140
141 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
142 EXPECT_FALSE(load_timing_info.request_start.is_null());
143
144 ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing);
145
146 EXPECT_LE(load_timing_info.request_start,
147 load_timing_info.proxy_resolve_start);
148 EXPECT_LE(load_timing_info.proxy_resolve_start,
149 load_timing_info.proxy_resolve_end);
150 EXPECT_LE(load_timing_info.proxy_resolve_end,
151 load_timing_info.send_start);
152 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
153 EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
154}
155
[email protected]58e32bb2013-01-21 18:23:25156// Tests load timing in the case that there is no underlying connection. This
157// can be used to test in the case of cached responses, errors, or non-HTTP
158// requests.
159void TestLoadTimingNoHttpConnection(
160 const net::LoadTimingInfo& load_timing_info) {
161 EXPECT_FALSE(load_timing_info.socket_reused);
162 EXPECT_EQ(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
163
164 // Only the request times should be non-null.
165 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
166 EXPECT_FALSE(load_timing_info.request_start.is_null());
167
168 ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing);
169
170 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null());
171 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null());
172 EXPECT_TRUE(load_timing_info.send_start.is_null());
173 EXPECT_TRUE(load_timing_info.send_end.is_null());
174 EXPECT_TRUE(load_timing_info.receive_headers_end.is_null());
175}
176
[email protected]8a16266e2009-09-10 21:08:39177base::StringPiece TestNetResourceProvider(int key) {
initial.commit586acc5fe2008-07-26 22:42:52178 return "header";
179}
180
[email protected]71c64f62008-11-15 04:36:51181// Do a case-insensitive search through |haystack| for |needle|.
182bool ContainsString(const std::string& haystack, const char* needle) {
183 std::string::const_iterator it =
184 std::search(haystack.begin(),
185 haystack.end(),
186 needle,
187 needle + strlen(needle),
[email protected]07f1cee2010-11-03 03:53:35188 base::CaseInsensitiveCompare<char>());
[email protected]71c64f62008-11-15 04:36:51189 return it != haystack.end();
190}
191
[email protected]661376a2009-04-29 02:04:23192void FillBuffer(char* buffer, size_t len) {
193 static bool called = false;
194 if (!called) {
195 called = true;
196 int seed = static_cast<int>(Time::Now().ToInternalValue());
197 srand(seed);
198 }
199
200 for (size_t i = 0; i < len; i++) {
201 buffer[i] = static_cast<char>(rand());
202 if (!buffer[i])
203 buffer[i] = 'g';
204 }
205}
206
[email protected]f288ef02012-12-15 20:28:28207UploadDataStream* CreateSimpleUploadData(const char* data) {
208 scoped_ptr<UploadElementReader> reader(
209 new UploadBytesElementReader(data, strlen(data)));
210 return UploadDataStream::CreateWithReader(reader.Pass(), 0);
[email protected]195e77d2009-07-23 19:10:23211}
212
[email protected]96adadb2010-08-28 01:16:17213// Verify that the SSLInfo of a successful SSL connection has valid values.
[email protected]7461a402011-03-24 23:19:51214void CheckSSLInfo(const SSLInfo& ssl_info) {
[email protected]89af3d92010-10-15 20:31:37215 // Allow ChromeFrame fake SSLInfo to get through.
[email protected]c679be22010-08-29 15:20:05216 if (ssl_info.cert.get() &&
[email protected]89af3d92010-10-15 20:31:37217 ssl_info.cert.get()->issuer().GetDisplayName() == "Chrome Internal") {
218 // -1 means unknown.
219 EXPECT_EQ(ssl_info.security_bits, -1);
[email protected]c679be22010-08-29 15:20:05220 return;
[email protected]89af3d92010-10-15 20:31:37221 }
222
[email protected]96adadb2010-08-28 01:16:17223 // -1 means unknown. 0 means no encryption.
224 EXPECT_GT(ssl_info.security_bits, 0);
225
226 // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated.
[email protected]7461a402011-03-24 23:19:51227 int cipher_suite = SSLConnectionStatusToCipherSuite(
[email protected]96adadb2010-08-28 01:16:17228 ssl_info.connection_status);
229 EXPECT_NE(0, cipher_suite);
230}
231
[email protected]ede03212012-09-07 12:52:26232bool FingerprintsEqual(const HashValueVector& a, const HashValueVector& b) {
[email protected]69d7ff442012-02-13 22:41:27233 size_t size = a.size();
234
235 if (size != b.size())
236 return false;
237
238 for (size_t i = 0; i < size; ++i) {
239 if (!a[i].Equals(b[i]))
240 return false;
241 }
242
243 return true;
244}
245
[email protected]dc5a5cf2012-09-26 02:49:30246// A network delegate that allows the user to choose a subset of request stages
247// to block in. When blocking, the delegate can do one of the following:
248// * synchronously return a pre-specified error code, or
249// * asynchronously return that value via an automatically called callback,
250// or
251// * block and wait for the user to do a callback.
252// Additionally, the user may also specify a redirect URL -- then each request
253// with the current URL different from the redirect target will be redirected
254// to that target, in the on-before-URL-request stage, independent of whether
255// the delegate blocks in ON_BEFORE_URL_REQUEST or not.
[email protected]4c76d7c2011-04-15 19:14:12256class BlockingNetworkDelegate : public TestNetworkDelegate {
257 public:
[email protected]dc5a5cf2012-09-26 02:49:30258 // Stages in which the delegate can block.
259 enum Stage {
[email protected]9045b8822012-01-13 20:35:35260 NOT_BLOCKED = 0,
261 ON_BEFORE_URL_REQUEST = 1 << 0,
262 ON_BEFORE_SEND_HEADERS = 1 << 1,
263 ON_HEADERS_RECEIVED = 1 << 2,
264 ON_AUTH_REQUIRED = 1 << 3
265 };
266
[email protected]dc5a5cf2012-09-26 02:49:30267 // Behavior during blocked stages. During other stages, just
268 // returns net::OK or NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION.
269 enum BlockMode {
270 SYNCHRONOUS, // No callback, returns specified return values.
271 AUTO_CALLBACK, // |this| posts a task to run the callback using the
272 // specified return codes.
273 USER_CALLBACK, // User takes care of doing a callback. |retval_| and
274 // |auth_retval_| are ignored. In every blocking stage the
275 // message loop is quit.
276 };
277
278 // Creates a delegate which does not block at all.
279 explicit BlockingNetworkDelegate(BlockMode block_mode);
280
281 // For users to trigger a callback returning |response|.
282 // Side-effects: resets |stage_blocked_for_callback_| and stored callbacks.
283 // Only call if |block_mode_| == USER_CALLBACK.
284 void DoCallback(int response);
285 void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response);
286
287 // Setters.
288 void set_retval(int retval) {
289 ASSERT_NE(USER_CALLBACK, block_mode_);
290 ASSERT_NE(ERR_IO_PENDING, retval);
291 ASSERT_NE(OK, retval);
292 retval_ = retval;
[email protected]9045b8822012-01-13 20:35:35293 }
294
[email protected]dc5a5cf2012-09-26 02:49:30295 // If |auth_retval| == AUTH_REQUIRED_RESPONSE_SET_AUTH, then
296 // |auth_credentials_| will be passed with the response.
297 void set_auth_retval(AuthRequiredResponse auth_retval) {
298 ASSERT_NE(USER_CALLBACK, block_mode_);
299 ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, auth_retval);
300 auth_retval_ = auth_retval;
301 }
302 void set_auth_credentials(const AuthCredentials& auth_credentials) {
303 auth_credentials_ = auth_credentials;
[email protected]9045b8822012-01-13 20:35:35304 }
305
[email protected]dc5a5cf2012-09-26 02:49:30306 void set_redirect_url(const GURL& url) {
307 redirect_url_ = url;
[email protected]9045b8822012-01-13 20:35:35308 }
309
[email protected]dc5a5cf2012-09-26 02:49:30310 void set_block_on(int block_on) {
311 block_on_ = block_on;
[email protected]9045b8822012-01-13 20:35:35312 }
313
[email protected]dc5a5cf2012-09-26 02:49:30314 // Allows the user to check in which state did we block.
315 Stage stage_blocked_for_callback() const {
316 EXPECT_EQ(USER_CALLBACK, block_mode_);
317 return stage_blocked_for_callback_;
[email protected]9045b8822012-01-13 20:35:35318 }
319
320 private:
[email protected]dc5a5cf2012-09-26 02:49:30321 void RunCallback(int response, const CompletionCallback& callback);
322 void RunAuthCallback(AuthRequiredResponse response,
323 const AuthCallback& callback);
324
[email protected]9045b8822012-01-13 20:35:35325 // TestNetworkDelegate implementation.
326 virtual int OnBeforeURLRequest(URLRequest* request,
327 const CompletionCallback& callback,
[email protected]dc5a5cf2012-09-26 02:49:30328 GURL* new_url) OVERRIDE;
[email protected]9045b8822012-01-13 20:35:35329
330 virtual int OnBeforeSendHeaders(URLRequest* request,
331 const CompletionCallback& callback,
[email protected]dc5a5cf2012-09-26 02:49:30332 HttpRequestHeaders* headers) OVERRIDE;
[email protected]9045b8822012-01-13 20:35:35333
334 virtual int OnHeadersReceived(
335 URLRequest* request,
336 const CompletionCallback& callback,
[email protected]507af8f2012-10-20 00:42:32337 const HttpResponseHeaders* original_response_headers,
[email protected]dc5a5cf2012-09-26 02:49:30338 scoped_refptr<HttpResponseHeaders>* override_response_headers) OVERRIDE;
[email protected]9045b8822012-01-13 20:35:35339
340 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired(
341 URLRequest* request,
342 const AuthChallengeInfo& auth_info,
343 const AuthCallback& callback,
[email protected]dc5a5cf2012-09-26 02:49:30344 AuthCredentials* credentials) OVERRIDE;
[email protected]9045b8822012-01-13 20:35:35345
[email protected]dc5a5cf2012-09-26 02:49:30346 // Resets the callbacks and |stage_blocked_for_callback_|.
347 void Reset();
[email protected]9045b8822012-01-13 20:35:35348
[email protected]dc5a5cf2012-09-26 02:49:30349 // Checks whether we should block in |stage|. If yes, returns an error code
350 // and optionally sets up callback based on |block_mode_|. If no, returns OK.
351 int MaybeBlockStage(Stage stage, const CompletionCallback& callback);
352
353 // Configuration parameters, can be adjusted by public methods:
354 const BlockMode block_mode_;
355
356 // Values returned on blocking stages when mode is SYNCHRONOUS or
357 // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING.
358 int retval_; // To be returned in non-auth stages.
359 AuthRequiredResponse auth_retval_;
360
361 GURL redirect_url_; // Used if non-empty.
362 int block_on_; // Bit mask: in which stages to block.
363
364 // |auth_credentials_| will be copied to |*target_auth_credential_| on
365 // callback.
366 AuthCredentials auth_credentials_;
367 AuthCredentials* target_auth_credentials_;
368
369 // Internal variables, not set by not the user:
370 // Last blocked stage waiting for user callback (unused if |block_mode_| !=
371 // USER_CALLBACK).
372 Stage stage_blocked_for_callback_;
373
374 // Callback objects stored during blocking stages.
[email protected]9045b8822012-01-13 20:35:35375 CompletionCallback callback_;
376 AuthCallback auth_callback_;
[email protected]dc5a5cf2012-09-26 02:49:30377
378 base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_;
379
380 DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate);
[email protected]9045b8822012-01-13 20:35:35381};
382
[email protected]dc5a5cf2012-09-26 02:49:30383BlockingNetworkDelegate::BlockingNetworkDelegate(BlockMode block_mode)
384 : block_mode_(block_mode),
385 retval_(OK),
386 auth_retval_(AUTH_REQUIRED_RESPONSE_NO_ACTION),
387 block_on_(0),
388 target_auth_credentials_(NULL),
389 stage_blocked_for_callback_(NOT_BLOCKED),
390 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
391}
392
393void BlockingNetworkDelegate::DoCallback(int response) {
394 ASSERT_EQ(USER_CALLBACK, block_mode_);
395 ASSERT_NE(NOT_BLOCKED, stage_blocked_for_callback_);
396 ASSERT_NE(ON_AUTH_REQUIRED, stage_blocked_for_callback_);
397 CompletionCallback callback = callback_;
398 Reset();
399 RunCallback(response, callback);
400}
401
402void BlockingNetworkDelegate::DoAuthCallback(
403 NetworkDelegate::AuthRequiredResponse response) {
404 ASSERT_EQ(USER_CALLBACK, block_mode_);
405 ASSERT_EQ(ON_AUTH_REQUIRED, stage_blocked_for_callback_);
406 AuthCallback auth_callback = auth_callback_;
407 Reset();
408 RunAuthCallback(response, auth_callback);
409}
410
411void BlockingNetworkDelegate::RunCallback(int response,
412 const CompletionCallback& callback) {
413 callback.Run(response);
414}
415
416void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response,
417 const AuthCallback& callback) {
418 if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) {
419 ASSERT_TRUE(target_auth_credentials_ != NULL);
420 *target_auth_credentials_ = auth_credentials_;
421 }
422 callback.Run(response);
423}
424
425int BlockingNetworkDelegate::OnBeforeURLRequest(
426 URLRequest* request,
427 const CompletionCallback& callback,
428 GURL* new_url) {
429 if (redirect_url_ == request->url())
430 return OK; // We've already seen this request and redirected elsewhere.
431
432 TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url);
433
434 if (!redirect_url_.is_empty())
435 *new_url = redirect_url_;
436
437 return MaybeBlockStage(ON_BEFORE_URL_REQUEST, callback);
438}
439
440int BlockingNetworkDelegate::OnBeforeSendHeaders(
441 URLRequest* request,
442 const CompletionCallback& callback,
443 HttpRequestHeaders* headers) {
444 TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers);
445
446 return MaybeBlockStage(ON_BEFORE_SEND_HEADERS, callback);
447}
448
449int BlockingNetworkDelegate::OnHeadersReceived(
450 URLRequest* request,
451 const CompletionCallback& callback,
[email protected]507af8f2012-10-20 00:42:32452 const HttpResponseHeaders* original_response_headers,
[email protected]dc5a5cf2012-09-26 02:49:30453 scoped_refptr<HttpResponseHeaders>* override_response_headers) {
454 TestNetworkDelegate::OnHeadersReceived(
455 request, callback, original_response_headers,
456 override_response_headers);
457
458 return MaybeBlockStage(ON_HEADERS_RECEIVED, callback);
459}
460
461NetworkDelegate::AuthRequiredResponse BlockingNetworkDelegate::OnAuthRequired(
462 URLRequest* request,
463 const AuthChallengeInfo& auth_info,
464 const AuthCallback& callback,
465 AuthCredentials* credentials) {
466 TestNetworkDelegate::OnAuthRequired(request, auth_info, callback,
467 credentials);
468 // Check that the user has provided callback for the previous blocked stage.
469 EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_);
470
471 if ((block_on_ & ON_AUTH_REQUIRED) == 0) {
472 return AUTH_REQUIRED_RESPONSE_NO_ACTION;
473 }
474
475 target_auth_credentials_ = credentials;
476
477 switch (block_mode_) {
478 case SYNCHRONOUS:
479 if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH)
480 *target_auth_credentials_ = auth_credentials_;
481 return auth_retval_;
482
483 case AUTO_CALLBACK:
484 MessageLoop::current()->PostTask(
485 FROM_HERE,
486 base::Bind(&BlockingNetworkDelegate::RunAuthCallback,
487 weak_factory_.GetWeakPtr(), auth_retval_, callback));
488 return AUTH_REQUIRED_RESPONSE_IO_PENDING;
489
490 case USER_CALLBACK:
491 auth_callback_ = callback;
492 stage_blocked_for_callback_ = ON_AUTH_REQUIRED;
493 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
494 return AUTH_REQUIRED_RESPONSE_IO_PENDING;
495 }
496 NOTREACHED();
497 return AUTH_REQUIRED_RESPONSE_NO_ACTION; // Dummy value.
498}
499
500void BlockingNetworkDelegate::Reset() {
501 EXPECT_NE(NOT_BLOCKED, stage_blocked_for_callback_);
502 stage_blocked_for_callback_ = NOT_BLOCKED;
503 callback_.Reset();
504 auth_callback_.Reset();
505}
506
507int BlockingNetworkDelegate::MaybeBlockStage(
508 BlockingNetworkDelegate::Stage stage,
509 const CompletionCallback& callback) {
510 // Check that the user has provided callback for the previous blocked stage.
511 EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_);
512
513 if ((block_on_ & stage) == 0) {
514 return OK;
515 }
516
517 switch (block_mode_) {
518 case SYNCHRONOUS:
519 EXPECT_NE(OK, retval_);
520 return retval_;
521
522 case AUTO_CALLBACK:
523 MessageLoop::current()->PostTask(
524 FROM_HERE,
525 base::Bind(&BlockingNetworkDelegate::RunCallback,
526 weak_factory_.GetWeakPtr(), retval_, callback));
527 return ERR_IO_PENDING;
528
529 case USER_CALLBACK:
530 callback_ = callback;
531 stage_blocked_for_callback_ = stage;
532 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
533 return ERR_IO_PENDING;
534 }
535 NOTREACHED();
536 return 0;
537}
538
[email protected]d5a4dd62012-05-23 01:41:04539class TestURLRequestContextWithProxy : public TestURLRequestContext {
540 public:
541 // Does not own |delegate|.
542 TestURLRequestContextWithProxy(const std::string& proxy,
543 NetworkDelegate* delegate)
544 : TestURLRequestContext(true) {
545 context_storage_.set_proxy_service(ProxyService::CreateFixed(proxy));
546 set_network_delegate(delegate);
547 Init();
548 }
549 virtual ~TestURLRequestContextWithProxy() {}
550};
551
552} // namespace
553
[email protected]a592c0432012-12-01 18:10:29554// Inherit PlatformTest since we require the autorelease pool on Mac OS X.
[email protected]7a0bb4bf2008-11-19 21:41:48555class URLRequestTest : public PlatformTest {
[email protected]abb26092010-11-11 22:19:00556 public:
[email protected]ef2bf422012-05-11 03:27:09557 URLRequestTest() : default_context_(true) {
558 default_context_.set_network_delegate(&default_network_delegate_);
[email protected]58e32bb2013-01-21 18:23:25559 default_context_.set_net_log(&net_log_);
[email protected]ef2bf422012-05-11 03:27:09560 default_context_.Init();
[email protected]87a09a92011-07-14 15:50:50561 }
[email protected]f53b4802012-12-20 17:04:23562 virtual ~URLRequestTest() {}
[email protected]87a09a92011-07-14 15:50:50563
[email protected]3c5ca8c2011-09-29 01:14:51564 // Adds the TestJobInterceptor to the default context.
565 TestJobInterceptor* AddTestInterceptor() {
[email protected]f53b4802012-12-20 17:04:23566 TestJobInterceptor* protocol_handler_ = new TestJobInterceptor();
[email protected]e13c1462013-03-19 03:05:50567 job_factory_.reset(new URLRequestJobFactoryImpl);
568 job_factory_->SetProtocolHandler("http", protocol_handler_);
569 default_context_.set_job_factory(job_factory_.get());
[email protected]f53b4802012-12-20 17:04:23570 return protocol_handler_;
[email protected]3c5ca8c2011-09-29 01:14:51571 }
572
[email protected]87a09a92011-07-14 15:50:50573 protected:
[email protected]58e32bb2013-01-21 18:23:25574 CapturingNetLog net_log_;
[email protected]ceefd7fd2012-11-29 00:36:24575 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest.
[email protected]e13c1462013-03-19 03:05:50576 scoped_ptr<URLRequestJobFactoryImpl> job_factory_;
[email protected]ef2bf422012-05-11 03:27:09577 TestURLRequestContext default_context_;
[email protected]7a0bb4bf2008-11-19 21:41:48578};
579
[email protected]316c1e5e2012-09-12 15:17:44580TEST_F(URLRequestTest, AboutBlankTest) {
581 TestDelegate d;
582 {
583 URLRequest r(GURL("about:blank"), &d, &default_context_);
584
585 r.Start();
586 EXPECT_TRUE(r.is_pending());
587
588 MessageLoop::current()->Run();
589
590 EXPECT_TRUE(!r.is_pending());
591 EXPECT_FALSE(d.received_data_before_response());
592 EXPECT_EQ(d.bytes_received(), 0);
593 EXPECT_EQ("", r.GetSocketAddress().host());
594 EXPECT_EQ(0, r.GetSocketAddress().port());
595 }
596}
597
598TEST_F(URLRequestTest, DataURLImageTest) {
599 TestDelegate d;
600 {
601 // Use our nice little Chrome logo.
602 URLRequest r(GURL(
603 "data:image/png;base64,"
604 "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUBBjG3"
605 "w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQxIEVxitD"
606 "5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI5MpCVdW0gO7t"
607 "vNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKzMx1+fg9bAgK6zHq9"
608 "cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7CkIMDxQpF7r/MWq12UctI1"
609 "dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0WYUqt57yWf2McHTObYPbVD+Z"
610 "wbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2HQQcm43XwmtoYM6vVKleh0uoWvnzW"
611 "3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nkDFHF9ZS+uYVjRUasMeHUmyLYtcklTvzW"
612 "GFZnNOXczThvpKIzjcahSqIzkvDLayDq6D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTb"
613 "xXX1T5xsV9tm9r4TQwHLiZw/pdDZJea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5"
614 "MrVvMzNaI3+ERHfrFzPKQukrQGI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gV"
615 "Md6q5c8GdosynKmSeRuGzpjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq"
616 "/uxxZKzNiZFGD0wRC3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7F"
617 "Ux1qLnV7MGF40smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufB"
618 "JbsysjjW4kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJM"
619 "nlm2O34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfm"
620 "moRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV5En"
621 "hORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="),
622 &d,
623 &default_context_);
624
625 r.Start();
626 EXPECT_TRUE(r.is_pending());
627
628 MessageLoop::current()->Run();
629
630 EXPECT_TRUE(!r.is_pending());
631 EXPECT_FALSE(d.received_data_before_response());
632 EXPECT_EQ(d.bytes_received(), 911);
633 EXPECT_EQ("", r.GetSocketAddress().host());
634 EXPECT_EQ(0, r.GetSocketAddress().port());
635 }
636}
637
638TEST_F(URLRequestTest, FileTest) {
[email protected]6cdfd7f2013-02-08 20:40:15639 base::FilePath app_path;
[email protected]316c1e5e2012-09-12 15:17:44640 PathService::Get(base::FILE_EXE, &app_path);
641 GURL app_url = FilePathToFileURL(app_path);
642
643 TestDelegate d;
644 {
645 URLRequest r(app_url, &d, &default_context_);
646
647 r.Start();
648 EXPECT_TRUE(r.is_pending());
649
650 MessageLoop::current()->Run();
651
652 int64 file_size = -1;
653 EXPECT_TRUE(file_util::GetFileSize(app_path, &file_size));
654
655 EXPECT_TRUE(!r.is_pending());
656 EXPECT_EQ(1, d.response_started_count());
657 EXPECT_FALSE(d.received_data_before_response());
658 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
659 EXPECT_EQ("", r.GetSocketAddress().host());
660 EXPECT_EQ(0, r.GetSocketAddress().port());
661 }
662}
663
[email protected]ba40bb762012-12-17 07:11:04664TEST_F(URLRequestTest, FileTestCancel) {
[email protected]6cdfd7f2013-02-08 20:40:15665 base::FilePath app_path;
[email protected]ba40bb762012-12-17 07:11:04666 PathService::Get(base::FILE_EXE, &app_path);
667 GURL app_url = FilePathToFileURL(app_path);
668
669 TestDelegate d;
670 {
671 URLRequest r(app_url, &d, &default_context_);
672
673 r.Start();
674 EXPECT_TRUE(r.is_pending());
675 r.Cancel();
676 }
677 // Async cancelation should be safe even when URLRequest has been already
678 // destroyed.
679 MessageLoop::current()->RunUntilIdle();
680}
681
[email protected]316c1e5e2012-09-12 15:17:44682TEST_F(URLRequestTest, FileTestFullSpecifiedRange) {
683 const size_t buffer_size = 4000;
[email protected]4356f0f2013-04-07 00:58:17684 scoped_ptr<char[]> buffer(new char[buffer_size]);
[email protected]316c1e5e2012-09-12 15:17:44685 FillBuffer(buffer.get(), buffer_size);
686
[email protected]6cdfd7f2013-02-08 20:40:15687 base::FilePath temp_path;
[email protected]316c1e5e2012-09-12 15:17:44688 EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path));
689 GURL temp_url = FilePathToFileURL(temp_path);
690 EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size));
691
692 int64 file_size;
693 EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size));
694
695 const size_t first_byte_position = 500;
696 const size_t last_byte_position = buffer_size - first_byte_position;
697 const size_t content_length = last_byte_position - first_byte_position + 1;
698 std::string partial_buffer_string(buffer.get() + first_byte_position,
699 buffer.get() + last_byte_position + 1);
700
701 TestDelegate d;
702 {
703 URLRequest r(temp_url, &d, &default_context_);
704
705 HttpRequestHeaders headers;
706 headers.SetHeader(HttpRequestHeaders::kRange,
707 base::StringPrintf(
708 "bytes=%" PRIuS "-%" PRIuS,
709 first_byte_position, last_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, FileTestHalfSpecifiedRange) {
727 const size_t buffer_size = 4000;
[email protected]4356f0f2013-04-07 00:58:17728 scoped_ptr<char[]> buffer(new char[buffer_size]);
[email protected]316c1e5e2012-09-12 15:17:44729 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 const size_t first_byte_position = 500;
740 const size_t last_byte_position = buffer_size - 1;
741 const size_t content_length = last_byte_position - first_byte_position + 1;
742 std::string partial_buffer_string(buffer.get() + first_byte_position,
743 buffer.get() + last_byte_position + 1);
744
745 TestDelegate d;
746 {
747 URLRequest r(temp_url, &d, &default_context_);
748
749 HttpRequestHeaders headers;
750 headers.SetHeader(HttpRequestHeaders::kRange,
751 base::StringPrintf("bytes=%" PRIuS "-",
752 first_byte_position));
753 r.SetExtraRequestHeaders(headers);
754 r.Start();
755 EXPECT_TRUE(r.is_pending());
756
757 MessageLoop::current()->Run();
758 EXPECT_TRUE(!r.is_pending());
759 EXPECT_EQ(1, d.response_started_count());
760 EXPECT_FALSE(d.received_data_before_response());
761 EXPECT_EQ(static_cast<int>(content_length), d.bytes_received());
762 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
763 EXPECT_TRUE(partial_buffer_string == d.data_received());
764 }
765
766 EXPECT_TRUE(file_util::Delete(temp_path, false));
767}
768
769TEST_F(URLRequestTest, FileTestMultipleRanges) {
770 const size_t buffer_size = 400000;
[email protected]4356f0f2013-04-07 00:58:17771 scoped_ptr<char[]> buffer(new char[buffer_size]);
[email protected]316c1e5e2012-09-12 15:17:44772 FillBuffer(buffer.get(), buffer_size);
773
[email protected]6cdfd7f2013-02-08 20:40:15774 base::FilePath temp_path;
[email protected]316c1e5e2012-09-12 15:17:44775 EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path));
776 GURL temp_url = FilePathToFileURL(temp_path);
777 EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size));
778
779 int64 file_size;
780 EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size));
781
782 TestDelegate d;
783 {
784 URLRequest r(temp_url, &d, &default_context_);
785
786 HttpRequestHeaders headers;
787 headers.SetHeader(HttpRequestHeaders::kRange,
788 "bytes=0-0,10-200,200-300");
789 r.SetExtraRequestHeaders(headers);
790 r.Start();
791 EXPECT_TRUE(r.is_pending());
792
793 MessageLoop::current()->Run();
794 EXPECT_TRUE(d.request_failed());
795 }
796
797 EXPECT_TRUE(file_util::Delete(temp_path, false));
798}
799
800TEST_F(URLRequestTest, InvalidUrlTest) {
801 TestDelegate d;
802 {
803 URLRequest r(GURL("invalid url"), &d, &default_context_);
804
805 r.Start();
806 EXPECT_TRUE(r.is_pending());
807
808 MessageLoop::current()->Run();
809 EXPECT_TRUE(d.request_failed());
810 }
811}
812
813#if defined(OS_WIN)
814TEST_F(URLRequestTest, ResolveShortcutTest) {
[email protected]6cdfd7f2013-02-08 20:40:15815 base::FilePath app_path;
[email protected]316c1e5e2012-09-12 15:17:44816 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
817 app_path = app_path.AppendASCII("net");
818 app_path = app_path.AppendASCII("data");
819 app_path = app_path.AppendASCII("url_request_unittest");
820 app_path = app_path.AppendASCII("with-headers.html");
821
822 std::wstring lnk_path = app_path.value() + L".lnk";
823
[email protected]451fd902012-10-03 17:14:48824 base::win::ScopedCOMInitializer com_initializer;
[email protected]00d60fa2012-10-01 18:20:17825
[email protected]316c1e5e2012-09-12 15:17:44826 // Temporarily create a shortcut for test
[email protected]00d60fa2012-10-01 18:20:17827 IShellLink* shell = NULL;
828 ASSERT_TRUE(SUCCEEDED(CoCreateInstance(
829 CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink,
830 reinterpret_cast<LPVOID*>(&shell))));
831 IPersistFile* persist = NULL;
832 ASSERT_TRUE(SUCCEEDED(shell->QueryInterface(
833 IID_IPersistFile, reinterpret_cast<LPVOID*>(&persist))));
834 EXPECT_TRUE(SUCCEEDED(shell->SetPath(app_path.value().c_str())));
835 EXPECT_TRUE(SUCCEEDED(shell->SetDescription(L"ResolveShortcutTest")));
836 EXPECT_TRUE(SUCCEEDED(persist->Save(lnk_path.c_str(), TRUE)));
[email protected]316c1e5e2012-09-12 15:17:44837 if (persist)
838 persist->Release();
839 if (shell)
840 shell->Release();
841
842 TestDelegate d;
843 {
[email protected]6cdfd7f2013-02-08 20:40:15844 URLRequest r(FilePathToFileURL(base::FilePath(lnk_path)), &d,
845 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:44846
847 r.Start();
848 EXPECT_TRUE(r.is_pending());
849
850 MessageLoop::current()->Run();
851
852 WIN32_FILE_ATTRIBUTE_DATA data;
853 GetFileAttributesEx(app_path.value().c_str(),
854 GetFileExInfoStandard, &data);
855 HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ,
856 FILE_SHARE_READ, NULL, OPEN_EXISTING,
857 FILE_ATTRIBUTE_NORMAL, NULL);
858 EXPECT_NE(INVALID_HANDLE_VALUE, file);
[email protected]ec530c22013-04-11 15:54:04859 scoped_ptr<char[]> buffer(new char[data.nFileSizeLow]);
[email protected]316c1e5e2012-09-12 15:17:44860 DWORD read_size;
861 BOOL result;
862 result = ReadFile(file, buffer.get(), data.nFileSizeLow,
863 &read_size, NULL);
864 std::string content(buffer.get(), read_size);
865 CloseHandle(file);
866
867 EXPECT_TRUE(!r.is_pending());
868 EXPECT_EQ(1, d.received_redirect_count());
869 EXPECT_EQ(content, d.data_received());
870 }
871
872 // Clean the shortcut
873 DeleteFile(lnk_path.c_str());
[email protected]316c1e5e2012-09-12 15:17:44874}
875#endif // defined(OS_WIN)
876
877TEST_F(URLRequestTest, FileDirCancelTest) {
878 // Put in mock resource provider.
879 NetModule::SetResourceProvider(TestNetResourceProvider);
880
881 TestDelegate d;
882 {
[email protected]6cdfd7f2013-02-08 20:40:15883 base::FilePath file_path;
[email protected]316c1e5e2012-09-12 15:17:44884 PathService::Get(base::DIR_SOURCE_ROOT, &file_path);
885 file_path = file_path.Append(FILE_PATH_LITERAL("net"));
886 file_path = file_path.Append(FILE_PATH_LITERAL("data"));
887
888 URLRequest req(FilePathToFileURL(file_path), &d, &default_context_);
889 req.Start();
890 EXPECT_TRUE(req.is_pending());
891
892 d.set_cancel_in_received_data_pending(true);
893
894 MessageLoop::current()->Run();
895 }
896
897 // Take out mock resource provider.
898 NetModule::SetResourceProvider(NULL);
899}
900
901TEST_F(URLRequestTest, FileDirRedirectNoCrash) {
902 // There is an implicit redirect when loading a file path that matches a
903 // directory and does not end with a slash. Ensure that following such
904 // redirects does not crash. See http://crbug.com/18686.
905
[email protected]6cdfd7f2013-02-08 20:40:15906 base::FilePath path;
[email protected]316c1e5e2012-09-12 15:17:44907 PathService::Get(base::DIR_SOURCE_ROOT, &path);
908 path = path.Append(FILE_PATH_LITERAL("net"));
909 path = path.Append(FILE_PATH_LITERAL("data"));
910 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
911
912 TestDelegate d;
913 URLRequest req(FilePathToFileURL(path), &d, &default_context_);
914 req.Start();
915 MessageLoop::current()->Run();
916
917 ASSERT_EQ(1, d.received_redirect_count());
918 ASSERT_LT(0, d.bytes_received());
919 ASSERT_FALSE(d.request_failed());
920 ASSERT_TRUE(req.status().is_success());
921}
922
923#if defined(OS_WIN)
924// Don't accept the url "file:///" on windows. See http://crbug.com/1474.
925TEST_F(URLRequestTest, FileDirRedirectSingleSlash) {
926 TestDelegate d;
927 URLRequest req(GURL("file:///"), &d, &default_context_);
928 req.Start();
929 MessageLoop::current()->Run();
930
931 ASSERT_EQ(1, d.received_redirect_count());
932 ASSERT_FALSE(req.status().is_success());
933}
934#endif
935
936// Custom URLRequestJobs for use with interceptor tests
937class RestartTestJob : public URLRequestTestJob {
938 public:
939 RestartTestJob(URLRequest* request, NetworkDelegate* network_delegate)
940 : URLRequestTestJob(request, network_delegate, true) {}
941 protected:
[email protected]46fadfd2013-02-06 09:40:16942 virtual void StartAsync() OVERRIDE {
[email protected]316c1e5e2012-09-12 15:17:44943 this->NotifyRestartRequired();
944 }
945 private:
[email protected]46fadfd2013-02-06 09:40:16946 virtual ~RestartTestJob() {}
[email protected]316c1e5e2012-09-12 15:17:44947};
948
949class CancelTestJob : public URLRequestTestJob {
950 public:
951 explicit CancelTestJob(URLRequest* request, NetworkDelegate* network_delegate)
952 : URLRequestTestJob(request, network_delegate, true) {}
953 protected:
[email protected]46fadfd2013-02-06 09:40:16954 virtual void StartAsync() OVERRIDE {
[email protected]316c1e5e2012-09-12 15:17:44955 request_->Cancel();
956 }
957 private:
[email protected]46fadfd2013-02-06 09:40:16958 virtual ~CancelTestJob() {}
[email protected]316c1e5e2012-09-12 15:17:44959};
960
961class CancelThenRestartTestJob : public URLRequestTestJob {
962 public:
963 explicit CancelThenRestartTestJob(URLRequest* request,
964 NetworkDelegate* network_delegate)
965 : URLRequestTestJob(request, network_delegate, true) {
966 }
967 protected:
[email protected]46fadfd2013-02-06 09:40:16968 virtual void StartAsync() OVERRIDE {
[email protected]316c1e5e2012-09-12 15:17:44969 request_->Cancel();
970 this->NotifyRestartRequired();
971 }
972 private:
[email protected]46fadfd2013-02-06 09:40:16973 virtual ~CancelThenRestartTestJob() {}
[email protected]316c1e5e2012-09-12 15:17:44974};
975
976// An Interceptor for use with interceptor tests
977class TestInterceptor : URLRequest::Interceptor {
978 public:
979 TestInterceptor()
980 : intercept_main_request_(false), restart_main_request_(false),
981 cancel_main_request_(false), cancel_then_restart_main_request_(false),
982 simulate_main_network_error_(false),
983 intercept_redirect_(false), cancel_redirect_request_(false),
984 intercept_final_response_(false), cancel_final_request_(false),
985 did_intercept_main_(false), did_restart_main_(false),
986 did_cancel_main_(false), did_cancel_then_restart_main_(false),
987 did_simulate_error_main_(false),
988 did_intercept_redirect_(false), did_cancel_redirect_(false),
989 did_intercept_final_(false), did_cancel_final_(false) {
990 URLRequest::Deprecated::RegisterRequestInterceptor(this);
991 }
992
[email protected]46fadfd2013-02-06 09:40:16993 virtual ~TestInterceptor() {
[email protected]316c1e5e2012-09-12 15:17:44994 URLRequest::Deprecated::UnregisterRequestInterceptor(this);
995 }
996
[email protected]46fadfd2013-02-06 09:40:16997 virtual URLRequestJob* MaybeIntercept(
998 URLRequest* request,
999 NetworkDelegate* network_delegate) OVERRIDE {
[email protected]316c1e5e2012-09-12 15:17:441000 if (restart_main_request_) {
1001 restart_main_request_ = false;
1002 did_restart_main_ = true;
1003 return new RestartTestJob(request, network_delegate);
1004 }
1005 if (cancel_main_request_) {
1006 cancel_main_request_ = false;
1007 did_cancel_main_ = true;
1008 return new CancelTestJob(request, network_delegate);
1009 }
1010 if (cancel_then_restart_main_request_) {
1011 cancel_then_restart_main_request_ = false;
1012 did_cancel_then_restart_main_ = true;
1013 return new CancelThenRestartTestJob(request, network_delegate);
1014 }
1015 if (simulate_main_network_error_) {
1016 simulate_main_network_error_ = false;
1017 did_simulate_error_main_ = true;
1018 // will error since the requeted url is not one of its canned urls
1019 return new URLRequestTestJob(request, network_delegate, true);
1020 }
1021 if (!intercept_main_request_)
1022 return NULL;
1023 intercept_main_request_ = false;
1024 did_intercept_main_ = true;
[email protected]2bba3252013-04-08 19:50:591025 URLRequestTestJob* job = new URLRequestTestJob(request,
1026 network_delegate,
1027 main_headers_,
1028 main_data_,
1029 true);
1030 job->set_load_timing_info(main_request_load_timing_info_);
1031 return job;
[email protected]316c1e5e2012-09-12 15:17:441032 }
1033
1034 virtual URLRequestJob* MaybeInterceptRedirect(
1035 URLRequest* request,
1036 NetworkDelegate* network_delegate,
[email protected]46fadfd2013-02-06 09:40:161037 const GURL& location) OVERRIDE {
[email protected]316c1e5e2012-09-12 15:17:441038 if (cancel_redirect_request_) {
1039 cancel_redirect_request_ = false;
1040 did_cancel_redirect_ = true;
1041 return new CancelTestJob(request, network_delegate);
1042 }
1043 if (!intercept_redirect_)
1044 return NULL;
1045 intercept_redirect_ = false;
1046 did_intercept_redirect_ = true;
1047 return new URLRequestTestJob(request,
1048 network_delegate,
1049 redirect_headers_,
1050 redirect_data_,
1051 true);
1052 }
1053
1054 virtual URLRequestJob* MaybeInterceptResponse(
[email protected]46fadfd2013-02-06 09:40:161055 URLRequest* request, NetworkDelegate* network_delegate) OVERRIDE {
[email protected]316c1e5e2012-09-12 15:17:441056 if (cancel_final_request_) {
1057 cancel_final_request_ = false;
1058 did_cancel_final_ = true;
1059 return new CancelTestJob(request, network_delegate);
1060 }
1061 if (!intercept_final_response_)
1062 return NULL;
1063 intercept_final_response_ = false;
1064 did_intercept_final_ = true;
1065 return new URLRequestTestJob(request,
1066 network_delegate,
1067 final_headers_,
1068 final_data_,
1069 true);
1070 }
1071
[email protected]2bba3252013-04-08 19:50:591072 // Whether to intercept the main request, and if so the response to return and
1073 // the LoadTimingInfo to use.
[email protected]316c1e5e2012-09-12 15:17:441074 bool intercept_main_request_;
1075 std::string main_headers_;
1076 std::string main_data_;
[email protected]2bba3252013-04-08 19:50:591077 LoadTimingInfo main_request_load_timing_info_;
[email protected]316c1e5e2012-09-12 15:17:441078
1079 // Other actions we take at MaybeIntercept time
1080 bool restart_main_request_;
1081 bool cancel_main_request_;
1082 bool cancel_then_restart_main_request_;
1083 bool simulate_main_network_error_;
1084
1085 // Whether to intercept redirects, and if so the response to return.
1086 bool intercept_redirect_;
1087 std::string redirect_headers_;
1088 std::string redirect_data_;
1089
1090 // Other actions we can take at MaybeInterceptRedirect time
1091 bool cancel_redirect_request_;
1092
1093 // Whether to intercept final response, and if so the response to return.
1094 bool intercept_final_response_;
1095 std::string final_headers_;
1096 std::string final_data_;
1097
1098 // Other actions we can take at MaybeInterceptResponse time
1099 bool cancel_final_request_;
1100
1101 // If we did something or not
1102 bool did_intercept_main_;
1103 bool did_restart_main_;
1104 bool did_cancel_main_;
1105 bool did_cancel_then_restart_main_;
1106 bool did_simulate_error_main_;
1107 bool did_intercept_redirect_;
1108 bool did_cancel_redirect_;
1109 bool did_intercept_final_;
1110 bool did_cancel_final_;
1111
1112 // Static getters for canned response header and data strings
1113
1114 static std::string ok_data() {
1115 return URLRequestTestJob::test_data_1();
1116 }
1117
1118 static std::string ok_headers() {
1119 return URLRequestTestJob::test_headers();
1120 }
1121
1122 static std::string redirect_data() {
1123 return std::string();
1124 }
1125
1126 static std::string redirect_headers() {
1127 return URLRequestTestJob::test_redirect_headers();
1128 }
1129
1130 static std::string error_data() {
1131 return std::string("ohhh nooooo mr. bill!");
1132 }
1133
1134 static std::string error_headers() {
1135 return URLRequestTestJob::test_error_headers();
1136 }
1137};
1138
1139TEST_F(URLRequestTest, Intercept) {
1140 TestInterceptor interceptor;
1141
1142 // intercept the main request and respond with a simple response
1143 interceptor.intercept_main_request_ = true;
1144 interceptor.main_headers_ = TestInterceptor::ok_headers();
1145 interceptor.main_data_ = TestInterceptor::ok_data();
1146
1147 TestDelegate d;
1148 URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_);
1149 base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data();
1150 base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data();
1151 base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data();
1152 req.SetUserData(NULL, user_data0);
1153 req.SetUserData(&user_data1, user_data1);
1154 req.SetUserData(&user_data2, user_data2);
1155 req.set_method("GET");
1156 req.Start();
1157 MessageLoop::current()->Run();
1158
1159 // Make sure we can retrieve our specific user data
1160 EXPECT_EQ(user_data0, req.GetUserData(NULL));
1161 EXPECT_EQ(user_data1, req.GetUserData(&user_data1));
1162 EXPECT_EQ(user_data2, req.GetUserData(&user_data2));
1163
1164 // Check the interceptor got called as expected
1165 EXPECT_TRUE(interceptor.did_intercept_main_);
1166
1167 // Check we got one good response
1168 EXPECT_TRUE(req.status().is_success());
1169 EXPECT_EQ(200, req.response_headers()->response_code());
1170 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1171 EXPECT_EQ(1, d.response_started_count());
1172 EXPECT_EQ(0, d.received_redirect_count());
1173}
1174
1175TEST_F(URLRequestTest, InterceptRedirect) {
1176 TestInterceptor interceptor;
1177
1178 // intercept the main request and respond with a redirect
1179 interceptor.intercept_main_request_ = true;
1180 interceptor.main_headers_ = TestInterceptor::redirect_headers();
1181 interceptor.main_data_ = TestInterceptor::redirect_data();
1182
1183 // intercept that redirect and respond a final OK response
1184 interceptor.intercept_redirect_ = true;
1185 interceptor.redirect_headers_ = TestInterceptor::ok_headers();
1186 interceptor.redirect_data_ = TestInterceptor::ok_data();
1187
1188 TestDelegate d;
1189 URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_);
1190 req.set_method("GET");
1191 req.Start();
1192 MessageLoop::current()->Run();
1193
1194 // Check the interceptor got called as expected
1195 EXPECT_TRUE(interceptor.did_intercept_main_);
1196 EXPECT_TRUE(interceptor.did_intercept_redirect_);
1197
1198 // Check we got one good response
1199 EXPECT_TRUE(req.status().is_success());
1200 if (req.status().is_success()) {
1201 EXPECT_EQ(200, req.response_headers()->response_code());
1202 }
1203 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1204 EXPECT_EQ(1, d.response_started_count());
1205 EXPECT_EQ(0, d.received_redirect_count());
1206}
1207
1208TEST_F(URLRequestTest, InterceptServerError) {
1209 TestInterceptor interceptor;
1210
1211 // intercept the main request to generate a server error response
1212 interceptor.intercept_main_request_ = true;
1213 interceptor.main_headers_ = TestInterceptor::error_headers();
1214 interceptor.main_data_ = TestInterceptor::error_data();
1215
1216 // intercept that error and respond with an OK response
1217 interceptor.intercept_final_response_ = true;
1218 interceptor.final_headers_ = TestInterceptor::ok_headers();
1219 interceptor.final_data_ = TestInterceptor::ok_data();
1220
1221 TestDelegate d;
1222 URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_);
1223 req.set_method("GET");
1224 req.Start();
1225 MessageLoop::current()->Run();
1226
1227 // Check the interceptor got called as expected
1228 EXPECT_TRUE(interceptor.did_intercept_main_);
1229 EXPECT_TRUE(interceptor.did_intercept_final_);
1230
1231 // Check we got one good response
1232 EXPECT_TRUE(req.status().is_success());
1233 EXPECT_EQ(200, req.response_headers()->response_code());
1234 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1235 EXPECT_EQ(1, d.response_started_count());
1236 EXPECT_EQ(0, d.received_redirect_count());
1237}
1238
1239TEST_F(URLRequestTest, InterceptNetworkError) {
1240 TestInterceptor interceptor;
1241
1242 // intercept the main request to simulate a network error
1243 interceptor.simulate_main_network_error_ = true;
1244
1245 // intercept that error and respond with an OK response
1246 interceptor.intercept_final_response_ = true;
1247 interceptor.final_headers_ = TestInterceptor::ok_headers();
1248 interceptor.final_data_ = TestInterceptor::ok_data();
1249
1250 TestDelegate d;
1251 URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_);
1252 req.set_method("GET");
1253 req.Start();
1254 MessageLoop::current()->Run();
1255
1256 // Check the interceptor got called as expected
1257 EXPECT_TRUE(interceptor.did_simulate_error_main_);
1258 EXPECT_TRUE(interceptor.did_intercept_final_);
1259
1260 // Check we received one good response
1261 EXPECT_TRUE(req.status().is_success());
1262 EXPECT_EQ(200, req.response_headers()->response_code());
1263 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1264 EXPECT_EQ(1, d.response_started_count());
1265 EXPECT_EQ(0, d.received_redirect_count());
1266}
1267
1268TEST_F(URLRequestTest, InterceptRestartRequired) {
1269 TestInterceptor interceptor;
1270
1271 // restart the main request
1272 interceptor.restart_main_request_ = true;
1273
1274 // then intercept the new main request and respond with an OK response
1275 interceptor.intercept_main_request_ = true;
1276 interceptor.main_headers_ = TestInterceptor::ok_headers();
1277 interceptor.main_data_ = TestInterceptor::ok_data();
1278
1279 TestDelegate d;
1280 URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_);
1281 req.set_method("GET");
1282 req.Start();
1283 MessageLoop::current()->Run();
1284
1285 // Check the interceptor got called as expected
1286 EXPECT_TRUE(interceptor.did_restart_main_);
1287 EXPECT_TRUE(interceptor.did_intercept_main_);
1288
1289 // Check we received one good response
1290 EXPECT_TRUE(req.status().is_success());
1291 if (req.status().is_success()) {
1292 EXPECT_EQ(200, req.response_headers()->response_code());
1293 }
1294 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1295 EXPECT_EQ(1, d.response_started_count());
1296 EXPECT_EQ(0, d.received_redirect_count());
1297}
1298
1299TEST_F(URLRequestTest, InterceptRespectsCancelMain) {
1300 TestInterceptor interceptor;
1301
1302 // intercept the main request and cancel from within the restarted job
1303 interceptor.cancel_main_request_ = true;
1304
1305 // setup to intercept final response and override it with an OK response
1306 interceptor.intercept_final_response_ = true;
1307 interceptor.final_headers_ = TestInterceptor::ok_headers();
1308 interceptor.final_data_ = TestInterceptor::ok_data();
1309
1310 TestDelegate d;
1311 URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_);
1312 req.set_method("GET");
1313 req.Start();
1314 MessageLoop::current()->Run();
1315
1316 // Check the interceptor got called as expected
1317 EXPECT_TRUE(interceptor.did_cancel_main_);
1318 EXPECT_FALSE(interceptor.did_intercept_final_);
1319
1320 // Check we see a canceled request
1321 EXPECT_FALSE(req.status().is_success());
1322 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1323}
1324
1325TEST_F(URLRequestTest, InterceptRespectsCancelRedirect) {
1326 TestInterceptor interceptor;
1327
1328 // intercept the main request and respond with a redirect
1329 interceptor.intercept_main_request_ = true;
1330 interceptor.main_headers_ = TestInterceptor::redirect_headers();
1331 interceptor.main_data_ = TestInterceptor::redirect_data();
1332
1333 // intercept the redirect and cancel from within that job
1334 interceptor.cancel_redirect_request_ = true;
1335
1336 // setup to intercept final response and override it with an OK response
1337 interceptor.intercept_final_response_ = true;
1338 interceptor.final_headers_ = TestInterceptor::ok_headers();
1339 interceptor.final_data_ = TestInterceptor::ok_data();
1340
1341 TestDelegate d;
1342 URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_);
1343 req.set_method("GET");
1344 req.Start();
1345 MessageLoop::current()->Run();
1346
1347 // Check the interceptor got called as expected
1348 EXPECT_TRUE(interceptor.did_intercept_main_);
1349 EXPECT_TRUE(interceptor.did_cancel_redirect_);
1350 EXPECT_FALSE(interceptor.did_intercept_final_);
1351
1352 // Check we see a canceled request
1353 EXPECT_FALSE(req.status().is_success());
1354 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1355}
1356
1357TEST_F(URLRequestTest, InterceptRespectsCancelFinal) {
1358 TestInterceptor interceptor;
1359
1360 // intercept the main request to simulate a network error
1361 interceptor.simulate_main_network_error_ = true;
1362
1363 // setup to intercept final response and cancel from within that job
1364 interceptor.cancel_final_request_ = true;
1365
1366 TestDelegate d;
1367 URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_);
1368 req.set_method("GET");
1369 req.Start();
1370 MessageLoop::current()->Run();
1371
1372 // Check the interceptor got called as expected
1373 EXPECT_TRUE(interceptor.did_simulate_error_main_);
1374 EXPECT_TRUE(interceptor.did_cancel_final_);
1375
1376 // Check we see a canceled request
1377 EXPECT_FALSE(req.status().is_success());
1378 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1379}
1380
1381TEST_F(URLRequestTest, InterceptRespectsCancelInRestart) {
1382 TestInterceptor interceptor;
1383
1384 // intercept the main request and cancel then restart from within that job
1385 interceptor.cancel_then_restart_main_request_ = true;
1386
1387 // setup to intercept final response and override it with an OK response
1388 interceptor.intercept_final_response_ = true;
1389 interceptor.final_headers_ = TestInterceptor::ok_headers();
1390 interceptor.final_data_ = TestInterceptor::ok_data();
1391
1392 TestDelegate d;
1393 URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_);
1394 req.set_method("GET");
1395 req.Start();
1396 MessageLoop::current()->Run();
1397
1398 // Check the interceptor got called as expected
1399 EXPECT_TRUE(interceptor.did_cancel_then_restart_main_);
1400 EXPECT_FALSE(interceptor.did_intercept_final_);
1401
1402 // Check we see a canceled request
1403 EXPECT_FALSE(req.status().is_success());
1404 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1405}
1406
[email protected]2bba3252013-04-08 19:50:591407LoadTimingInfo RunLoadTimingTest(const LoadTimingInfo& job_load_timing,
1408 URLRequestContext* context) {
1409 TestInterceptor interceptor;
1410 interceptor.intercept_main_request_ = true;
1411 interceptor.main_request_load_timing_info_ = job_load_timing;
1412 TestDelegate d;
1413 URLRequest req(GURL("http://test_intercept/foo"), &d, context);
1414 req.Start();
1415 MessageLoop::current()->Run();
1416
1417 LoadTimingInfo resulting_load_timing;
1418 req.GetLoadTimingInfo(&resulting_load_timing);
1419
1420 // None of these should be modified by the URLRequest.
1421 EXPECT_EQ(job_load_timing.socket_reused, resulting_load_timing.socket_reused);
1422 EXPECT_EQ(job_load_timing.socket_log_id, resulting_load_timing.socket_log_id);
1423 EXPECT_EQ(job_load_timing.send_start, resulting_load_timing.send_start);
1424 EXPECT_EQ(job_load_timing.send_end, resulting_load_timing.send_end);
1425 EXPECT_EQ(job_load_timing.receive_headers_end,
1426 resulting_load_timing.receive_headers_end);
1427
1428 return resulting_load_timing;
1429}
1430
1431// "Normal" LoadTimingInfo as returned by a job. Everything is in order, not
1432// reused. |connect_time_flags| is used to indicate if there should be dns
1433// or SSL times, and |used_proxy| is used for proxy times.
1434LoadTimingInfo NormalLoadTimingInfo(base::TimeTicks now,
1435 int connect_time_flags,
1436 bool used_proxy) {
1437 LoadTimingInfo load_timing;
1438 load_timing.socket_log_id = 1;
1439
1440 if (used_proxy) {
1441 load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1);
1442 load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2);
1443 }
1444
1445 LoadTimingInfo::ConnectTiming& connect_timing = load_timing.connect_timing;
1446 if (connect_time_flags & CONNECT_TIMING_HAS_DNS_TIMES) {
1447 connect_timing.dns_start = now + base::TimeDelta::FromDays(3);
1448 connect_timing.dns_end = now + base::TimeDelta::FromDays(4);
1449 }
1450 connect_timing.connect_start = now + base::TimeDelta::FromDays(5);
1451 if (connect_time_flags & CONNECT_TIMING_HAS_SSL_TIMES) {
1452 connect_timing.ssl_start = now + base::TimeDelta::FromDays(6);
1453 connect_timing.ssl_end = now + base::TimeDelta::FromDays(7);
1454 }
1455 connect_timing.connect_end = now + base::TimeDelta::FromDays(8);
1456
1457 load_timing.send_start = now + base::TimeDelta::FromDays(9);
1458 load_timing.send_end = now + base::TimeDelta::FromDays(10);
1459 load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11);
1460 return load_timing;
1461}
1462
1463// Same as above, but in the case of a reused socket.
1464LoadTimingInfo NormalLoadTimingInfoReused(base::TimeTicks now,
1465 bool used_proxy) {
1466 LoadTimingInfo load_timing;
1467 load_timing.socket_log_id = 1;
1468 load_timing.socket_reused = true;
1469
1470 if (used_proxy) {
1471 load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1);
1472 load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2);
1473 }
1474
1475 load_timing.send_start = now + base::TimeDelta::FromDays(9);
1476 load_timing.send_end = now + base::TimeDelta::FromDays(10);
1477 load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11);
1478 return load_timing;
1479}
1480
1481// Basic test that the intercept + load timing tests work.
1482TEST_F(URLRequestTest, InterceptLoadTiming) {
1483 base::TimeTicks now = base::TimeTicks::Now();
1484 LoadTimingInfo job_load_timing =
1485 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, false);
1486
1487 LoadTimingInfo load_timing_result = RunLoadTimingTest(job_load_timing,
1488 &default_context_);
1489
1490 // Nothing should have been changed by the URLRequest.
1491 EXPECT_EQ(job_load_timing.proxy_resolve_start,
1492 load_timing_result.proxy_resolve_start);
1493 EXPECT_EQ(job_load_timing.proxy_resolve_end,
1494 load_timing_result.proxy_resolve_end);
1495 EXPECT_EQ(job_load_timing.connect_timing.dns_start,
1496 load_timing_result.connect_timing.dns_start);
1497 EXPECT_EQ(job_load_timing.connect_timing.dns_end,
1498 load_timing_result.connect_timing.dns_end);
1499 EXPECT_EQ(job_load_timing.connect_timing.connect_start,
1500 load_timing_result.connect_timing.connect_start);
1501 EXPECT_EQ(job_load_timing.connect_timing.connect_end,
1502 load_timing_result.connect_timing.connect_end);
1503 EXPECT_EQ(job_load_timing.connect_timing.ssl_start,
1504 load_timing_result.connect_timing.ssl_start);
1505 EXPECT_EQ(job_load_timing.connect_timing.ssl_end,
1506 load_timing_result.connect_timing.ssl_end);
1507
1508 // Redundant sanity check.
1509 TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_DNS_TIMES);
1510}
1511
1512// Another basic test, with proxy and SSL times, but no DNS times.
1513TEST_F(URLRequestTest, InterceptLoadTimingProxy) {
1514 base::TimeTicks now = base::TimeTicks::Now();
1515 LoadTimingInfo job_load_timing =
1516 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, true);
1517
1518 LoadTimingInfo load_timing_result = RunLoadTimingTest(job_load_timing,
1519 &default_context_);
1520
1521 // Nothing should have been changed by the URLRequest.
1522 EXPECT_EQ(job_load_timing.proxy_resolve_start,
1523 load_timing_result.proxy_resolve_start);
1524 EXPECT_EQ(job_load_timing.proxy_resolve_end,
1525 load_timing_result.proxy_resolve_end);
1526 EXPECT_EQ(job_load_timing.connect_timing.dns_start,
1527 load_timing_result.connect_timing.dns_start);
1528 EXPECT_EQ(job_load_timing.connect_timing.dns_end,
1529 load_timing_result.connect_timing.dns_end);
1530 EXPECT_EQ(job_load_timing.connect_timing.connect_start,
1531 load_timing_result.connect_timing.connect_start);
1532 EXPECT_EQ(job_load_timing.connect_timing.connect_end,
1533 load_timing_result.connect_timing.connect_end);
1534 EXPECT_EQ(job_load_timing.connect_timing.ssl_start,
1535 load_timing_result.connect_timing.ssl_start);
1536 EXPECT_EQ(job_load_timing.connect_timing.ssl_end,
1537 load_timing_result.connect_timing.ssl_end);
1538
1539 // Redundant sanity check.
1540 TestLoadTimingNotReusedWithProxy(load_timing_result,
1541 CONNECT_TIMING_HAS_SSL_TIMES);
1542}
1543
1544// Make sure that URLRequest correctly adjusts proxy times when they're before
1545// |request_start|, due to already having a connected socket. This happens in
1546// the case of reusing a SPDY session or HTTP pipeline. The connected socket is
1547// not considered reused in this test (May be a preconnect).
1548//
1549// To mix things up from the test above, assumes DNS times but no SSL times.
1550TEST_F(URLRequestTest, InterceptLoadTimingEarlyProxyResolution) {
1551 base::TimeTicks now = base::TimeTicks::Now();
1552 LoadTimingInfo job_load_timing =
1553 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, true);
1554 job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(6);
1555 job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(5);
1556 job_load_timing.connect_timing.dns_start = now - base::TimeDelta::FromDays(4);
1557 job_load_timing.connect_timing.dns_end = now - base::TimeDelta::FromDays(3);
1558 job_load_timing.connect_timing.connect_start =
1559 now - base::TimeDelta::FromDays(2);
1560 job_load_timing.connect_timing.connect_end =
1561 now - base::TimeDelta::FromDays(1);
1562
1563 LoadTimingInfo load_timing_result = RunLoadTimingTest(job_load_timing,
1564 &default_context_);
1565
1566 // Proxy times, connect times, and DNS times should all be replaced with
1567 // request_start.
1568 EXPECT_EQ(load_timing_result.request_start,
1569 load_timing_result.proxy_resolve_start);
1570 EXPECT_EQ(load_timing_result.request_start,
1571 load_timing_result.proxy_resolve_end);
1572 EXPECT_EQ(load_timing_result.request_start,
1573 load_timing_result.connect_timing.dns_start);
1574 EXPECT_EQ(load_timing_result.request_start,
1575 load_timing_result.connect_timing.dns_end);
1576 EXPECT_EQ(load_timing_result.request_start,
1577 load_timing_result.connect_timing.connect_start);
1578 EXPECT_EQ(load_timing_result.request_start,
1579 load_timing_result.connect_timing.connect_end);
1580
1581 // Other times should have been left null.
1582 TestLoadTimingNotReusedWithProxy(load_timing_result,
1583 CONNECT_TIMING_HAS_DNS_TIMES);
1584}
1585
1586// Same as above, but in the reused case.
1587TEST_F(URLRequestTest, InterceptLoadTimingEarlyProxyResolutionReused) {
1588 base::TimeTicks now = base::TimeTicks::Now();
1589 LoadTimingInfo job_load_timing = NormalLoadTimingInfoReused(now, true);
1590 job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(4);
1591 job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(3);
1592
1593 LoadTimingInfo load_timing_result = RunLoadTimingTest(job_load_timing,
1594 &default_context_);
1595
1596 // Proxy times and connect times should all be replaced with request_start.
1597 EXPECT_EQ(load_timing_result.request_start,
1598 load_timing_result.proxy_resolve_start);
1599 EXPECT_EQ(load_timing_result.request_start,
1600 load_timing_result.proxy_resolve_end);
1601
1602 // Other times should have been left null.
1603 TestLoadTimingReusedWithProxy(load_timing_result);
1604}
1605
1606// Make sure that URLRequest correctly adjusts connect times when they're before
1607// |request_start|, due to reusing a connected socket. The connected socket is
1608// not considered reused in this test (May be a preconnect).
1609//
1610// To mix things up, the request has SSL times, but no DNS times.
1611TEST_F(URLRequestTest, InterceptLoadTimingEarlyConnect) {
1612 base::TimeTicks now = base::TimeTicks::Now();
1613 LoadTimingInfo job_load_timing =
1614 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, false);
1615 job_load_timing.connect_timing.connect_start =
1616 now - base::TimeDelta::FromDays(1);
1617 job_load_timing.connect_timing.ssl_start = now - base::TimeDelta::FromDays(2);
1618 job_load_timing.connect_timing.ssl_end = now - base::TimeDelta::FromDays(3);
1619 job_load_timing.connect_timing.connect_end =
1620 now - base::TimeDelta::FromDays(4);
1621
1622 LoadTimingInfo load_timing_result = RunLoadTimingTest(job_load_timing,
1623 &default_context_);
1624
1625 // Connect times, and SSL times should be replaced with request_start.
1626 EXPECT_EQ(load_timing_result.request_start,
1627 load_timing_result.connect_timing.connect_start);
1628 EXPECT_EQ(load_timing_result.request_start,
1629 load_timing_result.connect_timing.ssl_start);
1630 EXPECT_EQ(load_timing_result.request_start,
1631 load_timing_result.connect_timing.ssl_end);
1632 EXPECT_EQ(load_timing_result.request_start,
1633 load_timing_result.connect_timing.connect_end);
1634
1635 // Other times should have been left null.
1636 TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_SSL_TIMES);
1637}
1638
1639// Make sure that URLRequest correctly adjusts connect times when they're before
1640// |request_start|, due to reusing a connected socket in the case that there
1641// are also proxy times. The connected socket is not considered reused in this
1642// test (May be a preconnect).
1643//
1644// In this test, there are no SSL or DNS times.
1645TEST_F(URLRequestTest, InterceptLoadTimingEarlyConnectWithProxy) {
1646 base::TimeTicks now = base::TimeTicks::Now();
1647 LoadTimingInfo job_load_timing =
1648 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY, true);
1649 job_load_timing.connect_timing.connect_start =
1650 now - base::TimeDelta::FromDays(1);
1651 job_load_timing.connect_timing.connect_end =
1652 now - base::TimeDelta::FromDays(2);
1653
1654 LoadTimingInfo load_timing_result = RunLoadTimingTest(job_load_timing,
1655 &default_context_);
1656
1657 // Connect times should be replaced with proxy_resolve_end.
1658 EXPECT_EQ(load_timing_result.proxy_resolve_end,
1659 load_timing_result.connect_timing.connect_start);
1660 EXPECT_EQ(load_timing_result.proxy_resolve_end,
1661 load_timing_result.connect_timing.connect_end);
1662
1663 // Other times should have been left null.
1664 TestLoadTimingNotReusedWithProxy(load_timing_result,
1665 CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY);
1666}
1667
[email protected]316c1e5e2012-09-12 15:17:441668// Check that two different URL requests have different identifiers.
1669TEST_F(URLRequestTest, Identifiers) {
1670 TestDelegate d;
1671 TestURLRequestContext context;
[email protected]cc05edc2013-03-08 18:04:411672 TestURLRequest req(GURL("http://example.com"), &d, &context, NULL);
1673 TestURLRequest other_req(GURL("http://example.com"), &d, &context, NULL);
[email protected]316c1e5e2012-09-12 15:17:441674
1675 ASSERT_NE(req.identifier(), other_req.identifier());
1676}
1677
1678// Check that a failure to connect to the proxy is reported to the network
1679// delegate.
1680TEST_F(URLRequestTest, NetworkDelegateProxyError) {
1681 MockHostResolver host_resolver;
1682 host_resolver.rules()->AddSimulatedFailure("*");
1683
[email protected]ceefd7fd2012-11-29 00:36:241684 TestNetworkDelegate network_delegate; // Must outlive URLRequests.
[email protected]316c1e5e2012-09-12 15:17:441685 TestURLRequestContextWithProxy context("myproxy:70", &network_delegate);
1686
1687 TestDelegate d;
1688 URLRequest req(GURL("http://example.com"), &d, &context);
1689 req.set_method("GET");
1690
1691 req.Start();
1692 MessageLoop::current()->Run();
1693
1694 // Check we see a failed request.
1695 EXPECT_FALSE(req.status().is_success());
1696 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
1697 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, req.status().error());
1698
1699 EXPECT_EQ(1, network_delegate.error_count());
1700 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, network_delegate.last_error());
1701 EXPECT_EQ(1, network_delegate.completed_requests());
1702}
1703
1704// Make sure that net::NetworkDelegate::NotifyCompleted is called if
1705// content is empty.
1706TEST_F(URLRequestTest, RequestCompletionForEmptyResponse) {
1707 TestDelegate d;
1708 URLRequest req(GURL("data:,"), &d, &default_context_);
1709 req.Start();
1710 MessageLoop::current()->Run();
1711 EXPECT_EQ("", d.data_received());
1712 EXPECT_EQ(1, default_network_delegate_.completed_requests());
1713}
1714
[email protected]5033ab82013-03-22 20:17:461715// Make sure that SetPriority actually sets the URLRequest's priority
1716// correctly, both before and after start.
1717TEST_F(URLRequestTest, SetPriorityBasic) {
1718 TestDelegate d;
1719 URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_);
1720 EXPECT_EQ(DEFAULT_PRIORITY, req.priority());
1721
1722 req.SetPriority(LOW);
1723 EXPECT_EQ(LOW, req.priority());
1724
1725 req.Start();
1726 EXPECT_EQ(LOW, req.priority());
1727
1728 req.SetPriority(MEDIUM);
1729 EXPECT_EQ(MEDIUM, req.priority());
1730}
1731
1732// Make sure that URLRequest calls SetPriority on a job before calling
1733// Start on it.
1734TEST_F(URLRequestTest, SetJobPriorityBeforeJobStart) {
1735 TestDelegate d;
1736 URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_);
1737 EXPECT_EQ(DEFAULT_PRIORITY, req.priority());
1738
1739 scoped_refptr<URLRequestTestJob> job =
1740 new URLRequestTestJob(&req, &default_network_delegate_);
1741 AddTestInterceptor()->set_main_intercept_job(job);
1742 EXPECT_EQ(DEFAULT_PRIORITY, job->priority());
1743
1744 req.SetPriority(LOW);
1745
1746 req.Start();
1747 EXPECT_EQ(LOW, job->priority());
1748}
1749
1750// Make sure that URLRequest passes on its priority updates to its
1751// job.
1752TEST_F(URLRequestTest, SetJobPriority) {
1753 TestDelegate d;
1754 URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_);
1755
1756 scoped_refptr<URLRequestTestJob> job =
1757 new URLRequestTestJob(&req, &default_network_delegate_);
1758 AddTestInterceptor()->set_main_intercept_job(job);
1759
1760 req.SetPriority(LOW);
1761 req.Start();
1762 EXPECT_EQ(LOW, job->priority());
1763
1764 req.SetPriority(MEDIUM);
1765 EXPECT_EQ(MEDIUM, req.priority());
1766 EXPECT_EQ(MEDIUM, job->priority());
1767}
1768
[email protected]316c1e5e2012-09-12 15:17:441769// TODO(droger): Support TestServer on iOS (see http://crbug.com/148666).
1770#if !defined(OS_IOS)
1771// A subclass of TestServer that uses a statically-configured hostname. This is
1772// to work around mysterious failures in chrome_frame_net_tests. See:
1773// http://crbug.com/114369
1774class LocalHttpTestServer : public TestServer {
1775 public:
[email protected]6cdfd7f2013-02-08 20:40:151776 explicit LocalHttpTestServer(const base::FilePath& document_root)
[email protected]316c1e5e2012-09-12 15:17:441777 : TestServer(TestServer::TYPE_HTTP,
1778 ScopedCustomUrlRequestTestHttpHost::value(),
1779 document_root) {}
1780 LocalHttpTestServer()
1781 : TestServer(TestServer::TYPE_HTTP,
1782 ScopedCustomUrlRequestTestHttpHost::value(),
[email protected]6cdfd7f2013-02-08 20:40:151783 base::FilePath()) {}
[email protected]316c1e5e2012-09-12 15:17:441784};
1785
[email protected]f2f31b32013-01-16 23:24:091786TEST_F(URLRequestTest, DelayedCookieCallback) {
[email protected]316c1e5e2012-09-12 15:17:441787 LocalHttpTestServer test_server;
1788 ASSERT_TRUE(test_server.Start());
1789
1790 TestURLRequestContext context;
1791 scoped_refptr<DelayedCookieMonster> delayed_cm =
1792 new DelayedCookieMonster();
1793 scoped_refptr<CookieStore> cookie_store = delayed_cm;
1794 context.set_cookie_store(delayed_cm);
1795
1796 // Set up a cookie.
1797 {
1798 TestNetworkDelegate network_delegate;
1799 context.set_network_delegate(&network_delegate);
1800 TestDelegate d;
1801 URLRequest req(
1802 test_server.GetURL("set-cookie?CookieToNotSend=1"), &d, &context);
1803 req.Start();
1804 MessageLoop::current()->Run();
1805 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1806 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1807 EXPECT_EQ(1, network_delegate.set_cookie_count());
1808 }
1809
1810 // Verify that the cookie is set.
1811 {
1812 TestNetworkDelegate network_delegate;
1813 context.set_network_delegate(&network_delegate);
1814 TestDelegate d;
1815 URLRequest req(test_server.GetURL("echoheader?Cookie"), &d, &context);
1816 req.Start();
1817 MessageLoop::current()->Run();
1818
1819 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
1820 != std::string::npos);
1821 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1822 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1823 }
1824}
1825
[email protected]f2f31b32013-01-16 23:24:091826TEST_F(URLRequestTest, DoNotSendCookies) {
[email protected]316c1e5e2012-09-12 15:17:441827 LocalHttpTestServer test_server;
1828 ASSERT_TRUE(test_server.Start());
1829
1830 // Set up a cookie.
1831 {
1832 TestNetworkDelegate network_delegate;
1833 default_context_.set_network_delegate(&network_delegate);
1834 TestDelegate d;
1835 URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"),
1836 &d,
1837 &default_context_);
1838 req.Start();
1839 MessageLoop::current()->Run();
1840 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1841 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1842 }
1843
1844 // Verify that the cookie is set.
1845 {
1846 TestNetworkDelegate network_delegate;
1847 default_context_.set_network_delegate(&network_delegate);
1848 TestDelegate d;
1849 URLRequest req(
1850 test_server.GetURL("echoheader?Cookie"), &d, &default_context_);
1851 req.Start();
1852 MessageLoop::current()->Run();
1853
1854 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
1855 != std::string::npos);
1856 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1857 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1858 }
1859
1860 // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set.
1861 {
1862 TestNetworkDelegate network_delegate;
1863 default_context_.set_network_delegate(&network_delegate);
1864 TestDelegate d;
1865 URLRequest req(
1866 test_server.GetURL("echoheader?Cookie"), &d, &default_context_);
1867 req.set_load_flags(LOAD_DO_NOT_SEND_COOKIES);
1868 req.Start();
1869 MessageLoop::current()->Run();
1870
1871 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
1872 == std::string::npos);
1873
1874 // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies.
1875 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1876 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1877 }
1878}
1879
1880TEST_F(URLRequestTest, DoNotSaveCookies) {
1881 LocalHttpTestServer test_server;
1882 ASSERT_TRUE(test_server.Start());
1883
1884 // Set up a cookie.
1885 {
1886 TestNetworkDelegate network_delegate;
1887 default_context_.set_network_delegate(&network_delegate);
1888 TestDelegate d;
1889 URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"),
1890 &d,
1891 &default_context_);
1892 req.Start();
1893 MessageLoop::current()->Run();
1894
1895 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1896 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1897 EXPECT_EQ(1, network_delegate.set_cookie_count());
1898 }
1899
1900 // Try to set-up another cookie and update the previous cookie.
1901 {
1902 TestNetworkDelegate network_delegate;
1903 default_context_.set_network_delegate(&network_delegate);
1904 TestDelegate d;
1905 URLRequest req(
1906 test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
1907 &d,
1908 &default_context_);
1909 req.set_load_flags(LOAD_DO_NOT_SAVE_COOKIES);
1910 req.Start();
1911
1912 MessageLoop::current()->Run();
1913
1914 // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie.
1915 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1916 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1917 EXPECT_EQ(0, network_delegate.set_cookie_count());
1918 }
1919
1920 // Verify the cookies weren't saved or updated.
1921 {
1922 TestNetworkDelegate network_delegate;
1923 default_context_.set_network_delegate(&network_delegate);
1924 TestDelegate d;
1925 URLRequest req(
1926 test_server.GetURL("echoheader?Cookie"), &d, &default_context_);
1927 req.Start();
1928 MessageLoop::current()->Run();
1929
1930 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
1931 == std::string::npos);
1932 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
1933 != std::string::npos);
1934
1935 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1936 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1937 EXPECT_EQ(0, network_delegate.set_cookie_count());
1938 }
1939}
1940
1941TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) {
1942 LocalHttpTestServer test_server;
1943 ASSERT_TRUE(test_server.Start());
1944
1945 // Set up a cookie.
1946 {
1947 TestNetworkDelegate network_delegate;
1948 default_context_.set_network_delegate(&network_delegate);
1949 TestDelegate d;
1950 URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"),
1951 &d,
1952 &default_context_);
1953 req.Start();
1954 MessageLoop::current()->Run();
1955
1956 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1957 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1958 }
1959
1960 // Verify that the cookie is set.
1961 {
1962 TestNetworkDelegate network_delegate;
1963 default_context_.set_network_delegate(&network_delegate);
1964 TestDelegate d;
1965 URLRequest req(
1966 test_server.GetURL("echoheader?Cookie"), &d, &default_context_);
1967 req.Start();
1968 MessageLoop::current()->Run();
1969
1970 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
1971 != std::string::npos);
1972
1973 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1974 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1975 }
1976
1977 // Verify that the cookie isn't sent.
1978 {
1979 TestNetworkDelegate network_delegate;
1980 default_context_.set_network_delegate(&network_delegate);
1981 TestDelegate d;
1982 network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES);
1983 URLRequest req(
1984 test_server.GetURL("echoheader?Cookie"), &d, &default_context_);
1985 req.Start();
1986 MessageLoop::current()->Run();
1987
1988 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
1989 == std::string::npos);
1990
1991 EXPECT_EQ(1, network_delegate.blocked_get_cookies_count());
1992 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1993 }
1994}
1995
1996TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) {
1997 LocalHttpTestServer test_server;
1998 ASSERT_TRUE(test_server.Start());
1999
2000 // Set up a cookie.
2001 {
2002 TestNetworkDelegate network_delegate;
2003 default_context_.set_network_delegate(&network_delegate);
2004 TestDelegate d;
2005 URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"),
2006 &d,
2007 &default_context_);
2008 req.Start();
2009 MessageLoop::current()->Run();
2010
2011 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2012 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2013 }
2014
2015 // Try to set-up another cookie and update the previous cookie.
2016 {
2017 TestNetworkDelegate network_delegate;
2018 default_context_.set_network_delegate(&network_delegate);
2019 TestDelegate d;
2020 network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE);
2021 URLRequest req(
2022 test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
2023 &d,
2024 &default_context_);
2025 req.Start();
2026
2027 MessageLoop::current()->Run();
2028
2029 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2030 EXPECT_EQ(2, network_delegate.blocked_set_cookie_count());
2031 }
2032
2033 // Verify the cookies weren't saved or updated.
2034 {
2035 TestNetworkDelegate network_delegate;
2036 default_context_.set_network_delegate(&network_delegate);
2037 TestDelegate d;
2038 URLRequest req(
2039 test_server.GetURL("echoheader?Cookie"), &d, &default_context_);
2040 req.Start();
2041 MessageLoop::current()->Run();
2042
2043 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
2044 == std::string::npos);
2045 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
2046 != std::string::npos);
2047
2048 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2049 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2050 }
2051}
2052
2053TEST_F(URLRequestTest, DoNotSaveEmptyCookies) {
2054 LocalHttpTestServer test_server;
2055 ASSERT_TRUE(test_server.Start());
2056
2057 // Set up an empty cookie.
2058 {
2059 TestNetworkDelegate network_delegate;
2060 default_context_.set_network_delegate(&network_delegate);
2061 TestDelegate d;
2062 URLRequest req(test_server.GetURL("set-cookie"), &d, &default_context_);
2063 req.Start();
2064 MessageLoop::current()->Run();
2065
2066 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2067 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2068 EXPECT_EQ(0, network_delegate.set_cookie_count());
2069 }
2070}
2071
2072TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) {
2073 LocalHttpTestServer test_server;
2074 ASSERT_TRUE(test_server.Start());
2075
2076 // Set up a cookie.
2077 {
2078 TestNetworkDelegate network_delegate;
2079 default_context_.set_network_delegate(&network_delegate);
2080 TestDelegate d;
2081 URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"),
2082 &d,
2083 &default_context_);
2084 req.Start();
2085 MessageLoop::current()->Run();
2086
2087 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2088 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2089 }
2090
2091 // Verify that the cookie is set.
2092 {
2093 TestNetworkDelegate network_delegate;
2094 default_context_.set_network_delegate(&network_delegate);
2095 TestDelegate d;
2096 URLRequest req(
2097 test_server.GetURL("echoheader?Cookie"), &d, &default_context_);
2098 req.Start();
2099 MessageLoop::current()->Run();
2100
2101 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2102 != std::string::npos);
2103
2104 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2105 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2106 }
2107
2108 // Verify that the cookie isn't sent.
2109 {
2110 TestNetworkDelegate network_delegate;
2111 default_context_.set_network_delegate(&network_delegate);
2112 TestDelegate d;
2113 network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES);
2114 URLRequest req(
2115 test_server.GetURL("echoheader?Cookie"), &d, &default_context_);
2116 req.Start();
2117 MessageLoop::current()->Run();
2118
2119 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
2120 == std::string::npos);
2121
2122 EXPECT_EQ(1, network_delegate.blocked_get_cookies_count());
2123 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2124 }
2125}
2126
2127TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) {
2128 LocalHttpTestServer test_server;
2129 ASSERT_TRUE(test_server.Start());
2130
2131 // Set up a cookie.
2132 {
2133 TestNetworkDelegate network_delegate;
2134 default_context_.set_network_delegate(&network_delegate);
2135 TestDelegate d;
2136 URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"),
2137 &d,
2138 &default_context_);
2139 req.Start();
2140 MessageLoop::current()->Run();
2141
2142 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2143 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2144 }
2145
2146 // Try to set-up another cookie and update the previous cookie.
2147 {
2148 TestNetworkDelegate network_delegate;
2149 default_context_.set_network_delegate(&network_delegate);
2150 TestDelegate d;
2151 network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE);
2152 URLRequest req(
2153 test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
2154 &d,
2155 &default_context_);
2156 req.Start();
2157
2158 MessageLoop::current()->Run();
2159
2160 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2161 EXPECT_EQ(2, network_delegate.blocked_set_cookie_count());
2162 }
2163
2164 // Verify the cookies weren't saved or updated.
2165 {
2166 TestNetworkDelegate network_delegate;
2167 default_context_.set_network_delegate(&network_delegate);
2168 TestDelegate d;
2169 URLRequest req(
2170 test_server.GetURL("echoheader?Cookie"), &d, &default_context_);
2171 req.Start();
2172 MessageLoop::current()->Run();
2173
2174 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
2175 == std::string::npos);
2176 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
2177 != std::string::npos);
2178
2179 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2180 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2181 }
2182}
2183
[email protected]5095cd72012-11-01 10:29:162184// FixedDateNetworkDelegate swaps out the server's HTTP Date response header
2185// value for the |fixed_date| argument given to the constructor.
2186class FixedDateNetworkDelegate : public TestNetworkDelegate {
2187 public:
2188 explicit FixedDateNetworkDelegate(const std::string& fixed_date)
2189 : fixed_date_(fixed_date) {}
2190 virtual ~FixedDateNetworkDelegate() {}
2191
2192 // net::NetworkDelegate implementation
2193 virtual int OnHeadersReceived(
2194 net::URLRequest* request,
2195 const net::CompletionCallback& callback,
2196 const net::HttpResponseHeaders* original_response_headers,
2197 scoped_refptr<net::HttpResponseHeaders>* override_response_headers)
2198 OVERRIDE;
2199
2200 private:
2201 std::string fixed_date_;
2202
2203 DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate);
2204};
2205
2206int FixedDateNetworkDelegate::OnHeadersReceived(
2207 net::URLRequest* request,
2208 const net::CompletionCallback& callback,
2209 const net::HttpResponseHeaders* original_response_headers,
2210 scoped_refptr<net::HttpResponseHeaders>* override_response_headers) {
2211 net::HttpResponseHeaders* new_response_headers =
2212 new net::HttpResponseHeaders(original_response_headers->raw_headers());
2213
2214 new_response_headers->RemoveHeader("Date");
2215 new_response_headers->AddHeader("Date: " + fixed_date_);
2216
2217 *override_response_headers = new_response_headers;
2218 return TestNetworkDelegate::OnHeadersReceived(request,
2219 callback,
2220 original_response_headers,
2221 override_response_headers);
2222}
2223
2224// Test that cookie expiration times are adjusted for server/client clock
2225// skew and that we handle incorrect timezone specifier "UTC" in HTTP Date
2226// headers by defaulting to GMT. (crbug.com/135131)
2227TEST_F(URLRequestTest, AcceptClockSkewCookieWithWrongDateTimezone) {
2228 LocalHttpTestServer test_server;
2229 ASSERT_TRUE(test_server.Start());
2230
2231 // Set up an expired cookie.
2232 {
2233 TestNetworkDelegate network_delegate;
2234 default_context_.set_network_delegate(&network_delegate);
2235 TestDelegate d;
2236 URLRequest req(test_server.GetURL(
2237 "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"),
2238 &d,
2239 &default_context_);
2240 req.Start();
2241 MessageLoop::current()->Run();
2242 }
2243 // Verify that the cookie is not set.
2244 {
2245 TestNetworkDelegate network_delegate;
2246 default_context_.set_network_delegate(&network_delegate);
2247 TestDelegate d;
2248 URLRequest req(
2249 test_server.GetURL("echoheader?Cookie"), &d, &default_context_);
2250 req.Start();
2251 MessageLoop::current()->Run();
2252
2253 EXPECT_TRUE(d.data_received().find("StillGood=1") == std::string::npos);
2254 }
2255 // Set up a cookie with clock skew and "UTC" HTTP Date timezone specifier.
2256 {
2257 FixedDateNetworkDelegate network_delegate("18-Apr-1977 22:49:13 UTC");
2258 default_context_.set_network_delegate(&network_delegate);
2259 TestDelegate d;
2260 URLRequest req(test_server.GetURL(
2261 "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"),
2262 &d,
2263 &default_context_);
2264 req.Start();
2265 MessageLoop::current()->Run();
2266 }
2267 // Verify that the cookie is set.
2268 {
2269 TestNetworkDelegate network_delegate;
2270 default_context_.set_network_delegate(&network_delegate);
2271 TestDelegate d;
2272 URLRequest req(
2273 test_server.GetURL("echoheader?Cookie"), &d, &default_context_);
2274 req.Start();
2275 MessageLoop::current()->Run();
2276
2277 EXPECT_TRUE(d.data_received().find("StillGood=1") != std::string::npos);
2278 }
2279}
2280
2281
[email protected]316c1e5e2012-09-12 15:17:442282// Check that it is impossible to change the referrer in the extra headers of
2283// an URLRequest.
2284TEST_F(URLRequestTest, DoNotOverrideReferrer) {
2285 LocalHttpTestServer test_server;
2286 ASSERT_TRUE(test_server.Start());
2287
2288 // If extra headers contain referer and the request contains a referer,
2289 // only the latter shall be respected.
2290 {
2291 TestDelegate d;
2292 URLRequest req(
2293 test_server.GetURL("echoheader?Referer"), &d, &default_context_);
[email protected]99ecf6e2013-04-10 22:46:132294 req.SetReferrer("http://foo.com/");
[email protected]316c1e5e2012-09-12 15:17:442295
2296 HttpRequestHeaders headers;
2297 headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/");
2298 req.SetExtraRequestHeaders(headers);
2299
2300 req.Start();
2301 MessageLoop::current()->Run();
2302
2303 EXPECT_EQ("http://foo.com/", d.data_received());
2304 }
2305
2306 // If extra headers contain a referer but the request does not, no referer
2307 // shall be sent in the header.
2308 {
2309 TestDelegate d;
2310 URLRequest req(
2311 test_server.GetURL("echoheader?Referer"), &d, &default_context_);
2312
2313 HttpRequestHeaders headers;
2314 headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/");
2315 req.SetExtraRequestHeaders(headers);
2316 req.set_load_flags(LOAD_VALIDATE_CACHE);
2317
2318 req.Start();
2319 MessageLoop::current()->Run();
2320
2321 EXPECT_EQ("None", d.data_received());
2322 }
2323}
2324
[email protected]b89290212009-08-14 22:37:352325class URLRequestTestHTTP : public URLRequestTest {
[email protected]95409e12010-08-17 20:07:112326 public:
2327 URLRequestTestHTTP()
[email protected]6cdfd7f2013-02-08 20:40:152328 : test_server_(base::FilePath(FILE_PATH_LITERAL(
[email protected]95409e12010-08-17 20:07:112329 "net/data/url_request_unittest"))) {
2330 }
2331
[email protected]b89290212009-08-14 22:37:352332 protected:
[email protected]21184962011-10-26 00:50:302333 // Requests |redirect_url|, which must return a HTTP 3xx redirect.
2334 // |request_method| is the method to use for the initial request.
2335 // |redirect_method| is the method that is expected to be used for the second
2336 // request, after redirection.
2337 // If |include_data| is true, data is uploaded with the request. The
2338 // response body is expected to match it exactly, if and only if
2339 // |request_method| == |redirect_method|.
2340 void HTTPRedirectMethodTest(const GURL& redirect_url,
2341 const std::string& request_method,
2342 const std::string& redirect_method,
2343 bool include_data) {
2344 static const char kData[] = "hello world";
2345 TestDelegate d;
[email protected]94e2bbe2012-06-22 15:26:132346 URLRequest req(redirect_url, &d, &default_context_);
[email protected]21184962011-10-26 00:50:302347 req.set_method(request_method);
2348 if (include_data) {
[email protected]f288ef02012-12-15 20:28:282349 req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
[email protected]21184962011-10-26 00:50:302350 HttpRequestHeaders headers;
2351 headers.SetHeader(HttpRequestHeaders::kContentLength,
2352 base::UintToString(arraysize(kData) - 1));
2353 req.SetExtraRequestHeaders(headers);
2354 }
2355 req.Start();
2356 MessageLoop::current()->Run();
2357 EXPECT_EQ(redirect_method, req.method());
2358 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
2359 EXPECT_EQ(OK, req.status().error());
2360 if (include_data) {
2361 if (request_method == redirect_method) {
2362 EXPECT_EQ(kData, d.data_received());
2363 } else {
2364 EXPECT_NE(kData, d.data_received());
2365 }
2366 }
2367 if (HasFailure())
2368 LOG(WARNING) << "Request method was: " << request_method;
2369 }
2370
[email protected]762d2db2010-01-11 19:03:012371 void HTTPUploadDataOperationTest(const std::string& method) {
[email protected]762d2db2010-01-11 19:03:012372 const int kMsgSize = 20000; // multiple of 10
2373 const int kIterations = 50;
[email protected]f43b89f32012-05-01 19:39:482374 char* uploadBytes = new char[kMsgSize+1];
2375 char* ptr = uploadBytes;
[email protected]762d2db2010-01-11 19:03:012376 char marker = 'a';
2377 for (int idx = 0; idx < kMsgSize/10; idx++) {
2378 memcpy(ptr, "----------", 10);
2379 ptr += 10;
2380 if (idx % 100 == 0) {
2381 ptr--;
2382 *ptr++ = marker;
2383 if (++marker > 'z')
2384 marker = 'a';
2385 }
2386 }
2387 uploadBytes[kMsgSize] = '\0';
2388
[email protected]762d2db2010-01-11 19:03:012389 for (int i = 0; i < kIterations; ++i) {
2390 TestDelegate d;
[email protected]94e2bbe2012-06-22 15:26:132391 URLRequest r(test_server_.GetURL("echo"), &d, &default_context_);
[email protected]762d2db2010-01-11 19:03:012392 r.set_method(method.c_str());
2393
[email protected]f288ef02012-12-15 20:28:282394 r.set_upload(make_scoped_ptr(CreateSimpleUploadData(uploadBytes)));
[email protected]762d2db2010-01-11 19:03:012395
2396 r.Start();
2397 EXPECT_TRUE(r.is_pending());
2398
2399 MessageLoop::current()->Run();
2400
[email protected]329b68b2012-11-14 17:54:272401 ASSERT_EQ(1, d.response_started_count())
2402 << "request failed: " << r.status().status()
2403 << ", os error: " << r.status().error();
[email protected]762d2db2010-01-11 19:03:012404
2405 EXPECT_FALSE(d.received_data_before_response());
2406 EXPECT_EQ(uploadBytes, d.data_received());
[email protected]762d2db2010-01-11 19:03:012407 }
2408 delete[] uploadBytes;
2409 }
2410
[email protected]ef2bf422012-05-11 03:27:092411 void AddChunksToUpload(URLRequest* r) {
[email protected]0c9bf872011-03-04 17:53:222412 r->AppendChunkToUpload("a", 1, false);
2413 r->AppendChunkToUpload("bcd", 3, false);
2414 r->AppendChunkToUpload("this is a longer chunk than before.", 35, false);
2415 r->AppendChunkToUpload("\r\n\r\n", 4, false);
2416 r->AppendChunkToUpload("0", 1, false);
2417 r->AppendChunkToUpload("2323", 4, true);
[email protected]699efe602011-01-25 07:17:112418 }
2419
[email protected]ef2bf422012-05-11 03:27:092420 void VerifyReceivedDataMatchesChunks(URLRequest* r, TestDelegate* d) {
[email protected]699efe602011-01-25 07:17:112421 // This should match the chunks sent by AddChunksToUpload().
[email protected]329b68b2012-11-14 17:54:272422 const std::string expected_data =
[email protected]699efe602011-01-25 07:17:112423 "abcdthis is a longer chunk than before.\r\n\r\n02323";
2424
[email protected]329b68b2012-11-14 17:54:272425 ASSERT_EQ(1, d->response_started_count())
2426 << "request failed: " << r->status().status()
2427 << ", os error: " << r->status().error();
[email protected]699efe602011-01-25 07:17:112428
2429 EXPECT_FALSE(d->received_data_before_response());
2430
[email protected]329b68b2012-11-14 17:54:272431 EXPECT_EQ(expected_data.size(), static_cast<size_t>(d->bytes_received()));
2432 EXPECT_EQ(expected_data, d->data_received());
[email protected]699efe602011-01-25 07:17:112433 }
2434
[email protected]ede03212012-09-07 12:52:262435 bool DoManyCookiesRequest(int num_cookies) {
[email protected]263163f2012-06-14 22:40:342436 TestDelegate d;
2437 URLRequest r(test_server_.GetURL("set-many-cookies?" +
2438 base::IntToString(num_cookies)),
[email protected]94e2bbe2012-06-22 15:26:132439 &d,
2440 &default_context_);
[email protected]263163f2012-06-14 22:40:342441
2442 r.Start();
2443 EXPECT_TRUE(r.is_pending());
2444
2445 MessageLoop::current()->Run();
2446
2447 bool is_success = r.status().is_success();
2448
[email protected]ede03212012-09-07 12:52:262449 if (!is_success) {
[email protected]263163f2012-06-14 22:40:342450 // Requests handled by ChromeFrame send a less precise error message,
2451 // ERR_CONNECTION_ABORTED.
2452 EXPECT_TRUE(r.status().error() == ERR_RESPONSE_HEADERS_TOO_BIG ||
2453 r.status().error() == ERR_CONNECTION_ABORTED);
2454 // The test server appears to be unable to handle subsequent requests
2455 // after this error is triggered. Force it to restart.
2456 EXPECT_TRUE(test_server_.Stop());
2457 EXPECT_TRUE(test_server_.Start());
2458 }
2459
2460 return is_success;
2461 }
2462
[email protected]1700c6a2012-02-22 18:07:072463 LocalHttpTestServer test_server_;
[email protected]b89290212009-08-14 22:37:352464};
2465
[email protected]95409e12010-08-17 20:07:112466// In this unit test, we're using the HTTPTestServer as a proxy server and
2467// issuing a CONNECT request with the magic host name "www.redirect.com".
2468// The HTTPTestServer will return a 302 response, which we should not
2469// follow.
[email protected]f2f31b32013-01-16 23:24:092470TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) {
[email protected]95409e12010-08-17 20:07:112471 ASSERT_TRUE(test_server_.Start());
2472
[email protected]ceefd7fd2012-11-29 00:36:242473 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]d5a4dd62012-05-23 01:41:042474 TestURLRequestContextWithProxy context(
2475 test_server_.host_port_pair().ToString(),
2476 &network_delegate);
[email protected]87a09a92011-07-14 15:50:502477
[email protected]d1ec59082009-02-11 02:48:152478 TestDelegate d;
2479 {
[email protected]94e2bbe2012-06-22 15:26:132480 URLRequest r(GURL("https://www.redirect.com/"), &d, &context);
[email protected]d1ec59082009-02-11 02:48:152481 r.Start();
2482 EXPECT_TRUE(r.is_pending());
2483
2484 MessageLoop::current()->Run();
2485
[email protected]7461a402011-03-24 23:19:512486 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
[email protected]d0cc35b2011-09-08 12:02:052487 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().error());
[email protected]dc651782009-02-14 01:45:082488 EXPECT_EQ(1, d.response_started_count());
[email protected]d1ec59082009-02-11 02:48:152489 // We should not have followed the redirect.
2490 EXPECT_EQ(0, d.received_redirect_count());
2491 }
2492}
2493
[email protected]8202d0c2011-02-23 08:31:142494// This is the same as the previous test, but checks that the network delegate
2495// registers the error.
[email protected]c044616e2013-02-20 02:01:262496TEST_F(URLRequestTestHTTP, NetworkDelegateTunnelConnectionFailed) {
[email protected]8202d0c2011-02-23 08:31:142497 ASSERT_TRUE(test_server_.Start());
2498
[email protected]ceefd7fd2012-11-29 00:36:242499 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]d5a4dd62012-05-23 01:41:042500 TestURLRequestContextWithProxy context(
2501 test_server_.host_port_pair().ToString(),
2502 &network_delegate);
[email protected]87a09a92011-07-14 15:50:502503
[email protected]8202d0c2011-02-23 08:31:142504 TestDelegate d;
2505 {
[email protected]94e2bbe2012-06-22 15:26:132506 URLRequest r(GURL("https://www.redirect.com/"), &d, &context);
[email protected]8202d0c2011-02-23 08:31:142507 r.Start();
2508 EXPECT_TRUE(r.is_pending());
2509
2510 MessageLoop::current()->Run();
2511
[email protected]7461a402011-03-24 23:19:512512 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
[email protected]d0cc35b2011-09-08 12:02:052513 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().error());
[email protected]8202d0c2011-02-23 08:31:142514 EXPECT_EQ(1, d.response_started_count());
2515 // We should not have followed the redirect.
2516 EXPECT_EQ(0, d.received_redirect_count());
2517
2518 EXPECT_EQ(1, network_delegate.error_count());
[email protected]d0cc35b2011-09-08 12:02:052519 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, network_delegate.last_error());
[email protected]8202d0c2011-02-23 08:31:142520 }
2521}
2522
[email protected]dc5a5cf2012-09-26 02:49:302523// Tests that we can block and asynchronously return OK in various stages.
2524TEST_F(URLRequestTestHTTP, NetworkDelegateBlockAsynchronously) {
2525 static const BlockingNetworkDelegate::Stage blocking_stages[] = {
2526 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
2527 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
2528 BlockingNetworkDelegate::ON_HEADERS_RECEIVED
2529 };
2530 static const size_t blocking_stages_length = arraysize(blocking_stages);
2531
2532 ASSERT_TRUE(test_server_.Start());
2533
2534 TestDelegate d;
2535 BlockingNetworkDelegate network_delegate(
2536 BlockingNetworkDelegate::USER_CALLBACK);
2537 network_delegate.set_block_on(
2538 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST |
2539 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS |
2540 BlockingNetworkDelegate::ON_HEADERS_RECEIVED);
2541
2542 TestURLRequestContext context(true);
2543 context.set_network_delegate(&network_delegate);
2544 context.Init();
2545
2546 {
2547 URLRequest r(test_server_.GetURL("empty.html"), &d, &context);
2548
2549 r.Start();
2550 for (size_t i = 0; i < blocking_stages_length; ++i) {
2551 MessageLoop::current()->Run();
2552 EXPECT_EQ(blocking_stages[i],
2553 network_delegate.stage_blocked_for_callback());
2554 network_delegate.DoCallback(OK);
2555 }
2556 MessageLoop::current()->Run();
2557 EXPECT_EQ(200, r.GetResponseCode());
2558 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2559 EXPECT_EQ(1, network_delegate.created_requests());
2560 EXPECT_EQ(0, network_delegate.destroyed_requests());
2561 }
2562 EXPECT_EQ(1, network_delegate.destroyed_requests());
2563}
2564
[email protected]4c76d7c2011-04-15 19:14:122565// Tests that the network delegate can block and cancel a request.
2566TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) {
2567 ASSERT_TRUE(test_server_.Start());
2568
2569 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302570 BlockingNetworkDelegate network_delegate(
2571 BlockingNetworkDelegate::AUTO_CALLBACK);
2572 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
2573 network_delegate.set_retval(ERR_EMPTY_RESPONSE);
[email protected]4c76d7c2011-04-15 19:14:122574
[email protected]d5a4dd62012-05-23 01:41:042575 TestURLRequestContextWithProxy context(
2576 test_server_.host_port_pair().ToString(),
2577 &network_delegate);
[email protected]87a09a92011-07-14 15:50:502578
[email protected]4c76d7c2011-04-15 19:14:122579 {
[email protected]007b3f82013-04-09 08:46:452580 URLRequest r(test_server_.GetURL(std::string()), &d, &context);
[email protected]4c76d7c2011-04-15 19:14:122581
2582 r.Start();
2583 MessageLoop::current()->Run();
2584
2585 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
[email protected]d0cc35b2011-09-08 12:02:052586 EXPECT_EQ(ERR_EMPTY_RESPONSE, r.status().error());
[email protected]4c76d7c2011-04-15 19:14:122587 EXPECT_EQ(1, network_delegate.created_requests());
2588 EXPECT_EQ(0, network_delegate.destroyed_requests());
2589 }
2590 EXPECT_EQ(1, network_delegate.destroyed_requests());
2591}
2592
[email protected]b4438d32012-09-27 06:15:302593// Helper function for NetworkDelegateCancelRequestAsynchronously and
2594// NetworkDelegateCancelRequestSynchronously. Sets up a blocking network
2595// delegate operating in |block_mode| and a request for |url|. It blocks the
2596// request in |stage| and cancels it with ERR_BLOCKED_BY_CLIENT.
2597void NetworkDelegateCancelRequest(BlockingNetworkDelegate::BlockMode block_mode,
2598 BlockingNetworkDelegate::Stage stage,
2599 const GURL& url) {
[email protected]3cd384c602011-08-31 16:12:362600 TestDelegate d;
[email protected]b4438d32012-09-27 06:15:302601 BlockingNetworkDelegate network_delegate(block_mode);
2602 network_delegate.set_retval(ERR_BLOCKED_BY_CLIENT);
2603 network_delegate.set_block_on(stage);
[email protected]3cd384c602011-08-31 16:12:362604
[email protected]b4438d32012-09-27 06:15:302605 TestURLRequestContext context(true);
2606 context.set_network_delegate(&network_delegate);
2607 context.Init();
[email protected]3cd384c602011-08-31 16:12:362608
2609 {
[email protected]b4438d32012-09-27 06:15:302610 URLRequest r(url, &d, &context);
[email protected]3cd384c602011-08-31 16:12:362611
2612 r.Start();
2613 MessageLoop::current()->Run();
2614
2615 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
[email protected]b4438d32012-09-27 06:15:302616 EXPECT_EQ(ERR_BLOCKED_BY_CLIENT, r.status().error());
[email protected]3cd384c602011-08-31 16:12:362617 EXPECT_EQ(1, network_delegate.created_requests());
2618 EXPECT_EQ(0, network_delegate.destroyed_requests());
2619 }
2620 EXPECT_EQ(1, network_delegate.destroyed_requests());
2621}
2622
[email protected]b4438d32012-09-27 06:15:302623// The following 3 tests check that the network delegate can cancel a request
2624// synchronously in various stages of the request.
2625TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously1) {
2626 ASSERT_TRUE(test_server_.Start());
2627 NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS,
2628 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
[email protected]007b3f82013-04-09 08:46:452629 test_server_.GetURL(std::string()));
[email protected]b4438d32012-09-27 06:15:302630}
2631
2632TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously2) {
2633 ASSERT_TRUE(test_server_.Start());
2634 NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS,
2635 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
[email protected]007b3f82013-04-09 08:46:452636 test_server_.GetURL(std::string()));
[email protected]b4438d32012-09-27 06:15:302637}
2638
2639TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously3) {
2640 ASSERT_TRUE(test_server_.Start());
2641 NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS,
2642 BlockingNetworkDelegate::ON_HEADERS_RECEIVED,
[email protected]007b3f82013-04-09 08:46:452643 test_server_.GetURL(std::string()));
[email protected]b4438d32012-09-27 06:15:302644}
2645
2646// The following 3 tests check that the network delegate can cancel a request
2647// asynchronously in various stages of the request.
2648TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously1) {
2649 ASSERT_TRUE(test_server_.Start());
2650 NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK,
2651 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
[email protected]007b3f82013-04-09 08:46:452652 test_server_.GetURL(std::string()));
[email protected]b4438d32012-09-27 06:15:302653}
2654
2655TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously2) {
2656 ASSERT_TRUE(test_server_.Start());
2657 NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK,
2658 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
[email protected]007b3f82013-04-09 08:46:452659 test_server_.GetURL(std::string()));
[email protected]b4438d32012-09-27 06:15:302660}
2661
2662TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously3) {
2663 ASSERT_TRUE(test_server_.Start());
2664 NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK,
2665 BlockingNetworkDelegate::ON_HEADERS_RECEIVED,
[email protected]007b3f82013-04-09 08:46:452666 test_server_.GetURL(std::string()));
[email protected]b4438d32012-09-27 06:15:302667}
2668
[email protected]4c76d7c2011-04-15 19:14:122669// Tests that the network delegate can block and redirect a request to a new
2670// URL.
2671TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) {
2672 ASSERT_TRUE(test_server_.Start());
2673
2674 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302675 BlockingNetworkDelegate network_delegate(
2676 BlockingNetworkDelegate::AUTO_CALLBACK);
2677 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
[email protected]4c76d7c2011-04-15 19:14:122678 GURL redirect_url(test_server_.GetURL("simple.html"));
2679 network_delegate.set_redirect_url(redirect_url);
2680
[email protected]d5a4dd62012-05-23 01:41:042681 TestURLRequestContextWithProxy context(
2682 test_server_.host_port_pair().ToString(),
2683 &network_delegate);
[email protected]87a09a92011-07-14 15:50:502684
[email protected]4c76d7c2011-04-15 19:14:122685 {
[email protected]8f1ac082011-04-19 21:14:132686 GURL original_url(test_server_.GetURL("empty.html"));
[email protected]94e2bbe2012-06-22 15:26:132687 URLRequest r(original_url, &d, &context);
[email protected]4c76d7c2011-04-15 19:14:122688
2689 r.Start();
2690 MessageLoop::current()->Run();
2691
2692 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
[email protected]d0cc35b2011-09-08 12:02:052693 EXPECT_EQ(0, r.status().error());
[email protected]4c76d7c2011-04-15 19:14:122694 EXPECT_EQ(redirect_url, r.url());
[email protected]8f1ac082011-04-19 21:14:132695 EXPECT_EQ(original_url, r.original_url());
2696 EXPECT_EQ(2U, r.url_chain().size());
[email protected]4c76d7c2011-04-15 19:14:122697 EXPECT_EQ(1, network_delegate.created_requests());
2698 EXPECT_EQ(0, network_delegate.destroyed_requests());
2699 }
2700 EXPECT_EQ(1, network_delegate.destroyed_requests());
2701}
2702
[email protected]b813ed72012-04-05 08:21:362703// Tests that the network delegate can block and redirect a request to a new
2704// URL by setting a redirect_url and returning in OnBeforeURLRequest directly.
2705TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) {
2706 ASSERT_TRUE(test_server_.Start());
2707
2708 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302709 BlockingNetworkDelegate network_delegate(
2710 BlockingNetworkDelegate::SYNCHRONOUS);
[email protected]b813ed72012-04-05 08:21:362711 GURL redirect_url(test_server_.GetURL("simple.html"));
2712 network_delegate.set_redirect_url(redirect_url);
[email protected]b813ed72012-04-05 08:21:362713
[email protected]d5a4dd62012-05-23 01:41:042714 TestURLRequestContextWithProxy context(
2715 test_server_.host_port_pair().ToString(),
2716 &network_delegate);
[email protected]b813ed72012-04-05 08:21:362717
2718 {
2719 GURL original_url(test_server_.GetURL("empty.html"));
[email protected]94e2bbe2012-06-22 15:26:132720 URLRequest r(original_url, &d, &context);
[email protected]b813ed72012-04-05 08:21:362721
2722 r.Start();
2723 MessageLoop::current()->Run();
2724
2725 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2726 EXPECT_EQ(0, r.status().error());
2727 EXPECT_EQ(redirect_url, r.url());
2728 EXPECT_EQ(original_url, r.original_url());
2729 EXPECT_EQ(2U, r.url_chain().size());
2730 EXPECT_EQ(1, network_delegate.created_requests());
2731 EXPECT_EQ(0, network_delegate.destroyed_requests());
2732 }
2733 EXPECT_EQ(1, network_delegate.destroyed_requests());
2734}
2735
[email protected]3c5ca8c2011-09-29 01:14:512736// Tests that redirects caused by the network delegate preserve POST data.
2737TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) {
2738 ASSERT_TRUE(test_server_.Start());
2739
2740 const char kData[] = "hello world";
2741
2742 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302743 BlockingNetworkDelegate network_delegate(
2744 BlockingNetworkDelegate::AUTO_CALLBACK);
2745 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
[email protected]3c5ca8c2011-09-29 01:14:512746 GURL redirect_url(test_server_.GetURL("echo"));
2747 network_delegate.set_redirect_url(redirect_url);
2748
[email protected]ef2bf422012-05-11 03:27:092749 TestURLRequestContext context(true);
2750 context.set_network_delegate(&network_delegate);
2751 context.Init();
[email protected]3c5ca8c2011-09-29 01:14:512752
2753 {
2754 GURL original_url(test_server_.GetURL("empty.html"));
[email protected]94e2bbe2012-06-22 15:26:132755 URLRequest r(original_url, &d, &context);
[email protected]3c5ca8c2011-09-29 01:14:512756 r.set_method("POST");
[email protected]f288ef02012-12-15 20:28:282757 r.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
[email protected]3c5ca8c2011-09-29 01:14:512758 HttpRequestHeaders headers;
2759 headers.SetHeader(HttpRequestHeaders::kContentLength,
2760 base::UintToString(arraysize(kData) - 1));
2761 r.SetExtraRequestHeaders(headers);
[email protected]3c5ca8c2011-09-29 01:14:512762 r.Start();
2763 MessageLoop::current()->Run();
2764
2765 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2766 EXPECT_EQ(0, r.status().error());
2767 EXPECT_EQ(redirect_url, r.url());
2768 EXPECT_EQ(original_url, r.original_url());
2769 EXPECT_EQ(2U, r.url_chain().size());
2770 EXPECT_EQ(1, network_delegate.created_requests());
2771 EXPECT_EQ(0, network_delegate.destroyed_requests());
2772 EXPECT_EQ("POST", r.method());
2773 EXPECT_EQ(kData, d.data_received());
2774 }
2775 EXPECT_EQ(1, network_delegate.destroyed_requests());
2776}
2777
[email protected]c2911d72011-10-03 22:16:362778// Tests that the network delegate can synchronously complete OnAuthRequired
2779// by taking no action. This indicates that the NetworkDelegate does not want to
2780// handle the challenge, and is passing the buck along to the
2781// URLRequest::Delegate.
2782TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) {
2783 ASSERT_TRUE(test_server_.Start());
2784
2785 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302786 BlockingNetworkDelegate network_delegate(
2787 BlockingNetworkDelegate::SYNCHRONOUS);
[email protected]c2911d72011-10-03 22:16:362788
[email protected]ef2bf422012-05-11 03:27:092789 TestURLRequestContext context(true);
2790 context.set_network_delegate(&network_delegate);
2791 context.Init();
[email protected]c2911d72011-10-03 22:16:362792
[email protected]f3cf9802011-10-28 18:44:582793 d.set_credentials(AuthCredentials(kUser, kSecret));
[email protected]c2911d72011-10-03 22:16:362794
2795 {
2796 GURL url(test_server_.GetURL("auth-basic"));
[email protected]94e2bbe2012-06-22 15:26:132797 URLRequest r(url, &d, &context);
[email protected]c2911d72011-10-03 22:16:362798 r.Start();
2799 MessageLoop::current()->Run();
2800
2801 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2802 EXPECT_EQ(0, r.status().error());
2803 EXPECT_EQ(200, r.GetResponseCode());
2804 EXPECT_TRUE(d.auth_required_called());
2805 EXPECT_EQ(1, network_delegate.created_requests());
2806 EXPECT_EQ(0, network_delegate.destroyed_requests());
2807 }
2808 EXPECT_EQ(1, network_delegate.destroyed_requests());
2809}
2810
2811// Tests that the network delegate can synchronously complete OnAuthRequired
2812// by setting credentials.
2813TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) {
2814 ASSERT_TRUE(test_server_.Start());
2815
2816 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302817 BlockingNetworkDelegate network_delegate(
2818 BlockingNetworkDelegate::SYNCHRONOUS);
2819 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]c2911d72011-10-03 22:16:362820 network_delegate.set_auth_retval(
2821 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
2822
[email protected]f3cf9802011-10-28 18:44:582823 network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret));
[email protected]c2911d72011-10-03 22:16:362824
[email protected]ef2bf422012-05-11 03:27:092825 TestURLRequestContext context(true);
2826 context.set_network_delegate(&network_delegate);
2827 context.Init();
[email protected]c2911d72011-10-03 22:16:362828
2829 {
2830 GURL url(test_server_.GetURL("auth-basic"));
[email protected]94e2bbe2012-06-22 15:26:132831 URLRequest r(url, &d, &context);
[email protected]c2911d72011-10-03 22:16:362832 r.Start();
2833 MessageLoop::current()->Run();
2834
2835 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2836 EXPECT_EQ(0, r.status().error());
2837 EXPECT_EQ(200, r.GetResponseCode());
2838 EXPECT_FALSE(d.auth_required_called());
2839 EXPECT_EQ(1, network_delegate.created_requests());
2840 EXPECT_EQ(0, network_delegate.destroyed_requests());
2841 }
2842 EXPECT_EQ(1, network_delegate.destroyed_requests());
2843}
2844
2845// Tests that the network delegate can synchronously complete OnAuthRequired
2846// by cancelling authentication.
2847TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) {
2848 ASSERT_TRUE(test_server_.Start());
2849
2850 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302851 BlockingNetworkDelegate network_delegate(
2852 BlockingNetworkDelegate::SYNCHRONOUS);
2853 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]c2911d72011-10-03 22:16:362854 network_delegate.set_auth_retval(
2855 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH);
2856
[email protected]ef2bf422012-05-11 03:27:092857 TestURLRequestContext context(true);
2858 context.set_network_delegate(&network_delegate);
2859 context.Init();
[email protected]c2911d72011-10-03 22:16:362860
2861 {
2862 GURL url(test_server_.GetURL("auth-basic"));
[email protected]94e2bbe2012-06-22 15:26:132863 URLRequest r(url, &d, &context);
[email protected]c2911d72011-10-03 22:16:362864 r.Start();
2865 MessageLoop::current()->Run();
2866
2867 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2868 EXPECT_EQ(OK, r.status().error());
2869 EXPECT_EQ(401, r.GetResponseCode());
2870 EXPECT_FALSE(d.auth_required_called());
2871 EXPECT_EQ(1, network_delegate.created_requests());
2872 EXPECT_EQ(0, network_delegate.destroyed_requests());
2873 }
2874 EXPECT_EQ(1, network_delegate.destroyed_requests());
2875}
2876
2877// Tests that the network delegate can asynchronously complete OnAuthRequired
2878// by taking no action. This indicates that the NetworkDelegate does not want
2879// to handle the challenge, and is passing the buck along to the
2880// URLRequest::Delegate.
2881TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) {
2882 ASSERT_TRUE(test_server_.Start());
2883
2884 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302885 BlockingNetworkDelegate network_delegate(
2886 BlockingNetworkDelegate::AUTO_CALLBACK);
2887 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]c2911d72011-10-03 22:16:362888
[email protected]ef2bf422012-05-11 03:27:092889 TestURLRequestContext context(true);
2890 context.set_network_delegate(&network_delegate);
2891 context.Init();
[email protected]c2911d72011-10-03 22:16:362892
[email protected]f3cf9802011-10-28 18:44:582893 d.set_credentials(AuthCredentials(kUser, kSecret));
[email protected]c2911d72011-10-03 22:16:362894
2895 {
2896 GURL url(test_server_.GetURL("auth-basic"));
[email protected]94e2bbe2012-06-22 15:26:132897 URLRequest r(url, &d, &context);
[email protected]c2911d72011-10-03 22:16:362898 r.Start();
2899 MessageLoop::current()->Run();
2900
2901 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2902 EXPECT_EQ(0, r.status().error());
2903 EXPECT_EQ(200, r.GetResponseCode());
2904 EXPECT_TRUE(d.auth_required_called());
2905 EXPECT_EQ(1, network_delegate.created_requests());
2906 EXPECT_EQ(0, network_delegate.destroyed_requests());
2907 }
2908 EXPECT_EQ(1, network_delegate.destroyed_requests());
2909}
2910
2911// Tests that the network delegate can asynchronously complete OnAuthRequired
2912// by setting credentials.
2913TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) {
2914 ASSERT_TRUE(test_server_.Start());
2915
2916 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302917 BlockingNetworkDelegate network_delegate(
2918 BlockingNetworkDelegate::AUTO_CALLBACK);
2919 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]c2911d72011-10-03 22:16:362920 network_delegate.set_auth_retval(
[email protected]c2911d72011-10-03 22:16:362921 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
2922
[email protected]f3cf9802011-10-28 18:44:582923 AuthCredentials auth_credentials(kUser, kSecret);
[email protected]c2911d72011-10-03 22:16:362924 network_delegate.set_auth_credentials(auth_credentials);
2925
[email protected]ef2bf422012-05-11 03:27:092926 TestURLRequestContext context(true);
2927 context.set_network_delegate(&network_delegate);
2928 context.Init();
[email protected]c2911d72011-10-03 22:16:362929
2930 {
2931 GURL url(test_server_.GetURL("auth-basic"));
[email protected]94e2bbe2012-06-22 15:26:132932 URLRequest r(url, &d, &context);
[email protected]c2911d72011-10-03 22:16:362933 r.Start();
2934 MessageLoop::current()->Run();
2935
2936 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2937 EXPECT_EQ(0, r.status().error());
2938
2939 EXPECT_EQ(200, r.GetResponseCode());
2940 EXPECT_FALSE(d.auth_required_called());
2941 EXPECT_EQ(1, network_delegate.created_requests());
2942 EXPECT_EQ(0, network_delegate.destroyed_requests());
2943 }
2944 EXPECT_EQ(1, network_delegate.destroyed_requests());
2945}
2946
2947// Tests that the network delegate can asynchronously complete OnAuthRequired
2948// by cancelling authentication.
2949TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) {
2950 ASSERT_TRUE(test_server_.Start());
2951
2952 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302953 BlockingNetworkDelegate network_delegate(
2954 BlockingNetworkDelegate::AUTO_CALLBACK);
2955 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]c2911d72011-10-03 22:16:362956 network_delegate.set_auth_retval(
[email protected]c2911d72011-10-03 22:16:362957 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH);
2958
[email protected]ef2bf422012-05-11 03:27:092959 TestURLRequestContext context(true);
2960 context.set_network_delegate(&network_delegate);
2961 context.Init();
[email protected]c2911d72011-10-03 22:16:362962
2963 {
2964 GURL url(test_server_.GetURL("auth-basic"));
[email protected]94e2bbe2012-06-22 15:26:132965 URLRequest r(url, &d, &context);
[email protected]c2911d72011-10-03 22:16:362966 r.Start();
2967 MessageLoop::current()->Run();
2968
2969 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2970 EXPECT_EQ(OK, r.status().error());
2971 EXPECT_EQ(401, r.GetResponseCode());
2972 EXPECT_FALSE(d.auth_required_called());
2973 EXPECT_EQ(1, network_delegate.created_requests());
2974 EXPECT_EQ(0, network_delegate.destroyed_requests());
2975 }
2976 EXPECT_EQ(1, network_delegate.destroyed_requests());
2977}
2978
[email protected]9045b8822012-01-13 20:35:352979// Tests that we can handle when a network request was canceled while we were
2980// waiting for the network delegate.
2981// Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback.
2982TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) {
2983 ASSERT_TRUE(test_server_.Start());
2984
2985 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302986 BlockingNetworkDelegate network_delegate(
2987 BlockingNetworkDelegate::USER_CALLBACK);
2988 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
[email protected]9045b8822012-01-13 20:35:352989
[email protected]ef2bf422012-05-11 03:27:092990 TestURLRequestContext context(true);
2991 context.set_network_delegate(&network_delegate);
2992 context.Init();
[email protected]9045b8822012-01-13 20:35:352993
2994 {
[email protected]007b3f82013-04-09 08:46:452995 URLRequest r(test_server_.GetURL(std::string()), &d, &context);
[email protected]9045b8822012-01-13 20:35:352996
2997 r.Start();
[email protected]dc5a5cf2012-09-26 02:49:302998 MessageLoop::current()->Run();
2999 EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
3000 network_delegate.stage_blocked_for_callback());
[email protected]9045b8822012-01-13 20:35:353001 EXPECT_EQ(0, network_delegate.completed_requests());
3002 // Cancel before callback.
3003 r.Cancel();
3004 // Ensure that network delegate is notified.
3005 EXPECT_EQ(1, network_delegate.completed_requests());
3006 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
3007 EXPECT_EQ(ERR_ABORTED, r.status().error());
3008 EXPECT_EQ(1, network_delegate.created_requests());
3009 EXPECT_EQ(0, network_delegate.destroyed_requests());
3010 }
3011 EXPECT_EQ(1, network_delegate.destroyed_requests());
3012}
3013
3014// Tests that we can handle when a network request was canceled while we were
3015// waiting for the network delegate.
3016// Part 2: Request is cancelled while waiting for OnBeforeSendHeaders callback.
3017TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) {
3018 ASSERT_TRUE(test_server_.Start());
3019
3020 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303021 BlockingNetworkDelegate network_delegate(
3022 BlockingNetworkDelegate::USER_CALLBACK);
3023 network_delegate.set_block_on(
3024 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS);
[email protected]9045b8822012-01-13 20:35:353025
[email protected]ef2bf422012-05-11 03:27:093026 TestURLRequestContext context(true);
3027 context.set_network_delegate(&network_delegate);
3028 context.Init();
[email protected]9045b8822012-01-13 20:35:353029
3030 {
[email protected]007b3f82013-04-09 08:46:453031 URLRequest r(test_server_.GetURL(std::string()), &d, &context);
[email protected]9045b8822012-01-13 20:35:353032
3033 r.Start();
[email protected]dc5a5cf2012-09-26 02:49:303034 MessageLoop::current()->Run();
3035 EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
3036 network_delegate.stage_blocked_for_callback());
[email protected]9045b8822012-01-13 20:35:353037 EXPECT_EQ(0, network_delegate.completed_requests());
3038 // Cancel before callback.
3039 r.Cancel();
3040 // Ensure that network delegate is notified.
3041 EXPECT_EQ(1, network_delegate.completed_requests());
3042 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
3043 EXPECT_EQ(ERR_ABORTED, r.status().error());
3044 EXPECT_EQ(1, network_delegate.created_requests());
3045 EXPECT_EQ(0, network_delegate.destroyed_requests());
3046 }
3047 EXPECT_EQ(1, network_delegate.destroyed_requests());
3048}
3049
3050// Tests that we can handle when a network request was canceled while we were
3051// waiting for the network delegate.
3052// Part 3: Request is cancelled while waiting for OnHeadersReceived callback.
3053TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) {
3054 ASSERT_TRUE(test_server_.Start());
3055
3056 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303057 BlockingNetworkDelegate network_delegate(
3058 BlockingNetworkDelegate::USER_CALLBACK);
3059 network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED);
[email protected]9045b8822012-01-13 20:35:353060
[email protected]ef2bf422012-05-11 03:27:093061 TestURLRequestContext context(true);
3062 context.set_network_delegate(&network_delegate);
3063 context.Init();
[email protected]9045b8822012-01-13 20:35:353064
3065 {
[email protected]007b3f82013-04-09 08:46:453066 URLRequest r(test_server_.GetURL(std::string()), &d, &context);
[email protected]9045b8822012-01-13 20:35:353067
3068 r.Start();
[email protected]dc5a5cf2012-09-26 02:49:303069 MessageLoop::current()->Run();
3070 EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED,
3071 network_delegate.stage_blocked_for_callback());
[email protected]9045b8822012-01-13 20:35:353072 EXPECT_EQ(0, network_delegate.completed_requests());
3073 // Cancel before callback.
3074 r.Cancel();
3075 // Ensure that network delegate is notified.
3076 EXPECT_EQ(1, network_delegate.completed_requests());
3077 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
3078 EXPECT_EQ(ERR_ABORTED, r.status().error());
3079 EXPECT_EQ(1, network_delegate.created_requests());
3080 EXPECT_EQ(0, network_delegate.destroyed_requests());
3081 }
3082 EXPECT_EQ(1, network_delegate.destroyed_requests());
3083}
3084
3085// Tests that we can handle when a network request was canceled while we were
3086// waiting for the network delegate.
3087// Part 4: Request is cancelled while waiting for OnAuthRequired callback.
[email protected]bfe8b302013-02-15 12:16:023088TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) {
[email protected]9045b8822012-01-13 20:35:353089 ASSERT_TRUE(test_server_.Start());
3090
3091 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303092 BlockingNetworkDelegate network_delegate(
3093 BlockingNetworkDelegate::USER_CALLBACK);
3094 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]9045b8822012-01-13 20:35:353095
[email protected]ef2bf422012-05-11 03:27:093096 TestURLRequestContext context(true);
3097 context.set_network_delegate(&network_delegate);
3098 context.Init();
[email protected]9045b8822012-01-13 20:35:353099
3100 {
[email protected]94e2bbe2012-06-22 15:26:133101 URLRequest r(test_server_.GetURL("auth-basic"), &d, &context);
[email protected]9045b8822012-01-13 20:35:353102
3103 r.Start();
[email protected]dc5a5cf2012-09-26 02:49:303104 MessageLoop::current()->Run();
3105 EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED,
3106 network_delegate.stage_blocked_for_callback());
[email protected]9045b8822012-01-13 20:35:353107 EXPECT_EQ(0, network_delegate.completed_requests());
3108 // Cancel before callback.
3109 r.Cancel();
3110 // Ensure that network delegate is notified.
3111 EXPECT_EQ(1, network_delegate.completed_requests());
3112 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
3113 EXPECT_EQ(ERR_ABORTED, r.status().error());
3114 EXPECT_EQ(1, network_delegate.created_requests());
3115 EXPECT_EQ(0, network_delegate.destroyed_requests());
3116 }
3117 EXPECT_EQ(1, network_delegate.destroyed_requests());
3118}
3119
[email protected]95409e12010-08-17 20:07:113120// In this unit test, we're using the HTTPTestServer as a proxy server and
3121// issuing a CONNECT request with the magic host name "www.server-auth.com".
3122// The HTTPTestServer will return a 401 response, which we should balk at.
[email protected]b89290212009-08-14 22:37:353123TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) {
[email protected]95409e12010-08-17 20:07:113124 ASSERT_TRUE(test_server_.Start());
3125
[email protected]ceefd7fd2012-11-29 00:36:243126 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]d5a4dd62012-05-23 01:41:043127 TestURLRequestContextWithProxy context(
3128 test_server_.host_port_pair().ToString(),
3129 &network_delegate);
[email protected]87a09a92011-07-14 15:50:503130
[email protected]dc651782009-02-14 01:45:083131 TestDelegate d;
3132 {
[email protected]94e2bbe2012-06-22 15:26:133133 URLRequest r(GURL("https://www.server-auth.com/"), &d, &context);
[email protected]dc651782009-02-14 01:45:083134
3135 r.Start();
3136 EXPECT_TRUE(r.is_pending());
3137
3138 MessageLoop::current()->Run();
3139
[email protected]7461a402011-03-24 23:19:513140 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
[email protected]d0cc35b2011-09-08 12:02:053141 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().error());
[email protected]dc651782009-02-14 01:45:083142 }
3143}
3144
[email protected]b89290212009-08-14 22:37:353145TEST_F(URLRequestTestHTTP, GetTest_NoCache) {
[email protected]95409e12010-08-17 20:07:113146 ASSERT_TRUE(test_server_.Start());
3147
initial.commit586acc5fe2008-07-26 22:42:523148 TestDelegate d;
3149 {
[email protected]007b3f82013-04-09 08:46:453150 URLRequest r(test_server_.GetURL(std::string()), &d, &default_context_);
initial.commit586acc5fe2008-07-26 22:42:523151
3152 r.Start();
3153 EXPECT_TRUE(r.is_pending());
3154
3155 MessageLoop::current()->Run();
3156
3157 EXPECT_EQ(1, d.response_started_count());
3158 EXPECT_FALSE(d.received_data_before_response());
3159 EXPECT_NE(0, d.bytes_received());
[email protected]6d81b482011-02-22 19:47:193160 EXPECT_EQ(test_server_.host_port_pair().host(),
3161 r.GetSocketAddress().host());
3162 EXPECT_EQ(test_server_.host_port_pair().port(),
3163 r.GetSocketAddress().port());
[email protected]c31a54592009-09-04 02:36:163164
[email protected]9e743cd2010-03-16 07:03:533165 // TODO(eroman): Add back the NetLog tests...
initial.commit586acc5fe2008-07-26 22:42:523166 }
initial.commit586acc5fe2008-07-26 22:42:523167}
3168
[email protected]263163f2012-06-14 22:40:343169// This test has the server send a large number of cookies to the client.
3170// To ensure that no number of cookies causes a crash, a galloping binary
3171// search is used to estimate that maximum number of cookies that are accepted
3172// by the browser. Beyond the maximum number, the request will fail with
3173// ERR_RESPONSE_HEADERS_TOO_BIG.
[email protected]69dd6fe2013-02-23 23:15:303174#if defined(OS_WIN)
3175// http://crbug.com/177916
3176#define MAYBE_GetTest_ManyCookies DISABLED_GetTest_ManyCookies
3177#else
3178#define MAYBE_GetTest_ManyCookies GetTest_ManyCookies
3179#endif // defined(OS_WIN)
3180TEST_F(URLRequestTestHTTP, MAYBE_GetTest_ManyCookies) {
[email protected]263163f2012-06-14 22:40:343181 ASSERT_TRUE(test_server_.Start());
3182
3183 int lower_bound = 0;
3184 int upper_bound = 1;
3185
3186 // Double the number of cookies until the response header limits are
3187 // exceeded.
3188 while (DoManyCookiesRequest(upper_bound)) {
3189 lower_bound = upper_bound;
3190 upper_bound *= 2;
3191 ASSERT_LT(upper_bound, 1000000);
3192 }
3193
3194 int tolerance = upper_bound * 0.005;
3195 if (tolerance < 2)
3196 tolerance = 2;
3197
3198 // Perform a binary search to find the highest possible number of cookies,
3199 // within the desired tolerance.
3200 while (upper_bound - lower_bound >= tolerance) {
3201 int num_cookies = (lower_bound + upper_bound) / 2;
3202
3203 if (DoManyCookiesRequest(num_cookies))
3204 lower_bound = num_cookies;
3205 else
3206 upper_bound = num_cookies;
3207 }
3208 // Success: the test did not crash.
3209}
3210
[email protected]b89290212009-08-14 22:37:353211TEST_F(URLRequestTestHTTP, GetTest) {
[email protected]95409e12010-08-17 20:07:113212 ASSERT_TRUE(test_server_.Start());
3213
initial.commit586acc5fe2008-07-26 22:42:523214 TestDelegate d;
3215 {
[email protected]007b3f82013-04-09 08:46:453216 URLRequest r(test_server_.GetURL(std::string()), &d, &default_context_);
initial.commit586acc5fe2008-07-26 22:42:523217
3218 r.Start();
3219 EXPECT_TRUE(r.is_pending());
3220
3221 MessageLoop::current()->Run();
3222
3223 EXPECT_EQ(1, d.response_started_count());
3224 EXPECT_FALSE(d.received_data_before_response());
3225 EXPECT_NE(0, d.bytes_received());
[email protected]6d81b482011-02-22 19:47:193226 EXPECT_EQ(test_server_.host_port_pair().host(),
3227 r.GetSocketAddress().host());
3228 EXPECT_EQ(test_server_.host_port_pair().port(),
3229 r.GetSocketAddress().port());
initial.commit586acc5fe2008-07-26 22:42:523230 }
[email protected]5d7b373e2009-09-02 07:19:033231}
3232
[email protected]58e32bb2013-01-21 18:23:253233TEST_F(URLRequestTestHTTP, GetTestLoadTiming) {
3234 ASSERT_TRUE(test_server_.Start());
3235
3236 TestDelegate d;
3237 {
[email protected]007b3f82013-04-09 08:46:453238 URLRequest r(test_server_.GetURL(std::string()), &d, &default_context_);
[email protected]58e32bb2013-01-21 18:23:253239
3240 r.Start();
3241 EXPECT_TRUE(r.is_pending());
3242
3243 MessageLoop::current()->Run();
3244
3245 LoadTimingInfo load_timing_info;
[email protected]0a703912013-02-02 04:25:173246 r.GetLoadTimingInfo(&load_timing_info);
[email protected]58e32bb2013-01-21 18:23:253247 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
3248
3249 EXPECT_EQ(1, d.response_started_count());
3250 EXPECT_FALSE(d.received_data_before_response());
3251 EXPECT_NE(0, d.bytes_received());
3252 EXPECT_EQ(test_server_.host_port_pair().host(),
3253 r.GetSocketAddress().host());
3254 EXPECT_EQ(test_server_.host_port_pair().port(),
3255 r.GetSocketAddress().port());
3256 }
3257}
3258
[email protected]aad63572011-05-24 20:14:393259TEST_F(URLRequestTestHTTP, GetZippedTest) {
3260 ASSERT_TRUE(test_server_.Start());
3261
3262 // Parameter that specifies the Content-Length field in the response:
3263 // C - Compressed length.
3264 // U - Uncompressed length.
3265 // L - Large length (larger than both C & U).
3266 // M - Medium length (between C & U).
3267 // S - Small length (smaller than both C & U).
3268 const char test_parameters[] = "CULMS";
3269 const int num_tests = arraysize(test_parameters)- 1; // Skip NULL.
3270 // C & U should be OK.
[email protected]f0e2bf42011-07-22 21:21:443271 // L & M are larger than the data sent, and show an error.
[email protected]aad63572011-05-24 20:14:393272 // S has too little data, but we seem to accept it.
3273 const bool test_expect_success[num_tests] =
[email protected]f001bd6a2011-12-08 04:31:373274 { true, true, false, false, true };
[email protected]aad63572011-05-24 20:14:393275
3276 for (int i = 0; i < num_tests ; i++) {
3277 TestDelegate d;
3278 {
3279 std::string test_file =
3280 base::StringPrintf("compressedfiles/BullRunSpeech.txt?%c",
3281 test_parameters[i]);
[email protected]aad63572011-05-24 20:14:393282
[email protected]ceefd7fd2012-11-29 00:36:243283 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]ef2bf422012-05-11 03:27:093284 TestURLRequestContext context(true);
3285 context.set_network_delegate(&network_delegate);
3286 context.Init();
[email protected]87a09a92011-07-14 15:50:503287
[email protected]94e2bbe2012-06-22 15:26:133288 URLRequest r(test_server_.GetURL(test_file), &d, &context);
[email protected]aad63572011-05-24 20:14:393289 r.Start();
3290 EXPECT_TRUE(r.is_pending());
3291
3292 MessageLoop::current()->Run();
3293
3294 EXPECT_EQ(1, d.response_started_count());
3295 EXPECT_FALSE(d.received_data_before_response());
3296 VLOG(1) << " Received " << d.bytes_received() << " bytes"
3297 << " status = " << r.status().status()
[email protected]d0cc35b2011-09-08 12:02:053298 << " error = " << r.status().error();
[email protected]aad63572011-05-24 20:14:393299 if (test_expect_success[i]) {
3300 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status())
3301 << " Parameter = \"" << test_file << "\"";
3302 } else {
3303 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
[email protected]5543cbb2012-04-20 16:35:233304 EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, r.status().error())
[email protected]aad63572011-05-24 20:14:393305 << " Parameter = \"" << test_file << "\"";
3306 }
3307 }
3308 }
3309}
3310
[email protected]c044616e2013-02-20 02:01:263311TEST_F(URLRequestTestHTTP, HTTPSToHTTPRedirectNoRefererTest) {
[email protected]95409e12010-08-17 20:07:113312 ASSERT_TRUE(test_server_.Start());
3313
[email protected]6cdfd7f2013-02-08 20:40:153314 TestServer https_test_server(
3315 TestServer::TYPE_HTTPS, TestServer::kLocalhost,
3316 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]95409e12010-08-17 20:07:113317 ASSERT_TRUE(https_test_server.Start());
[email protected]7844480a2009-12-16 21:18:583318
3319 // An https server is sent a request with an https referer,
3320 // and responds with a redirect to an http url. The http
3321 // server should not be sent the referer.
[email protected]007b3f82013-04-09 08:46:453322 GURL http_destination = test_server_.GetURL(std::string());
[email protected]7844480a2009-12-16 21:18:583323 TestDelegate d;
[email protected]ef2bf422012-05-11 03:27:093324 URLRequest req(https_test_server.GetURL(
[email protected]94e2bbe2012-06-22 15:26:133325 "server-redirect?" + http_destination.spec()), &d, &default_context_);
[email protected]99ecf6e2013-04-10 22:46:133326 req.SetReferrer("https://www.referrer.com/");
[email protected]7844480a2009-12-16 21:18:583327 req.Start();
3328 MessageLoop::current()->Run();
3329
3330 EXPECT_EQ(1, d.response_started_count());
3331 EXPECT_EQ(1, d.received_redirect_count());
3332 EXPECT_EQ(http_destination, req.url());
3333 EXPECT_EQ(std::string(), req.referrer());
3334}
3335
[email protected]58e32bb2013-01-21 18:23:253336TEST_F(URLRequestTestHTTP, RedirectLoadTiming) {
3337 ASSERT_TRUE(test_server_.Start());
3338
[email protected]007b3f82013-04-09 08:46:453339 GURL destination_url = test_server_.GetURL(std::string());
3340 GURL original_url =
3341 test_server_.GetURL("server-redirect?" + destination_url.spec());
[email protected]58e32bb2013-01-21 18:23:253342 TestDelegate d;
3343 URLRequest req(original_url, &d, &default_context_);
3344 req.Start();
3345 MessageLoop::current()->Run();
3346
3347 EXPECT_EQ(1, d.response_started_count());
3348 EXPECT_EQ(1, d.received_redirect_count());
3349 EXPECT_EQ(destination_url, req.url());
3350 EXPECT_EQ(original_url, req.original_url());
3351 ASSERT_EQ(2U, req.url_chain().size());
3352 EXPECT_EQ(original_url, req.url_chain()[0]);
3353 EXPECT_EQ(destination_url, req.url_chain()[1]);
3354
3355 LoadTimingInfo load_timing_info_before_redirect;
3356 EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeRedirect(
3357 &load_timing_info_before_redirect));
3358 TestLoadTimingNotReused(load_timing_info_before_redirect,
3359 CONNECT_TIMING_HAS_DNS_TIMES);
3360
3361 LoadTimingInfo load_timing_info;
[email protected]0a703912013-02-02 04:25:173362 req.GetLoadTimingInfo(&load_timing_info);
[email protected]58e32bb2013-01-21 18:23:253363 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
3364
3365 // Check that a new socket was used on redirect, since the server does not
3366 // supposed keep-alive sockets, and that the times before the redirect are
3367 // before the ones recorded for the second request.
3368 EXPECT_NE(load_timing_info_before_redirect.socket_log_id,
3369 load_timing_info.socket_log_id);
3370 EXPECT_LE(load_timing_info_before_redirect.receive_headers_end,
3371 load_timing_info.connect_timing.connect_start);
3372}
3373
[email protected]8f1ac082011-04-19 21:14:133374TEST_F(URLRequestTestHTTP, MultipleRedirectTest) {
3375 ASSERT_TRUE(test_server_.Start());
3376
[email protected]007b3f82013-04-09 08:46:453377 GURL destination_url = test_server_.GetURL(std::string());
3378 GURL middle_redirect_url =
3379 test_server_.GetURL("server-redirect?" + destination_url.spec());
[email protected]8f1ac082011-04-19 21:14:133380 GURL original_url = test_server_.GetURL(
3381 "server-redirect?" + middle_redirect_url.spec());
3382 TestDelegate d;
[email protected]94e2bbe2012-06-22 15:26:133383 URLRequest req(original_url, &d, &default_context_);
[email protected]8f1ac082011-04-19 21:14:133384 req.Start();
3385 MessageLoop::current()->Run();
3386
3387 EXPECT_EQ(1, d.response_started_count());
3388 EXPECT_EQ(2, d.received_redirect_count());
3389 EXPECT_EQ(destination_url, req.url());
3390 EXPECT_EQ(original_url, req.original_url());
3391 ASSERT_EQ(3U, req.url_chain().size());
3392 EXPECT_EQ(original_url, req.url_chain()[0]);
3393 EXPECT_EQ(middle_redirect_url, req.url_chain()[1]);
3394 EXPECT_EQ(destination_url, req.url_chain()[2]);
3395}
3396
[email protected]847c0fa92012-11-06 16:37:423397namespace {
3398
3399const char kExtraHeader[] = "Allow-Snafu";
3400const char kExtraValue[] = "fubar";
3401
3402class RedirectWithAdditionalHeadersDelegate : public TestDelegate {
[email protected]46fadfd2013-02-06 09:40:163403 virtual void OnReceivedRedirect(net::URLRequest* request,
3404 const GURL& new_url,
3405 bool* defer_redirect) OVERRIDE {
[email protected]847c0fa92012-11-06 16:37:423406 TestDelegate::OnReceivedRedirect(request, new_url, defer_redirect);
3407 request->SetExtraRequestHeaderByName(kExtraHeader, kExtraValue, false);
3408 }
3409};
3410
3411} // namespace
3412
3413TEST_F(URLRequestTestHTTP, RedirectWithAdditionalHeadersTest) {
3414 ASSERT_TRUE(test_server_.Start());
3415
3416 GURL destination_url = test_server_.GetURL(
3417 "echoheader?" + std::string(kExtraHeader));
3418 GURL original_url = test_server_.GetURL(
3419 "server-redirect?" + destination_url.spec());
3420 RedirectWithAdditionalHeadersDelegate d;
3421 URLRequest req(original_url, &d, &default_context_);
3422 req.Start();
3423 MessageLoop::current()->Run();
3424
3425 std::string value;
3426 const HttpRequestHeaders& headers = req.extra_request_headers();
3427 EXPECT_TRUE(headers.GetHeader(kExtraHeader, &value));
3428 EXPECT_EQ(kExtraValue, value);
3429 EXPECT_FALSE(req.is_pending());
3430 EXPECT_FALSE(req.is_redirecting());
3431 EXPECT_EQ(kExtraValue, d.data_received());
3432}
3433
[email protected]251a1b92012-11-13 11:01:093434namespace {
3435
3436const char kExtraHeaderToRemove[] = "To-Be-Removed";
3437
3438class RedirectWithHeaderRemovalDelegate : public TestDelegate {
[email protected]46fadfd2013-02-06 09:40:163439 virtual void OnReceivedRedirect(net::URLRequest* request,
[email protected]251a1b92012-11-13 11:01:093440 const GURL& new_url,
[email protected]46fadfd2013-02-06 09:40:163441 bool* defer_redirect) OVERRIDE {
[email protected]251a1b92012-11-13 11:01:093442 TestDelegate::OnReceivedRedirect(request, new_url, defer_redirect);
3443 request->RemoveRequestHeaderByName(kExtraHeaderToRemove);
3444 }
3445};
3446
3447} // namespace
3448
3449TEST_F(URLRequestTestHTTP, RedirectWithHeaderRemovalTest) {
3450 ASSERT_TRUE(test_server_.Start());
3451
3452 GURL destination_url = test_server_.GetURL(
3453 "echoheader?" + std::string(kExtraHeaderToRemove));
3454 GURL original_url = test_server_.GetURL(
3455 "server-redirect?" + destination_url.spec());
3456 RedirectWithHeaderRemovalDelegate d;
3457 URLRequest req(original_url, &d, &default_context_);
3458 req.SetExtraRequestHeaderByName(kExtraHeaderToRemove, "dummy", false);
3459 req.Start();
3460 MessageLoop::current()->Run();
3461
3462 std::string value;
3463 const HttpRequestHeaders& headers = req.extra_request_headers();
3464 EXPECT_FALSE(headers.GetHeader(kExtraHeaderToRemove, &value));
3465 EXPECT_FALSE(req.is_pending());
3466 EXPECT_FALSE(req.is_redirecting());
3467 EXPECT_EQ("None", d.data_received());
3468}
3469
[email protected]316c1e5e2012-09-12 15:17:443470TEST_F(URLRequestTestHTTP, CancelTest) {
3471 TestDelegate d;
3472 {
3473 URLRequest r(GURL("http://www.google.com/"), &d, &default_context_);
3474
3475 r.Start();
3476 EXPECT_TRUE(r.is_pending());
3477
3478 r.Cancel();
3479
3480 MessageLoop::current()->Run();
3481
3482 // We expect to receive OnResponseStarted even though the request has been
3483 // cancelled.
3484 EXPECT_EQ(1, d.response_started_count());
3485 EXPECT_EQ(0, d.bytes_received());
3486 EXPECT_FALSE(d.received_data_before_response());
3487 }
3488}
3489
3490TEST_F(URLRequestTestHTTP, CancelTest2) {
3491 ASSERT_TRUE(test_server_.Start());
3492
3493 TestDelegate d;
3494 {
[email protected]007b3f82013-04-09 08:46:453495 URLRequest r(test_server_.GetURL(std::string()), &d, &default_context_);
[email protected]316c1e5e2012-09-12 15:17:443496
3497 d.set_cancel_in_response_started(true);
3498
3499 r.Start();
3500 EXPECT_TRUE(r.is_pending());
3501
3502 MessageLoop::current()->Run();
3503
3504 EXPECT_EQ(1, d.response_started_count());
3505 EXPECT_EQ(0, d.bytes_received());
3506 EXPECT_FALSE(d.received_data_before_response());
3507 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
3508 }
3509}
3510
3511TEST_F(URLRequestTestHTTP, CancelTest3) {
3512 ASSERT_TRUE(test_server_.Start());
3513
3514 TestDelegate d;
3515 {
[email protected]007b3f82013-04-09 08:46:453516 URLRequest r(test_server_.GetURL(std::string()), &d, &default_context_);
[email protected]316c1e5e2012-09-12 15:17:443517
3518 d.set_cancel_in_received_data(true);
3519
3520 r.Start();
3521 EXPECT_TRUE(r.is_pending());
3522
3523 MessageLoop::current()->Run();
3524
3525 EXPECT_EQ(1, d.response_started_count());
3526 // There is no guarantee about how much data was received
3527 // before the cancel was issued. It could have been 0 bytes,
3528 // or it could have been all the bytes.
3529 // EXPECT_EQ(0, d.bytes_received());
3530 EXPECT_FALSE(d.received_data_before_response());
3531 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
3532 }
3533}
3534
3535TEST_F(URLRequestTestHTTP, CancelTest4) {
3536 ASSERT_TRUE(test_server_.Start());
3537
3538 TestDelegate d;
3539 {
[email protected]007b3f82013-04-09 08:46:453540 URLRequest r(test_server_.GetURL(std::string()), &d, &default_context_);
[email protected]316c1e5e2012-09-12 15:17:443541
3542 r.Start();
3543 EXPECT_TRUE(r.is_pending());
3544
3545 // The request will be implicitly canceled when it is destroyed. The
3546 // test delegate must not post a quit message when this happens because
3547 // this test doesn't actually have a message loop. The quit message would
3548 // get put on this thread's message queue and the next test would exit
3549 // early, causing problems.
3550 d.set_quit_on_complete(false);
3551 }
3552 // expect things to just cleanup properly.
3553
3554 // we won't actually get a received reponse here because we've never run the
3555 // message loop
3556 EXPECT_FALSE(d.received_data_before_response());
3557 EXPECT_EQ(0, d.bytes_received());
3558}
3559
3560TEST_F(URLRequestTestHTTP, CancelTest5) {
3561 ASSERT_TRUE(test_server_.Start());
3562
3563 // populate cache
3564 {
3565 TestDelegate d;
3566 URLRequest r(test_server_.GetURL("cachetime"), &d, &default_context_);
3567 r.Start();
3568 MessageLoop::current()->Run();
3569 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3570 }
3571
3572 // cancel read from cache (see bug 990242)
3573 {
3574 TestDelegate d;
3575 URLRequest r(test_server_.GetURL("cachetime"), &d, &default_context_);
3576 r.Start();
3577 r.Cancel();
3578 MessageLoop::current()->Run();
3579
3580 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
3581 EXPECT_EQ(1, d.response_started_count());
3582 EXPECT_EQ(0, d.bytes_received());
3583 EXPECT_FALSE(d.received_data_before_response());
3584 }
3585}
3586
3587TEST_F(URLRequestTestHTTP, PostTest) {
3588 ASSERT_TRUE(test_server_.Start());
3589 HTTPUploadDataOperationTest("POST");
3590}
3591
3592TEST_F(URLRequestTestHTTP, PutTest) {
3593 ASSERT_TRUE(test_server_.Start());
3594 HTTPUploadDataOperationTest("PUT");
3595}
3596
3597TEST_F(URLRequestTestHTTP, PostEmptyTest) {
3598 ASSERT_TRUE(test_server_.Start());
3599
3600 TestDelegate d;
3601 {
3602 URLRequest r(test_server_.GetURL("echo"), &d, &default_context_);
3603 r.set_method("POST");
3604
3605 r.Start();
3606 EXPECT_TRUE(r.is_pending());
3607
3608 MessageLoop::current()->Run();
3609
[email protected]329b68b2012-11-14 17:54:273610 ASSERT_EQ(1, d.response_started_count())
3611 << "request failed: " << r.status().status()
3612 << ", error: " << r.status().error();
[email protected]316c1e5e2012-09-12 15:17:443613
3614 EXPECT_FALSE(d.received_data_before_response());
3615 EXPECT_TRUE(d.data_received().empty());
3616 }
3617}
3618
3619TEST_F(URLRequestTestHTTP, PostFileTest) {
3620 ASSERT_TRUE(test_server_.Start());
3621
3622 TestDelegate d;
3623 {
3624 URLRequest r(test_server_.GetURL("echo"), &d, &default_context_);
3625 r.set_method("POST");
3626
[email protected]6cdfd7f2013-02-08 20:40:153627 base::FilePath dir;
[email protected]316c1e5e2012-09-12 15:17:443628 PathService::Get(base::DIR_EXE, &dir);
3629 file_util::SetCurrentDirectory(dir);
3630
[email protected]f288ef02012-12-15 20:28:283631 ScopedVector<UploadElementReader> element_readers;
[email protected]316c1e5e2012-09-12 15:17:443632
[email protected]6cdfd7f2013-02-08 20:40:153633 base::FilePath path;
[email protected]316c1e5e2012-09-12 15:17:443634 PathService::Get(base::DIR_SOURCE_ROOT, &path);
3635 path = path.Append(FILE_PATH_LITERAL("net"));
3636 path = path.Append(FILE_PATH_LITERAL("data"));
3637 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
3638 path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
[email protected]f288ef02012-12-15 20:28:283639 element_readers.push_back(new UploadFileElementReader(
[email protected]671a7142013-01-10 14:08:073640 base::MessageLoopProxy::current(), path, 0, kuint64max, base::Time()));
[email protected]316c1e5e2012-09-12 15:17:443641
3642 // This file should just be ignored in the upload stream.
[email protected]f288ef02012-12-15 20:28:283643 element_readers.push_back(new UploadFileElementReader(
[email protected]671a7142013-01-10 14:08:073644 base::MessageLoopProxy::current(),
[email protected]6cdfd7f2013-02-08 20:40:153645 base::FilePath(FILE_PATH_LITERAL(
[email protected]316c1e5e2012-09-12 15:17:443646 "c:\\path\\to\\non\\existant\\file.randomness.12345")),
[email protected]f288ef02012-12-15 20:28:283647 0, kuint64max, base::Time()));
3648 r.set_upload(make_scoped_ptr(new UploadDataStream(&element_readers, 0)));
[email protected]316c1e5e2012-09-12 15:17:443649
3650 r.Start();
3651 EXPECT_TRUE(r.is_pending());
3652
3653 MessageLoop::current()->Run();
3654
[email protected]329b68b2012-11-14 17:54:273655 int64 size = 0;
3656 ASSERT_EQ(true, file_util::GetFileSize(path, &size));
[email protected]4356f0f2013-04-07 00:58:173657 scoped_ptr<char[]> buf(new char[size]);
[email protected]316c1e5e2012-09-12 15:17:443658
[email protected]329b68b2012-11-14 17:54:273659 ASSERT_EQ(size, file_util::ReadFile(path, buf.get(), size));
[email protected]316c1e5e2012-09-12 15:17:443660
[email protected]329b68b2012-11-14 17:54:273661 ASSERT_EQ(1, d.response_started_count())
3662 << "request failed: " << r.status().status()
3663 << ", error: " << r.status().error();
[email protected]316c1e5e2012-09-12 15:17:443664
3665 EXPECT_FALSE(d.received_data_before_response());
3666
[email protected]329b68b2012-11-14 17:54:273667 EXPECT_EQ(size, d.bytes_received());
3668 EXPECT_EQ(std::string(&buf[0], size), d.data_received());
[email protected]316c1e5e2012-09-12 15:17:443669 }
3670}
3671
3672TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) {
3673 ASSERT_TRUE(test_server_.Start());
3674
3675 TestDelegate d;
3676 {
3677 URLRequest r(test_server_.GetURL("echo"), &d, &default_context_);
3678 r.EnableChunkedUpload();
3679 r.set_method("POST");
3680 AddChunksToUpload(&r);
3681 r.Start();
3682 EXPECT_TRUE(r.is_pending());
3683
3684 MessageLoop::current()->Run();
3685
3686 VerifyReceivedDataMatchesChunks(&r, &d);
3687 }
3688}
3689
[email protected]329b68b2012-11-14 17:54:273690TEST_F(URLRequestTestHTTP, TestPostChunkedDataJustAfterStart) {
3691 ASSERT_TRUE(test_server_.Start());
3692
3693 TestDelegate d;
3694 {
3695 URLRequest r(test_server_.GetURL("echo"), &d, &default_context_);
3696 r.EnableChunkedUpload();
3697 r.set_method("POST");
3698 r.Start();
3699 EXPECT_TRUE(r.is_pending());
3700 AddChunksToUpload(&r);
3701 MessageLoop::current()->Run();
3702
3703 VerifyReceivedDataMatchesChunks(&r, &d);
3704 }
3705}
3706
[email protected]316c1e5e2012-09-12 15:17:443707TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) {
3708 ASSERT_TRUE(test_server_.Start());
3709
3710 TestDelegate d;
3711 {
3712 URLRequest r(test_server_.GetURL("echo"), &d, &default_context_);
3713 r.EnableChunkedUpload();
3714 r.set_method("POST");
3715 r.Start();
3716 EXPECT_TRUE(r.is_pending());
3717
[email protected]b4c62eb2012-11-14 18:36:513718 MessageLoop::current()->RunUntilIdle();
[email protected]316c1e5e2012-09-12 15:17:443719 AddChunksToUpload(&r);
3720 MessageLoop::current()->Run();
3721
3722 VerifyReceivedDataMatchesChunks(&r, &d);
3723 }
3724}
3725
3726TEST_F(URLRequestTestHTTP, ResponseHeadersTest) {
3727 ASSERT_TRUE(test_server_.Start());
3728
3729 TestDelegate d;
3730 URLRequest req(
3731 test_server_.GetURL("files/with-headers.html"), &d, &default_context_);
3732 req.Start();
3733 MessageLoop::current()->Run();
3734
3735 const HttpResponseHeaders* headers = req.response_headers();
3736
3737 // Simple sanity check that response_info() accesses the same data.
3738 EXPECT_EQ(headers, req.response_info().headers.get());
3739
3740 std::string header;
3741 EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header));
3742 EXPECT_EQ("private", header);
3743
3744 header.clear();
3745 EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header));
3746 EXPECT_EQ("text/html; charset=ISO-8859-1", header);
3747
3748 // The response has two "X-Multiple-Entries" headers.
3749 // This verfies our output has them concatenated together.
3750 header.clear();
3751 EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header));
3752 EXPECT_EQ("a, b", header);
3753}
3754
[email protected]242d8562012-10-30 21:20:463755TEST_F(URLRequestTestHTTP, ProcessSTS) {
3756 TestServer::SSLOptions ssl_options;
3757 TestServer https_test_server(
3758 TestServer::TYPE_HTTPS,
3759 ssl_options,
[email protected]6cdfd7f2013-02-08 20:40:153760 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
[email protected]242d8562012-10-30 21:20:463761 ASSERT_TRUE(https_test_server.Start());
3762
3763 TestDelegate d;
3764 URLRequest request(
3765 https_test_server.GetURL("files/hsts-headers.html"),
3766 &d,
3767 &default_context_);
3768 request.Start();
3769 MessageLoop::current()->Run();
3770
3771 TransportSecurityState* security_state =
3772 default_context_.transport_security_state();
3773 bool sni_available = true;
3774 TransportSecurityState::DomainState domain_state;
3775 EXPECT_TRUE(security_state->GetDomainState(
3776 TestServer::kLocalhost, sni_available, &domain_state));
3777 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
3778 domain_state.upgrade_mode);
3779 EXPECT_TRUE(domain_state.include_subdomains);
3780}
3781
3782TEST_F(URLRequestTestHTTP, ProcessSTSOnce) {
3783 TestServer::SSLOptions ssl_options;
3784 TestServer https_test_server(
3785 TestServer::TYPE_HTTPS,
3786 ssl_options,
[email protected]6cdfd7f2013-02-08 20:40:153787 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
[email protected]242d8562012-10-30 21:20:463788 ASSERT_TRUE(https_test_server.Start());
3789
3790 TestDelegate d;
3791 URLRequest request(
3792 https_test_server.GetURL("files/hsts-multiple-headers.html"),
3793 &d,
3794 &default_context_);
3795 request.Start();
3796 MessageLoop::current()->Run();
3797
3798 // We should have set parameters from the first header, not the second.
3799 TransportSecurityState* security_state =
3800 default_context_.transport_security_state();
3801 bool sni_available = true;
3802 TransportSecurityState::DomainState domain_state;
3803 EXPECT_TRUE(security_state->GetDomainState(
3804 TestServer::kLocalhost, sni_available, &domain_state));
3805 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
3806 domain_state.upgrade_mode);
3807 EXPECT_FALSE(domain_state.include_subdomains);
3808}
3809
[email protected]9f972ec2013-04-10 20:24:363810TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP) {
3811 TestServer::SSLOptions ssl_options;
3812 TestServer https_test_server(
3813 TestServer::TYPE_HTTPS,
3814 ssl_options,
3815 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
3816 ASSERT_TRUE(https_test_server.Start());
3817
3818 TestDelegate d;
3819 URLRequest request(
3820 https_test_server.GetURL("files/hsts-and-hpkp-headers.html"),
3821 &d,
3822 &default_context_);
3823 request.Start();
3824 MessageLoop::current()->Run();
3825
3826 // We should have set parameters from the first header, not the second.
3827 TransportSecurityState* security_state =
3828 default_context_.transport_security_state();
3829 bool sni_available = true;
3830 TransportSecurityState::DomainState domain_state;
3831 EXPECT_TRUE(security_state->GetDomainState(
3832 TestServer::kLocalhost, sni_available, &domain_state));
3833 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
3834 domain_state.upgrade_mode);
3835#if defined(OS_ANDROID)
3836 // Android's CertVerifyProc does not (yet) handle pins.
3837#else
3838 EXPECT_TRUE(domain_state.HasPublicKeyPins());
3839#endif
3840 EXPECT_NE(domain_state.upgrade_expiry,
3841 domain_state.dynamic_spki_hashes_expiry);
3842
3843 // TODO(palmer): In the (near) future, TransportSecurityState will have a
3844 // storage model allowing us to have independent values for
3845 // include_subdomains. At that time, extend this test.
3846 //EXPECT_FALSE(domain_state.include_subdomains);
3847}
3848
[email protected]316c1e5e2012-09-12 15:17:443849TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) {
3850 ASSERT_TRUE(test_server_.Start());
3851
3852 TestDelegate d;
3853 URLRequest req(test_server_.GetURL(
3854 "files/content-type-normalization.html"), &d, &default_context_);
3855 req.Start();
3856 MessageLoop::current()->Run();
3857
3858 std::string mime_type;
3859 req.GetMimeType(&mime_type);
3860 EXPECT_EQ("text/html", mime_type);
3861
3862 std::string charset;
3863 req.GetCharset(&charset);
3864 EXPECT_EQ("utf-8", charset);
3865 req.Cancel();
3866}
3867
3868TEST_F(URLRequestTestHTTP, RestrictRedirects) {
3869 ASSERT_TRUE(test_server_.Start());
3870
3871 TestDelegate d;
3872 URLRequest req(test_server_.GetURL(
3873 "files/redirect-to-file.html"), &d, &default_context_);
3874 req.Start();
3875 MessageLoop::current()->Run();
3876
3877 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
3878 EXPECT_EQ(ERR_UNSAFE_REDIRECT, req.status().error());
3879}
3880
3881TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) {
3882 ASSERT_TRUE(test_server_.Start());
3883
3884 TestDelegate d;
3885 URLRequest req(test_server_.GetURL(
3886 "files/redirect-to-invalid-url.html"), &d, &default_context_);
3887 req.Start();
3888 MessageLoop::current()->Run();
3889
3890 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
3891 EXPECT_EQ(ERR_INVALID_URL, req.status().error());
3892}
3893
3894TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) {
3895 ASSERT_TRUE(test_server_.Start());
3896
3897 TestDelegate d;
3898 URLRequest req(
3899 test_server_.GetURL("echoheader?Referer"), &d, &default_context_);
[email protected]99ecf6e2013-04-10 22:46:133900 req.SetReferrer("http://user:[email protected]/");
[email protected]316c1e5e2012-09-12 15:17:443901 req.Start();
3902 MessageLoop::current()->Run();
3903
3904 EXPECT_EQ(std::string("http://foo.com/"), d.data_received());
3905}
3906
[email protected]99ecf6e2013-04-10 22:46:133907TEST_F(URLRequestTestHTTP, NoFragmentInReferrer) {
3908 ASSERT_TRUE(test_server_.Start());
3909
3910 TestDelegate d;
3911 URLRequest req(
3912 test_server_.GetURL("echoheader?Referer"), &d, &default_context_);
3913 req.SetReferrer("http://foo.com/test#fragment");
3914 req.Start();
3915 MessageLoop::current()->Run();
3916
3917 EXPECT_EQ(std::string("http://foo.com/test"), d.data_received());
3918}
3919
3920TEST_F(URLRequestTestHTTP, EmptyReferrerAfterValidReferrer) {
3921 ASSERT_TRUE(test_server_.Start());
3922
3923 TestDelegate d;
3924 URLRequest req(
3925 test_server_.GetURL("echoheader?Referer"), &d, &default_context_);
3926 req.SetReferrer("http://foo.com/test#fragment");
3927 req.SetReferrer("");
3928 req.Start();
3929 MessageLoop::current()->Run();
3930
3931 EXPECT_EQ(std::string("None"), d.data_received());
3932}
3933
[email protected]316c1e5e2012-09-12 15:17:443934TEST_F(URLRequestTestHTTP, CancelRedirect) {
3935 ASSERT_TRUE(test_server_.Start());
3936
3937 TestDelegate d;
3938 {
3939 d.set_cancel_in_received_redirect(true);
3940 URLRequest req(
3941 test_server_.GetURL("files/redirect-test.html"), &d, &default_context_);
3942 req.Start();
3943 MessageLoop::current()->Run();
3944
3945 EXPECT_EQ(1, d.response_started_count());
3946 EXPECT_EQ(0, d.bytes_received());
3947 EXPECT_FALSE(d.received_data_before_response());
3948 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
3949 }
3950}
3951
3952TEST_F(URLRequestTestHTTP, DeferredRedirect) {
3953 ASSERT_TRUE(test_server_.Start());
3954
3955 TestDelegate d;
3956 {
3957 d.set_quit_on_redirect(true);
3958 URLRequest req(
3959 test_server_.GetURL("files/redirect-test.html"), &d, &default_context_);
3960 req.Start();
3961 MessageLoop::current()->Run();
3962
3963 EXPECT_EQ(1, d.received_redirect_count());
3964
3965 req.FollowDeferredRedirect();
3966 MessageLoop::current()->Run();
3967
3968 EXPECT_EQ(1, d.response_started_count());
3969 EXPECT_FALSE(d.received_data_before_response());
3970 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
3971
[email protected]6cdfd7f2013-02-08 20:40:153972 base::FilePath path;
[email protected]316c1e5e2012-09-12 15:17:443973 PathService::Get(base::DIR_SOURCE_ROOT, &path);
3974 path = path.Append(FILE_PATH_LITERAL("net"));
3975 path = path.Append(FILE_PATH_LITERAL("data"));
3976 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
3977 path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
3978
3979 std::string contents;
3980 EXPECT_TRUE(file_util::ReadFileToString(path, &contents));
3981 EXPECT_EQ(contents, d.data_received());
3982 }
3983}
3984
3985TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) {
3986 ASSERT_TRUE(test_server_.Start());
3987
3988 TestDelegate d;
3989 {
3990 d.set_quit_on_redirect(true);
3991 URLRequest req(
3992 test_server_.GetURL("files/redirect-test.html"), &d, &default_context_);
3993 req.Start();
3994 MessageLoop::current()->Run();
3995
3996 EXPECT_EQ(1, d.received_redirect_count());
3997
3998 req.Cancel();
3999 MessageLoop::current()->Run();
4000
4001 EXPECT_EQ(1, d.response_started_count());
4002 EXPECT_EQ(0, d.bytes_received());
4003 EXPECT_FALSE(d.received_data_before_response());
4004 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
4005 }
4006}
4007
4008TEST_F(URLRequestTestHTTP, VaryHeader) {
4009 ASSERT_TRUE(test_server_.Start());
4010
4011 // populate the cache
4012 {
4013 TestDelegate d;
4014 URLRequest req(
4015 test_server_.GetURL("echoheadercache?foo"), &d, &default_context_);
4016 HttpRequestHeaders headers;
4017 headers.SetHeader("foo", "1");
4018 req.SetExtraRequestHeaders(headers);
4019 req.Start();
4020 MessageLoop::current()->Run();
4021 }
4022
4023 // expect a cache hit
4024 {
4025 TestDelegate d;
4026 URLRequest req(
4027 test_server_.GetURL("echoheadercache?foo"), &d, &default_context_);
4028 HttpRequestHeaders headers;
4029 headers.SetHeader("foo", "1");
4030 req.SetExtraRequestHeaders(headers);
4031 req.Start();
4032 MessageLoop::current()->Run();
4033
4034 EXPECT_TRUE(req.was_cached());
4035 }
4036
4037 // expect a cache miss
4038 {
4039 TestDelegate d;
4040 URLRequest req(
4041 test_server_.GetURL("echoheadercache?foo"), &d, &default_context_);
4042 HttpRequestHeaders headers;
4043 headers.SetHeader("foo", "2");
4044 req.SetExtraRequestHeaders(headers);
4045 req.Start();
4046 MessageLoop::current()->Run();
4047
4048 EXPECT_FALSE(req.was_cached());
4049 }
4050}
4051
4052TEST_F(URLRequestTestHTTP, BasicAuth) {
4053 ASSERT_TRUE(test_server_.Start());
4054
4055 // populate the cache
4056 {
4057 TestDelegate d;
4058 d.set_credentials(AuthCredentials(kUser, kSecret));
4059
4060 URLRequest r(test_server_.GetURL("auth-basic"), &d, &default_context_);
4061 r.Start();
4062
4063 MessageLoop::current()->Run();
4064
4065 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
4066 }
4067
4068 // repeat request with end-to-end validation. since auth-basic results in a
4069 // cachable page, we expect this test to result in a 304. in which case, the
4070 // response should be fetched from the cache.
4071 {
4072 TestDelegate d;
4073 d.set_credentials(AuthCredentials(kUser, kSecret));
4074
4075 URLRequest r(test_server_.GetURL("auth-basic"), &d, &default_context_);
4076 r.set_load_flags(LOAD_VALIDATE_CACHE);
4077 r.Start();
4078
4079 MessageLoop::current()->Run();
4080
4081 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
4082
4083 // Should be the same cached document.
4084 EXPECT_TRUE(r.was_cached());
4085 }
4086}
4087
4088// Check that Set-Cookie headers in 401 responses are respected.
4089// http://crbug.com/6450
4090TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) {
4091 ASSERT_TRUE(test_server_.Start());
4092
4093 GURL url_requiring_auth =
4094 test_server_.GetURL("auth-basic?set-cookie-if-challenged");
4095
4096 // Request a page that will give a 401 containing a Set-Cookie header.
4097 // Verify that when the transaction is restarted, it includes the new cookie.
4098 {
[email protected]ceefd7fd2012-11-29 00:36:244099 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]316c1e5e2012-09-12 15:17:444100 TestURLRequestContext context(true);
4101 context.set_network_delegate(&network_delegate);
4102 context.Init();
4103
4104 TestDelegate d;
4105 d.set_credentials(AuthCredentials(kUser, kSecret));
4106
4107 URLRequest r(url_requiring_auth, &d, &context);
4108 r.Start();
4109
4110 MessageLoop::current()->Run();
4111
4112 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
4113
4114 // Make sure we sent the cookie in the restarted transaction.
4115 EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true")
4116 != std::string::npos);
4117 }
4118
4119 // Same test as above, except this time the restart is initiated earlier
4120 // (without user intervention since identity is embedded in the URL).
4121 {
[email protected]ceefd7fd2012-11-29 00:36:244122 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]316c1e5e2012-09-12 15:17:444123 TestURLRequestContext context(true);
4124 context.set_network_delegate(&network_delegate);
4125 context.Init();
4126
4127 TestDelegate d;
4128
4129 GURL::Replacements replacements;
4130 std::string username("user2");
4131 std::string password("secret");
4132 replacements.SetUsernameStr(username);
4133 replacements.SetPasswordStr(password);
4134 GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements);
4135
4136 URLRequest r(url_with_identity, &d, &context);
4137 r.Start();
4138
4139 MessageLoop::current()->Run();
4140
4141 EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos);
4142
4143 // Make sure we sent the cookie in the restarted transaction.
4144 EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true")
4145 != std::string::npos);
4146 }
4147}
4148
[email protected]58e32bb2013-01-21 18:23:254149// Tests that load timing works as expected with auth and the cache.
4150TEST_F(URLRequestTestHTTP, BasicAuthLoadTiming) {
4151 ASSERT_TRUE(test_server_.Start());
4152
4153 // populate the cache
4154 {
4155 TestDelegate d;
4156 d.set_credentials(AuthCredentials(kUser, kSecret));
4157
4158 URLRequest r(test_server_.GetURL("auth-basic"), &d, &default_context_);
4159 r.Start();
4160
4161 MessageLoop::current()->Run();
4162
4163 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
4164
4165 LoadTimingInfo load_timing_info_before_auth;
4166 EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeAuth(
4167 &load_timing_info_before_auth));
4168 TestLoadTimingNotReused(load_timing_info_before_auth,
4169 CONNECT_TIMING_HAS_DNS_TIMES);
4170
4171 LoadTimingInfo load_timing_info;
[email protected]0a703912013-02-02 04:25:174172 r.GetLoadTimingInfo(&load_timing_info);
[email protected]58e32bb2013-01-21 18:23:254173 // The test server does not support keep alive sockets, so the second
4174 // request with auth should use a new socket.
4175 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
4176 EXPECT_NE(load_timing_info_before_auth.socket_log_id,
4177 load_timing_info.socket_log_id);
4178 EXPECT_LE(load_timing_info_before_auth.receive_headers_end,
4179 load_timing_info.connect_timing.connect_start);
4180 }
4181
4182 // repeat request with end-to-end validation. since auth-basic results in a
4183 // cachable page, we expect this test to result in a 304. in which case, the
4184 // response should be fetched from the cache.
4185 {
4186 TestDelegate d;
4187 d.set_credentials(AuthCredentials(kUser, kSecret));
4188
4189 URLRequest r(test_server_.GetURL("auth-basic"), &d, &default_context_);
4190 r.set_load_flags(LOAD_VALIDATE_CACHE);
4191 r.Start();
4192
4193 MessageLoop::current()->Run();
4194
4195 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
4196
4197 // Should be the same cached document.
4198 EXPECT_TRUE(r.was_cached());
4199
4200 LoadTimingInfo load_timing_info;
[email protected]0a703912013-02-02 04:25:174201 r.GetLoadTimingInfo(&load_timing_info);
[email protected]58e32bb2013-01-21 18:23:254202 TestLoadTimingNoHttpConnection(load_timing_info);
4203 }
4204}
4205
[email protected]316c1e5e2012-09-12 15:17:444206// In this test, we do a POST which the server will 302 redirect.
4207// The subsequent transaction should use GET, and should not send the
4208// Content-Type header.
4209// http://code.google.com/p/chromium/issues/detail?id=843
4210TEST_F(URLRequestTestHTTP, Post302RedirectGet) {
4211 ASSERT_TRUE(test_server_.Start());
4212
4213 const char kData[] = "hello world";
4214
4215 TestDelegate d;
4216 URLRequest req(
4217 test_server_.GetURL("files/redirect-to-echoall"), &d, &default_context_);
4218 req.set_method("POST");
[email protected]f288ef02012-12-15 20:28:284219 req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
[email protected]316c1e5e2012-09-12 15:17:444220
4221 // Set headers (some of which are specific to the POST).
4222 HttpRequestHeaders headers;
4223 headers.AddHeadersFromString(
4224 "Content-Type: multipart/form-data; "
4225 "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n"
4226 "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,"
4227 "text/plain;q=0.8,image/png,*/*;q=0.5\r\n"
4228 "Accept-Language: en-US,en\r\n"
4229 "Accept-Charset: ISO-8859-1,*,utf-8\r\n"
4230 "Content-Length: 11\r\n"
4231 "Origin: http://localhost:1337/");
4232 req.SetExtraRequestHeaders(headers);
4233 req.Start();
4234 MessageLoop::current()->Run();
4235
4236 std::string mime_type;
4237 req.GetMimeType(&mime_type);
4238 EXPECT_EQ("text/html", mime_type);
4239
4240 const std::string& data = d.data_received();
4241
4242 // Check that the post-specific headers were stripped:
4243 EXPECT_FALSE(ContainsString(data, "Content-Length:"));
4244 EXPECT_FALSE(ContainsString(data, "Content-Type:"));
4245 EXPECT_FALSE(ContainsString(data, "Origin:"));
4246
4247 // These extra request headers should not have been stripped.
4248 EXPECT_TRUE(ContainsString(data, "Accept:"));
4249 EXPECT_TRUE(ContainsString(data, "Accept-Language:"));
4250 EXPECT_TRUE(ContainsString(data, "Accept-Charset:"));
4251}
4252
4253// The following tests check that we handle mutating the request method for
4254// HTTP redirects as expected.
4255// See http://crbug.com/56373 and http://crbug.com/102130.
4256
4257TEST_F(URLRequestTestHTTP, Redirect301Tests) {
4258 ASSERT_TRUE(test_server_.Start());
4259
4260 const GURL url = test_server_.GetURL("files/redirect301-to-echo");
4261
4262 HTTPRedirectMethodTest(url, "POST", "GET", true);
4263 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
4264 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
4265}
4266
4267TEST_F(URLRequestTestHTTP, Redirect302Tests) {
4268 ASSERT_TRUE(test_server_.Start());
4269
4270 const GURL url = test_server_.GetURL("files/redirect302-to-echo");
4271
4272 HTTPRedirectMethodTest(url, "POST", "GET", true);
4273 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
4274 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
4275}
4276
4277TEST_F(URLRequestTestHTTP, Redirect303Tests) {
4278 ASSERT_TRUE(test_server_.Start());
4279
4280 const GURL url = test_server_.GetURL("files/redirect303-to-echo");
4281
4282 HTTPRedirectMethodTest(url, "POST", "GET", true);
4283 HTTPRedirectMethodTest(url, "PUT", "GET", true);
4284 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
4285}
4286
4287TEST_F(URLRequestTestHTTP, Redirect307Tests) {
4288 ASSERT_TRUE(test_server_.Start());
4289
4290 const GURL url = test_server_.GetURL("files/redirect307-to-echo");
4291
4292 HTTPRedirectMethodTest(url, "POST", "POST", true);
4293 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
4294 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
4295}
4296
4297TEST_F(URLRequestTestHTTP, InterceptPost302RedirectGet) {
4298 ASSERT_TRUE(test_server_.Start());
4299
4300 const char kData[] = "hello world";
4301
4302 TestDelegate d;
4303 URLRequest req(test_server_.GetURL("empty.html"), &d, &default_context_);
4304 req.set_method("POST");
[email protected]f288ef02012-12-15 20:28:284305 req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
[email protected]316c1e5e2012-09-12 15:17:444306 HttpRequestHeaders headers;
4307 headers.SetHeader(HttpRequestHeaders::kContentLength,
4308 base::UintToString(arraysize(kData) - 1));
4309 req.SetExtraRequestHeaders(headers);
4310
4311 URLRequestRedirectJob* job = new URLRequestRedirectJob(
[email protected]cc05edc2013-03-08 18:04:414312 &req, &default_network_delegate_, test_server_.GetURL("echo"),
[email protected]8ccc69f2012-11-28 19:52:144313 URLRequestRedirectJob::REDIRECT_302_FOUND);
[email protected]316c1e5e2012-09-12 15:17:444314 AddTestInterceptor()->set_main_intercept_job(job);
4315
4316 req.Start();
4317 MessageLoop::current()->Run();
4318 EXPECT_EQ("GET", req.method());
4319}
4320
4321TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) {
4322 ASSERT_TRUE(test_server_.Start());
4323
4324 const char kData[] = "hello world";
4325
4326 TestDelegate d;
4327 URLRequest req(test_server_.GetURL("empty.html"), &d, &default_context_);
4328 req.set_method("POST");
[email protected]f288ef02012-12-15 20:28:284329 req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
[email protected]316c1e5e2012-09-12 15:17:444330 HttpRequestHeaders headers;
4331 headers.SetHeader(HttpRequestHeaders::kContentLength,
4332 base::UintToString(arraysize(kData) - 1));
4333 req.SetExtraRequestHeaders(headers);
4334
4335 URLRequestRedirectJob* job = new URLRequestRedirectJob(
[email protected]cc05edc2013-03-08 18:04:414336 &req, &default_network_delegate_, test_server_.GetURL("echo"),
[email protected]316c1e5e2012-09-12 15:17:444337 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT);
4338 AddTestInterceptor()->set_main_intercept_job(job);
4339
4340 req.Start();
4341 MessageLoop::current()->Run();
4342 EXPECT_EQ("POST", req.method());
4343 EXPECT_EQ(kData, d.data_received());
4344}
4345
4346// Check that default A-L header is sent.
4347TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) {
4348 ASSERT_TRUE(test_server_.Start());
4349
[email protected]84f05432013-03-15 01:00:124350 StaticHttpUserAgentSettings settings("en", EmptyString());
[email protected]ceefd7fd2012-11-29 00:36:244351 TestNetworkDelegate network_delegate; // Must outlive URLRequests.
[email protected]316c1e5e2012-09-12 15:17:444352 TestURLRequestContext context(true);
4353 context.set_network_delegate(&network_delegate);
[email protected]ee4c30d2012-11-07 15:08:434354 context.set_http_user_agent_settings(&settings);
[email protected]316c1e5e2012-09-12 15:17:444355 context.Init();
4356
4357 TestDelegate d;
4358 URLRequest req(
4359 test_server_.GetURL("echoheader?Accept-Language"), &d, &context);
4360 req.Start();
4361 MessageLoop::current()->Run();
4362 EXPECT_EQ("en", d.data_received());
4363}
4364
4365// Check that an empty A-L header is not sent. http://crbug.com/77365.
4366TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) {
4367 ASSERT_TRUE(test_server_.Start());
4368
[email protected]84f05432013-03-15 01:00:124369 StaticHttpUserAgentSettings settings(EmptyString(), EmptyString());
[email protected]ceefd7fd2012-11-29 00:36:244370 TestNetworkDelegate network_delegate; // Must outlive URLRequests.
[email protected]316c1e5e2012-09-12 15:17:444371 TestURLRequestContext context(true);
4372 context.set_network_delegate(&network_delegate);
4373 context.Init();
4374 // We override the language after initialization because empty entries
4375 // get overridden by Init().
[email protected]ee4c30d2012-11-07 15:08:434376 context.set_http_user_agent_settings(&settings);
[email protected]316c1e5e2012-09-12 15:17:444377
4378 TestDelegate d;
4379 URLRequest req(
4380 test_server_.GetURL("echoheader?Accept-Language"), &d, &context);
4381 req.Start();
4382 MessageLoop::current()->Run();
4383 EXPECT_EQ("None", d.data_received());
4384}
4385
4386// Check that if request overrides the A-L header, the default is not appended.
4387// See http://crbug.com/20894
4388TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) {
4389 ASSERT_TRUE(test_server_.Start());
4390
4391 TestDelegate d;
4392 URLRequest req(test_server_.GetURL("echoheader?Accept-Language"),
4393 &d,
4394 &default_context_);
4395 HttpRequestHeaders headers;
4396 headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru");
4397 req.SetExtraRequestHeaders(headers);
4398 req.Start();
4399 MessageLoop::current()->Run();
4400 EXPECT_EQ(std::string("ru"), d.data_received());
4401}
4402
4403// Check that default A-E header is sent.
4404TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) {
4405 ASSERT_TRUE(test_server_.Start());
4406
4407 TestDelegate d;
4408 URLRequest req(test_server_.GetURL("echoheader?Accept-Encoding"),
4409 &d,
4410 &default_context_);
4411 HttpRequestHeaders headers;
4412 req.SetExtraRequestHeaders(headers);
4413 req.Start();
4414 MessageLoop::current()->Run();
4415 EXPECT_TRUE(ContainsString(d.data_received(), "gzip"));
4416}
4417
4418// Check that if request overrides the A-E header, the default is not appended.
4419// See http://crbug.com/47381
4420TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) {
4421 ASSERT_TRUE(test_server_.Start());
4422
4423 TestDelegate d;
4424 URLRequest req(test_server_.GetURL("echoheader?Accept-Encoding"),
4425 &d,
4426 &default_context_);
4427 HttpRequestHeaders headers;
4428 headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, "identity");
4429 req.SetExtraRequestHeaders(headers);
4430 req.Start();
4431 MessageLoop::current()->Run();
4432 EXPECT_FALSE(ContainsString(d.data_received(), "gzip"));
4433 EXPECT_TRUE(ContainsString(d.data_received(), "identity"));
4434}
4435
[email protected]84f05432013-03-15 01:00:124436// Check that setting the A-C header sends the proper header.
4437TEST_F(URLRequestTestHTTP, SetAcceptCharset) {
[email protected]316c1e5e2012-09-12 15:17:444438 ASSERT_TRUE(test_server_.Start());
4439
4440 TestDelegate d;
4441 URLRequest req(test_server_.GetURL("echoheader?Accept-Charset"),
4442 &d,
4443 &default_context_);
4444 HttpRequestHeaders headers;
4445 headers.SetHeader(HttpRequestHeaders::kAcceptCharset, "koi-8r");
4446 req.SetExtraRequestHeaders(headers);
4447 req.Start();
4448 MessageLoop::current()->Run();
4449 EXPECT_EQ(std::string("koi-8r"), d.data_received());
4450}
4451
4452// Check that default User-Agent header is sent.
4453TEST_F(URLRequestTestHTTP, DefaultUserAgent) {
4454 ASSERT_TRUE(test_server_.Start());
4455
4456 TestDelegate d;
4457 URLRequest req(test_server_.GetURL("echoheader?User-Agent"),
4458 &d,
4459 &default_context_);
4460 req.Start();
4461 MessageLoop::current()->Run();
4462 EXPECT_EQ(req.context()->GetUserAgent(req.url()), d.data_received());
4463}
4464
4465// Check that if request overrides the User-Agent header,
4466// the default is not appended.
4467TEST_F(URLRequestTestHTTP, OverrideUserAgent) {
4468 ASSERT_TRUE(test_server_.Start());
4469
4470 TestDelegate d;
4471 URLRequest req(test_server_.GetURL("echoheader?User-Agent"),
4472 &d,
4473 &default_context_);
4474 HttpRequestHeaders headers;
4475 headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)");
4476 req.SetExtraRequestHeaders(headers);
4477 req.Start();
4478 MessageLoop::current()->Run();
4479 // If the net tests are being run with ChromeFrame then we need to allow for
4480 // the 'chromeframe' suffix which is added to the user agent before the
4481 // closing parentheses.
4482 EXPECT_TRUE(StartsWithASCII(d.data_received(), "Lynx (textmode", true));
4483}
4484
[email protected]ee4c30d2012-11-07 15:08:434485// Check that a NULL HttpUserAgentSettings causes the corresponding empty
4486// User-Agent header to be sent but does not send the Accept-Language and
4487// Accept-Charset headers.
4488TEST_F(URLRequestTestHTTP, EmptyHttpUserAgentSettings) {
4489 ASSERT_TRUE(test_server_.Start());
4490
[email protected]ceefd7fd2012-11-29 00:36:244491 TestNetworkDelegate network_delegate; // Must outlive URLRequests.
[email protected]ee4c30d2012-11-07 15:08:434492 TestURLRequestContext context(true);
4493 context.set_network_delegate(&network_delegate);
4494 context.Init();
4495 // We override the HttpUserAgentSettings after initialization because empty
4496 // entries get overridden by Init().
4497 context.set_http_user_agent_settings(NULL);
4498
4499 struct {
4500 const char* request;
4501 const char* expected_response;
4502 } tests[] = { { "echoheader?Accept-Language", "None" },
4503 { "echoheader?Accept-Charset", "None" },
4504 { "echoheader?User-Agent", "" } };
4505
4506 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); i++) {
4507 TestDelegate d;
4508 URLRequest req(test_server_.GetURL(tests[i].request), &d, &context);
4509 req.Start();
4510 MessageLoop::current()->Run();
4511 EXPECT_EQ(tests[i].expected_response, d.data_received())
4512 << " Request = \"" << tests[i].request << "\"";
4513 }
4514}
4515
[email protected]5033ab82013-03-22 20:17:464516// Make sure that URLRequest passes on its priority updates to
4517// newly-created jobs after the first one.
4518TEST_F(URLRequestTestHTTP, SetSubsequentJobPriority) {
4519 ASSERT_TRUE(test_server_.Start());
4520
4521 TestDelegate d;
4522 URLRequest req(test_server_.GetURL("empty.html"), &d, &default_context_);
4523 EXPECT_EQ(DEFAULT_PRIORITY, req.priority());
4524
4525 scoped_refptr<URLRequestRedirectJob> redirect_job =
4526 new URLRequestRedirectJob(
4527 &req, &default_network_delegate_, test_server_.GetURL("echo"),
4528 URLRequestRedirectJob::REDIRECT_302_FOUND);
4529 AddTestInterceptor()->set_main_intercept_job(redirect_job);
4530
4531 req.SetPriority(LOW);
4532 req.Start();
4533 EXPECT_TRUE(req.is_pending());
4534
4535 scoped_refptr<URLRequestTestJob> job =
4536 new URLRequestTestJob(&req, &default_network_delegate_);
4537 AddTestInterceptor()->set_main_intercept_job(job);
4538
4539 // Should trigger |job| to be started.
4540 MessageLoop::current()->Run();
4541 EXPECT_EQ(LOW, job->priority());
4542}
4543
[email protected]73e0bba2009-02-19 22:57:094544class HTTPSRequestTest : public testing::Test {
[email protected]87a09a92011-07-14 15:50:504545 public:
[email protected]ef2bf422012-05-11 03:27:094546 HTTPSRequestTest() : default_context_(true) {
4547 default_context_.set_network_delegate(&default_network_delegate_);
4548 default_context_.Init();
[email protected]87a09a92011-07-14 15:50:504549 }
4550 virtual ~HTTPSRequestTest() {}
4551
4552 protected:
[email protected]ceefd7fd2012-11-29 00:36:244553 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest.
[email protected]ef2bf422012-05-11 03:27:094554 TestURLRequestContext default_context_;
[email protected]ea224582008-12-07 20:25:464555};
4556
[email protected]c044616e2013-02-20 02:01:264557TEST_F(HTTPSRequestTest, HTTPSGetTest) {
[email protected]7461a402011-03-24 23:19:514558 TestServer test_server(TestServer::TYPE_HTTPS,
[email protected]d9fca99a2012-02-24 16:16:204559 TestServer::kLocalhost,
[email protected]6cdfd7f2013-02-08 20:40:154560 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]95409e12010-08-17 20:07:114561 ASSERT_TRUE(test_server.Start());
[email protected]ea224582008-12-07 20:25:464562
[email protected]ea224582008-12-07 20:25:464563 TestDelegate d;
4564 {
[email protected]007b3f82013-04-09 08:46:454565 URLRequest r(test_server.GetURL(std::string()), &d, &default_context_);
[email protected]ea224582008-12-07 20:25:464566 r.Start();
4567 EXPECT_TRUE(r.is_pending());
4568
4569 MessageLoop::current()->Run();
4570
4571 EXPECT_EQ(1, d.response_started_count());
4572 EXPECT_FALSE(d.received_data_before_response());
4573 EXPECT_NE(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:174574 CheckSSLInfo(r.ssl_info());
[email protected]6d81b482011-02-22 19:47:194575 EXPECT_EQ(test_server.host_port_pair().host(),
4576 r.GetSocketAddress().host());
4577 EXPECT_EQ(test_server.host_port_pair().port(),
4578 r.GetSocketAddress().port());
[email protected]ea224582008-12-07 20:25:464579 }
[email protected]ea224582008-12-07 20:25:464580}
4581
[email protected]5774ada2010-07-15 06:30:544582TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) {
[email protected]295e5cd2012-08-23 01:05:124583 TestServer::SSLOptions ssl_options(
4584 TestServer::SSLOptions::CERT_MISMATCHED_NAME);
4585 TestServer test_server(TestServer::TYPE_HTTPS,
4586 ssl_options,
[email protected]6cdfd7f2013-02-08 20:40:154587 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]95409e12010-08-17 20:07:114588 ASSERT_TRUE(test_server.Start());
[email protected]bacff652009-03-31 17:50:334589
4590 bool err_allowed = true;
4591 for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) {
4592 TestDelegate d;
4593 {
4594 d.set_allow_certificate_errors(err_allowed);
[email protected]007b3f82013-04-09 08:46:454595 URLRequest r(test_server.GetURL(std::string()), &d, &default_context_);
[email protected]bacff652009-03-31 17:50:334596
4597 r.Start();
4598 EXPECT_TRUE(r.is_pending());
4599
4600 MessageLoop::current()->Run();
4601
4602 EXPECT_EQ(1, d.response_started_count());
4603 EXPECT_FALSE(d.received_data_before_response());
4604 EXPECT_TRUE(d.have_certificate_errors());
[email protected]96adadb2010-08-28 01:16:174605 if (err_allowed) {
[email protected]bacff652009-03-31 17:50:334606 EXPECT_NE(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:174607 CheckSSLInfo(r.ssl_info());
4608 } else {
[email protected]bacff652009-03-31 17:50:334609 EXPECT_EQ(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:174610 }
[email protected]bacff652009-03-31 17:50:334611 }
4612 }
4613}
4614
[email protected]5774ada2010-07-15 06:30:544615TEST_F(HTTPSRequestTest, HTTPSExpiredTest) {
[email protected]295e5cd2012-08-23 01:05:124616 TestServer::SSLOptions ssl_options(
4617 TestServer::SSLOptions::CERT_EXPIRED);
4618 TestServer test_server(TestServer::TYPE_HTTPS,
4619 ssl_options,
[email protected]6cdfd7f2013-02-08 20:40:154620 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]95409e12010-08-17 20:07:114621 ASSERT_TRUE(test_server.Start());
[email protected]bacff652009-03-31 17:50:334622
4623 // Iterate from false to true, just so that we do the opposite of the
4624 // previous test in order to increase test coverage.
4625 bool err_allowed = false;
4626 for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) {
4627 TestDelegate d;
4628 {
4629 d.set_allow_certificate_errors(err_allowed);
[email protected]007b3f82013-04-09 08:46:454630 URLRequest r(test_server.GetURL(std::string()), &d, &default_context_);
[email protected]bacff652009-03-31 17:50:334631
4632 r.Start();
4633 EXPECT_TRUE(r.is_pending());
4634
4635 MessageLoop::current()->Run();
4636
4637 EXPECT_EQ(1, d.response_started_count());
4638 EXPECT_FALSE(d.received_data_before_response());
4639 EXPECT_TRUE(d.have_certificate_errors());
[email protected]96adadb2010-08-28 01:16:174640 if (err_allowed) {
[email protected]bacff652009-03-31 17:50:334641 EXPECT_NE(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:174642 CheckSSLInfo(r.ssl_info());
4643 } else {
[email protected]bacff652009-03-31 17:50:334644 EXPECT_EQ(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:174645 }
[email protected]bacff652009-03-31 17:50:334646 }
4647 }
4648}
[email protected]73e0bba2009-02-19 22:57:094649
[email protected]316c1e5e2012-09-12 15:17:444650// Tests TLSv1.1 -> TLSv1 fallback. Verifies that we don't fall back more
4651// than necessary.
4652TEST_F(HTTPSRequestTest, TLSv1Fallback) {
4653 uint16 default_version_max = SSLConfigService::default_version_max();
4654 // The OpenSSL library in use may not support TLS 1.1.
4655#if !defined(USE_OPENSSL)
4656 EXPECT_GT(default_version_max, SSL_PROTOCOL_VERSION_TLS1);
4657#endif
4658 if (default_version_max <= SSL_PROTOCOL_VERSION_TLS1)
4659 return;
4660
4661 TestServer::SSLOptions ssl_options(
4662 TestServer::SSLOptions::CERT_OK);
4663 ssl_options.tls_intolerant =
4664 TestServer::SSLOptions::TLS_INTOLERANT_TLS1_1;
4665 TestServer test_server(TestServer::TYPE_HTTPS,
4666 ssl_options,
[email protected]6cdfd7f2013-02-08 20:40:154667 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:444668 ASSERT_TRUE(test_server.Start());
4669
4670 TestDelegate d;
4671 TestURLRequestContext context(true);
4672 context.Init();
4673 d.set_allow_certificate_errors(true);
[email protected]007b3f82013-04-09 08:46:454674 URLRequest r(test_server.GetURL(std::string()), &d, &context);
[email protected]316c1e5e2012-09-12 15:17:444675 r.Start();
4676
4677 MessageLoop::current()->Run();
4678
4679 EXPECT_EQ(1, d.response_started_count());
4680 EXPECT_NE(0, d.bytes_received());
4681 EXPECT_EQ(static_cast<int>(SSL_CONNECTION_VERSION_TLS1),
4682 SSLConnectionStatusToVersion(r.ssl_info().connection_status));
4683 EXPECT_TRUE(r.ssl_info().connection_status & SSL_CONNECTION_VERSION_FALLBACK);
4684}
4685
4686// This tests that a load of www.google.com with a certificate error sets
4687// the |certificate_errors_are_fatal| flag correctly. This flag will cause
4688// the interstitial to be fatal.
4689TEST_F(HTTPSRequestTest, HTTPSPreloadedHSTSTest) {
4690 TestServer::SSLOptions ssl_options(
4691 TestServer::SSLOptions::CERT_MISMATCHED_NAME);
4692 TestServer test_server(TestServer::TYPE_HTTPS,
4693 ssl_options,
[email protected]6cdfd7f2013-02-08 20:40:154694 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:444695 ASSERT_TRUE(test_server.Start());
4696
4697 // We require that the URL be www.google.com in order to pick up the
4698 // preloaded HSTS entries in the TransportSecurityState. This means that we
4699 // have to use a MockHostResolver in order to direct www.google.com to the
[email protected]ceefd7fd2012-11-29 00:36:244700 // testserver. By default, MockHostResolver maps all hosts to 127.0.0.1.
[email protected]316c1e5e2012-09-12 15:17:444701
4702 MockHostResolver host_resolver;
[email protected]ceefd7fd2012-11-29 00:36:244703 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]316c1e5e2012-09-12 15:17:444704 TestURLRequestContext context(true);
4705 context.set_network_delegate(&network_delegate);
4706 context.set_host_resolver(&host_resolver);
4707 TransportSecurityState transport_security_state;
4708 context.set_transport_security_state(&transport_security_state);
4709 context.Init();
4710
4711 TestDelegate d;
[email protected]7d3cbc92013-03-18 22:33:044712 URLRequest r(GURL(base::StringPrintf("https://www.google.com:%d",
4713 test_server.host_port_pair().port())),
[email protected]316c1e5e2012-09-12 15:17:444714 &d,
4715 &context);
4716
4717 r.Start();
4718 EXPECT_TRUE(r.is_pending());
4719
4720 MessageLoop::current()->Run();
4721
4722 EXPECT_EQ(1, d.response_started_count());
4723 EXPECT_FALSE(d.received_data_before_response());
4724 EXPECT_TRUE(d.have_certificate_errors());
4725 EXPECT_TRUE(d.certificate_errors_are_fatal());
4726}
4727
4728// This tests that cached HTTPS page loads do not cause any updates to the
4729// TransportSecurityState.
4730TEST_F(HTTPSRequestTest, HTTPSErrorsNoClobberTSSTest) {
4731 // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't
4732 // matter. It just has to be any error.
4733 TestServer::SSLOptions ssl_options(
4734 TestServer::SSLOptions::CERT_MISMATCHED_NAME);
4735 TestServer test_server(TestServer::TYPE_HTTPS,
4736 ssl_options,
[email protected]6cdfd7f2013-02-08 20:40:154737 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:444738 ASSERT_TRUE(test_server.Start());
4739
4740 // We require that the URL be www.google.com in order to pick up the
4741 // preloaded and dynamic HSTS and public key pin entries in the
4742 // TransportSecurityState. This means that we have to use a
4743 // MockHostResolver in order to direct www.google.com to the testserver.
[email protected]ceefd7fd2012-11-29 00:36:244744 // By default, MockHostResolver maps all hosts to 127.0.0.1.
[email protected]316c1e5e2012-09-12 15:17:444745
4746 MockHostResolver host_resolver;
[email protected]ceefd7fd2012-11-29 00:36:244747 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]316c1e5e2012-09-12 15:17:444748 TestURLRequestContext context(true);
4749 context.set_network_delegate(&network_delegate);
4750 context.set_host_resolver(&host_resolver);
4751 TransportSecurityState transport_security_state;
4752 TransportSecurityState::DomainState domain_state;
4753 EXPECT_TRUE(transport_security_state.GetDomainState("www.google.com", true,
4754 &domain_state));
4755 context.set_transport_security_state(&transport_security_state);
4756 context.Init();
4757
4758 TestDelegate d;
[email protected]7d3cbc92013-03-18 22:33:044759 URLRequest r(GURL(base::StringPrintf("https://www.google.com:%d",
4760 test_server.host_port_pair().port())),
[email protected]316c1e5e2012-09-12 15:17:444761 &d,
4762 &context);
4763
4764 r.Start();
4765 EXPECT_TRUE(r.is_pending());
4766
4767 MessageLoop::current()->Run();
4768
4769 EXPECT_EQ(1, d.response_started_count());
4770 EXPECT_FALSE(d.received_data_before_response());
4771 EXPECT_TRUE(d.have_certificate_errors());
4772 EXPECT_TRUE(d.certificate_errors_are_fatal());
4773
4774 // Get a fresh copy of the state, and check that it hasn't been updated.
4775 TransportSecurityState::DomainState new_domain_state;
4776 EXPECT_TRUE(transport_security_state.GetDomainState("www.google.com", true,
4777 &new_domain_state));
4778 EXPECT_EQ(new_domain_state.upgrade_mode, domain_state.upgrade_mode);
4779 EXPECT_EQ(new_domain_state.include_subdomains,
4780 domain_state.include_subdomains);
4781 EXPECT_TRUE(FingerprintsEqual(new_domain_state.static_spki_hashes,
4782 domain_state.static_spki_hashes));
4783 EXPECT_TRUE(FingerprintsEqual(new_domain_state.dynamic_spki_hashes,
4784 domain_state.dynamic_spki_hashes));
4785 EXPECT_TRUE(FingerprintsEqual(new_domain_state.bad_static_spki_hashes,
4786 domain_state.bad_static_spki_hashes));
4787}
4788
[email protected]8ccc69f2012-11-28 19:52:144789// Make sure HSTS preserves a POST request's method and body.
4790TEST_F(HTTPSRequestTest, HSTSPreservesPosts) {
4791 static const char kData[] = "hello world";
4792
4793 TestServer::SSLOptions ssl_options(TestServer::SSLOptions::CERT_OK);
4794 TestServer test_server(TestServer::TYPE_HTTPS,
4795 ssl_options,
[email protected]6cdfd7f2013-02-08 20:40:154796 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]8ccc69f2012-11-28 19:52:144797 ASSERT_TRUE(test_server.Start());
4798
4799
4800 // Per spec, TransportSecurityState expects a domain name, rather than an IP
4801 // address, so a MockHostResolver is needed to redirect www.somewhere.com to
[email protected]ceefd7fd2012-11-29 00:36:244802 // the TestServer. By default, MockHostResolver maps all hosts to 127.0.0.1.
[email protected]8ccc69f2012-11-28 19:52:144803 MockHostResolver host_resolver;
[email protected]8ccc69f2012-11-28 19:52:144804
4805 // Force https for www.somewhere.com.
4806 TransportSecurityState transport_security_state;
[email protected]474f079e2013-03-02 19:11:204807 base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000);
4808 bool include_subdomains = false;
4809 transport_security_state.AddHSTS("www.somewhere.com", expiry,
4810 include_subdomains);
[email protected]8ccc69f2012-11-28 19:52:144811
4812 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
4813
4814 TestURLRequestContext context(true);
4815 context.set_host_resolver(&host_resolver);
4816 context.set_transport_security_state(&transport_security_state);
4817 context.set_network_delegate(&network_delegate);
4818 context.Init();
4819
4820 TestDelegate d;
4821 // Navigating to https://www.somewhere.com instead of https://127.0.0.1 will
4822 // cause a certificate error. Ignore the error.
4823 d.set_allow_certificate_errors(true);
4824
[email protected]7d3cbc92013-03-18 22:33:044825 URLRequest req(GURL(base::StringPrintf("http://www.somewhere.com:%d/echo",
4826 test_server.host_port_pair().port())),
[email protected]8ccc69f2012-11-28 19:52:144827 &d,
4828 &context);
4829 req.set_method("POST");
[email protected]f288ef02012-12-15 20:28:284830 req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
[email protected]8ccc69f2012-11-28 19:52:144831
4832 req.Start();
4833 MessageLoop::current()->Run();
4834
4835 EXPECT_EQ("https", req.url().scheme());
4836 EXPECT_EQ("POST", req.method());
4837 EXPECT_EQ(kData, d.data_received());
4838}
4839
[email protected]316c1e5e2012-09-12 15:17:444840TEST_F(HTTPSRequestTest, SSLv3Fallback) {
4841 TestServer::SSLOptions ssl_options(
4842 TestServer::SSLOptions::CERT_OK);
4843 ssl_options.tls_intolerant = TestServer::SSLOptions::TLS_INTOLERANT_ALL;
4844 TestServer test_server(TestServer::TYPE_HTTPS,
4845 ssl_options,
[email protected]6cdfd7f2013-02-08 20:40:154846 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:444847 ASSERT_TRUE(test_server.Start());
4848
4849 TestDelegate d;
4850 TestURLRequestContext context(true);
4851 context.Init();
4852 d.set_allow_certificate_errors(true);
[email protected]007b3f82013-04-09 08:46:454853 URLRequest r(test_server.GetURL(std::string()), &d, &context);
[email protected]316c1e5e2012-09-12 15:17:444854 r.Start();
4855
4856 MessageLoop::current()->Run();
4857
4858 EXPECT_EQ(1, d.response_started_count());
4859 EXPECT_NE(0, d.bytes_received());
4860 EXPECT_EQ(static_cast<int>(SSL_CONNECTION_VERSION_SSL3),
4861 SSLConnectionStatusToVersion(r.ssl_info().connection_status));
4862 EXPECT_TRUE(r.ssl_info().connection_status & SSL_CONNECTION_VERSION_FALLBACK);
4863}
4864
4865namespace {
4866
4867class SSLClientAuthTestDelegate : public TestDelegate {
4868 public:
4869 SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) {
4870 }
4871 virtual void OnCertificateRequested(
4872 URLRequest* request,
[email protected]46fadfd2013-02-06 09:40:164873 SSLCertRequestInfo* cert_request_info) OVERRIDE {
[email protected]316c1e5e2012-09-12 15:17:444874 on_certificate_requested_count_++;
4875 MessageLoop::current()->Quit();
4876 }
4877 int on_certificate_requested_count() {
4878 return on_certificate_requested_count_;
4879 }
4880 private:
4881 int on_certificate_requested_count_;
4882};
4883
4884} // namespace
4885
4886// TODO(davidben): Test the rest of the code. Specifically,
4887// - Filtering which certificates to select.
4888// - Sending a certificate back.
4889// - Getting a certificate request in an SSL renegotiation sending the
4890// HTTP request.
4891TEST_F(HTTPSRequestTest, ClientAuthTest) {
4892 TestServer::SSLOptions ssl_options;
4893 ssl_options.request_client_certificate = true;
4894 TestServer test_server(TestServer::TYPE_HTTPS,
4895 ssl_options,
[email protected]6cdfd7f2013-02-08 20:40:154896 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:444897 ASSERT_TRUE(test_server.Start());
4898
4899 SSLClientAuthTestDelegate d;
4900 {
[email protected]007b3f82013-04-09 08:46:454901 URLRequest r(test_server.GetURL(std::string()), &d, &default_context_);
[email protected]316c1e5e2012-09-12 15:17:444902
4903 r.Start();
4904 EXPECT_TRUE(r.is_pending());
4905
4906 MessageLoop::current()->Run();
4907
4908 EXPECT_EQ(1, d.on_certificate_requested_count());
4909 EXPECT_FALSE(d.received_data_before_response());
4910 EXPECT_EQ(0, d.bytes_received());
4911
4912 // Send no certificate.
4913 // TODO(davidben): Get temporary client cert import (with keys) working on
4914 // all platforms so we can test sending a cert as well.
4915 r.ContinueWithCertificate(NULL);
4916
4917 MessageLoop::current()->Run();
4918
4919 EXPECT_EQ(1, d.response_started_count());
4920 EXPECT_FALSE(d.received_data_before_response());
4921 EXPECT_NE(0, d.bytes_received());
4922 }
4923}
4924
4925TEST_F(HTTPSRequestTest, ResumeTest) {
4926 // Test that we attempt a session resume when making two connections to the
4927 // same host.
4928 TestServer::SSLOptions ssl_options;
4929 ssl_options.record_resume = true;
4930 TestServer test_server(TestServer::TYPE_HTTPS,
4931 ssl_options,
[email protected]6cdfd7f2013-02-08 20:40:154932 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:444933 ASSERT_TRUE(test_server.Start());
4934
4935 SSLClientSocket::ClearSessionCache();
4936
4937 {
4938 TestDelegate d;
4939 URLRequest r(
4940 test_server.GetURL("ssl-session-cache"), &d, &default_context_);
4941
4942 r.Start();
4943 EXPECT_TRUE(r.is_pending());
4944
4945 MessageLoop::current()->Run();
4946
4947 EXPECT_EQ(1, d.response_started_count());
4948 }
4949
4950 reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())->
4951 CloseAllConnections();
4952
4953 {
4954 TestDelegate d;
4955 URLRequest r(
4956 test_server.GetURL("ssl-session-cache"), &d, &default_context_);
4957
4958 r.Start();
4959 EXPECT_TRUE(r.is_pending());
4960
4961 MessageLoop::current()->Run();
4962
4963 // The response will look like;
4964 // insert abc
4965 // lookup abc
4966 // insert xyz
4967 //
4968 // With a newline at the end which makes the split think that there are
4969 // four lines.
4970
4971 EXPECT_EQ(1, d.response_started_count());
4972 std::vector<std::string> lines;
4973 base::SplitString(d.data_received(), '\n', &lines);
4974 ASSERT_EQ(4u, lines.size()) << d.data_received();
4975
4976 std::string session_id;
4977
4978 for (size_t i = 0; i < 2; i++) {
4979 std::vector<std::string> parts;
4980 base::SplitString(lines[i], '\t', &parts);
4981 ASSERT_EQ(2u, parts.size());
4982 if (i == 0) {
4983 EXPECT_EQ("insert", parts[0]);
4984 session_id = parts[1];
4985 } else {
4986 EXPECT_EQ("lookup", parts[0]);
4987 EXPECT_EQ(session_id, parts[1]);
4988 }
4989 }
4990 }
4991}
4992
4993TEST_F(HTTPSRequestTest, SSLSessionCacheShardTest) {
4994 // Test that sessions aren't resumed when the value of ssl_session_cache_shard
4995 // differs.
4996 TestServer::SSLOptions ssl_options;
4997 ssl_options.record_resume = true;
4998 TestServer test_server(TestServer::TYPE_HTTPS,
4999 ssl_options,
[email protected]6cdfd7f2013-02-08 20:40:155000 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:445001 ASSERT_TRUE(test_server.Start());
5002
5003 SSLClientSocket::ClearSessionCache();
5004
5005 {
5006 TestDelegate d;
5007 URLRequest r(
5008 test_server.GetURL("ssl-session-cache"), &d, &default_context_);
5009
5010 r.Start();
5011 EXPECT_TRUE(r.is_pending());
5012
5013 MessageLoop::current()->Run();
5014
5015 EXPECT_EQ(1, d.response_started_count());
5016 }
5017
5018 // Now create a new HttpCache with a different ssl_session_cache_shard value.
5019 HttpNetworkSession::Params params;
5020 params.host_resolver = default_context_.host_resolver();
5021 params.cert_verifier = default_context_.cert_verifier();
5022 params.proxy_service = default_context_.proxy_service();
5023 params.ssl_config_service = default_context_.ssl_config_service();
5024 params.http_auth_handler_factory =
5025 default_context_.http_auth_handler_factory();
[email protected]cc05edc2013-03-08 18:04:415026 params.network_delegate = &default_network_delegate_;
[email protected]316c1e5e2012-09-12 15:17:445027 params.http_server_properties = default_context_.http_server_properties();
5028 params.ssl_session_cache_shard = "alternate";
5029
5030 scoped_ptr<net::HttpCache> cache(new net::HttpCache(
5031 new net::HttpNetworkSession(params),
5032 net::HttpCache::DefaultBackend::InMemory(0)));
5033
5034 default_context_.set_http_transaction_factory(cache.get());
5035
5036 {
5037 TestDelegate d;
5038 URLRequest r(
5039 test_server.GetURL("ssl-session-cache"), &d, &default_context_);
5040
5041 r.Start();
5042 EXPECT_TRUE(r.is_pending());
5043
5044 MessageLoop::current()->Run();
5045
5046 // The response will look like;
5047 // insert abc
5048 // insert xyz
5049 //
5050 // With a newline at the end which makes the split think that there are
5051 // three lines.
5052
5053 EXPECT_EQ(1, d.response_started_count());
5054 std::vector<std::string> lines;
5055 base::SplitString(d.data_received(), '\n', &lines);
5056 ASSERT_EQ(3u, lines.size());
5057
5058 std::string session_id;
5059 for (size_t i = 0; i < 2; i++) {
5060 std::vector<std::string> parts;
5061 base::SplitString(lines[i], '\t', &parts);
5062 ASSERT_EQ(2u, parts.size());
5063 EXPECT_EQ("insert", parts[0]);
5064 if (i == 0) {
5065 session_id = parts[1];
5066 } else {
5067 EXPECT_NE(session_id, parts[1]);
5068 }
5069 }
5070 }
5071}
5072
[email protected]a13234c2012-03-20 21:45:025073class TestSSLConfigService : public SSLConfigService {
[email protected]dffe8242012-03-20 15:14:275074 public:
[email protected]a13234c2012-03-20 21:45:025075 TestSSLConfigService(bool ev_enabled, bool online_rev_checking)
5076 : ev_enabled_(ev_enabled),
5077 online_rev_checking_(online_rev_checking) {
5078 }
5079
[email protected]a9813302012-04-28 09:29:285080 // SSLConfigService:
5081 virtual void GetSSLConfig(SSLConfig* config) OVERRIDE {
[email protected]dffe8242012-03-20 15:14:275082 *config = SSLConfig();
[email protected]a13234c2012-03-20 21:45:025083 config->rev_checking_enabled = online_rev_checking_;
5084 config->verify_ev_cert = ev_enabled_;
[email protected]dffe8242012-03-20 15:14:275085 }
[email protected]a13234c2012-03-20 21:45:025086
[email protected]a9813302012-04-28 09:29:285087 protected:
5088 virtual ~TestSSLConfigService() {}
5089
[email protected]a13234c2012-03-20 21:45:025090 private:
5091 const bool ev_enabled_;
5092 const bool online_rev_checking_;
[email protected]dffe8242012-03-20 15:14:275093};
5094
5095// This the fingerprint of the "Testing CA" certificate used by the testserver.
5096// See net/data/ssl/certificates/ocsp-test-root.pem.
[email protected]ede03212012-09-07 12:52:265097static const SHA1HashValue kOCSPTestCertFingerprint =
[email protected]dffe8242012-03-20 15:14:275098 { { 0xf1, 0xad, 0xf6, 0xce, 0x42, 0xac, 0xe7, 0xb4, 0xf4, 0x24,
5099 0xdb, 0x1a, 0xf7, 0xa0, 0x9f, 0x09, 0xa1, 0xea, 0xf1, 0x5c } };
5100
5101// This is the policy OID contained in the certificates that testserver
5102// generates.
5103static const char kOCSPTestCertPolicy[] = "1.3.6.1.4.1.11129.2.4.1";
5104
5105class HTTPSOCSPTest : public HTTPSRequestTest {
5106 public:
5107 HTTPSOCSPTest()
[email protected]ef2bf422012-05-11 03:27:095108 : context_(true),
[email protected]b6f2de32012-08-17 04:35:085109 ev_test_policy_(
5110 new ScopedTestEVPolicy(EVRootCAMetadata::GetInstance(),
5111 kOCSPTestCertFingerprint,
5112 kOCSPTestCertPolicy)) {
[email protected]a13234c2012-03-20 21:45:025113 }
5114
5115 virtual void SetUp() OVERRIDE {
[email protected]ef2bf422012-05-11 03:27:095116 SetupContext(&context_);
5117 context_.Init();
[email protected]dffe8242012-03-20 15:14:275118
5119 scoped_refptr<net::X509Certificate> root_cert =
5120 ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem");
5121 CHECK_NE(static_cast<X509Certificate*>(NULL), root_cert);
5122 test_root_.reset(new ScopedTestRoot(root_cert));
5123
[email protected]a592c0432012-12-01 18:10:295124#if defined(USE_NSS) || defined(OS_IOS)
[email protected]ef2bf422012-05-11 03:27:095125 SetURLRequestContextForNSSHttpIO(&context_);
[email protected]dffe8242012-03-20 15:14:275126 EnsureNSSHttpIOInit();
5127#endif
5128 }
5129
[email protected]295e5cd2012-08-23 01:05:125130 void DoConnection(const TestServer::SSLOptions& ssl_options,
[email protected]dffe8242012-03-20 15:14:275131 CertStatus* out_cert_status) {
[email protected]924e9f92012-12-16 22:00:535132 // We always overwrite out_cert_status.
5133 *out_cert_status = 0;
[email protected]295e5cd2012-08-23 01:05:125134 TestServer test_server(TestServer::TYPE_HTTPS,
5135 ssl_options,
[email protected]6cdfd7f2013-02-08 20:40:155136 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]dffe8242012-03-20 15:14:275137 ASSERT_TRUE(test_server.Start());
5138
5139 TestDelegate d;
5140 d.set_allow_certificate_errors(true);
[email protected]007b3f82013-04-09 08:46:455141 URLRequest r(test_server.GetURL(std::string()), &d, &context_);
[email protected]dffe8242012-03-20 15:14:275142 r.Start();
5143
5144 MessageLoop::current()->Run();
5145
5146 EXPECT_EQ(1, d.response_started_count());
5147 *out_cert_status = r.ssl_info().cert_status;
5148 }
5149
[email protected]46fadfd2013-02-06 09:40:165150 virtual ~HTTPSOCSPTest() {
[email protected]a592c0432012-12-01 18:10:295151#if defined(USE_NSS) || defined(OS_IOS)
[email protected]dffe8242012-03-20 15:14:275152 ShutdownNSSHttpIO();
5153#endif
5154 }
5155
[email protected]a13234c2012-03-20 21:45:025156 protected:
5157 // SetupContext configures the URLRequestContext that will be used for making
5158 // connetions to testserver. This can be overridden in test subclasses for
5159 // different behaviour.
5160 virtual void SetupContext(URLRequestContext* context) {
5161 context->set_ssl_config_service(
5162 new TestSSLConfigService(true /* check for EV */,
5163 true /* online revocation checking */));
5164 }
5165
[email protected]dffe8242012-03-20 15:14:275166 scoped_ptr<ScopedTestRoot> test_root_;
[email protected]ef2bf422012-05-11 03:27:095167 TestURLRequestContext context_;
[email protected]b6f2de32012-08-17 04:35:085168 scoped_ptr<ScopedTestEVPolicy> ev_test_policy_;
[email protected]dffe8242012-03-20 15:14:275169};
5170
[email protected]a13234c2012-03-20 21:45:025171static CertStatus ExpectedCertStatusForFailedOnlineRevocationCheck() {
[email protected]05454a432012-03-20 20:04:015172#if defined(OS_WIN)
[email protected]a13234c2012-03-20 21:45:025173 // Windows can return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION but we don't
5174 // have that ability on other platforms.
5175 return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION;
5176#else
5177 return 0;
5178#endif
5179}
5180
5181// SystemUsesChromiumEVMetadata returns true iff the current operating system
5182// uses Chromium's EV metadata (i.e. EVRootCAMetadata). If it does not, then
5183// several tests are effected because our testing EV certificate won't be
5184// recognised as EV.
5185static bool SystemUsesChromiumEVMetadata() {
[email protected]5c504192012-03-27 19:00:575186#if defined(USE_OPENSSL)
5187 // http://crbug.com/117478 - OpenSSL does not support EV validation.
5188 return false;
[email protected]a592c0432012-12-01 18:10:295189#elif defined(OS_MACOSX) && !defined(OS_IOS)
[email protected]a13234c2012-03-20 21:45:025190 // On OS X, we use the system to tell us whether a certificate is EV or not
5191 // and the system won't recognise our testing root.
[email protected]05454a432012-03-20 20:04:015192 return false;
5193#else
5194 return true;
5195#endif
5196}
5197
[email protected]b6f2de32012-08-17 04:35:085198static bool SystemSupportsOCSP() {
[email protected]5c504192012-03-27 19:00:575199#if defined(USE_OPENSSL)
5200 // http://crbug.com/117478 - OpenSSL does not support OCSP.
5201 return false;
5202#elif defined(OS_WIN)
[email protected]a13234c2012-03-20 21:45:025203 return base::win::GetVersion() >= base::win::VERSION_VISTA;
5204#elif defined(OS_ANDROID)
5205 // TODO(jnd): http://crbug.com/117478 - EV verification is not yet supported.
5206 return false;
5207#else
5208 return true;
5209#endif
5210}
5211
[email protected]dffe8242012-03-20 15:14:275212TEST_F(HTTPSOCSPTest, Valid) {
5213 if (!SystemSupportsOCSP()) {
5214 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
5215 return;
5216 }
5217
[email protected]295e5cd2012-08-23 01:05:125218 TestServer::SSLOptions ssl_options(TestServer::SSLOptions::CERT_AUTO);
5219 ssl_options.ocsp_status = TestServer::SSLOptions::OCSP_OK;
[email protected]dffe8242012-03-20 15:14:275220
[email protected]924e9f92012-12-16 22:00:535221 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:125222 DoConnection(ssl_options, &cert_status);
[email protected]dffe8242012-03-20 15:14:275223
5224 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
5225
[email protected]a13234c2012-03-20 21:45:025226 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
5227 static_cast<bool>(cert_status & CERT_STATUS_IS_EV));
[email protected]dffe8242012-03-20 15:14:275228
5229 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
5230}
5231
5232TEST_F(HTTPSOCSPTest, Revoked) {
5233 if (!SystemSupportsOCSP()) {
5234 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
5235 return;
5236 }
5237
[email protected]295e5cd2012-08-23 01:05:125238 TestServer::SSLOptions ssl_options(
5239 TestServer::SSLOptions::CERT_AUTO);
5240 ssl_options.ocsp_status = TestServer::SSLOptions::OCSP_REVOKED;
[email protected]dffe8242012-03-20 15:14:275241
5242 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:125243 DoConnection(ssl_options, &cert_status);
[email protected]dffe8242012-03-20 15:14:275244
[email protected]a592c0432012-12-01 18:10:295245#if !(defined(OS_MACOSX) && !defined(OS_IOS))
[email protected]dffe8242012-03-20 15:14:275246 // Doesn't pass on OS X yet for reasons that need to be investigated.
5247 EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS);
5248#endif
5249 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
5250 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
5251}
5252
5253TEST_F(HTTPSOCSPTest, Invalid) {
5254 if (!SystemSupportsOCSP()) {
5255 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
5256 return;
5257 }
5258
[email protected]295e5cd2012-08-23 01:05:125259 TestServer::SSLOptions ssl_options(
5260 TestServer::SSLOptions::CERT_AUTO);
5261 ssl_options.ocsp_status = TestServer::SSLOptions::OCSP_INVALID;
[email protected]dffe8242012-03-20 15:14:275262
[email protected]924e9f92012-12-16 22:00:535263 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:125264 DoConnection(ssl_options, &cert_status);
[email protected]dffe8242012-03-20 15:14:275265
[email protected]a13234c2012-03-20 21:45:025266 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
[email protected]dffe8242012-03-20 15:14:275267 cert_status & CERT_STATUS_ALL_ERRORS);
[email protected]dffe8242012-03-20 15:14:275268
5269 // Without a positive OCSP response, we shouldn't show the EV status.
5270 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
5271 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
5272}
[email protected]a13234c2012-03-20 21:45:025273
5274class HTTPSEVCRLSetTest : public HTTPSOCSPTest {
5275 protected:
5276 virtual void SetupContext(URLRequestContext* context) OVERRIDE {
5277 context->set_ssl_config_service(
5278 new TestSSLConfigService(true /* check for EV */,
5279 false /* online revocation checking */));
5280 }
5281};
5282
5283TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndInvalidOCSP) {
5284 if (!SystemSupportsOCSP()) {
5285 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
5286 return;
5287 }
5288
[email protected]295e5cd2012-08-23 01:05:125289 TestServer::SSLOptions ssl_options(
5290 TestServer::SSLOptions::CERT_AUTO);
5291 ssl_options.ocsp_status = TestServer::SSLOptions::OCSP_INVALID;
[email protected]a13234c2012-03-20 21:45:025292 SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>());
5293
[email protected]924e9f92012-12-16 22:00:535294 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:125295 DoConnection(ssl_options, &cert_status);
[email protected]a13234c2012-03-20 21:45:025296
5297 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
5298 cert_status & CERT_STATUS_ALL_ERRORS);
5299
5300 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
[email protected]b6f2de32012-08-17 04:35:085301 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
5302 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
[email protected]a13234c2012-03-20 21:45:025303}
5304
5305TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndGoodOCSP) {
5306 if (!SystemSupportsOCSP()) {
5307 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
5308 return;
5309 }
5310
[email protected]295e5cd2012-08-23 01:05:125311 TestServer::SSLOptions ssl_options(
5312 TestServer::SSLOptions::CERT_AUTO);
5313 ssl_options.ocsp_status = TestServer::SSLOptions::OCSP_OK;
[email protected]a13234c2012-03-20 21:45:025314 SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>());
5315
5316 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:125317 DoConnection(ssl_options, &cert_status);
[email protected]a13234c2012-03-20 21:45:025318
5319 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
5320
5321 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
5322 static_cast<bool>(cert_status & CERT_STATUS_IS_EV));
[email protected]b6f2de32012-08-17 04:35:085323 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
5324 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
[email protected]a13234c2012-03-20 21:45:025325}
5326
5327TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSet) {
5328 if (!SystemSupportsOCSP()) {
5329 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
5330 return;
5331 }
5332
[email protected]295e5cd2012-08-23 01:05:125333 TestServer::SSLOptions ssl_options(
5334 TestServer::SSLOptions::CERT_AUTO);
5335 ssl_options.ocsp_status = TestServer::SSLOptions::OCSP_INVALID;
[email protected]a13234c2012-03-20 21:45:025336 SSLConfigService::SetCRLSet(
5337 scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting()));
5338
5339 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:125340 DoConnection(ssl_options, &cert_status);
[email protected]a13234c2012-03-20 21:45:025341
5342 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
5343 cert_status & CERT_STATUS_ALL_ERRORS);
5344
5345 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
[email protected]b6f2de32012-08-17 04:35:085346 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
5347 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
[email protected]a13234c2012-03-20 21:45:025348}
5349
5350TEST_F(HTTPSEVCRLSetTest, FreshCRLSet) {
[email protected]295e5cd2012-08-23 01:05:125351 TestServer::SSLOptions ssl_options(
5352 TestServer::SSLOptions::CERT_AUTO);
5353 ssl_options.ocsp_status = TestServer::SSLOptions::OCSP_INVALID;
[email protected]a13234c2012-03-20 21:45:025354 SSLConfigService::SetCRLSet(
5355 scoped_refptr<CRLSet>(CRLSet::EmptyCRLSetForTesting()));
5356
[email protected]924e9f92012-12-16 22:00:535357 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:125358 DoConnection(ssl_options, &cert_status);
[email protected]a13234c2012-03-20 21:45:025359
5360 // With a valid, fresh CRLSet the bad OCSP response shouldn't matter because
5361 // we wont check it.
5362 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
5363
5364 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
5365 static_cast<bool>(cert_status & CERT_STATUS_IS_EV));
5366
5367 EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
5368}
5369
[email protected]b6f2de32012-08-17 04:35:085370TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSetAndRevokedNonEVCert) {
5371 // Test that when EV verification is requested, but online revocation
5372 // checking is disabled, and the leaf certificate is not in fact EV, that
5373 // no revocation checking actually happens.
5374 if (!SystemSupportsOCSP()) {
5375 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
5376 return;
5377 }
5378
5379 // Unmark the certificate's OID as EV, which should disable revocation
5380 // checking (as per the user preference)
5381 ev_test_policy_.reset();
5382
[email protected]295e5cd2012-08-23 01:05:125383 TestServer::SSLOptions ssl_options(
5384 TestServer::SSLOptions::CERT_AUTO);
5385 ssl_options.ocsp_status = TestServer::SSLOptions::OCSP_REVOKED;
[email protected]b6f2de32012-08-17 04:35:085386 SSLConfigService::SetCRLSet(
5387 scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting()));
5388
5389 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:125390 DoConnection(ssl_options, &cert_status);
[email protected]b6f2de32012-08-17 04:35:085391
5392 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
5393
5394 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
5395 EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
5396}
5397
[email protected]a13234c2012-03-20 21:45:025398class HTTPSCRLSetTest : public HTTPSOCSPTest {
5399 protected:
5400 virtual void SetupContext(URLRequestContext* context) OVERRIDE {
5401 context->set_ssl_config_service(
5402 new TestSSLConfigService(false /* check for EV */,
5403 false /* online revocation checking */));
5404 }
5405};
5406
5407TEST_F(HTTPSCRLSetTest, ExpiredCRLSet) {
[email protected]295e5cd2012-08-23 01:05:125408 TestServer::SSLOptions ssl_options(
5409 TestServer::SSLOptions::CERT_AUTO);
5410 ssl_options.ocsp_status = TestServer::SSLOptions::OCSP_INVALID;
[email protected]a13234c2012-03-20 21:45:025411 SSLConfigService::SetCRLSet(
5412 scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting()));
5413
[email protected]924e9f92012-12-16 22:00:535414 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:125415 DoConnection(ssl_options, &cert_status);
[email protected]a13234c2012-03-20 21:45:025416
5417 // If we're not trying EV verification then, even if the CRLSet has expired,
5418 // we don't fall back to online revocation checks.
5419 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
5420 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
5421 EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
5422}
[email protected]316c1e5e2012-09-12 15:17:445423#endif // !defined(OS_IOS)
[email protected]dffe8242012-03-20 15:14:275424
[email protected]316c1e5e2012-09-12 15:17:445425#if !defined(DISABLE_FTP_SUPPORT)
[email protected]b89ca032009-08-31 21:41:315426class URLRequestTestFTP : public URLRequestTest {
[email protected]95409e12010-08-17 20:07:115427 public:
[email protected]d9fca99a2012-02-24 16:16:205428 URLRequestTestFTP()
[email protected]6cdfd7f2013-02-08 20:40:155429 : test_server_(TestServer::TYPE_FTP, TestServer::kLocalhost,
5430 base::FilePath()) {
[email protected]95409e12010-08-17 20:07:115431 }
5432
[email protected]b89ca032009-08-31 21:41:315433 protected:
[email protected]7461a402011-03-24 23:19:515434 TestServer test_server_;
[email protected]b89ca032009-08-31 21:41:315435};
5436
[email protected]d2a133182012-08-05 16:44:085437// Make sure an FTP request using an unsafe ports fails.
[email protected]f2f31b32013-01-16 23:24:095438TEST_F(URLRequestTestFTP, UnsafePort) {
[email protected]d2a133182012-08-05 16:44:085439 ASSERT_TRUE(test_server_.Start());
5440
[email protected]9d5730b2012-08-24 17:42:495441 URLRequestJobFactoryImpl job_factory;
[email protected]d2a133182012-08-05 16:44:085442
5443 GURL url("ftp://127.0.0.1:7");
[email protected]e13c1462013-03-19 03:05:505444 FtpProtocolHandler ftp_protocol_handler(
5445 default_context_.ftp_transaction_factory(),
5446 default_context_.ftp_auth_cache());
[email protected]d2a133182012-08-05 16:44:085447 job_factory.SetProtocolHandler(
5448 "ftp",
[email protected]e13c1462013-03-19 03:05:505449 new FtpProtocolHandler(default_context_.ftp_transaction_factory(),
5450 default_context_.ftp_auth_cache()));
[email protected]d2a133182012-08-05 16:44:085451 default_context_.set_job_factory(&job_factory);
5452
5453 TestDelegate d;
5454 {
5455 URLRequest r(url, &d, &default_context_);
5456 r.Start();
5457 EXPECT_TRUE(r.is_pending());
5458
5459 MessageLoop::current()->Run();
5460
5461 EXPECT_FALSE(r.is_pending());
5462 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
5463 EXPECT_EQ(ERR_UNSAFE_PORT, r.status().error());
5464 }
5465}
5466
[email protected]5accf7332009-11-24 03:41:385467// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:125468TEST_F(URLRequestTestFTP, DISABLED_FTPDirectoryListing) {
[email protected]95409e12010-08-17 20:07:115469 ASSERT_TRUE(test_server_.Start());
5470
[email protected]a25e90e2009-09-09 17:05:375471 TestDelegate d;
5472 {
[email protected]94e2bbe2012-06-22 15:26:135473 URLRequest r(test_server_.GetURL("/"), &d, &default_context_);
[email protected]a25e90e2009-09-09 17:05:375474 r.Start();
5475 EXPECT_TRUE(r.is_pending());
5476
5477 MessageLoop::current()->Run();
5478
5479 EXPECT_FALSE(r.is_pending());
5480 EXPECT_EQ(1, d.response_started_count());
5481 EXPECT_FALSE(d.received_data_before_response());
5482 EXPECT_LT(0, d.bytes_received());
[email protected]6d81b482011-02-22 19:47:195483 EXPECT_EQ(test_server_.host_port_pair().host(),
5484 r.GetSocketAddress().host());
5485 EXPECT_EQ(test_server_.host_port_pair().port(),
5486 r.GetSocketAddress().port());
[email protected]a25e90e2009-09-09 17:05:375487 }
5488}
5489
[email protected]7df70012010-02-04 00:09:555490// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:125491TEST_F(URLRequestTestFTP, DISABLED_FTPGetTestAnonymous) {
[email protected]95409e12010-08-17 20:07:115492 ASSERT_TRUE(test_server_.Start());
5493
[email protected]6cdfd7f2013-02-08 20:40:155494 base::FilePath app_path;
[email protected]dd265012009-01-08 20:45:275495 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:025496 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:275497 TestDelegate d;
5498 {
[email protected]94e2bbe2012-06-22 15:26:135499 URLRequest r(test_server_.GetURL("/LICENSE"), &d, &default_context_);
[email protected]dd265012009-01-08 20:45:275500 r.Start();
5501 EXPECT_TRUE(r.is_pending());
5502
5503 MessageLoop::current()->Run();
5504
5505 int64 file_size = 0;
5506 file_util::GetFileSize(app_path, &file_size);
5507
[email protected]ba2f3342009-07-30 18:08:425508 EXPECT_FALSE(r.is_pending());
[email protected]dd265012009-01-08 20:45:275509 EXPECT_EQ(1, d.response_started_count());
5510 EXPECT_FALSE(d.received_data_before_response());
5511 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
[email protected]6d81b482011-02-22 19:47:195512 EXPECT_EQ(test_server_.host_port_pair().host(),
5513 r.GetSocketAddress().host());
5514 EXPECT_EQ(test_server_.host_port_pair().port(),
5515 r.GetSocketAddress().port());
[email protected]dd265012009-01-08 20:45:275516 }
5517}
5518
[email protected]e9ecbd12009-12-20 18:44:405519// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:125520TEST_F(URLRequestTestFTP, DISABLED_FTPGetTest) {
[email protected]95409e12010-08-17 20:07:115521 ASSERT_TRUE(test_server_.Start());
5522
[email protected]6cdfd7f2013-02-08 20:40:155523 base::FilePath app_path;
[email protected]dd265012009-01-08 20:45:275524 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:025525 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:275526 TestDelegate d;
5527 {
[email protected]ef2bf422012-05-11 03:27:095528 URLRequest r(
[email protected]95409e12010-08-17 20:07:115529 test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", "chrome"),
[email protected]94e2bbe2012-06-22 15:26:135530 &d,
5531 &default_context_);
[email protected]dd265012009-01-08 20:45:275532 r.Start();
5533 EXPECT_TRUE(r.is_pending());
5534
5535 MessageLoop::current()->Run();
5536
5537 int64 file_size = 0;
5538 file_util::GetFileSize(app_path, &file_size);
5539
[email protected]ba2f3342009-07-30 18:08:425540 EXPECT_FALSE(r.is_pending());
[email protected]6d81b482011-02-22 19:47:195541 EXPECT_EQ(test_server_.host_port_pair().host(),
5542 r.GetSocketAddress().host());
5543 EXPECT_EQ(test_server_.host_port_pair().port(),
5544 r.GetSocketAddress().port());
[email protected]dd265012009-01-08 20:45:275545 EXPECT_EQ(1, d.response_started_count());
5546 EXPECT_FALSE(d.received_data_before_response());
5547 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
[email protected]58e32bb2013-01-21 18:23:255548
5549 LoadTimingInfo load_timing_info;
[email protected]0a703912013-02-02 04:25:175550 r.GetLoadTimingInfo(&load_timing_info);
[email protected]58e32bb2013-01-21 18:23:255551 TestLoadTimingNoHttpConnection(load_timing_info);
[email protected]dd265012009-01-08 20:45:275552 }
5553}
5554
[email protected]49abd652010-08-05 05:04:535555// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:125556TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPassword) {
[email protected]95409e12010-08-17 20:07:115557 ASSERT_TRUE(test_server_.Start());
5558
[email protected]6cdfd7f2013-02-08 20:40:155559 base::FilePath app_path;
[email protected]dd265012009-01-08 20:45:275560 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:025561 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:275562 TestDelegate d;
5563 {
[email protected]ef2bf422012-05-11 03:27:095564 URLRequest r(
[email protected]95409e12010-08-17 20:07:115565 test_server_.GetURLWithUserAndPassword("/LICENSE",
5566 "chrome",
5567 "wrong_password"),
[email protected]94e2bbe2012-06-22 15:26:135568 &d,
5569 &default_context_);
[email protected]dd265012009-01-08 20:45:275570 r.Start();
5571 EXPECT_TRUE(r.is_pending());
5572
5573 MessageLoop::current()->Run();
5574
5575 int64 file_size = 0;
5576 file_util::GetFileSize(app_path, &file_size);
5577
[email protected]ba2f3342009-07-30 18:08:425578 EXPECT_FALSE(r.is_pending());
[email protected]dd265012009-01-08 20:45:275579 EXPECT_EQ(1, d.response_started_count());
5580 EXPECT_FALSE(d.received_data_before_response());
5581 EXPECT_EQ(d.bytes_received(), 0);
5582 }
5583}
5584
[email protected]cde4e80d2009-10-16 19:58:155585// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:125586TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPasswordRestart) {
[email protected]95409e12010-08-17 20:07:115587 ASSERT_TRUE(test_server_.Start());
5588
[email protected]6cdfd7f2013-02-08 20:40:155589 base::FilePath app_path;
[email protected]8b8a197d2009-08-26 15:57:585590 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
5591 app_path = app_path.AppendASCII("LICENSE");
5592 TestDelegate d;
5593 // Set correct login credentials. The delegate will be asked for them when
5594 // the initial login with wrong credentials will fail.
[email protected]f3cf9802011-10-28 18:44:585595 d.set_credentials(AuthCredentials(kChrome, kChrome));
[email protected]8b8a197d2009-08-26 15:57:585596 {
[email protected]ef2bf422012-05-11 03:27:095597 URLRequest r(
[email protected]95409e12010-08-17 20:07:115598 test_server_.GetURLWithUserAndPassword("/LICENSE",
5599 "chrome",
5600 "wrong_password"),
[email protected]94e2bbe2012-06-22 15:26:135601 &d,
5602 &default_context_);
[email protected]8b8a197d2009-08-26 15:57:585603 r.Start();
5604 EXPECT_TRUE(r.is_pending());
5605
5606 MessageLoop::current()->Run();
5607
5608 int64 file_size = 0;
5609 file_util::GetFileSize(app_path, &file_size);
5610
5611 EXPECT_FALSE(r.is_pending());
5612 EXPECT_EQ(1, d.response_started_count());
5613 EXPECT_FALSE(d.received_data_before_response());
5614 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
5615 }
5616}
5617
[email protected]49abd652010-08-05 05:04:535618// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:125619TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUser) {
[email protected]95409e12010-08-17 20:07:115620 ASSERT_TRUE(test_server_.Start());
5621
[email protected]6cdfd7f2013-02-08 20:40:155622 base::FilePath app_path;
[email protected]dd265012009-01-08 20:45:275623 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:025624 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:275625 TestDelegate d;
5626 {
[email protected]ef2bf422012-05-11 03:27:095627 URLRequest r(
[email protected]95409e12010-08-17 20:07:115628 test_server_.GetURLWithUserAndPassword("/LICENSE",
5629 "wrong_user",
5630 "chrome"),
[email protected]94e2bbe2012-06-22 15:26:135631 &d,
5632 &default_context_);
[email protected]dd265012009-01-08 20:45:275633 r.Start();
5634 EXPECT_TRUE(r.is_pending());
5635
5636 MessageLoop::current()->Run();
5637
5638 int64 file_size = 0;
5639 file_util::GetFileSize(app_path, &file_size);
5640
[email protected]ba2f3342009-07-30 18:08:425641 EXPECT_FALSE(r.is_pending());
[email protected]dd265012009-01-08 20:45:275642 EXPECT_EQ(1, d.response_started_count());
5643 EXPECT_FALSE(d.received_data_before_response());
5644 EXPECT_EQ(d.bytes_received(), 0);
5645 }
5646}
[email protected]8b8a197d2009-08-26 15:57:585647
[email protected]cde4e80d2009-10-16 19:58:155648// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:125649TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUserRestart) {
[email protected]95409e12010-08-17 20:07:115650 ASSERT_TRUE(test_server_.Start());
5651
[email protected]6cdfd7f2013-02-08 20:40:155652 base::FilePath app_path;
[email protected]8b8a197d2009-08-26 15:57:585653 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
5654 app_path = app_path.AppendASCII("LICENSE");
5655 TestDelegate d;
5656 // Set correct login credentials. The delegate will be asked for them when
5657 // the initial login with wrong credentials will fail.
[email protected]f3cf9802011-10-28 18:44:585658 d.set_credentials(AuthCredentials(kChrome, kChrome));
[email protected]8b8a197d2009-08-26 15:57:585659 {
[email protected]ef2bf422012-05-11 03:27:095660 URLRequest r(
[email protected]95409e12010-08-17 20:07:115661 test_server_.GetURLWithUserAndPassword("/LICENSE",
5662 "wrong_user",
5663 "chrome"),
[email protected]94e2bbe2012-06-22 15:26:135664 &d,
5665 &default_context_);
[email protected]8b8a197d2009-08-26 15:57:585666 r.Start();
5667 EXPECT_TRUE(r.is_pending());
5668
5669 MessageLoop::current()->Run();
5670
5671 int64 file_size = 0;
5672 file_util::GetFileSize(app_path, &file_size);
5673
5674 EXPECT_FALSE(r.is_pending());
5675 EXPECT_EQ(1, d.response_started_count());
5676 EXPECT_FALSE(d.received_data_before_response());
5677 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
5678 }
5679}
[email protected]60a3df52009-09-22 16:13:245680
[email protected]cde4e80d2009-10-16 19:58:155681// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:125682TEST_F(URLRequestTestFTP, DISABLED_FTPCacheURLCredentials) {
[email protected]95409e12010-08-17 20:07:115683 ASSERT_TRUE(test_server_.Start());
5684
[email protected]6cdfd7f2013-02-08 20:40:155685 base::FilePath app_path;
[email protected]60a3df52009-09-22 16:13:245686 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
5687 app_path = app_path.AppendASCII("LICENSE");
5688
5689 scoped_ptr<TestDelegate> d(new TestDelegate);
5690 {
5691 // Pass correct login identity in the URL.
[email protected]ef2bf422012-05-11 03:27:095692 URLRequest r(
[email protected]95409e12010-08-17 20:07:115693 test_server_.GetURLWithUserAndPassword("/LICENSE",
5694 "chrome",
5695 "chrome"),
[email protected]94e2bbe2012-06-22 15:26:135696 d.get(),
5697 &default_context_);
[email protected]60a3df52009-09-22 16:13:245698 r.Start();
5699 EXPECT_TRUE(r.is_pending());
5700
5701 MessageLoop::current()->Run();
5702
5703 int64 file_size = 0;
5704 file_util::GetFileSize(app_path, &file_size);
5705
5706 EXPECT_FALSE(r.is_pending());
5707 EXPECT_EQ(1, d->response_started_count());
5708 EXPECT_FALSE(d->received_data_before_response());
5709 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
5710 }
5711
5712 d.reset(new TestDelegate);
5713 {
5714 // This request should use cached identity from previous request.
[email protected]94e2bbe2012-06-22 15:26:135715 URLRequest r(test_server_.GetURL("/LICENSE"), d.get(), &default_context_);
[email protected]60a3df52009-09-22 16:13:245716 r.Start();
5717 EXPECT_TRUE(r.is_pending());
5718
5719 MessageLoop::current()->Run();
5720
5721 int64 file_size = 0;
5722 file_util::GetFileSize(app_path, &file_size);
5723
5724 EXPECT_FALSE(r.is_pending());
5725 EXPECT_EQ(1, d->response_started_count());
5726 EXPECT_FALSE(d->received_data_before_response());
5727 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
5728 }
5729}
5730
[email protected]cde4e80d2009-10-16 19:58:155731// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:125732TEST_F(URLRequestTestFTP, DISABLED_FTPCacheLoginBoxCredentials) {
[email protected]95409e12010-08-17 20:07:115733 ASSERT_TRUE(test_server_.Start());
5734
[email protected]6cdfd7f2013-02-08 20:40:155735 base::FilePath app_path;
[email protected]60a3df52009-09-22 16:13:245736 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
5737 app_path = app_path.AppendASCII("LICENSE");
5738
5739 scoped_ptr<TestDelegate> d(new TestDelegate);
5740 // Set correct login credentials. The delegate will be asked for them when
5741 // the initial login with wrong credentials will fail.
[email protected]f3cf9802011-10-28 18:44:585742 d->set_credentials(AuthCredentials(kChrome, kChrome));
[email protected]60a3df52009-09-22 16:13:245743 {
[email protected]ef2bf422012-05-11 03:27:095744 URLRequest r(
[email protected]95409e12010-08-17 20:07:115745 test_server_.GetURLWithUserAndPassword("/LICENSE",
5746 "chrome",
5747 "wrong_password"),
[email protected]94e2bbe2012-06-22 15:26:135748 d.get(),
5749 &default_context_);
[email protected]60a3df52009-09-22 16:13:245750 r.Start();
5751 EXPECT_TRUE(r.is_pending());
5752
5753 MessageLoop::current()->Run();
5754
5755 int64 file_size = 0;
5756 file_util::GetFileSize(app_path, &file_size);
5757
5758 EXPECT_FALSE(r.is_pending());
5759 EXPECT_EQ(1, d->response_started_count());
5760 EXPECT_FALSE(d->received_data_before_response());
5761 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
5762 }
5763
5764 // Use a new delegate without explicit credentials. The cached ones should be
5765 // used.
5766 d.reset(new TestDelegate);
5767 {
5768 // Don't pass wrong credentials in the URL, they would override valid cached
5769 // ones.
[email protected]94e2bbe2012-06-22 15:26:135770 URLRequest r(test_server_.GetURL("/LICENSE"), d.get(), &default_context_);
[email protected]60a3df52009-09-22 16:13:245771 r.Start();
5772 EXPECT_TRUE(r.is_pending());
5773
5774 MessageLoop::current()->Run();
5775
5776 int64 file_size = 0;
5777 file_util::GetFileSize(app_path, &file_size);
5778
5779 EXPECT_FALSE(r.is_pending());
5780 EXPECT_EQ(1, d->response_started_count());
5781 EXPECT_FALSE(d->received_data_before_response());
5782 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
5783 }
5784}
[email protected]316c1e5e2012-09-12 15:17:445785#endif // !defined(DISABLE_FTP_SUPPORT)
[email protected]7461a402011-03-24 23:19:515786
5787} // namespace net