blob: 700b07064557a9e1b601a964f0827c708859c26d [file] [log] [blame]
[email protected]9045b8822012-01-13 20:35:351// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
[email protected]ea224582008-12-07 20:25:465#include "build/build_config.h"
6
[email protected]9396b252008-09-29 17:29:387#if defined(OS_WIN)
[email protected]d8eb84242010-09-25 02:25:068#include <windows.h>
[email protected]aa84a7e2012-03-15 21:29:069#include <shlobj.h>
[email protected]9396b252008-09-29 17:29:3810#endif
11
initial.commit586acc5fe2008-07-26 22:42:5212#include <algorithm>
13#include <string>
14
[email protected]aad63572011-05-24 20:14:3915#include "base/basictypes.h"
[email protected]218aa6a12011-09-13 17:38:3816#include "base/bind.h"
[email protected]8523ba52011-05-22 19:00:5817#include "base/compiler_specific.h"
thestigd8df0332014-09-04 06:33:2918#include "base/files/file_util.h"
[email protected]3ca8b362013-11-11 22:18:0719#include "base/files/scoped_temp_dir.h"
[email protected]34b2b002009-11-20 06:53:2820#include "base/format_macros.h"
mmenke19378d22014-09-09 04:12:5921#include "base/memory/scoped_ptr.h"
[email protected]084262c2011-12-01 21:12:4722#include "base/memory/weak_ptr.h"
[email protected]7f86564d2013-07-18 00:41:2223#include "base/message_loop/message_loop.h"
[email protected]255620da2013-08-19 13:14:2924#include "base/message_loop/message_loop_proxy.h"
initial.commit586acc5fe2008-07-26 22:42:5225#include "base/path_service.h"
[email protected]255620da2013-08-19 13:14:2926#include "base/run_loop.h"
[email protected]4dc3ad4f2013-06-11 07:15:5027#include "base/strings/string_number_conversions.h"
[email protected]d069c11a2013-04-13 00:01:5528#include "base/strings/string_piece.h"
[email protected]d778e0422013-03-06 18:10:2229#include "base/strings/string_split.h"
[email protected]7f86564d2013-07-18 00:41:2230#include "base/strings/string_util.h"
31#include "base/strings/stringprintf.h"
[email protected]750b2f3c2013-06-07 18:41:0532#include "base/strings/utf_string_conversions.h"
[email protected]58e32bb2013-01-21 18:23:2533#include "net/base/capturing_net_log.h"
mmenkecbc2b712014-10-09 20:29:0734#include "net/base/chunked_upload_data_stream.h"
35#include "net/base/elements_upload_data_stream.h"
initial.commit586acc5fe2008-07-26 22:42:5236#include "net/base/load_flags.h"
[email protected]58e32bb2013-01-21 18:23:2537#include "net/base/load_timing_info.h"
38#include "net/base/load_timing_info_test_util.h"
[email protected]d8eb84242010-09-25 02:25:0639#include "net/base/net_errors.h"
[email protected]9e743cd2010-03-16 07:03:5340#include "net/base/net_log.h"
41#include "net/base/net_log_unittest.h"
initial.commit586acc5fe2008-07-26 22:42:5242#include "net/base/net_module.h"
43#include "net/base/net_util.h"
[email protected]2ca01e52013-10-31 22:05:1944#include "net/base/request_priority.h"
[email protected]42fdb452012-11-01 12:44:4045#include "net/base/test_data_directory.h"
[email protected]f288ef02012-12-15 20:28:2846#include "net/base/upload_bytes_element_reader.h"
47#include "net/base/upload_data_stream.h"
48#include "net/base/upload_file_element_reader.h"
[email protected]6e7845ae2013-03-29 21:48:1149#include "net/cert/ev_root_ca_metadata.h"
[email protected]a8fed1742013-12-27 02:14:2450#include "net/cert/mock_cert_verifier.h"
[email protected]6e7845ae2013-03-29 21:48:1151#include "net/cert/test_root_certs.h"
[email protected]aa84a7e2012-03-15 21:29:0652#include "net/cookies/cookie_monster.h"
53#include "net/cookies/cookie_store_test_helpers.h"
initial.commit586acc5fe2008-07-26 22:42:5254#include "net/disk_cache/disk_cache.h"
[email protected]f2cb3cf2013-03-21 01:40:5355#include "net/dns/mock_host_resolver.h"
[email protected]ba2f3342009-07-30 18:08:4256#include "net/ftp/ftp_network_layer.h"
[email protected]b7572ea2013-11-26 20:16:3857#include "net/http/http_byte_range.h"
initial.commit586acc5fe2008-07-26 22:42:5258#include "net/http/http_cache.h"
59#include "net/http/http_network_layer.h"
[email protected]c3456bb2011-12-12 22:22:1960#include "net/http/http_network_session.h"
[email protected]88e6b6f32010-05-07 23:14:2561#include "net/http/http_request_headers.h"
[email protected]319d9e6f2009-02-18 19:47:2162#include "net/http/http_response_headers.h"
[email protected]e50efea2014-03-24 18:41:0063#include "net/http/http_util.h"
[email protected]dffe8242012-03-20 15:14:2764#include "net/ocsp/nss_ocsp.h"
[email protected]63de95b2008-12-10 04:11:2765#include "net/proxy/proxy_service.h"
[email protected]c3456bb2011-12-12 22:22:1966#include "net/socket/ssl_client_socket.h"
davidben8ecc3072014-09-03 23:19:0967#include "net/ssl/ssl_cipher_suite_names.h"
[email protected]536fd0b2013-03-14 17:41:5768#include "net/ssl/ssl_connection_status_flags.h"
[email protected]6e7845ae2013-03-29 21:48:1169#include "net/test/cert_test_util.h"
[email protected]89b32522013-05-07 20:04:2170#include "net/test/spawned_test_server/spawned_test_server.h"
[email protected]e0f35c92013-05-08 16:04:3471#include "net/url_request/data_protocol_handler.h"
[email protected]ee4c30d2012-11-07 15:08:4372#include "net/url_request/static_http_user_agent_settings.h"
initial.commit586acc5fe2008-07-26 22:42:5273#include "net/url_request/url_request.h"
[email protected]bcb84f8b2009-08-31 16:20:1474#include "net/url_request/url_request_http_job.h"
[email protected]9d5730b2012-08-24 17:42:4975#include "net/url_request/url_request_job_factory_impl.h"
[email protected]3c5ca8c2011-09-29 01:14:5176#include "net/url_request/url_request_redirect_job.h"
[email protected]a5c713f2009-04-16 21:05:4777#include "net/url_request/url_request_test_job.h"
[email protected]d2db0292011-01-26 20:23:4478#include "net/url_request/url_request_test_util.h"
initial.commit586acc5fe2008-07-26 22:42:5279#include "testing/gtest/include/gtest/gtest.h"
[email protected]23887f04f2008-12-02 19:20:1580#include "testing/platform_test.h"
initial.commit586acc5fe2008-07-26 22:42:5281
[email protected]02494ec2014-05-07 15:05:2982#if !defined(DISABLE_FILE_SUPPORT)
[email protected]5ecf7cb282014-05-11 01:49:5583#include "net/base/filename_util.h"
[email protected]02494ec2014-05-07 15:05:2984#include "net/url_request/file_protocol_handler.h"
85#include "net/url_request/url_request_file_dir_job.h"
86#endif
87
88#if !defined(DISABLE_FTP_SUPPORT)
89#include "net/url_request/ftp_protocol_handler.h"
90#endif
91
[email protected]dffe8242012-03-20 15:14:2792#if defined(OS_WIN)
[email protected]451fd902012-10-03 17:14:4893#include "base/win/scoped_com_initializer.h"
[email protected]aed9efb2013-04-13 01:20:5694#include "base/win/scoped_comptr.h"
[email protected]dffe8242012-03-20 15:14:2795#include "base/win/windows_version.h"
96#endif
97
[email protected]ad65a3e2013-12-25 18:18:0198using base::ASCIIToUTF16;
[email protected]e1acf6f2008-10-27 20:43:3399using base::Time;
100
[email protected]7461a402011-03-24 23:19:51101namespace net {
102
initial.commit586acc5fe2008-07-26 22:42:52103namespace {
104
[email protected]42cba2fb2013-03-29 19:58:57105const base::string16 kChrome(ASCIIToUTF16("chrome"));
106const base::string16 kSecret(ASCIIToUTF16("secret"));
107const base::string16 kUser(ASCIIToUTF16("user"));
[email protected]13c8a092010-07-29 06:15:44108
[email protected]2bba3252013-04-08 19:50:59109// Tests load timing information in the case a fresh connection was used, with
110// no proxy.
[email protected]cba24642014-08-15 20:49:59111void TestLoadTimingNotReused(const LoadTimingInfo& load_timing_info,
[email protected]58e32bb2013-01-21 18:23:25112 int connect_timing_flags) {
113 EXPECT_FALSE(load_timing_info.socket_reused);
[email protected]cba24642014-08-15 20:49:59114 EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
[email protected]58e32bb2013-01-21 18:23:25115
116 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
117 EXPECT_FALSE(load_timing_info.request_start.is_null());
118
119 EXPECT_LE(load_timing_info.request_start,
120 load_timing_info.connect_timing.connect_start);
121 ExpectConnectTimingHasTimes(load_timing_info.connect_timing,
122 connect_timing_flags);
123 EXPECT_LE(load_timing_info.connect_timing.connect_end,
124 load_timing_info.send_start);
125 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
126 EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
127
[email protected]58e32bb2013-01-21 18:23:25128 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null());
129 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null());
130}
131
[email protected]2bba3252013-04-08 19:50:59132// Same as above, but with proxy times.
133void TestLoadTimingNotReusedWithProxy(
[email protected]cba24642014-08-15 20:49:59134 const LoadTimingInfo& load_timing_info,
[email protected]2bba3252013-04-08 19:50:59135 int connect_timing_flags) {
136 EXPECT_FALSE(load_timing_info.socket_reused);
[email protected]cba24642014-08-15 20:49:59137 EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
[email protected]2bba3252013-04-08 19:50:59138
139 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
140 EXPECT_FALSE(load_timing_info.request_start.is_null());
141
142 EXPECT_LE(load_timing_info.request_start,
143 load_timing_info.proxy_resolve_start);
144 EXPECT_LE(load_timing_info.proxy_resolve_start,
145 load_timing_info.proxy_resolve_end);
146 EXPECT_LE(load_timing_info.proxy_resolve_end,
147 load_timing_info.connect_timing.connect_start);
148 ExpectConnectTimingHasTimes(load_timing_info.connect_timing,
149 connect_timing_flags);
150 EXPECT_LE(load_timing_info.connect_timing.connect_end,
151 load_timing_info.send_start);
152 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
153 EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
154}
155
156// Same as above, but with a reused socket and proxy times.
157void TestLoadTimingReusedWithProxy(
[email protected]cba24642014-08-15 20:49:59158 const LoadTimingInfo& load_timing_info) {
[email protected]2bba3252013-04-08 19:50:59159 EXPECT_TRUE(load_timing_info.socket_reused);
[email protected]cba24642014-08-15 20:49:59160 EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
[email protected]2bba3252013-04-08 19:50:59161
162 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
163 EXPECT_FALSE(load_timing_info.request_start.is_null());
164
165 ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing);
166
167 EXPECT_LE(load_timing_info.request_start,
168 load_timing_info.proxy_resolve_start);
169 EXPECT_LE(load_timing_info.proxy_resolve_start,
170 load_timing_info.proxy_resolve_end);
171 EXPECT_LE(load_timing_info.proxy_resolve_end,
172 load_timing_info.send_start);
173 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
174 EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
175}
176
[email protected]3b23a222013-05-15 21:33:25177// Tests load timing information in the case of a cache hit, when no cache
178// validation request was sent over the wire.
[email protected]e3a85452013-11-14 01:46:17179base::StringPiece TestNetResourceProvider(int key) {
180 return "header";
181}
182
183void FillBuffer(char* buffer, size_t len) {
184 static bool called = false;
185 if (!called) {
186 called = true;
187 int seed = static_cast<int>(Time::Now().ToInternalValue());
188 srand(seed);
189 }
190
191 for (size_t i = 0; i < len; i++) {
192 buffer[i] = static_cast<char>(rand());
193 if (!buffer[i])
194 buffer[i] = 'g';
195 }
196}
197
198#if !defined(OS_IOS)
[email protected]3b23a222013-05-15 21:33:25199void TestLoadTimingCacheHitNoNetwork(
[email protected]cba24642014-08-15 20:49:59200 const LoadTimingInfo& load_timing_info) {
[email protected]3b23a222013-05-15 21:33:25201 EXPECT_FALSE(load_timing_info.socket_reused);
[email protected]cba24642014-08-15 20:49:59202 EXPECT_EQ(NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
[email protected]3b23a222013-05-15 21:33:25203
204 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
205 EXPECT_FALSE(load_timing_info.request_start.is_null());
206
207 ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing);
208 EXPECT_LE(load_timing_info.request_start, load_timing_info.send_start);
209 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
210 EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
211
212 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null());
213 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null());
214}
215
216// Tests load timing in the case that there is no HTTP response. This can be
217// used to test in the case of errors or non-HTTP requests.
218void TestLoadTimingNoHttpResponse(
[email protected]cba24642014-08-15 20:49:59219 const LoadTimingInfo& load_timing_info) {
[email protected]58e32bb2013-01-21 18:23:25220 EXPECT_FALSE(load_timing_info.socket_reused);
[email protected]cba24642014-08-15 20:49:59221 EXPECT_EQ(NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
[email protected]58e32bb2013-01-21 18:23:25222
223 // Only the request times should be non-null.
224 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
225 EXPECT_FALSE(load_timing_info.request_start.is_null());
226
227 ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing);
228
229 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null());
230 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null());
231 EXPECT_TRUE(load_timing_info.send_start.is_null());
232 EXPECT_TRUE(load_timing_info.send_end.is_null());
233 EXPECT_TRUE(load_timing_info.receive_headers_end.is_null());
234}
235
[email protected]71c64f62008-11-15 04:36:51236// Do a case-insensitive search through |haystack| for |needle|.
237bool ContainsString(const std::string& haystack, const char* needle) {
238 std::string::const_iterator it =
239 std::search(haystack.begin(),
240 haystack.end(),
241 needle,
242 needle + strlen(needle),
[email protected]07f1cee2010-11-03 03:53:35243 base::CaseInsensitiveCompare<char>());
[email protected]71c64f62008-11-15 04:36:51244 return it != haystack.end();
245}
246
mmenkecbc2b712014-10-09 20:29:07247scoped_ptr<UploadDataStream> CreateSimpleUploadData(const char* data) {
[email protected]f288ef02012-12-15 20:28:28248 scoped_ptr<UploadElementReader> reader(
249 new UploadBytesElementReader(data, strlen(data)));
mmenkecbc2b712014-10-09 20:29:07250 return ElementsUploadDataStream::CreateWithReader(reader.Pass(), 0);
[email protected]195e77d2009-07-23 19:10:23251}
252
[email protected]96adadb2010-08-28 01:16:17253// Verify that the SSLInfo of a successful SSL connection has valid values.
[email protected]7461a402011-03-24 23:19:51254void CheckSSLInfo(const SSLInfo& ssl_info) {
[email protected]96adadb2010-08-28 01:16:17255 // -1 means unknown. 0 means no encryption.
256 EXPECT_GT(ssl_info.security_bits, 0);
257
258 // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated.
[email protected]7461a402011-03-24 23:19:51259 int cipher_suite = SSLConnectionStatusToCipherSuite(
[email protected]96adadb2010-08-28 01:16:17260 ssl_info.connection_status);
261 EXPECT_NE(0, cipher_suite);
262}
263
[email protected]79e1fd62013-06-20 06:50:04264void CheckFullRequestHeaders(const HttpRequestHeaders& headers,
265 const GURL& host_url) {
266 std::string sent_value;
267
268 EXPECT_TRUE(headers.GetHeader("Host", &sent_value));
269 EXPECT_EQ(GetHostAndOptionalPort(host_url), sent_value);
270
271 EXPECT_TRUE(headers.GetHeader("Connection", &sent_value));
272 EXPECT_EQ("keep-alive", sent_value);
273}
274
[email protected]ede03212012-09-07 12:52:26275bool FingerprintsEqual(const HashValueVector& a, const HashValueVector& b) {
[email protected]69d7ff442012-02-13 22:41:27276 size_t size = a.size();
277
278 if (size != b.size())
279 return false;
280
281 for (size_t i = 0; i < size; ++i) {
282 if (!a[i].Equals(b[i]))
283 return false;
284 }
285
286 return true;
287}
[email protected]e3a85452013-11-14 01:46:17288#endif // !defined(OS_IOS)
[email protected]69d7ff442012-02-13 22:41:27289
[email protected]dc5a5cf2012-09-26 02:49:30290// A network delegate that allows the user to choose a subset of request stages
291// to block in. When blocking, the delegate can do one of the following:
292// * synchronously return a pre-specified error code, or
293// * asynchronously return that value via an automatically called callback,
294// or
295// * block and wait for the user to do a callback.
296// Additionally, the user may also specify a redirect URL -- then each request
297// with the current URL different from the redirect target will be redirected
298// to that target, in the on-before-URL-request stage, independent of whether
299// the delegate blocks in ON_BEFORE_URL_REQUEST or not.
[email protected]4c76d7c2011-04-15 19:14:12300class BlockingNetworkDelegate : public TestNetworkDelegate {
301 public:
[email protected]dc5a5cf2012-09-26 02:49:30302 // Stages in which the delegate can block.
303 enum Stage {
[email protected]9045b8822012-01-13 20:35:35304 NOT_BLOCKED = 0,
305 ON_BEFORE_URL_REQUEST = 1 << 0,
306 ON_BEFORE_SEND_HEADERS = 1 << 1,
307 ON_HEADERS_RECEIVED = 1 << 2,
308 ON_AUTH_REQUIRED = 1 << 3
309 };
310
[email protected]dc5a5cf2012-09-26 02:49:30311 // Behavior during blocked stages. During other stages, just
[email protected]cba24642014-08-15 20:49:59312 // returns OK or NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION.
[email protected]dc5a5cf2012-09-26 02:49:30313 enum BlockMode {
314 SYNCHRONOUS, // No callback, returns specified return values.
315 AUTO_CALLBACK, // |this| posts a task to run the callback using the
316 // specified return codes.
317 USER_CALLBACK, // User takes care of doing a callback. |retval_| and
318 // |auth_retval_| are ignored. In every blocking stage the
319 // message loop is quit.
320 };
321
322 // Creates a delegate which does not block at all.
323 explicit BlockingNetworkDelegate(BlockMode block_mode);
324
325 // For users to trigger a callback returning |response|.
326 // Side-effects: resets |stage_blocked_for_callback_| and stored callbacks.
327 // Only call if |block_mode_| == USER_CALLBACK.
328 void DoCallback(int response);
329 void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response);
330
331 // Setters.
332 void set_retval(int retval) {
333 ASSERT_NE(USER_CALLBACK, block_mode_);
334 ASSERT_NE(ERR_IO_PENDING, retval);
335 ASSERT_NE(OK, retval);
336 retval_ = retval;
[email protected]9045b8822012-01-13 20:35:35337 }
338
[email protected]dc5a5cf2012-09-26 02:49:30339 // If |auth_retval| == AUTH_REQUIRED_RESPONSE_SET_AUTH, then
340 // |auth_credentials_| will be passed with the response.
341 void set_auth_retval(AuthRequiredResponse auth_retval) {
342 ASSERT_NE(USER_CALLBACK, block_mode_);
343 ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, auth_retval);
344 auth_retval_ = auth_retval;
345 }
346 void set_auth_credentials(const AuthCredentials& auth_credentials) {
347 auth_credentials_ = auth_credentials;
[email protected]9045b8822012-01-13 20:35:35348 }
349
[email protected]dc5a5cf2012-09-26 02:49:30350 void set_redirect_url(const GURL& url) {
351 redirect_url_ = url;
[email protected]9045b8822012-01-13 20:35:35352 }
353
[email protected]dc5a5cf2012-09-26 02:49:30354 void set_block_on(int block_on) {
355 block_on_ = block_on;
[email protected]9045b8822012-01-13 20:35:35356 }
357
[email protected]dc5a5cf2012-09-26 02:49:30358 // Allows the user to check in which state did we block.
359 Stage stage_blocked_for_callback() const {
360 EXPECT_EQ(USER_CALLBACK, block_mode_);
361 return stage_blocked_for_callback_;
[email protected]9045b8822012-01-13 20:35:35362 }
363
364 private:
[email protected]dc5a5cf2012-09-26 02:49:30365 void RunCallback(int response, const CompletionCallback& callback);
366 void RunAuthCallback(AuthRequiredResponse response,
367 const AuthCallback& callback);
368
[email protected]9045b8822012-01-13 20:35:35369 // TestNetworkDelegate implementation.
dchengb03027d2014-10-21 12:00:20370 int OnBeforeURLRequest(URLRequest* request,
371 const CompletionCallback& callback,
372 GURL* new_url) override;
[email protected]9045b8822012-01-13 20:35:35373
dchengb03027d2014-10-21 12:00:20374 int OnBeforeSendHeaders(URLRequest* request,
375 const CompletionCallback& callback,
376 HttpRequestHeaders* headers) override;
[email protected]9045b8822012-01-13 20:35:35377
dchengb03027d2014-10-21 12:00:20378 int OnHeadersReceived(
[email protected]9045b8822012-01-13 20:35:35379 URLRequest* request,
380 const CompletionCallback& callback,
[email protected]507af8f2012-10-20 00:42:32381 const HttpResponseHeaders* original_response_headers,
[email protected]5f714132014-03-26 10:41:16382 scoped_refptr<HttpResponseHeaders>* override_response_headers,
mostynbba063d6032014-10-09 11:01:13383 GURL* allowed_unsafe_redirect_url) override;
[email protected]9045b8822012-01-13 20:35:35384
dchengb03027d2014-10-21 12:00:20385 NetworkDelegate::AuthRequiredResponse OnAuthRequired(
[email protected]9045b8822012-01-13 20:35:35386 URLRequest* request,
387 const AuthChallengeInfo& auth_info,
388 const AuthCallback& callback,
mostynbba063d6032014-10-09 11:01:13389 AuthCredentials* credentials) override;
[email protected]9045b8822012-01-13 20:35:35390
[email protected]dc5a5cf2012-09-26 02:49:30391 // Resets the callbacks and |stage_blocked_for_callback_|.
392 void Reset();
[email protected]9045b8822012-01-13 20:35:35393
[email protected]dc5a5cf2012-09-26 02:49:30394 // Checks whether we should block in |stage|. If yes, returns an error code
395 // and optionally sets up callback based on |block_mode_|. If no, returns OK.
396 int MaybeBlockStage(Stage stage, const CompletionCallback& callback);
397
398 // Configuration parameters, can be adjusted by public methods:
399 const BlockMode block_mode_;
400
401 // Values returned on blocking stages when mode is SYNCHRONOUS or
402 // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING.
403 int retval_; // To be returned in non-auth stages.
404 AuthRequiredResponse auth_retval_;
405
[email protected]5f714132014-03-26 10:41:16406 GURL redirect_url_; // Used if non-empty during OnBeforeURLRequest.
[email protected]dc5a5cf2012-09-26 02:49:30407 int block_on_; // Bit mask: in which stages to block.
408
409 // |auth_credentials_| will be copied to |*target_auth_credential_| on
410 // callback.
411 AuthCredentials auth_credentials_;
412 AuthCredentials* target_auth_credentials_;
413
414 // Internal variables, not set by not the user:
415 // Last blocked stage waiting for user callback (unused if |block_mode_| !=
416 // USER_CALLBACK).
417 Stage stage_blocked_for_callback_;
418
419 // Callback objects stored during blocking stages.
[email protected]9045b8822012-01-13 20:35:35420 CompletionCallback callback_;
421 AuthCallback auth_callback_;
[email protected]dc5a5cf2012-09-26 02:49:30422
423 base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_;
424
425 DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate);
[email protected]9045b8822012-01-13 20:35:35426};
427
[email protected]dc5a5cf2012-09-26 02:49:30428BlockingNetworkDelegate::BlockingNetworkDelegate(BlockMode block_mode)
429 : block_mode_(block_mode),
430 retval_(OK),
431 auth_retval_(AUTH_REQUIRED_RESPONSE_NO_ACTION),
432 block_on_(0),
433 target_auth_credentials_(NULL),
434 stage_blocked_for_callback_(NOT_BLOCKED),
[email protected]aa249b52013-04-30 01:04:32435 weak_factory_(this) {
[email protected]dc5a5cf2012-09-26 02:49:30436}
437
438void BlockingNetworkDelegate::DoCallback(int response) {
439 ASSERT_EQ(USER_CALLBACK, block_mode_);
440 ASSERT_NE(NOT_BLOCKED, stage_blocked_for_callback_);
441 ASSERT_NE(ON_AUTH_REQUIRED, stage_blocked_for_callback_);
442 CompletionCallback callback = callback_;
443 Reset();
444 RunCallback(response, callback);
445}
446
447void BlockingNetworkDelegate::DoAuthCallback(
448 NetworkDelegate::AuthRequiredResponse response) {
449 ASSERT_EQ(USER_CALLBACK, block_mode_);
450 ASSERT_EQ(ON_AUTH_REQUIRED, stage_blocked_for_callback_);
451 AuthCallback auth_callback = auth_callback_;
452 Reset();
453 RunAuthCallback(response, auth_callback);
454}
455
456void BlockingNetworkDelegate::RunCallback(int response,
457 const CompletionCallback& callback) {
458 callback.Run(response);
459}
460
461void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response,
462 const AuthCallback& callback) {
463 if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) {
464 ASSERT_TRUE(target_auth_credentials_ != NULL);
465 *target_auth_credentials_ = auth_credentials_;
466 }
467 callback.Run(response);
468}
469
470int BlockingNetworkDelegate::OnBeforeURLRequest(
471 URLRequest* request,
472 const CompletionCallback& callback,
473 GURL* new_url) {
474 if (redirect_url_ == request->url())
475 return OK; // We've already seen this request and redirected elsewhere.
476
477 TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url);
478
479 if (!redirect_url_.is_empty())
480 *new_url = redirect_url_;
481
482 return MaybeBlockStage(ON_BEFORE_URL_REQUEST, callback);
483}
484
485int BlockingNetworkDelegate::OnBeforeSendHeaders(
486 URLRequest* request,
487 const CompletionCallback& callback,
488 HttpRequestHeaders* headers) {
489 TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers);
490
491 return MaybeBlockStage(ON_BEFORE_SEND_HEADERS, callback);
492}
493
494int BlockingNetworkDelegate::OnHeadersReceived(
495 URLRequest* request,
496 const CompletionCallback& callback,
[email protected]507af8f2012-10-20 00:42:32497 const HttpResponseHeaders* original_response_headers,
[email protected]5f714132014-03-26 10:41:16498 scoped_refptr<HttpResponseHeaders>* override_response_headers,
499 GURL* allowed_unsafe_redirect_url) {
500 TestNetworkDelegate::OnHeadersReceived(request,
501 callback,
502 original_response_headers,
503 override_response_headers,
504 allowed_unsafe_redirect_url);
[email protected]dc5a5cf2012-09-26 02:49:30505
506 return MaybeBlockStage(ON_HEADERS_RECEIVED, callback);
507}
508
509NetworkDelegate::AuthRequiredResponse BlockingNetworkDelegate::OnAuthRequired(
510 URLRequest* request,
511 const AuthChallengeInfo& auth_info,
512 const AuthCallback& callback,
513 AuthCredentials* credentials) {
514 TestNetworkDelegate::OnAuthRequired(request, auth_info, callback,
515 credentials);
516 // Check that the user has provided callback for the previous blocked stage.
517 EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_);
518
519 if ((block_on_ & ON_AUTH_REQUIRED) == 0) {
520 return AUTH_REQUIRED_RESPONSE_NO_ACTION;
521 }
522
523 target_auth_credentials_ = credentials;
524
525 switch (block_mode_) {
526 case SYNCHRONOUS:
527 if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH)
528 *target_auth_credentials_ = auth_credentials_;
529 return auth_retval_;
530
531 case AUTO_CALLBACK:
[email protected]2da659e2013-05-23 20:51:34532 base::MessageLoop::current()->PostTask(
[email protected]dc5a5cf2012-09-26 02:49:30533 FROM_HERE,
534 base::Bind(&BlockingNetworkDelegate::RunAuthCallback,
535 weak_factory_.GetWeakPtr(), auth_retval_, callback));
536 return AUTH_REQUIRED_RESPONSE_IO_PENDING;
537
538 case USER_CALLBACK:
539 auth_callback_ = callback;
540 stage_blocked_for_callback_ = ON_AUTH_REQUIRED;
[email protected]2da659e2013-05-23 20:51:34541 base::MessageLoop::current()->PostTask(FROM_HERE,
542 base::MessageLoop::QuitClosure());
[email protected]dc5a5cf2012-09-26 02:49:30543 return AUTH_REQUIRED_RESPONSE_IO_PENDING;
544 }
545 NOTREACHED();
546 return AUTH_REQUIRED_RESPONSE_NO_ACTION; // Dummy value.
547}
548
549void BlockingNetworkDelegate::Reset() {
550 EXPECT_NE(NOT_BLOCKED, stage_blocked_for_callback_);
551 stage_blocked_for_callback_ = NOT_BLOCKED;
552 callback_.Reset();
553 auth_callback_.Reset();
554}
555
556int BlockingNetworkDelegate::MaybeBlockStage(
557 BlockingNetworkDelegate::Stage stage,
558 const CompletionCallback& callback) {
559 // Check that the user has provided callback for the previous blocked stage.
560 EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_);
561
562 if ((block_on_ & stage) == 0) {
563 return OK;
564 }
565
566 switch (block_mode_) {
567 case SYNCHRONOUS:
568 EXPECT_NE(OK, retval_);
569 return retval_;
570
571 case AUTO_CALLBACK:
[email protected]2da659e2013-05-23 20:51:34572 base::MessageLoop::current()->PostTask(
[email protected]dc5a5cf2012-09-26 02:49:30573 FROM_HERE,
574 base::Bind(&BlockingNetworkDelegate::RunCallback,
575 weak_factory_.GetWeakPtr(), retval_, callback));
576 return ERR_IO_PENDING;
577
578 case USER_CALLBACK:
579 callback_ = callback;
580 stage_blocked_for_callback_ = stage;
[email protected]2da659e2013-05-23 20:51:34581 base::MessageLoop::current()->PostTask(FROM_HERE,
582 base::MessageLoop::QuitClosure());
[email protected]dc5a5cf2012-09-26 02:49:30583 return ERR_IO_PENDING;
584 }
585 NOTREACHED();
586 return 0;
587}
588
[email protected]d5a4dd62012-05-23 01:41:04589class TestURLRequestContextWithProxy : public TestURLRequestContext {
590 public:
591 // Does not own |delegate|.
592 TestURLRequestContextWithProxy(const std::string& proxy,
593 NetworkDelegate* delegate)
594 : TestURLRequestContext(true) {
595 context_storage_.set_proxy_service(ProxyService::CreateFixed(proxy));
596 set_network_delegate(delegate);
597 Init();
598 }
dchengb03027d2014-10-21 12:00:20599 ~TestURLRequestContextWithProxy() override {}
[email protected]d5a4dd62012-05-23 01:41:04600};
601
602} // namespace
603
[email protected]a592c0432012-12-01 18:10:29604// Inherit PlatformTest since we require the autorelease pool on Mac OS X.
[email protected]7a0bb4bf2008-11-19 21:41:48605class URLRequestTest : public PlatformTest {
[email protected]abb26092010-11-11 22:19:00606 public:
[email protected]ef2bf422012-05-11 03:27:09607 URLRequestTest() : default_context_(true) {
608 default_context_.set_network_delegate(&default_network_delegate_);
[email protected]58e32bb2013-01-21 18:23:25609 default_context_.set_net_log(&net_log_);
[email protected]e0f35c92013-05-08 16:04:34610 job_factory_.SetProtocolHandler("data", new DataProtocolHandler);
[email protected]02494ec2014-05-07 15:05:29611#if !defined(DISABLE_FILE_SUPPORT)
[email protected]255620da2013-08-19 13:14:29612 job_factory_.SetProtocolHandler(
613 "file", new FileProtocolHandler(base::MessageLoopProxy::current()));
[email protected]02494ec2014-05-07 15:05:29614#endif
[email protected]e0f35c92013-05-08 16:04:34615 default_context_.set_job_factory(&job_factory_);
[email protected]ef2bf422012-05-11 03:27:09616 default_context_.Init();
[email protected]87a09a92011-07-14 15:50:50617 }
[email protected]e4034ad2013-09-20 08:36:18618 virtual ~URLRequestTest() {
619 // URLRequestJobs may post clean-up tasks on destruction.
620 base::RunLoop().RunUntilIdle();
621 }
[email protected]87a09a92011-07-14 15:50:50622
[email protected]3c5ca8c2011-09-29 01:14:51623 // Adds the TestJobInterceptor to the default context.
624 TestJobInterceptor* AddTestInterceptor() {
[email protected]f53b4802012-12-20 17:04:23625 TestJobInterceptor* protocol_handler_ = new TestJobInterceptor();
[email protected]e0f35c92013-05-08 16:04:34626 job_factory_.SetProtocolHandler("http", NULL);
627 job_factory_.SetProtocolHandler("http", protocol_handler_);
[email protected]f53b4802012-12-20 17:04:23628 return protocol_handler_;
[email protected]3c5ca8c2011-09-29 01:14:51629 }
630
[email protected]87a09a92011-07-14 15:50:50631 protected:
[email protected]58e32bb2013-01-21 18:23:25632 CapturingNetLog net_log_;
[email protected]ceefd7fd2012-11-29 00:36:24633 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest.
[email protected]e0f35c92013-05-08 16:04:34634 URLRequestJobFactoryImpl job_factory_;
[email protected]ef2bf422012-05-11 03:27:09635 TestURLRequestContext default_context_;
[email protected]7a0bb4bf2008-11-19 21:41:48636};
637
[email protected]316c1e5e2012-09-12 15:17:44638TEST_F(URLRequestTest, AboutBlankTest) {
639 TestDelegate d;
640 {
[email protected]f7022f32014-08-21 16:32:19641 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
642 GURL("about:blank"), DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:44643
[email protected]f7022f32014-08-21 16:32:19644 r->Start();
645 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44646
[email protected]255620da2013-08-19 13:14:29647 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:44648
[email protected]f7022f32014-08-21 16:32:19649 EXPECT_TRUE(!r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44650 EXPECT_FALSE(d.received_data_before_response());
651 EXPECT_EQ(d.bytes_received(), 0);
[email protected]f7022f32014-08-21 16:32:19652 EXPECT_EQ("", r->GetSocketAddress().host());
653 EXPECT_EQ(0, r->GetSocketAddress().port());
[email protected]79e1fd62013-06-20 06:50:04654
655 HttpRequestHeaders headers;
[email protected]f7022f32014-08-21 16:32:19656 EXPECT_FALSE(r->GetFullRequestHeaders(&headers));
[email protected]316c1e5e2012-09-12 15:17:44657 }
658}
659
660TEST_F(URLRequestTest, DataURLImageTest) {
661 TestDelegate d;
662 {
663 // Use our nice little Chrome logo.
[email protected]f7022f32014-08-21 16:32:19664 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
[email protected]2ca01e52013-10-31 22:05:19665 GURL(
[email protected]316c1e5e2012-09-12 15:17:44666 "data:image/png;base64,"
667 "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUBBjG3"
668 "w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQxIEVxitD"
669 "5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI5MpCVdW0gO7t"
670 "vNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKzMx1+fg9bAgK6zHq9"
671 "cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7CkIMDxQpF7r/MWq12UctI1"
672 "dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0WYUqt57yWf2McHTObYPbVD+Z"
673 "wbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2HQQcm43XwmtoYM6vVKleh0uoWvnzW"
674 "3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nkDFHF9ZS+uYVjRUasMeHUmyLYtcklTvzW"
675 "GFZnNOXczThvpKIzjcahSqIzkvDLayDq6D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTb"
676 "xXX1T5xsV9tm9r4TQwHLiZw/pdDZJea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5"
677 "MrVvMzNaI3+ERHfrFzPKQukrQGI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gV"
678 "Md6q5c8GdosynKmSeRuGzpjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq"
679 "/uxxZKzNiZFGD0wRC3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7F"
680 "Ux1qLnV7MGF40smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufB"
681 "JbsysjjW4kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJM"
682 "nlm2O34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfm"
683 "moRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV5En"
684 "hORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="),
[email protected]f7022f32014-08-21 16:32:19685 DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:44686
[email protected]f7022f32014-08-21 16:32:19687 r->Start();
688 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44689
[email protected]255620da2013-08-19 13:14:29690 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:44691
[email protected]f7022f32014-08-21 16:32:19692 EXPECT_TRUE(!r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44693 EXPECT_FALSE(d.received_data_before_response());
694 EXPECT_EQ(d.bytes_received(), 911);
[email protected]f7022f32014-08-21 16:32:19695 EXPECT_EQ("", r->GetSocketAddress().host());
696 EXPECT_EQ(0, r->GetSocketAddress().port());
[email protected]79e1fd62013-06-20 06:50:04697
698 HttpRequestHeaders headers;
[email protected]f7022f32014-08-21 16:32:19699 EXPECT_FALSE(r->GetFullRequestHeaders(&headers));
[email protected]316c1e5e2012-09-12 15:17:44700 }
701}
702
[email protected]5ecf7cb282014-05-11 01:49:55703#if !defined(DISABLE_FILE_SUPPORT)
[email protected]316c1e5e2012-09-12 15:17:44704TEST_F(URLRequestTest, FileTest) {
[email protected]6cdfd7f2013-02-08 20:40:15705 base::FilePath app_path;
[email protected]316c1e5e2012-09-12 15:17:44706 PathService::Get(base::FILE_EXE, &app_path);
707 GURL app_url = FilePathToFileURL(app_path);
708
709 TestDelegate d;
710 {
[email protected]f7022f32014-08-21 16:32:19711 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
712 app_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:44713
[email protected]f7022f32014-08-21 16:32:19714 r->Start();
715 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44716
[email protected]255620da2013-08-19 13:14:29717 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:44718
719 int64 file_size = -1;
[email protected]56285702013-12-04 18:22:49720 EXPECT_TRUE(base::GetFileSize(app_path, &file_size));
[email protected]316c1e5e2012-09-12 15:17:44721
[email protected]f7022f32014-08-21 16:32:19722 EXPECT_TRUE(!r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44723 EXPECT_EQ(1, d.response_started_count());
724 EXPECT_FALSE(d.received_data_before_response());
725 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
[email protected]f7022f32014-08-21 16:32:19726 EXPECT_EQ("", r->GetSocketAddress().host());
727 EXPECT_EQ(0, r->GetSocketAddress().port());
[email protected]79e1fd62013-06-20 06:50:04728
729 HttpRequestHeaders headers;
[email protected]f7022f32014-08-21 16:32:19730 EXPECT_FALSE(r->GetFullRequestHeaders(&headers));
[email protected]316c1e5e2012-09-12 15:17:44731 }
732}
733
[email protected]ba40bb762012-12-17 07:11:04734TEST_F(URLRequestTest, FileTestCancel) {
[email protected]6cdfd7f2013-02-08 20:40:15735 base::FilePath app_path;
[email protected]ba40bb762012-12-17 07:11:04736 PathService::Get(base::FILE_EXE, &app_path);
737 GURL app_url = FilePathToFileURL(app_path);
738
739 TestDelegate d;
740 {
[email protected]f7022f32014-08-21 16:32:19741 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
742 app_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]ba40bb762012-12-17 07:11:04743
[email protected]f7022f32014-08-21 16:32:19744 r->Start();
745 EXPECT_TRUE(r->is_pending());
746 r->Cancel();
[email protected]ba40bb762012-12-17 07:11:04747 }
[email protected]79e1fd62013-06-20 06:50:04748 // Async cancellation should be safe even when URLRequest has been already
[email protected]ba40bb762012-12-17 07:11:04749 // destroyed.
[email protected]255620da2013-08-19 13:14:29750 base::RunLoop().RunUntilIdle();
[email protected]ba40bb762012-12-17 07:11:04751}
752
[email protected]316c1e5e2012-09-12 15:17:44753TEST_F(URLRequestTest, FileTestFullSpecifiedRange) {
754 const size_t buffer_size = 4000;
[email protected]4356f0f2013-04-07 00:58:17755 scoped_ptr<char[]> buffer(new char[buffer_size]);
[email protected]316c1e5e2012-09-12 15:17:44756 FillBuffer(buffer.get(), buffer_size);
757
[email protected]6cdfd7f2013-02-08 20:40:15758 base::FilePath temp_path;
[email protected]03d9afc02013-12-03 17:55:52759 EXPECT_TRUE(base::CreateTemporaryFile(&temp_path));
[email protected]316c1e5e2012-09-12 15:17:44760 GURL temp_url = FilePathToFileURL(temp_path);
[email protected]e5c2a22e2014-03-06 20:42:30761 EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size));
[email protected]316c1e5e2012-09-12 15:17:44762
763 int64 file_size;
[email protected]56285702013-12-04 18:22:49764 EXPECT_TRUE(base::GetFileSize(temp_path, &file_size));
[email protected]316c1e5e2012-09-12 15:17:44765
766 const size_t first_byte_position = 500;
767 const size_t last_byte_position = buffer_size - first_byte_position;
768 const size_t content_length = last_byte_position - first_byte_position + 1;
769 std::string partial_buffer_string(buffer.get() + first_byte_position,
770 buffer.get() + last_byte_position + 1);
771
772 TestDelegate d;
773 {
[email protected]f7022f32014-08-21 16:32:19774 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
775 temp_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:44776
777 HttpRequestHeaders headers;
[email protected]b7572ea2013-11-26 20:16:38778 headers.SetHeader(
779 HttpRequestHeaders::kRange,
[email protected]cba24642014-08-15 20:49:59780 HttpByteRange::Bounded(
[email protected]b7572ea2013-11-26 20:16:38781 first_byte_position, last_byte_position).GetHeaderValue());
[email protected]f7022f32014-08-21 16:32:19782 r->SetExtraRequestHeaders(headers);
783 r->Start();
784 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44785
[email protected]255620da2013-08-19 13:14:29786 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:19787 EXPECT_TRUE(!r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44788 EXPECT_EQ(1, d.response_started_count());
789 EXPECT_FALSE(d.received_data_before_response());
790 EXPECT_EQ(static_cast<int>(content_length), d.bytes_received());
791 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
792 EXPECT_TRUE(partial_buffer_string == d.data_received());
793 }
794
[email protected]dd3aa792013-07-16 19:10:23795 EXPECT_TRUE(base::DeleteFile(temp_path, false));
[email protected]316c1e5e2012-09-12 15:17:44796}
797
798TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) {
799 const size_t buffer_size = 4000;
[email protected]4356f0f2013-04-07 00:58:17800 scoped_ptr<char[]> buffer(new char[buffer_size]);
[email protected]316c1e5e2012-09-12 15:17:44801 FillBuffer(buffer.get(), buffer_size);
802
[email protected]6cdfd7f2013-02-08 20:40:15803 base::FilePath temp_path;
[email protected]03d9afc02013-12-03 17:55:52804 EXPECT_TRUE(base::CreateTemporaryFile(&temp_path));
[email protected]316c1e5e2012-09-12 15:17:44805 GURL temp_url = FilePathToFileURL(temp_path);
[email protected]e5c2a22e2014-03-06 20:42:30806 EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size));
[email protected]316c1e5e2012-09-12 15:17:44807
808 int64 file_size;
[email protected]56285702013-12-04 18:22:49809 EXPECT_TRUE(base::GetFileSize(temp_path, &file_size));
[email protected]316c1e5e2012-09-12 15:17:44810
811 const size_t first_byte_position = 500;
812 const size_t last_byte_position = buffer_size - 1;
813 const size_t content_length = last_byte_position - first_byte_position + 1;
814 std::string partial_buffer_string(buffer.get() + first_byte_position,
815 buffer.get() + last_byte_position + 1);
816
817 TestDelegate d;
818 {
[email protected]f7022f32014-08-21 16:32:19819 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
820 temp_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:44821
822 HttpRequestHeaders headers;
823 headers.SetHeader(HttpRequestHeaders::kRange,
[email protected]cba24642014-08-15 20:49:59824 HttpByteRange::RightUnbounded(
[email protected]b7572ea2013-11-26 20:16:38825 first_byte_position).GetHeaderValue());
[email protected]f7022f32014-08-21 16:32:19826 r->SetExtraRequestHeaders(headers);
827 r->Start();
828 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44829
[email protected]255620da2013-08-19 13:14:29830 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:19831 EXPECT_TRUE(!r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44832 EXPECT_EQ(1, d.response_started_count());
833 EXPECT_FALSE(d.received_data_before_response());
834 EXPECT_EQ(static_cast<int>(content_length), d.bytes_received());
835 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
836 EXPECT_TRUE(partial_buffer_string == d.data_received());
837 }
838
[email protected]dd3aa792013-07-16 19:10:23839 EXPECT_TRUE(base::DeleteFile(temp_path, false));
[email protected]316c1e5e2012-09-12 15:17:44840}
841
842TEST_F(URLRequestTest, FileTestMultipleRanges) {
843 const size_t buffer_size = 400000;
[email protected]4356f0f2013-04-07 00:58:17844 scoped_ptr<char[]> buffer(new char[buffer_size]);
[email protected]316c1e5e2012-09-12 15:17:44845 FillBuffer(buffer.get(), buffer_size);
846
[email protected]6cdfd7f2013-02-08 20:40:15847 base::FilePath temp_path;
[email protected]03d9afc02013-12-03 17:55:52848 EXPECT_TRUE(base::CreateTemporaryFile(&temp_path));
[email protected]316c1e5e2012-09-12 15:17:44849 GURL temp_url = FilePathToFileURL(temp_path);
[email protected]e5c2a22e2014-03-06 20:42:30850 EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size));
[email protected]316c1e5e2012-09-12 15:17:44851
852 int64 file_size;
[email protected]56285702013-12-04 18:22:49853 EXPECT_TRUE(base::GetFileSize(temp_path, &file_size));
[email protected]316c1e5e2012-09-12 15:17:44854
855 TestDelegate d;
856 {
[email protected]f7022f32014-08-21 16:32:19857 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
858 temp_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:44859
860 HttpRequestHeaders headers;
[email protected]b7572ea2013-11-26 20:16:38861 headers.SetHeader(HttpRequestHeaders::kRange, "bytes=0-0,10-200,200-300");
[email protected]f7022f32014-08-21 16:32:19862 r->SetExtraRequestHeaders(headers);
863 r->Start();
864 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44865
[email protected]255620da2013-08-19 13:14:29866 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:44867 EXPECT_TRUE(d.request_failed());
868 }
869
[email protected]dd3aa792013-07-16 19:10:23870 EXPECT_TRUE(base::DeleteFile(temp_path, false));
[email protected]316c1e5e2012-09-12 15:17:44871}
872
[email protected]3ca8b362013-11-11 22:18:07873TEST_F(URLRequestTest, AllowFileURLs) {
874 base::ScopedTempDir temp_dir;
875 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
876 base::FilePath test_file;
[email protected]03d9afc02013-12-03 17:55:52877 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &test_file));
[email protected]3ca8b362013-11-11 22:18:07878 std::string test_data("monkey");
[email protected]e5c2a22e2014-03-06 20:42:30879 base::WriteFile(test_file, test_data.data(), test_data.size());
[email protected]cba24642014-08-15 20:49:59880 GURL test_file_url = FilePathToFileURL(test_file);
[email protected]3ca8b362013-11-11 22:18:07881
882 {
883 TestDelegate d;
884 TestNetworkDelegate network_delegate;
885 network_delegate.set_can_access_files(true);
886 default_context_.set_network_delegate(&network_delegate);
[email protected]f7022f32014-08-21 16:32:19887 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
888 test_file_url, DEFAULT_PRIORITY, &d, NULL));
889 r->Start();
[email protected]3ca8b362013-11-11 22:18:07890 base::RunLoop().Run();
891 EXPECT_FALSE(d.request_failed());
892 EXPECT_EQ(test_data, d.data_received());
893 }
894
895 {
896 TestDelegate d;
897 TestNetworkDelegate network_delegate;
898 network_delegate.set_can_access_files(false);
899 default_context_.set_network_delegate(&network_delegate);
[email protected]f7022f32014-08-21 16:32:19900 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
901 test_file_url, DEFAULT_PRIORITY, &d, NULL));
902 r->Start();
[email protected]3ca8b362013-11-11 22:18:07903 base::RunLoop().Run();
904 EXPECT_TRUE(d.request_failed());
905 EXPECT_EQ("", d.data_received());
906 }
907}
908
[email protected]316c1e5e2012-09-12 15:17:44909
910TEST_F(URLRequestTest, FileDirCancelTest) {
911 // Put in mock resource provider.
912 NetModule::SetResourceProvider(TestNetResourceProvider);
913
914 TestDelegate d;
915 {
[email protected]6cdfd7f2013-02-08 20:40:15916 base::FilePath file_path;
[email protected]316c1e5e2012-09-12 15:17:44917 PathService::Get(base::DIR_SOURCE_ROOT, &file_path);
918 file_path = file_path.Append(FILE_PATH_LITERAL("net"));
919 file_path = file_path.Append(FILE_PATH_LITERAL("data"));
920
[email protected]f7022f32014-08-21 16:32:19921 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
922 FilePathToFileURL(file_path), DEFAULT_PRIORITY, &d, NULL));
923 req->Start();
924 EXPECT_TRUE(req->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44925
926 d.set_cancel_in_received_data_pending(true);
927
[email protected]255620da2013-08-19 13:14:29928 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:44929 }
930
931 // Take out mock resource provider.
932 NetModule::SetResourceProvider(NULL);
933}
934
[email protected]5f9581402013-10-30 13:08:32935TEST_F(URLRequestTest, FileDirOutputSanity) {
936 // Verify the general sanity of the the output of the file:
937 // directory lister by checking for the output of a known existing
938 // file.
939 const char sentinel_name[] = "filedir-sentinel";
940
941 base::FilePath path;
942 PathService::Get(base::DIR_SOURCE_ROOT, &path);
943 path = path.Append(FILE_PATH_LITERAL("net"));
944 path = path.Append(FILE_PATH_LITERAL("data"));
945 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
946
947 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:19948 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
949 FilePathToFileURL(path), DEFAULT_PRIORITY, &d, NULL));
950 req->Start();
[email protected]5f9581402013-10-30 13:08:32951 base::RunLoop().Run();
952
953 // Generate entry for the sentinel file.
954 base::FilePath sentinel_path = path.AppendASCII(sentinel_name);
[email protected]54124ed02014-01-07 10:06:58955 base::File::Info info;
[email protected]9eae4e62013-12-04 20:56:49956 EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info));
[email protected]5f9581402013-10-30 13:08:32957 EXPECT_GT(info.size, 0);
958 std::string sentinel_output = GetDirectoryListingEntry(
959 base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)),
960 std::string(sentinel_name),
961 false /* is_dir */,
962 info.size,
963 info.last_modified);
964
965 ASSERT_LT(0, d.bytes_received());
966 ASSERT_FALSE(d.request_failed());
[email protected]f7022f32014-08-21 16:32:19967 ASSERT_TRUE(req->status().is_success());
[email protected]5f9581402013-10-30 13:08:32968 // Check for the entry generated for the "sentinel" file.
969 const std::string& data = d.data_received();
970 ASSERT_NE(data.find(sentinel_output), std::string::npos);
971}
972
[email protected]316c1e5e2012-09-12 15:17:44973TEST_F(URLRequestTest, FileDirRedirectNoCrash) {
974 // There is an implicit redirect when loading a file path that matches a
975 // directory and does not end with a slash. Ensure that following such
976 // redirects does not crash. See http://crbug.com/18686.
977
[email protected]6cdfd7f2013-02-08 20:40:15978 base::FilePath path;
[email protected]316c1e5e2012-09-12 15:17:44979 PathService::Get(base::DIR_SOURCE_ROOT, &path);
980 path = path.Append(FILE_PATH_LITERAL("net"));
981 path = path.Append(FILE_PATH_LITERAL("data"));
982 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
983
984 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:19985 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
986 FilePathToFileURL(path), DEFAULT_PRIORITY, &d, NULL));
987 req->Start();
[email protected]255620da2013-08-19 13:14:29988 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:44989
990 ASSERT_EQ(1, d.received_redirect_count());
991 ASSERT_LT(0, d.bytes_received());
992 ASSERT_FALSE(d.request_failed());
[email protected]f7022f32014-08-21 16:32:19993 ASSERT_TRUE(req->status().is_success());
[email protected]316c1e5e2012-09-12 15:17:44994}
995
996#if defined(OS_WIN)
997// Don't accept the url "file:///" on windows. See http://crbug.com/1474.
998TEST_F(URLRequestTest, FileDirRedirectSingleSlash) {
999 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:191000 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1001 GURL("file:///"), DEFAULT_PRIORITY, &d, NULL));
1002 req->Start();
[email protected]255620da2013-08-19 13:14:291003 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441004
1005 ASSERT_EQ(1, d.received_redirect_count());
[email protected]f7022f32014-08-21 16:32:191006 ASSERT_FALSE(req->status().is_success());
[email protected]316c1e5e2012-09-12 15:17:441007}
[email protected]5ecf7cb282014-05-11 01:49:551008#endif // defined(OS_WIN)
1009
1010#endif // !defined(DISABLE_FILE_SUPPORT)
1011
1012TEST_F(URLRequestTest, InvalidUrlTest) {
1013 TestDelegate d;
1014 {
[email protected]f7022f32014-08-21 16:32:191015 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
1016 GURL("invalid url"), DEFAULT_PRIORITY, &d, NULL));
[email protected]5ecf7cb282014-05-11 01:49:551017
[email protected]f7022f32014-08-21 16:32:191018 r->Start();
1019 EXPECT_TRUE(r->is_pending());
[email protected]5ecf7cb282014-05-11 01:49:551020
1021 base::RunLoop().Run();
1022 EXPECT_TRUE(d.request_failed());
1023 }
1024}
1025
jochen0e3b3a62014-09-16 18:31:231026TEST_F(URLRequestTest, InvalidReferrerTest) {
1027 TestURLRequestContext context;
1028 TestNetworkDelegate network_delegate;
1029 network_delegate.set_cancel_request_with_policy_violating_referrer(true);
1030 context.set_network_delegate(&network_delegate);
1031 TestDelegate d;
1032 scoped_ptr<URLRequest> req(context.CreateRequest(
1033 GURL("http://localhost/"), DEFAULT_PRIORITY, &d, NULL));
1034 req->SetReferrer("https://somewhere.com/");
1035
1036 req->Start();
1037 base::RunLoop().Run();
1038 EXPECT_TRUE(d.request_failed());
1039}
1040
[email protected]5ecf7cb282014-05-11 01:49:551041#if defined(OS_WIN)
1042TEST_F(URLRequestTest, ResolveShortcutTest) {
1043 base::FilePath app_path;
1044 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
1045 app_path = app_path.AppendASCII("net");
1046 app_path = app_path.AppendASCII("data");
1047 app_path = app_path.AppendASCII("url_request_unittest");
1048 app_path = app_path.AppendASCII("with-headers.html");
1049
1050 std::wstring lnk_path = app_path.value() + L".lnk";
1051
1052 base::win::ScopedCOMInitializer com_initializer;
1053
1054 // Temporarily create a shortcut for test
1055 {
1056 base::win::ScopedComPtr<IShellLink> shell;
1057 ASSERT_TRUE(SUCCEEDED(shell.CreateInstance(CLSID_ShellLink, NULL,
1058 CLSCTX_INPROC_SERVER)));
1059 base::win::ScopedComPtr<IPersistFile> persist;
1060 ASSERT_TRUE(SUCCEEDED(shell.QueryInterface(persist.Receive())));
1061 EXPECT_TRUE(SUCCEEDED(shell->SetPath(app_path.value().c_str())));
1062 EXPECT_TRUE(SUCCEEDED(shell->SetDescription(L"ResolveShortcutTest")));
1063 EXPECT_TRUE(SUCCEEDED(persist->Save(lnk_path.c_str(), TRUE)));
1064 }
1065
1066 TestDelegate d;
1067 {
[email protected]f7022f32014-08-21 16:32:191068 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
1069 FilePathToFileURL(base::FilePath(lnk_path)), DEFAULT_PRIORITY, &d,
1070 NULL));
[email protected]5ecf7cb282014-05-11 01:49:551071
[email protected]f7022f32014-08-21 16:32:191072 r->Start();
1073 EXPECT_TRUE(r->is_pending());
[email protected]5ecf7cb282014-05-11 01:49:551074
1075 base::RunLoop().Run();
1076
1077 WIN32_FILE_ATTRIBUTE_DATA data;
1078 GetFileAttributesEx(app_path.value().c_str(),
1079 GetFileExInfoStandard, &data);
1080 HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ,
1081 FILE_SHARE_READ, NULL, OPEN_EXISTING,
1082 FILE_ATTRIBUTE_NORMAL, NULL);
1083 EXPECT_NE(INVALID_HANDLE_VALUE, file);
1084 scoped_ptr<char[]> buffer(new char[data.nFileSizeLow]);
1085 DWORD read_size;
1086 BOOL result;
1087 result = ReadFile(file, buffer.get(), data.nFileSizeLow,
1088 &read_size, NULL);
1089 std::string content(buffer.get(), read_size);
1090 CloseHandle(file);
1091
[email protected]f7022f32014-08-21 16:32:191092 EXPECT_TRUE(!r->is_pending());
[email protected]5ecf7cb282014-05-11 01:49:551093 EXPECT_EQ(1, d.received_redirect_count());
1094 EXPECT_EQ(content, d.data_received());
1095 }
1096
1097 // Clean the shortcut
1098 DeleteFile(lnk_path.c_str());
1099}
1100#endif // defined(OS_WIN)
[email protected]316c1e5e2012-09-12 15:17:441101
1102// Custom URLRequestJobs for use with interceptor tests
1103class RestartTestJob : public URLRequestTestJob {
1104 public:
1105 RestartTestJob(URLRequest* request, NetworkDelegate* network_delegate)
1106 : URLRequestTestJob(request, network_delegate, true) {}
1107 protected:
dchengb03027d2014-10-21 12:00:201108 void StartAsync() override { this->NotifyRestartRequired(); }
[email protected]316c1e5e2012-09-12 15:17:441109 private:
dchengb03027d2014-10-21 12:00:201110 ~RestartTestJob() override {}
[email protected]316c1e5e2012-09-12 15:17:441111};
1112
1113class CancelTestJob : public URLRequestTestJob {
1114 public:
1115 explicit CancelTestJob(URLRequest* request, NetworkDelegate* network_delegate)
1116 : URLRequestTestJob(request, network_delegate, true) {}
1117 protected:
dchengb03027d2014-10-21 12:00:201118 void StartAsync() override { request_->Cancel(); }
[email protected]316c1e5e2012-09-12 15:17:441119 private:
dchengb03027d2014-10-21 12:00:201120 ~CancelTestJob() override {}
[email protected]316c1e5e2012-09-12 15:17:441121};
1122
1123class CancelThenRestartTestJob : public URLRequestTestJob {
1124 public:
1125 explicit CancelThenRestartTestJob(URLRequest* request,
1126 NetworkDelegate* network_delegate)
1127 : URLRequestTestJob(request, network_delegate, true) {
1128 }
1129 protected:
dchengb03027d2014-10-21 12:00:201130 void StartAsync() override {
[email protected]316c1e5e2012-09-12 15:17:441131 request_->Cancel();
1132 this->NotifyRestartRequired();
1133 }
1134 private:
dchengb03027d2014-10-21 12:00:201135 ~CancelThenRestartTestJob() override {}
[email protected]316c1e5e2012-09-12 15:17:441136};
1137
1138// An Interceptor for use with interceptor tests
1139class TestInterceptor : URLRequest::Interceptor {
1140 public:
1141 TestInterceptor()
1142 : intercept_main_request_(false), restart_main_request_(false),
1143 cancel_main_request_(false), cancel_then_restart_main_request_(false),
1144 simulate_main_network_error_(false),
1145 intercept_redirect_(false), cancel_redirect_request_(false),
1146 intercept_final_response_(false), cancel_final_request_(false),
1147 did_intercept_main_(false), did_restart_main_(false),
1148 did_cancel_main_(false), did_cancel_then_restart_main_(false),
1149 did_simulate_error_main_(false),
1150 did_intercept_redirect_(false), did_cancel_redirect_(false),
1151 did_intercept_final_(false), did_cancel_final_(false) {
1152 URLRequest::Deprecated::RegisterRequestInterceptor(this);
1153 }
1154
dchengb03027d2014-10-21 12:00:201155 ~TestInterceptor() override {
[email protected]316c1e5e2012-09-12 15:17:441156 URLRequest::Deprecated::UnregisterRequestInterceptor(this);
1157 }
1158
dchengb03027d2014-10-21 12:00:201159 URLRequestJob* MaybeIntercept(URLRequest* request,
1160 NetworkDelegate* network_delegate) override {
[email protected]316c1e5e2012-09-12 15:17:441161 if (restart_main_request_) {
1162 restart_main_request_ = false;
1163 did_restart_main_ = true;
1164 return new RestartTestJob(request, network_delegate);
1165 }
1166 if (cancel_main_request_) {
1167 cancel_main_request_ = false;
1168 did_cancel_main_ = true;
1169 return new CancelTestJob(request, network_delegate);
1170 }
1171 if (cancel_then_restart_main_request_) {
1172 cancel_then_restart_main_request_ = false;
1173 did_cancel_then_restart_main_ = true;
1174 return new CancelThenRestartTestJob(request, network_delegate);
1175 }
1176 if (simulate_main_network_error_) {
1177 simulate_main_network_error_ = false;
1178 did_simulate_error_main_ = true;
1179 // will error since the requeted url is not one of its canned urls
1180 return new URLRequestTestJob(request, network_delegate, true);
1181 }
1182 if (!intercept_main_request_)
1183 return NULL;
1184 intercept_main_request_ = false;
1185 did_intercept_main_ = true;
[email protected]2bba3252013-04-08 19:50:591186 URLRequestTestJob* job = new URLRequestTestJob(request,
1187 network_delegate,
1188 main_headers_,
1189 main_data_,
1190 true);
1191 job->set_load_timing_info(main_request_load_timing_info_);
1192 return job;
[email protected]316c1e5e2012-09-12 15:17:441193 }
1194
dchengb03027d2014-10-21 12:00:201195 URLRequestJob* MaybeInterceptRedirect(URLRequest* request,
1196 NetworkDelegate* network_delegate,
1197 const GURL& location) override {
[email protected]316c1e5e2012-09-12 15:17:441198 if (cancel_redirect_request_) {
1199 cancel_redirect_request_ = false;
1200 did_cancel_redirect_ = true;
1201 return new CancelTestJob(request, network_delegate);
1202 }
1203 if (!intercept_redirect_)
1204 return NULL;
1205 intercept_redirect_ = false;
1206 did_intercept_redirect_ = true;
1207 return new URLRequestTestJob(request,
1208 network_delegate,
1209 redirect_headers_,
1210 redirect_data_,
1211 true);
1212 }
1213
dchengb03027d2014-10-21 12:00:201214 URLRequestJob* MaybeInterceptResponse(
1215 URLRequest* request,
1216 NetworkDelegate* network_delegate) override {
[email protected]316c1e5e2012-09-12 15:17:441217 if (cancel_final_request_) {
1218 cancel_final_request_ = false;
1219 did_cancel_final_ = true;
1220 return new CancelTestJob(request, network_delegate);
1221 }
1222 if (!intercept_final_response_)
1223 return NULL;
1224 intercept_final_response_ = false;
1225 did_intercept_final_ = true;
1226 return new URLRequestTestJob(request,
1227 network_delegate,
1228 final_headers_,
1229 final_data_,
1230 true);
1231 }
1232
[email protected]2bba3252013-04-08 19:50:591233 // Whether to intercept the main request, and if so the response to return and
1234 // the LoadTimingInfo to use.
[email protected]316c1e5e2012-09-12 15:17:441235 bool intercept_main_request_;
1236 std::string main_headers_;
1237 std::string main_data_;
[email protected]2bba3252013-04-08 19:50:591238 LoadTimingInfo main_request_load_timing_info_;
[email protected]316c1e5e2012-09-12 15:17:441239
1240 // Other actions we take at MaybeIntercept time
1241 bool restart_main_request_;
1242 bool cancel_main_request_;
1243 bool cancel_then_restart_main_request_;
1244 bool simulate_main_network_error_;
1245
1246 // Whether to intercept redirects, and if so the response to return.
1247 bool intercept_redirect_;
1248 std::string redirect_headers_;
1249 std::string redirect_data_;
1250
1251 // Other actions we can take at MaybeInterceptRedirect time
1252 bool cancel_redirect_request_;
1253
1254 // Whether to intercept final response, and if so the response to return.
1255 bool intercept_final_response_;
1256 std::string final_headers_;
1257 std::string final_data_;
1258
1259 // Other actions we can take at MaybeInterceptResponse time
1260 bool cancel_final_request_;
1261
1262 // If we did something or not
1263 bool did_intercept_main_;
1264 bool did_restart_main_;
1265 bool did_cancel_main_;
1266 bool did_cancel_then_restart_main_;
1267 bool did_simulate_error_main_;
1268 bool did_intercept_redirect_;
1269 bool did_cancel_redirect_;
1270 bool did_intercept_final_;
1271 bool did_cancel_final_;
1272
1273 // Static getters for canned response header and data strings
1274
1275 static std::string ok_data() {
1276 return URLRequestTestJob::test_data_1();
1277 }
1278
1279 static std::string ok_headers() {
1280 return URLRequestTestJob::test_headers();
1281 }
1282
1283 static std::string redirect_data() {
1284 return std::string();
1285 }
1286
1287 static std::string redirect_headers() {
1288 return URLRequestTestJob::test_redirect_headers();
1289 }
1290
1291 static std::string error_data() {
1292 return std::string("ohhh nooooo mr. bill!");
1293 }
1294
1295 static std::string error_headers() {
1296 return URLRequestTestJob::test_error_headers();
1297 }
1298};
1299
1300TEST_F(URLRequestTest, Intercept) {
1301 TestInterceptor interceptor;
1302
1303 // intercept the main request and respond with a simple response
1304 interceptor.intercept_main_request_ = true;
1305 interceptor.main_headers_ = TestInterceptor::ok_headers();
1306 interceptor.main_data_ = TestInterceptor::ok_data();
1307
1308 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:191309 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1310 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:441311 base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data();
1312 base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data();
1313 base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data();
[email protected]f7022f32014-08-21 16:32:191314 req->SetUserData(NULL, user_data0);
1315 req->SetUserData(&user_data1, user_data1);
1316 req->SetUserData(&user_data2, user_data2);
1317 req->set_method("GET");
1318 req->Start();
[email protected]255620da2013-08-19 13:14:291319 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441320
1321 // Make sure we can retrieve our specific user data
[email protected]f7022f32014-08-21 16:32:191322 EXPECT_EQ(user_data0, req->GetUserData(NULL));
1323 EXPECT_EQ(user_data1, req->GetUserData(&user_data1));
1324 EXPECT_EQ(user_data2, req->GetUserData(&user_data2));
[email protected]316c1e5e2012-09-12 15:17:441325
1326 // Check the interceptor got called as expected
1327 EXPECT_TRUE(interceptor.did_intercept_main_);
1328
1329 // Check we got one good response
[email protected]f7022f32014-08-21 16:32:191330 EXPECT_TRUE(req->status().is_success());
1331 EXPECT_EQ(200, req->response_headers()->response_code());
[email protected]316c1e5e2012-09-12 15:17:441332 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1333 EXPECT_EQ(1, d.response_started_count());
1334 EXPECT_EQ(0, d.received_redirect_count());
1335}
1336
1337TEST_F(URLRequestTest, InterceptRedirect) {
1338 TestInterceptor interceptor;
1339
1340 // intercept the main request and respond with a redirect
1341 interceptor.intercept_main_request_ = true;
1342 interceptor.main_headers_ = TestInterceptor::redirect_headers();
1343 interceptor.main_data_ = TestInterceptor::redirect_data();
1344
1345 // intercept that redirect and respond a final OK response
1346 interceptor.intercept_redirect_ = true;
1347 interceptor.redirect_headers_ = TestInterceptor::ok_headers();
1348 interceptor.redirect_data_ = TestInterceptor::ok_data();
1349
1350 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:191351 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1352 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL));
1353 req->set_method("GET");
1354 req->Start();
[email protected]255620da2013-08-19 13:14:291355 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441356
1357 // Check the interceptor got called as expected
1358 EXPECT_TRUE(interceptor.did_intercept_main_);
1359 EXPECT_TRUE(interceptor.did_intercept_redirect_);
1360
1361 // Check we got one good response
[email protected]f7022f32014-08-21 16:32:191362 EXPECT_TRUE(req->status().is_success());
1363 if (req->status().is_success()) {
1364 EXPECT_EQ(200, req->response_headers()->response_code());
[email protected]316c1e5e2012-09-12 15:17:441365 }
1366 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1367 EXPECT_EQ(1, d.response_started_count());
1368 EXPECT_EQ(0, d.received_redirect_count());
1369}
1370
1371TEST_F(URLRequestTest, InterceptServerError) {
1372 TestInterceptor interceptor;
1373
1374 // intercept the main request to generate a server error response
1375 interceptor.intercept_main_request_ = true;
1376 interceptor.main_headers_ = TestInterceptor::error_headers();
1377 interceptor.main_data_ = TestInterceptor::error_data();
1378
1379 // intercept that error and respond with an OK response
1380 interceptor.intercept_final_response_ = true;
1381 interceptor.final_headers_ = TestInterceptor::ok_headers();
1382 interceptor.final_data_ = TestInterceptor::ok_data();
1383
1384 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:191385 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1386 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL));
1387 req->set_method("GET");
1388 req->Start();
[email protected]255620da2013-08-19 13:14:291389 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441390
1391 // Check the interceptor got called as expected
1392 EXPECT_TRUE(interceptor.did_intercept_main_);
1393 EXPECT_TRUE(interceptor.did_intercept_final_);
1394
1395 // Check we got one good response
[email protected]f7022f32014-08-21 16:32:191396 EXPECT_TRUE(req->status().is_success());
1397 EXPECT_EQ(200, req->response_headers()->response_code());
[email protected]316c1e5e2012-09-12 15:17:441398 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1399 EXPECT_EQ(1, d.response_started_count());
1400 EXPECT_EQ(0, d.received_redirect_count());
1401}
1402
1403TEST_F(URLRequestTest, InterceptNetworkError) {
1404 TestInterceptor interceptor;
1405
1406 // intercept the main request to simulate a network error
1407 interceptor.simulate_main_network_error_ = true;
1408
1409 // intercept that error and respond with an OK response
1410 interceptor.intercept_final_response_ = true;
1411 interceptor.final_headers_ = TestInterceptor::ok_headers();
1412 interceptor.final_data_ = TestInterceptor::ok_data();
1413
1414 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:191415 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1416 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL));
1417 req->set_method("GET");
1418 req->Start();
[email protected]255620da2013-08-19 13:14:291419 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441420
1421 // Check the interceptor got called as expected
1422 EXPECT_TRUE(interceptor.did_simulate_error_main_);
1423 EXPECT_TRUE(interceptor.did_intercept_final_);
1424
1425 // Check we received one good response
[email protected]f7022f32014-08-21 16:32:191426 EXPECT_TRUE(req->status().is_success());
1427 EXPECT_EQ(200, req->response_headers()->response_code());
[email protected]316c1e5e2012-09-12 15:17:441428 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1429 EXPECT_EQ(1, d.response_started_count());
1430 EXPECT_EQ(0, d.received_redirect_count());
1431}
1432
1433TEST_F(URLRequestTest, InterceptRestartRequired) {
1434 TestInterceptor interceptor;
1435
1436 // restart the main request
1437 interceptor.restart_main_request_ = true;
1438
1439 // then intercept the new main request and respond with an OK response
1440 interceptor.intercept_main_request_ = true;
1441 interceptor.main_headers_ = TestInterceptor::ok_headers();
1442 interceptor.main_data_ = TestInterceptor::ok_data();
1443
1444 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:191445 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1446 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL));
1447 req->set_method("GET");
1448 req->Start();
[email protected]255620da2013-08-19 13:14:291449 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441450
1451 // Check the interceptor got called as expected
1452 EXPECT_TRUE(interceptor.did_restart_main_);
1453 EXPECT_TRUE(interceptor.did_intercept_main_);
1454
1455 // Check we received one good response
[email protected]f7022f32014-08-21 16:32:191456 EXPECT_TRUE(req->status().is_success());
1457 if (req->status().is_success()) {
1458 EXPECT_EQ(200, req->response_headers()->response_code());
[email protected]316c1e5e2012-09-12 15:17:441459 }
1460 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1461 EXPECT_EQ(1, d.response_started_count());
1462 EXPECT_EQ(0, d.received_redirect_count());
1463}
1464
1465TEST_F(URLRequestTest, InterceptRespectsCancelMain) {
1466 TestInterceptor interceptor;
1467
1468 // intercept the main request and cancel from within the restarted job
1469 interceptor.cancel_main_request_ = true;
1470
1471 // setup to intercept final response and override it with an OK response
1472 interceptor.intercept_final_response_ = true;
1473 interceptor.final_headers_ = TestInterceptor::ok_headers();
1474 interceptor.final_data_ = TestInterceptor::ok_data();
1475
1476 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:191477 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1478 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL));
1479 req->set_method("GET");
1480 req->Start();
[email protected]255620da2013-08-19 13:14:291481 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441482
1483 // Check the interceptor got called as expected
1484 EXPECT_TRUE(interceptor.did_cancel_main_);
1485 EXPECT_FALSE(interceptor.did_intercept_final_);
1486
1487 // Check we see a canceled request
[email protected]f7022f32014-08-21 16:32:191488 EXPECT_FALSE(req->status().is_success());
1489 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status());
[email protected]316c1e5e2012-09-12 15:17:441490}
1491
1492TEST_F(URLRequestTest, InterceptRespectsCancelRedirect) {
1493 TestInterceptor interceptor;
1494
1495 // intercept the main request and respond with a redirect
1496 interceptor.intercept_main_request_ = true;
1497 interceptor.main_headers_ = TestInterceptor::redirect_headers();
1498 interceptor.main_data_ = TestInterceptor::redirect_data();
1499
1500 // intercept the redirect and cancel from within that job
1501 interceptor.cancel_redirect_request_ = true;
1502
1503 // setup to intercept final response and override it with an OK response
1504 interceptor.intercept_final_response_ = true;
1505 interceptor.final_headers_ = TestInterceptor::ok_headers();
1506 interceptor.final_data_ = TestInterceptor::ok_data();
1507
1508 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:191509 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1510 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL));
1511 req->set_method("GET");
1512 req->Start();
[email protected]255620da2013-08-19 13:14:291513 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441514
1515 // Check the interceptor got called as expected
1516 EXPECT_TRUE(interceptor.did_intercept_main_);
1517 EXPECT_TRUE(interceptor.did_cancel_redirect_);
1518 EXPECT_FALSE(interceptor.did_intercept_final_);
1519
1520 // Check we see a canceled request
[email protected]f7022f32014-08-21 16:32:191521 EXPECT_FALSE(req->status().is_success());
1522 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status());
[email protected]316c1e5e2012-09-12 15:17:441523}
1524
1525TEST_F(URLRequestTest, InterceptRespectsCancelFinal) {
1526 TestInterceptor interceptor;
1527
1528 // intercept the main request to simulate a network error
1529 interceptor.simulate_main_network_error_ = true;
1530
1531 // setup to intercept final response and cancel from within that job
1532 interceptor.cancel_final_request_ = true;
1533
1534 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:191535 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1536 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL));
1537 req->set_method("GET");
1538 req->Start();
[email protected]255620da2013-08-19 13:14:291539 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441540
1541 // Check the interceptor got called as expected
1542 EXPECT_TRUE(interceptor.did_simulate_error_main_);
1543 EXPECT_TRUE(interceptor.did_cancel_final_);
1544
1545 // Check we see a canceled request
[email protected]f7022f32014-08-21 16:32:191546 EXPECT_FALSE(req->status().is_success());
1547 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status());
[email protected]316c1e5e2012-09-12 15:17:441548}
1549
1550TEST_F(URLRequestTest, InterceptRespectsCancelInRestart) {
1551 TestInterceptor interceptor;
1552
1553 // intercept the main request and cancel then restart from within that job
1554 interceptor.cancel_then_restart_main_request_ = true;
1555
1556 // setup to intercept final response and override it with an OK response
1557 interceptor.intercept_final_response_ = true;
1558 interceptor.final_headers_ = TestInterceptor::ok_headers();
1559 interceptor.final_data_ = TestInterceptor::ok_data();
1560
1561 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:191562 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1563 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL));
1564 req->set_method("GET");
1565 req->Start();
[email protected]255620da2013-08-19 13:14:291566 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441567
1568 // Check the interceptor got called as expected
1569 EXPECT_TRUE(interceptor.did_cancel_then_restart_main_);
1570 EXPECT_FALSE(interceptor.did_intercept_final_);
1571
1572 // Check we see a canceled request
[email protected]f7022f32014-08-21 16:32:191573 EXPECT_FALSE(req->status().is_success());
1574 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status());
[email protected]316c1e5e2012-09-12 15:17:441575}
1576
[email protected]2bba3252013-04-08 19:50:591577LoadTimingInfo RunLoadTimingTest(const LoadTimingInfo& job_load_timing,
1578 URLRequestContext* context) {
1579 TestInterceptor interceptor;
1580 interceptor.intercept_main_request_ = true;
1581 interceptor.main_request_load_timing_info_ = job_load_timing;
1582 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:191583 scoped_ptr<URLRequest> req(context->CreateRequest(
1584 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL));
1585 req->Start();
[email protected]255620da2013-08-19 13:14:291586 base::RunLoop().Run();
[email protected]2bba3252013-04-08 19:50:591587
1588 LoadTimingInfo resulting_load_timing;
[email protected]f7022f32014-08-21 16:32:191589 req->GetLoadTimingInfo(&resulting_load_timing);
[email protected]2bba3252013-04-08 19:50:591590
1591 // None of these should be modified by the URLRequest.
1592 EXPECT_EQ(job_load_timing.socket_reused, resulting_load_timing.socket_reused);
1593 EXPECT_EQ(job_load_timing.socket_log_id, resulting_load_timing.socket_log_id);
1594 EXPECT_EQ(job_load_timing.send_start, resulting_load_timing.send_start);
1595 EXPECT_EQ(job_load_timing.send_end, resulting_load_timing.send_end);
1596 EXPECT_EQ(job_load_timing.receive_headers_end,
1597 resulting_load_timing.receive_headers_end);
1598
1599 return resulting_load_timing;
1600}
1601
1602// "Normal" LoadTimingInfo as returned by a job. Everything is in order, not
1603// reused. |connect_time_flags| is used to indicate if there should be dns
1604// or SSL times, and |used_proxy| is used for proxy times.
1605LoadTimingInfo NormalLoadTimingInfo(base::TimeTicks now,
1606 int connect_time_flags,
1607 bool used_proxy) {
1608 LoadTimingInfo load_timing;
1609 load_timing.socket_log_id = 1;
1610
1611 if (used_proxy) {
1612 load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1);
1613 load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2);
1614 }
1615
1616 LoadTimingInfo::ConnectTiming& connect_timing = load_timing.connect_timing;
1617 if (connect_time_flags & CONNECT_TIMING_HAS_DNS_TIMES) {
1618 connect_timing.dns_start = now + base::TimeDelta::FromDays(3);
1619 connect_timing.dns_end = now + base::TimeDelta::FromDays(4);
1620 }
1621 connect_timing.connect_start = now + base::TimeDelta::FromDays(5);
1622 if (connect_time_flags & CONNECT_TIMING_HAS_SSL_TIMES) {
1623 connect_timing.ssl_start = now + base::TimeDelta::FromDays(6);
1624 connect_timing.ssl_end = now + base::TimeDelta::FromDays(7);
1625 }
1626 connect_timing.connect_end = now + base::TimeDelta::FromDays(8);
1627
1628 load_timing.send_start = now + base::TimeDelta::FromDays(9);
1629 load_timing.send_end = now + base::TimeDelta::FromDays(10);
1630 load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11);
1631 return load_timing;
1632}
1633
1634// Same as above, but in the case of a reused socket.
1635LoadTimingInfo NormalLoadTimingInfoReused(base::TimeTicks now,
1636 bool used_proxy) {
1637 LoadTimingInfo load_timing;
1638 load_timing.socket_log_id = 1;
1639 load_timing.socket_reused = true;
1640
1641 if (used_proxy) {
1642 load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1);
1643 load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2);
1644 }
1645
1646 load_timing.send_start = now + base::TimeDelta::FromDays(9);
1647 load_timing.send_end = now + base::TimeDelta::FromDays(10);
1648 load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11);
1649 return load_timing;
1650}
1651
1652// Basic test that the intercept + load timing tests work.
1653TEST_F(URLRequestTest, InterceptLoadTiming) {
1654 base::TimeTicks now = base::TimeTicks::Now();
1655 LoadTimingInfo job_load_timing =
1656 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, false);
1657
[email protected]2ca01e52013-10-31 22:05:191658 LoadTimingInfo load_timing_result =
1659 RunLoadTimingTest(job_load_timing, &default_context_);
[email protected]2bba3252013-04-08 19:50:591660
1661 // Nothing should have been changed by the URLRequest.
1662 EXPECT_EQ(job_load_timing.proxy_resolve_start,
1663 load_timing_result.proxy_resolve_start);
1664 EXPECT_EQ(job_load_timing.proxy_resolve_end,
1665 load_timing_result.proxy_resolve_end);
1666 EXPECT_EQ(job_load_timing.connect_timing.dns_start,
1667 load_timing_result.connect_timing.dns_start);
1668 EXPECT_EQ(job_load_timing.connect_timing.dns_end,
1669 load_timing_result.connect_timing.dns_end);
1670 EXPECT_EQ(job_load_timing.connect_timing.connect_start,
1671 load_timing_result.connect_timing.connect_start);
1672 EXPECT_EQ(job_load_timing.connect_timing.connect_end,
1673 load_timing_result.connect_timing.connect_end);
1674 EXPECT_EQ(job_load_timing.connect_timing.ssl_start,
1675 load_timing_result.connect_timing.ssl_start);
1676 EXPECT_EQ(job_load_timing.connect_timing.ssl_end,
1677 load_timing_result.connect_timing.ssl_end);
1678
1679 // Redundant sanity check.
1680 TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_DNS_TIMES);
1681}
1682
1683// Another basic test, with proxy and SSL times, but no DNS times.
1684TEST_F(URLRequestTest, InterceptLoadTimingProxy) {
1685 base::TimeTicks now = base::TimeTicks::Now();
1686 LoadTimingInfo job_load_timing =
1687 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, true);
1688
[email protected]2ca01e52013-10-31 22:05:191689 LoadTimingInfo load_timing_result =
1690 RunLoadTimingTest(job_load_timing, &default_context_);
[email protected]2bba3252013-04-08 19:50:591691
1692 // Nothing should have been changed by the URLRequest.
1693 EXPECT_EQ(job_load_timing.proxy_resolve_start,
1694 load_timing_result.proxy_resolve_start);
1695 EXPECT_EQ(job_load_timing.proxy_resolve_end,
1696 load_timing_result.proxy_resolve_end);
1697 EXPECT_EQ(job_load_timing.connect_timing.dns_start,
1698 load_timing_result.connect_timing.dns_start);
1699 EXPECT_EQ(job_load_timing.connect_timing.dns_end,
1700 load_timing_result.connect_timing.dns_end);
1701 EXPECT_EQ(job_load_timing.connect_timing.connect_start,
1702 load_timing_result.connect_timing.connect_start);
1703 EXPECT_EQ(job_load_timing.connect_timing.connect_end,
1704 load_timing_result.connect_timing.connect_end);
1705 EXPECT_EQ(job_load_timing.connect_timing.ssl_start,
1706 load_timing_result.connect_timing.ssl_start);
1707 EXPECT_EQ(job_load_timing.connect_timing.ssl_end,
1708 load_timing_result.connect_timing.ssl_end);
1709
1710 // Redundant sanity check.
1711 TestLoadTimingNotReusedWithProxy(load_timing_result,
1712 CONNECT_TIMING_HAS_SSL_TIMES);
1713}
1714
1715// Make sure that URLRequest correctly adjusts proxy times when they're before
1716// |request_start|, due to already having a connected socket. This happens in
[email protected]cf4cae32014-05-27 00:39:101717// the case of reusing a SPDY session. The connected socket is not considered
1718// reused in this test (May be a preconnect).
[email protected]2bba3252013-04-08 19:50:591719//
1720// To mix things up from the test above, assumes DNS times but no SSL times.
1721TEST_F(URLRequestTest, InterceptLoadTimingEarlyProxyResolution) {
1722 base::TimeTicks now = base::TimeTicks::Now();
1723 LoadTimingInfo job_load_timing =
1724 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, true);
1725 job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(6);
1726 job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(5);
1727 job_load_timing.connect_timing.dns_start = now - base::TimeDelta::FromDays(4);
1728 job_load_timing.connect_timing.dns_end = now - base::TimeDelta::FromDays(3);
1729 job_load_timing.connect_timing.connect_start =
1730 now - base::TimeDelta::FromDays(2);
1731 job_load_timing.connect_timing.connect_end =
1732 now - base::TimeDelta::FromDays(1);
1733
[email protected]2ca01e52013-10-31 22:05:191734 LoadTimingInfo load_timing_result =
1735 RunLoadTimingTest(job_load_timing, &default_context_);
[email protected]2bba3252013-04-08 19:50:591736
1737 // Proxy times, connect times, and DNS times should all be replaced with
1738 // request_start.
1739 EXPECT_EQ(load_timing_result.request_start,
1740 load_timing_result.proxy_resolve_start);
1741 EXPECT_EQ(load_timing_result.request_start,
1742 load_timing_result.proxy_resolve_end);
1743 EXPECT_EQ(load_timing_result.request_start,
1744 load_timing_result.connect_timing.dns_start);
1745 EXPECT_EQ(load_timing_result.request_start,
1746 load_timing_result.connect_timing.dns_end);
1747 EXPECT_EQ(load_timing_result.request_start,
1748 load_timing_result.connect_timing.connect_start);
1749 EXPECT_EQ(load_timing_result.request_start,
1750 load_timing_result.connect_timing.connect_end);
1751
1752 // Other times should have been left null.
1753 TestLoadTimingNotReusedWithProxy(load_timing_result,
1754 CONNECT_TIMING_HAS_DNS_TIMES);
1755}
1756
1757// Same as above, but in the reused case.
1758TEST_F(URLRequestTest, InterceptLoadTimingEarlyProxyResolutionReused) {
1759 base::TimeTicks now = base::TimeTicks::Now();
1760 LoadTimingInfo job_load_timing = NormalLoadTimingInfoReused(now, true);
1761 job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(4);
1762 job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(3);
1763
[email protected]2ca01e52013-10-31 22:05:191764 LoadTimingInfo load_timing_result =
1765 RunLoadTimingTest(job_load_timing, &default_context_);
[email protected]2bba3252013-04-08 19:50:591766
1767 // Proxy times and connect times should all be replaced with request_start.
1768 EXPECT_EQ(load_timing_result.request_start,
1769 load_timing_result.proxy_resolve_start);
1770 EXPECT_EQ(load_timing_result.request_start,
1771 load_timing_result.proxy_resolve_end);
1772
1773 // Other times should have been left null.
1774 TestLoadTimingReusedWithProxy(load_timing_result);
1775}
1776
1777// Make sure that URLRequest correctly adjusts connect times when they're before
1778// |request_start|, due to reusing a connected socket. The connected socket is
1779// not considered reused in this test (May be a preconnect).
1780//
1781// To mix things up, the request has SSL times, but no DNS times.
1782TEST_F(URLRequestTest, InterceptLoadTimingEarlyConnect) {
1783 base::TimeTicks now = base::TimeTicks::Now();
1784 LoadTimingInfo job_load_timing =
1785 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, false);
1786 job_load_timing.connect_timing.connect_start =
1787 now - base::TimeDelta::FromDays(1);
1788 job_load_timing.connect_timing.ssl_start = now - base::TimeDelta::FromDays(2);
1789 job_load_timing.connect_timing.ssl_end = now - base::TimeDelta::FromDays(3);
1790 job_load_timing.connect_timing.connect_end =
1791 now - base::TimeDelta::FromDays(4);
1792
[email protected]2ca01e52013-10-31 22:05:191793 LoadTimingInfo load_timing_result =
1794 RunLoadTimingTest(job_load_timing, &default_context_);
[email protected]2bba3252013-04-08 19:50:591795
1796 // Connect times, and SSL times should be replaced with request_start.
1797 EXPECT_EQ(load_timing_result.request_start,
1798 load_timing_result.connect_timing.connect_start);
1799 EXPECT_EQ(load_timing_result.request_start,
1800 load_timing_result.connect_timing.ssl_start);
1801 EXPECT_EQ(load_timing_result.request_start,
1802 load_timing_result.connect_timing.ssl_end);
1803 EXPECT_EQ(load_timing_result.request_start,
1804 load_timing_result.connect_timing.connect_end);
1805
1806 // Other times should have been left null.
1807 TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_SSL_TIMES);
1808}
1809
1810// Make sure that URLRequest correctly adjusts connect times when they're before
1811// |request_start|, due to reusing a connected socket in the case that there
1812// are also proxy times. The connected socket is not considered reused in this
1813// test (May be a preconnect).
1814//
1815// In this test, there are no SSL or DNS times.
1816TEST_F(URLRequestTest, InterceptLoadTimingEarlyConnectWithProxy) {
1817 base::TimeTicks now = base::TimeTicks::Now();
1818 LoadTimingInfo job_load_timing =
1819 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY, true);
1820 job_load_timing.connect_timing.connect_start =
1821 now - base::TimeDelta::FromDays(1);
1822 job_load_timing.connect_timing.connect_end =
1823 now - base::TimeDelta::FromDays(2);
1824
[email protected]2ca01e52013-10-31 22:05:191825 LoadTimingInfo load_timing_result =
1826 RunLoadTimingTest(job_load_timing, &default_context_);
[email protected]2bba3252013-04-08 19:50:591827
1828 // Connect times should be replaced with proxy_resolve_end.
1829 EXPECT_EQ(load_timing_result.proxy_resolve_end,
1830 load_timing_result.connect_timing.connect_start);
1831 EXPECT_EQ(load_timing_result.proxy_resolve_end,
1832 load_timing_result.connect_timing.connect_end);
1833
1834 // Other times should have been left null.
1835 TestLoadTimingNotReusedWithProxy(load_timing_result,
1836 CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY);
1837}
1838
[email protected]316c1e5e2012-09-12 15:17:441839// Check that two different URL requests have different identifiers.
1840TEST_F(URLRequestTest, Identifiers) {
1841 TestDelegate d;
1842 TestURLRequestContext context;
mmenke19378d22014-09-09 04:12:591843 scoped_ptr<URLRequest> req(context.CreateRequest(
1844 GURL("http://example.com"), DEFAULT_PRIORITY, &d, NULL));
1845 scoped_ptr<URLRequest> other_req(context.CreateRequest(
1846 GURL("http://example.com"), DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:441847
mmenke19378d22014-09-09 04:12:591848 ASSERT_NE(req->identifier(), other_req->identifier());
[email protected]316c1e5e2012-09-12 15:17:441849}
1850
1851// Check that a failure to connect to the proxy is reported to the network
1852// delegate.
1853TEST_F(URLRequestTest, NetworkDelegateProxyError) {
1854 MockHostResolver host_resolver;
1855 host_resolver.rules()->AddSimulatedFailure("*");
1856
[email protected]ceefd7fd2012-11-29 00:36:241857 TestNetworkDelegate network_delegate; // Must outlive URLRequests.
[email protected]316c1e5e2012-09-12 15:17:441858 TestURLRequestContextWithProxy context("myproxy:70", &network_delegate);
1859
1860 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:191861 scoped_ptr<URLRequest> req(context.CreateRequest(
1862 GURL("http://example.com"), DEFAULT_PRIORITY, &d, NULL));
1863 req->set_method("GET");
[email protected]316c1e5e2012-09-12 15:17:441864
[email protected]f7022f32014-08-21 16:32:191865 req->Start();
[email protected]255620da2013-08-19 13:14:291866 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441867
1868 // Check we see a failed request.
[email protected]f7022f32014-08-21 16:32:191869 EXPECT_FALSE(req->status().is_success());
[email protected]d8fc4722014-06-13 13:17:151870 // The proxy server is not set before failure.
[email protected]f7022f32014-08-21 16:32:191871 EXPECT_TRUE(req->proxy_server().IsEmpty());
1872 EXPECT_EQ(URLRequestStatus::FAILED, req->status().status());
1873 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, req->status().error());
[email protected]316c1e5e2012-09-12 15:17:441874
1875 EXPECT_EQ(1, network_delegate.error_count());
1876 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, network_delegate.last_error());
1877 EXPECT_EQ(1, network_delegate.completed_requests());
1878}
1879
[email protected]cba24642014-08-15 20:49:591880// Make sure that NetworkDelegate::NotifyCompleted is called if
[email protected]316c1e5e2012-09-12 15:17:441881// content is empty.
1882TEST_F(URLRequestTest, RequestCompletionForEmptyResponse) {
1883 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:191884 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1885 GURL("data:,"), DEFAULT_PRIORITY, &d, NULL));
1886 req->Start();
[email protected]255620da2013-08-19 13:14:291887 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441888 EXPECT_EQ("", d.data_received());
1889 EXPECT_EQ(1, default_network_delegate_.completed_requests());
1890}
1891
[email protected]5033ab82013-03-22 20:17:461892// Make sure that SetPriority actually sets the URLRequest's priority
1893// correctly, both before and after start.
1894TEST_F(URLRequestTest, SetPriorityBasic) {
1895 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:191896 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1897 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL));
1898 EXPECT_EQ(DEFAULT_PRIORITY, req->priority());
[email protected]5033ab82013-03-22 20:17:461899
[email protected]f7022f32014-08-21 16:32:191900 req->SetPriority(LOW);
1901 EXPECT_EQ(LOW, req->priority());
[email protected]5033ab82013-03-22 20:17:461902
[email protected]f7022f32014-08-21 16:32:191903 req->Start();
1904 EXPECT_EQ(LOW, req->priority());
[email protected]5033ab82013-03-22 20:17:461905
[email protected]f7022f32014-08-21 16:32:191906 req->SetPriority(MEDIUM);
1907 EXPECT_EQ(MEDIUM, req->priority());
[email protected]5033ab82013-03-22 20:17:461908}
1909
1910// Make sure that URLRequest calls SetPriority on a job before calling
1911// Start on it.
1912TEST_F(URLRequestTest, SetJobPriorityBeforeJobStart) {
1913 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:191914 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1915 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL));
1916 EXPECT_EQ(DEFAULT_PRIORITY, req->priority());
[email protected]5033ab82013-03-22 20:17:461917
1918 scoped_refptr<URLRequestTestJob> job =
[email protected]f7022f32014-08-21 16:32:191919 new URLRequestTestJob(req.get(), &default_network_delegate_);
[email protected]90499482013-06-01 00:39:501920 AddTestInterceptor()->set_main_intercept_job(job.get());
[email protected]5033ab82013-03-22 20:17:461921 EXPECT_EQ(DEFAULT_PRIORITY, job->priority());
1922
[email protected]f7022f32014-08-21 16:32:191923 req->SetPriority(LOW);
[email protected]5033ab82013-03-22 20:17:461924
[email protected]f7022f32014-08-21 16:32:191925 req->Start();
[email protected]5033ab82013-03-22 20:17:461926 EXPECT_EQ(LOW, job->priority());
1927}
1928
1929// Make sure that URLRequest passes on its priority updates to its
1930// job.
1931TEST_F(URLRequestTest, SetJobPriority) {
1932 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:191933 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1934 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL));
[email protected]5033ab82013-03-22 20:17:461935
1936 scoped_refptr<URLRequestTestJob> job =
[email protected]f7022f32014-08-21 16:32:191937 new URLRequestTestJob(req.get(), &default_network_delegate_);
[email protected]90499482013-06-01 00:39:501938 AddTestInterceptor()->set_main_intercept_job(job.get());
[email protected]5033ab82013-03-22 20:17:461939
[email protected]f7022f32014-08-21 16:32:191940 req->SetPriority(LOW);
1941 req->Start();
[email protected]5033ab82013-03-22 20:17:461942 EXPECT_EQ(LOW, job->priority());
1943
[email protected]f7022f32014-08-21 16:32:191944 req->SetPriority(MEDIUM);
1945 EXPECT_EQ(MEDIUM, req->priority());
[email protected]5033ab82013-03-22 20:17:461946 EXPECT_EQ(MEDIUM, job->priority());
1947}
1948
[email protected]bb1c4662013-11-14 00:00:071949// Setting the IGNORE_LIMITS load flag should be okay if the priority
1950// is MAXIMUM_PRIORITY.
1951TEST_F(URLRequestTest, PriorityIgnoreLimits) {
1952 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:191953 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1954 GURL("http://test_intercept/foo"), MAXIMUM_PRIORITY, &d, NULL));
1955 EXPECT_EQ(MAXIMUM_PRIORITY, req->priority());
[email protected]bb1c4662013-11-14 00:00:071956
1957 scoped_refptr<URLRequestTestJob> job =
[email protected]f7022f32014-08-21 16:32:191958 new URLRequestTestJob(req.get(), &default_network_delegate_);
[email protected]bb1c4662013-11-14 00:00:071959 AddTestInterceptor()->set_main_intercept_job(job.get());
1960
[email protected]f7022f32014-08-21 16:32:191961 req->SetLoadFlags(LOAD_IGNORE_LIMITS);
1962 EXPECT_EQ(MAXIMUM_PRIORITY, req->priority());
[email protected]bb1c4662013-11-14 00:00:071963
[email protected]f7022f32014-08-21 16:32:191964 req->SetPriority(MAXIMUM_PRIORITY);
1965 EXPECT_EQ(MAXIMUM_PRIORITY, req->priority());
[email protected]bb1c4662013-11-14 00:00:071966
[email protected]f7022f32014-08-21 16:32:191967 req->Start();
1968 EXPECT_EQ(MAXIMUM_PRIORITY, req->priority());
[email protected]bb1c4662013-11-14 00:00:071969 EXPECT_EQ(MAXIMUM_PRIORITY, job->priority());
1970}
1971
[email protected]ce7d0cbc2013-05-03 18:57:221972// TODO(droger): Support SpawnedTestServer on iOS (see http://crbug.com/148666).
[email protected]316c1e5e2012-09-12 15:17:441973#if !defined(OS_IOS)
[email protected]ce7d0cbc2013-05-03 18:57:221974// A subclass of SpawnedTestServer that uses a statically-configured hostname.
1975// This is to work around mysterious failures in chrome_frame_net_tests. See:
[email protected]316c1e5e2012-09-12 15:17:441976// http://crbug.com/114369
[email protected]cd6f2522014-01-16 18:27:351977// TODO(erikwright): remove or update as needed; see http://crbug.com/334634.
[email protected]ce7d0cbc2013-05-03 18:57:221978class LocalHttpTestServer : public SpawnedTestServer {
[email protected]316c1e5e2012-09-12 15:17:441979 public:
[email protected]6cdfd7f2013-02-08 20:40:151980 explicit LocalHttpTestServer(const base::FilePath& document_root)
[email protected]ce7d0cbc2013-05-03 18:57:221981 : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP,
1982 ScopedCustomUrlRequestTestHttpHost::value(),
1983 document_root) {}
[email protected]316c1e5e2012-09-12 15:17:441984 LocalHttpTestServer()
[email protected]ce7d0cbc2013-05-03 18:57:221985 : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP,
1986 ScopedCustomUrlRequestTestHttpHost::value(),
1987 base::FilePath()) {}
[email protected]316c1e5e2012-09-12 15:17:441988};
1989
[email protected]f2f31b32013-01-16 23:24:091990TEST_F(URLRequestTest, DelayedCookieCallback) {
[email protected]316c1e5e2012-09-12 15:17:441991 LocalHttpTestServer test_server;
1992 ASSERT_TRUE(test_server.Start());
1993
1994 TestURLRequestContext context;
1995 scoped_refptr<DelayedCookieMonster> delayed_cm =
1996 new DelayedCookieMonster();
1997 scoped_refptr<CookieStore> cookie_store = delayed_cm;
[email protected]90499482013-06-01 00:39:501998 context.set_cookie_store(delayed_cm.get());
[email protected]316c1e5e2012-09-12 15:17:441999
2000 // Set up a cookie.
2001 {
2002 TestNetworkDelegate network_delegate;
2003 context.set_network_delegate(&network_delegate);
2004 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192005 scoped_ptr<URLRequest> req(context.CreateRequest(
2006 test_server.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY,
2007 &d, NULL));
2008 req->Start();
[email protected]255620da2013-08-19 13:14:292009 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442010 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2011 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2012 EXPECT_EQ(1, network_delegate.set_cookie_count());
2013 }
2014
2015 // Verify that the cookie is set.
2016 {
2017 TestNetworkDelegate network_delegate;
2018 context.set_network_delegate(&network_delegate);
2019 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192020 scoped_ptr<URLRequest> req(context.CreateRequest(
2021 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL));
2022 req->Start();
[email protected]255620da2013-08-19 13:14:292023 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442024
2025 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2026 != std::string::npos);
2027 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2028 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2029 }
2030}
2031
[email protected]f2f31b32013-01-16 23:24:092032TEST_F(URLRequestTest, DoNotSendCookies) {
[email protected]316c1e5e2012-09-12 15:17:442033 LocalHttpTestServer test_server;
2034 ASSERT_TRUE(test_server.Start());
2035
2036 // Set up a cookie.
2037 {
2038 TestNetworkDelegate network_delegate;
2039 default_context_.set_network_delegate(&network_delegate);
2040 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192041 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2042 test_server.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY,
2043 &d, NULL));
2044 req->Start();
[email protected]255620da2013-08-19 13:14:292045 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442046 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2047 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2048 }
2049
2050 // Verify that the cookie is set.
2051 {
2052 TestNetworkDelegate network_delegate;
2053 default_context_.set_network_delegate(&network_delegate);
2054 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192055 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2056 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL));
2057 req->Start();
[email protected]255620da2013-08-19 13:14:292058 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442059
2060 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2061 != std::string::npos);
2062 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2063 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2064 }
2065
2066 // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set.
2067 {
2068 TestNetworkDelegate network_delegate;
2069 default_context_.set_network_delegate(&network_delegate);
2070 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192071 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2072 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL));
2073 req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES);
2074 req->Start();
[email protected]255620da2013-08-19 13:14:292075 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442076
2077 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
2078 == std::string::npos);
2079
2080 // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies.
2081 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2082 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2083 }
2084}
2085
2086TEST_F(URLRequestTest, DoNotSaveCookies) {
2087 LocalHttpTestServer test_server;
2088 ASSERT_TRUE(test_server.Start());
2089
2090 // Set up a cookie.
2091 {
2092 TestNetworkDelegate network_delegate;
2093 default_context_.set_network_delegate(&network_delegate);
2094 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192095 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2096 test_server.GetURL("set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY,
2097 &d, NULL));
2098 req->Start();
[email protected]255620da2013-08-19 13:14:292099 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442100
2101 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2102 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2103 EXPECT_EQ(1, network_delegate.set_cookie_count());
2104 }
2105
2106 // Try to set-up another cookie and update the previous cookie.
2107 {
2108 TestNetworkDelegate network_delegate;
2109 default_context_.set_network_delegate(&network_delegate);
2110 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192111 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
[email protected]316c1e5e2012-09-12 15:17:442112 test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
[email protected]f7022f32014-08-21 16:32:192113 DEFAULT_PRIORITY, &d, NULL));
2114 req->SetLoadFlags(LOAD_DO_NOT_SAVE_COOKIES);
2115 req->Start();
[email protected]316c1e5e2012-09-12 15:17:442116
[email protected]255620da2013-08-19 13:14:292117 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442118
2119 // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie.
2120 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2121 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2122 EXPECT_EQ(0, network_delegate.set_cookie_count());
2123 }
2124
2125 // Verify the cookies weren't saved or updated.
2126 {
2127 TestNetworkDelegate network_delegate;
2128 default_context_.set_network_delegate(&network_delegate);
2129 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192130 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2131 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL));
2132 req->Start();
[email protected]255620da2013-08-19 13:14:292133 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442134
2135 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
2136 == std::string::npos);
2137 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
2138 != std::string::npos);
2139
2140 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2141 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2142 EXPECT_EQ(0, network_delegate.set_cookie_count());
2143 }
2144}
2145
2146TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) {
2147 LocalHttpTestServer test_server;
2148 ASSERT_TRUE(test_server.Start());
2149
2150 // Set up a cookie.
2151 {
2152 TestNetworkDelegate network_delegate;
2153 default_context_.set_network_delegate(&network_delegate);
2154 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192155 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2156 test_server.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY,
2157 &d, NULL));
2158 req->Start();
[email protected]255620da2013-08-19 13:14:292159 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442160
2161 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2162 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2163 }
2164
2165 // Verify that the cookie is set.
2166 {
2167 TestNetworkDelegate network_delegate;
2168 default_context_.set_network_delegate(&network_delegate);
2169 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192170 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2171 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL));
2172 req->Start();
[email protected]255620da2013-08-19 13:14:292173 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442174
2175 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2176 != std::string::npos);
2177
2178 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2179 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2180 }
2181
2182 // Verify that the cookie isn't sent.
2183 {
2184 TestNetworkDelegate network_delegate;
2185 default_context_.set_network_delegate(&network_delegate);
2186 TestDelegate d;
2187 network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES);
[email protected]f7022f32014-08-21 16:32:192188 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2189 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL));
2190 req->Start();
[email protected]255620da2013-08-19 13:14:292191 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442192
2193 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
2194 == std::string::npos);
2195
[email protected]22e045f2013-09-20 03:54:032196 EXPECT_EQ(1, network_delegate.blocked_get_cookies_count());
[email protected]316c1e5e2012-09-12 15:17:442197 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2198 }
2199}
2200
2201TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) {
2202 LocalHttpTestServer test_server;
2203 ASSERT_TRUE(test_server.Start());
2204
2205 // Set up a cookie.
2206 {
2207 TestNetworkDelegate network_delegate;
2208 default_context_.set_network_delegate(&network_delegate);
2209 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192210 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2211 test_server.GetURL("set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY,
2212 &d, NULL));
2213 req->Start();
[email protected]255620da2013-08-19 13:14:292214 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442215
2216 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2217 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2218 }
2219
2220 // Try to set-up another cookie and update the previous cookie.
2221 {
2222 TestNetworkDelegate network_delegate;
2223 default_context_.set_network_delegate(&network_delegate);
2224 TestDelegate d;
2225 network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE);
[email protected]f7022f32014-08-21 16:32:192226 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
[email protected]316c1e5e2012-09-12 15:17:442227 test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
[email protected]f7022f32014-08-21 16:32:192228 DEFAULT_PRIORITY, &d, NULL));
2229 req->Start();
[email protected]316c1e5e2012-09-12 15:17:442230
[email protected]255620da2013-08-19 13:14:292231 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442232
2233 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2234 EXPECT_EQ(2, network_delegate.blocked_set_cookie_count());
2235 }
2236
2237 // Verify the cookies weren't saved or updated.
2238 {
2239 TestNetworkDelegate network_delegate;
2240 default_context_.set_network_delegate(&network_delegate);
2241 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192242 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2243 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL));
2244 req->Start();
[email protected]255620da2013-08-19 13:14:292245 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442246
2247 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
2248 == std::string::npos);
2249 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
2250 != std::string::npos);
2251
2252 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2253 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2254 }
2255}
2256
2257TEST_F(URLRequestTest, DoNotSaveEmptyCookies) {
2258 LocalHttpTestServer test_server;
2259 ASSERT_TRUE(test_server.Start());
2260
2261 // Set up an empty cookie.
2262 {
2263 TestNetworkDelegate network_delegate;
2264 default_context_.set_network_delegate(&network_delegate);
2265 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192266 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2267 test_server.GetURL("set-cookie"), DEFAULT_PRIORITY, &d, NULL));
2268 req->Start();
[email protected]255620da2013-08-19 13:14:292269 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442270
2271 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2272 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2273 EXPECT_EQ(0, network_delegate.set_cookie_count());
2274 }
2275}
2276
2277TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) {
2278 LocalHttpTestServer test_server;
2279 ASSERT_TRUE(test_server.Start());
2280
2281 // Set up a cookie.
2282 {
2283 TestNetworkDelegate network_delegate;
2284 default_context_.set_network_delegate(&network_delegate);
2285 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192286 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2287 test_server.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY,
2288 &d, NULL));
2289 req->Start();
[email protected]255620da2013-08-19 13:14:292290 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442291
2292 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2293 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2294 }
2295
2296 // Verify that the cookie is set.
2297 {
2298 TestNetworkDelegate network_delegate;
2299 default_context_.set_network_delegate(&network_delegate);
2300 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192301 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2302 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL));
2303 req->Start();
[email protected]255620da2013-08-19 13:14:292304 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442305
2306 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2307 != std::string::npos);
2308
2309 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2310 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2311 }
2312
2313 // Verify that the cookie isn't sent.
2314 {
2315 TestNetworkDelegate network_delegate;
2316 default_context_.set_network_delegate(&network_delegate);
2317 TestDelegate d;
2318 network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES);
[email protected]f7022f32014-08-21 16:32:192319 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2320 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL));
2321 req->Start();
[email protected]255620da2013-08-19 13:14:292322 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442323
2324 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
2325 == std::string::npos);
2326
[email protected]22e045f2013-09-20 03:54:032327 EXPECT_EQ(1, network_delegate.blocked_get_cookies_count());
[email protected]316c1e5e2012-09-12 15:17:442328 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2329 }
2330}
2331
2332TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) {
2333 LocalHttpTestServer test_server;
2334 ASSERT_TRUE(test_server.Start());
2335
2336 // Set up a cookie.
2337 {
2338 TestNetworkDelegate network_delegate;
2339 default_context_.set_network_delegate(&network_delegate);
2340 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192341 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2342 test_server.GetURL("set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY,
2343 &d, NULL));
2344 req->Start();
[email protected]255620da2013-08-19 13:14:292345 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442346
2347 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2348 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2349 }
2350
2351 // Try to set-up another cookie and update the previous cookie.
2352 {
2353 TestNetworkDelegate network_delegate;
2354 default_context_.set_network_delegate(&network_delegate);
2355 TestDelegate d;
2356 network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE);
[email protected]f7022f32014-08-21 16:32:192357 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
[email protected]316c1e5e2012-09-12 15:17:442358 test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
[email protected]f7022f32014-08-21 16:32:192359 DEFAULT_PRIORITY, &d, NULL));
2360 req->Start();
[email protected]316c1e5e2012-09-12 15:17:442361
[email protected]255620da2013-08-19 13:14:292362 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442363
2364 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2365 EXPECT_EQ(2, network_delegate.blocked_set_cookie_count());
2366 }
2367
2368 // Verify the cookies weren't saved or updated.
2369 {
2370 TestNetworkDelegate network_delegate;
2371 default_context_.set_network_delegate(&network_delegate);
2372 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192373 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2374 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL));
2375 req->Start();
[email protected]255620da2013-08-19 13:14:292376 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442377
2378 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
2379 == std::string::npos);
2380 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
2381 != std::string::npos);
2382
2383 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2384 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2385 }
2386}
2387
[email protected]5095cd72012-11-01 10:29:162388// FixedDateNetworkDelegate swaps out the server's HTTP Date response header
2389// value for the |fixed_date| argument given to the constructor.
2390class FixedDateNetworkDelegate : public TestNetworkDelegate {
2391 public:
2392 explicit FixedDateNetworkDelegate(const std::string& fixed_date)
2393 : fixed_date_(fixed_date) {}
dchengb03027d2014-10-21 12:00:202394 ~FixedDateNetworkDelegate() override {}
[email protected]5095cd72012-11-01 10:29:162395
[email protected]cba24642014-08-15 20:49:592396 // NetworkDelegate implementation
dchengb03027d2014-10-21 12:00:202397 int OnHeadersReceived(
[email protected]cba24642014-08-15 20:49:592398 URLRequest* request,
2399 const CompletionCallback& callback,
2400 const HttpResponseHeaders* original_response_headers,
2401 scoped_refptr<HttpResponseHeaders>* override_response_headers,
mostynbba063d6032014-10-09 11:01:132402 GURL* allowed_unsafe_redirect_url) override;
[email protected]5095cd72012-11-01 10:29:162403
2404 private:
2405 std::string fixed_date_;
2406
2407 DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate);
2408};
2409
2410int FixedDateNetworkDelegate::OnHeadersReceived(
[email protected]cba24642014-08-15 20:49:592411 URLRequest* request,
2412 const CompletionCallback& callback,
2413 const HttpResponseHeaders* original_response_headers,
2414 scoped_refptr<HttpResponseHeaders>* override_response_headers,
[email protected]5f714132014-03-26 10:41:162415 GURL* allowed_unsafe_redirect_url) {
[email protected]cba24642014-08-15 20:49:592416 HttpResponseHeaders* new_response_headers =
2417 new HttpResponseHeaders(original_response_headers->raw_headers());
[email protected]5095cd72012-11-01 10:29:162418
2419 new_response_headers->RemoveHeader("Date");
2420 new_response_headers->AddHeader("Date: " + fixed_date_);
2421
2422 *override_response_headers = new_response_headers;
2423 return TestNetworkDelegate::OnHeadersReceived(request,
2424 callback,
2425 original_response_headers,
[email protected]5f714132014-03-26 10:41:162426 override_response_headers,
2427 allowed_unsafe_redirect_url);
[email protected]5095cd72012-11-01 10:29:162428}
2429
2430// Test that cookie expiration times are adjusted for server/client clock
2431// skew and that we handle incorrect timezone specifier "UTC" in HTTP Date
2432// headers by defaulting to GMT. (crbug.com/135131)
2433TEST_F(URLRequestTest, AcceptClockSkewCookieWithWrongDateTimezone) {
2434 LocalHttpTestServer test_server;
2435 ASSERT_TRUE(test_server.Start());
2436
2437 // Set up an expired cookie.
2438 {
2439 TestNetworkDelegate network_delegate;
2440 default_context_.set_network_delegate(&network_delegate);
2441 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192442 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
[email protected]2ca01e52013-10-31 22:05:192443 test_server.GetURL(
2444 "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"),
[email protected]f7022f32014-08-21 16:32:192445 DEFAULT_PRIORITY, &d, NULL));
2446 req->Start();
[email protected]255620da2013-08-19 13:14:292447 base::RunLoop().Run();
[email protected]5095cd72012-11-01 10:29:162448 }
2449 // Verify that the cookie is not set.
2450 {
2451 TestNetworkDelegate network_delegate;
2452 default_context_.set_network_delegate(&network_delegate);
2453 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192454 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2455 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL));
2456 req->Start();
[email protected]255620da2013-08-19 13:14:292457 base::RunLoop().Run();
[email protected]5095cd72012-11-01 10:29:162458
2459 EXPECT_TRUE(d.data_received().find("StillGood=1") == std::string::npos);
2460 }
2461 // Set up a cookie with clock skew and "UTC" HTTP Date timezone specifier.
2462 {
2463 FixedDateNetworkDelegate network_delegate("18-Apr-1977 22:49:13 UTC");
2464 default_context_.set_network_delegate(&network_delegate);
2465 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192466 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
[email protected]2ca01e52013-10-31 22:05:192467 test_server.GetURL(
2468 "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"),
[email protected]f7022f32014-08-21 16:32:192469 DEFAULT_PRIORITY, &d, NULL));
2470 req->Start();
[email protected]255620da2013-08-19 13:14:292471 base::RunLoop().Run();
[email protected]5095cd72012-11-01 10:29:162472 }
2473 // Verify that the cookie is set.
2474 {
2475 TestNetworkDelegate network_delegate;
2476 default_context_.set_network_delegate(&network_delegate);
2477 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192478 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2479 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL));
2480 req->Start();
[email protected]255620da2013-08-19 13:14:292481 base::RunLoop().Run();
[email protected]5095cd72012-11-01 10:29:162482
2483 EXPECT_TRUE(d.data_received().find("StillGood=1") != std::string::npos);
2484 }
2485}
2486
2487
[email protected]316c1e5e2012-09-12 15:17:442488// Check that it is impossible to change the referrer in the extra headers of
2489// an URLRequest.
2490TEST_F(URLRequestTest, DoNotOverrideReferrer) {
2491 LocalHttpTestServer test_server;
2492 ASSERT_TRUE(test_server.Start());
2493
2494 // If extra headers contain referer and the request contains a referer,
2495 // only the latter shall be respected.
2496 {
2497 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192498 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2499 test_server.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d, NULL));
2500 req->SetReferrer("http://foo.com/");
[email protected]316c1e5e2012-09-12 15:17:442501
2502 HttpRequestHeaders headers;
2503 headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/");
[email protected]f7022f32014-08-21 16:32:192504 req->SetExtraRequestHeaders(headers);
[email protected]316c1e5e2012-09-12 15:17:442505
[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("http://foo.com/", d.data_received());
2510 }
2511
2512 // If extra headers contain a referer but the request does not, no referer
2513 // shall be sent in the header.
2514 {
2515 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192516 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2517 test_server.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:442518
2519 HttpRequestHeaders headers;
2520 headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/");
[email protected]f7022f32014-08-21 16:32:192521 req->SetExtraRequestHeaders(headers);
2522 req->SetLoadFlags(LOAD_VALIDATE_CACHE);
[email protected]316c1e5e2012-09-12 15:17:442523
[email protected]f7022f32014-08-21 16:32:192524 req->Start();
[email protected]255620da2013-08-19 13:14:292525 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442526
2527 EXPECT_EQ("None", d.data_received());
2528 }
2529}
2530
[email protected]b89290212009-08-14 22:37:352531class URLRequestTestHTTP : public URLRequestTest {
[email protected]95409e12010-08-17 20:07:112532 public:
2533 URLRequestTestHTTP()
[email protected]6cdfd7f2013-02-08 20:40:152534 : test_server_(base::FilePath(FILE_PATH_LITERAL(
[email protected]95409e12010-08-17 20:07:112535 "net/data/url_request_unittest"))) {
2536 }
2537
[email protected]b89290212009-08-14 22:37:352538 protected:
[email protected]21184962011-10-26 00:50:302539 // Requests |redirect_url|, which must return a HTTP 3xx redirect.
2540 // |request_method| is the method to use for the initial request.
2541 // |redirect_method| is the method that is expected to be used for the second
2542 // request, after redirection.
2543 // If |include_data| is true, data is uploaded with the request. The
2544 // response body is expected to match it exactly, if and only if
2545 // |request_method| == |redirect_method|.
2546 void HTTPRedirectMethodTest(const GURL& redirect_url,
2547 const std::string& request_method,
2548 const std::string& redirect_method,
2549 bool include_data) {
2550 static const char kData[] = "hello world";
2551 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192552 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2553 redirect_url, DEFAULT_PRIORITY, &d, NULL));
2554 req->set_method(request_method);
[email protected]21184962011-10-26 00:50:302555 if (include_data) {
mmenkecbc2b712014-10-09 20:29:072556 req->set_upload(CreateSimpleUploadData(kData));
[email protected]21184962011-10-26 00:50:302557 HttpRequestHeaders headers;
2558 headers.SetHeader(HttpRequestHeaders::kContentLength,
2559 base::UintToString(arraysize(kData) - 1));
[email protected]f7022f32014-08-21 16:32:192560 req->SetExtraRequestHeaders(headers);
[email protected]21184962011-10-26 00:50:302561 }
[email protected]f7022f32014-08-21 16:32:192562 req->Start();
[email protected]255620da2013-08-19 13:14:292563 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:192564 EXPECT_EQ(redirect_method, req->method());
2565 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
2566 EXPECT_EQ(OK, req->status().error());
[email protected]21184962011-10-26 00:50:302567 if (include_data) {
2568 if (request_method == redirect_method) {
2569 EXPECT_EQ(kData, d.data_received());
2570 } else {
2571 EXPECT_NE(kData, d.data_received());
2572 }
2573 }
2574 if (HasFailure())
2575 LOG(WARNING) << "Request method was: " << request_method;
2576 }
2577
[email protected]762d2db2010-01-11 19:03:012578 void HTTPUploadDataOperationTest(const std::string& method) {
[email protected]762d2db2010-01-11 19:03:012579 const int kMsgSize = 20000; // multiple of 10
2580 const int kIterations = 50;
[email protected]f43b89f32012-05-01 19:39:482581 char* uploadBytes = new char[kMsgSize+1];
2582 char* ptr = uploadBytes;
[email protected]762d2db2010-01-11 19:03:012583 char marker = 'a';
2584 for (int idx = 0; idx < kMsgSize/10; idx++) {
2585 memcpy(ptr, "----------", 10);
2586 ptr += 10;
2587 if (idx % 100 == 0) {
2588 ptr--;
2589 *ptr++ = marker;
2590 if (++marker > 'z')
2591 marker = 'a';
2592 }
2593 }
2594 uploadBytes[kMsgSize] = '\0';
2595
[email protected]762d2db2010-01-11 19:03:012596 for (int i = 0; i < kIterations; ++i) {
2597 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192598 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
2599 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, NULL));
2600 r->set_method(method.c_str());
[email protected]762d2db2010-01-11 19:03:012601
mmenkecbc2b712014-10-09 20:29:072602 r->set_upload(CreateSimpleUploadData(uploadBytes));
[email protected]762d2db2010-01-11 19:03:012603
[email protected]f7022f32014-08-21 16:32:192604 r->Start();
2605 EXPECT_TRUE(r->is_pending());
[email protected]762d2db2010-01-11 19:03:012606
[email protected]255620da2013-08-19 13:14:292607 base::RunLoop().Run();
[email protected]762d2db2010-01-11 19:03:012608
[email protected]329b68b2012-11-14 17:54:272609 ASSERT_EQ(1, d.response_started_count())
[email protected]f7022f32014-08-21 16:32:192610 << "request failed: " << r->status().status()
2611 << ", os error: " << r->status().error();
[email protected]762d2db2010-01-11 19:03:012612
2613 EXPECT_FALSE(d.received_data_before_response());
2614 EXPECT_EQ(uploadBytes, d.data_received());
[email protected]762d2db2010-01-11 19:03:012615 }
2616 delete[] uploadBytes;
2617 }
2618
[email protected]ef2bf422012-05-11 03:27:092619 void AddChunksToUpload(URLRequest* r) {
[email protected]0c9bf872011-03-04 17:53:222620 r->AppendChunkToUpload("a", 1, false);
2621 r->AppendChunkToUpload("bcd", 3, false);
2622 r->AppendChunkToUpload("this is a longer chunk than before.", 35, false);
2623 r->AppendChunkToUpload("\r\n\r\n", 4, false);
2624 r->AppendChunkToUpload("0", 1, false);
2625 r->AppendChunkToUpload("2323", 4, true);
[email protected]699efe602011-01-25 07:17:112626 }
2627
[email protected]ef2bf422012-05-11 03:27:092628 void VerifyReceivedDataMatchesChunks(URLRequest* r, TestDelegate* d) {
[email protected]699efe602011-01-25 07:17:112629 // This should match the chunks sent by AddChunksToUpload().
[email protected]329b68b2012-11-14 17:54:272630 const std::string expected_data =
[email protected]699efe602011-01-25 07:17:112631 "abcdthis is a longer chunk than before.\r\n\r\n02323";
2632
[email protected]329b68b2012-11-14 17:54:272633 ASSERT_EQ(1, d->response_started_count())
2634 << "request failed: " << r->status().status()
2635 << ", os error: " << r->status().error();
[email protected]699efe602011-01-25 07:17:112636
2637 EXPECT_FALSE(d->received_data_before_response());
2638
[email protected]329b68b2012-11-14 17:54:272639 EXPECT_EQ(expected_data.size(), static_cast<size_t>(d->bytes_received()));
2640 EXPECT_EQ(expected_data, d->data_received());
[email protected]699efe602011-01-25 07:17:112641 }
2642
[email protected]ede03212012-09-07 12:52:262643 bool DoManyCookiesRequest(int num_cookies) {
[email protected]263163f2012-06-14 22:40:342644 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192645 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
2646 test_server_.GetURL("set-many-cookies?" +
2647 base::IntToString(num_cookies)),
2648 DEFAULT_PRIORITY, &d, NULL));
[email protected]263163f2012-06-14 22:40:342649
[email protected]f7022f32014-08-21 16:32:192650 r->Start();
2651 EXPECT_TRUE(r->is_pending());
[email protected]263163f2012-06-14 22:40:342652
[email protected]255620da2013-08-19 13:14:292653 base::RunLoop().Run();
[email protected]263163f2012-06-14 22:40:342654
[email protected]f7022f32014-08-21 16:32:192655 bool is_success = r->status().is_success();
[email protected]263163f2012-06-14 22:40:342656
[email protected]ede03212012-09-07 12:52:262657 if (!is_success) {
[email protected]f7022f32014-08-21 16:32:192658 EXPECT_TRUE(r->status().error() == ERR_RESPONSE_HEADERS_TOO_BIG);
[email protected]263163f2012-06-14 22:40:342659 // The test server appears to be unable to handle subsequent requests
2660 // after this error is triggered. Force it to restart.
2661 EXPECT_TRUE(test_server_.Stop());
2662 EXPECT_TRUE(test_server_.Start());
2663 }
2664
2665 return is_success;
2666 }
2667
[email protected]1700c6a2012-02-22 18:07:072668 LocalHttpTestServer test_server_;
[email protected]b89290212009-08-14 22:37:352669};
2670
[email protected]95409e12010-08-17 20:07:112671// In this unit test, we're using the HTTPTestServer as a proxy server and
2672// issuing a CONNECT request with the magic host name "www.redirect.com".
2673// The HTTPTestServer will return a 302 response, which we should not
2674// follow.
[email protected]f2f31b32013-01-16 23:24:092675TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) {
[email protected]95409e12010-08-17 20:07:112676 ASSERT_TRUE(test_server_.Start());
2677
[email protected]ceefd7fd2012-11-29 00:36:242678 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]d5a4dd62012-05-23 01:41:042679 TestURLRequestContextWithProxy context(
[email protected]2ca01e52013-10-31 22:05:192680 test_server_.host_port_pair().ToString(), &network_delegate);
[email protected]87a09a92011-07-14 15:50:502681
[email protected]d1ec59082009-02-11 02:48:152682 TestDelegate d;
2683 {
[email protected]f7022f32014-08-21 16:32:192684 scoped_ptr<URLRequest> r(context.CreateRequest(
2685 GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, NULL));
2686 r->Start();
2687 EXPECT_TRUE(r->is_pending());
[email protected]d1ec59082009-02-11 02:48:152688
[email protected]255620da2013-08-19 13:14:292689 base::RunLoop().Run();
[email protected]d1ec59082009-02-11 02:48:152690
[email protected]f7022f32014-08-21 16:32:192691 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
[email protected]d8fc4722014-06-13 13:17:152692 // The proxy server is not set before failure.
[email protected]f7022f32014-08-21 16:32:192693 EXPECT_TRUE(r->proxy_server().IsEmpty());
2694 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r->status().error());
[email protected]dc651782009-02-14 01:45:082695 EXPECT_EQ(1, d.response_started_count());
[email protected]d1ec59082009-02-11 02:48:152696 // We should not have followed the redirect.
2697 EXPECT_EQ(0, d.received_redirect_count());
2698 }
2699}
2700
[email protected]8202d0c2011-02-23 08:31:142701// This is the same as the previous test, but checks that the network delegate
2702// registers the error.
[email protected]c044616e2013-02-20 02:01:262703TEST_F(URLRequestTestHTTP, NetworkDelegateTunnelConnectionFailed) {
[email protected]8202d0c2011-02-23 08:31:142704 ASSERT_TRUE(test_server_.Start());
2705
[email protected]ceefd7fd2012-11-29 00:36:242706 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]d5a4dd62012-05-23 01:41:042707 TestURLRequestContextWithProxy context(
[email protected]2ca01e52013-10-31 22:05:192708 test_server_.host_port_pair().ToString(), &network_delegate);
[email protected]87a09a92011-07-14 15:50:502709
[email protected]8202d0c2011-02-23 08:31:142710 TestDelegate d;
2711 {
[email protected]f7022f32014-08-21 16:32:192712 scoped_ptr<URLRequest> r(context.CreateRequest(
2713 GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, NULL));
2714 r->Start();
2715 EXPECT_TRUE(r->is_pending());
[email protected]8202d0c2011-02-23 08:31:142716
[email protected]255620da2013-08-19 13:14:292717 base::RunLoop().Run();
[email protected]8202d0c2011-02-23 08:31:142718
[email protected]f7022f32014-08-21 16:32:192719 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
[email protected]d8fc4722014-06-13 13:17:152720 // The proxy server is not set before failure.
[email protected]f7022f32014-08-21 16:32:192721 EXPECT_TRUE(r->proxy_server().IsEmpty());
2722 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r->status().error());
[email protected]8202d0c2011-02-23 08:31:142723 EXPECT_EQ(1, d.response_started_count());
2724 // We should not have followed the redirect.
2725 EXPECT_EQ(0, d.received_redirect_count());
2726
2727 EXPECT_EQ(1, network_delegate.error_count());
[email protected]d0cc35b2011-09-08 12:02:052728 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, network_delegate.last_error());
[email protected]8202d0c2011-02-23 08:31:142729 }
2730}
2731
[email protected]dc5a5cf2012-09-26 02:49:302732// Tests that we can block and asynchronously return OK in various stages.
2733TEST_F(URLRequestTestHTTP, NetworkDelegateBlockAsynchronously) {
2734 static const BlockingNetworkDelegate::Stage blocking_stages[] = {
2735 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
2736 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
2737 BlockingNetworkDelegate::ON_HEADERS_RECEIVED
2738 };
2739 static const size_t blocking_stages_length = arraysize(blocking_stages);
2740
2741 ASSERT_TRUE(test_server_.Start());
2742
2743 TestDelegate d;
2744 BlockingNetworkDelegate network_delegate(
2745 BlockingNetworkDelegate::USER_CALLBACK);
2746 network_delegate.set_block_on(
2747 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST |
2748 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS |
2749 BlockingNetworkDelegate::ON_HEADERS_RECEIVED);
2750
2751 TestURLRequestContext context(true);
2752 context.set_network_delegate(&network_delegate);
2753 context.Init();
2754
2755 {
[email protected]f7022f32014-08-21 16:32:192756 scoped_ptr<URLRequest> r(context.CreateRequest(
2757 test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d, NULL));
[email protected]dc5a5cf2012-09-26 02:49:302758
[email protected]f7022f32014-08-21 16:32:192759 r->Start();
[email protected]dc5a5cf2012-09-26 02:49:302760 for (size_t i = 0; i < blocking_stages_length; ++i) {
[email protected]255620da2013-08-19 13:14:292761 base::RunLoop().Run();
[email protected]dc5a5cf2012-09-26 02:49:302762 EXPECT_EQ(blocking_stages[i],
2763 network_delegate.stage_blocked_for_callback());
2764 network_delegate.DoCallback(OK);
2765 }
[email protected]255620da2013-08-19 13:14:292766 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:192767 EXPECT_EQ(200, r->GetResponseCode());
2768 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
[email protected]dc5a5cf2012-09-26 02:49:302769 EXPECT_EQ(1, network_delegate.created_requests());
2770 EXPECT_EQ(0, network_delegate.destroyed_requests());
2771 }
2772 EXPECT_EQ(1, network_delegate.destroyed_requests());
2773}
2774
[email protected]4c76d7c2011-04-15 19:14:122775// Tests that the network delegate can block and cancel a request.
2776TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) {
2777 ASSERT_TRUE(test_server_.Start());
2778
2779 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302780 BlockingNetworkDelegate network_delegate(
2781 BlockingNetworkDelegate::AUTO_CALLBACK);
2782 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
2783 network_delegate.set_retval(ERR_EMPTY_RESPONSE);
[email protected]4c76d7c2011-04-15 19:14:122784
[email protected]d5a4dd62012-05-23 01:41:042785 TestURLRequestContextWithProxy context(
[email protected]2ca01e52013-10-31 22:05:192786 test_server_.host_port_pair().ToString(), &network_delegate);
[email protected]87a09a92011-07-14 15:50:502787
[email protected]4c76d7c2011-04-15 19:14:122788 {
[email protected]f7022f32014-08-21 16:32:192789 scoped_ptr<URLRequest> r(context.CreateRequest(
2790 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL));
[email protected]4c76d7c2011-04-15 19:14:122791
[email protected]f7022f32014-08-21 16:32:192792 r->Start();
[email protected]255620da2013-08-19 13:14:292793 base::RunLoop().Run();
[email protected]4c76d7c2011-04-15 19:14:122794
[email protected]f7022f32014-08-21 16:32:192795 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
[email protected]d8fc4722014-06-13 13:17:152796 // The proxy server is not set before cancellation.
[email protected]f7022f32014-08-21 16:32:192797 EXPECT_TRUE(r->proxy_server().IsEmpty());
2798 EXPECT_EQ(ERR_EMPTY_RESPONSE, r->status().error());
[email protected]4c76d7c2011-04-15 19:14:122799 EXPECT_EQ(1, network_delegate.created_requests());
2800 EXPECT_EQ(0, network_delegate.destroyed_requests());
2801 }
2802 EXPECT_EQ(1, network_delegate.destroyed_requests());
2803}
2804
[email protected]b4438d32012-09-27 06:15:302805// Helper function for NetworkDelegateCancelRequestAsynchronously and
2806// NetworkDelegateCancelRequestSynchronously. Sets up a blocking network
2807// delegate operating in |block_mode| and a request for |url|. It blocks the
2808// request in |stage| and cancels it with ERR_BLOCKED_BY_CLIENT.
2809void NetworkDelegateCancelRequest(BlockingNetworkDelegate::BlockMode block_mode,
2810 BlockingNetworkDelegate::Stage stage,
2811 const GURL& url) {
[email protected]3cd384c602011-08-31 16:12:362812 TestDelegate d;
[email protected]b4438d32012-09-27 06:15:302813 BlockingNetworkDelegate network_delegate(block_mode);
2814 network_delegate.set_retval(ERR_BLOCKED_BY_CLIENT);
2815 network_delegate.set_block_on(stage);
[email protected]3cd384c602011-08-31 16:12:362816
[email protected]b4438d32012-09-27 06:15:302817 TestURLRequestContext context(true);
2818 context.set_network_delegate(&network_delegate);
2819 context.Init();
[email protected]3cd384c602011-08-31 16:12:362820
2821 {
[email protected]f7022f32014-08-21 16:32:192822 scoped_ptr<URLRequest> r(context.CreateRequest(
2823 url, DEFAULT_PRIORITY, &d, NULL));
[email protected]3cd384c602011-08-31 16:12:362824
[email protected]f7022f32014-08-21 16:32:192825 r->Start();
[email protected]255620da2013-08-19 13:14:292826 base::RunLoop().Run();
[email protected]3cd384c602011-08-31 16:12:362827
[email protected]f7022f32014-08-21 16:32:192828 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
[email protected]d8fc4722014-06-13 13:17:152829 // The proxy server is not set before cancellation.
[email protected]f7022f32014-08-21 16:32:192830 EXPECT_TRUE(r->proxy_server().IsEmpty());
2831 EXPECT_EQ(ERR_BLOCKED_BY_CLIENT, r->status().error());
[email protected]3cd384c602011-08-31 16:12:362832 EXPECT_EQ(1, network_delegate.created_requests());
2833 EXPECT_EQ(0, network_delegate.destroyed_requests());
2834 }
2835 EXPECT_EQ(1, network_delegate.destroyed_requests());
2836}
2837
[email protected]b4438d32012-09-27 06:15:302838// The following 3 tests check that the network delegate can cancel a request
2839// synchronously in various stages of the request.
2840TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously1) {
2841 ASSERT_TRUE(test_server_.Start());
2842 NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS,
2843 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
[email protected]007b3f82013-04-09 08:46:452844 test_server_.GetURL(std::string()));
[email protected]b4438d32012-09-27 06:15:302845}
2846
2847TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously2) {
2848 ASSERT_TRUE(test_server_.Start());
2849 NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS,
2850 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
[email protected]007b3f82013-04-09 08:46:452851 test_server_.GetURL(std::string()));
[email protected]b4438d32012-09-27 06:15:302852}
2853
2854TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously3) {
2855 ASSERT_TRUE(test_server_.Start());
2856 NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS,
2857 BlockingNetworkDelegate::ON_HEADERS_RECEIVED,
[email protected]007b3f82013-04-09 08:46:452858 test_server_.GetURL(std::string()));
[email protected]b4438d32012-09-27 06:15:302859}
2860
2861// The following 3 tests check that the network delegate can cancel a request
2862// asynchronously in various stages of the request.
2863TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously1) {
2864 ASSERT_TRUE(test_server_.Start());
2865 NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK,
2866 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
[email protected]007b3f82013-04-09 08:46:452867 test_server_.GetURL(std::string()));
[email protected]b4438d32012-09-27 06:15:302868}
2869
2870TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously2) {
2871 ASSERT_TRUE(test_server_.Start());
2872 NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK,
2873 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
[email protected]007b3f82013-04-09 08:46:452874 test_server_.GetURL(std::string()));
[email protected]b4438d32012-09-27 06:15:302875}
2876
2877TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously3) {
2878 ASSERT_TRUE(test_server_.Start());
2879 NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK,
2880 BlockingNetworkDelegate::ON_HEADERS_RECEIVED,
[email protected]007b3f82013-04-09 08:46:452881 test_server_.GetURL(std::string()));
[email protected]b4438d32012-09-27 06:15:302882}
2883
[email protected]4c76d7c2011-04-15 19:14:122884// Tests that the network delegate can block and redirect a request to a new
2885// URL.
2886TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) {
2887 ASSERT_TRUE(test_server_.Start());
2888
2889 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302890 BlockingNetworkDelegate network_delegate(
2891 BlockingNetworkDelegate::AUTO_CALLBACK);
2892 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
[email protected]4c76d7c2011-04-15 19:14:122893 GURL redirect_url(test_server_.GetURL("simple.html"));
2894 network_delegate.set_redirect_url(redirect_url);
2895
[email protected]d5a4dd62012-05-23 01:41:042896 TestURLRequestContextWithProxy context(
[email protected]2ca01e52013-10-31 22:05:192897 test_server_.host_port_pair().ToString(), &network_delegate);
[email protected]87a09a92011-07-14 15:50:502898
[email protected]4c76d7c2011-04-15 19:14:122899 {
[email protected]8f1ac082011-04-19 21:14:132900 GURL original_url(test_server_.GetURL("empty.html"));
[email protected]f7022f32014-08-21 16:32:192901 scoped_ptr<URLRequest> r(context.CreateRequest(
2902 original_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]4c76d7c2011-04-15 19:14:122903
[email protected]6be6fa92014-08-06 23:44:562904 // Quit after hitting the redirect, so can check the headers.
2905 d.set_quit_on_redirect(true);
[email protected]f7022f32014-08-21 16:32:192906 r->Start();
[email protected]255620da2013-08-19 13:14:292907 base::RunLoop().Run();
[email protected]4c76d7c2011-04-15 19:14:122908
[email protected]6be6fa92014-08-06 23:44:562909 // Check headers from URLRequestJob.
[email protected]f7022f32014-08-21 16:32:192910 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
2911 EXPECT_EQ(307, r->GetResponseCode());
2912 EXPECT_EQ(307, r->response_headers()->response_code());
[email protected]6be6fa92014-08-06 23:44:562913 std::string location;
[email protected]f7022f32014-08-21 16:32:192914 ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location",
2915 &location));
[email protected]6be6fa92014-08-06 23:44:562916 EXPECT_EQ(redirect_url, GURL(location));
2917
2918 // Let the request finish.
[email protected]f7022f32014-08-21 16:32:192919 r->FollowDeferredRedirect();
[email protected]6be6fa92014-08-06 23:44:562920 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:192921 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
2922 EXPECT_TRUE(r->proxy_server().Equals(test_server_.host_port_pair()));
[email protected]597a1ab2014-06-26 08:12:272923 EXPECT_EQ(
2924 1, network_delegate.observed_before_proxy_headers_sent_callbacks());
2925 EXPECT_TRUE(
2926 network_delegate.last_observed_proxy().Equals(
2927 test_server_.host_port_pair()));
2928
[email protected]f7022f32014-08-21 16:32:192929 EXPECT_EQ(0, r->status().error());
2930 EXPECT_EQ(redirect_url, r->url());
2931 EXPECT_EQ(original_url, r->original_url());
2932 EXPECT_EQ(2U, r->url_chain().size());
[email protected]4c76d7c2011-04-15 19:14:122933 EXPECT_EQ(1, network_delegate.created_requests());
2934 EXPECT_EQ(0, network_delegate.destroyed_requests());
2935 }
2936 EXPECT_EQ(1, network_delegate.destroyed_requests());
2937}
2938
[email protected]b813ed72012-04-05 08:21:362939// Tests that the network delegate can block and redirect a request to a new
2940// URL by setting a redirect_url and returning in OnBeforeURLRequest directly.
2941TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) {
2942 ASSERT_TRUE(test_server_.Start());
2943
2944 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302945 BlockingNetworkDelegate network_delegate(
2946 BlockingNetworkDelegate::SYNCHRONOUS);
[email protected]b813ed72012-04-05 08:21:362947 GURL redirect_url(test_server_.GetURL("simple.html"));
2948 network_delegate.set_redirect_url(redirect_url);
[email protected]b813ed72012-04-05 08:21:362949
[email protected]d5a4dd62012-05-23 01:41:042950 TestURLRequestContextWithProxy context(
[email protected]2ca01e52013-10-31 22:05:192951 test_server_.host_port_pair().ToString(), &network_delegate);
[email protected]b813ed72012-04-05 08:21:362952
2953 {
2954 GURL original_url(test_server_.GetURL("empty.html"));
[email protected]f7022f32014-08-21 16:32:192955 scoped_ptr<URLRequest> r(context.CreateRequest(
2956 original_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]b813ed72012-04-05 08:21:362957
[email protected]6be6fa92014-08-06 23:44:562958 // Quit after hitting the redirect, so can check the headers.
2959 d.set_quit_on_redirect(true);
[email protected]f7022f32014-08-21 16:32:192960 r->Start();
[email protected]255620da2013-08-19 13:14:292961 base::RunLoop().Run();
[email protected]b813ed72012-04-05 08:21:362962
[email protected]6be6fa92014-08-06 23:44:562963 // Check headers from URLRequestJob.
[email protected]f7022f32014-08-21 16:32:192964 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
2965 EXPECT_EQ(307, r->GetResponseCode());
2966 EXPECT_EQ(307, r->response_headers()->response_code());
[email protected]6be6fa92014-08-06 23:44:562967 std::string location;
[email protected]f7022f32014-08-21 16:32:192968 ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location",
2969 &location));
[email protected]6be6fa92014-08-06 23:44:562970 EXPECT_EQ(redirect_url, GURL(location));
2971
2972 // Let the request finish.
[email protected]f7022f32014-08-21 16:32:192973 r->FollowDeferredRedirect();
[email protected]6be6fa92014-08-06 23:44:562974 base::RunLoop().Run();
2975
[email protected]f7022f32014-08-21 16:32:192976 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
2977 EXPECT_TRUE(r->proxy_server().Equals(test_server_.host_port_pair()));
[email protected]597a1ab2014-06-26 08:12:272978 EXPECT_EQ(
2979 1, network_delegate.observed_before_proxy_headers_sent_callbacks());
2980 EXPECT_TRUE(
2981 network_delegate.last_observed_proxy().Equals(
2982 test_server_.host_port_pair()));
[email protected]f7022f32014-08-21 16:32:192983 EXPECT_EQ(0, r->status().error());
2984 EXPECT_EQ(redirect_url, r->url());
2985 EXPECT_EQ(original_url, r->original_url());
2986 EXPECT_EQ(2U, r->url_chain().size());
[email protected]b813ed72012-04-05 08:21:362987 EXPECT_EQ(1, network_delegate.created_requests());
2988 EXPECT_EQ(0, network_delegate.destroyed_requests());
2989 }
2990 EXPECT_EQ(1, network_delegate.destroyed_requests());
2991}
2992
[email protected]3c5ca8c2011-09-29 01:14:512993// Tests that redirects caused by the network delegate preserve POST data.
2994TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) {
2995 ASSERT_TRUE(test_server_.Start());
2996
2997 const char kData[] = "hello world";
2998
2999 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303000 BlockingNetworkDelegate network_delegate(
3001 BlockingNetworkDelegate::AUTO_CALLBACK);
3002 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
[email protected]3c5ca8c2011-09-29 01:14:513003 GURL redirect_url(test_server_.GetURL("echo"));
3004 network_delegate.set_redirect_url(redirect_url);
3005
[email protected]ef2bf422012-05-11 03:27:093006 TestURLRequestContext context(true);
3007 context.set_network_delegate(&network_delegate);
3008 context.Init();
[email protected]3c5ca8c2011-09-29 01:14:513009
3010 {
3011 GURL original_url(test_server_.GetURL("empty.html"));
[email protected]f7022f32014-08-21 16:32:193012 scoped_ptr<URLRequest> r(context.CreateRequest(
3013 original_url, DEFAULT_PRIORITY, &d, NULL));
3014 r->set_method("POST");
mmenkecbc2b712014-10-09 20:29:073015 r->set_upload(CreateSimpleUploadData(kData));
[email protected]3c5ca8c2011-09-29 01:14:513016 HttpRequestHeaders headers;
3017 headers.SetHeader(HttpRequestHeaders::kContentLength,
3018 base::UintToString(arraysize(kData) - 1));
[email protected]f7022f32014-08-21 16:32:193019 r->SetExtraRequestHeaders(headers);
[email protected]6be6fa92014-08-06 23:44:563020
3021 // Quit after hitting the redirect, so can check the headers.
3022 d.set_quit_on_redirect(true);
[email protected]f7022f32014-08-21 16:32:193023 r->Start();
[email protected]255620da2013-08-19 13:14:293024 base::RunLoop().Run();
[email protected]3c5ca8c2011-09-29 01:14:513025
[email protected]6be6fa92014-08-06 23:44:563026 // Check headers from URLRequestJob.
[email protected]f7022f32014-08-21 16:32:193027 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3028 EXPECT_EQ(307, r->GetResponseCode());
3029 EXPECT_EQ(307, r->response_headers()->response_code());
[email protected]6be6fa92014-08-06 23:44:563030 std::string location;
[email protected]f7022f32014-08-21 16:32:193031 ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location",
3032 &location));
[email protected]6be6fa92014-08-06 23:44:563033 EXPECT_EQ(redirect_url, GURL(location));
3034
3035 // Let the request finish.
[email protected]f7022f32014-08-21 16:32:193036 r->FollowDeferredRedirect();
[email protected]6be6fa92014-08-06 23:44:563037 base::RunLoop().Run();
3038
[email protected]f7022f32014-08-21 16:32:193039 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3040 EXPECT_EQ(0, r->status().error());
3041 EXPECT_EQ(redirect_url, r->url());
3042 EXPECT_EQ(original_url, r->original_url());
3043 EXPECT_EQ(2U, r->url_chain().size());
[email protected]3c5ca8c2011-09-29 01:14:513044 EXPECT_EQ(1, network_delegate.created_requests());
3045 EXPECT_EQ(0, network_delegate.destroyed_requests());
[email protected]f7022f32014-08-21 16:32:193046 EXPECT_EQ("POST", r->method());
[email protected]3c5ca8c2011-09-29 01:14:513047 EXPECT_EQ(kData, d.data_received());
3048 }
3049 EXPECT_EQ(1, network_delegate.destroyed_requests());
3050}
3051
[email protected]5f714132014-03-26 10:41:163052// Tests that the network delegate can block and redirect a request to a new
3053// URL during OnHeadersReceived.
3054TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestOnHeadersReceived) {
3055 ASSERT_TRUE(test_server_.Start());
3056
3057 TestDelegate d;
3058 BlockingNetworkDelegate network_delegate(
3059 BlockingNetworkDelegate::AUTO_CALLBACK);
3060 network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED);
3061 GURL redirect_url(test_server_.GetURL("simple.html"));
3062 network_delegate.set_redirect_on_headers_received_url(redirect_url);
3063
3064 TestURLRequestContextWithProxy context(
3065 test_server_.host_port_pair().ToString(), &network_delegate);
3066
3067 {
3068 GURL original_url(test_server_.GetURL("empty.html"));
[email protected]f7022f32014-08-21 16:32:193069 scoped_ptr<URLRequest> r(context.CreateRequest(
3070 original_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]5f714132014-03-26 10:41:163071
[email protected]f7022f32014-08-21 16:32:193072 r->Start();
[email protected]5f714132014-03-26 10:41:163073 base::RunLoop().Run();
3074
[email protected]f7022f32014-08-21 16:32:193075 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3076 EXPECT_TRUE(r->proxy_server().Equals(test_server_.host_port_pair()));
[email protected]597a1ab2014-06-26 08:12:273077 EXPECT_EQ(
3078 2, network_delegate.observed_before_proxy_headers_sent_callbacks());
3079 EXPECT_TRUE(
3080 network_delegate.last_observed_proxy().Equals(
3081 test_server_.host_port_pair()));
[email protected]f7022f32014-08-21 16:32:193082
3083 EXPECT_EQ(OK, r->status().error());
3084 EXPECT_EQ(redirect_url, r->url());
3085 EXPECT_EQ(original_url, r->original_url());
3086 EXPECT_EQ(2U, r->url_chain().size());
[email protected]5f714132014-03-26 10:41:163087 EXPECT_EQ(2, network_delegate.created_requests());
3088 EXPECT_EQ(0, network_delegate.destroyed_requests());
3089 }
3090 EXPECT_EQ(1, network_delegate.destroyed_requests());
3091}
3092
[email protected]c2911d72011-10-03 22:16:363093// Tests that the network delegate can synchronously complete OnAuthRequired
3094// by taking no action. This indicates that the NetworkDelegate does not want to
3095// handle the challenge, and is passing the buck along to the
3096// URLRequest::Delegate.
3097TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) {
3098 ASSERT_TRUE(test_server_.Start());
3099
3100 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303101 BlockingNetworkDelegate network_delegate(
3102 BlockingNetworkDelegate::SYNCHRONOUS);
[email protected]c2911d72011-10-03 22:16:363103
[email protected]ef2bf422012-05-11 03:27:093104 TestURLRequestContext context(true);
3105 context.set_network_delegate(&network_delegate);
3106 context.Init();
[email protected]c2911d72011-10-03 22:16:363107
[email protected]f3cf9802011-10-28 18:44:583108 d.set_credentials(AuthCredentials(kUser, kSecret));
[email protected]c2911d72011-10-03 22:16:363109
3110 {
3111 GURL url(test_server_.GetURL("auth-basic"));
[email protected]f7022f32014-08-21 16:32:193112 scoped_ptr<URLRequest> r(context.CreateRequest(
3113 url, DEFAULT_PRIORITY, &d, NULL));
3114 r->Start();
[email protected]79e1fd62013-06-20 06:50:043115
[email protected]255620da2013-08-19 13:14:293116 base::RunLoop().Run();
[email protected]79e1fd62013-06-20 06:50:043117
[email protected]f7022f32014-08-21 16:32:193118 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3119 EXPECT_EQ(0, r->status().error());
3120 EXPECT_EQ(200, r->GetResponseCode());
[email protected]79e1fd62013-06-20 06:50:043121 EXPECT_TRUE(d.auth_required_called());
3122 EXPECT_EQ(1, network_delegate.created_requests());
3123 EXPECT_EQ(0, network_delegate.destroyed_requests());
3124 }
3125 EXPECT_EQ(1, network_delegate.destroyed_requests());
3126}
3127
3128TEST_F(URLRequestTestHTTP,
3129 NetworkDelegateOnAuthRequiredSyncNoAction_GetFullRequestHeaders) {
3130 ASSERT_TRUE(test_server_.Start());
3131
3132 TestDelegate d;
3133 BlockingNetworkDelegate network_delegate(
3134 BlockingNetworkDelegate::SYNCHRONOUS);
3135
3136 TestURLRequestContext context(true);
3137 context.set_network_delegate(&network_delegate);
3138 context.Init();
3139
3140 d.set_credentials(AuthCredentials(kUser, kSecret));
3141
3142 {
3143 GURL url(test_server_.GetURL("auth-basic"));
[email protected]f7022f32014-08-21 16:32:193144 scoped_ptr<URLRequest> r(context.CreateRequest(
3145 url, DEFAULT_PRIORITY, &d, NULL));
3146 r->Start();
[email protected]79e1fd62013-06-20 06:50:043147
3148 {
3149 HttpRequestHeaders headers;
[email protected]f7022f32014-08-21 16:32:193150 EXPECT_TRUE(r->GetFullRequestHeaders(&headers));
[email protected]79e1fd62013-06-20 06:50:043151 EXPECT_FALSE(headers.HasHeader("Authorization"));
3152 }
3153
[email protected]255620da2013-08-19 13:14:293154 base::RunLoop().Run();
[email protected]c2911d72011-10-03 22:16:363155
[email protected]f7022f32014-08-21 16:32:193156 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3157 EXPECT_EQ(0, r->status().error());
3158 EXPECT_EQ(200, r->GetResponseCode());
[email protected]c2911d72011-10-03 22:16:363159 EXPECT_TRUE(d.auth_required_called());
3160 EXPECT_EQ(1, network_delegate.created_requests());
3161 EXPECT_EQ(0, network_delegate.destroyed_requests());
3162 }
3163 EXPECT_EQ(1, network_delegate.destroyed_requests());
3164}
3165
3166// Tests that the network delegate can synchronously complete OnAuthRequired
[email protected]1e110eae2013-05-10 22:02:403167// by setting credentials.
[email protected]c2911d72011-10-03 22:16:363168TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) {
3169 ASSERT_TRUE(test_server_.Start());
3170
3171 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303172 BlockingNetworkDelegate network_delegate(
3173 BlockingNetworkDelegate::SYNCHRONOUS);
3174 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]c2911d72011-10-03 22:16:363175 network_delegate.set_auth_retval(
3176 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
3177
[email protected]f3cf9802011-10-28 18:44:583178 network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret));
[email protected]c2911d72011-10-03 22:16:363179
[email protected]ef2bf422012-05-11 03:27:093180 TestURLRequestContext context(true);
3181 context.set_network_delegate(&network_delegate);
3182 context.Init();
[email protected]c2911d72011-10-03 22:16:363183
3184 {
3185 GURL url(test_server_.GetURL("auth-basic"));
[email protected]f7022f32014-08-21 16:32:193186 scoped_ptr<URLRequest> r(context.CreateRequest(
3187 url, DEFAULT_PRIORITY, &d, NULL));
3188 r->Start();
[email protected]255620da2013-08-19 13:14:293189 base::RunLoop().Run();
[email protected]c2911d72011-10-03 22:16:363190
[email protected]f7022f32014-08-21 16:32:193191 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3192 EXPECT_EQ(0, r->status().error());
3193 EXPECT_EQ(200, r->GetResponseCode());
[email protected]c2911d72011-10-03 22:16:363194 EXPECT_FALSE(d.auth_required_called());
3195 EXPECT_EQ(1, network_delegate.created_requests());
3196 EXPECT_EQ(0, network_delegate.destroyed_requests());
3197 }
3198 EXPECT_EQ(1, network_delegate.destroyed_requests());
3199}
3200
[email protected]79e1fd62013-06-20 06:50:043201// Same as above, but also tests that GetFullRequestHeaders returns the proper
3202// headers (for the first or second request) when called at the proper times.
3203TEST_F(URLRequestTestHTTP,
3204 NetworkDelegateOnAuthRequiredSyncSetAuth_GetFullRequestHeaders) {
3205 ASSERT_TRUE(test_server_.Start());
3206
3207 TestDelegate d;
3208 BlockingNetworkDelegate network_delegate(
3209 BlockingNetworkDelegate::SYNCHRONOUS);
3210 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
3211 network_delegate.set_auth_retval(
3212 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
3213
3214 network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret));
3215
3216 TestURLRequestContext context(true);
3217 context.set_network_delegate(&network_delegate);
3218 context.Init();
3219
3220 {
3221 GURL url(test_server_.GetURL("auth-basic"));
[email protected]f7022f32014-08-21 16:32:193222 scoped_ptr<URLRequest> r(context.CreateRequest(
3223 url, DEFAULT_PRIORITY, &d, NULL));
3224 r->Start();
[email protected]255620da2013-08-19 13:14:293225 base::RunLoop().Run();
[email protected]79e1fd62013-06-20 06:50:043226
[email protected]f7022f32014-08-21 16:32:193227 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3228 EXPECT_EQ(0, r->status().error());
3229 EXPECT_EQ(200, r->GetResponseCode());
[email protected]79e1fd62013-06-20 06:50:043230 EXPECT_FALSE(d.auth_required_called());
3231 EXPECT_EQ(1, network_delegate.created_requests());
3232 EXPECT_EQ(0, network_delegate.destroyed_requests());
3233
3234 {
3235 HttpRequestHeaders headers;
[email protected]f7022f32014-08-21 16:32:193236 EXPECT_TRUE(r->GetFullRequestHeaders(&headers));
[email protected]79e1fd62013-06-20 06:50:043237 EXPECT_TRUE(headers.HasHeader("Authorization"));
3238 }
3239 }
3240 EXPECT_EQ(1, network_delegate.destroyed_requests());
3241}
3242
[email protected]c2911d72011-10-03 22:16:363243// Tests that the network delegate can synchronously complete OnAuthRequired
3244// by cancelling authentication.
3245TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) {
3246 ASSERT_TRUE(test_server_.Start());
3247
3248 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303249 BlockingNetworkDelegate network_delegate(
3250 BlockingNetworkDelegate::SYNCHRONOUS);
3251 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]c2911d72011-10-03 22:16:363252 network_delegate.set_auth_retval(
3253 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH);
3254
[email protected]ef2bf422012-05-11 03:27:093255 TestURLRequestContext context(true);
3256 context.set_network_delegate(&network_delegate);
3257 context.Init();
[email protected]c2911d72011-10-03 22:16:363258
3259 {
3260 GURL url(test_server_.GetURL("auth-basic"));
[email protected]f7022f32014-08-21 16:32:193261 scoped_ptr<URLRequest> r(context.CreateRequest(
3262 url, DEFAULT_PRIORITY, &d, NULL));
3263 r->Start();
[email protected]255620da2013-08-19 13:14:293264 base::RunLoop().Run();
[email protected]c2911d72011-10-03 22:16:363265
[email protected]f7022f32014-08-21 16:32:193266 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3267 EXPECT_EQ(OK, r->status().error());
3268 EXPECT_EQ(401, r->GetResponseCode());
[email protected]c2911d72011-10-03 22:16:363269 EXPECT_FALSE(d.auth_required_called());
3270 EXPECT_EQ(1, network_delegate.created_requests());
3271 EXPECT_EQ(0, network_delegate.destroyed_requests());
3272 }
3273 EXPECT_EQ(1, network_delegate.destroyed_requests());
3274}
3275
3276// Tests that the network delegate can asynchronously complete OnAuthRequired
3277// by taking no action. This indicates that the NetworkDelegate does not want
3278// to handle the challenge, and is passing the buck along to the
3279// URLRequest::Delegate.
3280TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) {
3281 ASSERT_TRUE(test_server_.Start());
3282
3283 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303284 BlockingNetworkDelegate network_delegate(
3285 BlockingNetworkDelegate::AUTO_CALLBACK);
3286 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]c2911d72011-10-03 22:16:363287
[email protected]ef2bf422012-05-11 03:27:093288 TestURLRequestContext context(true);
3289 context.set_network_delegate(&network_delegate);
3290 context.Init();
[email protected]c2911d72011-10-03 22:16:363291
[email protected]f3cf9802011-10-28 18:44:583292 d.set_credentials(AuthCredentials(kUser, kSecret));
[email protected]c2911d72011-10-03 22:16:363293
3294 {
3295 GURL url(test_server_.GetURL("auth-basic"));
[email protected]f7022f32014-08-21 16:32:193296 scoped_ptr<URLRequest> r(context.CreateRequest(
3297 url, DEFAULT_PRIORITY, &d, NULL));
3298 r->Start();
[email protected]255620da2013-08-19 13:14:293299 base::RunLoop().Run();
[email protected]c2911d72011-10-03 22:16:363300
[email protected]f7022f32014-08-21 16:32:193301 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3302 EXPECT_EQ(0, r->status().error());
3303 EXPECT_EQ(200, r->GetResponseCode());
[email protected]c2911d72011-10-03 22:16:363304 EXPECT_TRUE(d.auth_required_called());
3305 EXPECT_EQ(1, network_delegate.created_requests());
3306 EXPECT_EQ(0, network_delegate.destroyed_requests());
3307 }
3308 EXPECT_EQ(1, network_delegate.destroyed_requests());
3309}
3310
3311// Tests that the network delegate can asynchronously complete OnAuthRequired
3312// by setting credentials.
3313TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) {
3314 ASSERT_TRUE(test_server_.Start());
3315
3316 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303317 BlockingNetworkDelegate network_delegate(
3318 BlockingNetworkDelegate::AUTO_CALLBACK);
3319 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]c2911d72011-10-03 22:16:363320 network_delegate.set_auth_retval(
[email protected]c2911d72011-10-03 22:16:363321 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
3322
[email protected]f3cf9802011-10-28 18:44:583323 AuthCredentials auth_credentials(kUser, kSecret);
[email protected]c2911d72011-10-03 22:16:363324 network_delegate.set_auth_credentials(auth_credentials);
3325
[email protected]ef2bf422012-05-11 03:27:093326 TestURLRequestContext context(true);
3327 context.set_network_delegate(&network_delegate);
3328 context.Init();
[email protected]c2911d72011-10-03 22:16:363329
3330 {
3331 GURL url(test_server_.GetURL("auth-basic"));
[email protected]f7022f32014-08-21 16:32:193332 scoped_ptr<URLRequest> r(context.CreateRequest(
3333 url, DEFAULT_PRIORITY, &d, NULL));
3334 r->Start();
[email protected]255620da2013-08-19 13:14:293335 base::RunLoop().Run();
[email protected]c2911d72011-10-03 22:16:363336
[email protected]f7022f32014-08-21 16:32:193337 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3338 EXPECT_EQ(0, r->status().error());
[email protected]c2911d72011-10-03 22:16:363339
[email protected]f7022f32014-08-21 16:32:193340 EXPECT_EQ(200, r->GetResponseCode());
[email protected]c2911d72011-10-03 22:16:363341 EXPECT_FALSE(d.auth_required_called());
3342 EXPECT_EQ(1, network_delegate.created_requests());
3343 EXPECT_EQ(0, network_delegate.destroyed_requests());
3344 }
3345 EXPECT_EQ(1, network_delegate.destroyed_requests());
3346}
3347
3348// Tests that the network delegate can asynchronously complete OnAuthRequired
3349// by cancelling authentication.
3350TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) {
3351 ASSERT_TRUE(test_server_.Start());
3352
3353 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303354 BlockingNetworkDelegate network_delegate(
3355 BlockingNetworkDelegate::AUTO_CALLBACK);
3356 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]c2911d72011-10-03 22:16:363357 network_delegate.set_auth_retval(
[email protected]c2911d72011-10-03 22:16:363358 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH);
3359
[email protected]ef2bf422012-05-11 03:27:093360 TestURLRequestContext context(true);
3361 context.set_network_delegate(&network_delegate);
3362 context.Init();
[email protected]c2911d72011-10-03 22:16:363363
3364 {
3365 GURL url(test_server_.GetURL("auth-basic"));
[email protected]f7022f32014-08-21 16:32:193366 scoped_ptr<URLRequest> r(context.CreateRequest(
3367 url, DEFAULT_PRIORITY, &d, NULL));
3368 r->Start();
[email protected]255620da2013-08-19 13:14:293369 base::RunLoop().Run();
[email protected]c2911d72011-10-03 22:16:363370
[email protected]f7022f32014-08-21 16:32:193371 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3372 EXPECT_EQ(OK, r->status().error());
3373 EXPECT_EQ(401, r->GetResponseCode());
[email protected]c2911d72011-10-03 22:16:363374 EXPECT_FALSE(d.auth_required_called());
3375 EXPECT_EQ(1, network_delegate.created_requests());
3376 EXPECT_EQ(0, network_delegate.destroyed_requests());
3377 }
3378 EXPECT_EQ(1, network_delegate.destroyed_requests());
3379}
3380
[email protected]9045b8822012-01-13 20:35:353381// Tests that we can handle when a network request was canceled while we were
3382// waiting for the network delegate.
3383// Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback.
3384TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) {
3385 ASSERT_TRUE(test_server_.Start());
3386
3387 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303388 BlockingNetworkDelegate network_delegate(
3389 BlockingNetworkDelegate::USER_CALLBACK);
3390 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
[email protected]9045b8822012-01-13 20:35:353391
[email protected]ef2bf422012-05-11 03:27:093392 TestURLRequestContext context(true);
3393 context.set_network_delegate(&network_delegate);
3394 context.Init();
[email protected]9045b8822012-01-13 20:35:353395
3396 {
[email protected]f7022f32014-08-21 16:32:193397 scoped_ptr<URLRequest> r(context.CreateRequest(
3398 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL));
[email protected]9045b8822012-01-13 20:35:353399
[email protected]f7022f32014-08-21 16:32:193400 r->Start();
[email protected]255620da2013-08-19 13:14:293401 base::RunLoop().Run();
[email protected]dc5a5cf2012-09-26 02:49:303402 EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
3403 network_delegate.stage_blocked_for_callback());
[email protected]9045b8822012-01-13 20:35:353404 EXPECT_EQ(0, network_delegate.completed_requests());
3405 // Cancel before callback.
[email protected]f7022f32014-08-21 16:32:193406 r->Cancel();
[email protected]9045b8822012-01-13 20:35:353407 // Ensure that network delegate is notified.
3408 EXPECT_EQ(1, network_delegate.completed_requests());
[email protected]f7022f32014-08-21 16:32:193409 EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status());
3410 EXPECT_EQ(ERR_ABORTED, r->status().error());
[email protected]9045b8822012-01-13 20:35:353411 EXPECT_EQ(1, network_delegate.created_requests());
3412 EXPECT_EQ(0, network_delegate.destroyed_requests());
3413 }
3414 EXPECT_EQ(1, network_delegate.destroyed_requests());
3415}
3416
3417// Tests that we can handle when a network request was canceled while we were
3418// waiting for the network delegate.
3419// Part 2: Request is cancelled while waiting for OnBeforeSendHeaders callback.
3420TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) {
3421 ASSERT_TRUE(test_server_.Start());
3422
3423 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303424 BlockingNetworkDelegate network_delegate(
3425 BlockingNetworkDelegate::USER_CALLBACK);
3426 network_delegate.set_block_on(
3427 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS);
[email protected]9045b8822012-01-13 20:35:353428
[email protected]ef2bf422012-05-11 03:27:093429 TestURLRequestContext context(true);
3430 context.set_network_delegate(&network_delegate);
3431 context.Init();
[email protected]9045b8822012-01-13 20:35:353432
3433 {
[email protected]f7022f32014-08-21 16:32:193434 scoped_ptr<URLRequest> r(context.CreateRequest(
3435 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL));
[email protected]9045b8822012-01-13 20:35:353436
[email protected]f7022f32014-08-21 16:32:193437 r->Start();
[email protected]255620da2013-08-19 13:14:293438 base::RunLoop().Run();
[email protected]dc5a5cf2012-09-26 02:49:303439 EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
3440 network_delegate.stage_blocked_for_callback());
[email protected]9045b8822012-01-13 20:35:353441 EXPECT_EQ(0, network_delegate.completed_requests());
3442 // Cancel before callback.
[email protected]f7022f32014-08-21 16:32:193443 r->Cancel();
[email protected]9045b8822012-01-13 20:35:353444 // Ensure that network delegate is notified.
3445 EXPECT_EQ(1, network_delegate.completed_requests());
[email protected]f7022f32014-08-21 16:32:193446 EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status());
3447 EXPECT_EQ(ERR_ABORTED, r->status().error());
[email protected]9045b8822012-01-13 20:35:353448 EXPECT_EQ(1, network_delegate.created_requests());
3449 EXPECT_EQ(0, network_delegate.destroyed_requests());
3450 }
3451 EXPECT_EQ(1, network_delegate.destroyed_requests());
3452}
3453
3454// Tests that we can handle when a network request was canceled while we were
3455// waiting for the network delegate.
3456// Part 3: Request is cancelled while waiting for OnHeadersReceived callback.
3457TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) {
3458 ASSERT_TRUE(test_server_.Start());
3459
3460 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303461 BlockingNetworkDelegate network_delegate(
3462 BlockingNetworkDelegate::USER_CALLBACK);
3463 network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED);
[email protected]9045b8822012-01-13 20:35:353464
[email protected]ef2bf422012-05-11 03:27:093465 TestURLRequestContext context(true);
3466 context.set_network_delegate(&network_delegate);
3467 context.Init();
[email protected]9045b8822012-01-13 20:35:353468
3469 {
[email protected]f7022f32014-08-21 16:32:193470 scoped_ptr<URLRequest> r(context.CreateRequest(
3471 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL));
[email protected]9045b8822012-01-13 20:35:353472
[email protected]f7022f32014-08-21 16:32:193473 r->Start();
[email protected]255620da2013-08-19 13:14:293474 base::RunLoop().Run();
[email protected]dc5a5cf2012-09-26 02:49:303475 EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED,
3476 network_delegate.stage_blocked_for_callback());
[email protected]9045b8822012-01-13 20:35:353477 EXPECT_EQ(0, network_delegate.completed_requests());
3478 // Cancel before callback.
[email protected]f7022f32014-08-21 16:32:193479 r->Cancel();
[email protected]9045b8822012-01-13 20:35:353480 // Ensure that network delegate is notified.
3481 EXPECT_EQ(1, network_delegate.completed_requests());
[email protected]f7022f32014-08-21 16:32:193482 EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status());
3483 EXPECT_EQ(ERR_ABORTED, r->status().error());
[email protected]9045b8822012-01-13 20:35:353484 EXPECT_EQ(1, network_delegate.created_requests());
3485 EXPECT_EQ(0, network_delegate.destroyed_requests());
3486 }
3487 EXPECT_EQ(1, network_delegate.destroyed_requests());
3488}
3489
3490// Tests that we can handle when a network request was canceled while we were
3491// waiting for the network delegate.
3492// Part 4: Request is cancelled while waiting for OnAuthRequired callback.
[email protected]bfe8b302013-02-15 12:16:023493TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) {
[email protected]9045b8822012-01-13 20:35:353494 ASSERT_TRUE(test_server_.Start());
3495
3496 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303497 BlockingNetworkDelegate network_delegate(
3498 BlockingNetworkDelegate::USER_CALLBACK);
3499 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]9045b8822012-01-13 20:35:353500
[email protected]ef2bf422012-05-11 03:27:093501 TestURLRequestContext context(true);
3502 context.set_network_delegate(&network_delegate);
3503 context.Init();
[email protected]9045b8822012-01-13 20:35:353504
3505 {
[email protected]f7022f32014-08-21 16:32:193506 scoped_ptr<URLRequest> r(context.CreateRequest(
3507 test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d, NULL));
[email protected]9045b8822012-01-13 20:35:353508
[email protected]f7022f32014-08-21 16:32:193509 r->Start();
[email protected]255620da2013-08-19 13:14:293510 base::RunLoop().Run();
[email protected]dc5a5cf2012-09-26 02:49:303511 EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED,
3512 network_delegate.stage_blocked_for_callback());
[email protected]9045b8822012-01-13 20:35:353513 EXPECT_EQ(0, network_delegate.completed_requests());
3514 // Cancel before callback.
[email protected]f7022f32014-08-21 16:32:193515 r->Cancel();
[email protected]9045b8822012-01-13 20:35:353516 // Ensure that network delegate is notified.
3517 EXPECT_EQ(1, network_delegate.completed_requests());
[email protected]f7022f32014-08-21 16:32:193518 EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status());
3519 EXPECT_EQ(ERR_ABORTED, r->status().error());
[email protected]9045b8822012-01-13 20:35:353520 EXPECT_EQ(1, network_delegate.created_requests());
3521 EXPECT_EQ(0, network_delegate.destroyed_requests());
3522 }
3523 EXPECT_EQ(1, network_delegate.destroyed_requests());
3524}
3525
[email protected]95409e12010-08-17 20:07:113526// In this unit test, we're using the HTTPTestServer as a proxy server and
3527// issuing a CONNECT request with the magic host name "www.server-auth.com".
3528// The HTTPTestServer will return a 401 response, which we should balk at.
[email protected]b89290212009-08-14 22:37:353529TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) {
[email protected]95409e12010-08-17 20:07:113530 ASSERT_TRUE(test_server_.Start());
3531
[email protected]ceefd7fd2012-11-29 00:36:243532 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]d5a4dd62012-05-23 01:41:043533 TestURLRequestContextWithProxy context(
[email protected]2ca01e52013-10-31 22:05:193534 test_server_.host_port_pair().ToString(), &network_delegate);
[email protected]87a09a92011-07-14 15:50:503535
[email protected]dc651782009-02-14 01:45:083536 TestDelegate d;
3537 {
[email protected]f7022f32014-08-21 16:32:193538 scoped_ptr<URLRequest> r(context.CreateRequest(
3539 GURL("https://www.server-auth.com/"), DEFAULT_PRIORITY, &d, NULL));
[email protected]dc651782009-02-14 01:45:083540
[email protected]f7022f32014-08-21 16:32:193541 r->Start();
3542 EXPECT_TRUE(r->is_pending());
[email protected]dc651782009-02-14 01:45:083543
[email protected]255620da2013-08-19 13:14:293544 base::RunLoop().Run();
[email protected]dc651782009-02-14 01:45:083545
[email protected]f7022f32014-08-21 16:32:193546 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
[email protected]d8fc4722014-06-13 13:17:153547 // The proxy server is not set before failure.
[email protected]f7022f32014-08-21 16:32:193548 EXPECT_TRUE(r->proxy_server().IsEmpty());
3549 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r->status().error());
[email protected]dc651782009-02-14 01:45:083550 }
3551}
3552
[email protected]b89290212009-08-14 22:37:353553TEST_F(URLRequestTestHTTP, GetTest_NoCache) {
[email protected]95409e12010-08-17 20:07:113554 ASSERT_TRUE(test_server_.Start());
3555
initial.commit586acc5fe2008-07-26 22:42:523556 TestDelegate d;
3557 {
[email protected]f7022f32014-08-21 16:32:193558 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
3559 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL));
initial.commit586acc5fe2008-07-26 22:42:523560
[email protected]f7022f32014-08-21 16:32:193561 r->Start();
3562 EXPECT_TRUE(r->is_pending());
initial.commit586acc5fe2008-07-26 22:42:523563
[email protected]255620da2013-08-19 13:14:293564 base::RunLoop().Run();
initial.commit586acc5fe2008-07-26 22:42:523565
3566 EXPECT_EQ(1, d.response_started_count());
3567 EXPECT_FALSE(d.received_data_before_response());
3568 EXPECT_NE(0, d.bytes_received());
[email protected]6d81b482011-02-22 19:47:193569 EXPECT_EQ(test_server_.host_port_pair().host(),
[email protected]f7022f32014-08-21 16:32:193570 r->GetSocketAddress().host());
[email protected]6d81b482011-02-22 19:47:193571 EXPECT_EQ(test_server_.host_port_pair().port(),
[email protected]f7022f32014-08-21 16:32:193572 r->GetSocketAddress().port());
[email protected]c31a54592009-09-04 02:36:163573
[email protected]9e743cd2010-03-16 07:03:533574 // TODO(eroman): Add back the NetLog tests...
initial.commit586acc5fe2008-07-26 22:42:523575 }
initial.commit586acc5fe2008-07-26 22:42:523576}
3577
[email protected]263163f2012-06-14 22:40:343578// This test has the server send a large number of cookies to the client.
3579// To ensure that no number of cookies causes a crash, a galloping binary
3580// search is used to estimate that maximum number of cookies that are accepted
3581// by the browser. Beyond the maximum number, the request will fail with
3582// ERR_RESPONSE_HEADERS_TOO_BIG.
[email protected]69dd6fe2013-02-23 23:15:303583#if defined(OS_WIN)
3584// http://crbug.com/177916
3585#define MAYBE_GetTest_ManyCookies DISABLED_GetTest_ManyCookies
3586#else
3587#define MAYBE_GetTest_ManyCookies GetTest_ManyCookies
3588#endif // defined(OS_WIN)
3589TEST_F(URLRequestTestHTTP, MAYBE_GetTest_ManyCookies) {
[email protected]263163f2012-06-14 22:40:343590 ASSERT_TRUE(test_server_.Start());
3591
3592 int lower_bound = 0;
3593 int upper_bound = 1;
3594
3595 // Double the number of cookies until the response header limits are
3596 // exceeded.
3597 while (DoManyCookiesRequest(upper_bound)) {
3598 lower_bound = upper_bound;
3599 upper_bound *= 2;
3600 ASSERT_LT(upper_bound, 1000000);
3601 }
3602
3603 int tolerance = upper_bound * 0.005;
3604 if (tolerance < 2)
3605 tolerance = 2;
3606
3607 // Perform a binary search to find the highest possible number of cookies,
3608 // within the desired tolerance.
3609 while (upper_bound - lower_bound >= tolerance) {
3610 int num_cookies = (lower_bound + upper_bound) / 2;
3611
3612 if (DoManyCookiesRequest(num_cookies))
3613 lower_bound = num_cookies;
3614 else
3615 upper_bound = num_cookies;
3616 }
3617 // Success: the test did not crash.
3618}
3619
[email protected]b89290212009-08-14 22:37:353620TEST_F(URLRequestTestHTTP, GetTest) {
[email protected]95409e12010-08-17 20:07:113621 ASSERT_TRUE(test_server_.Start());
3622
initial.commit586acc5fe2008-07-26 22:42:523623 TestDelegate d;
3624 {
[email protected]f7022f32014-08-21 16:32:193625 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
3626 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL));
initial.commit586acc5fe2008-07-26 22:42:523627
[email protected]f7022f32014-08-21 16:32:193628 r->Start();
3629 EXPECT_TRUE(r->is_pending());
initial.commit586acc5fe2008-07-26 22:42:523630
[email protected]255620da2013-08-19 13:14:293631 base::RunLoop().Run();
initial.commit586acc5fe2008-07-26 22:42:523632
3633 EXPECT_EQ(1, d.response_started_count());
3634 EXPECT_FALSE(d.received_data_before_response());
3635 EXPECT_NE(0, d.bytes_received());
[email protected]6d81b482011-02-22 19:47:193636 EXPECT_EQ(test_server_.host_port_pair().host(),
[email protected]f7022f32014-08-21 16:32:193637 r->GetSocketAddress().host());
[email protected]6d81b482011-02-22 19:47:193638 EXPECT_EQ(test_server_.host_port_pair().port(),
[email protected]f7022f32014-08-21 16:32:193639 r->GetSocketAddress().port());
initial.commit586acc5fe2008-07-26 22:42:523640 }
[email protected]5d7b373e2009-09-02 07:19:033641}
3642
[email protected]79e1fd62013-06-20 06:50:043643TEST_F(URLRequestTestHTTP, GetTest_GetFullRequestHeaders) {
3644 ASSERT_TRUE(test_server_.Start());
3645
3646 TestDelegate d;
3647 {
3648 GURL test_url(test_server_.GetURL(std::string()));
[email protected]f7022f32014-08-21 16:32:193649 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
3650 test_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]79e1fd62013-06-20 06:50:043651
3652 HttpRequestHeaders headers;
[email protected]f7022f32014-08-21 16:32:193653 EXPECT_FALSE(r->GetFullRequestHeaders(&headers));
[email protected]79e1fd62013-06-20 06:50:043654
[email protected]f7022f32014-08-21 16:32:193655 r->Start();
3656 EXPECT_TRUE(r->is_pending());
[email protected]79e1fd62013-06-20 06:50:043657
[email protected]255620da2013-08-19 13:14:293658 base::RunLoop().Run();
[email protected]79e1fd62013-06-20 06:50:043659
3660 EXPECT_EQ(1, d.response_started_count());
3661 EXPECT_FALSE(d.received_data_before_response());
3662 EXPECT_NE(0, d.bytes_received());
3663 EXPECT_EQ(test_server_.host_port_pair().host(),
[email protected]f7022f32014-08-21 16:32:193664 r->GetSocketAddress().host());
[email protected]79e1fd62013-06-20 06:50:043665 EXPECT_EQ(test_server_.host_port_pair().port(),
[email protected]f7022f32014-08-21 16:32:193666 r->GetSocketAddress().port());
[email protected]79e1fd62013-06-20 06:50:043667
3668 EXPECT_TRUE(d.have_full_request_headers());
3669 CheckFullRequestHeaders(d.full_request_headers(), test_url);
3670 }
3671}
3672
[email protected]58e32bb2013-01-21 18:23:253673TEST_F(URLRequestTestHTTP, GetTestLoadTiming) {
3674 ASSERT_TRUE(test_server_.Start());
3675
3676 TestDelegate d;
3677 {
[email protected]f7022f32014-08-21 16:32:193678 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
3679 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL));
[email protected]58e32bb2013-01-21 18:23:253680
[email protected]f7022f32014-08-21 16:32:193681 r->Start();
3682 EXPECT_TRUE(r->is_pending());
[email protected]58e32bb2013-01-21 18:23:253683
[email protected]255620da2013-08-19 13:14:293684 base::RunLoop().Run();
[email protected]58e32bb2013-01-21 18:23:253685
3686 LoadTimingInfo load_timing_info;
[email protected]f7022f32014-08-21 16:32:193687 r->GetLoadTimingInfo(&load_timing_info);
[email protected]58e32bb2013-01-21 18:23:253688 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
3689
3690 EXPECT_EQ(1, d.response_started_count());
3691 EXPECT_FALSE(d.received_data_before_response());
3692 EXPECT_NE(0, d.bytes_received());
3693 EXPECT_EQ(test_server_.host_port_pair().host(),
[email protected]f7022f32014-08-21 16:32:193694 r->GetSocketAddress().host());
[email protected]58e32bb2013-01-21 18:23:253695 EXPECT_EQ(test_server_.host_port_pair().port(),
[email protected]f7022f32014-08-21 16:32:193696 r->GetSocketAddress().port());
[email protected]58e32bb2013-01-21 18:23:253697 }
3698}
3699
[email protected]aad63572011-05-24 20:14:393700TEST_F(URLRequestTestHTTP, GetZippedTest) {
3701 ASSERT_TRUE(test_server_.Start());
3702
3703 // Parameter that specifies the Content-Length field in the response:
3704 // C - Compressed length.
3705 // U - Uncompressed length.
3706 // L - Large length (larger than both C & U).
3707 // M - Medium length (between C & U).
3708 // S - Small length (smaller than both C & U).
3709 const char test_parameters[] = "CULMS";
3710 const int num_tests = arraysize(test_parameters)- 1; // Skip NULL.
3711 // C & U should be OK.
[email protected]f0e2bf42011-07-22 21:21:443712 // L & M are larger than the data sent, and show an error.
[email protected]aad63572011-05-24 20:14:393713 // S has too little data, but we seem to accept it.
3714 const bool test_expect_success[num_tests] =
[email protected]f001bd6a2011-12-08 04:31:373715 { true, true, false, false, true };
[email protected]aad63572011-05-24 20:14:393716
3717 for (int i = 0; i < num_tests ; i++) {
3718 TestDelegate d;
3719 {
3720 std::string test_file =
3721 base::StringPrintf("compressedfiles/BullRunSpeech.txt?%c",
3722 test_parameters[i]);
[email protected]aad63572011-05-24 20:14:393723
[email protected]ceefd7fd2012-11-29 00:36:243724 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]ef2bf422012-05-11 03:27:093725 TestURLRequestContext context(true);
3726 context.set_network_delegate(&network_delegate);
3727 context.Init();
[email protected]87a09a92011-07-14 15:50:503728
[email protected]f7022f32014-08-21 16:32:193729 scoped_ptr<URLRequest> r(context.CreateRequest(
3730 test_server_.GetURL(test_file), DEFAULT_PRIORITY, &d, NULL));
3731 r->Start();
3732 EXPECT_TRUE(r->is_pending());
[email protected]aad63572011-05-24 20:14:393733
[email protected]255620da2013-08-19 13:14:293734 base::RunLoop().Run();
[email protected]aad63572011-05-24 20:14:393735
3736 EXPECT_EQ(1, d.response_started_count());
3737 EXPECT_FALSE(d.received_data_before_response());
3738 VLOG(1) << " Received " << d.bytes_received() << " bytes"
[email protected]f7022f32014-08-21 16:32:193739 << " status = " << r->status().status()
3740 << " error = " << r->status().error();
[email protected]aad63572011-05-24 20:14:393741 if (test_expect_success[i]) {
[email protected]f7022f32014-08-21 16:32:193742 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status())
[email protected]aad63572011-05-24 20:14:393743 << " Parameter = \"" << test_file << "\"";
3744 } else {
[email protected]f7022f32014-08-21 16:32:193745 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
3746 EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, r->status().error())
[email protected]aad63572011-05-24 20:14:393747 << " Parameter = \"" << test_file << "\"";
3748 }
3749 }
3750 }
3751}
3752
[email protected]c044616e2013-02-20 02:01:263753TEST_F(URLRequestTestHTTP, HTTPSToHTTPRedirectNoRefererTest) {
[email protected]95409e12010-08-17 20:07:113754 ASSERT_TRUE(test_server_.Start());
3755
[email protected]ce7d0cbc2013-05-03 18:57:223756 SpawnedTestServer https_test_server(
3757 SpawnedTestServer::TYPE_HTTPS, SpawnedTestServer::kLocalhost,
[email protected]6cdfd7f2013-02-08 20:40:153758 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]95409e12010-08-17 20:07:113759 ASSERT_TRUE(https_test_server.Start());
[email protected]7844480a2009-12-16 21:18:583760
3761 // An https server is sent a request with an https referer,
3762 // and responds with a redirect to an http url. The http
3763 // server should not be sent the referer.
[email protected]007b3f82013-04-09 08:46:453764 GURL http_destination = test_server_.GetURL(std::string());
[email protected]7844480a2009-12-16 21:18:583765 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:193766 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
[email protected]2ca01e52013-10-31 22:05:193767 https_test_server.GetURL("server-redirect?" + http_destination.spec()),
[email protected]f7022f32014-08-21 16:32:193768 DEFAULT_PRIORITY, &d, NULL));
3769 req->SetReferrer("https://www.referrer.com/");
3770 req->Start();
[email protected]255620da2013-08-19 13:14:293771 base::RunLoop().Run();
[email protected]7844480a2009-12-16 21:18:583772
3773 EXPECT_EQ(1, d.response_started_count());
3774 EXPECT_EQ(1, d.received_redirect_count());
[email protected]f7022f32014-08-21 16:32:193775 EXPECT_EQ(http_destination, req->url());
3776 EXPECT_EQ(std::string(), req->referrer());
[email protected]7844480a2009-12-16 21:18:583777}
3778
[email protected]58e32bb2013-01-21 18:23:253779TEST_F(URLRequestTestHTTP, RedirectLoadTiming) {
3780 ASSERT_TRUE(test_server_.Start());
3781
[email protected]007b3f82013-04-09 08:46:453782 GURL destination_url = test_server_.GetURL(std::string());
3783 GURL original_url =
3784 test_server_.GetURL("server-redirect?" + destination_url.spec());
[email protected]58e32bb2013-01-21 18:23:253785 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:193786 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
3787 original_url, DEFAULT_PRIORITY, &d, NULL));
3788 req->Start();
[email protected]255620da2013-08-19 13:14:293789 base::RunLoop().Run();
[email protected]58e32bb2013-01-21 18:23:253790
3791 EXPECT_EQ(1, d.response_started_count());
3792 EXPECT_EQ(1, d.received_redirect_count());
[email protected]f7022f32014-08-21 16:32:193793 EXPECT_EQ(destination_url, req->url());
3794 EXPECT_EQ(original_url, req->original_url());
3795 ASSERT_EQ(2U, req->url_chain().size());
3796 EXPECT_EQ(original_url, req->url_chain()[0]);
3797 EXPECT_EQ(destination_url, req->url_chain()[1]);
[email protected]58e32bb2013-01-21 18:23:253798
3799 LoadTimingInfo load_timing_info_before_redirect;
3800 EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeRedirect(
3801 &load_timing_info_before_redirect));
3802 TestLoadTimingNotReused(load_timing_info_before_redirect,
3803 CONNECT_TIMING_HAS_DNS_TIMES);
3804
3805 LoadTimingInfo load_timing_info;
[email protected]f7022f32014-08-21 16:32:193806 req->GetLoadTimingInfo(&load_timing_info);
[email protected]58e32bb2013-01-21 18:23:253807 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
3808
3809 // Check that a new socket was used on redirect, since the server does not
3810 // supposed keep-alive sockets, and that the times before the redirect are
3811 // before the ones recorded for the second request.
3812 EXPECT_NE(load_timing_info_before_redirect.socket_log_id,
3813 load_timing_info.socket_log_id);
3814 EXPECT_LE(load_timing_info_before_redirect.receive_headers_end,
3815 load_timing_info.connect_timing.connect_start);
3816}
3817
[email protected]8f1ac082011-04-19 21:14:133818TEST_F(URLRequestTestHTTP, MultipleRedirectTest) {
3819 ASSERT_TRUE(test_server_.Start());
3820
[email protected]007b3f82013-04-09 08:46:453821 GURL destination_url = test_server_.GetURL(std::string());
3822 GURL middle_redirect_url =
3823 test_server_.GetURL("server-redirect?" + destination_url.spec());
[email protected]8f1ac082011-04-19 21:14:133824 GURL original_url = test_server_.GetURL(
3825 "server-redirect?" + middle_redirect_url.spec());
3826 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:193827 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
3828 original_url, DEFAULT_PRIORITY, &d, NULL));
3829 req->Start();
[email protected]255620da2013-08-19 13:14:293830 base::RunLoop().Run();
[email protected]8f1ac082011-04-19 21:14:133831
3832 EXPECT_EQ(1, d.response_started_count());
3833 EXPECT_EQ(2, d.received_redirect_count());
[email protected]f7022f32014-08-21 16:32:193834 EXPECT_EQ(destination_url, req->url());
3835 EXPECT_EQ(original_url, req->original_url());
3836 ASSERT_EQ(3U, req->url_chain().size());
3837 EXPECT_EQ(original_url, req->url_chain()[0]);
3838 EXPECT_EQ(middle_redirect_url, req->url_chain()[1]);
3839 EXPECT_EQ(destination_url, req->url_chain()[2]);
[email protected]8f1ac082011-04-19 21:14:133840}
3841
[email protected]abe1c4a2013-10-25 19:28:513842// First and second pieces of information logged by delegates to URLRequests.
3843const char kFirstDelegateInfo[] = "Wonderful delegate";
3844const char kSecondDelegateInfo[] = "Exciting delegate";
3845
3846// Logs delegate information to a URLRequest. The first string is logged
3847// synchronously on Start(), using DELEGATE_INFO_DEBUG_ONLY. The second is
3848// logged asynchronously, using DELEGATE_INFO_DISPLAY_TO_USER. Then
3849// another asynchronous call is used to clear the delegate information
3850// before calling a callback. The object then deletes itself.
3851class AsyncDelegateLogger : public base::RefCounted<AsyncDelegateLogger> {
3852 public:
3853 typedef base::Callback<void()> Callback;
3854
3855 // Each time delegate information is added to the URLRequest, the resulting
3856 // load state is checked. The expected load state after each request is
3857 // passed in as an argument.
3858 static void Run(URLRequest* url_request,
3859 LoadState expected_first_load_state,
3860 LoadState expected_second_load_state,
3861 LoadState expected_third_load_state,
3862 const Callback& callback) {
3863 AsyncDelegateLogger* logger = new AsyncDelegateLogger(
3864 url_request,
3865 expected_first_load_state,
3866 expected_second_load_state,
3867 expected_third_load_state,
3868 callback);
3869 logger->Start();
3870 }
3871
3872 // Checks that the log entries, starting with log_position, contain the
3873 // DELEGATE_INFO NetLog events that an AsyncDelegateLogger should have
3874 // recorded. Returns the index of entry after the expected number of
3875 // events this logged, or entries.size() if there aren't enough entries.
3876 static size_t CheckDelegateInfo(
3877 const CapturingNetLog::CapturedEntryList& entries, size_t log_position) {
3878 // There should be 4 DELEGATE_INFO events: Two begins and two ends.
3879 if (log_position + 3 >= entries.size()) {
3880 ADD_FAILURE() << "Not enough log entries";
3881 return entries.size();
3882 }
3883 std::string delegate_info;
3884 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type);
3885 EXPECT_EQ(NetLog::PHASE_BEGIN, entries[log_position].phase);
3886 EXPECT_TRUE(entries[log_position].GetStringValue("delegate_info",
3887 &delegate_info));
3888 EXPECT_EQ(kFirstDelegateInfo, delegate_info);
3889
3890 ++log_position;
3891 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type);
3892 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
3893
3894 ++log_position;
3895 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type);
3896 EXPECT_EQ(NetLog::PHASE_BEGIN, entries[log_position].phase);
3897 EXPECT_TRUE(entries[log_position].GetStringValue("delegate_info",
3898 &delegate_info));
3899 EXPECT_EQ(kSecondDelegateInfo, delegate_info);
3900
3901 ++log_position;
3902 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type);
3903 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
3904
3905 return log_position + 1;
3906 }
3907
[email protected]1826a402014-01-08 15:40:483908 // Find delegate request begin and end messages for OnBeforeNetworkStart.
3909 // Returns the position of the end message.
3910 static size_t ExpectBeforeNetworkEvents(
3911 const CapturingNetLog::CapturedEntryList& entries,
3912 size_t log_position) {
3913 log_position =
3914 ExpectLogContainsSomewhereAfter(entries,
3915 log_position,
3916 NetLog::TYPE_URL_REQUEST_DELEGATE,
3917 NetLog::PHASE_BEGIN);
3918 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE,
3919 entries[log_position + 1].type);
3920 EXPECT_EQ(NetLog::PHASE_END, entries[log_position + 1].phase);
3921 return log_position + 1;
3922 }
3923
[email protected]abe1c4a2013-10-25 19:28:513924 private:
3925 friend class base::RefCounted<AsyncDelegateLogger>;
3926
3927 AsyncDelegateLogger(URLRequest* url_request,
3928 LoadState expected_first_load_state,
3929 LoadState expected_second_load_state,
3930 LoadState expected_third_load_state,
3931 const Callback& callback)
3932 : url_request_(url_request),
3933 expected_first_load_state_(expected_first_load_state),
3934 expected_second_load_state_(expected_second_load_state),
3935 expected_third_load_state_(expected_third_load_state),
3936 callback_(callback) {
3937 }
3938
3939 ~AsyncDelegateLogger() {}
3940
3941 void Start() {
[email protected]f8fe5cf2013-12-04 20:11:533942 url_request_->LogBlockedBy(kFirstDelegateInfo);
[email protected]abe1c4a2013-10-25 19:28:513943 LoadStateWithParam load_state = url_request_->GetLoadState();
3944 EXPECT_EQ(expected_first_load_state_, load_state.state);
3945 EXPECT_NE(ASCIIToUTF16(kFirstDelegateInfo), load_state.param);
3946 base::MessageLoop::current()->PostTask(
3947 FROM_HERE,
3948 base::Bind(&AsyncDelegateLogger::LogSecondDelegate, this));
3949 }
3950
3951 void LogSecondDelegate() {
[email protected]f8fe5cf2013-12-04 20:11:533952 url_request_->LogAndReportBlockedBy(kSecondDelegateInfo);
[email protected]abe1c4a2013-10-25 19:28:513953 LoadStateWithParam load_state = url_request_->GetLoadState();
3954 EXPECT_EQ(expected_second_load_state_, load_state.state);
3955 if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) {
3956 EXPECT_EQ(ASCIIToUTF16(kSecondDelegateInfo), load_state.param);
3957 } else {
3958 EXPECT_NE(ASCIIToUTF16(kSecondDelegateInfo), load_state.param);
3959 }
3960 base::MessageLoop::current()->PostTask(
3961 FROM_HERE,
3962 base::Bind(&AsyncDelegateLogger::LogComplete, this));
3963 }
3964
3965 void LogComplete() {
[email protected]f8fe5cf2013-12-04 20:11:533966 url_request_->LogUnblocked();
[email protected]abe1c4a2013-10-25 19:28:513967 LoadStateWithParam load_state = url_request_->GetLoadState();
3968 EXPECT_EQ(expected_third_load_state_, load_state.state);
3969 if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE)
[email protected]754bd202013-12-18 08:29:083970 EXPECT_EQ(base::string16(), load_state.param);
[email protected]abe1c4a2013-10-25 19:28:513971 callback_.Run();
3972 }
3973
3974 URLRequest* url_request_;
3975 const int expected_first_load_state_;
3976 const int expected_second_load_state_;
3977 const int expected_third_load_state_;
3978 const Callback callback_;
3979
3980 DISALLOW_COPY_AND_ASSIGN(AsyncDelegateLogger);
3981};
3982
3983// NetworkDelegate that logs delegate information before a request is started,
3984// before headers are sent, when headers are read, and when auth information
3985// is requested. Uses AsyncDelegateLogger.
3986class AsyncLoggingNetworkDelegate : public TestNetworkDelegate {
3987 public:
3988 AsyncLoggingNetworkDelegate() {}
dchengb03027d2014-10-21 12:00:203989 ~AsyncLoggingNetworkDelegate() override {}
[email protected]abe1c4a2013-10-25 19:28:513990
3991 // NetworkDelegate implementation.
dchengb03027d2014-10-21 12:00:203992 int OnBeforeURLRequest(URLRequest* request,
3993 const CompletionCallback& callback,
3994 GURL* new_url) override {
[email protected]abe1c4a2013-10-25 19:28:513995 TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url);
3996 return RunCallbackAsynchronously(request, callback);
3997 }
3998
dchengb03027d2014-10-21 12:00:203999 int OnBeforeSendHeaders(URLRequest* request,
4000 const CompletionCallback& callback,
4001 HttpRequestHeaders* headers) override {
[email protected]abe1c4a2013-10-25 19:28:514002 TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers);
4003 return RunCallbackAsynchronously(request, callback);
4004 }
4005
dchengb03027d2014-10-21 12:00:204006 int OnHeadersReceived(
[email protected]abe1c4a2013-10-25 19:28:514007 URLRequest* request,
4008 const CompletionCallback& callback,
4009 const HttpResponseHeaders* original_response_headers,
[email protected]5f714132014-03-26 10:41:164010 scoped_refptr<HttpResponseHeaders>* override_response_headers,
mostynbba063d6032014-10-09 11:01:134011 GURL* allowed_unsafe_redirect_url) override {
[email protected]5f714132014-03-26 10:41:164012 TestNetworkDelegate::OnHeadersReceived(request,
4013 callback,
[email protected]abe1c4a2013-10-25 19:28:514014 original_response_headers,
[email protected]5f714132014-03-26 10:41:164015 override_response_headers,
4016 allowed_unsafe_redirect_url);
[email protected]abe1c4a2013-10-25 19:28:514017 return RunCallbackAsynchronously(request, callback);
4018 }
4019
dchengb03027d2014-10-21 12:00:204020 NetworkDelegate::AuthRequiredResponse OnAuthRequired(
[email protected]abe1c4a2013-10-25 19:28:514021 URLRequest* request,
4022 const AuthChallengeInfo& auth_info,
4023 const AuthCallback& callback,
mostynbba063d6032014-10-09 11:01:134024 AuthCredentials* credentials) override {
[email protected]abe1c4a2013-10-25 19:28:514025 AsyncDelegateLogger::Run(
4026 request,
4027 LOAD_STATE_WAITING_FOR_DELEGATE,
4028 LOAD_STATE_WAITING_FOR_DELEGATE,
4029 LOAD_STATE_WAITING_FOR_DELEGATE,
4030 base::Bind(&AsyncLoggingNetworkDelegate::SetAuthAndResume,
4031 callback, credentials));
4032 return AUTH_REQUIRED_RESPONSE_IO_PENDING;
4033 }
4034
4035 private:
4036 static int RunCallbackAsynchronously(
4037 URLRequest* request,
4038 const CompletionCallback& callback) {
4039 AsyncDelegateLogger::Run(
4040 request,
4041 LOAD_STATE_WAITING_FOR_DELEGATE,
4042 LOAD_STATE_WAITING_FOR_DELEGATE,
4043 LOAD_STATE_WAITING_FOR_DELEGATE,
4044 base::Bind(callback, OK));
4045 return ERR_IO_PENDING;
4046 }
4047
4048 static void SetAuthAndResume(const AuthCallback& callback,
4049 AuthCredentials* credentials) {
4050 *credentials = AuthCredentials(kUser, kSecret);
4051 callback.Run(NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
4052 }
4053
4054 DISALLOW_COPY_AND_ASSIGN(AsyncLoggingNetworkDelegate);
4055};
4056
4057// URLRequest::Delegate that logs delegate information when the headers
4058// are received, when each read completes, and during redirects. Uses
4059// AsyncDelegateLogger. Can optionally cancel a request in any phase.
4060//
4061// Inherits from TestDelegate to reuse the TestDelegate code to handle
4062// advancing to the next step in most cases, as well as cancellation.
4063class AsyncLoggingUrlRequestDelegate : public TestDelegate {
4064 public:
4065 enum CancelStage {
4066 NO_CANCEL = 0,
4067 CANCEL_ON_RECEIVED_REDIRECT,
4068 CANCEL_ON_RESPONSE_STARTED,
4069 CANCEL_ON_READ_COMPLETED
4070 };
4071
4072 explicit AsyncLoggingUrlRequestDelegate(CancelStage cancel_stage)
4073 : cancel_stage_(cancel_stage) {
4074 if (cancel_stage == CANCEL_ON_RECEIVED_REDIRECT)
4075 set_cancel_in_received_redirect(true);
4076 else if (cancel_stage == CANCEL_ON_RESPONSE_STARTED)
4077 set_cancel_in_response_started(true);
4078 else if (cancel_stage == CANCEL_ON_READ_COMPLETED)
4079 set_cancel_in_received_data(true);
4080 }
dchengb03027d2014-10-21 12:00:204081 ~AsyncLoggingUrlRequestDelegate() override {}
[email protected]abe1c4a2013-10-25 19:28:514082
4083 // URLRequest::Delegate implementation:
dchengb03027d2014-10-21 12:00:204084 void OnReceivedRedirect(URLRequest* request,
4085 const RedirectInfo& redirect_info,
4086 bool* defer_redirect) override {
[email protected]abe1c4a2013-10-25 19:28:514087 *defer_redirect = true;
4088 AsyncDelegateLogger::Run(
4089 request,
4090 LOAD_STATE_WAITING_FOR_DELEGATE,
4091 LOAD_STATE_WAITING_FOR_DELEGATE,
4092 LOAD_STATE_WAITING_FOR_DELEGATE,
4093 base::Bind(
4094 &AsyncLoggingUrlRequestDelegate::OnReceivedRedirectLoggingComplete,
[email protected]cba24642014-08-15 20:49:594095 base::Unretained(this), request, redirect_info));
[email protected]abe1c4a2013-10-25 19:28:514096 }
4097
dchengb03027d2014-10-21 12:00:204098 void OnResponseStarted(URLRequest* request) override {
[email protected]abe1c4a2013-10-25 19:28:514099 AsyncDelegateLogger::Run(
4100 request,
4101 LOAD_STATE_WAITING_FOR_DELEGATE,
4102 LOAD_STATE_WAITING_FOR_DELEGATE,
4103 LOAD_STATE_WAITING_FOR_DELEGATE,
4104 base::Bind(
4105 &AsyncLoggingUrlRequestDelegate::OnResponseStartedLoggingComplete,
4106 base::Unretained(this), request));
4107 }
4108
dchengb03027d2014-10-21 12:00:204109 void OnReadCompleted(URLRequest* request, int bytes_read) override {
[email protected]abe1c4a2013-10-25 19:28:514110 AsyncDelegateLogger::Run(
4111 request,
4112 LOAD_STATE_IDLE,
4113 LOAD_STATE_IDLE,
4114 LOAD_STATE_IDLE,
4115 base::Bind(
4116 &AsyncLoggingUrlRequestDelegate::AfterReadCompletedLoggingComplete,
4117 base::Unretained(this), request, bytes_read));
4118 }
4119
4120 private:
4121 void OnReceivedRedirectLoggingComplete(URLRequest* request,
[email protected]cba24642014-08-15 20:49:594122 const RedirectInfo& redirect_info) {
[email protected]abe1c4a2013-10-25 19:28:514123 bool defer_redirect = false;
[email protected]cba24642014-08-15 20:49:594124 TestDelegate::OnReceivedRedirect(request, redirect_info, &defer_redirect);
[email protected]abe1c4a2013-10-25 19:28:514125 // FollowDeferredRedirect should not be called after cancellation.
4126 if (cancel_stage_ == CANCEL_ON_RECEIVED_REDIRECT)
4127 return;
4128 if (!defer_redirect)
4129 request->FollowDeferredRedirect();
4130 }
4131
4132 void OnResponseStartedLoggingComplete(URLRequest* request) {
4133 // The parent class continues the request.
4134 TestDelegate::OnResponseStarted(request);
4135 }
4136
4137 void AfterReadCompletedLoggingComplete(URLRequest* request, int bytes_read) {
4138 // The parent class continues the request.
4139 TestDelegate::OnReadCompleted(request, bytes_read);
4140 }
4141
4142 const CancelStage cancel_stage_;
4143
4144 DISALLOW_COPY_AND_ASSIGN(AsyncLoggingUrlRequestDelegate);
4145};
4146
4147// Tests handling of delegate info before a request starts.
4148TEST_F(URLRequestTestHTTP, DelegateInfoBeforeStart) {
4149 ASSERT_TRUE(test_server_.Start());
4150
4151 TestDelegate request_delegate;
4152 TestURLRequestContext context(true);
4153 context.set_network_delegate(NULL);
4154 context.set_net_log(&net_log_);
4155 context.Init();
4156
4157 {
[email protected]f7022f32014-08-21 16:32:194158 scoped_ptr<URLRequest> r(context.CreateRequest(
4159 test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &request_delegate,
4160 NULL));
4161 LoadStateWithParam load_state = r->GetLoadState();
[email protected]abe1c4a2013-10-25 19:28:514162 EXPECT_EQ(LOAD_STATE_IDLE, load_state.state);
[email protected]754bd202013-12-18 08:29:084163 EXPECT_EQ(base::string16(), load_state.param);
[email protected]abe1c4a2013-10-25 19:28:514164
4165 AsyncDelegateLogger::Run(
[email protected]f7022f32014-08-21 16:32:194166 r.get(),
[email protected]abe1c4a2013-10-25 19:28:514167 LOAD_STATE_WAITING_FOR_DELEGATE,
4168 LOAD_STATE_WAITING_FOR_DELEGATE,
4169 LOAD_STATE_IDLE,
[email protected]f7022f32014-08-21 16:32:194170 base::Bind(&URLRequest::Start, base::Unretained(r.get())));
[email protected]abe1c4a2013-10-25 19:28:514171
4172 base::RunLoop().Run();
4173
[email protected]f7022f32014-08-21 16:32:194174 EXPECT_EQ(200, r->GetResponseCode());
4175 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
[email protected]abe1c4a2013-10-25 19:28:514176 }
4177
4178 CapturingNetLog::CapturedEntryList entries;
4179 net_log_.GetEntries(&entries);
4180 size_t log_position = ExpectLogContainsSomewhereAfter(
4181 entries,
4182 0,
4183 NetLog::TYPE_DELEGATE_INFO,
4184 NetLog::PHASE_BEGIN);
4185
4186 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, log_position);
4187
4188 // Nothing else should add any delegate info to the request.
4189 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4190 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4191}
4192
4193// Tests handling of delegate info from a network delegate.
4194TEST_F(URLRequestTestHTTP, NetworkDelegateInfo) {
4195 ASSERT_TRUE(test_server_.Start());
4196
4197 TestDelegate request_delegate;
4198 AsyncLoggingNetworkDelegate network_delegate;
4199 TestURLRequestContext context(true);
4200 context.set_network_delegate(&network_delegate);
4201 context.set_net_log(&net_log_);
4202 context.Init();
4203
4204 {
[email protected]f7022f32014-08-21 16:32:194205 scoped_ptr<URLRequest> r(context.CreateRequest(
4206 test_server_.GetURL("simple.html"), DEFAULT_PRIORITY, &request_delegate,
4207 NULL));
4208 LoadStateWithParam load_state = r->GetLoadState();
[email protected]abe1c4a2013-10-25 19:28:514209 EXPECT_EQ(LOAD_STATE_IDLE, load_state.state);
[email protected]754bd202013-12-18 08:29:084210 EXPECT_EQ(base::string16(), load_state.param);
[email protected]abe1c4a2013-10-25 19:28:514211
[email protected]f7022f32014-08-21 16:32:194212 r->Start();
[email protected]abe1c4a2013-10-25 19:28:514213 base::RunLoop().Run();
4214
[email protected]f7022f32014-08-21 16:32:194215 EXPECT_EQ(200, r->GetResponseCode());
4216 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
[email protected]abe1c4a2013-10-25 19:28:514217 EXPECT_EQ(1, network_delegate.created_requests());
4218 EXPECT_EQ(0, network_delegate.destroyed_requests());
4219 }
4220 EXPECT_EQ(1, network_delegate.destroyed_requests());
4221
4222 size_t log_position = 0;
4223 CapturingNetLog::CapturedEntryList entries;
4224 net_log_.GetEntries(&entries);
4225 for (size_t i = 0; i < 3; ++i) {
4226 log_position = ExpectLogContainsSomewhereAfter(
4227 entries,
4228 log_position + 1,
4229 NetLog::TYPE_URL_REQUEST_DELEGATE,
4230 NetLog::PHASE_BEGIN);
4231
4232 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4233 log_position + 1);
4234
4235 ASSERT_LT(log_position, entries.size());
4236 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4237 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
[email protected]1826a402014-01-08 15:40:484238
4239 if (i == 1) {
4240 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4241 entries, log_position + 1);
4242 }
[email protected]abe1c4a2013-10-25 19:28:514243 }
4244
4245 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4246 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4247}
4248
4249// Tests handling of delegate info from a network delegate in the case of an
4250// HTTP redirect.
4251TEST_F(URLRequestTestHTTP, NetworkDelegateInfoRedirect) {
4252 ASSERT_TRUE(test_server_.Start());
4253
4254 TestDelegate request_delegate;
4255 AsyncLoggingNetworkDelegate network_delegate;
4256 TestURLRequestContext context(true);
4257 context.set_network_delegate(&network_delegate);
4258 context.set_net_log(&net_log_);
4259 context.Init();
4260
4261 {
[email protected]f7022f32014-08-21 16:32:194262 scoped_ptr<URLRequest> r(context.CreateRequest(
4263 test_server_.GetURL("server-redirect?simple.html"), DEFAULT_PRIORITY,
4264 &request_delegate, NULL));
4265 LoadStateWithParam load_state = r->GetLoadState();
[email protected]abe1c4a2013-10-25 19:28:514266 EXPECT_EQ(LOAD_STATE_IDLE, load_state.state);
[email protected]754bd202013-12-18 08:29:084267 EXPECT_EQ(base::string16(), load_state.param);
[email protected]abe1c4a2013-10-25 19:28:514268
[email protected]f7022f32014-08-21 16:32:194269 r->Start();
[email protected]abe1c4a2013-10-25 19:28:514270 base::RunLoop().Run();
4271
[email protected]f7022f32014-08-21 16:32:194272 EXPECT_EQ(200, r->GetResponseCode());
4273 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
[email protected]abe1c4a2013-10-25 19:28:514274 EXPECT_EQ(2, network_delegate.created_requests());
4275 EXPECT_EQ(0, network_delegate.destroyed_requests());
4276 }
4277 EXPECT_EQ(1, network_delegate.destroyed_requests());
4278
4279 size_t log_position = 0;
4280 CapturingNetLog::CapturedEntryList entries;
4281 net_log_.GetEntries(&entries);
4282 // The NetworkDelegate logged information in OnBeforeURLRequest,
4283 // OnBeforeSendHeaders, and OnHeadersReceived.
4284 for (size_t i = 0; i < 3; ++i) {
4285 log_position = ExpectLogContainsSomewhereAfter(
4286 entries,
4287 log_position + 1,
4288 NetLog::TYPE_URL_REQUEST_DELEGATE,
4289 NetLog::PHASE_BEGIN);
4290
4291 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4292 log_position + 1);
4293
4294 ASSERT_LT(log_position, entries.size());
4295 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4296 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
[email protected]1826a402014-01-08 15:40:484297
4298 if (i == 1) {
4299 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4300 entries, log_position + 1);
4301 }
[email protected]abe1c4a2013-10-25 19:28:514302 }
4303
4304 // The URLRequest::Delegate then gets informed about the redirect.
4305 log_position = ExpectLogContainsSomewhereAfter(
4306 entries,
4307 log_position + 1,
4308 NetLog::TYPE_URL_REQUEST_DELEGATE,
4309 NetLog::PHASE_BEGIN);
4310
4311 // The NetworkDelegate logged information in the same three events as before.
4312 for (size_t i = 0; i < 3; ++i) {
4313 log_position = ExpectLogContainsSomewhereAfter(
4314 entries,
4315 log_position + 1,
4316 NetLog::TYPE_URL_REQUEST_DELEGATE,
4317 NetLog::PHASE_BEGIN);
4318
4319 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4320 log_position + 1);
4321
4322 ASSERT_LT(log_position, entries.size());
4323 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4324 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4325 }
4326
4327 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4328 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4329}
4330
4331// Tests handling of delegate info from a network delegate in the case of HTTP
4332// AUTH.
4333TEST_F(URLRequestTestHTTP, NetworkDelegateInfoAuth) {
4334 ASSERT_TRUE(test_server_.Start());
4335
4336 TestDelegate request_delegate;
4337 AsyncLoggingNetworkDelegate network_delegate;
4338 TestURLRequestContext context(true);
4339 context.set_network_delegate(&network_delegate);
4340 context.set_net_log(&net_log_);
4341 context.Init();
4342
4343 {
[email protected]f7022f32014-08-21 16:32:194344 scoped_ptr<URLRequest> r(context.CreateRequest(
4345 test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &request_delegate,
4346 NULL));
4347 LoadStateWithParam load_state = r->GetLoadState();
[email protected]abe1c4a2013-10-25 19:28:514348 EXPECT_EQ(LOAD_STATE_IDLE, load_state.state);
[email protected]754bd202013-12-18 08:29:084349 EXPECT_EQ(base::string16(), load_state.param);
[email protected]abe1c4a2013-10-25 19:28:514350
[email protected]f7022f32014-08-21 16:32:194351 r->Start();
[email protected]abe1c4a2013-10-25 19:28:514352 base::RunLoop().Run();
4353
[email protected]f7022f32014-08-21 16:32:194354 EXPECT_EQ(200, r->GetResponseCode());
4355 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
[email protected]abe1c4a2013-10-25 19:28:514356 EXPECT_EQ(1, network_delegate.created_requests());
4357 EXPECT_EQ(0, network_delegate.destroyed_requests());
4358 }
4359 EXPECT_EQ(1, network_delegate.destroyed_requests());
4360
4361 size_t log_position = 0;
4362 CapturingNetLog::CapturedEntryList entries;
4363 net_log_.GetEntries(&entries);
4364 // The NetworkDelegate should have logged information in OnBeforeURLRequest,
4365 // OnBeforeSendHeaders, OnHeadersReceived, OnAuthRequired, and then again in
4366 // OnBeforeURLRequest and OnBeforeSendHeaders.
4367 for (size_t i = 0; i < 6; ++i) {
4368 log_position = ExpectLogContainsSomewhereAfter(
4369 entries,
4370 log_position + 1,
4371 NetLog::TYPE_URL_REQUEST_DELEGATE,
4372 NetLog::PHASE_BEGIN);
4373
4374 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4375 log_position + 1);
4376
4377 ASSERT_LT(log_position, entries.size());
4378 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4379 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
[email protected]1826a402014-01-08 15:40:484380
4381 if (i == 1) {
4382 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4383 entries, log_position + 1);
4384 }
[email protected]abe1c4a2013-10-25 19:28:514385 }
4386
4387 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4388 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4389}
4390
4391// Tests handling of delegate info from a URLRequest::Delegate.
4392TEST_F(URLRequestTestHTTP, URLRequestDelegateInfo) {
4393 ASSERT_TRUE(test_server_.Start());
4394
4395 AsyncLoggingUrlRequestDelegate request_delegate(
4396 AsyncLoggingUrlRequestDelegate::NO_CANCEL);
4397 TestURLRequestContext context(true);
4398 context.set_network_delegate(NULL);
4399 context.set_net_log(&net_log_);
4400 context.Init();
4401
4402 {
4403 // A chunked response with delays between chunks is used to make sure that
4404 // attempts by the URLRequest delegate to log information while reading the
4405 // body are ignored. Since they are ignored, this test is robust against
[email protected]1826a402014-01-08 15:40:484406 // the possibility of multiple reads being combined in the unlikely event
[email protected]abe1c4a2013-10-25 19:28:514407 // that it occurs.
[email protected]f7022f32014-08-21 16:32:194408 scoped_ptr<URLRequest> r(context.CreateRequest(
4409 test_server_.GetURL("chunked?waitBetweenChunks=20"), DEFAULT_PRIORITY,
4410 &request_delegate, NULL));
4411 LoadStateWithParam load_state = r->GetLoadState();
4412 r->Start();
[email protected]abe1c4a2013-10-25 19:28:514413 base::RunLoop().Run();
4414
[email protected]f7022f32014-08-21 16:32:194415 EXPECT_EQ(200, r->GetResponseCode());
4416 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
[email protected]abe1c4a2013-10-25 19:28:514417 }
4418
4419 CapturingNetLog::CapturedEntryList entries;
4420 net_log_.GetEntries(&entries);
4421
[email protected]1826a402014-01-08 15:40:484422 size_t log_position = 0;
4423
4424 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4425 entries, log_position);
4426
[email protected]abe1c4a2013-10-25 19:28:514427 // The delegate info should only have been logged on header complete. Other
4428 // times it should silently be ignored.
[email protected]1826a402014-01-08 15:40:484429 log_position =
4430 ExpectLogContainsSomewhereAfter(entries,
4431 log_position + 1,
4432 NetLog::TYPE_URL_REQUEST_DELEGATE,
4433 NetLog::PHASE_BEGIN);
[email protected]abe1c4a2013-10-25 19:28:514434
4435 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4436 log_position + 1);
4437
4438 ASSERT_LT(log_position, entries.size());
4439 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4440 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4441
4442 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4443 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4444 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4445 entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE));
4446}
4447
4448// Tests handling of delegate info from a URLRequest::Delegate in the case of
4449// an HTTP redirect.
4450TEST_F(URLRequestTestHTTP, URLRequestDelegateInfoOnRedirect) {
4451 ASSERT_TRUE(test_server_.Start());
4452
4453 AsyncLoggingUrlRequestDelegate request_delegate(
4454 AsyncLoggingUrlRequestDelegate::NO_CANCEL);
4455 TestURLRequestContext context(true);
4456 context.set_network_delegate(NULL);
4457 context.set_net_log(&net_log_);
4458 context.Init();
4459
4460 {
[email protected]f7022f32014-08-21 16:32:194461 scoped_ptr<URLRequest> r(context.CreateRequest(
4462 test_server_.GetURL("server-redirect?simple.html"), DEFAULT_PRIORITY,
4463 &request_delegate, NULL));
4464 LoadStateWithParam load_state = r->GetLoadState();
4465 r->Start();
[email protected]abe1c4a2013-10-25 19:28:514466 base::RunLoop().Run();
4467
[email protected]f7022f32014-08-21 16:32:194468 EXPECT_EQ(200, r->GetResponseCode());
4469 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
[email protected]abe1c4a2013-10-25 19:28:514470 }
4471
4472 CapturingNetLog::CapturedEntryList entries;
4473 net_log_.GetEntries(&entries);
4474
4475 // Delegate info should only have been logged in OnReceivedRedirect and
4476 // OnResponseStarted.
4477 size_t log_position = 0;
4478 for (int i = 0; i < 2; ++i) {
[email protected]1826a402014-01-08 15:40:484479 if (i == 0) {
4480 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4481 entries, log_position) + 1;
4482 }
4483
[email protected]abe1c4a2013-10-25 19:28:514484 log_position = ExpectLogContainsSomewhereAfter(
4485 entries,
4486 log_position,
4487 NetLog::TYPE_URL_REQUEST_DELEGATE,
4488 NetLog::PHASE_BEGIN);
4489
4490 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4491 log_position + 1);
4492
4493 ASSERT_LT(log_position, entries.size());
4494 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4495 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4496 }
4497
4498 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4499 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4500 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4501 entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE));
4502}
4503
4504// Tests handling of delegate info from a URLRequest::Delegate in the case of
4505// an HTTP redirect, with cancellation at various points.
4506TEST_F(URLRequestTestHTTP, URLRequestDelegateOnRedirectCancelled) {
4507 ASSERT_TRUE(test_server_.Start());
4508
4509 const AsyncLoggingUrlRequestDelegate::CancelStage kCancelStages[] = {
4510 AsyncLoggingUrlRequestDelegate::CANCEL_ON_RECEIVED_REDIRECT,
4511 AsyncLoggingUrlRequestDelegate::CANCEL_ON_RESPONSE_STARTED,
4512 AsyncLoggingUrlRequestDelegate::CANCEL_ON_READ_COMPLETED,
4513 };
4514
4515 for (size_t test_case = 0; test_case < arraysize(kCancelStages);
4516 ++test_case) {
4517 AsyncLoggingUrlRequestDelegate request_delegate(kCancelStages[test_case]);
4518 TestURLRequestContext context(true);
4519 CapturingNetLog net_log;
4520 context.set_network_delegate(NULL);
4521 context.set_net_log(&net_log);
4522 context.Init();
4523
4524 {
[email protected]f7022f32014-08-21 16:32:194525 scoped_ptr<URLRequest> r(context.CreateRequest(
4526 test_server_.GetURL("server-redirect?simple.html"), DEFAULT_PRIORITY,
4527 &request_delegate, NULL));
4528 LoadStateWithParam load_state = r->GetLoadState();
4529 r->Start();
[email protected]abe1c4a2013-10-25 19:28:514530 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:194531 EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status());
[email protected]abe1c4a2013-10-25 19:28:514532 }
4533
4534 CapturingNetLog::CapturedEntryList entries;
4535 net_log.GetEntries(&entries);
4536
4537 // Delegate info is always logged in both OnReceivedRedirect and
4538 // OnResponseStarted. In the CANCEL_ON_RECEIVED_REDIRECT, the
4539 // OnResponseStarted delegate call is after cancellation, but logging is
4540 // still currently supported in that call.
4541 size_t log_position = 0;
4542 for (int i = 0; i < 2; ++i) {
[email protected]1826a402014-01-08 15:40:484543 if (i == 0) {
4544 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4545 entries, log_position) + 1;
4546 }
4547
[email protected]abe1c4a2013-10-25 19:28:514548 log_position = ExpectLogContainsSomewhereAfter(
4549 entries,
4550 log_position,
4551 NetLog::TYPE_URL_REQUEST_DELEGATE,
4552 NetLog::PHASE_BEGIN);
4553
4554 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4555 log_position + 1);
4556
4557 ASSERT_LT(log_position, entries.size());
4558 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4559 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4560 }
4561
4562 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4563 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4564 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4565 entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE));
4566 }
4567}
4568
[email protected]847c0fa92012-11-06 16:37:424569namespace {
4570
4571const char kExtraHeader[] = "Allow-Snafu";
4572const char kExtraValue[] = "fubar";
4573
4574class RedirectWithAdditionalHeadersDelegate : public TestDelegate {
dchengb03027d2014-10-21 12:00:204575 void OnReceivedRedirect(URLRequest* request,
4576 const RedirectInfo& redirect_info,
4577 bool* defer_redirect) override {
[email protected]cba24642014-08-15 20:49:594578 TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect);
[email protected]847c0fa92012-11-06 16:37:424579 request->SetExtraRequestHeaderByName(kExtraHeader, kExtraValue, false);
4580 }
4581};
4582
4583} // namespace
4584
4585TEST_F(URLRequestTestHTTP, RedirectWithAdditionalHeadersTest) {
4586 ASSERT_TRUE(test_server_.Start());
4587
4588 GURL destination_url = test_server_.GetURL(
4589 "echoheader?" + std::string(kExtraHeader));
4590 GURL original_url = test_server_.GetURL(
4591 "server-redirect?" + destination_url.spec());
4592 RedirectWithAdditionalHeadersDelegate d;
[email protected]f7022f32014-08-21 16:32:194593 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
4594 original_url, DEFAULT_PRIORITY, &d, NULL));
4595 req->Start();
[email protected]255620da2013-08-19 13:14:294596 base::RunLoop().Run();
[email protected]847c0fa92012-11-06 16:37:424597
4598 std::string value;
[email protected]f7022f32014-08-21 16:32:194599 const HttpRequestHeaders& headers = req->extra_request_headers();
[email protected]847c0fa92012-11-06 16:37:424600 EXPECT_TRUE(headers.GetHeader(kExtraHeader, &value));
4601 EXPECT_EQ(kExtraValue, value);
[email protected]f7022f32014-08-21 16:32:194602 EXPECT_FALSE(req->is_pending());
4603 EXPECT_FALSE(req->is_redirecting());
[email protected]847c0fa92012-11-06 16:37:424604 EXPECT_EQ(kExtraValue, d.data_received());
4605}
4606
[email protected]251a1b92012-11-13 11:01:094607namespace {
4608
4609const char kExtraHeaderToRemove[] = "To-Be-Removed";
4610
4611class RedirectWithHeaderRemovalDelegate : public TestDelegate {
dchengb03027d2014-10-21 12:00:204612 void OnReceivedRedirect(URLRequest* request,
4613 const RedirectInfo& redirect_info,
4614 bool* defer_redirect) override {
[email protected]cba24642014-08-15 20:49:594615 TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect);
[email protected]251a1b92012-11-13 11:01:094616 request->RemoveRequestHeaderByName(kExtraHeaderToRemove);
4617 }
4618};
4619
4620} // namespace
4621
4622TEST_F(URLRequestTestHTTP, RedirectWithHeaderRemovalTest) {
4623 ASSERT_TRUE(test_server_.Start());
4624
4625 GURL destination_url = test_server_.GetURL(
4626 "echoheader?" + std::string(kExtraHeaderToRemove));
4627 GURL original_url = test_server_.GetURL(
4628 "server-redirect?" + destination_url.spec());
4629 RedirectWithHeaderRemovalDelegate d;
[email protected]f7022f32014-08-21 16:32:194630 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
4631 original_url, DEFAULT_PRIORITY, &d, NULL));
4632 req->SetExtraRequestHeaderByName(kExtraHeaderToRemove, "dummy", false);
4633 req->Start();
[email protected]255620da2013-08-19 13:14:294634 base::RunLoop().Run();
[email protected]251a1b92012-11-13 11:01:094635
4636 std::string value;
[email protected]f7022f32014-08-21 16:32:194637 const HttpRequestHeaders& headers = req->extra_request_headers();
[email protected]251a1b92012-11-13 11:01:094638 EXPECT_FALSE(headers.GetHeader(kExtraHeaderToRemove, &value));
[email protected]f7022f32014-08-21 16:32:194639 EXPECT_FALSE(req->is_pending());
4640 EXPECT_FALSE(req->is_redirecting());
[email protected]251a1b92012-11-13 11:01:094641 EXPECT_EQ("None", d.data_received());
4642}
4643
[email protected]316c1e5e2012-09-12 15:17:444644TEST_F(URLRequestTestHTTP, CancelTest) {
4645 TestDelegate d;
4646 {
[email protected]f7022f32014-08-21 16:32:194647 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
4648 GURL("http://www.google.com/"), DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:444649
[email protected]f7022f32014-08-21 16:32:194650 r->Start();
4651 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:444652
[email protected]f7022f32014-08-21 16:32:194653 r->Cancel();
[email protected]316c1e5e2012-09-12 15:17:444654
[email protected]255620da2013-08-19 13:14:294655 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:444656
4657 // We expect to receive OnResponseStarted even though the request has been
4658 // cancelled.
4659 EXPECT_EQ(1, d.response_started_count());
4660 EXPECT_EQ(0, d.bytes_received());
4661 EXPECT_FALSE(d.received_data_before_response());
4662 }
4663}
4664
4665TEST_F(URLRequestTestHTTP, CancelTest2) {
4666 ASSERT_TRUE(test_server_.Start());
4667
4668 TestDelegate d;
4669 {
[email protected]f7022f32014-08-21 16:32:194670 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
4671 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:444672
4673 d.set_cancel_in_response_started(true);
4674
[email protected]f7022f32014-08-21 16:32:194675 r->Start();
4676 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:444677
[email protected]255620da2013-08-19 13:14:294678 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:444679
4680 EXPECT_EQ(1, d.response_started_count());
4681 EXPECT_EQ(0, d.bytes_received());
4682 EXPECT_FALSE(d.received_data_before_response());
[email protected]f7022f32014-08-21 16:32:194683 EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status());
[email protected]316c1e5e2012-09-12 15:17:444684 }
4685}
4686
4687TEST_F(URLRequestTestHTTP, CancelTest3) {
4688 ASSERT_TRUE(test_server_.Start());
4689
4690 TestDelegate d;
4691 {
[email protected]f7022f32014-08-21 16:32:194692 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
4693 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:444694
4695 d.set_cancel_in_received_data(true);
4696
[email protected]f7022f32014-08-21 16:32:194697 r->Start();
4698 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:444699
[email protected]255620da2013-08-19 13:14:294700 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:444701
4702 EXPECT_EQ(1, d.response_started_count());
4703 // There is no guarantee about how much data was received
4704 // before the cancel was issued. It could have been 0 bytes,
4705 // or it could have been all the bytes.
4706 // EXPECT_EQ(0, d.bytes_received());
4707 EXPECT_FALSE(d.received_data_before_response());
[email protected]f7022f32014-08-21 16:32:194708 EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status());
[email protected]316c1e5e2012-09-12 15:17:444709 }
4710}
4711
4712TEST_F(URLRequestTestHTTP, CancelTest4) {
4713 ASSERT_TRUE(test_server_.Start());
4714
4715 TestDelegate d;
4716 {
[email protected]f7022f32014-08-21 16:32:194717 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
4718 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:444719
[email protected]f7022f32014-08-21 16:32:194720 r->Start();
4721 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:444722
4723 // The request will be implicitly canceled when it is destroyed. The
4724 // test delegate must not post a quit message when this happens because
4725 // this test doesn't actually have a message loop. The quit message would
4726 // get put on this thread's message queue and the next test would exit
4727 // early, causing problems.
4728 d.set_quit_on_complete(false);
4729 }
4730 // expect things to just cleanup properly.
4731
4732 // we won't actually get a received reponse here because we've never run the
4733 // message loop
4734 EXPECT_FALSE(d.received_data_before_response());
4735 EXPECT_EQ(0, d.bytes_received());
4736}
4737
4738TEST_F(URLRequestTestHTTP, CancelTest5) {
4739 ASSERT_TRUE(test_server_.Start());
4740
4741 // populate cache
4742 {
4743 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:194744 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
4745 test_server_.GetURL("cachetime"), DEFAULT_PRIORITY, &d, NULL));
4746 r->Start();
[email protected]255620da2013-08-19 13:14:294747 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:194748 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
[email protected]316c1e5e2012-09-12 15:17:444749 }
4750
4751 // cancel read from cache (see bug 990242)
4752 {
4753 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:194754 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
4755 test_server_.GetURL("cachetime"), DEFAULT_PRIORITY, &d, NULL));
4756 r->Start();
4757 r->Cancel();
[email protected]255620da2013-08-19 13:14:294758 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:444759
[email protected]f7022f32014-08-21 16:32:194760 EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status());
[email protected]316c1e5e2012-09-12 15:17:444761 EXPECT_EQ(1, d.response_started_count());
4762 EXPECT_EQ(0, d.bytes_received());
4763 EXPECT_FALSE(d.received_data_before_response());
4764 }
4765}
4766
4767TEST_F(URLRequestTestHTTP, PostTest) {
4768 ASSERT_TRUE(test_server_.Start());
4769 HTTPUploadDataOperationTest("POST");
4770}
4771
4772TEST_F(URLRequestTestHTTP, PutTest) {
4773 ASSERT_TRUE(test_server_.Start());
4774 HTTPUploadDataOperationTest("PUT");
4775}
4776
4777TEST_F(URLRequestTestHTTP, PostEmptyTest) {
4778 ASSERT_TRUE(test_server_.Start());
4779
4780 TestDelegate d;
4781 {
[email protected]f7022f32014-08-21 16:32:194782 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
4783 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, NULL));
4784 r->set_method("POST");
[email protected]316c1e5e2012-09-12 15:17:444785
[email protected]f7022f32014-08-21 16:32:194786 r->Start();
4787 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:444788
[email protected]255620da2013-08-19 13:14:294789 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:444790
[email protected]329b68b2012-11-14 17:54:274791 ASSERT_EQ(1, d.response_started_count())
[email protected]f7022f32014-08-21 16:32:194792 << "request failed: " << r->status().status()
4793 << ", error: " << r->status().error();
[email protected]316c1e5e2012-09-12 15:17:444794
4795 EXPECT_FALSE(d.received_data_before_response());
4796 EXPECT_TRUE(d.data_received().empty());
4797 }
4798}
4799
4800TEST_F(URLRequestTestHTTP, PostFileTest) {
4801 ASSERT_TRUE(test_server_.Start());
4802
4803 TestDelegate d;
4804 {
[email protected]f7022f32014-08-21 16:32:194805 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
4806 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, NULL));
4807 r->set_method("POST");
[email protected]316c1e5e2012-09-12 15:17:444808
[email protected]6cdfd7f2013-02-08 20:40:154809 base::FilePath dir;
[email protected]316c1e5e2012-09-12 15:17:444810 PathService::Get(base::DIR_EXE, &dir);
[email protected]37b3c1992014-03-11 20:59:024811 base::SetCurrentDirectory(dir);
[email protected]316c1e5e2012-09-12 15:17:444812
[email protected]f288ef02012-12-15 20:28:284813 ScopedVector<UploadElementReader> element_readers;
[email protected]316c1e5e2012-09-12 15:17:444814
[email protected]6cdfd7f2013-02-08 20:40:154815 base::FilePath path;
[email protected]316c1e5e2012-09-12 15:17:444816 PathService::Get(base::DIR_SOURCE_ROOT, &path);
4817 path = path.Append(FILE_PATH_LITERAL("net"));
4818 path = path.Append(FILE_PATH_LITERAL("data"));
4819 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
4820 path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
[email protected]cadac622013-06-11 16:46:364821 element_readers.push_back(
4822 new UploadFileElementReader(base::MessageLoopProxy::current().get(),
4823 path,
4824 0,
4825 kuint64max,
4826 base::Time()));
mmenkecbc2b712014-10-09 20:29:074827 r->set_upload(make_scoped_ptr<UploadDataStream>(
4828 new ElementsUploadDataStream(element_readers.Pass(), 0)));
[email protected]316c1e5e2012-09-12 15:17:444829
[email protected]f7022f32014-08-21 16:32:194830 r->Start();
4831 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:444832
[email protected]255620da2013-08-19 13:14:294833 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:444834
[email protected]329b68b2012-11-14 17:54:274835 int64 size = 0;
[email protected]56285702013-12-04 18:22:494836 ASSERT_EQ(true, base::GetFileSize(path, &size));
[email protected]4356f0f2013-04-07 00:58:174837 scoped_ptr<char[]> buf(new char[size]);
[email protected]316c1e5e2012-09-12 15:17:444838
[email protected]7600d0b2013-12-08 21:43:304839 ASSERT_EQ(size, base::ReadFile(path, buf.get(), size));
[email protected]316c1e5e2012-09-12 15:17:444840
[email protected]329b68b2012-11-14 17:54:274841 ASSERT_EQ(1, d.response_started_count())
[email protected]f7022f32014-08-21 16:32:194842 << "request failed: " << r->status().status()
4843 << ", error: " << r->status().error();
[email protected]316c1e5e2012-09-12 15:17:444844
4845 EXPECT_FALSE(d.received_data_before_response());
4846
[email protected]329b68b2012-11-14 17:54:274847 EXPECT_EQ(size, d.bytes_received());
4848 EXPECT_EQ(std::string(&buf[0], size), d.data_received());
[email protected]316c1e5e2012-09-12 15:17:444849 }
4850}
4851
[email protected]999dd8c2013-11-12 06:45:544852TEST_F(URLRequestTestHTTP, PostUnreadableFileTest) {
4853 ASSERT_TRUE(test_server_.Start());
4854
4855 TestDelegate d;
4856 {
[email protected]f7022f32014-08-21 16:32:194857 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
4858 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, NULL));
4859 r->set_method("POST");
[email protected]999dd8c2013-11-12 06:45:544860
4861 ScopedVector<UploadElementReader> element_readers;
4862
4863 element_readers.push_back(new UploadFileElementReader(
4864 base::MessageLoopProxy::current().get(),
4865 base::FilePath(FILE_PATH_LITERAL(
4866 "c:\\path\\to\\non\\existant\\file.randomness.12345")),
4867 0,
4868 kuint64max,
4869 base::Time()));
mmenkecbc2b712014-10-09 20:29:074870 r->set_upload(make_scoped_ptr<UploadDataStream>(
4871 new ElementsUploadDataStream(element_readers.Pass(), 0)));
[email protected]999dd8c2013-11-12 06:45:544872
[email protected]f7022f32014-08-21 16:32:194873 r->Start();
4874 EXPECT_TRUE(r->is_pending());
[email protected]999dd8c2013-11-12 06:45:544875
4876 base::RunLoop().Run();
4877
[email protected]999dd8c2013-11-12 06:45:544878 EXPECT_TRUE(d.request_failed());
4879 EXPECT_FALSE(d.received_data_before_response());
4880 EXPECT_EQ(0, d.bytes_received());
[email protected]f7022f32014-08-21 16:32:194881 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
4882 EXPECT_EQ(ERR_FILE_NOT_FOUND, r->status().error());
[email protected]999dd8c2013-11-12 06:45:544883 }
4884}
4885
[email protected]316c1e5e2012-09-12 15:17:444886TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) {
4887 ASSERT_TRUE(test_server_.Start());
4888
4889 TestDelegate d;
4890 {
[email protected]f7022f32014-08-21 16:32:194891 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
4892 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, NULL));
4893 r->EnableChunkedUpload();
4894 r->set_method("POST");
4895 AddChunksToUpload(r.get());
4896 r->Start();
4897 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:444898
[email protected]255620da2013-08-19 13:14:294899 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:444900
[email protected]f7022f32014-08-21 16:32:194901 VerifyReceivedDataMatchesChunks(r.get(), &d);
[email protected]316c1e5e2012-09-12 15:17:444902 }
4903}
4904
[email protected]329b68b2012-11-14 17:54:274905TEST_F(URLRequestTestHTTP, TestPostChunkedDataJustAfterStart) {
4906 ASSERT_TRUE(test_server_.Start());
4907
4908 TestDelegate d;
4909 {
[email protected]f7022f32014-08-21 16:32:194910 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
4911 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, NULL));
4912 r->EnableChunkedUpload();
4913 r->set_method("POST");
4914 r->Start();
4915 EXPECT_TRUE(r->is_pending());
4916 AddChunksToUpload(r.get());
[email protected]255620da2013-08-19 13:14:294917 base::RunLoop().Run();
[email protected]329b68b2012-11-14 17:54:274918
[email protected]f7022f32014-08-21 16:32:194919 VerifyReceivedDataMatchesChunks(r.get(), &d);
[email protected]329b68b2012-11-14 17:54:274920 }
4921}
4922
[email protected]316c1e5e2012-09-12 15:17:444923TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) {
4924 ASSERT_TRUE(test_server_.Start());
4925
4926 TestDelegate d;
4927 {
[email protected]f7022f32014-08-21 16:32:194928 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
4929 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, NULL));
4930 r->EnableChunkedUpload();
4931 r->set_method("POST");
4932 r->Start();
4933 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:444934
[email protected]255620da2013-08-19 13:14:294935 base::RunLoop().RunUntilIdle();
[email protected]f7022f32014-08-21 16:32:194936 AddChunksToUpload(r.get());
[email protected]255620da2013-08-19 13:14:294937 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:444938
[email protected]f7022f32014-08-21 16:32:194939 VerifyReceivedDataMatchesChunks(r.get(), &d);
[email protected]316c1e5e2012-09-12 15:17:444940 }
4941}
4942
4943TEST_F(URLRequestTestHTTP, ResponseHeadersTest) {
4944 ASSERT_TRUE(test_server_.Start());
4945
4946 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:194947 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
4948 test_server_.GetURL("files/with-headers.html"), DEFAULT_PRIORITY, &d,
4949 NULL));
4950 req->Start();
[email protected]255620da2013-08-19 13:14:294951 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:444952
[email protected]f7022f32014-08-21 16:32:194953 const HttpResponseHeaders* headers = req->response_headers();
[email protected]316c1e5e2012-09-12 15:17:444954
4955 // Simple sanity check that response_info() accesses the same data.
[email protected]f7022f32014-08-21 16:32:194956 EXPECT_EQ(headers, req->response_info().headers.get());
[email protected]316c1e5e2012-09-12 15:17:444957
4958 std::string header;
4959 EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header));
4960 EXPECT_EQ("private", header);
4961
4962 header.clear();
4963 EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header));
4964 EXPECT_EQ("text/html; charset=ISO-8859-1", header);
4965
4966 // The response has two "X-Multiple-Entries" headers.
4967 // This verfies our output has them concatenated together.
4968 header.clear();
4969 EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header));
4970 EXPECT_EQ("a, b", header);
4971}
4972
[email protected]242d8562012-10-30 21:20:464973TEST_F(URLRequestTestHTTP, ProcessSTS) {
[email protected]ce7d0cbc2013-05-03 18:57:224974 SpawnedTestServer::SSLOptions ssl_options;
4975 SpawnedTestServer https_test_server(
4976 SpawnedTestServer::TYPE_HTTPS,
[email protected]242d8562012-10-30 21:20:464977 ssl_options,
[email protected]6cdfd7f2013-02-08 20:40:154978 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
[email protected]242d8562012-10-30 21:20:464979 ASSERT_TRUE(https_test_server.Start());
4980
4981 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:194982 scoped_ptr<URLRequest> request(default_context_.CreateRequest(
4983 https_test_server.GetURL("files/hsts-headers.html"), DEFAULT_PRIORITY, &d,
4984 NULL));
4985 request->Start();
[email protected]255620da2013-08-19 13:14:294986 base::RunLoop().Run();
[email protected]242d8562012-10-30 21:20:464987
4988 TransportSecurityState* security_state =
4989 default_context_.transport_security_state();
[email protected]242d8562012-10-30 21:20:464990 TransportSecurityState::DomainState domain_state;
[email protected]9e6968d2014-05-07 21:46:264991 EXPECT_TRUE(security_state->GetDynamicDomainState(
4992 SpawnedTestServer::kLocalhost, &domain_state));
[email protected]242d8562012-10-30 21:20:464993 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
[email protected]9e6968d2014-05-07 21:46:264994 domain_state.sts.upgrade_mode);
4995 EXPECT_TRUE(domain_state.sts.include_subdomains);
4996 EXPECT_FALSE(domain_state.pkp.include_subdomains);
[email protected]37fd55fb2013-06-29 13:13:274997#if defined(OS_ANDROID)
4998 // Android's CertVerifyProc does not (yet) handle pins.
4999#else
5000 EXPECT_FALSE(domain_state.HasPublicKeyPins());
5001#endif
5002}
5003
5004// Android's CertVerifyProc does not (yet) handle pins. Therefore, it will
5005// reject HPKP headers, and a test setting only HPKP headers will fail (no
5006// DomainState present because header rejected).
5007#if defined(OS_ANDROID)
5008#define MAYBE_ProcessPKP DISABLED_ProcessPKP
5009#else
5010#define MAYBE_ProcessPKP ProcessPKP
5011#endif
5012
5013// Tests that enabling HPKP on a domain does not affect the HSTS
5014// validity/expiration.
5015TEST_F(URLRequestTestHTTP, MAYBE_ProcessPKP) {
5016 SpawnedTestServer::SSLOptions ssl_options;
5017 SpawnedTestServer https_test_server(
5018 SpawnedTestServer::TYPE_HTTPS,
5019 ssl_options,
5020 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
5021 ASSERT_TRUE(https_test_server.Start());
5022
5023 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195024 scoped_ptr<URLRequest> request(default_context_.CreateRequest(
5025 https_test_server.GetURL("files/hpkp-headers.html"), DEFAULT_PRIORITY, &d,
5026 NULL));
5027 request->Start();
[email protected]255620da2013-08-19 13:14:295028 base::RunLoop().Run();
[email protected]37fd55fb2013-06-29 13:13:275029
5030 TransportSecurityState* security_state =
5031 default_context_.transport_security_state();
[email protected]37fd55fb2013-06-29 13:13:275032 TransportSecurityState::DomainState domain_state;
[email protected]9e6968d2014-05-07 21:46:265033 EXPECT_TRUE(security_state->GetDynamicDomainState(
5034 SpawnedTestServer::kLocalhost, &domain_state));
[email protected]37fd55fb2013-06-29 13:13:275035 EXPECT_EQ(TransportSecurityState::DomainState::MODE_DEFAULT,
[email protected]9e6968d2014-05-07 21:46:265036 domain_state.sts.upgrade_mode);
5037 EXPECT_FALSE(domain_state.sts.include_subdomains);
5038 EXPECT_FALSE(domain_state.pkp.include_subdomains);
[email protected]37fd55fb2013-06-29 13:13:275039 EXPECT_TRUE(domain_state.HasPublicKeyPins());
[email protected]9e6968d2014-05-07 21:46:265040 EXPECT_NE(domain_state.sts.expiry, domain_state.pkp.expiry);
[email protected]242d8562012-10-30 21:20:465041}
5042
5043TEST_F(URLRequestTestHTTP, ProcessSTSOnce) {
[email protected]ce7d0cbc2013-05-03 18:57:225044 SpawnedTestServer::SSLOptions ssl_options;
5045 SpawnedTestServer https_test_server(
5046 SpawnedTestServer::TYPE_HTTPS,
[email protected]242d8562012-10-30 21:20:465047 ssl_options,
[email protected]6cdfd7f2013-02-08 20:40:155048 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
[email protected]242d8562012-10-30 21:20:465049 ASSERT_TRUE(https_test_server.Start());
5050
5051 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195052 scoped_ptr<URLRequest> request(default_context_.CreateRequest(
[email protected]242d8562012-10-30 21:20:465053 https_test_server.GetURL("files/hsts-multiple-headers.html"),
[email protected]f7022f32014-08-21 16:32:195054 DEFAULT_PRIORITY, &d, NULL));
5055 request->Start();
[email protected]255620da2013-08-19 13:14:295056 base::RunLoop().Run();
[email protected]242d8562012-10-30 21:20:465057
5058 // We should have set parameters from the first header, not the second.
5059 TransportSecurityState* security_state =
5060 default_context_.transport_security_state();
[email protected]242d8562012-10-30 21:20:465061 TransportSecurityState::DomainState domain_state;
[email protected]9e6968d2014-05-07 21:46:265062 EXPECT_TRUE(security_state->GetDynamicDomainState(
5063 SpawnedTestServer::kLocalhost, &domain_state));
[email protected]242d8562012-10-30 21:20:465064 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
[email protected]9e6968d2014-05-07 21:46:265065 domain_state.sts.upgrade_mode);
5066 EXPECT_FALSE(domain_state.sts.include_subdomains);
5067 EXPECT_FALSE(domain_state.pkp.include_subdomains);
[email protected]242d8562012-10-30 21:20:465068}
5069
[email protected]9f972ec2013-04-10 20:24:365070TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP) {
[email protected]ce7d0cbc2013-05-03 18:57:225071 SpawnedTestServer::SSLOptions ssl_options;
5072 SpawnedTestServer https_test_server(
5073 SpawnedTestServer::TYPE_HTTPS,
[email protected]9f972ec2013-04-10 20:24:365074 ssl_options,
5075 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
5076 ASSERT_TRUE(https_test_server.Start());
5077
5078 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195079 scoped_ptr<URLRequest> request(default_context_.CreateRequest(
[email protected]9f972ec2013-04-10 20:24:365080 https_test_server.GetURL("files/hsts-and-hpkp-headers.html"),
[email protected]f7022f32014-08-21 16:32:195081 DEFAULT_PRIORITY, &d, NULL));
5082 request->Start();
[email protected]255620da2013-08-19 13:14:295083 base::RunLoop().Run();
[email protected]9f972ec2013-04-10 20:24:365084
5085 // We should have set parameters from the first header, not the second.
5086 TransportSecurityState* security_state =
5087 default_context_.transport_security_state();
[email protected]9f972ec2013-04-10 20:24:365088 TransportSecurityState::DomainState domain_state;
[email protected]9e6968d2014-05-07 21:46:265089 EXPECT_TRUE(security_state->GetDynamicDomainState(
5090 SpawnedTestServer::kLocalhost, &domain_state));
[email protected]9f972ec2013-04-10 20:24:365091 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
[email protected]9e6968d2014-05-07 21:46:265092 domain_state.sts.upgrade_mode);
[email protected]9f972ec2013-04-10 20:24:365093#if defined(OS_ANDROID)
5094 // Android's CertVerifyProc does not (yet) handle pins.
5095#else
5096 EXPECT_TRUE(domain_state.HasPublicKeyPins());
5097#endif
[email protected]9e6968d2014-05-07 21:46:265098 EXPECT_NE(domain_state.sts.expiry, domain_state.pkp.expiry);
[email protected]9f972ec2013-04-10 20:24:365099
[email protected]a165f092013-06-12 16:10:055100 // Even though there is an HSTS header asserting includeSubdomains, it is
5101 // the *second* such header, and we MUST process only the first.
[email protected]9e6968d2014-05-07 21:46:265102 EXPECT_FALSE(domain_state.sts.include_subdomains);
[email protected]a165f092013-06-12 16:10:055103 // includeSubdomains does not occur in the test HPKP header.
[email protected]9e6968d2014-05-07 21:46:265104 EXPECT_FALSE(domain_state.pkp.include_subdomains);
[email protected]9f972ec2013-04-10 20:24:365105}
5106
[email protected]37fd55fb2013-06-29 13:13:275107// Tests that when multiple HPKP headers are present, asserting different
5108// policies, that only the first such policy is processed.
5109TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP2) {
5110 SpawnedTestServer::SSLOptions ssl_options;
5111 SpawnedTestServer https_test_server(
5112 SpawnedTestServer::TYPE_HTTPS,
5113 ssl_options,
5114 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
5115 ASSERT_TRUE(https_test_server.Start());
5116
5117 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195118 scoped_ptr<URLRequest> request(default_context_.CreateRequest(
[email protected]37fd55fb2013-06-29 13:13:275119 https_test_server.GetURL("files/hsts-and-hpkp-headers2.html"),
[email protected]f7022f32014-08-21 16:32:195120 DEFAULT_PRIORITY, &d, NULL));
5121 request->Start();
[email protected]255620da2013-08-19 13:14:295122 base::RunLoop().Run();
[email protected]37fd55fb2013-06-29 13:13:275123
5124 TransportSecurityState* security_state =
5125 default_context_.transport_security_state();
[email protected]37fd55fb2013-06-29 13:13:275126 TransportSecurityState::DomainState domain_state;
[email protected]9e6968d2014-05-07 21:46:265127 EXPECT_TRUE(security_state->GetDynamicDomainState(
5128 SpawnedTestServer::kLocalhost, &domain_state));
[email protected]37fd55fb2013-06-29 13:13:275129 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
[email protected]9e6968d2014-05-07 21:46:265130 domain_state.sts.upgrade_mode);
[email protected]37fd55fb2013-06-29 13:13:275131#if defined(OS_ANDROID)
5132 // Android's CertVerifyProc does not (yet) handle pins.
5133#else
5134 EXPECT_TRUE(domain_state.HasPublicKeyPins());
5135#endif
[email protected]9e6968d2014-05-07 21:46:265136 EXPECT_NE(domain_state.sts.expiry, domain_state.pkp.expiry);
[email protected]37fd55fb2013-06-29 13:13:275137
[email protected]9e6968d2014-05-07 21:46:265138 EXPECT_TRUE(domain_state.sts.include_subdomains);
5139 EXPECT_FALSE(domain_state.pkp.include_subdomains);
[email protected]37fd55fb2013-06-29 13:13:275140}
5141
[email protected]316c1e5e2012-09-12 15:17:445142TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) {
5143 ASSERT_TRUE(test_server_.Start());
5144
5145 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195146 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5147 test_server_.GetURL("files/content-type-normalization.html"),
5148 DEFAULT_PRIORITY, &d, NULL));
5149 req->Start();
[email protected]255620da2013-08-19 13:14:295150 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445151
5152 std::string mime_type;
[email protected]f7022f32014-08-21 16:32:195153 req->GetMimeType(&mime_type);
[email protected]316c1e5e2012-09-12 15:17:445154 EXPECT_EQ("text/html", mime_type);
5155
5156 std::string charset;
[email protected]f7022f32014-08-21 16:32:195157 req->GetCharset(&charset);
[email protected]316c1e5e2012-09-12 15:17:445158 EXPECT_EQ("utf-8", charset);
[email protected]f7022f32014-08-21 16:32:195159 req->Cancel();
[email protected]316c1e5e2012-09-12 15:17:445160}
5161
[email protected]02494ec2014-05-07 15:05:295162TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictDataRedirects) {
[email protected]e0f35c92013-05-08 16:04:345163 // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget().
[email protected]e0f35c92013-05-08 16:04:345164 GURL data_url("data:,foo");
[email protected]e0f35c92013-05-08 16:04:345165 DataProtocolHandler data_protocol_handler;
[email protected]588614c22013-08-16 00:09:025166 EXPECT_FALSE(data_protocol_handler.IsSafeRedirectTarget(data_url));
[email protected]e0f35c92013-05-08 16:04:345167
5168 // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget().
[email protected]588614c22013-08-16 00:09:025169 EXPECT_FALSE(job_factory_.IsSafeRedirectTarget(data_url));
[email protected]e0f35c92013-05-08 16:04:345170}
5171
[email protected]02494ec2014-05-07 15:05:295172#if !defined(DISABLE_FILE_SUPPORT)
5173TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictFileRedirects) {
5174 // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget().
5175 GURL file_url("file:///foo.txt");
5176 FileProtocolHandler file_protocol_handler(base::MessageLoopProxy::current());
5177 EXPECT_FALSE(file_protocol_handler.IsSafeRedirectTarget(file_url));
5178
5179 // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget().
5180 EXPECT_FALSE(job_factory_.IsSafeRedirectTarget(file_url));
5181}
5182
[email protected]588614c22013-08-16 00:09:025183TEST_F(URLRequestTestHTTP, RestrictFileRedirects) {
[email protected]316c1e5e2012-09-12 15:17:445184 ASSERT_TRUE(test_server_.Start());
5185
5186 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195187 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5188 test_server_.GetURL("files/redirect-to-file.html"), DEFAULT_PRIORITY, &d,
5189 NULL));
5190 req->Start();
[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 EXPECT_EQ(URLRequestStatus::FAILED, req->status().status());
5194 EXPECT_EQ(ERR_UNSAFE_REDIRECT, req->status().error());
[email protected]316c1e5e2012-09-12 15:17:445195}
[email protected]02494ec2014-05-07 15:05:295196#endif // !defined(DISABLE_FILE_SUPPORT)
[email protected]316c1e5e2012-09-12 15:17:445197
[email protected]588614c22013-08-16 00:09:025198TEST_F(URLRequestTestHTTP, RestrictDataRedirects) {
5199 ASSERT_TRUE(test_server_.Start());
5200
5201 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195202 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5203 test_server_.GetURL("files/redirect-to-data.html"), DEFAULT_PRIORITY, &d,
5204 NULL));
5205 req->Start();
[email protected]588614c22013-08-16 00:09:025206 base::MessageLoop::current()->Run();
5207
[email protected]f7022f32014-08-21 16:32:195208 EXPECT_EQ(URLRequestStatus::FAILED, req->status().status());
5209 EXPECT_EQ(ERR_UNSAFE_REDIRECT, req->status().error());
[email protected]588614c22013-08-16 00:09:025210}
5211
[email protected]316c1e5e2012-09-12 15:17:445212TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) {
5213 ASSERT_TRUE(test_server_.Start());
5214
5215 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195216 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5217 test_server_.GetURL("files/redirect-to-invalid-url.html"),
5218 DEFAULT_PRIORITY, &d, NULL));
5219 req->Start();
[email protected]255620da2013-08-19 13:14:295220 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445221
[email protected]f7022f32014-08-21 16:32:195222 EXPECT_EQ(URLRequestStatus::FAILED, req->status().status());
5223 EXPECT_EQ(ERR_INVALID_URL, req->status().error());
[email protected]316c1e5e2012-09-12 15:17:445224}
5225
[email protected]e50efea2014-03-24 18:41:005226// Make sure redirects are cached, despite not reading their bodies.
5227TEST_F(URLRequestTestHTTP, CacheRedirect) {
5228 ASSERT_TRUE(test_server_.Start());
5229 GURL redirect_url =
5230 test_server_.GetURL("files/redirect302-to-echo-cacheable");
5231
5232 {
5233 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195234 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5235 redirect_url, DEFAULT_PRIORITY, &d, NULL));
5236 req->Start();
[email protected]e50efea2014-03-24 18:41:005237 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:195238 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
[email protected]e50efea2014-03-24 18:41:005239 EXPECT_EQ(1, d.received_redirect_count());
[email protected]f7022f32014-08-21 16:32:195240 EXPECT_EQ(test_server_.GetURL("echo"), req->url());
[email protected]e50efea2014-03-24 18:41:005241 }
5242
5243 {
5244 TestDelegate d;
5245 d.set_quit_on_redirect(true);
[email protected]f7022f32014-08-21 16:32:195246 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5247 redirect_url, DEFAULT_PRIORITY, &d, NULL));
5248 req->Start();
[email protected]e50efea2014-03-24 18:41:005249 base::RunLoop().Run();
5250
5251 EXPECT_EQ(1, d.received_redirect_count());
5252 EXPECT_EQ(0, d.response_started_count());
[email protected]f7022f32014-08-21 16:32:195253 EXPECT_TRUE(req->was_cached());
[email protected]e50efea2014-03-24 18:41:005254
[email protected]f7022f32014-08-21 16:32:195255 req->FollowDeferredRedirect();
[email protected]e50efea2014-03-24 18:41:005256 base::RunLoop().Run();
5257 EXPECT_EQ(1, d.received_redirect_count());
5258 EXPECT_EQ(1, d.response_started_count());
[email protected]f7022f32014-08-21 16:32:195259 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
5260 EXPECT_EQ(test_server_.GetURL("echo"), req->url());
[email protected]e50efea2014-03-24 18:41:005261 }
5262}
5263
5264// Make sure a request isn't cached when a NetworkDelegate forces a redirect
5265// when the headers are read, since the body won't have been read.
5266TEST_F(URLRequestTestHTTP, NoCacheOnNetworkDelegateRedirect) {
5267 ASSERT_TRUE(test_server_.Start());
5268 // URL that is normally cached.
5269 GURL initial_url = test_server_.GetURL("cachetime");
5270
5271 {
5272 // Set up the TestNetworkDelegate tp force a redirect.
5273 GURL redirect_to_url = test_server_.GetURL("echo");
5274 default_network_delegate_.set_redirect_on_headers_received_url(
5275 redirect_to_url);
5276
5277 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195278 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5279 initial_url, DEFAULT_PRIORITY, &d, NULL));
5280 req->Start();
[email protected]e50efea2014-03-24 18:41:005281 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:195282 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
[email protected]e50efea2014-03-24 18:41:005283 EXPECT_EQ(1, d.received_redirect_count());
[email protected]f7022f32014-08-21 16:32:195284 EXPECT_EQ(redirect_to_url, req->url());
[email protected]e50efea2014-03-24 18:41:005285 }
5286
5287 {
5288 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195289 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5290 initial_url, DEFAULT_PRIORITY, &d, NULL));
5291 req->Start();
[email protected]e50efea2014-03-24 18:41:005292 base::RunLoop().Run();
5293
[email protected]f7022f32014-08-21 16:32:195294 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
5295 EXPECT_FALSE(req->was_cached());
[email protected]e50efea2014-03-24 18:41:005296 EXPECT_EQ(0, d.received_redirect_count());
[email protected]f7022f32014-08-21 16:32:195297 EXPECT_EQ(initial_url, req->url());
[email protected]e50efea2014-03-24 18:41:005298 }
5299}
5300
[email protected]5f714132014-03-26 10:41:165301// Tests that redirection to an unsafe URL is allowed when it has been marked as
5302// safe.
5303TEST_F(URLRequestTestHTTP, UnsafeRedirectToWhitelistedUnsafeURL) {
5304 ASSERT_TRUE(test_server_.Start());
5305
5306 GURL unsafe_url("data:text/html,this-is-considered-an-unsafe-url");
5307 default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url);
5308 default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url);
5309
5310 TestDelegate d;
5311 {
[email protected]f7022f32014-08-21 16:32:195312 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
5313 test_server_.GetURL("whatever"), DEFAULT_PRIORITY, &d, NULL));
[email protected]5f714132014-03-26 10:41:165314
[email protected]f7022f32014-08-21 16:32:195315 r->Start();
[email protected]5f714132014-03-26 10:41:165316 base::RunLoop().Run();
5317
[email protected]f7022f32014-08-21 16:32:195318 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
[email protected]5f714132014-03-26 10:41:165319
[email protected]f7022f32014-08-21 16:32:195320 EXPECT_EQ(2U, r->url_chain().size());
5321 EXPECT_EQ(OK, r->status().error());
5322 EXPECT_EQ(unsafe_url, r->url());
[email protected]5f714132014-03-26 10:41:165323 EXPECT_EQ("this-is-considered-an-unsafe-url", d.data_received());
5324 }
5325}
5326
5327// Tests that a redirect to a different unsafe URL is blocked, even after adding
5328// some other URL to the whitelist.
5329TEST_F(URLRequestTestHTTP, UnsafeRedirectToDifferentUnsafeURL) {
5330 ASSERT_TRUE(test_server_.Start());
5331
5332 GURL unsafe_url("data:text/html,something");
5333 GURL different_unsafe_url("data:text/html,something-else");
5334 default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url);
5335 default_network_delegate_.set_allowed_unsafe_redirect_url(
5336 different_unsafe_url);
5337
5338 TestDelegate d;
5339 {
[email protected]f7022f32014-08-21 16:32:195340 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
5341 test_server_.GetURL("whatever"), DEFAULT_PRIORITY, &d, NULL));
[email protected]5f714132014-03-26 10:41:165342
[email protected]f7022f32014-08-21 16:32:195343 r->Start();
[email protected]5f714132014-03-26 10:41:165344 base::RunLoop().Run();
5345
[email protected]f7022f32014-08-21 16:32:195346 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
5347 EXPECT_EQ(ERR_UNSAFE_REDIRECT, r->status().error());
[email protected]5f714132014-03-26 10:41:165348 }
5349}
5350
[email protected]5f714132014-03-26 10:41:165351// Redirects from an URL with fragment to an unsafe URL with fragment should
5352// be allowed, and the reference fragment of the target URL should be preserved.
5353TEST_F(URLRequestTestHTTP, UnsafeRedirectWithDifferentReferenceFragment) {
5354 ASSERT_TRUE(test_server_.Start());
5355
5356 GURL original_url(test_server_.GetURL("original#fragment1"));
5357 GURL unsafe_url("data:,url-marked-safe-and-used-in-redirect#fragment2");
5358 GURL expected_url("data:,url-marked-safe-and-used-in-redirect#fragment2");
5359
5360 default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url);
5361 default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url);
5362
5363 TestDelegate d;
5364 {
[email protected]f7022f32014-08-21 16:32:195365 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
5366 original_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]5f714132014-03-26 10:41:165367
[email protected]f7022f32014-08-21 16:32:195368 r->Start();
[email protected]5f714132014-03-26 10:41:165369 base::RunLoop().Run();
5370
[email protected]f7022f32014-08-21 16:32:195371 EXPECT_EQ(2U, r->url_chain().size());
5372 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
5373 EXPECT_EQ(OK, r->status().error());
5374 EXPECT_EQ(original_url, r->original_url());
5375 EXPECT_EQ(expected_url, r->url());
[email protected]5f714132014-03-26 10:41:165376 }
5377}
5378
5379// When a delegate has specified a safe redirect URL, but it does not match the
5380// redirect target, then do not prevent the reference fragment from being added.
[email protected]f878230e2014-04-03 15:36:145381TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragmentAndUnrelatedUnsafeUrl) {
[email protected]5f714132014-03-26 10:41:165382 ASSERT_TRUE(test_server_.Start());
5383
5384 GURL original_url(test_server_.GetURL("original#expected-fragment"));
5385 GURL unsafe_url("data:text/html,this-url-does-not-match-redirect-url");
5386 GURL redirect_url(test_server_.GetURL("target"));
5387 GURL expected_redirect_url(test_server_.GetURL("target#expected-fragment"));
5388
5389 default_network_delegate_.set_redirect_on_headers_received_url(redirect_url);
5390 default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url);
5391
5392 TestDelegate d;
5393 {
[email protected]f7022f32014-08-21 16:32:195394 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
5395 original_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]5f714132014-03-26 10:41:165396
[email protected]f7022f32014-08-21 16:32:195397 r->Start();
[email protected]5f714132014-03-26 10:41:165398 base::RunLoop().Run();
5399
[email protected]f7022f32014-08-21 16:32:195400 EXPECT_EQ(2U, r->url_chain().size());
5401 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
5402 EXPECT_EQ(OK, r->status().error());
5403 EXPECT_EQ(original_url, r->original_url());
5404 EXPECT_EQ(expected_redirect_url, r->url());
[email protected]5f714132014-03-26 10:41:165405 }
5406}
5407
[email protected]f878230e2014-04-03 15:36:145408// When a delegate has specified a safe redirect URL, assume that the redirect
5409// URL should not be changed. In particular, the reference fragment should not
5410// be modified.
5411TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragment) {
5412 ASSERT_TRUE(test_server_.Start());
5413
5414 GURL original_url(test_server_.GetURL("original#should-not-be-appended"));
5415 GURL redirect_url("data:text/html,expect-no-reference-fragment");
5416
5417 default_network_delegate_.set_redirect_on_headers_received_url(redirect_url);
5418 default_network_delegate_.set_allowed_unsafe_redirect_url(redirect_url);
5419
5420 TestDelegate d;
5421 {
[email protected]f7022f32014-08-21 16:32:195422 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
5423 original_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]f878230e2014-04-03 15:36:145424
[email protected]f7022f32014-08-21 16:32:195425 r->Start();
[email protected]f878230e2014-04-03 15:36:145426 base::RunLoop().Run();
5427
[email protected]f7022f32014-08-21 16:32:195428 EXPECT_EQ(2U, r->url_chain().size());
5429 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
5430 EXPECT_EQ(OK, r->status().error());
5431 EXPECT_EQ(original_url, r->original_url());
5432 EXPECT_EQ(redirect_url, r->url());
[email protected]f878230e2014-04-03 15:36:145433 }
5434}
5435
5436// When a URLRequestRedirectJob is created, the redirection must be followed and
5437// the reference fragment of the target URL must not be modified.
5438TEST_F(URLRequestTestHTTP, RedirectJobWithReferenceFragment) {
5439 ASSERT_TRUE(test_server_.Start());
5440
5441 GURL original_url(test_server_.GetURL("original#should-not-be-appended"));
5442 GURL redirect_url(test_server_.GetURL("echo"));
5443
5444 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195445 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
5446 original_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]f878230e2014-04-03 15:36:145447
5448 URLRequestRedirectJob* job = new URLRequestRedirectJob(
[email protected]f7022f32014-08-21 16:32:195449 r.get(), &default_network_delegate_, redirect_url,
[email protected]f878230e2014-04-03 15:36:145450 URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason");
5451 AddTestInterceptor()->set_main_intercept_job(job);
5452
[email protected]f7022f32014-08-21 16:32:195453 r->Start();
[email protected]f878230e2014-04-03 15:36:145454 base::RunLoop().Run();
5455
[email protected]f7022f32014-08-21 16:32:195456 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
5457 EXPECT_EQ(OK, r->status().error());
5458 EXPECT_EQ(original_url, r->original_url());
5459 EXPECT_EQ(redirect_url, r->url());
[email protected]f878230e2014-04-03 15:36:145460}
5461
[email protected]316c1e5e2012-09-12 15:17:445462TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) {
5463 ASSERT_TRUE(test_server_.Start());
5464
5465 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195466 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5467 test_server_.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d, NULL));
5468 req->SetReferrer("http://user:[email protected]/");
5469 req->Start();
[email protected]255620da2013-08-19 13:14:295470 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445471
5472 EXPECT_EQ(std::string("http://foo.com/"), d.data_received());
5473}
5474
[email protected]99ecf6e2013-04-10 22:46:135475TEST_F(URLRequestTestHTTP, NoFragmentInReferrer) {
5476 ASSERT_TRUE(test_server_.Start());
5477
5478 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195479 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5480 test_server_.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d, NULL));
5481 req->SetReferrer("http://foo.com/test#fragment");
5482 req->Start();
[email protected]255620da2013-08-19 13:14:295483 base::RunLoop().Run();
[email protected]99ecf6e2013-04-10 22:46:135484
5485 EXPECT_EQ(std::string("http://foo.com/test"), d.data_received());
5486}
5487
5488TEST_F(URLRequestTestHTTP, EmptyReferrerAfterValidReferrer) {
5489 ASSERT_TRUE(test_server_.Start());
5490
5491 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195492 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5493 test_server_.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d, NULL));
5494 req->SetReferrer("http://foo.com/test#fragment");
5495 req->SetReferrer("");
5496 req->Start();
[email protected]255620da2013-08-19 13:14:295497 base::RunLoop().Run();
[email protected]99ecf6e2013-04-10 22:46:135498
5499 EXPECT_EQ(std::string("None"), d.data_received());
5500}
5501
[email protected]1826a402014-01-08 15:40:485502// Defer network start and then resume, checking that the request was a success
5503// and bytes were received.
5504TEST_F(URLRequestTestHTTP, DeferredBeforeNetworkStart) {
5505 ASSERT_TRUE(test_server_.Start());
5506
5507 TestDelegate d;
5508 {
5509 d.set_quit_on_network_start(true);
5510 GURL test_url(test_server_.GetURL("echo"));
[email protected]f7022f32014-08-21 16:32:195511 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5512 test_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]1826a402014-01-08 15:40:485513
[email protected]f7022f32014-08-21 16:32:195514 req->Start();
[email protected]1826a402014-01-08 15:40:485515 base::RunLoop().Run();
5516
5517 EXPECT_EQ(1, d.received_before_network_start_count());
5518 EXPECT_EQ(0, d.response_started_count());
5519
[email protected]f7022f32014-08-21 16:32:195520 req->ResumeNetworkStart();
[email protected]1826a402014-01-08 15:40:485521 base::RunLoop().Run();
5522
5523 EXPECT_EQ(1, d.response_started_count());
5524 EXPECT_NE(0, d.bytes_received());
[email protected]f7022f32014-08-21 16:32:195525 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
[email protected]1826a402014-01-08 15:40:485526 }
5527}
5528
5529// Check that OnBeforeNetworkStart is only called once even if there is a
5530// redirect.
5531TEST_F(URLRequestTestHTTP, BeforeNetworkStartCalledOnce) {
5532 ASSERT_TRUE(test_server_.Start());
5533
5534 TestDelegate d;
5535 {
5536 d.set_quit_on_redirect(true);
5537 d.set_quit_on_network_start(true);
[email protected]f7022f32014-08-21 16:32:195538 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5539 test_server_.GetURL("server-redirect?echo"), DEFAULT_PRIORITY, &d,
5540 NULL));
[email protected]1826a402014-01-08 15:40:485541
[email protected]f7022f32014-08-21 16:32:195542 req->Start();
[email protected]1826a402014-01-08 15:40:485543 base::RunLoop().Run();
5544
5545 EXPECT_EQ(1, d.received_before_network_start_count());
5546 EXPECT_EQ(0, d.response_started_count());
5547 EXPECT_EQ(0, d.received_redirect_count());
5548
[email protected]f7022f32014-08-21 16:32:195549 req->ResumeNetworkStart();
[email protected]1826a402014-01-08 15:40:485550 base::RunLoop().Run();
5551
5552 EXPECT_EQ(1, d.received_redirect_count());
[email protected]f7022f32014-08-21 16:32:195553 req->FollowDeferredRedirect();
[email protected]1826a402014-01-08 15:40:485554 base::RunLoop().Run();
5555
5556 // Check that the redirect's new network transaction does not get propagated
5557 // to a second OnBeforeNetworkStart() notification.
5558 EXPECT_EQ(1, d.received_before_network_start_count());
5559
5560 EXPECT_EQ(1, d.response_started_count());
5561 EXPECT_NE(0, d.bytes_received());
[email protected]f7022f32014-08-21 16:32:195562 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
[email protected]1826a402014-01-08 15:40:485563 }
5564}
5565
5566// Cancel the request after learning that the request would use the network.
5567TEST_F(URLRequestTestHTTP, CancelOnBeforeNetworkStart) {
5568 ASSERT_TRUE(test_server_.Start());
5569
5570 TestDelegate d;
5571 {
5572 d.set_quit_on_network_start(true);
5573 GURL test_url(test_server_.GetURL("echo"));
[email protected]f7022f32014-08-21 16:32:195574 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5575 test_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]1826a402014-01-08 15:40:485576
[email protected]f7022f32014-08-21 16:32:195577 req->Start();
[email protected]1826a402014-01-08 15:40:485578 base::RunLoop().Run();
5579
5580 EXPECT_EQ(1, d.received_before_network_start_count());
5581 EXPECT_EQ(0, d.response_started_count());
5582
[email protected]f7022f32014-08-21 16:32:195583 req->Cancel();
[email protected]1826a402014-01-08 15:40:485584 base::RunLoop().Run();
5585
5586 EXPECT_EQ(1, d.response_started_count());
5587 EXPECT_EQ(0, d.bytes_received());
[email protected]f7022f32014-08-21 16:32:195588 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status());
[email protected]1826a402014-01-08 15:40:485589 }
5590}
5591
[email protected]316c1e5e2012-09-12 15:17:445592TEST_F(URLRequestTestHTTP, CancelRedirect) {
5593 ASSERT_TRUE(test_server_.Start());
5594
5595 TestDelegate d;
5596 {
5597 d.set_cancel_in_received_redirect(true);
[email protected]f7022f32014-08-21 16:32:195598 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5599 test_server_.GetURL("files/redirect-test.html"), DEFAULT_PRIORITY, &d,
5600 NULL));
5601 req->Start();
[email protected]255620da2013-08-19 13:14:295602 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445603
5604 EXPECT_EQ(1, d.response_started_count());
5605 EXPECT_EQ(0, d.bytes_received());
5606 EXPECT_FALSE(d.received_data_before_response());
[email protected]f7022f32014-08-21 16:32:195607 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status());
[email protected]316c1e5e2012-09-12 15:17:445608 }
5609}
5610
5611TEST_F(URLRequestTestHTTP, DeferredRedirect) {
5612 ASSERT_TRUE(test_server_.Start());
5613
5614 TestDelegate d;
5615 {
5616 d.set_quit_on_redirect(true);
[email protected]79e1fd62013-06-20 06:50:045617 GURL test_url(test_server_.GetURL("files/redirect-test.html"));
[email protected]f7022f32014-08-21 16:32:195618 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5619 test_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]79e1fd62013-06-20 06:50:045620
[email protected]f7022f32014-08-21 16:32:195621 req->Start();
[email protected]255620da2013-08-19 13:14:295622 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445623
5624 EXPECT_EQ(1, d.received_redirect_count());
5625
[email protected]f7022f32014-08-21 16:32:195626 req->FollowDeferredRedirect();
[email protected]255620da2013-08-19 13:14:295627 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445628
5629 EXPECT_EQ(1, d.response_started_count());
5630 EXPECT_FALSE(d.received_data_before_response());
[email protected]f7022f32014-08-21 16:32:195631 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
[email protected]316c1e5e2012-09-12 15:17:445632
[email protected]6cdfd7f2013-02-08 20:40:155633 base::FilePath path;
[email protected]316c1e5e2012-09-12 15:17:445634 PathService::Get(base::DIR_SOURCE_ROOT, &path);
5635 path = path.Append(FILE_PATH_LITERAL("net"));
5636 path = path.Append(FILE_PATH_LITERAL("data"));
5637 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
5638 path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
5639
5640 std::string contents;
[email protected]82f84b92013-08-30 18:23:505641 EXPECT_TRUE(base::ReadFileToString(path, &contents));
[email protected]316c1e5e2012-09-12 15:17:445642 EXPECT_EQ(contents, d.data_received());
5643 }
5644}
5645
[email protected]79e1fd62013-06-20 06:50:045646TEST_F(URLRequestTestHTTP, DeferredRedirect_GetFullRequestHeaders) {
5647 ASSERT_TRUE(test_server_.Start());
5648
5649 TestDelegate d;
5650 {
5651 d.set_quit_on_redirect(true);
5652 GURL test_url(test_server_.GetURL("files/redirect-test.html"));
[email protected]f7022f32014-08-21 16:32:195653 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5654 test_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]79e1fd62013-06-20 06:50:045655
5656 EXPECT_FALSE(d.have_full_request_headers());
5657
[email protected]f7022f32014-08-21 16:32:195658 req->Start();
[email protected]255620da2013-08-19 13:14:295659 base::RunLoop().Run();
[email protected]79e1fd62013-06-20 06:50:045660
5661 EXPECT_EQ(1, d.received_redirect_count());
5662 EXPECT_TRUE(d.have_full_request_headers());
5663 CheckFullRequestHeaders(d.full_request_headers(), test_url);
5664 d.ClearFullRequestHeaders();
5665
[email protected]f7022f32014-08-21 16:32:195666 req->FollowDeferredRedirect();
[email protected]255620da2013-08-19 13:14:295667 base::RunLoop().Run();
[email protected]79e1fd62013-06-20 06:50:045668
5669 GURL target_url(test_server_.GetURL("files/with-headers.html"));
5670 EXPECT_EQ(1, d.response_started_count());
5671 EXPECT_TRUE(d.have_full_request_headers());
5672 CheckFullRequestHeaders(d.full_request_headers(), target_url);
5673 EXPECT_FALSE(d.received_data_before_response());
[email protected]f7022f32014-08-21 16:32:195674 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
[email protected]79e1fd62013-06-20 06:50:045675
5676 base::FilePath path;
5677 PathService::Get(base::DIR_SOURCE_ROOT, &path);
5678 path = path.Append(FILE_PATH_LITERAL("net"));
5679 path = path.Append(FILE_PATH_LITERAL("data"));
5680 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
5681 path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
5682
5683 std::string contents;
[email protected]82f84b92013-08-30 18:23:505684 EXPECT_TRUE(base::ReadFileToString(path, &contents));
[email protected]79e1fd62013-06-20 06:50:045685 EXPECT_EQ(contents, d.data_received());
5686 }
5687}
5688
[email protected]316c1e5e2012-09-12 15:17:445689TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) {
5690 ASSERT_TRUE(test_server_.Start());
5691
5692 TestDelegate d;
5693 {
5694 d.set_quit_on_redirect(true);
[email protected]f7022f32014-08-21 16:32:195695 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5696 test_server_.GetURL("files/redirect-test.html"), DEFAULT_PRIORITY, &d,
5697 NULL));
5698 req->Start();
[email protected]255620da2013-08-19 13:14:295699 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445700
5701 EXPECT_EQ(1, d.received_redirect_count());
5702
[email protected]f7022f32014-08-21 16:32:195703 req->Cancel();
[email protected]255620da2013-08-19 13:14:295704 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445705
5706 EXPECT_EQ(1, d.response_started_count());
5707 EXPECT_EQ(0, d.bytes_received());
5708 EXPECT_FALSE(d.received_data_before_response());
[email protected]f7022f32014-08-21 16:32:195709 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status());
[email protected]316c1e5e2012-09-12 15:17:445710 }
5711}
5712
5713TEST_F(URLRequestTestHTTP, VaryHeader) {
5714 ASSERT_TRUE(test_server_.Start());
5715
[email protected]3b23a222013-05-15 21:33:255716 // Populate the cache.
[email protected]316c1e5e2012-09-12 15:17:445717 {
5718 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195719 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5720 test_server_.GetURL("echoheadercache?foo"), DEFAULT_PRIORITY, &d,
5721 NULL));
[email protected]316c1e5e2012-09-12 15:17:445722 HttpRequestHeaders headers;
5723 headers.SetHeader("foo", "1");
[email protected]f7022f32014-08-21 16:32:195724 req->SetExtraRequestHeaders(headers);
5725 req->Start();
[email protected]255620da2013-08-19 13:14:295726 base::RunLoop().Run();
[email protected]3b23a222013-05-15 21:33:255727
5728 LoadTimingInfo load_timing_info;
[email protected]f7022f32014-08-21 16:32:195729 req->GetLoadTimingInfo(&load_timing_info);
[email protected]3b23a222013-05-15 21:33:255730 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
[email protected]316c1e5e2012-09-12 15:17:445731 }
5732
[email protected]3b23a222013-05-15 21:33:255733 // Expect a cache hit.
[email protected]316c1e5e2012-09-12 15:17:445734 {
5735 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195736 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5737 test_server_.GetURL("echoheadercache?foo"), DEFAULT_PRIORITY, &d,
5738 NULL));
[email protected]316c1e5e2012-09-12 15:17:445739 HttpRequestHeaders headers;
5740 headers.SetHeader("foo", "1");
[email protected]f7022f32014-08-21 16:32:195741 req->SetExtraRequestHeaders(headers);
5742 req->Start();
[email protected]255620da2013-08-19 13:14:295743 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445744
[email protected]f7022f32014-08-21 16:32:195745 EXPECT_TRUE(req->was_cached());
[email protected]3b23a222013-05-15 21:33:255746
5747 LoadTimingInfo load_timing_info;
[email protected]f7022f32014-08-21 16:32:195748 req->GetLoadTimingInfo(&load_timing_info);
[email protected]3b23a222013-05-15 21:33:255749 TestLoadTimingCacheHitNoNetwork(load_timing_info);
[email protected]316c1e5e2012-09-12 15:17:445750 }
5751
[email protected]3b23a222013-05-15 21:33:255752 // Expect a cache miss.
[email protected]316c1e5e2012-09-12 15:17:445753 {
5754 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195755 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5756 test_server_.GetURL("echoheadercache?foo"), DEFAULT_PRIORITY, &d,
5757 NULL));
[email protected]316c1e5e2012-09-12 15:17:445758 HttpRequestHeaders headers;
5759 headers.SetHeader("foo", "2");
[email protected]f7022f32014-08-21 16:32:195760 req->SetExtraRequestHeaders(headers);
5761 req->Start();
[email protected]255620da2013-08-19 13:14:295762 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445763
[email protected]f7022f32014-08-21 16:32:195764 EXPECT_FALSE(req->was_cached());
[email protected]3b23a222013-05-15 21:33:255765
5766 LoadTimingInfo load_timing_info;
[email protected]f7022f32014-08-21 16:32:195767 req->GetLoadTimingInfo(&load_timing_info);
[email protected]3b23a222013-05-15 21:33:255768 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
[email protected]316c1e5e2012-09-12 15:17:445769 }
5770}
5771
5772TEST_F(URLRequestTestHTTP, BasicAuth) {
5773 ASSERT_TRUE(test_server_.Start());
5774
5775 // populate the cache
5776 {
5777 TestDelegate d;
5778 d.set_credentials(AuthCredentials(kUser, kSecret));
5779
[email protected]f7022f32014-08-21 16:32:195780 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
5781 test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d, NULL));
5782 r->Start();
[email protected]316c1e5e2012-09-12 15:17:445783
[email protected]255620da2013-08-19 13:14:295784 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445785
5786 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
5787 }
5788
5789 // repeat request with end-to-end validation. since auth-basic results in a
5790 // cachable page, we expect this test to result in a 304. in which case, the
5791 // response should be fetched from the cache.
5792 {
5793 TestDelegate d;
5794 d.set_credentials(AuthCredentials(kUser, kSecret));
5795
[email protected]f7022f32014-08-21 16:32:195796 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
5797 test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d, NULL));
5798 r->SetLoadFlags(LOAD_VALIDATE_CACHE);
5799 r->Start();
[email protected]316c1e5e2012-09-12 15:17:445800
[email protected]255620da2013-08-19 13:14:295801 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445802
5803 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
5804
5805 // Should be the same cached document.
[email protected]f7022f32014-08-21 16:32:195806 EXPECT_TRUE(r->was_cached());
[email protected]316c1e5e2012-09-12 15:17:445807 }
5808}
5809
5810// Check that Set-Cookie headers in 401 responses are respected.
5811// http://crbug.com/6450
5812TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) {
5813 ASSERT_TRUE(test_server_.Start());
5814
5815 GURL url_requiring_auth =
5816 test_server_.GetURL("auth-basic?set-cookie-if-challenged");
5817
5818 // Request a page that will give a 401 containing a Set-Cookie header.
5819 // Verify that when the transaction is restarted, it includes the new cookie.
5820 {
[email protected]ceefd7fd2012-11-29 00:36:245821 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]316c1e5e2012-09-12 15:17:445822 TestURLRequestContext context(true);
5823 context.set_network_delegate(&network_delegate);
5824 context.Init();
5825
5826 TestDelegate d;
5827 d.set_credentials(AuthCredentials(kUser, kSecret));
5828
[email protected]f7022f32014-08-21 16:32:195829 scoped_ptr<URLRequest> r(context.CreateRequest(
5830 url_requiring_auth, DEFAULT_PRIORITY, &d, NULL));
5831 r->Start();
[email protected]316c1e5e2012-09-12 15:17:445832
[email protected]255620da2013-08-19 13:14:295833 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445834
5835 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
5836
5837 // Make sure we sent the cookie in the restarted transaction.
5838 EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true")
5839 != std::string::npos);
5840 }
5841
5842 // Same test as above, except this time the restart is initiated earlier
5843 // (without user intervention since identity is embedded in the URL).
5844 {
[email protected]ceefd7fd2012-11-29 00:36:245845 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]316c1e5e2012-09-12 15:17:445846 TestURLRequestContext context(true);
5847 context.set_network_delegate(&network_delegate);
5848 context.Init();
5849
5850 TestDelegate d;
5851
5852 GURL::Replacements replacements;
5853 std::string username("user2");
5854 std::string password("secret");
5855 replacements.SetUsernameStr(username);
5856 replacements.SetPasswordStr(password);
5857 GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements);
5858
[email protected]f7022f32014-08-21 16:32:195859 scoped_ptr<URLRequest> r(context.CreateRequest(
5860 url_with_identity, DEFAULT_PRIORITY, &d, NULL));
5861 r->Start();
[email protected]316c1e5e2012-09-12 15:17:445862
[email protected]255620da2013-08-19 13:14:295863 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445864
5865 EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos);
5866
5867 // Make sure we sent the cookie in the restarted transaction.
5868 EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true")
5869 != std::string::npos);
5870 }
5871}
5872
[email protected]58e32bb2013-01-21 18:23:255873// Tests that load timing works as expected with auth and the cache.
5874TEST_F(URLRequestTestHTTP, BasicAuthLoadTiming) {
5875 ASSERT_TRUE(test_server_.Start());
5876
5877 // populate the cache
5878 {
5879 TestDelegate d;
5880 d.set_credentials(AuthCredentials(kUser, kSecret));
5881
[email protected]f7022f32014-08-21 16:32:195882 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
5883 test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d, NULL));
5884 r->Start();
[email protected]58e32bb2013-01-21 18:23:255885
[email protected]255620da2013-08-19 13:14:295886 base::RunLoop().Run();
[email protected]58e32bb2013-01-21 18:23:255887
5888 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
5889
5890 LoadTimingInfo load_timing_info_before_auth;
5891 EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeAuth(
5892 &load_timing_info_before_auth));
5893 TestLoadTimingNotReused(load_timing_info_before_auth,
5894 CONNECT_TIMING_HAS_DNS_TIMES);
5895
5896 LoadTimingInfo load_timing_info;
[email protected]f7022f32014-08-21 16:32:195897 r->GetLoadTimingInfo(&load_timing_info);
[email protected]58e32bb2013-01-21 18:23:255898 // The test server does not support keep alive sockets, so the second
5899 // request with auth should use a new socket.
5900 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
5901 EXPECT_NE(load_timing_info_before_auth.socket_log_id,
5902 load_timing_info.socket_log_id);
5903 EXPECT_LE(load_timing_info_before_auth.receive_headers_end,
5904 load_timing_info.connect_timing.connect_start);
5905 }
5906
[email protected]3b23a222013-05-15 21:33:255907 // Repeat request with end-to-end validation. Since auth-basic results in a
5908 // cachable page, we expect this test to result in a 304. In which case, the
[email protected]58e32bb2013-01-21 18:23:255909 // response should be fetched from the cache.
5910 {
5911 TestDelegate d;
5912 d.set_credentials(AuthCredentials(kUser, kSecret));
5913
[email protected]f7022f32014-08-21 16:32:195914 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
5915 test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d, NULL));
5916 r->SetLoadFlags(LOAD_VALIDATE_CACHE);
5917 r->Start();
[email protected]58e32bb2013-01-21 18:23:255918
[email protected]255620da2013-08-19 13:14:295919 base::RunLoop().Run();
[email protected]58e32bb2013-01-21 18:23:255920
5921 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
5922
5923 // Should be the same cached document.
[email protected]f7022f32014-08-21 16:32:195924 EXPECT_TRUE(r->was_cached());
[email protected]58e32bb2013-01-21 18:23:255925
[email protected]3b23a222013-05-15 21:33:255926 // Since there was a request that went over the wire, the load timing
5927 // information should include connection times.
[email protected]58e32bb2013-01-21 18:23:255928 LoadTimingInfo load_timing_info;
[email protected]f7022f32014-08-21 16:32:195929 r->GetLoadTimingInfo(&load_timing_info);
[email protected]3b23a222013-05-15 21:33:255930 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
[email protected]58e32bb2013-01-21 18:23:255931 }
5932}
5933
[email protected]316c1e5e2012-09-12 15:17:445934// In this test, we do a POST which the server will 302 redirect.
5935// The subsequent transaction should use GET, and should not send the
5936// Content-Type header.
5937// http://code.google.com/p/chromium/issues/detail?id=843
5938TEST_F(URLRequestTestHTTP, Post302RedirectGet) {
5939 ASSERT_TRUE(test_server_.Start());
5940
5941 const char kData[] = "hello world";
5942
5943 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195944 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5945 test_server_.GetURL("files/redirect-to-echoall"), DEFAULT_PRIORITY, &d,
5946 NULL));
5947 req->set_method("POST");
mmenkecbc2b712014-10-09 20:29:075948 req->set_upload(CreateSimpleUploadData(kData));
[email protected]316c1e5e2012-09-12 15:17:445949
5950 // Set headers (some of which are specific to the POST).
5951 HttpRequestHeaders headers;
5952 headers.AddHeadersFromString(
5953 "Content-Type: multipart/form-data; "
5954 "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n"
5955 "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,"
5956 "text/plain;q=0.8,image/png,*/*;q=0.5\r\n"
5957 "Accept-Language: en-US,en\r\n"
5958 "Accept-Charset: ISO-8859-1,*,utf-8\r\n"
5959 "Content-Length: 11\r\n"
5960 "Origin: http://localhost:1337/");
[email protected]f7022f32014-08-21 16:32:195961 req->SetExtraRequestHeaders(headers);
5962 req->Start();
[email protected]255620da2013-08-19 13:14:295963 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445964
5965 std::string mime_type;
[email protected]f7022f32014-08-21 16:32:195966 req->GetMimeType(&mime_type);
[email protected]316c1e5e2012-09-12 15:17:445967 EXPECT_EQ("text/html", mime_type);
5968
5969 const std::string& data = d.data_received();
5970
5971 // Check that the post-specific headers were stripped:
5972 EXPECT_FALSE(ContainsString(data, "Content-Length:"));
5973 EXPECT_FALSE(ContainsString(data, "Content-Type:"));
5974 EXPECT_FALSE(ContainsString(data, "Origin:"));
5975
5976 // These extra request headers should not have been stripped.
5977 EXPECT_TRUE(ContainsString(data, "Accept:"));
5978 EXPECT_TRUE(ContainsString(data, "Accept-Language:"));
5979 EXPECT_TRUE(ContainsString(data, "Accept-Charset:"));
5980}
5981
5982// The following tests check that we handle mutating the request method for
5983// HTTP redirects as expected.
5984// See http://crbug.com/56373 and http://crbug.com/102130.
5985
5986TEST_F(URLRequestTestHTTP, Redirect301Tests) {
5987 ASSERT_TRUE(test_server_.Start());
5988
5989 const GURL url = test_server_.GetURL("files/redirect301-to-echo");
5990
5991 HTTPRedirectMethodTest(url, "POST", "GET", true);
5992 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
5993 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
5994}
5995
5996TEST_F(URLRequestTestHTTP, Redirect302Tests) {
5997 ASSERT_TRUE(test_server_.Start());
5998
5999 const GURL url = test_server_.GetURL("files/redirect302-to-echo");
6000
6001 HTTPRedirectMethodTest(url, "POST", "GET", true);
6002 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
6003 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
6004}
6005
6006TEST_F(URLRequestTestHTTP, Redirect303Tests) {
6007 ASSERT_TRUE(test_server_.Start());
6008
6009 const GURL url = test_server_.GetURL("files/redirect303-to-echo");
6010
6011 HTTPRedirectMethodTest(url, "POST", "GET", true);
6012 HTTPRedirectMethodTest(url, "PUT", "GET", true);
6013 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
6014}
6015
6016TEST_F(URLRequestTestHTTP, Redirect307Tests) {
6017 ASSERT_TRUE(test_server_.Start());
6018
6019 const GURL url = test_server_.GetURL("files/redirect307-to-echo");
6020
6021 HTTPRedirectMethodTest(url, "POST", "POST", true);
6022 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
6023 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
6024}
6025
[email protected]0a17aab32014-04-24 03:32:376026TEST_F(URLRequestTestHTTP, Redirect308Tests) {
6027 ASSERT_TRUE(test_server_.Start());
6028
6029 const GURL url = test_server_.GetURL("files/redirect308-to-echo");
6030
6031 HTTPRedirectMethodTest(url, "POST", "POST", true);
6032 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
6033 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
6034}
6035
6036// Make sure that 308 responses without bodies are not treated as redirects.
6037// Certain legacy apis that pre-date the response code expect this behavior
6038// (Like Google Drive).
6039TEST_F(URLRequestTestHTTP, NoRedirectOn308WithoutLocationHeader) {
6040 ASSERT_TRUE(test_server_.Start());
6041
6042 TestDelegate d;
6043 const GURL url = test_server_.GetURL("files/308-without-location-header");
6044
[email protected]f7022f32014-08-21 16:32:196045 scoped_ptr<URLRequest> request(default_context_.CreateRequest(
6046 url, DEFAULT_PRIORITY, &d, NULL));
[email protected]0a17aab32014-04-24 03:32:376047
[email protected]f7022f32014-08-21 16:32:196048 request->Start();
[email protected]0a17aab32014-04-24 03:32:376049 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:196050 EXPECT_EQ(URLRequestStatus::SUCCESS, request->status().status());
6051 EXPECT_EQ(OK, request->status().error());
[email protected]0a17aab32014-04-24 03:32:376052 EXPECT_EQ(0, d.received_redirect_count());
[email protected]f7022f32014-08-21 16:32:196053 EXPECT_EQ(308, request->response_headers()->response_code());
[email protected]0a17aab32014-04-24 03:32:376054 EXPECT_EQ("This is not a redirect.", d.data_received());
6055}
6056
[email protected]f878230e2014-04-03 15:36:146057TEST_F(URLRequestTestHTTP, Redirect302PreserveReferenceFragment) {
6058 ASSERT_TRUE(test_server_.Start());
6059
6060 GURL original_url(test_server_.GetURL("files/redirect302-to-echo#fragment"));
6061 GURL expected_url(test_server_.GetURL("echo#fragment"));
6062
6063 TestDelegate d;
6064 {
[email protected]f7022f32014-08-21 16:32:196065 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
6066 original_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]f878230e2014-04-03 15:36:146067
[email protected]f7022f32014-08-21 16:32:196068 r->Start();
[email protected]f878230e2014-04-03 15:36:146069 base::RunLoop().Run();
6070
[email protected]f7022f32014-08-21 16:32:196071 EXPECT_EQ(2U, r->url_chain().size());
6072 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
6073 EXPECT_EQ(OK, r->status().error());
6074 EXPECT_EQ(original_url, r->original_url());
6075 EXPECT_EQ(expected_url, r->url());
[email protected]f878230e2014-04-03 15:36:146076 }
6077}
6078
[email protected]cba24642014-08-15 20:49:596079TEST_F(URLRequestTestHTTP, RedirectPreserveFirstPartyURL) {
6080 ASSERT_TRUE(test_server_.Start());
6081
6082 GURL url(test_server_.GetURL("files/redirect302-to-echo"));
6083 GURL first_party_url("http://example.com");
6084
6085 TestDelegate d;
6086 {
[email protected]f7022f32014-08-21 16:32:196087 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
6088 url, DEFAULT_PRIORITY, &d, NULL));
6089 r->set_first_party_for_cookies(first_party_url);
[email protected]cba24642014-08-15 20:49:596090
[email protected]f7022f32014-08-21 16:32:196091 r->Start();
[email protected]cba24642014-08-15 20:49:596092 base::RunLoop().Run();
6093
[email protected]f7022f32014-08-21 16:32:196094 EXPECT_EQ(2U, r->url_chain().size());
6095 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
6096 EXPECT_EQ(OK, r->status().error());
6097 EXPECT_EQ(first_party_url, r->first_party_for_cookies());
[email protected]cba24642014-08-15 20:49:596098 }
6099}
6100
6101TEST_F(URLRequestTestHTTP, RedirectUpdateFirstPartyURL) {
6102 ASSERT_TRUE(test_server_.Start());
6103
6104 GURL url(test_server_.GetURL("files/redirect302-to-echo"));
6105 GURL original_first_party_url("http://example.com");
6106 GURL expected_first_party_url(test_server_.GetURL("echo"));
6107
6108 TestDelegate d;
6109 {
[email protected]f7022f32014-08-21 16:32:196110 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
6111 url, DEFAULT_PRIORITY, &d, NULL));
6112 r->set_first_party_for_cookies(original_first_party_url);
6113 r->set_first_party_url_policy(
[email protected]cba24642014-08-15 20:49:596114 URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT);
6115
[email protected]f7022f32014-08-21 16:32:196116 r->Start();
[email protected]cba24642014-08-15 20:49:596117 base::RunLoop().Run();
6118
[email protected]f7022f32014-08-21 16:32:196119 EXPECT_EQ(2U, r->url_chain().size());
6120 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
6121 EXPECT_EQ(OK, r->status().error());
6122 EXPECT_EQ(expected_first_party_url, r->first_party_for_cookies());
[email protected]cba24642014-08-15 20:49:596123 }
6124}
6125
[email protected]316c1e5e2012-09-12 15:17:446126TEST_F(URLRequestTestHTTP, InterceptPost302RedirectGet) {
6127 ASSERT_TRUE(test_server_.Start());
6128
6129 const char kData[] = "hello world";
6130
6131 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196132 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
6133 test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d, NULL));
6134 req->set_method("POST");
mmenkecbc2b712014-10-09 20:29:076135 req->set_upload(CreateSimpleUploadData(kData));
[email protected]316c1e5e2012-09-12 15:17:446136 HttpRequestHeaders headers;
6137 headers.SetHeader(HttpRequestHeaders::kContentLength,
6138 base::UintToString(arraysize(kData) - 1));
[email protected]f7022f32014-08-21 16:32:196139 req->SetExtraRequestHeaders(headers);
[email protected]316c1e5e2012-09-12 15:17:446140
6141 URLRequestRedirectJob* job = new URLRequestRedirectJob(
[email protected]f7022f32014-08-21 16:32:196142 req.get(), &default_network_delegate_, test_server_.GetURL("echo"),
[email protected]7983c4a2014-03-12 01:47:096143 URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason");
[email protected]316c1e5e2012-09-12 15:17:446144 AddTestInterceptor()->set_main_intercept_job(job);
6145
[email protected]f7022f32014-08-21 16:32:196146 req->Start();
[email protected]255620da2013-08-19 13:14:296147 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:196148 EXPECT_EQ("GET", req->method());
[email protected]316c1e5e2012-09-12 15:17:446149}
6150
6151TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) {
6152 ASSERT_TRUE(test_server_.Start());
6153
6154 const char kData[] = "hello world";
6155
6156 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196157 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
6158 test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d, NULL));
6159 req->set_method("POST");
mmenkecbc2b712014-10-09 20:29:076160 req->set_upload(CreateSimpleUploadData(kData));
[email protected]316c1e5e2012-09-12 15:17:446161 HttpRequestHeaders headers;
6162 headers.SetHeader(HttpRequestHeaders::kContentLength,
6163 base::UintToString(arraysize(kData) - 1));
[email protected]f7022f32014-08-21 16:32:196164 req->SetExtraRequestHeaders(headers);
[email protected]316c1e5e2012-09-12 15:17:446165
6166 URLRequestRedirectJob* job = new URLRequestRedirectJob(
[email protected]f7022f32014-08-21 16:32:196167 req.get(), &default_network_delegate_, test_server_.GetURL("echo"),
[email protected]7983c4a2014-03-12 01:47:096168 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT,
6169 "Very Good Reason");
[email protected]316c1e5e2012-09-12 15:17:446170 AddTestInterceptor()->set_main_intercept_job(job);
6171
[email protected]f7022f32014-08-21 16:32:196172 req->Start();
[email protected]255620da2013-08-19 13:14:296173 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:196174 EXPECT_EQ("POST", req->method());
[email protected]316c1e5e2012-09-12 15:17:446175 EXPECT_EQ(kData, d.data_received());
6176}
6177
6178// Check that default A-L header is sent.
6179TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) {
6180 ASSERT_TRUE(test_server_.Start());
6181
[email protected]8790210c2013-12-02 05:29:536182 StaticHttpUserAgentSettings settings("en", std::string());
[email protected]ceefd7fd2012-11-29 00:36:246183 TestNetworkDelegate network_delegate; // Must outlive URLRequests.
[email protected]316c1e5e2012-09-12 15:17:446184 TestURLRequestContext context(true);
6185 context.set_network_delegate(&network_delegate);
[email protected]ee4c30d2012-11-07 15:08:436186 context.set_http_user_agent_settings(&settings);
[email protected]316c1e5e2012-09-12 15:17:446187 context.Init();
6188
6189 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196190 scoped_ptr<URLRequest> req(context.CreateRequest(
6191 test_server_.GetURL("echoheader?Accept-Language"), DEFAULT_PRIORITY, &d,
6192 NULL));
6193 req->Start();
[email protected]255620da2013-08-19 13:14:296194 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446195 EXPECT_EQ("en", d.data_received());
6196}
6197
6198// Check that an empty A-L header is not sent. http://crbug.com/77365.
6199TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) {
6200 ASSERT_TRUE(test_server_.Start());
6201
[email protected]8790210c2013-12-02 05:29:536202 std::string empty_string; // Avoid most vexing parse on line below.
6203 StaticHttpUserAgentSettings settings(empty_string, empty_string);
[email protected]ceefd7fd2012-11-29 00:36:246204 TestNetworkDelegate network_delegate; // Must outlive URLRequests.
[email protected]316c1e5e2012-09-12 15:17:446205 TestURLRequestContext context(true);
6206 context.set_network_delegate(&network_delegate);
6207 context.Init();
6208 // We override the language after initialization because empty entries
6209 // get overridden by Init().
[email protected]ee4c30d2012-11-07 15:08:436210 context.set_http_user_agent_settings(&settings);
[email protected]316c1e5e2012-09-12 15:17:446211
6212 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196213 scoped_ptr<URLRequest> req(context.CreateRequest(
6214 test_server_.GetURL("echoheader?Accept-Language"), DEFAULT_PRIORITY, &d,
6215 NULL));
6216 req->Start();
[email protected]255620da2013-08-19 13:14:296217 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446218 EXPECT_EQ("None", d.data_received());
6219}
6220
6221// Check that if request overrides the A-L header, the default is not appended.
6222// See http://crbug.com/20894
6223TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) {
6224 ASSERT_TRUE(test_server_.Start());
6225
6226 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196227 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
6228 test_server_.GetURL("echoheader?Accept-Language"), DEFAULT_PRIORITY, &d,
6229 NULL));
[email protected]316c1e5e2012-09-12 15:17:446230 HttpRequestHeaders headers;
6231 headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru");
[email protected]f7022f32014-08-21 16:32:196232 req->SetExtraRequestHeaders(headers);
6233 req->Start();
[email protected]255620da2013-08-19 13:14:296234 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446235 EXPECT_EQ(std::string("ru"), d.data_received());
6236}
6237
6238// Check that default A-E header is sent.
6239TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) {
6240 ASSERT_TRUE(test_server_.Start());
6241
6242 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196243 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
6244 test_server_.GetURL("echoheader?Accept-Encoding"), DEFAULT_PRIORITY, &d,
6245 NULL));
[email protected]316c1e5e2012-09-12 15:17:446246 HttpRequestHeaders headers;
[email protected]f7022f32014-08-21 16:32:196247 req->SetExtraRequestHeaders(headers);
6248 req->Start();
[email protected]255620da2013-08-19 13:14:296249 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446250 EXPECT_TRUE(ContainsString(d.data_received(), "gzip"));
6251}
6252
6253// Check that if request overrides the A-E header, the default is not appended.
6254// See http://crbug.com/47381
6255TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) {
6256 ASSERT_TRUE(test_server_.Start());
6257
6258 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196259 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
6260 test_server_.GetURL("echoheader?Accept-Encoding"), DEFAULT_PRIORITY, &d,
6261 NULL));
[email protected]316c1e5e2012-09-12 15:17:446262 HttpRequestHeaders headers;
6263 headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, "identity");
[email protected]f7022f32014-08-21 16:32:196264 req->SetExtraRequestHeaders(headers);
6265 req->Start();
[email protected]255620da2013-08-19 13:14:296266 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446267 EXPECT_FALSE(ContainsString(d.data_received(), "gzip"));
6268 EXPECT_TRUE(ContainsString(d.data_received(), "identity"));
6269}
6270
[email protected]84f05432013-03-15 01:00:126271// Check that setting the A-C header sends the proper header.
6272TEST_F(URLRequestTestHTTP, SetAcceptCharset) {
[email protected]316c1e5e2012-09-12 15:17:446273 ASSERT_TRUE(test_server_.Start());
6274
6275 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196276 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
6277 test_server_.GetURL("echoheader?Accept-Charset"), DEFAULT_PRIORITY, &d,
6278 NULL));
[email protected]316c1e5e2012-09-12 15:17:446279 HttpRequestHeaders headers;
6280 headers.SetHeader(HttpRequestHeaders::kAcceptCharset, "koi-8r");
[email protected]f7022f32014-08-21 16:32:196281 req->SetExtraRequestHeaders(headers);
6282 req->Start();
[email protected]255620da2013-08-19 13:14:296283 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446284 EXPECT_EQ(std::string("koi-8r"), d.data_received());
6285}
6286
6287// Check that default User-Agent header is sent.
6288TEST_F(URLRequestTestHTTP, DefaultUserAgent) {
6289 ASSERT_TRUE(test_server_.Start());
6290
6291 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196292 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
6293 test_server_.GetURL("echoheader?User-Agent"), DEFAULT_PRIORITY, &d,
6294 NULL));
6295 req->Start();
[email protected]255620da2013-08-19 13:14:296296 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:196297 EXPECT_EQ(default_context_.http_user_agent_settings()->GetUserAgent(),
[email protected]051a4b32014-01-09 04:02:376298 d.data_received());
[email protected]316c1e5e2012-09-12 15:17:446299}
6300
6301// Check that if request overrides the User-Agent header,
6302// the default is not appended.
6303TEST_F(URLRequestTestHTTP, OverrideUserAgent) {
6304 ASSERT_TRUE(test_server_.Start());
6305
6306 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196307 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
6308 test_server_.GetURL("echoheader?User-Agent"), DEFAULT_PRIORITY, &d,
6309 NULL));
[email protected]316c1e5e2012-09-12 15:17:446310 HttpRequestHeaders headers;
6311 headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)");
[email protected]f7022f32014-08-21 16:32:196312 req->SetExtraRequestHeaders(headers);
6313 req->Start();
[email protected]255620da2013-08-19 13:14:296314 base::RunLoop().Run();
[email protected]cd6f2522014-01-16 18:27:356315 EXPECT_EQ(std::string("Lynx (textmode)"), d.data_received());
[email protected]316c1e5e2012-09-12 15:17:446316}
6317
[email protected]ee4c30d2012-11-07 15:08:436318// Check that a NULL HttpUserAgentSettings causes the corresponding empty
6319// User-Agent header to be sent but does not send the Accept-Language and
6320// Accept-Charset headers.
6321TEST_F(URLRequestTestHTTP, EmptyHttpUserAgentSettings) {
6322 ASSERT_TRUE(test_server_.Start());
6323
[email protected]ceefd7fd2012-11-29 00:36:246324 TestNetworkDelegate network_delegate; // Must outlive URLRequests.
[email protected]ee4c30d2012-11-07 15:08:436325 TestURLRequestContext context(true);
6326 context.set_network_delegate(&network_delegate);
6327 context.Init();
6328 // We override the HttpUserAgentSettings after initialization because empty
6329 // entries get overridden by Init().
6330 context.set_http_user_agent_settings(NULL);
6331
6332 struct {
6333 const char* request;
6334 const char* expected_response;
6335 } tests[] = { { "echoheader?Accept-Language", "None" },
6336 { "echoheader?Accept-Charset", "None" },
6337 { "echoheader?User-Agent", "" } };
6338
viettrungluue4a8b882014-10-16 06:17:386339 for (size_t i = 0; i < arraysize(tests); i++) {
[email protected]ee4c30d2012-11-07 15:08:436340 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196341 scoped_ptr<URLRequest> req(context.CreateRequest(
6342 test_server_.GetURL(tests[i].request), DEFAULT_PRIORITY, &d, NULL));
6343 req->Start();
[email protected]255620da2013-08-19 13:14:296344 base::RunLoop().Run();
[email protected]ee4c30d2012-11-07 15:08:436345 EXPECT_EQ(tests[i].expected_response, d.data_received())
6346 << " Request = \"" << tests[i].request << "\"";
6347 }
6348}
6349
[email protected]5033ab82013-03-22 20:17:466350// Make sure that URLRequest passes on its priority updates to
6351// newly-created jobs after the first one.
6352TEST_F(URLRequestTestHTTP, SetSubsequentJobPriority) {
6353 ASSERT_TRUE(test_server_.Start());
6354
6355 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196356 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
6357 test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d, NULL));
6358 EXPECT_EQ(DEFAULT_PRIORITY, req->priority());
[email protected]5033ab82013-03-22 20:17:466359
6360 scoped_refptr<URLRequestRedirectJob> redirect_job =
6361 new URLRequestRedirectJob(
[email protected]f7022f32014-08-21 16:32:196362 req.get(), &default_network_delegate_, test_server_.GetURL("echo"),
[email protected]7983c4a2014-03-12 01:47:096363 URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason");
[email protected]90499482013-06-01 00:39:506364 AddTestInterceptor()->set_main_intercept_job(redirect_job.get());
[email protected]5033ab82013-03-22 20:17:466365
[email protected]f7022f32014-08-21 16:32:196366 req->SetPriority(LOW);
6367 req->Start();
6368 EXPECT_TRUE(req->is_pending());
[email protected]5033ab82013-03-22 20:17:466369
6370 scoped_refptr<URLRequestTestJob> job =
[email protected]f7022f32014-08-21 16:32:196371 new URLRequestTestJob(req.get(), &default_network_delegate_);
[email protected]90499482013-06-01 00:39:506372 AddTestInterceptor()->set_main_intercept_job(job.get());
[email protected]5033ab82013-03-22 20:17:466373
6374 // Should trigger |job| to be started.
[email protected]255620da2013-08-19 13:14:296375 base::RunLoop().Run();
[email protected]5033ab82013-03-22 20:17:466376 EXPECT_EQ(LOW, job->priority());
6377}
6378
[email protected]80abdad2014-03-15 00:20:546379// Check that creating a network request while entering/exiting suspend mode
6380// fails as it should. This is the only case where an HttpTransactionFactory
6381// does not return an HttpTransaction.
6382TEST_F(URLRequestTestHTTP, NetworkSuspendTest) {
6383 // Create a new HttpNetworkLayer that thinks it's suspended.
6384 HttpNetworkSession::Params params;
6385 params.host_resolver = default_context_.host_resolver();
6386 params.cert_verifier = default_context_.cert_verifier();
6387 params.transport_security_state = default_context_.transport_security_state();
6388 params.proxy_service = default_context_.proxy_service();
6389 params.ssl_config_service = default_context_.ssl_config_service();
6390 params.http_auth_handler_factory =
6391 default_context_.http_auth_handler_factory();
6392 params.network_delegate = &default_network_delegate_;
6393 params.http_server_properties = default_context_.http_server_properties();
6394 scoped_ptr<HttpNetworkLayer> network_layer(
6395 new HttpNetworkLayer(new HttpNetworkSession(params)));
6396 network_layer->OnSuspend();
6397
6398 HttpCache http_cache(network_layer.release(), default_context_.net_log(),
6399 HttpCache::DefaultBackend::InMemory(0));
6400
6401 TestURLRequestContext context(true);
6402 context.set_http_transaction_factory(&http_cache);
6403 context.Init();
6404
6405 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196406 scoped_ptr<URLRequest> req(context.CreateRequest(
6407 GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d, NULL));
6408 req->Start();
[email protected]80abdad2014-03-15 00:20:546409 base::RunLoop().Run();
6410
6411 EXPECT_TRUE(d.request_failed());
[email protected]f7022f32014-08-21 16:32:196412 EXPECT_EQ(URLRequestStatus::FAILED, req->status().status());
6413 EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, req->status().error());
[email protected]80abdad2014-03-15 00:20:546414}
6415
6416// Check that creating a network request while entering/exiting suspend mode
6417// fails as it should in the case there is no cache. This is the only case
6418// where an HttpTransactionFactory does not return an HttpTransaction.
6419TEST_F(URLRequestTestHTTP, NetworkSuspendTestNoCache) {
6420 // Create a new HttpNetworkLayer that thinks it's suspended.
6421 HttpNetworkSession::Params params;
6422 params.host_resolver = default_context_.host_resolver();
6423 params.cert_verifier = default_context_.cert_verifier();
6424 params.transport_security_state = default_context_.transport_security_state();
6425 params.proxy_service = default_context_.proxy_service();
6426 params.ssl_config_service = default_context_.ssl_config_service();
6427 params.http_auth_handler_factory =
6428 default_context_.http_auth_handler_factory();
6429 params.network_delegate = &default_network_delegate_;
6430 params.http_server_properties = default_context_.http_server_properties();
6431 HttpNetworkLayer network_layer(new HttpNetworkSession(params));
6432 network_layer.OnSuspend();
6433
6434 TestURLRequestContext context(true);
6435 context.set_http_transaction_factory(&network_layer);
6436 context.Init();
6437
6438 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196439 scoped_ptr<URLRequest> req(context.CreateRequest(
6440 GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d, NULL));
6441 req->Start();
[email protected]80abdad2014-03-15 00:20:546442 base::RunLoop().Run();
6443
6444 EXPECT_TRUE(d.request_failed());
[email protected]f7022f32014-08-21 16:32:196445 EXPECT_EQ(URLRequestStatus::FAILED, req->status().status());
6446 EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, req->status().error());
[email protected]80abdad2014-03-15 00:20:546447}
6448
[email protected]73e0bba2009-02-19 22:57:096449class HTTPSRequestTest : public testing::Test {
[email protected]87a09a92011-07-14 15:50:506450 public:
[email protected]ef2bf422012-05-11 03:27:096451 HTTPSRequestTest() : default_context_(true) {
6452 default_context_.set_network_delegate(&default_network_delegate_);
6453 default_context_.Init();
[email protected]87a09a92011-07-14 15:50:506454 }
6455 virtual ~HTTPSRequestTest() {}
6456
6457 protected:
[email protected]ceefd7fd2012-11-29 00:36:246458 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest.
[email protected]ef2bf422012-05-11 03:27:096459 TestURLRequestContext default_context_;
[email protected]ea224582008-12-07 20:25:466460};
6461
[email protected]c044616e2013-02-20 02:01:266462TEST_F(HTTPSRequestTest, HTTPSGetTest) {
[email protected]ce7d0cbc2013-05-03 18:57:226463 SpawnedTestServer test_server(
6464 SpawnedTestServer::TYPE_HTTPS,
6465 SpawnedTestServer::kLocalhost,
6466 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]95409e12010-08-17 20:07:116467 ASSERT_TRUE(test_server.Start());
[email protected]ea224582008-12-07 20:25:466468
[email protected]ea224582008-12-07 20:25:466469 TestDelegate d;
6470 {
[email protected]f7022f32014-08-21 16:32:196471 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
6472 test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL));
6473 r->Start();
6474 EXPECT_TRUE(r->is_pending());
[email protected]ea224582008-12-07 20:25:466475
[email protected]255620da2013-08-19 13:14:296476 base::RunLoop().Run();
[email protected]ea224582008-12-07 20:25:466477
6478 EXPECT_EQ(1, d.response_started_count());
6479 EXPECT_FALSE(d.received_data_before_response());
6480 EXPECT_NE(0, d.bytes_received());
[email protected]f7022f32014-08-21 16:32:196481 CheckSSLInfo(r->ssl_info());
[email protected]6d81b482011-02-22 19:47:196482 EXPECT_EQ(test_server.host_port_pair().host(),
[email protected]f7022f32014-08-21 16:32:196483 r->GetSocketAddress().host());
[email protected]6d81b482011-02-22 19:47:196484 EXPECT_EQ(test_server.host_port_pair().port(),
[email protected]f7022f32014-08-21 16:32:196485 r->GetSocketAddress().port());
[email protected]ea224582008-12-07 20:25:466486 }
[email protected]ea224582008-12-07 20:25:466487}
6488
[email protected]5774ada2010-07-15 06:30:546489TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) {
[email protected]ce7d0cbc2013-05-03 18:57:226490 SpawnedTestServer::SSLOptions ssl_options(
6491 SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME);
6492 SpawnedTestServer test_server(
6493 SpawnedTestServer::TYPE_HTTPS,
6494 ssl_options,
6495 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]95409e12010-08-17 20:07:116496 ASSERT_TRUE(test_server.Start());
[email protected]bacff652009-03-31 17:50:336497
6498 bool err_allowed = true;
6499 for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) {
6500 TestDelegate d;
6501 {
6502 d.set_allow_certificate_errors(err_allowed);
[email protected]f7022f32014-08-21 16:32:196503 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
6504 test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL));
[email protected]bacff652009-03-31 17:50:336505
[email protected]f7022f32014-08-21 16:32:196506 r->Start();
6507 EXPECT_TRUE(r->is_pending());
[email protected]bacff652009-03-31 17:50:336508
[email protected]255620da2013-08-19 13:14:296509 base::RunLoop().Run();
[email protected]bacff652009-03-31 17:50:336510
6511 EXPECT_EQ(1, d.response_started_count());
6512 EXPECT_FALSE(d.received_data_before_response());
6513 EXPECT_TRUE(d.have_certificate_errors());
[email protected]96adadb2010-08-28 01:16:176514 if (err_allowed) {
[email protected]bacff652009-03-31 17:50:336515 EXPECT_NE(0, d.bytes_received());
[email protected]f7022f32014-08-21 16:32:196516 CheckSSLInfo(r->ssl_info());
[email protected]96adadb2010-08-28 01:16:176517 } else {
[email protected]bacff652009-03-31 17:50:336518 EXPECT_EQ(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:176519 }
[email protected]bacff652009-03-31 17:50:336520 }
6521 }
6522}
6523
[email protected]5774ada2010-07-15 06:30:546524TEST_F(HTTPSRequestTest, HTTPSExpiredTest) {
[email protected]ce7d0cbc2013-05-03 18:57:226525 SpawnedTestServer::SSLOptions ssl_options(
6526 SpawnedTestServer::SSLOptions::CERT_EXPIRED);
6527 SpawnedTestServer test_server(
6528 SpawnedTestServer::TYPE_HTTPS,
6529 ssl_options,
6530 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]95409e12010-08-17 20:07:116531 ASSERT_TRUE(test_server.Start());
[email protected]bacff652009-03-31 17:50:336532
6533 // Iterate from false to true, just so that we do the opposite of the
6534 // previous test in order to increase test coverage.
6535 bool err_allowed = false;
6536 for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) {
6537 TestDelegate d;
6538 {
6539 d.set_allow_certificate_errors(err_allowed);
[email protected]f7022f32014-08-21 16:32:196540 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
6541 test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL));
[email protected]bacff652009-03-31 17:50:336542
[email protected]f7022f32014-08-21 16:32:196543 r->Start();
6544 EXPECT_TRUE(r->is_pending());
[email protected]bacff652009-03-31 17:50:336545
[email protected]255620da2013-08-19 13:14:296546 base::RunLoop().Run();
[email protected]bacff652009-03-31 17:50:336547
6548 EXPECT_EQ(1, d.response_started_count());
6549 EXPECT_FALSE(d.received_data_before_response());
6550 EXPECT_TRUE(d.have_certificate_errors());
[email protected]96adadb2010-08-28 01:16:176551 if (err_allowed) {
[email protected]bacff652009-03-31 17:50:336552 EXPECT_NE(0, d.bytes_received());
[email protected]f7022f32014-08-21 16:32:196553 CheckSSLInfo(r->ssl_info());
[email protected]96adadb2010-08-28 01:16:176554 } else {
[email protected]bacff652009-03-31 17:50:336555 EXPECT_EQ(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:176556 }
[email protected]bacff652009-03-31 17:50:336557 }
6558 }
6559}
[email protected]73e0bba2009-02-19 22:57:096560
[email protected]316c1e5e2012-09-12 15:17:446561// This tests that a load of www.google.com with a certificate error sets
6562// the |certificate_errors_are_fatal| flag correctly. This flag will cause
6563// the interstitial to be fatal.
6564TEST_F(HTTPSRequestTest, HTTPSPreloadedHSTSTest) {
[email protected]ce7d0cbc2013-05-03 18:57:226565 SpawnedTestServer::SSLOptions ssl_options(
6566 SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME);
6567 SpawnedTestServer test_server(
6568 SpawnedTestServer::TYPE_HTTPS,
6569 ssl_options,
6570 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:446571 ASSERT_TRUE(test_server.Start());
6572
6573 // We require that the URL be www.google.com in order to pick up the
6574 // preloaded HSTS entries in the TransportSecurityState. This means that we
6575 // have to use a MockHostResolver in order to direct www.google.com to the
[email protected]ceefd7fd2012-11-29 00:36:246576 // testserver. By default, MockHostResolver maps all hosts to 127.0.0.1.
[email protected]316c1e5e2012-09-12 15:17:446577
6578 MockHostResolver host_resolver;
[email protected]ceefd7fd2012-11-29 00:36:246579 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]316c1e5e2012-09-12 15:17:446580 TestURLRequestContext context(true);
6581 context.set_network_delegate(&network_delegate);
6582 context.set_host_resolver(&host_resolver);
6583 TransportSecurityState transport_security_state;
6584 context.set_transport_security_state(&transport_security_state);
6585 context.Init();
6586
6587 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196588 scoped_ptr<URLRequest> r(context.CreateRequest(
6589 GURL(base::StringPrintf("https://www.google.com:%d",
6590 test_server.host_port_pair().port())),
6591 DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:446592
[email protected]f7022f32014-08-21 16:32:196593 r->Start();
6594 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:446595
[email protected]255620da2013-08-19 13:14:296596 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446597
6598 EXPECT_EQ(1, d.response_started_count());
6599 EXPECT_FALSE(d.received_data_before_response());
6600 EXPECT_TRUE(d.have_certificate_errors());
6601 EXPECT_TRUE(d.certificate_errors_are_fatal());
6602}
6603
6604// This tests that cached HTTPS page loads do not cause any updates to the
6605// TransportSecurityState.
6606TEST_F(HTTPSRequestTest, HTTPSErrorsNoClobberTSSTest) {
6607 // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't
6608 // matter. It just has to be any error.
[email protected]ce7d0cbc2013-05-03 18:57:226609 SpawnedTestServer::SSLOptions ssl_options(
6610 SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME);
6611 SpawnedTestServer test_server(
6612 SpawnedTestServer::TYPE_HTTPS,
6613 ssl_options,
6614 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:446615 ASSERT_TRUE(test_server.Start());
6616
[email protected]9e6968d2014-05-07 21:46:266617 // We require that the URL be www.google.com in order to pick up the static
6618 // and dynamic STS and PKP entries in the TransportSecurityState. This means
6619 // that we have to use a MockHostResolver in order to direct www.google.com to
6620 // the testserver. By default, MockHostResolver maps all hosts to 127.0.0.1.
[email protected]316c1e5e2012-09-12 15:17:446621
6622 MockHostResolver host_resolver;
[email protected]ceefd7fd2012-11-29 00:36:246623 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]316c1e5e2012-09-12 15:17:446624 TestURLRequestContext context(true);
6625 context.set_network_delegate(&network_delegate);
6626 context.set_host_resolver(&host_resolver);
6627 TransportSecurityState transport_security_state;
[email protected]9e6968d2014-05-07 21:46:266628
6629 TransportSecurityState::DomainState static_domain_state;
6630 EXPECT_TRUE(transport_security_state.GetStaticDomainState(
Adam Langley5cbb7d7a2014-09-25 23:14:126631 "www.google.com", &static_domain_state));
[email protected]316c1e5e2012-09-12 15:17:446632 context.set_transport_security_state(&transport_security_state);
6633 context.Init();
6634
[email protected]9e6968d2014-05-07 21:46:266635 TransportSecurityState::DomainState dynamic_domain_state;
6636 EXPECT_FALSE(transport_security_state.GetDynamicDomainState(
6637 "www.google.com", &dynamic_domain_state));
6638
[email protected]316c1e5e2012-09-12 15:17:446639 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196640 scoped_ptr<URLRequest> r(context.CreateRequest(
6641 GURL(base::StringPrintf("https://www.google.com:%d",
6642 test_server.host_port_pair().port())),
6643 DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:446644
[email protected]f7022f32014-08-21 16:32:196645 r->Start();
6646 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:446647
[email protected]255620da2013-08-19 13:14:296648 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446649
6650 EXPECT_EQ(1, d.response_started_count());
6651 EXPECT_FALSE(d.received_data_before_response());
6652 EXPECT_TRUE(d.have_certificate_errors());
6653 EXPECT_TRUE(d.certificate_errors_are_fatal());
6654
[email protected]9e6968d2014-05-07 21:46:266655 // Get a fresh copy of the states, and check that they haven't changed.
6656 TransportSecurityState::DomainState new_static_domain_state;
6657 EXPECT_TRUE(transport_security_state.GetStaticDomainState(
Adam Langley5cbb7d7a2014-09-25 23:14:126658 "www.google.com", &new_static_domain_state));
[email protected]9e6968d2014-05-07 21:46:266659 TransportSecurityState::DomainState new_dynamic_domain_state;
6660 EXPECT_FALSE(transport_security_state.GetDynamicDomainState(
6661 "www.google.com", &new_dynamic_domain_state));
6662
6663 EXPECT_EQ(new_static_domain_state.sts.upgrade_mode,
6664 static_domain_state.sts.upgrade_mode);
6665 EXPECT_EQ(new_static_domain_state.sts.include_subdomains,
6666 static_domain_state.sts.include_subdomains);
6667 EXPECT_EQ(new_static_domain_state.pkp.include_subdomains,
6668 static_domain_state.pkp.include_subdomains);
6669 EXPECT_TRUE(FingerprintsEqual(new_static_domain_state.pkp.spki_hashes,
6670 static_domain_state.pkp.spki_hashes));
6671 EXPECT_TRUE(FingerprintsEqual(new_static_domain_state.pkp.bad_spki_hashes,
6672 static_domain_state.pkp.bad_spki_hashes));
[email protected]316c1e5e2012-09-12 15:17:446673}
6674
[email protected]8ccc69f2012-11-28 19:52:146675// Make sure HSTS preserves a POST request's method and body.
6676TEST_F(HTTPSRequestTest, HSTSPreservesPosts) {
6677 static const char kData[] = "hello world";
6678
[email protected]ce7d0cbc2013-05-03 18:57:226679 SpawnedTestServer::SSLOptions ssl_options(
6680 SpawnedTestServer::SSLOptions::CERT_OK);
6681 SpawnedTestServer test_server(
6682 SpawnedTestServer::TYPE_HTTPS,
6683 ssl_options,
6684 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]8ccc69f2012-11-28 19:52:146685 ASSERT_TRUE(test_server.Start());
6686
6687
6688 // Per spec, TransportSecurityState expects a domain name, rather than an IP
6689 // address, so a MockHostResolver is needed to redirect www.somewhere.com to
[email protected]ce7d0cbc2013-05-03 18:57:226690 // the SpawnedTestServer. By default, MockHostResolver maps all hosts
6691 // to 127.0.0.1.
[email protected]8ccc69f2012-11-28 19:52:146692 MockHostResolver host_resolver;
[email protected]8ccc69f2012-11-28 19:52:146693
6694 // Force https for www.somewhere.com.
6695 TransportSecurityState transport_security_state;
[email protected]474f079e2013-03-02 19:11:206696 base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000);
6697 bool include_subdomains = false;
6698 transport_security_state.AddHSTS("www.somewhere.com", expiry,
6699 include_subdomains);
[email protected]8ccc69f2012-11-28 19:52:146700
6701 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
6702
6703 TestURLRequestContext context(true);
6704 context.set_host_resolver(&host_resolver);
6705 context.set_transport_security_state(&transport_security_state);
6706 context.set_network_delegate(&network_delegate);
6707 context.Init();
6708
6709 TestDelegate d;
6710 // Navigating to https://www.somewhere.com instead of https://127.0.0.1 will
6711 // cause a certificate error. Ignore the error.
6712 d.set_allow_certificate_errors(true);
6713
[email protected]f7022f32014-08-21 16:32:196714 scoped_ptr<URLRequest> req(context.CreateRequest(
6715 GURL(base::StringPrintf("http://www.somewhere.com:%d/echo",
6716 test_server.host_port_pair().port())),
6717 DEFAULT_PRIORITY, &d, NULL));
6718 req->set_method("POST");
mmenkecbc2b712014-10-09 20:29:076719 req->set_upload(CreateSimpleUploadData(kData));
[email protected]8ccc69f2012-11-28 19:52:146720
[email protected]f7022f32014-08-21 16:32:196721 req->Start();
[email protected]255620da2013-08-19 13:14:296722 base::RunLoop().Run();
[email protected]8ccc69f2012-11-28 19:52:146723
[email protected]f7022f32014-08-21 16:32:196724 EXPECT_EQ("https", req->url().scheme());
6725 EXPECT_EQ("POST", req->method());
[email protected]8ccc69f2012-11-28 19:52:146726 EXPECT_EQ(kData, d.data_received());
[email protected]07d9bfd2013-06-27 14:16:406727
6728 LoadTimingInfo load_timing_info;
6729 network_delegate.GetLoadTimingInfoBeforeRedirect(&load_timing_info);
6730 // LoadTimingInfo of HSTS redirects is similar to that of network cache hits
6731 TestLoadTimingCacheHitNoNetwork(load_timing_info);
[email protected]8ccc69f2012-11-28 19:52:146732}
6733
rob4e0be1f2014-09-11 23:40:226734// Make sure that the CORS headers are added to cross-origin HSTS redirects.
6735TEST_F(HTTPSRequestTest, HSTSCrossOriginAddHeaders) {
6736 static const char kOriginHeaderValue[] = "http://www.example.com";
6737
6738 SpawnedTestServer::SSLOptions ssl_options(
6739 SpawnedTestServer::SSLOptions::CERT_OK);
6740 SpawnedTestServer test_server(
6741 SpawnedTestServer::TYPE_HTTPS,
6742 ssl_options,
6743 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6744 ASSERT_TRUE(test_server.Start());
6745
6746 // Per spec, TransportSecurityState expects a domain name, rather than an IP
6747 // address, so a MockHostResolver is needed to redirect example.net to the
6748 // SpawnedTestServer. MockHostResolver maps all hosts to 127.0.0.1 by default.
6749 MockHostResolver host_resolver;
6750
6751 TransportSecurityState transport_security_state;
6752 base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1);
6753 bool include_subdomains = false;
6754 transport_security_state.AddHSTS("example.net", expiry, include_subdomains);
6755
6756 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
6757
6758 MockCertVerifier cert_verifier;
6759 cert_verifier.set_default_result(OK);
6760
6761 TestURLRequestContext context(true);
6762 context.set_host_resolver(&host_resolver);
6763 context.set_transport_security_state(&transport_security_state);
6764 context.set_network_delegate(&network_delegate);
6765 context.set_cert_verifier(&cert_verifier);
6766 context.Init();
6767
6768 GURL hsts_http_url(base::StringPrintf("http://example.net:%d/somehstssite",
6769 test_server.host_port_pair().port()));
6770 url::Replacements<char> replacements;
6771 const char kNewScheme[] = "https";
6772 replacements.SetScheme(kNewScheme, url::Component(0, strlen(kNewScheme)));
6773 GURL hsts_https_url = hsts_http_url.ReplaceComponents(replacements);
6774
6775 TestDelegate d;
6776 // Quit on redirect to allow response header inspection upon redirect.
6777 d.set_quit_on_redirect(true);
6778
6779 scoped_ptr<URLRequest> req(context.CreateRequest(hsts_http_url,
6780 DEFAULT_PRIORITY, &d, NULL));
6781 // Set Origin header to simulate a cross-origin request.
6782 HttpRequestHeaders request_headers;
6783 request_headers.SetHeader("Origin", kOriginHeaderValue);
6784 req->SetExtraRequestHeaders(request_headers);
6785
6786 req->Start();
6787 base::RunLoop().Run();
6788
6789 EXPECT_EQ(1, d.received_redirect_count());
6790
6791 const HttpResponseHeaders* headers = req->response_headers();
6792 std::string redirect_location;
6793 EXPECT_TRUE(headers->EnumerateHeader(NULL, "Location", &redirect_location));
6794 EXPECT_EQ(hsts_https_url.spec(), redirect_location);
6795
6796 std::string received_cors_header;
6797 EXPECT_TRUE(headers->EnumerateHeader(NULL, "Access-Control-Allow-Origin",
6798 &received_cors_header));
6799 EXPECT_EQ(kOriginHeaderValue, received_cors_header);
6800}
6801
[email protected]316c1e5e2012-09-12 15:17:446802namespace {
6803
6804class SSLClientAuthTestDelegate : public TestDelegate {
6805 public:
6806 SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) {
6807 }
dchengb03027d2014-10-21 12:00:206808 void OnCertificateRequested(URLRequest* request,
6809 SSLCertRequestInfo* cert_request_info) override {
[email protected]316c1e5e2012-09-12 15:17:446810 on_certificate_requested_count_++;
[email protected]2da659e2013-05-23 20:51:346811 base::MessageLoop::current()->Quit();
[email protected]316c1e5e2012-09-12 15:17:446812 }
6813 int on_certificate_requested_count() {
6814 return on_certificate_requested_count_;
6815 }
6816 private:
6817 int on_certificate_requested_count_;
6818};
6819
6820} // namespace
6821
6822// TODO(davidben): Test the rest of the code. Specifically,
6823// - Filtering which certificates to select.
6824// - Sending a certificate back.
6825// - Getting a certificate request in an SSL renegotiation sending the
6826// HTTP request.
6827TEST_F(HTTPSRequestTest, ClientAuthTest) {
[email protected]ce7d0cbc2013-05-03 18:57:226828 SpawnedTestServer::SSLOptions ssl_options;
[email protected]316c1e5e2012-09-12 15:17:446829 ssl_options.request_client_certificate = true;
[email protected]ce7d0cbc2013-05-03 18:57:226830 SpawnedTestServer test_server(
6831 SpawnedTestServer::TYPE_HTTPS,
6832 ssl_options,
6833 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:446834 ASSERT_TRUE(test_server.Start());
6835
6836 SSLClientAuthTestDelegate d;
6837 {
[email protected]f7022f32014-08-21 16:32:196838 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
6839 test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:446840
[email protected]f7022f32014-08-21 16:32:196841 r->Start();
6842 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:446843
[email protected]255620da2013-08-19 13:14:296844 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446845
6846 EXPECT_EQ(1, d.on_certificate_requested_count());
6847 EXPECT_FALSE(d.received_data_before_response());
6848 EXPECT_EQ(0, d.bytes_received());
6849
6850 // Send no certificate.
6851 // TODO(davidben): Get temporary client cert import (with keys) working on
6852 // all platforms so we can test sending a cert as well.
[email protected]f7022f32014-08-21 16:32:196853 r->ContinueWithCertificate(NULL);
[email protected]316c1e5e2012-09-12 15:17:446854
[email protected]255620da2013-08-19 13:14:296855 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446856
6857 EXPECT_EQ(1, d.response_started_count());
6858 EXPECT_FALSE(d.received_data_before_response());
6859 EXPECT_NE(0, d.bytes_received());
6860 }
6861}
6862
6863TEST_F(HTTPSRequestTest, ResumeTest) {
6864 // Test that we attempt a session resume when making two connections to the
6865 // same host.
[email protected]ce7d0cbc2013-05-03 18:57:226866 SpawnedTestServer::SSLOptions ssl_options;
[email protected]316c1e5e2012-09-12 15:17:446867 ssl_options.record_resume = true;
[email protected]ce7d0cbc2013-05-03 18:57:226868 SpawnedTestServer test_server(
6869 SpawnedTestServer::TYPE_HTTPS,
6870 ssl_options,
6871 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:446872 ASSERT_TRUE(test_server.Start());
6873
6874 SSLClientSocket::ClearSessionCache();
6875
6876 {
6877 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196878 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
6879 test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:446880
[email protected]f7022f32014-08-21 16:32:196881 r->Start();
6882 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:446883
[email protected]255620da2013-08-19 13:14:296884 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446885
6886 EXPECT_EQ(1, d.response_started_count());
6887 }
6888
6889 reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())->
6890 CloseAllConnections();
6891
6892 {
6893 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196894 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
6895 test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:446896
[email protected]f7022f32014-08-21 16:32:196897 r->Start();
6898 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:446899
[email protected]255620da2013-08-19 13:14:296900 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446901
6902 // The response will look like;
6903 // insert abc
6904 // lookup abc
6905 // insert xyz
6906 //
6907 // With a newline at the end which makes the split think that there are
6908 // four lines.
6909
6910 EXPECT_EQ(1, d.response_started_count());
6911 std::vector<std::string> lines;
6912 base::SplitString(d.data_received(), '\n', &lines);
6913 ASSERT_EQ(4u, lines.size()) << d.data_received();
6914
6915 std::string session_id;
6916
6917 for (size_t i = 0; i < 2; i++) {
6918 std::vector<std::string> parts;
6919 base::SplitString(lines[i], '\t', &parts);
6920 ASSERT_EQ(2u, parts.size());
6921 if (i == 0) {
6922 EXPECT_EQ("insert", parts[0]);
6923 session_id = parts[1];
6924 } else {
6925 EXPECT_EQ("lookup", parts[0]);
6926 EXPECT_EQ(session_id, parts[1]);
6927 }
6928 }
6929 }
6930}
6931
Adam Langley32352ad2014-10-14 22:31:006932// AssertTwoDistinctSessionsInserted checks that |session_info|, which must be
6933// the result of fetching "ssl-session-cache" from the test server, indicates
6934// that exactly two different sessions were inserted, with no lookups etc.
6935static void AssertTwoDistinctSessionsInserted(const string& session_info) {
6936 std::vector<std::string> lines;
6937 base::SplitString(session_info, '\n', &lines);
6938 ASSERT_EQ(3u, lines.size()) << session_info;
6939
6940 std::string session_id;
6941 for (size_t i = 0; i < 2; i++) {
6942 std::vector<std::string> parts;
6943 base::SplitString(lines[i], '\t', &parts);
6944 ASSERT_EQ(2u, parts.size());
6945 EXPECT_EQ("insert", parts[0]);
6946 if (i == 0) {
6947 session_id = parts[1];
6948 } else {
6949 EXPECT_NE(session_id, parts[1]);
6950 }
6951 }
6952}
6953
[email protected]316c1e5e2012-09-12 15:17:446954TEST_F(HTTPSRequestTest, SSLSessionCacheShardTest) {
6955 // Test that sessions aren't resumed when the value of ssl_session_cache_shard
6956 // differs.
[email protected]ce7d0cbc2013-05-03 18:57:226957 SpawnedTestServer::SSLOptions ssl_options;
[email protected]316c1e5e2012-09-12 15:17:446958 ssl_options.record_resume = true;
[email protected]ce7d0cbc2013-05-03 18:57:226959 SpawnedTestServer test_server(
6960 SpawnedTestServer::TYPE_HTTPS,
6961 ssl_options,
6962 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:446963 ASSERT_TRUE(test_server.Start());
6964
6965 SSLClientSocket::ClearSessionCache();
6966
6967 {
6968 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196969 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
6970 test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:446971
[email protected]f7022f32014-08-21 16:32:196972 r->Start();
6973 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:446974
[email protected]255620da2013-08-19 13:14:296975 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446976
6977 EXPECT_EQ(1, d.response_started_count());
6978 }
6979
6980 // Now create a new HttpCache with a different ssl_session_cache_shard value.
6981 HttpNetworkSession::Params params;
6982 params.host_resolver = default_context_.host_resolver();
6983 params.cert_verifier = default_context_.cert_verifier();
[email protected]b1c988b2013-06-13 06:48:116984 params.transport_security_state = default_context_.transport_security_state();
[email protected]316c1e5e2012-09-12 15:17:446985 params.proxy_service = default_context_.proxy_service();
6986 params.ssl_config_service = default_context_.ssl_config_service();
6987 params.http_auth_handler_factory =
6988 default_context_.http_auth_handler_factory();
[email protected]cc05edc2013-03-08 18:04:416989 params.network_delegate = &default_network_delegate_;
[email protected]316c1e5e2012-09-12 15:17:446990 params.http_server_properties = default_context_.http_server_properties();
6991 params.ssl_session_cache_shard = "alternate";
6992
[email protected]cba24642014-08-15 20:49:596993 scoped_ptr<HttpCache> cache(new HttpCache(
6994 new HttpNetworkSession(params),
6995 HttpCache::DefaultBackend::InMemory(0)));
[email protected]316c1e5e2012-09-12 15:17:446996
6997 default_context_.set_http_transaction_factory(cache.get());
6998
6999 {
7000 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:197001 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
7002 test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:447003
[email protected]f7022f32014-08-21 16:32:197004 r->Start();
7005 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:447006
[email protected]255620da2013-08-19 13:14:297007 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:447008
7009 // The response will look like;
7010 // insert abc
7011 // insert xyz
7012 //
7013 // With a newline at the end which makes the split think that there are
7014 // three lines.
7015
7016 EXPECT_EQ(1, d.response_started_count());
Adam Langley32352ad2014-10-14 22:31:007017 AssertTwoDistinctSessionsInserted(d.data_received());
[email protected]316c1e5e2012-09-12 15:17:447018 }
7019}
7020
davidben8ecc3072014-09-03 23:19:097021#if defined(OS_WIN)
7022
7023namespace {
7024
7025bool IsECDSACipherSuite(uint16_t cipher_suite) {
7026 const char* key_exchange;
7027 const char* cipher;
7028 const char* mac;
7029 bool is_aead;
7030 SSLCipherSuiteToStrings(&key_exchange, &cipher, &mac, &is_aead, cipher_suite);
7031 return std::string(key_exchange).find("ECDSA") != std::string::npos;
7032}
7033
7034} // namespace
7035
7036// Test that ECDSA is disabled on Windows XP, where ECDSA certificates cannot be
7037// verified.
davidben9399c952014-10-07 04:09:117038TEST_F(HTTPSRequestTest, DisableECDSAOnXP) {
davidben8ecc3072014-09-03 23:19:097039 if (base::win::GetVersion() >= base::win::VERSION_VISTA) {
7040 LOG(INFO) << "Skipping test on this version.";
7041 return;
7042 }
7043
7044 SpawnedTestServer test_server(
7045 SpawnedTestServer::TYPE_HTTPS,
7046 SpawnedTestServer::kLocalhost,
7047 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7048 ASSERT_TRUE(test_server.Start());
7049
7050 TestDelegate d;
7051 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
7052 test_server.GetURL("client-cipher-list"), DEFAULT_PRIORITY, &d, NULL));
7053 r->Start();
7054 EXPECT_TRUE(r->is_pending());
7055
7056 base::RunLoop().Run();
7057
7058 EXPECT_EQ(1, d.response_started_count());
7059 std::vector<std::string> lines;
7060 base::SplitString(d.data_received(), '\n', &lines);
7061
7062 for (size_t i = 0; i < lines.size(); i++) {
7063 int cipher_suite;
7064 ASSERT_TRUE(base::StringToInt(lines[i], &cipher_suite));
7065 EXPECT_FALSE(IsECDSACipherSuite(cipher_suite))
7066 << "ClientHello advertised " << cipher_suite;
7067 }
7068}
7069
7070#endif // OS_WIN
7071
Adam Langley32352ad2014-10-14 22:31:007072class TestSSLConfigService : public SSLConfigService {
7073 public:
7074 TestSSLConfigService(bool ev_enabled,
7075 bool online_rev_checking,
7076 bool rev_checking_required_local_anchors)
7077 : ev_enabled_(ev_enabled),
7078 online_rev_checking_(online_rev_checking),
7079 rev_checking_required_local_anchors_(
7080 rev_checking_required_local_anchors),
7081 fallback_min_version_(0) {}
7082
7083 void set_fallback_min_version(uint16 version) {
7084 fallback_min_version_ = version;
7085 }
7086
7087 // SSLConfigService:
dchengb03027d2014-10-21 12:00:207088 void GetSSLConfig(SSLConfig* config) override {
Adam Langley32352ad2014-10-14 22:31:007089 *config = SSLConfig();
7090 config->rev_checking_enabled = online_rev_checking_;
7091 config->verify_ev_cert = ev_enabled_;
7092 config->rev_checking_required_local_anchors =
7093 rev_checking_required_local_anchors_;
7094 if (fallback_min_version_) {
7095 config->version_fallback_min = fallback_min_version_;
7096 }
7097 }
7098
7099 protected:
dchengb03027d2014-10-21 12:00:207100 ~TestSSLConfigService() override {}
Adam Langley32352ad2014-10-14 22:31:007101
7102 private:
7103 const bool ev_enabled_;
7104 const bool online_rev_checking_;
7105 const bool rev_checking_required_local_anchors_;
7106 uint16 fallback_min_version_;
7107};
7108
7109class FallbackTestURLRequestContext : public TestURLRequestContext {
7110 public:
7111 explicit FallbackTestURLRequestContext(bool delay_initialization)
7112 : TestURLRequestContext(delay_initialization) {}
7113
7114 void set_fallback_min_version(uint16 version) {
7115 TestSSLConfigService *ssl_config_service =
7116 new TestSSLConfigService(true /* check for EV */,
7117 false /* online revocation checking */,
7118 false /* require rev. checking for local
7119 anchors */);
7120 ssl_config_service->set_fallback_min_version(version);
7121 set_ssl_config_service(ssl_config_service);
7122 }
7123};
7124
[email protected]48d2b7c52014-06-27 01:16:557125class HTTPSFallbackTest : public testing::Test {
7126 public:
Adam Langley32352ad2014-10-14 22:31:007127 HTTPSFallbackTest() : context_(true) {}
[email protected]48d2b7c52014-06-27 01:16:557128 virtual ~HTTPSFallbackTest() {}
7129
7130 protected:
7131 void DoFallbackTest(const SpawnedTestServer::SSLOptions& ssl_options) {
7132 DCHECK(!request_);
Adam Langley32352ad2014-10-14 22:31:007133 context_.Init();
7134 delegate_.set_allow_certificate_errors(true);
7135
[email protected]48d2b7c52014-06-27 01:16:557136 SpawnedTestServer test_server(
7137 SpawnedTestServer::TYPE_HTTPS,
7138 ssl_options,
7139 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7140 ASSERT_TRUE(test_server.Start());
7141
[email protected]f7022f32014-08-21 16:32:197142 request_ = context_.CreateRequest(
7143 test_server.GetURL(std::string()), DEFAULT_PRIORITY, &delegate_, NULL);
[email protected]48d2b7c52014-06-27 01:16:557144 request_->Start();
7145
7146 base::RunLoop().Run();
7147 }
7148
Adam Langley32352ad2014-10-14 22:31:007149 void set_fallback_min_version(uint16 version) {
7150 context_.set_fallback_min_version(version);
7151 }
7152
[email protected]48d2b7c52014-06-27 01:16:557153 void ExpectConnection(int version) {
7154 EXPECT_EQ(1, delegate_.response_started_count());
7155 EXPECT_NE(0, delegate_.bytes_received());
7156 EXPECT_EQ(version, SSLConnectionStatusToVersion(
7157 request_->ssl_info().connection_status));
7158 EXPECT_TRUE(request_->ssl_info().connection_status &
7159 SSL_CONNECTION_VERSION_FALLBACK);
7160 }
7161
7162 void ExpectFailure(int error) {
7163 EXPECT_EQ(1, delegate_.response_started_count());
7164 EXPECT_FALSE(request_->status().is_success());
7165 EXPECT_EQ(URLRequestStatus::FAILED, request_->status().status());
7166 EXPECT_EQ(error, request_->status().error());
7167 }
7168
7169 private:
7170 TestDelegate delegate_;
Adam Langley32352ad2014-10-14 22:31:007171 FallbackTestURLRequestContext context_;
[email protected]48d2b7c52014-06-27 01:16:557172 scoped_ptr<URLRequest> request_;
7173};
7174
7175// Tests TLSv1.1 -> TLSv1 fallback. Verifies that we don't fall back more
7176// than necessary.
7177TEST_F(HTTPSFallbackTest, TLSv1Fallback) {
7178 SpawnedTestServer::SSLOptions ssl_options(
7179 SpawnedTestServer::SSLOptions::CERT_OK);
7180 ssl_options.tls_intolerant =
7181 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1;
7182
7183 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
7184 ExpectConnection(SSL_CONNECTION_VERSION_TLS1);
7185}
7186
7187// This test is disabled on Android because the remote test server doesn't cause
7188// a TCP reset.
7189#if !defined(OS_ANDROID)
7190// Tests fallback to TLS 1.0 on connection reset.
7191TEST_F(HTTPSFallbackTest, TLSv1FallbackReset) {
7192 SpawnedTestServer::SSLOptions ssl_options(
7193 SpawnedTestServer::SSLOptions::CERT_OK);
7194 ssl_options.tls_intolerant =
7195 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1;
7196 ssl_options.tls_intolerance_type =
7197 SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_RESET;
7198
7199 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
7200 ExpectConnection(SSL_CONNECTION_VERSION_TLS1);
7201}
7202#endif // !OS_ANDROID
7203
[email protected]12833302014-07-02 01:57:317204// Tests that we don't fallback on handshake failure with servers that implement
7205// TLS_FALLBACK_SCSV. Also ensure that the original error code is reported.
[email protected]48d2b7c52014-06-27 01:16:557206TEST_F(HTTPSFallbackTest, FallbackSCSV) {
[email protected]48d2b7c52014-06-27 01:16:557207 SpawnedTestServer::SSLOptions ssl_options(
7208 SpawnedTestServer::SSLOptions::CERT_OK);
7209 // Configure HTTPS server to be intolerant of TLS >= 1.0 in order to trigger
7210 // a version fallback.
7211 ssl_options.tls_intolerant =
7212 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL;
7213 // Have the server process TLS_FALLBACK_SCSV so that version fallback
7214 // connections are rejected.
7215 ssl_options.fallback_scsv_enabled = true;
7216
7217 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
7218
7219 // ERR_SSL_VERSION_OR_CIPHER_MISMATCH is how the server simulates version
7220 // intolerance. If the fallback SCSV is processed when the original error
7221 // that caused the fallback should be returned, which should be
7222 // ERR_SSL_VERSION_OR_CIPHER_MISMATCH.
7223 ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH);
7224}
7225
[email protected]12833302014-07-02 01:57:317226// Tests that we don't fallback on connection closed with servers that implement
7227// TLS_FALLBACK_SCSV. Also ensure that the original error code is reported.
[email protected]12833302014-07-02 01:57:317228TEST_F(HTTPSFallbackTest, FallbackSCSVClosed) {
[email protected]12833302014-07-02 01:57:317229 SpawnedTestServer::SSLOptions ssl_options(
7230 SpawnedTestServer::SSLOptions::CERT_OK);
7231 // Configure HTTPS server to be intolerant of TLS >= 1.0 in order to trigger
7232 // a version fallback.
7233 ssl_options.tls_intolerant =
7234 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL;
7235 ssl_options.tls_intolerance_type =
7236 SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE;
7237 // Have the server process TLS_FALLBACK_SCSV so that version fallback
7238 // connections are rejected.
7239 ssl_options.fallback_scsv_enabled = true;
7240
7241 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
7242
7243 // The original error should be replayed on rejected fallback.
7244 ExpectFailure(ERR_CONNECTION_CLOSED);
7245}
7246
Adam Langley32352ad2014-10-14 22:31:007247// Tests that the SSLv3 fallback doesn't happen by default.
[email protected]48d2b7c52014-06-27 01:16:557248TEST_F(HTTPSFallbackTest, SSLv3Fallback) {
7249 SpawnedTestServer::SSLOptions ssl_options(
7250 SpawnedTestServer::SSLOptions::CERT_OK);
7251 ssl_options.tls_intolerant =
7252 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL;
7253
7254 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
Adam Langley32352ad2014-10-14 22:31:007255 ExpectFailure(ERR_SSL_FALLBACK_BEYOND_MINIMUM_VERSION);
7256}
7257
7258// Tests that the SSLv3 fallback works when explicitly enabled.
7259TEST_F(HTTPSFallbackTest, SSLv3FallbackEnabled) {
7260 SpawnedTestServer::SSLOptions ssl_options(
7261 SpawnedTestServer::SSLOptions::CERT_OK);
7262 ssl_options.tls_intolerant =
7263 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL;
7264 set_fallback_min_version(SSL_PROTOCOL_VERSION_SSL3);
7265
7266 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
[email protected]48d2b7c52014-06-27 01:16:557267 ExpectConnection(SSL_CONNECTION_VERSION_SSL3);
7268}
7269
Adam Langley32352ad2014-10-14 22:31:007270// Tests that the SSLv3 fallback triggers on closed connections when explicitly
7271// enabled.
[email protected]48d2b7c52014-06-27 01:16:557272TEST_F(HTTPSFallbackTest, SSLv3FallbackClosed) {
7273 SpawnedTestServer::SSLOptions ssl_options(
7274 SpawnedTestServer::SSLOptions::CERT_OK);
7275 ssl_options.tls_intolerant =
7276 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL;
7277 ssl_options.tls_intolerance_type =
7278 SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE;
Adam Langley32352ad2014-10-14 22:31:007279 set_fallback_min_version(SSL_PROTOCOL_VERSION_SSL3);
[email protected]48d2b7c52014-06-27 01:16:557280
7281 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
7282 ExpectConnection(SSL_CONNECTION_VERSION_SSL3);
7283}
7284
Adam Langley32352ad2014-10-14 22:31:007285// Test that SSLv3 fallback probe connections don't cause sessions to be cached.
7286TEST_F(HTTPSRequestTest, SSLv3FallbackNoCache) {
7287 SpawnedTestServer::SSLOptions ssl_options(
7288 SpawnedTestServer::SSLOptions::CERT_OK);
7289 ssl_options.tls_intolerant =
7290 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL;
7291 ssl_options.tls_intolerance_type =
7292 SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE;
7293 ssl_options.record_resume = true;
7294
7295 SpawnedTestServer test_server(
7296 SpawnedTestServer::TYPE_HTTPS,
7297 ssl_options,
7298 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7299 ASSERT_TRUE(test_server.Start());
7300
7301 SSLClientSocket::ClearSessionCache();
7302
7303 // Make a connection that does a probe fallback to SSLv3 but fails because
7304 // SSLv3 fallback is disabled. We don't wish a session for this connection to
7305 // be inserted locally.
7306 {
7307 TestDelegate delegate;
7308 FallbackTestURLRequestContext context(true);
7309
7310 context.Init();
7311 scoped_ptr<URLRequest> request(context.CreateRequest(
7312 test_server.GetURL(std::string()), DEFAULT_PRIORITY, &delegate, NULL));
7313 request->Start();
7314
7315 base::RunLoop().Run();
7316
7317 EXPECT_EQ(1, delegate.response_started_count());
7318 EXPECT_FALSE(request->status().is_success());
7319 EXPECT_EQ(URLRequestStatus::FAILED, request->status().status());
7320 EXPECT_EQ(ERR_SSL_FALLBACK_BEYOND_MINIMUM_VERSION,
7321 request->status().error());
7322 }
7323
7324 // Now allow SSLv3 connections and request the session cache log.
7325 {
7326 TestDelegate delegate;
7327 FallbackTestURLRequestContext context(true);
7328 context.set_fallback_min_version(SSL_PROTOCOL_VERSION_SSL3);
7329
7330 context.Init();
7331 scoped_ptr<URLRequest> request(
7332 context.CreateRequest(test_server.GetURL("ssl-session-cache"),
7333 DEFAULT_PRIORITY,
7334 &delegate,
7335 NULL));
7336 request->Start();
7337
7338 base::RunLoop().Run();
7339
7340 EXPECT_EQ(1, delegate.response_started_count());
7341 EXPECT_NE(0, delegate.bytes_received());
7342 EXPECT_EQ(SSL_CONNECTION_VERSION_SSL3, SSLConnectionStatusToVersion(
7343 request->ssl_info().connection_status));
7344 EXPECT_TRUE(request->ssl_info().connection_status &
7345 SSL_CONNECTION_VERSION_FALLBACK);
7346
7347 std::vector<std::string> lines;
7348 // If no sessions were cached then the server should have seen two sessions
7349 // inserted with no lookups.
7350 AssertTwoDistinctSessionsInserted(delegate.data_received());
7351 }
7352}
7353
[email protected]48d2b7c52014-06-27 01:16:557354// This test is disabled on Android because the remote test server doesn't cause
[email protected]5aea79182014-07-14 20:43:417355// a TCP reset.
7356#if !defined(OS_ANDROID)
[email protected]48d2b7c52014-06-27 01:16:557357// Tests that a reset connection does not fallback down to SSL3.
7358TEST_F(HTTPSFallbackTest, SSLv3NoFallbackReset) {
7359 SpawnedTestServer::SSLOptions ssl_options(
7360 SpawnedTestServer::SSLOptions::CERT_OK);
7361 ssl_options.tls_intolerant =
7362 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL;
7363 ssl_options.tls_intolerance_type =
7364 SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_RESET;
7365
7366 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
7367 ExpectFailure(ERR_CONNECTION_RESET);
7368}
[email protected]5aea79182014-07-14 20:43:417369#endif // !OS_ANDROID
[email protected]48d2b7c52014-06-27 01:16:557370
[email protected]a8fed1742013-12-27 02:14:247371class HTTPSSessionTest : public testing::Test {
7372 public:
7373 HTTPSSessionTest() : default_context_(true) {
[email protected]cba24642014-08-15 20:49:597374 cert_verifier_.set_default_result(OK);
[email protected]a8fed1742013-12-27 02:14:247375
7376 default_context_.set_network_delegate(&default_network_delegate_);
7377 default_context_.set_cert_verifier(&cert_verifier_);
7378 default_context_.Init();
7379 }
7380 virtual ~HTTPSSessionTest() {}
7381
7382 protected:
7383 MockCertVerifier cert_verifier_;
7384 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest.
7385 TestURLRequestContext default_context_;
7386};
7387
7388// Tests that session resumption is not attempted if an invalid certificate
7389// is presented.
7390TEST_F(HTTPSSessionTest, DontResumeSessionsForInvalidCertificates) {
7391 SpawnedTestServer::SSLOptions ssl_options;
7392 ssl_options.record_resume = true;
7393 SpawnedTestServer test_server(
7394 SpawnedTestServer::TYPE_HTTPS,
7395 ssl_options,
7396 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7397 ASSERT_TRUE(test_server.Start());
7398
7399 SSLClientSocket::ClearSessionCache();
7400
7401 // Simulate the certificate being expired and attempt a connection.
[email protected]cba24642014-08-15 20:49:597402 cert_verifier_.set_default_result(ERR_CERT_DATE_INVALID);
[email protected]a8fed1742013-12-27 02:14:247403 {
7404 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:197405 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
7406 test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, NULL));
[email protected]a8fed1742013-12-27 02:14:247407
[email protected]f7022f32014-08-21 16:32:197408 r->Start();
7409 EXPECT_TRUE(r->is_pending());
[email protected]a8fed1742013-12-27 02:14:247410
7411 base::RunLoop().Run();
7412
7413 EXPECT_EQ(1, d.response_started_count());
7414 }
7415
7416 reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())->
7417 CloseAllConnections();
7418
7419 // Now change the certificate to be acceptable (so that the response is
7420 // loaded), and ensure that no session id is presented to the peer.
[email protected]cba24642014-08-15 20:49:597421 cert_verifier_.set_default_result(OK);
[email protected]a8fed1742013-12-27 02:14:247422 {
7423 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:197424 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
7425 test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, NULL));
[email protected]a8fed1742013-12-27 02:14:247426
[email protected]f7022f32014-08-21 16:32:197427 r->Start();
7428 EXPECT_TRUE(r->is_pending());
[email protected]a8fed1742013-12-27 02:14:247429
7430 base::RunLoop().Run();
7431
7432 // The response will look like;
7433 // insert abc
7434 // insert xyz
7435 //
7436 // With a newline at the end which makes the split think that there are
7437 // three lines.
7438 //
7439 // If a session was presented (eg: a bug), then the response would look
7440 // like;
7441 // insert abc
7442 // lookup abc
7443 // insert xyz
7444
7445 EXPECT_EQ(1, d.response_started_count());
Adam Langley32352ad2014-10-14 22:31:007446 AssertTwoDistinctSessionsInserted(d.data_received());
[email protected]a8fed1742013-12-27 02:14:247447 }
7448}
7449
[email protected]dffe8242012-03-20 15:14:277450// This the fingerprint of the "Testing CA" certificate used by the testserver.
7451// See net/data/ssl/certificates/ocsp-test-root.pem.
[email protected]ede03212012-09-07 12:52:267452static const SHA1HashValue kOCSPTestCertFingerprint =
[email protected]dffe8242012-03-20 15:14:277453 { { 0xf1, 0xad, 0xf6, 0xce, 0x42, 0xac, 0xe7, 0xb4, 0xf4, 0x24,
7454 0xdb, 0x1a, 0xf7, 0xa0, 0x9f, 0x09, 0xa1, 0xea, 0xf1, 0x5c } };
7455
[email protected]51523f52013-07-31 21:57:287456// This is the SHA256, SPKI hash of the "Testing CA" certificate used by the
7457// testserver.
7458static const SHA256HashValue kOCSPTestCertSPKI = { {
7459 0xee, 0xe6, 0x51, 0x2d, 0x4c, 0xfa, 0xf7, 0x3e,
7460 0x6c, 0xd8, 0xca, 0x67, 0xed, 0xb5, 0x5d, 0x49,
7461 0x76, 0xe1, 0x52, 0xa7, 0x6e, 0x0e, 0xa0, 0x74,
7462 0x09, 0x75, 0xe6, 0x23, 0x24, 0xbd, 0x1b, 0x28,
7463} };
7464
[email protected]dffe8242012-03-20 15:14:277465// This is the policy OID contained in the certificates that testserver
7466// generates.
7467static const char kOCSPTestCertPolicy[] = "1.3.6.1.4.1.11129.2.4.1";
7468
7469class HTTPSOCSPTest : public HTTPSRequestTest {
7470 public:
7471 HTTPSOCSPTest()
[email protected]ef2bf422012-05-11 03:27:097472 : context_(true),
[email protected]b6f2de32012-08-17 04:35:087473 ev_test_policy_(
7474 new ScopedTestEVPolicy(EVRootCAMetadata::GetInstance(),
7475 kOCSPTestCertFingerprint,
7476 kOCSPTestCertPolicy)) {
[email protected]a13234c2012-03-20 21:45:027477 }
7478
mostynbba063d6032014-10-09 11:01:137479 virtual void SetUp() override {
[email protected]ef2bf422012-05-11 03:27:097480 SetupContext(&context_);
7481 context_.Init();
[email protected]dffe8242012-03-20 15:14:277482
[email protected]cba24642014-08-15 20:49:597483 scoped_refptr<X509Certificate> root_cert =
[email protected]3a86a712013-07-30 07:16:207484 ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem");
dchengc2e01e82014-08-27 00:24:427485 CHECK_NE(static_cast<X509Certificate*>(NULL), root_cert.get());
[email protected]90499482013-06-01 00:39:507486 test_root_.reset(new ScopedTestRoot(root_cert.get()));
[email protected]dffe8242012-03-20 15:14:277487
[email protected]a592c0432012-12-01 18:10:297488#if defined(USE_NSS) || defined(OS_IOS)
[email protected]ef2bf422012-05-11 03:27:097489 SetURLRequestContextForNSSHttpIO(&context_);
[email protected]dffe8242012-03-20 15:14:277490 EnsureNSSHttpIOInit();
7491#endif
7492 }
7493
[email protected]ce7d0cbc2013-05-03 18:57:227494 void DoConnection(const SpawnedTestServer::SSLOptions& ssl_options,
[email protected]dffe8242012-03-20 15:14:277495 CertStatus* out_cert_status) {
[email protected]924e9f92012-12-16 22:00:537496 // We always overwrite out_cert_status.
7497 *out_cert_status = 0;
[email protected]ce7d0cbc2013-05-03 18:57:227498 SpawnedTestServer test_server(
7499 SpawnedTestServer::TYPE_HTTPS,
7500 ssl_options,
7501 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]dffe8242012-03-20 15:14:277502 ASSERT_TRUE(test_server.Start());
7503
7504 TestDelegate d;
7505 d.set_allow_certificate_errors(true);
[email protected]f7022f32014-08-21 16:32:197506 scoped_ptr<URLRequest> r(context_.CreateRequest(
7507 test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL));
7508 r->Start();
[email protected]dffe8242012-03-20 15:14:277509
[email protected]255620da2013-08-19 13:14:297510 base::RunLoop().Run();
[email protected]dffe8242012-03-20 15:14:277511
7512 EXPECT_EQ(1, d.response_started_count());
[email protected]f7022f32014-08-21 16:32:197513 *out_cert_status = r->ssl_info().cert_status;
[email protected]dffe8242012-03-20 15:14:277514 }
7515
[email protected]46fadfd2013-02-06 09:40:167516 virtual ~HTTPSOCSPTest() {
[email protected]a592c0432012-12-01 18:10:297517#if defined(USE_NSS) || defined(OS_IOS)
[email protected]dffe8242012-03-20 15:14:277518 ShutdownNSSHttpIO();
7519#endif
7520 }
7521
[email protected]a13234c2012-03-20 21:45:027522 protected:
7523 // SetupContext configures the URLRequestContext that will be used for making
7524 // connetions to testserver. This can be overridden in test subclasses for
7525 // different behaviour.
7526 virtual void SetupContext(URLRequestContext* context) {
7527 context->set_ssl_config_service(
7528 new TestSSLConfigService(true /* check for EV */,
[email protected]3a86a712013-07-30 07:16:207529 true /* online revocation checking */,
7530 false /* require rev. checking for local
7531 anchors */));
[email protected]a13234c2012-03-20 21:45:027532 }
7533
[email protected]dffe8242012-03-20 15:14:277534 scoped_ptr<ScopedTestRoot> test_root_;
[email protected]ef2bf422012-05-11 03:27:097535 TestURLRequestContext context_;
[email protected]b6f2de32012-08-17 04:35:087536 scoped_ptr<ScopedTestEVPolicy> ev_test_policy_;
[email protected]dffe8242012-03-20 15:14:277537};
7538
[email protected]a13234c2012-03-20 21:45:027539static CertStatus ExpectedCertStatusForFailedOnlineRevocationCheck() {
[email protected]05454a432012-03-20 20:04:017540#if defined(OS_WIN)
[email protected]a13234c2012-03-20 21:45:027541 // Windows can return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION but we don't
7542 // have that ability on other platforms.
7543 return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION;
7544#else
7545 return 0;
7546#endif
7547}
7548
[email protected]3a86a712013-07-30 07:16:207549// SystemSupportsHardFailRevocationChecking returns true iff the current
7550// operating system supports revocation checking and can distinguish between
7551// situations where a given certificate lacks any revocation information (eg:
7552// no CRLDistributionPoints and no OCSP Responder AuthorityInfoAccess) and when
7553// revocation information cannot be obtained (eg: the CRL was unreachable).
7554// If it does not, then tests which rely on 'hard fail' behaviour should be
7555// skipped.
7556static bool SystemSupportsHardFailRevocationChecking() {
7557#if defined(OS_WIN) || defined(USE_NSS) || defined(OS_IOS)
7558 return true;
7559#else
7560 return false;
7561#endif
7562}
7563
[email protected]a13234c2012-03-20 21:45:027564// SystemUsesChromiumEVMetadata returns true iff the current operating system
7565// uses Chromium's EV metadata (i.e. EVRootCAMetadata). If it does not, then
7566// several tests are effected because our testing EV certificate won't be
7567// recognised as EV.
7568static bool SystemUsesChromiumEVMetadata() {
[email protected]e1b2d732014-03-28 16:20:327569#if defined(USE_OPENSSL_CERTS) && !defined(OS_ANDROID)
[email protected]5c504192012-03-27 19:00:577570 // http://crbug.com/117478 - OpenSSL does not support EV validation.
7571 return false;
[email protected]e1b2d732014-03-28 16:20:327572#elif (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID)
7573 // On OS X and Android, we use the system to tell us whether a certificate is
7574 // EV or not and the system won't recognise our testing root.
[email protected]05454a432012-03-20 20:04:017575 return false;
7576#else
7577 return true;
7578#endif
7579}
7580
[email protected]b6f2de32012-08-17 04:35:087581static bool SystemSupportsOCSP() {
[email protected]5c504192012-03-27 19:00:577582#if defined(USE_OPENSSL)
7583 // http://crbug.com/117478 - OpenSSL does not support OCSP.
7584 return false;
7585#elif defined(OS_WIN)
[email protected]a13234c2012-03-20 21:45:027586 return base::win::GetVersion() >= base::win::VERSION_VISTA;
7587#elif defined(OS_ANDROID)
7588 // TODO(jnd): http://crbug.com/117478 - EV verification is not yet supported.
7589 return false;
7590#else
7591 return true;
7592#endif
7593}
7594
[email protected]dffe8242012-03-20 15:14:277595TEST_F(HTTPSOCSPTest, Valid) {
7596 if (!SystemSupportsOCSP()) {
7597 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7598 return;
7599 }
7600
[email protected]ce7d0cbc2013-05-03 18:57:227601 SpawnedTestServer::SSLOptions ssl_options(
7602 SpawnedTestServer::SSLOptions::CERT_AUTO);
7603 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK;
[email protected]dffe8242012-03-20 15:14:277604
[email protected]924e9f92012-12-16 22:00:537605 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:127606 DoConnection(ssl_options, &cert_status);
[email protected]dffe8242012-03-20 15:14:277607
7608 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
7609
[email protected]a13234c2012-03-20 21:45:027610 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7611 static_cast<bool>(cert_status & CERT_STATUS_IS_EV));
[email protected]dffe8242012-03-20 15:14:277612
7613 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
7614}
7615
7616TEST_F(HTTPSOCSPTest, Revoked) {
7617 if (!SystemSupportsOCSP()) {
7618 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7619 return;
7620 }
7621
[email protected]ce7d0cbc2013-05-03 18:57:227622 SpawnedTestServer::SSLOptions ssl_options(
7623 SpawnedTestServer::SSLOptions::CERT_AUTO);
7624 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED;
[email protected]dffe8242012-03-20 15:14:277625
7626 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:127627 DoConnection(ssl_options, &cert_status);
[email protected]dffe8242012-03-20 15:14:277628
[email protected]a592c0432012-12-01 18:10:297629#if !(defined(OS_MACOSX) && !defined(OS_IOS))
[email protected]dffe8242012-03-20 15:14:277630 // Doesn't pass on OS X yet for reasons that need to be investigated.
7631 EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS);
7632#endif
7633 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7634 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
7635}
7636
7637TEST_F(HTTPSOCSPTest, Invalid) {
7638 if (!SystemSupportsOCSP()) {
7639 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7640 return;
7641 }
7642
[email protected]ce7d0cbc2013-05-03 18:57:227643 SpawnedTestServer::SSLOptions ssl_options(
7644 SpawnedTestServer::SSLOptions::CERT_AUTO);
7645 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
[email protected]dffe8242012-03-20 15:14:277646
[email protected]924e9f92012-12-16 22:00:537647 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:127648 DoConnection(ssl_options, &cert_status);
[email protected]dffe8242012-03-20 15:14:277649
[email protected]a13234c2012-03-20 21:45:027650 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
[email protected]dffe8242012-03-20 15:14:277651 cert_status & CERT_STATUS_ALL_ERRORS);
[email protected]dffe8242012-03-20 15:14:277652
7653 // Without a positive OCSP response, we shouldn't show the EV status.
7654 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7655 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
7656}
[email protected]a13234c2012-03-20 21:45:027657
[email protected]3a86a712013-07-30 07:16:207658class HTTPSHardFailTest : public HTTPSOCSPTest {
7659 protected:
dchengb03027d2014-10-21 12:00:207660 void SetupContext(URLRequestContext* context) override {
[email protected]3a86a712013-07-30 07:16:207661 context->set_ssl_config_service(
7662 new TestSSLConfigService(false /* check for EV */,
7663 false /* online revocation checking */,
7664 true /* require rev. checking for local
7665 anchors */));
7666 }
7667};
7668
7669
7670TEST_F(HTTPSHardFailTest, FailsOnOCSPInvalid) {
7671 if (!SystemSupportsOCSP()) {
7672 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7673 return;
7674 }
7675
7676 if (!SystemSupportsHardFailRevocationChecking()) {
7677 LOG(WARNING) << "Skipping test because system doesn't support hard fail "
7678 << "revocation checking";
7679 return;
7680 }
7681
7682 SpawnedTestServer::SSLOptions ssl_options(
7683 SpawnedTestServer::SSLOptions::CERT_AUTO);
7684 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
7685
7686 CertStatus cert_status;
7687 DoConnection(ssl_options, &cert_status);
7688
7689 EXPECT_EQ(CERT_STATUS_REVOKED,
7690 cert_status & CERT_STATUS_REVOKED);
7691
7692 // Without a positive OCSP response, we shouldn't show the EV status.
7693 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
7694}
7695
[email protected]a13234c2012-03-20 21:45:027696class HTTPSEVCRLSetTest : public HTTPSOCSPTest {
7697 protected:
dchengb03027d2014-10-21 12:00:207698 void SetupContext(URLRequestContext* context) override {
[email protected]a13234c2012-03-20 21:45:027699 context->set_ssl_config_service(
7700 new TestSSLConfigService(true /* check for EV */,
[email protected]3a86a712013-07-30 07:16:207701 false /* online revocation checking */,
7702 false /* require rev. checking for local
7703 anchors */));
[email protected]a13234c2012-03-20 21:45:027704 }
7705};
7706
7707TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndInvalidOCSP) {
7708 if (!SystemSupportsOCSP()) {
7709 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7710 return;
7711 }
7712
[email protected]ce7d0cbc2013-05-03 18:57:227713 SpawnedTestServer::SSLOptions ssl_options(
7714 SpawnedTestServer::SSLOptions::CERT_AUTO);
7715 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
[email protected]a13234c2012-03-20 21:45:027716 SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>());
7717
[email protected]924e9f92012-12-16 22:00:537718 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:127719 DoConnection(ssl_options, &cert_status);
[email protected]a13234c2012-03-20 21:45:027720
7721 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
7722 cert_status & CERT_STATUS_ALL_ERRORS);
7723
7724 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
[email protected]b6f2de32012-08-17 04:35:087725 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7726 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
[email protected]a13234c2012-03-20 21:45:027727}
7728
[email protected]be0fff62013-08-29 23:37:487729TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndRevokedOCSP) {
7730 if (!SystemSupportsOCSP()) {
7731 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7732 return;
7733 }
7734
7735 SpawnedTestServer::SSLOptions ssl_options(
7736 SpawnedTestServer::SSLOptions::CERT_AUTO);
7737 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED;
7738 SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>());
7739
7740 CertStatus cert_status;
7741 DoConnection(ssl_options, &cert_status);
7742
7743 // Currently only works for Windows. When using NSS or OS X, it's not
7744 // possible to determine whether the check failed because of actual
7745 // revocation or because there was an OCSP failure.
7746#if defined(OS_WIN)
7747 EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS);
7748#else
7749 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
7750#endif
7751
7752 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7753 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7754 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
7755}
7756
[email protected]a13234c2012-03-20 21:45:027757TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndGoodOCSP) {
7758 if (!SystemSupportsOCSP()) {
7759 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7760 return;
7761 }
7762
[email protected]ce7d0cbc2013-05-03 18:57:227763 SpawnedTestServer::SSLOptions ssl_options(
7764 SpawnedTestServer::SSLOptions::CERT_AUTO);
7765 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK;
[email protected]a13234c2012-03-20 21:45:027766 SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>());
7767
7768 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:127769 DoConnection(ssl_options, &cert_status);
[email protected]a13234c2012-03-20 21:45:027770
7771 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
7772
7773 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7774 static_cast<bool>(cert_status & CERT_STATUS_IS_EV));
[email protected]b6f2de32012-08-17 04:35:087775 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7776 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
[email protected]a13234c2012-03-20 21:45:027777}
7778
7779TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSet) {
7780 if (!SystemSupportsOCSP()) {
7781 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7782 return;
7783 }
7784
[email protected]ce7d0cbc2013-05-03 18:57:227785 SpawnedTestServer::SSLOptions ssl_options(
7786 SpawnedTestServer::SSLOptions::CERT_AUTO);
7787 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
[email protected]a13234c2012-03-20 21:45:027788 SSLConfigService::SetCRLSet(
7789 scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting()));
7790
7791 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:127792 DoConnection(ssl_options, &cert_status);
[email protected]a13234c2012-03-20 21:45:027793
7794 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
7795 cert_status & CERT_STATUS_ALL_ERRORS);
7796
7797 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
[email protected]b6f2de32012-08-17 04:35:087798 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7799 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
[email protected]a13234c2012-03-20 21:45:027800}
7801
[email protected]51523f52013-07-31 21:57:287802TEST_F(HTTPSEVCRLSetTest, FreshCRLSetCovered) {
7803 if (!SystemSupportsOCSP()) {
7804 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7805 return;
7806 }
7807
7808 SpawnedTestServer::SSLOptions ssl_options(
7809 SpawnedTestServer::SSLOptions::CERT_AUTO);
7810 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
7811 SSLConfigService::SetCRLSet(
7812 scoped_refptr<CRLSet>(CRLSet::ForTesting(
7813 false, &kOCSPTestCertSPKI, "")));
7814
7815 CertStatus cert_status;
7816 DoConnection(ssl_options, &cert_status);
7817
7818 // With a fresh CRLSet that covers the issuing certificate, we shouldn't do a
7819 // revocation check for EV.
7820 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
7821 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7822 static_cast<bool>(cert_status & CERT_STATUS_IS_EV));
7823 EXPECT_FALSE(
7824 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
7825}
7826
7827TEST_F(HTTPSEVCRLSetTest, FreshCRLSetNotCovered) {
7828 if (!SystemSupportsOCSP()) {
7829 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7830 return;
7831 }
7832
[email protected]ce7d0cbc2013-05-03 18:57:227833 SpawnedTestServer::SSLOptions ssl_options(
7834 SpawnedTestServer::SSLOptions::CERT_AUTO);
7835 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
[email protected]a13234c2012-03-20 21:45:027836 SSLConfigService::SetCRLSet(
7837 scoped_refptr<CRLSet>(CRLSet::EmptyCRLSetForTesting()));
7838
[email protected]51523f52013-07-31 21:57:287839 CertStatus cert_status = 0;
[email protected]295e5cd2012-08-23 01:05:127840 DoConnection(ssl_options, &cert_status);
[email protected]a13234c2012-03-20 21:45:027841
[email protected]51523f52013-07-31 21:57:287842 // Even with a fresh CRLSet, we should still do online revocation checks when
7843 // the certificate chain isn't covered by the CRLSet, which it isn't in this
7844 // test.
7845 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
7846 cert_status & CERT_STATUS_ALL_ERRORS);
[email protected]a13234c2012-03-20 21:45:027847
[email protected]51523f52013-07-31 21:57:287848 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
[email protected]a13234c2012-03-20 21:45:027849 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
[email protected]51523f52013-07-31 21:57:287850 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
[email protected]a13234c2012-03-20 21:45:027851}
7852
[email protected]b6f2de32012-08-17 04:35:087853TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSetAndRevokedNonEVCert) {
7854 // Test that when EV verification is requested, but online revocation
7855 // checking is disabled, and the leaf certificate is not in fact EV, that
7856 // no revocation checking actually happens.
7857 if (!SystemSupportsOCSP()) {
7858 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7859 return;
7860 }
7861
7862 // Unmark the certificate's OID as EV, which should disable revocation
7863 // checking (as per the user preference)
7864 ev_test_policy_.reset();
7865
[email protected]ce7d0cbc2013-05-03 18:57:227866 SpawnedTestServer::SSLOptions ssl_options(
7867 SpawnedTestServer::SSLOptions::CERT_AUTO);
7868 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED;
[email protected]b6f2de32012-08-17 04:35:087869 SSLConfigService::SetCRLSet(
7870 scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting()));
7871
7872 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:127873 DoConnection(ssl_options, &cert_status);
[email protected]b6f2de32012-08-17 04:35:087874
7875 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
7876
7877 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7878 EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
7879}
7880
[email protected]a13234c2012-03-20 21:45:027881class HTTPSCRLSetTest : public HTTPSOCSPTest {
7882 protected:
dchengb03027d2014-10-21 12:00:207883 void SetupContext(URLRequestContext* context) override {
[email protected]a13234c2012-03-20 21:45:027884 context->set_ssl_config_service(
7885 new TestSSLConfigService(false /* check for EV */,
[email protected]3a86a712013-07-30 07:16:207886 false /* online revocation checking */,
7887 false /* require rev. checking for local
7888 anchors */));
[email protected]a13234c2012-03-20 21:45:027889 }
7890};
7891
7892TEST_F(HTTPSCRLSetTest, ExpiredCRLSet) {
[email protected]ce7d0cbc2013-05-03 18:57:227893 SpawnedTestServer::SSLOptions ssl_options(
7894 SpawnedTestServer::SSLOptions::CERT_AUTO);
7895 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
[email protected]a13234c2012-03-20 21:45:027896 SSLConfigService::SetCRLSet(
7897 scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting()));
7898
[email protected]924e9f92012-12-16 22:00:537899 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:127900 DoConnection(ssl_options, &cert_status);
[email protected]a13234c2012-03-20 21:45:027901
7902 // If we're not trying EV verification then, even if the CRLSet has expired,
7903 // we don't fall back to online revocation checks.
7904 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
7905 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7906 EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
7907}
[email protected]51523f52013-07-31 21:57:287908
7909TEST_F(HTTPSCRLSetTest, CRLSetRevoked) {
[email protected]591cffcd2014-08-18 20:02:307910#if defined(OS_ANDROID)
[email protected]51523f52013-07-31 21:57:287911 LOG(WARNING) << "Skipping test because system doesn't support CRLSets";
7912 return;
7913#endif
7914
7915 SpawnedTestServer::SSLOptions ssl_options(
7916 SpawnedTestServer::SSLOptions::CERT_AUTO);
7917 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK;
7918 ssl_options.cert_serial = 10;
7919 SSLConfigService::SetCRLSet(
7920 scoped_refptr<CRLSet>(CRLSet::ForTesting(
7921 false, &kOCSPTestCertSPKI, "\x0a")));
7922
7923 CertStatus cert_status = 0;
7924 DoConnection(ssl_options, &cert_status);
7925
7926 // If the certificate is recorded as revoked in the CRLSet, that should be
7927 // reflected without online revocation checking.
7928 EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS);
7929 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7930 EXPECT_FALSE(
7931 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
7932}
[email protected]316c1e5e2012-09-12 15:17:447933#endif // !defined(OS_IOS)
[email protected]dffe8242012-03-20 15:14:277934
[email protected]316c1e5e2012-09-12 15:17:447935#if !defined(DISABLE_FTP_SUPPORT)
[email protected]b89ca032009-08-31 21:41:317936class URLRequestTestFTP : public URLRequestTest {
[email protected]95409e12010-08-17 20:07:117937 public:
[email protected]d9fca99a2012-02-24 16:16:207938 URLRequestTestFTP()
[email protected]ce7d0cbc2013-05-03 18:57:227939 : test_server_(SpawnedTestServer::TYPE_FTP, SpawnedTestServer::kLocalhost,
[email protected]6cdfd7f2013-02-08 20:40:157940 base::FilePath()) {
[email protected]95409e12010-08-17 20:07:117941 }
7942
[email protected]b89ca032009-08-31 21:41:317943 protected:
[email protected]ce7d0cbc2013-05-03 18:57:227944 SpawnedTestServer test_server_;
[email protected]b89ca032009-08-31 21:41:317945};
7946
[email protected]d2a133182012-08-05 16:44:087947// Make sure an FTP request using an unsafe ports fails.
[email protected]f2f31b32013-01-16 23:24:097948TEST_F(URLRequestTestFTP, UnsafePort) {
[email protected]d2a133182012-08-05 16:44:087949 ASSERT_TRUE(test_server_.Start());
7950
[email protected]9d5730b2012-08-24 17:42:497951 URLRequestJobFactoryImpl job_factory;
[email protected]e0f35c92013-05-08 16:04:347952 FtpNetworkLayer ftp_transaction_factory(default_context_.host_resolver());
[email protected]d2a133182012-08-05 16:44:087953
7954 GURL url("ftp://127.0.0.1:7");
[email protected]d2a133182012-08-05 16:44:087955 job_factory.SetProtocolHandler(
7956 "ftp",
[email protected]e0f35c92013-05-08 16:04:347957 new FtpProtocolHandler(&ftp_transaction_factory));
[email protected]d2a133182012-08-05 16:44:087958 default_context_.set_job_factory(&job_factory);
7959
7960 TestDelegate d;
7961 {
[email protected]f7022f32014-08-21 16:32:197962 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
7963 url, DEFAULT_PRIORITY, &d, NULL));
7964 r->Start();
7965 EXPECT_TRUE(r->is_pending());
[email protected]d2a133182012-08-05 16:44:087966
[email protected]255620da2013-08-19 13:14:297967 base::RunLoop().Run();
[email protected]d2a133182012-08-05 16:44:087968
[email protected]f7022f32014-08-21 16:32:197969 EXPECT_FALSE(r->is_pending());
7970 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
7971 EXPECT_EQ(ERR_UNSAFE_PORT, r->status().error());
[email protected]d2a133182012-08-05 16:44:087972 }
7973}
7974
[email protected]5accf7332009-11-24 03:41:387975// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:127976TEST_F(URLRequestTestFTP, DISABLED_FTPDirectoryListing) {
[email protected]95409e12010-08-17 20:07:117977 ASSERT_TRUE(test_server_.Start());
7978
[email protected]a25e90e2009-09-09 17:05:377979 TestDelegate d;
7980 {
[email protected]f7022f32014-08-21 16:32:197981 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
7982 test_server_.GetURL("/"), DEFAULT_PRIORITY, &d, NULL));
7983 r->Start();
7984 EXPECT_TRUE(r->is_pending());
[email protected]a25e90e2009-09-09 17:05:377985
[email protected]255620da2013-08-19 13:14:297986 base::RunLoop().Run();
[email protected]a25e90e2009-09-09 17:05:377987
[email protected]f7022f32014-08-21 16:32:197988 EXPECT_FALSE(r->is_pending());
[email protected]a25e90e2009-09-09 17:05:377989 EXPECT_EQ(1, d.response_started_count());
7990 EXPECT_FALSE(d.received_data_before_response());
7991 EXPECT_LT(0, d.bytes_received());
[email protected]6d81b482011-02-22 19:47:197992 EXPECT_EQ(test_server_.host_port_pair().host(),
[email protected]f7022f32014-08-21 16:32:197993 r->GetSocketAddress().host());
[email protected]6d81b482011-02-22 19:47:197994 EXPECT_EQ(test_server_.host_port_pair().port(),
[email protected]f7022f32014-08-21 16:32:197995 r->GetSocketAddress().port());
[email protected]a25e90e2009-09-09 17:05:377996 }
7997}
7998
[email protected]7df70012010-02-04 00:09:557999// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:128000TEST_F(URLRequestTestFTP, DISABLED_FTPGetTestAnonymous) {
[email protected]95409e12010-08-17 20:07:118001 ASSERT_TRUE(test_server_.Start());
8002
[email protected]6cdfd7f2013-02-08 20:40:158003 base::FilePath app_path;
[email protected]dd265012009-01-08 20:45:278004 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:028005 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:278006 TestDelegate d;
8007 {
[email protected]f7022f32014-08-21 16:32:198008 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
8009 test_server_.GetURL("/LICENSE"), DEFAULT_PRIORITY, &d, NULL));
8010 r->Start();
8011 EXPECT_TRUE(r->is_pending());
[email protected]dd265012009-01-08 20:45:278012
[email protected]255620da2013-08-19 13:14:298013 base::RunLoop().Run();
[email protected]dd265012009-01-08 20:45:278014
8015 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:498016 base::GetFileSize(app_path, &file_size);
[email protected]dd265012009-01-08 20:45:278017
[email protected]f7022f32014-08-21 16:32:198018 EXPECT_FALSE(r->is_pending());
[email protected]dd265012009-01-08 20:45:278019 EXPECT_EQ(1, d.response_started_count());
8020 EXPECT_FALSE(d.received_data_before_response());
8021 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
[email protected]6d81b482011-02-22 19:47:198022 EXPECT_EQ(test_server_.host_port_pair().host(),
[email protected]f7022f32014-08-21 16:32:198023 r->GetSocketAddress().host());
[email protected]6d81b482011-02-22 19:47:198024 EXPECT_EQ(test_server_.host_port_pair().port(),
[email protected]f7022f32014-08-21 16:32:198025 r->GetSocketAddress().port());
[email protected]dd265012009-01-08 20:45:278026 }
8027}
8028
[email protected]e9ecbd12009-12-20 18:44:408029// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:128030TEST_F(URLRequestTestFTP, DISABLED_FTPGetTest) {
[email protected]95409e12010-08-17 20:07:118031 ASSERT_TRUE(test_server_.Start());
8032
[email protected]6cdfd7f2013-02-08 20:40:158033 base::FilePath app_path;
[email protected]dd265012009-01-08 20:45:278034 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:028035 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:278036 TestDelegate d;
8037 {
[email protected]f7022f32014-08-21 16:32:198038 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
[email protected]95409e12010-08-17 20:07:118039 test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", "chrome"),
[email protected]f7022f32014-08-21 16:32:198040 DEFAULT_PRIORITY, &d, NULL));
8041 r->Start();
8042 EXPECT_TRUE(r->is_pending());
[email protected]dd265012009-01-08 20:45:278043
[email protected]255620da2013-08-19 13:14:298044 base::RunLoop().Run();
[email protected]dd265012009-01-08 20:45:278045
8046 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:498047 base::GetFileSize(app_path, &file_size);
[email protected]dd265012009-01-08 20:45:278048
[email protected]f7022f32014-08-21 16:32:198049 EXPECT_FALSE(r->is_pending());
[email protected]6d81b482011-02-22 19:47:198050 EXPECT_EQ(test_server_.host_port_pair().host(),
[email protected]f7022f32014-08-21 16:32:198051 r->GetSocketAddress().host());
[email protected]6d81b482011-02-22 19:47:198052 EXPECT_EQ(test_server_.host_port_pair().port(),
[email protected]f7022f32014-08-21 16:32:198053 r->GetSocketAddress().port());
[email protected]dd265012009-01-08 20:45:278054 EXPECT_EQ(1, d.response_started_count());
8055 EXPECT_FALSE(d.received_data_before_response());
8056 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
[email protected]58e32bb2013-01-21 18:23:258057
8058 LoadTimingInfo load_timing_info;
[email protected]f7022f32014-08-21 16:32:198059 r->GetLoadTimingInfo(&load_timing_info);
[email protected]3b23a222013-05-15 21:33:258060 TestLoadTimingNoHttpResponse(load_timing_info);
[email protected]dd265012009-01-08 20:45:278061 }
8062}
8063
[email protected]49abd652010-08-05 05:04:538064// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:128065TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPassword) {
[email protected]95409e12010-08-17 20:07:118066 ASSERT_TRUE(test_server_.Start());
8067
[email protected]6cdfd7f2013-02-08 20:40:158068 base::FilePath app_path;
[email protected]dd265012009-01-08 20:45:278069 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:028070 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:278071 TestDelegate d;
8072 {
[email protected]f7022f32014-08-21 16:32:198073 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
8074 test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome",
8075 "wrong_password"),
8076 DEFAULT_PRIORITY, &d, NULL));
8077 r->Start();
8078 EXPECT_TRUE(r->is_pending());
[email protected]dd265012009-01-08 20:45:278079
[email protected]255620da2013-08-19 13:14:298080 base::RunLoop().Run();
[email protected]dd265012009-01-08 20:45:278081
8082 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:498083 base::GetFileSize(app_path, &file_size);
[email protected]dd265012009-01-08 20:45:278084
[email protected]f7022f32014-08-21 16:32:198085 EXPECT_FALSE(r->is_pending());
[email protected]dd265012009-01-08 20:45:278086 EXPECT_EQ(1, d.response_started_count());
8087 EXPECT_FALSE(d.received_data_before_response());
8088 EXPECT_EQ(d.bytes_received(), 0);
8089 }
8090}
8091
[email protected]cde4e80d2009-10-16 19:58:158092// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:128093TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPasswordRestart) {
[email protected]95409e12010-08-17 20:07:118094 ASSERT_TRUE(test_server_.Start());
8095
[email protected]6cdfd7f2013-02-08 20:40:158096 base::FilePath app_path;
[email protected]8b8a197d2009-08-26 15:57:588097 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
8098 app_path = app_path.AppendASCII("LICENSE");
8099 TestDelegate d;
8100 // Set correct login credentials. The delegate will be asked for them when
8101 // the initial login with wrong credentials will fail.
[email protected]f3cf9802011-10-28 18:44:588102 d.set_credentials(AuthCredentials(kChrome, kChrome));
[email protected]8b8a197d2009-08-26 15:57:588103 {
[email protected]f7022f32014-08-21 16:32:198104 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
8105 test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome",
8106 "wrong_password"),
8107 DEFAULT_PRIORITY, &d, NULL));
8108 r->Start();
8109 EXPECT_TRUE(r->is_pending());
[email protected]8b8a197d2009-08-26 15:57:588110
[email protected]255620da2013-08-19 13:14:298111 base::RunLoop().Run();
[email protected]8b8a197d2009-08-26 15:57:588112
8113 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:498114 base::GetFileSize(app_path, &file_size);
[email protected]8b8a197d2009-08-26 15:57:588115
[email protected]f7022f32014-08-21 16:32:198116 EXPECT_FALSE(r->is_pending());
[email protected]8b8a197d2009-08-26 15:57:588117 EXPECT_EQ(1, d.response_started_count());
8118 EXPECT_FALSE(d.received_data_before_response());
8119 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
8120 }
8121}
8122
[email protected]49abd652010-08-05 05:04:538123// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:128124TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUser) {
[email protected]95409e12010-08-17 20:07:118125 ASSERT_TRUE(test_server_.Start());
8126
[email protected]6cdfd7f2013-02-08 20:40:158127 base::FilePath app_path;
[email protected]dd265012009-01-08 20:45:278128 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:028129 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:278130 TestDelegate d;
8131 {
[email protected]f7022f32014-08-21 16:32:198132 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
8133 test_server_.GetURLWithUserAndPassword("/LICENSE", "wrong_user",
8134 "chrome"),
8135 DEFAULT_PRIORITY, &d, NULL));
8136 r->Start();
8137 EXPECT_TRUE(r->is_pending());
[email protected]dd265012009-01-08 20:45:278138
[email protected]255620da2013-08-19 13:14:298139 base::RunLoop().Run();
[email protected]dd265012009-01-08 20:45:278140
8141 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:498142 base::GetFileSize(app_path, &file_size);
[email protected]dd265012009-01-08 20:45:278143
[email protected]f7022f32014-08-21 16:32:198144 EXPECT_FALSE(r->is_pending());
[email protected]dd265012009-01-08 20:45:278145 EXPECT_EQ(1, d.response_started_count());
8146 EXPECT_FALSE(d.received_data_before_response());
8147 EXPECT_EQ(d.bytes_received(), 0);
8148 }
8149}
[email protected]8b8a197d2009-08-26 15:57:588150
[email protected]cde4e80d2009-10-16 19:58:158151// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:128152TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUserRestart) {
[email protected]95409e12010-08-17 20:07:118153 ASSERT_TRUE(test_server_.Start());
8154
[email protected]6cdfd7f2013-02-08 20:40:158155 base::FilePath app_path;
[email protected]8b8a197d2009-08-26 15:57:588156 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
8157 app_path = app_path.AppendASCII("LICENSE");
8158 TestDelegate d;
8159 // Set correct login credentials. The delegate will be asked for them when
8160 // the initial login with wrong credentials will fail.
[email protected]f3cf9802011-10-28 18:44:588161 d.set_credentials(AuthCredentials(kChrome, kChrome));
[email protected]8b8a197d2009-08-26 15:57:588162 {
[email protected]f7022f32014-08-21 16:32:198163 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
8164 test_server_.GetURLWithUserAndPassword("/LICENSE", "wrong_user",
8165 "chrome"),
8166 DEFAULT_PRIORITY, &d, NULL));
8167 r->Start();
8168 EXPECT_TRUE(r->is_pending());
[email protected]8b8a197d2009-08-26 15:57:588169
[email protected]255620da2013-08-19 13:14:298170 base::RunLoop().Run();
[email protected]8b8a197d2009-08-26 15:57:588171
8172 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:498173 base::GetFileSize(app_path, &file_size);
[email protected]8b8a197d2009-08-26 15:57:588174
[email protected]f7022f32014-08-21 16:32:198175 EXPECT_FALSE(r->is_pending());
[email protected]8b8a197d2009-08-26 15:57:588176 EXPECT_EQ(1, d.response_started_count());
8177 EXPECT_FALSE(d.received_data_before_response());
8178 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
8179 }
8180}
[email protected]60a3df52009-09-22 16:13:248181
[email protected]cde4e80d2009-10-16 19:58:158182// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:128183TEST_F(URLRequestTestFTP, DISABLED_FTPCacheURLCredentials) {
[email protected]95409e12010-08-17 20:07:118184 ASSERT_TRUE(test_server_.Start());
8185
[email protected]6cdfd7f2013-02-08 20:40:158186 base::FilePath app_path;
[email protected]60a3df52009-09-22 16:13:248187 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
8188 app_path = app_path.AppendASCII("LICENSE");
8189
8190 scoped_ptr<TestDelegate> d(new TestDelegate);
8191 {
8192 // Pass correct login identity in the URL.
[email protected]f7022f32014-08-21 16:32:198193 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
[email protected]2ca01e52013-10-31 22:05:198194 test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", "chrome"),
[email protected]f7022f32014-08-21 16:32:198195 DEFAULT_PRIORITY, d.get(), NULL));
8196 r->Start();
8197 EXPECT_TRUE(r->is_pending());
[email protected]60a3df52009-09-22 16:13:248198
[email protected]255620da2013-08-19 13:14:298199 base::RunLoop().Run();
[email protected]60a3df52009-09-22 16:13:248200
8201 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:498202 base::GetFileSize(app_path, &file_size);
[email protected]60a3df52009-09-22 16:13:248203
[email protected]f7022f32014-08-21 16:32:198204 EXPECT_FALSE(r->is_pending());
[email protected]60a3df52009-09-22 16:13:248205 EXPECT_EQ(1, d->response_started_count());
8206 EXPECT_FALSE(d->received_data_before_response());
8207 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
8208 }
8209
8210 d.reset(new TestDelegate);
8211 {
8212 // This request should use cached identity from previous request.
[email protected]f7022f32014-08-21 16:32:198213 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
8214 test_server_.GetURL("/LICENSE"), DEFAULT_PRIORITY, d.get(), NULL));
8215 r->Start();
8216 EXPECT_TRUE(r->is_pending());
[email protected]60a3df52009-09-22 16:13:248217
[email protected]255620da2013-08-19 13:14:298218 base::RunLoop().Run();
[email protected]60a3df52009-09-22 16:13:248219
8220 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:498221 base::GetFileSize(app_path, &file_size);
[email protected]60a3df52009-09-22 16:13:248222
[email protected]f7022f32014-08-21 16:32:198223 EXPECT_FALSE(r->is_pending());
[email protected]60a3df52009-09-22 16:13:248224 EXPECT_EQ(1, d->response_started_count());
8225 EXPECT_FALSE(d->received_data_before_response());
8226 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
8227 }
8228}
8229
[email protected]cde4e80d2009-10-16 19:58:158230// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:128231TEST_F(URLRequestTestFTP, DISABLED_FTPCacheLoginBoxCredentials) {
[email protected]95409e12010-08-17 20:07:118232 ASSERT_TRUE(test_server_.Start());
8233
[email protected]6cdfd7f2013-02-08 20:40:158234 base::FilePath app_path;
[email protected]60a3df52009-09-22 16:13:248235 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
8236 app_path = app_path.AppendASCII("LICENSE");
8237
8238 scoped_ptr<TestDelegate> d(new TestDelegate);
8239 // Set correct login credentials. The delegate will be asked for them when
8240 // the initial login with wrong credentials will fail.
[email protected]f3cf9802011-10-28 18:44:588241 d->set_credentials(AuthCredentials(kChrome, kChrome));
[email protected]60a3df52009-09-22 16:13:248242 {
[email protected]f7022f32014-08-21 16:32:198243 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
8244 test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome",
8245 "wrong_password"),
8246 DEFAULT_PRIORITY, d.get(), NULL));
8247 r->Start();
8248 EXPECT_TRUE(r->is_pending());
[email protected]60a3df52009-09-22 16:13:248249
[email protected]255620da2013-08-19 13:14:298250 base::RunLoop().Run();
[email protected]60a3df52009-09-22 16:13:248251
8252 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:498253 base::GetFileSize(app_path, &file_size);
[email protected]60a3df52009-09-22 16:13:248254
[email protected]f7022f32014-08-21 16:32:198255 EXPECT_FALSE(r->is_pending());
[email protected]60a3df52009-09-22 16:13:248256 EXPECT_EQ(1, d->response_started_count());
8257 EXPECT_FALSE(d->received_data_before_response());
8258 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
8259 }
8260
8261 // Use a new delegate without explicit credentials. The cached ones should be
8262 // used.
8263 d.reset(new TestDelegate);
8264 {
8265 // Don't pass wrong credentials in the URL, they would override valid cached
8266 // ones.
[email protected]f7022f32014-08-21 16:32:198267 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
8268 test_server_.GetURL("/LICENSE"), DEFAULT_PRIORITY, d.get(), NULL));
8269 r->Start();
8270 EXPECT_TRUE(r->is_pending());
[email protected]60a3df52009-09-22 16:13:248271
[email protected]255620da2013-08-19 13:14:298272 base::RunLoop().Run();
[email protected]60a3df52009-09-22 16:13:248273
8274 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:498275 base::GetFileSize(app_path, &file_size);
[email protected]60a3df52009-09-22 16:13:248276
[email protected]f7022f32014-08-21 16:32:198277 EXPECT_FALSE(r->is_pending());
[email protected]60a3df52009-09-22 16:13:248278 EXPECT_EQ(1, d->response_started_count());
8279 EXPECT_FALSE(d->received_data_before_response());
8280 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
8281 }
8282}
[email protected]316c1e5e2012-09-12 15:17:448283#endif // !defined(DISABLE_FTP_SUPPORT)
[email protected]7461a402011-03-24 23:19:518284
8285} // namespace net