blob: 18ec0f397a58654fbcf038740bd0aa47a9035fba [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>
initial.commit586acc5fe2008-07-26 22:42:5213
[email protected]aad63572011-05-24 20:14:3914#include "base/basictypes.h"
[email protected]218aa6a12011-09-13 17:38:3815#include "base/bind.h"
[email protected]8523ba52011-05-22 19:00:5816#include "base/compiler_specific.h"
thestigd8df0332014-09-04 06:33:2917#include "base/files/file_util.h"
[email protected]3ca8b362013-11-11 22:18:0718#include "base/files/scoped_temp_dir.h"
[email protected]34b2b002009-11-20 06:53:2819#include "base/format_macros.h"
mmenke19378d22014-09-09 04:12:5920#include "base/memory/scoped_ptr.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"
mmenkecbc2b712014-10-09 20:29:0732#include "net/base/chunked_upload_data_stream.h"
33#include "net/base/elements_upload_data_stream.h"
initial.commit586acc5fe2008-07-26 22:42:5234#include "net/base/load_flags.h"
[email protected]58e32bb2013-01-21 18:23:2535#include "net/base/load_timing_info.h"
36#include "net/base/load_timing_info_test_util.h"
[email protected]d8eb84242010-09-25 02:25:0637#include "net/base/net_errors.h"
initial.commit586acc5fe2008-07-26 22:42:5238#include "net/base/net_module.h"
39#include "net/base/net_util.h"
[email protected]2ca01e52013-10-31 22:05:1940#include "net/base/request_priority.h"
[email protected]42fdb452012-11-01 12:44:4041#include "net/base/test_data_directory.h"
[email protected]f288ef02012-12-15 20:28:2842#include "net/base/upload_bytes_element_reader.h"
43#include "net/base/upload_data_stream.h"
44#include "net/base/upload_file_element_reader.h"
[email protected]6e7845ae2013-03-29 21:48:1145#include "net/cert/ev_root_ca_metadata.h"
[email protected]a8fed1742013-12-27 02:14:2446#include "net/cert/mock_cert_verifier.h"
[email protected]6e7845ae2013-03-29 21:48:1147#include "net/cert/test_root_certs.h"
eromaned744f32015-04-09 06:35:4948#include "net/cert_net/nss_ocsp.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"
eroman87c53d62015-04-02 06:51:0761#include "net/log/net_log.h"
62#include "net/log/net_log_unittest.h"
vishal.b62985ca92015-04-17 08:45:5163#include "net/log/test_net_log.h"
[email protected]63de95b2008-12-10 04:11:2764#include "net/proxy/proxy_service.h"
[email protected]c3456bb2011-12-12 22:22:1965#include "net/socket/ssl_client_socket.h"
davidben8ecc3072014-09-03 23:19:0966#include "net/ssl/ssl_cipher_suite_names.h"
[email protected]536fd0b2013-03-14 17:41:5767#include "net/ssl/ssl_connection_status_flags.h"
[email protected]6e7845ae2013-03-29 21:48:1168#include "net/test/cert_test_util.h"
[email protected]89b32522013-05-07 20:04:2169#include "net/test/spawned_test_server/spawned_test_server.h"
[email protected]e0f35c92013-05-08 16:04:3470#include "net/url_request/data_protocol_handler.h"
[email protected]ee4c30d2012-11-07 15:08:4371#include "net/url_request/static_http_user_agent_settings.h"
initial.commit586acc5fe2008-07-26 22:42:5272#include "net/url_request/url_request.h"
[email protected]bcb84f8b2009-08-31 16:20:1473#include "net/url_request/url_request_http_job.h"
bengr1bf8e942014-11-07 01:36:5074#include "net/url_request/url_request_intercepting_job_factory.h"
75#include "net/url_request/url_request_interceptor.h"
[email protected]9d5730b2012-08-24 17:42:4976#include "net/url_request/url_request_job_factory_impl.h"
[email protected]3c5ca8c2011-09-29 01:14:5177#include "net/url_request/url_request_redirect_job.h"
[email protected]a5c713f2009-04-16 21:05:4778#include "net/url_request/url_request_test_job.h"
[email protected]d2db0292011-01-26 20:23:4479#include "net/url_request/url_request_test_util.h"
initial.commit586acc5fe2008-07-26 22:42:5280#include "testing/gtest/include/gtest/gtest.h"
[email protected]23887f04f2008-12-02 19:20:1581#include "testing/platform_test.h"
initial.commit586acc5fe2008-07-26 22:42:5282
[email protected]02494ec2014-05-07 15:05:2983#if !defined(DISABLE_FILE_SUPPORT)
[email protected]5ecf7cb282014-05-11 01:49:5584#include "net/base/filename_util.h"
[email protected]02494ec2014-05-07 15:05:2985#include "net/url_request/file_protocol_handler.h"
86#include "net/url_request/url_request_file_dir_job.h"
87#endif
88
89#if !defined(DISABLE_FTP_SUPPORT)
90#include "net/url_request/ftp_protocol_handler.h"
91#endif
92
[email protected]dffe8242012-03-20 15:14:2793#if defined(OS_WIN)
[email protected]451fd902012-10-03 17:14:4894#include "base/win/scoped_com_initializer.h"
[email protected]aed9efb2013-04-13 01:20:5695#include "base/win/scoped_comptr.h"
[email protected]dffe8242012-03-20 15:14:2796#include "base/win/windows_version.h"
97#endif
98
[email protected]ad65a3e2013-12-25 18:18:0199using base::ASCIIToUTF16;
[email protected]e1acf6f2008-10-27 20:43:33100using base::Time;
halton.huoe4e45742014-12-08 07:55:46101using std::string;
[email protected]e1acf6f2008-10-27 20:43:33102
[email protected]7461a402011-03-24 23:19:51103namespace net {
104
initial.commit586acc5fe2008-07-26 22:42:52105namespace {
106
[email protected]42cba2fb2013-03-29 19:58:57107const base::string16 kChrome(ASCIIToUTF16("chrome"));
108const base::string16 kSecret(ASCIIToUTF16("secret"));
109const base::string16 kUser(ASCIIToUTF16("user"));
[email protected]13c8a092010-07-29 06:15:44110
[email protected]2bba3252013-04-08 19:50:59111// Tests load timing information in the case a fresh connection was used, with
112// no proxy.
[email protected]cba24642014-08-15 20:49:59113void TestLoadTimingNotReused(const LoadTimingInfo& load_timing_info,
[email protected]58e32bb2013-01-21 18:23:25114 int connect_timing_flags) {
115 EXPECT_FALSE(load_timing_info.socket_reused);
[email protected]cba24642014-08-15 20:49:59116 EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
[email protected]58e32bb2013-01-21 18:23:25117
118 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
119 EXPECT_FALSE(load_timing_info.request_start.is_null());
120
121 EXPECT_LE(load_timing_info.request_start,
122 load_timing_info.connect_timing.connect_start);
123 ExpectConnectTimingHasTimes(load_timing_info.connect_timing,
124 connect_timing_flags);
125 EXPECT_LE(load_timing_info.connect_timing.connect_end,
126 load_timing_info.send_start);
127 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
128 EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
129
[email protected]58e32bb2013-01-21 18:23:25130 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null());
131 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null());
132}
133
[email protected]2bba3252013-04-08 19:50:59134// Same as above, but with proxy times.
135void TestLoadTimingNotReusedWithProxy(
[email protected]cba24642014-08-15 20:49:59136 const LoadTimingInfo& load_timing_info,
[email protected]2bba3252013-04-08 19:50:59137 int connect_timing_flags) {
138 EXPECT_FALSE(load_timing_info.socket_reused);
[email protected]cba24642014-08-15 20:49:59139 EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
[email protected]2bba3252013-04-08 19:50:59140
141 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
142 EXPECT_FALSE(load_timing_info.request_start.is_null());
143
144 EXPECT_LE(load_timing_info.request_start,
145 load_timing_info.proxy_resolve_start);
146 EXPECT_LE(load_timing_info.proxy_resolve_start,
147 load_timing_info.proxy_resolve_end);
148 EXPECT_LE(load_timing_info.proxy_resolve_end,
149 load_timing_info.connect_timing.connect_start);
150 ExpectConnectTimingHasTimes(load_timing_info.connect_timing,
151 connect_timing_flags);
152 EXPECT_LE(load_timing_info.connect_timing.connect_end,
153 load_timing_info.send_start);
154 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
155 EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
156}
157
158// Same as above, but with a reused socket and proxy times.
159void TestLoadTimingReusedWithProxy(
[email protected]cba24642014-08-15 20:49:59160 const LoadTimingInfo& load_timing_info) {
[email protected]2bba3252013-04-08 19:50:59161 EXPECT_TRUE(load_timing_info.socket_reused);
[email protected]cba24642014-08-15 20:49:59162 EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
[email protected]2bba3252013-04-08 19:50:59163
164 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
165 EXPECT_FALSE(load_timing_info.request_start.is_null());
166
167 ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing);
168
169 EXPECT_LE(load_timing_info.request_start,
170 load_timing_info.proxy_resolve_start);
171 EXPECT_LE(load_timing_info.proxy_resolve_start,
172 load_timing_info.proxy_resolve_end);
173 EXPECT_LE(load_timing_info.proxy_resolve_end,
174 load_timing_info.send_start);
175 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
176 EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
177}
178
xunjielia6888202015-04-14 21:34:25179#if !defined(DISABLE_FILE_SUPPORT)
[email protected]3b23a222013-05-15 21:33:25180// Tests load timing information in the case of a cache hit, when no cache
181// validation request was sent over the wire.
[email protected]e3a85452013-11-14 01:46:17182base::StringPiece TestNetResourceProvider(int key) {
183 return "header";
184}
185
186void FillBuffer(char* buffer, size_t len) {
187 static bool called = false;
188 if (!called) {
189 called = true;
190 int seed = static_cast<int>(Time::Now().ToInternalValue());
191 srand(seed);
192 }
193
194 for (size_t i = 0; i < len; i++) {
195 buffer[i] = static_cast<char>(rand());
196 if (!buffer[i])
197 buffer[i] = 'g';
198 }
199}
xunjielia6888202015-04-14 21:34:25200#endif
[email protected]e3a85452013-11-14 01:46:17201
202#if !defined(OS_IOS)
[email protected]3b23a222013-05-15 21:33:25203void TestLoadTimingCacheHitNoNetwork(
[email protected]cba24642014-08-15 20:49:59204 const LoadTimingInfo& load_timing_info) {
[email protected]3b23a222013-05-15 21:33:25205 EXPECT_FALSE(load_timing_info.socket_reused);
[email protected]cba24642014-08-15 20:49:59206 EXPECT_EQ(NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
[email protected]3b23a222013-05-15 21:33:25207
208 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
209 EXPECT_FALSE(load_timing_info.request_start.is_null());
210
211 ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing);
212 EXPECT_LE(load_timing_info.request_start, load_timing_info.send_start);
213 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
214 EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
215
216 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null());
217 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null());
218}
219
xunjielia6888202015-04-14 21:34:25220#if !defined(DISABLE_FTP_SUPPORT)
[email protected]3b23a222013-05-15 21:33:25221// Tests load timing in the case that there is no HTTP response. This can be
222// used to test in the case of errors or non-HTTP requests.
223void TestLoadTimingNoHttpResponse(
[email protected]cba24642014-08-15 20:49:59224 const LoadTimingInfo& load_timing_info) {
[email protected]58e32bb2013-01-21 18:23:25225 EXPECT_FALSE(load_timing_info.socket_reused);
[email protected]cba24642014-08-15 20:49:59226 EXPECT_EQ(NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
[email protected]58e32bb2013-01-21 18:23:25227
228 // Only the request times should be non-null.
229 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
230 EXPECT_FALSE(load_timing_info.request_start.is_null());
231
232 ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing);
233
234 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null());
235 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null());
236 EXPECT_TRUE(load_timing_info.send_start.is_null());
237 EXPECT_TRUE(load_timing_info.send_end.is_null());
238 EXPECT_TRUE(load_timing_info.receive_headers_end.is_null());
239}
xunjielia6888202015-04-14 21:34:25240#endif
[email protected]58e32bb2013-01-21 18:23:25241
[email protected]71c64f62008-11-15 04:36:51242// Do a case-insensitive search through |haystack| for |needle|.
243bool ContainsString(const std::string& haystack, const char* needle) {
244 std::string::const_iterator it =
245 std::search(haystack.begin(),
246 haystack.end(),
247 needle,
248 needle + strlen(needle),
[email protected]07f1cee2010-11-03 03:53:35249 base::CaseInsensitiveCompare<char>());
[email protected]71c64f62008-11-15 04:36:51250 return it != haystack.end();
251}
252
mmenkecbc2b712014-10-09 20:29:07253scoped_ptr<UploadDataStream> CreateSimpleUploadData(const char* data) {
[email protected]f288ef02012-12-15 20:28:28254 scoped_ptr<UploadElementReader> reader(
255 new UploadBytesElementReader(data, strlen(data)));
mmenkecbc2b712014-10-09 20:29:07256 return ElementsUploadDataStream::CreateWithReader(reader.Pass(), 0);
[email protected]195e77d2009-07-23 19:10:23257}
258
[email protected]96adadb2010-08-28 01:16:17259// Verify that the SSLInfo of a successful SSL connection has valid values.
[email protected]7461a402011-03-24 23:19:51260void CheckSSLInfo(const SSLInfo& ssl_info) {
[email protected]96adadb2010-08-28 01:16:17261 // -1 means unknown. 0 means no encryption.
262 EXPECT_GT(ssl_info.security_bits, 0);
263
264 // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated.
pkasting6b68a162014-12-01 22:10:29265 uint16 cipher_suite = SSLConnectionStatusToCipherSuite(
[email protected]96adadb2010-08-28 01:16:17266 ssl_info.connection_status);
pkasting6b68a162014-12-01 22:10:29267 EXPECT_NE(0U, cipher_suite);
[email protected]96adadb2010-08-28 01:16:17268}
269
[email protected]79e1fd62013-06-20 06:50:04270void CheckFullRequestHeaders(const HttpRequestHeaders& headers,
271 const GURL& host_url) {
272 std::string sent_value;
273
274 EXPECT_TRUE(headers.GetHeader("Host", &sent_value));
275 EXPECT_EQ(GetHostAndOptionalPort(host_url), sent_value);
276
277 EXPECT_TRUE(headers.GetHeader("Connection", &sent_value));
278 EXPECT_EQ("keep-alive", sent_value);
279}
280
[email protected]ede03212012-09-07 12:52:26281bool FingerprintsEqual(const HashValueVector& a, const HashValueVector& b) {
[email protected]69d7ff442012-02-13 22:41:27282 size_t size = a.size();
283
284 if (size != b.size())
285 return false;
286
287 for (size_t i = 0; i < size; ++i) {
288 if (!a[i].Equals(b[i]))
289 return false;
290 }
291
292 return true;
293}
[email protected]e3a85452013-11-14 01:46:17294#endif // !defined(OS_IOS)
[email protected]69d7ff442012-02-13 22:41:27295
[email protected]dc5a5cf2012-09-26 02:49:30296// A network delegate that allows the user to choose a subset of request stages
297// to block in. When blocking, the delegate can do one of the following:
298// * synchronously return a pre-specified error code, or
299// * asynchronously return that value via an automatically called callback,
300// or
301// * block and wait for the user to do a callback.
302// Additionally, the user may also specify a redirect URL -- then each request
303// with the current URL different from the redirect target will be redirected
304// to that target, in the on-before-URL-request stage, independent of whether
305// the delegate blocks in ON_BEFORE_URL_REQUEST or not.
[email protected]4c76d7c2011-04-15 19:14:12306class BlockingNetworkDelegate : public TestNetworkDelegate {
307 public:
[email protected]dc5a5cf2012-09-26 02:49:30308 // Stages in which the delegate can block.
309 enum Stage {
[email protected]9045b8822012-01-13 20:35:35310 NOT_BLOCKED = 0,
311 ON_BEFORE_URL_REQUEST = 1 << 0,
312 ON_BEFORE_SEND_HEADERS = 1 << 1,
313 ON_HEADERS_RECEIVED = 1 << 2,
314 ON_AUTH_REQUIRED = 1 << 3
315 };
316
[email protected]dc5a5cf2012-09-26 02:49:30317 // Behavior during blocked stages. During other stages, just
[email protected]cba24642014-08-15 20:49:59318 // returns OK or NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION.
[email protected]dc5a5cf2012-09-26 02:49:30319 enum BlockMode {
320 SYNCHRONOUS, // No callback, returns specified return values.
321 AUTO_CALLBACK, // |this| posts a task to run the callback using the
322 // specified return codes.
323 USER_CALLBACK, // User takes care of doing a callback. |retval_| and
324 // |auth_retval_| are ignored. In every blocking stage the
325 // message loop is quit.
326 };
327
328 // Creates a delegate which does not block at all.
329 explicit BlockingNetworkDelegate(BlockMode block_mode);
330
331 // For users to trigger a callback returning |response|.
332 // Side-effects: resets |stage_blocked_for_callback_| and stored callbacks.
333 // Only call if |block_mode_| == USER_CALLBACK.
334 void DoCallback(int response);
335 void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response);
336
337 // Setters.
338 void set_retval(int retval) {
339 ASSERT_NE(USER_CALLBACK, block_mode_);
340 ASSERT_NE(ERR_IO_PENDING, retval);
341 ASSERT_NE(OK, retval);
342 retval_ = retval;
[email protected]9045b8822012-01-13 20:35:35343 }
344
[email protected]dc5a5cf2012-09-26 02:49:30345 // If |auth_retval| == AUTH_REQUIRED_RESPONSE_SET_AUTH, then
346 // |auth_credentials_| will be passed with the response.
347 void set_auth_retval(AuthRequiredResponse auth_retval) {
348 ASSERT_NE(USER_CALLBACK, block_mode_);
349 ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, auth_retval);
350 auth_retval_ = auth_retval;
351 }
352 void set_auth_credentials(const AuthCredentials& auth_credentials) {
353 auth_credentials_ = auth_credentials;
[email protected]9045b8822012-01-13 20:35:35354 }
355
[email protected]dc5a5cf2012-09-26 02:49:30356 void set_redirect_url(const GURL& url) {
357 redirect_url_ = url;
[email protected]9045b8822012-01-13 20:35:35358 }
359
[email protected]dc5a5cf2012-09-26 02:49:30360 void set_block_on(int block_on) {
361 block_on_ = block_on;
[email protected]9045b8822012-01-13 20:35:35362 }
363
[email protected]dc5a5cf2012-09-26 02:49:30364 // Allows the user to check in which state did we block.
365 Stage stage_blocked_for_callback() const {
366 EXPECT_EQ(USER_CALLBACK, block_mode_);
367 return stage_blocked_for_callback_;
[email protected]9045b8822012-01-13 20:35:35368 }
369
370 private:
[email protected]dc5a5cf2012-09-26 02:49:30371 void RunCallback(int response, const CompletionCallback& callback);
372 void RunAuthCallback(AuthRequiredResponse response,
373 const AuthCallback& callback);
374
[email protected]9045b8822012-01-13 20:35:35375 // TestNetworkDelegate implementation.
dchengb03027d2014-10-21 12:00:20376 int OnBeforeURLRequest(URLRequest* request,
377 const CompletionCallback& callback,
378 GURL* new_url) override;
[email protected]9045b8822012-01-13 20:35:35379
dchengb03027d2014-10-21 12:00:20380 int OnBeforeSendHeaders(URLRequest* request,
381 const CompletionCallback& callback,
382 HttpRequestHeaders* headers) override;
[email protected]9045b8822012-01-13 20:35:35383
dchengb03027d2014-10-21 12:00:20384 int OnHeadersReceived(
[email protected]9045b8822012-01-13 20:35:35385 URLRequest* request,
386 const CompletionCallback& callback,
[email protected]507af8f2012-10-20 00:42:32387 const HttpResponseHeaders* original_response_headers,
[email protected]5f714132014-03-26 10:41:16388 scoped_refptr<HttpResponseHeaders>* override_response_headers,
mostynbba063d6032014-10-09 11:01:13389 GURL* allowed_unsafe_redirect_url) override;
[email protected]9045b8822012-01-13 20:35:35390
dchengb03027d2014-10-21 12:00:20391 NetworkDelegate::AuthRequiredResponse OnAuthRequired(
[email protected]9045b8822012-01-13 20:35:35392 URLRequest* request,
393 const AuthChallengeInfo& auth_info,
394 const AuthCallback& callback,
mostynbba063d6032014-10-09 11:01:13395 AuthCredentials* credentials) override;
[email protected]9045b8822012-01-13 20:35:35396
[email protected]dc5a5cf2012-09-26 02:49:30397 // Resets the callbacks and |stage_blocked_for_callback_|.
398 void Reset();
[email protected]9045b8822012-01-13 20:35:35399
[email protected]dc5a5cf2012-09-26 02:49:30400 // Checks whether we should block in |stage|. If yes, returns an error code
401 // and optionally sets up callback based on |block_mode_|. If no, returns OK.
402 int MaybeBlockStage(Stage stage, const CompletionCallback& callback);
403
404 // Configuration parameters, can be adjusted by public methods:
405 const BlockMode block_mode_;
406
407 // Values returned on blocking stages when mode is SYNCHRONOUS or
408 // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING.
409 int retval_; // To be returned in non-auth stages.
410 AuthRequiredResponse auth_retval_;
411
[email protected]5f714132014-03-26 10:41:16412 GURL redirect_url_; // Used if non-empty during OnBeforeURLRequest.
[email protected]dc5a5cf2012-09-26 02:49:30413 int block_on_; // Bit mask: in which stages to block.
414
415 // |auth_credentials_| will be copied to |*target_auth_credential_| on
416 // callback.
417 AuthCredentials auth_credentials_;
418 AuthCredentials* target_auth_credentials_;
419
420 // Internal variables, not set by not the user:
421 // Last blocked stage waiting for user callback (unused if |block_mode_| !=
422 // USER_CALLBACK).
423 Stage stage_blocked_for_callback_;
424
425 // Callback objects stored during blocking stages.
[email protected]9045b8822012-01-13 20:35:35426 CompletionCallback callback_;
427 AuthCallback auth_callback_;
[email protected]dc5a5cf2012-09-26 02:49:30428
429 base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_;
430
431 DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate);
[email protected]9045b8822012-01-13 20:35:35432};
433
[email protected]dc5a5cf2012-09-26 02:49:30434BlockingNetworkDelegate::BlockingNetworkDelegate(BlockMode block_mode)
435 : block_mode_(block_mode),
436 retval_(OK),
437 auth_retval_(AUTH_REQUIRED_RESPONSE_NO_ACTION),
438 block_on_(0),
439 target_auth_credentials_(NULL),
440 stage_blocked_for_callback_(NOT_BLOCKED),
[email protected]aa249b52013-04-30 01:04:32441 weak_factory_(this) {
[email protected]dc5a5cf2012-09-26 02:49:30442}
443
444void BlockingNetworkDelegate::DoCallback(int response) {
445 ASSERT_EQ(USER_CALLBACK, block_mode_);
446 ASSERT_NE(NOT_BLOCKED, stage_blocked_for_callback_);
447 ASSERT_NE(ON_AUTH_REQUIRED, stage_blocked_for_callback_);
448 CompletionCallback callback = callback_;
449 Reset();
450 RunCallback(response, callback);
451}
452
453void BlockingNetworkDelegate::DoAuthCallback(
454 NetworkDelegate::AuthRequiredResponse response) {
455 ASSERT_EQ(USER_CALLBACK, block_mode_);
456 ASSERT_EQ(ON_AUTH_REQUIRED, stage_blocked_for_callback_);
457 AuthCallback auth_callback = auth_callback_;
458 Reset();
459 RunAuthCallback(response, auth_callback);
460}
461
462void BlockingNetworkDelegate::RunCallback(int response,
463 const CompletionCallback& callback) {
464 callback.Run(response);
465}
466
467void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response,
468 const AuthCallback& callback) {
469 if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) {
470 ASSERT_TRUE(target_auth_credentials_ != NULL);
471 *target_auth_credentials_ = auth_credentials_;
472 }
473 callback.Run(response);
474}
475
476int BlockingNetworkDelegate::OnBeforeURLRequest(
477 URLRequest* request,
478 const CompletionCallback& callback,
479 GURL* new_url) {
480 if (redirect_url_ == request->url())
481 return OK; // We've already seen this request and redirected elsewhere.
482
483 TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url);
484
485 if (!redirect_url_.is_empty())
486 *new_url = redirect_url_;
487
488 return MaybeBlockStage(ON_BEFORE_URL_REQUEST, callback);
489}
490
491int BlockingNetworkDelegate::OnBeforeSendHeaders(
492 URLRequest* request,
493 const CompletionCallback& callback,
494 HttpRequestHeaders* headers) {
495 TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers);
496
497 return MaybeBlockStage(ON_BEFORE_SEND_HEADERS, callback);
498}
499
500int BlockingNetworkDelegate::OnHeadersReceived(
501 URLRequest* request,
502 const CompletionCallback& callback,
[email protected]507af8f2012-10-20 00:42:32503 const HttpResponseHeaders* original_response_headers,
[email protected]5f714132014-03-26 10:41:16504 scoped_refptr<HttpResponseHeaders>* override_response_headers,
505 GURL* allowed_unsafe_redirect_url) {
506 TestNetworkDelegate::OnHeadersReceived(request,
507 callback,
508 original_response_headers,
509 override_response_headers,
510 allowed_unsafe_redirect_url);
[email protected]dc5a5cf2012-09-26 02:49:30511
512 return MaybeBlockStage(ON_HEADERS_RECEIVED, callback);
513}
514
515NetworkDelegate::AuthRequiredResponse BlockingNetworkDelegate::OnAuthRequired(
516 URLRequest* request,
517 const AuthChallengeInfo& auth_info,
518 const AuthCallback& callback,
519 AuthCredentials* credentials) {
520 TestNetworkDelegate::OnAuthRequired(request, auth_info, callback,
521 credentials);
522 // Check that the user has provided callback for the previous blocked stage.
523 EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_);
524
525 if ((block_on_ & ON_AUTH_REQUIRED) == 0) {
526 return AUTH_REQUIRED_RESPONSE_NO_ACTION;
527 }
528
529 target_auth_credentials_ = credentials;
530
531 switch (block_mode_) {
532 case SYNCHRONOUS:
533 if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH)
534 *target_auth_credentials_ = auth_credentials_;
535 return auth_retval_;
536
537 case AUTO_CALLBACK:
[email protected]2da659e2013-05-23 20:51:34538 base::MessageLoop::current()->PostTask(
[email protected]dc5a5cf2012-09-26 02:49:30539 FROM_HERE,
540 base::Bind(&BlockingNetworkDelegate::RunAuthCallback,
541 weak_factory_.GetWeakPtr(), auth_retval_, callback));
542 return AUTH_REQUIRED_RESPONSE_IO_PENDING;
543
544 case USER_CALLBACK:
545 auth_callback_ = callback;
546 stage_blocked_for_callback_ = ON_AUTH_REQUIRED;
[email protected]2da659e2013-05-23 20:51:34547 base::MessageLoop::current()->PostTask(FROM_HERE,
548 base::MessageLoop::QuitClosure());
[email protected]dc5a5cf2012-09-26 02:49:30549 return AUTH_REQUIRED_RESPONSE_IO_PENDING;
550 }
551 NOTREACHED();
552 return AUTH_REQUIRED_RESPONSE_NO_ACTION; // Dummy value.
553}
554
555void BlockingNetworkDelegate::Reset() {
556 EXPECT_NE(NOT_BLOCKED, stage_blocked_for_callback_);
557 stage_blocked_for_callback_ = NOT_BLOCKED;
558 callback_.Reset();
559 auth_callback_.Reset();
560}
561
562int BlockingNetworkDelegate::MaybeBlockStage(
563 BlockingNetworkDelegate::Stage stage,
564 const CompletionCallback& callback) {
565 // Check that the user has provided callback for the previous blocked stage.
566 EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_);
567
568 if ((block_on_ & stage) == 0) {
569 return OK;
570 }
571
572 switch (block_mode_) {
573 case SYNCHRONOUS:
574 EXPECT_NE(OK, retval_);
575 return retval_;
576
577 case AUTO_CALLBACK:
[email protected]2da659e2013-05-23 20:51:34578 base::MessageLoop::current()->PostTask(
[email protected]dc5a5cf2012-09-26 02:49:30579 FROM_HERE,
580 base::Bind(&BlockingNetworkDelegate::RunCallback,
581 weak_factory_.GetWeakPtr(), retval_, callback));
582 return ERR_IO_PENDING;
583
584 case USER_CALLBACK:
585 callback_ = callback;
586 stage_blocked_for_callback_ = stage;
[email protected]2da659e2013-05-23 20:51:34587 base::MessageLoop::current()->PostTask(FROM_HERE,
588 base::MessageLoop::QuitClosure());
[email protected]dc5a5cf2012-09-26 02:49:30589 return ERR_IO_PENDING;
590 }
591 NOTREACHED();
592 return 0;
593}
594
[email protected]d5a4dd62012-05-23 01:41:04595class TestURLRequestContextWithProxy : public TestURLRequestContext {
596 public:
597 // Does not own |delegate|.
598 TestURLRequestContextWithProxy(const std::string& proxy,
599 NetworkDelegate* delegate)
600 : TestURLRequestContext(true) {
601 context_storage_.set_proxy_service(ProxyService::CreateFixed(proxy));
602 set_network_delegate(delegate);
603 Init();
604 }
dchengb03027d2014-10-21 12:00:20605 ~TestURLRequestContextWithProxy() override {}
[email protected]d5a4dd62012-05-23 01:41:04606};
607
608} // namespace
609
[email protected]a592c0432012-12-01 18:10:29610// Inherit PlatformTest since we require the autorelease pool on Mac OS X.
[email protected]7a0bb4bf2008-11-19 21:41:48611class URLRequestTest : public PlatformTest {
[email protected]abb26092010-11-11 22:19:00612 public:
[email protected]ef2bf422012-05-11 03:27:09613 URLRequestTest() : default_context_(true) {
614 default_context_.set_network_delegate(&default_network_delegate_);
[email protected]58e32bb2013-01-21 18:23:25615 default_context_.set_net_log(&net_log_);
bengr1bf8e942014-11-07 01:36:50616 job_factory_impl_ = new URLRequestJobFactoryImpl();
617 job_factory_.reset(job_factory_impl_);
[email protected]87a09a92011-07-14 15:50:50618 }
bengr1bf8e942014-11-07 01:36:50619
dcheng67be2b1f2014-10-27 21:47:29620 ~URLRequestTest() override {
[email protected]e4034ad2013-09-20 08:36:18621 // URLRequestJobs may post clean-up tasks on destruction.
622 base::RunLoop().RunUntilIdle();
623 }
[email protected]87a09a92011-07-14 15:50:50624
dcheng2339883c2014-12-23 00:23:05625 void SetUp() override {
bengr1bf8e942014-11-07 01:36:50626 SetUpFactory();
627 default_context_.set_job_factory(job_factory_.get());
628 default_context_.Init();
629 PlatformTest::SetUp();
630 }
631
632 virtual void SetUpFactory() {
633 job_factory_impl_->SetProtocolHandler("data", new DataProtocolHandler);
634#if !defined(DISABLE_FILE_SUPPORT)
635 job_factory_impl_->SetProtocolHandler(
636 "file", new FileProtocolHandler(base::MessageLoopProxy::current()));
637#endif
638 }
639
640 TestNetworkDelegate* default_network_delegate() {
641 return &default_network_delegate_;
642 }
643
644 const TestURLRequestContext& default_context() const {
645 return default_context_;
646 }
647
648
[email protected]3c5ca8c2011-09-29 01:14:51649 // Adds the TestJobInterceptor to the default context.
650 TestJobInterceptor* AddTestInterceptor() {
[email protected]f53b4802012-12-20 17:04:23651 TestJobInterceptor* protocol_handler_ = new TestJobInterceptor();
bengr1bf8e942014-11-07 01:36:50652 job_factory_impl_->SetProtocolHandler("http", NULL);
653 job_factory_impl_->SetProtocolHandler("http", protocol_handler_);
[email protected]f53b4802012-12-20 17:04:23654 return protocol_handler_;
[email protected]3c5ca8c2011-09-29 01:14:51655 }
656
[email protected]87a09a92011-07-14 15:50:50657 protected:
vishal.b62985ca92015-04-17 08:45:51658 TestNetLog net_log_;
[email protected]ceefd7fd2012-11-29 00:36:24659 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest.
bengr1bf8e942014-11-07 01:36:50660 URLRequestJobFactoryImpl* job_factory_impl_;
661 scoped_ptr<URLRequestJobFactory> job_factory_;
[email protected]ef2bf422012-05-11 03:27:09662 TestURLRequestContext default_context_;
[email protected]7a0bb4bf2008-11-19 21:41:48663};
664
[email protected]316c1e5e2012-09-12 15:17:44665TEST_F(URLRequestTest, AboutBlankTest) {
666 TestDelegate d;
667 {
[email protected]f7022f32014-08-21 16:32:19668 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:36669 GURL("about:blank"), DEFAULT_PRIORITY, &d));
[email protected]316c1e5e2012-09-12 15:17:44670
[email protected]f7022f32014-08-21 16:32:19671 r->Start();
672 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44673
[email protected]255620da2013-08-19 13:14:29674 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:44675
[email protected]f7022f32014-08-21 16:32:19676 EXPECT_TRUE(!r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44677 EXPECT_FALSE(d.received_data_before_response());
678 EXPECT_EQ(d.bytes_received(), 0);
[email protected]f7022f32014-08-21 16:32:19679 EXPECT_EQ("", r->GetSocketAddress().host());
680 EXPECT_EQ(0, r->GetSocketAddress().port());
[email protected]79e1fd62013-06-20 06:50:04681
682 HttpRequestHeaders headers;
[email protected]f7022f32014-08-21 16:32:19683 EXPECT_FALSE(r->GetFullRequestHeaders(&headers));
[email protected]316c1e5e2012-09-12 15:17:44684 }
685}
686
687TEST_F(URLRequestTest, DataURLImageTest) {
688 TestDelegate d;
689 {
690 // Use our nice little Chrome logo.
[email protected]f7022f32014-08-21 16:32:19691 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
[email protected]2ca01e52013-10-31 22:05:19692 GURL(
davidben151423e2015-03-23 18:48:36693 "data:image/png;base64,"
694 "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUB"
695 "BjG3w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQ"
696 "xIEVxitD5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI"
697 "5MpCVdW0gO7tvNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKz"
698 "Mx1+fg9bAgK6zHq9cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7Ck"
699 "IMDxQpF7r/MWq12UctI1dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0"
700 "WYUqt57yWf2McHTObYPbVD+ZwbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2H"
701 "QQcm43XwmtoYM6vVKleh0uoWvnzW3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nk"
702 "DFHF9ZS+uYVjRUasMeHUmyLYtcklTvzWGFZnNOXczThvpKIzjcahSqIzkvDLayDq6"
703 "D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTbxXX1T5xsV9tm9r4TQwHLiZw/pdDZJ"
704 "ea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5MrVvMzNaI3+ERHfrFzPKQukrQ"
705 "GI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gVMd6q5c8GdosynKmSeRuGz"
706 "pjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq/uxxZKzNiZFGD0wRC"
707 "3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7FUx1qLnV7MGF40"
708 "smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufBJbsysjjW4"
709 "kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJMnlm2O"
710 "34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfmm"
711 "oRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV"
712 "5EnhORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="),
713 DEFAULT_PRIORITY, &d));
[email protected]316c1e5e2012-09-12 15:17:44714
[email protected]f7022f32014-08-21 16:32:19715 r->Start();
716 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44717
[email protected]255620da2013-08-19 13:14:29718 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:44719
[email protected]f7022f32014-08-21 16:32:19720 EXPECT_TRUE(!r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44721 EXPECT_FALSE(d.received_data_before_response());
722 EXPECT_EQ(d.bytes_received(), 911);
[email protected]f7022f32014-08-21 16:32:19723 EXPECT_EQ("", r->GetSocketAddress().host());
724 EXPECT_EQ(0, r->GetSocketAddress().port());
[email protected]79e1fd62013-06-20 06:50:04725
726 HttpRequestHeaders headers;
[email protected]f7022f32014-08-21 16:32:19727 EXPECT_FALSE(r->GetFullRequestHeaders(&headers));
[email protected]316c1e5e2012-09-12 15:17:44728 }
729}
730
[email protected]5ecf7cb282014-05-11 01:49:55731#if !defined(DISABLE_FILE_SUPPORT)
[email protected]316c1e5e2012-09-12 15:17:44732TEST_F(URLRequestTest, FileTest) {
[email protected]6cdfd7f2013-02-08 20:40:15733 base::FilePath app_path;
[email protected]316c1e5e2012-09-12 15:17:44734 PathService::Get(base::FILE_EXE, &app_path);
735 GURL app_url = FilePathToFileURL(app_path);
736
737 TestDelegate d;
738 {
davidben151423e2015-03-23 18:48:36739 scoped_ptr<URLRequest> r(
740 default_context_.CreateRequest(app_url, DEFAULT_PRIORITY, &d));
[email protected]316c1e5e2012-09-12 15:17:44741
[email protected]f7022f32014-08-21 16:32:19742 r->Start();
743 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44744
[email protected]255620da2013-08-19 13:14:29745 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:44746
747 int64 file_size = -1;
[email protected]56285702013-12-04 18:22:49748 EXPECT_TRUE(base::GetFileSize(app_path, &file_size));
[email protected]316c1e5e2012-09-12 15:17:44749
[email protected]f7022f32014-08-21 16:32:19750 EXPECT_TRUE(!r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44751 EXPECT_EQ(1, d.response_started_count());
752 EXPECT_FALSE(d.received_data_before_response());
753 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
[email protected]f7022f32014-08-21 16:32:19754 EXPECT_EQ("", r->GetSocketAddress().host());
755 EXPECT_EQ(0, r->GetSocketAddress().port());
[email protected]79e1fd62013-06-20 06:50:04756
757 HttpRequestHeaders headers;
[email protected]f7022f32014-08-21 16:32:19758 EXPECT_FALSE(r->GetFullRequestHeaders(&headers));
[email protected]316c1e5e2012-09-12 15:17:44759 }
760}
761
[email protected]ba40bb762012-12-17 07:11:04762TEST_F(URLRequestTest, FileTestCancel) {
[email protected]6cdfd7f2013-02-08 20:40:15763 base::FilePath app_path;
[email protected]ba40bb762012-12-17 07:11:04764 PathService::Get(base::FILE_EXE, &app_path);
765 GURL app_url = FilePathToFileURL(app_path);
766
767 TestDelegate d;
768 {
davidben151423e2015-03-23 18:48:36769 scoped_ptr<URLRequest> r(
770 default_context_.CreateRequest(app_url, DEFAULT_PRIORITY, &d));
[email protected]ba40bb762012-12-17 07:11:04771
[email protected]f7022f32014-08-21 16:32:19772 r->Start();
773 EXPECT_TRUE(r->is_pending());
774 r->Cancel();
[email protected]ba40bb762012-12-17 07:11:04775 }
[email protected]79e1fd62013-06-20 06:50:04776 // Async cancellation should be safe even when URLRequest has been already
[email protected]ba40bb762012-12-17 07:11:04777 // destroyed.
[email protected]255620da2013-08-19 13:14:29778 base::RunLoop().RunUntilIdle();
[email protected]ba40bb762012-12-17 07:11:04779}
780
[email protected]316c1e5e2012-09-12 15:17:44781TEST_F(URLRequestTest, FileTestFullSpecifiedRange) {
782 const size_t buffer_size = 4000;
[email protected]4356f0f2013-04-07 00:58:17783 scoped_ptr<char[]> buffer(new char[buffer_size]);
[email protected]316c1e5e2012-09-12 15:17:44784 FillBuffer(buffer.get(), buffer_size);
785
[email protected]6cdfd7f2013-02-08 20:40:15786 base::FilePath temp_path;
[email protected]03d9afc02013-12-03 17:55:52787 EXPECT_TRUE(base::CreateTemporaryFile(&temp_path));
[email protected]316c1e5e2012-09-12 15:17:44788 GURL temp_url = FilePathToFileURL(temp_path);
[email protected]e5c2a22e2014-03-06 20:42:30789 EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size));
[email protected]316c1e5e2012-09-12 15:17:44790
791 int64 file_size;
[email protected]56285702013-12-04 18:22:49792 EXPECT_TRUE(base::GetFileSize(temp_path, &file_size));
[email protected]316c1e5e2012-09-12 15:17:44793
794 const size_t first_byte_position = 500;
795 const size_t last_byte_position = buffer_size - first_byte_position;
796 const size_t content_length = last_byte_position - first_byte_position + 1;
797 std::string partial_buffer_string(buffer.get() + first_byte_position,
798 buffer.get() + last_byte_position + 1);
799
800 TestDelegate d;
801 {
davidben151423e2015-03-23 18:48:36802 scoped_ptr<URLRequest> r(
803 default_context_.CreateRequest(temp_url, DEFAULT_PRIORITY, &d));
[email protected]316c1e5e2012-09-12 15:17:44804
805 HttpRequestHeaders headers;
[email protected]b7572ea2013-11-26 20:16:38806 headers.SetHeader(
807 HttpRequestHeaders::kRange,
[email protected]cba24642014-08-15 20:49:59808 HttpByteRange::Bounded(
[email protected]b7572ea2013-11-26 20:16:38809 first_byte_position, last_byte_position).GetHeaderValue());
[email protected]f7022f32014-08-21 16:32:19810 r->SetExtraRequestHeaders(headers);
811 r->Start();
812 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44813
[email protected]255620da2013-08-19 13:14:29814 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:19815 EXPECT_TRUE(!r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44816 EXPECT_EQ(1, d.response_started_count());
817 EXPECT_FALSE(d.received_data_before_response());
818 EXPECT_EQ(static_cast<int>(content_length), d.bytes_received());
819 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
820 EXPECT_TRUE(partial_buffer_string == d.data_received());
821 }
822
[email protected]dd3aa792013-07-16 19:10:23823 EXPECT_TRUE(base::DeleteFile(temp_path, false));
[email protected]316c1e5e2012-09-12 15:17:44824}
825
826TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) {
827 const size_t buffer_size = 4000;
[email protected]4356f0f2013-04-07 00:58:17828 scoped_ptr<char[]> buffer(new char[buffer_size]);
[email protected]316c1e5e2012-09-12 15:17:44829 FillBuffer(buffer.get(), buffer_size);
830
[email protected]6cdfd7f2013-02-08 20:40:15831 base::FilePath temp_path;
[email protected]03d9afc02013-12-03 17:55:52832 EXPECT_TRUE(base::CreateTemporaryFile(&temp_path));
[email protected]316c1e5e2012-09-12 15:17:44833 GURL temp_url = FilePathToFileURL(temp_path);
[email protected]e5c2a22e2014-03-06 20:42:30834 EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size));
[email protected]316c1e5e2012-09-12 15:17:44835
836 int64 file_size;
[email protected]56285702013-12-04 18:22:49837 EXPECT_TRUE(base::GetFileSize(temp_path, &file_size));
[email protected]316c1e5e2012-09-12 15:17:44838
839 const size_t first_byte_position = 500;
840 const size_t last_byte_position = buffer_size - 1;
841 const size_t content_length = last_byte_position - first_byte_position + 1;
842 std::string partial_buffer_string(buffer.get() + first_byte_position,
843 buffer.get() + last_byte_position + 1);
844
845 TestDelegate d;
846 {
davidben151423e2015-03-23 18:48:36847 scoped_ptr<URLRequest> r(
848 default_context_.CreateRequest(temp_url, DEFAULT_PRIORITY, &d));
[email protected]316c1e5e2012-09-12 15:17:44849
850 HttpRequestHeaders headers;
851 headers.SetHeader(HttpRequestHeaders::kRange,
[email protected]cba24642014-08-15 20:49:59852 HttpByteRange::RightUnbounded(
[email protected]b7572ea2013-11-26 20:16:38853 first_byte_position).GetHeaderValue());
[email protected]f7022f32014-08-21 16:32:19854 r->SetExtraRequestHeaders(headers);
855 r->Start();
856 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44857
[email protected]255620da2013-08-19 13:14:29858 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:19859 EXPECT_TRUE(!r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44860 EXPECT_EQ(1, d.response_started_count());
861 EXPECT_FALSE(d.received_data_before_response());
862 EXPECT_EQ(static_cast<int>(content_length), d.bytes_received());
863 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
864 EXPECT_TRUE(partial_buffer_string == d.data_received());
865 }
866
[email protected]dd3aa792013-07-16 19:10:23867 EXPECT_TRUE(base::DeleteFile(temp_path, false));
[email protected]316c1e5e2012-09-12 15:17:44868}
869
870TEST_F(URLRequestTest, FileTestMultipleRanges) {
871 const size_t buffer_size = 400000;
[email protected]4356f0f2013-04-07 00:58:17872 scoped_ptr<char[]> buffer(new char[buffer_size]);
[email protected]316c1e5e2012-09-12 15:17:44873 FillBuffer(buffer.get(), buffer_size);
874
[email protected]6cdfd7f2013-02-08 20:40:15875 base::FilePath temp_path;
[email protected]03d9afc02013-12-03 17:55:52876 EXPECT_TRUE(base::CreateTemporaryFile(&temp_path));
[email protected]316c1e5e2012-09-12 15:17:44877 GURL temp_url = FilePathToFileURL(temp_path);
[email protected]e5c2a22e2014-03-06 20:42:30878 EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size));
[email protected]316c1e5e2012-09-12 15:17:44879
880 int64 file_size;
[email protected]56285702013-12-04 18:22:49881 EXPECT_TRUE(base::GetFileSize(temp_path, &file_size));
[email protected]316c1e5e2012-09-12 15:17:44882
883 TestDelegate d;
884 {
davidben151423e2015-03-23 18:48:36885 scoped_ptr<URLRequest> r(
886 default_context_.CreateRequest(temp_url, DEFAULT_PRIORITY, &d));
[email protected]316c1e5e2012-09-12 15:17:44887
888 HttpRequestHeaders headers;
[email protected]b7572ea2013-11-26 20:16:38889 headers.SetHeader(HttpRequestHeaders::kRange, "bytes=0-0,10-200,200-300");
[email protected]f7022f32014-08-21 16:32:19890 r->SetExtraRequestHeaders(headers);
891 r->Start();
892 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44893
[email protected]255620da2013-08-19 13:14:29894 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:44895 EXPECT_TRUE(d.request_failed());
896 }
897
[email protected]dd3aa792013-07-16 19:10:23898 EXPECT_TRUE(base::DeleteFile(temp_path, false));
[email protected]316c1e5e2012-09-12 15:17:44899}
900
[email protected]3ca8b362013-11-11 22:18:07901TEST_F(URLRequestTest, AllowFileURLs) {
902 base::ScopedTempDir temp_dir;
903 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
904 base::FilePath test_file;
[email protected]03d9afc02013-12-03 17:55:52905 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &test_file));
[email protected]3ca8b362013-11-11 22:18:07906 std::string test_data("monkey");
[email protected]e5c2a22e2014-03-06 20:42:30907 base::WriteFile(test_file, test_data.data(), test_data.size());
[email protected]cba24642014-08-15 20:49:59908 GURL test_file_url = FilePathToFileURL(test_file);
[email protected]3ca8b362013-11-11 22:18:07909
910 {
911 TestDelegate d;
912 TestNetworkDelegate network_delegate;
913 network_delegate.set_can_access_files(true);
914 default_context_.set_network_delegate(&network_delegate);
davidben151423e2015-03-23 18:48:36915 scoped_ptr<URLRequest> r(
916 default_context_.CreateRequest(test_file_url, DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:19917 r->Start();
[email protected]3ca8b362013-11-11 22:18:07918 base::RunLoop().Run();
919 EXPECT_FALSE(d.request_failed());
920 EXPECT_EQ(test_data, d.data_received());
921 }
922
923 {
924 TestDelegate d;
925 TestNetworkDelegate network_delegate;
926 network_delegate.set_can_access_files(false);
927 default_context_.set_network_delegate(&network_delegate);
davidben151423e2015-03-23 18:48:36928 scoped_ptr<URLRequest> r(
929 default_context_.CreateRequest(test_file_url, DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:19930 r->Start();
[email protected]3ca8b362013-11-11 22:18:07931 base::RunLoop().Run();
932 EXPECT_TRUE(d.request_failed());
933 EXPECT_EQ("", d.data_received());
934 }
935}
936
[email protected]316c1e5e2012-09-12 15:17:44937
938TEST_F(URLRequestTest, FileDirCancelTest) {
939 // Put in mock resource provider.
940 NetModule::SetResourceProvider(TestNetResourceProvider);
941
942 TestDelegate d;
943 {
[email protected]6cdfd7f2013-02-08 20:40:15944 base::FilePath file_path;
[email protected]316c1e5e2012-09-12 15:17:44945 PathService::Get(base::DIR_SOURCE_ROOT, &file_path);
946 file_path = file_path.Append(FILE_PATH_LITERAL("net"));
947 file_path = file_path.Append(FILE_PATH_LITERAL("data"));
948
[email protected]f7022f32014-08-21 16:32:19949 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:36950 FilePathToFileURL(file_path), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:19951 req->Start();
952 EXPECT_TRUE(req->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44953
954 d.set_cancel_in_received_data_pending(true);
955
[email protected]255620da2013-08-19 13:14:29956 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:44957 }
958
959 // Take out mock resource provider.
960 NetModule::SetResourceProvider(NULL);
961}
962
[email protected]5f9581402013-10-30 13:08:32963TEST_F(URLRequestTest, FileDirOutputSanity) {
964 // Verify the general sanity of the the output of the file:
965 // directory lister by checking for the output of a known existing
966 // file.
967 const char sentinel_name[] = "filedir-sentinel";
968
969 base::FilePath path;
970 PathService::Get(base::DIR_SOURCE_ROOT, &path);
971 path = path.Append(FILE_PATH_LITERAL("net"));
972 path = path.Append(FILE_PATH_LITERAL("data"));
973 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
974
975 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:19976 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:36977 FilePathToFileURL(path), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:19978 req->Start();
[email protected]5f9581402013-10-30 13:08:32979 base::RunLoop().Run();
980
981 // Generate entry for the sentinel file.
982 base::FilePath sentinel_path = path.AppendASCII(sentinel_name);
[email protected]54124ed02014-01-07 10:06:58983 base::File::Info info;
[email protected]9eae4e62013-12-04 20:56:49984 EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info));
[email protected]5f9581402013-10-30 13:08:32985 EXPECT_GT(info.size, 0);
986 std::string sentinel_output = GetDirectoryListingEntry(
987 base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)),
988 std::string(sentinel_name),
989 false /* is_dir */,
990 info.size,
991 info.last_modified);
992
993 ASSERT_LT(0, d.bytes_received());
994 ASSERT_FALSE(d.request_failed());
[email protected]f7022f32014-08-21 16:32:19995 ASSERT_TRUE(req->status().is_success());
[email protected]5f9581402013-10-30 13:08:32996 // Check for the entry generated for the "sentinel" file.
997 const std::string& data = d.data_received();
998 ASSERT_NE(data.find(sentinel_output), std::string::npos);
999}
1000
[email protected]316c1e5e2012-09-12 15:17:441001TEST_F(URLRequestTest, FileDirRedirectNoCrash) {
1002 // There is an implicit redirect when loading a file path that matches a
1003 // directory and does not end with a slash. Ensure that following such
1004 // redirects does not crash. See http://crbug.com/18686.
1005
[email protected]6cdfd7f2013-02-08 20:40:151006 base::FilePath path;
[email protected]316c1e5e2012-09-12 15:17:441007 PathService::Get(base::DIR_SOURCE_ROOT, &path);
1008 path = path.Append(FILE_PATH_LITERAL("net"));
1009 path = path.Append(FILE_PATH_LITERAL("data"));
1010 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
1011
1012 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:191013 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:361014 FilePathToFileURL(path), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:191015 req->Start();
[email protected]255620da2013-08-19 13:14:291016 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441017
1018 ASSERT_EQ(1, d.received_redirect_count());
1019 ASSERT_LT(0, d.bytes_received());
1020 ASSERT_FALSE(d.request_failed());
[email protected]f7022f32014-08-21 16:32:191021 ASSERT_TRUE(req->status().is_success());
[email protected]316c1e5e2012-09-12 15:17:441022}
1023
1024#if defined(OS_WIN)
1025// Don't accept the url "file:///" on windows. See http://crbug.com/1474.
1026TEST_F(URLRequestTest, FileDirRedirectSingleSlash) {
1027 TestDelegate d;
davidben151423e2015-03-23 18:48:361028 scoped_ptr<URLRequest> req(
1029 default_context_.CreateRequest(GURL("file:///"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:191030 req->Start();
[email protected]255620da2013-08-19 13:14:291031 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441032
1033 ASSERT_EQ(1, d.received_redirect_count());
[email protected]f7022f32014-08-21 16:32:191034 ASSERT_FALSE(req->status().is_success());
[email protected]316c1e5e2012-09-12 15:17:441035}
[email protected]5ecf7cb282014-05-11 01:49:551036#endif // defined(OS_WIN)
1037
1038#endif // !defined(DISABLE_FILE_SUPPORT)
1039
1040TEST_F(URLRequestTest, InvalidUrlTest) {
1041 TestDelegate d;
1042 {
[email protected]f7022f32014-08-21 16:32:191043 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:361044 GURL("invalid url"), DEFAULT_PRIORITY, &d));
[email protected]5ecf7cb282014-05-11 01:49:551045
[email protected]f7022f32014-08-21 16:32:191046 r->Start();
1047 EXPECT_TRUE(r->is_pending());
[email protected]5ecf7cb282014-05-11 01:49:551048
1049 base::RunLoop().Run();
1050 EXPECT_TRUE(d.request_failed());
1051 }
1052}
1053
jochen0e3b3a62014-09-16 18:31:231054TEST_F(URLRequestTest, InvalidReferrerTest) {
1055 TestURLRequestContext context;
1056 TestNetworkDelegate network_delegate;
1057 network_delegate.set_cancel_request_with_policy_violating_referrer(true);
1058 context.set_network_delegate(&network_delegate);
1059 TestDelegate d;
davidben151423e2015-03-23 18:48:361060 scoped_ptr<URLRequest> req(
1061 context.CreateRequest(GURL("http://localhost/"), DEFAULT_PRIORITY, &d));
jochen0e3b3a62014-09-16 18:31:231062 req->SetReferrer("https://somewhere.com/");
1063
1064 req->Start();
1065 base::RunLoop().Run();
1066 EXPECT_TRUE(d.request_failed());
1067}
1068
[email protected]5ecf7cb282014-05-11 01:49:551069#if defined(OS_WIN)
1070TEST_F(URLRequestTest, ResolveShortcutTest) {
1071 base::FilePath app_path;
1072 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
1073 app_path = app_path.AppendASCII("net");
1074 app_path = app_path.AppendASCII("data");
1075 app_path = app_path.AppendASCII("url_request_unittest");
1076 app_path = app_path.AppendASCII("with-headers.html");
1077
1078 std::wstring lnk_path = app_path.value() + L".lnk";
1079
1080 base::win::ScopedCOMInitializer com_initializer;
1081
1082 // Temporarily create a shortcut for test
1083 {
1084 base::win::ScopedComPtr<IShellLink> shell;
1085 ASSERT_TRUE(SUCCEEDED(shell.CreateInstance(CLSID_ShellLink, NULL,
1086 CLSCTX_INPROC_SERVER)));
1087 base::win::ScopedComPtr<IPersistFile> persist;
1088 ASSERT_TRUE(SUCCEEDED(shell.QueryInterface(persist.Receive())));
1089 EXPECT_TRUE(SUCCEEDED(shell->SetPath(app_path.value().c_str())));
1090 EXPECT_TRUE(SUCCEEDED(shell->SetDescription(L"ResolveShortcutTest")));
1091 EXPECT_TRUE(SUCCEEDED(persist->Save(lnk_path.c_str(), TRUE)));
1092 }
1093
1094 TestDelegate d;
1095 {
[email protected]f7022f32014-08-21 16:32:191096 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:361097 FilePathToFileURL(base::FilePath(lnk_path)), DEFAULT_PRIORITY, &d));
[email protected]5ecf7cb282014-05-11 01:49:551098
[email protected]f7022f32014-08-21 16:32:191099 r->Start();
1100 EXPECT_TRUE(r->is_pending());
[email protected]5ecf7cb282014-05-11 01:49:551101
1102 base::RunLoop().Run();
1103
1104 WIN32_FILE_ATTRIBUTE_DATA data;
1105 GetFileAttributesEx(app_path.value().c_str(),
1106 GetFileExInfoStandard, &data);
1107 HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ,
1108 FILE_SHARE_READ, NULL, OPEN_EXISTING,
1109 FILE_ATTRIBUTE_NORMAL, NULL);
1110 EXPECT_NE(INVALID_HANDLE_VALUE, file);
1111 scoped_ptr<char[]> buffer(new char[data.nFileSizeLow]);
1112 DWORD read_size;
1113 BOOL result;
1114 result = ReadFile(file, buffer.get(), data.nFileSizeLow,
1115 &read_size, NULL);
1116 std::string content(buffer.get(), read_size);
1117 CloseHandle(file);
1118
[email protected]f7022f32014-08-21 16:32:191119 EXPECT_TRUE(!r->is_pending());
[email protected]5ecf7cb282014-05-11 01:49:551120 EXPECT_EQ(1, d.received_redirect_count());
1121 EXPECT_EQ(content, d.data_received());
1122 }
1123
1124 // Clean the shortcut
1125 DeleteFile(lnk_path.c_str());
1126}
1127#endif // defined(OS_WIN)
[email protected]316c1e5e2012-09-12 15:17:441128
1129// Custom URLRequestJobs for use with interceptor tests
1130class RestartTestJob : public URLRequestTestJob {
1131 public:
1132 RestartTestJob(URLRequest* request, NetworkDelegate* network_delegate)
1133 : URLRequestTestJob(request, network_delegate, true) {}
1134 protected:
dchengb03027d2014-10-21 12:00:201135 void StartAsync() override { this->NotifyRestartRequired(); }
[email protected]316c1e5e2012-09-12 15:17:441136 private:
dchengb03027d2014-10-21 12:00:201137 ~RestartTestJob() override {}
[email protected]316c1e5e2012-09-12 15:17:441138};
1139
1140class CancelTestJob : public URLRequestTestJob {
1141 public:
1142 explicit CancelTestJob(URLRequest* request, NetworkDelegate* network_delegate)
1143 : URLRequestTestJob(request, network_delegate, true) {}
1144 protected:
dchengb03027d2014-10-21 12:00:201145 void StartAsync() override { request_->Cancel(); }
[email protected]316c1e5e2012-09-12 15:17:441146 private:
dchengb03027d2014-10-21 12:00:201147 ~CancelTestJob() override {}
[email protected]316c1e5e2012-09-12 15:17:441148};
1149
1150class CancelThenRestartTestJob : public URLRequestTestJob {
1151 public:
1152 explicit CancelThenRestartTestJob(URLRequest* request,
1153 NetworkDelegate* network_delegate)
1154 : URLRequestTestJob(request, network_delegate, true) {
1155 }
1156 protected:
dchengb03027d2014-10-21 12:00:201157 void StartAsync() override {
[email protected]316c1e5e2012-09-12 15:17:441158 request_->Cancel();
1159 this->NotifyRestartRequired();
1160 }
1161 private:
dchengb03027d2014-10-21 12:00:201162 ~CancelThenRestartTestJob() override {}
[email protected]316c1e5e2012-09-12 15:17:441163};
1164
bengr1bf8e942014-11-07 01:36:501165// An Interceptor for use with interceptor tests.
1166class MockURLRequestInterceptor : public URLRequestInterceptor {
1167 public:
1168 // Static getters for canned response header and data strings.
1169 static std::string ok_data() {
1170 return URLRequestTestJob::test_data_1();
1171 }
1172
1173 static std::string ok_headers() {
1174 return URLRequestTestJob::test_headers();
1175 }
1176
1177 static std::string redirect_data() {
1178 return std::string();
1179 }
1180
1181 static std::string redirect_headers() {
1182 return URLRequestTestJob::test_redirect_headers();
1183 }
1184
1185 static std::string error_data() {
1186 return std::string("ohhh nooooo mr. bill!");
1187 }
1188
1189 static std::string error_headers() {
1190 return URLRequestTestJob::test_error_headers();
1191 }
1192
1193 MockURLRequestInterceptor()
1194 : intercept_main_request_(false), restart_main_request_(false),
1195 cancel_main_request_(false), cancel_then_restart_main_request_(false),
1196 simulate_main_network_error_(false),
1197 intercept_redirect_(false), cancel_redirect_request_(false),
1198 intercept_final_response_(false), cancel_final_request_(false),
1199 use_url_request_http_job_(false),
1200 did_intercept_main_(false), did_restart_main_(false),
1201 did_cancel_main_(false), did_cancel_then_restart_main_(false),
1202 did_simulate_error_main_(false),
1203 did_intercept_redirect_(false), did_cancel_redirect_(false),
1204 did_intercept_final_(false), did_cancel_final_(false) {
1205 }
1206
1207 ~MockURLRequestInterceptor() override {
1208 }
1209
1210 // URLRequestInterceptor implementation:
1211 URLRequestJob* MaybeInterceptRequest(
1212 URLRequest* request,
1213 NetworkDelegate* network_delegate) const override {
1214 if (restart_main_request_) {
1215 restart_main_request_ = false;
1216 did_restart_main_ = true;
1217 return new RestartTestJob(request, network_delegate);
1218 }
1219 if (cancel_main_request_) {
1220 cancel_main_request_ = false;
1221 did_cancel_main_ = true;
1222 return new CancelTestJob(request, network_delegate);
1223 }
1224 if (cancel_then_restart_main_request_) {
1225 cancel_then_restart_main_request_ = false;
1226 did_cancel_then_restart_main_ = true;
1227 return new CancelThenRestartTestJob(request, network_delegate);
1228 }
1229 if (simulate_main_network_error_) {
1230 simulate_main_network_error_ = false;
1231 did_simulate_error_main_ = true;
1232 if (use_url_request_http_job_) {
1233 return URLRequestHttpJob::Factory(request, network_delegate, "http");
1234 }
1235 // This job will result in error since the requested URL is not one of the
1236 // URLs supported by these tests.
1237 return new URLRequestTestJob(request, network_delegate, true);
1238 }
1239 if (!intercept_main_request_)
1240 return nullptr;
1241 intercept_main_request_ = false;
1242 did_intercept_main_ = true;
1243 URLRequestTestJob* job = new URLRequestTestJob(request,
1244 network_delegate,
1245 main_headers_,
1246 main_data_,
1247 true);
1248 job->set_load_timing_info(main_request_load_timing_info_);
1249 return job;
1250 }
1251
1252 URLRequestJob* MaybeInterceptRedirect(URLRequest* request,
1253 NetworkDelegate* network_delegate,
1254 const GURL& location) const override {
1255 if (cancel_redirect_request_) {
1256 cancel_redirect_request_ = false;
1257 did_cancel_redirect_ = true;
1258 return new CancelTestJob(request, network_delegate);
1259 }
1260 if (!intercept_redirect_)
1261 return nullptr;
1262 intercept_redirect_ = false;
1263 did_intercept_redirect_ = true;
1264 if (use_url_request_http_job_) {
1265 return URLRequestHttpJob::Factory(request, network_delegate, "http");
1266 }
1267 return new URLRequestTestJob(request,
1268 network_delegate,
1269 redirect_headers_,
1270 redirect_data_,
1271 true);
1272 }
1273
1274 URLRequestJob* MaybeInterceptResponse(
1275 URLRequest* request,
1276 NetworkDelegate* network_delegate) const override {
1277 if (cancel_final_request_) {
1278 cancel_final_request_ = false;
1279 did_cancel_final_ = true;
1280 return new CancelTestJob(request, network_delegate);
1281 }
1282 if (!intercept_final_response_)
1283 return nullptr;
1284 intercept_final_response_ = false;
1285 did_intercept_final_ = true;
1286 if (use_url_request_http_job_) {
1287 return URLRequestHttpJob::Factory(request, network_delegate, "http");
1288 }
1289 return new URLRequestTestJob(request,
1290 network_delegate,
1291 final_headers_,
1292 final_data_,
1293 true);
1294 }
1295
1296 void set_intercept_main_request(bool intercept_main_request) {
1297 intercept_main_request_ = intercept_main_request;
1298 }
1299
1300 void set_main_headers(const std::string& main_headers) {
1301 main_headers_ = main_headers;
1302 }
1303
1304 void set_main_data(const std::string& main_data) {
1305 main_data_ = main_data;
1306 }
1307
1308 void set_main_request_load_timing_info(
1309 const LoadTimingInfo& main_request_load_timing_info) {
1310 main_request_load_timing_info_ = main_request_load_timing_info;
1311 }
1312
1313 void set_restart_main_request(bool restart_main_request) {
1314 restart_main_request_ = restart_main_request;
1315 }
1316
1317 void set_cancel_main_request(bool cancel_main_request) {
1318 cancel_main_request_ = cancel_main_request;
1319 }
1320
1321 void set_cancel_then_restart_main_request(
1322 bool cancel_then_restart_main_request) {
1323 cancel_then_restart_main_request_ = cancel_then_restart_main_request;
1324 }
1325
1326 void set_simulate_main_network_error(bool simulate_main_network_error) {
1327 simulate_main_network_error_ = simulate_main_network_error;
1328 }
1329
1330 void set_intercept_redirect(bool intercept_redirect) {
1331 intercept_redirect_ = intercept_redirect;
1332 }
1333
1334 void set_redirect_headers(const std::string& redirect_headers) {
1335 redirect_headers_ = redirect_headers;
1336 }
1337
1338 void set_redirect_data(const std::string& redirect_data) {
1339 redirect_data_ = redirect_data;
1340 }
1341
1342 void set_cancel_redirect_request(bool cancel_redirect_request) {
1343 cancel_redirect_request_ = cancel_redirect_request;
1344 }
1345
1346 void set_intercept_final_response(bool intercept_final_response) {
1347 intercept_final_response_ = intercept_final_response;
1348 }
1349
1350 void set_final_headers(const std::string& final_headers) {
1351 final_headers_ = final_headers;
1352 }
1353
1354 void set_final_data(const std::string& final_data) {
1355 final_data_ = final_data;
1356 }
1357
1358 void set_cancel_final_request(bool cancel_final_request) {
1359 cancel_final_request_ = cancel_final_request;
1360 }
1361
1362 void set_use_url_request_http_job(bool use_url_request_http_job) {
1363 use_url_request_http_job_ = use_url_request_http_job;
1364 }
1365
1366 bool did_intercept_main() const {
1367 return did_intercept_main_;
1368 }
1369
1370 bool did_restart_main() const {
1371 return did_restart_main_;
1372 }
1373
1374 bool did_cancel_main() const {
1375 return did_cancel_main_;
1376 }
1377
1378 bool did_cancel_then_restart_main() const {
1379 return did_cancel_then_restart_main_;
1380 }
1381
1382 bool did_simulate_error_main() const {
1383 return did_simulate_error_main_;
1384 }
1385
1386 bool did_intercept_redirect() const {
1387 return did_intercept_redirect_;
1388 }
1389
1390 bool did_cancel_redirect() const {
1391 return did_cancel_redirect_;
1392 }
1393
1394 bool did_intercept_final() const {
1395 return did_intercept_final_;
1396 }
1397
1398 bool did_cancel_final() const {
1399 return did_cancel_final_;
1400 }
1401
1402 private:
1403 // Indicate whether to intercept the main request, and if so specify the
1404 // response to return and the LoadTimingInfo to use.
1405 mutable bool intercept_main_request_;
1406 mutable std::string main_headers_;
1407 mutable std::string main_data_;
1408 mutable LoadTimingInfo main_request_load_timing_info_;
1409
1410 // These indicate actions that can be taken within MaybeInterceptRequest.
1411 mutable bool restart_main_request_;
1412 mutable bool cancel_main_request_;
1413 mutable bool cancel_then_restart_main_request_;
1414 mutable bool simulate_main_network_error_;
1415
1416 // Indicate whether to intercept redirects, and if so specify the response to
1417 // return.
1418 mutable bool intercept_redirect_;
1419 mutable std::string redirect_headers_;
1420 mutable std::string redirect_data_;
1421
1422 // Cancel the request within MaybeInterceptRedirect.
1423 mutable bool cancel_redirect_request_;
1424
1425 // Indicate whether to intercept the final response, and if so specify the
1426 // response to return.
1427 mutable bool intercept_final_response_;
1428 mutable std::string final_headers_;
1429 mutable std::string final_data_;
1430
1431 // Cancel the final request within MaybeInterceptResponse.
1432 mutable bool cancel_final_request_;
1433
1434 // Instruct the interceptor to use a real URLRequestHTTPJob.
1435 mutable bool use_url_request_http_job_;
1436
1437 // These indicate if the interceptor did something or not.
1438 mutable bool did_intercept_main_;
1439 mutable bool did_restart_main_;
1440 mutable bool did_cancel_main_;
1441 mutable bool did_cancel_then_restart_main_;
1442 mutable bool did_simulate_error_main_;
1443 mutable bool did_intercept_redirect_;
1444 mutable bool did_cancel_redirect_;
1445 mutable bool did_intercept_final_;
1446 mutable bool did_cancel_final_;
1447};
1448
1449// Inherit PlatformTest since we require the autorelease pool on Mac OS X.
1450class URLRequestInterceptorTest : public URLRequestTest {
1451 public:
1452 URLRequestInterceptorTest() : URLRequestTest(), interceptor_(NULL) {
1453 }
1454
1455 ~URLRequestInterceptorTest() override {
1456 // URLRequestJobs may post clean-up tasks on destruction.
1457 base::RunLoop().RunUntilIdle();
1458 }
1459
1460 void SetUpFactory() override {
1461 interceptor_ = new MockURLRequestInterceptor();
1462 job_factory_.reset(new URLRequestInterceptingJobFactory(
1463 job_factory_.Pass(), make_scoped_ptr(interceptor_)));
1464 }
1465
1466 MockURLRequestInterceptor* interceptor() const {
1467 return interceptor_;
1468 }
1469
1470 private:
1471 MockURLRequestInterceptor* interceptor_;
1472};
1473
1474TEST_F(URLRequestInterceptorTest, Intercept) {
1475 // Intercept the main request and respond with a simple response.
1476 interceptor()->set_intercept_main_request(true);
1477 interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers());
1478 interceptor()->set_main_data(MockURLRequestInterceptor::ok_data());
[email protected]2bba3252013-04-08 19:50:591479 TestDelegate d;
bengr1bf8e942014-11-07 01:36:501480 scoped_ptr<URLRequest> req(default_context().CreateRequest(
davidben151423e2015-03-23 18:48:361481 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d));
bengr1bf8e942014-11-07 01:36:501482 base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data();
1483 base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data();
1484 base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data();
1485 req->SetUserData(nullptr, user_data0);
1486 req->SetUserData(&user_data1, user_data1);
1487 req->SetUserData(&user_data2, user_data2);
1488 req->set_method("GET");
[email protected]f7022f32014-08-21 16:32:191489 req->Start();
[email protected]255620da2013-08-19 13:14:291490 base::RunLoop().Run();
[email protected]2bba3252013-04-08 19:50:591491
bengr1bf8e942014-11-07 01:36:501492 // Make sure we can retrieve our specific user data.
1493 EXPECT_EQ(user_data0, req->GetUserData(nullptr));
1494 EXPECT_EQ(user_data1, req->GetUserData(&user_data1));
1495 EXPECT_EQ(user_data2, req->GetUserData(&user_data2));
[email protected]2bba3252013-04-08 19:50:591496
bengr1bf8e942014-11-07 01:36:501497 // Check that we got one good response.
1498 EXPECT_TRUE(req->status().is_success());
1499 EXPECT_EQ(200, req->response_headers()->response_code());
1500 EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received());
1501 EXPECT_EQ(1, d.response_started_count());
1502 EXPECT_EQ(0, d.received_redirect_count());
1503}
[email protected]2bba3252013-04-08 19:50:591504
bengr1bf8e942014-11-07 01:36:501505TEST_F(URLRequestInterceptorTest, InterceptRedirect) {
1506 // Intercept the main request and respond with a redirect.
1507 interceptor()->set_intercept_main_request(true);
1508 interceptor()->set_main_headers(
1509 MockURLRequestInterceptor::redirect_headers());
1510 interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data());
1511
1512 // Intercept that redirect and respond with a final OK response.
1513 interceptor()->set_intercept_redirect(true);
1514 interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers());
1515 interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data());
1516
1517 TestDelegate d;
1518 scoped_ptr<URLRequest> req(default_context().CreateRequest(
davidben151423e2015-03-23 18:48:361519 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d));
bengr1bf8e942014-11-07 01:36:501520 req->set_method("GET");
1521 req->Start();
1522 base::RunLoop().Run();
1523
1524 // Check that the interceptor got called as expected.
1525 EXPECT_TRUE(interceptor()->did_intercept_main());
1526 EXPECT_TRUE(interceptor()->did_intercept_redirect());
1527
1528 // Check that we got one good response.
1529 EXPECT_TRUE(req->status().is_success());
1530 if (req->status().is_success())
1531 EXPECT_EQ(200, req->response_headers()->response_code());
1532
1533 EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received());
1534 EXPECT_EQ(1, d.response_started_count());
1535 EXPECT_EQ(0, d.received_redirect_count());
1536}
1537
1538TEST_F(URLRequestInterceptorTest, InterceptServerError) {
1539 // Intercept the main request to generate a server error response.
1540 interceptor()->set_intercept_main_request(true);
1541 interceptor()->set_main_headers(MockURLRequestInterceptor::error_headers());
1542 interceptor()->set_main_data(MockURLRequestInterceptor::error_data());
1543
1544 // Intercept that error and respond with an OK response.
1545 interceptor()->set_intercept_final_response(true);
1546 interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers());
1547 interceptor()->set_final_data(MockURLRequestInterceptor::ok_data());
1548
1549 TestDelegate d;
1550 scoped_ptr<URLRequest> req(default_context().CreateRequest(
davidben151423e2015-03-23 18:48:361551 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d));
bengr1bf8e942014-11-07 01:36:501552 req->set_method("GET");
1553 req->Start();
1554 base::RunLoop().Run();
1555
1556 // Check that the interceptor got called as expected.
1557 EXPECT_TRUE(interceptor()->did_intercept_main());
1558 EXPECT_TRUE(interceptor()->did_intercept_final());
1559
1560 // Check that we got one good response.
1561 EXPECT_TRUE(req->status().is_success());
1562 EXPECT_EQ(200, req->response_headers()->response_code());
1563 EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received());
1564 EXPECT_EQ(1, d.response_started_count());
1565 EXPECT_EQ(0, d.received_redirect_count());
1566}
1567
1568TEST_F(URLRequestInterceptorTest, InterceptNetworkError) {
1569 // Intercept the main request to simulate a network error.
1570 interceptor()->set_simulate_main_network_error(true);
1571
1572 // Intercept that error and respond with an OK response.
1573 interceptor()->set_intercept_final_response(true);
1574 interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers());
1575 interceptor()->set_final_data(MockURLRequestInterceptor::ok_data());
1576
1577 TestDelegate d;
1578 scoped_ptr<URLRequest> req(default_context().CreateRequest(
davidben151423e2015-03-23 18:48:361579 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d));
bengr1bf8e942014-11-07 01:36:501580 req->set_method("GET");
1581 req->Start();
1582 base::RunLoop().Run();
1583
1584 // Check that the interceptor got called as expected.
1585 EXPECT_TRUE(interceptor()->did_simulate_error_main());
1586 EXPECT_TRUE(interceptor()->did_intercept_final());
1587
1588 // Check that we received one good response.
1589 EXPECT_TRUE(req->status().is_success());
1590 EXPECT_EQ(200, req->response_headers()->response_code());
1591 EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received());
1592 EXPECT_EQ(1, d.response_started_count());
1593 EXPECT_EQ(0, d.received_redirect_count());
1594}
1595
1596TEST_F(URLRequestInterceptorTest, InterceptRestartRequired) {
1597 // Restart the main request.
1598 interceptor()->set_restart_main_request(true);
1599
1600 // then intercept the new main request and respond with an OK response
1601 interceptor()->set_intercept_main_request(true);
1602 interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers());
1603 interceptor()->set_main_data(MockURLRequestInterceptor::ok_data());
1604
1605 TestDelegate d;
1606 scoped_ptr<URLRequest> req(default_context().CreateRequest(
davidben151423e2015-03-23 18:48:361607 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d));
bengr1bf8e942014-11-07 01:36:501608 req->set_method("GET");
1609 req->Start();
1610 base::RunLoop().Run();
1611
1612 // Check that the interceptor got called as expected.
1613 EXPECT_TRUE(interceptor()->did_restart_main());
1614 EXPECT_TRUE(interceptor()->did_intercept_main());
1615
1616 // Check that we received one good response.
1617 EXPECT_TRUE(req->status().is_success());
1618 if (req->status().is_success())
1619 EXPECT_EQ(200, req->response_headers()->response_code());
1620
1621 EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received());
1622 EXPECT_EQ(1, d.response_started_count());
1623 EXPECT_EQ(0, d.received_redirect_count());
1624}
1625
1626TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelMain) {
1627 // Intercept the main request and cancel from within the restarted job.
1628 interceptor()->set_cancel_main_request(true);
1629
1630 // Set up to intercept the final response and override it with an OK response.
1631 interceptor()->set_intercept_final_response(true);
1632 interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers());
1633 interceptor()->set_final_data(MockURLRequestInterceptor::ok_data());
1634
1635 TestDelegate d;
1636 scoped_ptr<URLRequest> req(default_context().CreateRequest(
davidben151423e2015-03-23 18:48:361637 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d));
bengr1bf8e942014-11-07 01:36:501638 req->set_method("GET");
1639 req->Start();
1640 base::RunLoop().Run();
1641
1642 // Check that the interceptor got called as expected.
1643 EXPECT_TRUE(interceptor()->did_cancel_main());
1644 EXPECT_FALSE(interceptor()->did_intercept_final());
1645
1646 // Check that we see a canceled request.
1647 EXPECT_FALSE(req->status().is_success());
1648 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status());
1649}
1650
1651TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelRedirect) {
1652 // Intercept the main request and respond with a redirect.
1653 interceptor()->set_intercept_main_request(true);
1654 interceptor()->set_main_headers(
1655 MockURLRequestInterceptor::redirect_headers());
1656 interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data());
1657
1658 // Intercept the redirect and cancel from within that job.
1659 interceptor()->set_cancel_redirect_request(true);
1660
1661 // Set up to intercept the final response and override it with an OK response.
1662 interceptor()->set_intercept_final_response(true);
1663 interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers());
1664 interceptor()->set_final_data(MockURLRequestInterceptor::ok_data());
1665
1666 TestDelegate d;
1667 scoped_ptr<URLRequest> req(default_context().CreateRequest(
davidben151423e2015-03-23 18:48:361668 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d));
bengr1bf8e942014-11-07 01:36:501669 req->set_method("GET");
1670 req->Start();
1671 base::RunLoop().Run();
1672
1673 // Check that the interceptor got called as expected.
1674 EXPECT_TRUE(interceptor()->did_intercept_main());
1675 EXPECT_TRUE(interceptor()->did_cancel_redirect());
1676 EXPECT_FALSE(interceptor()->did_intercept_final());
1677
1678 // Check that we see a canceled request.
1679 EXPECT_FALSE(req->status().is_success());
1680 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status());
1681}
1682
1683TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelFinal) {
1684 // Intercept the main request to simulate a network error.
1685 interceptor()->set_simulate_main_network_error(true);
1686
1687 // Set up to intercept final the response and cancel from within that job.
1688 interceptor()->set_cancel_final_request(true);
1689
1690 TestDelegate d;
1691 scoped_ptr<URLRequest> req(default_context().CreateRequest(
davidben151423e2015-03-23 18:48:361692 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d));
bengr1bf8e942014-11-07 01:36:501693 req->set_method("GET");
1694 req->Start();
1695 base::RunLoop().Run();
1696
1697 // Check that the interceptor got called as expected.
1698 EXPECT_TRUE(interceptor()->did_simulate_error_main());
1699 EXPECT_TRUE(interceptor()->did_cancel_final());
1700
1701 // Check that we see a canceled request.
1702 EXPECT_FALSE(req->status().is_success());
1703 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status());
1704}
1705
1706TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelInRestart) {
1707 // Intercept the main request and cancel then restart from within that job.
1708 interceptor()->set_cancel_then_restart_main_request(true);
1709
1710 // Set up to intercept the final response and override it with an OK response.
1711 interceptor()->set_intercept_final_response(true);
bengrb50d631e2014-11-17 22:50:501712 interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers());
1713 interceptor()->set_final_data(MockURLRequestInterceptor::ok_data());
bengr1bf8e942014-11-07 01:36:501714
1715 TestDelegate d;
1716 scoped_ptr<URLRequest> req(default_context().CreateRequest(
davidben151423e2015-03-23 18:48:361717 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d));
bengr1bf8e942014-11-07 01:36:501718 req->set_method("GET");
1719 req->Start();
1720 base::RunLoop().Run();
1721
1722 // Check that the interceptor got called as expected.
1723 EXPECT_TRUE(interceptor()->did_cancel_then_restart_main());
1724 EXPECT_FALSE(interceptor()->did_intercept_final());
1725
1726 // Check that we see a canceled request.
1727 EXPECT_FALSE(req->status().is_success());
1728 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status());
[email protected]2bba3252013-04-08 19:50:591729}
1730
1731// "Normal" LoadTimingInfo as returned by a job. Everything is in order, not
1732// reused. |connect_time_flags| is used to indicate if there should be dns
1733// or SSL times, and |used_proxy| is used for proxy times.
1734LoadTimingInfo NormalLoadTimingInfo(base::TimeTicks now,
1735 int connect_time_flags,
1736 bool used_proxy) {
1737 LoadTimingInfo load_timing;
1738 load_timing.socket_log_id = 1;
1739
1740 if (used_proxy) {
1741 load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1);
1742 load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2);
1743 }
1744
1745 LoadTimingInfo::ConnectTiming& connect_timing = load_timing.connect_timing;
1746 if (connect_time_flags & CONNECT_TIMING_HAS_DNS_TIMES) {
1747 connect_timing.dns_start = now + base::TimeDelta::FromDays(3);
1748 connect_timing.dns_end = now + base::TimeDelta::FromDays(4);
1749 }
1750 connect_timing.connect_start = now + base::TimeDelta::FromDays(5);
1751 if (connect_time_flags & CONNECT_TIMING_HAS_SSL_TIMES) {
1752 connect_timing.ssl_start = now + base::TimeDelta::FromDays(6);
1753 connect_timing.ssl_end = now + base::TimeDelta::FromDays(7);
1754 }
1755 connect_timing.connect_end = now + base::TimeDelta::FromDays(8);
1756
1757 load_timing.send_start = now + base::TimeDelta::FromDays(9);
1758 load_timing.send_end = now + base::TimeDelta::FromDays(10);
1759 load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11);
1760 return load_timing;
1761}
1762
1763// Same as above, but in the case of a reused socket.
1764LoadTimingInfo NormalLoadTimingInfoReused(base::TimeTicks now,
1765 bool used_proxy) {
1766 LoadTimingInfo load_timing;
1767 load_timing.socket_log_id = 1;
1768 load_timing.socket_reused = true;
1769
1770 if (used_proxy) {
1771 load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1);
1772 load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2);
1773 }
1774
1775 load_timing.send_start = now + base::TimeDelta::FromDays(9);
1776 load_timing.send_end = now + base::TimeDelta::FromDays(10);
1777 load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11);
1778 return load_timing;
1779}
1780
bengr1bf8e942014-11-07 01:36:501781LoadTimingInfo RunURLRequestInterceptorLoadTimingTest(
1782 const LoadTimingInfo& job_load_timing,
1783 const URLRequestContext& context,
1784 MockURLRequestInterceptor* interceptor) {
1785 interceptor->set_intercept_main_request(true);
1786 interceptor->set_main_request_load_timing_info(job_load_timing);
1787 TestDelegate d;
1788 scoped_ptr<URLRequest> req(context.CreateRequest(
davidben151423e2015-03-23 18:48:361789 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d));
bengr1bf8e942014-11-07 01:36:501790 req->Start();
1791 base::RunLoop().Run();
1792
1793 LoadTimingInfo resulting_load_timing;
1794 req->GetLoadTimingInfo(&resulting_load_timing);
1795
1796 // None of these should be modified by the URLRequest.
1797 EXPECT_EQ(job_load_timing.socket_reused, resulting_load_timing.socket_reused);
1798 EXPECT_EQ(job_load_timing.socket_log_id, resulting_load_timing.socket_log_id);
1799 EXPECT_EQ(job_load_timing.send_start, resulting_load_timing.send_start);
1800 EXPECT_EQ(job_load_timing.send_end, resulting_load_timing.send_end);
1801 EXPECT_EQ(job_load_timing.receive_headers_end,
1802 resulting_load_timing.receive_headers_end);
1803
1804 return resulting_load_timing;
1805}
1806
[email protected]2bba3252013-04-08 19:50:591807// Basic test that the intercept + load timing tests work.
bengr1bf8e942014-11-07 01:36:501808TEST_F(URLRequestInterceptorTest, InterceptLoadTiming) {
[email protected]2bba3252013-04-08 19:50:591809 base::TimeTicks now = base::TimeTicks::Now();
1810 LoadTimingInfo job_load_timing =
1811 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, false);
1812
[email protected]2ca01e52013-10-31 22:05:191813 LoadTimingInfo load_timing_result =
bengr1bf8e942014-11-07 01:36:501814 RunURLRequestInterceptorLoadTimingTest(
1815 job_load_timing, default_context(), interceptor());
[email protected]2bba3252013-04-08 19:50:591816
1817 // Nothing should have been changed by the URLRequest.
1818 EXPECT_EQ(job_load_timing.proxy_resolve_start,
1819 load_timing_result.proxy_resolve_start);
1820 EXPECT_EQ(job_load_timing.proxy_resolve_end,
1821 load_timing_result.proxy_resolve_end);
1822 EXPECT_EQ(job_load_timing.connect_timing.dns_start,
1823 load_timing_result.connect_timing.dns_start);
1824 EXPECT_EQ(job_load_timing.connect_timing.dns_end,
1825 load_timing_result.connect_timing.dns_end);
1826 EXPECT_EQ(job_load_timing.connect_timing.connect_start,
1827 load_timing_result.connect_timing.connect_start);
1828 EXPECT_EQ(job_load_timing.connect_timing.connect_end,
1829 load_timing_result.connect_timing.connect_end);
1830 EXPECT_EQ(job_load_timing.connect_timing.ssl_start,
1831 load_timing_result.connect_timing.ssl_start);
1832 EXPECT_EQ(job_load_timing.connect_timing.ssl_end,
1833 load_timing_result.connect_timing.ssl_end);
1834
1835 // Redundant sanity check.
1836 TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_DNS_TIMES);
1837}
1838
1839// Another basic test, with proxy and SSL times, but no DNS times.
bengr1bf8e942014-11-07 01:36:501840TEST_F(URLRequestInterceptorTest, InterceptLoadTimingProxy) {
[email protected]2bba3252013-04-08 19:50:591841 base::TimeTicks now = base::TimeTicks::Now();
1842 LoadTimingInfo job_load_timing =
1843 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, true);
1844
[email protected]2ca01e52013-10-31 22:05:191845 LoadTimingInfo load_timing_result =
bengr1bf8e942014-11-07 01:36:501846 RunURLRequestInterceptorLoadTimingTest(
1847 job_load_timing, default_context(), interceptor());
[email protected]2bba3252013-04-08 19:50:591848
1849 // Nothing should have been changed by the URLRequest.
1850 EXPECT_EQ(job_load_timing.proxy_resolve_start,
1851 load_timing_result.proxy_resolve_start);
1852 EXPECT_EQ(job_load_timing.proxy_resolve_end,
1853 load_timing_result.proxy_resolve_end);
1854 EXPECT_EQ(job_load_timing.connect_timing.dns_start,
1855 load_timing_result.connect_timing.dns_start);
1856 EXPECT_EQ(job_load_timing.connect_timing.dns_end,
1857 load_timing_result.connect_timing.dns_end);
1858 EXPECT_EQ(job_load_timing.connect_timing.connect_start,
1859 load_timing_result.connect_timing.connect_start);
1860 EXPECT_EQ(job_load_timing.connect_timing.connect_end,
1861 load_timing_result.connect_timing.connect_end);
1862 EXPECT_EQ(job_load_timing.connect_timing.ssl_start,
1863 load_timing_result.connect_timing.ssl_start);
1864 EXPECT_EQ(job_load_timing.connect_timing.ssl_end,
1865 load_timing_result.connect_timing.ssl_end);
1866
1867 // Redundant sanity check.
1868 TestLoadTimingNotReusedWithProxy(load_timing_result,
1869 CONNECT_TIMING_HAS_SSL_TIMES);
1870}
1871
1872// Make sure that URLRequest correctly adjusts proxy times when they're before
1873// |request_start|, due to already having a connected socket. This happens in
[email protected]cf4cae32014-05-27 00:39:101874// the case of reusing a SPDY session. The connected socket is not considered
1875// reused in this test (May be a preconnect).
[email protected]2bba3252013-04-08 19:50:591876//
1877// To mix things up from the test above, assumes DNS times but no SSL times.
bengr1bf8e942014-11-07 01:36:501878TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyProxyResolution) {
[email protected]2bba3252013-04-08 19:50:591879 base::TimeTicks now = base::TimeTicks::Now();
1880 LoadTimingInfo job_load_timing =
1881 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, true);
1882 job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(6);
1883 job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(5);
1884 job_load_timing.connect_timing.dns_start = now - base::TimeDelta::FromDays(4);
1885 job_load_timing.connect_timing.dns_end = now - base::TimeDelta::FromDays(3);
1886 job_load_timing.connect_timing.connect_start =
1887 now - base::TimeDelta::FromDays(2);
1888 job_load_timing.connect_timing.connect_end =
1889 now - base::TimeDelta::FromDays(1);
1890
[email protected]2ca01e52013-10-31 22:05:191891 LoadTimingInfo load_timing_result =
bengr1bf8e942014-11-07 01:36:501892 RunURLRequestInterceptorLoadTimingTest(
1893 job_load_timing, default_context(), interceptor());
[email protected]2bba3252013-04-08 19:50:591894
1895 // Proxy times, connect times, and DNS times should all be replaced with
1896 // request_start.
1897 EXPECT_EQ(load_timing_result.request_start,
1898 load_timing_result.proxy_resolve_start);
1899 EXPECT_EQ(load_timing_result.request_start,
1900 load_timing_result.proxy_resolve_end);
1901 EXPECT_EQ(load_timing_result.request_start,
1902 load_timing_result.connect_timing.dns_start);
1903 EXPECT_EQ(load_timing_result.request_start,
1904 load_timing_result.connect_timing.dns_end);
1905 EXPECT_EQ(load_timing_result.request_start,
1906 load_timing_result.connect_timing.connect_start);
1907 EXPECT_EQ(load_timing_result.request_start,
1908 load_timing_result.connect_timing.connect_end);
1909
1910 // Other times should have been left null.
1911 TestLoadTimingNotReusedWithProxy(load_timing_result,
1912 CONNECT_TIMING_HAS_DNS_TIMES);
1913}
1914
1915// Same as above, but in the reused case.
bengr1bf8e942014-11-07 01:36:501916TEST_F(URLRequestInterceptorTest,
1917 InterceptLoadTimingEarlyProxyResolutionReused) {
[email protected]2bba3252013-04-08 19:50:591918 base::TimeTicks now = base::TimeTicks::Now();
1919 LoadTimingInfo job_load_timing = NormalLoadTimingInfoReused(now, true);
1920 job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(4);
1921 job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(3);
1922
[email protected]2ca01e52013-10-31 22:05:191923 LoadTimingInfo load_timing_result =
bengr1bf8e942014-11-07 01:36:501924 RunURLRequestInterceptorLoadTimingTest(
1925 job_load_timing, default_context(), interceptor());
[email protected]2bba3252013-04-08 19:50:591926
1927 // Proxy times and connect times should all be replaced with request_start.
1928 EXPECT_EQ(load_timing_result.request_start,
1929 load_timing_result.proxy_resolve_start);
1930 EXPECT_EQ(load_timing_result.request_start,
1931 load_timing_result.proxy_resolve_end);
1932
1933 // Other times should have been left null.
1934 TestLoadTimingReusedWithProxy(load_timing_result);
1935}
1936
1937// Make sure that URLRequest correctly adjusts connect times when they're before
1938// |request_start|, due to reusing a connected socket. The connected socket is
1939// not considered reused in this test (May be a preconnect).
1940//
1941// To mix things up, the request has SSL times, but no DNS times.
bengr1bf8e942014-11-07 01:36:501942TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnect) {
[email protected]2bba3252013-04-08 19:50:591943 base::TimeTicks now = base::TimeTicks::Now();
1944 LoadTimingInfo job_load_timing =
1945 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, false);
1946 job_load_timing.connect_timing.connect_start =
1947 now - base::TimeDelta::FromDays(1);
1948 job_load_timing.connect_timing.ssl_start = now - base::TimeDelta::FromDays(2);
1949 job_load_timing.connect_timing.ssl_end = now - base::TimeDelta::FromDays(3);
1950 job_load_timing.connect_timing.connect_end =
1951 now - base::TimeDelta::FromDays(4);
1952
[email protected]2ca01e52013-10-31 22:05:191953 LoadTimingInfo load_timing_result =
bengr1bf8e942014-11-07 01:36:501954 RunURLRequestInterceptorLoadTimingTest(
1955 job_load_timing, default_context(), interceptor());
[email protected]2bba3252013-04-08 19:50:591956
1957 // Connect times, and SSL times should be replaced with request_start.
1958 EXPECT_EQ(load_timing_result.request_start,
1959 load_timing_result.connect_timing.connect_start);
1960 EXPECT_EQ(load_timing_result.request_start,
1961 load_timing_result.connect_timing.ssl_start);
1962 EXPECT_EQ(load_timing_result.request_start,
1963 load_timing_result.connect_timing.ssl_end);
1964 EXPECT_EQ(load_timing_result.request_start,
1965 load_timing_result.connect_timing.connect_end);
1966
1967 // Other times should have been left null.
1968 TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_SSL_TIMES);
1969}
1970
1971// Make sure that URLRequest correctly adjusts connect times when they're before
1972// |request_start|, due to reusing a connected socket in the case that there
1973// are also proxy times. The connected socket is not considered reused in this
1974// test (May be a preconnect).
1975//
1976// In this test, there are no SSL or DNS times.
bengr1bf8e942014-11-07 01:36:501977TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnectWithProxy) {
[email protected]2bba3252013-04-08 19:50:591978 base::TimeTicks now = base::TimeTicks::Now();
1979 LoadTimingInfo job_load_timing =
1980 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY, true);
1981 job_load_timing.connect_timing.connect_start =
1982 now - base::TimeDelta::FromDays(1);
1983 job_load_timing.connect_timing.connect_end =
1984 now - base::TimeDelta::FromDays(2);
1985
[email protected]2ca01e52013-10-31 22:05:191986 LoadTimingInfo load_timing_result =
bengr1bf8e942014-11-07 01:36:501987 RunURLRequestInterceptorLoadTimingTest(
1988 job_load_timing, default_context(), interceptor());
[email protected]2bba3252013-04-08 19:50:591989
1990 // Connect times should be replaced with proxy_resolve_end.
1991 EXPECT_EQ(load_timing_result.proxy_resolve_end,
1992 load_timing_result.connect_timing.connect_start);
1993 EXPECT_EQ(load_timing_result.proxy_resolve_end,
1994 load_timing_result.connect_timing.connect_end);
1995
1996 // Other times should have been left null.
1997 TestLoadTimingNotReusedWithProxy(load_timing_result,
1998 CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY);
1999}
2000
[email protected]316c1e5e2012-09-12 15:17:442001// Check that two different URL requests have different identifiers.
2002TEST_F(URLRequestTest, Identifiers) {
2003 TestDelegate d;
2004 TestURLRequestContext context;
davidben151423e2015-03-23 18:48:362005 scoped_ptr<URLRequest> req(
2006 context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d));
2007 scoped_ptr<URLRequest> other_req(
2008 context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d));
[email protected]316c1e5e2012-09-12 15:17:442009
mmenke19378d22014-09-09 04:12:592010 ASSERT_NE(req->identifier(), other_req->identifier());
[email protected]316c1e5e2012-09-12 15:17:442011}
2012
2013// Check that a failure to connect to the proxy is reported to the network
2014// delegate.
2015TEST_F(URLRequestTest, NetworkDelegateProxyError) {
2016 MockHostResolver host_resolver;
2017 host_resolver.rules()->AddSimulatedFailure("*");
2018
[email protected]ceefd7fd2012-11-29 00:36:242019 TestNetworkDelegate network_delegate; // Must outlive URLRequests.
[email protected]316c1e5e2012-09-12 15:17:442020 TestURLRequestContextWithProxy context("myproxy:70", &network_delegate);
2021
2022 TestDelegate d;
davidben151423e2015-03-23 18:48:362023 scoped_ptr<URLRequest> req(
2024 context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:192025 req->set_method("GET");
[email protected]316c1e5e2012-09-12 15:17:442026
[email protected]f7022f32014-08-21 16:32:192027 req->Start();
[email protected]255620da2013-08-19 13:14:292028 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442029
2030 // Check we see a failed request.
[email protected]f7022f32014-08-21 16:32:192031 EXPECT_FALSE(req->status().is_success());
[email protected]d8fc4722014-06-13 13:17:152032 // The proxy server is not set before failure.
[email protected]f7022f32014-08-21 16:32:192033 EXPECT_TRUE(req->proxy_server().IsEmpty());
2034 EXPECT_EQ(URLRequestStatus::FAILED, req->status().status());
2035 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, req->status().error());
[email protected]316c1e5e2012-09-12 15:17:442036
2037 EXPECT_EQ(1, network_delegate.error_count());
2038 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, network_delegate.last_error());
2039 EXPECT_EQ(1, network_delegate.completed_requests());
2040}
2041
[email protected]cba24642014-08-15 20:49:592042// Make sure that NetworkDelegate::NotifyCompleted is called if
[email protected]316c1e5e2012-09-12 15:17:442043// content is empty.
2044TEST_F(URLRequestTest, RequestCompletionForEmptyResponse) {
2045 TestDelegate d;
davidben151423e2015-03-23 18:48:362046 scoped_ptr<URLRequest> req(
2047 default_context_.CreateRequest(GURL("data:,"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:192048 req->Start();
[email protected]255620da2013-08-19 13:14:292049 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442050 EXPECT_EQ("", d.data_received());
2051 EXPECT_EQ(1, default_network_delegate_.completed_requests());
2052}
2053
[email protected]5033ab82013-03-22 20:17:462054// Make sure that SetPriority actually sets the URLRequest's priority
2055// correctly, both before and after start.
2056TEST_F(URLRequestTest, SetPriorityBasic) {
2057 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192058 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:362059 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:192060 EXPECT_EQ(DEFAULT_PRIORITY, req->priority());
[email protected]5033ab82013-03-22 20:17:462061
[email protected]f7022f32014-08-21 16:32:192062 req->SetPriority(LOW);
2063 EXPECT_EQ(LOW, req->priority());
[email protected]5033ab82013-03-22 20:17:462064
[email protected]f7022f32014-08-21 16:32:192065 req->Start();
2066 EXPECT_EQ(LOW, req->priority());
[email protected]5033ab82013-03-22 20:17:462067
[email protected]f7022f32014-08-21 16:32:192068 req->SetPriority(MEDIUM);
2069 EXPECT_EQ(MEDIUM, req->priority());
[email protected]5033ab82013-03-22 20:17:462070}
2071
2072// Make sure that URLRequest calls SetPriority on a job before calling
2073// Start on it.
2074TEST_F(URLRequestTest, SetJobPriorityBeforeJobStart) {
2075 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192076 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:362077 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:192078 EXPECT_EQ(DEFAULT_PRIORITY, req->priority());
[email protected]5033ab82013-03-22 20:17:462079
2080 scoped_refptr<URLRequestTestJob> job =
[email protected]f7022f32014-08-21 16:32:192081 new URLRequestTestJob(req.get(), &default_network_delegate_);
[email protected]90499482013-06-01 00:39:502082 AddTestInterceptor()->set_main_intercept_job(job.get());
[email protected]5033ab82013-03-22 20:17:462083 EXPECT_EQ(DEFAULT_PRIORITY, job->priority());
2084
[email protected]f7022f32014-08-21 16:32:192085 req->SetPriority(LOW);
[email protected]5033ab82013-03-22 20:17:462086
[email protected]f7022f32014-08-21 16:32:192087 req->Start();
[email protected]5033ab82013-03-22 20:17:462088 EXPECT_EQ(LOW, job->priority());
2089}
2090
2091// Make sure that URLRequest passes on its priority updates to its
2092// job.
2093TEST_F(URLRequestTest, SetJobPriority) {
2094 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192095 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:362096 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d));
[email protected]5033ab82013-03-22 20:17:462097
2098 scoped_refptr<URLRequestTestJob> job =
[email protected]f7022f32014-08-21 16:32:192099 new URLRequestTestJob(req.get(), &default_network_delegate_);
[email protected]90499482013-06-01 00:39:502100 AddTestInterceptor()->set_main_intercept_job(job.get());
[email protected]5033ab82013-03-22 20:17:462101
[email protected]f7022f32014-08-21 16:32:192102 req->SetPriority(LOW);
2103 req->Start();
[email protected]5033ab82013-03-22 20:17:462104 EXPECT_EQ(LOW, job->priority());
2105
[email protected]f7022f32014-08-21 16:32:192106 req->SetPriority(MEDIUM);
2107 EXPECT_EQ(MEDIUM, req->priority());
[email protected]5033ab82013-03-22 20:17:462108 EXPECT_EQ(MEDIUM, job->priority());
2109}
2110
[email protected]bb1c4662013-11-14 00:00:072111// Setting the IGNORE_LIMITS load flag should be okay if the priority
2112// is MAXIMUM_PRIORITY.
2113TEST_F(URLRequestTest, PriorityIgnoreLimits) {
2114 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192115 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:362116 GURL("http://test_intercept/foo"), MAXIMUM_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:192117 EXPECT_EQ(MAXIMUM_PRIORITY, req->priority());
[email protected]bb1c4662013-11-14 00:00:072118
2119 scoped_refptr<URLRequestTestJob> job =
[email protected]f7022f32014-08-21 16:32:192120 new URLRequestTestJob(req.get(), &default_network_delegate_);
[email protected]bb1c4662013-11-14 00:00:072121 AddTestInterceptor()->set_main_intercept_job(job.get());
2122
[email protected]f7022f32014-08-21 16:32:192123 req->SetLoadFlags(LOAD_IGNORE_LIMITS);
2124 EXPECT_EQ(MAXIMUM_PRIORITY, req->priority());
[email protected]bb1c4662013-11-14 00:00:072125
[email protected]f7022f32014-08-21 16:32:192126 req->SetPriority(MAXIMUM_PRIORITY);
2127 EXPECT_EQ(MAXIMUM_PRIORITY, req->priority());
[email protected]bb1c4662013-11-14 00:00:072128
[email protected]f7022f32014-08-21 16:32:192129 req->Start();
2130 EXPECT_EQ(MAXIMUM_PRIORITY, req->priority());
[email protected]bb1c4662013-11-14 00:00:072131 EXPECT_EQ(MAXIMUM_PRIORITY, job->priority());
2132}
2133
[email protected]ce7d0cbc2013-05-03 18:57:222134// TODO(droger): Support SpawnedTestServer on iOS (see http://crbug.com/148666).
[email protected]316c1e5e2012-09-12 15:17:442135#if !defined(OS_IOS)
[email protected]ce7d0cbc2013-05-03 18:57:222136// A subclass of SpawnedTestServer that uses a statically-configured hostname.
2137// This is to work around mysterious failures in chrome_frame_net_tests. See:
[email protected]316c1e5e2012-09-12 15:17:442138// http://crbug.com/114369
[email protected]cd6f2522014-01-16 18:27:352139// TODO(erikwright): remove or update as needed; see http://crbug.com/334634.
[email protected]ce7d0cbc2013-05-03 18:57:222140class LocalHttpTestServer : public SpawnedTestServer {
[email protected]316c1e5e2012-09-12 15:17:442141 public:
[email protected]6cdfd7f2013-02-08 20:40:152142 explicit LocalHttpTestServer(const base::FilePath& document_root)
[email protected]ce7d0cbc2013-05-03 18:57:222143 : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP,
2144 ScopedCustomUrlRequestTestHttpHost::value(),
2145 document_root) {}
[email protected]316c1e5e2012-09-12 15:17:442146 LocalHttpTestServer()
[email protected]ce7d0cbc2013-05-03 18:57:222147 : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP,
2148 ScopedCustomUrlRequestTestHttpHost::value(),
2149 base::FilePath()) {}
[email protected]316c1e5e2012-09-12 15:17:442150};
2151
[email protected]f2f31b32013-01-16 23:24:092152TEST_F(URLRequestTest, DelayedCookieCallback) {
[email protected]316c1e5e2012-09-12 15:17:442153 LocalHttpTestServer test_server;
2154 ASSERT_TRUE(test_server.Start());
2155
2156 TestURLRequestContext context;
2157 scoped_refptr<DelayedCookieMonster> delayed_cm =
2158 new DelayedCookieMonster();
2159 scoped_refptr<CookieStore> cookie_store = delayed_cm;
[email protected]90499482013-06-01 00:39:502160 context.set_cookie_store(delayed_cm.get());
[email protected]316c1e5e2012-09-12 15:17:442161
2162 // Set up a cookie.
2163 {
2164 TestNetworkDelegate network_delegate;
2165 context.set_network_delegate(&network_delegate);
2166 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192167 scoped_ptr<URLRequest> req(context.CreateRequest(
2168 test_server.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY,
davidben151423e2015-03-23 18:48:362169 &d));
[email protected]f7022f32014-08-21 16:32:192170 req->Start();
[email protected]255620da2013-08-19 13:14:292171 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442172 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2173 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2174 EXPECT_EQ(1, network_delegate.set_cookie_count());
2175 }
2176
2177 // Verify that the cookie is set.
2178 {
2179 TestNetworkDelegate network_delegate;
2180 context.set_network_delegate(&network_delegate);
2181 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192182 scoped_ptr<URLRequest> req(context.CreateRequest(
davidben151423e2015-03-23 18:48:362183 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:192184 req->Start();
[email protected]255620da2013-08-19 13:14:292185 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442186
2187 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2188 != std::string::npos);
2189 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2190 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2191 }
2192}
2193
[email protected]f2f31b32013-01-16 23:24:092194TEST_F(URLRequestTest, DoNotSendCookies) {
[email protected]316c1e5e2012-09-12 15:17:442195 LocalHttpTestServer test_server;
2196 ASSERT_TRUE(test_server.Start());
2197
2198 // Set up a cookie.
2199 {
2200 TestNetworkDelegate network_delegate;
2201 default_context_.set_network_delegate(&network_delegate);
2202 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192203 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2204 test_server.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY,
davidben151423e2015-03-23 18:48:362205 &d));
[email protected]f7022f32014-08-21 16:32:192206 req->Start();
[email protected]255620da2013-08-19 13:14:292207 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442208 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2209 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2210 }
2211
2212 // Verify that the cookie is set.
2213 {
2214 TestNetworkDelegate network_delegate;
2215 default_context_.set_network_delegate(&network_delegate);
2216 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192217 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:362218 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:192219 req->Start();
[email protected]255620da2013-08-19 13:14:292220 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442221
2222 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2223 != std::string::npos);
2224 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2225 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2226 }
2227
2228 // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set.
2229 {
2230 TestNetworkDelegate network_delegate;
2231 default_context_.set_network_delegate(&network_delegate);
2232 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192233 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:362234 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:192235 req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES);
2236 req->Start();
[email protected]255620da2013-08-19 13:14:292237 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442238
2239 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
2240 == std::string::npos);
2241
2242 // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies.
2243 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2244 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2245 }
2246}
2247
2248TEST_F(URLRequestTest, DoNotSaveCookies) {
2249 LocalHttpTestServer test_server;
2250 ASSERT_TRUE(test_server.Start());
2251
2252 // Set up a cookie.
2253 {
2254 TestNetworkDelegate network_delegate;
2255 default_context_.set_network_delegate(&network_delegate);
2256 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192257 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2258 test_server.GetURL("set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY,
davidben151423e2015-03-23 18:48:362259 &d));
[email protected]f7022f32014-08-21 16:32:192260 req->Start();
[email protected]255620da2013-08-19 13:14:292261 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442262
2263 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2264 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2265 EXPECT_EQ(1, network_delegate.set_cookie_count());
2266 }
2267
2268 // Try to set-up another cookie and update the previous cookie.
2269 {
2270 TestNetworkDelegate network_delegate;
2271 default_context_.set_network_delegate(&network_delegate);
2272 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192273 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
[email protected]316c1e5e2012-09-12 15:17:442274 test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
davidben151423e2015-03-23 18:48:362275 DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:192276 req->SetLoadFlags(LOAD_DO_NOT_SAVE_COOKIES);
2277 req->Start();
[email protected]316c1e5e2012-09-12 15:17:442278
[email protected]255620da2013-08-19 13:14:292279 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442280
2281 // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie.
2282 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2283 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2284 EXPECT_EQ(0, network_delegate.set_cookie_count());
2285 }
2286
2287 // Verify the cookies weren't saved or updated.
2288 {
2289 TestNetworkDelegate network_delegate;
2290 default_context_.set_network_delegate(&network_delegate);
2291 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192292 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:362293 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:192294 req->Start();
[email protected]255620da2013-08-19 13:14:292295 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442296
2297 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
2298 == std::string::npos);
2299 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
2300 != std::string::npos);
2301
2302 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2303 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2304 EXPECT_EQ(0, network_delegate.set_cookie_count());
2305 }
2306}
2307
2308TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) {
2309 LocalHttpTestServer test_server;
2310 ASSERT_TRUE(test_server.Start());
2311
2312 // Set up a cookie.
2313 {
2314 TestNetworkDelegate network_delegate;
2315 default_context_.set_network_delegate(&network_delegate);
2316 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192317 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2318 test_server.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY,
davidben151423e2015-03-23 18:48:362319 &d));
[email protected]f7022f32014-08-21 16:32:192320 req->Start();
[email protected]255620da2013-08-19 13:14:292321 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442322
2323 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2324 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2325 }
2326
2327 // Verify that the cookie is set.
2328 {
2329 TestNetworkDelegate network_delegate;
2330 default_context_.set_network_delegate(&network_delegate);
2331 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192332 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:362333 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:192334 req->Start();
[email protected]255620da2013-08-19 13:14:292335 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442336
2337 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2338 != std::string::npos);
2339
2340 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2341 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2342 }
2343
2344 // Verify that the cookie isn't sent.
2345 {
2346 TestNetworkDelegate network_delegate;
2347 default_context_.set_network_delegate(&network_delegate);
2348 TestDelegate d;
2349 network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES);
[email protected]f7022f32014-08-21 16:32:192350 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:362351 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:192352 req->Start();
[email protected]255620da2013-08-19 13:14:292353 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442354
2355 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
2356 == std::string::npos);
2357
[email protected]22e045f2013-09-20 03:54:032358 EXPECT_EQ(1, network_delegate.blocked_get_cookies_count());
[email protected]316c1e5e2012-09-12 15:17:442359 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2360 }
2361}
2362
2363TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) {
2364 LocalHttpTestServer test_server;
2365 ASSERT_TRUE(test_server.Start());
2366
2367 // Set up a cookie.
2368 {
2369 TestNetworkDelegate network_delegate;
2370 default_context_.set_network_delegate(&network_delegate);
2371 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192372 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2373 test_server.GetURL("set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY,
davidben151423e2015-03-23 18:48:362374 &d));
[email protected]f7022f32014-08-21 16:32:192375 req->Start();
[email protected]255620da2013-08-19 13:14:292376 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442377
2378 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2379 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2380 }
2381
2382 // Try to set-up another cookie and update the previous cookie.
2383 {
2384 TestNetworkDelegate network_delegate;
2385 default_context_.set_network_delegate(&network_delegate);
2386 TestDelegate d;
2387 network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE);
[email protected]f7022f32014-08-21 16:32:192388 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
[email protected]316c1e5e2012-09-12 15:17:442389 test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
davidben151423e2015-03-23 18:48:362390 DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:192391 req->Start();
[email protected]316c1e5e2012-09-12 15:17:442392
[email protected]255620da2013-08-19 13:14:292393 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442394
2395 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2396 EXPECT_EQ(2, network_delegate.blocked_set_cookie_count());
2397 }
2398
2399 // Verify the cookies weren't saved or updated.
2400 {
2401 TestNetworkDelegate network_delegate;
2402 default_context_.set_network_delegate(&network_delegate);
2403 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192404 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:362405 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:192406 req->Start();
[email protected]255620da2013-08-19 13:14:292407 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442408
2409 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
2410 == std::string::npos);
2411 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
2412 != std::string::npos);
2413
2414 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2415 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2416 }
2417}
2418
2419TEST_F(URLRequestTest, DoNotSaveEmptyCookies) {
2420 LocalHttpTestServer test_server;
2421 ASSERT_TRUE(test_server.Start());
2422
2423 // Set up an empty cookie.
2424 {
2425 TestNetworkDelegate network_delegate;
2426 default_context_.set_network_delegate(&network_delegate);
2427 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192428 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:362429 test_server.GetURL("set-cookie"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:192430 req->Start();
[email protected]255620da2013-08-19 13:14:292431 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442432
2433 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2434 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2435 EXPECT_EQ(0, network_delegate.set_cookie_count());
2436 }
2437}
2438
2439TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) {
2440 LocalHttpTestServer test_server;
2441 ASSERT_TRUE(test_server.Start());
2442
2443 // Set up a cookie.
2444 {
2445 TestNetworkDelegate network_delegate;
2446 default_context_.set_network_delegate(&network_delegate);
2447 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192448 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2449 test_server.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY,
davidben151423e2015-03-23 18:48:362450 &d));
[email protected]f7022f32014-08-21 16:32:192451 req->Start();
[email protected]255620da2013-08-19 13:14:292452 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442453
2454 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2455 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2456 }
2457
2458 // Verify that the cookie is set.
2459 {
2460 TestNetworkDelegate network_delegate;
2461 default_context_.set_network_delegate(&network_delegate);
2462 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192463 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:362464 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:192465 req->Start();
[email protected]255620da2013-08-19 13:14:292466 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442467
2468 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2469 != std::string::npos);
2470
2471 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2472 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2473 }
2474
2475 // Verify that the cookie isn't sent.
2476 {
2477 TestNetworkDelegate network_delegate;
2478 default_context_.set_network_delegate(&network_delegate);
2479 TestDelegate d;
2480 network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES);
[email protected]f7022f32014-08-21 16:32:192481 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:362482 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:192483 req->Start();
[email protected]255620da2013-08-19 13:14:292484 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442485
2486 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
2487 == std::string::npos);
2488
[email protected]22e045f2013-09-20 03:54:032489 EXPECT_EQ(1, network_delegate.blocked_get_cookies_count());
[email protected]316c1e5e2012-09-12 15:17:442490 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2491 }
2492}
2493
2494TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) {
2495 LocalHttpTestServer test_server;
2496 ASSERT_TRUE(test_server.Start());
2497
2498 // Set up a cookie.
2499 {
2500 TestNetworkDelegate network_delegate;
2501 default_context_.set_network_delegate(&network_delegate);
2502 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192503 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2504 test_server.GetURL("set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY,
davidben151423e2015-03-23 18:48:362505 &d));
[email protected]f7022f32014-08-21 16:32:192506 req->Start();
[email protected]255620da2013-08-19 13:14:292507 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442508
2509 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2510 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2511 }
2512
2513 // Try to set-up another cookie and update the previous cookie.
2514 {
2515 TestNetworkDelegate network_delegate;
2516 default_context_.set_network_delegate(&network_delegate);
2517 TestDelegate d;
2518 network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE);
[email protected]f7022f32014-08-21 16:32:192519 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
[email protected]316c1e5e2012-09-12 15:17:442520 test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
davidben151423e2015-03-23 18:48:362521 DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:192522 req->Start();
[email protected]316c1e5e2012-09-12 15:17:442523
[email protected]255620da2013-08-19 13:14:292524 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442525
2526 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2527 EXPECT_EQ(2, network_delegate.blocked_set_cookie_count());
2528 }
2529
2530 // Verify the cookies weren't saved or updated.
2531 {
2532 TestNetworkDelegate network_delegate;
2533 default_context_.set_network_delegate(&network_delegate);
2534 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192535 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:362536 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:192537 req->Start();
[email protected]255620da2013-08-19 13:14:292538 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442539
2540 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
2541 == std::string::npos);
2542 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
2543 != std::string::npos);
2544
2545 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2546 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2547 }
2548}
2549
mkwst3f3daac2015-02-26 20:15:262550TEST_F(URLRequestTest, FirstPartyOnlyCookiesEnabled) {
2551 LocalHttpTestServer test_server;
2552 ASSERT_TRUE(test_server.Start());
2553
2554 // Set up a 'First-Party-Only' cookie (on '127.0.0.1', as that's where
2555 // LocalHttpTestServer points).
2556 {
2557 TestNetworkDelegate network_delegate;
mkwst0513c9d2015-04-01 05:53:152558 network_delegate.set_first_party_only_cookies_enabled(true);
mkwst3f3daac2015-02-26 20:15:262559 default_context_.set_network_delegate(&network_delegate);
2560
2561 TestDelegate d;
2562 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2563 test_server.GetURL(
2564 "set-cookie?FirstPartyCookieToSet=1;First-Party-Only"),
davidben151423e2015-03-23 18:48:362565 DEFAULT_PRIORITY, &d));
mkwst3f3daac2015-02-26 20:15:262566 req->Start();
2567 base::RunLoop().Run();
2568 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2569 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2570 EXPECT_EQ(1, network_delegate.set_cookie_count());
2571 }
2572
2573 // Verify that the cookie is sent for first-party requests.
2574 {
2575 TestNetworkDelegate network_delegate;
mkwst0513c9d2015-04-01 05:53:152576 network_delegate.set_first_party_only_cookies_enabled(true);
mkwst3f3daac2015-02-26 20:15:262577 default_context_.set_network_delegate(&network_delegate);
2578 TestDelegate d;
2579 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:362580 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d));
estark92df3fd2015-04-06 19:57:472581 req->set_first_party_for_cookies(test_server.GetURL(""));
mkwst3f3daac2015-02-26 20:15:262582 req->Start();
2583 base::RunLoop().Run();
2584
2585 EXPECT_TRUE(d.data_received().find("FirstPartyCookieToSet=1") !=
2586 std::string::npos);
2587 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2588 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2589 }
2590
2591 // Verify that the cookie is not-sent for non-first-party requests.
2592 {
2593 TestNetworkDelegate network_delegate;
mkwst0513c9d2015-04-01 05:53:152594 network_delegate.set_first_party_only_cookies_enabled(true);
mkwst3f3daac2015-02-26 20:15:262595 default_context_.set_network_delegate(&network_delegate);
2596 TestDelegate d;
2597 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:362598 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d));
mkwst3f3daac2015-02-26 20:15:262599 req->set_first_party_for_cookies(GURL("http://third-party.test/"));
2600 req->Start();
2601 base::RunLoop().Run();
2602
2603 EXPECT_TRUE(d.data_received().find("FirstPartyCookieToSet=1") ==
2604 std::string::npos);
2605 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2606 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2607 }
2608}
2609
mkwst0513c9d2015-04-01 05:53:152610TEST_F(URLRequestTest, FirstPartyOnlyCookiesDisabled) {
2611 LocalHttpTestServer test_server;
2612 ASSERT_TRUE(test_server.Start());
2613
2614 // Set up a 'First-Party-Only' cookie (on '127.0.0.1', as that's where
2615 // LocalHttpTestServer points).
2616 {
2617 TestNetworkDelegate network_delegate;
2618 network_delegate.set_first_party_only_cookies_enabled(false);
2619 default_context_.set_network_delegate(&network_delegate);
2620
2621 TestDelegate d;
2622 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2623 test_server.GetURL(
2624 "set-cookie?FirstPartyCookieToSet=1;First-Party-Only"),
2625 DEFAULT_PRIORITY, &d));
2626 req->Start();
2627 base::RunLoop().Run();
2628 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2629 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2630 EXPECT_EQ(1, network_delegate.set_cookie_count());
2631 }
2632
2633 // Verify that the cookie is sent for first-party requests.
2634 {
2635 TestNetworkDelegate network_delegate;
2636 network_delegate.set_first_party_only_cookies_enabled(false);
2637 default_context_.set_network_delegate(&network_delegate);
2638 TestDelegate d;
2639 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2640 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d));
estark92df3fd2015-04-06 19:57:472641 req->set_first_party_for_cookies(test_server.GetURL(""));
mkwst0513c9d2015-04-01 05:53:152642 req->Start();
2643 base::RunLoop().Run();
2644
2645 EXPECT_TRUE(d.data_received().find("FirstPartyCookieToSet=1") !=
2646 std::string::npos);
2647 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2648 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2649 }
2650
2651 // Verify that the cookie is also sent for non-first-party requests.
2652 {
2653 TestNetworkDelegate network_delegate;
2654 network_delegate.set_first_party_only_cookies_enabled(false);
2655 default_context_.set_network_delegate(&network_delegate);
2656 TestDelegate d;
2657 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2658 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d));
2659 req->set_first_party_for_cookies(GURL("http://third-party.test/"));
2660 req->Start();
2661 base::RunLoop().Run();
2662
2663 EXPECT_TRUE(d.data_received().find("FirstPartyCookieToSet=1") !=
2664 std::string::npos);
2665 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2666 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2667 }
2668}
2669
[email protected]5095cd72012-11-01 10:29:162670// FixedDateNetworkDelegate swaps out the server's HTTP Date response header
2671// value for the |fixed_date| argument given to the constructor.
2672class FixedDateNetworkDelegate : public TestNetworkDelegate {
2673 public:
2674 explicit FixedDateNetworkDelegate(const std::string& fixed_date)
2675 : fixed_date_(fixed_date) {}
dchengb03027d2014-10-21 12:00:202676 ~FixedDateNetworkDelegate() override {}
[email protected]5095cd72012-11-01 10:29:162677
[email protected]cba24642014-08-15 20:49:592678 // NetworkDelegate implementation
dchengb03027d2014-10-21 12:00:202679 int OnHeadersReceived(
[email protected]cba24642014-08-15 20:49:592680 URLRequest* request,
2681 const CompletionCallback& callback,
2682 const HttpResponseHeaders* original_response_headers,
2683 scoped_refptr<HttpResponseHeaders>* override_response_headers,
mostynbba063d6032014-10-09 11:01:132684 GURL* allowed_unsafe_redirect_url) override;
[email protected]5095cd72012-11-01 10:29:162685
2686 private:
2687 std::string fixed_date_;
2688
2689 DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate);
2690};
2691
2692int FixedDateNetworkDelegate::OnHeadersReceived(
[email protected]cba24642014-08-15 20:49:592693 URLRequest* request,
2694 const CompletionCallback& callback,
2695 const HttpResponseHeaders* original_response_headers,
2696 scoped_refptr<HttpResponseHeaders>* override_response_headers,
[email protected]5f714132014-03-26 10:41:162697 GURL* allowed_unsafe_redirect_url) {
[email protected]cba24642014-08-15 20:49:592698 HttpResponseHeaders* new_response_headers =
2699 new HttpResponseHeaders(original_response_headers->raw_headers());
[email protected]5095cd72012-11-01 10:29:162700
2701 new_response_headers->RemoveHeader("Date");
2702 new_response_headers->AddHeader("Date: " + fixed_date_);
2703
2704 *override_response_headers = new_response_headers;
2705 return TestNetworkDelegate::OnHeadersReceived(request,
2706 callback,
2707 original_response_headers,
[email protected]5f714132014-03-26 10:41:162708 override_response_headers,
2709 allowed_unsafe_redirect_url);
[email protected]5095cd72012-11-01 10:29:162710}
2711
2712// Test that cookie expiration times are adjusted for server/client clock
2713// skew and that we handle incorrect timezone specifier "UTC" in HTTP Date
2714// headers by defaulting to GMT. (crbug.com/135131)
2715TEST_F(URLRequestTest, AcceptClockSkewCookieWithWrongDateTimezone) {
2716 LocalHttpTestServer test_server;
2717 ASSERT_TRUE(test_server.Start());
2718
2719 // Set up an expired cookie.
2720 {
2721 TestNetworkDelegate network_delegate;
2722 default_context_.set_network_delegate(&network_delegate);
2723 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192724 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
[email protected]2ca01e52013-10-31 22:05:192725 test_server.GetURL(
2726 "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"),
davidben151423e2015-03-23 18:48:362727 DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:192728 req->Start();
[email protected]255620da2013-08-19 13:14:292729 base::RunLoop().Run();
[email protected]5095cd72012-11-01 10:29:162730 }
2731 // Verify that the cookie is not set.
2732 {
2733 TestNetworkDelegate network_delegate;
2734 default_context_.set_network_delegate(&network_delegate);
2735 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192736 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:362737 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:192738 req->Start();
[email protected]255620da2013-08-19 13:14:292739 base::RunLoop().Run();
[email protected]5095cd72012-11-01 10:29:162740
2741 EXPECT_TRUE(d.data_received().find("StillGood=1") == std::string::npos);
2742 }
2743 // Set up a cookie with clock skew and "UTC" HTTP Date timezone specifier.
2744 {
2745 FixedDateNetworkDelegate network_delegate("18-Apr-1977 22:49:13 UTC");
2746 default_context_.set_network_delegate(&network_delegate);
2747 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192748 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
[email protected]2ca01e52013-10-31 22:05:192749 test_server.GetURL(
2750 "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"),
davidben151423e2015-03-23 18:48:362751 DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:192752 req->Start();
[email protected]255620da2013-08-19 13:14:292753 base::RunLoop().Run();
[email protected]5095cd72012-11-01 10:29:162754 }
2755 // Verify that the cookie is set.
2756 {
2757 TestNetworkDelegate network_delegate;
2758 default_context_.set_network_delegate(&network_delegate);
2759 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192760 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:362761 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:192762 req->Start();
[email protected]255620da2013-08-19 13:14:292763 base::RunLoop().Run();
[email protected]5095cd72012-11-01 10:29:162764
2765 EXPECT_TRUE(d.data_received().find("StillGood=1") != std::string::npos);
2766 }
2767}
2768
2769
[email protected]316c1e5e2012-09-12 15:17:442770// Check that it is impossible to change the referrer in the extra headers of
2771// an URLRequest.
2772TEST_F(URLRequestTest, DoNotOverrideReferrer) {
2773 LocalHttpTestServer test_server;
2774 ASSERT_TRUE(test_server.Start());
2775
2776 // If extra headers contain referer and the request contains a referer,
2777 // only the latter shall be respected.
2778 {
2779 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192780 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:362781 test_server.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:192782 req->SetReferrer("http://foo.com/");
[email protected]316c1e5e2012-09-12 15:17:442783
2784 HttpRequestHeaders headers;
2785 headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/");
[email protected]f7022f32014-08-21 16:32:192786 req->SetExtraRequestHeaders(headers);
[email protected]316c1e5e2012-09-12 15:17:442787
[email protected]f7022f32014-08-21 16:32:192788 req->Start();
[email protected]255620da2013-08-19 13:14:292789 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442790
2791 EXPECT_EQ("http://foo.com/", d.data_received());
2792 }
2793
2794 // If extra headers contain a referer but the request does not, no referer
2795 // shall be sent in the header.
2796 {
2797 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192798 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:362799 test_server.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d));
[email protected]316c1e5e2012-09-12 15:17:442800
2801 HttpRequestHeaders headers;
2802 headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/");
[email protected]f7022f32014-08-21 16:32:192803 req->SetExtraRequestHeaders(headers);
2804 req->SetLoadFlags(LOAD_VALIDATE_CACHE);
[email protected]316c1e5e2012-09-12 15:17:442805
[email protected]f7022f32014-08-21 16:32:192806 req->Start();
[email protected]255620da2013-08-19 13:14:292807 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442808
2809 EXPECT_EQ("None", d.data_received());
2810 }
2811}
2812
[email protected]b89290212009-08-14 22:37:352813class URLRequestTestHTTP : public URLRequestTest {
[email protected]95409e12010-08-17 20:07:112814 public:
2815 URLRequestTestHTTP()
[email protected]6cdfd7f2013-02-08 20:40:152816 : test_server_(base::FilePath(FILE_PATH_LITERAL(
[email protected]95409e12010-08-17 20:07:112817 "net/data/url_request_unittest"))) {
2818 }
2819
[email protected]b89290212009-08-14 22:37:352820 protected:
[email protected]21184962011-10-26 00:50:302821 // Requests |redirect_url|, which must return a HTTP 3xx redirect.
2822 // |request_method| is the method to use for the initial request.
2823 // |redirect_method| is the method that is expected to be used for the second
2824 // request, after redirection.
2825 // If |include_data| is true, data is uploaded with the request. The
2826 // response body is expected to match it exactly, if and only if
2827 // |request_method| == |redirect_method|.
2828 void HTTPRedirectMethodTest(const GURL& redirect_url,
2829 const std::string& request_method,
2830 const std::string& redirect_method,
2831 bool include_data) {
2832 static const char kData[] = "hello world";
2833 TestDelegate d;
davidben151423e2015-03-23 18:48:362834 scoped_ptr<URLRequest> req(
2835 default_context_.CreateRequest(redirect_url, DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:192836 req->set_method(request_method);
[email protected]21184962011-10-26 00:50:302837 if (include_data) {
mmenkecbc2b712014-10-09 20:29:072838 req->set_upload(CreateSimpleUploadData(kData));
[email protected]21184962011-10-26 00:50:302839 HttpRequestHeaders headers;
2840 headers.SetHeader(HttpRequestHeaders::kContentLength,
2841 base::UintToString(arraysize(kData) - 1));
[email protected]f7022f32014-08-21 16:32:192842 req->SetExtraRequestHeaders(headers);
[email protected]21184962011-10-26 00:50:302843 }
[email protected]f7022f32014-08-21 16:32:192844 req->Start();
[email protected]255620da2013-08-19 13:14:292845 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:192846 EXPECT_EQ(redirect_method, req->method());
2847 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
2848 EXPECT_EQ(OK, req->status().error());
[email protected]21184962011-10-26 00:50:302849 if (include_data) {
2850 if (request_method == redirect_method) {
2851 EXPECT_EQ(kData, d.data_received());
2852 } else {
2853 EXPECT_NE(kData, d.data_received());
2854 }
2855 }
2856 if (HasFailure())
2857 LOG(WARNING) << "Request method was: " << request_method;
2858 }
2859
jww5fe460ff2015-03-28 00:22:512860 // Requests |redirect_url|, which must return a HTTP 3xx redirect.
2861 // |request_method| is the method to use for the initial request.
2862 // |redirect_method| is the method that is expected to be used for the second
2863 // request, after redirection.
2864 // |origin_value| is the expected value for the Origin header after
2865 // redirection. If empty, expects that there will be no Origin header.
2866 void HTTPRedirectOriginHeaderTest(const GURL& redirect_url,
2867 const std::string& request_method,
2868 const std::string& redirect_method,
2869 const std::string& origin_value) {
2870 TestDelegate d;
2871 scoped_ptr<URLRequest> req(
2872 default_context_.CreateRequest(redirect_url, DEFAULT_PRIORITY, &d));
2873 req->set_method(request_method);
2874 req->SetExtraRequestHeaderByName(HttpRequestHeaders::kOrigin,
2875 redirect_url.GetOrigin().spec(), false);
2876 req->Start();
2877
2878 base::RunLoop().Run();
2879
2880 EXPECT_EQ(redirect_method, req->method());
2881 // Note that there is no check for request success here because, for
2882 // purposes of testing, the request very well may fail. For example, if the
2883 // test redirects to an HTTPS server from an HTTP origin, thus it is cross
2884 // origin, there is not an HTTPS server in this unit test framework, so the
2885 // request would fail. However, that's fine, as long as the request headers
2886 // are in order and pass the checks below.
2887 if (origin_value.empty()) {
2888 EXPECT_FALSE(
2889 req->extra_request_headers().HasHeader(HttpRequestHeaders::kOrigin));
2890 } else {
2891 std::string origin_header;
2892 EXPECT_TRUE(req->extra_request_headers().GetHeader(
2893 HttpRequestHeaders::kOrigin, &origin_header));
2894 EXPECT_EQ(origin_value, origin_header);
2895 }
2896 }
2897
[email protected]762d2db2010-01-11 19:03:012898 void HTTPUploadDataOperationTest(const std::string& method) {
[email protected]762d2db2010-01-11 19:03:012899 const int kMsgSize = 20000; // multiple of 10
2900 const int kIterations = 50;
[email protected]f43b89f32012-05-01 19:39:482901 char* uploadBytes = new char[kMsgSize+1];
2902 char* ptr = uploadBytes;
[email protected]762d2db2010-01-11 19:03:012903 char marker = 'a';
2904 for (int idx = 0; idx < kMsgSize/10; idx++) {
2905 memcpy(ptr, "----------", 10);
2906 ptr += 10;
2907 if (idx % 100 == 0) {
2908 ptr--;
2909 *ptr++ = marker;
2910 if (++marker > 'z')
2911 marker = 'a';
2912 }
2913 }
2914 uploadBytes[kMsgSize] = '\0';
2915
[email protected]762d2db2010-01-11 19:03:012916 for (int i = 0; i < kIterations; ++i) {
2917 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192918 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:362919 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:192920 r->set_method(method.c_str());
[email protected]762d2db2010-01-11 19:03:012921
mmenkecbc2b712014-10-09 20:29:072922 r->set_upload(CreateSimpleUploadData(uploadBytes));
[email protected]762d2db2010-01-11 19:03:012923
[email protected]f7022f32014-08-21 16:32:192924 r->Start();
2925 EXPECT_TRUE(r->is_pending());
[email protected]762d2db2010-01-11 19:03:012926
[email protected]255620da2013-08-19 13:14:292927 base::RunLoop().Run();
[email protected]762d2db2010-01-11 19:03:012928
[email protected]329b68b2012-11-14 17:54:272929 ASSERT_EQ(1, d.response_started_count())
[email protected]f7022f32014-08-21 16:32:192930 << "request failed: " << r->status().status()
2931 << ", os error: " << r->status().error();
[email protected]762d2db2010-01-11 19:03:012932
2933 EXPECT_FALSE(d.received_data_before_response());
2934 EXPECT_EQ(uploadBytes, d.data_received());
[email protected]762d2db2010-01-11 19:03:012935 }
2936 delete[] uploadBytes;
2937 }
2938
[email protected]ef2bf422012-05-11 03:27:092939 void AddChunksToUpload(URLRequest* r) {
[email protected]0c9bf872011-03-04 17:53:222940 r->AppendChunkToUpload("a", 1, false);
2941 r->AppendChunkToUpload("bcd", 3, false);
2942 r->AppendChunkToUpload("this is a longer chunk than before.", 35, false);
2943 r->AppendChunkToUpload("\r\n\r\n", 4, false);
2944 r->AppendChunkToUpload("0", 1, false);
2945 r->AppendChunkToUpload("2323", 4, true);
[email protected]699efe602011-01-25 07:17:112946 }
2947
[email protected]ef2bf422012-05-11 03:27:092948 void VerifyReceivedDataMatchesChunks(URLRequest* r, TestDelegate* d) {
[email protected]699efe602011-01-25 07:17:112949 // This should match the chunks sent by AddChunksToUpload().
[email protected]329b68b2012-11-14 17:54:272950 const std::string expected_data =
[email protected]699efe602011-01-25 07:17:112951 "abcdthis is a longer chunk than before.\r\n\r\n02323";
2952
[email protected]329b68b2012-11-14 17:54:272953 ASSERT_EQ(1, d->response_started_count())
2954 << "request failed: " << r->status().status()
2955 << ", os error: " << r->status().error();
[email protected]699efe602011-01-25 07:17:112956
2957 EXPECT_FALSE(d->received_data_before_response());
2958
[email protected]329b68b2012-11-14 17:54:272959 EXPECT_EQ(expected_data.size(), static_cast<size_t>(d->bytes_received()));
2960 EXPECT_EQ(expected_data, d->data_received());
[email protected]699efe602011-01-25 07:17:112961 }
2962
[email protected]ede03212012-09-07 12:52:262963 bool DoManyCookiesRequest(int num_cookies) {
[email protected]263163f2012-06-14 22:40:342964 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192965 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
2966 test_server_.GetURL("set-many-cookies?" +
davidben151423e2015-03-23 18:48:362967 base::IntToString(num_cookies)),
2968 DEFAULT_PRIORITY, &d));
[email protected]263163f2012-06-14 22:40:342969
[email protected]f7022f32014-08-21 16:32:192970 r->Start();
2971 EXPECT_TRUE(r->is_pending());
[email protected]263163f2012-06-14 22:40:342972
[email protected]255620da2013-08-19 13:14:292973 base::RunLoop().Run();
[email protected]263163f2012-06-14 22:40:342974
[email protected]f7022f32014-08-21 16:32:192975 bool is_success = r->status().is_success();
[email protected]263163f2012-06-14 22:40:342976
[email protected]ede03212012-09-07 12:52:262977 if (!is_success) {
[email protected]f7022f32014-08-21 16:32:192978 EXPECT_TRUE(r->status().error() == ERR_RESPONSE_HEADERS_TOO_BIG);
[email protected]263163f2012-06-14 22:40:342979 // The test server appears to be unable to handle subsequent requests
2980 // after this error is triggered. Force it to restart.
2981 EXPECT_TRUE(test_server_.Stop());
2982 EXPECT_TRUE(test_server_.Start());
2983 }
2984
2985 return is_success;
2986 }
2987
bengr1bf8e942014-11-07 01:36:502988 LocalHttpTestServer* test_server() {
2989 return &test_server_;
2990 }
2991
2992 protected:
[email protected]1700c6a2012-02-22 18:07:072993 LocalHttpTestServer test_server_;
[email protected]b89290212009-08-14 22:37:352994};
2995
[email protected]95409e12010-08-17 20:07:112996// In this unit test, we're using the HTTPTestServer as a proxy server and
2997// issuing a CONNECT request with the magic host name "www.redirect.com".
2998// The HTTPTestServer will return a 302 response, which we should not
2999// follow.
[email protected]f2f31b32013-01-16 23:24:093000TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) {
[email protected]95409e12010-08-17 20:07:113001 ASSERT_TRUE(test_server_.Start());
3002
[email protected]ceefd7fd2012-11-29 00:36:243003 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]d5a4dd62012-05-23 01:41:043004 TestURLRequestContextWithProxy context(
[email protected]2ca01e52013-10-31 22:05:193005 test_server_.host_port_pair().ToString(), &network_delegate);
[email protected]87a09a92011-07-14 15:50:503006
[email protected]d1ec59082009-02-11 02:48:153007 TestDelegate d;
3008 {
[email protected]f7022f32014-08-21 16:32:193009 scoped_ptr<URLRequest> r(context.CreateRequest(
davidben151423e2015-03-23 18:48:363010 GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:193011 r->Start();
3012 EXPECT_TRUE(r->is_pending());
[email protected]d1ec59082009-02-11 02:48:153013
[email protected]255620da2013-08-19 13:14:293014 base::RunLoop().Run();
[email protected]d1ec59082009-02-11 02:48:153015
[email protected]f7022f32014-08-21 16:32:193016 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
[email protected]d8fc4722014-06-13 13:17:153017 // The proxy server is not set before failure.
[email protected]f7022f32014-08-21 16:32:193018 EXPECT_TRUE(r->proxy_server().IsEmpty());
3019 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r->status().error());
[email protected]dc651782009-02-14 01:45:083020 EXPECT_EQ(1, d.response_started_count());
[email protected]d1ec59082009-02-11 02:48:153021 // We should not have followed the redirect.
3022 EXPECT_EQ(0, d.received_redirect_count());
3023 }
3024}
3025
[email protected]8202d0c2011-02-23 08:31:143026// This is the same as the previous test, but checks that the network delegate
3027// registers the error.
[email protected]c044616e2013-02-20 02:01:263028TEST_F(URLRequestTestHTTP, NetworkDelegateTunnelConnectionFailed) {
[email protected]8202d0c2011-02-23 08:31:143029 ASSERT_TRUE(test_server_.Start());
3030
[email protected]ceefd7fd2012-11-29 00:36:243031 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]d5a4dd62012-05-23 01:41:043032 TestURLRequestContextWithProxy context(
[email protected]2ca01e52013-10-31 22:05:193033 test_server_.host_port_pair().ToString(), &network_delegate);
[email protected]87a09a92011-07-14 15:50:503034
[email protected]8202d0c2011-02-23 08:31:143035 TestDelegate d;
3036 {
[email protected]f7022f32014-08-21 16:32:193037 scoped_ptr<URLRequest> r(context.CreateRequest(
davidben151423e2015-03-23 18:48:363038 GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:193039 r->Start();
3040 EXPECT_TRUE(r->is_pending());
[email protected]8202d0c2011-02-23 08:31:143041
[email protected]255620da2013-08-19 13:14:293042 base::RunLoop().Run();
[email protected]8202d0c2011-02-23 08:31:143043
[email protected]f7022f32014-08-21 16:32:193044 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
[email protected]d8fc4722014-06-13 13:17:153045 // The proxy server is not set before failure.
[email protected]f7022f32014-08-21 16:32:193046 EXPECT_TRUE(r->proxy_server().IsEmpty());
3047 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r->status().error());
[email protected]8202d0c2011-02-23 08:31:143048 EXPECT_EQ(1, d.response_started_count());
3049 // We should not have followed the redirect.
3050 EXPECT_EQ(0, d.received_redirect_count());
3051
3052 EXPECT_EQ(1, network_delegate.error_count());
[email protected]d0cc35b2011-09-08 12:02:053053 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, network_delegate.last_error());
[email protected]8202d0c2011-02-23 08:31:143054 }
3055}
3056
[email protected]dc5a5cf2012-09-26 02:49:303057// Tests that we can block and asynchronously return OK in various stages.
3058TEST_F(URLRequestTestHTTP, NetworkDelegateBlockAsynchronously) {
3059 static const BlockingNetworkDelegate::Stage blocking_stages[] = {
3060 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
3061 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
3062 BlockingNetworkDelegate::ON_HEADERS_RECEIVED
3063 };
3064 static const size_t blocking_stages_length = arraysize(blocking_stages);
3065
3066 ASSERT_TRUE(test_server_.Start());
3067
3068 TestDelegate d;
3069 BlockingNetworkDelegate network_delegate(
3070 BlockingNetworkDelegate::USER_CALLBACK);
3071 network_delegate.set_block_on(
3072 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST |
3073 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS |
3074 BlockingNetworkDelegate::ON_HEADERS_RECEIVED);
3075
3076 TestURLRequestContext context(true);
3077 context.set_network_delegate(&network_delegate);
3078 context.Init();
3079
3080 {
[email protected]f7022f32014-08-21 16:32:193081 scoped_ptr<URLRequest> r(context.CreateRequest(
davidben151423e2015-03-23 18:48:363082 test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d));
[email protected]dc5a5cf2012-09-26 02:49:303083
[email protected]f7022f32014-08-21 16:32:193084 r->Start();
[email protected]dc5a5cf2012-09-26 02:49:303085 for (size_t i = 0; i < blocking_stages_length; ++i) {
[email protected]255620da2013-08-19 13:14:293086 base::RunLoop().Run();
[email protected]dc5a5cf2012-09-26 02:49:303087 EXPECT_EQ(blocking_stages[i],
3088 network_delegate.stage_blocked_for_callback());
3089 network_delegate.DoCallback(OK);
3090 }
[email protected]255620da2013-08-19 13:14:293091 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:193092 EXPECT_EQ(200, r->GetResponseCode());
3093 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
[email protected]dc5a5cf2012-09-26 02:49:303094 EXPECT_EQ(1, network_delegate.created_requests());
3095 EXPECT_EQ(0, network_delegate.destroyed_requests());
3096 }
3097 EXPECT_EQ(1, network_delegate.destroyed_requests());
3098}
3099
[email protected]4c76d7c2011-04-15 19:14:123100// Tests that the network delegate can block and cancel a request.
3101TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) {
3102 ASSERT_TRUE(test_server_.Start());
3103
3104 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303105 BlockingNetworkDelegate network_delegate(
3106 BlockingNetworkDelegate::AUTO_CALLBACK);
3107 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
3108 network_delegate.set_retval(ERR_EMPTY_RESPONSE);
[email protected]4c76d7c2011-04-15 19:14:123109
[email protected]d5a4dd62012-05-23 01:41:043110 TestURLRequestContextWithProxy context(
[email protected]2ca01e52013-10-31 22:05:193111 test_server_.host_port_pair().ToString(), &network_delegate);
[email protected]87a09a92011-07-14 15:50:503112
[email protected]4c76d7c2011-04-15 19:14:123113 {
[email protected]f7022f32014-08-21 16:32:193114 scoped_ptr<URLRequest> r(context.CreateRequest(
davidben151423e2015-03-23 18:48:363115 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d));
[email protected]4c76d7c2011-04-15 19:14:123116
[email protected]f7022f32014-08-21 16:32:193117 r->Start();
[email protected]255620da2013-08-19 13:14:293118 base::RunLoop().Run();
[email protected]4c76d7c2011-04-15 19:14:123119
[email protected]f7022f32014-08-21 16:32:193120 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
[email protected]d8fc4722014-06-13 13:17:153121 // The proxy server is not set before cancellation.
[email protected]f7022f32014-08-21 16:32:193122 EXPECT_TRUE(r->proxy_server().IsEmpty());
3123 EXPECT_EQ(ERR_EMPTY_RESPONSE, r->status().error());
[email protected]4c76d7c2011-04-15 19:14:123124 EXPECT_EQ(1, network_delegate.created_requests());
3125 EXPECT_EQ(0, network_delegate.destroyed_requests());
3126 }
3127 EXPECT_EQ(1, network_delegate.destroyed_requests());
3128}
3129
[email protected]b4438d32012-09-27 06:15:303130// Helper function for NetworkDelegateCancelRequestAsynchronously and
3131// NetworkDelegateCancelRequestSynchronously. Sets up a blocking network
3132// delegate operating in |block_mode| and a request for |url|. It blocks the
3133// request in |stage| and cancels it with ERR_BLOCKED_BY_CLIENT.
3134void NetworkDelegateCancelRequest(BlockingNetworkDelegate::BlockMode block_mode,
3135 BlockingNetworkDelegate::Stage stage,
3136 const GURL& url) {
[email protected]3cd384c602011-08-31 16:12:363137 TestDelegate d;
[email protected]b4438d32012-09-27 06:15:303138 BlockingNetworkDelegate network_delegate(block_mode);
3139 network_delegate.set_retval(ERR_BLOCKED_BY_CLIENT);
3140 network_delegate.set_block_on(stage);
[email protected]3cd384c602011-08-31 16:12:363141
[email protected]b4438d32012-09-27 06:15:303142 TestURLRequestContext context(true);
3143 context.set_network_delegate(&network_delegate);
3144 context.Init();
[email protected]3cd384c602011-08-31 16:12:363145
3146 {
davidben151423e2015-03-23 18:48:363147 scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d));
[email protected]3cd384c602011-08-31 16:12:363148
[email protected]f7022f32014-08-21 16:32:193149 r->Start();
[email protected]255620da2013-08-19 13:14:293150 base::RunLoop().Run();
[email protected]3cd384c602011-08-31 16:12:363151
[email protected]f7022f32014-08-21 16:32:193152 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
[email protected]d8fc4722014-06-13 13:17:153153 // The proxy server is not set before cancellation.
[email protected]f7022f32014-08-21 16:32:193154 EXPECT_TRUE(r->proxy_server().IsEmpty());
3155 EXPECT_EQ(ERR_BLOCKED_BY_CLIENT, r->status().error());
[email protected]3cd384c602011-08-31 16:12:363156 EXPECT_EQ(1, network_delegate.created_requests());
3157 EXPECT_EQ(0, network_delegate.destroyed_requests());
3158 }
3159 EXPECT_EQ(1, network_delegate.destroyed_requests());
3160}
3161
[email protected]b4438d32012-09-27 06:15:303162// The following 3 tests check that the network delegate can cancel a request
3163// synchronously in various stages of the request.
3164TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously1) {
3165 ASSERT_TRUE(test_server_.Start());
3166 NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS,
3167 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
[email protected]007b3f82013-04-09 08:46:453168 test_server_.GetURL(std::string()));
[email protected]b4438d32012-09-27 06:15:303169}
3170
3171TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously2) {
3172 ASSERT_TRUE(test_server_.Start());
3173 NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS,
3174 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
[email protected]007b3f82013-04-09 08:46:453175 test_server_.GetURL(std::string()));
[email protected]b4438d32012-09-27 06:15:303176}
3177
3178TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously3) {
3179 ASSERT_TRUE(test_server_.Start());
3180 NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS,
3181 BlockingNetworkDelegate::ON_HEADERS_RECEIVED,
[email protected]007b3f82013-04-09 08:46:453182 test_server_.GetURL(std::string()));
[email protected]b4438d32012-09-27 06:15:303183}
3184
3185// The following 3 tests check that the network delegate can cancel a request
3186// asynchronously in various stages of the request.
3187TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously1) {
3188 ASSERT_TRUE(test_server_.Start());
3189 NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK,
3190 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
[email protected]007b3f82013-04-09 08:46:453191 test_server_.GetURL(std::string()));
[email protected]b4438d32012-09-27 06:15:303192}
3193
3194TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously2) {
3195 ASSERT_TRUE(test_server_.Start());
3196 NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK,
3197 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
[email protected]007b3f82013-04-09 08:46:453198 test_server_.GetURL(std::string()));
[email protected]b4438d32012-09-27 06:15:303199}
3200
3201TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously3) {
3202 ASSERT_TRUE(test_server_.Start());
3203 NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK,
3204 BlockingNetworkDelegate::ON_HEADERS_RECEIVED,
[email protected]007b3f82013-04-09 08:46:453205 test_server_.GetURL(std::string()));
[email protected]b4438d32012-09-27 06:15:303206}
3207
[email protected]4c76d7c2011-04-15 19:14:123208// Tests that the network delegate can block and redirect a request to a new
3209// URL.
3210TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) {
3211 ASSERT_TRUE(test_server_.Start());
3212
3213 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303214 BlockingNetworkDelegate network_delegate(
3215 BlockingNetworkDelegate::AUTO_CALLBACK);
3216 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
[email protected]4c76d7c2011-04-15 19:14:123217 GURL redirect_url(test_server_.GetURL("simple.html"));
3218 network_delegate.set_redirect_url(redirect_url);
3219
[email protected]d5a4dd62012-05-23 01:41:043220 TestURLRequestContextWithProxy context(
[email protected]2ca01e52013-10-31 22:05:193221 test_server_.host_port_pair().ToString(), &network_delegate);
[email protected]87a09a92011-07-14 15:50:503222
[email protected]4c76d7c2011-04-15 19:14:123223 {
[email protected]8f1ac082011-04-19 21:14:133224 GURL original_url(test_server_.GetURL("empty.html"));
davidben151423e2015-03-23 18:48:363225 scoped_ptr<URLRequest> r(
3226 context.CreateRequest(original_url, DEFAULT_PRIORITY, &d));
[email protected]4c76d7c2011-04-15 19:14:123227
[email protected]6be6fa92014-08-06 23:44:563228 // Quit after hitting the redirect, so can check the headers.
3229 d.set_quit_on_redirect(true);
[email protected]f7022f32014-08-21 16:32:193230 r->Start();
[email protected]255620da2013-08-19 13:14:293231 base::RunLoop().Run();
[email protected]4c76d7c2011-04-15 19:14:123232
[email protected]6be6fa92014-08-06 23:44:563233 // Check headers from URLRequestJob.
[email protected]f7022f32014-08-21 16:32:193234 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3235 EXPECT_EQ(307, r->GetResponseCode());
3236 EXPECT_EQ(307, r->response_headers()->response_code());
[email protected]6be6fa92014-08-06 23:44:563237 std::string location;
[email protected]f7022f32014-08-21 16:32:193238 ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location",
3239 &location));
[email protected]6be6fa92014-08-06 23:44:563240 EXPECT_EQ(redirect_url, GURL(location));
3241
3242 // Let the request finish.
[email protected]f7022f32014-08-21 16:32:193243 r->FollowDeferredRedirect();
[email protected]6be6fa92014-08-06 23:44:563244 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:193245 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3246 EXPECT_TRUE(r->proxy_server().Equals(test_server_.host_port_pair()));
[email protected]597a1ab2014-06-26 08:12:273247 EXPECT_EQ(
3248 1, network_delegate.observed_before_proxy_headers_sent_callbacks());
3249 EXPECT_TRUE(
3250 network_delegate.last_observed_proxy().Equals(
3251 test_server_.host_port_pair()));
3252
[email protected]f7022f32014-08-21 16:32:193253 EXPECT_EQ(0, r->status().error());
3254 EXPECT_EQ(redirect_url, r->url());
3255 EXPECT_EQ(original_url, r->original_url());
3256 EXPECT_EQ(2U, r->url_chain().size());
[email protected]4c76d7c2011-04-15 19:14:123257 EXPECT_EQ(1, network_delegate.created_requests());
3258 EXPECT_EQ(0, network_delegate.destroyed_requests());
3259 }
3260 EXPECT_EQ(1, network_delegate.destroyed_requests());
3261}
3262
[email protected]b813ed72012-04-05 08:21:363263// Tests that the network delegate can block and redirect a request to a new
3264// URL by setting a redirect_url and returning in OnBeforeURLRequest directly.
3265TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) {
3266 ASSERT_TRUE(test_server_.Start());
3267
3268 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303269 BlockingNetworkDelegate network_delegate(
3270 BlockingNetworkDelegate::SYNCHRONOUS);
[email protected]b813ed72012-04-05 08:21:363271 GURL redirect_url(test_server_.GetURL("simple.html"));
3272 network_delegate.set_redirect_url(redirect_url);
[email protected]b813ed72012-04-05 08:21:363273
[email protected]d5a4dd62012-05-23 01:41:043274 TestURLRequestContextWithProxy context(
[email protected]2ca01e52013-10-31 22:05:193275 test_server_.host_port_pair().ToString(), &network_delegate);
[email protected]b813ed72012-04-05 08:21:363276
3277 {
3278 GURL original_url(test_server_.GetURL("empty.html"));
davidben151423e2015-03-23 18:48:363279 scoped_ptr<URLRequest> r(
3280 context.CreateRequest(original_url, DEFAULT_PRIORITY, &d));
[email protected]b813ed72012-04-05 08:21:363281
[email protected]6be6fa92014-08-06 23:44:563282 // Quit after hitting the redirect, so can check the headers.
3283 d.set_quit_on_redirect(true);
[email protected]f7022f32014-08-21 16:32:193284 r->Start();
[email protected]255620da2013-08-19 13:14:293285 base::RunLoop().Run();
[email protected]b813ed72012-04-05 08:21:363286
[email protected]6be6fa92014-08-06 23:44:563287 // Check headers from URLRequestJob.
[email protected]f7022f32014-08-21 16:32:193288 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3289 EXPECT_EQ(307, r->GetResponseCode());
3290 EXPECT_EQ(307, r->response_headers()->response_code());
[email protected]6be6fa92014-08-06 23:44:563291 std::string location;
[email protected]f7022f32014-08-21 16:32:193292 ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location",
3293 &location));
[email protected]6be6fa92014-08-06 23:44:563294 EXPECT_EQ(redirect_url, GURL(location));
3295
3296 // Let the request finish.
[email protected]f7022f32014-08-21 16:32:193297 r->FollowDeferredRedirect();
[email protected]6be6fa92014-08-06 23:44:563298 base::RunLoop().Run();
3299
[email protected]f7022f32014-08-21 16:32:193300 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3301 EXPECT_TRUE(r->proxy_server().Equals(test_server_.host_port_pair()));
[email protected]597a1ab2014-06-26 08:12:273302 EXPECT_EQ(
3303 1, network_delegate.observed_before_proxy_headers_sent_callbacks());
3304 EXPECT_TRUE(
3305 network_delegate.last_observed_proxy().Equals(
3306 test_server_.host_port_pair()));
[email protected]f7022f32014-08-21 16:32:193307 EXPECT_EQ(0, r->status().error());
3308 EXPECT_EQ(redirect_url, r->url());
3309 EXPECT_EQ(original_url, r->original_url());
3310 EXPECT_EQ(2U, r->url_chain().size());
[email protected]b813ed72012-04-05 08:21:363311 EXPECT_EQ(1, network_delegate.created_requests());
3312 EXPECT_EQ(0, network_delegate.destroyed_requests());
3313 }
3314 EXPECT_EQ(1, network_delegate.destroyed_requests());
3315}
3316
[email protected]3c5ca8c2011-09-29 01:14:513317// Tests that redirects caused by the network delegate preserve POST data.
3318TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) {
3319 ASSERT_TRUE(test_server_.Start());
3320
3321 const char kData[] = "hello world";
3322
3323 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303324 BlockingNetworkDelegate network_delegate(
3325 BlockingNetworkDelegate::AUTO_CALLBACK);
3326 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
[email protected]3c5ca8c2011-09-29 01:14:513327 GURL redirect_url(test_server_.GetURL("echo"));
3328 network_delegate.set_redirect_url(redirect_url);
3329
[email protected]ef2bf422012-05-11 03:27:093330 TestURLRequestContext context(true);
3331 context.set_network_delegate(&network_delegate);
3332 context.Init();
[email protected]3c5ca8c2011-09-29 01:14:513333
3334 {
3335 GURL original_url(test_server_.GetURL("empty.html"));
davidben151423e2015-03-23 18:48:363336 scoped_ptr<URLRequest> r(
3337 context.CreateRequest(original_url, DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:193338 r->set_method("POST");
mmenkecbc2b712014-10-09 20:29:073339 r->set_upload(CreateSimpleUploadData(kData));
[email protected]3c5ca8c2011-09-29 01:14:513340 HttpRequestHeaders headers;
3341 headers.SetHeader(HttpRequestHeaders::kContentLength,
3342 base::UintToString(arraysize(kData) - 1));
[email protected]f7022f32014-08-21 16:32:193343 r->SetExtraRequestHeaders(headers);
[email protected]6be6fa92014-08-06 23:44:563344
3345 // Quit after hitting the redirect, so can check the headers.
3346 d.set_quit_on_redirect(true);
[email protected]f7022f32014-08-21 16:32:193347 r->Start();
[email protected]255620da2013-08-19 13:14:293348 base::RunLoop().Run();
[email protected]3c5ca8c2011-09-29 01:14:513349
[email protected]6be6fa92014-08-06 23:44:563350 // Check headers from URLRequestJob.
[email protected]f7022f32014-08-21 16:32:193351 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3352 EXPECT_EQ(307, r->GetResponseCode());
3353 EXPECT_EQ(307, r->response_headers()->response_code());
[email protected]6be6fa92014-08-06 23:44:563354 std::string location;
[email protected]f7022f32014-08-21 16:32:193355 ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location",
3356 &location));
[email protected]6be6fa92014-08-06 23:44:563357 EXPECT_EQ(redirect_url, GURL(location));
3358
3359 // Let the request finish.
[email protected]f7022f32014-08-21 16:32:193360 r->FollowDeferredRedirect();
[email protected]6be6fa92014-08-06 23:44:563361 base::RunLoop().Run();
3362
[email protected]f7022f32014-08-21 16:32:193363 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3364 EXPECT_EQ(0, r->status().error());
3365 EXPECT_EQ(redirect_url, r->url());
3366 EXPECT_EQ(original_url, r->original_url());
3367 EXPECT_EQ(2U, r->url_chain().size());
[email protected]3c5ca8c2011-09-29 01:14:513368 EXPECT_EQ(1, network_delegate.created_requests());
3369 EXPECT_EQ(0, network_delegate.destroyed_requests());
[email protected]f7022f32014-08-21 16:32:193370 EXPECT_EQ("POST", r->method());
[email protected]3c5ca8c2011-09-29 01:14:513371 EXPECT_EQ(kData, d.data_received());
3372 }
3373 EXPECT_EQ(1, network_delegate.destroyed_requests());
3374}
3375
[email protected]5f714132014-03-26 10:41:163376// Tests that the network delegate can block and redirect a request to a new
3377// URL during OnHeadersReceived.
3378TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestOnHeadersReceived) {
3379 ASSERT_TRUE(test_server_.Start());
3380
3381 TestDelegate d;
3382 BlockingNetworkDelegate network_delegate(
3383 BlockingNetworkDelegate::AUTO_CALLBACK);
3384 network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED);
3385 GURL redirect_url(test_server_.GetURL("simple.html"));
3386 network_delegate.set_redirect_on_headers_received_url(redirect_url);
3387
3388 TestURLRequestContextWithProxy context(
3389 test_server_.host_port_pair().ToString(), &network_delegate);
3390
3391 {
3392 GURL original_url(test_server_.GetURL("empty.html"));
davidben151423e2015-03-23 18:48:363393 scoped_ptr<URLRequest> r(
3394 context.CreateRequest(original_url, DEFAULT_PRIORITY, &d));
[email protected]5f714132014-03-26 10:41:163395
[email protected]f7022f32014-08-21 16:32:193396 r->Start();
[email protected]5f714132014-03-26 10:41:163397 base::RunLoop().Run();
3398
[email protected]f7022f32014-08-21 16:32:193399 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3400 EXPECT_TRUE(r->proxy_server().Equals(test_server_.host_port_pair()));
[email protected]597a1ab2014-06-26 08:12:273401 EXPECT_EQ(
3402 2, network_delegate.observed_before_proxy_headers_sent_callbacks());
3403 EXPECT_TRUE(
3404 network_delegate.last_observed_proxy().Equals(
3405 test_server_.host_port_pair()));
[email protected]f7022f32014-08-21 16:32:193406
3407 EXPECT_EQ(OK, r->status().error());
3408 EXPECT_EQ(redirect_url, r->url());
3409 EXPECT_EQ(original_url, r->original_url());
3410 EXPECT_EQ(2U, r->url_chain().size());
[email protected]5f714132014-03-26 10:41:163411 EXPECT_EQ(2, network_delegate.created_requests());
3412 EXPECT_EQ(0, network_delegate.destroyed_requests());
3413 }
3414 EXPECT_EQ(1, network_delegate.destroyed_requests());
3415}
3416
[email protected]c2911d72011-10-03 22:16:363417// Tests that the network delegate can synchronously complete OnAuthRequired
3418// by taking no action. This indicates that the NetworkDelegate does not want to
3419// handle the challenge, and is passing the buck along to the
3420// URLRequest::Delegate.
3421TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) {
3422 ASSERT_TRUE(test_server_.Start());
3423
3424 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303425 BlockingNetworkDelegate network_delegate(
3426 BlockingNetworkDelegate::SYNCHRONOUS);
[email protected]c2911d72011-10-03 22:16:363427
[email protected]ef2bf422012-05-11 03:27:093428 TestURLRequestContext context(true);
3429 context.set_network_delegate(&network_delegate);
3430 context.Init();
[email protected]c2911d72011-10-03 22:16:363431
[email protected]f3cf9802011-10-28 18:44:583432 d.set_credentials(AuthCredentials(kUser, kSecret));
[email protected]c2911d72011-10-03 22:16:363433
3434 {
3435 GURL url(test_server_.GetURL("auth-basic"));
davidben151423e2015-03-23 18:48:363436 scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:193437 r->Start();
[email protected]79e1fd62013-06-20 06:50:043438
[email protected]255620da2013-08-19 13:14:293439 base::RunLoop().Run();
[email protected]79e1fd62013-06-20 06:50:043440
[email protected]f7022f32014-08-21 16:32:193441 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3442 EXPECT_EQ(0, r->status().error());
3443 EXPECT_EQ(200, r->GetResponseCode());
[email protected]79e1fd62013-06-20 06:50:043444 EXPECT_TRUE(d.auth_required_called());
3445 EXPECT_EQ(1, network_delegate.created_requests());
3446 EXPECT_EQ(0, network_delegate.destroyed_requests());
3447 }
3448 EXPECT_EQ(1, network_delegate.destroyed_requests());
3449}
3450
3451TEST_F(URLRequestTestHTTP,
3452 NetworkDelegateOnAuthRequiredSyncNoAction_GetFullRequestHeaders) {
3453 ASSERT_TRUE(test_server_.Start());
3454
3455 TestDelegate d;
3456 BlockingNetworkDelegate network_delegate(
3457 BlockingNetworkDelegate::SYNCHRONOUS);
3458
3459 TestURLRequestContext context(true);
3460 context.set_network_delegate(&network_delegate);
3461 context.Init();
3462
3463 d.set_credentials(AuthCredentials(kUser, kSecret));
3464
3465 {
3466 GURL url(test_server_.GetURL("auth-basic"));
davidben151423e2015-03-23 18:48:363467 scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:193468 r->Start();
[email protected]79e1fd62013-06-20 06:50:043469
3470 {
3471 HttpRequestHeaders headers;
[email protected]f7022f32014-08-21 16:32:193472 EXPECT_TRUE(r->GetFullRequestHeaders(&headers));
[email protected]79e1fd62013-06-20 06:50:043473 EXPECT_FALSE(headers.HasHeader("Authorization"));
3474 }
3475
[email protected]255620da2013-08-19 13:14:293476 base::RunLoop().Run();
[email protected]c2911d72011-10-03 22:16:363477
[email protected]f7022f32014-08-21 16:32:193478 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3479 EXPECT_EQ(0, r->status().error());
3480 EXPECT_EQ(200, r->GetResponseCode());
[email protected]c2911d72011-10-03 22:16:363481 EXPECT_TRUE(d.auth_required_called());
3482 EXPECT_EQ(1, network_delegate.created_requests());
3483 EXPECT_EQ(0, network_delegate.destroyed_requests());
3484 }
3485 EXPECT_EQ(1, network_delegate.destroyed_requests());
3486}
3487
3488// Tests that the network delegate can synchronously complete OnAuthRequired
[email protected]1e110eae2013-05-10 22:02:403489// by setting credentials.
[email protected]c2911d72011-10-03 22:16:363490TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) {
3491 ASSERT_TRUE(test_server_.Start());
3492
3493 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303494 BlockingNetworkDelegate network_delegate(
3495 BlockingNetworkDelegate::SYNCHRONOUS);
3496 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]c2911d72011-10-03 22:16:363497 network_delegate.set_auth_retval(
3498 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
3499
[email protected]f3cf9802011-10-28 18:44:583500 network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret));
[email protected]c2911d72011-10-03 22:16:363501
[email protected]ef2bf422012-05-11 03:27:093502 TestURLRequestContext context(true);
3503 context.set_network_delegate(&network_delegate);
3504 context.Init();
[email protected]c2911d72011-10-03 22:16:363505
3506 {
3507 GURL url(test_server_.GetURL("auth-basic"));
davidben151423e2015-03-23 18:48:363508 scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:193509 r->Start();
[email protected]255620da2013-08-19 13:14:293510 base::RunLoop().Run();
[email protected]c2911d72011-10-03 22:16:363511
[email protected]f7022f32014-08-21 16:32:193512 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3513 EXPECT_EQ(0, r->status().error());
3514 EXPECT_EQ(200, r->GetResponseCode());
[email protected]c2911d72011-10-03 22:16:363515 EXPECT_FALSE(d.auth_required_called());
3516 EXPECT_EQ(1, network_delegate.created_requests());
3517 EXPECT_EQ(0, network_delegate.destroyed_requests());
3518 }
3519 EXPECT_EQ(1, network_delegate.destroyed_requests());
3520}
3521
[email protected]79e1fd62013-06-20 06:50:043522// Same as above, but also tests that GetFullRequestHeaders returns the proper
3523// headers (for the first or second request) when called at the proper times.
3524TEST_F(URLRequestTestHTTP,
3525 NetworkDelegateOnAuthRequiredSyncSetAuth_GetFullRequestHeaders) {
3526 ASSERT_TRUE(test_server_.Start());
3527
3528 TestDelegate d;
3529 BlockingNetworkDelegate network_delegate(
3530 BlockingNetworkDelegate::SYNCHRONOUS);
3531 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
3532 network_delegate.set_auth_retval(
3533 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
3534
3535 network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret));
3536
3537 TestURLRequestContext context(true);
3538 context.set_network_delegate(&network_delegate);
3539 context.Init();
3540
3541 {
3542 GURL url(test_server_.GetURL("auth-basic"));
davidben151423e2015-03-23 18:48:363543 scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:193544 r->Start();
[email protected]255620da2013-08-19 13:14:293545 base::RunLoop().Run();
[email protected]79e1fd62013-06-20 06:50:043546
[email protected]f7022f32014-08-21 16:32:193547 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3548 EXPECT_EQ(0, r->status().error());
3549 EXPECT_EQ(200, r->GetResponseCode());
[email protected]79e1fd62013-06-20 06:50:043550 EXPECT_FALSE(d.auth_required_called());
3551 EXPECT_EQ(1, network_delegate.created_requests());
3552 EXPECT_EQ(0, network_delegate.destroyed_requests());
3553
3554 {
3555 HttpRequestHeaders headers;
[email protected]f7022f32014-08-21 16:32:193556 EXPECT_TRUE(r->GetFullRequestHeaders(&headers));
[email protected]79e1fd62013-06-20 06:50:043557 EXPECT_TRUE(headers.HasHeader("Authorization"));
3558 }
3559 }
3560 EXPECT_EQ(1, network_delegate.destroyed_requests());
3561}
3562
[email protected]c2911d72011-10-03 22:16:363563// Tests that the network delegate can synchronously complete OnAuthRequired
3564// by cancelling authentication.
3565TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) {
3566 ASSERT_TRUE(test_server_.Start());
3567
3568 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303569 BlockingNetworkDelegate network_delegate(
3570 BlockingNetworkDelegate::SYNCHRONOUS);
3571 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]c2911d72011-10-03 22:16:363572 network_delegate.set_auth_retval(
3573 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH);
3574
[email protected]ef2bf422012-05-11 03:27:093575 TestURLRequestContext context(true);
3576 context.set_network_delegate(&network_delegate);
3577 context.Init();
[email protected]c2911d72011-10-03 22:16:363578
3579 {
3580 GURL url(test_server_.GetURL("auth-basic"));
davidben151423e2015-03-23 18:48:363581 scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:193582 r->Start();
[email protected]255620da2013-08-19 13:14:293583 base::RunLoop().Run();
[email protected]c2911d72011-10-03 22:16:363584
[email protected]f7022f32014-08-21 16:32:193585 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3586 EXPECT_EQ(OK, r->status().error());
3587 EXPECT_EQ(401, r->GetResponseCode());
[email protected]c2911d72011-10-03 22:16:363588 EXPECT_FALSE(d.auth_required_called());
3589 EXPECT_EQ(1, network_delegate.created_requests());
3590 EXPECT_EQ(0, network_delegate.destroyed_requests());
3591 }
3592 EXPECT_EQ(1, network_delegate.destroyed_requests());
3593}
3594
3595// Tests that the network delegate can asynchronously complete OnAuthRequired
3596// by taking no action. This indicates that the NetworkDelegate does not want
3597// to handle the challenge, and is passing the buck along to the
3598// URLRequest::Delegate.
3599TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) {
3600 ASSERT_TRUE(test_server_.Start());
3601
3602 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303603 BlockingNetworkDelegate network_delegate(
3604 BlockingNetworkDelegate::AUTO_CALLBACK);
3605 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]c2911d72011-10-03 22:16:363606
[email protected]ef2bf422012-05-11 03:27:093607 TestURLRequestContext context(true);
3608 context.set_network_delegate(&network_delegate);
3609 context.Init();
[email protected]c2911d72011-10-03 22:16:363610
[email protected]f3cf9802011-10-28 18:44:583611 d.set_credentials(AuthCredentials(kUser, kSecret));
[email protected]c2911d72011-10-03 22:16:363612
3613 {
3614 GURL url(test_server_.GetURL("auth-basic"));
davidben151423e2015-03-23 18:48:363615 scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:193616 r->Start();
[email protected]255620da2013-08-19 13:14:293617 base::RunLoop().Run();
[email protected]c2911d72011-10-03 22:16:363618
[email protected]f7022f32014-08-21 16:32:193619 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3620 EXPECT_EQ(0, r->status().error());
3621 EXPECT_EQ(200, r->GetResponseCode());
[email protected]c2911d72011-10-03 22:16:363622 EXPECT_TRUE(d.auth_required_called());
3623 EXPECT_EQ(1, network_delegate.created_requests());
3624 EXPECT_EQ(0, network_delegate.destroyed_requests());
3625 }
3626 EXPECT_EQ(1, network_delegate.destroyed_requests());
3627}
3628
3629// Tests that the network delegate can asynchronously complete OnAuthRequired
3630// by setting credentials.
3631TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) {
3632 ASSERT_TRUE(test_server_.Start());
3633
3634 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303635 BlockingNetworkDelegate network_delegate(
3636 BlockingNetworkDelegate::AUTO_CALLBACK);
3637 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]c2911d72011-10-03 22:16:363638 network_delegate.set_auth_retval(
[email protected]c2911d72011-10-03 22:16:363639 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
3640
[email protected]f3cf9802011-10-28 18:44:583641 AuthCredentials auth_credentials(kUser, kSecret);
[email protected]c2911d72011-10-03 22:16:363642 network_delegate.set_auth_credentials(auth_credentials);
3643
[email protected]ef2bf422012-05-11 03:27:093644 TestURLRequestContext context(true);
3645 context.set_network_delegate(&network_delegate);
3646 context.Init();
[email protected]c2911d72011-10-03 22:16:363647
3648 {
3649 GURL url(test_server_.GetURL("auth-basic"));
davidben151423e2015-03-23 18:48:363650 scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:193651 r->Start();
[email protected]255620da2013-08-19 13:14:293652 base::RunLoop().Run();
[email protected]c2911d72011-10-03 22:16:363653
[email protected]f7022f32014-08-21 16:32:193654 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3655 EXPECT_EQ(0, r->status().error());
[email protected]c2911d72011-10-03 22:16:363656
[email protected]f7022f32014-08-21 16:32:193657 EXPECT_EQ(200, r->GetResponseCode());
[email protected]c2911d72011-10-03 22:16:363658 EXPECT_FALSE(d.auth_required_called());
3659 EXPECT_EQ(1, network_delegate.created_requests());
3660 EXPECT_EQ(0, network_delegate.destroyed_requests());
3661 }
3662 EXPECT_EQ(1, network_delegate.destroyed_requests());
3663}
3664
3665// Tests that the network delegate can asynchronously complete OnAuthRequired
3666// by cancelling authentication.
3667TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) {
3668 ASSERT_TRUE(test_server_.Start());
3669
3670 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303671 BlockingNetworkDelegate network_delegate(
3672 BlockingNetworkDelegate::AUTO_CALLBACK);
3673 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]c2911d72011-10-03 22:16:363674 network_delegate.set_auth_retval(
[email protected]c2911d72011-10-03 22:16:363675 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH);
3676
[email protected]ef2bf422012-05-11 03:27:093677 TestURLRequestContext context(true);
3678 context.set_network_delegate(&network_delegate);
3679 context.Init();
[email protected]c2911d72011-10-03 22:16:363680
3681 {
3682 GURL url(test_server_.GetURL("auth-basic"));
davidben151423e2015-03-23 18:48:363683 scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:193684 r->Start();
[email protected]255620da2013-08-19 13:14:293685 base::RunLoop().Run();
[email protected]c2911d72011-10-03 22:16:363686
[email protected]f7022f32014-08-21 16:32:193687 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3688 EXPECT_EQ(OK, r->status().error());
3689 EXPECT_EQ(401, r->GetResponseCode());
[email protected]c2911d72011-10-03 22:16:363690 EXPECT_FALSE(d.auth_required_called());
3691 EXPECT_EQ(1, network_delegate.created_requests());
3692 EXPECT_EQ(0, network_delegate.destroyed_requests());
3693 }
3694 EXPECT_EQ(1, network_delegate.destroyed_requests());
3695}
3696
[email protected]9045b8822012-01-13 20:35:353697// Tests that we can handle when a network request was canceled while we were
3698// waiting for the network delegate.
3699// Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback.
3700TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) {
3701 ASSERT_TRUE(test_server_.Start());
3702
3703 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303704 BlockingNetworkDelegate network_delegate(
3705 BlockingNetworkDelegate::USER_CALLBACK);
3706 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
[email protected]9045b8822012-01-13 20:35:353707
[email protected]ef2bf422012-05-11 03:27:093708 TestURLRequestContext context(true);
3709 context.set_network_delegate(&network_delegate);
3710 context.Init();
[email protected]9045b8822012-01-13 20:35:353711
3712 {
[email protected]f7022f32014-08-21 16:32:193713 scoped_ptr<URLRequest> r(context.CreateRequest(
davidben151423e2015-03-23 18:48:363714 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d));
[email protected]9045b8822012-01-13 20:35:353715
[email protected]f7022f32014-08-21 16:32:193716 r->Start();
[email protected]255620da2013-08-19 13:14:293717 base::RunLoop().Run();
[email protected]dc5a5cf2012-09-26 02:49:303718 EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
3719 network_delegate.stage_blocked_for_callback());
[email protected]9045b8822012-01-13 20:35:353720 EXPECT_EQ(0, network_delegate.completed_requests());
3721 // Cancel before callback.
[email protected]f7022f32014-08-21 16:32:193722 r->Cancel();
[email protected]9045b8822012-01-13 20:35:353723 // Ensure that network delegate is notified.
3724 EXPECT_EQ(1, network_delegate.completed_requests());
[email protected]f7022f32014-08-21 16:32:193725 EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status());
3726 EXPECT_EQ(ERR_ABORTED, r->status().error());
[email protected]9045b8822012-01-13 20:35:353727 EXPECT_EQ(1, network_delegate.created_requests());
3728 EXPECT_EQ(0, network_delegate.destroyed_requests());
3729 }
3730 EXPECT_EQ(1, network_delegate.destroyed_requests());
3731}
3732
3733// Tests that we can handle when a network request was canceled while we were
3734// waiting for the network delegate.
3735// Part 2: Request is cancelled while waiting for OnBeforeSendHeaders callback.
3736TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) {
3737 ASSERT_TRUE(test_server_.Start());
3738
3739 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303740 BlockingNetworkDelegate network_delegate(
3741 BlockingNetworkDelegate::USER_CALLBACK);
3742 network_delegate.set_block_on(
3743 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS);
[email protected]9045b8822012-01-13 20:35:353744
[email protected]ef2bf422012-05-11 03:27:093745 TestURLRequestContext context(true);
3746 context.set_network_delegate(&network_delegate);
3747 context.Init();
[email protected]9045b8822012-01-13 20:35:353748
3749 {
[email protected]f7022f32014-08-21 16:32:193750 scoped_ptr<URLRequest> r(context.CreateRequest(
davidben151423e2015-03-23 18:48:363751 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d));
[email protected]9045b8822012-01-13 20:35:353752
[email protected]f7022f32014-08-21 16:32:193753 r->Start();
[email protected]255620da2013-08-19 13:14:293754 base::RunLoop().Run();
[email protected]dc5a5cf2012-09-26 02:49:303755 EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
3756 network_delegate.stage_blocked_for_callback());
[email protected]9045b8822012-01-13 20:35:353757 EXPECT_EQ(0, network_delegate.completed_requests());
3758 // Cancel before callback.
[email protected]f7022f32014-08-21 16:32:193759 r->Cancel();
[email protected]9045b8822012-01-13 20:35:353760 // Ensure that network delegate is notified.
3761 EXPECT_EQ(1, network_delegate.completed_requests());
[email protected]f7022f32014-08-21 16:32:193762 EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status());
3763 EXPECT_EQ(ERR_ABORTED, r->status().error());
[email protected]9045b8822012-01-13 20:35:353764 EXPECT_EQ(1, network_delegate.created_requests());
3765 EXPECT_EQ(0, network_delegate.destroyed_requests());
3766 }
3767 EXPECT_EQ(1, network_delegate.destroyed_requests());
3768}
3769
3770// Tests that we can handle when a network request was canceled while we were
3771// waiting for the network delegate.
3772// Part 3: Request is cancelled while waiting for OnHeadersReceived callback.
3773TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) {
3774 ASSERT_TRUE(test_server_.Start());
3775
3776 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303777 BlockingNetworkDelegate network_delegate(
3778 BlockingNetworkDelegate::USER_CALLBACK);
3779 network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED);
[email protected]9045b8822012-01-13 20:35:353780
[email protected]ef2bf422012-05-11 03:27:093781 TestURLRequestContext context(true);
3782 context.set_network_delegate(&network_delegate);
3783 context.Init();
[email protected]9045b8822012-01-13 20:35:353784
3785 {
[email protected]f7022f32014-08-21 16:32:193786 scoped_ptr<URLRequest> r(context.CreateRequest(
davidben151423e2015-03-23 18:48:363787 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d));
[email protected]9045b8822012-01-13 20:35:353788
[email protected]f7022f32014-08-21 16:32:193789 r->Start();
[email protected]255620da2013-08-19 13:14:293790 base::RunLoop().Run();
[email protected]dc5a5cf2012-09-26 02:49:303791 EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED,
3792 network_delegate.stage_blocked_for_callback());
[email protected]9045b8822012-01-13 20:35:353793 EXPECT_EQ(0, network_delegate.completed_requests());
3794 // Cancel before callback.
[email protected]f7022f32014-08-21 16:32:193795 r->Cancel();
[email protected]9045b8822012-01-13 20:35:353796 // Ensure that network delegate is notified.
3797 EXPECT_EQ(1, network_delegate.completed_requests());
[email protected]f7022f32014-08-21 16:32:193798 EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status());
3799 EXPECT_EQ(ERR_ABORTED, r->status().error());
[email protected]9045b8822012-01-13 20:35:353800 EXPECT_EQ(1, network_delegate.created_requests());
3801 EXPECT_EQ(0, network_delegate.destroyed_requests());
3802 }
3803 EXPECT_EQ(1, network_delegate.destroyed_requests());
3804}
3805
3806// Tests that we can handle when a network request was canceled while we were
3807// waiting for the network delegate.
3808// Part 4: Request is cancelled while waiting for OnAuthRequired callback.
[email protected]bfe8b302013-02-15 12:16:023809TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) {
[email protected]9045b8822012-01-13 20:35:353810 ASSERT_TRUE(test_server_.Start());
3811
3812 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303813 BlockingNetworkDelegate network_delegate(
3814 BlockingNetworkDelegate::USER_CALLBACK);
3815 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]9045b8822012-01-13 20:35:353816
[email protected]ef2bf422012-05-11 03:27:093817 TestURLRequestContext context(true);
3818 context.set_network_delegate(&network_delegate);
3819 context.Init();
[email protected]9045b8822012-01-13 20:35:353820
3821 {
[email protected]f7022f32014-08-21 16:32:193822 scoped_ptr<URLRequest> r(context.CreateRequest(
davidben151423e2015-03-23 18:48:363823 test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d));
[email protected]9045b8822012-01-13 20:35:353824
[email protected]f7022f32014-08-21 16:32:193825 r->Start();
[email protected]255620da2013-08-19 13:14:293826 base::RunLoop().Run();
[email protected]dc5a5cf2012-09-26 02:49:303827 EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED,
3828 network_delegate.stage_blocked_for_callback());
[email protected]9045b8822012-01-13 20:35:353829 EXPECT_EQ(0, network_delegate.completed_requests());
3830 // Cancel before callback.
[email protected]f7022f32014-08-21 16:32:193831 r->Cancel();
[email protected]9045b8822012-01-13 20:35:353832 // Ensure that network delegate is notified.
3833 EXPECT_EQ(1, network_delegate.completed_requests());
[email protected]f7022f32014-08-21 16:32:193834 EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status());
3835 EXPECT_EQ(ERR_ABORTED, r->status().error());
[email protected]9045b8822012-01-13 20:35:353836 EXPECT_EQ(1, network_delegate.created_requests());
3837 EXPECT_EQ(0, network_delegate.destroyed_requests());
3838 }
3839 EXPECT_EQ(1, network_delegate.destroyed_requests());
3840}
3841
[email protected]95409e12010-08-17 20:07:113842// In this unit test, we're using the HTTPTestServer as a proxy server and
3843// issuing a CONNECT request with the magic host name "www.server-auth.com".
3844// The HTTPTestServer will return a 401 response, which we should balk at.
[email protected]b89290212009-08-14 22:37:353845TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) {
[email protected]95409e12010-08-17 20:07:113846 ASSERT_TRUE(test_server_.Start());
3847
[email protected]ceefd7fd2012-11-29 00:36:243848 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]d5a4dd62012-05-23 01:41:043849 TestURLRequestContextWithProxy context(
[email protected]2ca01e52013-10-31 22:05:193850 test_server_.host_port_pair().ToString(), &network_delegate);
[email protected]87a09a92011-07-14 15:50:503851
[email protected]dc651782009-02-14 01:45:083852 TestDelegate d;
3853 {
[email protected]f7022f32014-08-21 16:32:193854 scoped_ptr<URLRequest> r(context.CreateRequest(
davidben151423e2015-03-23 18:48:363855 GURL("https://www.server-auth.com/"), DEFAULT_PRIORITY, &d));
[email protected]dc651782009-02-14 01:45:083856
[email protected]f7022f32014-08-21 16:32:193857 r->Start();
3858 EXPECT_TRUE(r->is_pending());
[email protected]dc651782009-02-14 01:45:083859
[email protected]255620da2013-08-19 13:14:293860 base::RunLoop().Run();
[email protected]dc651782009-02-14 01:45:083861
[email protected]f7022f32014-08-21 16:32:193862 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
[email protected]d8fc4722014-06-13 13:17:153863 // The proxy server is not set before failure.
[email protected]f7022f32014-08-21 16:32:193864 EXPECT_TRUE(r->proxy_server().IsEmpty());
3865 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r->status().error());
[email protected]dc651782009-02-14 01:45:083866 }
3867}
3868
[email protected]b89290212009-08-14 22:37:353869TEST_F(URLRequestTestHTTP, GetTest_NoCache) {
[email protected]95409e12010-08-17 20:07:113870 ASSERT_TRUE(test_server_.Start());
3871
initial.commit586acc5fe2008-07-26 22:42:523872 TestDelegate d;
3873 {
[email protected]f7022f32014-08-21 16:32:193874 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:363875 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d));
initial.commit586acc5fe2008-07-26 22:42:523876
[email protected]f7022f32014-08-21 16:32:193877 r->Start();
3878 EXPECT_TRUE(r->is_pending());
initial.commit586acc5fe2008-07-26 22:42:523879
[email protected]255620da2013-08-19 13:14:293880 base::RunLoop().Run();
initial.commit586acc5fe2008-07-26 22:42:523881
3882 EXPECT_EQ(1, d.response_started_count());
3883 EXPECT_FALSE(d.received_data_before_response());
3884 EXPECT_NE(0, d.bytes_received());
[email protected]6d81b482011-02-22 19:47:193885 EXPECT_EQ(test_server_.host_port_pair().host(),
[email protected]f7022f32014-08-21 16:32:193886 r->GetSocketAddress().host());
[email protected]6d81b482011-02-22 19:47:193887 EXPECT_EQ(test_server_.host_port_pair().port(),
[email protected]f7022f32014-08-21 16:32:193888 r->GetSocketAddress().port());
[email protected]c31a54592009-09-04 02:36:163889
[email protected]9e743cd2010-03-16 07:03:533890 // TODO(eroman): Add back the NetLog tests...
initial.commit586acc5fe2008-07-26 22:42:523891 }
initial.commit586acc5fe2008-07-26 22:42:523892}
3893
[email protected]263163f2012-06-14 22:40:343894// This test has the server send a large number of cookies to the client.
3895// To ensure that no number of cookies causes a crash, a galloping binary
3896// search is used to estimate that maximum number of cookies that are accepted
3897// by the browser. Beyond the maximum number, the request will fail with
3898// ERR_RESPONSE_HEADERS_TOO_BIG.
[email protected]69dd6fe2013-02-23 23:15:303899#if defined(OS_WIN)
3900// http://crbug.com/177916
3901#define MAYBE_GetTest_ManyCookies DISABLED_GetTest_ManyCookies
3902#else
3903#define MAYBE_GetTest_ManyCookies GetTest_ManyCookies
3904#endif // defined(OS_WIN)
3905TEST_F(URLRequestTestHTTP, MAYBE_GetTest_ManyCookies) {
[email protected]263163f2012-06-14 22:40:343906 ASSERT_TRUE(test_server_.Start());
3907
3908 int lower_bound = 0;
3909 int upper_bound = 1;
3910
3911 // Double the number of cookies until the response header limits are
3912 // exceeded.
3913 while (DoManyCookiesRequest(upper_bound)) {
3914 lower_bound = upper_bound;
3915 upper_bound *= 2;
3916 ASSERT_LT(upper_bound, 1000000);
3917 }
3918
pkasting6b68a162014-12-01 22:10:293919 int tolerance = static_cast<int>(upper_bound * 0.005);
[email protected]263163f2012-06-14 22:40:343920 if (tolerance < 2)
3921 tolerance = 2;
3922
3923 // Perform a binary search to find the highest possible number of cookies,
3924 // within the desired tolerance.
3925 while (upper_bound - lower_bound >= tolerance) {
3926 int num_cookies = (lower_bound + upper_bound) / 2;
3927
3928 if (DoManyCookiesRequest(num_cookies))
3929 lower_bound = num_cookies;
3930 else
3931 upper_bound = num_cookies;
3932 }
3933 // Success: the test did not crash.
3934}
3935
[email protected]b89290212009-08-14 22:37:353936TEST_F(URLRequestTestHTTP, GetTest) {
[email protected]95409e12010-08-17 20:07:113937 ASSERT_TRUE(test_server_.Start());
3938
initial.commit586acc5fe2008-07-26 22:42:523939 TestDelegate d;
3940 {
[email protected]f7022f32014-08-21 16:32:193941 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:363942 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d));
initial.commit586acc5fe2008-07-26 22:42:523943
[email protected]f7022f32014-08-21 16:32:193944 r->Start();
3945 EXPECT_TRUE(r->is_pending());
initial.commit586acc5fe2008-07-26 22:42:523946
[email protected]255620da2013-08-19 13:14:293947 base::RunLoop().Run();
initial.commit586acc5fe2008-07-26 22:42:523948
3949 EXPECT_EQ(1, d.response_started_count());
3950 EXPECT_FALSE(d.received_data_before_response());
3951 EXPECT_NE(0, d.bytes_received());
[email protected]6d81b482011-02-22 19:47:193952 EXPECT_EQ(test_server_.host_port_pair().host(),
[email protected]f7022f32014-08-21 16:32:193953 r->GetSocketAddress().host());
[email protected]6d81b482011-02-22 19:47:193954 EXPECT_EQ(test_server_.host_port_pair().port(),
[email protected]f7022f32014-08-21 16:32:193955 r->GetSocketAddress().port());
initial.commit586acc5fe2008-07-26 22:42:523956 }
[email protected]5d7b373e2009-09-02 07:19:033957}
3958
[email protected]79e1fd62013-06-20 06:50:043959TEST_F(URLRequestTestHTTP, GetTest_GetFullRequestHeaders) {
3960 ASSERT_TRUE(test_server_.Start());
3961
3962 TestDelegate d;
3963 {
3964 GURL test_url(test_server_.GetURL(std::string()));
davidben151423e2015-03-23 18:48:363965 scoped_ptr<URLRequest> r(
3966 default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d));
[email protected]79e1fd62013-06-20 06:50:043967
3968 HttpRequestHeaders headers;
[email protected]f7022f32014-08-21 16:32:193969 EXPECT_FALSE(r->GetFullRequestHeaders(&headers));
[email protected]79e1fd62013-06-20 06:50:043970
[email protected]f7022f32014-08-21 16:32:193971 r->Start();
3972 EXPECT_TRUE(r->is_pending());
[email protected]79e1fd62013-06-20 06:50:043973
[email protected]255620da2013-08-19 13:14:293974 base::RunLoop().Run();
[email protected]79e1fd62013-06-20 06:50:043975
3976 EXPECT_EQ(1, d.response_started_count());
3977 EXPECT_FALSE(d.received_data_before_response());
3978 EXPECT_NE(0, d.bytes_received());
3979 EXPECT_EQ(test_server_.host_port_pair().host(),
[email protected]f7022f32014-08-21 16:32:193980 r->GetSocketAddress().host());
[email protected]79e1fd62013-06-20 06:50:043981 EXPECT_EQ(test_server_.host_port_pair().port(),
[email protected]f7022f32014-08-21 16:32:193982 r->GetSocketAddress().port());
[email protected]79e1fd62013-06-20 06:50:043983
3984 EXPECT_TRUE(d.have_full_request_headers());
3985 CheckFullRequestHeaders(d.full_request_headers(), test_url);
3986 }
3987}
3988
[email protected]58e32bb2013-01-21 18:23:253989TEST_F(URLRequestTestHTTP, GetTestLoadTiming) {
3990 ASSERT_TRUE(test_server_.Start());
3991
3992 TestDelegate d;
3993 {
[email protected]f7022f32014-08-21 16:32:193994 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:363995 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d));
[email protected]58e32bb2013-01-21 18:23:253996
[email protected]f7022f32014-08-21 16:32:193997 r->Start();
3998 EXPECT_TRUE(r->is_pending());
[email protected]58e32bb2013-01-21 18:23:253999
[email protected]255620da2013-08-19 13:14:294000 base::RunLoop().Run();
[email protected]58e32bb2013-01-21 18:23:254001
4002 LoadTimingInfo load_timing_info;
[email protected]f7022f32014-08-21 16:32:194003 r->GetLoadTimingInfo(&load_timing_info);
[email protected]58e32bb2013-01-21 18:23:254004 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
4005
4006 EXPECT_EQ(1, d.response_started_count());
4007 EXPECT_FALSE(d.received_data_before_response());
4008 EXPECT_NE(0, d.bytes_received());
4009 EXPECT_EQ(test_server_.host_port_pair().host(),
[email protected]f7022f32014-08-21 16:32:194010 r->GetSocketAddress().host());
[email protected]58e32bb2013-01-21 18:23:254011 EXPECT_EQ(test_server_.host_port_pair().port(),
[email protected]f7022f32014-08-21 16:32:194012 r->GetSocketAddress().port());
[email protected]58e32bb2013-01-21 18:23:254013 }
4014}
4015
[email protected]aad63572011-05-24 20:14:394016TEST_F(URLRequestTestHTTP, GetZippedTest) {
4017 ASSERT_TRUE(test_server_.Start());
4018
4019 // Parameter that specifies the Content-Length field in the response:
4020 // C - Compressed length.
4021 // U - Uncompressed length.
4022 // L - Large length (larger than both C & U).
4023 // M - Medium length (between C & U).
4024 // S - Small length (smaller than both C & U).
4025 const char test_parameters[] = "CULMS";
4026 const int num_tests = arraysize(test_parameters)- 1; // Skip NULL.
4027 // C & U should be OK.
[email protected]f0e2bf42011-07-22 21:21:444028 // L & M are larger than the data sent, and show an error.
[email protected]aad63572011-05-24 20:14:394029 // S has too little data, but we seem to accept it.
4030 const bool test_expect_success[num_tests] =
[email protected]f001bd6a2011-12-08 04:31:374031 { true, true, false, false, true };
[email protected]aad63572011-05-24 20:14:394032
4033 for (int i = 0; i < num_tests ; i++) {
4034 TestDelegate d;
4035 {
4036 std::string test_file =
4037 base::StringPrintf("compressedfiles/BullRunSpeech.txt?%c",
4038 test_parameters[i]);
[email protected]aad63572011-05-24 20:14:394039
[email protected]ceefd7fd2012-11-29 00:36:244040 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]ef2bf422012-05-11 03:27:094041 TestURLRequestContext context(true);
4042 context.set_network_delegate(&network_delegate);
4043 context.Init();
[email protected]87a09a92011-07-14 15:50:504044
[email protected]f7022f32014-08-21 16:32:194045 scoped_ptr<URLRequest> r(context.CreateRequest(
davidben151423e2015-03-23 18:48:364046 test_server_.GetURL(test_file), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:194047 r->Start();
4048 EXPECT_TRUE(r->is_pending());
[email protected]aad63572011-05-24 20:14:394049
[email protected]255620da2013-08-19 13:14:294050 base::RunLoop().Run();
[email protected]aad63572011-05-24 20:14:394051
4052 EXPECT_EQ(1, d.response_started_count());
4053 EXPECT_FALSE(d.received_data_before_response());
4054 VLOG(1) << " Received " << d.bytes_received() << " bytes"
[email protected]f7022f32014-08-21 16:32:194055 << " status = " << r->status().status()
4056 << " error = " << r->status().error();
[email protected]aad63572011-05-24 20:14:394057 if (test_expect_success[i]) {
[email protected]f7022f32014-08-21 16:32:194058 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status())
[email protected]aad63572011-05-24 20:14:394059 << " Parameter = \"" << test_file << "\"";
4060 } else {
[email protected]f7022f32014-08-21 16:32:194061 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
4062 EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, r->status().error())
[email protected]aad63572011-05-24 20:14:394063 << " Parameter = \"" << test_file << "\"";
4064 }
4065 }
4066 }
4067}
4068
[email protected]58e32bb2013-01-21 18:23:254069TEST_F(URLRequestTestHTTP, RedirectLoadTiming) {
4070 ASSERT_TRUE(test_server_.Start());
4071
[email protected]007b3f82013-04-09 08:46:454072 GURL destination_url = test_server_.GetURL(std::string());
4073 GURL original_url =
4074 test_server_.GetURL("server-redirect?" + destination_url.spec());
[email protected]58e32bb2013-01-21 18:23:254075 TestDelegate d;
davidben151423e2015-03-23 18:48:364076 scoped_ptr<URLRequest> req(
4077 default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:194078 req->Start();
[email protected]255620da2013-08-19 13:14:294079 base::RunLoop().Run();
[email protected]58e32bb2013-01-21 18:23:254080
4081 EXPECT_EQ(1, d.response_started_count());
4082 EXPECT_EQ(1, d.received_redirect_count());
[email protected]f7022f32014-08-21 16:32:194083 EXPECT_EQ(destination_url, req->url());
4084 EXPECT_EQ(original_url, req->original_url());
4085 ASSERT_EQ(2U, req->url_chain().size());
4086 EXPECT_EQ(original_url, req->url_chain()[0]);
4087 EXPECT_EQ(destination_url, req->url_chain()[1]);
[email protected]58e32bb2013-01-21 18:23:254088
4089 LoadTimingInfo load_timing_info_before_redirect;
4090 EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeRedirect(
4091 &load_timing_info_before_redirect));
4092 TestLoadTimingNotReused(load_timing_info_before_redirect,
4093 CONNECT_TIMING_HAS_DNS_TIMES);
4094
4095 LoadTimingInfo load_timing_info;
[email protected]f7022f32014-08-21 16:32:194096 req->GetLoadTimingInfo(&load_timing_info);
[email protected]58e32bb2013-01-21 18:23:254097 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
4098
4099 // Check that a new socket was used on redirect, since the server does not
4100 // supposed keep-alive sockets, and that the times before the redirect are
4101 // before the ones recorded for the second request.
4102 EXPECT_NE(load_timing_info_before_redirect.socket_log_id,
4103 load_timing_info.socket_log_id);
4104 EXPECT_LE(load_timing_info_before_redirect.receive_headers_end,
4105 load_timing_info.connect_timing.connect_start);
4106}
4107
[email protected]8f1ac082011-04-19 21:14:134108TEST_F(URLRequestTestHTTP, MultipleRedirectTest) {
4109 ASSERT_TRUE(test_server_.Start());
4110
[email protected]007b3f82013-04-09 08:46:454111 GURL destination_url = test_server_.GetURL(std::string());
4112 GURL middle_redirect_url =
4113 test_server_.GetURL("server-redirect?" + destination_url.spec());
[email protected]8f1ac082011-04-19 21:14:134114 GURL original_url = test_server_.GetURL(
4115 "server-redirect?" + middle_redirect_url.spec());
4116 TestDelegate d;
davidben151423e2015-03-23 18:48:364117 scoped_ptr<URLRequest> req(
4118 default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:194119 req->Start();
[email protected]255620da2013-08-19 13:14:294120 base::RunLoop().Run();
[email protected]8f1ac082011-04-19 21:14:134121
4122 EXPECT_EQ(1, d.response_started_count());
4123 EXPECT_EQ(2, d.received_redirect_count());
[email protected]f7022f32014-08-21 16:32:194124 EXPECT_EQ(destination_url, req->url());
4125 EXPECT_EQ(original_url, req->original_url());
4126 ASSERT_EQ(3U, req->url_chain().size());
4127 EXPECT_EQ(original_url, req->url_chain()[0]);
4128 EXPECT_EQ(middle_redirect_url, req->url_chain()[1]);
4129 EXPECT_EQ(destination_url, req->url_chain()[2]);
[email protected]8f1ac082011-04-19 21:14:134130}
4131
[email protected]abe1c4a2013-10-25 19:28:514132// First and second pieces of information logged by delegates to URLRequests.
4133const char kFirstDelegateInfo[] = "Wonderful delegate";
4134const char kSecondDelegateInfo[] = "Exciting delegate";
4135
4136// Logs delegate information to a URLRequest. The first string is logged
4137// synchronously on Start(), using DELEGATE_INFO_DEBUG_ONLY. The second is
4138// logged asynchronously, using DELEGATE_INFO_DISPLAY_TO_USER. Then
4139// another asynchronous call is used to clear the delegate information
4140// before calling a callback. The object then deletes itself.
4141class AsyncDelegateLogger : public base::RefCounted<AsyncDelegateLogger> {
4142 public:
4143 typedef base::Callback<void()> Callback;
4144
4145 // Each time delegate information is added to the URLRequest, the resulting
4146 // load state is checked. The expected load state after each request is
4147 // passed in as an argument.
4148 static void Run(URLRequest* url_request,
4149 LoadState expected_first_load_state,
4150 LoadState expected_second_load_state,
4151 LoadState expected_third_load_state,
4152 const Callback& callback) {
4153 AsyncDelegateLogger* logger = new AsyncDelegateLogger(
4154 url_request,
4155 expected_first_load_state,
4156 expected_second_load_state,
4157 expected_third_load_state,
4158 callback);
4159 logger->Start();
4160 }
4161
4162 // Checks that the log entries, starting with log_position, contain the
4163 // DELEGATE_INFO NetLog events that an AsyncDelegateLogger should have
4164 // recorded. Returns the index of entry after the expected number of
4165 // events this logged, or entries.size() if there aren't enough entries.
vishal.b62985ca92015-04-17 08:45:514166 static size_t CheckDelegateInfo(const TestNetLog::CapturedEntryList& entries,
4167 size_t log_position) {
[email protected]abe1c4a2013-10-25 19:28:514168 // There should be 4 DELEGATE_INFO events: Two begins and two ends.
4169 if (log_position + 3 >= entries.size()) {
4170 ADD_FAILURE() << "Not enough log entries";
4171 return entries.size();
4172 }
4173 std::string delegate_info;
4174 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type);
4175 EXPECT_EQ(NetLog::PHASE_BEGIN, entries[log_position].phase);
4176 EXPECT_TRUE(entries[log_position].GetStringValue("delegate_info",
4177 &delegate_info));
4178 EXPECT_EQ(kFirstDelegateInfo, delegate_info);
4179
4180 ++log_position;
4181 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type);
4182 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4183
4184 ++log_position;
4185 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type);
4186 EXPECT_EQ(NetLog::PHASE_BEGIN, entries[log_position].phase);
4187 EXPECT_TRUE(entries[log_position].GetStringValue("delegate_info",
4188 &delegate_info));
4189 EXPECT_EQ(kSecondDelegateInfo, delegate_info);
4190
4191 ++log_position;
4192 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type);
4193 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4194
4195 return log_position + 1;
4196 }
4197
[email protected]1826a402014-01-08 15:40:484198 // Find delegate request begin and end messages for OnBeforeNetworkStart.
4199 // Returns the position of the end message.
4200 static size_t ExpectBeforeNetworkEvents(
vishal.b62985ca92015-04-17 08:45:514201 const TestNetLog::CapturedEntryList& entries,
[email protected]1826a402014-01-08 15:40:484202 size_t log_position) {
4203 log_position =
4204 ExpectLogContainsSomewhereAfter(entries,
4205 log_position,
4206 NetLog::TYPE_URL_REQUEST_DELEGATE,
4207 NetLog::PHASE_BEGIN);
4208 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE,
4209 entries[log_position + 1].type);
4210 EXPECT_EQ(NetLog::PHASE_END, entries[log_position + 1].phase);
4211 return log_position + 1;
4212 }
4213
[email protected]abe1c4a2013-10-25 19:28:514214 private:
4215 friend class base::RefCounted<AsyncDelegateLogger>;
4216
4217 AsyncDelegateLogger(URLRequest* url_request,
4218 LoadState expected_first_load_state,
4219 LoadState expected_second_load_state,
4220 LoadState expected_third_load_state,
4221 const Callback& callback)
4222 : url_request_(url_request),
4223 expected_first_load_state_(expected_first_load_state),
4224 expected_second_load_state_(expected_second_load_state),
4225 expected_third_load_state_(expected_third_load_state),
4226 callback_(callback) {
4227 }
4228
4229 ~AsyncDelegateLogger() {}
4230
4231 void Start() {
[email protected]f8fe5cf2013-12-04 20:11:534232 url_request_->LogBlockedBy(kFirstDelegateInfo);
[email protected]abe1c4a2013-10-25 19:28:514233 LoadStateWithParam load_state = url_request_->GetLoadState();
4234 EXPECT_EQ(expected_first_load_state_, load_state.state);
4235 EXPECT_NE(ASCIIToUTF16(kFirstDelegateInfo), load_state.param);
4236 base::MessageLoop::current()->PostTask(
4237 FROM_HERE,
4238 base::Bind(&AsyncDelegateLogger::LogSecondDelegate, this));
4239 }
4240
4241 void LogSecondDelegate() {
[email protected]f8fe5cf2013-12-04 20:11:534242 url_request_->LogAndReportBlockedBy(kSecondDelegateInfo);
[email protected]abe1c4a2013-10-25 19:28:514243 LoadStateWithParam load_state = url_request_->GetLoadState();
4244 EXPECT_EQ(expected_second_load_state_, load_state.state);
4245 if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) {
4246 EXPECT_EQ(ASCIIToUTF16(kSecondDelegateInfo), load_state.param);
4247 } else {
4248 EXPECT_NE(ASCIIToUTF16(kSecondDelegateInfo), load_state.param);
4249 }
4250 base::MessageLoop::current()->PostTask(
4251 FROM_HERE,
4252 base::Bind(&AsyncDelegateLogger::LogComplete, this));
4253 }
4254
4255 void LogComplete() {
[email protected]f8fe5cf2013-12-04 20:11:534256 url_request_->LogUnblocked();
[email protected]abe1c4a2013-10-25 19:28:514257 LoadStateWithParam load_state = url_request_->GetLoadState();
4258 EXPECT_EQ(expected_third_load_state_, load_state.state);
4259 if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE)
[email protected]754bd202013-12-18 08:29:084260 EXPECT_EQ(base::string16(), load_state.param);
[email protected]abe1c4a2013-10-25 19:28:514261 callback_.Run();
4262 }
4263
4264 URLRequest* url_request_;
4265 const int expected_first_load_state_;
4266 const int expected_second_load_state_;
4267 const int expected_third_load_state_;
4268 const Callback callback_;
4269
4270 DISALLOW_COPY_AND_ASSIGN(AsyncDelegateLogger);
4271};
4272
4273// NetworkDelegate that logs delegate information before a request is started,
4274// before headers are sent, when headers are read, and when auth information
4275// is requested. Uses AsyncDelegateLogger.
4276class AsyncLoggingNetworkDelegate : public TestNetworkDelegate {
4277 public:
4278 AsyncLoggingNetworkDelegate() {}
dchengb03027d2014-10-21 12:00:204279 ~AsyncLoggingNetworkDelegate() override {}
[email protected]abe1c4a2013-10-25 19:28:514280
4281 // NetworkDelegate implementation.
dchengb03027d2014-10-21 12:00:204282 int OnBeforeURLRequest(URLRequest* request,
4283 const CompletionCallback& callback,
4284 GURL* new_url) override {
[email protected]abe1c4a2013-10-25 19:28:514285 TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url);
4286 return RunCallbackAsynchronously(request, callback);
4287 }
4288
dchengb03027d2014-10-21 12:00:204289 int OnBeforeSendHeaders(URLRequest* request,
4290 const CompletionCallback& callback,
4291 HttpRequestHeaders* headers) override {
[email protected]abe1c4a2013-10-25 19:28:514292 TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers);
4293 return RunCallbackAsynchronously(request, callback);
4294 }
4295
dchengb03027d2014-10-21 12:00:204296 int OnHeadersReceived(
[email protected]abe1c4a2013-10-25 19:28:514297 URLRequest* request,
4298 const CompletionCallback& callback,
4299 const HttpResponseHeaders* original_response_headers,
[email protected]5f714132014-03-26 10:41:164300 scoped_refptr<HttpResponseHeaders>* override_response_headers,
mostynbba063d6032014-10-09 11:01:134301 GURL* allowed_unsafe_redirect_url) override {
[email protected]5f714132014-03-26 10:41:164302 TestNetworkDelegate::OnHeadersReceived(request,
4303 callback,
[email protected]abe1c4a2013-10-25 19:28:514304 original_response_headers,
[email protected]5f714132014-03-26 10:41:164305 override_response_headers,
4306 allowed_unsafe_redirect_url);
[email protected]abe1c4a2013-10-25 19:28:514307 return RunCallbackAsynchronously(request, callback);
4308 }
4309
dchengb03027d2014-10-21 12:00:204310 NetworkDelegate::AuthRequiredResponse OnAuthRequired(
[email protected]abe1c4a2013-10-25 19:28:514311 URLRequest* request,
4312 const AuthChallengeInfo& auth_info,
4313 const AuthCallback& callback,
mostynbba063d6032014-10-09 11:01:134314 AuthCredentials* credentials) override {
[email protected]abe1c4a2013-10-25 19:28:514315 AsyncDelegateLogger::Run(
4316 request,
4317 LOAD_STATE_WAITING_FOR_DELEGATE,
4318 LOAD_STATE_WAITING_FOR_DELEGATE,
4319 LOAD_STATE_WAITING_FOR_DELEGATE,
4320 base::Bind(&AsyncLoggingNetworkDelegate::SetAuthAndResume,
4321 callback, credentials));
4322 return AUTH_REQUIRED_RESPONSE_IO_PENDING;
4323 }
4324
4325 private:
4326 static int RunCallbackAsynchronously(
4327 URLRequest* request,
4328 const CompletionCallback& callback) {
4329 AsyncDelegateLogger::Run(
4330 request,
4331 LOAD_STATE_WAITING_FOR_DELEGATE,
4332 LOAD_STATE_WAITING_FOR_DELEGATE,
4333 LOAD_STATE_WAITING_FOR_DELEGATE,
4334 base::Bind(callback, OK));
4335 return ERR_IO_PENDING;
4336 }
4337
4338 static void SetAuthAndResume(const AuthCallback& callback,
4339 AuthCredentials* credentials) {
4340 *credentials = AuthCredentials(kUser, kSecret);
4341 callback.Run(NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
4342 }
4343
4344 DISALLOW_COPY_AND_ASSIGN(AsyncLoggingNetworkDelegate);
4345};
4346
4347// URLRequest::Delegate that logs delegate information when the headers
4348// are received, when each read completes, and during redirects. Uses
4349// AsyncDelegateLogger. Can optionally cancel a request in any phase.
4350//
4351// Inherits from TestDelegate to reuse the TestDelegate code to handle
4352// advancing to the next step in most cases, as well as cancellation.
4353class AsyncLoggingUrlRequestDelegate : public TestDelegate {
4354 public:
4355 enum CancelStage {
4356 NO_CANCEL = 0,
4357 CANCEL_ON_RECEIVED_REDIRECT,
4358 CANCEL_ON_RESPONSE_STARTED,
4359 CANCEL_ON_READ_COMPLETED
4360 };
4361
4362 explicit AsyncLoggingUrlRequestDelegate(CancelStage cancel_stage)
4363 : cancel_stage_(cancel_stage) {
4364 if (cancel_stage == CANCEL_ON_RECEIVED_REDIRECT)
4365 set_cancel_in_received_redirect(true);
4366 else if (cancel_stage == CANCEL_ON_RESPONSE_STARTED)
4367 set_cancel_in_response_started(true);
4368 else if (cancel_stage == CANCEL_ON_READ_COMPLETED)
4369 set_cancel_in_received_data(true);
4370 }
dchengb03027d2014-10-21 12:00:204371 ~AsyncLoggingUrlRequestDelegate() override {}
[email protected]abe1c4a2013-10-25 19:28:514372
4373 // URLRequest::Delegate implementation:
dchengb03027d2014-10-21 12:00:204374 void OnReceivedRedirect(URLRequest* request,
4375 const RedirectInfo& redirect_info,
4376 bool* defer_redirect) override {
[email protected]abe1c4a2013-10-25 19:28:514377 *defer_redirect = true;
4378 AsyncDelegateLogger::Run(
4379 request,
4380 LOAD_STATE_WAITING_FOR_DELEGATE,
4381 LOAD_STATE_WAITING_FOR_DELEGATE,
4382 LOAD_STATE_WAITING_FOR_DELEGATE,
4383 base::Bind(
4384 &AsyncLoggingUrlRequestDelegate::OnReceivedRedirectLoggingComplete,
[email protected]cba24642014-08-15 20:49:594385 base::Unretained(this), request, redirect_info));
[email protected]abe1c4a2013-10-25 19:28:514386 }
4387
dchengb03027d2014-10-21 12:00:204388 void OnResponseStarted(URLRequest* request) override {
[email protected]abe1c4a2013-10-25 19:28:514389 AsyncDelegateLogger::Run(
4390 request,
4391 LOAD_STATE_WAITING_FOR_DELEGATE,
4392 LOAD_STATE_WAITING_FOR_DELEGATE,
4393 LOAD_STATE_WAITING_FOR_DELEGATE,
4394 base::Bind(
4395 &AsyncLoggingUrlRequestDelegate::OnResponseStartedLoggingComplete,
4396 base::Unretained(this), request));
4397 }
4398
dchengb03027d2014-10-21 12:00:204399 void OnReadCompleted(URLRequest* request, int bytes_read) override {
[email protected]abe1c4a2013-10-25 19:28:514400 AsyncDelegateLogger::Run(
4401 request,
4402 LOAD_STATE_IDLE,
4403 LOAD_STATE_IDLE,
4404 LOAD_STATE_IDLE,
4405 base::Bind(
4406 &AsyncLoggingUrlRequestDelegate::AfterReadCompletedLoggingComplete,
4407 base::Unretained(this), request, bytes_read));
4408 }
4409
4410 private:
4411 void OnReceivedRedirectLoggingComplete(URLRequest* request,
[email protected]cba24642014-08-15 20:49:594412 const RedirectInfo& redirect_info) {
[email protected]abe1c4a2013-10-25 19:28:514413 bool defer_redirect = false;
[email protected]cba24642014-08-15 20:49:594414 TestDelegate::OnReceivedRedirect(request, redirect_info, &defer_redirect);
[email protected]abe1c4a2013-10-25 19:28:514415 // FollowDeferredRedirect should not be called after cancellation.
4416 if (cancel_stage_ == CANCEL_ON_RECEIVED_REDIRECT)
4417 return;
4418 if (!defer_redirect)
4419 request->FollowDeferredRedirect();
4420 }
4421
4422 void OnResponseStartedLoggingComplete(URLRequest* request) {
4423 // The parent class continues the request.
4424 TestDelegate::OnResponseStarted(request);
4425 }
4426
4427 void AfterReadCompletedLoggingComplete(URLRequest* request, int bytes_read) {
4428 // The parent class continues the request.
4429 TestDelegate::OnReadCompleted(request, bytes_read);
4430 }
4431
4432 const CancelStage cancel_stage_;
4433
4434 DISALLOW_COPY_AND_ASSIGN(AsyncLoggingUrlRequestDelegate);
4435};
4436
4437// Tests handling of delegate info before a request starts.
4438TEST_F(URLRequestTestHTTP, DelegateInfoBeforeStart) {
4439 ASSERT_TRUE(test_server_.Start());
4440
4441 TestDelegate request_delegate;
4442 TestURLRequestContext context(true);
4443 context.set_network_delegate(NULL);
4444 context.set_net_log(&net_log_);
4445 context.Init();
4446
4447 {
davidben151423e2015-03-23 18:48:364448 scoped_ptr<URLRequest> r(
4449 context.CreateRequest(test_server_.GetURL("empty.html"),
4450 DEFAULT_PRIORITY, &request_delegate));
[email protected]f7022f32014-08-21 16:32:194451 LoadStateWithParam load_state = r->GetLoadState();
[email protected]abe1c4a2013-10-25 19:28:514452 EXPECT_EQ(LOAD_STATE_IDLE, load_state.state);
[email protected]754bd202013-12-18 08:29:084453 EXPECT_EQ(base::string16(), load_state.param);
[email protected]abe1c4a2013-10-25 19:28:514454
4455 AsyncDelegateLogger::Run(
[email protected]f7022f32014-08-21 16:32:194456 r.get(),
[email protected]abe1c4a2013-10-25 19:28:514457 LOAD_STATE_WAITING_FOR_DELEGATE,
4458 LOAD_STATE_WAITING_FOR_DELEGATE,
4459 LOAD_STATE_IDLE,
[email protected]f7022f32014-08-21 16:32:194460 base::Bind(&URLRequest::Start, base::Unretained(r.get())));
[email protected]abe1c4a2013-10-25 19:28:514461
4462 base::RunLoop().Run();
4463
[email protected]f7022f32014-08-21 16:32:194464 EXPECT_EQ(200, r->GetResponseCode());
4465 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
[email protected]abe1c4a2013-10-25 19:28:514466 }
4467
vishal.b62985ca92015-04-17 08:45:514468 TestNetLog::CapturedEntryList entries;
[email protected]abe1c4a2013-10-25 19:28:514469 net_log_.GetEntries(&entries);
4470 size_t log_position = ExpectLogContainsSomewhereAfter(
4471 entries,
4472 0,
4473 NetLog::TYPE_DELEGATE_INFO,
4474 NetLog::PHASE_BEGIN);
4475
4476 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, log_position);
4477
4478 // Nothing else should add any delegate info to the request.
4479 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4480 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4481}
4482
4483// Tests handling of delegate info from a network delegate.
4484TEST_F(URLRequestTestHTTP, NetworkDelegateInfo) {
4485 ASSERT_TRUE(test_server_.Start());
4486
4487 TestDelegate request_delegate;
4488 AsyncLoggingNetworkDelegate network_delegate;
4489 TestURLRequestContext context(true);
4490 context.set_network_delegate(&network_delegate);
4491 context.set_net_log(&net_log_);
4492 context.Init();
4493
4494 {
davidben151423e2015-03-23 18:48:364495 scoped_ptr<URLRequest> r(
4496 context.CreateRequest(test_server_.GetURL("simple.html"),
4497 DEFAULT_PRIORITY, &request_delegate));
[email protected]f7022f32014-08-21 16:32:194498 LoadStateWithParam load_state = r->GetLoadState();
[email protected]abe1c4a2013-10-25 19:28:514499 EXPECT_EQ(LOAD_STATE_IDLE, load_state.state);
[email protected]754bd202013-12-18 08:29:084500 EXPECT_EQ(base::string16(), load_state.param);
[email protected]abe1c4a2013-10-25 19:28:514501
[email protected]f7022f32014-08-21 16:32:194502 r->Start();
[email protected]abe1c4a2013-10-25 19:28:514503 base::RunLoop().Run();
4504
[email protected]f7022f32014-08-21 16:32:194505 EXPECT_EQ(200, r->GetResponseCode());
4506 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
[email protected]abe1c4a2013-10-25 19:28:514507 EXPECT_EQ(1, network_delegate.created_requests());
4508 EXPECT_EQ(0, network_delegate.destroyed_requests());
4509 }
4510 EXPECT_EQ(1, network_delegate.destroyed_requests());
4511
4512 size_t log_position = 0;
vishal.b62985ca92015-04-17 08:45:514513 TestNetLog::CapturedEntryList entries;
[email protected]abe1c4a2013-10-25 19:28:514514 net_log_.GetEntries(&entries);
4515 for (size_t i = 0; i < 3; ++i) {
4516 log_position = ExpectLogContainsSomewhereAfter(
4517 entries,
4518 log_position + 1,
4519 NetLog::TYPE_URL_REQUEST_DELEGATE,
4520 NetLog::PHASE_BEGIN);
4521
4522 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4523 log_position + 1);
4524
4525 ASSERT_LT(log_position, entries.size());
4526 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4527 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
[email protected]1826a402014-01-08 15:40:484528
4529 if (i == 1) {
4530 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4531 entries, log_position + 1);
4532 }
[email protected]abe1c4a2013-10-25 19:28:514533 }
4534
4535 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4536 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4537}
4538
4539// Tests handling of delegate info from a network delegate in the case of an
4540// HTTP redirect.
4541TEST_F(URLRequestTestHTTP, NetworkDelegateInfoRedirect) {
4542 ASSERT_TRUE(test_server_.Start());
4543
4544 TestDelegate request_delegate;
4545 AsyncLoggingNetworkDelegate network_delegate;
4546 TestURLRequestContext context(true);
4547 context.set_network_delegate(&network_delegate);
4548 context.set_net_log(&net_log_);
4549 context.Init();
4550
4551 {
[email protected]f7022f32014-08-21 16:32:194552 scoped_ptr<URLRequest> r(context.CreateRequest(
4553 test_server_.GetURL("server-redirect?simple.html"), DEFAULT_PRIORITY,
davidben151423e2015-03-23 18:48:364554 &request_delegate));
[email protected]f7022f32014-08-21 16:32:194555 LoadStateWithParam load_state = r->GetLoadState();
[email protected]abe1c4a2013-10-25 19:28:514556 EXPECT_EQ(LOAD_STATE_IDLE, load_state.state);
[email protected]754bd202013-12-18 08:29:084557 EXPECT_EQ(base::string16(), load_state.param);
[email protected]abe1c4a2013-10-25 19:28:514558
[email protected]f7022f32014-08-21 16:32:194559 r->Start();
[email protected]abe1c4a2013-10-25 19:28:514560 base::RunLoop().Run();
4561
[email protected]f7022f32014-08-21 16:32:194562 EXPECT_EQ(200, r->GetResponseCode());
4563 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
[email protected]abe1c4a2013-10-25 19:28:514564 EXPECT_EQ(2, network_delegate.created_requests());
4565 EXPECT_EQ(0, network_delegate.destroyed_requests());
4566 }
4567 EXPECT_EQ(1, network_delegate.destroyed_requests());
4568
4569 size_t log_position = 0;
vishal.b62985ca92015-04-17 08:45:514570 TestNetLog::CapturedEntryList entries;
[email protected]abe1c4a2013-10-25 19:28:514571 net_log_.GetEntries(&entries);
4572 // The NetworkDelegate logged information in OnBeforeURLRequest,
4573 // OnBeforeSendHeaders, and OnHeadersReceived.
4574 for (size_t i = 0; i < 3; ++i) {
4575 log_position = ExpectLogContainsSomewhereAfter(
4576 entries,
4577 log_position + 1,
4578 NetLog::TYPE_URL_REQUEST_DELEGATE,
4579 NetLog::PHASE_BEGIN);
4580
4581 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4582 log_position + 1);
4583
4584 ASSERT_LT(log_position, entries.size());
4585 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4586 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
[email protected]1826a402014-01-08 15:40:484587
4588 if (i == 1) {
4589 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4590 entries, log_position + 1);
4591 }
[email protected]abe1c4a2013-10-25 19:28:514592 }
4593
4594 // The URLRequest::Delegate then gets informed about the redirect.
4595 log_position = ExpectLogContainsSomewhereAfter(
4596 entries,
4597 log_position + 1,
4598 NetLog::TYPE_URL_REQUEST_DELEGATE,
4599 NetLog::PHASE_BEGIN);
4600
4601 // The NetworkDelegate logged information in the same three events as before.
4602 for (size_t i = 0; i < 3; ++i) {
4603 log_position = ExpectLogContainsSomewhereAfter(
4604 entries,
4605 log_position + 1,
4606 NetLog::TYPE_URL_REQUEST_DELEGATE,
4607 NetLog::PHASE_BEGIN);
4608
4609 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4610 log_position + 1);
4611
4612 ASSERT_LT(log_position, entries.size());
4613 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4614 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4615 }
4616
4617 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4618 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4619}
4620
4621// Tests handling of delegate info from a network delegate in the case of HTTP
4622// AUTH.
4623TEST_F(URLRequestTestHTTP, NetworkDelegateInfoAuth) {
4624 ASSERT_TRUE(test_server_.Start());
4625
4626 TestDelegate request_delegate;
4627 AsyncLoggingNetworkDelegate network_delegate;
4628 TestURLRequestContext context(true);
4629 context.set_network_delegate(&network_delegate);
4630 context.set_net_log(&net_log_);
4631 context.Init();
4632
4633 {
davidben151423e2015-03-23 18:48:364634 scoped_ptr<URLRequest> r(
4635 context.CreateRequest(test_server_.GetURL("auth-basic"),
4636 DEFAULT_PRIORITY, &request_delegate));
[email protected]f7022f32014-08-21 16:32:194637 LoadStateWithParam load_state = r->GetLoadState();
[email protected]abe1c4a2013-10-25 19:28:514638 EXPECT_EQ(LOAD_STATE_IDLE, load_state.state);
[email protected]754bd202013-12-18 08:29:084639 EXPECT_EQ(base::string16(), load_state.param);
[email protected]abe1c4a2013-10-25 19:28:514640
[email protected]f7022f32014-08-21 16:32:194641 r->Start();
[email protected]abe1c4a2013-10-25 19:28:514642 base::RunLoop().Run();
4643
[email protected]f7022f32014-08-21 16:32:194644 EXPECT_EQ(200, r->GetResponseCode());
4645 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
[email protected]abe1c4a2013-10-25 19:28:514646 EXPECT_EQ(1, network_delegate.created_requests());
4647 EXPECT_EQ(0, network_delegate.destroyed_requests());
4648 }
4649 EXPECT_EQ(1, network_delegate.destroyed_requests());
4650
4651 size_t log_position = 0;
vishal.b62985ca92015-04-17 08:45:514652 TestNetLog::CapturedEntryList entries;
[email protected]abe1c4a2013-10-25 19:28:514653 net_log_.GetEntries(&entries);
4654 // The NetworkDelegate should have logged information in OnBeforeURLRequest,
4655 // OnBeforeSendHeaders, OnHeadersReceived, OnAuthRequired, and then again in
4656 // OnBeforeURLRequest and OnBeforeSendHeaders.
4657 for (size_t i = 0; i < 6; ++i) {
4658 log_position = ExpectLogContainsSomewhereAfter(
4659 entries,
4660 log_position + 1,
4661 NetLog::TYPE_URL_REQUEST_DELEGATE,
4662 NetLog::PHASE_BEGIN);
4663
4664 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4665 log_position + 1);
4666
4667 ASSERT_LT(log_position, entries.size());
4668 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4669 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
[email protected]1826a402014-01-08 15:40:484670
4671 if (i == 1) {
4672 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4673 entries, log_position + 1);
4674 }
[email protected]abe1c4a2013-10-25 19:28:514675 }
4676
4677 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4678 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4679}
4680
4681// Tests handling of delegate info from a URLRequest::Delegate.
4682TEST_F(URLRequestTestHTTP, URLRequestDelegateInfo) {
4683 ASSERT_TRUE(test_server_.Start());
4684
4685 AsyncLoggingUrlRequestDelegate request_delegate(
4686 AsyncLoggingUrlRequestDelegate::NO_CANCEL);
4687 TestURLRequestContext context(true);
4688 context.set_network_delegate(NULL);
4689 context.set_net_log(&net_log_);
4690 context.Init();
4691
4692 {
4693 // A chunked response with delays between chunks is used to make sure that
4694 // attempts by the URLRequest delegate to log information while reading the
4695 // body are ignored. Since they are ignored, this test is robust against
[email protected]1826a402014-01-08 15:40:484696 // the possibility of multiple reads being combined in the unlikely event
[email protected]abe1c4a2013-10-25 19:28:514697 // that it occurs.
[email protected]f7022f32014-08-21 16:32:194698 scoped_ptr<URLRequest> r(context.CreateRequest(
4699 test_server_.GetURL("chunked?waitBetweenChunks=20"), DEFAULT_PRIORITY,
davidben151423e2015-03-23 18:48:364700 &request_delegate));
[email protected]f7022f32014-08-21 16:32:194701 LoadStateWithParam load_state = r->GetLoadState();
4702 r->Start();
[email protected]abe1c4a2013-10-25 19:28:514703 base::RunLoop().Run();
4704
[email protected]f7022f32014-08-21 16:32:194705 EXPECT_EQ(200, r->GetResponseCode());
4706 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
[email protected]abe1c4a2013-10-25 19:28:514707 }
4708
vishal.b62985ca92015-04-17 08:45:514709 TestNetLog::CapturedEntryList entries;
[email protected]abe1c4a2013-10-25 19:28:514710 net_log_.GetEntries(&entries);
4711
[email protected]1826a402014-01-08 15:40:484712 size_t log_position = 0;
4713
4714 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4715 entries, log_position);
4716
[email protected]abe1c4a2013-10-25 19:28:514717 // The delegate info should only have been logged on header complete. Other
4718 // times it should silently be ignored.
[email protected]1826a402014-01-08 15:40:484719 log_position =
4720 ExpectLogContainsSomewhereAfter(entries,
4721 log_position + 1,
4722 NetLog::TYPE_URL_REQUEST_DELEGATE,
4723 NetLog::PHASE_BEGIN);
[email protected]abe1c4a2013-10-25 19:28:514724
4725 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4726 log_position + 1);
4727
4728 ASSERT_LT(log_position, entries.size());
4729 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4730 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4731
4732 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4733 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4734 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4735 entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE));
4736}
4737
4738// Tests handling of delegate info from a URLRequest::Delegate in the case of
4739// an HTTP redirect.
4740TEST_F(URLRequestTestHTTP, URLRequestDelegateInfoOnRedirect) {
4741 ASSERT_TRUE(test_server_.Start());
4742
4743 AsyncLoggingUrlRequestDelegate request_delegate(
4744 AsyncLoggingUrlRequestDelegate::NO_CANCEL);
4745 TestURLRequestContext context(true);
4746 context.set_network_delegate(NULL);
4747 context.set_net_log(&net_log_);
4748 context.Init();
4749
4750 {
[email protected]f7022f32014-08-21 16:32:194751 scoped_ptr<URLRequest> r(context.CreateRequest(
4752 test_server_.GetURL("server-redirect?simple.html"), DEFAULT_PRIORITY,
davidben151423e2015-03-23 18:48:364753 &request_delegate));
[email protected]f7022f32014-08-21 16:32:194754 LoadStateWithParam load_state = r->GetLoadState();
4755 r->Start();
[email protected]abe1c4a2013-10-25 19:28:514756 base::RunLoop().Run();
4757
[email protected]f7022f32014-08-21 16:32:194758 EXPECT_EQ(200, r->GetResponseCode());
4759 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
[email protected]abe1c4a2013-10-25 19:28:514760 }
4761
vishal.b62985ca92015-04-17 08:45:514762 TestNetLog::CapturedEntryList entries;
[email protected]abe1c4a2013-10-25 19:28:514763 net_log_.GetEntries(&entries);
4764
4765 // Delegate info should only have been logged in OnReceivedRedirect and
4766 // OnResponseStarted.
4767 size_t log_position = 0;
4768 for (int i = 0; i < 2; ++i) {
[email protected]1826a402014-01-08 15:40:484769 if (i == 0) {
4770 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4771 entries, log_position) + 1;
4772 }
4773
[email protected]abe1c4a2013-10-25 19:28:514774 log_position = ExpectLogContainsSomewhereAfter(
4775 entries,
4776 log_position,
4777 NetLog::TYPE_URL_REQUEST_DELEGATE,
4778 NetLog::PHASE_BEGIN);
4779
4780 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4781 log_position + 1);
4782
4783 ASSERT_LT(log_position, entries.size());
4784 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4785 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4786 }
4787
4788 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4789 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4790 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4791 entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE));
4792}
4793
4794// Tests handling of delegate info from a URLRequest::Delegate in the case of
4795// an HTTP redirect, with cancellation at various points.
4796TEST_F(URLRequestTestHTTP, URLRequestDelegateOnRedirectCancelled) {
4797 ASSERT_TRUE(test_server_.Start());
4798
4799 const AsyncLoggingUrlRequestDelegate::CancelStage kCancelStages[] = {
4800 AsyncLoggingUrlRequestDelegate::CANCEL_ON_RECEIVED_REDIRECT,
4801 AsyncLoggingUrlRequestDelegate::CANCEL_ON_RESPONSE_STARTED,
4802 AsyncLoggingUrlRequestDelegate::CANCEL_ON_READ_COMPLETED,
4803 };
4804
4805 for (size_t test_case = 0; test_case < arraysize(kCancelStages);
4806 ++test_case) {
4807 AsyncLoggingUrlRequestDelegate request_delegate(kCancelStages[test_case]);
4808 TestURLRequestContext context(true);
vishal.b62985ca92015-04-17 08:45:514809 TestNetLog net_log;
[email protected]abe1c4a2013-10-25 19:28:514810 context.set_network_delegate(NULL);
4811 context.set_net_log(&net_log);
4812 context.Init();
4813
4814 {
[email protected]f7022f32014-08-21 16:32:194815 scoped_ptr<URLRequest> r(context.CreateRequest(
4816 test_server_.GetURL("server-redirect?simple.html"), DEFAULT_PRIORITY,
davidben151423e2015-03-23 18:48:364817 &request_delegate));
[email protected]f7022f32014-08-21 16:32:194818 LoadStateWithParam load_state = r->GetLoadState();
4819 r->Start();
[email protected]abe1c4a2013-10-25 19:28:514820 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:194821 EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status());
[email protected]abe1c4a2013-10-25 19:28:514822 }
4823
vishal.b62985ca92015-04-17 08:45:514824 TestNetLog::CapturedEntryList entries;
[email protected]abe1c4a2013-10-25 19:28:514825 net_log.GetEntries(&entries);
4826
4827 // Delegate info is always logged in both OnReceivedRedirect and
4828 // OnResponseStarted. In the CANCEL_ON_RECEIVED_REDIRECT, the
4829 // OnResponseStarted delegate call is after cancellation, but logging is
4830 // still currently supported in that call.
4831 size_t log_position = 0;
4832 for (int i = 0; i < 2; ++i) {
[email protected]1826a402014-01-08 15:40:484833 if (i == 0) {
4834 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4835 entries, log_position) + 1;
4836 }
4837
[email protected]abe1c4a2013-10-25 19:28:514838 log_position = ExpectLogContainsSomewhereAfter(
4839 entries,
4840 log_position,
4841 NetLog::TYPE_URL_REQUEST_DELEGATE,
4842 NetLog::PHASE_BEGIN);
4843
4844 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4845 log_position + 1);
4846
4847 ASSERT_LT(log_position, entries.size());
4848 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4849 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4850 }
4851
4852 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4853 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4854 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4855 entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE));
4856 }
4857}
4858
[email protected]847c0fa92012-11-06 16:37:424859namespace {
4860
4861const char kExtraHeader[] = "Allow-Snafu";
4862const char kExtraValue[] = "fubar";
4863
4864class RedirectWithAdditionalHeadersDelegate : public TestDelegate {
dchengb03027d2014-10-21 12:00:204865 void OnReceivedRedirect(URLRequest* request,
4866 const RedirectInfo& redirect_info,
4867 bool* defer_redirect) override {
[email protected]cba24642014-08-15 20:49:594868 TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect);
[email protected]847c0fa92012-11-06 16:37:424869 request->SetExtraRequestHeaderByName(kExtraHeader, kExtraValue, false);
4870 }
4871};
4872
4873} // namespace
4874
4875TEST_F(URLRequestTestHTTP, RedirectWithAdditionalHeadersTest) {
4876 ASSERT_TRUE(test_server_.Start());
4877
4878 GURL destination_url = test_server_.GetURL(
4879 "echoheader?" + std::string(kExtraHeader));
4880 GURL original_url = test_server_.GetURL(
4881 "server-redirect?" + destination_url.spec());
4882 RedirectWithAdditionalHeadersDelegate d;
davidben151423e2015-03-23 18:48:364883 scoped_ptr<URLRequest> req(
4884 default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:194885 req->Start();
[email protected]255620da2013-08-19 13:14:294886 base::RunLoop().Run();
[email protected]847c0fa92012-11-06 16:37:424887
4888 std::string value;
[email protected]f7022f32014-08-21 16:32:194889 const HttpRequestHeaders& headers = req->extra_request_headers();
[email protected]847c0fa92012-11-06 16:37:424890 EXPECT_TRUE(headers.GetHeader(kExtraHeader, &value));
4891 EXPECT_EQ(kExtraValue, value);
[email protected]f7022f32014-08-21 16:32:194892 EXPECT_FALSE(req->is_pending());
4893 EXPECT_FALSE(req->is_redirecting());
[email protected]847c0fa92012-11-06 16:37:424894 EXPECT_EQ(kExtraValue, d.data_received());
4895}
4896
[email protected]251a1b92012-11-13 11:01:094897namespace {
4898
4899const char kExtraHeaderToRemove[] = "To-Be-Removed";
4900
4901class RedirectWithHeaderRemovalDelegate : public TestDelegate {
dchengb03027d2014-10-21 12:00:204902 void OnReceivedRedirect(URLRequest* request,
4903 const RedirectInfo& redirect_info,
4904 bool* defer_redirect) override {
[email protected]cba24642014-08-15 20:49:594905 TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect);
[email protected]251a1b92012-11-13 11:01:094906 request->RemoveRequestHeaderByName(kExtraHeaderToRemove);
4907 }
4908};
4909
4910} // namespace
4911
4912TEST_F(URLRequestTestHTTP, RedirectWithHeaderRemovalTest) {
4913 ASSERT_TRUE(test_server_.Start());
4914
4915 GURL destination_url = test_server_.GetURL(
4916 "echoheader?" + std::string(kExtraHeaderToRemove));
4917 GURL original_url = test_server_.GetURL(
4918 "server-redirect?" + destination_url.spec());
4919 RedirectWithHeaderRemovalDelegate d;
davidben151423e2015-03-23 18:48:364920 scoped_ptr<URLRequest> req(
4921 default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:194922 req->SetExtraRequestHeaderByName(kExtraHeaderToRemove, "dummy", false);
4923 req->Start();
[email protected]255620da2013-08-19 13:14:294924 base::RunLoop().Run();
[email protected]251a1b92012-11-13 11:01:094925
4926 std::string value;
[email protected]f7022f32014-08-21 16:32:194927 const HttpRequestHeaders& headers = req->extra_request_headers();
[email protected]251a1b92012-11-13 11:01:094928 EXPECT_FALSE(headers.GetHeader(kExtraHeaderToRemove, &value));
[email protected]f7022f32014-08-21 16:32:194929 EXPECT_FALSE(req->is_pending());
4930 EXPECT_FALSE(req->is_redirecting());
[email protected]251a1b92012-11-13 11:01:094931 EXPECT_EQ("None", d.data_received());
4932}
4933
[email protected]316c1e5e2012-09-12 15:17:444934TEST_F(URLRequestTestHTTP, CancelTest) {
4935 TestDelegate d;
4936 {
[email protected]f7022f32014-08-21 16:32:194937 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:364938 GURL("http://www.google.com/"), DEFAULT_PRIORITY, &d));
[email protected]316c1e5e2012-09-12 15:17:444939
[email protected]f7022f32014-08-21 16:32:194940 r->Start();
4941 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:444942
[email protected]f7022f32014-08-21 16:32:194943 r->Cancel();
[email protected]316c1e5e2012-09-12 15:17:444944
[email protected]255620da2013-08-19 13:14:294945 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:444946
4947 // We expect to receive OnResponseStarted even though the request has been
4948 // cancelled.
4949 EXPECT_EQ(1, d.response_started_count());
4950 EXPECT_EQ(0, d.bytes_received());
4951 EXPECT_FALSE(d.received_data_before_response());
4952 }
4953}
4954
4955TEST_F(URLRequestTestHTTP, CancelTest2) {
4956 ASSERT_TRUE(test_server_.Start());
4957
4958 TestDelegate d;
4959 {
[email protected]f7022f32014-08-21 16:32:194960 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:364961 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d));
[email protected]316c1e5e2012-09-12 15:17:444962
4963 d.set_cancel_in_response_started(true);
4964
[email protected]f7022f32014-08-21 16:32:194965 r->Start();
4966 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:444967
[email protected]255620da2013-08-19 13:14:294968 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:444969
4970 EXPECT_EQ(1, d.response_started_count());
4971 EXPECT_EQ(0, d.bytes_received());
4972 EXPECT_FALSE(d.received_data_before_response());
[email protected]f7022f32014-08-21 16:32:194973 EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status());
[email protected]316c1e5e2012-09-12 15:17:444974 }
4975}
4976
4977TEST_F(URLRequestTestHTTP, CancelTest3) {
4978 ASSERT_TRUE(test_server_.Start());
4979
4980 TestDelegate d;
4981 {
[email protected]f7022f32014-08-21 16:32:194982 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:364983 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d));
[email protected]316c1e5e2012-09-12 15:17:444984
4985 d.set_cancel_in_received_data(true);
4986
[email protected]f7022f32014-08-21 16:32:194987 r->Start();
4988 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:444989
[email protected]255620da2013-08-19 13:14:294990 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:444991
4992 EXPECT_EQ(1, d.response_started_count());
4993 // There is no guarantee about how much data was received
4994 // before the cancel was issued. It could have been 0 bytes,
4995 // or it could have been all the bytes.
4996 // EXPECT_EQ(0, d.bytes_received());
4997 EXPECT_FALSE(d.received_data_before_response());
[email protected]f7022f32014-08-21 16:32:194998 EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status());
[email protected]316c1e5e2012-09-12 15:17:444999 }
5000}
5001
5002TEST_F(URLRequestTestHTTP, CancelTest4) {
5003 ASSERT_TRUE(test_server_.Start());
5004
5005 TestDelegate d;
5006 {
[email protected]f7022f32014-08-21 16:32:195007 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:365008 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d));
[email protected]316c1e5e2012-09-12 15:17:445009
[email protected]f7022f32014-08-21 16:32:195010 r->Start();
5011 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:445012
5013 // The request will be implicitly canceled when it is destroyed. The
5014 // test delegate must not post a quit message when this happens because
5015 // this test doesn't actually have a message loop. The quit message would
5016 // get put on this thread's message queue and the next test would exit
5017 // early, causing problems.
5018 d.set_quit_on_complete(false);
5019 }
5020 // expect things to just cleanup properly.
5021
5022 // we won't actually get a received reponse here because we've never run the
5023 // message loop
5024 EXPECT_FALSE(d.received_data_before_response());
5025 EXPECT_EQ(0, d.bytes_received());
5026}
5027
5028TEST_F(URLRequestTestHTTP, CancelTest5) {
5029 ASSERT_TRUE(test_server_.Start());
5030
5031 // populate cache
5032 {
5033 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195034 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:365035 test_server_.GetURL("cachetime"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:195036 r->Start();
[email protected]255620da2013-08-19 13:14:295037 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:195038 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
[email protected]316c1e5e2012-09-12 15:17:445039 }
5040
5041 // cancel read from cache (see bug 990242)
5042 {
5043 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195044 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:365045 test_server_.GetURL("cachetime"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:195046 r->Start();
5047 r->Cancel();
[email protected]255620da2013-08-19 13:14:295048 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445049
[email protected]f7022f32014-08-21 16:32:195050 EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status());
[email protected]316c1e5e2012-09-12 15:17:445051 EXPECT_EQ(1, d.response_started_count());
5052 EXPECT_EQ(0, d.bytes_received());
5053 EXPECT_FALSE(d.received_data_before_response());
5054 }
5055}
5056
5057TEST_F(URLRequestTestHTTP, PostTest) {
5058 ASSERT_TRUE(test_server_.Start());
5059 HTTPUploadDataOperationTest("POST");
5060}
5061
5062TEST_F(URLRequestTestHTTP, PutTest) {
5063 ASSERT_TRUE(test_server_.Start());
5064 HTTPUploadDataOperationTest("PUT");
5065}
5066
5067TEST_F(URLRequestTestHTTP, PostEmptyTest) {
5068 ASSERT_TRUE(test_server_.Start());
5069
5070 TestDelegate d;
5071 {
[email protected]f7022f32014-08-21 16:32:195072 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:365073 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:195074 r->set_method("POST");
[email protected]316c1e5e2012-09-12 15:17:445075
[email protected]f7022f32014-08-21 16:32:195076 r->Start();
5077 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:445078
[email protected]255620da2013-08-19 13:14:295079 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445080
[email protected]329b68b2012-11-14 17:54:275081 ASSERT_EQ(1, d.response_started_count())
[email protected]f7022f32014-08-21 16:32:195082 << "request failed: " << r->status().status()
5083 << ", error: " << r->status().error();
[email protected]316c1e5e2012-09-12 15:17:445084
5085 EXPECT_FALSE(d.received_data_before_response());
5086 EXPECT_TRUE(d.data_received().empty());
5087 }
5088}
5089
5090TEST_F(URLRequestTestHTTP, PostFileTest) {
5091 ASSERT_TRUE(test_server_.Start());
5092
5093 TestDelegate d;
5094 {
[email protected]f7022f32014-08-21 16:32:195095 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:365096 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:195097 r->set_method("POST");
[email protected]316c1e5e2012-09-12 15:17:445098
[email protected]6cdfd7f2013-02-08 20:40:155099 base::FilePath dir;
[email protected]316c1e5e2012-09-12 15:17:445100 PathService::Get(base::DIR_EXE, &dir);
[email protected]37b3c1992014-03-11 20:59:025101 base::SetCurrentDirectory(dir);
[email protected]316c1e5e2012-09-12 15:17:445102
[email protected]f288ef02012-12-15 20:28:285103 ScopedVector<UploadElementReader> element_readers;
[email protected]316c1e5e2012-09-12 15:17:445104
[email protected]6cdfd7f2013-02-08 20:40:155105 base::FilePath path;
[email protected]316c1e5e2012-09-12 15:17:445106 PathService::Get(base::DIR_SOURCE_ROOT, &path);
5107 path = path.Append(FILE_PATH_LITERAL("net"));
5108 path = path.Append(FILE_PATH_LITERAL("data"));
5109 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
5110 path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
[email protected]cadac622013-06-11 16:46:365111 element_readers.push_back(
5112 new UploadFileElementReader(base::MessageLoopProxy::current().get(),
5113 path,
5114 0,
5115 kuint64max,
5116 base::Time()));
mmenkecbc2b712014-10-09 20:29:075117 r->set_upload(make_scoped_ptr<UploadDataStream>(
5118 new ElementsUploadDataStream(element_readers.Pass(), 0)));
[email protected]316c1e5e2012-09-12 15:17:445119
[email protected]f7022f32014-08-21 16:32:195120 r->Start();
5121 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:445122
[email protected]255620da2013-08-19 13:14:295123 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445124
pkasting6b68a162014-12-01 22:10:295125 int64 size64 = 0;
5126 ASSERT_EQ(true, base::GetFileSize(path, &size64));
5127 ASSERT_LE(size64, std::numeric_limits<int>::max());
5128 int size = static_cast<int>(size64);
[email protected]4356f0f2013-04-07 00:58:175129 scoped_ptr<char[]> buf(new char[size]);
[email protected]316c1e5e2012-09-12 15:17:445130
[email protected]7600d0b2013-12-08 21:43:305131 ASSERT_EQ(size, base::ReadFile(path, buf.get(), size));
[email protected]316c1e5e2012-09-12 15:17:445132
[email protected]329b68b2012-11-14 17:54:275133 ASSERT_EQ(1, d.response_started_count())
[email protected]f7022f32014-08-21 16:32:195134 << "request failed: " << r->status().status()
5135 << ", error: " << r->status().error();
[email protected]316c1e5e2012-09-12 15:17:445136
5137 EXPECT_FALSE(d.received_data_before_response());
5138
[email protected]329b68b2012-11-14 17:54:275139 EXPECT_EQ(size, d.bytes_received());
5140 EXPECT_EQ(std::string(&buf[0], size), d.data_received());
[email protected]316c1e5e2012-09-12 15:17:445141 }
5142}
5143
[email protected]999dd8c2013-11-12 06:45:545144TEST_F(URLRequestTestHTTP, PostUnreadableFileTest) {
5145 ASSERT_TRUE(test_server_.Start());
5146
5147 TestDelegate d;
5148 {
[email protected]f7022f32014-08-21 16:32:195149 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:365150 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:195151 r->set_method("POST");
[email protected]999dd8c2013-11-12 06:45:545152
5153 ScopedVector<UploadElementReader> element_readers;
5154
5155 element_readers.push_back(new UploadFileElementReader(
5156 base::MessageLoopProxy::current().get(),
5157 base::FilePath(FILE_PATH_LITERAL(
5158 "c:\\path\\to\\non\\existant\\file.randomness.12345")),
5159 0,
5160 kuint64max,
5161 base::Time()));
mmenkecbc2b712014-10-09 20:29:075162 r->set_upload(make_scoped_ptr<UploadDataStream>(
5163 new ElementsUploadDataStream(element_readers.Pass(), 0)));
[email protected]999dd8c2013-11-12 06:45:545164
[email protected]f7022f32014-08-21 16:32:195165 r->Start();
5166 EXPECT_TRUE(r->is_pending());
[email protected]999dd8c2013-11-12 06:45:545167
5168 base::RunLoop().Run();
5169
[email protected]999dd8c2013-11-12 06:45:545170 EXPECT_TRUE(d.request_failed());
5171 EXPECT_FALSE(d.received_data_before_response());
5172 EXPECT_EQ(0, d.bytes_received());
[email protected]f7022f32014-08-21 16:32:195173 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
5174 EXPECT_EQ(ERR_FILE_NOT_FOUND, r->status().error());
[email protected]999dd8c2013-11-12 06:45:545175 }
5176}
5177
[email protected]316c1e5e2012-09-12 15:17:445178TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) {
5179 ASSERT_TRUE(test_server_.Start());
5180
5181 TestDelegate d;
5182 {
[email protected]f7022f32014-08-21 16:32:195183 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:365184 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:195185 r->EnableChunkedUpload();
5186 r->set_method("POST");
5187 AddChunksToUpload(r.get());
5188 r->Start();
5189 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:445190
[email protected]255620da2013-08-19 13:14:295191 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445192
[email protected]f7022f32014-08-21 16:32:195193 VerifyReceivedDataMatchesChunks(r.get(), &d);
[email protected]316c1e5e2012-09-12 15:17:445194 }
5195}
5196
[email protected]329b68b2012-11-14 17:54:275197TEST_F(URLRequestTestHTTP, TestPostChunkedDataJustAfterStart) {
5198 ASSERT_TRUE(test_server_.Start());
5199
5200 TestDelegate d;
5201 {
[email protected]f7022f32014-08-21 16:32:195202 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:365203 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:195204 r->EnableChunkedUpload();
5205 r->set_method("POST");
5206 r->Start();
5207 EXPECT_TRUE(r->is_pending());
5208 AddChunksToUpload(r.get());
[email protected]255620da2013-08-19 13:14:295209 base::RunLoop().Run();
[email protected]329b68b2012-11-14 17:54:275210
[email protected]f7022f32014-08-21 16:32:195211 VerifyReceivedDataMatchesChunks(r.get(), &d);
[email protected]329b68b2012-11-14 17:54:275212 }
5213}
5214
[email protected]316c1e5e2012-09-12 15:17:445215TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) {
5216 ASSERT_TRUE(test_server_.Start());
5217
5218 TestDelegate d;
5219 {
[email protected]f7022f32014-08-21 16:32:195220 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:365221 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:195222 r->EnableChunkedUpload();
5223 r->set_method("POST");
5224 r->Start();
5225 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:445226
[email protected]255620da2013-08-19 13:14:295227 base::RunLoop().RunUntilIdle();
[email protected]f7022f32014-08-21 16:32:195228 AddChunksToUpload(r.get());
[email protected]255620da2013-08-19 13:14:295229 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445230
[email protected]f7022f32014-08-21 16:32:195231 VerifyReceivedDataMatchesChunks(r.get(), &d);
[email protected]316c1e5e2012-09-12 15:17:445232 }
5233}
5234
5235TEST_F(URLRequestTestHTTP, ResponseHeadersTest) {
5236 ASSERT_TRUE(test_server_.Start());
5237
5238 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195239 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:365240 test_server_.GetURL("files/with-headers.html"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:195241 req->Start();
[email protected]255620da2013-08-19 13:14:295242 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445243
[email protected]f7022f32014-08-21 16:32:195244 const HttpResponseHeaders* headers = req->response_headers();
[email protected]316c1e5e2012-09-12 15:17:445245
5246 // Simple sanity check that response_info() accesses the same data.
[email protected]f7022f32014-08-21 16:32:195247 EXPECT_EQ(headers, req->response_info().headers.get());
[email protected]316c1e5e2012-09-12 15:17:445248
5249 std::string header;
5250 EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header));
5251 EXPECT_EQ("private", header);
5252
5253 header.clear();
5254 EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header));
5255 EXPECT_EQ("text/html; charset=ISO-8859-1", header);
5256
5257 // The response has two "X-Multiple-Entries" headers.
5258 // This verfies our output has them concatenated together.
5259 header.clear();
5260 EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header));
5261 EXPECT_EQ("a, b", header);
5262}
5263
[email protected]242d8562012-10-30 21:20:465264TEST_F(URLRequestTestHTTP, ProcessSTS) {
estarka5da76702015-04-09 04:00:165265 SpawnedTestServer::SSLOptions ssl_options(
5266 SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN);
[email protected]ce7d0cbc2013-05-03 18:57:225267 SpawnedTestServer https_test_server(
5268 SpawnedTestServer::TYPE_HTTPS,
[email protected]242d8562012-10-30 21:20:465269 ssl_options,
[email protected]6cdfd7f2013-02-08 20:40:155270 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
[email protected]242d8562012-10-30 21:20:465271 ASSERT_TRUE(https_test_server.Start());
5272
estarka5da76702015-04-09 04:00:165273 std::string test_server_hostname = https_test_server.GetURL("").host();
5274
[email protected]242d8562012-10-30 21:20:465275 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195276 scoped_ptr<URLRequest> request(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:365277 https_test_server.GetURL("files/hsts-headers.html"), DEFAULT_PRIORITY,
5278 &d));
[email protected]f7022f32014-08-21 16:32:195279 request->Start();
[email protected]255620da2013-08-19 13:14:295280 base::RunLoop().Run();
[email protected]242d8562012-10-30 21:20:465281
5282 TransportSecurityState* security_state =
5283 default_context_.transport_security_state();
[email protected]242d8562012-10-30 21:20:465284 TransportSecurityState::DomainState domain_state;
estarka5da76702015-04-09 04:00:165285 EXPECT_TRUE(security_state->GetDynamicDomainState(test_server_hostname,
5286 &domain_state));
[email protected]242d8562012-10-30 21:20:465287 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
[email protected]9e6968d2014-05-07 21:46:265288 domain_state.sts.upgrade_mode);
5289 EXPECT_TRUE(domain_state.sts.include_subdomains);
5290 EXPECT_FALSE(domain_state.pkp.include_subdomains);
[email protected]37fd55fb2013-06-29 13:13:275291#if defined(OS_ANDROID)
5292 // Android's CertVerifyProc does not (yet) handle pins.
5293#else
5294 EXPECT_FALSE(domain_state.HasPublicKeyPins());
5295#endif
5296}
5297
estarka5da76702015-04-09 04:00:165298TEST_F(URLRequestTestHTTP, STSNotProcessedOnIP) {
5299 SpawnedTestServer https_test_server(
5300 SpawnedTestServer::TYPE_HTTPS, SpawnedTestServer::SSLOptions(),
5301 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
5302 ASSERT_TRUE(https_test_server.Start());
5303 // Make sure this test fails if the test server is changed to not
5304 // listen on an IP by default.
5305 ASSERT_TRUE(https_test_server.GetURL("").HostIsIPAddress());
5306 std::string test_server_hostname = https_test_server.GetURL("").host();
5307
5308 TestDelegate d;
5309 scoped_ptr<URLRequest> request(default_context_.CreateRequest(
5310 https_test_server.GetURL("files/hsts-headers.html"), DEFAULT_PRIORITY,
5311 &d));
5312 request->Start();
5313 base::RunLoop().Run();
5314
5315 TransportSecurityState* security_state =
5316 default_context_.transport_security_state();
5317 TransportSecurityState::DomainState domain_state;
5318 EXPECT_FALSE(security_state->GetDynamicDomainState(test_server_hostname,
5319 &domain_state));
5320}
5321
[email protected]37fd55fb2013-06-29 13:13:275322// Android's CertVerifyProc does not (yet) handle pins. Therefore, it will
5323// reject HPKP headers, and a test setting only HPKP headers will fail (no
5324// DomainState present because header rejected).
5325#if defined(OS_ANDROID)
5326#define MAYBE_ProcessPKP DISABLED_ProcessPKP
5327#else
5328#define MAYBE_ProcessPKP ProcessPKP
5329#endif
5330
5331// Tests that enabling HPKP on a domain does not affect the HSTS
5332// validity/expiration.
5333TEST_F(URLRequestTestHTTP, MAYBE_ProcessPKP) {
estarka5da76702015-04-09 04:00:165334 SpawnedTestServer::SSLOptions ssl_options(
5335 SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN);
[email protected]37fd55fb2013-06-29 13:13:275336 SpawnedTestServer https_test_server(
5337 SpawnedTestServer::TYPE_HTTPS,
5338 ssl_options,
5339 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
5340 ASSERT_TRUE(https_test_server.Start());
5341
estarka5da76702015-04-09 04:00:165342 std::string test_server_hostname = https_test_server.GetURL("").host();
5343
[email protected]37fd55fb2013-06-29 13:13:275344 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195345 scoped_ptr<URLRequest> request(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:365346 https_test_server.GetURL("files/hpkp-headers.html"), DEFAULT_PRIORITY,
5347 &d));
[email protected]f7022f32014-08-21 16:32:195348 request->Start();
[email protected]255620da2013-08-19 13:14:295349 base::RunLoop().Run();
[email protected]37fd55fb2013-06-29 13:13:275350
5351 TransportSecurityState* security_state =
5352 default_context_.transport_security_state();
[email protected]37fd55fb2013-06-29 13:13:275353 TransportSecurityState::DomainState domain_state;
estarka5da76702015-04-09 04:00:165354 EXPECT_TRUE(security_state->GetDynamicDomainState(test_server_hostname,
5355 &domain_state));
[email protected]37fd55fb2013-06-29 13:13:275356 EXPECT_EQ(TransportSecurityState::DomainState::MODE_DEFAULT,
[email protected]9e6968d2014-05-07 21:46:265357 domain_state.sts.upgrade_mode);
5358 EXPECT_FALSE(domain_state.sts.include_subdomains);
5359 EXPECT_FALSE(domain_state.pkp.include_subdomains);
[email protected]37fd55fb2013-06-29 13:13:275360 EXPECT_TRUE(domain_state.HasPublicKeyPins());
[email protected]9e6968d2014-05-07 21:46:265361 EXPECT_NE(domain_state.sts.expiry, domain_state.pkp.expiry);
[email protected]242d8562012-10-30 21:20:465362}
5363
estarka5da76702015-04-09 04:00:165364TEST_F(URLRequestTestHTTP, PKPNotProcessedOnIP) {
5365 SpawnedTestServer https_test_server(
5366 SpawnedTestServer::TYPE_HTTPS, SpawnedTestServer::SSLOptions(),
5367 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
5368 ASSERT_TRUE(https_test_server.Start());
5369 // Make sure this test fails if the test server is changed to not
5370 // listen on an IP by default.
5371 ASSERT_TRUE(https_test_server.GetURL("").HostIsIPAddress());
5372 std::string test_server_hostname = https_test_server.GetURL("").host();
5373
5374 TestDelegate d;
5375 scoped_ptr<URLRequest> request(default_context_.CreateRequest(
5376 https_test_server.GetURL("files/hpkp-headers.html"), DEFAULT_PRIORITY,
5377 &d));
5378 request->Start();
5379 base::RunLoop().Run();
5380
5381 TransportSecurityState* security_state =
5382 default_context_.transport_security_state();
5383 TransportSecurityState::DomainState domain_state;
5384 EXPECT_FALSE(security_state->GetDynamicDomainState(test_server_hostname,
5385 &domain_state));
5386}
5387
[email protected]242d8562012-10-30 21:20:465388TEST_F(URLRequestTestHTTP, ProcessSTSOnce) {
estarka5da76702015-04-09 04:00:165389 SpawnedTestServer::SSLOptions ssl_options(
5390 SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN);
[email protected]ce7d0cbc2013-05-03 18:57:225391 SpawnedTestServer https_test_server(
5392 SpawnedTestServer::TYPE_HTTPS,
[email protected]242d8562012-10-30 21:20:465393 ssl_options,
[email protected]6cdfd7f2013-02-08 20:40:155394 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
[email protected]242d8562012-10-30 21:20:465395 ASSERT_TRUE(https_test_server.Start());
5396
estarka5da76702015-04-09 04:00:165397 std::string test_server_hostname = https_test_server.GetURL("").host();
5398
[email protected]242d8562012-10-30 21:20:465399 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195400 scoped_ptr<URLRequest> request(default_context_.CreateRequest(
[email protected]242d8562012-10-30 21:20:465401 https_test_server.GetURL("files/hsts-multiple-headers.html"),
davidben151423e2015-03-23 18:48:365402 DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:195403 request->Start();
[email protected]255620da2013-08-19 13:14:295404 base::RunLoop().Run();
[email protected]242d8562012-10-30 21:20:465405
5406 // We should have set parameters from the first header, not the second.
5407 TransportSecurityState* security_state =
5408 default_context_.transport_security_state();
[email protected]242d8562012-10-30 21:20:465409 TransportSecurityState::DomainState domain_state;
estarka5da76702015-04-09 04:00:165410 EXPECT_TRUE(security_state->GetDynamicDomainState(test_server_hostname,
5411 &domain_state));
[email protected]242d8562012-10-30 21:20:465412 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
[email protected]9e6968d2014-05-07 21:46:265413 domain_state.sts.upgrade_mode);
5414 EXPECT_FALSE(domain_state.sts.include_subdomains);
5415 EXPECT_FALSE(domain_state.pkp.include_subdomains);
[email protected]242d8562012-10-30 21:20:465416}
5417
[email protected]9f972ec2013-04-10 20:24:365418TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP) {
estarka5da76702015-04-09 04:00:165419 SpawnedTestServer::SSLOptions ssl_options(
5420 SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN);
[email protected]ce7d0cbc2013-05-03 18:57:225421 SpawnedTestServer https_test_server(
5422 SpawnedTestServer::TYPE_HTTPS,
[email protected]9f972ec2013-04-10 20:24:365423 ssl_options,
5424 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
5425 ASSERT_TRUE(https_test_server.Start());
5426
estarka5da76702015-04-09 04:00:165427 std::string test_server_hostname = https_test_server.GetURL("").host();
5428
[email protected]9f972ec2013-04-10 20:24:365429 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195430 scoped_ptr<URLRequest> request(default_context_.CreateRequest(
[email protected]9f972ec2013-04-10 20:24:365431 https_test_server.GetURL("files/hsts-and-hpkp-headers.html"),
davidben151423e2015-03-23 18:48:365432 DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:195433 request->Start();
[email protected]255620da2013-08-19 13:14:295434 base::RunLoop().Run();
[email protected]9f972ec2013-04-10 20:24:365435
5436 // We should have set parameters from the first header, not the second.
5437 TransportSecurityState* security_state =
5438 default_context_.transport_security_state();
[email protected]9f972ec2013-04-10 20:24:365439 TransportSecurityState::DomainState domain_state;
estarka5da76702015-04-09 04:00:165440 EXPECT_TRUE(security_state->GetDynamicDomainState(test_server_hostname,
5441 &domain_state));
[email protected]9f972ec2013-04-10 20:24:365442 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
[email protected]9e6968d2014-05-07 21:46:265443 domain_state.sts.upgrade_mode);
[email protected]9f972ec2013-04-10 20:24:365444#if defined(OS_ANDROID)
5445 // Android's CertVerifyProc does not (yet) handle pins.
5446#else
5447 EXPECT_TRUE(domain_state.HasPublicKeyPins());
5448#endif
[email protected]9e6968d2014-05-07 21:46:265449 EXPECT_NE(domain_state.sts.expiry, domain_state.pkp.expiry);
[email protected]9f972ec2013-04-10 20:24:365450
[email protected]a165f092013-06-12 16:10:055451 // Even though there is an HSTS header asserting includeSubdomains, it is
5452 // the *second* such header, and we MUST process only the first.
[email protected]9e6968d2014-05-07 21:46:265453 EXPECT_FALSE(domain_state.sts.include_subdomains);
[email protected]a165f092013-06-12 16:10:055454 // includeSubdomains does not occur in the test HPKP header.
[email protected]9e6968d2014-05-07 21:46:265455 EXPECT_FALSE(domain_state.pkp.include_subdomains);
[email protected]9f972ec2013-04-10 20:24:365456}
5457
[email protected]37fd55fb2013-06-29 13:13:275458// Tests that when multiple HPKP headers are present, asserting different
5459// policies, that only the first such policy is processed.
5460TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP2) {
estarka5da76702015-04-09 04:00:165461 SpawnedTestServer::SSLOptions ssl_options(
5462 SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN);
[email protected]37fd55fb2013-06-29 13:13:275463 SpawnedTestServer https_test_server(
5464 SpawnedTestServer::TYPE_HTTPS,
5465 ssl_options,
5466 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
5467 ASSERT_TRUE(https_test_server.Start());
5468
estarka5da76702015-04-09 04:00:165469 std::string test_server_hostname = https_test_server.GetURL("").host();
5470
[email protected]37fd55fb2013-06-29 13:13:275471 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195472 scoped_ptr<URLRequest> request(default_context_.CreateRequest(
[email protected]37fd55fb2013-06-29 13:13:275473 https_test_server.GetURL("files/hsts-and-hpkp-headers2.html"),
davidben151423e2015-03-23 18:48:365474 DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:195475 request->Start();
[email protected]255620da2013-08-19 13:14:295476 base::RunLoop().Run();
[email protected]37fd55fb2013-06-29 13:13:275477
5478 TransportSecurityState* security_state =
5479 default_context_.transport_security_state();
[email protected]37fd55fb2013-06-29 13:13:275480 TransportSecurityState::DomainState domain_state;
estarka5da76702015-04-09 04:00:165481 EXPECT_TRUE(security_state->GetDynamicDomainState(test_server_hostname,
5482 &domain_state));
[email protected]37fd55fb2013-06-29 13:13:275483 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
[email protected]9e6968d2014-05-07 21:46:265484 domain_state.sts.upgrade_mode);
[email protected]37fd55fb2013-06-29 13:13:275485#if defined(OS_ANDROID)
5486 // Android's CertVerifyProc does not (yet) handle pins.
5487#else
5488 EXPECT_TRUE(domain_state.HasPublicKeyPins());
5489#endif
[email protected]9e6968d2014-05-07 21:46:265490 EXPECT_NE(domain_state.sts.expiry, domain_state.pkp.expiry);
[email protected]37fd55fb2013-06-29 13:13:275491
[email protected]9e6968d2014-05-07 21:46:265492 EXPECT_TRUE(domain_state.sts.include_subdomains);
5493 EXPECT_FALSE(domain_state.pkp.include_subdomains);
[email protected]37fd55fb2013-06-29 13:13:275494}
5495
[email protected]316c1e5e2012-09-12 15:17:445496TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) {
5497 ASSERT_TRUE(test_server_.Start());
5498
5499 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195500 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5501 test_server_.GetURL("files/content-type-normalization.html"),
davidben151423e2015-03-23 18:48:365502 DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:195503 req->Start();
[email protected]255620da2013-08-19 13:14:295504 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445505
5506 std::string mime_type;
[email protected]f7022f32014-08-21 16:32:195507 req->GetMimeType(&mime_type);
[email protected]316c1e5e2012-09-12 15:17:445508 EXPECT_EQ("text/html", mime_type);
5509
5510 std::string charset;
[email protected]f7022f32014-08-21 16:32:195511 req->GetCharset(&charset);
[email protected]316c1e5e2012-09-12 15:17:445512 EXPECT_EQ("utf-8", charset);
[email protected]f7022f32014-08-21 16:32:195513 req->Cancel();
[email protected]316c1e5e2012-09-12 15:17:445514}
5515
[email protected]02494ec2014-05-07 15:05:295516TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictDataRedirects) {
[email protected]e0f35c92013-05-08 16:04:345517 // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget().
[email protected]e0f35c92013-05-08 16:04:345518 GURL data_url("data:,foo");
[email protected]e0f35c92013-05-08 16:04:345519 DataProtocolHandler data_protocol_handler;
[email protected]588614c22013-08-16 00:09:025520 EXPECT_FALSE(data_protocol_handler.IsSafeRedirectTarget(data_url));
[email protected]e0f35c92013-05-08 16:04:345521
5522 // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget().
bengr1bf8e942014-11-07 01:36:505523 EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(data_url));
[email protected]e0f35c92013-05-08 16:04:345524}
5525
[email protected]02494ec2014-05-07 15:05:295526#if !defined(DISABLE_FILE_SUPPORT)
5527TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictFileRedirects) {
5528 // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget().
5529 GURL file_url("file:///foo.txt");
5530 FileProtocolHandler file_protocol_handler(base::MessageLoopProxy::current());
5531 EXPECT_FALSE(file_protocol_handler.IsSafeRedirectTarget(file_url));
5532
5533 // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget().
bengr1bf8e942014-11-07 01:36:505534 EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(file_url));
[email protected]02494ec2014-05-07 15:05:295535}
5536
[email protected]588614c22013-08-16 00:09:025537TEST_F(URLRequestTestHTTP, RestrictFileRedirects) {
[email protected]316c1e5e2012-09-12 15:17:445538 ASSERT_TRUE(test_server_.Start());
5539
5540 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195541 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:365542 test_server_.GetURL("files/redirect-to-file.html"), DEFAULT_PRIORITY,
5543 &d));
[email protected]f7022f32014-08-21 16:32:195544 req->Start();
[email protected]255620da2013-08-19 13:14:295545 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445546
[email protected]f7022f32014-08-21 16:32:195547 EXPECT_EQ(URLRequestStatus::FAILED, req->status().status());
5548 EXPECT_EQ(ERR_UNSAFE_REDIRECT, req->status().error());
[email protected]316c1e5e2012-09-12 15:17:445549}
[email protected]02494ec2014-05-07 15:05:295550#endif // !defined(DISABLE_FILE_SUPPORT)
[email protected]316c1e5e2012-09-12 15:17:445551
[email protected]588614c22013-08-16 00:09:025552TEST_F(URLRequestTestHTTP, RestrictDataRedirects) {
5553 ASSERT_TRUE(test_server_.Start());
5554
5555 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195556 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:365557 test_server_.GetURL("files/redirect-to-data.html"), DEFAULT_PRIORITY,
5558 &d));
[email protected]f7022f32014-08-21 16:32:195559 req->Start();
[email protected]588614c22013-08-16 00:09:025560 base::MessageLoop::current()->Run();
5561
[email protected]f7022f32014-08-21 16:32:195562 EXPECT_EQ(URLRequestStatus::FAILED, req->status().status());
5563 EXPECT_EQ(ERR_UNSAFE_REDIRECT, req->status().error());
[email protected]588614c22013-08-16 00:09:025564}
5565
[email protected]316c1e5e2012-09-12 15:17:445566TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) {
5567 ASSERT_TRUE(test_server_.Start());
5568
5569 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195570 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5571 test_server_.GetURL("files/redirect-to-invalid-url.html"),
davidben151423e2015-03-23 18:48:365572 DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:195573 req->Start();
[email protected]255620da2013-08-19 13:14:295574 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445575
[email protected]f7022f32014-08-21 16:32:195576 EXPECT_EQ(URLRequestStatus::FAILED, req->status().status());
5577 EXPECT_EQ(ERR_INVALID_URL, req->status().error());
[email protected]316c1e5e2012-09-12 15:17:445578}
5579
[email protected]e50efea2014-03-24 18:41:005580// Make sure redirects are cached, despite not reading their bodies.
5581TEST_F(URLRequestTestHTTP, CacheRedirect) {
5582 ASSERT_TRUE(test_server_.Start());
5583 GURL redirect_url =
5584 test_server_.GetURL("files/redirect302-to-echo-cacheable");
5585
5586 {
5587 TestDelegate d;
davidben151423e2015-03-23 18:48:365588 scoped_ptr<URLRequest> req(
5589 default_context_.CreateRequest(redirect_url, DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:195590 req->Start();
[email protected]e50efea2014-03-24 18:41:005591 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:195592 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
[email protected]e50efea2014-03-24 18:41:005593 EXPECT_EQ(1, d.received_redirect_count());
[email protected]f7022f32014-08-21 16:32:195594 EXPECT_EQ(test_server_.GetURL("echo"), req->url());
[email protected]e50efea2014-03-24 18:41:005595 }
5596
5597 {
5598 TestDelegate d;
5599 d.set_quit_on_redirect(true);
davidben151423e2015-03-23 18:48:365600 scoped_ptr<URLRequest> req(
5601 default_context_.CreateRequest(redirect_url, DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:195602 req->Start();
[email protected]e50efea2014-03-24 18:41:005603 base::RunLoop().Run();
5604
5605 EXPECT_EQ(1, d.received_redirect_count());
5606 EXPECT_EQ(0, d.response_started_count());
[email protected]f7022f32014-08-21 16:32:195607 EXPECT_TRUE(req->was_cached());
[email protected]e50efea2014-03-24 18:41:005608
[email protected]f7022f32014-08-21 16:32:195609 req->FollowDeferredRedirect();
[email protected]e50efea2014-03-24 18:41:005610 base::RunLoop().Run();
5611 EXPECT_EQ(1, d.received_redirect_count());
5612 EXPECT_EQ(1, d.response_started_count());
[email protected]f7022f32014-08-21 16:32:195613 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
5614 EXPECT_EQ(test_server_.GetURL("echo"), req->url());
[email protected]e50efea2014-03-24 18:41:005615 }
5616}
5617
5618// Make sure a request isn't cached when a NetworkDelegate forces a redirect
5619// when the headers are read, since the body won't have been read.
5620TEST_F(URLRequestTestHTTP, NoCacheOnNetworkDelegateRedirect) {
5621 ASSERT_TRUE(test_server_.Start());
5622 // URL that is normally cached.
5623 GURL initial_url = test_server_.GetURL("cachetime");
5624
5625 {
5626 // Set up the TestNetworkDelegate tp force a redirect.
5627 GURL redirect_to_url = test_server_.GetURL("echo");
5628 default_network_delegate_.set_redirect_on_headers_received_url(
5629 redirect_to_url);
5630
5631 TestDelegate d;
davidben151423e2015-03-23 18:48:365632 scoped_ptr<URLRequest> req(
5633 default_context_.CreateRequest(initial_url, DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:195634 req->Start();
[email protected]e50efea2014-03-24 18:41:005635 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:195636 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
[email protected]e50efea2014-03-24 18:41:005637 EXPECT_EQ(1, d.received_redirect_count());
[email protected]f7022f32014-08-21 16:32:195638 EXPECT_EQ(redirect_to_url, req->url());
[email protected]e50efea2014-03-24 18:41:005639 }
5640
5641 {
5642 TestDelegate d;
davidben151423e2015-03-23 18:48:365643 scoped_ptr<URLRequest> req(
5644 default_context_.CreateRequest(initial_url, DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:195645 req->Start();
[email protected]e50efea2014-03-24 18:41:005646 base::RunLoop().Run();
5647
[email protected]f7022f32014-08-21 16:32:195648 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
5649 EXPECT_FALSE(req->was_cached());
[email protected]e50efea2014-03-24 18:41:005650 EXPECT_EQ(0, d.received_redirect_count());
[email protected]f7022f32014-08-21 16:32:195651 EXPECT_EQ(initial_url, req->url());
[email protected]e50efea2014-03-24 18:41:005652 }
5653}
5654
[email protected]5f714132014-03-26 10:41:165655// Tests that redirection to an unsafe URL is allowed when it has been marked as
5656// safe.
5657TEST_F(URLRequestTestHTTP, UnsafeRedirectToWhitelistedUnsafeURL) {
5658 ASSERT_TRUE(test_server_.Start());
5659
5660 GURL unsafe_url("data:text/html,this-is-considered-an-unsafe-url");
5661 default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url);
5662 default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url);
5663
5664 TestDelegate d;
5665 {
[email protected]f7022f32014-08-21 16:32:195666 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:365667 test_server_.GetURL("whatever"), DEFAULT_PRIORITY, &d));
[email protected]5f714132014-03-26 10:41:165668
[email protected]f7022f32014-08-21 16:32:195669 r->Start();
[email protected]5f714132014-03-26 10:41:165670 base::RunLoop().Run();
5671
[email protected]f7022f32014-08-21 16:32:195672 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
[email protected]5f714132014-03-26 10:41:165673
[email protected]f7022f32014-08-21 16:32:195674 EXPECT_EQ(2U, r->url_chain().size());
5675 EXPECT_EQ(OK, r->status().error());
5676 EXPECT_EQ(unsafe_url, r->url());
[email protected]5f714132014-03-26 10:41:165677 EXPECT_EQ("this-is-considered-an-unsafe-url", d.data_received());
5678 }
5679}
5680
5681// Tests that a redirect to a different unsafe URL is blocked, even after adding
5682// some other URL to the whitelist.
5683TEST_F(URLRequestTestHTTP, UnsafeRedirectToDifferentUnsafeURL) {
5684 ASSERT_TRUE(test_server_.Start());
5685
5686 GURL unsafe_url("data:text/html,something");
5687 GURL different_unsafe_url("data:text/html,something-else");
5688 default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url);
5689 default_network_delegate_.set_allowed_unsafe_redirect_url(
5690 different_unsafe_url);
5691
5692 TestDelegate d;
5693 {
[email protected]f7022f32014-08-21 16:32:195694 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:365695 test_server_.GetURL("whatever"), DEFAULT_PRIORITY, &d));
[email protected]5f714132014-03-26 10:41:165696
[email protected]f7022f32014-08-21 16:32:195697 r->Start();
[email protected]5f714132014-03-26 10:41:165698 base::RunLoop().Run();
5699
[email protected]f7022f32014-08-21 16:32:195700 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
5701 EXPECT_EQ(ERR_UNSAFE_REDIRECT, r->status().error());
[email protected]5f714132014-03-26 10:41:165702 }
5703}
5704
[email protected]5f714132014-03-26 10:41:165705// Redirects from an URL with fragment to an unsafe URL with fragment should
5706// be allowed, and the reference fragment of the target URL should be preserved.
5707TEST_F(URLRequestTestHTTP, UnsafeRedirectWithDifferentReferenceFragment) {
5708 ASSERT_TRUE(test_server_.Start());
5709
5710 GURL original_url(test_server_.GetURL("original#fragment1"));
5711 GURL unsafe_url("data:,url-marked-safe-and-used-in-redirect#fragment2");
5712 GURL expected_url("data:,url-marked-safe-and-used-in-redirect#fragment2");
5713
5714 default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url);
5715 default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url);
5716
5717 TestDelegate d;
5718 {
davidben151423e2015-03-23 18:48:365719 scoped_ptr<URLRequest> r(
5720 default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d));
[email protected]5f714132014-03-26 10:41:165721
[email protected]f7022f32014-08-21 16:32:195722 r->Start();
[email protected]5f714132014-03-26 10:41:165723 base::RunLoop().Run();
5724
[email protected]f7022f32014-08-21 16:32:195725 EXPECT_EQ(2U, r->url_chain().size());
5726 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
5727 EXPECT_EQ(OK, r->status().error());
5728 EXPECT_EQ(original_url, r->original_url());
5729 EXPECT_EQ(expected_url, r->url());
[email protected]5f714132014-03-26 10:41:165730 }
5731}
5732
5733// When a delegate has specified a safe redirect URL, but it does not match the
5734// redirect target, then do not prevent the reference fragment from being added.
[email protected]f878230e2014-04-03 15:36:145735TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragmentAndUnrelatedUnsafeUrl) {
[email protected]5f714132014-03-26 10:41:165736 ASSERT_TRUE(test_server_.Start());
5737
5738 GURL original_url(test_server_.GetURL("original#expected-fragment"));
5739 GURL unsafe_url("data:text/html,this-url-does-not-match-redirect-url");
5740 GURL redirect_url(test_server_.GetURL("target"));
5741 GURL expected_redirect_url(test_server_.GetURL("target#expected-fragment"));
5742
5743 default_network_delegate_.set_redirect_on_headers_received_url(redirect_url);
5744 default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url);
5745
5746 TestDelegate d;
5747 {
davidben151423e2015-03-23 18:48:365748 scoped_ptr<URLRequest> r(
5749 default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d));
[email protected]5f714132014-03-26 10:41:165750
[email protected]f7022f32014-08-21 16:32:195751 r->Start();
[email protected]5f714132014-03-26 10:41:165752 base::RunLoop().Run();
5753
[email protected]f7022f32014-08-21 16:32:195754 EXPECT_EQ(2U, r->url_chain().size());
5755 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
5756 EXPECT_EQ(OK, r->status().error());
5757 EXPECT_EQ(original_url, r->original_url());
5758 EXPECT_EQ(expected_redirect_url, r->url());
[email protected]5f714132014-03-26 10:41:165759 }
5760}
5761
[email protected]f878230e2014-04-03 15:36:145762// When a delegate has specified a safe redirect URL, assume that the redirect
5763// URL should not be changed. In particular, the reference fragment should not
5764// be modified.
5765TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragment) {
5766 ASSERT_TRUE(test_server_.Start());
5767
5768 GURL original_url(test_server_.GetURL("original#should-not-be-appended"));
5769 GURL redirect_url("data:text/html,expect-no-reference-fragment");
5770
5771 default_network_delegate_.set_redirect_on_headers_received_url(redirect_url);
5772 default_network_delegate_.set_allowed_unsafe_redirect_url(redirect_url);
5773
5774 TestDelegate d;
5775 {
davidben151423e2015-03-23 18:48:365776 scoped_ptr<URLRequest> r(
5777 default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d));
[email protected]f878230e2014-04-03 15:36:145778
[email protected]f7022f32014-08-21 16:32:195779 r->Start();
[email protected]f878230e2014-04-03 15:36:145780 base::RunLoop().Run();
5781
[email protected]f7022f32014-08-21 16:32:195782 EXPECT_EQ(2U, r->url_chain().size());
5783 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
5784 EXPECT_EQ(OK, r->status().error());
5785 EXPECT_EQ(original_url, r->original_url());
5786 EXPECT_EQ(redirect_url, r->url());
[email protected]f878230e2014-04-03 15:36:145787 }
5788}
5789
5790// When a URLRequestRedirectJob is created, the redirection must be followed and
5791// the reference fragment of the target URL must not be modified.
5792TEST_F(URLRequestTestHTTP, RedirectJobWithReferenceFragment) {
5793 ASSERT_TRUE(test_server_.Start());
5794
5795 GURL original_url(test_server_.GetURL("original#should-not-be-appended"));
5796 GURL redirect_url(test_server_.GetURL("echo"));
5797
5798 TestDelegate d;
davidben151423e2015-03-23 18:48:365799 scoped_ptr<URLRequest> r(
5800 default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d));
[email protected]f878230e2014-04-03 15:36:145801
5802 URLRequestRedirectJob* job = new URLRequestRedirectJob(
[email protected]f7022f32014-08-21 16:32:195803 r.get(), &default_network_delegate_, redirect_url,
[email protected]f878230e2014-04-03 15:36:145804 URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason");
5805 AddTestInterceptor()->set_main_intercept_job(job);
5806
[email protected]f7022f32014-08-21 16:32:195807 r->Start();
[email protected]f878230e2014-04-03 15:36:145808 base::RunLoop().Run();
5809
[email protected]f7022f32014-08-21 16:32:195810 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
5811 EXPECT_EQ(OK, r->status().error());
5812 EXPECT_EQ(original_url, r->original_url());
5813 EXPECT_EQ(redirect_url, r->url());
[email protected]f878230e2014-04-03 15:36:145814}
5815
[email protected]316c1e5e2012-09-12 15:17:445816TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) {
5817 ASSERT_TRUE(test_server_.Start());
5818
5819 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195820 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:365821 test_server_.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:195822 req->SetReferrer("http://user:[email protected]/");
5823 req->Start();
[email protected]255620da2013-08-19 13:14:295824 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445825
5826 EXPECT_EQ(std::string("http://foo.com/"), d.data_received());
5827}
5828
[email protected]99ecf6e2013-04-10 22:46:135829TEST_F(URLRequestTestHTTP, NoFragmentInReferrer) {
5830 ASSERT_TRUE(test_server_.Start());
5831
5832 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195833 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:365834 test_server_.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:195835 req->SetReferrer("http://foo.com/test#fragment");
5836 req->Start();
[email protected]255620da2013-08-19 13:14:295837 base::RunLoop().Run();
[email protected]99ecf6e2013-04-10 22:46:135838
5839 EXPECT_EQ(std::string("http://foo.com/test"), d.data_received());
5840}
5841
5842TEST_F(URLRequestTestHTTP, EmptyReferrerAfterValidReferrer) {
5843 ASSERT_TRUE(test_server_.Start());
5844
5845 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195846 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:365847 test_server_.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:195848 req->SetReferrer("http://foo.com/test#fragment");
5849 req->SetReferrer("");
5850 req->Start();
[email protected]255620da2013-08-19 13:14:295851 base::RunLoop().Run();
[email protected]99ecf6e2013-04-10 22:46:135852
5853 EXPECT_EQ(std::string("None"), d.data_received());
5854}
5855
[email protected]1826a402014-01-08 15:40:485856// Defer network start and then resume, checking that the request was a success
5857// and bytes were received.
5858TEST_F(URLRequestTestHTTP, DeferredBeforeNetworkStart) {
5859 ASSERT_TRUE(test_server_.Start());
5860
5861 TestDelegate d;
5862 {
5863 d.set_quit_on_network_start(true);
5864 GURL test_url(test_server_.GetURL("echo"));
davidben151423e2015-03-23 18:48:365865 scoped_ptr<URLRequest> req(
5866 default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d));
[email protected]1826a402014-01-08 15:40:485867
[email protected]f7022f32014-08-21 16:32:195868 req->Start();
[email protected]1826a402014-01-08 15:40:485869 base::RunLoop().Run();
5870
5871 EXPECT_EQ(1, d.received_before_network_start_count());
5872 EXPECT_EQ(0, d.response_started_count());
5873
[email protected]f7022f32014-08-21 16:32:195874 req->ResumeNetworkStart();
[email protected]1826a402014-01-08 15:40:485875 base::RunLoop().Run();
5876
5877 EXPECT_EQ(1, d.response_started_count());
5878 EXPECT_NE(0, d.bytes_received());
[email protected]f7022f32014-08-21 16:32:195879 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
[email protected]1826a402014-01-08 15:40:485880 }
5881}
5882
5883// Check that OnBeforeNetworkStart is only called once even if there is a
5884// redirect.
5885TEST_F(URLRequestTestHTTP, BeforeNetworkStartCalledOnce) {
5886 ASSERT_TRUE(test_server_.Start());
5887
5888 TestDelegate d;
5889 {
5890 d.set_quit_on_redirect(true);
5891 d.set_quit_on_network_start(true);
[email protected]f7022f32014-08-21 16:32:195892 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:365893 test_server_.GetURL("server-redirect?echo"), DEFAULT_PRIORITY, &d));
[email protected]1826a402014-01-08 15:40:485894
[email protected]f7022f32014-08-21 16:32:195895 req->Start();
[email protected]1826a402014-01-08 15:40:485896 base::RunLoop().Run();
5897
5898 EXPECT_EQ(1, d.received_before_network_start_count());
5899 EXPECT_EQ(0, d.response_started_count());
5900 EXPECT_EQ(0, d.received_redirect_count());
5901
[email protected]f7022f32014-08-21 16:32:195902 req->ResumeNetworkStart();
[email protected]1826a402014-01-08 15:40:485903 base::RunLoop().Run();
5904
5905 EXPECT_EQ(1, d.received_redirect_count());
[email protected]f7022f32014-08-21 16:32:195906 req->FollowDeferredRedirect();
[email protected]1826a402014-01-08 15:40:485907 base::RunLoop().Run();
5908
5909 // Check that the redirect's new network transaction does not get propagated
5910 // to a second OnBeforeNetworkStart() notification.
5911 EXPECT_EQ(1, d.received_before_network_start_count());
5912
5913 EXPECT_EQ(1, d.response_started_count());
5914 EXPECT_NE(0, d.bytes_received());
[email protected]f7022f32014-08-21 16:32:195915 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
[email protected]1826a402014-01-08 15:40:485916 }
5917}
5918
5919// Cancel the request after learning that the request would use the network.
5920TEST_F(URLRequestTestHTTP, CancelOnBeforeNetworkStart) {
5921 ASSERT_TRUE(test_server_.Start());
5922
5923 TestDelegate d;
5924 {
5925 d.set_quit_on_network_start(true);
5926 GURL test_url(test_server_.GetURL("echo"));
davidben151423e2015-03-23 18:48:365927 scoped_ptr<URLRequest> req(
5928 default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d));
[email protected]1826a402014-01-08 15:40:485929
[email protected]f7022f32014-08-21 16:32:195930 req->Start();
[email protected]1826a402014-01-08 15:40:485931 base::RunLoop().Run();
5932
5933 EXPECT_EQ(1, d.received_before_network_start_count());
5934 EXPECT_EQ(0, d.response_started_count());
5935
[email protected]f7022f32014-08-21 16:32:195936 req->Cancel();
[email protected]1826a402014-01-08 15:40:485937 base::RunLoop().Run();
5938
5939 EXPECT_EQ(1, d.response_started_count());
5940 EXPECT_EQ(0, d.bytes_received());
[email protected]f7022f32014-08-21 16:32:195941 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status());
[email protected]1826a402014-01-08 15:40:485942 }
5943}
5944
[email protected]316c1e5e2012-09-12 15:17:445945TEST_F(URLRequestTestHTTP, CancelRedirect) {
5946 ASSERT_TRUE(test_server_.Start());
5947
5948 TestDelegate d;
5949 {
5950 d.set_cancel_in_received_redirect(true);
[email protected]f7022f32014-08-21 16:32:195951 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:365952 test_server_.GetURL("files/redirect-test.html"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:195953 req->Start();
[email protected]255620da2013-08-19 13:14:295954 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445955
5956 EXPECT_EQ(1, d.response_started_count());
5957 EXPECT_EQ(0, d.bytes_received());
5958 EXPECT_FALSE(d.received_data_before_response());
[email protected]f7022f32014-08-21 16:32:195959 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status());
[email protected]316c1e5e2012-09-12 15:17:445960 }
5961}
5962
5963TEST_F(URLRequestTestHTTP, DeferredRedirect) {
5964 ASSERT_TRUE(test_server_.Start());
5965
5966 TestDelegate d;
5967 {
5968 d.set_quit_on_redirect(true);
[email protected]79e1fd62013-06-20 06:50:045969 GURL test_url(test_server_.GetURL("files/redirect-test.html"));
davidben151423e2015-03-23 18:48:365970 scoped_ptr<URLRequest> req(
5971 default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d));
[email protected]79e1fd62013-06-20 06:50:045972
[email protected]f7022f32014-08-21 16:32:195973 req->Start();
[email protected]255620da2013-08-19 13:14:295974 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445975
5976 EXPECT_EQ(1, d.received_redirect_count());
5977
[email protected]f7022f32014-08-21 16:32:195978 req->FollowDeferredRedirect();
[email protected]255620da2013-08-19 13:14:295979 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445980
5981 EXPECT_EQ(1, d.response_started_count());
5982 EXPECT_FALSE(d.received_data_before_response());
[email protected]f7022f32014-08-21 16:32:195983 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
[email protected]316c1e5e2012-09-12 15:17:445984
[email protected]6cdfd7f2013-02-08 20:40:155985 base::FilePath path;
[email protected]316c1e5e2012-09-12 15:17:445986 PathService::Get(base::DIR_SOURCE_ROOT, &path);
5987 path = path.Append(FILE_PATH_LITERAL("net"));
5988 path = path.Append(FILE_PATH_LITERAL("data"));
5989 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
5990 path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
5991
5992 std::string contents;
[email protected]82f84b92013-08-30 18:23:505993 EXPECT_TRUE(base::ReadFileToString(path, &contents));
[email protected]316c1e5e2012-09-12 15:17:445994 EXPECT_EQ(contents, d.data_received());
5995 }
5996}
5997
[email protected]79e1fd62013-06-20 06:50:045998TEST_F(URLRequestTestHTTP, DeferredRedirect_GetFullRequestHeaders) {
5999 ASSERT_TRUE(test_server_.Start());
6000
6001 TestDelegate d;
6002 {
6003 d.set_quit_on_redirect(true);
6004 GURL test_url(test_server_.GetURL("files/redirect-test.html"));
davidben151423e2015-03-23 18:48:366005 scoped_ptr<URLRequest> req(
6006 default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d));
[email protected]79e1fd62013-06-20 06:50:046007
6008 EXPECT_FALSE(d.have_full_request_headers());
6009
[email protected]f7022f32014-08-21 16:32:196010 req->Start();
[email protected]255620da2013-08-19 13:14:296011 base::RunLoop().Run();
[email protected]79e1fd62013-06-20 06:50:046012
6013 EXPECT_EQ(1, d.received_redirect_count());
6014 EXPECT_TRUE(d.have_full_request_headers());
6015 CheckFullRequestHeaders(d.full_request_headers(), test_url);
6016 d.ClearFullRequestHeaders();
6017
[email protected]f7022f32014-08-21 16:32:196018 req->FollowDeferredRedirect();
[email protected]255620da2013-08-19 13:14:296019 base::RunLoop().Run();
[email protected]79e1fd62013-06-20 06:50:046020
6021 GURL target_url(test_server_.GetURL("files/with-headers.html"));
6022 EXPECT_EQ(1, d.response_started_count());
6023 EXPECT_TRUE(d.have_full_request_headers());
6024 CheckFullRequestHeaders(d.full_request_headers(), target_url);
6025 EXPECT_FALSE(d.received_data_before_response());
[email protected]f7022f32014-08-21 16:32:196026 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
[email protected]79e1fd62013-06-20 06:50:046027
6028 base::FilePath path;
6029 PathService::Get(base::DIR_SOURCE_ROOT, &path);
6030 path = path.Append(FILE_PATH_LITERAL("net"));
6031 path = path.Append(FILE_PATH_LITERAL("data"));
6032 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
6033 path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
6034
6035 std::string contents;
[email protected]82f84b92013-08-30 18:23:506036 EXPECT_TRUE(base::ReadFileToString(path, &contents));
[email protected]79e1fd62013-06-20 06:50:046037 EXPECT_EQ(contents, d.data_received());
6038 }
6039}
6040
[email protected]316c1e5e2012-09-12 15:17:446041TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) {
6042 ASSERT_TRUE(test_server_.Start());
6043
6044 TestDelegate d;
6045 {
6046 d.set_quit_on_redirect(true);
[email protected]f7022f32014-08-21 16:32:196047 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:366048 test_server_.GetURL("files/redirect-test.html"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:196049 req->Start();
[email protected]255620da2013-08-19 13:14:296050 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446051
6052 EXPECT_EQ(1, d.received_redirect_count());
6053
[email protected]f7022f32014-08-21 16:32:196054 req->Cancel();
[email protected]255620da2013-08-19 13:14:296055 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446056
6057 EXPECT_EQ(1, d.response_started_count());
6058 EXPECT_EQ(0, d.bytes_received());
6059 EXPECT_FALSE(d.received_data_before_response());
[email protected]f7022f32014-08-21 16:32:196060 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status());
[email protected]316c1e5e2012-09-12 15:17:446061 }
6062}
6063
6064TEST_F(URLRequestTestHTTP, VaryHeader) {
6065 ASSERT_TRUE(test_server_.Start());
6066
[email protected]3b23a222013-05-15 21:33:256067 // Populate the cache.
[email protected]316c1e5e2012-09-12 15:17:446068 {
6069 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196070 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:366071 test_server_.GetURL("echoheadercache?foo"), DEFAULT_PRIORITY, &d));
[email protected]316c1e5e2012-09-12 15:17:446072 HttpRequestHeaders headers;
6073 headers.SetHeader("foo", "1");
[email protected]f7022f32014-08-21 16:32:196074 req->SetExtraRequestHeaders(headers);
6075 req->Start();
[email protected]255620da2013-08-19 13:14:296076 base::RunLoop().Run();
[email protected]3b23a222013-05-15 21:33:256077
6078 LoadTimingInfo load_timing_info;
[email protected]f7022f32014-08-21 16:32:196079 req->GetLoadTimingInfo(&load_timing_info);
[email protected]3b23a222013-05-15 21:33:256080 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
[email protected]316c1e5e2012-09-12 15:17:446081 }
6082
[email protected]3b23a222013-05-15 21:33:256083 // Expect a cache hit.
[email protected]316c1e5e2012-09-12 15:17:446084 {
6085 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196086 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:366087 test_server_.GetURL("echoheadercache?foo"), DEFAULT_PRIORITY, &d));
[email protected]316c1e5e2012-09-12 15:17:446088 HttpRequestHeaders headers;
6089 headers.SetHeader("foo", "1");
[email protected]f7022f32014-08-21 16:32:196090 req->SetExtraRequestHeaders(headers);
6091 req->Start();
[email protected]255620da2013-08-19 13:14:296092 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446093
[email protected]f7022f32014-08-21 16:32:196094 EXPECT_TRUE(req->was_cached());
[email protected]3b23a222013-05-15 21:33:256095
6096 LoadTimingInfo load_timing_info;
[email protected]f7022f32014-08-21 16:32:196097 req->GetLoadTimingInfo(&load_timing_info);
[email protected]3b23a222013-05-15 21:33:256098 TestLoadTimingCacheHitNoNetwork(load_timing_info);
[email protected]316c1e5e2012-09-12 15:17:446099 }
6100
[email protected]3b23a222013-05-15 21:33:256101 // Expect a cache miss.
[email protected]316c1e5e2012-09-12 15:17:446102 {
6103 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196104 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:366105 test_server_.GetURL("echoheadercache?foo"), DEFAULT_PRIORITY, &d));
[email protected]316c1e5e2012-09-12 15:17:446106 HttpRequestHeaders headers;
6107 headers.SetHeader("foo", "2");
[email protected]f7022f32014-08-21 16:32:196108 req->SetExtraRequestHeaders(headers);
6109 req->Start();
[email protected]255620da2013-08-19 13:14:296110 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446111
[email protected]f7022f32014-08-21 16:32:196112 EXPECT_FALSE(req->was_cached());
[email protected]3b23a222013-05-15 21:33:256113
6114 LoadTimingInfo load_timing_info;
[email protected]f7022f32014-08-21 16:32:196115 req->GetLoadTimingInfo(&load_timing_info);
[email protected]3b23a222013-05-15 21:33:256116 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
[email protected]316c1e5e2012-09-12 15:17:446117 }
6118}
6119
6120TEST_F(URLRequestTestHTTP, BasicAuth) {
6121 ASSERT_TRUE(test_server_.Start());
6122
6123 // populate the cache
6124 {
6125 TestDelegate d;
6126 d.set_credentials(AuthCredentials(kUser, kSecret));
6127
[email protected]f7022f32014-08-21 16:32:196128 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:366129 test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:196130 r->Start();
[email protected]316c1e5e2012-09-12 15:17:446131
[email protected]255620da2013-08-19 13:14:296132 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446133
6134 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
6135 }
6136
6137 // repeat request with end-to-end validation. since auth-basic results in a
6138 // cachable page, we expect this test to result in a 304. in which case, the
6139 // response should be fetched from the cache.
6140 {
6141 TestDelegate d;
6142 d.set_credentials(AuthCredentials(kUser, kSecret));
6143
[email protected]f7022f32014-08-21 16:32:196144 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:366145 test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:196146 r->SetLoadFlags(LOAD_VALIDATE_CACHE);
6147 r->Start();
[email protected]316c1e5e2012-09-12 15:17:446148
[email protected]255620da2013-08-19 13:14:296149 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446150
6151 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
6152
6153 // Should be the same cached document.
[email protected]f7022f32014-08-21 16:32:196154 EXPECT_TRUE(r->was_cached());
[email protected]316c1e5e2012-09-12 15:17:446155 }
6156}
6157
6158// Check that Set-Cookie headers in 401 responses are respected.
6159// http://crbug.com/6450
6160TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) {
6161 ASSERT_TRUE(test_server_.Start());
6162
6163 GURL url_requiring_auth =
6164 test_server_.GetURL("auth-basic?set-cookie-if-challenged");
6165
6166 // Request a page that will give a 401 containing a Set-Cookie header.
6167 // Verify that when the transaction is restarted, it includes the new cookie.
6168 {
[email protected]ceefd7fd2012-11-29 00:36:246169 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]316c1e5e2012-09-12 15:17:446170 TestURLRequestContext context(true);
6171 context.set_network_delegate(&network_delegate);
6172 context.Init();
6173
6174 TestDelegate d;
6175 d.set_credentials(AuthCredentials(kUser, kSecret));
6176
davidben151423e2015-03-23 18:48:366177 scoped_ptr<URLRequest> r(
6178 context.CreateRequest(url_requiring_auth, DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:196179 r->Start();
[email protected]316c1e5e2012-09-12 15:17:446180
[email protected]255620da2013-08-19 13:14:296181 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446182
6183 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
6184
6185 // Make sure we sent the cookie in the restarted transaction.
6186 EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true")
6187 != std::string::npos);
6188 }
6189
6190 // Same test as above, except this time the restart is initiated earlier
6191 // (without user intervention since identity is embedded in the URL).
6192 {
[email protected]ceefd7fd2012-11-29 00:36:246193 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]316c1e5e2012-09-12 15:17:446194 TestURLRequestContext context(true);
6195 context.set_network_delegate(&network_delegate);
6196 context.Init();
6197
6198 TestDelegate d;
6199
6200 GURL::Replacements replacements;
mgiuca77752c32015-02-05 07:31:186201 replacements.SetUsernameStr("user2");
6202 replacements.SetPasswordStr("secret");
[email protected]316c1e5e2012-09-12 15:17:446203 GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements);
6204
davidben151423e2015-03-23 18:48:366205 scoped_ptr<URLRequest> r(
6206 context.CreateRequest(url_with_identity, DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:196207 r->Start();
[email protected]316c1e5e2012-09-12 15:17:446208
[email protected]255620da2013-08-19 13:14:296209 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446210
6211 EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos);
6212
6213 // Make sure we sent the cookie in the restarted transaction.
6214 EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true")
6215 != std::string::npos);
6216 }
6217}
6218
[email protected]58e32bb2013-01-21 18:23:256219// Tests that load timing works as expected with auth and the cache.
6220TEST_F(URLRequestTestHTTP, BasicAuthLoadTiming) {
6221 ASSERT_TRUE(test_server_.Start());
6222
6223 // populate the cache
6224 {
6225 TestDelegate d;
6226 d.set_credentials(AuthCredentials(kUser, kSecret));
6227
[email protected]f7022f32014-08-21 16:32:196228 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:366229 test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:196230 r->Start();
[email protected]58e32bb2013-01-21 18:23:256231
[email protected]255620da2013-08-19 13:14:296232 base::RunLoop().Run();
[email protected]58e32bb2013-01-21 18:23:256233
6234 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
6235
6236 LoadTimingInfo load_timing_info_before_auth;
6237 EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeAuth(
6238 &load_timing_info_before_auth));
6239 TestLoadTimingNotReused(load_timing_info_before_auth,
6240 CONNECT_TIMING_HAS_DNS_TIMES);
6241
6242 LoadTimingInfo load_timing_info;
[email protected]f7022f32014-08-21 16:32:196243 r->GetLoadTimingInfo(&load_timing_info);
[email protected]58e32bb2013-01-21 18:23:256244 // The test server does not support keep alive sockets, so the second
6245 // request with auth should use a new socket.
6246 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
6247 EXPECT_NE(load_timing_info_before_auth.socket_log_id,
6248 load_timing_info.socket_log_id);
6249 EXPECT_LE(load_timing_info_before_auth.receive_headers_end,
6250 load_timing_info.connect_timing.connect_start);
6251 }
6252
[email protected]3b23a222013-05-15 21:33:256253 // Repeat request with end-to-end validation. Since auth-basic results in a
6254 // cachable page, we expect this test to result in a 304. In which case, the
[email protected]58e32bb2013-01-21 18:23:256255 // response should be fetched from the cache.
6256 {
6257 TestDelegate d;
6258 d.set_credentials(AuthCredentials(kUser, kSecret));
6259
[email protected]f7022f32014-08-21 16:32:196260 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:366261 test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:196262 r->SetLoadFlags(LOAD_VALIDATE_CACHE);
6263 r->Start();
[email protected]58e32bb2013-01-21 18:23:256264
[email protected]255620da2013-08-19 13:14:296265 base::RunLoop().Run();
[email protected]58e32bb2013-01-21 18:23:256266
6267 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
6268
6269 // Should be the same cached document.
[email protected]f7022f32014-08-21 16:32:196270 EXPECT_TRUE(r->was_cached());
[email protected]58e32bb2013-01-21 18:23:256271
[email protected]3b23a222013-05-15 21:33:256272 // Since there was a request that went over the wire, the load timing
6273 // information should include connection times.
[email protected]58e32bb2013-01-21 18:23:256274 LoadTimingInfo load_timing_info;
[email protected]f7022f32014-08-21 16:32:196275 r->GetLoadTimingInfo(&load_timing_info);
[email protected]3b23a222013-05-15 21:33:256276 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
[email protected]58e32bb2013-01-21 18:23:256277 }
6278}
6279
[email protected]316c1e5e2012-09-12 15:17:446280// In this test, we do a POST which the server will 302 redirect.
6281// The subsequent transaction should use GET, and should not send the
6282// Content-Type header.
6283// http://code.google.com/p/chromium/issues/detail?id=843
6284TEST_F(URLRequestTestHTTP, Post302RedirectGet) {
6285 ASSERT_TRUE(test_server_.Start());
6286
6287 const char kData[] = "hello world";
6288
6289 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196290 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:366291 test_server_.GetURL("files/redirect-to-echoall"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:196292 req->set_method("POST");
mmenkecbc2b712014-10-09 20:29:076293 req->set_upload(CreateSimpleUploadData(kData));
[email protected]316c1e5e2012-09-12 15:17:446294
6295 // Set headers (some of which are specific to the POST).
6296 HttpRequestHeaders headers;
6297 headers.AddHeadersFromString(
6298 "Content-Type: multipart/form-data; "
6299 "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n"
6300 "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,"
6301 "text/plain;q=0.8,image/png,*/*;q=0.5\r\n"
6302 "Accept-Language: en-US,en\r\n"
6303 "Accept-Charset: ISO-8859-1,*,utf-8\r\n"
6304 "Content-Length: 11\r\n"
6305 "Origin: http://localhost:1337/");
[email protected]f7022f32014-08-21 16:32:196306 req->SetExtraRequestHeaders(headers);
6307 req->Start();
[email protected]255620da2013-08-19 13:14:296308 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446309
6310 std::string mime_type;
[email protected]f7022f32014-08-21 16:32:196311 req->GetMimeType(&mime_type);
[email protected]316c1e5e2012-09-12 15:17:446312 EXPECT_EQ("text/html", mime_type);
6313
6314 const std::string& data = d.data_received();
6315
6316 // Check that the post-specific headers were stripped:
6317 EXPECT_FALSE(ContainsString(data, "Content-Length:"));
6318 EXPECT_FALSE(ContainsString(data, "Content-Type:"));
6319 EXPECT_FALSE(ContainsString(data, "Origin:"));
6320
6321 // These extra request headers should not have been stripped.
6322 EXPECT_TRUE(ContainsString(data, "Accept:"));
6323 EXPECT_TRUE(ContainsString(data, "Accept-Language:"));
6324 EXPECT_TRUE(ContainsString(data, "Accept-Charset:"));
6325}
6326
jww5fe460ff2015-03-28 00:22:516327// The following tests check that we handle mutating the request for HTTP
6328// redirects as expected.
6329// See https://crbug.com/56373, https://crbug.com/102130, and
6330// https://crbug.com/465517.
[email protected]316c1e5e2012-09-12 15:17:446331
6332TEST_F(URLRequestTestHTTP, Redirect301Tests) {
6333 ASSERT_TRUE(test_server_.Start());
6334
6335 const GURL url = test_server_.GetURL("files/redirect301-to-echo");
jww5fe460ff2015-03-28 00:22:516336 const GURL https_redirect_url =
6337 test_server_.GetURL("files/redirect301-to-https");
[email protected]316c1e5e2012-09-12 15:17:446338
6339 HTTPRedirectMethodTest(url, "POST", "GET", true);
6340 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
6341 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
jww5fe460ff2015-03-28 00:22:516342
6343 HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec());
6344 HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null");
6345 HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string());
6346 HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET",
6347 std::string());
[email protected]316c1e5e2012-09-12 15:17:446348}
6349
6350TEST_F(URLRequestTestHTTP, Redirect302Tests) {
6351 ASSERT_TRUE(test_server_.Start());
6352
6353 const GURL url = test_server_.GetURL("files/redirect302-to-echo");
jww5fe460ff2015-03-28 00:22:516354 const GURL https_redirect_url =
6355 test_server_.GetURL("files/redirect302-to-https");
[email protected]316c1e5e2012-09-12 15:17:446356
6357 HTTPRedirectMethodTest(url, "POST", "GET", true);
6358 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
6359 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
jww5fe460ff2015-03-28 00:22:516360
6361 HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec());
6362 HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null");
6363 HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string());
6364 HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET",
6365 std::string());
[email protected]316c1e5e2012-09-12 15:17:446366}
6367
6368TEST_F(URLRequestTestHTTP, Redirect303Tests) {
6369 ASSERT_TRUE(test_server_.Start());
6370
6371 const GURL url = test_server_.GetURL("files/redirect303-to-echo");
jww5fe460ff2015-03-28 00:22:516372 const GURL https_redirect_url =
6373 test_server_.GetURL("files/redirect303-to-https");
[email protected]316c1e5e2012-09-12 15:17:446374
6375 HTTPRedirectMethodTest(url, "POST", "GET", true);
6376 HTTPRedirectMethodTest(url, "PUT", "GET", true);
6377 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
jww5fe460ff2015-03-28 00:22:516378
6379 HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec());
6380 HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null");
6381 HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string());
6382 HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET",
6383 std::string());
[email protected]316c1e5e2012-09-12 15:17:446384}
6385
6386TEST_F(URLRequestTestHTTP, Redirect307Tests) {
6387 ASSERT_TRUE(test_server_.Start());
6388
6389 const GURL url = test_server_.GetURL("files/redirect307-to-echo");
jww5fe460ff2015-03-28 00:22:516390 const GURL https_redirect_url =
6391 test_server_.GetURL("files/redirect307-to-https");
[email protected]316c1e5e2012-09-12 15:17:446392
6393 HTTPRedirectMethodTest(url, "POST", "POST", true);
6394 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
6395 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
jww5fe460ff2015-03-28 00:22:516396
6397 HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec());
6398 HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null");
6399 HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec());
6400 HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null");
[email protected]316c1e5e2012-09-12 15:17:446401}
6402
[email protected]0a17aab32014-04-24 03:32:376403TEST_F(URLRequestTestHTTP, Redirect308Tests) {
6404 ASSERT_TRUE(test_server_.Start());
6405
6406 const GURL url = test_server_.GetURL("files/redirect308-to-echo");
jww5fe460ff2015-03-28 00:22:516407 const GURL https_redirect_url =
6408 test_server_.GetURL("files/redirect308-to-https");
[email protected]0a17aab32014-04-24 03:32:376409
6410 HTTPRedirectMethodTest(url, "POST", "POST", true);
6411 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
6412 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
jww5fe460ff2015-03-28 00:22:516413
6414 HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec());
6415 HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null");
6416 HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec());
6417 HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null");
[email protected]0a17aab32014-04-24 03:32:376418}
6419
6420// Make sure that 308 responses without bodies are not treated as redirects.
6421// Certain legacy apis that pre-date the response code expect this behavior
6422// (Like Google Drive).
6423TEST_F(URLRequestTestHTTP, NoRedirectOn308WithoutLocationHeader) {
6424 ASSERT_TRUE(test_server_.Start());
6425
6426 TestDelegate d;
6427 const GURL url = test_server_.GetURL("files/308-without-location-header");
6428
davidben151423e2015-03-23 18:48:366429 scoped_ptr<URLRequest> request(
6430 default_context_.CreateRequest(url, DEFAULT_PRIORITY, &d));
[email protected]0a17aab32014-04-24 03:32:376431
[email protected]f7022f32014-08-21 16:32:196432 request->Start();
[email protected]0a17aab32014-04-24 03:32:376433 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:196434 EXPECT_EQ(URLRequestStatus::SUCCESS, request->status().status());
6435 EXPECT_EQ(OK, request->status().error());
[email protected]0a17aab32014-04-24 03:32:376436 EXPECT_EQ(0, d.received_redirect_count());
[email protected]f7022f32014-08-21 16:32:196437 EXPECT_EQ(308, request->response_headers()->response_code());
[email protected]0a17aab32014-04-24 03:32:376438 EXPECT_EQ("This is not a redirect.", d.data_received());
6439}
6440
[email protected]f878230e2014-04-03 15:36:146441TEST_F(URLRequestTestHTTP, Redirect302PreserveReferenceFragment) {
6442 ASSERT_TRUE(test_server_.Start());
6443
6444 GURL original_url(test_server_.GetURL("files/redirect302-to-echo#fragment"));
6445 GURL expected_url(test_server_.GetURL("echo#fragment"));
6446
6447 TestDelegate d;
6448 {
davidben151423e2015-03-23 18:48:366449 scoped_ptr<URLRequest> r(
6450 default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d));
[email protected]f878230e2014-04-03 15:36:146451
[email protected]f7022f32014-08-21 16:32:196452 r->Start();
[email protected]f878230e2014-04-03 15:36:146453 base::RunLoop().Run();
6454
[email protected]f7022f32014-08-21 16:32:196455 EXPECT_EQ(2U, r->url_chain().size());
6456 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
6457 EXPECT_EQ(OK, r->status().error());
6458 EXPECT_EQ(original_url, r->original_url());
6459 EXPECT_EQ(expected_url, r->url());
[email protected]f878230e2014-04-03 15:36:146460 }
6461}
6462
[email protected]cba24642014-08-15 20:49:596463TEST_F(URLRequestTestHTTP, RedirectPreserveFirstPartyURL) {
6464 ASSERT_TRUE(test_server_.Start());
6465
6466 GURL url(test_server_.GetURL("files/redirect302-to-echo"));
6467 GURL first_party_url("http://example.com");
6468
6469 TestDelegate d;
6470 {
davidben151423e2015-03-23 18:48:366471 scoped_ptr<URLRequest> r(
6472 default_context_.CreateRequest(url, DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:196473 r->set_first_party_for_cookies(first_party_url);
[email protected]cba24642014-08-15 20:49:596474
[email protected]f7022f32014-08-21 16:32:196475 r->Start();
[email protected]cba24642014-08-15 20:49:596476 base::RunLoop().Run();
6477
[email protected]f7022f32014-08-21 16:32:196478 EXPECT_EQ(2U, r->url_chain().size());
6479 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
6480 EXPECT_EQ(OK, r->status().error());
6481 EXPECT_EQ(first_party_url, r->first_party_for_cookies());
[email protected]cba24642014-08-15 20:49:596482 }
6483}
6484
6485TEST_F(URLRequestTestHTTP, RedirectUpdateFirstPartyURL) {
6486 ASSERT_TRUE(test_server_.Start());
6487
6488 GURL url(test_server_.GetURL("files/redirect302-to-echo"));
6489 GURL original_first_party_url("http://example.com");
6490 GURL expected_first_party_url(test_server_.GetURL("echo"));
6491
6492 TestDelegate d;
6493 {
davidben151423e2015-03-23 18:48:366494 scoped_ptr<URLRequest> r(
6495 default_context_.CreateRequest(url, DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:196496 r->set_first_party_for_cookies(original_first_party_url);
6497 r->set_first_party_url_policy(
[email protected]cba24642014-08-15 20:49:596498 URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT);
6499
[email protected]f7022f32014-08-21 16:32:196500 r->Start();
[email protected]cba24642014-08-15 20:49:596501 base::RunLoop().Run();
6502
[email protected]f7022f32014-08-21 16:32:196503 EXPECT_EQ(2U, r->url_chain().size());
6504 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
6505 EXPECT_EQ(OK, r->status().error());
6506 EXPECT_EQ(expected_first_party_url, r->first_party_for_cookies());
[email protected]cba24642014-08-15 20:49:596507 }
6508}
6509
[email protected]316c1e5e2012-09-12 15:17:446510TEST_F(URLRequestTestHTTP, InterceptPost302RedirectGet) {
6511 ASSERT_TRUE(test_server_.Start());
6512
6513 const char kData[] = "hello world";
6514
6515 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196516 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:366517 test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:196518 req->set_method("POST");
mmenkecbc2b712014-10-09 20:29:076519 req->set_upload(CreateSimpleUploadData(kData));
[email protected]316c1e5e2012-09-12 15:17:446520 HttpRequestHeaders headers;
6521 headers.SetHeader(HttpRequestHeaders::kContentLength,
6522 base::UintToString(arraysize(kData) - 1));
[email protected]f7022f32014-08-21 16:32:196523 req->SetExtraRequestHeaders(headers);
[email protected]316c1e5e2012-09-12 15:17:446524
6525 URLRequestRedirectJob* job = new URLRequestRedirectJob(
[email protected]f7022f32014-08-21 16:32:196526 req.get(), &default_network_delegate_, test_server_.GetURL("echo"),
[email protected]7983c4a2014-03-12 01:47:096527 URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason");
[email protected]316c1e5e2012-09-12 15:17:446528 AddTestInterceptor()->set_main_intercept_job(job);
6529
[email protected]f7022f32014-08-21 16:32:196530 req->Start();
[email protected]255620da2013-08-19 13:14:296531 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:196532 EXPECT_EQ("GET", req->method());
[email protected]316c1e5e2012-09-12 15:17:446533}
6534
6535TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) {
6536 ASSERT_TRUE(test_server_.Start());
6537
6538 const char kData[] = "hello world";
6539
6540 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196541 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:366542 test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:196543 req->set_method("POST");
mmenkecbc2b712014-10-09 20:29:076544 req->set_upload(CreateSimpleUploadData(kData));
[email protected]316c1e5e2012-09-12 15:17:446545 HttpRequestHeaders headers;
6546 headers.SetHeader(HttpRequestHeaders::kContentLength,
6547 base::UintToString(arraysize(kData) - 1));
[email protected]f7022f32014-08-21 16:32:196548 req->SetExtraRequestHeaders(headers);
[email protected]316c1e5e2012-09-12 15:17:446549
6550 URLRequestRedirectJob* job = new URLRequestRedirectJob(
[email protected]f7022f32014-08-21 16:32:196551 req.get(), &default_network_delegate_, test_server_.GetURL("echo"),
[email protected]7983c4a2014-03-12 01:47:096552 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT,
6553 "Very Good Reason");
[email protected]316c1e5e2012-09-12 15:17:446554 AddTestInterceptor()->set_main_intercept_job(job);
6555
[email protected]f7022f32014-08-21 16:32:196556 req->Start();
[email protected]255620da2013-08-19 13:14:296557 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:196558 EXPECT_EQ("POST", req->method());
[email protected]316c1e5e2012-09-12 15:17:446559 EXPECT_EQ(kData, d.data_received());
6560}
6561
6562// Check that default A-L header is sent.
6563TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) {
6564 ASSERT_TRUE(test_server_.Start());
6565
[email protected]8790210c2013-12-02 05:29:536566 StaticHttpUserAgentSettings settings("en", std::string());
[email protected]ceefd7fd2012-11-29 00:36:246567 TestNetworkDelegate network_delegate; // Must outlive URLRequests.
[email protected]316c1e5e2012-09-12 15:17:446568 TestURLRequestContext context(true);
6569 context.set_network_delegate(&network_delegate);
[email protected]ee4c30d2012-11-07 15:08:436570 context.set_http_user_agent_settings(&settings);
[email protected]316c1e5e2012-09-12 15:17:446571 context.Init();
6572
6573 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196574 scoped_ptr<URLRequest> req(context.CreateRequest(
davidben151423e2015-03-23 18:48:366575 test_server_.GetURL("echoheader?Accept-Language"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:196576 req->Start();
[email protected]255620da2013-08-19 13:14:296577 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446578 EXPECT_EQ("en", d.data_received());
6579}
6580
6581// Check that an empty A-L header is not sent. http://crbug.com/77365.
6582TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) {
6583 ASSERT_TRUE(test_server_.Start());
6584
[email protected]8790210c2013-12-02 05:29:536585 std::string empty_string; // Avoid most vexing parse on line below.
6586 StaticHttpUserAgentSettings settings(empty_string, empty_string);
[email protected]ceefd7fd2012-11-29 00:36:246587 TestNetworkDelegate network_delegate; // Must outlive URLRequests.
[email protected]316c1e5e2012-09-12 15:17:446588 TestURLRequestContext context(true);
6589 context.set_network_delegate(&network_delegate);
6590 context.Init();
6591 // We override the language after initialization because empty entries
6592 // get overridden by Init().
[email protected]ee4c30d2012-11-07 15:08:436593 context.set_http_user_agent_settings(&settings);
[email protected]316c1e5e2012-09-12 15:17:446594
6595 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196596 scoped_ptr<URLRequest> req(context.CreateRequest(
davidben151423e2015-03-23 18:48:366597 test_server_.GetURL("echoheader?Accept-Language"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:196598 req->Start();
[email protected]255620da2013-08-19 13:14:296599 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446600 EXPECT_EQ("None", d.data_received());
6601}
6602
6603// Check that if request overrides the A-L header, the default is not appended.
6604// See http://crbug.com/20894
6605TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) {
6606 ASSERT_TRUE(test_server_.Start());
6607
6608 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196609 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:366610 test_server_.GetURL("echoheader?Accept-Language"), DEFAULT_PRIORITY, &d));
[email protected]316c1e5e2012-09-12 15:17:446611 HttpRequestHeaders headers;
6612 headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru");
[email protected]f7022f32014-08-21 16:32:196613 req->SetExtraRequestHeaders(headers);
6614 req->Start();
[email protected]255620da2013-08-19 13:14:296615 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446616 EXPECT_EQ(std::string("ru"), d.data_received());
6617}
6618
6619// Check that default A-E header is sent.
6620TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) {
6621 ASSERT_TRUE(test_server_.Start());
6622
6623 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196624 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:366625 test_server_.GetURL("echoheader?Accept-Encoding"), DEFAULT_PRIORITY, &d));
[email protected]316c1e5e2012-09-12 15:17:446626 HttpRequestHeaders headers;
[email protected]f7022f32014-08-21 16:32:196627 req->SetExtraRequestHeaders(headers);
6628 req->Start();
[email protected]255620da2013-08-19 13:14:296629 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446630 EXPECT_TRUE(ContainsString(d.data_received(), "gzip"));
6631}
6632
6633// Check that if request overrides the A-E header, the default is not appended.
6634// See http://crbug.com/47381
6635TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) {
6636 ASSERT_TRUE(test_server_.Start());
6637
6638 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196639 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:366640 test_server_.GetURL("echoheader?Accept-Encoding"), DEFAULT_PRIORITY, &d));
[email protected]316c1e5e2012-09-12 15:17:446641 HttpRequestHeaders headers;
6642 headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, "identity");
[email protected]f7022f32014-08-21 16:32:196643 req->SetExtraRequestHeaders(headers);
6644 req->Start();
[email protected]255620da2013-08-19 13:14:296645 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446646 EXPECT_FALSE(ContainsString(d.data_received(), "gzip"));
6647 EXPECT_TRUE(ContainsString(d.data_received(), "identity"));
6648}
6649
[email protected]84f05432013-03-15 01:00:126650// Check that setting the A-C header sends the proper header.
6651TEST_F(URLRequestTestHTTP, SetAcceptCharset) {
[email protected]316c1e5e2012-09-12 15:17:446652 ASSERT_TRUE(test_server_.Start());
6653
6654 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196655 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:366656 test_server_.GetURL("echoheader?Accept-Charset"), DEFAULT_PRIORITY, &d));
[email protected]316c1e5e2012-09-12 15:17:446657 HttpRequestHeaders headers;
6658 headers.SetHeader(HttpRequestHeaders::kAcceptCharset, "koi-8r");
[email protected]f7022f32014-08-21 16:32:196659 req->SetExtraRequestHeaders(headers);
6660 req->Start();
[email protected]255620da2013-08-19 13:14:296661 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446662 EXPECT_EQ(std::string("koi-8r"), d.data_received());
6663}
6664
6665// Check that default User-Agent header is sent.
6666TEST_F(URLRequestTestHTTP, DefaultUserAgent) {
6667 ASSERT_TRUE(test_server_.Start());
6668
6669 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196670 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:366671 test_server_.GetURL("echoheader?User-Agent"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:196672 req->Start();
[email protected]255620da2013-08-19 13:14:296673 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:196674 EXPECT_EQ(default_context_.http_user_agent_settings()->GetUserAgent(),
[email protected]051a4b32014-01-09 04:02:376675 d.data_received());
[email protected]316c1e5e2012-09-12 15:17:446676}
6677
6678// Check that if request overrides the User-Agent header,
6679// the default is not appended.
6680TEST_F(URLRequestTestHTTP, OverrideUserAgent) {
6681 ASSERT_TRUE(test_server_.Start());
6682
6683 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196684 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:366685 test_server_.GetURL("echoheader?User-Agent"), DEFAULT_PRIORITY, &d));
[email protected]316c1e5e2012-09-12 15:17:446686 HttpRequestHeaders headers;
6687 headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)");
[email protected]f7022f32014-08-21 16:32:196688 req->SetExtraRequestHeaders(headers);
6689 req->Start();
[email protected]255620da2013-08-19 13:14:296690 base::RunLoop().Run();
[email protected]cd6f2522014-01-16 18:27:356691 EXPECT_EQ(std::string("Lynx (textmode)"), d.data_received());
[email protected]316c1e5e2012-09-12 15:17:446692}
6693
[email protected]ee4c30d2012-11-07 15:08:436694// Check that a NULL HttpUserAgentSettings causes the corresponding empty
6695// User-Agent header to be sent but does not send the Accept-Language and
6696// Accept-Charset headers.
6697TEST_F(URLRequestTestHTTP, EmptyHttpUserAgentSettings) {
6698 ASSERT_TRUE(test_server_.Start());
6699
[email protected]ceefd7fd2012-11-29 00:36:246700 TestNetworkDelegate network_delegate; // Must outlive URLRequests.
[email protected]ee4c30d2012-11-07 15:08:436701 TestURLRequestContext context(true);
6702 context.set_network_delegate(&network_delegate);
6703 context.Init();
6704 // We override the HttpUserAgentSettings after initialization because empty
6705 // entries get overridden by Init().
6706 context.set_http_user_agent_settings(NULL);
6707
6708 struct {
6709 const char* request;
6710 const char* expected_response;
6711 } tests[] = { { "echoheader?Accept-Language", "None" },
6712 { "echoheader?Accept-Charset", "None" },
6713 { "echoheader?User-Agent", "" } };
6714
viettrungluue4a8b882014-10-16 06:17:386715 for (size_t i = 0; i < arraysize(tests); i++) {
[email protected]ee4c30d2012-11-07 15:08:436716 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196717 scoped_ptr<URLRequest> req(context.CreateRequest(
davidben151423e2015-03-23 18:48:366718 test_server_.GetURL(tests[i].request), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:196719 req->Start();
[email protected]255620da2013-08-19 13:14:296720 base::RunLoop().Run();
[email protected]ee4c30d2012-11-07 15:08:436721 EXPECT_EQ(tests[i].expected_response, d.data_received())
6722 << " Request = \"" << tests[i].request << "\"";
6723 }
6724}
6725
[email protected]5033ab82013-03-22 20:17:466726// Make sure that URLRequest passes on its priority updates to
6727// newly-created jobs after the first one.
6728TEST_F(URLRequestTestHTTP, SetSubsequentJobPriority) {
6729 ASSERT_TRUE(test_server_.Start());
6730
6731 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196732 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:366733 test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:196734 EXPECT_EQ(DEFAULT_PRIORITY, req->priority());
[email protected]5033ab82013-03-22 20:17:466735
6736 scoped_refptr<URLRequestRedirectJob> redirect_job =
6737 new URLRequestRedirectJob(
[email protected]f7022f32014-08-21 16:32:196738 req.get(), &default_network_delegate_, test_server_.GetURL("echo"),
[email protected]7983c4a2014-03-12 01:47:096739 URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason");
[email protected]90499482013-06-01 00:39:506740 AddTestInterceptor()->set_main_intercept_job(redirect_job.get());
[email protected]5033ab82013-03-22 20:17:466741
[email protected]f7022f32014-08-21 16:32:196742 req->SetPriority(LOW);
6743 req->Start();
6744 EXPECT_TRUE(req->is_pending());
[email protected]5033ab82013-03-22 20:17:466745
6746 scoped_refptr<URLRequestTestJob> job =
[email protected]f7022f32014-08-21 16:32:196747 new URLRequestTestJob(req.get(), &default_network_delegate_);
[email protected]90499482013-06-01 00:39:506748 AddTestInterceptor()->set_main_intercept_job(job.get());
[email protected]5033ab82013-03-22 20:17:466749
6750 // Should trigger |job| to be started.
[email protected]255620da2013-08-19 13:14:296751 base::RunLoop().Run();
[email protected]5033ab82013-03-22 20:17:466752 EXPECT_EQ(LOW, job->priority());
6753}
6754
[email protected]80abdad2014-03-15 00:20:546755// Check that creating a network request while entering/exiting suspend mode
6756// fails as it should. This is the only case where an HttpTransactionFactory
6757// does not return an HttpTransaction.
6758TEST_F(URLRequestTestHTTP, NetworkSuspendTest) {
6759 // Create a new HttpNetworkLayer that thinks it's suspended.
6760 HttpNetworkSession::Params params;
6761 params.host_resolver = default_context_.host_resolver();
6762 params.cert_verifier = default_context_.cert_verifier();
6763 params.transport_security_state = default_context_.transport_security_state();
6764 params.proxy_service = default_context_.proxy_service();
6765 params.ssl_config_service = default_context_.ssl_config_service();
6766 params.http_auth_handler_factory =
6767 default_context_.http_auth_handler_factory();
6768 params.network_delegate = &default_network_delegate_;
6769 params.http_server_properties = default_context_.http_server_properties();
6770 scoped_ptr<HttpNetworkLayer> network_layer(
6771 new HttpNetworkLayer(new HttpNetworkSession(params)));
6772 network_layer->OnSuspend();
6773
6774 HttpCache http_cache(network_layer.release(), default_context_.net_log(),
6775 HttpCache::DefaultBackend::InMemory(0));
6776
6777 TestURLRequestContext context(true);
6778 context.set_http_transaction_factory(&http_cache);
6779 context.Init();
6780
6781 TestDelegate d;
davidben151423e2015-03-23 18:48:366782 scoped_ptr<URLRequest> req(
6783 context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:196784 req->Start();
[email protected]80abdad2014-03-15 00:20:546785 base::RunLoop().Run();
6786
6787 EXPECT_TRUE(d.request_failed());
[email protected]f7022f32014-08-21 16:32:196788 EXPECT_EQ(URLRequestStatus::FAILED, req->status().status());
6789 EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, req->status().error());
[email protected]80abdad2014-03-15 00:20:546790}
6791
6792// Check that creating a network request while entering/exiting suspend mode
6793// fails as it should in the case there is no cache. This is the only case
6794// where an HttpTransactionFactory does not return an HttpTransaction.
6795TEST_F(URLRequestTestHTTP, NetworkSuspendTestNoCache) {
6796 // Create a new HttpNetworkLayer that thinks it's suspended.
6797 HttpNetworkSession::Params params;
6798 params.host_resolver = default_context_.host_resolver();
6799 params.cert_verifier = default_context_.cert_verifier();
6800 params.transport_security_state = default_context_.transport_security_state();
6801 params.proxy_service = default_context_.proxy_service();
6802 params.ssl_config_service = default_context_.ssl_config_service();
6803 params.http_auth_handler_factory =
6804 default_context_.http_auth_handler_factory();
6805 params.network_delegate = &default_network_delegate_;
6806 params.http_server_properties = default_context_.http_server_properties();
6807 HttpNetworkLayer network_layer(new HttpNetworkSession(params));
6808 network_layer.OnSuspend();
6809
6810 TestURLRequestContext context(true);
6811 context.set_http_transaction_factory(&network_layer);
6812 context.Init();
6813
6814 TestDelegate d;
davidben151423e2015-03-23 18:48:366815 scoped_ptr<URLRequest> req(
6816 context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:196817 req->Start();
[email protected]80abdad2014-03-15 00:20:546818 base::RunLoop().Run();
6819
6820 EXPECT_TRUE(d.request_failed());
[email protected]f7022f32014-08-21 16:32:196821 EXPECT_EQ(URLRequestStatus::FAILED, req->status().status());
6822 EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, req->status().error());
[email protected]80abdad2014-03-15 00:20:546823}
6824
bengr1bf8e942014-11-07 01:36:506825class URLRequestInterceptorTestHTTP : public URLRequestTestHTTP {
6826 public:
6827 // TODO(bengr): Merge this with the URLRequestInterceptorHTTPTest fixture,
6828 // ideally remove the dependency on URLRequestTestJob, and maybe move these
6829 // tests into the factory tests.
6830 URLRequestInterceptorTestHTTP() : URLRequestTestHTTP(), interceptor_(NULL) {
6831 }
6832
6833 void SetUpFactory() override {
6834 interceptor_ = new MockURLRequestInterceptor();
6835 job_factory_.reset(new URLRequestInterceptingJobFactory(
6836 job_factory_.Pass(), make_scoped_ptr(interceptor_)));
6837 }
6838
6839 MockURLRequestInterceptor* interceptor() const {
6840 return interceptor_;
6841 }
6842
6843 private:
6844 MockURLRequestInterceptor* interceptor_;
6845};
6846
6847TEST_F(URLRequestInterceptorTestHTTP,
6848 NetworkDelegateNotificationOnRedirectIntercept) {
6849 interceptor()->set_intercept_redirect(true);
6850 interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers());
6851 interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data());
6852
6853 ASSERT_TRUE(test_server()->Start());
6854
6855 TestDelegate d;
6856 scoped_ptr<URLRequest> req(default_context().CreateRequest(
davidben151423e2015-03-23 18:48:366857 test_server()->GetURL("files/redirect-test.html"), DEFAULT_PRIORITY, &d));
bengr1bf8e942014-11-07 01:36:506858 req->Start();
6859 base::RunLoop().Run();
6860
6861 EXPECT_TRUE(interceptor()->did_intercept_redirect());
6862 // Check we got one good response
6863 EXPECT_TRUE(req->status().is_success());
6864 if (req->status().is_success())
6865 EXPECT_EQ(200, req->response_headers()->response_code());
6866
6867 EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received());
6868 EXPECT_EQ(1, d.response_started_count());
6869 EXPECT_EQ(0, d.received_redirect_count());
6870
6871 EXPECT_EQ(1, default_network_delegate()->created_requests());
6872 EXPECT_EQ(1, default_network_delegate()->before_send_headers_count());
6873 EXPECT_EQ(1, default_network_delegate()->headers_received_count());
6874}
6875
6876TEST_F(URLRequestInterceptorTestHTTP,
6877 NetworkDelegateNotificationOnErrorIntercept) {
6878 // Intercept that error and respond with an OK response.
6879 interceptor()->set_intercept_final_response(true);
6880 interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers());
6881 interceptor()->set_final_data(MockURLRequestInterceptor::ok_data());
6882 default_network_delegate()->set_can_be_intercepted_on_error(true);
6883
6884 ASSERT_TRUE(test_server()->Start());
6885
6886 TestDelegate d;
6887 scoped_ptr<URLRequest> req(default_context().CreateRequest(
6888 test_server()->GetURL("files/two-content-lengths.html"), DEFAULT_PRIORITY,
davidben151423e2015-03-23 18:48:366889 &d));
bengr1bf8e942014-11-07 01:36:506890 req->set_method("GET");
6891 req->Start();
6892 base::RunLoop().Run();
6893
6894 EXPECT_TRUE(interceptor()->did_intercept_final());
6895
6896 // Check we received one good response.
6897 EXPECT_TRUE(req->status().is_success());
6898 if (req->status().is_success())
6899 EXPECT_EQ(200, req->response_headers()->response_code());
6900 EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received());
6901 EXPECT_EQ(1, d.response_started_count());
6902 EXPECT_EQ(0, d.received_redirect_count());
6903
6904 EXPECT_EQ(1, default_network_delegate()->created_requests());
6905 EXPECT_EQ(1, default_network_delegate()->before_send_headers_count());
6906 EXPECT_EQ(0, default_network_delegate()->headers_received_count());
6907}
6908
6909TEST_F(URLRequestInterceptorTestHTTP,
6910 NetworkDelegateNotificationOnResponseIntercept) {
6911 // Intercept that error and respond with an OK response.
6912 interceptor()->set_intercept_final_response(true);
6913
6914 // Intercept with a real URLRequestHttpJob.
6915 interceptor()->set_use_url_request_http_job(true);
6916
6917 ASSERT_TRUE(test_server()->Start());
6918
6919 TestDelegate d;
6920 scoped_ptr<URLRequest> req(default_context().CreateRequest(
davidben151423e2015-03-23 18:48:366921 test_server()->GetURL("files/simple.html"), DEFAULT_PRIORITY, &d));
bengr1bf8e942014-11-07 01:36:506922 req->set_method("GET");
6923 req->Start();
6924 base::RunLoop().Run();
6925
6926 EXPECT_TRUE(interceptor()->did_intercept_final());
6927
6928 // Check we received one good response.
6929 EXPECT_TRUE(req->status().is_success());
6930 if (req->status().is_success())
6931 EXPECT_EQ(200, req->response_headers()->response_code());
6932 EXPECT_EQ("hello", d.data_received());
6933 EXPECT_EQ(1, d.response_started_count());
6934 EXPECT_EQ(0, d.received_redirect_count());
6935
6936 EXPECT_EQ(1, default_network_delegate()->created_requests());
6937 EXPECT_EQ(2, default_network_delegate()->before_send_headers_count());
6938 EXPECT_EQ(2, default_network_delegate()->headers_received_count());
6939}
6940
mkwst0c5eab872014-11-21 14:18:546941class URLRequestTestReferrerPolicy : public URLRequestTest {
6942 public:
6943 URLRequestTestReferrerPolicy() {}
6944
6945 void InstantiateSameOriginServers(SpawnedTestServer::Type origin_type) {
6946 origin_server_.reset(new SpawnedTestServer(
6947 origin_type, SpawnedTestServer::kLocalhost,
6948 origin_type == SpawnedTestServer::TYPE_HTTPS
6949 ? base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))
6950 : base::FilePath(
6951 FILE_PATH_LITERAL("net/data/url_request_unittest"))));
6952 ASSERT_TRUE(origin_server_->Start());
6953 }
6954
6955 void InstantiateCrossOriginServers(SpawnedTestServer::Type origin_type,
6956 SpawnedTestServer::Type destination_type) {
6957 origin_server_.reset(new SpawnedTestServer(
6958 origin_type, SpawnedTestServer::kLocalhost,
6959 origin_type == SpawnedTestServer::TYPE_HTTPS
6960 ? base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))
6961 : base::FilePath(
6962 FILE_PATH_LITERAL("net/data/url_request_unittest"))));
6963 ASSERT_TRUE(origin_server_->Start());
6964
6965 destination_server_.reset(new SpawnedTestServer(
6966 destination_type, SpawnedTestServer::kLocalhost,
6967 destination_type == SpawnedTestServer::TYPE_HTTPS
6968 ? base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))
6969 : base::FilePath(
6970 FILE_PATH_LITERAL("net/data/url_request_unittest"))));
6971 ASSERT_TRUE(destination_server_->Start());
6972 }
6973
6974 void VerifyReferrerAfterRedirect(URLRequest::ReferrerPolicy policy,
6975 const GURL& referrer,
6976 const GURL& expected) {
6977 // Create and execute the request: we'll only have a |destination_server_|
6978 // if the origins are meant to be distinct. Otherwise, we'll use the
6979 // |origin_server_| for both endpoints.
6980 GURL destination_url =
6981 destination_server_ ? destination_server_->GetURL("echoheader?Referer")
6982 : origin_server_->GetURL("echoheader?Referer");
6983 GURL origin_url =
6984 origin_server_->GetURL("server-redirect?" + destination_url.spec());
6985
6986 TestDelegate d;
6987 scoped_ptr<URLRequest> req(
davidben151423e2015-03-23 18:48:366988 default_context_.CreateRequest(origin_url, DEFAULT_PRIORITY, &d));
mkwst0c5eab872014-11-21 14:18:546989 req->set_referrer_policy(policy);
6990 req->SetReferrer(referrer.spec());
6991 req->Start();
6992 base::RunLoop().Run();
6993
6994 EXPECT_EQ(1, d.response_started_count());
6995 EXPECT_EQ(1, d.received_redirect_count());
6996 EXPECT_EQ(destination_url, req->url());
6997 EXPECT_TRUE(req->status().is_success());
6998 EXPECT_EQ(200, req->response_headers()->response_code());
6999
7000 EXPECT_EQ(expected.spec(), req->referrer());
7001 if (expected.is_empty())
7002 EXPECT_EQ("None", d.data_received());
7003 else
7004 EXPECT_EQ(expected.spec(), d.data_received());
7005 }
7006
7007 SpawnedTestServer* origin_server() const { return origin_server_.get(); }
7008
7009 private:
7010 scoped_ptr<SpawnedTestServer> origin_server_;
7011 scoped_ptr<SpawnedTestServer> destination_server_;
7012};
7013
7014TEST_F(URLRequestTestReferrerPolicy, HTTPToSameOriginHTTP) {
7015 InstantiateSameOriginServers(SpawnedTestServer::TYPE_HTTP);
7016
7017 VerifyReferrerAfterRedirect(
7018 URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
7019 origin_server()->GetURL("path/to/file.html"),
7020 origin_server()->GetURL("path/to/file.html"));
7021
7022 VerifyReferrerAfterRedirect(
7023 URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN,
7024 origin_server()->GetURL("path/to/file.html"),
7025 origin_server()->GetURL("path/to/file.html"));
7026
7027 VerifyReferrerAfterRedirect(
7028 URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN,
7029 origin_server()->GetURL("path/to/file.html"),
7030 origin_server()->GetURL("path/to/file.html"));
7031
7032 VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER,
7033 origin_server()->GetURL("path/to/file.html"),
7034 origin_server()->GetURL("path/to/file.html"));
7035}
7036
7037TEST_F(URLRequestTestReferrerPolicy, HTTPToCrossOriginHTTP) {
7038 InstantiateCrossOriginServers(SpawnedTestServer::TYPE_HTTP,
7039 SpawnedTestServer::TYPE_HTTP);
7040
7041 VerifyReferrerAfterRedirect(
7042 URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
7043 origin_server()->GetURL("path/to/file.html"),
7044 origin_server()->GetURL("path/to/file.html"));
7045
7046 VerifyReferrerAfterRedirect(
7047 URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN,
7048 origin_server()->GetURL("path/to/file.html"),
7049 origin_server()->GetURL(std::string()));
7050
7051 VerifyReferrerAfterRedirect(
7052 URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN,
7053 origin_server()->GetURL("path/to/file.html"),
7054 origin_server()->GetURL(std::string()));
7055
7056 VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER,
7057 origin_server()->GetURL("path/to/file.html"),
7058 origin_server()->GetURL("path/to/file.html"));
7059}
7060
7061TEST_F(URLRequestTestReferrerPolicy, HTTPSToSameOriginHTTPS) {
7062 InstantiateSameOriginServers(SpawnedTestServer::TYPE_HTTPS);
7063
7064 VerifyReferrerAfterRedirect(
7065 URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
7066 origin_server()->GetURL("path/to/file.html"),
7067 origin_server()->GetURL("path/to/file.html"));
7068
7069 VerifyReferrerAfterRedirect(
7070 URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN,
7071 origin_server()->GetURL("path/to/file.html"),
7072 origin_server()->GetURL("path/to/file.html"));
7073
7074 VerifyReferrerAfterRedirect(
7075 URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN,
7076 origin_server()->GetURL("path/to/file.html"),
7077 origin_server()->GetURL("path/to/file.html"));
7078
7079 VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER,
7080 origin_server()->GetURL("path/to/file.html"),
7081 origin_server()->GetURL("path/to/file.html"));
7082}
7083
7084TEST_F(URLRequestTestReferrerPolicy, HTTPSToCrossOriginHTTPS) {
7085 InstantiateCrossOriginServers(SpawnedTestServer::TYPE_HTTPS,
7086 SpawnedTestServer::TYPE_HTTPS);
7087
7088 VerifyReferrerAfterRedirect(
7089 URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
7090 origin_server()->GetURL("path/to/file.html"),
7091 origin_server()->GetURL("path/to/file.html"));
7092
7093 VerifyReferrerAfterRedirect(
7094 URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN,
7095 origin_server()->GetURL("path/to/file.html"),
7096 origin_server()->GetURL(std::string()));
7097
7098 VerifyReferrerAfterRedirect(
7099 URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN,
7100 origin_server()->GetURL("path/to/file.html"),
7101 origin_server()->GetURL(std::string()));
7102
7103 VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER,
7104 origin_server()->GetURL("path/to/file.html"),
7105 origin_server()->GetURL("path/to/file.html"));
7106}
7107
7108TEST_F(URLRequestTestReferrerPolicy, HTTPToHTTPS) {
7109 InstantiateCrossOriginServers(SpawnedTestServer::TYPE_HTTP,
7110 SpawnedTestServer::TYPE_HTTPS);
7111
7112 VerifyReferrerAfterRedirect(
7113 URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
7114 origin_server()->GetURL("path/to/file.html"),
7115 origin_server()->GetURL("path/to/file.html"));
7116
7117 VerifyReferrerAfterRedirect(
7118 URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN,
7119 origin_server()->GetURL("path/to/file.html"),
7120 origin_server()->GetURL(std::string()));
7121
7122 VerifyReferrerAfterRedirect(
7123 URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN,
7124 origin_server()->GetURL("path/to/file.html"),
7125 origin_server()->GetURL(std::string()));
7126
7127 VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER,
7128 origin_server()->GetURL("path/to/file.html"),
7129 origin_server()->GetURL("path/to/file.html"));
7130}
7131
7132TEST_F(URLRequestTestReferrerPolicy, HTTPSToHTTP) {
7133 InstantiateCrossOriginServers(SpawnedTestServer::TYPE_HTTPS,
7134 SpawnedTestServer::TYPE_HTTP);
7135
7136 VerifyReferrerAfterRedirect(
7137 URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
7138 origin_server()->GetURL("path/to/file.html"), GURL());
7139
7140 VerifyReferrerAfterRedirect(
7141 URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN,
7142 origin_server()->GetURL("path/to/file.html"), GURL());
7143
7144 VerifyReferrerAfterRedirect(
7145 URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN,
7146 origin_server()->GetURL("path/to/file.html"),
7147 origin_server()->GetURL(std::string()));
7148
7149 VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER,
7150 origin_server()->GetURL("path/to/file.html"),
7151 origin_server()->GetURL("path/to/file.html"));
7152}
7153
[email protected]73e0bba2009-02-19 22:57:097154class HTTPSRequestTest : public testing::Test {
[email protected]87a09a92011-07-14 15:50:507155 public:
[email protected]ef2bf422012-05-11 03:27:097156 HTTPSRequestTest() : default_context_(true) {
7157 default_context_.set_network_delegate(&default_network_delegate_);
7158 default_context_.Init();
[email protected]87a09a92011-07-14 15:50:507159 }
dcheng67be2b1f2014-10-27 21:47:297160 ~HTTPSRequestTest() override {}
[email protected]87a09a92011-07-14 15:50:507161
7162 protected:
[email protected]ceefd7fd2012-11-29 00:36:247163 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest.
[email protected]ef2bf422012-05-11 03:27:097164 TestURLRequestContext default_context_;
[email protected]ea224582008-12-07 20:25:467165};
7166
[email protected]c044616e2013-02-20 02:01:267167TEST_F(HTTPSRequestTest, HTTPSGetTest) {
[email protected]ce7d0cbc2013-05-03 18:57:227168 SpawnedTestServer test_server(
7169 SpawnedTestServer::TYPE_HTTPS,
7170 SpawnedTestServer::kLocalhost,
7171 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]95409e12010-08-17 20:07:117172 ASSERT_TRUE(test_server.Start());
[email protected]ea224582008-12-07 20:25:467173
[email protected]ea224582008-12-07 20:25:467174 TestDelegate d;
7175 {
[email protected]f7022f32014-08-21 16:32:197176 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:367177 test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:197178 r->Start();
7179 EXPECT_TRUE(r->is_pending());
[email protected]ea224582008-12-07 20:25:467180
[email protected]255620da2013-08-19 13:14:297181 base::RunLoop().Run();
[email protected]ea224582008-12-07 20:25:467182
7183 EXPECT_EQ(1, d.response_started_count());
7184 EXPECT_FALSE(d.received_data_before_response());
7185 EXPECT_NE(0, d.bytes_received());
[email protected]f7022f32014-08-21 16:32:197186 CheckSSLInfo(r->ssl_info());
[email protected]6d81b482011-02-22 19:47:197187 EXPECT_EQ(test_server.host_port_pair().host(),
[email protected]f7022f32014-08-21 16:32:197188 r->GetSocketAddress().host());
[email protected]6d81b482011-02-22 19:47:197189 EXPECT_EQ(test_server.host_port_pair().port(),
[email protected]f7022f32014-08-21 16:32:197190 r->GetSocketAddress().port());
[email protected]ea224582008-12-07 20:25:467191 }
[email protected]ea224582008-12-07 20:25:467192}
7193
[email protected]5774ada2010-07-15 06:30:547194TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) {
[email protected]ce7d0cbc2013-05-03 18:57:227195 SpawnedTestServer::SSLOptions ssl_options(
7196 SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME);
7197 SpawnedTestServer test_server(
7198 SpawnedTestServer::TYPE_HTTPS,
7199 ssl_options,
7200 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]95409e12010-08-17 20:07:117201 ASSERT_TRUE(test_server.Start());
[email protected]bacff652009-03-31 17:50:337202
7203 bool err_allowed = true;
7204 for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) {
7205 TestDelegate d;
7206 {
7207 d.set_allow_certificate_errors(err_allowed);
[email protected]f7022f32014-08-21 16:32:197208 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:367209 test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d));
[email protected]bacff652009-03-31 17:50:337210
[email protected]f7022f32014-08-21 16:32:197211 r->Start();
7212 EXPECT_TRUE(r->is_pending());
[email protected]bacff652009-03-31 17:50:337213
[email protected]255620da2013-08-19 13:14:297214 base::RunLoop().Run();
[email protected]bacff652009-03-31 17:50:337215
7216 EXPECT_EQ(1, d.response_started_count());
7217 EXPECT_FALSE(d.received_data_before_response());
7218 EXPECT_TRUE(d.have_certificate_errors());
[email protected]96adadb2010-08-28 01:16:177219 if (err_allowed) {
[email protected]bacff652009-03-31 17:50:337220 EXPECT_NE(0, d.bytes_received());
[email protected]f7022f32014-08-21 16:32:197221 CheckSSLInfo(r->ssl_info());
[email protected]96adadb2010-08-28 01:16:177222 } else {
[email protected]bacff652009-03-31 17:50:337223 EXPECT_EQ(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:177224 }
[email protected]bacff652009-03-31 17:50:337225 }
7226 }
7227}
7228
[email protected]5774ada2010-07-15 06:30:547229TEST_F(HTTPSRequestTest, HTTPSExpiredTest) {
[email protected]ce7d0cbc2013-05-03 18:57:227230 SpawnedTestServer::SSLOptions ssl_options(
7231 SpawnedTestServer::SSLOptions::CERT_EXPIRED);
7232 SpawnedTestServer test_server(
7233 SpawnedTestServer::TYPE_HTTPS,
7234 ssl_options,
7235 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]95409e12010-08-17 20:07:117236 ASSERT_TRUE(test_server.Start());
[email protected]bacff652009-03-31 17:50:337237
7238 // Iterate from false to true, just so that we do the opposite of the
7239 // previous test in order to increase test coverage.
7240 bool err_allowed = false;
7241 for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) {
7242 TestDelegate d;
7243 {
7244 d.set_allow_certificate_errors(err_allowed);
[email protected]f7022f32014-08-21 16:32:197245 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:367246 test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d));
[email protected]bacff652009-03-31 17:50:337247
[email protected]f7022f32014-08-21 16:32:197248 r->Start();
7249 EXPECT_TRUE(r->is_pending());
[email protected]bacff652009-03-31 17:50:337250
[email protected]255620da2013-08-19 13:14:297251 base::RunLoop().Run();
[email protected]bacff652009-03-31 17:50:337252
7253 EXPECT_EQ(1, d.response_started_count());
7254 EXPECT_FALSE(d.received_data_before_response());
7255 EXPECT_TRUE(d.have_certificate_errors());
[email protected]96adadb2010-08-28 01:16:177256 if (err_allowed) {
[email protected]bacff652009-03-31 17:50:337257 EXPECT_NE(0, d.bytes_received());
[email protected]f7022f32014-08-21 16:32:197258 CheckSSLInfo(r->ssl_info());
[email protected]96adadb2010-08-28 01:16:177259 } else {
[email protected]bacff652009-03-31 17:50:337260 EXPECT_EQ(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:177261 }
[email protected]bacff652009-03-31 17:50:337262 }
7263 }
7264}
[email protected]73e0bba2009-02-19 22:57:097265
davidbena4c9d062015-04-03 22:34:257266// Tests that servers which require a deprecated cipher suite still work.
7267TEST_F(HTTPSRequestTest, CipherFallbackTest) {
vishal.b62985ca92015-04-17 08:45:517268 TestNetLog net_log;
davidbena4c9d062015-04-03 22:34:257269 default_context_.set_net_log(&net_log);
7270
7271 SpawnedTestServer::SSLOptions ssl_options;
7272 ssl_options.bulk_ciphers = SpawnedTestServer::SSLOptions::BULK_CIPHER_RC4;
7273 SpawnedTestServer test_server(
7274 SpawnedTestServer::TYPE_HTTPS, ssl_options,
7275 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7276 ASSERT_TRUE(test_server.Start());
7277
7278 TestDelegate d;
7279 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
7280 test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d));
7281 r->Start();
7282 EXPECT_TRUE(r->is_pending());
7283
7284 base::RunLoop().Run();
7285
7286 EXPECT_EQ(1, d.response_started_count());
7287 EXPECT_FALSE(d.received_data_before_response());
7288 EXPECT_NE(0, d.bytes_received());
7289 CheckSSLInfo(r->ssl_info());
7290 EXPECT_EQ(test_server.host_port_pair().host(), r->GetSocketAddress().host());
7291 EXPECT_EQ(test_server.host_port_pair().port(), r->GetSocketAddress().port());
7292
7293 // No version downgrade should have been necessary.
7294 EXPECT_FALSE(r->ssl_info().connection_status &
7295 SSL_CONNECTION_VERSION_FALLBACK);
7296 int expected_version = SSL_CONNECTION_VERSION_TLS1_2;
7297 if (SSLClientSocket::GetMaxSupportedSSLVersion() <
7298 SSL_PROTOCOL_VERSION_TLS1_2) {
7299 expected_version = SSL_CONNECTION_VERSION_TLS1_1;
7300 }
7301 EXPECT_EQ(expected_version,
7302 SSLConnectionStatusToVersion(r->ssl_info().connection_status));
7303
vishal.b62985ca92015-04-17 08:45:517304 TestNetLog::CapturedEntryList entries;
davidbena4c9d062015-04-03 22:34:257305 net_log.GetEntries(&entries);
7306 ExpectLogContainsSomewhere(entries, 0, NetLog::TYPE_SSL_CIPHER_FALLBACK,
7307 NetLog::PHASE_NONE);
7308}
7309
[email protected]316c1e5e2012-09-12 15:17:447310// This tests that a load of www.google.com with a certificate error sets
7311// the |certificate_errors_are_fatal| flag correctly. This flag will cause
7312// the interstitial to be fatal.
7313TEST_F(HTTPSRequestTest, HTTPSPreloadedHSTSTest) {
[email protected]ce7d0cbc2013-05-03 18:57:227314 SpawnedTestServer::SSLOptions ssl_options(
7315 SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME);
7316 SpawnedTestServer test_server(
7317 SpawnedTestServer::TYPE_HTTPS,
7318 ssl_options,
7319 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:447320 ASSERT_TRUE(test_server.Start());
7321
7322 // We require that the URL be www.google.com in order to pick up the
7323 // preloaded HSTS entries in the TransportSecurityState. This means that we
7324 // have to use a MockHostResolver in order to direct www.google.com to the
[email protected]ceefd7fd2012-11-29 00:36:247325 // testserver. By default, MockHostResolver maps all hosts to 127.0.0.1.
[email protected]316c1e5e2012-09-12 15:17:447326
7327 MockHostResolver host_resolver;
[email protected]ceefd7fd2012-11-29 00:36:247328 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]316c1e5e2012-09-12 15:17:447329 TestURLRequestContext context(true);
7330 context.set_network_delegate(&network_delegate);
7331 context.set_host_resolver(&host_resolver);
7332 TransportSecurityState transport_security_state;
7333 context.set_transport_security_state(&transport_security_state);
7334 context.Init();
7335
7336 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:197337 scoped_ptr<URLRequest> r(context.CreateRequest(
7338 GURL(base::StringPrintf("https://www.google.com:%d",
davidben151423e2015-03-23 18:48:367339 test_server.host_port_pair().port())),
7340 DEFAULT_PRIORITY, &d));
[email protected]316c1e5e2012-09-12 15:17:447341
[email protected]f7022f32014-08-21 16:32:197342 r->Start();
7343 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:447344
[email protected]255620da2013-08-19 13:14:297345 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:447346
7347 EXPECT_EQ(1, d.response_started_count());
7348 EXPECT_FALSE(d.received_data_before_response());
7349 EXPECT_TRUE(d.have_certificate_errors());
7350 EXPECT_TRUE(d.certificate_errors_are_fatal());
7351}
7352
7353// This tests that cached HTTPS page loads do not cause any updates to the
7354// TransportSecurityState.
7355TEST_F(HTTPSRequestTest, HTTPSErrorsNoClobberTSSTest) {
7356 // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't
7357 // matter. It just has to be any error.
[email protected]ce7d0cbc2013-05-03 18:57:227358 SpawnedTestServer::SSLOptions ssl_options(
7359 SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME);
7360 SpawnedTestServer test_server(
7361 SpawnedTestServer::TYPE_HTTPS,
7362 ssl_options,
7363 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:447364 ASSERT_TRUE(test_server.Start());
7365
[email protected]9e6968d2014-05-07 21:46:267366 // We require that the URL be www.google.com in order to pick up the static
7367 // and dynamic STS and PKP entries in the TransportSecurityState. This means
7368 // that we have to use a MockHostResolver in order to direct www.google.com to
7369 // the testserver. By default, MockHostResolver maps all hosts to 127.0.0.1.
[email protected]316c1e5e2012-09-12 15:17:447370
7371 MockHostResolver host_resolver;
[email protected]ceefd7fd2012-11-29 00:36:247372 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]316c1e5e2012-09-12 15:17:447373 TestURLRequestContext context(true);
7374 context.set_network_delegate(&network_delegate);
7375 context.set_host_resolver(&host_resolver);
7376 TransportSecurityState transport_security_state;
[email protected]9e6968d2014-05-07 21:46:267377
7378 TransportSecurityState::DomainState static_domain_state;
7379 EXPECT_TRUE(transport_security_state.GetStaticDomainState(
Adam Langley5cbb7d7a2014-09-25 23:14:127380 "www.google.com", &static_domain_state));
[email protected]316c1e5e2012-09-12 15:17:447381 context.set_transport_security_state(&transport_security_state);
7382 context.Init();
7383
[email protected]9e6968d2014-05-07 21:46:267384 TransportSecurityState::DomainState dynamic_domain_state;
7385 EXPECT_FALSE(transport_security_state.GetDynamicDomainState(
7386 "www.google.com", &dynamic_domain_state));
7387
[email protected]316c1e5e2012-09-12 15:17:447388 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:197389 scoped_ptr<URLRequest> r(context.CreateRequest(
7390 GURL(base::StringPrintf("https://www.google.com:%d",
davidben151423e2015-03-23 18:48:367391 test_server.host_port_pair().port())),
7392 DEFAULT_PRIORITY, &d));
[email protected]316c1e5e2012-09-12 15:17:447393
[email protected]f7022f32014-08-21 16:32:197394 r->Start();
7395 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:447396
[email protected]255620da2013-08-19 13:14:297397 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:447398
7399 EXPECT_EQ(1, d.response_started_count());
7400 EXPECT_FALSE(d.received_data_before_response());
7401 EXPECT_TRUE(d.have_certificate_errors());
7402 EXPECT_TRUE(d.certificate_errors_are_fatal());
7403
[email protected]9e6968d2014-05-07 21:46:267404 // Get a fresh copy of the states, and check that they haven't changed.
7405 TransportSecurityState::DomainState new_static_domain_state;
7406 EXPECT_TRUE(transport_security_state.GetStaticDomainState(
Adam Langley5cbb7d7a2014-09-25 23:14:127407 "www.google.com", &new_static_domain_state));
[email protected]9e6968d2014-05-07 21:46:267408 TransportSecurityState::DomainState new_dynamic_domain_state;
7409 EXPECT_FALSE(transport_security_state.GetDynamicDomainState(
7410 "www.google.com", &new_dynamic_domain_state));
7411
7412 EXPECT_EQ(new_static_domain_state.sts.upgrade_mode,
7413 static_domain_state.sts.upgrade_mode);
7414 EXPECT_EQ(new_static_domain_state.sts.include_subdomains,
7415 static_domain_state.sts.include_subdomains);
7416 EXPECT_EQ(new_static_domain_state.pkp.include_subdomains,
7417 static_domain_state.pkp.include_subdomains);
7418 EXPECT_TRUE(FingerprintsEqual(new_static_domain_state.pkp.spki_hashes,
7419 static_domain_state.pkp.spki_hashes));
7420 EXPECT_TRUE(FingerprintsEqual(new_static_domain_state.pkp.bad_spki_hashes,
7421 static_domain_state.pkp.bad_spki_hashes));
[email protected]316c1e5e2012-09-12 15:17:447422}
7423
[email protected]8ccc69f2012-11-28 19:52:147424// Make sure HSTS preserves a POST request's method and body.
7425TEST_F(HTTPSRequestTest, HSTSPreservesPosts) {
7426 static const char kData[] = "hello world";
7427
[email protected]ce7d0cbc2013-05-03 18:57:227428 SpawnedTestServer::SSLOptions ssl_options(
7429 SpawnedTestServer::SSLOptions::CERT_OK);
7430 SpawnedTestServer test_server(
7431 SpawnedTestServer::TYPE_HTTPS,
7432 ssl_options,
7433 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]8ccc69f2012-11-28 19:52:147434 ASSERT_TRUE(test_server.Start());
7435
7436
7437 // Per spec, TransportSecurityState expects a domain name, rather than an IP
7438 // address, so a MockHostResolver is needed to redirect www.somewhere.com to
[email protected]ce7d0cbc2013-05-03 18:57:227439 // the SpawnedTestServer. By default, MockHostResolver maps all hosts
7440 // to 127.0.0.1.
[email protected]8ccc69f2012-11-28 19:52:147441 MockHostResolver host_resolver;
[email protected]8ccc69f2012-11-28 19:52:147442
7443 // Force https for www.somewhere.com.
7444 TransportSecurityState transport_security_state;
[email protected]474f079e2013-03-02 19:11:207445 base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000);
7446 bool include_subdomains = false;
7447 transport_security_state.AddHSTS("www.somewhere.com", expiry,
7448 include_subdomains);
[email protected]8ccc69f2012-11-28 19:52:147449
7450 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
7451
7452 TestURLRequestContext context(true);
7453 context.set_host_resolver(&host_resolver);
7454 context.set_transport_security_state(&transport_security_state);
7455 context.set_network_delegate(&network_delegate);
7456 context.Init();
7457
7458 TestDelegate d;
7459 // Navigating to https://www.somewhere.com instead of https://127.0.0.1 will
7460 // cause a certificate error. Ignore the error.
7461 d.set_allow_certificate_errors(true);
7462
[email protected]f7022f32014-08-21 16:32:197463 scoped_ptr<URLRequest> req(context.CreateRequest(
7464 GURL(base::StringPrintf("http://www.somewhere.com:%d/echo",
davidben151423e2015-03-23 18:48:367465 test_server.host_port_pair().port())),
7466 DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:197467 req->set_method("POST");
mmenkecbc2b712014-10-09 20:29:077468 req->set_upload(CreateSimpleUploadData(kData));
[email protected]8ccc69f2012-11-28 19:52:147469
[email protected]f7022f32014-08-21 16:32:197470 req->Start();
[email protected]255620da2013-08-19 13:14:297471 base::RunLoop().Run();
[email protected]8ccc69f2012-11-28 19:52:147472
[email protected]f7022f32014-08-21 16:32:197473 EXPECT_EQ("https", req->url().scheme());
7474 EXPECT_EQ("POST", req->method());
[email protected]8ccc69f2012-11-28 19:52:147475 EXPECT_EQ(kData, d.data_received());
[email protected]07d9bfd2013-06-27 14:16:407476
7477 LoadTimingInfo load_timing_info;
7478 network_delegate.GetLoadTimingInfoBeforeRedirect(&load_timing_info);
7479 // LoadTimingInfo of HSTS redirects is similar to that of network cache hits
7480 TestLoadTimingCacheHitNoNetwork(load_timing_info);
[email protected]8ccc69f2012-11-28 19:52:147481}
7482
rob4e0be1f2014-09-11 23:40:227483// Make sure that the CORS headers are added to cross-origin HSTS redirects.
7484TEST_F(HTTPSRequestTest, HSTSCrossOriginAddHeaders) {
7485 static const char kOriginHeaderValue[] = "http://www.example.com";
7486
7487 SpawnedTestServer::SSLOptions ssl_options(
7488 SpawnedTestServer::SSLOptions::CERT_OK);
7489 SpawnedTestServer test_server(
7490 SpawnedTestServer::TYPE_HTTPS,
7491 ssl_options,
7492 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7493 ASSERT_TRUE(test_server.Start());
7494
7495 // Per spec, TransportSecurityState expects a domain name, rather than an IP
7496 // address, so a MockHostResolver is needed to redirect example.net to the
7497 // SpawnedTestServer. MockHostResolver maps all hosts to 127.0.0.1 by default.
7498 MockHostResolver host_resolver;
7499
7500 TransportSecurityState transport_security_state;
7501 base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1);
7502 bool include_subdomains = false;
7503 transport_security_state.AddHSTS("example.net", expiry, include_subdomains);
7504
7505 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
7506
7507 MockCertVerifier cert_verifier;
7508 cert_verifier.set_default_result(OK);
7509
7510 TestURLRequestContext context(true);
7511 context.set_host_resolver(&host_resolver);
7512 context.set_transport_security_state(&transport_security_state);
7513 context.set_network_delegate(&network_delegate);
7514 context.set_cert_verifier(&cert_verifier);
7515 context.Init();
7516
7517 GURL hsts_http_url(base::StringPrintf("http://example.net:%d/somehstssite",
7518 test_server.host_port_pair().port()));
7519 url::Replacements<char> replacements;
7520 const char kNewScheme[] = "https";
7521 replacements.SetScheme(kNewScheme, url::Component(0, strlen(kNewScheme)));
7522 GURL hsts_https_url = hsts_http_url.ReplaceComponents(replacements);
7523
7524 TestDelegate d;
7525 // Quit on redirect to allow response header inspection upon redirect.
7526 d.set_quit_on_redirect(true);
7527
davidben151423e2015-03-23 18:48:367528 scoped_ptr<URLRequest> req(
7529 context.CreateRequest(hsts_http_url, DEFAULT_PRIORITY, &d));
rob4e0be1f2014-09-11 23:40:227530 // Set Origin header to simulate a cross-origin request.
7531 HttpRequestHeaders request_headers;
7532 request_headers.SetHeader("Origin", kOriginHeaderValue);
7533 req->SetExtraRequestHeaders(request_headers);
7534
7535 req->Start();
7536 base::RunLoop().Run();
7537
7538 EXPECT_EQ(1, d.received_redirect_count());
7539
7540 const HttpResponseHeaders* headers = req->response_headers();
7541 std::string redirect_location;
7542 EXPECT_TRUE(headers->EnumerateHeader(NULL, "Location", &redirect_location));
7543 EXPECT_EQ(hsts_https_url.spec(), redirect_location);
7544
7545 std::string received_cors_header;
7546 EXPECT_TRUE(headers->EnumerateHeader(NULL, "Access-Control-Allow-Origin",
7547 &received_cors_header));
7548 EXPECT_EQ(kOriginHeaderValue, received_cors_header);
7549}
7550
Adam Ricecb76ac62015-02-20 05:33:257551// This just tests the behaviour of GetHSTSRedirect(). End-to-end tests of HSTS
7552// are performed in net/websockets/websocket_end_to_end_test.cc.
7553TEST(WebSocketURLRequestTest, HSTSApplied) {
7554 TestNetworkDelegate network_delegate;
7555 TransportSecurityState transport_security_state;
7556 base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1);
7557 bool include_subdomains = false;
7558 transport_security_state.AddHSTS("example.net", expiry, include_subdomains);
7559 TestURLRequestContext context(true);
7560 context.set_transport_security_state(&transport_security_state);
7561 context.set_network_delegate(&network_delegate);
7562 context.Init();
7563 GURL ws_url("ws://example.net/echo");
7564 TestDelegate delegate;
7565 scoped_ptr<URLRequest> request(
davidben151423e2015-03-23 18:48:367566 context.CreateRequest(ws_url, DEFAULT_PRIORITY, &delegate));
Adam Ricecb76ac62015-02-20 05:33:257567 EXPECT_TRUE(request->GetHSTSRedirect(&ws_url));
7568 EXPECT_TRUE(ws_url.SchemeIs("wss"));
7569}
7570
[email protected]316c1e5e2012-09-12 15:17:447571namespace {
7572
7573class SSLClientAuthTestDelegate : public TestDelegate {
7574 public:
7575 SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) {
7576 }
dchengb03027d2014-10-21 12:00:207577 void OnCertificateRequested(URLRequest* request,
7578 SSLCertRequestInfo* cert_request_info) override {
[email protected]316c1e5e2012-09-12 15:17:447579 on_certificate_requested_count_++;
[email protected]2da659e2013-05-23 20:51:347580 base::MessageLoop::current()->Quit();
[email protected]316c1e5e2012-09-12 15:17:447581 }
7582 int on_certificate_requested_count() {
7583 return on_certificate_requested_count_;
7584 }
7585 private:
7586 int on_certificate_requested_count_;
7587};
7588
7589} // namespace
7590
7591// TODO(davidben): Test the rest of the code. Specifically,
7592// - Filtering which certificates to select.
7593// - Sending a certificate back.
7594// - Getting a certificate request in an SSL renegotiation sending the
7595// HTTP request.
7596TEST_F(HTTPSRequestTest, ClientAuthTest) {
[email protected]ce7d0cbc2013-05-03 18:57:227597 SpawnedTestServer::SSLOptions ssl_options;
[email protected]316c1e5e2012-09-12 15:17:447598 ssl_options.request_client_certificate = true;
[email protected]ce7d0cbc2013-05-03 18:57:227599 SpawnedTestServer test_server(
7600 SpawnedTestServer::TYPE_HTTPS,
7601 ssl_options,
7602 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:447603 ASSERT_TRUE(test_server.Start());
7604
7605 SSLClientAuthTestDelegate d;
7606 {
[email protected]f7022f32014-08-21 16:32:197607 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:367608 test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d));
[email protected]316c1e5e2012-09-12 15:17:447609
[email protected]f7022f32014-08-21 16:32:197610 r->Start();
7611 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:447612
[email protected]255620da2013-08-19 13:14:297613 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:447614
7615 EXPECT_EQ(1, d.on_certificate_requested_count());
7616 EXPECT_FALSE(d.received_data_before_response());
7617 EXPECT_EQ(0, d.bytes_received());
7618
7619 // Send no certificate.
7620 // TODO(davidben): Get temporary client cert import (with keys) working on
7621 // all platforms so we can test sending a cert as well.
[email protected]f7022f32014-08-21 16:32:197622 r->ContinueWithCertificate(NULL);
[email protected]316c1e5e2012-09-12 15:17:447623
[email protected]255620da2013-08-19 13:14:297624 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:447625
7626 EXPECT_EQ(1, d.response_started_count());
7627 EXPECT_FALSE(d.received_data_before_response());
7628 EXPECT_NE(0, d.bytes_received());
7629 }
7630}
7631
7632TEST_F(HTTPSRequestTest, ResumeTest) {
7633 // Test that we attempt a session resume when making two connections to the
7634 // same host.
[email protected]ce7d0cbc2013-05-03 18:57:227635 SpawnedTestServer::SSLOptions ssl_options;
[email protected]316c1e5e2012-09-12 15:17:447636 ssl_options.record_resume = true;
[email protected]ce7d0cbc2013-05-03 18:57:227637 SpawnedTestServer test_server(
7638 SpawnedTestServer::TYPE_HTTPS,
7639 ssl_options,
7640 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:447641 ASSERT_TRUE(test_server.Start());
7642
7643 SSLClientSocket::ClearSessionCache();
7644
7645 {
7646 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:197647 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:367648 test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d));
[email protected]316c1e5e2012-09-12 15:17:447649
[email protected]f7022f32014-08-21 16:32:197650 r->Start();
7651 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:447652
[email protected]255620da2013-08-19 13:14:297653 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:447654
7655 EXPECT_EQ(1, d.response_started_count());
7656 }
7657
7658 reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())->
7659 CloseAllConnections();
7660
7661 {
7662 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:197663 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:367664 test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d));
[email protected]316c1e5e2012-09-12 15:17:447665
[email protected]f7022f32014-08-21 16:32:197666 r->Start();
7667 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:447668
[email protected]255620da2013-08-19 13:14:297669 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:447670
7671 // The response will look like;
7672 // insert abc
7673 // lookup abc
7674 // insert xyz
7675 //
7676 // With a newline at the end which makes the split think that there are
7677 // four lines.
7678
7679 EXPECT_EQ(1, d.response_started_count());
7680 std::vector<std::string> lines;
7681 base::SplitString(d.data_received(), '\n', &lines);
7682 ASSERT_EQ(4u, lines.size()) << d.data_received();
7683
7684 std::string session_id;
7685
7686 for (size_t i = 0; i < 2; i++) {
7687 std::vector<std::string> parts;
7688 base::SplitString(lines[i], '\t', &parts);
7689 ASSERT_EQ(2u, parts.size());
7690 if (i == 0) {
7691 EXPECT_EQ("insert", parts[0]);
7692 session_id = parts[1];
7693 } else {
7694 EXPECT_EQ("lookup", parts[0]);
7695 EXPECT_EQ(session_id, parts[1]);
7696 }
7697 }
7698 }
7699}
7700
Adam Langley32352ad2014-10-14 22:31:007701// AssertTwoDistinctSessionsInserted checks that |session_info|, which must be
7702// the result of fetching "ssl-session-cache" from the test server, indicates
7703// that exactly two different sessions were inserted, with no lookups etc.
7704static void AssertTwoDistinctSessionsInserted(const string& session_info) {
7705 std::vector<std::string> lines;
7706 base::SplitString(session_info, '\n', &lines);
7707 ASSERT_EQ(3u, lines.size()) << session_info;
7708
7709 std::string session_id;
7710 for (size_t i = 0; i < 2; i++) {
7711 std::vector<std::string> parts;
7712 base::SplitString(lines[i], '\t', &parts);
7713 ASSERT_EQ(2u, parts.size());
7714 EXPECT_EQ("insert", parts[0]);
7715 if (i == 0) {
7716 session_id = parts[1];
7717 } else {
7718 EXPECT_NE(session_id, parts[1]);
7719 }
7720 }
7721}
7722
[email protected]316c1e5e2012-09-12 15:17:447723TEST_F(HTTPSRequestTest, SSLSessionCacheShardTest) {
7724 // Test that sessions aren't resumed when the value of ssl_session_cache_shard
7725 // differs.
[email protected]ce7d0cbc2013-05-03 18:57:227726 SpawnedTestServer::SSLOptions ssl_options;
[email protected]316c1e5e2012-09-12 15:17:447727 ssl_options.record_resume = true;
[email protected]ce7d0cbc2013-05-03 18:57:227728 SpawnedTestServer test_server(
7729 SpawnedTestServer::TYPE_HTTPS,
7730 ssl_options,
7731 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:447732 ASSERT_TRUE(test_server.Start());
7733
7734 SSLClientSocket::ClearSessionCache();
7735
7736 {
7737 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:197738 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:367739 test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d));
[email protected]316c1e5e2012-09-12 15:17:447740
[email protected]f7022f32014-08-21 16:32:197741 r->Start();
7742 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:447743
[email protected]255620da2013-08-19 13:14:297744 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:447745
7746 EXPECT_EQ(1, d.response_started_count());
7747 }
7748
7749 // Now create a new HttpCache with a different ssl_session_cache_shard value.
7750 HttpNetworkSession::Params params;
7751 params.host_resolver = default_context_.host_resolver();
7752 params.cert_verifier = default_context_.cert_verifier();
[email protected]b1c988b2013-06-13 06:48:117753 params.transport_security_state = default_context_.transport_security_state();
[email protected]316c1e5e2012-09-12 15:17:447754 params.proxy_service = default_context_.proxy_service();
7755 params.ssl_config_service = default_context_.ssl_config_service();
7756 params.http_auth_handler_factory =
7757 default_context_.http_auth_handler_factory();
[email protected]cc05edc2013-03-08 18:04:417758 params.network_delegate = &default_network_delegate_;
[email protected]316c1e5e2012-09-12 15:17:447759 params.http_server_properties = default_context_.http_server_properties();
7760 params.ssl_session_cache_shard = "alternate";
7761
[email protected]cba24642014-08-15 20:49:597762 scoped_ptr<HttpCache> cache(new HttpCache(
7763 new HttpNetworkSession(params),
7764 HttpCache::DefaultBackend::InMemory(0)));
[email protected]316c1e5e2012-09-12 15:17:447765
7766 default_context_.set_http_transaction_factory(cache.get());
7767
7768 {
7769 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:197770 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:367771 test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d));
[email protected]316c1e5e2012-09-12 15:17:447772
[email protected]f7022f32014-08-21 16:32:197773 r->Start();
7774 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:447775
[email protected]255620da2013-08-19 13:14:297776 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:447777
7778 // The response will look like;
7779 // insert abc
7780 // insert xyz
7781 //
7782 // With a newline at the end which makes the split think that there are
7783 // three lines.
7784
7785 EXPECT_EQ(1, d.response_started_count());
Adam Langley32352ad2014-10-14 22:31:007786 AssertTwoDistinctSessionsInserted(d.data_received());
[email protected]316c1e5e2012-09-12 15:17:447787 }
7788}
7789
davidben8ecc3072014-09-03 23:19:097790#if defined(OS_WIN)
7791
7792namespace {
7793
7794bool IsECDSACipherSuite(uint16_t cipher_suite) {
7795 const char* key_exchange;
7796 const char* cipher;
7797 const char* mac;
7798 bool is_aead;
7799 SSLCipherSuiteToStrings(&key_exchange, &cipher, &mac, &is_aead, cipher_suite);
7800 return std::string(key_exchange).find("ECDSA") != std::string::npos;
7801}
7802
7803} // namespace
7804
7805// Test that ECDSA is disabled on Windows XP, where ECDSA certificates cannot be
7806// verified.
davidben9399c952014-10-07 04:09:117807TEST_F(HTTPSRequestTest, DisableECDSAOnXP) {
davidben8ecc3072014-09-03 23:19:097808 if (base::win::GetVersion() >= base::win::VERSION_VISTA) {
7809 LOG(INFO) << "Skipping test on this version.";
7810 return;
7811 }
7812
7813 SpawnedTestServer test_server(
7814 SpawnedTestServer::TYPE_HTTPS,
7815 SpawnedTestServer::kLocalhost,
7816 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7817 ASSERT_TRUE(test_server.Start());
7818
7819 TestDelegate d;
7820 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:367821 test_server.GetURL("client-cipher-list"), DEFAULT_PRIORITY, &d));
davidben8ecc3072014-09-03 23:19:097822 r->Start();
7823 EXPECT_TRUE(r->is_pending());
7824
7825 base::RunLoop().Run();
7826
7827 EXPECT_EQ(1, d.response_started_count());
7828 std::vector<std::string> lines;
7829 base::SplitString(d.data_received(), '\n', &lines);
7830
7831 for (size_t i = 0; i < lines.size(); i++) {
7832 int cipher_suite;
7833 ASSERT_TRUE(base::StringToInt(lines[i], &cipher_suite));
7834 EXPECT_FALSE(IsECDSACipherSuite(cipher_suite))
7835 << "ClientHello advertised " << cipher_suite;
7836 }
7837}
7838
7839#endif // OS_WIN
7840
Adam Langley32352ad2014-10-14 22:31:007841class TestSSLConfigService : public SSLConfigService {
7842 public:
7843 TestSSLConfigService(bool ev_enabled,
7844 bool online_rev_checking,
7845 bool rev_checking_required_local_anchors)
7846 : ev_enabled_(ev_enabled),
7847 online_rev_checking_(online_rev_checking),
7848 rev_checking_required_local_anchors_(
7849 rev_checking_required_local_anchors),
Adam Langleyac6f24b42014-10-31 20:24:027850 min_version_(kDefaultSSLVersionMin),
7851 fallback_min_version_(kDefaultSSLVersionFallbackMin) {}
7852
7853 void set_min_version(uint16 version) {
7854 min_version_ = version;
7855 }
Adam Langley32352ad2014-10-14 22:31:007856
7857 void set_fallback_min_version(uint16 version) {
7858 fallback_min_version_ = version;
7859 }
7860
7861 // SSLConfigService:
dchengb03027d2014-10-21 12:00:207862 void GetSSLConfig(SSLConfig* config) override {
Adam Langley32352ad2014-10-14 22:31:007863 *config = SSLConfig();
7864 config->rev_checking_enabled = online_rev_checking_;
7865 config->verify_ev_cert = ev_enabled_;
7866 config->rev_checking_required_local_anchors =
7867 rev_checking_required_local_anchors_;
7868 if (fallback_min_version_) {
7869 config->version_fallback_min = fallback_min_version_;
7870 }
Adam Langleyac6f24b42014-10-31 20:24:027871 if (min_version_) {
7872 config->version_min = min_version_;
7873 }
Adam Langley32352ad2014-10-14 22:31:007874 }
7875
7876 protected:
dchengb03027d2014-10-21 12:00:207877 ~TestSSLConfigService() override {}
Adam Langley32352ad2014-10-14 22:31:007878
7879 private:
7880 const bool ev_enabled_;
7881 const bool online_rev_checking_;
7882 const bool rev_checking_required_local_anchors_;
Adam Langleyac6f24b42014-10-31 20:24:027883 uint16 min_version_;
Adam Langley32352ad2014-10-14 22:31:007884 uint16 fallback_min_version_;
7885};
7886
7887class FallbackTestURLRequestContext : public TestURLRequestContext {
7888 public:
7889 explicit FallbackTestURLRequestContext(bool delay_initialization)
7890 : TestURLRequestContext(delay_initialization) {}
7891
7892 void set_fallback_min_version(uint16 version) {
7893 TestSSLConfigService *ssl_config_service =
7894 new TestSSLConfigService(true /* check for EV */,
7895 false /* online revocation checking */,
7896 false /* require rev. checking for local
7897 anchors */);
Adam Langleyac6f24b42014-10-31 20:24:027898 ssl_config_service->set_min_version(SSL_PROTOCOL_VERSION_SSL3);
Adam Langley32352ad2014-10-14 22:31:007899 ssl_config_service->set_fallback_min_version(version);
7900 set_ssl_config_service(ssl_config_service);
7901 }
7902};
7903
[email protected]48d2b7c52014-06-27 01:16:557904class HTTPSFallbackTest : public testing::Test {
7905 public:
Adam Langley32352ad2014-10-14 22:31:007906 HTTPSFallbackTest() : context_(true) {}
dcheng67be2b1f2014-10-27 21:47:297907 ~HTTPSFallbackTest() override {}
[email protected]48d2b7c52014-06-27 01:16:557908
7909 protected:
7910 void DoFallbackTest(const SpawnedTestServer::SSLOptions& ssl_options) {
7911 DCHECK(!request_);
Adam Langley32352ad2014-10-14 22:31:007912 context_.Init();
7913 delegate_.set_allow_certificate_errors(true);
7914
[email protected]48d2b7c52014-06-27 01:16:557915 SpawnedTestServer test_server(
7916 SpawnedTestServer::TYPE_HTTPS,
7917 ssl_options,
7918 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7919 ASSERT_TRUE(test_server.Start());
7920
davidben151423e2015-03-23 18:48:367921 request_ = context_.CreateRequest(test_server.GetURL(std::string()),
7922 DEFAULT_PRIORITY, &delegate_);
[email protected]48d2b7c52014-06-27 01:16:557923 request_->Start();
7924
7925 base::RunLoop().Run();
7926 }
7927
Adam Langley32352ad2014-10-14 22:31:007928 void set_fallback_min_version(uint16 version) {
7929 context_.set_fallback_min_version(version);
7930 }
7931
[email protected]48d2b7c52014-06-27 01:16:557932 void ExpectConnection(int version) {
7933 EXPECT_EQ(1, delegate_.response_started_count());
7934 EXPECT_NE(0, delegate_.bytes_received());
7935 EXPECT_EQ(version, SSLConnectionStatusToVersion(
7936 request_->ssl_info().connection_status));
7937 EXPECT_TRUE(request_->ssl_info().connection_status &
7938 SSL_CONNECTION_VERSION_FALLBACK);
7939 }
7940
7941 void ExpectFailure(int error) {
7942 EXPECT_EQ(1, delegate_.response_started_count());
7943 EXPECT_FALSE(request_->status().is_success());
7944 EXPECT_EQ(URLRequestStatus::FAILED, request_->status().status());
7945 EXPECT_EQ(error, request_->status().error());
7946 }
7947
7948 private:
7949 TestDelegate delegate_;
Adam Langley32352ad2014-10-14 22:31:007950 FallbackTestURLRequestContext context_;
[email protected]48d2b7c52014-06-27 01:16:557951 scoped_ptr<URLRequest> request_;
7952};
7953
7954// Tests TLSv1.1 -> TLSv1 fallback. Verifies that we don't fall back more
7955// than necessary.
7956TEST_F(HTTPSFallbackTest, TLSv1Fallback) {
7957 SpawnedTestServer::SSLOptions ssl_options(
7958 SpawnedTestServer::SSLOptions::CERT_OK);
7959 ssl_options.tls_intolerant =
7960 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1;
7961
7962 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
7963 ExpectConnection(SSL_CONNECTION_VERSION_TLS1);
7964}
7965
7966// This test is disabled on Android because the remote test server doesn't cause
7967// a TCP reset.
7968#if !defined(OS_ANDROID)
7969// Tests fallback to TLS 1.0 on connection reset.
7970TEST_F(HTTPSFallbackTest, TLSv1FallbackReset) {
7971 SpawnedTestServer::SSLOptions ssl_options(
7972 SpawnedTestServer::SSLOptions::CERT_OK);
7973 ssl_options.tls_intolerant =
7974 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1;
7975 ssl_options.tls_intolerance_type =
7976 SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_RESET;
7977
7978 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
7979 ExpectConnection(SSL_CONNECTION_VERSION_TLS1);
7980}
7981#endif // !OS_ANDROID
7982
[email protected]12833302014-07-02 01:57:317983// Tests that we don't fallback on handshake failure with servers that implement
7984// TLS_FALLBACK_SCSV. Also ensure that the original error code is reported.
[email protected]48d2b7c52014-06-27 01:16:557985TEST_F(HTTPSFallbackTest, FallbackSCSV) {
[email protected]48d2b7c52014-06-27 01:16:557986 SpawnedTestServer::SSLOptions ssl_options(
7987 SpawnedTestServer::SSLOptions::CERT_OK);
davidbenc8dc58c2015-01-06 21:08:437988 // Configure HTTPS server to be intolerant of TLS >= 1.1 in order to trigger
[email protected]48d2b7c52014-06-27 01:16:557989 // a version fallback.
7990 ssl_options.tls_intolerant =
davidbenc8dc58c2015-01-06 21:08:437991 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1;
[email protected]48d2b7c52014-06-27 01:16:557992 // Have the server process TLS_FALLBACK_SCSV so that version fallback
7993 // connections are rejected.
7994 ssl_options.fallback_scsv_enabled = true;
7995
7996 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
7997
7998 // ERR_SSL_VERSION_OR_CIPHER_MISMATCH is how the server simulates version
7999 // intolerance. If the fallback SCSV is processed when the original error
8000 // that caused the fallback should be returned, which should be
8001 // ERR_SSL_VERSION_OR_CIPHER_MISMATCH.
8002 ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH);
8003}
8004
[email protected]12833302014-07-02 01:57:318005// Tests that we don't fallback on connection closed with servers that implement
8006// TLS_FALLBACK_SCSV. Also ensure that the original error code is reported.
[email protected]12833302014-07-02 01:57:318007TEST_F(HTTPSFallbackTest, FallbackSCSVClosed) {
[email protected]12833302014-07-02 01:57:318008 SpawnedTestServer::SSLOptions ssl_options(
8009 SpawnedTestServer::SSLOptions::CERT_OK);
davidbenc8dc58c2015-01-06 21:08:438010 // Configure HTTPS server to be intolerant of TLS >= 1.1 in order to trigger
[email protected]12833302014-07-02 01:57:318011 // a version fallback.
8012 ssl_options.tls_intolerant =
davidbenc8dc58c2015-01-06 21:08:438013 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1;
[email protected]12833302014-07-02 01:57:318014 ssl_options.tls_intolerance_type =
8015 SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE;
8016 // Have the server process TLS_FALLBACK_SCSV so that version fallback
8017 // connections are rejected.
8018 ssl_options.fallback_scsv_enabled = true;
8019
8020 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
8021
8022 // The original error should be replayed on rejected fallback.
8023 ExpectFailure(ERR_CONNECTION_CLOSED);
8024}
8025
Adam Langley32352ad2014-10-14 22:31:008026// Tests that the SSLv3 fallback doesn't happen by default.
[email protected]48d2b7c52014-06-27 01:16:558027TEST_F(HTTPSFallbackTest, SSLv3Fallback) {
8028 SpawnedTestServer::SSLOptions ssl_options(
8029 SpawnedTestServer::SSLOptions::CERT_OK);
8030 ssl_options.tls_intolerant =
8031 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL;
8032
8033 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
Adam Langleyac6f24b42014-10-31 20:24:028034 ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH);
Adam Langley32352ad2014-10-14 22:31:008035}
8036
8037// Tests that the SSLv3 fallback works when explicitly enabled.
8038TEST_F(HTTPSFallbackTest, SSLv3FallbackEnabled) {
8039 SpawnedTestServer::SSLOptions ssl_options(
8040 SpawnedTestServer::SSLOptions::CERT_OK);
8041 ssl_options.tls_intolerant =
8042 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL;
8043 set_fallback_min_version(SSL_PROTOCOL_VERSION_SSL3);
8044
8045 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
[email protected]48d2b7c52014-06-27 01:16:558046 ExpectConnection(SSL_CONNECTION_VERSION_SSL3);
8047}
8048
Adam Langley32352ad2014-10-14 22:31:008049// Tests that the SSLv3 fallback triggers on closed connections when explicitly
8050// enabled.
[email protected]48d2b7c52014-06-27 01:16:558051TEST_F(HTTPSFallbackTest, SSLv3FallbackClosed) {
8052 SpawnedTestServer::SSLOptions ssl_options(
8053 SpawnedTestServer::SSLOptions::CERT_OK);
8054 ssl_options.tls_intolerant =
8055 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL;
8056 ssl_options.tls_intolerance_type =
8057 SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE;
Adam Langley32352ad2014-10-14 22:31:008058 set_fallback_min_version(SSL_PROTOCOL_VERSION_SSL3);
[email protected]48d2b7c52014-06-27 01:16:558059
8060 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
8061 ExpectConnection(SSL_CONNECTION_VERSION_SSL3);
8062}
8063
Adam Langley32352ad2014-10-14 22:31:008064// Test that SSLv3 fallback probe connections don't cause sessions to be cached.
8065TEST_F(HTTPSRequestTest, SSLv3FallbackNoCache) {
8066 SpawnedTestServer::SSLOptions ssl_options(
8067 SpawnedTestServer::SSLOptions::CERT_OK);
8068 ssl_options.tls_intolerant =
8069 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL;
8070 ssl_options.tls_intolerance_type =
8071 SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE;
8072 ssl_options.record_resume = true;
8073
8074 SpawnedTestServer test_server(
8075 SpawnedTestServer::TYPE_HTTPS,
8076 ssl_options,
8077 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
8078 ASSERT_TRUE(test_server.Start());
8079
8080 SSLClientSocket::ClearSessionCache();
8081
8082 // Make a connection that does a probe fallback to SSLv3 but fails because
8083 // SSLv3 fallback is disabled. We don't wish a session for this connection to
8084 // be inserted locally.
8085 {
8086 TestDelegate delegate;
8087 FallbackTestURLRequestContext context(true);
8088
Adam Langleyac6f24b42014-10-31 20:24:028089 context.set_fallback_min_version(SSL_PROTOCOL_VERSION_TLS1);
Adam Langley32352ad2014-10-14 22:31:008090 context.Init();
8091 scoped_ptr<URLRequest> request(context.CreateRequest(
davidben151423e2015-03-23 18:48:368092 test_server.GetURL(std::string()), DEFAULT_PRIORITY, &delegate));
Adam Langley32352ad2014-10-14 22:31:008093 request->Start();
8094
8095 base::RunLoop().Run();
8096
8097 EXPECT_EQ(1, delegate.response_started_count());
8098 EXPECT_FALSE(request->status().is_success());
8099 EXPECT_EQ(URLRequestStatus::FAILED, request->status().status());
8100 EXPECT_EQ(ERR_SSL_FALLBACK_BEYOND_MINIMUM_VERSION,
8101 request->status().error());
8102 }
8103
8104 // Now allow SSLv3 connections and request the session cache log.
8105 {
8106 TestDelegate delegate;
8107 FallbackTestURLRequestContext context(true);
8108 context.set_fallback_min_version(SSL_PROTOCOL_VERSION_SSL3);
8109
8110 context.Init();
davidben151423e2015-03-23 18:48:368111 scoped_ptr<URLRequest> request(context.CreateRequest(
8112 test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &delegate));
Adam Langley32352ad2014-10-14 22:31:008113 request->Start();
8114
8115 base::RunLoop().Run();
8116
8117 EXPECT_EQ(1, delegate.response_started_count());
8118 EXPECT_NE(0, delegate.bytes_received());
8119 EXPECT_EQ(SSL_CONNECTION_VERSION_SSL3, SSLConnectionStatusToVersion(
8120 request->ssl_info().connection_status));
8121 EXPECT_TRUE(request->ssl_info().connection_status &
8122 SSL_CONNECTION_VERSION_FALLBACK);
8123
8124 std::vector<std::string> lines;
8125 // If no sessions were cached then the server should have seen two sessions
8126 // inserted with no lookups.
8127 AssertTwoDistinctSessionsInserted(delegate.data_received());
8128 }
8129}
8130
[email protected]48d2b7c52014-06-27 01:16:558131// This test is disabled on Android because the remote test server doesn't cause
[email protected]5aea79182014-07-14 20:43:418132// a TCP reset.
8133#if !defined(OS_ANDROID)
[email protected]48d2b7c52014-06-27 01:16:558134// Tests that a reset connection does not fallback down to SSL3.
8135TEST_F(HTTPSFallbackTest, SSLv3NoFallbackReset) {
8136 SpawnedTestServer::SSLOptions ssl_options(
8137 SpawnedTestServer::SSLOptions::CERT_OK);
8138 ssl_options.tls_intolerant =
8139 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL;
8140 ssl_options.tls_intolerance_type =
8141 SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_RESET;
8142
8143 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
8144 ExpectFailure(ERR_CONNECTION_RESET);
8145}
[email protected]5aea79182014-07-14 20:43:418146#endif // !OS_ANDROID
[email protected]48d2b7c52014-06-27 01:16:558147
[email protected]a8fed1742013-12-27 02:14:248148class HTTPSSessionTest : public testing::Test {
8149 public:
8150 HTTPSSessionTest() : default_context_(true) {
[email protected]cba24642014-08-15 20:49:598151 cert_verifier_.set_default_result(OK);
[email protected]a8fed1742013-12-27 02:14:248152
8153 default_context_.set_network_delegate(&default_network_delegate_);
8154 default_context_.set_cert_verifier(&cert_verifier_);
8155 default_context_.Init();
8156 }
dcheng67be2b1f2014-10-27 21:47:298157 ~HTTPSSessionTest() override {}
[email protected]a8fed1742013-12-27 02:14:248158
8159 protected:
8160 MockCertVerifier cert_verifier_;
8161 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest.
8162 TestURLRequestContext default_context_;
8163};
8164
8165// Tests that session resumption is not attempted if an invalid certificate
8166// is presented.
8167TEST_F(HTTPSSessionTest, DontResumeSessionsForInvalidCertificates) {
8168 SpawnedTestServer::SSLOptions ssl_options;
8169 ssl_options.record_resume = true;
8170 SpawnedTestServer test_server(
8171 SpawnedTestServer::TYPE_HTTPS,
8172 ssl_options,
8173 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
8174 ASSERT_TRUE(test_server.Start());
8175
8176 SSLClientSocket::ClearSessionCache();
8177
8178 // Simulate the certificate being expired and attempt a connection.
[email protected]cba24642014-08-15 20:49:598179 cert_verifier_.set_default_result(ERR_CERT_DATE_INVALID);
[email protected]a8fed1742013-12-27 02:14:248180 {
8181 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:198182 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:368183 test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d));
[email protected]a8fed1742013-12-27 02:14:248184
[email protected]f7022f32014-08-21 16:32:198185 r->Start();
8186 EXPECT_TRUE(r->is_pending());
[email protected]a8fed1742013-12-27 02:14:248187
8188 base::RunLoop().Run();
8189
8190 EXPECT_EQ(1, d.response_started_count());
8191 }
8192
8193 reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())->
8194 CloseAllConnections();
8195
8196 // Now change the certificate to be acceptable (so that the response is
8197 // loaded), and ensure that no session id is presented to the peer.
[email protected]cba24642014-08-15 20:49:598198 cert_verifier_.set_default_result(OK);
[email protected]a8fed1742013-12-27 02:14:248199 {
8200 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:198201 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:368202 test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d));
[email protected]a8fed1742013-12-27 02:14:248203
[email protected]f7022f32014-08-21 16:32:198204 r->Start();
8205 EXPECT_TRUE(r->is_pending());
[email protected]a8fed1742013-12-27 02:14:248206
8207 base::RunLoop().Run();
8208
8209 // The response will look like;
8210 // insert abc
8211 // insert xyz
8212 //
8213 // With a newline at the end which makes the split think that there are
8214 // three lines.
8215 //
8216 // If a session was presented (eg: a bug), then the response would look
8217 // like;
8218 // insert abc
8219 // lookup abc
8220 // insert xyz
8221
8222 EXPECT_EQ(1, d.response_started_count());
Adam Langley32352ad2014-10-14 22:31:008223 AssertTwoDistinctSessionsInserted(d.data_received());
[email protected]a8fed1742013-12-27 02:14:248224 }
8225}
8226
[email protected]dffe8242012-03-20 15:14:278227// This the fingerprint of the "Testing CA" certificate used by the testserver.
8228// See net/data/ssl/certificates/ocsp-test-root.pem.
[email protected]ede03212012-09-07 12:52:268229static const SHA1HashValue kOCSPTestCertFingerprint =
[email protected]dffe8242012-03-20 15:14:278230 { { 0xf1, 0xad, 0xf6, 0xce, 0x42, 0xac, 0xe7, 0xb4, 0xf4, 0x24,
8231 0xdb, 0x1a, 0xf7, 0xa0, 0x9f, 0x09, 0xa1, 0xea, 0xf1, 0x5c } };
8232
[email protected]51523f52013-07-31 21:57:288233// This is the SHA256, SPKI hash of the "Testing CA" certificate used by the
8234// testserver.
8235static const SHA256HashValue kOCSPTestCertSPKI = { {
8236 0xee, 0xe6, 0x51, 0x2d, 0x4c, 0xfa, 0xf7, 0x3e,
8237 0x6c, 0xd8, 0xca, 0x67, 0xed, 0xb5, 0x5d, 0x49,
8238 0x76, 0xe1, 0x52, 0xa7, 0x6e, 0x0e, 0xa0, 0x74,
8239 0x09, 0x75, 0xe6, 0x23, 0x24, 0xbd, 0x1b, 0x28,
8240} };
8241
[email protected]dffe8242012-03-20 15:14:278242// This is the policy OID contained in the certificates that testserver
8243// generates.
8244static const char kOCSPTestCertPolicy[] = "1.3.6.1.4.1.11129.2.4.1";
8245
8246class HTTPSOCSPTest : public HTTPSRequestTest {
8247 public:
8248 HTTPSOCSPTest()
[email protected]ef2bf422012-05-11 03:27:098249 : context_(true),
[email protected]b6f2de32012-08-17 04:35:088250 ev_test_policy_(
8251 new ScopedTestEVPolicy(EVRootCAMetadata::GetInstance(),
8252 kOCSPTestCertFingerprint,
8253 kOCSPTestCertPolicy)) {
[email protected]a13234c2012-03-20 21:45:028254 }
8255
dcheng67be2b1f2014-10-27 21:47:298256 void SetUp() override {
[email protected]ef2bf422012-05-11 03:27:098257 SetupContext(&context_);
8258 context_.Init();
[email protected]dffe8242012-03-20 15:14:278259
[email protected]cba24642014-08-15 20:49:598260 scoped_refptr<X509Certificate> root_cert =
[email protected]3a86a712013-07-30 07:16:208261 ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem");
dchengc2e01e82014-08-27 00:24:428262 CHECK_NE(static_cast<X509Certificate*>(NULL), root_cert.get());
[email protected]90499482013-06-01 00:39:508263 test_root_.reset(new ScopedTestRoot(root_cert.get()));
[email protected]dffe8242012-03-20 15:14:278264
davidben71f35ff2015-04-17 20:54:488265#if defined(USE_NSS_CERTS) || defined(OS_IOS)
[email protected]ef2bf422012-05-11 03:27:098266 SetURLRequestContextForNSSHttpIO(&context_);
[email protected]dffe8242012-03-20 15:14:278267 EnsureNSSHttpIOInit();
8268#endif
8269 }
8270
[email protected]ce7d0cbc2013-05-03 18:57:228271 void DoConnection(const SpawnedTestServer::SSLOptions& ssl_options,
[email protected]dffe8242012-03-20 15:14:278272 CertStatus* out_cert_status) {
[email protected]924e9f92012-12-16 22:00:538273 // We always overwrite out_cert_status.
8274 *out_cert_status = 0;
[email protected]ce7d0cbc2013-05-03 18:57:228275 SpawnedTestServer test_server(
8276 SpawnedTestServer::TYPE_HTTPS,
8277 ssl_options,
8278 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]dffe8242012-03-20 15:14:278279 ASSERT_TRUE(test_server.Start());
8280
8281 TestDelegate d;
8282 d.set_allow_certificate_errors(true);
[email protected]f7022f32014-08-21 16:32:198283 scoped_ptr<URLRequest> r(context_.CreateRequest(
davidben151423e2015-03-23 18:48:368284 test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:198285 r->Start();
[email protected]dffe8242012-03-20 15:14:278286
[email protected]255620da2013-08-19 13:14:298287 base::RunLoop().Run();
[email protected]dffe8242012-03-20 15:14:278288
8289 EXPECT_EQ(1, d.response_started_count());
[email protected]f7022f32014-08-21 16:32:198290 *out_cert_status = r->ssl_info().cert_status;
[email protected]dffe8242012-03-20 15:14:278291 }
8292
dcheng67be2b1f2014-10-27 21:47:298293 ~HTTPSOCSPTest() override {
davidben71f35ff2015-04-17 20:54:488294#if defined(USE_NSS_CERTS) || defined(OS_IOS)
[email protected]dffe8242012-03-20 15:14:278295 ShutdownNSSHttpIO();
8296#endif
8297 }
8298
[email protected]a13234c2012-03-20 21:45:028299 protected:
8300 // SetupContext configures the URLRequestContext that will be used for making
8301 // connetions to testserver. This can be overridden in test subclasses for
8302 // different behaviour.
8303 virtual void SetupContext(URLRequestContext* context) {
8304 context->set_ssl_config_service(
8305 new TestSSLConfigService(true /* check for EV */,
[email protected]3a86a712013-07-30 07:16:208306 true /* online revocation checking */,
8307 false /* require rev. checking for local
8308 anchors */));
[email protected]a13234c2012-03-20 21:45:028309 }
8310
[email protected]dffe8242012-03-20 15:14:278311 scoped_ptr<ScopedTestRoot> test_root_;
[email protected]ef2bf422012-05-11 03:27:098312 TestURLRequestContext context_;
[email protected]b6f2de32012-08-17 04:35:088313 scoped_ptr<ScopedTestEVPolicy> ev_test_policy_;
[email protected]dffe8242012-03-20 15:14:278314};
8315
[email protected]a13234c2012-03-20 21:45:028316static CertStatus ExpectedCertStatusForFailedOnlineRevocationCheck() {
[email protected]05454a432012-03-20 20:04:018317#if defined(OS_WIN)
[email protected]a13234c2012-03-20 21:45:028318 // Windows can return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION but we don't
8319 // have that ability on other platforms.
8320 return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION;
8321#else
8322 return 0;
8323#endif
8324}
8325
[email protected]3a86a712013-07-30 07:16:208326// SystemSupportsHardFailRevocationChecking returns true iff the current
8327// operating system supports revocation checking and can distinguish between
8328// situations where a given certificate lacks any revocation information (eg:
8329// no CRLDistributionPoints and no OCSP Responder AuthorityInfoAccess) and when
8330// revocation information cannot be obtained (eg: the CRL was unreachable).
8331// If it does not, then tests which rely on 'hard fail' behaviour should be
8332// skipped.
8333static bool SystemSupportsHardFailRevocationChecking() {
davidben71f35ff2015-04-17 20:54:488334#if defined(OS_WIN) || defined(USE_NSS_CERTS) || defined(OS_IOS)
[email protected]3a86a712013-07-30 07:16:208335 return true;
8336#else
8337 return false;
8338#endif
8339}
8340
[email protected]a13234c2012-03-20 21:45:028341// SystemUsesChromiumEVMetadata returns true iff the current operating system
8342// uses Chromium's EV metadata (i.e. EVRootCAMetadata). If it does not, then
8343// several tests are effected because our testing EV certificate won't be
8344// recognised as EV.
8345static bool SystemUsesChromiumEVMetadata() {
[email protected]e1b2d732014-03-28 16:20:328346#if defined(USE_OPENSSL_CERTS) && !defined(OS_ANDROID)
[email protected]5c504192012-03-27 19:00:578347 // http://crbug.com/117478 - OpenSSL does not support EV validation.
8348 return false;
[email protected]e1b2d732014-03-28 16:20:328349#elif (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID)
8350 // On OS X and Android, we use the system to tell us whether a certificate is
8351 // EV or not and the system won't recognise our testing root.
[email protected]05454a432012-03-20 20:04:018352 return false;
8353#else
8354 return true;
8355#endif
8356}
8357
[email protected]b6f2de32012-08-17 04:35:088358static bool SystemSupportsOCSP() {
davidbend1fb2f12014-11-08 02:51:008359#if defined(USE_OPENSSL_CERTS)
[email protected]5c504192012-03-27 19:00:578360 // http://crbug.com/117478 - OpenSSL does not support OCSP.
8361 return false;
8362#elif defined(OS_WIN)
[email protected]a13234c2012-03-20 21:45:028363 return base::win::GetVersion() >= base::win::VERSION_VISTA;
8364#elif defined(OS_ANDROID)
8365 // TODO(jnd): http://crbug.com/117478 - EV verification is not yet supported.
8366 return false;
8367#else
8368 return true;
8369#endif
8370}
8371
davidbend1fb2f12014-11-08 02:51:008372static bool SystemSupportsOCSPStapling() {
davidben71f35ff2015-04-17 20:54:488373#if defined(USE_NSS_CERTS)
davidbend1fb2f12014-11-08 02:51:008374 return true;
8375#elif defined(OS_WIN)
8376 return base::win::GetVersion() >= base::win::VERSION_VISTA;
8377#else
8378 return false;
8379#endif
8380}
8381
[email protected]dffe8242012-03-20 15:14:278382TEST_F(HTTPSOCSPTest, Valid) {
8383 if (!SystemSupportsOCSP()) {
8384 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
8385 return;
8386 }
8387
[email protected]ce7d0cbc2013-05-03 18:57:228388 SpawnedTestServer::SSLOptions ssl_options(
8389 SpawnedTestServer::SSLOptions::CERT_AUTO);
8390 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK;
[email protected]dffe8242012-03-20 15:14:278391
[email protected]924e9f92012-12-16 22:00:538392 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:128393 DoConnection(ssl_options, &cert_status);
[email protected]dffe8242012-03-20 15:14:278394
8395 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
8396
[email protected]a13234c2012-03-20 21:45:028397 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
8398 static_cast<bool>(cert_status & CERT_STATUS_IS_EV));
[email protected]dffe8242012-03-20 15:14:278399
8400 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
8401}
8402
8403TEST_F(HTTPSOCSPTest, Revoked) {
8404 if (!SystemSupportsOCSP()) {
8405 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
8406 return;
8407 }
8408
[email protected]ce7d0cbc2013-05-03 18:57:228409 SpawnedTestServer::SSLOptions ssl_options(
8410 SpawnedTestServer::SSLOptions::CERT_AUTO);
8411 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED;
[email protected]dffe8242012-03-20 15:14:278412
8413 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:128414 DoConnection(ssl_options, &cert_status);
[email protected]dffe8242012-03-20 15:14:278415
[email protected]a592c0432012-12-01 18:10:298416#if !(defined(OS_MACOSX) && !defined(OS_IOS))
[email protected]dffe8242012-03-20 15:14:278417 // Doesn't pass on OS X yet for reasons that need to be investigated.
8418 EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS);
8419#endif
8420 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
8421 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
8422}
8423
8424TEST_F(HTTPSOCSPTest, Invalid) {
8425 if (!SystemSupportsOCSP()) {
8426 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
8427 return;
8428 }
8429
[email protected]ce7d0cbc2013-05-03 18:57:228430 SpawnedTestServer::SSLOptions ssl_options(
8431 SpawnedTestServer::SSLOptions::CERT_AUTO);
8432 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
[email protected]dffe8242012-03-20 15:14:278433
[email protected]924e9f92012-12-16 22:00:538434 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:128435 DoConnection(ssl_options, &cert_status);
[email protected]dffe8242012-03-20 15:14:278436
[email protected]a13234c2012-03-20 21:45:028437 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
[email protected]dffe8242012-03-20 15:14:278438 cert_status & CERT_STATUS_ALL_ERRORS);
[email protected]dffe8242012-03-20 15:14:278439
8440 // Without a positive OCSP response, we shouldn't show the EV status.
8441 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
8442 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
8443}
[email protected]a13234c2012-03-20 21:45:028444
davidbend1fb2f12014-11-08 02:51:008445TEST_F(HTTPSOCSPTest, ValidStapled) {
8446 if (!SystemSupportsOCSPStapling()) {
8447 LOG(WARNING)
8448 << "Skipping test because system doesn't support OCSP stapling";
8449 return;
8450 }
8451
8452 SpawnedTestServer::SSLOptions ssl_options(
8453 SpawnedTestServer::SSLOptions::CERT_AUTO);
8454 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK;
8455 ssl_options.staple_ocsp_response = true;
8456 ssl_options.ocsp_server_unavailable = true;
8457
8458 CertStatus cert_status;
8459 DoConnection(ssl_options, &cert_status);
8460
8461 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
8462
8463 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
8464 static_cast<bool>(cert_status & CERT_STATUS_IS_EV));
8465
8466 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
8467}
8468
davidben6c3191b2014-11-21 22:38:048469// Disabled on NSS ports. See https://crbug.com/431716.
davidben71f35ff2015-04-17 20:54:488470#if defined(USE_NSS_CERTS)
davidben6c3191b2014-11-21 22:38:048471#define MAYBE_RevokedStapled DISABLED_RevokedStapled
8472#else
8473#define MAYBE_RevokedStapled RevokedStapled
8474#endif
8475TEST_F(HTTPSOCSPTest, MAYBE_RevokedStapled) {
davidbend1fb2f12014-11-08 02:51:008476 if (!SystemSupportsOCSPStapling()) {
8477 LOG(WARNING)
8478 << "Skipping test because system doesn't support OCSP stapling";
8479 return;
8480 }
8481
8482 SpawnedTestServer::SSLOptions ssl_options(
8483 SpawnedTestServer::SSLOptions::CERT_AUTO);
8484 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED;
8485 ssl_options.staple_ocsp_response = true;
8486 ssl_options.ocsp_server_unavailable = true;
8487
8488 CertStatus cert_status;
8489 DoConnection(ssl_options, &cert_status);
8490
8491 EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS);
8492 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
8493 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
8494}
8495
[email protected]3a86a712013-07-30 07:16:208496class HTTPSHardFailTest : public HTTPSOCSPTest {
8497 protected:
dchengb03027d2014-10-21 12:00:208498 void SetupContext(URLRequestContext* context) override {
[email protected]3a86a712013-07-30 07:16:208499 context->set_ssl_config_service(
8500 new TestSSLConfigService(false /* check for EV */,
8501 false /* online revocation checking */,
8502 true /* require rev. checking for local
8503 anchors */));
8504 }
8505};
8506
[email protected]3a86a712013-07-30 07:16:208507TEST_F(HTTPSHardFailTest, FailsOnOCSPInvalid) {
8508 if (!SystemSupportsOCSP()) {
8509 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
8510 return;
8511 }
8512
8513 if (!SystemSupportsHardFailRevocationChecking()) {
8514 LOG(WARNING) << "Skipping test because system doesn't support hard fail "
8515 << "revocation checking";
8516 return;
8517 }
8518
8519 SpawnedTestServer::SSLOptions ssl_options(
8520 SpawnedTestServer::SSLOptions::CERT_AUTO);
8521 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
8522
8523 CertStatus cert_status;
8524 DoConnection(ssl_options, &cert_status);
8525
8526 EXPECT_EQ(CERT_STATUS_REVOKED,
8527 cert_status & CERT_STATUS_REVOKED);
8528
8529 // Without a positive OCSP response, we shouldn't show the EV status.
8530 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
8531}
8532
[email protected]a13234c2012-03-20 21:45:028533class HTTPSEVCRLSetTest : public HTTPSOCSPTest {
8534 protected:
dchengb03027d2014-10-21 12:00:208535 void SetupContext(URLRequestContext* context) override {
[email protected]a13234c2012-03-20 21:45:028536 context->set_ssl_config_service(
8537 new TestSSLConfigService(true /* check for EV */,
[email protected]3a86a712013-07-30 07:16:208538 false /* online revocation checking */,
8539 false /* require rev. checking for local
8540 anchors */));
[email protected]a13234c2012-03-20 21:45:028541 }
8542};
8543
8544TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndInvalidOCSP) {
8545 if (!SystemSupportsOCSP()) {
8546 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
8547 return;
8548 }
8549
[email protected]ce7d0cbc2013-05-03 18:57:228550 SpawnedTestServer::SSLOptions ssl_options(
8551 SpawnedTestServer::SSLOptions::CERT_AUTO);
8552 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
[email protected]a13234c2012-03-20 21:45:028553 SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>());
8554
[email protected]924e9f92012-12-16 22:00:538555 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:128556 DoConnection(ssl_options, &cert_status);
[email protected]a13234c2012-03-20 21:45:028557
8558 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
8559 cert_status & CERT_STATUS_ALL_ERRORS);
8560
8561 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
[email protected]b6f2de32012-08-17 04:35:088562 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
8563 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
[email protected]a13234c2012-03-20 21:45:028564}
8565
[email protected]be0fff62013-08-29 23:37:488566TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndRevokedOCSP) {
8567 if (!SystemSupportsOCSP()) {
8568 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
8569 return;
8570 }
8571
8572 SpawnedTestServer::SSLOptions ssl_options(
8573 SpawnedTestServer::SSLOptions::CERT_AUTO);
8574 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED;
8575 SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>());
8576
8577 CertStatus cert_status;
8578 DoConnection(ssl_options, &cert_status);
8579
8580 // Currently only works for Windows. When using NSS or OS X, it's not
8581 // possible to determine whether the check failed because of actual
8582 // revocation or because there was an OCSP failure.
8583#if defined(OS_WIN)
8584 EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS);
8585#else
8586 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
8587#endif
8588
8589 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
8590 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
8591 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
8592}
8593
[email protected]a13234c2012-03-20 21:45:028594TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndGoodOCSP) {
8595 if (!SystemSupportsOCSP()) {
8596 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
8597 return;
8598 }
8599
[email protected]ce7d0cbc2013-05-03 18:57:228600 SpawnedTestServer::SSLOptions ssl_options(
8601 SpawnedTestServer::SSLOptions::CERT_AUTO);
8602 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK;
[email protected]a13234c2012-03-20 21:45:028603 SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>());
8604
8605 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:128606 DoConnection(ssl_options, &cert_status);
[email protected]a13234c2012-03-20 21:45:028607
8608 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
8609
8610 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
8611 static_cast<bool>(cert_status & CERT_STATUS_IS_EV));
[email protected]b6f2de32012-08-17 04:35:088612 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
8613 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
[email protected]a13234c2012-03-20 21:45:028614}
8615
8616TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSet) {
8617 if (!SystemSupportsOCSP()) {
8618 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
8619 return;
8620 }
8621
[email protected]ce7d0cbc2013-05-03 18:57:228622 SpawnedTestServer::SSLOptions ssl_options(
8623 SpawnedTestServer::SSLOptions::CERT_AUTO);
8624 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
[email protected]a13234c2012-03-20 21:45:028625 SSLConfigService::SetCRLSet(
8626 scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting()));
8627
8628 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:128629 DoConnection(ssl_options, &cert_status);
[email protected]a13234c2012-03-20 21:45:028630
8631 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
8632 cert_status & CERT_STATUS_ALL_ERRORS);
8633
8634 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
[email protected]b6f2de32012-08-17 04:35:088635 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
8636 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
[email protected]a13234c2012-03-20 21:45:028637}
8638
[email protected]51523f52013-07-31 21:57:288639TEST_F(HTTPSEVCRLSetTest, FreshCRLSetCovered) {
8640 if (!SystemSupportsOCSP()) {
8641 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
8642 return;
8643 }
8644
8645 SpawnedTestServer::SSLOptions ssl_options(
8646 SpawnedTestServer::SSLOptions::CERT_AUTO);
8647 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
8648 SSLConfigService::SetCRLSet(
8649 scoped_refptr<CRLSet>(CRLSet::ForTesting(
8650 false, &kOCSPTestCertSPKI, "")));
8651
8652 CertStatus cert_status;
8653 DoConnection(ssl_options, &cert_status);
8654
8655 // With a fresh CRLSet that covers the issuing certificate, we shouldn't do a
8656 // revocation check for EV.
8657 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
8658 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
8659 static_cast<bool>(cert_status & CERT_STATUS_IS_EV));
8660 EXPECT_FALSE(
8661 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
8662}
8663
8664TEST_F(HTTPSEVCRLSetTest, FreshCRLSetNotCovered) {
8665 if (!SystemSupportsOCSP()) {
8666 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
8667 return;
8668 }
8669
[email protected]ce7d0cbc2013-05-03 18:57:228670 SpawnedTestServer::SSLOptions ssl_options(
8671 SpawnedTestServer::SSLOptions::CERT_AUTO);
8672 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
[email protected]a13234c2012-03-20 21:45:028673 SSLConfigService::SetCRLSet(
8674 scoped_refptr<CRLSet>(CRLSet::EmptyCRLSetForTesting()));
8675
[email protected]51523f52013-07-31 21:57:288676 CertStatus cert_status = 0;
[email protected]295e5cd2012-08-23 01:05:128677 DoConnection(ssl_options, &cert_status);
[email protected]a13234c2012-03-20 21:45:028678
[email protected]51523f52013-07-31 21:57:288679 // Even with a fresh CRLSet, we should still do online revocation checks when
8680 // the certificate chain isn't covered by the CRLSet, which it isn't in this
8681 // test.
8682 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
8683 cert_status & CERT_STATUS_ALL_ERRORS);
[email protected]a13234c2012-03-20 21:45:028684
[email protected]51523f52013-07-31 21:57:288685 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
[email protected]a13234c2012-03-20 21:45:028686 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
[email protected]51523f52013-07-31 21:57:288687 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
[email protected]a13234c2012-03-20 21:45:028688}
8689
[email protected]b6f2de32012-08-17 04:35:088690TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSetAndRevokedNonEVCert) {
8691 // Test that when EV verification is requested, but online revocation
8692 // checking is disabled, and the leaf certificate is not in fact EV, that
8693 // no revocation checking actually happens.
8694 if (!SystemSupportsOCSP()) {
8695 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
8696 return;
8697 }
8698
8699 // Unmark the certificate's OID as EV, which should disable revocation
8700 // checking (as per the user preference)
8701 ev_test_policy_.reset();
8702
[email protected]ce7d0cbc2013-05-03 18:57:228703 SpawnedTestServer::SSLOptions ssl_options(
8704 SpawnedTestServer::SSLOptions::CERT_AUTO);
8705 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED;
[email protected]b6f2de32012-08-17 04:35:088706 SSLConfigService::SetCRLSet(
8707 scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting()));
8708
8709 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:128710 DoConnection(ssl_options, &cert_status);
[email protected]b6f2de32012-08-17 04:35:088711
8712 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
8713
8714 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
8715 EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
8716}
8717
[email protected]a13234c2012-03-20 21:45:028718class HTTPSCRLSetTest : public HTTPSOCSPTest {
8719 protected:
dchengb03027d2014-10-21 12:00:208720 void SetupContext(URLRequestContext* context) override {
[email protected]a13234c2012-03-20 21:45:028721 context->set_ssl_config_service(
8722 new TestSSLConfigService(false /* check for EV */,
[email protected]3a86a712013-07-30 07:16:208723 false /* online revocation checking */,
8724 false /* require rev. checking for local
8725 anchors */));
[email protected]a13234c2012-03-20 21:45:028726 }
8727};
8728
8729TEST_F(HTTPSCRLSetTest, ExpiredCRLSet) {
[email protected]ce7d0cbc2013-05-03 18:57:228730 SpawnedTestServer::SSLOptions ssl_options(
8731 SpawnedTestServer::SSLOptions::CERT_AUTO);
8732 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
[email protected]a13234c2012-03-20 21:45:028733 SSLConfigService::SetCRLSet(
8734 scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting()));
8735
[email protected]924e9f92012-12-16 22:00:538736 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:128737 DoConnection(ssl_options, &cert_status);
[email protected]a13234c2012-03-20 21:45:028738
8739 // If we're not trying EV verification then, even if the CRLSet has expired,
8740 // we don't fall back to online revocation checks.
8741 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
8742 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
8743 EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
8744}
[email protected]51523f52013-07-31 21:57:288745
8746TEST_F(HTTPSCRLSetTest, CRLSetRevoked) {
[email protected]591cffcd2014-08-18 20:02:308747#if defined(OS_ANDROID)
[email protected]51523f52013-07-31 21:57:288748 LOG(WARNING) << "Skipping test because system doesn't support CRLSets";
8749 return;
8750#endif
8751
8752 SpawnedTestServer::SSLOptions ssl_options(
8753 SpawnedTestServer::SSLOptions::CERT_AUTO);
8754 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK;
8755 ssl_options.cert_serial = 10;
8756 SSLConfigService::SetCRLSet(
8757 scoped_refptr<CRLSet>(CRLSet::ForTesting(
8758 false, &kOCSPTestCertSPKI, "\x0a")));
8759
8760 CertStatus cert_status = 0;
8761 DoConnection(ssl_options, &cert_status);
8762
8763 // If the certificate is recorded as revoked in the CRLSet, that should be
8764 // reflected without online revocation checking.
8765 EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS);
8766 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
8767 EXPECT_FALSE(
8768 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
8769}
[email protected]316c1e5e2012-09-12 15:17:448770#endif // !defined(OS_IOS)
[email protected]dffe8242012-03-20 15:14:278771
[email protected]316c1e5e2012-09-12 15:17:448772#if !defined(DISABLE_FTP_SUPPORT)
[email protected]b89ca032009-08-31 21:41:318773class URLRequestTestFTP : public URLRequestTest {
[email protected]95409e12010-08-17 20:07:118774 public:
[email protected]d9fca99a2012-02-24 16:16:208775 URLRequestTestFTP()
[email protected]ce7d0cbc2013-05-03 18:57:228776 : test_server_(SpawnedTestServer::TYPE_FTP, SpawnedTestServer::kLocalhost,
[email protected]6cdfd7f2013-02-08 20:40:158777 base::FilePath()) {
[email protected]95409e12010-08-17 20:07:118778 }
8779
[email protected]b89ca032009-08-31 21:41:318780 protected:
[email protected]ce7d0cbc2013-05-03 18:57:228781 SpawnedTestServer test_server_;
[email protected]b89ca032009-08-31 21:41:318782};
8783
[email protected]d2a133182012-08-05 16:44:088784// Make sure an FTP request using an unsafe ports fails.
[email protected]f2f31b32013-01-16 23:24:098785TEST_F(URLRequestTestFTP, UnsafePort) {
[email protected]d2a133182012-08-05 16:44:088786 ASSERT_TRUE(test_server_.Start());
8787
[email protected]9d5730b2012-08-24 17:42:498788 URLRequestJobFactoryImpl job_factory;
[email protected]e0f35c92013-05-08 16:04:348789 FtpNetworkLayer ftp_transaction_factory(default_context_.host_resolver());
[email protected]d2a133182012-08-05 16:44:088790
8791 GURL url("ftp://127.0.0.1:7");
[email protected]d2a133182012-08-05 16:44:088792 job_factory.SetProtocolHandler(
8793 "ftp",
[email protected]e0f35c92013-05-08 16:04:348794 new FtpProtocolHandler(&ftp_transaction_factory));
[email protected]d2a133182012-08-05 16:44:088795 default_context_.set_job_factory(&job_factory);
8796
8797 TestDelegate d;
8798 {
davidben151423e2015-03-23 18:48:368799 scoped_ptr<URLRequest> r(
8800 default_context_.CreateRequest(url, DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:198801 r->Start();
8802 EXPECT_TRUE(r->is_pending());
[email protected]d2a133182012-08-05 16:44:088803
[email protected]255620da2013-08-19 13:14:298804 base::RunLoop().Run();
[email protected]d2a133182012-08-05 16:44:088805
[email protected]f7022f32014-08-21 16:32:198806 EXPECT_FALSE(r->is_pending());
8807 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
8808 EXPECT_EQ(ERR_UNSAFE_PORT, r->status().error());
[email protected]d2a133182012-08-05 16:44:088809 }
8810}
8811
[email protected]5accf7332009-11-24 03:41:388812// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:128813TEST_F(URLRequestTestFTP, DISABLED_FTPDirectoryListing) {
[email protected]95409e12010-08-17 20:07:118814 ASSERT_TRUE(test_server_.Start());
8815
[email protected]a25e90e2009-09-09 17:05:378816 TestDelegate d;
8817 {
[email protected]f7022f32014-08-21 16:32:198818 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:368819 test_server_.GetURL("/"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:198820 r->Start();
8821 EXPECT_TRUE(r->is_pending());
[email protected]a25e90e2009-09-09 17:05:378822
[email protected]255620da2013-08-19 13:14:298823 base::RunLoop().Run();
[email protected]a25e90e2009-09-09 17:05:378824
[email protected]f7022f32014-08-21 16:32:198825 EXPECT_FALSE(r->is_pending());
[email protected]a25e90e2009-09-09 17:05:378826 EXPECT_EQ(1, d.response_started_count());
8827 EXPECT_FALSE(d.received_data_before_response());
8828 EXPECT_LT(0, d.bytes_received());
[email protected]6d81b482011-02-22 19:47:198829 EXPECT_EQ(test_server_.host_port_pair().host(),
[email protected]f7022f32014-08-21 16:32:198830 r->GetSocketAddress().host());
[email protected]6d81b482011-02-22 19:47:198831 EXPECT_EQ(test_server_.host_port_pair().port(),
[email protected]f7022f32014-08-21 16:32:198832 r->GetSocketAddress().port());
[email protected]a25e90e2009-09-09 17:05:378833 }
8834}
8835
[email protected]7df70012010-02-04 00:09:558836// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:128837TEST_F(URLRequestTestFTP, DISABLED_FTPGetTestAnonymous) {
[email protected]95409e12010-08-17 20:07:118838 ASSERT_TRUE(test_server_.Start());
8839
[email protected]6cdfd7f2013-02-08 20:40:158840 base::FilePath app_path;
[email protected]dd265012009-01-08 20:45:278841 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:028842 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:278843 TestDelegate d;
8844 {
[email protected]f7022f32014-08-21 16:32:198845 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:368846 test_server_.GetURL("/LICENSE"), DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:198847 r->Start();
8848 EXPECT_TRUE(r->is_pending());
[email protected]dd265012009-01-08 20:45:278849
[email protected]255620da2013-08-19 13:14:298850 base::RunLoop().Run();
[email protected]dd265012009-01-08 20:45:278851
8852 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:498853 base::GetFileSize(app_path, &file_size);
[email protected]dd265012009-01-08 20:45:278854
[email protected]f7022f32014-08-21 16:32:198855 EXPECT_FALSE(r->is_pending());
[email protected]dd265012009-01-08 20:45:278856 EXPECT_EQ(1, d.response_started_count());
8857 EXPECT_FALSE(d.received_data_before_response());
8858 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
[email protected]6d81b482011-02-22 19:47:198859 EXPECT_EQ(test_server_.host_port_pair().host(),
[email protected]f7022f32014-08-21 16:32:198860 r->GetSocketAddress().host());
[email protected]6d81b482011-02-22 19:47:198861 EXPECT_EQ(test_server_.host_port_pair().port(),
[email protected]f7022f32014-08-21 16:32:198862 r->GetSocketAddress().port());
[email protected]dd265012009-01-08 20:45:278863 }
8864}
8865
[email protected]e9ecbd12009-12-20 18:44:408866// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:128867TEST_F(URLRequestTestFTP, DISABLED_FTPGetTest) {
[email protected]95409e12010-08-17 20:07:118868 ASSERT_TRUE(test_server_.Start());
8869
[email protected]6cdfd7f2013-02-08 20:40:158870 base::FilePath app_path;
[email protected]dd265012009-01-08 20:45:278871 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:028872 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:278873 TestDelegate d;
8874 {
[email protected]f7022f32014-08-21 16:32:198875 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
[email protected]95409e12010-08-17 20:07:118876 test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", "chrome"),
davidben151423e2015-03-23 18:48:368877 DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:198878 r->Start();
8879 EXPECT_TRUE(r->is_pending());
[email protected]dd265012009-01-08 20:45:278880
[email protected]255620da2013-08-19 13:14:298881 base::RunLoop().Run();
[email protected]dd265012009-01-08 20:45:278882
8883 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:498884 base::GetFileSize(app_path, &file_size);
[email protected]dd265012009-01-08 20:45:278885
[email protected]f7022f32014-08-21 16:32:198886 EXPECT_FALSE(r->is_pending());
[email protected]6d81b482011-02-22 19:47:198887 EXPECT_EQ(test_server_.host_port_pair().host(),
[email protected]f7022f32014-08-21 16:32:198888 r->GetSocketAddress().host());
[email protected]6d81b482011-02-22 19:47:198889 EXPECT_EQ(test_server_.host_port_pair().port(),
[email protected]f7022f32014-08-21 16:32:198890 r->GetSocketAddress().port());
[email protected]dd265012009-01-08 20:45:278891 EXPECT_EQ(1, d.response_started_count());
8892 EXPECT_FALSE(d.received_data_before_response());
8893 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
[email protected]58e32bb2013-01-21 18:23:258894
8895 LoadTimingInfo load_timing_info;
[email protected]f7022f32014-08-21 16:32:198896 r->GetLoadTimingInfo(&load_timing_info);
[email protected]3b23a222013-05-15 21:33:258897 TestLoadTimingNoHttpResponse(load_timing_info);
[email protected]dd265012009-01-08 20:45:278898 }
8899}
8900
[email protected]49abd652010-08-05 05:04:538901// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:128902TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPassword) {
[email protected]95409e12010-08-17 20:07:118903 ASSERT_TRUE(test_server_.Start());
8904
[email protected]6cdfd7f2013-02-08 20:40:158905 base::FilePath app_path;
[email protected]dd265012009-01-08 20:45:278906 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:028907 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:278908 TestDelegate d;
8909 {
[email protected]f7022f32014-08-21 16:32:198910 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
8911 test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome",
8912 "wrong_password"),
davidben151423e2015-03-23 18:48:368913 DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:198914 r->Start();
8915 EXPECT_TRUE(r->is_pending());
[email protected]dd265012009-01-08 20:45:278916
[email protected]255620da2013-08-19 13:14:298917 base::RunLoop().Run();
[email protected]dd265012009-01-08 20:45:278918
8919 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:498920 base::GetFileSize(app_path, &file_size);
[email protected]dd265012009-01-08 20:45:278921
[email protected]f7022f32014-08-21 16:32:198922 EXPECT_FALSE(r->is_pending());
[email protected]dd265012009-01-08 20:45:278923 EXPECT_EQ(1, d.response_started_count());
8924 EXPECT_FALSE(d.received_data_before_response());
8925 EXPECT_EQ(d.bytes_received(), 0);
8926 }
8927}
8928
[email protected]cde4e80d2009-10-16 19:58:158929// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:128930TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPasswordRestart) {
[email protected]95409e12010-08-17 20:07:118931 ASSERT_TRUE(test_server_.Start());
8932
[email protected]6cdfd7f2013-02-08 20:40:158933 base::FilePath app_path;
[email protected]8b8a197d2009-08-26 15:57:588934 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
8935 app_path = app_path.AppendASCII("LICENSE");
8936 TestDelegate d;
8937 // Set correct login credentials. The delegate will be asked for them when
8938 // the initial login with wrong credentials will fail.
[email protected]f3cf9802011-10-28 18:44:588939 d.set_credentials(AuthCredentials(kChrome, kChrome));
[email protected]8b8a197d2009-08-26 15:57:588940 {
[email protected]f7022f32014-08-21 16:32:198941 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
8942 test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome",
8943 "wrong_password"),
davidben151423e2015-03-23 18:48:368944 DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:198945 r->Start();
8946 EXPECT_TRUE(r->is_pending());
[email protected]8b8a197d2009-08-26 15:57:588947
[email protected]255620da2013-08-19 13:14:298948 base::RunLoop().Run();
[email protected]8b8a197d2009-08-26 15:57:588949
8950 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:498951 base::GetFileSize(app_path, &file_size);
[email protected]8b8a197d2009-08-26 15:57:588952
[email protected]f7022f32014-08-21 16:32:198953 EXPECT_FALSE(r->is_pending());
[email protected]8b8a197d2009-08-26 15:57:588954 EXPECT_EQ(1, d.response_started_count());
8955 EXPECT_FALSE(d.received_data_before_response());
8956 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
8957 }
8958}
8959
[email protected]49abd652010-08-05 05:04:538960// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:128961TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUser) {
[email protected]95409e12010-08-17 20:07:118962 ASSERT_TRUE(test_server_.Start());
8963
[email protected]6cdfd7f2013-02-08 20:40:158964 base::FilePath app_path;
[email protected]dd265012009-01-08 20:45:278965 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:028966 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:278967 TestDelegate d;
8968 {
davidben151423e2015-03-23 18:48:368969 scoped_ptr<URLRequest> r(
8970 default_context_.CreateRequest(test_server_.GetURLWithUserAndPassword(
8971 "/LICENSE", "wrong_user", "chrome"),
8972 DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:198973 r->Start();
8974 EXPECT_TRUE(r->is_pending());
[email protected]dd265012009-01-08 20:45:278975
[email protected]255620da2013-08-19 13:14:298976 base::RunLoop().Run();
[email protected]dd265012009-01-08 20:45:278977
8978 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:498979 base::GetFileSize(app_path, &file_size);
[email protected]dd265012009-01-08 20:45:278980
[email protected]f7022f32014-08-21 16:32:198981 EXPECT_FALSE(r->is_pending());
[email protected]dd265012009-01-08 20:45:278982 EXPECT_EQ(1, d.response_started_count());
8983 EXPECT_FALSE(d.received_data_before_response());
8984 EXPECT_EQ(d.bytes_received(), 0);
8985 }
8986}
[email protected]8b8a197d2009-08-26 15:57:588987
[email protected]cde4e80d2009-10-16 19:58:158988// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:128989TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUserRestart) {
[email protected]95409e12010-08-17 20:07:118990 ASSERT_TRUE(test_server_.Start());
8991
[email protected]6cdfd7f2013-02-08 20:40:158992 base::FilePath app_path;
[email protected]8b8a197d2009-08-26 15:57:588993 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
8994 app_path = app_path.AppendASCII("LICENSE");
8995 TestDelegate d;
8996 // Set correct login credentials. The delegate will be asked for them when
8997 // the initial login with wrong credentials will fail.
[email protected]f3cf9802011-10-28 18:44:588998 d.set_credentials(AuthCredentials(kChrome, kChrome));
[email protected]8b8a197d2009-08-26 15:57:588999 {
davidben151423e2015-03-23 18:48:369000 scoped_ptr<URLRequest> r(
9001 default_context_.CreateRequest(test_server_.GetURLWithUserAndPassword(
9002 "/LICENSE", "wrong_user", "chrome"),
9003 DEFAULT_PRIORITY, &d));
[email protected]f7022f32014-08-21 16:32:199004 r->Start();
9005 EXPECT_TRUE(r->is_pending());
[email protected]8b8a197d2009-08-26 15:57:589006
[email protected]255620da2013-08-19 13:14:299007 base::RunLoop().Run();
[email protected]8b8a197d2009-08-26 15:57:589008
9009 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:499010 base::GetFileSize(app_path, &file_size);
[email protected]8b8a197d2009-08-26 15:57:589011
[email protected]f7022f32014-08-21 16:32:199012 EXPECT_FALSE(r->is_pending());
[email protected]8b8a197d2009-08-26 15:57:589013 EXPECT_EQ(1, d.response_started_count());
9014 EXPECT_FALSE(d.received_data_before_response());
9015 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
9016 }
9017}
[email protected]60a3df52009-09-22 16:13:249018
[email protected]cde4e80d2009-10-16 19:58:159019// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:129020TEST_F(URLRequestTestFTP, DISABLED_FTPCacheURLCredentials) {
[email protected]95409e12010-08-17 20:07:119021 ASSERT_TRUE(test_server_.Start());
9022
[email protected]6cdfd7f2013-02-08 20:40:159023 base::FilePath app_path;
[email protected]60a3df52009-09-22 16:13:249024 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
9025 app_path = app_path.AppendASCII("LICENSE");
9026
9027 scoped_ptr<TestDelegate> d(new TestDelegate);
9028 {
9029 // Pass correct login identity in the URL.
[email protected]f7022f32014-08-21 16:32:199030 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
[email protected]2ca01e52013-10-31 22:05:199031 test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", "chrome"),
davidben151423e2015-03-23 18:48:369032 DEFAULT_PRIORITY, d.get()));
[email protected]f7022f32014-08-21 16:32:199033 r->Start();
9034 EXPECT_TRUE(r->is_pending());
[email protected]60a3df52009-09-22 16:13:249035
[email protected]255620da2013-08-19 13:14:299036 base::RunLoop().Run();
[email protected]60a3df52009-09-22 16:13:249037
9038 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:499039 base::GetFileSize(app_path, &file_size);
[email protected]60a3df52009-09-22 16:13:249040
[email protected]f7022f32014-08-21 16:32:199041 EXPECT_FALSE(r->is_pending());
[email protected]60a3df52009-09-22 16:13:249042 EXPECT_EQ(1, d->response_started_count());
9043 EXPECT_FALSE(d->received_data_before_response());
9044 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
9045 }
9046
9047 d.reset(new TestDelegate);
9048 {
9049 // This request should use cached identity from previous request.
[email protected]f7022f32014-08-21 16:32:199050 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:369051 test_server_.GetURL("/LICENSE"), DEFAULT_PRIORITY, d.get()));
[email protected]f7022f32014-08-21 16:32:199052 r->Start();
9053 EXPECT_TRUE(r->is_pending());
[email protected]60a3df52009-09-22 16:13:249054
[email protected]255620da2013-08-19 13:14:299055 base::RunLoop().Run();
[email protected]60a3df52009-09-22 16:13:249056
9057 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:499058 base::GetFileSize(app_path, &file_size);
[email protected]60a3df52009-09-22 16:13:249059
[email protected]f7022f32014-08-21 16:32:199060 EXPECT_FALSE(r->is_pending());
[email protected]60a3df52009-09-22 16:13:249061 EXPECT_EQ(1, d->response_started_count());
9062 EXPECT_FALSE(d->received_data_before_response());
9063 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
9064 }
9065}
9066
[email protected]cde4e80d2009-10-16 19:58:159067// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:129068TEST_F(URLRequestTestFTP, DISABLED_FTPCacheLoginBoxCredentials) {
[email protected]95409e12010-08-17 20:07:119069 ASSERT_TRUE(test_server_.Start());
9070
[email protected]6cdfd7f2013-02-08 20:40:159071 base::FilePath app_path;
[email protected]60a3df52009-09-22 16:13:249072 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
9073 app_path = app_path.AppendASCII("LICENSE");
9074
9075 scoped_ptr<TestDelegate> d(new TestDelegate);
9076 // Set correct login credentials. The delegate will be asked for them when
9077 // the initial login with wrong credentials will fail.
[email protected]f3cf9802011-10-28 18:44:589078 d->set_credentials(AuthCredentials(kChrome, kChrome));
[email protected]60a3df52009-09-22 16:13:249079 {
[email protected]f7022f32014-08-21 16:32:199080 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
9081 test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome",
9082 "wrong_password"),
davidben151423e2015-03-23 18:48:369083 DEFAULT_PRIORITY, d.get()));
[email protected]f7022f32014-08-21 16:32:199084 r->Start();
9085 EXPECT_TRUE(r->is_pending());
[email protected]60a3df52009-09-22 16:13:249086
[email protected]255620da2013-08-19 13:14:299087 base::RunLoop().Run();
[email protected]60a3df52009-09-22 16:13:249088
9089 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:499090 base::GetFileSize(app_path, &file_size);
[email protected]60a3df52009-09-22 16:13:249091
[email protected]f7022f32014-08-21 16:32:199092 EXPECT_FALSE(r->is_pending());
[email protected]60a3df52009-09-22 16:13:249093 EXPECT_EQ(1, d->response_started_count());
9094 EXPECT_FALSE(d->received_data_before_response());
9095 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
9096 }
9097
9098 // Use a new delegate without explicit credentials. The cached ones should be
9099 // used.
9100 d.reset(new TestDelegate);
9101 {
9102 // Don't pass wrong credentials in the URL, they would override valid cached
9103 // ones.
[email protected]f7022f32014-08-21 16:32:199104 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
davidben151423e2015-03-23 18:48:369105 test_server_.GetURL("/LICENSE"), DEFAULT_PRIORITY, d.get()));
[email protected]f7022f32014-08-21 16:32:199106 r->Start();
9107 EXPECT_TRUE(r->is_pending());
[email protected]60a3df52009-09-22 16:13:249108
[email protected]255620da2013-08-19 13:14:299109 base::RunLoop().Run();
[email protected]60a3df52009-09-22 16:13:249110
9111 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:499112 base::GetFileSize(app_path, &file_size);
[email protected]60a3df52009-09-22 16:13:249113
[email protected]f7022f32014-08-21 16:32:199114 EXPECT_FALSE(r->is_pending());
[email protected]60a3df52009-09-22 16:13:249115 EXPECT_EQ(1, d->response_started_count());
9116 EXPECT_FALSE(d->received_data_before_response());
9117 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
9118 }
9119}
[email protected]316c1e5e2012-09-12 15:17:449120#endif // !defined(DISABLE_FTP_SUPPORT)
[email protected]7461a402011-03-24 23:19:519121
9122} // namespace net