blob: 43d5d308f5c5a813e6d509ceabc4ac0e1bb7e935 [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]3ca8b362013-11-11 22:18:0719#include "base/files/scoped_temp_dir.h"
[email protected]34b2b002009-11-20 06:53:2820#include "base/format_macros.h"
[email protected]084262c2011-12-01 21:12:4721#include "base/memory/weak_ptr.h"
[email protected]7f86564d2013-07-18 00:41:2222#include "base/message_loop/message_loop.h"
[email protected]255620da2013-08-19 13:14:2923#include "base/message_loop/message_loop_proxy.h"
initial.commit586acc5fe2008-07-26 22:42:5224#include "base/path_service.h"
[email protected]255620da2013-08-19 13:14:2925#include "base/run_loop.h"
[email protected]4dc3ad4f2013-06-11 07:15:5026#include "base/strings/string_number_conversions.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]7f86564d2013-07-18 00:41:2229#include "base/strings/string_util.h"
30#include "base/strings/stringprintf.h"
[email protected]750b2f3c2013-06-07 18:41:0531#include "base/strings/utf_string_conversions.h"
[email protected]58e32bb2013-01-21 18:23:2532#include "net/base/capturing_net_log.h"
initial.commit586acc5fe2008-07-26 22:42:5233#include "net/base/load_flags.h"
[email protected]58e32bb2013-01-21 18:23:2534#include "net/base/load_timing_info.h"
35#include "net/base/load_timing_info_test_util.h"
[email protected]d8eb84242010-09-25 02:25:0636#include "net/base/net_errors.h"
[email protected]9e743cd2010-03-16 07:03:5337#include "net/base/net_log.h"
38#include "net/base/net_log_unittest.h"
initial.commit586acc5fe2008-07-26 22:42:5239#include "net/base/net_module.h"
40#include "net/base/net_util.h"
[email protected]2ca01e52013-10-31 22:05:1941#include "net/base/request_priority.h"
[email protected]42fdb452012-11-01 12:44:4042#include "net/base/test_data_directory.h"
[email protected]f288ef02012-12-15 20:28:2843#include "net/base/upload_bytes_element_reader.h"
44#include "net/base/upload_data_stream.h"
45#include "net/base/upload_file_element_reader.h"
[email protected]6e7845ae2013-03-29 21:48:1146#include "net/cert/ev_root_ca_metadata.h"
[email protected]a8fed1742013-12-27 02:14:2447#include "net/cert/mock_cert_verifier.h"
[email protected]6e7845ae2013-03-29 21:48:1148#include "net/cert/test_root_certs.h"
[email protected]aa84a7e2012-03-15 21:29:0649#include "net/cookies/cookie_monster.h"
50#include "net/cookies/cookie_store_test_helpers.h"
initial.commit586acc5fe2008-07-26 22:42:5251#include "net/disk_cache/disk_cache.h"
[email protected]f2cb3cf2013-03-21 01:40:5352#include "net/dns/mock_host_resolver.h"
[email protected]ba2f3342009-07-30 18:08:4253#include "net/ftp/ftp_network_layer.h"
[email protected]b7572ea2013-11-26 20:16:3854#include "net/http/http_byte_range.h"
initial.commit586acc5fe2008-07-26 22:42:5255#include "net/http/http_cache.h"
56#include "net/http/http_network_layer.h"
[email protected]c3456bb2011-12-12 22:22:1957#include "net/http/http_network_session.h"
[email protected]88e6b6f32010-05-07 23:14:2558#include "net/http/http_request_headers.h"
[email protected]319d9e6f2009-02-18 19:47:2159#include "net/http/http_response_headers.h"
[email protected]e50efea2014-03-24 18:41:0060#include "net/http/http_util.h"
[email protected]dffe8242012-03-20 15:14:2761#include "net/ocsp/nss_ocsp.h"
[email protected]63de95b2008-12-10 04:11:2762#include "net/proxy/proxy_service.h"
[email protected]c3456bb2011-12-12 22:22:1963#include "net/socket/ssl_client_socket.h"
[email protected]536fd0b2013-03-14 17:41:5764#include "net/ssl/ssl_connection_status_flags.h"
[email protected]6e7845ae2013-03-29 21:48:1165#include "net/test/cert_test_util.h"
[email protected]89b32522013-05-07 20:04:2166#include "net/test/spawned_test_server/spawned_test_server.h"
[email protected]e0f35c92013-05-08 16:04:3467#include "net/url_request/data_protocol_handler.h"
68#include "net/url_request/file_protocol_handler.h"
[email protected]d2a133182012-08-05 16:44:0869#include "net/url_request/ftp_protocol_handler.h"
[email protected]ee4c30d2012-11-07 15:08:4370#include "net/url_request/static_http_user_agent_settings.h"
initial.commit586acc5fe2008-07-26 22:42:5271#include "net/url_request/url_request.h"
[email protected]7886a8c2009-08-21 04:11:0972#include "net/url_request/url_request_file_dir_job.h"
[email protected]bcb84f8b2009-08-31 16:20:1473#include "net/url_request/url_request_http_job.h"
[email protected]9d5730b2012-08-24 17:42:4974#include "net/url_request/url_request_job_factory_impl.h"
[email protected]3c5ca8c2011-09-29 01:14:5175#include "net/url_request/url_request_redirect_job.h"
[email protected]a5c713f2009-04-16 21:05:4776#include "net/url_request/url_request_test_job.h"
[email protected]d2db0292011-01-26 20:23:4477#include "net/url_request/url_request_test_util.h"
initial.commit586acc5fe2008-07-26 22:42:5278#include "testing/gtest/include/gtest/gtest.h"
[email protected]23887f04f2008-12-02 19:20:1579#include "testing/platform_test.h"
initial.commit586acc5fe2008-07-26 22:42:5280
[email protected]dffe8242012-03-20 15:14:2781#if defined(OS_WIN)
[email protected]451fd902012-10-03 17:14:4882#include "base/win/scoped_com_initializer.h"
[email protected]aed9efb2013-04-13 01:20:5683#include "base/win/scoped_comptr.h"
[email protected]dffe8242012-03-20 15:14:2784#include "base/win/windows_version.h"
85#endif
86
[email protected]ad65a3e2013-12-25 18:18:0187using base::ASCIIToUTF16;
[email protected]e1acf6f2008-10-27 20:43:3388using base::Time;
89
[email protected]7461a402011-03-24 23:19:5190namespace net {
91
initial.commit586acc5fe2008-07-26 22:42:5292namespace {
93
[email protected]42cba2fb2013-03-29 19:58:5794const base::string16 kChrome(ASCIIToUTF16("chrome"));
95const base::string16 kSecret(ASCIIToUTF16("secret"));
96const base::string16 kUser(ASCIIToUTF16("user"));
[email protected]13c8a092010-07-29 06:15:4497
[email protected]2bba3252013-04-08 19:50:5998// Tests load timing information in the case a fresh connection was used, with
99// no proxy.
[email protected]58e32bb2013-01-21 18:23:25100void TestLoadTimingNotReused(const net::LoadTimingInfo& load_timing_info,
101 int connect_timing_flags) {
102 EXPECT_FALSE(load_timing_info.socket_reused);
103 EXPECT_NE(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
104
105 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
106 EXPECT_FALSE(load_timing_info.request_start.is_null());
107
108 EXPECT_LE(load_timing_info.request_start,
109 load_timing_info.connect_timing.connect_start);
110 ExpectConnectTimingHasTimes(load_timing_info.connect_timing,
111 connect_timing_flags);
112 EXPECT_LE(load_timing_info.connect_timing.connect_end,
113 load_timing_info.send_start);
114 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
115 EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
116
[email protected]58e32bb2013-01-21 18:23:25117 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null());
118 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null());
119}
120
[email protected]2bba3252013-04-08 19:50:59121// Same as above, but with proxy times.
122void TestLoadTimingNotReusedWithProxy(
123 const net::LoadTimingInfo& load_timing_info,
124 int connect_timing_flags) {
125 EXPECT_FALSE(load_timing_info.socket_reused);
126 EXPECT_NE(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
127
128 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
129 EXPECT_FALSE(load_timing_info.request_start.is_null());
130
131 EXPECT_LE(load_timing_info.request_start,
132 load_timing_info.proxy_resolve_start);
133 EXPECT_LE(load_timing_info.proxy_resolve_start,
134 load_timing_info.proxy_resolve_end);
135 EXPECT_LE(load_timing_info.proxy_resolve_end,
136 load_timing_info.connect_timing.connect_start);
137 ExpectConnectTimingHasTimes(load_timing_info.connect_timing,
138 connect_timing_flags);
139 EXPECT_LE(load_timing_info.connect_timing.connect_end,
140 load_timing_info.send_start);
141 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
142 EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
143}
144
145// Same as above, but with a reused socket and proxy times.
146void TestLoadTimingReusedWithProxy(
147 const net::LoadTimingInfo& load_timing_info) {
148 EXPECT_TRUE(load_timing_info.socket_reused);
149 EXPECT_NE(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
150
151 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
152 EXPECT_FALSE(load_timing_info.request_start.is_null());
153
154 ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing);
155
156 EXPECT_LE(load_timing_info.request_start,
157 load_timing_info.proxy_resolve_start);
158 EXPECT_LE(load_timing_info.proxy_resolve_start,
159 load_timing_info.proxy_resolve_end);
160 EXPECT_LE(load_timing_info.proxy_resolve_end,
161 load_timing_info.send_start);
162 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
163 EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
164}
165
[email protected]3b23a222013-05-15 21:33:25166// Tests load timing information in the case of a cache hit, when no cache
167// validation request was sent over the wire.
[email protected]e3a85452013-11-14 01:46:17168base::StringPiece TestNetResourceProvider(int key) {
169 return "header";
170}
171
172void FillBuffer(char* buffer, size_t len) {
173 static bool called = false;
174 if (!called) {
175 called = true;
176 int seed = static_cast<int>(Time::Now().ToInternalValue());
177 srand(seed);
178 }
179
180 for (size_t i = 0; i < len; i++) {
181 buffer[i] = static_cast<char>(rand());
182 if (!buffer[i])
183 buffer[i] = 'g';
184 }
185}
186
187#if !defined(OS_IOS)
[email protected]3b23a222013-05-15 21:33:25188void TestLoadTimingCacheHitNoNetwork(
189 const net::LoadTimingInfo& load_timing_info) {
190 EXPECT_FALSE(load_timing_info.socket_reused);
191 EXPECT_EQ(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
192
193 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
194 EXPECT_FALSE(load_timing_info.request_start.is_null());
195
196 ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing);
197 EXPECT_LE(load_timing_info.request_start, load_timing_info.send_start);
198 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
199 EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
200
201 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null());
202 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null());
203}
204
205// Tests load timing in the case that there is no HTTP response. This can be
206// used to test in the case of errors or non-HTTP requests.
207void TestLoadTimingNoHttpResponse(
[email protected]58e32bb2013-01-21 18:23:25208 const net::LoadTimingInfo& load_timing_info) {
209 EXPECT_FALSE(load_timing_info.socket_reused);
210 EXPECT_EQ(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
211
212 // Only the request times should be non-null.
213 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
214 EXPECT_FALSE(load_timing_info.request_start.is_null());
215
216 ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing);
217
218 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null());
219 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null());
220 EXPECT_TRUE(load_timing_info.send_start.is_null());
221 EXPECT_TRUE(load_timing_info.send_end.is_null());
222 EXPECT_TRUE(load_timing_info.receive_headers_end.is_null());
223}
224
[email protected]71c64f62008-11-15 04:36:51225// Do a case-insensitive search through |haystack| for |needle|.
226bool ContainsString(const std::string& haystack, const char* needle) {
227 std::string::const_iterator it =
228 std::search(haystack.begin(),
229 haystack.end(),
230 needle,
231 needle + strlen(needle),
[email protected]07f1cee2010-11-03 03:53:35232 base::CaseInsensitiveCompare<char>());
[email protected]71c64f62008-11-15 04:36:51233 return it != haystack.end();
234}
235
[email protected]f288ef02012-12-15 20:28:28236UploadDataStream* CreateSimpleUploadData(const char* data) {
237 scoped_ptr<UploadElementReader> reader(
238 new UploadBytesElementReader(data, strlen(data)));
239 return UploadDataStream::CreateWithReader(reader.Pass(), 0);
[email protected]195e77d2009-07-23 19:10:23240}
241
[email protected]96adadb2010-08-28 01:16:17242// Verify that the SSLInfo of a successful SSL connection has valid values.
[email protected]7461a402011-03-24 23:19:51243void CheckSSLInfo(const SSLInfo& ssl_info) {
[email protected]96adadb2010-08-28 01:16:17244 // -1 means unknown. 0 means no encryption.
245 EXPECT_GT(ssl_info.security_bits, 0);
246
247 // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated.
[email protected]7461a402011-03-24 23:19:51248 int cipher_suite = SSLConnectionStatusToCipherSuite(
[email protected]96adadb2010-08-28 01:16:17249 ssl_info.connection_status);
250 EXPECT_NE(0, cipher_suite);
251}
252
[email protected]79e1fd62013-06-20 06:50:04253void CheckFullRequestHeaders(const HttpRequestHeaders& headers,
254 const GURL& host_url) {
255 std::string sent_value;
256
257 EXPECT_TRUE(headers.GetHeader("Host", &sent_value));
258 EXPECT_EQ(GetHostAndOptionalPort(host_url), sent_value);
259
260 EXPECT_TRUE(headers.GetHeader("Connection", &sent_value));
261 EXPECT_EQ("keep-alive", sent_value);
262}
263
[email protected]ede03212012-09-07 12:52:26264bool FingerprintsEqual(const HashValueVector& a, const HashValueVector& b) {
[email protected]69d7ff442012-02-13 22:41:27265 size_t size = a.size();
266
267 if (size != b.size())
268 return false;
269
270 for (size_t i = 0; i < size; ++i) {
271 if (!a[i].Equals(b[i]))
272 return false;
273 }
274
275 return true;
276}
[email protected]e3a85452013-11-14 01:46:17277#endif // !defined(OS_IOS)
[email protected]69d7ff442012-02-13 22:41:27278
[email protected]dc5a5cf2012-09-26 02:49:30279// A network delegate that allows the user to choose a subset of request stages
280// to block in. When blocking, the delegate can do one of the following:
281// * synchronously return a pre-specified error code, or
282// * asynchronously return that value via an automatically called callback,
283// or
284// * block and wait for the user to do a callback.
285// Additionally, the user may also specify a redirect URL -- then each request
286// with the current URL different from the redirect target will be redirected
287// to that target, in the on-before-URL-request stage, independent of whether
288// the delegate blocks in ON_BEFORE_URL_REQUEST or not.
[email protected]4c76d7c2011-04-15 19:14:12289class BlockingNetworkDelegate : public TestNetworkDelegate {
290 public:
[email protected]dc5a5cf2012-09-26 02:49:30291 // Stages in which the delegate can block.
292 enum Stage {
[email protected]9045b8822012-01-13 20:35:35293 NOT_BLOCKED = 0,
294 ON_BEFORE_URL_REQUEST = 1 << 0,
295 ON_BEFORE_SEND_HEADERS = 1 << 1,
296 ON_HEADERS_RECEIVED = 1 << 2,
297 ON_AUTH_REQUIRED = 1 << 3
298 };
299
[email protected]dc5a5cf2012-09-26 02:49:30300 // Behavior during blocked stages. During other stages, just
301 // returns net::OK or NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION.
302 enum BlockMode {
303 SYNCHRONOUS, // No callback, returns specified return values.
304 AUTO_CALLBACK, // |this| posts a task to run the callback using the
305 // specified return codes.
306 USER_CALLBACK, // User takes care of doing a callback. |retval_| and
307 // |auth_retval_| are ignored. In every blocking stage the
308 // message loop is quit.
309 };
310
311 // Creates a delegate which does not block at all.
312 explicit BlockingNetworkDelegate(BlockMode block_mode);
313
314 // For users to trigger a callback returning |response|.
315 // Side-effects: resets |stage_blocked_for_callback_| and stored callbacks.
316 // Only call if |block_mode_| == USER_CALLBACK.
317 void DoCallback(int response);
318 void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response);
319
320 // Setters.
321 void set_retval(int retval) {
322 ASSERT_NE(USER_CALLBACK, block_mode_);
323 ASSERT_NE(ERR_IO_PENDING, retval);
324 ASSERT_NE(OK, retval);
325 retval_ = retval;
[email protected]9045b8822012-01-13 20:35:35326 }
327
[email protected]dc5a5cf2012-09-26 02:49:30328 // If |auth_retval| == AUTH_REQUIRED_RESPONSE_SET_AUTH, then
329 // |auth_credentials_| will be passed with the response.
330 void set_auth_retval(AuthRequiredResponse auth_retval) {
331 ASSERT_NE(USER_CALLBACK, block_mode_);
332 ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, auth_retval);
333 auth_retval_ = auth_retval;
334 }
335 void set_auth_credentials(const AuthCredentials& auth_credentials) {
336 auth_credentials_ = auth_credentials;
[email protected]9045b8822012-01-13 20:35:35337 }
338
[email protected]dc5a5cf2012-09-26 02:49:30339 void set_redirect_url(const GURL& url) {
340 redirect_url_ = url;
[email protected]9045b8822012-01-13 20:35:35341 }
342
[email protected]dc5a5cf2012-09-26 02:49:30343 void set_block_on(int block_on) {
344 block_on_ = block_on;
[email protected]9045b8822012-01-13 20:35:35345 }
346
[email protected]dc5a5cf2012-09-26 02:49:30347 // Allows the user to check in which state did we block.
348 Stage stage_blocked_for_callback() const {
349 EXPECT_EQ(USER_CALLBACK, block_mode_);
350 return stage_blocked_for_callback_;
[email protected]9045b8822012-01-13 20:35:35351 }
352
353 private:
[email protected]dc5a5cf2012-09-26 02:49:30354 void RunCallback(int response, const CompletionCallback& callback);
355 void RunAuthCallback(AuthRequiredResponse response,
356 const AuthCallback& callback);
357
[email protected]9045b8822012-01-13 20:35:35358 // TestNetworkDelegate implementation.
359 virtual int OnBeforeURLRequest(URLRequest* request,
360 const CompletionCallback& callback,
[email protected]dc5a5cf2012-09-26 02:49:30361 GURL* new_url) OVERRIDE;
[email protected]9045b8822012-01-13 20:35:35362
363 virtual int OnBeforeSendHeaders(URLRequest* request,
364 const CompletionCallback& callback,
[email protected]dc5a5cf2012-09-26 02:49:30365 HttpRequestHeaders* headers) OVERRIDE;
[email protected]9045b8822012-01-13 20:35:35366
367 virtual int OnHeadersReceived(
368 URLRequest* request,
369 const CompletionCallback& callback,
[email protected]507af8f2012-10-20 00:42:32370 const HttpResponseHeaders* original_response_headers,
[email protected]dc5a5cf2012-09-26 02:49:30371 scoped_refptr<HttpResponseHeaders>* override_response_headers) OVERRIDE;
[email protected]9045b8822012-01-13 20:35:35372
373 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired(
374 URLRequest* request,
375 const AuthChallengeInfo& auth_info,
376 const AuthCallback& callback,
[email protected]dc5a5cf2012-09-26 02:49:30377 AuthCredentials* credentials) OVERRIDE;
[email protected]9045b8822012-01-13 20:35:35378
[email protected]dc5a5cf2012-09-26 02:49:30379 // Resets the callbacks and |stage_blocked_for_callback_|.
380 void Reset();
[email protected]9045b8822012-01-13 20:35:35381
[email protected]dc5a5cf2012-09-26 02:49:30382 // Checks whether we should block in |stage|. If yes, returns an error code
383 // and optionally sets up callback based on |block_mode_|. If no, returns OK.
384 int MaybeBlockStage(Stage stage, const CompletionCallback& callback);
385
386 // Configuration parameters, can be adjusted by public methods:
387 const BlockMode block_mode_;
388
389 // Values returned on blocking stages when mode is SYNCHRONOUS or
390 // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING.
391 int retval_; // To be returned in non-auth stages.
392 AuthRequiredResponse auth_retval_;
393
394 GURL redirect_url_; // Used if non-empty.
395 int block_on_; // Bit mask: in which stages to block.
396
397 // |auth_credentials_| will be copied to |*target_auth_credential_| on
398 // callback.
399 AuthCredentials auth_credentials_;
400 AuthCredentials* target_auth_credentials_;
401
402 // Internal variables, not set by not the user:
403 // Last blocked stage waiting for user callback (unused if |block_mode_| !=
404 // USER_CALLBACK).
405 Stage stage_blocked_for_callback_;
406
407 // Callback objects stored during blocking stages.
[email protected]9045b8822012-01-13 20:35:35408 CompletionCallback callback_;
409 AuthCallback auth_callback_;
[email protected]dc5a5cf2012-09-26 02:49:30410
411 base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_;
412
413 DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate);
[email protected]9045b8822012-01-13 20:35:35414};
415
[email protected]dc5a5cf2012-09-26 02:49:30416BlockingNetworkDelegate::BlockingNetworkDelegate(BlockMode block_mode)
417 : block_mode_(block_mode),
418 retval_(OK),
419 auth_retval_(AUTH_REQUIRED_RESPONSE_NO_ACTION),
420 block_on_(0),
421 target_auth_credentials_(NULL),
422 stage_blocked_for_callback_(NOT_BLOCKED),
[email protected]aa249b52013-04-30 01:04:32423 weak_factory_(this) {
[email protected]dc5a5cf2012-09-26 02:49:30424}
425
426void BlockingNetworkDelegate::DoCallback(int response) {
427 ASSERT_EQ(USER_CALLBACK, block_mode_);
428 ASSERT_NE(NOT_BLOCKED, stage_blocked_for_callback_);
429 ASSERT_NE(ON_AUTH_REQUIRED, stage_blocked_for_callback_);
430 CompletionCallback callback = callback_;
431 Reset();
432 RunCallback(response, callback);
433}
434
435void BlockingNetworkDelegate::DoAuthCallback(
436 NetworkDelegate::AuthRequiredResponse response) {
437 ASSERT_EQ(USER_CALLBACK, block_mode_);
438 ASSERT_EQ(ON_AUTH_REQUIRED, stage_blocked_for_callback_);
439 AuthCallback auth_callback = auth_callback_;
440 Reset();
441 RunAuthCallback(response, auth_callback);
442}
443
444void BlockingNetworkDelegate::RunCallback(int response,
445 const CompletionCallback& callback) {
446 callback.Run(response);
447}
448
449void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response,
450 const AuthCallback& callback) {
451 if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) {
452 ASSERT_TRUE(target_auth_credentials_ != NULL);
453 *target_auth_credentials_ = auth_credentials_;
454 }
455 callback.Run(response);
456}
457
458int BlockingNetworkDelegate::OnBeforeURLRequest(
459 URLRequest* request,
460 const CompletionCallback& callback,
461 GURL* new_url) {
462 if (redirect_url_ == request->url())
463 return OK; // We've already seen this request and redirected elsewhere.
464
465 TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url);
466
467 if (!redirect_url_.is_empty())
468 *new_url = redirect_url_;
469
470 return MaybeBlockStage(ON_BEFORE_URL_REQUEST, callback);
471}
472
473int BlockingNetworkDelegate::OnBeforeSendHeaders(
474 URLRequest* request,
475 const CompletionCallback& callback,
476 HttpRequestHeaders* headers) {
477 TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers);
478
479 return MaybeBlockStage(ON_BEFORE_SEND_HEADERS, callback);
480}
481
482int BlockingNetworkDelegate::OnHeadersReceived(
483 URLRequest* request,
484 const CompletionCallback& callback,
[email protected]507af8f2012-10-20 00:42:32485 const HttpResponseHeaders* original_response_headers,
[email protected]dc5a5cf2012-09-26 02:49:30486 scoped_refptr<HttpResponseHeaders>* override_response_headers) {
487 TestNetworkDelegate::OnHeadersReceived(
488 request, callback, original_response_headers,
489 override_response_headers);
490
491 return MaybeBlockStage(ON_HEADERS_RECEIVED, callback);
492}
493
494NetworkDelegate::AuthRequiredResponse BlockingNetworkDelegate::OnAuthRequired(
495 URLRequest* request,
496 const AuthChallengeInfo& auth_info,
497 const AuthCallback& callback,
498 AuthCredentials* credentials) {
499 TestNetworkDelegate::OnAuthRequired(request, auth_info, callback,
500 credentials);
501 // Check that the user has provided callback for the previous blocked stage.
502 EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_);
503
504 if ((block_on_ & ON_AUTH_REQUIRED) == 0) {
505 return AUTH_REQUIRED_RESPONSE_NO_ACTION;
506 }
507
508 target_auth_credentials_ = credentials;
509
510 switch (block_mode_) {
511 case SYNCHRONOUS:
512 if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH)
513 *target_auth_credentials_ = auth_credentials_;
514 return auth_retval_;
515
516 case AUTO_CALLBACK:
[email protected]2da659e2013-05-23 20:51:34517 base::MessageLoop::current()->PostTask(
[email protected]dc5a5cf2012-09-26 02:49:30518 FROM_HERE,
519 base::Bind(&BlockingNetworkDelegate::RunAuthCallback,
520 weak_factory_.GetWeakPtr(), auth_retval_, callback));
521 return AUTH_REQUIRED_RESPONSE_IO_PENDING;
522
523 case USER_CALLBACK:
524 auth_callback_ = callback;
525 stage_blocked_for_callback_ = ON_AUTH_REQUIRED;
[email protected]2da659e2013-05-23 20:51:34526 base::MessageLoop::current()->PostTask(FROM_HERE,
527 base::MessageLoop::QuitClosure());
[email protected]dc5a5cf2012-09-26 02:49:30528 return AUTH_REQUIRED_RESPONSE_IO_PENDING;
529 }
530 NOTREACHED();
531 return AUTH_REQUIRED_RESPONSE_NO_ACTION; // Dummy value.
532}
533
534void BlockingNetworkDelegate::Reset() {
535 EXPECT_NE(NOT_BLOCKED, stage_blocked_for_callback_);
536 stage_blocked_for_callback_ = NOT_BLOCKED;
537 callback_.Reset();
538 auth_callback_.Reset();
539}
540
541int BlockingNetworkDelegate::MaybeBlockStage(
542 BlockingNetworkDelegate::Stage stage,
543 const CompletionCallback& callback) {
544 // Check that the user has provided callback for the previous blocked stage.
545 EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_);
546
547 if ((block_on_ & stage) == 0) {
548 return OK;
549 }
550
551 switch (block_mode_) {
552 case SYNCHRONOUS:
553 EXPECT_NE(OK, retval_);
554 return retval_;
555
556 case AUTO_CALLBACK:
[email protected]2da659e2013-05-23 20:51:34557 base::MessageLoop::current()->PostTask(
[email protected]dc5a5cf2012-09-26 02:49:30558 FROM_HERE,
559 base::Bind(&BlockingNetworkDelegate::RunCallback,
560 weak_factory_.GetWeakPtr(), retval_, callback));
561 return ERR_IO_PENDING;
562
563 case USER_CALLBACK:
564 callback_ = callback;
565 stage_blocked_for_callback_ = stage;
[email protected]2da659e2013-05-23 20:51:34566 base::MessageLoop::current()->PostTask(FROM_HERE,
567 base::MessageLoop::QuitClosure());
[email protected]dc5a5cf2012-09-26 02:49:30568 return ERR_IO_PENDING;
569 }
570 NOTREACHED();
571 return 0;
572}
573
[email protected]d5a4dd62012-05-23 01:41:04574class TestURLRequestContextWithProxy : public TestURLRequestContext {
575 public:
576 // Does not own |delegate|.
577 TestURLRequestContextWithProxy(const std::string& proxy,
578 NetworkDelegate* delegate)
579 : TestURLRequestContext(true) {
580 context_storage_.set_proxy_service(ProxyService::CreateFixed(proxy));
581 set_network_delegate(delegate);
582 Init();
583 }
584 virtual ~TestURLRequestContextWithProxy() {}
585};
586
587} // namespace
588
[email protected]a592c0432012-12-01 18:10:29589// Inherit PlatformTest since we require the autorelease pool on Mac OS X.
[email protected]7a0bb4bf2008-11-19 21:41:48590class URLRequestTest : public PlatformTest {
[email protected]abb26092010-11-11 22:19:00591 public:
[email protected]ef2bf422012-05-11 03:27:09592 URLRequestTest() : default_context_(true) {
593 default_context_.set_network_delegate(&default_network_delegate_);
[email protected]58e32bb2013-01-21 18:23:25594 default_context_.set_net_log(&net_log_);
[email protected]e0f35c92013-05-08 16:04:34595 job_factory_.SetProtocolHandler("data", new DataProtocolHandler);
[email protected]255620da2013-08-19 13:14:29596 job_factory_.SetProtocolHandler(
597 "file", new FileProtocolHandler(base::MessageLoopProxy::current()));
[email protected]e0f35c92013-05-08 16:04:34598 default_context_.set_job_factory(&job_factory_);
[email protected]ef2bf422012-05-11 03:27:09599 default_context_.Init();
[email protected]87a09a92011-07-14 15:50:50600 }
[email protected]e4034ad2013-09-20 08:36:18601 virtual ~URLRequestTest() {
602 // URLRequestJobs may post clean-up tasks on destruction.
603 base::RunLoop().RunUntilIdle();
604 }
[email protected]87a09a92011-07-14 15:50:50605
[email protected]3c5ca8c2011-09-29 01:14:51606 // Adds the TestJobInterceptor to the default context.
607 TestJobInterceptor* AddTestInterceptor() {
[email protected]f53b4802012-12-20 17:04:23608 TestJobInterceptor* protocol_handler_ = new TestJobInterceptor();
[email protected]e0f35c92013-05-08 16:04:34609 job_factory_.SetProtocolHandler("http", NULL);
610 job_factory_.SetProtocolHandler("http", protocol_handler_);
[email protected]f53b4802012-12-20 17:04:23611 return protocol_handler_;
[email protected]3c5ca8c2011-09-29 01:14:51612 }
613
[email protected]87a09a92011-07-14 15:50:50614 protected:
[email protected]58e32bb2013-01-21 18:23:25615 CapturingNetLog net_log_;
[email protected]ceefd7fd2012-11-29 00:36:24616 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest.
[email protected]e0f35c92013-05-08 16:04:34617 URLRequestJobFactoryImpl job_factory_;
[email protected]ef2bf422012-05-11 03:27:09618 TestURLRequestContext default_context_;
[email protected]7a0bb4bf2008-11-19 21:41:48619};
620
[email protected]316c1e5e2012-09-12 15:17:44621TEST_F(URLRequestTest, AboutBlankTest) {
622 TestDelegate d;
623 {
[email protected]2ca01e52013-10-31 22:05:19624 URLRequest r(GURL("about:blank"), DEFAULT_PRIORITY, &d, &default_context_);
[email protected]316c1e5e2012-09-12 15:17:44625
626 r.Start();
627 EXPECT_TRUE(r.is_pending());
628
[email protected]255620da2013-08-19 13:14:29629 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:44630
631 EXPECT_TRUE(!r.is_pending());
632 EXPECT_FALSE(d.received_data_before_response());
633 EXPECT_EQ(d.bytes_received(), 0);
634 EXPECT_EQ("", r.GetSocketAddress().host());
635 EXPECT_EQ(0, r.GetSocketAddress().port());
[email protected]79e1fd62013-06-20 06:50:04636
637 HttpRequestHeaders headers;
638 EXPECT_FALSE(r.GetFullRequestHeaders(&headers));
[email protected]316c1e5e2012-09-12 15:17:44639 }
640}
641
642TEST_F(URLRequestTest, DataURLImageTest) {
643 TestDelegate d;
644 {
645 // Use our nice little Chrome logo.
[email protected]2ca01e52013-10-31 22:05:19646 URLRequest r(
647 GURL(
[email protected]316c1e5e2012-09-12 15:17:44648 "data:image/png;base64,"
649 "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUBBjG3"
650 "w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQxIEVxitD"
651 "5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI5MpCVdW0gO7t"
652 "vNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKzMx1+fg9bAgK6zHq9"
653 "cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7CkIMDxQpF7r/MWq12UctI1"
654 "dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0WYUqt57yWf2McHTObYPbVD+Z"
655 "wbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2HQQcm43XwmtoYM6vVKleh0uoWvnzW"
656 "3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nkDFHF9ZS+uYVjRUasMeHUmyLYtcklTvzW"
657 "GFZnNOXczThvpKIzjcahSqIzkvDLayDq6D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTb"
658 "xXX1T5xsV9tm9r4TQwHLiZw/pdDZJea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5"
659 "MrVvMzNaI3+ERHfrFzPKQukrQGI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gV"
660 "Md6q5c8GdosynKmSeRuGzpjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq"
661 "/uxxZKzNiZFGD0wRC3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7F"
662 "Ux1qLnV7MGF40smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufB"
663 "JbsysjjW4kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJM"
664 "nlm2O34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfm"
665 "moRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV5En"
666 "hORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="),
[email protected]2ca01e52013-10-31 22:05:19667 DEFAULT_PRIORITY,
[email protected]316c1e5e2012-09-12 15:17:44668 &d,
669 &default_context_);
670
671 r.Start();
672 EXPECT_TRUE(r.is_pending());
673
[email protected]255620da2013-08-19 13:14:29674 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:44675
676 EXPECT_TRUE(!r.is_pending());
677 EXPECT_FALSE(d.received_data_before_response());
678 EXPECT_EQ(d.bytes_received(), 911);
679 EXPECT_EQ("", r.GetSocketAddress().host());
680 EXPECT_EQ(0, r.GetSocketAddress().port());
[email protected]79e1fd62013-06-20 06:50:04681
682 HttpRequestHeaders headers;
683 EXPECT_FALSE(r.GetFullRequestHeaders(&headers));
[email protected]316c1e5e2012-09-12 15:17:44684 }
685}
686
687TEST_F(URLRequestTest, FileTest) {
[email protected]6cdfd7f2013-02-08 20:40:15688 base::FilePath app_path;
[email protected]316c1e5e2012-09-12 15:17:44689 PathService::Get(base::FILE_EXE, &app_path);
690 GURL app_url = FilePathToFileURL(app_path);
691
692 TestDelegate d;
693 {
[email protected]2ca01e52013-10-31 22:05:19694 URLRequest r(app_url, DEFAULT_PRIORITY, &d, &default_context_);
[email protected]316c1e5e2012-09-12 15:17:44695
696 r.Start();
697 EXPECT_TRUE(r.is_pending());
698
[email protected]255620da2013-08-19 13:14:29699 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:44700
701 int64 file_size = -1;
[email protected]56285702013-12-04 18:22:49702 EXPECT_TRUE(base::GetFileSize(app_path, &file_size));
[email protected]316c1e5e2012-09-12 15:17:44703
704 EXPECT_TRUE(!r.is_pending());
705 EXPECT_EQ(1, d.response_started_count());
706 EXPECT_FALSE(d.received_data_before_response());
707 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
708 EXPECT_EQ("", r.GetSocketAddress().host());
709 EXPECT_EQ(0, r.GetSocketAddress().port());
[email protected]79e1fd62013-06-20 06:50:04710
711 HttpRequestHeaders headers;
712 EXPECT_FALSE(r.GetFullRequestHeaders(&headers));
[email protected]316c1e5e2012-09-12 15:17:44713 }
714}
715
[email protected]ba40bb762012-12-17 07:11:04716TEST_F(URLRequestTest, FileTestCancel) {
[email protected]6cdfd7f2013-02-08 20:40:15717 base::FilePath app_path;
[email protected]ba40bb762012-12-17 07:11:04718 PathService::Get(base::FILE_EXE, &app_path);
719 GURL app_url = FilePathToFileURL(app_path);
720
721 TestDelegate d;
722 {
[email protected]2ca01e52013-10-31 22:05:19723 URLRequest r(app_url, DEFAULT_PRIORITY, &d, &default_context_);
[email protected]ba40bb762012-12-17 07:11:04724
725 r.Start();
726 EXPECT_TRUE(r.is_pending());
727 r.Cancel();
728 }
[email protected]79e1fd62013-06-20 06:50:04729 // Async cancellation should be safe even when URLRequest has been already
[email protected]ba40bb762012-12-17 07:11:04730 // destroyed.
[email protected]255620da2013-08-19 13:14:29731 base::RunLoop().RunUntilIdle();
[email protected]ba40bb762012-12-17 07:11:04732}
733
[email protected]316c1e5e2012-09-12 15:17:44734TEST_F(URLRequestTest, FileTestFullSpecifiedRange) {
735 const size_t buffer_size = 4000;
[email protected]4356f0f2013-04-07 00:58:17736 scoped_ptr<char[]> buffer(new char[buffer_size]);
[email protected]316c1e5e2012-09-12 15:17:44737 FillBuffer(buffer.get(), buffer_size);
738
[email protected]6cdfd7f2013-02-08 20:40:15739 base::FilePath temp_path;
[email protected]03d9afc02013-12-03 17:55:52740 EXPECT_TRUE(base::CreateTemporaryFile(&temp_path));
[email protected]316c1e5e2012-09-12 15:17:44741 GURL temp_url = FilePathToFileURL(temp_path);
[email protected]e5c2a22e2014-03-06 20:42:30742 EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size));
[email protected]316c1e5e2012-09-12 15:17:44743
744 int64 file_size;
[email protected]56285702013-12-04 18:22:49745 EXPECT_TRUE(base::GetFileSize(temp_path, &file_size));
[email protected]316c1e5e2012-09-12 15:17:44746
747 const size_t first_byte_position = 500;
748 const size_t last_byte_position = buffer_size - first_byte_position;
749 const size_t content_length = last_byte_position - first_byte_position + 1;
750 std::string partial_buffer_string(buffer.get() + first_byte_position,
751 buffer.get() + last_byte_position + 1);
752
753 TestDelegate d;
754 {
[email protected]2ca01e52013-10-31 22:05:19755 URLRequest r(temp_url, DEFAULT_PRIORITY, &d, &default_context_);
[email protected]316c1e5e2012-09-12 15:17:44756
757 HttpRequestHeaders headers;
[email protected]b7572ea2013-11-26 20:16:38758 headers.SetHeader(
759 HttpRequestHeaders::kRange,
760 net::HttpByteRange::Bounded(
761 first_byte_position, last_byte_position).GetHeaderValue());
[email protected]316c1e5e2012-09-12 15:17:44762 r.SetExtraRequestHeaders(headers);
763 r.Start();
764 EXPECT_TRUE(r.is_pending());
765
[email protected]255620da2013-08-19 13:14:29766 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:44767 EXPECT_TRUE(!r.is_pending());
768 EXPECT_EQ(1, d.response_started_count());
769 EXPECT_FALSE(d.received_data_before_response());
770 EXPECT_EQ(static_cast<int>(content_length), d.bytes_received());
771 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
772 EXPECT_TRUE(partial_buffer_string == d.data_received());
773 }
774
[email protected]dd3aa792013-07-16 19:10:23775 EXPECT_TRUE(base::DeleteFile(temp_path, false));
[email protected]316c1e5e2012-09-12 15:17:44776}
777
778TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) {
779 const size_t buffer_size = 4000;
[email protected]4356f0f2013-04-07 00:58:17780 scoped_ptr<char[]> buffer(new char[buffer_size]);
[email protected]316c1e5e2012-09-12 15:17:44781 FillBuffer(buffer.get(), buffer_size);
782
[email protected]6cdfd7f2013-02-08 20:40:15783 base::FilePath temp_path;
[email protected]03d9afc02013-12-03 17:55:52784 EXPECT_TRUE(base::CreateTemporaryFile(&temp_path));
[email protected]316c1e5e2012-09-12 15:17:44785 GURL temp_url = FilePathToFileURL(temp_path);
[email protected]e5c2a22e2014-03-06 20:42:30786 EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size));
[email protected]316c1e5e2012-09-12 15:17:44787
788 int64 file_size;
[email protected]56285702013-12-04 18:22:49789 EXPECT_TRUE(base::GetFileSize(temp_path, &file_size));
[email protected]316c1e5e2012-09-12 15:17:44790
791 const size_t first_byte_position = 500;
792 const size_t last_byte_position = buffer_size - 1;
793 const size_t content_length = last_byte_position - first_byte_position + 1;
794 std::string partial_buffer_string(buffer.get() + first_byte_position,
795 buffer.get() + last_byte_position + 1);
796
797 TestDelegate d;
798 {
[email protected]2ca01e52013-10-31 22:05:19799 URLRequest r(temp_url, DEFAULT_PRIORITY, &d, &default_context_);
[email protected]316c1e5e2012-09-12 15:17:44800
801 HttpRequestHeaders headers;
802 headers.SetHeader(HttpRequestHeaders::kRange,
[email protected]b7572ea2013-11-26 20:16:38803 net::HttpByteRange::RightUnbounded(
804 first_byte_position).GetHeaderValue());
[email protected]316c1e5e2012-09-12 15:17:44805 r.SetExtraRequestHeaders(headers);
806 r.Start();
807 EXPECT_TRUE(r.is_pending());
808
[email protected]255620da2013-08-19 13:14:29809 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:44810 EXPECT_TRUE(!r.is_pending());
811 EXPECT_EQ(1, d.response_started_count());
812 EXPECT_FALSE(d.received_data_before_response());
813 EXPECT_EQ(static_cast<int>(content_length), d.bytes_received());
814 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
815 EXPECT_TRUE(partial_buffer_string == d.data_received());
816 }
817
[email protected]dd3aa792013-07-16 19:10:23818 EXPECT_TRUE(base::DeleteFile(temp_path, false));
[email protected]316c1e5e2012-09-12 15:17:44819}
820
821TEST_F(URLRequestTest, FileTestMultipleRanges) {
822 const size_t buffer_size = 400000;
[email protected]4356f0f2013-04-07 00:58:17823 scoped_ptr<char[]> buffer(new char[buffer_size]);
[email protected]316c1e5e2012-09-12 15:17:44824 FillBuffer(buffer.get(), buffer_size);
825
[email protected]6cdfd7f2013-02-08 20:40:15826 base::FilePath temp_path;
[email protected]03d9afc02013-12-03 17:55:52827 EXPECT_TRUE(base::CreateTemporaryFile(&temp_path));
[email protected]316c1e5e2012-09-12 15:17:44828 GURL temp_url = FilePathToFileURL(temp_path);
[email protected]e5c2a22e2014-03-06 20:42:30829 EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size));
[email protected]316c1e5e2012-09-12 15:17:44830
831 int64 file_size;
[email protected]56285702013-12-04 18:22:49832 EXPECT_TRUE(base::GetFileSize(temp_path, &file_size));
[email protected]316c1e5e2012-09-12 15:17:44833
834 TestDelegate d;
835 {
[email protected]2ca01e52013-10-31 22:05:19836 URLRequest r(temp_url, DEFAULT_PRIORITY, &d, &default_context_);
[email protected]316c1e5e2012-09-12 15:17:44837
838 HttpRequestHeaders headers;
[email protected]b7572ea2013-11-26 20:16:38839 headers.SetHeader(HttpRequestHeaders::kRange, "bytes=0-0,10-200,200-300");
[email protected]316c1e5e2012-09-12 15:17:44840 r.SetExtraRequestHeaders(headers);
841 r.Start();
842 EXPECT_TRUE(r.is_pending());
843
[email protected]255620da2013-08-19 13:14:29844 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:44845 EXPECT_TRUE(d.request_failed());
846 }
847
[email protected]dd3aa792013-07-16 19:10:23848 EXPECT_TRUE(base::DeleteFile(temp_path, false));
[email protected]316c1e5e2012-09-12 15:17:44849}
850
[email protected]3ca8b362013-11-11 22:18:07851TEST_F(URLRequestTest, AllowFileURLs) {
852 base::ScopedTempDir temp_dir;
853 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
854 base::FilePath test_file;
[email protected]03d9afc02013-12-03 17:55:52855 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &test_file));
[email protected]3ca8b362013-11-11 22:18:07856 std::string test_data("monkey");
[email protected]e5c2a22e2014-03-06 20:42:30857 base::WriteFile(test_file, test_data.data(), test_data.size());
[email protected]3ca8b362013-11-11 22:18:07858 GURL test_file_url = net::FilePathToFileURL(test_file);
859
860 {
861 TestDelegate d;
862 TestNetworkDelegate network_delegate;
863 network_delegate.set_can_access_files(true);
864 default_context_.set_network_delegate(&network_delegate);
865 URLRequest r(test_file_url, DEFAULT_PRIORITY, &d, &default_context_);
866 r.Start();
867 base::RunLoop().Run();
868 EXPECT_FALSE(d.request_failed());
869 EXPECT_EQ(test_data, d.data_received());
870 }
871
872 {
873 TestDelegate d;
874 TestNetworkDelegate network_delegate;
875 network_delegate.set_can_access_files(false);
876 default_context_.set_network_delegate(&network_delegate);
877 URLRequest r(test_file_url, DEFAULT_PRIORITY, &d, &default_context_);
878 r.Start();
879 base::RunLoop().Run();
880 EXPECT_TRUE(d.request_failed());
881 EXPECT_EQ("", d.data_received());
882 }
883}
884
[email protected]316c1e5e2012-09-12 15:17:44885TEST_F(URLRequestTest, InvalidUrlTest) {
886 TestDelegate d;
887 {
[email protected]2ca01e52013-10-31 22:05:19888 URLRequest r(GURL("invalid url"), DEFAULT_PRIORITY, &d, &default_context_);
[email protected]316c1e5e2012-09-12 15:17:44889
890 r.Start();
891 EXPECT_TRUE(r.is_pending());
892
[email protected]255620da2013-08-19 13:14:29893 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:44894 EXPECT_TRUE(d.request_failed());
895 }
896}
897
898#if defined(OS_WIN)
899TEST_F(URLRequestTest, ResolveShortcutTest) {
[email protected]6cdfd7f2013-02-08 20:40:15900 base::FilePath app_path;
[email protected]316c1e5e2012-09-12 15:17:44901 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
902 app_path = app_path.AppendASCII("net");
903 app_path = app_path.AppendASCII("data");
904 app_path = app_path.AppendASCII("url_request_unittest");
905 app_path = app_path.AppendASCII("with-headers.html");
906
907 std::wstring lnk_path = app_path.value() + L".lnk";
908
[email protected]451fd902012-10-03 17:14:48909 base::win::ScopedCOMInitializer com_initializer;
[email protected]00d60fa2012-10-01 18:20:17910
[email protected]316c1e5e2012-09-12 15:17:44911 // Temporarily create a shortcut for test
[email protected]aed9efb2013-04-13 01:20:56912 {
913 base::win::ScopedComPtr<IShellLink> shell;
914 ASSERT_TRUE(SUCCEEDED(shell.CreateInstance(CLSID_ShellLink, NULL,
915 CLSCTX_INPROC_SERVER)));
916 base::win::ScopedComPtr<IPersistFile> persist;
917 ASSERT_TRUE(SUCCEEDED(shell.QueryInterface(persist.Receive())));
918 EXPECT_TRUE(SUCCEEDED(shell->SetPath(app_path.value().c_str())));
919 EXPECT_TRUE(SUCCEEDED(shell->SetDescription(L"ResolveShortcutTest")));
920 EXPECT_TRUE(SUCCEEDED(persist->Save(lnk_path.c_str(), TRUE)));
921 }
[email protected]316c1e5e2012-09-12 15:17:44922
923 TestDelegate d;
924 {
[email protected]2ca01e52013-10-31 22:05:19925 URLRequest r(FilePathToFileURL(base::FilePath(lnk_path)),
926 DEFAULT_PRIORITY,
927 &d,
[email protected]6cdfd7f2013-02-08 20:40:15928 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:44929
930 r.Start();
931 EXPECT_TRUE(r.is_pending());
932
[email protected]255620da2013-08-19 13:14:29933 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:44934
935 WIN32_FILE_ATTRIBUTE_DATA data;
936 GetFileAttributesEx(app_path.value().c_str(),
937 GetFileExInfoStandard, &data);
938 HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ,
939 FILE_SHARE_READ, NULL, OPEN_EXISTING,
940 FILE_ATTRIBUTE_NORMAL, NULL);
941 EXPECT_NE(INVALID_HANDLE_VALUE, file);
[email protected]ec530c22013-04-11 15:54:04942 scoped_ptr<char[]> buffer(new char[data.nFileSizeLow]);
[email protected]316c1e5e2012-09-12 15:17:44943 DWORD read_size;
944 BOOL result;
945 result = ReadFile(file, buffer.get(), data.nFileSizeLow,
946 &read_size, NULL);
947 std::string content(buffer.get(), read_size);
948 CloseHandle(file);
949
950 EXPECT_TRUE(!r.is_pending());
951 EXPECT_EQ(1, d.received_redirect_count());
952 EXPECT_EQ(content, d.data_received());
953 }
954
955 // Clean the shortcut
956 DeleteFile(lnk_path.c_str());
[email protected]316c1e5e2012-09-12 15:17:44957}
958#endif // defined(OS_WIN)
959
960TEST_F(URLRequestTest, FileDirCancelTest) {
961 // Put in mock resource provider.
962 NetModule::SetResourceProvider(TestNetResourceProvider);
963
964 TestDelegate d;
965 {
[email protected]6cdfd7f2013-02-08 20:40:15966 base::FilePath file_path;
[email protected]316c1e5e2012-09-12 15:17:44967 PathService::Get(base::DIR_SOURCE_ROOT, &file_path);
968 file_path = file_path.Append(FILE_PATH_LITERAL("net"));
969 file_path = file_path.Append(FILE_PATH_LITERAL("data"));
970
[email protected]2ca01e52013-10-31 22:05:19971 URLRequest req(
972 FilePathToFileURL(file_path), DEFAULT_PRIORITY, &d, &default_context_);
[email protected]316c1e5e2012-09-12 15:17:44973 req.Start();
974 EXPECT_TRUE(req.is_pending());
975
976 d.set_cancel_in_received_data_pending(true);
977
[email protected]255620da2013-08-19 13:14:29978 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:44979 }
980
981 // Take out mock resource provider.
982 NetModule::SetResourceProvider(NULL);
983}
984
[email protected]5f9581402013-10-30 13:08:32985TEST_F(URLRequestTest, FileDirOutputSanity) {
986 // Verify the general sanity of the the output of the file:
987 // directory lister by checking for the output of a known existing
988 // file.
989 const char sentinel_name[] = "filedir-sentinel";
990
991 base::FilePath path;
992 PathService::Get(base::DIR_SOURCE_ROOT, &path);
993 path = path.Append(FILE_PATH_LITERAL("net"));
994 path = path.Append(FILE_PATH_LITERAL("data"));
995 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
996
997 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:19998 URLRequest req(
999 FilePathToFileURL(path), DEFAULT_PRIORITY, &d, &default_context_);
[email protected]5f9581402013-10-30 13:08:321000 req.Start();
1001 base::RunLoop().Run();
1002
1003 // Generate entry for the sentinel file.
1004 base::FilePath sentinel_path = path.AppendASCII(sentinel_name);
[email protected]54124ed02014-01-07 10:06:581005 base::File::Info info;
[email protected]9eae4e62013-12-04 20:56:491006 EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info));
[email protected]5f9581402013-10-30 13:08:321007 EXPECT_GT(info.size, 0);
1008 std::string sentinel_output = GetDirectoryListingEntry(
1009 base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)),
1010 std::string(sentinel_name),
1011 false /* is_dir */,
1012 info.size,
1013 info.last_modified);
1014
1015 ASSERT_LT(0, d.bytes_received());
1016 ASSERT_FALSE(d.request_failed());
1017 ASSERT_TRUE(req.status().is_success());
1018 // Check for the entry generated for the "sentinel" file.
1019 const std::string& data = d.data_received();
1020 ASSERT_NE(data.find(sentinel_output), std::string::npos);
1021}
1022
[email protected]316c1e5e2012-09-12 15:17:441023TEST_F(URLRequestTest, FileDirRedirectNoCrash) {
1024 // There is an implicit redirect when loading a file path that matches a
1025 // directory and does not end with a slash. Ensure that following such
1026 // redirects does not crash. See http://crbug.com/18686.
1027
[email protected]6cdfd7f2013-02-08 20:40:151028 base::FilePath path;
[email protected]316c1e5e2012-09-12 15:17:441029 PathService::Get(base::DIR_SOURCE_ROOT, &path);
1030 path = path.Append(FILE_PATH_LITERAL("net"));
1031 path = path.Append(FILE_PATH_LITERAL("data"));
1032 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
1033
1034 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:191035 URLRequest req(
1036 FilePathToFileURL(path), DEFAULT_PRIORITY, &d, &default_context_);
[email protected]316c1e5e2012-09-12 15:17:441037 req.Start();
[email protected]255620da2013-08-19 13:14:291038 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441039
1040 ASSERT_EQ(1, d.received_redirect_count());
1041 ASSERT_LT(0, d.bytes_received());
1042 ASSERT_FALSE(d.request_failed());
1043 ASSERT_TRUE(req.status().is_success());
1044}
1045
1046#if defined(OS_WIN)
1047// Don't accept the url "file:///" on windows. See http://crbug.com/1474.
1048TEST_F(URLRequestTest, FileDirRedirectSingleSlash) {
1049 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:191050 URLRequest req(GURL("file:///"), DEFAULT_PRIORITY, &d, &default_context_);
[email protected]316c1e5e2012-09-12 15:17:441051 req.Start();
[email protected]255620da2013-08-19 13:14:291052 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441053
1054 ASSERT_EQ(1, d.received_redirect_count());
1055 ASSERT_FALSE(req.status().is_success());
1056}
1057#endif
1058
1059// Custom URLRequestJobs for use with interceptor tests
1060class RestartTestJob : public URLRequestTestJob {
1061 public:
1062 RestartTestJob(URLRequest* request, NetworkDelegate* network_delegate)
1063 : URLRequestTestJob(request, network_delegate, true) {}
1064 protected:
[email protected]46fadfd2013-02-06 09:40:161065 virtual void StartAsync() OVERRIDE {
[email protected]316c1e5e2012-09-12 15:17:441066 this->NotifyRestartRequired();
1067 }
1068 private:
[email protected]46fadfd2013-02-06 09:40:161069 virtual ~RestartTestJob() {}
[email protected]316c1e5e2012-09-12 15:17:441070};
1071
1072class CancelTestJob : public URLRequestTestJob {
1073 public:
1074 explicit CancelTestJob(URLRequest* request, NetworkDelegate* network_delegate)
1075 : URLRequestTestJob(request, network_delegate, true) {}
1076 protected:
[email protected]46fadfd2013-02-06 09:40:161077 virtual void StartAsync() OVERRIDE {
[email protected]316c1e5e2012-09-12 15:17:441078 request_->Cancel();
1079 }
1080 private:
[email protected]46fadfd2013-02-06 09:40:161081 virtual ~CancelTestJob() {}
[email protected]316c1e5e2012-09-12 15:17:441082};
1083
1084class CancelThenRestartTestJob : public URLRequestTestJob {
1085 public:
1086 explicit CancelThenRestartTestJob(URLRequest* request,
1087 NetworkDelegate* network_delegate)
1088 : URLRequestTestJob(request, network_delegate, true) {
1089 }
1090 protected:
[email protected]46fadfd2013-02-06 09:40:161091 virtual void StartAsync() OVERRIDE {
[email protected]316c1e5e2012-09-12 15:17:441092 request_->Cancel();
1093 this->NotifyRestartRequired();
1094 }
1095 private:
[email protected]46fadfd2013-02-06 09:40:161096 virtual ~CancelThenRestartTestJob() {}
[email protected]316c1e5e2012-09-12 15:17:441097};
1098
1099// An Interceptor for use with interceptor tests
1100class TestInterceptor : URLRequest::Interceptor {
1101 public:
1102 TestInterceptor()
1103 : intercept_main_request_(false), restart_main_request_(false),
1104 cancel_main_request_(false), cancel_then_restart_main_request_(false),
1105 simulate_main_network_error_(false),
1106 intercept_redirect_(false), cancel_redirect_request_(false),
1107 intercept_final_response_(false), cancel_final_request_(false),
1108 did_intercept_main_(false), did_restart_main_(false),
1109 did_cancel_main_(false), did_cancel_then_restart_main_(false),
1110 did_simulate_error_main_(false),
1111 did_intercept_redirect_(false), did_cancel_redirect_(false),
1112 did_intercept_final_(false), did_cancel_final_(false) {
1113 URLRequest::Deprecated::RegisterRequestInterceptor(this);
1114 }
1115
[email protected]46fadfd2013-02-06 09:40:161116 virtual ~TestInterceptor() {
[email protected]316c1e5e2012-09-12 15:17:441117 URLRequest::Deprecated::UnregisterRequestInterceptor(this);
1118 }
1119
[email protected]46fadfd2013-02-06 09:40:161120 virtual URLRequestJob* MaybeIntercept(
1121 URLRequest* request,
1122 NetworkDelegate* network_delegate) OVERRIDE {
[email protected]316c1e5e2012-09-12 15:17:441123 if (restart_main_request_) {
1124 restart_main_request_ = false;
1125 did_restart_main_ = true;
1126 return new RestartTestJob(request, network_delegate);
1127 }
1128 if (cancel_main_request_) {
1129 cancel_main_request_ = false;
1130 did_cancel_main_ = true;
1131 return new CancelTestJob(request, network_delegate);
1132 }
1133 if (cancel_then_restart_main_request_) {
1134 cancel_then_restart_main_request_ = false;
1135 did_cancel_then_restart_main_ = true;
1136 return new CancelThenRestartTestJob(request, network_delegate);
1137 }
1138 if (simulate_main_network_error_) {
1139 simulate_main_network_error_ = false;
1140 did_simulate_error_main_ = true;
1141 // will error since the requeted url is not one of its canned urls
1142 return new URLRequestTestJob(request, network_delegate, true);
1143 }
1144 if (!intercept_main_request_)
1145 return NULL;
1146 intercept_main_request_ = false;
1147 did_intercept_main_ = true;
[email protected]2bba3252013-04-08 19:50:591148 URLRequestTestJob* job = new URLRequestTestJob(request,
1149 network_delegate,
1150 main_headers_,
1151 main_data_,
1152 true);
1153 job->set_load_timing_info(main_request_load_timing_info_);
1154 return job;
[email protected]316c1e5e2012-09-12 15:17:441155 }
1156
1157 virtual URLRequestJob* MaybeInterceptRedirect(
1158 URLRequest* request,
1159 NetworkDelegate* network_delegate,
[email protected]46fadfd2013-02-06 09:40:161160 const GURL& location) OVERRIDE {
[email protected]316c1e5e2012-09-12 15:17:441161 if (cancel_redirect_request_) {
1162 cancel_redirect_request_ = false;
1163 did_cancel_redirect_ = true;
1164 return new CancelTestJob(request, network_delegate);
1165 }
1166 if (!intercept_redirect_)
1167 return NULL;
1168 intercept_redirect_ = false;
1169 did_intercept_redirect_ = true;
1170 return new URLRequestTestJob(request,
1171 network_delegate,
1172 redirect_headers_,
1173 redirect_data_,
1174 true);
1175 }
1176
1177 virtual URLRequestJob* MaybeInterceptResponse(
[email protected]46fadfd2013-02-06 09:40:161178 URLRequest* request, NetworkDelegate* network_delegate) OVERRIDE {
[email protected]316c1e5e2012-09-12 15:17:441179 if (cancel_final_request_) {
1180 cancel_final_request_ = false;
1181 did_cancel_final_ = true;
1182 return new CancelTestJob(request, network_delegate);
1183 }
1184 if (!intercept_final_response_)
1185 return NULL;
1186 intercept_final_response_ = false;
1187 did_intercept_final_ = true;
1188 return new URLRequestTestJob(request,
1189 network_delegate,
1190 final_headers_,
1191 final_data_,
1192 true);
1193 }
1194
[email protected]2bba3252013-04-08 19:50:591195 // Whether to intercept the main request, and if so the response to return and
1196 // the LoadTimingInfo to use.
[email protected]316c1e5e2012-09-12 15:17:441197 bool intercept_main_request_;
1198 std::string main_headers_;
1199 std::string main_data_;
[email protected]2bba3252013-04-08 19:50:591200 LoadTimingInfo main_request_load_timing_info_;
[email protected]316c1e5e2012-09-12 15:17:441201
1202 // Other actions we take at MaybeIntercept time
1203 bool restart_main_request_;
1204 bool cancel_main_request_;
1205 bool cancel_then_restart_main_request_;
1206 bool simulate_main_network_error_;
1207
1208 // Whether to intercept redirects, and if so the response to return.
1209 bool intercept_redirect_;
1210 std::string redirect_headers_;
1211 std::string redirect_data_;
1212
1213 // Other actions we can take at MaybeInterceptRedirect time
1214 bool cancel_redirect_request_;
1215
1216 // Whether to intercept final response, and if so the response to return.
1217 bool intercept_final_response_;
1218 std::string final_headers_;
1219 std::string final_data_;
1220
1221 // Other actions we can take at MaybeInterceptResponse time
1222 bool cancel_final_request_;
1223
1224 // If we did something or not
1225 bool did_intercept_main_;
1226 bool did_restart_main_;
1227 bool did_cancel_main_;
1228 bool did_cancel_then_restart_main_;
1229 bool did_simulate_error_main_;
1230 bool did_intercept_redirect_;
1231 bool did_cancel_redirect_;
1232 bool did_intercept_final_;
1233 bool did_cancel_final_;
1234
1235 // Static getters for canned response header and data strings
1236
1237 static std::string ok_data() {
1238 return URLRequestTestJob::test_data_1();
1239 }
1240
1241 static std::string ok_headers() {
1242 return URLRequestTestJob::test_headers();
1243 }
1244
1245 static std::string redirect_data() {
1246 return std::string();
1247 }
1248
1249 static std::string redirect_headers() {
1250 return URLRequestTestJob::test_redirect_headers();
1251 }
1252
1253 static std::string error_data() {
1254 return std::string("ohhh nooooo mr. bill!");
1255 }
1256
1257 static std::string error_headers() {
1258 return URLRequestTestJob::test_error_headers();
1259 }
1260};
1261
1262TEST_F(URLRequestTest, Intercept) {
1263 TestInterceptor interceptor;
1264
1265 // intercept the main request and respond with a simple response
1266 interceptor.intercept_main_request_ = true;
1267 interceptor.main_headers_ = TestInterceptor::ok_headers();
1268 interceptor.main_data_ = TestInterceptor::ok_data();
1269
1270 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:191271 URLRequest req(GURL("http://test_intercept/foo"),
1272 DEFAULT_PRIORITY,
1273 &d,
1274 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:441275 base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data();
1276 base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data();
1277 base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data();
1278 req.SetUserData(NULL, user_data0);
1279 req.SetUserData(&user_data1, user_data1);
1280 req.SetUserData(&user_data2, user_data2);
1281 req.set_method("GET");
1282 req.Start();
[email protected]255620da2013-08-19 13:14:291283 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441284
1285 // Make sure we can retrieve our specific user data
1286 EXPECT_EQ(user_data0, req.GetUserData(NULL));
1287 EXPECT_EQ(user_data1, req.GetUserData(&user_data1));
1288 EXPECT_EQ(user_data2, req.GetUserData(&user_data2));
1289
1290 // Check the interceptor got called as expected
1291 EXPECT_TRUE(interceptor.did_intercept_main_);
1292
1293 // Check we got one good response
1294 EXPECT_TRUE(req.status().is_success());
1295 EXPECT_EQ(200, req.response_headers()->response_code());
1296 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1297 EXPECT_EQ(1, d.response_started_count());
1298 EXPECT_EQ(0, d.received_redirect_count());
1299}
1300
1301TEST_F(URLRequestTest, InterceptRedirect) {
1302 TestInterceptor interceptor;
1303
1304 // intercept the main request and respond with a redirect
1305 interceptor.intercept_main_request_ = true;
1306 interceptor.main_headers_ = TestInterceptor::redirect_headers();
1307 interceptor.main_data_ = TestInterceptor::redirect_data();
1308
1309 // intercept that redirect and respond a final OK response
1310 interceptor.intercept_redirect_ = true;
1311 interceptor.redirect_headers_ = TestInterceptor::ok_headers();
1312 interceptor.redirect_data_ = TestInterceptor::ok_data();
1313
1314 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:191315 URLRequest req(GURL("http://test_intercept/foo"),
1316 DEFAULT_PRIORITY,
1317 &d,
1318 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:441319 req.set_method("GET");
1320 req.Start();
[email protected]255620da2013-08-19 13:14:291321 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441322
1323 // Check the interceptor got called as expected
1324 EXPECT_TRUE(interceptor.did_intercept_main_);
1325 EXPECT_TRUE(interceptor.did_intercept_redirect_);
1326
1327 // Check we got one good response
1328 EXPECT_TRUE(req.status().is_success());
1329 if (req.status().is_success()) {
1330 EXPECT_EQ(200, req.response_headers()->response_code());
1331 }
1332 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1333 EXPECT_EQ(1, d.response_started_count());
1334 EXPECT_EQ(0, d.received_redirect_count());
1335}
1336
1337TEST_F(URLRequestTest, InterceptServerError) {
1338 TestInterceptor interceptor;
1339
1340 // intercept the main request to generate a server error response
1341 interceptor.intercept_main_request_ = true;
1342 interceptor.main_headers_ = TestInterceptor::error_headers();
1343 interceptor.main_data_ = TestInterceptor::error_data();
1344
1345 // intercept that error and respond with an OK response
1346 interceptor.intercept_final_response_ = true;
1347 interceptor.final_headers_ = TestInterceptor::ok_headers();
1348 interceptor.final_data_ = TestInterceptor::ok_data();
1349
1350 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:191351 URLRequest req(GURL("http://test_intercept/foo"),
1352 DEFAULT_PRIORITY,
1353 &d,
1354 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:441355 req.set_method("GET");
1356 req.Start();
[email protected]255620da2013-08-19 13:14:291357 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441358
1359 // Check the interceptor got called as expected
1360 EXPECT_TRUE(interceptor.did_intercept_main_);
1361 EXPECT_TRUE(interceptor.did_intercept_final_);
1362
1363 // Check we got one good response
1364 EXPECT_TRUE(req.status().is_success());
1365 EXPECT_EQ(200, req.response_headers()->response_code());
1366 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1367 EXPECT_EQ(1, d.response_started_count());
1368 EXPECT_EQ(0, d.received_redirect_count());
1369}
1370
1371TEST_F(URLRequestTest, InterceptNetworkError) {
1372 TestInterceptor interceptor;
1373
1374 // intercept the main request to simulate a network error
1375 interceptor.simulate_main_network_error_ = true;
1376
1377 // intercept that error and respond with an OK response
1378 interceptor.intercept_final_response_ = true;
1379 interceptor.final_headers_ = TestInterceptor::ok_headers();
1380 interceptor.final_data_ = TestInterceptor::ok_data();
1381
1382 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:191383 URLRequest req(GURL("http://test_intercept/foo"),
1384 DEFAULT_PRIORITY,
1385 &d,
1386 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:441387 req.set_method("GET");
1388 req.Start();
[email protected]255620da2013-08-19 13:14:291389 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441390
1391 // Check the interceptor got called as expected
1392 EXPECT_TRUE(interceptor.did_simulate_error_main_);
1393 EXPECT_TRUE(interceptor.did_intercept_final_);
1394
1395 // Check we received one good response
1396 EXPECT_TRUE(req.status().is_success());
1397 EXPECT_EQ(200, req.response_headers()->response_code());
1398 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1399 EXPECT_EQ(1, d.response_started_count());
1400 EXPECT_EQ(0, d.received_redirect_count());
1401}
1402
1403TEST_F(URLRequestTest, InterceptRestartRequired) {
1404 TestInterceptor interceptor;
1405
1406 // restart the main request
1407 interceptor.restart_main_request_ = true;
1408
1409 // then intercept the new main request and respond with an OK response
1410 interceptor.intercept_main_request_ = true;
1411 interceptor.main_headers_ = TestInterceptor::ok_headers();
1412 interceptor.main_data_ = TestInterceptor::ok_data();
1413
1414 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:191415 URLRequest req(GURL("http://test_intercept/foo"),
1416 DEFAULT_PRIORITY,
1417 &d,
1418 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:441419 req.set_method("GET");
1420 req.Start();
[email protected]255620da2013-08-19 13:14:291421 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441422
1423 // Check the interceptor got called as expected
1424 EXPECT_TRUE(interceptor.did_restart_main_);
1425 EXPECT_TRUE(interceptor.did_intercept_main_);
1426
1427 // Check we received one good response
1428 EXPECT_TRUE(req.status().is_success());
1429 if (req.status().is_success()) {
1430 EXPECT_EQ(200, req.response_headers()->response_code());
1431 }
1432 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1433 EXPECT_EQ(1, d.response_started_count());
1434 EXPECT_EQ(0, d.received_redirect_count());
1435}
1436
1437TEST_F(URLRequestTest, InterceptRespectsCancelMain) {
1438 TestInterceptor interceptor;
1439
1440 // intercept the main request and cancel from within the restarted job
1441 interceptor.cancel_main_request_ = true;
1442
1443 // setup to intercept final response and override it with an OK response
1444 interceptor.intercept_final_response_ = true;
1445 interceptor.final_headers_ = TestInterceptor::ok_headers();
1446 interceptor.final_data_ = TestInterceptor::ok_data();
1447
1448 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:191449 URLRequest req(GURL("http://test_intercept/foo"),
1450 DEFAULT_PRIORITY,
1451 &d,
1452 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:441453 req.set_method("GET");
1454 req.Start();
[email protected]255620da2013-08-19 13:14:291455 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441456
1457 // Check the interceptor got called as expected
1458 EXPECT_TRUE(interceptor.did_cancel_main_);
1459 EXPECT_FALSE(interceptor.did_intercept_final_);
1460
1461 // Check we see a canceled request
1462 EXPECT_FALSE(req.status().is_success());
1463 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1464}
1465
1466TEST_F(URLRequestTest, InterceptRespectsCancelRedirect) {
1467 TestInterceptor interceptor;
1468
1469 // intercept the main request and respond with a redirect
1470 interceptor.intercept_main_request_ = true;
1471 interceptor.main_headers_ = TestInterceptor::redirect_headers();
1472 interceptor.main_data_ = TestInterceptor::redirect_data();
1473
1474 // intercept the redirect and cancel from within that job
1475 interceptor.cancel_redirect_request_ = true;
1476
1477 // setup to intercept final response and override it with an OK response
1478 interceptor.intercept_final_response_ = true;
1479 interceptor.final_headers_ = TestInterceptor::ok_headers();
1480 interceptor.final_data_ = TestInterceptor::ok_data();
1481
1482 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:191483 URLRequest req(GURL("http://test_intercept/foo"),
1484 DEFAULT_PRIORITY,
1485 &d,
1486 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:441487 req.set_method("GET");
1488 req.Start();
[email protected]255620da2013-08-19 13:14:291489 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441490
1491 // Check the interceptor got called as expected
1492 EXPECT_TRUE(interceptor.did_intercept_main_);
1493 EXPECT_TRUE(interceptor.did_cancel_redirect_);
1494 EXPECT_FALSE(interceptor.did_intercept_final_);
1495
1496 // Check we see a canceled request
1497 EXPECT_FALSE(req.status().is_success());
1498 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1499}
1500
1501TEST_F(URLRequestTest, InterceptRespectsCancelFinal) {
1502 TestInterceptor interceptor;
1503
1504 // intercept the main request to simulate a network error
1505 interceptor.simulate_main_network_error_ = true;
1506
1507 // setup to intercept final response and cancel from within that job
1508 interceptor.cancel_final_request_ = true;
1509
1510 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:191511 URLRequest req(GURL("http://test_intercept/foo"),
1512 DEFAULT_PRIORITY,
1513 &d,
1514 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:441515 req.set_method("GET");
1516 req.Start();
[email protected]255620da2013-08-19 13:14:291517 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441518
1519 // Check the interceptor got called as expected
1520 EXPECT_TRUE(interceptor.did_simulate_error_main_);
1521 EXPECT_TRUE(interceptor.did_cancel_final_);
1522
1523 // Check we see a canceled request
1524 EXPECT_FALSE(req.status().is_success());
1525 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1526}
1527
1528TEST_F(URLRequestTest, InterceptRespectsCancelInRestart) {
1529 TestInterceptor interceptor;
1530
1531 // intercept the main request and cancel then restart from within that job
1532 interceptor.cancel_then_restart_main_request_ = true;
1533
1534 // setup to intercept final response and override it with an OK response
1535 interceptor.intercept_final_response_ = true;
1536 interceptor.final_headers_ = TestInterceptor::ok_headers();
1537 interceptor.final_data_ = TestInterceptor::ok_data();
1538
1539 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:191540 URLRequest req(GURL("http://test_intercept/foo"),
1541 DEFAULT_PRIORITY,
1542 &d,
1543 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:441544 req.set_method("GET");
1545 req.Start();
[email protected]255620da2013-08-19 13:14:291546 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441547
1548 // Check the interceptor got called as expected
1549 EXPECT_TRUE(interceptor.did_cancel_then_restart_main_);
1550 EXPECT_FALSE(interceptor.did_intercept_final_);
1551
1552 // Check we see a canceled request
1553 EXPECT_FALSE(req.status().is_success());
1554 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1555}
1556
[email protected]2bba3252013-04-08 19:50:591557LoadTimingInfo RunLoadTimingTest(const LoadTimingInfo& job_load_timing,
1558 URLRequestContext* context) {
1559 TestInterceptor interceptor;
1560 interceptor.intercept_main_request_ = true;
1561 interceptor.main_request_load_timing_info_ = job_load_timing;
1562 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:191563 URLRequest req(
1564 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, context);
[email protected]2bba3252013-04-08 19:50:591565 req.Start();
[email protected]255620da2013-08-19 13:14:291566 base::RunLoop().Run();
[email protected]2bba3252013-04-08 19:50:591567
1568 LoadTimingInfo resulting_load_timing;
1569 req.GetLoadTimingInfo(&resulting_load_timing);
1570
1571 // None of these should be modified by the URLRequest.
1572 EXPECT_EQ(job_load_timing.socket_reused, resulting_load_timing.socket_reused);
1573 EXPECT_EQ(job_load_timing.socket_log_id, resulting_load_timing.socket_log_id);
1574 EXPECT_EQ(job_load_timing.send_start, resulting_load_timing.send_start);
1575 EXPECT_EQ(job_load_timing.send_end, resulting_load_timing.send_end);
1576 EXPECT_EQ(job_load_timing.receive_headers_end,
1577 resulting_load_timing.receive_headers_end);
1578
1579 return resulting_load_timing;
1580}
1581
1582// "Normal" LoadTimingInfo as returned by a job. Everything is in order, not
1583// reused. |connect_time_flags| is used to indicate if there should be dns
1584// or SSL times, and |used_proxy| is used for proxy times.
1585LoadTimingInfo NormalLoadTimingInfo(base::TimeTicks now,
1586 int connect_time_flags,
1587 bool used_proxy) {
1588 LoadTimingInfo load_timing;
1589 load_timing.socket_log_id = 1;
1590
1591 if (used_proxy) {
1592 load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1);
1593 load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2);
1594 }
1595
1596 LoadTimingInfo::ConnectTiming& connect_timing = load_timing.connect_timing;
1597 if (connect_time_flags & CONNECT_TIMING_HAS_DNS_TIMES) {
1598 connect_timing.dns_start = now + base::TimeDelta::FromDays(3);
1599 connect_timing.dns_end = now + base::TimeDelta::FromDays(4);
1600 }
1601 connect_timing.connect_start = now + base::TimeDelta::FromDays(5);
1602 if (connect_time_flags & CONNECT_TIMING_HAS_SSL_TIMES) {
1603 connect_timing.ssl_start = now + base::TimeDelta::FromDays(6);
1604 connect_timing.ssl_end = now + base::TimeDelta::FromDays(7);
1605 }
1606 connect_timing.connect_end = now + base::TimeDelta::FromDays(8);
1607
1608 load_timing.send_start = now + base::TimeDelta::FromDays(9);
1609 load_timing.send_end = now + base::TimeDelta::FromDays(10);
1610 load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11);
1611 return load_timing;
1612}
1613
1614// Same as above, but in the case of a reused socket.
1615LoadTimingInfo NormalLoadTimingInfoReused(base::TimeTicks now,
1616 bool used_proxy) {
1617 LoadTimingInfo load_timing;
1618 load_timing.socket_log_id = 1;
1619 load_timing.socket_reused = true;
1620
1621 if (used_proxy) {
1622 load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1);
1623 load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2);
1624 }
1625
1626 load_timing.send_start = now + base::TimeDelta::FromDays(9);
1627 load_timing.send_end = now + base::TimeDelta::FromDays(10);
1628 load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11);
1629 return load_timing;
1630}
1631
1632// Basic test that the intercept + load timing tests work.
1633TEST_F(URLRequestTest, InterceptLoadTiming) {
1634 base::TimeTicks now = base::TimeTicks::Now();
1635 LoadTimingInfo job_load_timing =
1636 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, false);
1637
[email protected]2ca01e52013-10-31 22:05:191638 LoadTimingInfo load_timing_result =
1639 RunLoadTimingTest(job_load_timing, &default_context_);
[email protected]2bba3252013-04-08 19:50:591640
1641 // Nothing should have been changed by the URLRequest.
1642 EXPECT_EQ(job_load_timing.proxy_resolve_start,
1643 load_timing_result.proxy_resolve_start);
1644 EXPECT_EQ(job_load_timing.proxy_resolve_end,
1645 load_timing_result.proxy_resolve_end);
1646 EXPECT_EQ(job_load_timing.connect_timing.dns_start,
1647 load_timing_result.connect_timing.dns_start);
1648 EXPECT_EQ(job_load_timing.connect_timing.dns_end,
1649 load_timing_result.connect_timing.dns_end);
1650 EXPECT_EQ(job_load_timing.connect_timing.connect_start,
1651 load_timing_result.connect_timing.connect_start);
1652 EXPECT_EQ(job_load_timing.connect_timing.connect_end,
1653 load_timing_result.connect_timing.connect_end);
1654 EXPECT_EQ(job_load_timing.connect_timing.ssl_start,
1655 load_timing_result.connect_timing.ssl_start);
1656 EXPECT_EQ(job_load_timing.connect_timing.ssl_end,
1657 load_timing_result.connect_timing.ssl_end);
1658
1659 // Redundant sanity check.
1660 TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_DNS_TIMES);
1661}
1662
1663// Another basic test, with proxy and SSL times, but no DNS times.
1664TEST_F(URLRequestTest, InterceptLoadTimingProxy) {
1665 base::TimeTicks now = base::TimeTicks::Now();
1666 LoadTimingInfo job_load_timing =
1667 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, true);
1668
[email protected]2ca01e52013-10-31 22:05:191669 LoadTimingInfo load_timing_result =
1670 RunLoadTimingTest(job_load_timing, &default_context_);
[email protected]2bba3252013-04-08 19:50:591671
1672 // Nothing should have been changed by the URLRequest.
1673 EXPECT_EQ(job_load_timing.proxy_resolve_start,
1674 load_timing_result.proxy_resolve_start);
1675 EXPECT_EQ(job_load_timing.proxy_resolve_end,
1676 load_timing_result.proxy_resolve_end);
1677 EXPECT_EQ(job_load_timing.connect_timing.dns_start,
1678 load_timing_result.connect_timing.dns_start);
1679 EXPECT_EQ(job_load_timing.connect_timing.dns_end,
1680 load_timing_result.connect_timing.dns_end);
1681 EXPECT_EQ(job_load_timing.connect_timing.connect_start,
1682 load_timing_result.connect_timing.connect_start);
1683 EXPECT_EQ(job_load_timing.connect_timing.connect_end,
1684 load_timing_result.connect_timing.connect_end);
1685 EXPECT_EQ(job_load_timing.connect_timing.ssl_start,
1686 load_timing_result.connect_timing.ssl_start);
1687 EXPECT_EQ(job_load_timing.connect_timing.ssl_end,
1688 load_timing_result.connect_timing.ssl_end);
1689
1690 // Redundant sanity check.
1691 TestLoadTimingNotReusedWithProxy(load_timing_result,
1692 CONNECT_TIMING_HAS_SSL_TIMES);
1693}
1694
1695// Make sure that URLRequest correctly adjusts proxy times when they're before
1696// |request_start|, due to already having a connected socket. This happens in
1697// the case of reusing a SPDY session or HTTP pipeline. The connected socket is
1698// not considered reused in this test (May be a preconnect).
1699//
1700// To mix things up from the test above, assumes DNS times but no SSL times.
1701TEST_F(URLRequestTest, InterceptLoadTimingEarlyProxyResolution) {
1702 base::TimeTicks now = base::TimeTicks::Now();
1703 LoadTimingInfo job_load_timing =
1704 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, true);
1705 job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(6);
1706 job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(5);
1707 job_load_timing.connect_timing.dns_start = now - base::TimeDelta::FromDays(4);
1708 job_load_timing.connect_timing.dns_end = now - base::TimeDelta::FromDays(3);
1709 job_load_timing.connect_timing.connect_start =
1710 now - base::TimeDelta::FromDays(2);
1711 job_load_timing.connect_timing.connect_end =
1712 now - base::TimeDelta::FromDays(1);
1713
[email protected]2ca01e52013-10-31 22:05:191714 LoadTimingInfo load_timing_result =
1715 RunLoadTimingTest(job_load_timing, &default_context_);
[email protected]2bba3252013-04-08 19:50:591716
1717 // Proxy times, connect times, and DNS times should all be replaced with
1718 // request_start.
1719 EXPECT_EQ(load_timing_result.request_start,
1720 load_timing_result.proxy_resolve_start);
1721 EXPECT_EQ(load_timing_result.request_start,
1722 load_timing_result.proxy_resolve_end);
1723 EXPECT_EQ(load_timing_result.request_start,
1724 load_timing_result.connect_timing.dns_start);
1725 EXPECT_EQ(load_timing_result.request_start,
1726 load_timing_result.connect_timing.dns_end);
1727 EXPECT_EQ(load_timing_result.request_start,
1728 load_timing_result.connect_timing.connect_start);
1729 EXPECT_EQ(load_timing_result.request_start,
1730 load_timing_result.connect_timing.connect_end);
1731
1732 // Other times should have been left null.
1733 TestLoadTimingNotReusedWithProxy(load_timing_result,
1734 CONNECT_TIMING_HAS_DNS_TIMES);
1735}
1736
1737// Same as above, but in the reused case.
1738TEST_F(URLRequestTest, InterceptLoadTimingEarlyProxyResolutionReused) {
1739 base::TimeTicks now = base::TimeTicks::Now();
1740 LoadTimingInfo job_load_timing = NormalLoadTimingInfoReused(now, true);
1741 job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(4);
1742 job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(3);
1743
[email protected]2ca01e52013-10-31 22:05:191744 LoadTimingInfo load_timing_result =
1745 RunLoadTimingTest(job_load_timing, &default_context_);
[email protected]2bba3252013-04-08 19:50:591746
1747 // Proxy times and connect times should all be replaced with request_start.
1748 EXPECT_EQ(load_timing_result.request_start,
1749 load_timing_result.proxy_resolve_start);
1750 EXPECT_EQ(load_timing_result.request_start,
1751 load_timing_result.proxy_resolve_end);
1752
1753 // Other times should have been left null.
1754 TestLoadTimingReusedWithProxy(load_timing_result);
1755}
1756
1757// Make sure that URLRequest correctly adjusts connect times when they're before
1758// |request_start|, due to reusing a connected socket. The connected socket is
1759// not considered reused in this test (May be a preconnect).
1760//
1761// To mix things up, the request has SSL times, but no DNS times.
1762TEST_F(URLRequestTest, InterceptLoadTimingEarlyConnect) {
1763 base::TimeTicks now = base::TimeTicks::Now();
1764 LoadTimingInfo job_load_timing =
1765 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, false);
1766 job_load_timing.connect_timing.connect_start =
1767 now - base::TimeDelta::FromDays(1);
1768 job_load_timing.connect_timing.ssl_start = now - base::TimeDelta::FromDays(2);
1769 job_load_timing.connect_timing.ssl_end = now - base::TimeDelta::FromDays(3);
1770 job_load_timing.connect_timing.connect_end =
1771 now - base::TimeDelta::FromDays(4);
1772
[email protected]2ca01e52013-10-31 22:05:191773 LoadTimingInfo load_timing_result =
1774 RunLoadTimingTest(job_load_timing, &default_context_);
[email protected]2bba3252013-04-08 19:50:591775
1776 // Connect times, and SSL times should be replaced with request_start.
1777 EXPECT_EQ(load_timing_result.request_start,
1778 load_timing_result.connect_timing.connect_start);
1779 EXPECT_EQ(load_timing_result.request_start,
1780 load_timing_result.connect_timing.ssl_start);
1781 EXPECT_EQ(load_timing_result.request_start,
1782 load_timing_result.connect_timing.ssl_end);
1783 EXPECT_EQ(load_timing_result.request_start,
1784 load_timing_result.connect_timing.connect_end);
1785
1786 // Other times should have been left null.
1787 TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_SSL_TIMES);
1788}
1789
1790// Make sure that URLRequest correctly adjusts connect times when they're before
1791// |request_start|, due to reusing a connected socket in the case that there
1792// are also proxy times. The connected socket is not considered reused in this
1793// test (May be a preconnect).
1794//
1795// In this test, there are no SSL or DNS times.
1796TEST_F(URLRequestTest, InterceptLoadTimingEarlyConnectWithProxy) {
1797 base::TimeTicks now = base::TimeTicks::Now();
1798 LoadTimingInfo job_load_timing =
1799 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY, true);
1800 job_load_timing.connect_timing.connect_start =
1801 now - base::TimeDelta::FromDays(1);
1802 job_load_timing.connect_timing.connect_end =
1803 now - base::TimeDelta::FromDays(2);
1804
[email protected]2ca01e52013-10-31 22:05:191805 LoadTimingInfo load_timing_result =
1806 RunLoadTimingTest(job_load_timing, &default_context_);
[email protected]2bba3252013-04-08 19:50:591807
1808 // Connect times should be replaced with proxy_resolve_end.
1809 EXPECT_EQ(load_timing_result.proxy_resolve_end,
1810 load_timing_result.connect_timing.connect_start);
1811 EXPECT_EQ(load_timing_result.proxy_resolve_end,
1812 load_timing_result.connect_timing.connect_end);
1813
1814 // Other times should have been left null.
1815 TestLoadTimingNotReusedWithProxy(load_timing_result,
1816 CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY);
1817}
1818
[email protected]316c1e5e2012-09-12 15:17:441819// Check that two different URL requests have different identifiers.
1820TEST_F(URLRequestTest, Identifiers) {
1821 TestDelegate d;
1822 TestURLRequestContext context;
[email protected]2ca01e52013-10-31 22:05:191823 TestURLRequest req(
1824 GURL("http://example.com"), DEFAULT_PRIORITY, &d, &context);
1825 TestURLRequest other_req(
1826 GURL("http://example.com"), DEFAULT_PRIORITY, &d, &context);
[email protected]316c1e5e2012-09-12 15:17:441827
1828 ASSERT_NE(req.identifier(), other_req.identifier());
1829}
1830
1831// Check that a failure to connect to the proxy is reported to the network
1832// delegate.
1833TEST_F(URLRequestTest, NetworkDelegateProxyError) {
1834 MockHostResolver host_resolver;
1835 host_resolver.rules()->AddSimulatedFailure("*");
1836
[email protected]ceefd7fd2012-11-29 00:36:241837 TestNetworkDelegate network_delegate; // Must outlive URLRequests.
[email protected]316c1e5e2012-09-12 15:17:441838 TestURLRequestContextWithProxy context("myproxy:70", &network_delegate);
1839
1840 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:191841 URLRequest req(GURL("http://example.com"), DEFAULT_PRIORITY, &d, &context);
[email protected]316c1e5e2012-09-12 15:17:441842 req.set_method("GET");
1843
1844 req.Start();
[email protected]255620da2013-08-19 13:14:291845 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441846
1847 // Check we see a failed request.
1848 EXPECT_FALSE(req.status().is_success());
1849 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
1850 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, req.status().error());
1851
1852 EXPECT_EQ(1, network_delegate.error_count());
1853 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, network_delegate.last_error());
1854 EXPECT_EQ(1, network_delegate.completed_requests());
1855}
1856
1857// Make sure that net::NetworkDelegate::NotifyCompleted is called if
1858// content is empty.
1859TEST_F(URLRequestTest, RequestCompletionForEmptyResponse) {
1860 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:191861 URLRequest req(GURL("data:,"), DEFAULT_PRIORITY, &d, &default_context_);
[email protected]316c1e5e2012-09-12 15:17:441862 req.Start();
[email protected]255620da2013-08-19 13:14:291863 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441864 EXPECT_EQ("", d.data_received());
1865 EXPECT_EQ(1, default_network_delegate_.completed_requests());
1866}
1867
[email protected]5033ab82013-03-22 20:17:461868// Make sure that SetPriority actually sets the URLRequest's priority
1869// correctly, both before and after start.
1870TEST_F(URLRequestTest, SetPriorityBasic) {
1871 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:191872 URLRequest req(GURL("http://test_intercept/foo"),
1873 DEFAULT_PRIORITY,
1874 &d,
1875 &default_context_);
[email protected]5033ab82013-03-22 20:17:461876 EXPECT_EQ(DEFAULT_PRIORITY, req.priority());
1877
1878 req.SetPriority(LOW);
1879 EXPECT_EQ(LOW, req.priority());
1880
1881 req.Start();
1882 EXPECT_EQ(LOW, req.priority());
1883
1884 req.SetPriority(MEDIUM);
1885 EXPECT_EQ(MEDIUM, req.priority());
1886}
1887
1888// Make sure that URLRequest calls SetPriority on a job before calling
1889// Start on it.
1890TEST_F(URLRequestTest, SetJobPriorityBeforeJobStart) {
1891 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:191892 URLRequest req(GURL("http://test_intercept/foo"),
1893 DEFAULT_PRIORITY,
1894 &d,
1895 &default_context_);
[email protected]5033ab82013-03-22 20:17:461896 EXPECT_EQ(DEFAULT_PRIORITY, req.priority());
1897
1898 scoped_refptr<URLRequestTestJob> job =
1899 new URLRequestTestJob(&req, &default_network_delegate_);
[email protected]90499482013-06-01 00:39:501900 AddTestInterceptor()->set_main_intercept_job(job.get());
[email protected]5033ab82013-03-22 20:17:461901 EXPECT_EQ(DEFAULT_PRIORITY, job->priority());
1902
1903 req.SetPriority(LOW);
1904
1905 req.Start();
1906 EXPECT_EQ(LOW, job->priority());
1907}
1908
1909// Make sure that URLRequest passes on its priority updates to its
1910// job.
1911TEST_F(URLRequestTest, SetJobPriority) {
1912 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:191913 URLRequest req(GURL("http://test_intercept/foo"),
1914 DEFAULT_PRIORITY,
1915 &d,
1916 &default_context_);
[email protected]5033ab82013-03-22 20:17:461917
1918 scoped_refptr<URLRequestTestJob> job =
1919 new URLRequestTestJob(&req, &default_network_delegate_);
[email protected]90499482013-06-01 00:39:501920 AddTestInterceptor()->set_main_intercept_job(job.get());
[email protected]5033ab82013-03-22 20:17:461921
1922 req.SetPriority(LOW);
1923 req.Start();
1924 EXPECT_EQ(LOW, job->priority());
1925
1926 req.SetPriority(MEDIUM);
1927 EXPECT_EQ(MEDIUM, req.priority());
1928 EXPECT_EQ(MEDIUM, job->priority());
1929}
1930
[email protected]bb1c4662013-11-14 00:00:071931// Setting the IGNORE_LIMITS load flag should be okay if the priority
1932// is MAXIMUM_PRIORITY.
1933TEST_F(URLRequestTest, PriorityIgnoreLimits) {
1934 TestDelegate d;
1935 URLRequest req(GURL("http://test_intercept/foo"),
1936 MAXIMUM_PRIORITY,
1937 &d,
1938 &default_context_);
1939 EXPECT_EQ(MAXIMUM_PRIORITY, req.priority());
1940
1941 scoped_refptr<URLRequestTestJob> job =
1942 new URLRequestTestJob(&req, &default_network_delegate_);
1943 AddTestInterceptor()->set_main_intercept_job(job.get());
1944
1945 req.SetLoadFlags(LOAD_IGNORE_LIMITS);
1946 EXPECT_EQ(MAXIMUM_PRIORITY, req.priority());
1947
1948 req.SetPriority(MAXIMUM_PRIORITY);
1949 EXPECT_EQ(MAXIMUM_PRIORITY, req.priority());
1950
1951 req.Start();
1952 EXPECT_EQ(MAXIMUM_PRIORITY, req.priority());
1953 EXPECT_EQ(MAXIMUM_PRIORITY, job->priority());
1954}
1955
[email protected]ce7d0cbc2013-05-03 18:57:221956// TODO(droger): Support SpawnedTestServer on iOS (see http://crbug.com/148666).
[email protected]316c1e5e2012-09-12 15:17:441957#if !defined(OS_IOS)
[email protected]ce7d0cbc2013-05-03 18:57:221958// A subclass of SpawnedTestServer that uses a statically-configured hostname.
1959// This is to work around mysterious failures in chrome_frame_net_tests. See:
[email protected]316c1e5e2012-09-12 15:17:441960// http://crbug.com/114369
[email protected]cd6f2522014-01-16 18:27:351961// TODO(erikwright): remove or update as needed; see http://crbug.com/334634.
[email protected]ce7d0cbc2013-05-03 18:57:221962class LocalHttpTestServer : public SpawnedTestServer {
[email protected]316c1e5e2012-09-12 15:17:441963 public:
[email protected]6cdfd7f2013-02-08 20:40:151964 explicit LocalHttpTestServer(const base::FilePath& document_root)
[email protected]ce7d0cbc2013-05-03 18:57:221965 : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP,
1966 ScopedCustomUrlRequestTestHttpHost::value(),
1967 document_root) {}
[email protected]316c1e5e2012-09-12 15:17:441968 LocalHttpTestServer()
[email protected]ce7d0cbc2013-05-03 18:57:221969 : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP,
1970 ScopedCustomUrlRequestTestHttpHost::value(),
1971 base::FilePath()) {}
[email protected]316c1e5e2012-09-12 15:17:441972};
1973
[email protected]f2f31b32013-01-16 23:24:091974TEST_F(URLRequestTest, DelayedCookieCallback) {
[email protected]316c1e5e2012-09-12 15:17:441975 LocalHttpTestServer test_server;
1976 ASSERT_TRUE(test_server.Start());
1977
1978 TestURLRequestContext context;
1979 scoped_refptr<DelayedCookieMonster> delayed_cm =
1980 new DelayedCookieMonster();
1981 scoped_refptr<CookieStore> cookie_store = delayed_cm;
[email protected]90499482013-06-01 00:39:501982 context.set_cookie_store(delayed_cm.get());
[email protected]316c1e5e2012-09-12 15:17:441983
1984 // Set up a cookie.
1985 {
1986 TestNetworkDelegate network_delegate;
1987 context.set_network_delegate(&network_delegate);
1988 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:191989 URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"),
1990 DEFAULT_PRIORITY,
1991 &d,
1992 &context);
[email protected]316c1e5e2012-09-12 15:17:441993 req.Start();
[email protected]255620da2013-08-19 13:14:291994 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441995 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1996 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1997 EXPECT_EQ(1, network_delegate.set_cookie_count());
1998 }
1999
2000 // Verify that the cookie is set.
2001 {
2002 TestNetworkDelegate network_delegate;
2003 context.set_network_delegate(&network_delegate);
2004 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:192005 URLRequest req(test_server.GetURL("echoheader?Cookie"),
2006 DEFAULT_PRIORITY,
2007 &d,
2008 &context);
[email protected]316c1e5e2012-09-12 15:17:442009 req.Start();
[email protected]255620da2013-08-19 13:14:292010 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442011
2012 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2013 != std::string::npos);
2014 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2015 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2016 }
2017}
2018
[email protected]f2f31b32013-01-16 23:24:092019TEST_F(URLRequestTest, DoNotSendCookies) {
[email protected]316c1e5e2012-09-12 15:17:442020 LocalHttpTestServer test_server;
2021 ASSERT_TRUE(test_server.Start());
2022
2023 // Set up a cookie.
2024 {
2025 TestNetworkDelegate network_delegate;
2026 default_context_.set_network_delegate(&network_delegate);
2027 TestDelegate d;
2028 URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"),
[email protected]2ca01e52013-10-31 22:05:192029 DEFAULT_PRIORITY,
[email protected]316c1e5e2012-09-12 15:17:442030 &d,
2031 &default_context_);
2032 req.Start();
[email protected]255620da2013-08-19 13:14:292033 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442034 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2035 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2036 }
2037
2038 // Verify that the cookie is set.
2039 {
2040 TestNetworkDelegate network_delegate;
2041 default_context_.set_network_delegate(&network_delegate);
2042 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:192043 URLRequest req(test_server.GetURL("echoheader?Cookie"),
2044 DEFAULT_PRIORITY,
2045 &d,
2046 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:442047 req.Start();
[email protected]255620da2013-08-19 13:14:292048 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442049
2050 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2051 != std::string::npos);
2052 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2053 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2054 }
2055
2056 // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set.
2057 {
2058 TestNetworkDelegate network_delegate;
2059 default_context_.set_network_delegate(&network_delegate);
2060 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:192061 URLRequest req(test_server.GetURL("echoheader?Cookie"),
2062 DEFAULT_PRIORITY,
2063 &d,
2064 &default_context_);
[email protected]bb1c4662013-11-14 00:00:072065 req.SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES);
[email protected]316c1e5e2012-09-12 15:17:442066 req.Start();
[email protected]255620da2013-08-19 13:14:292067 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442068
2069 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
2070 == std::string::npos);
2071
2072 // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies.
2073 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2074 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2075 }
2076}
2077
2078TEST_F(URLRequestTest, DoNotSaveCookies) {
2079 LocalHttpTestServer test_server;
2080 ASSERT_TRUE(test_server.Start());
2081
2082 // Set up a cookie.
2083 {
2084 TestNetworkDelegate network_delegate;
2085 default_context_.set_network_delegate(&network_delegate);
2086 TestDelegate d;
2087 URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"),
[email protected]2ca01e52013-10-31 22:05:192088 DEFAULT_PRIORITY,
[email protected]316c1e5e2012-09-12 15:17:442089 &d,
2090 &default_context_);
2091 req.Start();
[email protected]255620da2013-08-19 13:14:292092 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442093
2094 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2095 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2096 EXPECT_EQ(1, network_delegate.set_cookie_count());
2097 }
2098
2099 // Try to set-up another cookie and update the previous cookie.
2100 {
2101 TestNetworkDelegate network_delegate;
2102 default_context_.set_network_delegate(&network_delegate);
2103 TestDelegate d;
2104 URLRequest req(
2105 test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
[email protected]2ca01e52013-10-31 22:05:192106 DEFAULT_PRIORITY,
[email protected]316c1e5e2012-09-12 15:17:442107 &d,
2108 &default_context_);
[email protected]bb1c4662013-11-14 00:00:072109 req.SetLoadFlags(LOAD_DO_NOT_SAVE_COOKIES);
[email protected]316c1e5e2012-09-12 15:17:442110 req.Start();
2111
[email protected]255620da2013-08-19 13:14:292112 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442113
2114 // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie.
2115 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2116 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2117 EXPECT_EQ(0, network_delegate.set_cookie_count());
2118 }
2119
2120 // Verify the cookies weren't saved or updated.
2121 {
2122 TestNetworkDelegate network_delegate;
2123 default_context_.set_network_delegate(&network_delegate);
2124 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:192125 URLRequest req(test_server.GetURL("echoheader?Cookie"),
2126 DEFAULT_PRIORITY,
2127 &d,
2128 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:442129 req.Start();
[email protected]255620da2013-08-19 13:14:292130 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442131
2132 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
2133 == std::string::npos);
2134 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
2135 != std::string::npos);
2136
2137 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2138 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2139 EXPECT_EQ(0, network_delegate.set_cookie_count());
2140 }
2141}
2142
2143TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) {
2144 LocalHttpTestServer test_server;
2145 ASSERT_TRUE(test_server.Start());
2146
2147 // Set up a cookie.
2148 {
2149 TestNetworkDelegate network_delegate;
2150 default_context_.set_network_delegate(&network_delegate);
2151 TestDelegate d;
2152 URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"),
[email protected]2ca01e52013-10-31 22:05:192153 DEFAULT_PRIORITY,
[email protected]316c1e5e2012-09-12 15:17:442154 &d,
2155 &default_context_);
2156 req.Start();
[email protected]255620da2013-08-19 13:14:292157 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442158
2159 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2160 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2161 }
2162
2163 // Verify that the cookie is set.
2164 {
2165 TestNetworkDelegate network_delegate;
2166 default_context_.set_network_delegate(&network_delegate);
2167 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:192168 URLRequest req(test_server.GetURL("echoheader?Cookie"),
2169 DEFAULT_PRIORITY,
2170 &d,
2171 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:442172 req.Start();
[email protected]255620da2013-08-19 13:14:292173 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442174
2175 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2176 != std::string::npos);
2177
2178 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2179 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2180 }
2181
2182 // Verify that the cookie isn't sent.
2183 {
2184 TestNetworkDelegate network_delegate;
2185 default_context_.set_network_delegate(&network_delegate);
2186 TestDelegate d;
2187 network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES);
[email protected]2ca01e52013-10-31 22:05:192188 URLRequest req(test_server.GetURL("echoheader?Cookie"),
2189 DEFAULT_PRIORITY,
2190 &d,
2191 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:442192 req.Start();
[email protected]255620da2013-08-19 13:14:292193 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442194
2195 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
2196 == std::string::npos);
2197
[email protected]22e045f2013-09-20 03:54:032198 EXPECT_EQ(1, network_delegate.blocked_get_cookies_count());
[email protected]316c1e5e2012-09-12 15:17:442199 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2200 }
2201}
2202
2203TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) {
2204 LocalHttpTestServer test_server;
2205 ASSERT_TRUE(test_server.Start());
2206
2207 // Set up a cookie.
2208 {
2209 TestNetworkDelegate network_delegate;
2210 default_context_.set_network_delegate(&network_delegate);
2211 TestDelegate d;
2212 URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"),
[email protected]2ca01e52013-10-31 22:05:192213 DEFAULT_PRIORITY,
[email protected]316c1e5e2012-09-12 15:17:442214 &d,
2215 &default_context_);
2216 req.Start();
[email protected]255620da2013-08-19 13:14:292217 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442218
2219 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2220 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2221 }
2222
2223 // Try to set-up another cookie and update the previous cookie.
2224 {
2225 TestNetworkDelegate network_delegate;
2226 default_context_.set_network_delegate(&network_delegate);
2227 TestDelegate d;
2228 network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE);
2229 URLRequest req(
2230 test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
[email protected]2ca01e52013-10-31 22:05:192231 DEFAULT_PRIORITY,
[email protected]316c1e5e2012-09-12 15:17:442232 &d,
2233 &default_context_);
2234 req.Start();
2235
[email protected]255620da2013-08-19 13:14:292236 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442237
2238 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2239 EXPECT_EQ(2, network_delegate.blocked_set_cookie_count());
2240 }
2241
2242 // Verify the cookies weren't saved or updated.
2243 {
2244 TestNetworkDelegate network_delegate;
2245 default_context_.set_network_delegate(&network_delegate);
2246 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:192247 URLRequest req(test_server.GetURL("echoheader?Cookie"),
2248 DEFAULT_PRIORITY,
2249 &d,
2250 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:442251 req.Start();
[email protected]255620da2013-08-19 13:14:292252 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442253
2254 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
2255 == std::string::npos);
2256 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
2257 != std::string::npos);
2258
2259 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2260 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2261 }
2262}
2263
2264TEST_F(URLRequestTest, DoNotSaveEmptyCookies) {
2265 LocalHttpTestServer test_server;
2266 ASSERT_TRUE(test_server.Start());
2267
2268 // Set up an empty cookie.
2269 {
2270 TestNetworkDelegate network_delegate;
2271 default_context_.set_network_delegate(&network_delegate);
2272 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:192273 URLRequest req(test_server.GetURL("set-cookie"),
2274 DEFAULT_PRIORITY,
2275 &d,
2276 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:442277 req.Start();
[email protected]255620da2013-08-19 13:14:292278 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442279
2280 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2281 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2282 EXPECT_EQ(0, network_delegate.set_cookie_count());
2283 }
2284}
2285
2286TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) {
2287 LocalHttpTestServer test_server;
2288 ASSERT_TRUE(test_server.Start());
2289
2290 // Set up a cookie.
2291 {
2292 TestNetworkDelegate network_delegate;
2293 default_context_.set_network_delegate(&network_delegate);
2294 TestDelegate d;
2295 URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"),
[email protected]2ca01e52013-10-31 22:05:192296 DEFAULT_PRIORITY,
[email protected]316c1e5e2012-09-12 15:17:442297 &d,
2298 &default_context_);
2299 req.Start();
[email protected]255620da2013-08-19 13:14:292300 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442301
2302 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2303 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2304 }
2305
2306 // Verify that the cookie is set.
2307 {
2308 TestNetworkDelegate network_delegate;
2309 default_context_.set_network_delegate(&network_delegate);
2310 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:192311 URLRequest req(test_server.GetURL("echoheader?Cookie"),
2312 DEFAULT_PRIORITY,
2313 &d,
2314 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:442315 req.Start();
[email protected]255620da2013-08-19 13:14:292316 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442317
2318 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2319 != std::string::npos);
2320
2321 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2322 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2323 }
2324
2325 // Verify that the cookie isn't sent.
2326 {
2327 TestNetworkDelegate network_delegate;
2328 default_context_.set_network_delegate(&network_delegate);
2329 TestDelegate d;
2330 network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES);
[email protected]2ca01e52013-10-31 22:05:192331 URLRequest req(test_server.GetURL("echoheader?Cookie"),
2332 DEFAULT_PRIORITY,
2333 &d,
2334 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:442335 req.Start();
[email protected]255620da2013-08-19 13:14:292336 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442337
2338 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
2339 == std::string::npos);
2340
[email protected]22e045f2013-09-20 03:54:032341 EXPECT_EQ(1, network_delegate.blocked_get_cookies_count());
[email protected]316c1e5e2012-09-12 15:17:442342 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2343 }
2344}
2345
2346TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) {
2347 LocalHttpTestServer test_server;
2348 ASSERT_TRUE(test_server.Start());
2349
2350 // Set up a cookie.
2351 {
2352 TestNetworkDelegate network_delegate;
2353 default_context_.set_network_delegate(&network_delegate);
2354 TestDelegate d;
2355 URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"),
[email protected]2ca01e52013-10-31 22:05:192356 DEFAULT_PRIORITY,
[email protected]316c1e5e2012-09-12 15:17:442357 &d,
2358 &default_context_);
2359 req.Start();
[email protected]255620da2013-08-19 13:14:292360 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442361
2362 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2363 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2364 }
2365
2366 // Try to set-up another cookie and update the previous cookie.
2367 {
2368 TestNetworkDelegate network_delegate;
2369 default_context_.set_network_delegate(&network_delegate);
2370 TestDelegate d;
2371 network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE);
2372 URLRequest req(
2373 test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
[email protected]2ca01e52013-10-31 22:05:192374 DEFAULT_PRIORITY,
[email protected]316c1e5e2012-09-12 15:17:442375 &d,
2376 &default_context_);
2377 req.Start();
2378
[email protected]255620da2013-08-19 13:14:292379 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442380
2381 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2382 EXPECT_EQ(2, network_delegate.blocked_set_cookie_count());
2383 }
2384
2385 // Verify the cookies weren't saved or updated.
2386 {
2387 TestNetworkDelegate network_delegate;
2388 default_context_.set_network_delegate(&network_delegate);
2389 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:192390 URLRequest req(test_server.GetURL("echoheader?Cookie"),
2391 DEFAULT_PRIORITY,
2392 &d,
2393 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:442394 req.Start();
[email protected]255620da2013-08-19 13:14:292395 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442396
2397 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
2398 == std::string::npos);
2399 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
2400 != std::string::npos);
2401
2402 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2403 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2404 }
2405}
2406
[email protected]5095cd72012-11-01 10:29:162407// FixedDateNetworkDelegate swaps out the server's HTTP Date response header
2408// value for the |fixed_date| argument given to the constructor.
2409class FixedDateNetworkDelegate : public TestNetworkDelegate {
2410 public:
2411 explicit FixedDateNetworkDelegate(const std::string& fixed_date)
2412 : fixed_date_(fixed_date) {}
2413 virtual ~FixedDateNetworkDelegate() {}
2414
2415 // net::NetworkDelegate implementation
2416 virtual int OnHeadersReceived(
2417 net::URLRequest* request,
2418 const net::CompletionCallback& callback,
2419 const net::HttpResponseHeaders* original_response_headers,
2420 scoped_refptr<net::HttpResponseHeaders>* override_response_headers)
2421 OVERRIDE;
2422
2423 private:
2424 std::string fixed_date_;
2425
2426 DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate);
2427};
2428
2429int FixedDateNetworkDelegate::OnHeadersReceived(
2430 net::URLRequest* request,
2431 const net::CompletionCallback& callback,
2432 const net::HttpResponseHeaders* original_response_headers,
2433 scoped_refptr<net::HttpResponseHeaders>* override_response_headers) {
2434 net::HttpResponseHeaders* new_response_headers =
2435 new net::HttpResponseHeaders(original_response_headers->raw_headers());
2436
2437 new_response_headers->RemoveHeader("Date");
2438 new_response_headers->AddHeader("Date: " + fixed_date_);
2439
2440 *override_response_headers = new_response_headers;
2441 return TestNetworkDelegate::OnHeadersReceived(request,
2442 callback,
2443 original_response_headers,
2444 override_response_headers);
2445}
2446
2447// Test that cookie expiration times are adjusted for server/client clock
2448// skew and that we handle incorrect timezone specifier "UTC" in HTTP Date
2449// headers by defaulting to GMT. (crbug.com/135131)
2450TEST_F(URLRequestTest, AcceptClockSkewCookieWithWrongDateTimezone) {
2451 LocalHttpTestServer test_server;
2452 ASSERT_TRUE(test_server.Start());
2453
2454 // Set up an expired cookie.
2455 {
2456 TestNetworkDelegate network_delegate;
2457 default_context_.set_network_delegate(&network_delegate);
2458 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:192459 URLRequest req(
2460 test_server.GetURL(
2461 "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"),
2462 DEFAULT_PRIORITY,
[email protected]5095cd72012-11-01 10:29:162463 &d,
2464 &default_context_);
2465 req.Start();
[email protected]255620da2013-08-19 13:14:292466 base::RunLoop().Run();
[email protected]5095cd72012-11-01 10:29:162467 }
2468 // Verify that the cookie is not set.
2469 {
2470 TestNetworkDelegate network_delegate;
2471 default_context_.set_network_delegate(&network_delegate);
2472 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:192473 URLRequest req(test_server.GetURL("echoheader?Cookie"),
2474 DEFAULT_PRIORITY,
2475 &d,
2476 &default_context_);
[email protected]5095cd72012-11-01 10:29:162477 req.Start();
[email protected]255620da2013-08-19 13:14:292478 base::RunLoop().Run();
[email protected]5095cd72012-11-01 10:29:162479
2480 EXPECT_TRUE(d.data_received().find("StillGood=1") == std::string::npos);
2481 }
2482 // Set up a cookie with clock skew and "UTC" HTTP Date timezone specifier.
2483 {
2484 FixedDateNetworkDelegate network_delegate("18-Apr-1977 22:49:13 UTC");
2485 default_context_.set_network_delegate(&network_delegate);
2486 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:192487 URLRequest req(
2488 test_server.GetURL(
2489 "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"),
2490 DEFAULT_PRIORITY,
[email protected]5095cd72012-11-01 10:29:162491 &d,
2492 &default_context_);
2493 req.Start();
[email protected]255620da2013-08-19 13:14:292494 base::RunLoop().Run();
[email protected]5095cd72012-11-01 10:29:162495 }
2496 // Verify that the cookie is set.
2497 {
2498 TestNetworkDelegate network_delegate;
2499 default_context_.set_network_delegate(&network_delegate);
2500 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:192501 URLRequest req(test_server.GetURL("echoheader?Cookie"),
2502 DEFAULT_PRIORITY,
2503 &d,
2504 &default_context_);
[email protected]5095cd72012-11-01 10:29:162505 req.Start();
[email protected]255620da2013-08-19 13:14:292506 base::RunLoop().Run();
[email protected]5095cd72012-11-01 10:29:162507
2508 EXPECT_TRUE(d.data_received().find("StillGood=1") != std::string::npos);
2509 }
2510}
2511
2512
[email protected]316c1e5e2012-09-12 15:17:442513// Check that it is impossible to change the referrer in the extra headers of
2514// an URLRequest.
2515TEST_F(URLRequestTest, DoNotOverrideReferrer) {
2516 LocalHttpTestServer test_server;
2517 ASSERT_TRUE(test_server.Start());
2518
2519 // If extra headers contain referer and the request contains a referer,
2520 // only the latter shall be respected.
2521 {
2522 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:192523 URLRequest req(test_server.GetURL("echoheader?Referer"),
2524 DEFAULT_PRIORITY,
2525 &d,
2526 &default_context_);
[email protected]99ecf6e2013-04-10 22:46:132527 req.SetReferrer("http://foo.com/");
[email protected]316c1e5e2012-09-12 15:17:442528
2529 HttpRequestHeaders headers;
2530 headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/");
2531 req.SetExtraRequestHeaders(headers);
2532
2533 req.Start();
[email protected]255620da2013-08-19 13:14:292534 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442535
2536 EXPECT_EQ("http://foo.com/", d.data_received());
2537 }
2538
2539 // If extra headers contain a referer but the request does not, no referer
2540 // shall be sent in the header.
2541 {
2542 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:192543 URLRequest req(test_server.GetURL("echoheader?Referer"),
2544 DEFAULT_PRIORITY,
2545 &d,
2546 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:442547
2548 HttpRequestHeaders headers;
2549 headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/");
2550 req.SetExtraRequestHeaders(headers);
[email protected]bb1c4662013-11-14 00:00:072551 req.SetLoadFlags(LOAD_VALIDATE_CACHE);
[email protected]316c1e5e2012-09-12 15:17:442552
2553 req.Start();
[email protected]255620da2013-08-19 13:14:292554 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442555
2556 EXPECT_EQ("None", d.data_received());
2557 }
2558}
2559
[email protected]b89290212009-08-14 22:37:352560class URLRequestTestHTTP : public URLRequestTest {
[email protected]95409e12010-08-17 20:07:112561 public:
2562 URLRequestTestHTTP()
[email protected]6cdfd7f2013-02-08 20:40:152563 : test_server_(base::FilePath(FILE_PATH_LITERAL(
[email protected]95409e12010-08-17 20:07:112564 "net/data/url_request_unittest"))) {
2565 }
2566
[email protected]b89290212009-08-14 22:37:352567 protected:
[email protected]21184962011-10-26 00:50:302568 // Requests |redirect_url|, which must return a HTTP 3xx redirect.
2569 // |request_method| is the method to use for the initial request.
2570 // |redirect_method| is the method that is expected to be used for the second
2571 // request, after redirection.
2572 // If |include_data| is true, data is uploaded with the request. The
2573 // response body is expected to match it exactly, if and only if
2574 // |request_method| == |redirect_method|.
2575 void HTTPRedirectMethodTest(const GURL& redirect_url,
2576 const std::string& request_method,
2577 const std::string& redirect_method,
2578 bool include_data) {
2579 static const char kData[] = "hello world";
2580 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:192581 URLRequest req(redirect_url, DEFAULT_PRIORITY, &d, &default_context_);
[email protected]21184962011-10-26 00:50:302582 req.set_method(request_method);
2583 if (include_data) {
[email protected]f288ef02012-12-15 20:28:282584 req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
[email protected]21184962011-10-26 00:50:302585 HttpRequestHeaders headers;
2586 headers.SetHeader(HttpRequestHeaders::kContentLength,
2587 base::UintToString(arraysize(kData) - 1));
2588 req.SetExtraRequestHeaders(headers);
2589 }
2590 req.Start();
[email protected]255620da2013-08-19 13:14:292591 base::RunLoop().Run();
[email protected]21184962011-10-26 00:50:302592 EXPECT_EQ(redirect_method, req.method());
2593 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
2594 EXPECT_EQ(OK, req.status().error());
2595 if (include_data) {
2596 if (request_method == redirect_method) {
2597 EXPECT_EQ(kData, d.data_received());
2598 } else {
2599 EXPECT_NE(kData, d.data_received());
2600 }
2601 }
2602 if (HasFailure())
2603 LOG(WARNING) << "Request method was: " << request_method;
2604 }
2605
[email protected]762d2db2010-01-11 19:03:012606 void HTTPUploadDataOperationTest(const std::string& method) {
[email protected]762d2db2010-01-11 19:03:012607 const int kMsgSize = 20000; // multiple of 10
2608 const int kIterations = 50;
[email protected]f43b89f32012-05-01 19:39:482609 char* uploadBytes = new char[kMsgSize+1];
2610 char* ptr = uploadBytes;
[email protected]762d2db2010-01-11 19:03:012611 char marker = 'a';
2612 for (int idx = 0; idx < kMsgSize/10; idx++) {
2613 memcpy(ptr, "----------", 10);
2614 ptr += 10;
2615 if (idx % 100 == 0) {
2616 ptr--;
2617 *ptr++ = marker;
2618 if (++marker > 'z')
2619 marker = 'a';
2620 }
2621 }
2622 uploadBytes[kMsgSize] = '\0';
2623
[email protected]762d2db2010-01-11 19:03:012624 for (int i = 0; i < kIterations; ++i) {
2625 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:192626 URLRequest r(
2627 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, &default_context_);
[email protected]762d2db2010-01-11 19:03:012628 r.set_method(method.c_str());
2629
[email protected]f288ef02012-12-15 20:28:282630 r.set_upload(make_scoped_ptr(CreateSimpleUploadData(uploadBytes)));
[email protected]762d2db2010-01-11 19:03:012631
2632 r.Start();
2633 EXPECT_TRUE(r.is_pending());
2634
[email protected]255620da2013-08-19 13:14:292635 base::RunLoop().Run();
[email protected]762d2db2010-01-11 19:03:012636
[email protected]329b68b2012-11-14 17:54:272637 ASSERT_EQ(1, d.response_started_count())
2638 << "request failed: " << r.status().status()
2639 << ", os error: " << r.status().error();
[email protected]762d2db2010-01-11 19:03:012640
2641 EXPECT_FALSE(d.received_data_before_response());
2642 EXPECT_EQ(uploadBytes, d.data_received());
[email protected]762d2db2010-01-11 19:03:012643 }
2644 delete[] uploadBytes;
2645 }
2646
[email protected]ef2bf422012-05-11 03:27:092647 void AddChunksToUpload(URLRequest* r) {
[email protected]0c9bf872011-03-04 17:53:222648 r->AppendChunkToUpload("a", 1, false);
2649 r->AppendChunkToUpload("bcd", 3, false);
2650 r->AppendChunkToUpload("this is a longer chunk than before.", 35, false);
2651 r->AppendChunkToUpload("\r\n\r\n", 4, false);
2652 r->AppendChunkToUpload("0", 1, false);
2653 r->AppendChunkToUpload("2323", 4, true);
[email protected]699efe602011-01-25 07:17:112654 }
2655
[email protected]ef2bf422012-05-11 03:27:092656 void VerifyReceivedDataMatchesChunks(URLRequest* r, TestDelegate* d) {
[email protected]699efe602011-01-25 07:17:112657 // This should match the chunks sent by AddChunksToUpload().
[email protected]329b68b2012-11-14 17:54:272658 const std::string expected_data =
[email protected]699efe602011-01-25 07:17:112659 "abcdthis is a longer chunk than before.\r\n\r\n02323";
2660
[email protected]329b68b2012-11-14 17:54:272661 ASSERT_EQ(1, d->response_started_count())
2662 << "request failed: " << r->status().status()
2663 << ", os error: " << r->status().error();
[email protected]699efe602011-01-25 07:17:112664
2665 EXPECT_FALSE(d->received_data_before_response());
2666
[email protected]329b68b2012-11-14 17:54:272667 EXPECT_EQ(expected_data.size(), static_cast<size_t>(d->bytes_received()));
2668 EXPECT_EQ(expected_data, d->data_received());
[email protected]699efe602011-01-25 07:17:112669 }
2670
[email protected]ede03212012-09-07 12:52:262671 bool DoManyCookiesRequest(int num_cookies) {
[email protected]263163f2012-06-14 22:40:342672 TestDelegate d;
2673 URLRequest r(test_server_.GetURL("set-many-cookies?" +
2674 base::IntToString(num_cookies)),
[email protected]2ca01e52013-10-31 22:05:192675 DEFAULT_PRIORITY,
2676 &d,
2677 &default_context_);
[email protected]263163f2012-06-14 22:40:342678
2679 r.Start();
2680 EXPECT_TRUE(r.is_pending());
2681
[email protected]255620da2013-08-19 13:14:292682 base::RunLoop().Run();
[email protected]263163f2012-06-14 22:40:342683
2684 bool is_success = r.status().is_success();
2685
[email protected]ede03212012-09-07 12:52:262686 if (!is_success) {
[email protected]cd6f2522014-01-16 18:27:352687 EXPECT_TRUE(r.status().error() == ERR_RESPONSE_HEADERS_TOO_BIG);
[email protected]263163f2012-06-14 22:40:342688 // The test server appears to be unable to handle subsequent requests
2689 // after this error is triggered. Force it to restart.
2690 EXPECT_TRUE(test_server_.Stop());
2691 EXPECT_TRUE(test_server_.Start());
2692 }
2693
2694 return is_success;
2695 }
2696
[email protected]1700c6a2012-02-22 18:07:072697 LocalHttpTestServer test_server_;
[email protected]b89290212009-08-14 22:37:352698};
2699
[email protected]95409e12010-08-17 20:07:112700// In this unit test, we're using the HTTPTestServer as a proxy server and
2701// issuing a CONNECT request with the magic host name "www.redirect.com".
2702// The HTTPTestServer will return a 302 response, which we should not
2703// follow.
[email protected]f2f31b32013-01-16 23:24:092704TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) {
[email protected]95409e12010-08-17 20:07:112705 ASSERT_TRUE(test_server_.Start());
2706
[email protected]ceefd7fd2012-11-29 00:36:242707 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]d5a4dd62012-05-23 01:41:042708 TestURLRequestContextWithProxy context(
[email protected]2ca01e52013-10-31 22:05:192709 test_server_.host_port_pair().ToString(), &network_delegate);
[email protected]87a09a92011-07-14 15:50:502710
[email protected]d1ec59082009-02-11 02:48:152711 TestDelegate d;
2712 {
[email protected]2ca01e52013-10-31 22:05:192713 URLRequest r(
2714 GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, &context);
[email protected]d1ec59082009-02-11 02:48:152715 r.Start();
2716 EXPECT_TRUE(r.is_pending());
2717
[email protected]255620da2013-08-19 13:14:292718 base::RunLoop().Run();
[email protected]d1ec59082009-02-11 02:48:152719
[email protected]7461a402011-03-24 23:19:512720 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
[email protected]d0cc35b2011-09-08 12:02:052721 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().error());
[email protected]dc651782009-02-14 01:45:082722 EXPECT_EQ(1, d.response_started_count());
[email protected]d1ec59082009-02-11 02:48:152723 // We should not have followed the redirect.
2724 EXPECT_EQ(0, d.received_redirect_count());
2725 }
2726}
2727
[email protected]8202d0c2011-02-23 08:31:142728// This is the same as the previous test, but checks that the network delegate
2729// registers the error.
[email protected]c044616e2013-02-20 02:01:262730TEST_F(URLRequestTestHTTP, NetworkDelegateTunnelConnectionFailed) {
[email protected]8202d0c2011-02-23 08:31:142731 ASSERT_TRUE(test_server_.Start());
2732
[email protected]ceefd7fd2012-11-29 00:36:242733 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]d5a4dd62012-05-23 01:41:042734 TestURLRequestContextWithProxy context(
[email protected]2ca01e52013-10-31 22:05:192735 test_server_.host_port_pair().ToString(), &network_delegate);
[email protected]87a09a92011-07-14 15:50:502736
[email protected]8202d0c2011-02-23 08:31:142737 TestDelegate d;
2738 {
[email protected]2ca01e52013-10-31 22:05:192739 URLRequest r(
2740 GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, &context);
[email protected]8202d0c2011-02-23 08:31:142741 r.Start();
2742 EXPECT_TRUE(r.is_pending());
2743
[email protected]255620da2013-08-19 13:14:292744 base::RunLoop().Run();
[email protected]8202d0c2011-02-23 08:31:142745
[email protected]7461a402011-03-24 23:19:512746 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
[email protected]d0cc35b2011-09-08 12:02:052747 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().error());
[email protected]8202d0c2011-02-23 08:31:142748 EXPECT_EQ(1, d.response_started_count());
2749 // We should not have followed the redirect.
2750 EXPECT_EQ(0, d.received_redirect_count());
2751
2752 EXPECT_EQ(1, network_delegate.error_count());
[email protected]d0cc35b2011-09-08 12:02:052753 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, network_delegate.last_error());
[email protected]8202d0c2011-02-23 08:31:142754 }
2755}
2756
[email protected]dc5a5cf2012-09-26 02:49:302757// Tests that we can block and asynchronously return OK in various stages.
2758TEST_F(URLRequestTestHTTP, NetworkDelegateBlockAsynchronously) {
2759 static const BlockingNetworkDelegate::Stage blocking_stages[] = {
2760 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
2761 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
2762 BlockingNetworkDelegate::ON_HEADERS_RECEIVED
2763 };
2764 static const size_t blocking_stages_length = arraysize(blocking_stages);
2765
2766 ASSERT_TRUE(test_server_.Start());
2767
2768 TestDelegate d;
2769 BlockingNetworkDelegate network_delegate(
2770 BlockingNetworkDelegate::USER_CALLBACK);
2771 network_delegate.set_block_on(
2772 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST |
2773 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS |
2774 BlockingNetworkDelegate::ON_HEADERS_RECEIVED);
2775
2776 TestURLRequestContext context(true);
2777 context.set_network_delegate(&network_delegate);
2778 context.Init();
2779
2780 {
[email protected]2ca01e52013-10-31 22:05:192781 URLRequest r(
2782 test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d, &context);
[email protected]dc5a5cf2012-09-26 02:49:302783
2784 r.Start();
2785 for (size_t i = 0; i < blocking_stages_length; ++i) {
[email protected]255620da2013-08-19 13:14:292786 base::RunLoop().Run();
[email protected]dc5a5cf2012-09-26 02:49:302787 EXPECT_EQ(blocking_stages[i],
2788 network_delegate.stage_blocked_for_callback());
2789 network_delegate.DoCallback(OK);
2790 }
[email protected]255620da2013-08-19 13:14:292791 base::RunLoop().Run();
[email protected]dc5a5cf2012-09-26 02:49:302792 EXPECT_EQ(200, r.GetResponseCode());
2793 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2794 EXPECT_EQ(1, network_delegate.created_requests());
2795 EXPECT_EQ(0, network_delegate.destroyed_requests());
2796 }
2797 EXPECT_EQ(1, network_delegate.destroyed_requests());
2798}
2799
[email protected]4c76d7c2011-04-15 19:14:122800// Tests that the network delegate can block and cancel a request.
2801TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) {
2802 ASSERT_TRUE(test_server_.Start());
2803
2804 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302805 BlockingNetworkDelegate network_delegate(
2806 BlockingNetworkDelegate::AUTO_CALLBACK);
2807 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
2808 network_delegate.set_retval(ERR_EMPTY_RESPONSE);
[email protected]4c76d7c2011-04-15 19:14:122809
[email protected]d5a4dd62012-05-23 01:41:042810 TestURLRequestContextWithProxy context(
[email protected]2ca01e52013-10-31 22:05:192811 test_server_.host_port_pair().ToString(), &network_delegate);
[email protected]87a09a92011-07-14 15:50:502812
[email protected]4c76d7c2011-04-15 19:14:122813 {
[email protected]2ca01e52013-10-31 22:05:192814 URLRequest r(
2815 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, &context);
[email protected]4c76d7c2011-04-15 19:14:122816
2817 r.Start();
[email protected]255620da2013-08-19 13:14:292818 base::RunLoop().Run();
[email protected]4c76d7c2011-04-15 19:14:122819
2820 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
[email protected]d0cc35b2011-09-08 12:02:052821 EXPECT_EQ(ERR_EMPTY_RESPONSE, r.status().error());
[email protected]4c76d7c2011-04-15 19:14:122822 EXPECT_EQ(1, network_delegate.created_requests());
2823 EXPECT_EQ(0, network_delegate.destroyed_requests());
2824 }
2825 EXPECT_EQ(1, network_delegate.destroyed_requests());
2826}
2827
[email protected]b4438d32012-09-27 06:15:302828// Helper function for NetworkDelegateCancelRequestAsynchronously and
2829// NetworkDelegateCancelRequestSynchronously. Sets up a blocking network
2830// delegate operating in |block_mode| and a request for |url|. It blocks the
2831// request in |stage| and cancels it with ERR_BLOCKED_BY_CLIENT.
2832void NetworkDelegateCancelRequest(BlockingNetworkDelegate::BlockMode block_mode,
2833 BlockingNetworkDelegate::Stage stage,
2834 const GURL& url) {
[email protected]3cd384c602011-08-31 16:12:362835 TestDelegate d;
[email protected]b4438d32012-09-27 06:15:302836 BlockingNetworkDelegate network_delegate(block_mode);
2837 network_delegate.set_retval(ERR_BLOCKED_BY_CLIENT);
2838 network_delegate.set_block_on(stage);
[email protected]3cd384c602011-08-31 16:12:362839
[email protected]b4438d32012-09-27 06:15:302840 TestURLRequestContext context(true);
2841 context.set_network_delegate(&network_delegate);
2842 context.Init();
[email protected]3cd384c602011-08-31 16:12:362843
2844 {
[email protected]2ca01e52013-10-31 22:05:192845 URLRequest r(url, DEFAULT_PRIORITY, &d, &context);
[email protected]3cd384c602011-08-31 16:12:362846
2847 r.Start();
[email protected]255620da2013-08-19 13:14:292848 base::RunLoop().Run();
[email protected]3cd384c602011-08-31 16:12:362849
2850 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
[email protected]b4438d32012-09-27 06:15:302851 EXPECT_EQ(ERR_BLOCKED_BY_CLIENT, r.status().error());
[email protected]3cd384c602011-08-31 16:12:362852 EXPECT_EQ(1, network_delegate.created_requests());
2853 EXPECT_EQ(0, network_delegate.destroyed_requests());
2854 }
2855 EXPECT_EQ(1, network_delegate.destroyed_requests());
2856}
2857
[email protected]b4438d32012-09-27 06:15:302858// The following 3 tests check that the network delegate can cancel a request
2859// synchronously in various stages of the request.
2860TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously1) {
2861 ASSERT_TRUE(test_server_.Start());
2862 NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS,
2863 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
[email protected]007b3f82013-04-09 08:46:452864 test_server_.GetURL(std::string()));
[email protected]b4438d32012-09-27 06:15:302865}
2866
2867TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously2) {
2868 ASSERT_TRUE(test_server_.Start());
2869 NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS,
2870 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
[email protected]007b3f82013-04-09 08:46:452871 test_server_.GetURL(std::string()));
[email protected]b4438d32012-09-27 06:15:302872}
2873
2874TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously3) {
2875 ASSERT_TRUE(test_server_.Start());
2876 NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS,
2877 BlockingNetworkDelegate::ON_HEADERS_RECEIVED,
[email protected]007b3f82013-04-09 08:46:452878 test_server_.GetURL(std::string()));
[email protected]b4438d32012-09-27 06:15:302879}
2880
2881// The following 3 tests check that the network delegate can cancel a request
2882// asynchronously in various stages of the request.
2883TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously1) {
2884 ASSERT_TRUE(test_server_.Start());
2885 NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK,
2886 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
[email protected]007b3f82013-04-09 08:46:452887 test_server_.GetURL(std::string()));
[email protected]b4438d32012-09-27 06:15:302888}
2889
2890TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously2) {
2891 ASSERT_TRUE(test_server_.Start());
2892 NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK,
2893 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
[email protected]007b3f82013-04-09 08:46:452894 test_server_.GetURL(std::string()));
[email protected]b4438d32012-09-27 06:15:302895}
2896
2897TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously3) {
2898 ASSERT_TRUE(test_server_.Start());
2899 NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK,
2900 BlockingNetworkDelegate::ON_HEADERS_RECEIVED,
[email protected]007b3f82013-04-09 08:46:452901 test_server_.GetURL(std::string()));
[email protected]b4438d32012-09-27 06:15:302902}
2903
[email protected]4c76d7c2011-04-15 19:14:122904// Tests that the network delegate can block and redirect a request to a new
2905// URL.
2906TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) {
2907 ASSERT_TRUE(test_server_.Start());
2908
2909 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302910 BlockingNetworkDelegate network_delegate(
2911 BlockingNetworkDelegate::AUTO_CALLBACK);
2912 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
[email protected]4c76d7c2011-04-15 19:14:122913 GURL redirect_url(test_server_.GetURL("simple.html"));
2914 network_delegate.set_redirect_url(redirect_url);
2915
[email protected]d5a4dd62012-05-23 01:41:042916 TestURLRequestContextWithProxy context(
[email protected]2ca01e52013-10-31 22:05:192917 test_server_.host_port_pair().ToString(), &network_delegate);
[email protected]87a09a92011-07-14 15:50:502918
[email protected]4c76d7c2011-04-15 19:14:122919 {
[email protected]8f1ac082011-04-19 21:14:132920 GURL original_url(test_server_.GetURL("empty.html"));
[email protected]2ca01e52013-10-31 22:05:192921 URLRequest r(original_url, DEFAULT_PRIORITY, &d, &context);
[email protected]4c76d7c2011-04-15 19:14:122922
2923 r.Start();
[email protected]255620da2013-08-19 13:14:292924 base::RunLoop().Run();
[email protected]4c76d7c2011-04-15 19:14:122925
2926 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
[email protected]d0cc35b2011-09-08 12:02:052927 EXPECT_EQ(0, r.status().error());
[email protected]4c76d7c2011-04-15 19:14:122928 EXPECT_EQ(redirect_url, r.url());
[email protected]8f1ac082011-04-19 21:14:132929 EXPECT_EQ(original_url, r.original_url());
2930 EXPECT_EQ(2U, r.url_chain().size());
[email protected]4c76d7c2011-04-15 19:14:122931 EXPECT_EQ(1, network_delegate.created_requests());
2932 EXPECT_EQ(0, network_delegate.destroyed_requests());
2933 }
2934 EXPECT_EQ(1, network_delegate.destroyed_requests());
2935}
2936
[email protected]b813ed72012-04-05 08:21:362937// Tests that the network delegate can block and redirect a request to a new
2938// URL by setting a redirect_url and returning in OnBeforeURLRequest directly.
2939TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) {
2940 ASSERT_TRUE(test_server_.Start());
2941
2942 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302943 BlockingNetworkDelegate network_delegate(
2944 BlockingNetworkDelegate::SYNCHRONOUS);
[email protected]b813ed72012-04-05 08:21:362945 GURL redirect_url(test_server_.GetURL("simple.html"));
2946 network_delegate.set_redirect_url(redirect_url);
[email protected]b813ed72012-04-05 08:21:362947
[email protected]d5a4dd62012-05-23 01:41:042948 TestURLRequestContextWithProxy context(
[email protected]2ca01e52013-10-31 22:05:192949 test_server_.host_port_pair().ToString(), &network_delegate);
[email protected]b813ed72012-04-05 08:21:362950
2951 {
2952 GURL original_url(test_server_.GetURL("empty.html"));
[email protected]2ca01e52013-10-31 22:05:192953 URLRequest r(original_url, DEFAULT_PRIORITY, &d, &context);
[email protected]b813ed72012-04-05 08:21:362954
2955 r.Start();
[email protected]255620da2013-08-19 13:14:292956 base::RunLoop().Run();
[email protected]b813ed72012-04-05 08:21:362957
2958 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2959 EXPECT_EQ(0, r.status().error());
2960 EXPECT_EQ(redirect_url, r.url());
2961 EXPECT_EQ(original_url, r.original_url());
2962 EXPECT_EQ(2U, r.url_chain().size());
2963 EXPECT_EQ(1, network_delegate.created_requests());
2964 EXPECT_EQ(0, network_delegate.destroyed_requests());
2965 }
2966 EXPECT_EQ(1, network_delegate.destroyed_requests());
2967}
2968
[email protected]3c5ca8c2011-09-29 01:14:512969// Tests that redirects caused by the network delegate preserve POST data.
2970TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) {
2971 ASSERT_TRUE(test_server_.Start());
2972
2973 const char kData[] = "hello world";
2974
2975 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302976 BlockingNetworkDelegate network_delegate(
2977 BlockingNetworkDelegate::AUTO_CALLBACK);
2978 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
[email protected]3c5ca8c2011-09-29 01:14:512979 GURL redirect_url(test_server_.GetURL("echo"));
2980 network_delegate.set_redirect_url(redirect_url);
2981
[email protected]ef2bf422012-05-11 03:27:092982 TestURLRequestContext context(true);
2983 context.set_network_delegate(&network_delegate);
2984 context.Init();
[email protected]3c5ca8c2011-09-29 01:14:512985
2986 {
2987 GURL original_url(test_server_.GetURL("empty.html"));
[email protected]2ca01e52013-10-31 22:05:192988 URLRequest r(original_url, DEFAULT_PRIORITY, &d, &context);
[email protected]3c5ca8c2011-09-29 01:14:512989 r.set_method("POST");
[email protected]f288ef02012-12-15 20:28:282990 r.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
[email protected]3c5ca8c2011-09-29 01:14:512991 HttpRequestHeaders headers;
2992 headers.SetHeader(HttpRequestHeaders::kContentLength,
2993 base::UintToString(arraysize(kData) - 1));
2994 r.SetExtraRequestHeaders(headers);
[email protected]3c5ca8c2011-09-29 01:14:512995 r.Start();
[email protected]255620da2013-08-19 13:14:292996 base::RunLoop().Run();
[email protected]3c5ca8c2011-09-29 01:14:512997
2998 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2999 EXPECT_EQ(0, r.status().error());
3000 EXPECT_EQ(redirect_url, r.url());
3001 EXPECT_EQ(original_url, r.original_url());
3002 EXPECT_EQ(2U, r.url_chain().size());
3003 EXPECT_EQ(1, network_delegate.created_requests());
3004 EXPECT_EQ(0, network_delegate.destroyed_requests());
3005 EXPECT_EQ("POST", r.method());
3006 EXPECT_EQ(kData, d.data_received());
3007 }
3008 EXPECT_EQ(1, network_delegate.destroyed_requests());
3009}
3010
[email protected]c2911d72011-10-03 22:16:363011// Tests that the network delegate can synchronously complete OnAuthRequired
3012// by taking no action. This indicates that the NetworkDelegate does not want to
3013// handle the challenge, and is passing the buck along to the
3014// URLRequest::Delegate.
3015TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) {
3016 ASSERT_TRUE(test_server_.Start());
3017
3018 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303019 BlockingNetworkDelegate network_delegate(
3020 BlockingNetworkDelegate::SYNCHRONOUS);
[email protected]c2911d72011-10-03 22:16:363021
[email protected]ef2bf422012-05-11 03:27:093022 TestURLRequestContext context(true);
3023 context.set_network_delegate(&network_delegate);
3024 context.Init();
[email protected]c2911d72011-10-03 22:16:363025
[email protected]f3cf9802011-10-28 18:44:583026 d.set_credentials(AuthCredentials(kUser, kSecret));
[email protected]c2911d72011-10-03 22:16:363027
3028 {
3029 GURL url(test_server_.GetURL("auth-basic"));
[email protected]2ca01e52013-10-31 22:05:193030 URLRequest r(url, DEFAULT_PRIORITY, &d, &context);
[email protected]c2911d72011-10-03 22:16:363031 r.Start();
[email protected]79e1fd62013-06-20 06:50:043032
[email protected]255620da2013-08-19 13:14:293033 base::RunLoop().Run();
[email protected]79e1fd62013-06-20 06:50:043034
3035 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3036 EXPECT_EQ(0, r.status().error());
3037 EXPECT_EQ(200, r.GetResponseCode());
3038 EXPECT_TRUE(d.auth_required_called());
3039 EXPECT_EQ(1, network_delegate.created_requests());
3040 EXPECT_EQ(0, network_delegate.destroyed_requests());
3041 }
3042 EXPECT_EQ(1, network_delegate.destroyed_requests());
3043}
3044
3045TEST_F(URLRequestTestHTTP,
3046 NetworkDelegateOnAuthRequiredSyncNoAction_GetFullRequestHeaders) {
3047 ASSERT_TRUE(test_server_.Start());
3048
3049 TestDelegate d;
3050 BlockingNetworkDelegate network_delegate(
3051 BlockingNetworkDelegate::SYNCHRONOUS);
3052
3053 TestURLRequestContext context(true);
3054 context.set_network_delegate(&network_delegate);
3055 context.Init();
3056
3057 d.set_credentials(AuthCredentials(kUser, kSecret));
3058
3059 {
3060 GURL url(test_server_.GetURL("auth-basic"));
[email protected]2ca01e52013-10-31 22:05:193061 URLRequest r(url, DEFAULT_PRIORITY, &d, &context);
[email protected]79e1fd62013-06-20 06:50:043062 r.Start();
3063
3064 {
3065 HttpRequestHeaders headers;
3066 EXPECT_TRUE(r.GetFullRequestHeaders(&headers));
3067 EXPECT_FALSE(headers.HasHeader("Authorization"));
3068 }
3069
[email protected]255620da2013-08-19 13:14:293070 base::RunLoop().Run();
[email protected]c2911d72011-10-03 22:16:363071
3072 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3073 EXPECT_EQ(0, r.status().error());
3074 EXPECT_EQ(200, r.GetResponseCode());
3075 EXPECT_TRUE(d.auth_required_called());
3076 EXPECT_EQ(1, network_delegate.created_requests());
3077 EXPECT_EQ(0, network_delegate.destroyed_requests());
3078 }
3079 EXPECT_EQ(1, network_delegate.destroyed_requests());
3080}
3081
3082// Tests that the network delegate can synchronously complete OnAuthRequired
[email protected]1e110eae2013-05-10 22:02:403083// by setting credentials.
[email protected]c2911d72011-10-03 22:16:363084TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) {
3085 ASSERT_TRUE(test_server_.Start());
3086
3087 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303088 BlockingNetworkDelegate network_delegate(
3089 BlockingNetworkDelegate::SYNCHRONOUS);
3090 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]c2911d72011-10-03 22:16:363091 network_delegate.set_auth_retval(
3092 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
3093
[email protected]f3cf9802011-10-28 18:44:583094 network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret));
[email protected]c2911d72011-10-03 22:16:363095
[email protected]ef2bf422012-05-11 03:27:093096 TestURLRequestContext context(true);
3097 context.set_network_delegate(&network_delegate);
3098 context.Init();
[email protected]c2911d72011-10-03 22:16:363099
3100 {
3101 GURL url(test_server_.GetURL("auth-basic"));
[email protected]2ca01e52013-10-31 22:05:193102 URLRequest r(url, DEFAULT_PRIORITY, &d, &context);
[email protected]c2911d72011-10-03 22:16:363103 r.Start();
[email protected]255620da2013-08-19 13:14:293104 base::RunLoop().Run();
[email protected]c2911d72011-10-03 22:16:363105
3106 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3107 EXPECT_EQ(0, r.status().error());
3108 EXPECT_EQ(200, r.GetResponseCode());
3109 EXPECT_FALSE(d.auth_required_called());
3110 EXPECT_EQ(1, network_delegate.created_requests());
3111 EXPECT_EQ(0, network_delegate.destroyed_requests());
3112 }
3113 EXPECT_EQ(1, network_delegate.destroyed_requests());
3114}
3115
[email protected]79e1fd62013-06-20 06:50:043116// Same as above, but also tests that GetFullRequestHeaders returns the proper
3117// headers (for the first or second request) when called at the proper times.
3118TEST_F(URLRequestTestHTTP,
3119 NetworkDelegateOnAuthRequiredSyncSetAuth_GetFullRequestHeaders) {
3120 ASSERT_TRUE(test_server_.Start());
3121
3122 TestDelegate d;
3123 BlockingNetworkDelegate network_delegate(
3124 BlockingNetworkDelegate::SYNCHRONOUS);
3125 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
3126 network_delegate.set_auth_retval(
3127 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
3128
3129 network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret));
3130
3131 TestURLRequestContext context(true);
3132 context.set_network_delegate(&network_delegate);
3133 context.Init();
3134
3135 {
3136 GURL url(test_server_.GetURL("auth-basic"));
[email protected]2ca01e52013-10-31 22:05:193137 URLRequest r(url, DEFAULT_PRIORITY, &d, &context);
[email protected]79e1fd62013-06-20 06:50:043138 r.Start();
[email protected]255620da2013-08-19 13:14:293139 base::RunLoop().Run();
[email protected]79e1fd62013-06-20 06:50:043140
3141 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3142 EXPECT_EQ(0, r.status().error());
3143 EXPECT_EQ(200, r.GetResponseCode());
3144 EXPECT_FALSE(d.auth_required_called());
3145 EXPECT_EQ(1, network_delegate.created_requests());
3146 EXPECT_EQ(0, network_delegate.destroyed_requests());
3147
3148 {
3149 HttpRequestHeaders headers;
3150 EXPECT_TRUE(r.GetFullRequestHeaders(&headers));
3151 EXPECT_TRUE(headers.HasHeader("Authorization"));
3152 }
3153 }
3154 EXPECT_EQ(1, network_delegate.destroyed_requests());
3155}
3156
[email protected]c2911d72011-10-03 22:16:363157// Tests that the network delegate can synchronously complete OnAuthRequired
3158// by cancelling authentication.
3159TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) {
3160 ASSERT_TRUE(test_server_.Start());
3161
3162 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303163 BlockingNetworkDelegate network_delegate(
3164 BlockingNetworkDelegate::SYNCHRONOUS);
3165 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]c2911d72011-10-03 22:16:363166 network_delegate.set_auth_retval(
3167 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH);
3168
[email protected]ef2bf422012-05-11 03:27:093169 TestURLRequestContext context(true);
3170 context.set_network_delegate(&network_delegate);
3171 context.Init();
[email protected]c2911d72011-10-03 22:16:363172
3173 {
3174 GURL url(test_server_.GetURL("auth-basic"));
[email protected]2ca01e52013-10-31 22:05:193175 URLRequest r(url, DEFAULT_PRIORITY, &d, &context);
[email protected]c2911d72011-10-03 22:16:363176 r.Start();
[email protected]255620da2013-08-19 13:14:293177 base::RunLoop().Run();
[email protected]c2911d72011-10-03 22:16:363178
3179 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3180 EXPECT_EQ(OK, r.status().error());
3181 EXPECT_EQ(401, r.GetResponseCode());
3182 EXPECT_FALSE(d.auth_required_called());
3183 EXPECT_EQ(1, network_delegate.created_requests());
3184 EXPECT_EQ(0, network_delegate.destroyed_requests());
3185 }
3186 EXPECT_EQ(1, network_delegate.destroyed_requests());
3187}
3188
3189// Tests that the network delegate can asynchronously complete OnAuthRequired
3190// by taking no action. This indicates that the NetworkDelegate does not want
3191// to handle the challenge, and is passing the buck along to the
3192// URLRequest::Delegate.
3193TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) {
3194 ASSERT_TRUE(test_server_.Start());
3195
3196 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303197 BlockingNetworkDelegate network_delegate(
3198 BlockingNetworkDelegate::AUTO_CALLBACK);
3199 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]c2911d72011-10-03 22:16:363200
[email protected]ef2bf422012-05-11 03:27:093201 TestURLRequestContext context(true);
3202 context.set_network_delegate(&network_delegate);
3203 context.Init();
[email protected]c2911d72011-10-03 22:16:363204
[email protected]f3cf9802011-10-28 18:44:583205 d.set_credentials(AuthCredentials(kUser, kSecret));
[email protected]c2911d72011-10-03 22:16:363206
3207 {
3208 GURL url(test_server_.GetURL("auth-basic"));
[email protected]2ca01e52013-10-31 22:05:193209 URLRequest r(url, DEFAULT_PRIORITY, &d, &context);
[email protected]c2911d72011-10-03 22:16:363210 r.Start();
[email protected]255620da2013-08-19 13:14:293211 base::RunLoop().Run();
[email protected]c2911d72011-10-03 22:16:363212
3213 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3214 EXPECT_EQ(0, r.status().error());
3215 EXPECT_EQ(200, r.GetResponseCode());
3216 EXPECT_TRUE(d.auth_required_called());
3217 EXPECT_EQ(1, network_delegate.created_requests());
3218 EXPECT_EQ(0, network_delegate.destroyed_requests());
3219 }
3220 EXPECT_EQ(1, network_delegate.destroyed_requests());
3221}
3222
3223// Tests that the network delegate can asynchronously complete OnAuthRequired
3224// by setting credentials.
3225TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) {
3226 ASSERT_TRUE(test_server_.Start());
3227
3228 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303229 BlockingNetworkDelegate network_delegate(
3230 BlockingNetworkDelegate::AUTO_CALLBACK);
3231 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]c2911d72011-10-03 22:16:363232 network_delegate.set_auth_retval(
[email protected]c2911d72011-10-03 22:16:363233 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
3234
[email protected]f3cf9802011-10-28 18:44:583235 AuthCredentials auth_credentials(kUser, kSecret);
[email protected]c2911d72011-10-03 22:16:363236 network_delegate.set_auth_credentials(auth_credentials);
3237
[email protected]ef2bf422012-05-11 03:27:093238 TestURLRequestContext context(true);
3239 context.set_network_delegate(&network_delegate);
3240 context.Init();
[email protected]c2911d72011-10-03 22:16:363241
3242 {
3243 GURL url(test_server_.GetURL("auth-basic"));
[email protected]2ca01e52013-10-31 22:05:193244 URLRequest r(url, DEFAULT_PRIORITY, &d, &context);
[email protected]c2911d72011-10-03 22:16:363245 r.Start();
[email protected]255620da2013-08-19 13:14:293246 base::RunLoop().Run();
[email protected]c2911d72011-10-03 22:16:363247
3248 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3249 EXPECT_EQ(0, r.status().error());
3250
3251 EXPECT_EQ(200, r.GetResponseCode());
3252 EXPECT_FALSE(d.auth_required_called());
3253 EXPECT_EQ(1, network_delegate.created_requests());
3254 EXPECT_EQ(0, network_delegate.destroyed_requests());
3255 }
3256 EXPECT_EQ(1, network_delegate.destroyed_requests());
3257}
3258
3259// Tests that the network delegate can asynchronously complete OnAuthRequired
3260// by cancelling authentication.
3261TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) {
3262 ASSERT_TRUE(test_server_.Start());
3263
3264 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303265 BlockingNetworkDelegate network_delegate(
3266 BlockingNetworkDelegate::AUTO_CALLBACK);
3267 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]c2911d72011-10-03 22:16:363268 network_delegate.set_auth_retval(
[email protected]c2911d72011-10-03 22:16:363269 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH);
3270
[email protected]ef2bf422012-05-11 03:27:093271 TestURLRequestContext context(true);
3272 context.set_network_delegate(&network_delegate);
3273 context.Init();
[email protected]c2911d72011-10-03 22:16:363274
3275 {
3276 GURL url(test_server_.GetURL("auth-basic"));
[email protected]2ca01e52013-10-31 22:05:193277 URLRequest r(url, DEFAULT_PRIORITY, &d, &context);
[email protected]c2911d72011-10-03 22:16:363278 r.Start();
[email protected]255620da2013-08-19 13:14:293279 base::RunLoop().Run();
[email protected]c2911d72011-10-03 22:16:363280
3281 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3282 EXPECT_EQ(OK, r.status().error());
3283 EXPECT_EQ(401, r.GetResponseCode());
3284 EXPECT_FALSE(d.auth_required_called());
3285 EXPECT_EQ(1, network_delegate.created_requests());
3286 EXPECT_EQ(0, network_delegate.destroyed_requests());
3287 }
3288 EXPECT_EQ(1, network_delegate.destroyed_requests());
3289}
3290
[email protected]9045b8822012-01-13 20:35:353291// Tests that we can handle when a network request was canceled while we were
3292// waiting for the network delegate.
3293// Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback.
3294TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) {
3295 ASSERT_TRUE(test_server_.Start());
3296
3297 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303298 BlockingNetworkDelegate network_delegate(
3299 BlockingNetworkDelegate::USER_CALLBACK);
3300 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
[email protected]9045b8822012-01-13 20:35:353301
[email protected]ef2bf422012-05-11 03:27:093302 TestURLRequestContext context(true);
3303 context.set_network_delegate(&network_delegate);
3304 context.Init();
[email protected]9045b8822012-01-13 20:35:353305
3306 {
[email protected]2ca01e52013-10-31 22:05:193307 URLRequest r(
3308 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, &context);
[email protected]9045b8822012-01-13 20:35:353309
3310 r.Start();
[email protected]255620da2013-08-19 13:14:293311 base::RunLoop().Run();
[email protected]dc5a5cf2012-09-26 02:49:303312 EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
3313 network_delegate.stage_blocked_for_callback());
[email protected]9045b8822012-01-13 20:35:353314 EXPECT_EQ(0, network_delegate.completed_requests());
3315 // Cancel before callback.
3316 r.Cancel();
3317 // Ensure that network delegate is notified.
3318 EXPECT_EQ(1, network_delegate.completed_requests());
3319 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
3320 EXPECT_EQ(ERR_ABORTED, r.status().error());
3321 EXPECT_EQ(1, network_delegate.created_requests());
3322 EXPECT_EQ(0, network_delegate.destroyed_requests());
3323 }
3324 EXPECT_EQ(1, network_delegate.destroyed_requests());
3325}
3326
3327// Tests that we can handle when a network request was canceled while we were
3328// waiting for the network delegate.
3329// Part 2: Request is cancelled while waiting for OnBeforeSendHeaders callback.
3330TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) {
3331 ASSERT_TRUE(test_server_.Start());
3332
3333 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303334 BlockingNetworkDelegate network_delegate(
3335 BlockingNetworkDelegate::USER_CALLBACK);
3336 network_delegate.set_block_on(
3337 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS);
[email protected]9045b8822012-01-13 20:35:353338
[email protected]ef2bf422012-05-11 03:27:093339 TestURLRequestContext context(true);
3340 context.set_network_delegate(&network_delegate);
3341 context.Init();
[email protected]9045b8822012-01-13 20:35:353342
3343 {
[email protected]2ca01e52013-10-31 22:05:193344 URLRequest r(
3345 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, &context);
[email protected]9045b8822012-01-13 20:35:353346
3347 r.Start();
[email protected]255620da2013-08-19 13:14:293348 base::RunLoop().Run();
[email protected]dc5a5cf2012-09-26 02:49:303349 EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
3350 network_delegate.stage_blocked_for_callback());
[email protected]9045b8822012-01-13 20:35:353351 EXPECT_EQ(0, network_delegate.completed_requests());
3352 // Cancel before callback.
3353 r.Cancel();
3354 // Ensure that network delegate is notified.
3355 EXPECT_EQ(1, network_delegate.completed_requests());
3356 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
3357 EXPECT_EQ(ERR_ABORTED, r.status().error());
3358 EXPECT_EQ(1, network_delegate.created_requests());
3359 EXPECT_EQ(0, network_delegate.destroyed_requests());
3360 }
3361 EXPECT_EQ(1, network_delegate.destroyed_requests());
3362}
3363
3364// Tests that we can handle when a network request was canceled while we were
3365// waiting for the network delegate.
3366// Part 3: Request is cancelled while waiting for OnHeadersReceived callback.
3367TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) {
3368 ASSERT_TRUE(test_server_.Start());
3369
3370 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303371 BlockingNetworkDelegate network_delegate(
3372 BlockingNetworkDelegate::USER_CALLBACK);
3373 network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED);
[email protected]9045b8822012-01-13 20:35:353374
[email protected]ef2bf422012-05-11 03:27:093375 TestURLRequestContext context(true);
3376 context.set_network_delegate(&network_delegate);
3377 context.Init();
[email protected]9045b8822012-01-13 20:35:353378
3379 {
[email protected]2ca01e52013-10-31 22:05:193380 URLRequest r(
3381 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, &context);
[email protected]9045b8822012-01-13 20:35:353382
3383 r.Start();
[email protected]255620da2013-08-19 13:14:293384 base::RunLoop().Run();
[email protected]dc5a5cf2012-09-26 02:49:303385 EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED,
3386 network_delegate.stage_blocked_for_callback());
[email protected]9045b8822012-01-13 20:35:353387 EXPECT_EQ(0, network_delegate.completed_requests());
3388 // Cancel before callback.
3389 r.Cancel();
3390 // Ensure that network delegate is notified.
3391 EXPECT_EQ(1, network_delegate.completed_requests());
3392 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
3393 EXPECT_EQ(ERR_ABORTED, r.status().error());
3394 EXPECT_EQ(1, network_delegate.created_requests());
3395 EXPECT_EQ(0, network_delegate.destroyed_requests());
3396 }
3397 EXPECT_EQ(1, network_delegate.destroyed_requests());
3398}
3399
3400// Tests that we can handle when a network request was canceled while we were
3401// waiting for the network delegate.
3402// Part 4: Request is cancelled while waiting for OnAuthRequired callback.
[email protected]bfe8b302013-02-15 12:16:023403TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) {
[email protected]9045b8822012-01-13 20:35:353404 ASSERT_TRUE(test_server_.Start());
3405
3406 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303407 BlockingNetworkDelegate network_delegate(
3408 BlockingNetworkDelegate::USER_CALLBACK);
3409 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]9045b8822012-01-13 20:35:353410
[email protected]ef2bf422012-05-11 03:27:093411 TestURLRequestContext context(true);
3412 context.set_network_delegate(&network_delegate);
3413 context.Init();
[email protected]9045b8822012-01-13 20:35:353414
3415 {
[email protected]2ca01e52013-10-31 22:05:193416 URLRequest r(
3417 test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d, &context);
[email protected]9045b8822012-01-13 20:35:353418
3419 r.Start();
[email protected]255620da2013-08-19 13:14:293420 base::RunLoop().Run();
[email protected]dc5a5cf2012-09-26 02:49:303421 EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED,
3422 network_delegate.stage_blocked_for_callback());
[email protected]9045b8822012-01-13 20:35:353423 EXPECT_EQ(0, network_delegate.completed_requests());
3424 // Cancel before callback.
3425 r.Cancel();
3426 // Ensure that network delegate is notified.
3427 EXPECT_EQ(1, network_delegate.completed_requests());
3428 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
3429 EXPECT_EQ(ERR_ABORTED, r.status().error());
3430 EXPECT_EQ(1, network_delegate.created_requests());
3431 EXPECT_EQ(0, network_delegate.destroyed_requests());
3432 }
3433 EXPECT_EQ(1, network_delegate.destroyed_requests());
3434}
3435
[email protected]95409e12010-08-17 20:07:113436// In this unit test, we're using the HTTPTestServer as a proxy server and
3437// issuing a CONNECT request with the magic host name "www.server-auth.com".
3438// The HTTPTestServer will return a 401 response, which we should balk at.
[email protected]b89290212009-08-14 22:37:353439TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) {
[email protected]95409e12010-08-17 20:07:113440 ASSERT_TRUE(test_server_.Start());
3441
[email protected]ceefd7fd2012-11-29 00:36:243442 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]d5a4dd62012-05-23 01:41:043443 TestURLRequestContextWithProxy context(
[email protected]2ca01e52013-10-31 22:05:193444 test_server_.host_port_pair().ToString(), &network_delegate);
[email protected]87a09a92011-07-14 15:50:503445
[email protected]dc651782009-02-14 01:45:083446 TestDelegate d;
3447 {
[email protected]2ca01e52013-10-31 22:05:193448 URLRequest r(
3449 GURL("https://www.server-auth.com/"), DEFAULT_PRIORITY, &d, &context);
[email protected]dc651782009-02-14 01:45:083450
3451 r.Start();
3452 EXPECT_TRUE(r.is_pending());
3453
[email protected]255620da2013-08-19 13:14:293454 base::RunLoop().Run();
[email protected]dc651782009-02-14 01:45:083455
[email protected]7461a402011-03-24 23:19:513456 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
[email protected]d0cc35b2011-09-08 12:02:053457 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().error());
[email protected]dc651782009-02-14 01:45:083458 }
3459}
3460
[email protected]b89290212009-08-14 22:37:353461TEST_F(URLRequestTestHTTP, GetTest_NoCache) {
[email protected]95409e12010-08-17 20:07:113462 ASSERT_TRUE(test_server_.Start());
3463
initial.commit586acc5fe2008-07-26 22:42:523464 TestDelegate d;
3465 {
[email protected]2ca01e52013-10-31 22:05:193466 URLRequest r(test_server_.GetURL(std::string()),
3467 DEFAULT_PRIORITY,
3468 &d,
3469 &default_context_);
initial.commit586acc5fe2008-07-26 22:42:523470
3471 r.Start();
3472 EXPECT_TRUE(r.is_pending());
3473
[email protected]255620da2013-08-19 13:14:293474 base::RunLoop().Run();
initial.commit586acc5fe2008-07-26 22:42:523475
3476 EXPECT_EQ(1, d.response_started_count());
3477 EXPECT_FALSE(d.received_data_before_response());
3478 EXPECT_NE(0, d.bytes_received());
[email protected]6d81b482011-02-22 19:47:193479 EXPECT_EQ(test_server_.host_port_pair().host(),
3480 r.GetSocketAddress().host());
3481 EXPECT_EQ(test_server_.host_port_pair().port(),
3482 r.GetSocketAddress().port());
[email protected]c31a54592009-09-04 02:36:163483
[email protected]9e743cd2010-03-16 07:03:533484 // TODO(eroman): Add back the NetLog tests...
initial.commit586acc5fe2008-07-26 22:42:523485 }
initial.commit586acc5fe2008-07-26 22:42:523486}
3487
[email protected]263163f2012-06-14 22:40:343488// This test has the server send a large number of cookies to the client.
3489// To ensure that no number of cookies causes a crash, a galloping binary
3490// search is used to estimate that maximum number of cookies that are accepted
3491// by the browser. Beyond the maximum number, the request will fail with
3492// ERR_RESPONSE_HEADERS_TOO_BIG.
[email protected]69dd6fe2013-02-23 23:15:303493#if defined(OS_WIN)
3494// http://crbug.com/177916
3495#define MAYBE_GetTest_ManyCookies DISABLED_GetTest_ManyCookies
3496#else
3497#define MAYBE_GetTest_ManyCookies GetTest_ManyCookies
3498#endif // defined(OS_WIN)
3499TEST_F(URLRequestTestHTTP, MAYBE_GetTest_ManyCookies) {
[email protected]263163f2012-06-14 22:40:343500 ASSERT_TRUE(test_server_.Start());
3501
3502 int lower_bound = 0;
3503 int upper_bound = 1;
3504
3505 // Double the number of cookies until the response header limits are
3506 // exceeded.
3507 while (DoManyCookiesRequest(upper_bound)) {
3508 lower_bound = upper_bound;
3509 upper_bound *= 2;
3510 ASSERT_LT(upper_bound, 1000000);
3511 }
3512
3513 int tolerance = upper_bound * 0.005;
3514 if (tolerance < 2)
3515 tolerance = 2;
3516
3517 // Perform a binary search to find the highest possible number of cookies,
3518 // within the desired tolerance.
3519 while (upper_bound - lower_bound >= tolerance) {
3520 int num_cookies = (lower_bound + upper_bound) / 2;
3521
3522 if (DoManyCookiesRequest(num_cookies))
3523 lower_bound = num_cookies;
3524 else
3525 upper_bound = num_cookies;
3526 }
3527 // Success: the test did not crash.
3528}
3529
[email protected]b89290212009-08-14 22:37:353530TEST_F(URLRequestTestHTTP, GetTest) {
[email protected]95409e12010-08-17 20:07:113531 ASSERT_TRUE(test_server_.Start());
3532
initial.commit586acc5fe2008-07-26 22:42:523533 TestDelegate d;
3534 {
[email protected]2ca01e52013-10-31 22:05:193535 URLRequest r(test_server_.GetURL(std::string()),
3536 DEFAULT_PRIORITY,
3537 &d,
3538 &default_context_);
initial.commit586acc5fe2008-07-26 22:42:523539
3540 r.Start();
3541 EXPECT_TRUE(r.is_pending());
3542
[email protected]255620da2013-08-19 13:14:293543 base::RunLoop().Run();
initial.commit586acc5fe2008-07-26 22:42:523544
3545 EXPECT_EQ(1, d.response_started_count());
3546 EXPECT_FALSE(d.received_data_before_response());
3547 EXPECT_NE(0, d.bytes_received());
[email protected]6d81b482011-02-22 19:47:193548 EXPECT_EQ(test_server_.host_port_pair().host(),
3549 r.GetSocketAddress().host());
3550 EXPECT_EQ(test_server_.host_port_pair().port(),
3551 r.GetSocketAddress().port());
initial.commit586acc5fe2008-07-26 22:42:523552 }
[email protected]5d7b373e2009-09-02 07:19:033553}
3554
[email protected]79e1fd62013-06-20 06:50:043555TEST_F(URLRequestTestHTTP, GetTest_GetFullRequestHeaders) {
3556 ASSERT_TRUE(test_server_.Start());
3557
3558 TestDelegate d;
3559 {
3560 GURL test_url(test_server_.GetURL(std::string()));
[email protected]2ca01e52013-10-31 22:05:193561 URLRequest r(test_url, DEFAULT_PRIORITY, &d, &default_context_);
[email protected]79e1fd62013-06-20 06:50:043562
3563 HttpRequestHeaders headers;
3564 EXPECT_FALSE(r.GetFullRequestHeaders(&headers));
3565
3566 r.Start();
3567 EXPECT_TRUE(r.is_pending());
3568
[email protected]255620da2013-08-19 13:14:293569 base::RunLoop().Run();
[email protected]79e1fd62013-06-20 06:50:043570
3571 EXPECT_EQ(1, d.response_started_count());
3572 EXPECT_FALSE(d.received_data_before_response());
3573 EXPECT_NE(0, d.bytes_received());
3574 EXPECT_EQ(test_server_.host_port_pair().host(),
3575 r.GetSocketAddress().host());
3576 EXPECT_EQ(test_server_.host_port_pair().port(),
3577 r.GetSocketAddress().port());
3578
3579 EXPECT_TRUE(d.have_full_request_headers());
3580 CheckFullRequestHeaders(d.full_request_headers(), test_url);
3581 }
3582}
3583
[email protected]58e32bb2013-01-21 18:23:253584TEST_F(URLRequestTestHTTP, GetTestLoadTiming) {
3585 ASSERT_TRUE(test_server_.Start());
3586
3587 TestDelegate d;
3588 {
[email protected]2ca01e52013-10-31 22:05:193589 URLRequest r(test_server_.GetURL(std::string()),
3590 DEFAULT_PRIORITY,
3591 &d,
3592 &default_context_);
[email protected]58e32bb2013-01-21 18:23:253593
3594 r.Start();
3595 EXPECT_TRUE(r.is_pending());
3596
[email protected]255620da2013-08-19 13:14:293597 base::RunLoop().Run();
[email protected]58e32bb2013-01-21 18:23:253598
3599 LoadTimingInfo load_timing_info;
[email protected]0a703912013-02-02 04:25:173600 r.GetLoadTimingInfo(&load_timing_info);
[email protected]58e32bb2013-01-21 18:23:253601 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
3602
3603 EXPECT_EQ(1, d.response_started_count());
3604 EXPECT_FALSE(d.received_data_before_response());
3605 EXPECT_NE(0, d.bytes_received());
3606 EXPECT_EQ(test_server_.host_port_pair().host(),
3607 r.GetSocketAddress().host());
3608 EXPECT_EQ(test_server_.host_port_pair().port(),
3609 r.GetSocketAddress().port());
3610 }
3611}
3612
[email protected]aad63572011-05-24 20:14:393613TEST_F(URLRequestTestHTTP, GetZippedTest) {
3614 ASSERT_TRUE(test_server_.Start());
3615
3616 // Parameter that specifies the Content-Length field in the response:
3617 // C - Compressed length.
3618 // U - Uncompressed length.
3619 // L - Large length (larger than both C & U).
3620 // M - Medium length (between C & U).
3621 // S - Small length (smaller than both C & U).
3622 const char test_parameters[] = "CULMS";
3623 const int num_tests = arraysize(test_parameters)- 1; // Skip NULL.
3624 // C & U should be OK.
[email protected]f0e2bf42011-07-22 21:21:443625 // L & M are larger than the data sent, and show an error.
[email protected]aad63572011-05-24 20:14:393626 // S has too little data, but we seem to accept it.
3627 const bool test_expect_success[num_tests] =
[email protected]f001bd6a2011-12-08 04:31:373628 { true, true, false, false, true };
[email protected]aad63572011-05-24 20:14:393629
3630 for (int i = 0; i < num_tests ; i++) {
3631 TestDelegate d;
3632 {
3633 std::string test_file =
3634 base::StringPrintf("compressedfiles/BullRunSpeech.txt?%c",
3635 test_parameters[i]);
[email protected]aad63572011-05-24 20:14:393636
[email protected]ceefd7fd2012-11-29 00:36:243637 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]ef2bf422012-05-11 03:27:093638 TestURLRequestContext context(true);
3639 context.set_network_delegate(&network_delegate);
3640 context.Init();
[email protected]87a09a92011-07-14 15:50:503641
[email protected]2ca01e52013-10-31 22:05:193642 URLRequest r(
3643 test_server_.GetURL(test_file), DEFAULT_PRIORITY, &d, &context);
[email protected]aad63572011-05-24 20:14:393644 r.Start();
3645 EXPECT_TRUE(r.is_pending());
3646
[email protected]255620da2013-08-19 13:14:293647 base::RunLoop().Run();
[email protected]aad63572011-05-24 20:14:393648
3649 EXPECT_EQ(1, d.response_started_count());
3650 EXPECT_FALSE(d.received_data_before_response());
3651 VLOG(1) << " Received " << d.bytes_received() << " bytes"
3652 << " status = " << r.status().status()
[email protected]d0cc35b2011-09-08 12:02:053653 << " error = " << r.status().error();
[email protected]aad63572011-05-24 20:14:393654 if (test_expect_success[i]) {
3655 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status())
3656 << " Parameter = \"" << test_file << "\"";
3657 } else {
3658 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
[email protected]5543cbb2012-04-20 16:35:233659 EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, r.status().error())
[email protected]aad63572011-05-24 20:14:393660 << " Parameter = \"" << test_file << "\"";
3661 }
3662 }
3663 }
3664}
3665
[email protected]c044616e2013-02-20 02:01:263666TEST_F(URLRequestTestHTTP, HTTPSToHTTPRedirectNoRefererTest) {
[email protected]95409e12010-08-17 20:07:113667 ASSERT_TRUE(test_server_.Start());
3668
[email protected]ce7d0cbc2013-05-03 18:57:223669 SpawnedTestServer https_test_server(
3670 SpawnedTestServer::TYPE_HTTPS, SpawnedTestServer::kLocalhost,
[email protected]6cdfd7f2013-02-08 20:40:153671 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]95409e12010-08-17 20:07:113672 ASSERT_TRUE(https_test_server.Start());
[email protected]7844480a2009-12-16 21:18:583673
3674 // An https server is sent a request with an https referer,
3675 // and responds with a redirect to an http url. The http
3676 // server should not be sent the referer.
[email protected]007b3f82013-04-09 08:46:453677 GURL http_destination = test_server_.GetURL(std::string());
[email protected]7844480a2009-12-16 21:18:583678 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:193679 URLRequest req(
3680 https_test_server.GetURL("server-redirect?" + http_destination.spec()),
3681 DEFAULT_PRIORITY,
3682 &d,
3683 &default_context_);
[email protected]99ecf6e2013-04-10 22:46:133684 req.SetReferrer("https://www.referrer.com/");
[email protected]7844480a2009-12-16 21:18:583685 req.Start();
[email protected]255620da2013-08-19 13:14:293686 base::RunLoop().Run();
[email protected]7844480a2009-12-16 21:18:583687
3688 EXPECT_EQ(1, d.response_started_count());
3689 EXPECT_EQ(1, d.received_redirect_count());
3690 EXPECT_EQ(http_destination, req.url());
3691 EXPECT_EQ(std::string(), req.referrer());
3692}
3693
[email protected]58e32bb2013-01-21 18:23:253694TEST_F(URLRequestTestHTTP, RedirectLoadTiming) {
3695 ASSERT_TRUE(test_server_.Start());
3696
[email protected]007b3f82013-04-09 08:46:453697 GURL destination_url = test_server_.GetURL(std::string());
3698 GURL original_url =
3699 test_server_.GetURL("server-redirect?" + destination_url.spec());
[email protected]58e32bb2013-01-21 18:23:253700 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:193701 URLRequest req(original_url, DEFAULT_PRIORITY, &d, &default_context_);
[email protected]58e32bb2013-01-21 18:23:253702 req.Start();
[email protected]255620da2013-08-19 13:14:293703 base::RunLoop().Run();
[email protected]58e32bb2013-01-21 18:23:253704
3705 EXPECT_EQ(1, d.response_started_count());
3706 EXPECT_EQ(1, d.received_redirect_count());
3707 EXPECT_EQ(destination_url, req.url());
3708 EXPECT_EQ(original_url, req.original_url());
3709 ASSERT_EQ(2U, req.url_chain().size());
3710 EXPECT_EQ(original_url, req.url_chain()[0]);
3711 EXPECT_EQ(destination_url, req.url_chain()[1]);
3712
3713 LoadTimingInfo load_timing_info_before_redirect;
3714 EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeRedirect(
3715 &load_timing_info_before_redirect));
3716 TestLoadTimingNotReused(load_timing_info_before_redirect,
3717 CONNECT_TIMING_HAS_DNS_TIMES);
3718
3719 LoadTimingInfo load_timing_info;
[email protected]0a703912013-02-02 04:25:173720 req.GetLoadTimingInfo(&load_timing_info);
[email protected]58e32bb2013-01-21 18:23:253721 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
3722
3723 // Check that a new socket was used on redirect, since the server does not
3724 // supposed keep-alive sockets, and that the times before the redirect are
3725 // before the ones recorded for the second request.
3726 EXPECT_NE(load_timing_info_before_redirect.socket_log_id,
3727 load_timing_info.socket_log_id);
3728 EXPECT_LE(load_timing_info_before_redirect.receive_headers_end,
3729 load_timing_info.connect_timing.connect_start);
3730}
3731
[email protected]8f1ac082011-04-19 21:14:133732TEST_F(URLRequestTestHTTP, MultipleRedirectTest) {
3733 ASSERT_TRUE(test_server_.Start());
3734
[email protected]007b3f82013-04-09 08:46:453735 GURL destination_url = test_server_.GetURL(std::string());
3736 GURL middle_redirect_url =
3737 test_server_.GetURL("server-redirect?" + destination_url.spec());
[email protected]8f1ac082011-04-19 21:14:133738 GURL original_url = test_server_.GetURL(
3739 "server-redirect?" + middle_redirect_url.spec());
3740 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:193741 URLRequest req(original_url, DEFAULT_PRIORITY, &d, &default_context_);
[email protected]8f1ac082011-04-19 21:14:133742 req.Start();
[email protected]255620da2013-08-19 13:14:293743 base::RunLoop().Run();
[email protected]8f1ac082011-04-19 21:14:133744
3745 EXPECT_EQ(1, d.response_started_count());
3746 EXPECT_EQ(2, d.received_redirect_count());
3747 EXPECT_EQ(destination_url, req.url());
3748 EXPECT_EQ(original_url, req.original_url());
3749 ASSERT_EQ(3U, req.url_chain().size());
3750 EXPECT_EQ(original_url, req.url_chain()[0]);
3751 EXPECT_EQ(middle_redirect_url, req.url_chain()[1]);
3752 EXPECT_EQ(destination_url, req.url_chain()[2]);
3753}
3754
[email protected]abe1c4a2013-10-25 19:28:513755// First and second pieces of information logged by delegates to URLRequests.
3756const char kFirstDelegateInfo[] = "Wonderful delegate";
3757const char kSecondDelegateInfo[] = "Exciting delegate";
3758
3759// Logs delegate information to a URLRequest. The first string is logged
3760// synchronously on Start(), using DELEGATE_INFO_DEBUG_ONLY. The second is
3761// logged asynchronously, using DELEGATE_INFO_DISPLAY_TO_USER. Then
3762// another asynchronous call is used to clear the delegate information
3763// before calling a callback. The object then deletes itself.
3764class AsyncDelegateLogger : public base::RefCounted<AsyncDelegateLogger> {
3765 public:
3766 typedef base::Callback<void()> Callback;
3767
3768 // Each time delegate information is added to the URLRequest, the resulting
3769 // load state is checked. The expected load state after each request is
3770 // passed in as an argument.
3771 static void Run(URLRequest* url_request,
3772 LoadState expected_first_load_state,
3773 LoadState expected_second_load_state,
3774 LoadState expected_third_load_state,
3775 const Callback& callback) {
3776 AsyncDelegateLogger* logger = new AsyncDelegateLogger(
3777 url_request,
3778 expected_first_load_state,
3779 expected_second_load_state,
3780 expected_third_load_state,
3781 callback);
3782 logger->Start();
3783 }
3784
3785 // Checks that the log entries, starting with log_position, contain the
3786 // DELEGATE_INFO NetLog events that an AsyncDelegateLogger should have
3787 // recorded. Returns the index of entry after the expected number of
3788 // events this logged, or entries.size() if there aren't enough entries.
3789 static size_t CheckDelegateInfo(
3790 const CapturingNetLog::CapturedEntryList& entries, size_t log_position) {
3791 // There should be 4 DELEGATE_INFO events: Two begins and two ends.
3792 if (log_position + 3 >= entries.size()) {
3793 ADD_FAILURE() << "Not enough log entries";
3794 return entries.size();
3795 }
3796 std::string delegate_info;
3797 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type);
3798 EXPECT_EQ(NetLog::PHASE_BEGIN, entries[log_position].phase);
3799 EXPECT_TRUE(entries[log_position].GetStringValue("delegate_info",
3800 &delegate_info));
3801 EXPECT_EQ(kFirstDelegateInfo, delegate_info);
3802
3803 ++log_position;
3804 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type);
3805 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
3806
3807 ++log_position;
3808 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type);
3809 EXPECT_EQ(NetLog::PHASE_BEGIN, entries[log_position].phase);
3810 EXPECT_TRUE(entries[log_position].GetStringValue("delegate_info",
3811 &delegate_info));
3812 EXPECT_EQ(kSecondDelegateInfo, delegate_info);
3813
3814 ++log_position;
3815 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type);
3816 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
3817
3818 return log_position + 1;
3819 }
3820
[email protected]1826a402014-01-08 15:40:483821 // Find delegate request begin and end messages for OnBeforeNetworkStart.
3822 // Returns the position of the end message.
3823 static size_t ExpectBeforeNetworkEvents(
3824 const CapturingNetLog::CapturedEntryList& entries,
3825 size_t log_position) {
3826 log_position =
3827 ExpectLogContainsSomewhereAfter(entries,
3828 log_position,
3829 NetLog::TYPE_URL_REQUEST_DELEGATE,
3830 NetLog::PHASE_BEGIN);
3831 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE,
3832 entries[log_position + 1].type);
3833 EXPECT_EQ(NetLog::PHASE_END, entries[log_position + 1].phase);
3834 return log_position + 1;
3835 }
3836
[email protected]abe1c4a2013-10-25 19:28:513837 private:
3838 friend class base::RefCounted<AsyncDelegateLogger>;
3839
3840 AsyncDelegateLogger(URLRequest* url_request,
3841 LoadState expected_first_load_state,
3842 LoadState expected_second_load_state,
3843 LoadState expected_third_load_state,
3844 const Callback& callback)
3845 : url_request_(url_request),
3846 expected_first_load_state_(expected_first_load_state),
3847 expected_second_load_state_(expected_second_load_state),
3848 expected_third_load_state_(expected_third_load_state),
3849 callback_(callback) {
3850 }
3851
3852 ~AsyncDelegateLogger() {}
3853
3854 void Start() {
[email protected]f8fe5cf2013-12-04 20:11:533855 url_request_->LogBlockedBy(kFirstDelegateInfo);
[email protected]abe1c4a2013-10-25 19:28:513856 LoadStateWithParam load_state = url_request_->GetLoadState();
3857 EXPECT_EQ(expected_first_load_state_, load_state.state);
3858 EXPECT_NE(ASCIIToUTF16(kFirstDelegateInfo), load_state.param);
3859 base::MessageLoop::current()->PostTask(
3860 FROM_HERE,
3861 base::Bind(&AsyncDelegateLogger::LogSecondDelegate, this));
3862 }
3863
3864 void LogSecondDelegate() {
[email protected]f8fe5cf2013-12-04 20:11:533865 url_request_->LogAndReportBlockedBy(kSecondDelegateInfo);
[email protected]abe1c4a2013-10-25 19:28:513866 LoadStateWithParam load_state = url_request_->GetLoadState();
3867 EXPECT_EQ(expected_second_load_state_, load_state.state);
3868 if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) {
3869 EXPECT_EQ(ASCIIToUTF16(kSecondDelegateInfo), load_state.param);
3870 } else {
3871 EXPECT_NE(ASCIIToUTF16(kSecondDelegateInfo), load_state.param);
3872 }
3873 base::MessageLoop::current()->PostTask(
3874 FROM_HERE,
3875 base::Bind(&AsyncDelegateLogger::LogComplete, this));
3876 }
3877
3878 void LogComplete() {
[email protected]f8fe5cf2013-12-04 20:11:533879 url_request_->LogUnblocked();
[email protected]abe1c4a2013-10-25 19:28:513880 LoadStateWithParam load_state = url_request_->GetLoadState();
3881 EXPECT_EQ(expected_third_load_state_, load_state.state);
3882 if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE)
[email protected]754bd202013-12-18 08:29:083883 EXPECT_EQ(base::string16(), load_state.param);
[email protected]abe1c4a2013-10-25 19:28:513884 callback_.Run();
3885 }
3886
3887 URLRequest* url_request_;
3888 const int expected_first_load_state_;
3889 const int expected_second_load_state_;
3890 const int expected_third_load_state_;
3891 const Callback callback_;
3892
3893 DISALLOW_COPY_AND_ASSIGN(AsyncDelegateLogger);
3894};
3895
3896// NetworkDelegate that logs delegate information before a request is started,
3897// before headers are sent, when headers are read, and when auth information
3898// is requested. Uses AsyncDelegateLogger.
3899class AsyncLoggingNetworkDelegate : public TestNetworkDelegate {
3900 public:
3901 AsyncLoggingNetworkDelegate() {}
3902 virtual ~AsyncLoggingNetworkDelegate() {}
3903
3904 // NetworkDelegate implementation.
3905 virtual int OnBeforeURLRequest(URLRequest* request,
3906 const CompletionCallback& callback,
3907 GURL* new_url) OVERRIDE {
3908 TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url);
3909 return RunCallbackAsynchronously(request, callback);
3910 }
3911
3912 virtual int OnBeforeSendHeaders(URLRequest* request,
3913 const CompletionCallback& callback,
3914 HttpRequestHeaders* headers) OVERRIDE {
3915 TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers);
3916 return RunCallbackAsynchronously(request, callback);
3917 }
3918
3919 virtual int OnHeadersReceived(
3920 URLRequest* request,
3921 const CompletionCallback& callback,
3922 const HttpResponseHeaders* original_response_headers,
3923 scoped_refptr<HttpResponseHeaders>* override_response_headers) OVERRIDE {
3924 TestNetworkDelegate::OnHeadersReceived(request, callback,
3925 original_response_headers,
3926 override_response_headers);
3927 return RunCallbackAsynchronously(request, callback);
3928 }
3929
3930 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired(
3931 URLRequest* request,
3932 const AuthChallengeInfo& auth_info,
3933 const AuthCallback& callback,
3934 AuthCredentials* credentials) OVERRIDE {
3935 AsyncDelegateLogger::Run(
3936 request,
3937 LOAD_STATE_WAITING_FOR_DELEGATE,
3938 LOAD_STATE_WAITING_FOR_DELEGATE,
3939 LOAD_STATE_WAITING_FOR_DELEGATE,
3940 base::Bind(&AsyncLoggingNetworkDelegate::SetAuthAndResume,
3941 callback, credentials));
3942 return AUTH_REQUIRED_RESPONSE_IO_PENDING;
3943 }
3944
3945 private:
3946 static int RunCallbackAsynchronously(
3947 URLRequest* request,
3948 const CompletionCallback& callback) {
3949 AsyncDelegateLogger::Run(
3950 request,
3951 LOAD_STATE_WAITING_FOR_DELEGATE,
3952 LOAD_STATE_WAITING_FOR_DELEGATE,
3953 LOAD_STATE_WAITING_FOR_DELEGATE,
3954 base::Bind(callback, OK));
3955 return ERR_IO_PENDING;
3956 }
3957
3958 static void SetAuthAndResume(const AuthCallback& callback,
3959 AuthCredentials* credentials) {
3960 *credentials = AuthCredentials(kUser, kSecret);
3961 callback.Run(NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
3962 }
3963
3964 DISALLOW_COPY_AND_ASSIGN(AsyncLoggingNetworkDelegate);
3965};
3966
3967// URLRequest::Delegate that logs delegate information when the headers
3968// are received, when each read completes, and during redirects. Uses
3969// AsyncDelegateLogger. Can optionally cancel a request in any phase.
3970//
3971// Inherits from TestDelegate to reuse the TestDelegate code to handle
3972// advancing to the next step in most cases, as well as cancellation.
3973class AsyncLoggingUrlRequestDelegate : public TestDelegate {
3974 public:
3975 enum CancelStage {
3976 NO_CANCEL = 0,
3977 CANCEL_ON_RECEIVED_REDIRECT,
3978 CANCEL_ON_RESPONSE_STARTED,
3979 CANCEL_ON_READ_COMPLETED
3980 };
3981
3982 explicit AsyncLoggingUrlRequestDelegate(CancelStage cancel_stage)
3983 : cancel_stage_(cancel_stage) {
3984 if (cancel_stage == CANCEL_ON_RECEIVED_REDIRECT)
3985 set_cancel_in_received_redirect(true);
3986 else if (cancel_stage == CANCEL_ON_RESPONSE_STARTED)
3987 set_cancel_in_response_started(true);
3988 else if (cancel_stage == CANCEL_ON_READ_COMPLETED)
3989 set_cancel_in_received_data(true);
3990 }
3991 virtual ~AsyncLoggingUrlRequestDelegate() {}
3992
3993 // URLRequest::Delegate implementation:
3994 void virtual OnReceivedRedirect(URLRequest* request,
3995 const GURL& new_url,
3996 bool* defer_redirect) OVERRIDE {
3997 *defer_redirect = true;
3998 AsyncDelegateLogger::Run(
3999 request,
4000 LOAD_STATE_WAITING_FOR_DELEGATE,
4001 LOAD_STATE_WAITING_FOR_DELEGATE,
4002 LOAD_STATE_WAITING_FOR_DELEGATE,
4003 base::Bind(
4004 &AsyncLoggingUrlRequestDelegate::OnReceivedRedirectLoggingComplete,
4005 base::Unretained(this), request, new_url));
4006 }
4007
4008 virtual void OnResponseStarted(URLRequest* request) OVERRIDE {
4009 AsyncDelegateLogger::Run(
4010 request,
4011 LOAD_STATE_WAITING_FOR_DELEGATE,
4012 LOAD_STATE_WAITING_FOR_DELEGATE,
4013 LOAD_STATE_WAITING_FOR_DELEGATE,
4014 base::Bind(
4015 &AsyncLoggingUrlRequestDelegate::OnResponseStartedLoggingComplete,
4016 base::Unretained(this), request));
4017 }
4018
4019 virtual void OnReadCompleted(URLRequest* request,
4020 int bytes_read) OVERRIDE {
4021 AsyncDelegateLogger::Run(
4022 request,
4023 LOAD_STATE_IDLE,
4024 LOAD_STATE_IDLE,
4025 LOAD_STATE_IDLE,
4026 base::Bind(
4027 &AsyncLoggingUrlRequestDelegate::AfterReadCompletedLoggingComplete,
4028 base::Unretained(this), request, bytes_read));
4029 }
4030
4031 private:
4032 void OnReceivedRedirectLoggingComplete(URLRequest* request,
4033 const GURL& new_url) {
4034 bool defer_redirect = false;
4035 TestDelegate::OnReceivedRedirect(request, new_url, &defer_redirect);
4036 // FollowDeferredRedirect should not be called after cancellation.
4037 if (cancel_stage_ == CANCEL_ON_RECEIVED_REDIRECT)
4038 return;
4039 if (!defer_redirect)
4040 request->FollowDeferredRedirect();
4041 }
4042
4043 void OnResponseStartedLoggingComplete(URLRequest* request) {
4044 // The parent class continues the request.
4045 TestDelegate::OnResponseStarted(request);
4046 }
4047
4048 void AfterReadCompletedLoggingComplete(URLRequest* request, int bytes_read) {
4049 // The parent class continues the request.
4050 TestDelegate::OnReadCompleted(request, bytes_read);
4051 }
4052
4053 const CancelStage cancel_stage_;
4054
4055 DISALLOW_COPY_AND_ASSIGN(AsyncLoggingUrlRequestDelegate);
4056};
4057
4058// Tests handling of delegate info before a request starts.
4059TEST_F(URLRequestTestHTTP, DelegateInfoBeforeStart) {
4060 ASSERT_TRUE(test_server_.Start());
4061
4062 TestDelegate request_delegate;
4063 TestURLRequestContext context(true);
4064 context.set_network_delegate(NULL);
4065 context.set_net_log(&net_log_);
4066 context.Init();
4067
4068 {
4069 URLRequest r(test_server_.GetURL("empty.html"),
[email protected]2ca01e52013-10-31 22:05:194070 DEFAULT_PRIORITY,
4071 &request_delegate,
4072 &context);
[email protected]abe1c4a2013-10-25 19:28:514073 LoadStateWithParam load_state = r.GetLoadState();
4074 EXPECT_EQ(LOAD_STATE_IDLE, load_state.state);
[email protected]754bd202013-12-18 08:29:084075 EXPECT_EQ(base::string16(), load_state.param);
[email protected]abe1c4a2013-10-25 19:28:514076
4077 AsyncDelegateLogger::Run(
4078 &r,
4079 LOAD_STATE_WAITING_FOR_DELEGATE,
4080 LOAD_STATE_WAITING_FOR_DELEGATE,
4081 LOAD_STATE_IDLE,
4082 base::Bind(&URLRequest::Start, base::Unretained(&r)));
4083
4084 base::RunLoop().Run();
4085
4086 EXPECT_EQ(200, r.GetResponseCode());
4087 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
4088 }
4089
4090 CapturingNetLog::CapturedEntryList entries;
4091 net_log_.GetEntries(&entries);
4092 size_t log_position = ExpectLogContainsSomewhereAfter(
4093 entries,
4094 0,
4095 NetLog::TYPE_DELEGATE_INFO,
4096 NetLog::PHASE_BEGIN);
4097
4098 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, log_position);
4099
4100 // Nothing else should add any delegate info to the request.
4101 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4102 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4103}
4104
4105// Tests handling of delegate info from a network delegate.
4106TEST_F(URLRequestTestHTTP, NetworkDelegateInfo) {
4107 ASSERT_TRUE(test_server_.Start());
4108
4109 TestDelegate request_delegate;
4110 AsyncLoggingNetworkDelegate network_delegate;
4111 TestURLRequestContext context(true);
4112 context.set_network_delegate(&network_delegate);
4113 context.set_net_log(&net_log_);
4114 context.Init();
4115
4116 {
4117 URLRequest r(test_server_.GetURL("simple.html"),
[email protected]2ca01e52013-10-31 22:05:194118 DEFAULT_PRIORITY,
4119 &request_delegate,
4120 &context);
[email protected]abe1c4a2013-10-25 19:28:514121 LoadStateWithParam load_state = r.GetLoadState();
4122 EXPECT_EQ(LOAD_STATE_IDLE, load_state.state);
[email protected]754bd202013-12-18 08:29:084123 EXPECT_EQ(base::string16(), load_state.param);
[email protected]abe1c4a2013-10-25 19:28:514124
4125 r.Start();
4126 base::RunLoop().Run();
4127
4128 EXPECT_EQ(200, r.GetResponseCode());
4129 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
4130 EXPECT_EQ(1, network_delegate.created_requests());
4131 EXPECT_EQ(0, network_delegate.destroyed_requests());
4132 }
4133 EXPECT_EQ(1, network_delegate.destroyed_requests());
4134
4135 size_t log_position = 0;
4136 CapturingNetLog::CapturedEntryList entries;
4137 net_log_.GetEntries(&entries);
4138 for (size_t i = 0; i < 3; ++i) {
4139 log_position = ExpectLogContainsSomewhereAfter(
4140 entries,
4141 log_position + 1,
4142 NetLog::TYPE_URL_REQUEST_DELEGATE,
4143 NetLog::PHASE_BEGIN);
4144
4145 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4146 log_position + 1);
4147
4148 ASSERT_LT(log_position, entries.size());
4149 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4150 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
[email protected]1826a402014-01-08 15:40:484151
4152 if (i == 1) {
4153 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4154 entries, log_position + 1);
4155 }
[email protected]abe1c4a2013-10-25 19:28:514156 }
4157
4158 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4159 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4160}
4161
4162// Tests handling of delegate info from a network delegate in the case of an
4163// HTTP redirect.
4164TEST_F(URLRequestTestHTTP, NetworkDelegateInfoRedirect) {
4165 ASSERT_TRUE(test_server_.Start());
4166
4167 TestDelegate request_delegate;
4168 AsyncLoggingNetworkDelegate network_delegate;
4169 TestURLRequestContext context(true);
4170 context.set_network_delegate(&network_delegate);
4171 context.set_net_log(&net_log_);
4172 context.Init();
4173
4174 {
4175 URLRequest r(test_server_.GetURL("server-redirect?simple.html"),
[email protected]2ca01e52013-10-31 22:05:194176 DEFAULT_PRIORITY,
4177 &request_delegate,
4178 &context);
[email protected]abe1c4a2013-10-25 19:28:514179 LoadStateWithParam load_state = r.GetLoadState();
4180 EXPECT_EQ(LOAD_STATE_IDLE, load_state.state);
[email protected]754bd202013-12-18 08:29:084181 EXPECT_EQ(base::string16(), load_state.param);
[email protected]abe1c4a2013-10-25 19:28:514182
4183 r.Start();
4184 base::RunLoop().Run();
4185
4186 EXPECT_EQ(200, r.GetResponseCode());
4187 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
4188 EXPECT_EQ(2, network_delegate.created_requests());
4189 EXPECT_EQ(0, network_delegate.destroyed_requests());
4190 }
4191 EXPECT_EQ(1, network_delegate.destroyed_requests());
4192
4193 size_t log_position = 0;
4194 CapturingNetLog::CapturedEntryList entries;
4195 net_log_.GetEntries(&entries);
4196 // The NetworkDelegate logged information in OnBeforeURLRequest,
4197 // OnBeforeSendHeaders, and OnHeadersReceived.
4198 for (size_t i = 0; i < 3; ++i) {
4199 log_position = ExpectLogContainsSomewhereAfter(
4200 entries,
4201 log_position + 1,
4202 NetLog::TYPE_URL_REQUEST_DELEGATE,
4203 NetLog::PHASE_BEGIN);
4204
4205 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4206 log_position + 1);
4207
4208 ASSERT_LT(log_position, entries.size());
4209 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4210 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
[email protected]1826a402014-01-08 15:40:484211
4212 if (i == 1) {
4213 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4214 entries, log_position + 1);
4215 }
[email protected]abe1c4a2013-10-25 19:28:514216 }
4217
4218 // The URLRequest::Delegate then gets informed about the redirect.
4219 log_position = ExpectLogContainsSomewhereAfter(
4220 entries,
4221 log_position + 1,
4222 NetLog::TYPE_URL_REQUEST_DELEGATE,
4223 NetLog::PHASE_BEGIN);
4224
4225 // The NetworkDelegate logged information in the same three events as before.
4226 for (size_t i = 0; i < 3; ++i) {
4227 log_position = ExpectLogContainsSomewhereAfter(
4228 entries,
4229 log_position + 1,
4230 NetLog::TYPE_URL_REQUEST_DELEGATE,
4231 NetLog::PHASE_BEGIN);
4232
4233 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4234 log_position + 1);
4235
4236 ASSERT_LT(log_position, entries.size());
4237 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4238 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4239 }
4240
4241 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4242 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4243}
4244
4245// Tests handling of delegate info from a network delegate in the case of HTTP
4246// AUTH.
4247TEST_F(URLRequestTestHTTP, NetworkDelegateInfoAuth) {
4248 ASSERT_TRUE(test_server_.Start());
4249
4250 TestDelegate request_delegate;
4251 AsyncLoggingNetworkDelegate network_delegate;
4252 TestURLRequestContext context(true);
4253 context.set_network_delegate(&network_delegate);
4254 context.set_net_log(&net_log_);
4255 context.Init();
4256
4257 {
4258 URLRequest r(test_server_.GetURL("auth-basic"),
[email protected]2ca01e52013-10-31 22:05:194259 DEFAULT_PRIORITY,
4260 &request_delegate,
4261 &context);
[email protected]abe1c4a2013-10-25 19:28:514262 LoadStateWithParam load_state = r.GetLoadState();
4263 EXPECT_EQ(LOAD_STATE_IDLE, load_state.state);
[email protected]754bd202013-12-18 08:29:084264 EXPECT_EQ(base::string16(), load_state.param);
[email protected]abe1c4a2013-10-25 19:28:514265
4266 r.Start();
4267 base::RunLoop().Run();
4268
4269 EXPECT_EQ(200, r.GetResponseCode());
4270 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
4271 EXPECT_EQ(1, network_delegate.created_requests());
4272 EXPECT_EQ(0, network_delegate.destroyed_requests());
4273 }
4274 EXPECT_EQ(1, network_delegate.destroyed_requests());
4275
4276 size_t log_position = 0;
4277 CapturingNetLog::CapturedEntryList entries;
4278 net_log_.GetEntries(&entries);
4279 // The NetworkDelegate should have logged information in OnBeforeURLRequest,
4280 // OnBeforeSendHeaders, OnHeadersReceived, OnAuthRequired, and then again in
4281 // OnBeforeURLRequest and OnBeforeSendHeaders.
4282 for (size_t i = 0; i < 6; ++i) {
4283 log_position = ExpectLogContainsSomewhereAfter(
4284 entries,
4285 log_position + 1,
4286 NetLog::TYPE_URL_REQUEST_DELEGATE,
4287 NetLog::PHASE_BEGIN);
4288
4289 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4290 log_position + 1);
4291
4292 ASSERT_LT(log_position, entries.size());
4293 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4294 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
[email protected]1826a402014-01-08 15:40:484295
4296 if (i == 1) {
4297 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4298 entries, log_position + 1);
4299 }
[email protected]abe1c4a2013-10-25 19:28:514300 }
4301
4302 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4303 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4304}
4305
4306// Tests handling of delegate info from a URLRequest::Delegate.
4307TEST_F(URLRequestTestHTTP, URLRequestDelegateInfo) {
4308 ASSERT_TRUE(test_server_.Start());
4309
4310 AsyncLoggingUrlRequestDelegate request_delegate(
4311 AsyncLoggingUrlRequestDelegate::NO_CANCEL);
4312 TestURLRequestContext context(true);
4313 context.set_network_delegate(NULL);
4314 context.set_net_log(&net_log_);
4315 context.Init();
4316
4317 {
4318 // A chunked response with delays between chunks is used to make sure that
4319 // attempts by the URLRequest delegate to log information while reading the
4320 // body are ignored. Since they are ignored, this test is robust against
[email protected]1826a402014-01-08 15:40:484321 // the possibility of multiple reads being combined in the unlikely event
[email protected]abe1c4a2013-10-25 19:28:514322 // that it occurs.
4323 URLRequest r(test_server_.GetURL("chunked?waitBetweenChunks=20"),
[email protected]2ca01e52013-10-31 22:05:194324 DEFAULT_PRIORITY,
4325 &request_delegate,
4326 &context);
[email protected]abe1c4a2013-10-25 19:28:514327 LoadStateWithParam load_state = r.GetLoadState();
4328 r.Start();
4329 base::RunLoop().Run();
4330
4331 EXPECT_EQ(200, r.GetResponseCode());
4332 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
4333 }
4334
4335 CapturingNetLog::CapturedEntryList entries;
4336 net_log_.GetEntries(&entries);
4337
[email protected]1826a402014-01-08 15:40:484338 size_t log_position = 0;
4339
4340 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4341 entries, log_position);
4342
[email protected]abe1c4a2013-10-25 19:28:514343 // The delegate info should only have been logged on header complete. Other
4344 // times it should silently be ignored.
[email protected]1826a402014-01-08 15:40:484345 log_position =
4346 ExpectLogContainsSomewhereAfter(entries,
4347 log_position + 1,
4348 NetLog::TYPE_URL_REQUEST_DELEGATE,
4349 NetLog::PHASE_BEGIN);
[email protected]abe1c4a2013-10-25 19:28:514350
4351 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4352 log_position + 1);
4353
4354 ASSERT_LT(log_position, entries.size());
4355 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4356 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4357
4358 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4359 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4360 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4361 entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE));
4362}
4363
4364// Tests handling of delegate info from a URLRequest::Delegate in the case of
4365// an HTTP redirect.
4366TEST_F(URLRequestTestHTTP, URLRequestDelegateInfoOnRedirect) {
4367 ASSERT_TRUE(test_server_.Start());
4368
4369 AsyncLoggingUrlRequestDelegate request_delegate(
4370 AsyncLoggingUrlRequestDelegate::NO_CANCEL);
4371 TestURLRequestContext context(true);
4372 context.set_network_delegate(NULL);
4373 context.set_net_log(&net_log_);
4374 context.Init();
4375
4376 {
4377 URLRequest r(test_server_.GetURL("server-redirect?simple.html"),
[email protected]2ca01e52013-10-31 22:05:194378 DEFAULT_PRIORITY,
4379 &request_delegate,
4380 &context);
[email protected]abe1c4a2013-10-25 19:28:514381 LoadStateWithParam load_state = r.GetLoadState();
4382 r.Start();
4383 base::RunLoop().Run();
4384
4385 EXPECT_EQ(200, r.GetResponseCode());
4386 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
4387 }
4388
4389 CapturingNetLog::CapturedEntryList entries;
4390 net_log_.GetEntries(&entries);
4391
4392 // Delegate info should only have been logged in OnReceivedRedirect and
4393 // OnResponseStarted.
4394 size_t log_position = 0;
4395 for (int i = 0; i < 2; ++i) {
[email protected]1826a402014-01-08 15:40:484396 if (i == 0) {
4397 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4398 entries, log_position) + 1;
4399 }
4400
[email protected]abe1c4a2013-10-25 19:28:514401 log_position = ExpectLogContainsSomewhereAfter(
4402 entries,
4403 log_position,
4404 NetLog::TYPE_URL_REQUEST_DELEGATE,
4405 NetLog::PHASE_BEGIN);
4406
4407 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4408 log_position + 1);
4409
4410 ASSERT_LT(log_position, entries.size());
4411 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4412 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4413 }
4414
4415 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4416 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4417 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4418 entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE));
4419}
4420
4421// Tests handling of delegate info from a URLRequest::Delegate in the case of
4422// an HTTP redirect, with cancellation at various points.
4423TEST_F(URLRequestTestHTTP, URLRequestDelegateOnRedirectCancelled) {
4424 ASSERT_TRUE(test_server_.Start());
4425
4426 const AsyncLoggingUrlRequestDelegate::CancelStage kCancelStages[] = {
4427 AsyncLoggingUrlRequestDelegate::CANCEL_ON_RECEIVED_REDIRECT,
4428 AsyncLoggingUrlRequestDelegate::CANCEL_ON_RESPONSE_STARTED,
4429 AsyncLoggingUrlRequestDelegate::CANCEL_ON_READ_COMPLETED,
4430 };
4431
4432 for (size_t test_case = 0; test_case < arraysize(kCancelStages);
4433 ++test_case) {
4434 AsyncLoggingUrlRequestDelegate request_delegate(kCancelStages[test_case]);
4435 TestURLRequestContext context(true);
4436 CapturingNetLog net_log;
4437 context.set_network_delegate(NULL);
4438 context.set_net_log(&net_log);
4439 context.Init();
4440
4441 {
4442 URLRequest r(test_server_.GetURL("server-redirect?simple.html"),
[email protected]2ca01e52013-10-31 22:05:194443 DEFAULT_PRIORITY,
4444 &request_delegate,
4445 &context);
[email protected]abe1c4a2013-10-25 19:28:514446 LoadStateWithParam load_state = r.GetLoadState();
4447 r.Start();
4448 base::RunLoop().Run();
4449 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
4450 }
4451
4452 CapturingNetLog::CapturedEntryList entries;
4453 net_log.GetEntries(&entries);
4454
4455 // Delegate info is always logged in both OnReceivedRedirect and
4456 // OnResponseStarted. In the CANCEL_ON_RECEIVED_REDIRECT, the
4457 // OnResponseStarted delegate call is after cancellation, but logging is
4458 // still currently supported in that call.
4459 size_t log_position = 0;
4460 for (int i = 0; i < 2; ++i) {
[email protected]1826a402014-01-08 15:40:484461 if (i == 0) {
4462 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4463 entries, log_position) + 1;
4464 }
4465
[email protected]abe1c4a2013-10-25 19:28:514466 log_position = ExpectLogContainsSomewhereAfter(
4467 entries,
4468 log_position,
4469 NetLog::TYPE_URL_REQUEST_DELEGATE,
4470 NetLog::PHASE_BEGIN);
4471
4472 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4473 log_position + 1);
4474
4475 ASSERT_LT(log_position, entries.size());
4476 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4477 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4478 }
4479
4480 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4481 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4482 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4483 entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE));
4484 }
4485}
4486
[email protected]847c0fa92012-11-06 16:37:424487namespace {
4488
4489const char kExtraHeader[] = "Allow-Snafu";
4490const char kExtraValue[] = "fubar";
4491
4492class RedirectWithAdditionalHeadersDelegate : public TestDelegate {
[email protected]46fadfd2013-02-06 09:40:164493 virtual void OnReceivedRedirect(net::URLRequest* request,
4494 const GURL& new_url,
4495 bool* defer_redirect) OVERRIDE {
[email protected]847c0fa92012-11-06 16:37:424496 TestDelegate::OnReceivedRedirect(request, new_url, defer_redirect);
4497 request->SetExtraRequestHeaderByName(kExtraHeader, kExtraValue, false);
4498 }
4499};
4500
4501} // namespace
4502
4503TEST_F(URLRequestTestHTTP, RedirectWithAdditionalHeadersTest) {
4504 ASSERT_TRUE(test_server_.Start());
4505
4506 GURL destination_url = test_server_.GetURL(
4507 "echoheader?" + std::string(kExtraHeader));
4508 GURL original_url = test_server_.GetURL(
4509 "server-redirect?" + destination_url.spec());
4510 RedirectWithAdditionalHeadersDelegate d;
[email protected]2ca01e52013-10-31 22:05:194511 URLRequest req(original_url, DEFAULT_PRIORITY, &d, &default_context_);
[email protected]847c0fa92012-11-06 16:37:424512 req.Start();
[email protected]255620da2013-08-19 13:14:294513 base::RunLoop().Run();
[email protected]847c0fa92012-11-06 16:37:424514
4515 std::string value;
4516 const HttpRequestHeaders& headers = req.extra_request_headers();
4517 EXPECT_TRUE(headers.GetHeader(kExtraHeader, &value));
4518 EXPECT_EQ(kExtraValue, value);
4519 EXPECT_FALSE(req.is_pending());
4520 EXPECT_FALSE(req.is_redirecting());
4521 EXPECT_EQ(kExtraValue, d.data_received());
4522}
4523
[email protected]251a1b92012-11-13 11:01:094524namespace {
4525
4526const char kExtraHeaderToRemove[] = "To-Be-Removed";
4527
4528class RedirectWithHeaderRemovalDelegate : public TestDelegate {
[email protected]46fadfd2013-02-06 09:40:164529 virtual void OnReceivedRedirect(net::URLRequest* request,
[email protected]251a1b92012-11-13 11:01:094530 const GURL& new_url,
[email protected]46fadfd2013-02-06 09:40:164531 bool* defer_redirect) OVERRIDE {
[email protected]251a1b92012-11-13 11:01:094532 TestDelegate::OnReceivedRedirect(request, new_url, defer_redirect);
4533 request->RemoveRequestHeaderByName(kExtraHeaderToRemove);
4534 }
4535};
4536
4537} // namespace
4538
4539TEST_F(URLRequestTestHTTP, RedirectWithHeaderRemovalTest) {
4540 ASSERT_TRUE(test_server_.Start());
4541
4542 GURL destination_url = test_server_.GetURL(
4543 "echoheader?" + std::string(kExtraHeaderToRemove));
4544 GURL original_url = test_server_.GetURL(
4545 "server-redirect?" + destination_url.spec());
4546 RedirectWithHeaderRemovalDelegate d;
[email protected]2ca01e52013-10-31 22:05:194547 URLRequest req(original_url, DEFAULT_PRIORITY, &d, &default_context_);
[email protected]251a1b92012-11-13 11:01:094548 req.SetExtraRequestHeaderByName(kExtraHeaderToRemove, "dummy", false);
4549 req.Start();
[email protected]255620da2013-08-19 13:14:294550 base::RunLoop().Run();
[email protected]251a1b92012-11-13 11:01:094551
4552 std::string value;
4553 const HttpRequestHeaders& headers = req.extra_request_headers();
4554 EXPECT_FALSE(headers.GetHeader(kExtraHeaderToRemove, &value));
4555 EXPECT_FALSE(req.is_pending());
4556 EXPECT_FALSE(req.is_redirecting());
4557 EXPECT_EQ("None", d.data_received());
4558}
4559
[email protected]316c1e5e2012-09-12 15:17:444560TEST_F(URLRequestTestHTTP, CancelTest) {
4561 TestDelegate d;
4562 {
[email protected]2ca01e52013-10-31 22:05:194563 URLRequest r(GURL("http://www.google.com/"),
4564 DEFAULT_PRIORITY,
4565 &d,
4566 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:444567
4568 r.Start();
4569 EXPECT_TRUE(r.is_pending());
4570
4571 r.Cancel();
4572
[email protected]255620da2013-08-19 13:14:294573 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:444574
4575 // We expect to receive OnResponseStarted even though the request has been
4576 // cancelled.
4577 EXPECT_EQ(1, d.response_started_count());
4578 EXPECT_EQ(0, d.bytes_received());
4579 EXPECT_FALSE(d.received_data_before_response());
4580 }
4581}
4582
4583TEST_F(URLRequestTestHTTP, CancelTest2) {
4584 ASSERT_TRUE(test_server_.Start());
4585
4586 TestDelegate d;
4587 {
[email protected]2ca01e52013-10-31 22:05:194588 URLRequest r(test_server_.GetURL(std::string()),
4589 DEFAULT_PRIORITY,
4590 &d,
4591 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:444592
4593 d.set_cancel_in_response_started(true);
4594
4595 r.Start();
4596 EXPECT_TRUE(r.is_pending());
4597
[email protected]255620da2013-08-19 13:14:294598 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:444599
4600 EXPECT_EQ(1, d.response_started_count());
4601 EXPECT_EQ(0, d.bytes_received());
4602 EXPECT_FALSE(d.received_data_before_response());
4603 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
4604 }
4605}
4606
4607TEST_F(URLRequestTestHTTP, CancelTest3) {
4608 ASSERT_TRUE(test_server_.Start());
4609
4610 TestDelegate d;
4611 {
[email protected]2ca01e52013-10-31 22:05:194612 URLRequest r(test_server_.GetURL(std::string()),
4613 DEFAULT_PRIORITY,
4614 &d,
4615 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:444616
4617 d.set_cancel_in_received_data(true);
4618
4619 r.Start();
4620 EXPECT_TRUE(r.is_pending());
4621
[email protected]255620da2013-08-19 13:14:294622 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:444623
4624 EXPECT_EQ(1, d.response_started_count());
4625 // There is no guarantee about how much data was received
4626 // before the cancel was issued. It could have been 0 bytes,
4627 // or it could have been all the bytes.
4628 // EXPECT_EQ(0, d.bytes_received());
4629 EXPECT_FALSE(d.received_data_before_response());
4630 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
4631 }
4632}
4633
4634TEST_F(URLRequestTestHTTP, CancelTest4) {
4635 ASSERT_TRUE(test_server_.Start());
4636
4637 TestDelegate d;
4638 {
[email protected]2ca01e52013-10-31 22:05:194639 URLRequest r(test_server_.GetURL(std::string()),
4640 DEFAULT_PRIORITY,
4641 &d,
4642 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:444643
4644 r.Start();
4645 EXPECT_TRUE(r.is_pending());
4646
4647 // The request will be implicitly canceled when it is destroyed. The
4648 // test delegate must not post a quit message when this happens because
4649 // this test doesn't actually have a message loop. The quit message would
4650 // get put on this thread's message queue and the next test would exit
4651 // early, causing problems.
4652 d.set_quit_on_complete(false);
4653 }
4654 // expect things to just cleanup properly.
4655
4656 // we won't actually get a received reponse here because we've never run the
4657 // message loop
4658 EXPECT_FALSE(d.received_data_before_response());
4659 EXPECT_EQ(0, d.bytes_received());
4660}
4661
4662TEST_F(URLRequestTestHTTP, CancelTest5) {
4663 ASSERT_TRUE(test_server_.Start());
4664
4665 // populate cache
4666 {
4667 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:194668 URLRequest r(test_server_.GetURL("cachetime"),
4669 DEFAULT_PRIORITY,
4670 &d,
4671 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:444672 r.Start();
[email protected]255620da2013-08-19 13:14:294673 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:444674 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
4675 }
4676
4677 // cancel read from cache (see bug 990242)
4678 {
4679 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:194680 URLRequest r(test_server_.GetURL("cachetime"),
4681 DEFAULT_PRIORITY,
4682 &d,
4683 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:444684 r.Start();
4685 r.Cancel();
[email protected]255620da2013-08-19 13:14:294686 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:444687
4688 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
4689 EXPECT_EQ(1, d.response_started_count());
4690 EXPECT_EQ(0, d.bytes_received());
4691 EXPECT_FALSE(d.received_data_before_response());
4692 }
4693}
4694
4695TEST_F(URLRequestTestHTTP, PostTest) {
4696 ASSERT_TRUE(test_server_.Start());
4697 HTTPUploadDataOperationTest("POST");
4698}
4699
4700TEST_F(URLRequestTestHTTP, PutTest) {
4701 ASSERT_TRUE(test_server_.Start());
4702 HTTPUploadDataOperationTest("PUT");
4703}
4704
4705TEST_F(URLRequestTestHTTP, PostEmptyTest) {
4706 ASSERT_TRUE(test_server_.Start());
4707
4708 TestDelegate d;
4709 {
[email protected]2ca01e52013-10-31 22:05:194710 URLRequest r(
4711 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, &default_context_);
[email protected]316c1e5e2012-09-12 15:17:444712 r.set_method("POST");
4713
4714 r.Start();
4715 EXPECT_TRUE(r.is_pending());
4716
[email protected]255620da2013-08-19 13:14:294717 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:444718
[email protected]329b68b2012-11-14 17:54:274719 ASSERT_EQ(1, d.response_started_count())
4720 << "request failed: " << r.status().status()
4721 << ", error: " << r.status().error();
[email protected]316c1e5e2012-09-12 15:17:444722
4723 EXPECT_FALSE(d.received_data_before_response());
4724 EXPECT_TRUE(d.data_received().empty());
4725 }
4726}
4727
4728TEST_F(URLRequestTestHTTP, PostFileTest) {
4729 ASSERT_TRUE(test_server_.Start());
4730
4731 TestDelegate d;
4732 {
[email protected]2ca01e52013-10-31 22:05:194733 URLRequest r(
4734 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, &default_context_);
[email protected]316c1e5e2012-09-12 15:17:444735 r.set_method("POST");
4736
[email protected]6cdfd7f2013-02-08 20:40:154737 base::FilePath dir;
[email protected]316c1e5e2012-09-12 15:17:444738 PathService::Get(base::DIR_EXE, &dir);
[email protected]37b3c1992014-03-11 20:59:024739 base::SetCurrentDirectory(dir);
[email protected]316c1e5e2012-09-12 15:17:444740
[email protected]f288ef02012-12-15 20:28:284741 ScopedVector<UploadElementReader> element_readers;
[email protected]316c1e5e2012-09-12 15:17:444742
[email protected]6cdfd7f2013-02-08 20:40:154743 base::FilePath path;
[email protected]316c1e5e2012-09-12 15:17:444744 PathService::Get(base::DIR_SOURCE_ROOT, &path);
4745 path = path.Append(FILE_PATH_LITERAL("net"));
4746 path = path.Append(FILE_PATH_LITERAL("data"));
4747 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
4748 path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
[email protected]cadac622013-06-11 16:46:364749 element_readers.push_back(
4750 new UploadFileElementReader(base::MessageLoopProxy::current().get(),
4751 path,
4752 0,
4753 kuint64max,
4754 base::Time()));
[email protected]96c77a72013-09-24 09:49:204755 r.set_upload(make_scoped_ptr(
4756 new UploadDataStream(element_readers.Pass(), 0)));
[email protected]316c1e5e2012-09-12 15:17:444757
4758 r.Start();
4759 EXPECT_TRUE(r.is_pending());
4760
[email protected]255620da2013-08-19 13:14:294761 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:444762
[email protected]329b68b2012-11-14 17:54:274763 int64 size = 0;
[email protected]56285702013-12-04 18:22:494764 ASSERT_EQ(true, base::GetFileSize(path, &size));
[email protected]4356f0f2013-04-07 00:58:174765 scoped_ptr<char[]> buf(new char[size]);
[email protected]316c1e5e2012-09-12 15:17:444766
[email protected]7600d0b2013-12-08 21:43:304767 ASSERT_EQ(size, base::ReadFile(path, buf.get(), size));
[email protected]316c1e5e2012-09-12 15:17:444768
[email protected]329b68b2012-11-14 17:54:274769 ASSERT_EQ(1, d.response_started_count())
4770 << "request failed: " << r.status().status()
4771 << ", error: " << r.status().error();
[email protected]316c1e5e2012-09-12 15:17:444772
4773 EXPECT_FALSE(d.received_data_before_response());
4774
[email protected]329b68b2012-11-14 17:54:274775 EXPECT_EQ(size, d.bytes_received());
4776 EXPECT_EQ(std::string(&buf[0], size), d.data_received());
[email protected]316c1e5e2012-09-12 15:17:444777 }
4778}
4779
[email protected]999dd8c2013-11-12 06:45:544780TEST_F(URLRequestTestHTTP, PostUnreadableFileTest) {
4781 ASSERT_TRUE(test_server_.Start());
4782
4783 TestDelegate d;
4784 {
4785 URLRequest r(test_server_.GetURL("echo"), DEFAULT_PRIORITY,
4786 &d, &default_context_);
4787 r.set_method("POST");
4788
4789 ScopedVector<UploadElementReader> element_readers;
4790
4791 element_readers.push_back(new UploadFileElementReader(
4792 base::MessageLoopProxy::current().get(),
4793 base::FilePath(FILE_PATH_LITERAL(
4794 "c:\\path\\to\\non\\existant\\file.randomness.12345")),
4795 0,
4796 kuint64max,
4797 base::Time()));
4798 r.set_upload(make_scoped_ptr(
4799 new UploadDataStream(element_readers.Pass(), 0)));
4800
4801 r.Start();
4802 EXPECT_TRUE(r.is_pending());
4803
4804 base::RunLoop().Run();
4805
[email protected]999dd8c2013-11-12 06:45:544806 EXPECT_TRUE(d.request_failed());
4807 EXPECT_FALSE(d.received_data_before_response());
4808 EXPECT_EQ(0, d.bytes_received());
4809 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
4810 EXPECT_EQ(ERR_FILE_NOT_FOUND, r.status().error());
[email protected]999dd8c2013-11-12 06:45:544811 }
4812}
4813
[email protected]316c1e5e2012-09-12 15:17:444814TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) {
4815 ASSERT_TRUE(test_server_.Start());
4816
4817 TestDelegate d;
4818 {
[email protected]2ca01e52013-10-31 22:05:194819 URLRequest r(
4820 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, &default_context_);
[email protected]316c1e5e2012-09-12 15:17:444821 r.EnableChunkedUpload();
4822 r.set_method("POST");
4823 AddChunksToUpload(&r);
4824 r.Start();
4825 EXPECT_TRUE(r.is_pending());
4826
[email protected]255620da2013-08-19 13:14:294827 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:444828
4829 VerifyReceivedDataMatchesChunks(&r, &d);
4830 }
4831}
4832
[email protected]329b68b2012-11-14 17:54:274833TEST_F(URLRequestTestHTTP, TestPostChunkedDataJustAfterStart) {
4834 ASSERT_TRUE(test_server_.Start());
4835
4836 TestDelegate d;
4837 {
[email protected]2ca01e52013-10-31 22:05:194838 URLRequest r(
4839 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, &default_context_);
[email protected]329b68b2012-11-14 17:54:274840 r.EnableChunkedUpload();
4841 r.set_method("POST");
4842 r.Start();
4843 EXPECT_TRUE(r.is_pending());
4844 AddChunksToUpload(&r);
[email protected]255620da2013-08-19 13:14:294845 base::RunLoop().Run();
[email protected]329b68b2012-11-14 17:54:274846
4847 VerifyReceivedDataMatchesChunks(&r, &d);
4848 }
4849}
4850
[email protected]316c1e5e2012-09-12 15:17:444851TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) {
4852 ASSERT_TRUE(test_server_.Start());
4853
4854 TestDelegate d;
4855 {
[email protected]2ca01e52013-10-31 22:05:194856 URLRequest r(
4857 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, &default_context_);
[email protected]316c1e5e2012-09-12 15:17:444858 r.EnableChunkedUpload();
4859 r.set_method("POST");
4860 r.Start();
4861 EXPECT_TRUE(r.is_pending());
4862
[email protected]255620da2013-08-19 13:14:294863 base::RunLoop().RunUntilIdle();
[email protected]316c1e5e2012-09-12 15:17:444864 AddChunksToUpload(&r);
[email protected]255620da2013-08-19 13:14:294865 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:444866
4867 VerifyReceivedDataMatchesChunks(&r, &d);
4868 }
4869}
4870
4871TEST_F(URLRequestTestHTTP, ResponseHeadersTest) {
4872 ASSERT_TRUE(test_server_.Start());
4873
4874 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:194875 URLRequest req(test_server_.GetURL("files/with-headers.html"),
4876 DEFAULT_PRIORITY,
4877 &d,
4878 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:444879 req.Start();
[email protected]255620da2013-08-19 13:14:294880 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:444881
4882 const HttpResponseHeaders* headers = req.response_headers();
4883
4884 // Simple sanity check that response_info() accesses the same data.
4885 EXPECT_EQ(headers, req.response_info().headers.get());
4886
4887 std::string header;
4888 EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header));
4889 EXPECT_EQ("private", header);
4890
4891 header.clear();
4892 EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header));
4893 EXPECT_EQ("text/html; charset=ISO-8859-1", header);
4894
4895 // The response has two "X-Multiple-Entries" headers.
4896 // This verfies our output has them concatenated together.
4897 header.clear();
4898 EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header));
4899 EXPECT_EQ("a, b", header);
4900}
4901
[email protected]242d8562012-10-30 21:20:464902TEST_F(URLRequestTestHTTP, ProcessSTS) {
[email protected]ce7d0cbc2013-05-03 18:57:224903 SpawnedTestServer::SSLOptions ssl_options;
4904 SpawnedTestServer https_test_server(
4905 SpawnedTestServer::TYPE_HTTPS,
[email protected]242d8562012-10-30 21:20:464906 ssl_options,
[email protected]6cdfd7f2013-02-08 20:40:154907 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
[email protected]242d8562012-10-30 21:20:464908 ASSERT_TRUE(https_test_server.Start());
4909
4910 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:194911 URLRequest request(https_test_server.GetURL("files/hsts-headers.html"),
4912 DEFAULT_PRIORITY,
4913 &d,
4914 &default_context_);
[email protected]242d8562012-10-30 21:20:464915 request.Start();
[email protected]255620da2013-08-19 13:14:294916 base::RunLoop().Run();
[email protected]242d8562012-10-30 21:20:464917
4918 TransportSecurityState* security_state =
4919 default_context_.transport_security_state();
4920 bool sni_available = true;
4921 TransportSecurityState::DomainState domain_state;
4922 EXPECT_TRUE(security_state->GetDomainState(
[email protected]22e045f2013-09-20 03:54:034923 SpawnedTestServer::kLocalhost, sni_available, &domain_state));
[email protected]242d8562012-10-30 21:20:464924 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
4925 domain_state.upgrade_mode);
[email protected]a165f092013-06-12 16:10:054926 EXPECT_TRUE(domain_state.sts_include_subdomains);
[email protected]22e045f2013-09-20 03:54:034927 EXPECT_FALSE(domain_state.pkp_include_subdomains);
[email protected]37fd55fb2013-06-29 13:13:274928#if defined(OS_ANDROID)
4929 // Android's CertVerifyProc does not (yet) handle pins.
4930#else
4931 EXPECT_FALSE(domain_state.HasPublicKeyPins());
4932#endif
4933}
4934
4935// Android's CertVerifyProc does not (yet) handle pins. Therefore, it will
4936// reject HPKP headers, and a test setting only HPKP headers will fail (no
4937// DomainState present because header rejected).
4938#if defined(OS_ANDROID)
4939#define MAYBE_ProcessPKP DISABLED_ProcessPKP
4940#else
4941#define MAYBE_ProcessPKP ProcessPKP
4942#endif
4943
4944// Tests that enabling HPKP on a domain does not affect the HSTS
4945// validity/expiration.
4946TEST_F(URLRequestTestHTTP, MAYBE_ProcessPKP) {
4947 SpawnedTestServer::SSLOptions ssl_options;
4948 SpawnedTestServer https_test_server(
4949 SpawnedTestServer::TYPE_HTTPS,
4950 ssl_options,
4951 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
4952 ASSERT_TRUE(https_test_server.Start());
4953
4954 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:194955 URLRequest request(https_test_server.GetURL("files/hpkp-headers.html"),
4956 DEFAULT_PRIORITY,
4957 &d,
4958 &default_context_);
[email protected]37fd55fb2013-06-29 13:13:274959 request.Start();
[email protected]255620da2013-08-19 13:14:294960 base::RunLoop().Run();
[email protected]37fd55fb2013-06-29 13:13:274961
4962 TransportSecurityState* security_state =
4963 default_context_.transport_security_state();
4964 bool sni_available = true;
4965 TransportSecurityState::DomainState domain_state;
4966 EXPECT_TRUE(security_state->GetDomainState(
[email protected]22e045f2013-09-20 03:54:034967 SpawnedTestServer::kLocalhost, sni_available, &domain_state));
[email protected]37fd55fb2013-06-29 13:13:274968 EXPECT_EQ(TransportSecurityState::DomainState::MODE_DEFAULT,
4969 domain_state.upgrade_mode);
4970 EXPECT_FALSE(domain_state.sts_include_subdomains);
4971 EXPECT_FALSE(domain_state.pkp_include_subdomains);
4972 EXPECT_TRUE(domain_state.HasPublicKeyPins());
4973 EXPECT_NE(domain_state.upgrade_expiry,
4974 domain_state.dynamic_spki_hashes_expiry);
[email protected]242d8562012-10-30 21:20:464975}
4976
4977TEST_F(URLRequestTestHTTP, ProcessSTSOnce) {
[email protected]ce7d0cbc2013-05-03 18:57:224978 SpawnedTestServer::SSLOptions ssl_options;
4979 SpawnedTestServer https_test_server(
4980 SpawnedTestServer::TYPE_HTTPS,
[email protected]242d8562012-10-30 21:20:464981 ssl_options,
[email protected]6cdfd7f2013-02-08 20:40:154982 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
[email protected]242d8562012-10-30 21:20:464983 ASSERT_TRUE(https_test_server.Start());
4984
4985 TestDelegate d;
4986 URLRequest request(
4987 https_test_server.GetURL("files/hsts-multiple-headers.html"),
[email protected]2ca01e52013-10-31 22:05:194988 DEFAULT_PRIORITY,
[email protected]242d8562012-10-30 21:20:464989 &d,
4990 &default_context_);
4991 request.Start();
[email protected]255620da2013-08-19 13:14:294992 base::RunLoop().Run();
[email protected]242d8562012-10-30 21:20:464993
4994 // We should have set parameters from the first header, not the second.
4995 TransportSecurityState* security_state =
4996 default_context_.transport_security_state();
4997 bool sni_available = true;
4998 TransportSecurityState::DomainState domain_state;
4999 EXPECT_TRUE(security_state->GetDomainState(
[email protected]22e045f2013-09-20 03:54:035000 SpawnedTestServer::kLocalhost, sni_available, &domain_state));
[email protected]242d8562012-10-30 21:20:465001 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
5002 domain_state.upgrade_mode);
[email protected]a165f092013-06-12 16:10:055003 EXPECT_FALSE(domain_state.sts_include_subdomains);
5004 EXPECT_FALSE(domain_state.pkp_include_subdomains);
[email protected]242d8562012-10-30 21:20:465005}
5006
[email protected]9f972ec2013-04-10 20:24:365007TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP) {
[email protected]ce7d0cbc2013-05-03 18:57:225008 SpawnedTestServer::SSLOptions ssl_options;
5009 SpawnedTestServer https_test_server(
5010 SpawnedTestServer::TYPE_HTTPS,
[email protected]9f972ec2013-04-10 20:24:365011 ssl_options,
5012 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
5013 ASSERT_TRUE(https_test_server.Start());
5014
5015 TestDelegate d;
5016 URLRequest request(
5017 https_test_server.GetURL("files/hsts-and-hpkp-headers.html"),
[email protected]2ca01e52013-10-31 22:05:195018 DEFAULT_PRIORITY,
[email protected]9f972ec2013-04-10 20:24:365019 &d,
5020 &default_context_);
5021 request.Start();
[email protected]255620da2013-08-19 13:14:295022 base::RunLoop().Run();
[email protected]9f972ec2013-04-10 20:24:365023
5024 // We should have set parameters from the first header, not the second.
5025 TransportSecurityState* security_state =
5026 default_context_.transport_security_state();
5027 bool sni_available = true;
5028 TransportSecurityState::DomainState domain_state;
5029 EXPECT_TRUE(security_state->GetDomainState(
[email protected]22e045f2013-09-20 03:54:035030 SpawnedTestServer::kLocalhost, sni_available, &domain_state));
[email protected]9f972ec2013-04-10 20:24:365031 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
5032 domain_state.upgrade_mode);
5033#if defined(OS_ANDROID)
5034 // Android's CertVerifyProc does not (yet) handle pins.
5035#else
5036 EXPECT_TRUE(domain_state.HasPublicKeyPins());
5037#endif
5038 EXPECT_NE(domain_state.upgrade_expiry,
5039 domain_state.dynamic_spki_hashes_expiry);
5040
[email protected]a165f092013-06-12 16:10:055041 // Even though there is an HSTS header asserting includeSubdomains, it is
5042 // the *second* such header, and we MUST process only the first.
5043 EXPECT_FALSE(domain_state.sts_include_subdomains);
5044 // includeSubdomains does not occur in the test HPKP header.
5045 EXPECT_FALSE(domain_state.pkp_include_subdomains);
[email protected]9f972ec2013-04-10 20:24:365046}
5047
[email protected]37fd55fb2013-06-29 13:13:275048// Tests that when multiple HPKP headers are present, asserting different
5049// policies, that only the first such policy is processed.
5050TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP2) {
5051 SpawnedTestServer::SSLOptions ssl_options;
5052 SpawnedTestServer https_test_server(
5053 SpawnedTestServer::TYPE_HTTPS,
5054 ssl_options,
5055 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
5056 ASSERT_TRUE(https_test_server.Start());
5057
5058 TestDelegate d;
5059 URLRequest request(
5060 https_test_server.GetURL("files/hsts-and-hpkp-headers2.html"),
[email protected]2ca01e52013-10-31 22:05:195061 DEFAULT_PRIORITY,
[email protected]37fd55fb2013-06-29 13:13:275062 &d,
5063 &default_context_);
5064 request.Start();
[email protected]255620da2013-08-19 13:14:295065 base::RunLoop().Run();
[email protected]37fd55fb2013-06-29 13:13:275066
5067 TransportSecurityState* security_state =
5068 default_context_.transport_security_state();
5069 bool sni_available = true;
5070 TransportSecurityState::DomainState domain_state;
5071 EXPECT_TRUE(security_state->GetDomainState(
[email protected]22e045f2013-09-20 03:54:035072 SpawnedTestServer::kLocalhost, sni_available, &domain_state));
[email protected]37fd55fb2013-06-29 13:13:275073 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
5074 domain_state.upgrade_mode);
5075#if defined(OS_ANDROID)
5076 // Android's CertVerifyProc does not (yet) handle pins.
5077#else
5078 EXPECT_TRUE(domain_state.HasPublicKeyPins());
5079#endif
5080 EXPECT_NE(domain_state.upgrade_expiry,
5081 domain_state.dynamic_spki_hashes_expiry);
5082
5083 EXPECT_TRUE(domain_state.sts_include_subdomains);
5084 EXPECT_FALSE(domain_state.pkp_include_subdomains);
5085}
5086
[email protected]316c1e5e2012-09-12 15:17:445087TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) {
5088 ASSERT_TRUE(test_server_.Start());
5089
5090 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:195091 URLRequest req(test_server_.GetURL("files/content-type-normalization.html"),
5092 DEFAULT_PRIORITY,
5093 &d,
5094 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:445095 req.Start();
[email protected]255620da2013-08-19 13:14:295096 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445097
5098 std::string mime_type;
5099 req.GetMimeType(&mime_type);
5100 EXPECT_EQ("text/html", mime_type);
5101
5102 std::string charset;
5103 req.GetCharset(&charset);
5104 EXPECT_EQ("utf-8", charset);
5105 req.Cancel();
5106}
5107
[email protected]e0f35c92013-05-08 16:04:345108TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictRedirects) {
5109 // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget().
5110 GURL file_url("file:///foo.txt");
5111 GURL data_url("data:,foo");
[email protected]255620da2013-08-19 13:14:295112 FileProtocolHandler file_protocol_handler(base::MessageLoopProxy::current());
[email protected]e0f35c92013-05-08 16:04:345113 EXPECT_FALSE(file_protocol_handler.IsSafeRedirectTarget(file_url));
5114 DataProtocolHandler data_protocol_handler;
[email protected]588614c22013-08-16 00:09:025115 EXPECT_FALSE(data_protocol_handler.IsSafeRedirectTarget(data_url));
[email protected]e0f35c92013-05-08 16:04:345116
5117 // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget().
5118 EXPECT_FALSE(job_factory_.IsSafeRedirectTarget(file_url));
[email protected]588614c22013-08-16 00:09:025119 EXPECT_FALSE(job_factory_.IsSafeRedirectTarget(data_url));
[email protected]e0f35c92013-05-08 16:04:345120}
5121
[email protected]588614c22013-08-16 00:09:025122TEST_F(URLRequestTestHTTP, RestrictFileRedirects) {
[email protected]316c1e5e2012-09-12 15:17:445123 ASSERT_TRUE(test_server_.Start());
5124
5125 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:195126 URLRequest req(test_server_.GetURL("files/redirect-to-file.html"),
5127 DEFAULT_PRIORITY,
5128 &d,
5129 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:445130 req.Start();
[email protected]255620da2013-08-19 13:14:295131 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445132
5133 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
5134 EXPECT_EQ(ERR_UNSAFE_REDIRECT, req.status().error());
5135}
5136
[email protected]588614c22013-08-16 00:09:025137TEST_F(URLRequestTestHTTP, RestrictDataRedirects) {
5138 ASSERT_TRUE(test_server_.Start());
5139
5140 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:195141 URLRequest req(test_server_.GetURL("files/redirect-to-data.html"),
5142 DEFAULT_PRIORITY,
5143 &d,
5144 &default_context_);
[email protected]588614c22013-08-16 00:09:025145 req.Start();
5146 base::MessageLoop::current()->Run();
5147
5148 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
5149 EXPECT_EQ(ERR_UNSAFE_REDIRECT, req.status().error());
5150}
5151
[email protected]316c1e5e2012-09-12 15:17:445152TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) {
5153 ASSERT_TRUE(test_server_.Start());
5154
5155 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:195156 URLRequest req(test_server_.GetURL("files/redirect-to-invalid-url.html"),
5157 DEFAULT_PRIORITY,
5158 &d,
5159 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:445160 req.Start();
[email protected]255620da2013-08-19 13:14:295161 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445162
5163 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
5164 EXPECT_EQ(ERR_INVALID_URL, req.status().error());
5165}
5166
[email protected]e50efea2014-03-24 18:41:005167// Make sure redirects are cached, despite not reading their bodies.
5168TEST_F(URLRequestTestHTTP, CacheRedirect) {
5169 ASSERT_TRUE(test_server_.Start());
5170 GURL redirect_url =
5171 test_server_.GetURL("files/redirect302-to-echo-cacheable");
5172
5173 {
5174 TestDelegate d;
5175 URLRequest req(redirect_url, DEFAULT_PRIORITY, &d, &default_context_);
5176 req.Start();
5177 base::RunLoop().Run();
5178 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
5179 EXPECT_EQ(1, d.received_redirect_count());
5180 EXPECT_EQ(test_server_.GetURL("echo"), req.url());
5181 }
5182
5183 {
5184 TestDelegate d;
5185 d.set_quit_on_redirect(true);
5186 URLRequest req(redirect_url, DEFAULT_PRIORITY, &d, &default_context_);
5187 req.Start();
5188 base::RunLoop().Run();
5189
5190 EXPECT_EQ(1, d.received_redirect_count());
5191 EXPECT_EQ(0, d.response_started_count());
5192 EXPECT_TRUE(req.was_cached());
5193
5194 req.FollowDeferredRedirect();
5195 base::RunLoop().Run();
5196 EXPECT_EQ(1, d.received_redirect_count());
5197 EXPECT_EQ(1, d.response_started_count());
5198 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
5199 EXPECT_EQ(test_server_.GetURL("echo"), req.url());
5200 }
5201}
5202
5203// Make sure a request isn't cached when a NetworkDelegate forces a redirect
5204// when the headers are read, since the body won't have been read.
5205TEST_F(URLRequestTestHTTP, NoCacheOnNetworkDelegateRedirect) {
5206 ASSERT_TRUE(test_server_.Start());
5207 // URL that is normally cached.
5208 GURL initial_url = test_server_.GetURL("cachetime");
5209
5210 {
5211 // Set up the TestNetworkDelegate tp force a redirect.
5212 GURL redirect_to_url = test_server_.GetURL("echo");
5213 default_network_delegate_.set_redirect_on_headers_received_url(
5214 redirect_to_url);
5215
5216 TestDelegate d;
5217 URLRequest req(initial_url, DEFAULT_PRIORITY, &d, &default_context_);
5218 req.Start();
5219 base::RunLoop().Run();
5220 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
5221 EXPECT_EQ(1, d.received_redirect_count());
5222 EXPECT_EQ(redirect_to_url, req.url());
5223 }
5224
5225 {
5226 TestDelegate d;
5227 URLRequest req(initial_url, DEFAULT_PRIORITY, &d, &default_context_);
5228 req.Start();
5229 base::RunLoop().Run();
5230
5231 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
5232 EXPECT_FALSE(req.was_cached());
5233 EXPECT_EQ(0, d.received_redirect_count());
5234 EXPECT_EQ(initial_url, req.url());
5235 }
5236}
5237
[email protected]316c1e5e2012-09-12 15:17:445238TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) {
5239 ASSERT_TRUE(test_server_.Start());
5240
5241 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:195242 URLRequest req(test_server_.GetURL("echoheader?Referer"),
5243 DEFAULT_PRIORITY,
5244 &d,
5245 &default_context_);
[email protected]99ecf6e2013-04-10 22:46:135246 req.SetReferrer("http://user:[email protected]/");
[email protected]316c1e5e2012-09-12 15:17:445247 req.Start();
[email protected]255620da2013-08-19 13:14:295248 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445249
5250 EXPECT_EQ(std::string("http://foo.com/"), d.data_received());
5251}
5252
[email protected]99ecf6e2013-04-10 22:46:135253TEST_F(URLRequestTestHTTP, NoFragmentInReferrer) {
5254 ASSERT_TRUE(test_server_.Start());
5255
5256 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:195257 URLRequest req(test_server_.GetURL("echoheader?Referer"),
5258 DEFAULT_PRIORITY,
5259 &d,
5260 &default_context_);
[email protected]99ecf6e2013-04-10 22:46:135261 req.SetReferrer("http://foo.com/test#fragment");
5262 req.Start();
[email protected]255620da2013-08-19 13:14:295263 base::RunLoop().Run();
[email protected]99ecf6e2013-04-10 22:46:135264
5265 EXPECT_EQ(std::string("http://foo.com/test"), d.data_received());
5266}
5267
5268TEST_F(URLRequestTestHTTP, EmptyReferrerAfterValidReferrer) {
5269 ASSERT_TRUE(test_server_.Start());
5270
5271 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:195272 URLRequest req(test_server_.GetURL("echoheader?Referer"),
5273 DEFAULT_PRIORITY,
5274 &d,
5275 &default_context_);
[email protected]99ecf6e2013-04-10 22:46:135276 req.SetReferrer("http://foo.com/test#fragment");
5277 req.SetReferrer("");
5278 req.Start();
[email protected]255620da2013-08-19 13:14:295279 base::RunLoop().Run();
[email protected]99ecf6e2013-04-10 22:46:135280
5281 EXPECT_EQ(std::string("None"), d.data_received());
5282}
5283
[email protected]1826a402014-01-08 15:40:485284// Defer network start and then resume, checking that the request was a success
5285// and bytes were received.
5286TEST_F(URLRequestTestHTTP, DeferredBeforeNetworkStart) {
5287 ASSERT_TRUE(test_server_.Start());
5288
5289 TestDelegate d;
5290 {
5291 d.set_quit_on_network_start(true);
5292 GURL test_url(test_server_.GetURL("echo"));
5293 URLRequest req(test_url, DEFAULT_PRIORITY, &d, &default_context_);
5294
5295 req.Start();
5296 base::RunLoop().Run();
5297
5298 EXPECT_EQ(1, d.received_before_network_start_count());
5299 EXPECT_EQ(0, d.response_started_count());
5300
5301 req.ResumeNetworkStart();
5302 base::RunLoop().Run();
5303
5304 EXPECT_EQ(1, d.response_started_count());
5305 EXPECT_NE(0, d.bytes_received());
5306 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
5307 }
5308}
5309
5310// Check that OnBeforeNetworkStart is only called once even if there is a
5311// redirect.
5312TEST_F(URLRequestTestHTTP, BeforeNetworkStartCalledOnce) {
5313 ASSERT_TRUE(test_server_.Start());
5314
5315 TestDelegate d;
5316 {
5317 d.set_quit_on_redirect(true);
5318 d.set_quit_on_network_start(true);
5319 URLRequest req(test_server_.GetURL("server-redirect?echo"),
5320 DEFAULT_PRIORITY,
5321 &d,
5322 &default_context_);
5323
5324 req.Start();
5325 base::RunLoop().Run();
5326
5327 EXPECT_EQ(1, d.received_before_network_start_count());
5328 EXPECT_EQ(0, d.response_started_count());
5329 EXPECT_EQ(0, d.received_redirect_count());
5330
5331 req.ResumeNetworkStart();
5332 base::RunLoop().Run();
5333
5334 EXPECT_EQ(1, d.received_redirect_count());
5335 req.FollowDeferredRedirect();
5336 base::RunLoop().Run();
5337
5338 // Check that the redirect's new network transaction does not get propagated
5339 // to a second OnBeforeNetworkStart() notification.
5340 EXPECT_EQ(1, d.received_before_network_start_count());
5341
5342 EXPECT_EQ(1, d.response_started_count());
5343 EXPECT_NE(0, d.bytes_received());
5344 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
5345 }
5346}
5347
5348// Cancel the request after learning that the request would use the network.
5349TEST_F(URLRequestTestHTTP, CancelOnBeforeNetworkStart) {
5350 ASSERT_TRUE(test_server_.Start());
5351
5352 TestDelegate d;
5353 {
5354 d.set_quit_on_network_start(true);
5355 GURL test_url(test_server_.GetURL("echo"));
5356 URLRequest req(test_url, DEFAULT_PRIORITY, &d, &default_context_);
5357
5358 req.Start();
5359 base::RunLoop().Run();
5360
5361 EXPECT_EQ(1, d.received_before_network_start_count());
5362 EXPECT_EQ(0, d.response_started_count());
5363
5364 req.Cancel();
5365 base::RunLoop().Run();
5366
5367 EXPECT_EQ(1, d.response_started_count());
5368 EXPECT_EQ(0, d.bytes_received());
5369 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
5370 }
5371}
5372
[email protected]316c1e5e2012-09-12 15:17:445373TEST_F(URLRequestTestHTTP, CancelRedirect) {
5374 ASSERT_TRUE(test_server_.Start());
5375
5376 TestDelegate d;
5377 {
5378 d.set_cancel_in_received_redirect(true);
[email protected]2ca01e52013-10-31 22:05:195379 URLRequest req(test_server_.GetURL("files/redirect-test.html"),
5380 DEFAULT_PRIORITY,
5381 &d,
5382 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:445383 req.Start();
[email protected]255620da2013-08-19 13:14:295384 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445385
5386 EXPECT_EQ(1, d.response_started_count());
5387 EXPECT_EQ(0, d.bytes_received());
5388 EXPECT_FALSE(d.received_data_before_response());
5389 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
5390 }
5391}
5392
5393TEST_F(URLRequestTestHTTP, DeferredRedirect) {
5394 ASSERT_TRUE(test_server_.Start());
5395
5396 TestDelegate d;
5397 {
5398 d.set_quit_on_redirect(true);
[email protected]79e1fd62013-06-20 06:50:045399 GURL test_url(test_server_.GetURL("files/redirect-test.html"));
[email protected]2ca01e52013-10-31 22:05:195400 URLRequest req(test_url, DEFAULT_PRIORITY, &d, &default_context_);
[email protected]79e1fd62013-06-20 06:50:045401
[email protected]316c1e5e2012-09-12 15:17:445402 req.Start();
[email protected]255620da2013-08-19 13:14:295403 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445404
5405 EXPECT_EQ(1, d.received_redirect_count());
5406
5407 req.FollowDeferredRedirect();
[email protected]255620da2013-08-19 13:14:295408 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445409
5410 EXPECT_EQ(1, d.response_started_count());
5411 EXPECT_FALSE(d.received_data_before_response());
5412 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
5413
[email protected]6cdfd7f2013-02-08 20:40:155414 base::FilePath path;
[email protected]316c1e5e2012-09-12 15:17:445415 PathService::Get(base::DIR_SOURCE_ROOT, &path);
5416 path = path.Append(FILE_PATH_LITERAL("net"));
5417 path = path.Append(FILE_PATH_LITERAL("data"));
5418 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
5419 path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
5420
5421 std::string contents;
[email protected]82f84b92013-08-30 18:23:505422 EXPECT_TRUE(base::ReadFileToString(path, &contents));
[email protected]316c1e5e2012-09-12 15:17:445423 EXPECT_EQ(contents, d.data_received());
5424 }
5425}
5426
[email protected]79e1fd62013-06-20 06:50:045427TEST_F(URLRequestTestHTTP, DeferredRedirect_GetFullRequestHeaders) {
5428 ASSERT_TRUE(test_server_.Start());
5429
5430 TestDelegate d;
5431 {
5432 d.set_quit_on_redirect(true);
5433 GURL test_url(test_server_.GetURL("files/redirect-test.html"));
[email protected]2ca01e52013-10-31 22:05:195434 URLRequest req(test_url, DEFAULT_PRIORITY, &d, &default_context_);
[email protected]79e1fd62013-06-20 06:50:045435
5436 EXPECT_FALSE(d.have_full_request_headers());
5437
5438 req.Start();
[email protected]255620da2013-08-19 13:14:295439 base::RunLoop().Run();
[email protected]79e1fd62013-06-20 06:50:045440
5441 EXPECT_EQ(1, d.received_redirect_count());
5442 EXPECT_TRUE(d.have_full_request_headers());
5443 CheckFullRequestHeaders(d.full_request_headers(), test_url);
5444 d.ClearFullRequestHeaders();
5445
5446 req.FollowDeferredRedirect();
[email protected]255620da2013-08-19 13:14:295447 base::RunLoop().Run();
[email protected]79e1fd62013-06-20 06:50:045448
5449 GURL target_url(test_server_.GetURL("files/with-headers.html"));
5450 EXPECT_EQ(1, d.response_started_count());
5451 EXPECT_TRUE(d.have_full_request_headers());
5452 CheckFullRequestHeaders(d.full_request_headers(), target_url);
5453 EXPECT_FALSE(d.received_data_before_response());
5454 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
5455
5456 base::FilePath path;
5457 PathService::Get(base::DIR_SOURCE_ROOT, &path);
5458 path = path.Append(FILE_PATH_LITERAL("net"));
5459 path = path.Append(FILE_PATH_LITERAL("data"));
5460 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
5461 path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
5462
5463 std::string contents;
[email protected]82f84b92013-08-30 18:23:505464 EXPECT_TRUE(base::ReadFileToString(path, &contents));
[email protected]79e1fd62013-06-20 06:50:045465 EXPECT_EQ(contents, d.data_received());
5466 }
5467}
5468
[email protected]316c1e5e2012-09-12 15:17:445469TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) {
5470 ASSERT_TRUE(test_server_.Start());
5471
5472 TestDelegate d;
5473 {
5474 d.set_quit_on_redirect(true);
[email protected]2ca01e52013-10-31 22:05:195475 URLRequest req(test_server_.GetURL("files/redirect-test.html"),
5476 DEFAULT_PRIORITY,
5477 &d,
5478 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:445479 req.Start();
[email protected]255620da2013-08-19 13:14:295480 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445481
5482 EXPECT_EQ(1, d.received_redirect_count());
5483
5484 req.Cancel();
[email protected]255620da2013-08-19 13:14:295485 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445486
5487 EXPECT_EQ(1, d.response_started_count());
5488 EXPECT_EQ(0, d.bytes_received());
5489 EXPECT_FALSE(d.received_data_before_response());
5490 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
5491 }
5492}
5493
5494TEST_F(URLRequestTestHTTP, VaryHeader) {
5495 ASSERT_TRUE(test_server_.Start());
5496
[email protected]3b23a222013-05-15 21:33:255497 // Populate the cache.
[email protected]316c1e5e2012-09-12 15:17:445498 {
5499 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:195500 URLRequest req(test_server_.GetURL("echoheadercache?foo"),
5501 DEFAULT_PRIORITY,
5502 &d,
5503 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:445504 HttpRequestHeaders headers;
5505 headers.SetHeader("foo", "1");
5506 req.SetExtraRequestHeaders(headers);
5507 req.Start();
[email protected]255620da2013-08-19 13:14:295508 base::RunLoop().Run();
[email protected]3b23a222013-05-15 21:33:255509
5510 LoadTimingInfo load_timing_info;
5511 req.GetLoadTimingInfo(&load_timing_info);
5512 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
[email protected]316c1e5e2012-09-12 15:17:445513 }
5514
[email protected]3b23a222013-05-15 21:33:255515 // Expect a cache hit.
[email protected]316c1e5e2012-09-12 15:17:445516 {
5517 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:195518 URLRequest req(test_server_.GetURL("echoheadercache?foo"),
5519 DEFAULT_PRIORITY,
5520 &d,
5521 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:445522 HttpRequestHeaders headers;
5523 headers.SetHeader("foo", "1");
5524 req.SetExtraRequestHeaders(headers);
5525 req.Start();
[email protected]255620da2013-08-19 13:14:295526 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445527
5528 EXPECT_TRUE(req.was_cached());
[email protected]3b23a222013-05-15 21:33:255529
5530 LoadTimingInfo load_timing_info;
5531 req.GetLoadTimingInfo(&load_timing_info);
5532 TestLoadTimingCacheHitNoNetwork(load_timing_info);
[email protected]316c1e5e2012-09-12 15:17:445533 }
5534
[email protected]3b23a222013-05-15 21:33:255535 // Expect a cache miss.
[email protected]316c1e5e2012-09-12 15:17:445536 {
5537 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:195538 URLRequest req(test_server_.GetURL("echoheadercache?foo"),
5539 DEFAULT_PRIORITY,
5540 &d,
5541 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:445542 HttpRequestHeaders headers;
5543 headers.SetHeader("foo", "2");
5544 req.SetExtraRequestHeaders(headers);
5545 req.Start();
[email protected]255620da2013-08-19 13:14:295546 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445547
5548 EXPECT_FALSE(req.was_cached());
[email protected]3b23a222013-05-15 21:33:255549
5550 LoadTimingInfo load_timing_info;
5551 req.GetLoadTimingInfo(&load_timing_info);
5552 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
[email protected]316c1e5e2012-09-12 15:17:445553 }
5554}
5555
5556TEST_F(URLRequestTestHTTP, BasicAuth) {
5557 ASSERT_TRUE(test_server_.Start());
5558
5559 // populate the cache
5560 {
5561 TestDelegate d;
5562 d.set_credentials(AuthCredentials(kUser, kSecret));
5563
[email protected]2ca01e52013-10-31 22:05:195564 URLRequest r(test_server_.GetURL("auth-basic"),
5565 DEFAULT_PRIORITY,
5566 &d,
5567 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:445568 r.Start();
5569
[email protected]255620da2013-08-19 13:14:295570 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445571
5572 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
5573 }
5574
5575 // repeat request with end-to-end validation. since auth-basic results in a
5576 // cachable page, we expect this test to result in a 304. in which case, the
5577 // response should be fetched from the cache.
5578 {
5579 TestDelegate d;
5580 d.set_credentials(AuthCredentials(kUser, kSecret));
5581
[email protected]2ca01e52013-10-31 22:05:195582 URLRequest r(test_server_.GetURL("auth-basic"),
5583 DEFAULT_PRIORITY,
5584 &d,
5585 &default_context_);
[email protected]bb1c4662013-11-14 00:00:075586 r.SetLoadFlags(LOAD_VALIDATE_CACHE);
[email protected]316c1e5e2012-09-12 15:17:445587 r.Start();
5588
[email protected]255620da2013-08-19 13:14:295589 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445590
5591 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
5592
5593 // Should be the same cached document.
5594 EXPECT_TRUE(r.was_cached());
5595 }
5596}
5597
5598// Check that Set-Cookie headers in 401 responses are respected.
5599// http://crbug.com/6450
5600TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) {
5601 ASSERT_TRUE(test_server_.Start());
5602
5603 GURL url_requiring_auth =
5604 test_server_.GetURL("auth-basic?set-cookie-if-challenged");
5605
5606 // Request a page that will give a 401 containing a Set-Cookie header.
5607 // Verify that when the transaction is restarted, it includes the new cookie.
5608 {
[email protected]ceefd7fd2012-11-29 00:36:245609 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]316c1e5e2012-09-12 15:17:445610 TestURLRequestContext context(true);
5611 context.set_network_delegate(&network_delegate);
5612 context.Init();
5613
5614 TestDelegate d;
5615 d.set_credentials(AuthCredentials(kUser, kSecret));
5616
[email protected]2ca01e52013-10-31 22:05:195617 URLRequest r(url_requiring_auth, DEFAULT_PRIORITY, &d, &context);
[email protected]316c1e5e2012-09-12 15:17:445618 r.Start();
5619
[email protected]255620da2013-08-19 13:14:295620 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445621
5622 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
5623
5624 // Make sure we sent the cookie in the restarted transaction.
5625 EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true")
5626 != std::string::npos);
5627 }
5628
5629 // Same test as above, except this time the restart is initiated earlier
5630 // (without user intervention since identity is embedded in the URL).
5631 {
[email protected]ceefd7fd2012-11-29 00:36:245632 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]316c1e5e2012-09-12 15:17:445633 TestURLRequestContext context(true);
5634 context.set_network_delegate(&network_delegate);
5635 context.Init();
5636
5637 TestDelegate d;
5638
5639 GURL::Replacements replacements;
5640 std::string username("user2");
5641 std::string password("secret");
5642 replacements.SetUsernameStr(username);
5643 replacements.SetPasswordStr(password);
5644 GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements);
5645
[email protected]2ca01e52013-10-31 22:05:195646 URLRequest r(url_with_identity, DEFAULT_PRIORITY, &d, &context);
[email protected]316c1e5e2012-09-12 15:17:445647 r.Start();
5648
[email protected]255620da2013-08-19 13:14:295649 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445650
5651 EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos);
5652
5653 // Make sure we sent the cookie in the restarted transaction.
5654 EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true")
5655 != std::string::npos);
5656 }
5657}
5658
[email protected]58e32bb2013-01-21 18:23:255659// Tests that load timing works as expected with auth and the cache.
5660TEST_F(URLRequestTestHTTP, BasicAuthLoadTiming) {
5661 ASSERT_TRUE(test_server_.Start());
5662
5663 // populate the cache
5664 {
5665 TestDelegate d;
5666 d.set_credentials(AuthCredentials(kUser, kSecret));
5667
[email protected]2ca01e52013-10-31 22:05:195668 URLRequest r(test_server_.GetURL("auth-basic"),
5669 DEFAULT_PRIORITY,
5670 &d,
5671 &default_context_);
[email protected]58e32bb2013-01-21 18:23:255672 r.Start();
5673
[email protected]255620da2013-08-19 13:14:295674 base::RunLoop().Run();
[email protected]58e32bb2013-01-21 18:23:255675
5676 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
5677
5678 LoadTimingInfo load_timing_info_before_auth;
5679 EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeAuth(
5680 &load_timing_info_before_auth));
5681 TestLoadTimingNotReused(load_timing_info_before_auth,
5682 CONNECT_TIMING_HAS_DNS_TIMES);
5683
5684 LoadTimingInfo load_timing_info;
[email protected]0a703912013-02-02 04:25:175685 r.GetLoadTimingInfo(&load_timing_info);
[email protected]58e32bb2013-01-21 18:23:255686 // The test server does not support keep alive sockets, so the second
5687 // request with auth should use a new socket.
5688 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
5689 EXPECT_NE(load_timing_info_before_auth.socket_log_id,
5690 load_timing_info.socket_log_id);
5691 EXPECT_LE(load_timing_info_before_auth.receive_headers_end,
5692 load_timing_info.connect_timing.connect_start);
5693 }
5694
[email protected]3b23a222013-05-15 21:33:255695 // Repeat request with end-to-end validation. Since auth-basic results in a
5696 // cachable page, we expect this test to result in a 304. In which case, the
[email protected]58e32bb2013-01-21 18:23:255697 // response should be fetched from the cache.
5698 {
5699 TestDelegate d;
5700 d.set_credentials(AuthCredentials(kUser, kSecret));
5701
[email protected]2ca01e52013-10-31 22:05:195702 URLRequest r(test_server_.GetURL("auth-basic"),
5703 DEFAULT_PRIORITY,
5704 &d,
5705 &default_context_);
[email protected]bb1c4662013-11-14 00:00:075706 r.SetLoadFlags(LOAD_VALIDATE_CACHE);
[email protected]58e32bb2013-01-21 18:23:255707 r.Start();
5708
[email protected]255620da2013-08-19 13:14:295709 base::RunLoop().Run();
[email protected]58e32bb2013-01-21 18:23:255710
5711 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
5712
5713 // Should be the same cached document.
5714 EXPECT_TRUE(r.was_cached());
5715
[email protected]3b23a222013-05-15 21:33:255716 // Since there was a request that went over the wire, the load timing
5717 // information should include connection times.
[email protected]58e32bb2013-01-21 18:23:255718 LoadTimingInfo load_timing_info;
[email protected]0a703912013-02-02 04:25:175719 r.GetLoadTimingInfo(&load_timing_info);
[email protected]3b23a222013-05-15 21:33:255720 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
[email protected]58e32bb2013-01-21 18:23:255721 }
5722}
5723
[email protected]316c1e5e2012-09-12 15:17:445724// In this test, we do a POST which the server will 302 redirect.
5725// The subsequent transaction should use GET, and should not send the
5726// Content-Type header.
5727// http://code.google.com/p/chromium/issues/detail?id=843
5728TEST_F(URLRequestTestHTTP, Post302RedirectGet) {
5729 ASSERT_TRUE(test_server_.Start());
5730
5731 const char kData[] = "hello world";
5732
5733 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:195734 URLRequest req(test_server_.GetURL("files/redirect-to-echoall"),
5735 DEFAULT_PRIORITY,
5736 &d,
5737 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:445738 req.set_method("POST");
[email protected]f288ef02012-12-15 20:28:285739 req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
[email protected]316c1e5e2012-09-12 15:17:445740
5741 // Set headers (some of which are specific to the POST).
5742 HttpRequestHeaders headers;
5743 headers.AddHeadersFromString(
5744 "Content-Type: multipart/form-data; "
5745 "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n"
5746 "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,"
5747 "text/plain;q=0.8,image/png,*/*;q=0.5\r\n"
5748 "Accept-Language: en-US,en\r\n"
5749 "Accept-Charset: ISO-8859-1,*,utf-8\r\n"
5750 "Content-Length: 11\r\n"
5751 "Origin: http://localhost:1337/");
5752 req.SetExtraRequestHeaders(headers);
5753 req.Start();
[email protected]255620da2013-08-19 13:14:295754 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445755
5756 std::string mime_type;
5757 req.GetMimeType(&mime_type);
5758 EXPECT_EQ("text/html", mime_type);
5759
5760 const std::string& data = d.data_received();
5761
5762 // Check that the post-specific headers were stripped:
5763 EXPECT_FALSE(ContainsString(data, "Content-Length:"));
5764 EXPECT_FALSE(ContainsString(data, "Content-Type:"));
5765 EXPECT_FALSE(ContainsString(data, "Origin:"));
5766
5767 // These extra request headers should not have been stripped.
5768 EXPECT_TRUE(ContainsString(data, "Accept:"));
5769 EXPECT_TRUE(ContainsString(data, "Accept-Language:"));
5770 EXPECT_TRUE(ContainsString(data, "Accept-Charset:"));
5771}
5772
5773// The following tests check that we handle mutating the request method for
5774// HTTP redirects as expected.
5775// See http://crbug.com/56373 and http://crbug.com/102130.
5776
5777TEST_F(URLRequestTestHTTP, Redirect301Tests) {
5778 ASSERT_TRUE(test_server_.Start());
5779
5780 const GURL url = test_server_.GetURL("files/redirect301-to-echo");
5781
5782 HTTPRedirectMethodTest(url, "POST", "GET", true);
5783 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
5784 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
5785}
5786
5787TEST_F(URLRequestTestHTTP, Redirect302Tests) {
5788 ASSERT_TRUE(test_server_.Start());
5789
5790 const GURL url = test_server_.GetURL("files/redirect302-to-echo");
5791
5792 HTTPRedirectMethodTest(url, "POST", "GET", true);
5793 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
5794 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
5795}
5796
5797TEST_F(URLRequestTestHTTP, Redirect303Tests) {
5798 ASSERT_TRUE(test_server_.Start());
5799
5800 const GURL url = test_server_.GetURL("files/redirect303-to-echo");
5801
5802 HTTPRedirectMethodTest(url, "POST", "GET", true);
5803 HTTPRedirectMethodTest(url, "PUT", "GET", true);
5804 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
5805}
5806
5807TEST_F(URLRequestTestHTTP, Redirect307Tests) {
5808 ASSERT_TRUE(test_server_.Start());
5809
5810 const GURL url = test_server_.GetURL("files/redirect307-to-echo");
5811
5812 HTTPRedirectMethodTest(url, "POST", "POST", true);
5813 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
5814 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
5815}
5816
5817TEST_F(URLRequestTestHTTP, InterceptPost302RedirectGet) {
5818 ASSERT_TRUE(test_server_.Start());
5819
5820 const char kData[] = "hello world";
5821
5822 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:195823 URLRequest req(test_server_.GetURL("empty.html"),
5824 DEFAULT_PRIORITY,
5825 &d,
5826 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:445827 req.set_method("POST");
[email protected]f288ef02012-12-15 20:28:285828 req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
[email protected]316c1e5e2012-09-12 15:17:445829 HttpRequestHeaders headers;
5830 headers.SetHeader(HttpRequestHeaders::kContentLength,
5831 base::UintToString(arraysize(kData) - 1));
5832 req.SetExtraRequestHeaders(headers);
5833
5834 URLRequestRedirectJob* job = new URLRequestRedirectJob(
[email protected]cc05edc2013-03-08 18:04:415835 &req, &default_network_delegate_, test_server_.GetURL("echo"),
[email protected]7983c4a2014-03-12 01:47:095836 URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason");
[email protected]316c1e5e2012-09-12 15:17:445837 AddTestInterceptor()->set_main_intercept_job(job);
5838
5839 req.Start();
[email protected]255620da2013-08-19 13:14:295840 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445841 EXPECT_EQ("GET", req.method());
5842}
5843
5844TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) {
5845 ASSERT_TRUE(test_server_.Start());
5846
5847 const char kData[] = "hello world";
5848
5849 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:195850 URLRequest req(test_server_.GetURL("empty.html"),
5851 DEFAULT_PRIORITY,
5852 &d,
5853 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:445854 req.set_method("POST");
[email protected]f288ef02012-12-15 20:28:285855 req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
[email protected]316c1e5e2012-09-12 15:17:445856 HttpRequestHeaders headers;
5857 headers.SetHeader(HttpRequestHeaders::kContentLength,
5858 base::UintToString(arraysize(kData) - 1));
5859 req.SetExtraRequestHeaders(headers);
5860
5861 URLRequestRedirectJob* job = new URLRequestRedirectJob(
[email protected]cc05edc2013-03-08 18:04:415862 &req, &default_network_delegate_, test_server_.GetURL("echo"),
[email protected]7983c4a2014-03-12 01:47:095863 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT,
5864 "Very Good Reason");
[email protected]316c1e5e2012-09-12 15:17:445865 AddTestInterceptor()->set_main_intercept_job(job);
5866
5867 req.Start();
[email protected]255620da2013-08-19 13:14:295868 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445869 EXPECT_EQ("POST", req.method());
5870 EXPECT_EQ(kData, d.data_received());
5871}
5872
5873// Check that default A-L header is sent.
5874TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) {
5875 ASSERT_TRUE(test_server_.Start());
5876
[email protected]8790210c2013-12-02 05:29:535877 StaticHttpUserAgentSettings settings("en", std::string());
[email protected]ceefd7fd2012-11-29 00:36:245878 TestNetworkDelegate network_delegate; // Must outlive URLRequests.
[email protected]316c1e5e2012-09-12 15:17:445879 TestURLRequestContext context(true);
5880 context.set_network_delegate(&network_delegate);
[email protected]ee4c30d2012-11-07 15:08:435881 context.set_http_user_agent_settings(&settings);
[email protected]316c1e5e2012-09-12 15:17:445882 context.Init();
5883
5884 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:195885 URLRequest req(test_server_.GetURL("echoheader?Accept-Language"),
5886 DEFAULT_PRIORITY,
5887 &d,
5888 &context);
[email protected]316c1e5e2012-09-12 15:17:445889 req.Start();
[email protected]255620da2013-08-19 13:14:295890 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445891 EXPECT_EQ("en", d.data_received());
5892}
5893
5894// Check that an empty A-L header is not sent. http://crbug.com/77365.
5895TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) {
5896 ASSERT_TRUE(test_server_.Start());
5897
[email protected]8790210c2013-12-02 05:29:535898 std::string empty_string; // Avoid most vexing parse on line below.
5899 StaticHttpUserAgentSettings settings(empty_string, empty_string);
[email protected]ceefd7fd2012-11-29 00:36:245900 TestNetworkDelegate network_delegate; // Must outlive URLRequests.
[email protected]316c1e5e2012-09-12 15:17:445901 TestURLRequestContext context(true);
5902 context.set_network_delegate(&network_delegate);
5903 context.Init();
5904 // We override the language after initialization because empty entries
5905 // get overridden by Init().
[email protected]ee4c30d2012-11-07 15:08:435906 context.set_http_user_agent_settings(&settings);
[email protected]316c1e5e2012-09-12 15:17:445907
5908 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:195909 URLRequest req(test_server_.GetURL("echoheader?Accept-Language"),
5910 DEFAULT_PRIORITY,
5911 &d,
5912 &context);
[email protected]316c1e5e2012-09-12 15:17:445913 req.Start();
[email protected]255620da2013-08-19 13:14:295914 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445915 EXPECT_EQ("None", d.data_received());
5916}
5917
5918// Check that if request overrides the A-L header, the default is not appended.
5919// See http://crbug.com/20894
5920TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) {
5921 ASSERT_TRUE(test_server_.Start());
5922
5923 TestDelegate d;
5924 URLRequest req(test_server_.GetURL("echoheader?Accept-Language"),
[email protected]2ca01e52013-10-31 22:05:195925 DEFAULT_PRIORITY,
[email protected]316c1e5e2012-09-12 15:17:445926 &d,
5927 &default_context_);
5928 HttpRequestHeaders headers;
5929 headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru");
5930 req.SetExtraRequestHeaders(headers);
5931 req.Start();
[email protected]255620da2013-08-19 13:14:295932 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445933 EXPECT_EQ(std::string("ru"), d.data_received());
5934}
5935
5936// Check that default A-E header is sent.
5937TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) {
5938 ASSERT_TRUE(test_server_.Start());
5939
5940 TestDelegate d;
5941 URLRequest req(test_server_.GetURL("echoheader?Accept-Encoding"),
[email protected]2ca01e52013-10-31 22:05:195942 DEFAULT_PRIORITY,
[email protected]316c1e5e2012-09-12 15:17:445943 &d,
5944 &default_context_);
5945 HttpRequestHeaders headers;
5946 req.SetExtraRequestHeaders(headers);
5947 req.Start();
[email protected]255620da2013-08-19 13:14:295948 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445949 EXPECT_TRUE(ContainsString(d.data_received(), "gzip"));
5950}
5951
5952// Check that if request overrides the A-E header, the default is not appended.
5953// See http://crbug.com/47381
5954TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) {
5955 ASSERT_TRUE(test_server_.Start());
5956
5957 TestDelegate d;
5958 URLRequest req(test_server_.GetURL("echoheader?Accept-Encoding"),
[email protected]2ca01e52013-10-31 22:05:195959 DEFAULT_PRIORITY,
[email protected]316c1e5e2012-09-12 15:17:445960 &d,
5961 &default_context_);
5962 HttpRequestHeaders headers;
5963 headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, "identity");
5964 req.SetExtraRequestHeaders(headers);
5965 req.Start();
[email protected]255620da2013-08-19 13:14:295966 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445967 EXPECT_FALSE(ContainsString(d.data_received(), "gzip"));
5968 EXPECT_TRUE(ContainsString(d.data_received(), "identity"));
5969}
5970
[email protected]84f05432013-03-15 01:00:125971// Check that setting the A-C header sends the proper header.
5972TEST_F(URLRequestTestHTTP, SetAcceptCharset) {
[email protected]316c1e5e2012-09-12 15:17:445973 ASSERT_TRUE(test_server_.Start());
5974
5975 TestDelegate d;
5976 URLRequest req(test_server_.GetURL("echoheader?Accept-Charset"),
[email protected]2ca01e52013-10-31 22:05:195977 DEFAULT_PRIORITY,
[email protected]316c1e5e2012-09-12 15:17:445978 &d,
5979 &default_context_);
5980 HttpRequestHeaders headers;
5981 headers.SetHeader(HttpRequestHeaders::kAcceptCharset, "koi-8r");
5982 req.SetExtraRequestHeaders(headers);
5983 req.Start();
[email protected]255620da2013-08-19 13:14:295984 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445985 EXPECT_EQ(std::string("koi-8r"), d.data_received());
5986}
5987
5988// Check that default User-Agent header is sent.
5989TEST_F(URLRequestTestHTTP, DefaultUserAgent) {
5990 ASSERT_TRUE(test_server_.Start());
5991
5992 TestDelegate d;
5993 URLRequest req(test_server_.GetURL("echoheader?User-Agent"),
[email protected]2ca01e52013-10-31 22:05:195994 DEFAULT_PRIORITY,
[email protected]316c1e5e2012-09-12 15:17:445995 &d,
5996 &default_context_);
5997 req.Start();
[email protected]255620da2013-08-19 13:14:295998 base::RunLoop().Run();
[email protected]aa051272014-03-10 05:56:565999 EXPECT_EQ(req.context()->http_user_agent_settings()->GetUserAgent(),
[email protected]051a4b32014-01-09 04:02:376000 d.data_received());
[email protected]316c1e5e2012-09-12 15:17:446001}
6002
6003// Check that if request overrides the User-Agent header,
6004// the default is not appended.
6005TEST_F(URLRequestTestHTTP, OverrideUserAgent) {
6006 ASSERT_TRUE(test_server_.Start());
6007
6008 TestDelegate d;
6009 URLRequest req(test_server_.GetURL("echoheader?User-Agent"),
[email protected]2ca01e52013-10-31 22:05:196010 DEFAULT_PRIORITY,
[email protected]316c1e5e2012-09-12 15:17:446011 &d,
6012 &default_context_);
6013 HttpRequestHeaders headers;
6014 headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)");
6015 req.SetExtraRequestHeaders(headers);
6016 req.Start();
[email protected]255620da2013-08-19 13:14:296017 base::RunLoop().Run();
[email protected]cd6f2522014-01-16 18:27:356018 EXPECT_EQ(std::string("Lynx (textmode)"), d.data_received());
[email protected]316c1e5e2012-09-12 15:17:446019}
6020
[email protected]ee4c30d2012-11-07 15:08:436021// Check that a NULL HttpUserAgentSettings causes the corresponding empty
6022// User-Agent header to be sent but does not send the Accept-Language and
6023// Accept-Charset headers.
6024TEST_F(URLRequestTestHTTP, EmptyHttpUserAgentSettings) {
6025 ASSERT_TRUE(test_server_.Start());
6026
[email protected]ceefd7fd2012-11-29 00:36:246027 TestNetworkDelegate network_delegate; // Must outlive URLRequests.
[email protected]ee4c30d2012-11-07 15:08:436028 TestURLRequestContext context(true);
6029 context.set_network_delegate(&network_delegate);
6030 context.Init();
6031 // We override the HttpUserAgentSettings after initialization because empty
6032 // entries get overridden by Init().
6033 context.set_http_user_agent_settings(NULL);
6034
6035 struct {
6036 const char* request;
6037 const char* expected_response;
6038 } tests[] = { { "echoheader?Accept-Language", "None" },
6039 { "echoheader?Accept-Charset", "None" },
6040 { "echoheader?User-Agent", "" } };
6041
6042 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); i++) {
6043 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:196044 URLRequest req(
6045 test_server_.GetURL(tests[i].request), DEFAULT_PRIORITY, &d, &context);
[email protected]ee4c30d2012-11-07 15:08:436046 req.Start();
[email protected]255620da2013-08-19 13:14:296047 base::RunLoop().Run();
[email protected]ee4c30d2012-11-07 15:08:436048 EXPECT_EQ(tests[i].expected_response, d.data_received())
6049 << " Request = \"" << tests[i].request << "\"";
6050 }
6051}
6052
[email protected]5033ab82013-03-22 20:17:466053// Make sure that URLRequest passes on its priority updates to
6054// newly-created jobs after the first one.
6055TEST_F(URLRequestTestHTTP, SetSubsequentJobPriority) {
6056 ASSERT_TRUE(test_server_.Start());
6057
6058 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:196059 URLRequest req(test_server_.GetURL("empty.html"),
6060 DEFAULT_PRIORITY,
6061 &d,
6062 &default_context_);
[email protected]5033ab82013-03-22 20:17:466063 EXPECT_EQ(DEFAULT_PRIORITY, req.priority());
6064
6065 scoped_refptr<URLRequestRedirectJob> redirect_job =
6066 new URLRequestRedirectJob(
6067 &req, &default_network_delegate_, test_server_.GetURL("echo"),
[email protected]7983c4a2014-03-12 01:47:096068 URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason");
[email protected]90499482013-06-01 00:39:506069 AddTestInterceptor()->set_main_intercept_job(redirect_job.get());
[email protected]5033ab82013-03-22 20:17:466070
6071 req.SetPriority(LOW);
6072 req.Start();
6073 EXPECT_TRUE(req.is_pending());
6074
6075 scoped_refptr<URLRequestTestJob> job =
6076 new URLRequestTestJob(&req, &default_network_delegate_);
[email protected]90499482013-06-01 00:39:506077 AddTestInterceptor()->set_main_intercept_job(job.get());
[email protected]5033ab82013-03-22 20:17:466078
6079 // Should trigger |job| to be started.
[email protected]255620da2013-08-19 13:14:296080 base::RunLoop().Run();
[email protected]5033ab82013-03-22 20:17:466081 EXPECT_EQ(LOW, job->priority());
6082}
6083
[email protected]80abdad2014-03-15 00:20:546084// Check that creating a network request while entering/exiting suspend mode
6085// fails as it should. This is the only case where an HttpTransactionFactory
6086// does not return an HttpTransaction.
6087TEST_F(URLRequestTestHTTP, NetworkSuspendTest) {
6088 // Create a new HttpNetworkLayer that thinks it's suspended.
6089 HttpNetworkSession::Params params;
6090 params.host_resolver = default_context_.host_resolver();
6091 params.cert_verifier = default_context_.cert_verifier();
6092 params.transport_security_state = default_context_.transport_security_state();
6093 params.proxy_service = default_context_.proxy_service();
6094 params.ssl_config_service = default_context_.ssl_config_service();
6095 params.http_auth_handler_factory =
6096 default_context_.http_auth_handler_factory();
6097 params.network_delegate = &default_network_delegate_;
6098 params.http_server_properties = default_context_.http_server_properties();
6099 scoped_ptr<HttpNetworkLayer> network_layer(
6100 new HttpNetworkLayer(new HttpNetworkSession(params)));
6101 network_layer->OnSuspend();
6102
6103 HttpCache http_cache(network_layer.release(), default_context_.net_log(),
6104 HttpCache::DefaultBackend::InMemory(0));
6105
6106 TestURLRequestContext context(true);
6107 context.set_http_transaction_factory(&http_cache);
6108 context.Init();
6109
6110 TestDelegate d;
6111 URLRequest req(GURL("http://127.0.0.1/"),
6112 DEFAULT_PRIORITY,
6113 &d,
6114 &context);
6115 req.Start();
6116 base::RunLoop().Run();
6117
6118 EXPECT_TRUE(d.request_failed());
6119 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
6120 EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, req.status().error());
6121}
6122
6123// Check that creating a network request while entering/exiting suspend mode
6124// fails as it should in the case there is no cache. This is the only case
6125// where an HttpTransactionFactory does not return an HttpTransaction.
6126TEST_F(URLRequestTestHTTP, NetworkSuspendTestNoCache) {
6127 // Create a new HttpNetworkLayer that thinks it's suspended.
6128 HttpNetworkSession::Params params;
6129 params.host_resolver = default_context_.host_resolver();
6130 params.cert_verifier = default_context_.cert_verifier();
6131 params.transport_security_state = default_context_.transport_security_state();
6132 params.proxy_service = default_context_.proxy_service();
6133 params.ssl_config_service = default_context_.ssl_config_service();
6134 params.http_auth_handler_factory =
6135 default_context_.http_auth_handler_factory();
6136 params.network_delegate = &default_network_delegate_;
6137 params.http_server_properties = default_context_.http_server_properties();
6138 HttpNetworkLayer network_layer(new HttpNetworkSession(params));
6139 network_layer.OnSuspend();
6140
6141 TestURLRequestContext context(true);
6142 context.set_http_transaction_factory(&network_layer);
6143 context.Init();
6144
6145 TestDelegate d;
6146 URLRequest req(GURL("http://127.0.0.1/"),
6147 DEFAULT_PRIORITY,
6148 &d,
6149 &context);
6150 req.Start();
6151 base::RunLoop().Run();
6152
6153 EXPECT_TRUE(d.request_failed());
6154 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
6155 EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, req.status().error());
6156}
6157
[email protected]73e0bba2009-02-19 22:57:096158class HTTPSRequestTest : public testing::Test {
[email protected]87a09a92011-07-14 15:50:506159 public:
[email protected]ef2bf422012-05-11 03:27:096160 HTTPSRequestTest() : default_context_(true) {
6161 default_context_.set_network_delegate(&default_network_delegate_);
6162 default_context_.Init();
[email protected]87a09a92011-07-14 15:50:506163 }
6164 virtual ~HTTPSRequestTest() {}
6165
6166 protected:
[email protected]ceefd7fd2012-11-29 00:36:246167 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest.
[email protected]ef2bf422012-05-11 03:27:096168 TestURLRequestContext default_context_;
[email protected]ea224582008-12-07 20:25:466169};
6170
[email protected]c044616e2013-02-20 02:01:266171TEST_F(HTTPSRequestTest, HTTPSGetTest) {
[email protected]ce7d0cbc2013-05-03 18:57:226172 SpawnedTestServer test_server(
6173 SpawnedTestServer::TYPE_HTTPS,
6174 SpawnedTestServer::kLocalhost,
6175 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]95409e12010-08-17 20:07:116176 ASSERT_TRUE(test_server.Start());
[email protected]ea224582008-12-07 20:25:466177
[email protected]ea224582008-12-07 20:25:466178 TestDelegate d;
6179 {
[email protected]2ca01e52013-10-31 22:05:196180 URLRequest r(test_server.GetURL(std::string()),
6181 DEFAULT_PRIORITY,
6182 &d,
6183 &default_context_);
[email protected]ea224582008-12-07 20:25:466184 r.Start();
6185 EXPECT_TRUE(r.is_pending());
6186
[email protected]255620da2013-08-19 13:14:296187 base::RunLoop().Run();
[email protected]ea224582008-12-07 20:25:466188
6189 EXPECT_EQ(1, d.response_started_count());
6190 EXPECT_FALSE(d.received_data_before_response());
6191 EXPECT_NE(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:176192 CheckSSLInfo(r.ssl_info());
[email protected]6d81b482011-02-22 19:47:196193 EXPECT_EQ(test_server.host_port_pair().host(),
6194 r.GetSocketAddress().host());
6195 EXPECT_EQ(test_server.host_port_pair().port(),
6196 r.GetSocketAddress().port());
[email protected]ea224582008-12-07 20:25:466197 }
[email protected]ea224582008-12-07 20:25:466198}
6199
[email protected]5774ada2010-07-15 06:30:546200TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) {
[email protected]ce7d0cbc2013-05-03 18:57:226201 SpawnedTestServer::SSLOptions ssl_options(
6202 SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME);
6203 SpawnedTestServer test_server(
6204 SpawnedTestServer::TYPE_HTTPS,
6205 ssl_options,
6206 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]95409e12010-08-17 20:07:116207 ASSERT_TRUE(test_server.Start());
[email protected]bacff652009-03-31 17:50:336208
6209 bool err_allowed = true;
6210 for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) {
6211 TestDelegate d;
6212 {
6213 d.set_allow_certificate_errors(err_allowed);
[email protected]2ca01e52013-10-31 22:05:196214 URLRequest r(test_server.GetURL(std::string()),
6215 DEFAULT_PRIORITY,
6216 &d,
6217 &default_context_);
[email protected]bacff652009-03-31 17:50:336218
6219 r.Start();
6220 EXPECT_TRUE(r.is_pending());
6221
[email protected]255620da2013-08-19 13:14:296222 base::RunLoop().Run();
[email protected]bacff652009-03-31 17:50:336223
6224 EXPECT_EQ(1, d.response_started_count());
6225 EXPECT_FALSE(d.received_data_before_response());
6226 EXPECT_TRUE(d.have_certificate_errors());
[email protected]96adadb2010-08-28 01:16:176227 if (err_allowed) {
[email protected]bacff652009-03-31 17:50:336228 EXPECT_NE(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:176229 CheckSSLInfo(r.ssl_info());
6230 } else {
[email protected]bacff652009-03-31 17:50:336231 EXPECT_EQ(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:176232 }
[email protected]bacff652009-03-31 17:50:336233 }
6234 }
6235}
6236
[email protected]5774ada2010-07-15 06:30:546237TEST_F(HTTPSRequestTest, HTTPSExpiredTest) {
[email protected]ce7d0cbc2013-05-03 18:57:226238 SpawnedTestServer::SSLOptions ssl_options(
6239 SpawnedTestServer::SSLOptions::CERT_EXPIRED);
6240 SpawnedTestServer test_server(
6241 SpawnedTestServer::TYPE_HTTPS,
6242 ssl_options,
6243 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]95409e12010-08-17 20:07:116244 ASSERT_TRUE(test_server.Start());
[email protected]bacff652009-03-31 17:50:336245
6246 // Iterate from false to true, just so that we do the opposite of the
6247 // previous test in order to increase test coverage.
6248 bool err_allowed = false;
6249 for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) {
6250 TestDelegate d;
6251 {
6252 d.set_allow_certificate_errors(err_allowed);
[email protected]2ca01e52013-10-31 22:05:196253 URLRequest r(test_server.GetURL(std::string()),
6254 DEFAULT_PRIORITY,
6255 &d,
6256 &default_context_);
[email protected]bacff652009-03-31 17:50:336257
6258 r.Start();
6259 EXPECT_TRUE(r.is_pending());
6260
[email protected]255620da2013-08-19 13:14:296261 base::RunLoop().Run();
[email protected]bacff652009-03-31 17:50:336262
6263 EXPECT_EQ(1, d.response_started_count());
6264 EXPECT_FALSE(d.received_data_before_response());
6265 EXPECT_TRUE(d.have_certificate_errors());
[email protected]96adadb2010-08-28 01:16:176266 if (err_allowed) {
[email protected]bacff652009-03-31 17:50:336267 EXPECT_NE(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:176268 CheckSSLInfo(r.ssl_info());
6269 } else {
[email protected]bacff652009-03-31 17:50:336270 EXPECT_EQ(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:176271 }
[email protected]bacff652009-03-31 17:50:336272 }
6273 }
6274}
[email protected]73e0bba2009-02-19 22:57:096275
[email protected]316c1e5e2012-09-12 15:17:446276// Tests TLSv1.1 -> TLSv1 fallback. Verifies that we don't fall back more
6277// than necessary.
6278TEST_F(HTTPSRequestTest, TLSv1Fallback) {
[email protected]316c1e5e2012-09-12 15:17:446279 // The OpenSSL library in use may not support TLS 1.1.
6280#if !defined(USE_OPENSSL)
[email protected]c091d3602014-03-24 02:32:486281 EXPECT_GT(kDefaultSSLVersionMax, SSL_PROTOCOL_VERSION_TLS1);
[email protected]316c1e5e2012-09-12 15:17:446282#endif
[email protected]c091d3602014-03-24 02:32:486283 if (kDefaultSSLVersionMax <= SSL_PROTOCOL_VERSION_TLS1)
[email protected]316c1e5e2012-09-12 15:17:446284 return;
6285
[email protected]ce7d0cbc2013-05-03 18:57:226286 SpawnedTestServer::SSLOptions ssl_options(
6287 SpawnedTestServer::SSLOptions::CERT_OK);
[email protected]316c1e5e2012-09-12 15:17:446288 ssl_options.tls_intolerant =
[email protected]ce7d0cbc2013-05-03 18:57:226289 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1;
6290 SpawnedTestServer test_server(
6291 SpawnedTestServer::TYPE_HTTPS,
6292 ssl_options,
6293 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:446294 ASSERT_TRUE(test_server.Start());
6295
6296 TestDelegate d;
6297 TestURLRequestContext context(true);
6298 context.Init();
6299 d.set_allow_certificate_errors(true);
[email protected]2ca01e52013-10-31 22:05:196300 URLRequest r(
6301 test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d, &context);
[email protected]316c1e5e2012-09-12 15:17:446302 r.Start();
6303
[email protected]255620da2013-08-19 13:14:296304 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446305
6306 EXPECT_EQ(1, d.response_started_count());
6307 EXPECT_NE(0, d.bytes_received());
6308 EXPECT_EQ(static_cast<int>(SSL_CONNECTION_VERSION_TLS1),
6309 SSLConnectionStatusToVersion(r.ssl_info().connection_status));
6310 EXPECT_TRUE(r.ssl_info().connection_status & SSL_CONNECTION_VERSION_FALLBACK);
6311}
6312
[email protected]974fbdfb2013-12-11 20:16:136313// Tests that we don't fallback with servers that implement TLS_FALLBACK_SCSV.
6314#if defined(USE_OPENSSL)
6315TEST_F(HTTPSRequestTest, DISABLED_FallbackSCSV) {
6316#else
6317TEST_F(HTTPSRequestTest, FallbackSCSV) {
6318#endif
6319 SpawnedTestServer::SSLOptions ssl_options(
6320 SpawnedTestServer::SSLOptions::CERT_OK);
6321 // Configure HTTPS server to be intolerant of TLS >= 1.0 in order to trigger
6322 // a version fallback.
6323 ssl_options.tls_intolerant =
6324 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL;
6325 // Have the server process TLS_FALLBACK_SCSV so that version fallback
6326 // connections are rejected.
6327 ssl_options.fallback_scsv_enabled = true;
6328
6329 SpawnedTestServer test_server(
6330 SpawnedTestServer::TYPE_HTTPS,
6331 ssl_options,
6332 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6333 ASSERT_TRUE(test_server.Start());
6334
6335 TestDelegate d;
6336 TestURLRequestContext context(true);
6337 context.Init();
6338 d.set_allow_certificate_errors(true);
6339 URLRequest r(
6340 test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d, &context);
6341 r.Start();
6342
6343 base::RunLoop().Run();
6344
6345 EXPECT_EQ(1, d.response_started_count());
6346 // ERR_SSL_VERSION_OR_CIPHER_MISMATCH is how the server simulates version
6347 // intolerance. If the fallback SCSV is processed when the original error
6348 // that caused the fallback should be returned, which should be
6349 // ERR_SSL_VERSION_OR_CIPHER_MISMATCH.
6350 EXPECT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, r.status().error());
6351}
6352
[email protected]316c1e5e2012-09-12 15:17:446353// This tests that a load of www.google.com with a certificate error sets
6354// the |certificate_errors_are_fatal| flag correctly. This flag will cause
6355// the interstitial to be fatal.
6356TEST_F(HTTPSRequestTest, HTTPSPreloadedHSTSTest) {
[email protected]ce7d0cbc2013-05-03 18:57:226357 SpawnedTestServer::SSLOptions ssl_options(
6358 SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME);
6359 SpawnedTestServer test_server(
6360 SpawnedTestServer::TYPE_HTTPS,
6361 ssl_options,
6362 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:446363 ASSERT_TRUE(test_server.Start());
6364
6365 // We require that the URL be www.google.com in order to pick up the
6366 // preloaded HSTS entries in the TransportSecurityState. This means that we
6367 // have to use a MockHostResolver in order to direct www.google.com to the
[email protected]ceefd7fd2012-11-29 00:36:246368 // testserver. By default, MockHostResolver maps all hosts to 127.0.0.1.
[email protected]316c1e5e2012-09-12 15:17:446369
6370 MockHostResolver host_resolver;
[email protected]ceefd7fd2012-11-29 00:36:246371 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]316c1e5e2012-09-12 15:17:446372 TestURLRequestContext context(true);
6373 context.set_network_delegate(&network_delegate);
6374 context.set_host_resolver(&host_resolver);
6375 TransportSecurityState transport_security_state;
6376 context.set_transport_security_state(&transport_security_state);
6377 context.Init();
6378
6379 TestDelegate d;
[email protected]7d3cbc92013-03-18 22:33:046380 URLRequest r(GURL(base::StringPrintf("https://www.google.com:%d",
6381 test_server.host_port_pair().port())),
[email protected]2ca01e52013-10-31 22:05:196382 DEFAULT_PRIORITY,
[email protected]316c1e5e2012-09-12 15:17:446383 &d,
6384 &context);
6385
6386 r.Start();
6387 EXPECT_TRUE(r.is_pending());
6388
[email protected]255620da2013-08-19 13:14:296389 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446390
6391 EXPECT_EQ(1, d.response_started_count());
6392 EXPECT_FALSE(d.received_data_before_response());
6393 EXPECT_TRUE(d.have_certificate_errors());
6394 EXPECT_TRUE(d.certificate_errors_are_fatal());
6395}
6396
6397// This tests that cached HTTPS page loads do not cause any updates to the
6398// TransportSecurityState.
6399TEST_F(HTTPSRequestTest, HTTPSErrorsNoClobberTSSTest) {
6400 // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't
6401 // matter. It just has to be any error.
[email protected]ce7d0cbc2013-05-03 18:57:226402 SpawnedTestServer::SSLOptions ssl_options(
6403 SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME);
6404 SpawnedTestServer test_server(
6405 SpawnedTestServer::TYPE_HTTPS,
6406 ssl_options,
6407 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:446408 ASSERT_TRUE(test_server.Start());
6409
6410 // We require that the URL be www.google.com in order to pick up the
6411 // preloaded and dynamic HSTS and public key pin entries in the
6412 // TransportSecurityState. This means that we have to use a
6413 // MockHostResolver in order to direct www.google.com to the testserver.
[email protected]ceefd7fd2012-11-29 00:36:246414 // By default, MockHostResolver maps all hosts to 127.0.0.1.
[email protected]316c1e5e2012-09-12 15:17:446415
6416 MockHostResolver host_resolver;
[email protected]ceefd7fd2012-11-29 00:36:246417 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]316c1e5e2012-09-12 15:17:446418 TestURLRequestContext context(true);
6419 context.set_network_delegate(&network_delegate);
6420 context.set_host_resolver(&host_resolver);
6421 TransportSecurityState transport_security_state;
6422 TransportSecurityState::DomainState domain_state;
[email protected]22e045f2013-09-20 03:54:036423 EXPECT_TRUE(transport_security_state.GetDomainState("www.google.com", true,
6424 &domain_state));
[email protected]316c1e5e2012-09-12 15:17:446425 context.set_transport_security_state(&transport_security_state);
6426 context.Init();
6427
6428 TestDelegate d;
[email protected]7d3cbc92013-03-18 22:33:046429 URLRequest r(GURL(base::StringPrintf("https://www.google.com:%d",
6430 test_server.host_port_pair().port())),
[email protected]2ca01e52013-10-31 22:05:196431 DEFAULT_PRIORITY,
[email protected]316c1e5e2012-09-12 15:17:446432 &d,
6433 &context);
6434
6435 r.Start();
6436 EXPECT_TRUE(r.is_pending());
6437
[email protected]255620da2013-08-19 13:14:296438 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446439
6440 EXPECT_EQ(1, d.response_started_count());
6441 EXPECT_FALSE(d.received_data_before_response());
6442 EXPECT_TRUE(d.have_certificate_errors());
6443 EXPECT_TRUE(d.certificate_errors_are_fatal());
6444
6445 // Get a fresh copy of the state, and check that it hasn't been updated.
6446 TransportSecurityState::DomainState new_domain_state;
[email protected]22e045f2013-09-20 03:54:036447 EXPECT_TRUE(transport_security_state.GetDomainState("www.google.com", true,
6448 &new_domain_state));
[email protected]316c1e5e2012-09-12 15:17:446449 EXPECT_EQ(new_domain_state.upgrade_mode, domain_state.upgrade_mode);
[email protected]a165f092013-06-12 16:10:056450 EXPECT_EQ(new_domain_state.sts_include_subdomains,
6451 domain_state.sts_include_subdomains);
6452 EXPECT_EQ(new_domain_state.pkp_include_subdomains,
6453 domain_state.pkp_include_subdomains);
[email protected]316c1e5e2012-09-12 15:17:446454 EXPECT_TRUE(FingerprintsEqual(new_domain_state.static_spki_hashes,
6455 domain_state.static_spki_hashes));
6456 EXPECT_TRUE(FingerprintsEqual(new_domain_state.dynamic_spki_hashes,
6457 domain_state.dynamic_spki_hashes));
6458 EXPECT_TRUE(FingerprintsEqual(new_domain_state.bad_static_spki_hashes,
6459 domain_state.bad_static_spki_hashes));
6460}
6461
[email protected]8ccc69f2012-11-28 19:52:146462// Make sure HSTS preserves a POST request's method and body.
6463TEST_F(HTTPSRequestTest, HSTSPreservesPosts) {
6464 static const char kData[] = "hello world";
6465
[email protected]ce7d0cbc2013-05-03 18:57:226466 SpawnedTestServer::SSLOptions ssl_options(
6467 SpawnedTestServer::SSLOptions::CERT_OK);
6468 SpawnedTestServer test_server(
6469 SpawnedTestServer::TYPE_HTTPS,
6470 ssl_options,
6471 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]8ccc69f2012-11-28 19:52:146472 ASSERT_TRUE(test_server.Start());
6473
6474
6475 // Per spec, TransportSecurityState expects a domain name, rather than an IP
6476 // address, so a MockHostResolver is needed to redirect www.somewhere.com to
[email protected]ce7d0cbc2013-05-03 18:57:226477 // the SpawnedTestServer. By default, MockHostResolver maps all hosts
6478 // to 127.0.0.1.
[email protected]8ccc69f2012-11-28 19:52:146479 MockHostResolver host_resolver;
[email protected]8ccc69f2012-11-28 19:52:146480
6481 // Force https for www.somewhere.com.
6482 TransportSecurityState transport_security_state;
[email protected]474f079e2013-03-02 19:11:206483 base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000);
6484 bool include_subdomains = false;
6485 transport_security_state.AddHSTS("www.somewhere.com", expiry,
6486 include_subdomains);
[email protected]8ccc69f2012-11-28 19:52:146487
6488 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
6489
6490 TestURLRequestContext context(true);
6491 context.set_host_resolver(&host_resolver);
6492 context.set_transport_security_state(&transport_security_state);
6493 context.set_network_delegate(&network_delegate);
6494 context.Init();
6495
6496 TestDelegate d;
6497 // Navigating to https://www.somewhere.com instead of https://127.0.0.1 will
6498 // cause a certificate error. Ignore the error.
6499 d.set_allow_certificate_errors(true);
6500
[email protected]7d3cbc92013-03-18 22:33:046501 URLRequest req(GURL(base::StringPrintf("http://www.somewhere.com:%d/echo",
6502 test_server.host_port_pair().port())),
[email protected]2ca01e52013-10-31 22:05:196503 DEFAULT_PRIORITY,
[email protected]8ccc69f2012-11-28 19:52:146504 &d,
6505 &context);
6506 req.set_method("POST");
[email protected]f288ef02012-12-15 20:28:286507 req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
[email protected]8ccc69f2012-11-28 19:52:146508
6509 req.Start();
[email protected]255620da2013-08-19 13:14:296510 base::RunLoop().Run();
[email protected]8ccc69f2012-11-28 19:52:146511
6512 EXPECT_EQ("https", req.url().scheme());
6513 EXPECT_EQ("POST", req.method());
6514 EXPECT_EQ(kData, d.data_received());
[email protected]07d9bfd2013-06-27 14:16:406515
6516 LoadTimingInfo load_timing_info;
6517 network_delegate.GetLoadTimingInfoBeforeRedirect(&load_timing_info);
6518 // LoadTimingInfo of HSTS redirects is similar to that of network cache hits
6519 TestLoadTimingCacheHitNoNetwork(load_timing_info);
[email protected]8ccc69f2012-11-28 19:52:146520}
6521
[email protected]316c1e5e2012-09-12 15:17:446522TEST_F(HTTPSRequestTest, SSLv3Fallback) {
[email protected]ce7d0cbc2013-05-03 18:57:226523 SpawnedTestServer::SSLOptions ssl_options(
6524 SpawnedTestServer::SSLOptions::CERT_OK);
6525 ssl_options.tls_intolerant =
6526 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL;
6527 SpawnedTestServer test_server(
6528 SpawnedTestServer::TYPE_HTTPS,
6529 ssl_options,
6530 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:446531 ASSERT_TRUE(test_server.Start());
6532
6533 TestDelegate d;
6534 TestURLRequestContext context(true);
6535 context.Init();
6536 d.set_allow_certificate_errors(true);
[email protected]2ca01e52013-10-31 22:05:196537 URLRequest r(
6538 test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d, &context);
[email protected]316c1e5e2012-09-12 15:17:446539 r.Start();
6540
[email protected]255620da2013-08-19 13:14:296541 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446542
6543 EXPECT_EQ(1, d.response_started_count());
6544 EXPECT_NE(0, d.bytes_received());
6545 EXPECT_EQ(static_cast<int>(SSL_CONNECTION_VERSION_SSL3),
6546 SSLConnectionStatusToVersion(r.ssl_info().connection_status));
6547 EXPECT_TRUE(r.ssl_info().connection_status & SSL_CONNECTION_VERSION_FALLBACK);
6548}
6549
6550namespace {
6551
6552class SSLClientAuthTestDelegate : public TestDelegate {
6553 public:
6554 SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) {
6555 }
6556 virtual void OnCertificateRequested(
6557 URLRequest* request,
[email protected]46fadfd2013-02-06 09:40:166558 SSLCertRequestInfo* cert_request_info) OVERRIDE {
[email protected]316c1e5e2012-09-12 15:17:446559 on_certificate_requested_count_++;
[email protected]2da659e2013-05-23 20:51:346560 base::MessageLoop::current()->Quit();
[email protected]316c1e5e2012-09-12 15:17:446561 }
6562 int on_certificate_requested_count() {
6563 return on_certificate_requested_count_;
6564 }
6565 private:
6566 int on_certificate_requested_count_;
6567};
6568
6569} // namespace
6570
6571// TODO(davidben): Test the rest of the code. Specifically,
6572// - Filtering which certificates to select.
6573// - Sending a certificate back.
6574// - Getting a certificate request in an SSL renegotiation sending the
6575// HTTP request.
6576TEST_F(HTTPSRequestTest, ClientAuthTest) {
[email protected]ce7d0cbc2013-05-03 18:57:226577 SpawnedTestServer::SSLOptions ssl_options;
[email protected]316c1e5e2012-09-12 15:17:446578 ssl_options.request_client_certificate = true;
[email protected]ce7d0cbc2013-05-03 18:57:226579 SpawnedTestServer test_server(
6580 SpawnedTestServer::TYPE_HTTPS,
6581 ssl_options,
6582 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:446583 ASSERT_TRUE(test_server.Start());
6584
6585 SSLClientAuthTestDelegate d;
6586 {
[email protected]2ca01e52013-10-31 22:05:196587 URLRequest r(test_server.GetURL(std::string()),
6588 DEFAULT_PRIORITY,
6589 &d,
6590 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:446591
6592 r.Start();
6593 EXPECT_TRUE(r.is_pending());
6594
[email protected]255620da2013-08-19 13:14:296595 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446596
6597 EXPECT_EQ(1, d.on_certificate_requested_count());
6598 EXPECT_FALSE(d.received_data_before_response());
6599 EXPECT_EQ(0, d.bytes_received());
6600
6601 // Send no certificate.
6602 // TODO(davidben): Get temporary client cert import (with keys) working on
6603 // all platforms so we can test sending a cert as well.
6604 r.ContinueWithCertificate(NULL);
6605
[email protected]255620da2013-08-19 13:14:296606 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446607
6608 EXPECT_EQ(1, d.response_started_count());
6609 EXPECT_FALSE(d.received_data_before_response());
6610 EXPECT_NE(0, d.bytes_received());
6611 }
6612}
6613
6614TEST_F(HTTPSRequestTest, ResumeTest) {
6615 // Test that we attempt a session resume when making two connections to the
6616 // same host.
[email protected]ce7d0cbc2013-05-03 18:57:226617 SpawnedTestServer::SSLOptions ssl_options;
[email protected]316c1e5e2012-09-12 15:17:446618 ssl_options.record_resume = true;
[email protected]ce7d0cbc2013-05-03 18:57:226619 SpawnedTestServer test_server(
6620 SpawnedTestServer::TYPE_HTTPS,
6621 ssl_options,
6622 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:446623 ASSERT_TRUE(test_server.Start());
6624
6625 SSLClientSocket::ClearSessionCache();
6626
6627 {
6628 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:196629 URLRequest r(test_server.GetURL("ssl-session-cache"),
6630 DEFAULT_PRIORITY,
6631 &d,
6632 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:446633
6634 r.Start();
6635 EXPECT_TRUE(r.is_pending());
6636
[email protected]255620da2013-08-19 13:14:296637 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446638
6639 EXPECT_EQ(1, d.response_started_count());
6640 }
6641
6642 reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())->
6643 CloseAllConnections();
6644
6645 {
6646 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:196647 URLRequest r(test_server.GetURL("ssl-session-cache"),
6648 DEFAULT_PRIORITY,
6649 &d,
6650 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:446651
6652 r.Start();
6653 EXPECT_TRUE(r.is_pending());
6654
[email protected]255620da2013-08-19 13:14:296655 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446656
6657 // The response will look like;
6658 // insert abc
6659 // lookup abc
6660 // insert xyz
6661 //
6662 // With a newline at the end which makes the split think that there are
6663 // four lines.
6664
6665 EXPECT_EQ(1, d.response_started_count());
6666 std::vector<std::string> lines;
6667 base::SplitString(d.data_received(), '\n', &lines);
6668 ASSERT_EQ(4u, lines.size()) << d.data_received();
6669
6670 std::string session_id;
6671
6672 for (size_t i = 0; i < 2; i++) {
6673 std::vector<std::string> parts;
6674 base::SplitString(lines[i], '\t', &parts);
6675 ASSERT_EQ(2u, parts.size());
6676 if (i == 0) {
6677 EXPECT_EQ("insert", parts[0]);
6678 session_id = parts[1];
6679 } else {
6680 EXPECT_EQ("lookup", parts[0]);
6681 EXPECT_EQ(session_id, parts[1]);
6682 }
6683 }
6684 }
6685}
6686
6687TEST_F(HTTPSRequestTest, SSLSessionCacheShardTest) {
6688 // Test that sessions aren't resumed when the value of ssl_session_cache_shard
6689 // differs.
[email protected]ce7d0cbc2013-05-03 18:57:226690 SpawnedTestServer::SSLOptions ssl_options;
[email protected]316c1e5e2012-09-12 15:17:446691 ssl_options.record_resume = true;
[email protected]ce7d0cbc2013-05-03 18:57:226692 SpawnedTestServer test_server(
6693 SpawnedTestServer::TYPE_HTTPS,
6694 ssl_options,
6695 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:446696 ASSERT_TRUE(test_server.Start());
6697
6698 SSLClientSocket::ClearSessionCache();
6699
6700 {
6701 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:196702 URLRequest r(test_server.GetURL("ssl-session-cache"),
6703 DEFAULT_PRIORITY,
6704 &d,
6705 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:446706
6707 r.Start();
6708 EXPECT_TRUE(r.is_pending());
6709
[email protected]255620da2013-08-19 13:14:296710 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446711
6712 EXPECT_EQ(1, d.response_started_count());
6713 }
6714
6715 // Now create a new HttpCache with a different ssl_session_cache_shard value.
6716 HttpNetworkSession::Params params;
6717 params.host_resolver = default_context_.host_resolver();
6718 params.cert_verifier = default_context_.cert_verifier();
[email protected]b1c988b2013-06-13 06:48:116719 params.transport_security_state = default_context_.transport_security_state();
[email protected]316c1e5e2012-09-12 15:17:446720 params.proxy_service = default_context_.proxy_service();
6721 params.ssl_config_service = default_context_.ssl_config_service();
6722 params.http_auth_handler_factory =
6723 default_context_.http_auth_handler_factory();
[email protected]cc05edc2013-03-08 18:04:416724 params.network_delegate = &default_network_delegate_;
[email protected]316c1e5e2012-09-12 15:17:446725 params.http_server_properties = default_context_.http_server_properties();
6726 params.ssl_session_cache_shard = "alternate";
6727
6728 scoped_ptr<net::HttpCache> cache(new net::HttpCache(
6729 new net::HttpNetworkSession(params),
6730 net::HttpCache::DefaultBackend::InMemory(0)));
6731
6732 default_context_.set_http_transaction_factory(cache.get());
6733
6734 {
6735 TestDelegate d;
[email protected]2ca01e52013-10-31 22:05:196736 URLRequest r(test_server.GetURL("ssl-session-cache"),
6737 DEFAULT_PRIORITY,
6738 &d,
6739 &default_context_);
[email protected]316c1e5e2012-09-12 15:17:446740
6741 r.Start();
6742 EXPECT_TRUE(r.is_pending());
6743
[email protected]255620da2013-08-19 13:14:296744 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446745
6746 // The response will look like;
6747 // insert abc
6748 // insert xyz
6749 //
6750 // With a newline at the end which makes the split think that there are
6751 // three lines.
6752
6753 EXPECT_EQ(1, d.response_started_count());
6754 std::vector<std::string> lines;
6755 base::SplitString(d.data_received(), '\n', &lines);
6756 ASSERT_EQ(3u, lines.size());
6757
6758 std::string session_id;
6759 for (size_t i = 0; i < 2; i++) {
6760 std::vector<std::string> parts;
6761 base::SplitString(lines[i], '\t', &parts);
6762 ASSERT_EQ(2u, parts.size());
6763 EXPECT_EQ("insert", parts[0]);
6764 if (i == 0) {
6765 session_id = parts[1];
6766 } else {
6767 EXPECT_NE(session_id, parts[1]);
6768 }
6769 }
6770 }
6771}
6772
[email protected]a8fed1742013-12-27 02:14:246773class HTTPSSessionTest : public testing::Test {
6774 public:
6775 HTTPSSessionTest() : default_context_(true) {
6776 cert_verifier_.set_default_result(net::OK);
6777
6778 default_context_.set_network_delegate(&default_network_delegate_);
6779 default_context_.set_cert_verifier(&cert_verifier_);
6780 default_context_.Init();
6781 }
6782 virtual ~HTTPSSessionTest() {}
6783
6784 protected:
6785 MockCertVerifier cert_verifier_;
6786 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest.
6787 TestURLRequestContext default_context_;
6788};
6789
6790// Tests that session resumption is not attempted if an invalid certificate
6791// is presented.
6792TEST_F(HTTPSSessionTest, DontResumeSessionsForInvalidCertificates) {
6793 SpawnedTestServer::SSLOptions ssl_options;
6794 ssl_options.record_resume = true;
6795 SpawnedTestServer test_server(
6796 SpawnedTestServer::TYPE_HTTPS,
6797 ssl_options,
6798 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6799 ASSERT_TRUE(test_server.Start());
6800
6801 SSLClientSocket::ClearSessionCache();
6802
6803 // Simulate the certificate being expired and attempt a connection.
6804 cert_verifier_.set_default_result(net::ERR_CERT_DATE_INVALID);
6805 {
6806 TestDelegate d;
6807 URLRequest r(test_server.GetURL("ssl-session-cache"),
6808 DEFAULT_PRIORITY,
6809 &d,
6810 &default_context_);
6811
6812 r.Start();
6813 EXPECT_TRUE(r.is_pending());
6814
6815 base::RunLoop().Run();
6816
6817 EXPECT_EQ(1, d.response_started_count());
6818 }
6819
6820 reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())->
6821 CloseAllConnections();
6822
6823 // Now change the certificate to be acceptable (so that the response is
6824 // loaded), and ensure that no session id is presented to the peer.
6825 cert_verifier_.set_default_result(net::OK);
6826 {
6827 TestDelegate d;
6828 URLRequest r(test_server.GetURL("ssl-session-cache"),
6829 DEFAULT_PRIORITY,
6830 &d,
6831 &default_context_);
6832
6833 r.Start();
6834 EXPECT_TRUE(r.is_pending());
6835
6836 base::RunLoop().Run();
6837
6838 // The response will look like;
6839 // insert abc
6840 // insert xyz
6841 //
6842 // With a newline at the end which makes the split think that there are
6843 // three lines.
6844 //
6845 // If a session was presented (eg: a bug), then the response would look
6846 // like;
6847 // insert abc
6848 // lookup abc
6849 // insert xyz
6850
6851 EXPECT_EQ(1, d.response_started_count());
6852 std::vector<std::string> lines;
6853 base::SplitString(d.data_received(), '\n', &lines);
6854 ASSERT_EQ(3u, lines.size()) << d.data_received();
6855
6856 std::string session_id;
6857 for (size_t i = 0; i < 2; i++) {
6858 std::vector<std::string> parts;
6859 base::SplitString(lines[i], '\t', &parts);
6860 ASSERT_EQ(2u, parts.size());
6861 EXPECT_EQ("insert", parts[0]);
6862 if (i == 0) {
6863 session_id = parts[1];
6864 } else {
6865 EXPECT_NE(session_id, parts[1]);
6866 }
6867 }
6868 }
6869}
6870
[email protected]a13234c2012-03-20 21:45:026871class TestSSLConfigService : public SSLConfigService {
[email protected]dffe8242012-03-20 15:14:276872 public:
[email protected]3a86a712013-07-30 07:16:206873 TestSSLConfigService(bool ev_enabled,
6874 bool online_rev_checking,
6875 bool rev_checking_required_local_anchors)
[email protected]a13234c2012-03-20 21:45:026876 : ev_enabled_(ev_enabled),
[email protected]3a86a712013-07-30 07:16:206877 online_rev_checking_(online_rev_checking),
6878 rev_checking_required_local_anchors_(
6879 rev_checking_required_local_anchors) {}
[email protected]a13234c2012-03-20 21:45:026880
[email protected]a9813302012-04-28 09:29:286881 // SSLConfigService:
6882 virtual void GetSSLConfig(SSLConfig* config) OVERRIDE {
[email protected]dffe8242012-03-20 15:14:276883 *config = SSLConfig();
[email protected]a13234c2012-03-20 21:45:026884 config->rev_checking_enabled = online_rev_checking_;
6885 config->verify_ev_cert = ev_enabled_;
[email protected]3a86a712013-07-30 07:16:206886 config->rev_checking_required_local_anchors =
6887 rev_checking_required_local_anchors_;
[email protected]dffe8242012-03-20 15:14:276888 }
[email protected]a13234c2012-03-20 21:45:026889
[email protected]a9813302012-04-28 09:29:286890 protected:
6891 virtual ~TestSSLConfigService() {}
6892
[email protected]a13234c2012-03-20 21:45:026893 private:
6894 const bool ev_enabled_;
6895 const bool online_rev_checking_;
[email protected]3a86a712013-07-30 07:16:206896 const bool rev_checking_required_local_anchors_;
[email protected]dffe8242012-03-20 15:14:276897};
6898
6899// This the fingerprint of the "Testing CA" certificate used by the testserver.
6900// See net/data/ssl/certificates/ocsp-test-root.pem.
[email protected]ede03212012-09-07 12:52:266901static const SHA1HashValue kOCSPTestCertFingerprint =
[email protected]dffe8242012-03-20 15:14:276902 { { 0xf1, 0xad, 0xf6, 0xce, 0x42, 0xac, 0xe7, 0xb4, 0xf4, 0x24,
6903 0xdb, 0x1a, 0xf7, 0xa0, 0x9f, 0x09, 0xa1, 0xea, 0xf1, 0x5c } };
6904
[email protected]51523f52013-07-31 21:57:286905// This is the SHA256, SPKI hash of the "Testing CA" certificate used by the
6906// testserver.
6907static const SHA256HashValue kOCSPTestCertSPKI = { {
6908 0xee, 0xe6, 0x51, 0x2d, 0x4c, 0xfa, 0xf7, 0x3e,
6909 0x6c, 0xd8, 0xca, 0x67, 0xed, 0xb5, 0x5d, 0x49,
6910 0x76, 0xe1, 0x52, 0xa7, 0x6e, 0x0e, 0xa0, 0x74,
6911 0x09, 0x75, 0xe6, 0x23, 0x24, 0xbd, 0x1b, 0x28,
6912} };
6913
[email protected]dffe8242012-03-20 15:14:276914// This is the policy OID contained in the certificates that testserver
6915// generates.
6916static const char kOCSPTestCertPolicy[] = "1.3.6.1.4.1.11129.2.4.1";
6917
6918class HTTPSOCSPTest : public HTTPSRequestTest {
6919 public:
6920 HTTPSOCSPTest()
[email protected]ef2bf422012-05-11 03:27:096921 : context_(true),
[email protected]b6f2de32012-08-17 04:35:086922 ev_test_policy_(
6923 new ScopedTestEVPolicy(EVRootCAMetadata::GetInstance(),
6924 kOCSPTestCertFingerprint,
6925 kOCSPTestCertPolicy)) {
[email protected]a13234c2012-03-20 21:45:026926 }
6927
6928 virtual void SetUp() OVERRIDE {
[email protected]ef2bf422012-05-11 03:27:096929 SetupContext(&context_);
6930 context_.Init();
[email protected]dffe8242012-03-20 15:14:276931
6932 scoped_refptr<net::X509Certificate> root_cert =
[email protected]3a86a712013-07-30 07:16:206933 ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem");
[email protected]dffe8242012-03-20 15:14:276934 CHECK_NE(static_cast<X509Certificate*>(NULL), root_cert);
[email protected]90499482013-06-01 00:39:506935 test_root_.reset(new ScopedTestRoot(root_cert.get()));
[email protected]dffe8242012-03-20 15:14:276936
[email protected]a592c0432012-12-01 18:10:296937#if defined(USE_NSS) || defined(OS_IOS)
[email protected]ef2bf422012-05-11 03:27:096938 SetURLRequestContextForNSSHttpIO(&context_);
[email protected]dffe8242012-03-20 15:14:276939 EnsureNSSHttpIOInit();
6940#endif
6941 }
6942
[email protected]ce7d0cbc2013-05-03 18:57:226943 void DoConnection(const SpawnedTestServer::SSLOptions& ssl_options,
[email protected]dffe8242012-03-20 15:14:276944 CertStatus* out_cert_status) {
[email protected]924e9f92012-12-16 22:00:536945 // We always overwrite out_cert_status.
6946 *out_cert_status = 0;
[email protected]ce7d0cbc2013-05-03 18:57:226947 SpawnedTestServer test_server(
6948 SpawnedTestServer::TYPE_HTTPS,
6949 ssl_options,
6950 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]dffe8242012-03-20 15:14:276951 ASSERT_TRUE(test_server.Start());
6952
6953 TestDelegate d;
6954 d.set_allow_certificate_errors(true);
[email protected]2ca01e52013-10-31 22:05:196955 URLRequest r(
6956 test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d, &context_);
[email protected]dffe8242012-03-20 15:14:276957 r.Start();
6958
[email protected]255620da2013-08-19 13:14:296959 base::RunLoop().Run();
[email protected]dffe8242012-03-20 15:14:276960
6961 EXPECT_EQ(1, d.response_started_count());
6962 *out_cert_status = r.ssl_info().cert_status;
6963 }
6964
[email protected]46fadfd2013-02-06 09:40:166965 virtual ~HTTPSOCSPTest() {
[email protected]a592c0432012-12-01 18:10:296966#if defined(USE_NSS) || defined(OS_IOS)
[email protected]dffe8242012-03-20 15:14:276967 ShutdownNSSHttpIO();
6968#endif
6969 }
6970
[email protected]a13234c2012-03-20 21:45:026971 protected:
6972 // SetupContext configures the URLRequestContext that will be used for making
6973 // connetions to testserver. This can be overridden in test subclasses for
6974 // different behaviour.
6975 virtual void SetupContext(URLRequestContext* context) {
6976 context->set_ssl_config_service(
6977 new TestSSLConfigService(true /* check for EV */,
[email protected]3a86a712013-07-30 07:16:206978 true /* online revocation checking */,
6979 false /* require rev. checking for local
6980 anchors */));
[email protected]a13234c2012-03-20 21:45:026981 }
6982
[email protected]dffe8242012-03-20 15:14:276983 scoped_ptr<ScopedTestRoot> test_root_;
[email protected]ef2bf422012-05-11 03:27:096984 TestURLRequestContext context_;
[email protected]b6f2de32012-08-17 04:35:086985 scoped_ptr<ScopedTestEVPolicy> ev_test_policy_;
[email protected]dffe8242012-03-20 15:14:276986};
6987
[email protected]a13234c2012-03-20 21:45:026988static CertStatus ExpectedCertStatusForFailedOnlineRevocationCheck() {
[email protected]05454a432012-03-20 20:04:016989#if defined(OS_WIN)
[email protected]a13234c2012-03-20 21:45:026990 // Windows can return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION but we don't
6991 // have that ability on other platforms.
6992 return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION;
6993#else
6994 return 0;
6995#endif
6996}
6997
[email protected]3a86a712013-07-30 07:16:206998// SystemSupportsHardFailRevocationChecking returns true iff the current
6999// operating system supports revocation checking and can distinguish between
7000// situations where a given certificate lacks any revocation information (eg:
7001// no CRLDistributionPoints and no OCSP Responder AuthorityInfoAccess) and when
7002// revocation information cannot be obtained (eg: the CRL was unreachable).
7003// If it does not, then tests which rely on 'hard fail' behaviour should be
7004// skipped.
7005static bool SystemSupportsHardFailRevocationChecking() {
7006#if defined(OS_WIN) || defined(USE_NSS) || defined(OS_IOS)
7007 return true;
7008#else
7009 return false;
7010#endif
7011}
7012
[email protected]a13234c2012-03-20 21:45:027013// SystemUsesChromiumEVMetadata returns true iff the current operating system
7014// uses Chromium's EV metadata (i.e. EVRootCAMetadata). If it does not, then
7015// several tests are effected because our testing EV certificate won't be
7016// recognised as EV.
7017static bool SystemUsesChromiumEVMetadata() {
[email protected]5c504192012-03-27 19:00:577018#if defined(USE_OPENSSL)
7019 // http://crbug.com/117478 - OpenSSL does not support EV validation.
7020 return false;
[email protected]a592c0432012-12-01 18:10:297021#elif defined(OS_MACOSX) && !defined(OS_IOS)
[email protected]a13234c2012-03-20 21:45:027022 // On OS X, we use the system to tell us whether a certificate is EV or not
7023 // and the system won't recognise our testing root.
[email protected]05454a432012-03-20 20:04:017024 return false;
7025#else
7026 return true;
7027#endif
7028}
7029
[email protected]b6f2de32012-08-17 04:35:087030static bool SystemSupportsOCSP() {
[email protected]5c504192012-03-27 19:00:577031#if defined(USE_OPENSSL)
7032 // http://crbug.com/117478 - OpenSSL does not support OCSP.
7033 return false;
7034#elif defined(OS_WIN)
[email protected]a13234c2012-03-20 21:45:027035 return base::win::GetVersion() >= base::win::VERSION_VISTA;
7036#elif defined(OS_ANDROID)
7037 // TODO(jnd): http://crbug.com/117478 - EV verification is not yet supported.
7038 return false;
7039#else
7040 return true;
7041#endif
7042}
7043
[email protected]dffe8242012-03-20 15:14:277044TEST_F(HTTPSOCSPTest, Valid) {
7045 if (!SystemSupportsOCSP()) {
7046 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7047 return;
7048 }
7049
[email protected]ce7d0cbc2013-05-03 18:57:227050 SpawnedTestServer::SSLOptions ssl_options(
7051 SpawnedTestServer::SSLOptions::CERT_AUTO);
7052 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK;
[email protected]dffe8242012-03-20 15:14:277053
[email protected]924e9f92012-12-16 22:00:537054 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:127055 DoConnection(ssl_options, &cert_status);
[email protected]dffe8242012-03-20 15:14:277056
7057 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
7058
[email protected]a13234c2012-03-20 21:45:027059 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7060 static_cast<bool>(cert_status & CERT_STATUS_IS_EV));
[email protected]dffe8242012-03-20 15:14:277061
7062 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
7063}
7064
7065TEST_F(HTTPSOCSPTest, Revoked) {
7066 if (!SystemSupportsOCSP()) {
7067 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7068 return;
7069 }
7070
[email protected]ce7d0cbc2013-05-03 18:57:227071 SpawnedTestServer::SSLOptions ssl_options(
7072 SpawnedTestServer::SSLOptions::CERT_AUTO);
7073 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED;
[email protected]dffe8242012-03-20 15:14:277074
7075 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:127076 DoConnection(ssl_options, &cert_status);
[email protected]dffe8242012-03-20 15:14:277077
[email protected]a592c0432012-12-01 18:10:297078#if !(defined(OS_MACOSX) && !defined(OS_IOS))
[email protected]dffe8242012-03-20 15:14:277079 // Doesn't pass on OS X yet for reasons that need to be investigated.
7080 EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS);
7081#endif
7082 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7083 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
7084}
7085
7086TEST_F(HTTPSOCSPTest, Invalid) {
7087 if (!SystemSupportsOCSP()) {
7088 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7089 return;
7090 }
7091
[email protected]ce7d0cbc2013-05-03 18:57:227092 SpawnedTestServer::SSLOptions ssl_options(
7093 SpawnedTestServer::SSLOptions::CERT_AUTO);
7094 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
[email protected]dffe8242012-03-20 15:14:277095
[email protected]924e9f92012-12-16 22:00:537096 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:127097 DoConnection(ssl_options, &cert_status);
[email protected]dffe8242012-03-20 15:14:277098
[email protected]a13234c2012-03-20 21:45:027099 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
[email protected]dffe8242012-03-20 15:14:277100 cert_status & CERT_STATUS_ALL_ERRORS);
[email protected]dffe8242012-03-20 15:14:277101
7102 // Without a positive OCSP response, we shouldn't show the EV status.
7103 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7104 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
7105}
[email protected]a13234c2012-03-20 21:45:027106
[email protected]3a86a712013-07-30 07:16:207107class HTTPSHardFailTest : public HTTPSOCSPTest {
7108 protected:
7109 virtual void SetupContext(URLRequestContext* context) OVERRIDE {
7110 context->set_ssl_config_service(
7111 new TestSSLConfigService(false /* check for EV */,
7112 false /* online revocation checking */,
7113 true /* require rev. checking for local
7114 anchors */));
7115 }
7116};
7117
7118
7119TEST_F(HTTPSHardFailTest, FailsOnOCSPInvalid) {
7120 if (!SystemSupportsOCSP()) {
7121 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7122 return;
7123 }
7124
7125 if (!SystemSupportsHardFailRevocationChecking()) {
7126 LOG(WARNING) << "Skipping test because system doesn't support hard fail "
7127 << "revocation checking";
7128 return;
7129 }
7130
7131 SpawnedTestServer::SSLOptions ssl_options(
7132 SpawnedTestServer::SSLOptions::CERT_AUTO);
7133 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
7134
7135 CertStatus cert_status;
7136 DoConnection(ssl_options, &cert_status);
7137
7138 EXPECT_EQ(CERT_STATUS_REVOKED,
7139 cert_status & CERT_STATUS_REVOKED);
7140
7141 // Without a positive OCSP response, we shouldn't show the EV status.
7142 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
7143}
7144
[email protected]a13234c2012-03-20 21:45:027145class HTTPSEVCRLSetTest : public HTTPSOCSPTest {
7146 protected:
7147 virtual void SetupContext(URLRequestContext* context) OVERRIDE {
7148 context->set_ssl_config_service(
7149 new TestSSLConfigService(true /* check for EV */,
[email protected]3a86a712013-07-30 07:16:207150 false /* online revocation checking */,
7151 false /* require rev. checking for local
7152 anchors */));
[email protected]a13234c2012-03-20 21:45:027153 }
7154};
7155
7156TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndInvalidOCSP) {
7157 if (!SystemSupportsOCSP()) {
7158 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7159 return;
7160 }
7161
[email protected]ce7d0cbc2013-05-03 18:57:227162 SpawnedTestServer::SSLOptions ssl_options(
7163 SpawnedTestServer::SSLOptions::CERT_AUTO);
7164 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
[email protected]a13234c2012-03-20 21:45:027165 SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>());
7166
[email protected]924e9f92012-12-16 22:00:537167 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:127168 DoConnection(ssl_options, &cert_status);
[email protected]a13234c2012-03-20 21:45:027169
7170 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
7171 cert_status & CERT_STATUS_ALL_ERRORS);
7172
7173 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
[email protected]b6f2de32012-08-17 04:35:087174 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7175 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
[email protected]a13234c2012-03-20 21:45:027176}
7177
[email protected]be0fff62013-08-29 23:37:487178TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndRevokedOCSP) {
7179 if (!SystemSupportsOCSP()) {
7180 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7181 return;
7182 }
7183
7184 SpawnedTestServer::SSLOptions ssl_options(
7185 SpawnedTestServer::SSLOptions::CERT_AUTO);
7186 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED;
7187 SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>());
7188
7189 CertStatus cert_status;
7190 DoConnection(ssl_options, &cert_status);
7191
7192 // Currently only works for Windows. When using NSS or OS X, it's not
7193 // possible to determine whether the check failed because of actual
7194 // revocation or because there was an OCSP failure.
7195#if defined(OS_WIN)
7196 EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS);
7197#else
7198 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
7199#endif
7200
7201 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7202 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7203 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
7204}
7205
[email protected]a13234c2012-03-20 21:45:027206TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndGoodOCSP) {
7207 if (!SystemSupportsOCSP()) {
7208 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7209 return;
7210 }
7211
[email protected]ce7d0cbc2013-05-03 18:57:227212 SpawnedTestServer::SSLOptions ssl_options(
7213 SpawnedTestServer::SSLOptions::CERT_AUTO);
7214 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK;
[email protected]a13234c2012-03-20 21:45:027215 SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>());
7216
7217 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:127218 DoConnection(ssl_options, &cert_status);
[email protected]a13234c2012-03-20 21:45:027219
7220 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
7221
7222 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7223 static_cast<bool>(cert_status & CERT_STATUS_IS_EV));
[email protected]b6f2de32012-08-17 04:35:087224 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7225 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
[email protected]a13234c2012-03-20 21:45:027226}
7227
7228TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSet) {
7229 if (!SystemSupportsOCSP()) {
7230 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7231 return;
7232 }
7233
[email protected]ce7d0cbc2013-05-03 18:57:227234 SpawnedTestServer::SSLOptions ssl_options(
7235 SpawnedTestServer::SSLOptions::CERT_AUTO);
7236 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
[email protected]a13234c2012-03-20 21:45:027237 SSLConfigService::SetCRLSet(
7238 scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting()));
7239
7240 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:127241 DoConnection(ssl_options, &cert_status);
[email protected]a13234c2012-03-20 21:45:027242
7243 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
7244 cert_status & CERT_STATUS_ALL_ERRORS);
7245
7246 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
[email protected]b6f2de32012-08-17 04:35:087247 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7248 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
[email protected]a13234c2012-03-20 21:45:027249}
7250
[email protected]51523f52013-07-31 21:57:287251TEST_F(HTTPSEVCRLSetTest, FreshCRLSetCovered) {
7252 if (!SystemSupportsOCSP()) {
7253 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7254 return;
7255 }
7256
7257 SpawnedTestServer::SSLOptions ssl_options(
7258 SpawnedTestServer::SSLOptions::CERT_AUTO);
7259 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
7260 SSLConfigService::SetCRLSet(
7261 scoped_refptr<CRLSet>(CRLSet::ForTesting(
7262 false, &kOCSPTestCertSPKI, "")));
7263
7264 CertStatus cert_status;
7265 DoConnection(ssl_options, &cert_status);
7266
7267 // With a fresh CRLSet that covers the issuing certificate, we shouldn't do a
7268 // revocation check for EV.
7269 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
7270 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7271 static_cast<bool>(cert_status & CERT_STATUS_IS_EV));
7272 EXPECT_FALSE(
7273 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
7274}
7275
7276TEST_F(HTTPSEVCRLSetTest, FreshCRLSetNotCovered) {
7277 if (!SystemSupportsOCSP()) {
7278 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7279 return;
7280 }
7281
[email protected]ce7d0cbc2013-05-03 18:57:227282 SpawnedTestServer::SSLOptions ssl_options(
7283 SpawnedTestServer::SSLOptions::CERT_AUTO);
7284 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
[email protected]a13234c2012-03-20 21:45:027285 SSLConfigService::SetCRLSet(
7286 scoped_refptr<CRLSet>(CRLSet::EmptyCRLSetForTesting()));
7287
[email protected]51523f52013-07-31 21:57:287288 CertStatus cert_status = 0;
[email protected]295e5cd2012-08-23 01:05:127289 DoConnection(ssl_options, &cert_status);
[email protected]a13234c2012-03-20 21:45:027290
[email protected]51523f52013-07-31 21:57:287291 // Even with a fresh CRLSet, we should still do online revocation checks when
7292 // the certificate chain isn't covered by the CRLSet, which it isn't in this
7293 // test.
7294 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
7295 cert_status & CERT_STATUS_ALL_ERRORS);
[email protected]a13234c2012-03-20 21:45:027296
[email protected]51523f52013-07-31 21:57:287297 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
[email protected]a13234c2012-03-20 21:45:027298 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
[email protected]51523f52013-07-31 21:57:287299 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
[email protected]a13234c2012-03-20 21:45:027300}
7301
[email protected]b6f2de32012-08-17 04:35:087302TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSetAndRevokedNonEVCert) {
7303 // Test that when EV verification is requested, but online revocation
7304 // checking is disabled, and the leaf certificate is not in fact EV, that
7305 // no revocation checking actually happens.
7306 if (!SystemSupportsOCSP()) {
7307 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7308 return;
7309 }
7310
7311 // Unmark the certificate's OID as EV, which should disable revocation
7312 // checking (as per the user preference)
7313 ev_test_policy_.reset();
7314
[email protected]ce7d0cbc2013-05-03 18:57:227315 SpawnedTestServer::SSLOptions ssl_options(
7316 SpawnedTestServer::SSLOptions::CERT_AUTO);
7317 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED;
[email protected]b6f2de32012-08-17 04:35:087318 SSLConfigService::SetCRLSet(
7319 scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting()));
7320
7321 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:127322 DoConnection(ssl_options, &cert_status);
[email protected]b6f2de32012-08-17 04:35:087323
7324 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
7325
7326 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7327 EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
7328}
7329
[email protected]a13234c2012-03-20 21:45:027330class HTTPSCRLSetTest : public HTTPSOCSPTest {
7331 protected:
7332 virtual void SetupContext(URLRequestContext* context) OVERRIDE {
7333 context->set_ssl_config_service(
7334 new TestSSLConfigService(false /* check for EV */,
[email protected]3a86a712013-07-30 07:16:207335 false /* online revocation checking */,
7336 false /* require rev. checking for local
7337 anchors */));
[email protected]a13234c2012-03-20 21:45:027338 }
7339};
7340
7341TEST_F(HTTPSCRLSetTest, ExpiredCRLSet) {
[email protected]ce7d0cbc2013-05-03 18:57:227342 SpawnedTestServer::SSLOptions ssl_options(
7343 SpawnedTestServer::SSLOptions::CERT_AUTO);
7344 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
[email protected]a13234c2012-03-20 21:45:027345 SSLConfigService::SetCRLSet(
7346 scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting()));
7347
[email protected]924e9f92012-12-16 22:00:537348 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:127349 DoConnection(ssl_options, &cert_status);
[email protected]a13234c2012-03-20 21:45:027350
7351 // If we're not trying EV verification then, even if the CRLSet has expired,
7352 // we don't fall back to online revocation checks.
7353 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
7354 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7355 EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
7356}
[email protected]51523f52013-07-31 21:57:287357
7358TEST_F(HTTPSCRLSetTest, CRLSetRevoked) {
7359#if defined(USE_OPENSSL)
7360 LOG(WARNING) << "Skipping test because system doesn't support CRLSets";
7361 return;
7362#endif
7363
7364 SpawnedTestServer::SSLOptions ssl_options(
7365 SpawnedTestServer::SSLOptions::CERT_AUTO);
7366 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK;
7367 ssl_options.cert_serial = 10;
7368 SSLConfigService::SetCRLSet(
7369 scoped_refptr<CRLSet>(CRLSet::ForTesting(
7370 false, &kOCSPTestCertSPKI, "\x0a")));
7371
7372 CertStatus cert_status = 0;
7373 DoConnection(ssl_options, &cert_status);
7374
7375 // If the certificate is recorded as revoked in the CRLSet, that should be
7376 // reflected without online revocation checking.
7377 EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS);
7378 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7379 EXPECT_FALSE(
7380 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
7381}
[email protected]316c1e5e2012-09-12 15:17:447382#endif // !defined(OS_IOS)
[email protected]dffe8242012-03-20 15:14:277383
[email protected]316c1e5e2012-09-12 15:17:447384#if !defined(DISABLE_FTP_SUPPORT)
[email protected]b89ca032009-08-31 21:41:317385class URLRequestTestFTP : public URLRequestTest {
[email protected]95409e12010-08-17 20:07:117386 public:
[email protected]d9fca99a2012-02-24 16:16:207387 URLRequestTestFTP()
[email protected]ce7d0cbc2013-05-03 18:57:227388 : test_server_(SpawnedTestServer::TYPE_FTP, SpawnedTestServer::kLocalhost,
[email protected]6cdfd7f2013-02-08 20:40:157389 base::FilePath()) {
[email protected]95409e12010-08-17 20:07:117390 }
7391
[email protected]b89ca032009-08-31 21:41:317392 protected:
[email protected]ce7d0cbc2013-05-03 18:57:227393 SpawnedTestServer test_server_;
[email protected]b89ca032009-08-31 21:41:317394};
7395
[email protected]d2a133182012-08-05 16:44:087396// Make sure an FTP request using an unsafe ports fails.
[email protected]f2f31b32013-01-16 23:24:097397TEST_F(URLRequestTestFTP, UnsafePort) {
[email protected]d2a133182012-08-05 16:44:087398 ASSERT_TRUE(test_server_.Start());
7399
[email protected]9d5730b2012-08-24 17:42:497400 URLRequestJobFactoryImpl job_factory;
[email protected]e0f35c92013-05-08 16:04:347401 FtpNetworkLayer ftp_transaction_factory(default_context_.host_resolver());
[email protected]d2a133182012-08-05 16:44:087402
7403 GURL url("ftp://127.0.0.1:7");
[email protected]d2a133182012-08-05 16:44:087404 job_factory.SetProtocolHandler(
7405 "ftp",
[email protected]e0f35c92013-05-08 16:04:347406 new FtpProtocolHandler(&ftp_transaction_factory));
[email protected]d2a133182012-08-05 16:44:087407 default_context_.set_job_factory(&job_factory);
7408
7409 TestDelegate d;
7410 {
[email protected]2ca01e52013-10-31 22:05:197411 URLRequest r(url, DEFAULT_PRIORITY, &d, &default_context_);
[email protected]d2a133182012-08-05 16:44:087412 r.Start();
7413 EXPECT_TRUE(r.is_pending());
7414
[email protected]255620da2013-08-19 13:14:297415 base::RunLoop().Run();
[email protected]d2a133182012-08-05 16:44:087416
7417 EXPECT_FALSE(r.is_pending());
7418 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
7419 EXPECT_EQ(ERR_UNSAFE_PORT, r.status().error());
7420 }
7421}
7422
[email protected]5accf7332009-11-24 03:41:387423// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:127424TEST_F(URLRequestTestFTP, DISABLED_FTPDirectoryListing) {
[email protected]95409e12010-08-17 20:07:117425 ASSERT_TRUE(test_server_.Start());
7426
[email protected]a25e90e2009-09-09 17:05:377427 TestDelegate d;
7428 {
[email protected]2ca01e52013-10-31 22:05:197429 URLRequest r(
7430 test_server_.GetURL("/"), DEFAULT_PRIORITY, &d, &default_context_);
[email protected]a25e90e2009-09-09 17:05:377431 r.Start();
7432 EXPECT_TRUE(r.is_pending());
7433
[email protected]255620da2013-08-19 13:14:297434 base::RunLoop().Run();
[email protected]a25e90e2009-09-09 17:05:377435
7436 EXPECT_FALSE(r.is_pending());
7437 EXPECT_EQ(1, d.response_started_count());
7438 EXPECT_FALSE(d.received_data_before_response());
7439 EXPECT_LT(0, d.bytes_received());
[email protected]6d81b482011-02-22 19:47:197440 EXPECT_EQ(test_server_.host_port_pair().host(),
7441 r.GetSocketAddress().host());
7442 EXPECT_EQ(test_server_.host_port_pair().port(),
7443 r.GetSocketAddress().port());
[email protected]a25e90e2009-09-09 17:05:377444 }
7445}
7446
[email protected]7df70012010-02-04 00:09:557447// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:127448TEST_F(URLRequestTestFTP, DISABLED_FTPGetTestAnonymous) {
[email protected]95409e12010-08-17 20:07:117449 ASSERT_TRUE(test_server_.Start());
7450
[email protected]6cdfd7f2013-02-08 20:40:157451 base::FilePath app_path;
[email protected]dd265012009-01-08 20:45:277452 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:027453 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:277454 TestDelegate d;
7455 {
[email protected]2ca01e52013-10-31 22:05:197456 URLRequest r(test_server_.GetURL("/LICENSE"),
7457 DEFAULT_PRIORITY,
7458 &d,
7459 &default_context_);
[email protected]dd265012009-01-08 20:45:277460 r.Start();
7461 EXPECT_TRUE(r.is_pending());
7462
[email protected]255620da2013-08-19 13:14:297463 base::RunLoop().Run();
[email protected]dd265012009-01-08 20:45:277464
7465 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:497466 base::GetFileSize(app_path, &file_size);
[email protected]dd265012009-01-08 20:45:277467
[email protected]ba2f3342009-07-30 18:08:427468 EXPECT_FALSE(r.is_pending());
[email protected]dd265012009-01-08 20:45:277469 EXPECT_EQ(1, d.response_started_count());
7470 EXPECT_FALSE(d.received_data_before_response());
7471 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
[email protected]6d81b482011-02-22 19:47:197472 EXPECT_EQ(test_server_.host_port_pair().host(),
7473 r.GetSocketAddress().host());
7474 EXPECT_EQ(test_server_.host_port_pair().port(),
7475 r.GetSocketAddress().port());
[email protected]dd265012009-01-08 20:45:277476 }
7477}
7478
[email protected]e9ecbd12009-12-20 18:44:407479// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:127480TEST_F(URLRequestTestFTP, DISABLED_FTPGetTest) {
[email protected]95409e12010-08-17 20:07:117481 ASSERT_TRUE(test_server_.Start());
7482
[email protected]6cdfd7f2013-02-08 20:40:157483 base::FilePath app_path;
[email protected]dd265012009-01-08 20:45:277484 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:027485 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:277486 TestDelegate d;
7487 {
[email protected]ef2bf422012-05-11 03:27:097488 URLRequest r(
[email protected]95409e12010-08-17 20:07:117489 test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", "chrome"),
[email protected]2ca01e52013-10-31 22:05:197490 DEFAULT_PRIORITY,
[email protected]94e2bbe2012-06-22 15:26:137491 &d,
7492 &default_context_);
[email protected]dd265012009-01-08 20:45:277493 r.Start();
7494 EXPECT_TRUE(r.is_pending());
7495
[email protected]255620da2013-08-19 13:14:297496 base::RunLoop().Run();
[email protected]dd265012009-01-08 20:45:277497
7498 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:497499 base::GetFileSize(app_path, &file_size);
[email protected]dd265012009-01-08 20:45:277500
[email protected]ba2f3342009-07-30 18:08:427501 EXPECT_FALSE(r.is_pending());
[email protected]6d81b482011-02-22 19:47:197502 EXPECT_EQ(test_server_.host_port_pair().host(),
7503 r.GetSocketAddress().host());
7504 EXPECT_EQ(test_server_.host_port_pair().port(),
7505 r.GetSocketAddress().port());
[email protected]dd265012009-01-08 20:45:277506 EXPECT_EQ(1, d.response_started_count());
7507 EXPECT_FALSE(d.received_data_before_response());
7508 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
[email protected]58e32bb2013-01-21 18:23:257509
7510 LoadTimingInfo load_timing_info;
[email protected]0a703912013-02-02 04:25:177511 r.GetLoadTimingInfo(&load_timing_info);
[email protected]3b23a222013-05-15 21:33:257512 TestLoadTimingNoHttpResponse(load_timing_info);
[email protected]dd265012009-01-08 20:45:277513 }
7514}
7515
[email protected]49abd652010-08-05 05:04:537516// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:127517TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPassword) {
[email protected]95409e12010-08-17 20:07:117518 ASSERT_TRUE(test_server_.Start());
7519
[email protected]6cdfd7f2013-02-08 20:40:157520 base::FilePath app_path;
[email protected]dd265012009-01-08 20:45:277521 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:027522 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:277523 TestDelegate d;
7524 {
[email protected]2ca01e52013-10-31 22:05:197525 URLRequest r(test_server_.GetURLWithUserAndPassword(
7526 "/LICENSE", "chrome", "wrong_password"),
7527 DEFAULT_PRIORITY,
7528 &d,
7529 &default_context_);
[email protected]dd265012009-01-08 20:45:277530 r.Start();
7531 EXPECT_TRUE(r.is_pending());
7532
[email protected]255620da2013-08-19 13:14:297533 base::RunLoop().Run();
[email protected]dd265012009-01-08 20:45:277534
7535 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:497536 base::GetFileSize(app_path, &file_size);
[email protected]dd265012009-01-08 20:45:277537
[email protected]ba2f3342009-07-30 18:08:427538 EXPECT_FALSE(r.is_pending());
[email protected]dd265012009-01-08 20:45:277539 EXPECT_EQ(1, d.response_started_count());
7540 EXPECT_FALSE(d.received_data_before_response());
7541 EXPECT_EQ(d.bytes_received(), 0);
7542 }
7543}
7544
[email protected]cde4e80d2009-10-16 19:58:157545// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:127546TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPasswordRestart) {
[email protected]95409e12010-08-17 20:07:117547 ASSERT_TRUE(test_server_.Start());
7548
[email protected]6cdfd7f2013-02-08 20:40:157549 base::FilePath app_path;
[email protected]8b8a197d2009-08-26 15:57:587550 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
7551 app_path = app_path.AppendASCII("LICENSE");
7552 TestDelegate d;
7553 // Set correct login credentials. The delegate will be asked for them when
7554 // the initial login with wrong credentials will fail.
[email protected]f3cf9802011-10-28 18:44:587555 d.set_credentials(AuthCredentials(kChrome, kChrome));
[email protected]8b8a197d2009-08-26 15:57:587556 {
[email protected]2ca01e52013-10-31 22:05:197557 URLRequest r(test_server_.GetURLWithUserAndPassword(
7558 "/LICENSE", "chrome", "wrong_password"),
7559 DEFAULT_PRIORITY,
7560 &d,
7561 &default_context_);
[email protected]8b8a197d2009-08-26 15:57:587562 r.Start();
7563 EXPECT_TRUE(r.is_pending());
7564
[email protected]255620da2013-08-19 13:14:297565 base::RunLoop().Run();
[email protected]8b8a197d2009-08-26 15:57:587566
7567 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:497568 base::GetFileSize(app_path, &file_size);
[email protected]8b8a197d2009-08-26 15:57:587569
7570 EXPECT_FALSE(r.is_pending());
7571 EXPECT_EQ(1, d.response_started_count());
7572 EXPECT_FALSE(d.received_data_before_response());
7573 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
7574 }
7575}
7576
[email protected]49abd652010-08-05 05:04:537577// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:127578TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUser) {
[email protected]95409e12010-08-17 20:07:117579 ASSERT_TRUE(test_server_.Start());
7580
[email protected]6cdfd7f2013-02-08 20:40:157581 base::FilePath app_path;
[email protected]dd265012009-01-08 20:45:277582 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:027583 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:277584 TestDelegate d;
7585 {
[email protected]2ca01e52013-10-31 22:05:197586 URLRequest r(test_server_.GetURLWithUserAndPassword(
7587 "/LICENSE", "wrong_user", "chrome"),
7588 DEFAULT_PRIORITY,
7589 &d,
7590 &default_context_);
[email protected]dd265012009-01-08 20:45:277591 r.Start();
7592 EXPECT_TRUE(r.is_pending());
7593
[email protected]255620da2013-08-19 13:14:297594 base::RunLoop().Run();
[email protected]dd265012009-01-08 20:45:277595
7596 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:497597 base::GetFileSize(app_path, &file_size);
[email protected]dd265012009-01-08 20:45:277598
[email protected]ba2f3342009-07-30 18:08:427599 EXPECT_FALSE(r.is_pending());
[email protected]dd265012009-01-08 20:45:277600 EXPECT_EQ(1, d.response_started_count());
7601 EXPECT_FALSE(d.received_data_before_response());
7602 EXPECT_EQ(d.bytes_received(), 0);
7603 }
7604}
[email protected]8b8a197d2009-08-26 15:57:587605
[email protected]cde4e80d2009-10-16 19:58:157606// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:127607TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUserRestart) {
[email protected]95409e12010-08-17 20:07:117608 ASSERT_TRUE(test_server_.Start());
7609
[email protected]6cdfd7f2013-02-08 20:40:157610 base::FilePath app_path;
[email protected]8b8a197d2009-08-26 15:57:587611 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
7612 app_path = app_path.AppendASCII("LICENSE");
7613 TestDelegate d;
7614 // Set correct login credentials. The delegate will be asked for them when
7615 // the initial login with wrong credentials will fail.
[email protected]f3cf9802011-10-28 18:44:587616 d.set_credentials(AuthCredentials(kChrome, kChrome));
[email protected]8b8a197d2009-08-26 15:57:587617 {
[email protected]2ca01e52013-10-31 22:05:197618 URLRequest r(test_server_.GetURLWithUserAndPassword(
7619 "/LICENSE", "wrong_user", "chrome"),
7620 DEFAULT_PRIORITY,
7621 &d,
7622 &default_context_);
[email protected]8b8a197d2009-08-26 15:57:587623 r.Start();
7624 EXPECT_TRUE(r.is_pending());
7625
[email protected]255620da2013-08-19 13:14:297626 base::RunLoop().Run();
[email protected]8b8a197d2009-08-26 15:57:587627
7628 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:497629 base::GetFileSize(app_path, &file_size);
[email protected]8b8a197d2009-08-26 15:57:587630
7631 EXPECT_FALSE(r.is_pending());
7632 EXPECT_EQ(1, d.response_started_count());
7633 EXPECT_FALSE(d.received_data_before_response());
7634 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
7635 }
7636}
[email protected]60a3df52009-09-22 16:13:247637
[email protected]cde4e80d2009-10-16 19:58:157638// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:127639TEST_F(URLRequestTestFTP, DISABLED_FTPCacheURLCredentials) {
[email protected]95409e12010-08-17 20:07:117640 ASSERT_TRUE(test_server_.Start());
7641
[email protected]6cdfd7f2013-02-08 20:40:157642 base::FilePath app_path;
[email protected]60a3df52009-09-22 16:13:247643 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
7644 app_path = app_path.AppendASCII("LICENSE");
7645
7646 scoped_ptr<TestDelegate> d(new TestDelegate);
7647 {
7648 // Pass correct login identity in the URL.
[email protected]ef2bf422012-05-11 03:27:097649 URLRequest r(
[email protected]2ca01e52013-10-31 22:05:197650 test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", "chrome"),
7651 DEFAULT_PRIORITY,
[email protected]94e2bbe2012-06-22 15:26:137652 d.get(),
7653 &default_context_);
[email protected]60a3df52009-09-22 16:13:247654 r.Start();
7655 EXPECT_TRUE(r.is_pending());
7656
[email protected]255620da2013-08-19 13:14:297657 base::RunLoop().Run();
[email protected]60a3df52009-09-22 16:13:247658
7659 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:497660 base::GetFileSize(app_path, &file_size);
[email protected]60a3df52009-09-22 16:13:247661
7662 EXPECT_FALSE(r.is_pending());
7663 EXPECT_EQ(1, d->response_started_count());
7664 EXPECT_FALSE(d->received_data_before_response());
7665 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
7666 }
7667
7668 d.reset(new TestDelegate);
7669 {
7670 // This request should use cached identity from previous request.
[email protected]2ca01e52013-10-31 22:05:197671 URLRequest r(test_server_.GetURL("/LICENSE"),
7672 DEFAULT_PRIORITY,
7673 d.get(),
7674 &default_context_);
[email protected]60a3df52009-09-22 16:13:247675 r.Start();
7676 EXPECT_TRUE(r.is_pending());
7677
[email protected]255620da2013-08-19 13:14:297678 base::RunLoop().Run();
[email protected]60a3df52009-09-22 16:13:247679
7680 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:497681 base::GetFileSize(app_path, &file_size);
[email protected]60a3df52009-09-22 16:13:247682
7683 EXPECT_FALSE(r.is_pending());
7684 EXPECT_EQ(1, d->response_started_count());
7685 EXPECT_FALSE(d->received_data_before_response());
7686 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
7687 }
7688}
7689
[email protected]cde4e80d2009-10-16 19:58:157690// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:127691TEST_F(URLRequestTestFTP, DISABLED_FTPCacheLoginBoxCredentials) {
[email protected]95409e12010-08-17 20:07:117692 ASSERT_TRUE(test_server_.Start());
7693
[email protected]6cdfd7f2013-02-08 20:40:157694 base::FilePath app_path;
[email protected]60a3df52009-09-22 16:13:247695 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
7696 app_path = app_path.AppendASCII("LICENSE");
7697
7698 scoped_ptr<TestDelegate> d(new TestDelegate);
7699 // Set correct login credentials. The delegate will be asked for them when
7700 // the initial login with wrong credentials will fail.
[email protected]f3cf9802011-10-28 18:44:587701 d->set_credentials(AuthCredentials(kChrome, kChrome));
[email protected]60a3df52009-09-22 16:13:247702 {
[email protected]2ca01e52013-10-31 22:05:197703 URLRequest r(test_server_.GetURLWithUserAndPassword(
7704 "/LICENSE", "chrome", "wrong_password"),
7705 DEFAULT_PRIORITY,
7706 d.get(),
7707 &default_context_);
[email protected]60a3df52009-09-22 16:13:247708 r.Start();
7709 EXPECT_TRUE(r.is_pending());
7710
[email protected]255620da2013-08-19 13:14:297711 base::RunLoop().Run();
[email protected]60a3df52009-09-22 16:13:247712
7713 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:497714 base::GetFileSize(app_path, &file_size);
[email protected]60a3df52009-09-22 16:13:247715
7716 EXPECT_FALSE(r.is_pending());
7717 EXPECT_EQ(1, d->response_started_count());
7718 EXPECT_FALSE(d->received_data_before_response());
7719 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
7720 }
7721
7722 // Use a new delegate without explicit credentials. The cached ones should be
7723 // used.
7724 d.reset(new TestDelegate);
7725 {
7726 // Don't pass wrong credentials in the URL, they would override valid cached
7727 // ones.
[email protected]2ca01e52013-10-31 22:05:197728 URLRequest r(test_server_.GetURL("/LICENSE"),
7729 DEFAULT_PRIORITY,
7730 d.get(),
7731 &default_context_);
[email protected]60a3df52009-09-22 16:13:247732 r.Start();
7733 EXPECT_TRUE(r.is_pending());
7734
[email protected]255620da2013-08-19 13:14:297735 base::RunLoop().Run();
[email protected]60a3df52009-09-22 16:13:247736
7737 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:497738 base::GetFileSize(app_path, &file_size);
[email protected]60a3df52009-09-22 16:13:247739
7740 EXPECT_FALSE(r.is_pending());
7741 EXPECT_EQ(1, d->response_started_count());
7742 EXPECT_FALSE(d->received_data_before_response());
7743 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
7744 }
7745}
[email protected]316c1e5e2012-09-12 15:17:447746#endif // !defined(DISABLE_FTP_SUPPORT)
[email protected]7461a402011-03-24 23:19:517747
7748} // namespace net