blob: 7e22546891da70a95f2daeb57614a7c41bdf7289 [file] [log] [blame]
[email protected]9045b8822012-01-13 20:35:351// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
[email protected]ea224582008-12-07 20:25:465#include "build/build_config.h"
6
[email protected]9396b252008-09-29 17:29:387#if defined(OS_WIN)
[email protected]d8eb84242010-09-25 02:25:068#include <windows.h>
[email protected]aa84a7e2012-03-15 21:29:069#include <shlobj.h>
[email protected]9396b252008-09-29 17:29:3810#endif
11
initial.commit586acc5fe2008-07-26 22:42:5212#include <algorithm>
13#include <string>
14
[email protected]aad63572011-05-24 20:14:3915#include "base/basictypes.h"
[email protected]218aa6a12011-09-13 17:38:3816#include "base/bind.h"
[email protected]8523ba52011-05-22 19:00:5817#include "base/compiler_specific.h"
[email protected]399b8702009-05-01 20:34:0218#include "base/file_util.h"
[email protected]3ca8b362013-11-11 22:18:0719#include "base/files/scoped_temp_dir.h"
[email protected]34b2b002009-11-20 06:53:2820#include "base/format_macros.h"
[email protected]084262c2011-12-01 21:12:4721#include "base/memory/weak_ptr.h"
[email protected]7f86564d2013-07-18 00:41:2222#include "base/message_loop/message_loop.h"
[email protected]255620da2013-08-19 13:14:2923#include "base/message_loop/message_loop_proxy.h"
initial.commit586acc5fe2008-07-26 22:42:5224#include "base/path_service.h"
[email protected]255620da2013-08-19 13:14:2925#include "base/run_loop.h"
[email protected]4dc3ad4f2013-06-11 07:15:5026#include "base/strings/string_number_conversions.h"
[email protected]d069c11a2013-04-13 00:01:5527#include "base/strings/string_piece.h"
[email protected]d778e0422013-03-06 18:10:2228#include "base/strings/string_split.h"
[email protected]7f86564d2013-07-18 00:41:2229#include "base/strings/string_util.h"
30#include "base/strings/stringprintf.h"
[email protected]750b2f3c2013-06-07 18:41:0531#include "base/strings/utf_string_conversions.h"
[email protected]58e32bb2013-01-21 18:23:2532#include "net/base/capturing_net_log.h"
initial.commit586acc5fe2008-07-26 22:42:5233#include "net/base/load_flags.h"
[email protected]58e32bb2013-01-21 18:23:2534#include "net/base/load_timing_info.h"
35#include "net/base/load_timing_info_test_util.h"
[email protected]d8eb84242010-09-25 02:25:0636#include "net/base/net_errors.h"
[email protected]9e743cd2010-03-16 07:03:5337#include "net/base/net_log.h"
38#include "net/base/net_log_unittest.h"
initial.commit586acc5fe2008-07-26 22:42:5239#include "net/base/net_module.h"
40#include "net/base/net_util.h"
[email protected]2ca01e52013-10-31 22:05:1941#include "net/base/request_priority.h"
[email protected]42fdb452012-11-01 12:44:4042#include "net/base/test_data_directory.h"
[email protected]f288ef02012-12-15 20:28:2843#include "net/base/upload_bytes_element_reader.h"
44#include "net/base/upload_data_stream.h"
45#include "net/base/upload_file_element_reader.h"
[email protected]6e7845ae2013-03-29 21:48:1146#include "net/cert/ev_root_ca_metadata.h"
[email protected]a8fed1742013-12-27 02:14:2447#include "net/cert/mock_cert_verifier.h"
[email protected]6e7845ae2013-03-29 21:48:1148#include "net/cert/test_root_certs.h"
[email protected]aa84a7e2012-03-15 21:29:0649#include "net/cookies/cookie_monster.h"
50#include "net/cookies/cookie_store_test_helpers.h"
initial.commit586acc5fe2008-07-26 22:42:5251#include "net/disk_cache/disk_cache.h"
[email protected]f2cb3cf2013-03-21 01:40:5352#include "net/dns/mock_host_resolver.h"
[email protected]ba2f3342009-07-30 18:08:4253#include "net/ftp/ftp_network_layer.h"
[email protected]b7572ea2013-11-26 20:16:3854#include "net/http/http_byte_range.h"
initial.commit586acc5fe2008-07-26 22:42:5255#include "net/http/http_cache.h"
56#include "net/http/http_network_layer.h"
[email protected]c3456bb2011-12-12 22:22:1957#include "net/http/http_network_session.h"
[email protected]88e6b6f32010-05-07 23:14:2558#include "net/http/http_request_headers.h"
[email protected]319d9e6f2009-02-18 19:47:2159#include "net/http/http_response_headers.h"
[email protected]e50efea2014-03-24 18:41:0060#include "net/http/http_util.h"
[email protected]dffe8242012-03-20 15:14:2761#include "net/ocsp/nss_ocsp.h"
[email protected]63de95b2008-12-10 04:11:2762#include "net/proxy/proxy_service.h"
[email protected]c3456bb2011-12-12 22:22:1963#include "net/socket/ssl_client_socket.h"
[email protected]536fd0b2013-03-14 17:41:5764#include "net/ssl/ssl_connection_status_flags.h"
[email protected]6e7845ae2013-03-29 21:48:1165#include "net/test/cert_test_util.h"
[email protected]89b32522013-05-07 20:04:2166#include "net/test/spawned_test_server/spawned_test_server.h"
[email protected]e0f35c92013-05-08 16:04:3467#include "net/url_request/data_protocol_handler.h"
[email protected]ee4c30d2012-11-07 15:08:4368#include "net/url_request/static_http_user_agent_settings.h"
initial.commit586acc5fe2008-07-26 22:42:5269#include "net/url_request/url_request.h"
[email protected]bcb84f8b2009-08-31 16:20:1470#include "net/url_request/url_request_http_job.h"
[email protected]9d5730b2012-08-24 17:42:4971#include "net/url_request/url_request_job_factory_impl.h"
[email protected]3c5ca8c2011-09-29 01:14:5172#include "net/url_request/url_request_redirect_job.h"
[email protected]a5c713f2009-04-16 21:05:4773#include "net/url_request/url_request_test_job.h"
[email protected]d2db0292011-01-26 20:23:4474#include "net/url_request/url_request_test_util.h"
initial.commit586acc5fe2008-07-26 22:42:5275#include "testing/gtest/include/gtest/gtest.h"
[email protected]23887f04f2008-12-02 19:20:1576#include "testing/platform_test.h"
initial.commit586acc5fe2008-07-26 22:42:5277
[email protected]02494ec2014-05-07 15:05:2978#if !defined(DISABLE_FILE_SUPPORT)
[email protected]5ecf7cb282014-05-11 01:49:5579#include "net/base/filename_util.h"
[email protected]02494ec2014-05-07 15:05:2980#include "net/url_request/file_protocol_handler.h"
81#include "net/url_request/url_request_file_dir_job.h"
82#endif
83
84#if !defined(DISABLE_FTP_SUPPORT)
85#include "net/url_request/ftp_protocol_handler.h"
86#endif
87
[email protected]dffe8242012-03-20 15:14:2788#if defined(OS_WIN)
[email protected]451fd902012-10-03 17:14:4889#include "base/win/scoped_com_initializer.h"
[email protected]aed9efb2013-04-13 01:20:5690#include "base/win/scoped_comptr.h"
[email protected]dffe8242012-03-20 15:14:2791#include "base/win/windows_version.h"
92#endif
93
[email protected]ad65a3e2013-12-25 18:18:0194using base::ASCIIToUTF16;
[email protected]e1acf6f2008-10-27 20:43:3395using base::Time;
96
[email protected]7461a402011-03-24 23:19:5197namespace net {
98
initial.commit586acc5fe2008-07-26 22:42:5299namespace {
100
[email protected]42cba2fb2013-03-29 19:58:57101const base::string16 kChrome(ASCIIToUTF16("chrome"));
102const base::string16 kSecret(ASCIIToUTF16("secret"));
103const base::string16 kUser(ASCIIToUTF16("user"));
[email protected]13c8a092010-07-29 06:15:44104
[email protected]2bba3252013-04-08 19:50:59105// Tests load timing information in the case a fresh connection was used, with
106// no proxy.
[email protected]cba24642014-08-15 20:49:59107void TestLoadTimingNotReused(const LoadTimingInfo& load_timing_info,
[email protected]58e32bb2013-01-21 18:23:25108 int connect_timing_flags) {
109 EXPECT_FALSE(load_timing_info.socket_reused);
[email protected]cba24642014-08-15 20:49:59110 EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
[email protected]58e32bb2013-01-21 18:23:25111
112 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
113 EXPECT_FALSE(load_timing_info.request_start.is_null());
114
115 EXPECT_LE(load_timing_info.request_start,
116 load_timing_info.connect_timing.connect_start);
117 ExpectConnectTimingHasTimes(load_timing_info.connect_timing,
118 connect_timing_flags);
119 EXPECT_LE(load_timing_info.connect_timing.connect_end,
120 load_timing_info.send_start);
121 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
122 EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
123
[email protected]58e32bb2013-01-21 18:23:25124 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null());
125 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null());
126}
127
[email protected]2bba3252013-04-08 19:50:59128// Same as above, but with proxy times.
129void TestLoadTimingNotReusedWithProxy(
[email protected]cba24642014-08-15 20:49:59130 const LoadTimingInfo& load_timing_info,
[email protected]2bba3252013-04-08 19:50:59131 int connect_timing_flags) {
132 EXPECT_FALSE(load_timing_info.socket_reused);
[email protected]cba24642014-08-15 20:49:59133 EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
[email protected]2bba3252013-04-08 19:50:59134
135 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
136 EXPECT_FALSE(load_timing_info.request_start.is_null());
137
138 EXPECT_LE(load_timing_info.request_start,
139 load_timing_info.proxy_resolve_start);
140 EXPECT_LE(load_timing_info.proxy_resolve_start,
141 load_timing_info.proxy_resolve_end);
142 EXPECT_LE(load_timing_info.proxy_resolve_end,
143 load_timing_info.connect_timing.connect_start);
144 ExpectConnectTimingHasTimes(load_timing_info.connect_timing,
145 connect_timing_flags);
146 EXPECT_LE(load_timing_info.connect_timing.connect_end,
147 load_timing_info.send_start);
148 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
149 EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
150}
151
152// Same as above, but with a reused socket and proxy times.
153void TestLoadTimingReusedWithProxy(
[email protected]cba24642014-08-15 20:49:59154 const LoadTimingInfo& load_timing_info) {
[email protected]2bba3252013-04-08 19:50:59155 EXPECT_TRUE(load_timing_info.socket_reused);
[email protected]cba24642014-08-15 20:49:59156 EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
[email protected]2bba3252013-04-08 19:50:59157
158 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
159 EXPECT_FALSE(load_timing_info.request_start.is_null());
160
161 ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing);
162
163 EXPECT_LE(load_timing_info.request_start,
164 load_timing_info.proxy_resolve_start);
165 EXPECT_LE(load_timing_info.proxy_resolve_start,
166 load_timing_info.proxy_resolve_end);
167 EXPECT_LE(load_timing_info.proxy_resolve_end,
168 load_timing_info.send_start);
169 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
170 EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
171}
172
[email protected]3b23a222013-05-15 21:33:25173// Tests load timing information in the case of a cache hit, when no cache
174// validation request was sent over the wire.
[email protected]e3a85452013-11-14 01:46:17175base::StringPiece TestNetResourceProvider(int key) {
176 return "header";
177}
178
179void FillBuffer(char* buffer, size_t len) {
180 static bool called = false;
181 if (!called) {
182 called = true;
183 int seed = static_cast<int>(Time::Now().ToInternalValue());
184 srand(seed);
185 }
186
187 for (size_t i = 0; i < len; i++) {
188 buffer[i] = static_cast<char>(rand());
189 if (!buffer[i])
190 buffer[i] = 'g';
191 }
192}
193
194#if !defined(OS_IOS)
[email protected]3b23a222013-05-15 21:33:25195void TestLoadTimingCacheHitNoNetwork(
[email protected]cba24642014-08-15 20:49:59196 const LoadTimingInfo& load_timing_info) {
[email protected]3b23a222013-05-15 21:33:25197 EXPECT_FALSE(load_timing_info.socket_reused);
[email protected]cba24642014-08-15 20:49:59198 EXPECT_EQ(NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
[email protected]3b23a222013-05-15 21:33:25199
200 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
201 EXPECT_FALSE(load_timing_info.request_start.is_null());
202
203 ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing);
204 EXPECT_LE(load_timing_info.request_start, load_timing_info.send_start);
205 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
206 EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
207
208 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null());
209 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null());
210}
211
212// Tests load timing in the case that there is no HTTP response. This can be
213// used to test in the case of errors or non-HTTP requests.
214void TestLoadTimingNoHttpResponse(
[email protected]cba24642014-08-15 20:49:59215 const LoadTimingInfo& load_timing_info) {
[email protected]58e32bb2013-01-21 18:23:25216 EXPECT_FALSE(load_timing_info.socket_reused);
[email protected]cba24642014-08-15 20:49:59217 EXPECT_EQ(NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
[email protected]58e32bb2013-01-21 18:23:25218
219 // Only the request times should be non-null.
220 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
221 EXPECT_FALSE(load_timing_info.request_start.is_null());
222
223 ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing);
224
225 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null());
226 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null());
227 EXPECT_TRUE(load_timing_info.send_start.is_null());
228 EXPECT_TRUE(load_timing_info.send_end.is_null());
229 EXPECT_TRUE(load_timing_info.receive_headers_end.is_null());
230}
231
[email protected]71c64f62008-11-15 04:36:51232// Do a case-insensitive search through |haystack| for |needle|.
233bool ContainsString(const std::string& haystack, const char* needle) {
234 std::string::const_iterator it =
235 std::search(haystack.begin(),
236 haystack.end(),
237 needle,
238 needle + strlen(needle),
[email protected]07f1cee2010-11-03 03:53:35239 base::CaseInsensitiveCompare<char>());
[email protected]71c64f62008-11-15 04:36:51240 return it != haystack.end();
241}
242
[email protected]f288ef02012-12-15 20:28:28243UploadDataStream* CreateSimpleUploadData(const char* data) {
244 scoped_ptr<UploadElementReader> reader(
245 new UploadBytesElementReader(data, strlen(data)));
246 return UploadDataStream::CreateWithReader(reader.Pass(), 0);
[email protected]195e77d2009-07-23 19:10:23247}
248
[email protected]96adadb2010-08-28 01:16:17249// Verify that the SSLInfo of a successful SSL connection has valid values.
[email protected]7461a402011-03-24 23:19:51250void CheckSSLInfo(const SSLInfo& ssl_info) {
[email protected]96adadb2010-08-28 01:16:17251 // -1 means unknown. 0 means no encryption.
252 EXPECT_GT(ssl_info.security_bits, 0);
253
254 // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated.
[email protected]7461a402011-03-24 23:19:51255 int cipher_suite = SSLConnectionStatusToCipherSuite(
[email protected]96adadb2010-08-28 01:16:17256 ssl_info.connection_status);
257 EXPECT_NE(0, cipher_suite);
258}
259
[email protected]79e1fd62013-06-20 06:50:04260void CheckFullRequestHeaders(const HttpRequestHeaders& headers,
261 const GURL& host_url) {
262 std::string sent_value;
263
264 EXPECT_TRUE(headers.GetHeader("Host", &sent_value));
265 EXPECT_EQ(GetHostAndOptionalPort(host_url), sent_value);
266
267 EXPECT_TRUE(headers.GetHeader("Connection", &sent_value));
268 EXPECT_EQ("keep-alive", sent_value);
269}
270
[email protected]ede03212012-09-07 12:52:26271bool FingerprintsEqual(const HashValueVector& a, const HashValueVector& b) {
[email protected]69d7ff442012-02-13 22:41:27272 size_t size = a.size();
273
274 if (size != b.size())
275 return false;
276
277 for (size_t i = 0; i < size; ++i) {
278 if (!a[i].Equals(b[i]))
279 return false;
280 }
281
282 return true;
283}
[email protected]e3a85452013-11-14 01:46:17284#endif // !defined(OS_IOS)
[email protected]69d7ff442012-02-13 22:41:27285
[email protected]dc5a5cf2012-09-26 02:49:30286// A network delegate that allows the user to choose a subset of request stages
287// to block in. When blocking, the delegate can do one of the following:
288// * synchronously return a pre-specified error code, or
289// * asynchronously return that value via an automatically called callback,
290// or
291// * block and wait for the user to do a callback.
292// Additionally, the user may also specify a redirect URL -- then each request
293// with the current URL different from the redirect target will be redirected
294// to that target, in the on-before-URL-request stage, independent of whether
295// the delegate blocks in ON_BEFORE_URL_REQUEST or not.
[email protected]4c76d7c2011-04-15 19:14:12296class BlockingNetworkDelegate : public TestNetworkDelegate {
297 public:
[email protected]dc5a5cf2012-09-26 02:49:30298 // Stages in which the delegate can block.
299 enum Stage {
[email protected]9045b8822012-01-13 20:35:35300 NOT_BLOCKED = 0,
301 ON_BEFORE_URL_REQUEST = 1 << 0,
302 ON_BEFORE_SEND_HEADERS = 1 << 1,
303 ON_HEADERS_RECEIVED = 1 << 2,
304 ON_AUTH_REQUIRED = 1 << 3
305 };
306
[email protected]dc5a5cf2012-09-26 02:49:30307 // Behavior during blocked stages. During other stages, just
[email protected]cba24642014-08-15 20:49:59308 // returns OK or NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION.
[email protected]dc5a5cf2012-09-26 02:49:30309 enum BlockMode {
310 SYNCHRONOUS, // No callback, returns specified return values.
311 AUTO_CALLBACK, // |this| posts a task to run the callback using the
312 // specified return codes.
313 USER_CALLBACK, // User takes care of doing a callback. |retval_| and
314 // |auth_retval_| are ignored. In every blocking stage the
315 // message loop is quit.
316 };
317
318 // Creates a delegate which does not block at all.
319 explicit BlockingNetworkDelegate(BlockMode block_mode);
320
321 // For users to trigger a callback returning |response|.
322 // Side-effects: resets |stage_blocked_for_callback_| and stored callbacks.
323 // Only call if |block_mode_| == USER_CALLBACK.
324 void DoCallback(int response);
325 void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response);
326
327 // Setters.
328 void set_retval(int retval) {
329 ASSERT_NE(USER_CALLBACK, block_mode_);
330 ASSERT_NE(ERR_IO_PENDING, retval);
331 ASSERT_NE(OK, retval);
332 retval_ = retval;
[email protected]9045b8822012-01-13 20:35:35333 }
334
[email protected]dc5a5cf2012-09-26 02:49:30335 // If |auth_retval| == AUTH_REQUIRED_RESPONSE_SET_AUTH, then
336 // |auth_credentials_| will be passed with the response.
337 void set_auth_retval(AuthRequiredResponse auth_retval) {
338 ASSERT_NE(USER_CALLBACK, block_mode_);
339 ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, auth_retval);
340 auth_retval_ = auth_retval;
341 }
342 void set_auth_credentials(const AuthCredentials& auth_credentials) {
343 auth_credentials_ = auth_credentials;
[email protected]9045b8822012-01-13 20:35:35344 }
345
[email protected]dc5a5cf2012-09-26 02:49:30346 void set_redirect_url(const GURL& url) {
347 redirect_url_ = url;
[email protected]9045b8822012-01-13 20:35:35348 }
349
[email protected]dc5a5cf2012-09-26 02:49:30350 void set_block_on(int block_on) {
351 block_on_ = block_on;
[email protected]9045b8822012-01-13 20:35:35352 }
353
[email protected]dc5a5cf2012-09-26 02:49:30354 // Allows the user to check in which state did we block.
355 Stage stage_blocked_for_callback() const {
356 EXPECT_EQ(USER_CALLBACK, block_mode_);
357 return stage_blocked_for_callback_;
[email protected]9045b8822012-01-13 20:35:35358 }
359
360 private:
[email protected]dc5a5cf2012-09-26 02:49:30361 void RunCallback(int response, const CompletionCallback& callback);
362 void RunAuthCallback(AuthRequiredResponse response,
363 const AuthCallback& callback);
364
[email protected]9045b8822012-01-13 20:35:35365 // TestNetworkDelegate implementation.
366 virtual int OnBeforeURLRequest(URLRequest* request,
367 const CompletionCallback& callback,
[email protected]dc5a5cf2012-09-26 02:49:30368 GURL* new_url) OVERRIDE;
[email protected]9045b8822012-01-13 20:35:35369
370 virtual int OnBeforeSendHeaders(URLRequest* request,
371 const CompletionCallback& callback,
[email protected]dc5a5cf2012-09-26 02:49:30372 HttpRequestHeaders* headers) OVERRIDE;
[email protected]9045b8822012-01-13 20:35:35373
374 virtual int OnHeadersReceived(
375 URLRequest* request,
376 const CompletionCallback& callback,
[email protected]507af8f2012-10-20 00:42:32377 const HttpResponseHeaders* original_response_headers,
[email protected]5f714132014-03-26 10:41:16378 scoped_refptr<HttpResponseHeaders>* override_response_headers,
379 GURL* allowed_unsafe_redirect_url) OVERRIDE;
[email protected]9045b8822012-01-13 20:35:35380
381 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired(
382 URLRequest* request,
383 const AuthChallengeInfo& auth_info,
384 const AuthCallback& callback,
[email protected]dc5a5cf2012-09-26 02:49:30385 AuthCredentials* credentials) OVERRIDE;
[email protected]9045b8822012-01-13 20:35:35386
[email protected]dc5a5cf2012-09-26 02:49:30387 // Resets the callbacks and |stage_blocked_for_callback_|.
388 void Reset();
[email protected]9045b8822012-01-13 20:35:35389
[email protected]dc5a5cf2012-09-26 02:49:30390 // Checks whether we should block in |stage|. If yes, returns an error code
391 // and optionally sets up callback based on |block_mode_|. If no, returns OK.
392 int MaybeBlockStage(Stage stage, const CompletionCallback& callback);
393
394 // Configuration parameters, can be adjusted by public methods:
395 const BlockMode block_mode_;
396
397 // Values returned on blocking stages when mode is SYNCHRONOUS or
398 // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING.
399 int retval_; // To be returned in non-auth stages.
400 AuthRequiredResponse auth_retval_;
401
[email protected]5f714132014-03-26 10:41:16402 GURL redirect_url_; // Used if non-empty during OnBeforeURLRequest.
[email protected]dc5a5cf2012-09-26 02:49:30403 int block_on_; // Bit mask: in which stages to block.
404
405 // |auth_credentials_| will be copied to |*target_auth_credential_| on
406 // callback.
407 AuthCredentials auth_credentials_;
408 AuthCredentials* target_auth_credentials_;
409
410 // Internal variables, not set by not the user:
411 // Last blocked stage waiting for user callback (unused if |block_mode_| !=
412 // USER_CALLBACK).
413 Stage stage_blocked_for_callback_;
414
415 // Callback objects stored during blocking stages.
[email protected]9045b8822012-01-13 20:35:35416 CompletionCallback callback_;
417 AuthCallback auth_callback_;
[email protected]dc5a5cf2012-09-26 02:49:30418
419 base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_;
420
421 DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate);
[email protected]9045b8822012-01-13 20:35:35422};
423
[email protected]dc5a5cf2012-09-26 02:49:30424BlockingNetworkDelegate::BlockingNetworkDelegate(BlockMode block_mode)
425 : block_mode_(block_mode),
426 retval_(OK),
427 auth_retval_(AUTH_REQUIRED_RESPONSE_NO_ACTION),
428 block_on_(0),
429 target_auth_credentials_(NULL),
430 stage_blocked_for_callback_(NOT_BLOCKED),
[email protected]aa249b52013-04-30 01:04:32431 weak_factory_(this) {
[email protected]dc5a5cf2012-09-26 02:49:30432}
433
434void BlockingNetworkDelegate::DoCallback(int response) {
435 ASSERT_EQ(USER_CALLBACK, block_mode_);
436 ASSERT_NE(NOT_BLOCKED, stage_blocked_for_callback_);
437 ASSERT_NE(ON_AUTH_REQUIRED, stage_blocked_for_callback_);
438 CompletionCallback callback = callback_;
439 Reset();
440 RunCallback(response, callback);
441}
442
443void BlockingNetworkDelegate::DoAuthCallback(
444 NetworkDelegate::AuthRequiredResponse response) {
445 ASSERT_EQ(USER_CALLBACK, block_mode_);
446 ASSERT_EQ(ON_AUTH_REQUIRED, stage_blocked_for_callback_);
447 AuthCallback auth_callback = auth_callback_;
448 Reset();
449 RunAuthCallback(response, auth_callback);
450}
451
452void BlockingNetworkDelegate::RunCallback(int response,
453 const CompletionCallback& callback) {
454 callback.Run(response);
455}
456
457void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response,
458 const AuthCallback& callback) {
459 if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) {
460 ASSERT_TRUE(target_auth_credentials_ != NULL);
461 *target_auth_credentials_ = auth_credentials_;
462 }
463 callback.Run(response);
464}
465
466int BlockingNetworkDelegate::OnBeforeURLRequest(
467 URLRequest* request,
468 const CompletionCallback& callback,
469 GURL* new_url) {
470 if (redirect_url_ == request->url())
471 return OK; // We've already seen this request and redirected elsewhere.
472
473 TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url);
474
475 if (!redirect_url_.is_empty())
476 *new_url = redirect_url_;
477
478 return MaybeBlockStage(ON_BEFORE_URL_REQUEST, callback);
479}
480
481int BlockingNetworkDelegate::OnBeforeSendHeaders(
482 URLRequest* request,
483 const CompletionCallback& callback,
484 HttpRequestHeaders* headers) {
485 TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers);
486
487 return MaybeBlockStage(ON_BEFORE_SEND_HEADERS, callback);
488}
489
490int BlockingNetworkDelegate::OnHeadersReceived(
491 URLRequest* request,
492 const CompletionCallback& callback,
[email protected]507af8f2012-10-20 00:42:32493 const HttpResponseHeaders* original_response_headers,
[email protected]5f714132014-03-26 10:41:16494 scoped_refptr<HttpResponseHeaders>* override_response_headers,
495 GURL* allowed_unsafe_redirect_url) {
496 TestNetworkDelegate::OnHeadersReceived(request,
497 callback,
498 original_response_headers,
499 override_response_headers,
500 allowed_unsafe_redirect_url);
[email protected]dc5a5cf2012-09-26 02:49:30501
502 return MaybeBlockStage(ON_HEADERS_RECEIVED, callback);
503}
504
505NetworkDelegate::AuthRequiredResponse BlockingNetworkDelegate::OnAuthRequired(
506 URLRequest* request,
507 const AuthChallengeInfo& auth_info,
508 const AuthCallback& callback,
509 AuthCredentials* credentials) {
510 TestNetworkDelegate::OnAuthRequired(request, auth_info, callback,
511 credentials);
512 // Check that the user has provided callback for the previous blocked stage.
513 EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_);
514
515 if ((block_on_ & ON_AUTH_REQUIRED) == 0) {
516 return AUTH_REQUIRED_RESPONSE_NO_ACTION;
517 }
518
519 target_auth_credentials_ = credentials;
520
521 switch (block_mode_) {
522 case SYNCHRONOUS:
523 if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH)
524 *target_auth_credentials_ = auth_credentials_;
525 return auth_retval_;
526
527 case AUTO_CALLBACK:
[email protected]2da659e2013-05-23 20:51:34528 base::MessageLoop::current()->PostTask(
[email protected]dc5a5cf2012-09-26 02:49:30529 FROM_HERE,
530 base::Bind(&BlockingNetworkDelegate::RunAuthCallback,
531 weak_factory_.GetWeakPtr(), auth_retval_, callback));
532 return AUTH_REQUIRED_RESPONSE_IO_PENDING;
533
534 case USER_CALLBACK:
535 auth_callback_ = callback;
536 stage_blocked_for_callback_ = ON_AUTH_REQUIRED;
[email protected]2da659e2013-05-23 20:51:34537 base::MessageLoop::current()->PostTask(FROM_HERE,
538 base::MessageLoop::QuitClosure());
[email protected]dc5a5cf2012-09-26 02:49:30539 return AUTH_REQUIRED_RESPONSE_IO_PENDING;
540 }
541 NOTREACHED();
542 return AUTH_REQUIRED_RESPONSE_NO_ACTION; // Dummy value.
543}
544
545void BlockingNetworkDelegate::Reset() {
546 EXPECT_NE(NOT_BLOCKED, stage_blocked_for_callback_);
547 stage_blocked_for_callback_ = NOT_BLOCKED;
548 callback_.Reset();
549 auth_callback_.Reset();
550}
551
552int BlockingNetworkDelegate::MaybeBlockStage(
553 BlockingNetworkDelegate::Stage stage,
554 const CompletionCallback& callback) {
555 // Check that the user has provided callback for the previous blocked stage.
556 EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_);
557
558 if ((block_on_ & stage) == 0) {
559 return OK;
560 }
561
562 switch (block_mode_) {
563 case SYNCHRONOUS:
564 EXPECT_NE(OK, retval_);
565 return retval_;
566
567 case AUTO_CALLBACK:
[email protected]2da659e2013-05-23 20:51:34568 base::MessageLoop::current()->PostTask(
[email protected]dc5a5cf2012-09-26 02:49:30569 FROM_HERE,
570 base::Bind(&BlockingNetworkDelegate::RunCallback,
571 weak_factory_.GetWeakPtr(), retval_, callback));
572 return ERR_IO_PENDING;
573
574 case USER_CALLBACK:
575 callback_ = callback;
576 stage_blocked_for_callback_ = stage;
[email protected]2da659e2013-05-23 20:51:34577 base::MessageLoop::current()->PostTask(FROM_HERE,
578 base::MessageLoop::QuitClosure());
[email protected]dc5a5cf2012-09-26 02:49:30579 return ERR_IO_PENDING;
580 }
581 NOTREACHED();
582 return 0;
583}
584
[email protected]d5a4dd62012-05-23 01:41:04585class TestURLRequestContextWithProxy : public TestURLRequestContext {
586 public:
587 // Does not own |delegate|.
588 TestURLRequestContextWithProxy(const std::string& proxy,
589 NetworkDelegate* delegate)
590 : TestURLRequestContext(true) {
591 context_storage_.set_proxy_service(ProxyService::CreateFixed(proxy));
592 set_network_delegate(delegate);
593 Init();
594 }
595 virtual ~TestURLRequestContextWithProxy() {}
596};
597
598} // namespace
599
[email protected]a592c0432012-12-01 18:10:29600// Inherit PlatformTest since we require the autorelease pool on Mac OS X.
[email protected]7a0bb4bf2008-11-19 21:41:48601class URLRequestTest : public PlatformTest {
[email protected]abb26092010-11-11 22:19:00602 public:
[email protected]ef2bf422012-05-11 03:27:09603 URLRequestTest() : default_context_(true) {
604 default_context_.set_network_delegate(&default_network_delegate_);
[email protected]58e32bb2013-01-21 18:23:25605 default_context_.set_net_log(&net_log_);
[email protected]e0f35c92013-05-08 16:04:34606 job_factory_.SetProtocolHandler("data", new DataProtocolHandler);
[email protected]02494ec2014-05-07 15:05:29607#if !defined(DISABLE_FILE_SUPPORT)
[email protected]255620da2013-08-19 13:14:29608 job_factory_.SetProtocolHandler(
609 "file", new FileProtocolHandler(base::MessageLoopProxy::current()));
[email protected]02494ec2014-05-07 15:05:29610#endif
[email protected]e0f35c92013-05-08 16:04:34611 default_context_.set_job_factory(&job_factory_);
[email protected]ef2bf422012-05-11 03:27:09612 default_context_.Init();
[email protected]87a09a92011-07-14 15:50:50613 }
[email protected]e4034ad2013-09-20 08:36:18614 virtual ~URLRequestTest() {
615 // URLRequestJobs may post clean-up tasks on destruction.
616 base::RunLoop().RunUntilIdle();
617 }
[email protected]87a09a92011-07-14 15:50:50618
[email protected]3c5ca8c2011-09-29 01:14:51619 // Adds the TestJobInterceptor to the default context.
620 TestJobInterceptor* AddTestInterceptor() {
[email protected]f53b4802012-12-20 17:04:23621 TestJobInterceptor* protocol_handler_ = new TestJobInterceptor();
[email protected]e0f35c92013-05-08 16:04:34622 job_factory_.SetProtocolHandler("http", NULL);
623 job_factory_.SetProtocolHandler("http", protocol_handler_);
[email protected]f53b4802012-12-20 17:04:23624 return protocol_handler_;
[email protected]3c5ca8c2011-09-29 01:14:51625 }
626
[email protected]87a09a92011-07-14 15:50:50627 protected:
[email protected]58e32bb2013-01-21 18:23:25628 CapturingNetLog net_log_;
[email protected]ceefd7fd2012-11-29 00:36:24629 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest.
[email protected]e0f35c92013-05-08 16:04:34630 URLRequestJobFactoryImpl job_factory_;
[email protected]ef2bf422012-05-11 03:27:09631 TestURLRequestContext default_context_;
[email protected]7a0bb4bf2008-11-19 21:41:48632};
633
[email protected]316c1e5e2012-09-12 15:17:44634TEST_F(URLRequestTest, AboutBlankTest) {
635 TestDelegate d;
636 {
[email protected]f7022f32014-08-21 16:32:19637 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
638 GURL("about:blank"), DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:44639
[email protected]f7022f32014-08-21 16:32:19640 r->Start();
641 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44642
[email protected]255620da2013-08-19 13:14:29643 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:44644
[email protected]f7022f32014-08-21 16:32:19645 EXPECT_TRUE(!r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44646 EXPECT_FALSE(d.received_data_before_response());
647 EXPECT_EQ(d.bytes_received(), 0);
[email protected]f7022f32014-08-21 16:32:19648 EXPECT_EQ("", r->GetSocketAddress().host());
649 EXPECT_EQ(0, r->GetSocketAddress().port());
[email protected]79e1fd62013-06-20 06:50:04650
651 HttpRequestHeaders headers;
[email protected]f7022f32014-08-21 16:32:19652 EXPECT_FALSE(r->GetFullRequestHeaders(&headers));
[email protected]316c1e5e2012-09-12 15:17:44653 }
654}
655
656TEST_F(URLRequestTest, DataURLImageTest) {
657 TestDelegate d;
658 {
659 // Use our nice little Chrome logo.
[email protected]f7022f32014-08-21 16:32:19660 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
[email protected]2ca01e52013-10-31 22:05:19661 GURL(
[email protected]316c1e5e2012-09-12 15:17:44662 "data:image/png;base64,"
663 "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUBBjG3"
664 "w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQxIEVxitD"
665 "5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI5MpCVdW0gO7t"
666 "vNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKzMx1+fg9bAgK6zHq9"
667 "cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7CkIMDxQpF7r/MWq12UctI1"
668 "dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0WYUqt57yWf2McHTObYPbVD+Z"
669 "wbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2HQQcm43XwmtoYM6vVKleh0uoWvnzW"
670 "3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nkDFHF9ZS+uYVjRUasMeHUmyLYtcklTvzW"
671 "GFZnNOXczThvpKIzjcahSqIzkvDLayDq6D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTb"
672 "xXX1T5xsV9tm9r4TQwHLiZw/pdDZJea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5"
673 "MrVvMzNaI3+ERHfrFzPKQukrQGI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gV"
674 "Md6q5c8GdosynKmSeRuGzpjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq"
675 "/uxxZKzNiZFGD0wRC3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7F"
676 "Ux1qLnV7MGF40smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufB"
677 "JbsysjjW4kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJM"
678 "nlm2O34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfm"
679 "moRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV5En"
680 "hORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="),
[email protected]f7022f32014-08-21 16:32:19681 DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:44682
[email protected]f7022f32014-08-21 16:32:19683 r->Start();
684 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44685
[email protected]255620da2013-08-19 13:14:29686 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:44687
[email protected]f7022f32014-08-21 16:32:19688 EXPECT_TRUE(!r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44689 EXPECT_FALSE(d.received_data_before_response());
690 EXPECT_EQ(d.bytes_received(), 911);
[email protected]f7022f32014-08-21 16:32:19691 EXPECT_EQ("", r->GetSocketAddress().host());
692 EXPECT_EQ(0, r->GetSocketAddress().port());
[email protected]79e1fd62013-06-20 06:50:04693
694 HttpRequestHeaders headers;
[email protected]f7022f32014-08-21 16:32:19695 EXPECT_FALSE(r->GetFullRequestHeaders(&headers));
[email protected]316c1e5e2012-09-12 15:17:44696 }
697}
698
[email protected]5ecf7cb282014-05-11 01:49:55699#if !defined(DISABLE_FILE_SUPPORT)
[email protected]316c1e5e2012-09-12 15:17:44700TEST_F(URLRequestTest, FileTest) {
[email protected]6cdfd7f2013-02-08 20:40:15701 base::FilePath app_path;
[email protected]316c1e5e2012-09-12 15:17:44702 PathService::Get(base::FILE_EXE, &app_path);
703 GURL app_url = FilePathToFileURL(app_path);
704
705 TestDelegate d;
706 {
[email protected]f7022f32014-08-21 16:32:19707 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
708 app_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:44709
[email protected]f7022f32014-08-21 16:32:19710 r->Start();
711 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44712
[email protected]255620da2013-08-19 13:14:29713 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:44714
715 int64 file_size = -1;
[email protected]56285702013-12-04 18:22:49716 EXPECT_TRUE(base::GetFileSize(app_path, &file_size));
[email protected]316c1e5e2012-09-12 15:17:44717
[email protected]f7022f32014-08-21 16:32:19718 EXPECT_TRUE(!r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44719 EXPECT_EQ(1, d.response_started_count());
720 EXPECT_FALSE(d.received_data_before_response());
721 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
[email protected]f7022f32014-08-21 16:32:19722 EXPECT_EQ("", r->GetSocketAddress().host());
723 EXPECT_EQ(0, r->GetSocketAddress().port());
[email protected]79e1fd62013-06-20 06:50:04724
725 HttpRequestHeaders headers;
[email protected]f7022f32014-08-21 16:32:19726 EXPECT_FALSE(r->GetFullRequestHeaders(&headers));
[email protected]316c1e5e2012-09-12 15:17:44727 }
728}
729
[email protected]ba40bb762012-12-17 07:11:04730TEST_F(URLRequestTest, FileTestCancel) {
[email protected]6cdfd7f2013-02-08 20:40:15731 base::FilePath app_path;
[email protected]ba40bb762012-12-17 07:11:04732 PathService::Get(base::FILE_EXE, &app_path);
733 GURL app_url = FilePathToFileURL(app_path);
734
735 TestDelegate d;
736 {
[email protected]f7022f32014-08-21 16:32:19737 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
738 app_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]ba40bb762012-12-17 07:11:04739
[email protected]f7022f32014-08-21 16:32:19740 r->Start();
741 EXPECT_TRUE(r->is_pending());
742 r->Cancel();
[email protected]ba40bb762012-12-17 07:11:04743 }
[email protected]79e1fd62013-06-20 06:50:04744 // Async cancellation should be safe even when URLRequest has been already
[email protected]ba40bb762012-12-17 07:11:04745 // destroyed.
[email protected]255620da2013-08-19 13:14:29746 base::RunLoop().RunUntilIdle();
[email protected]ba40bb762012-12-17 07:11:04747}
748
[email protected]316c1e5e2012-09-12 15:17:44749TEST_F(URLRequestTest, FileTestFullSpecifiedRange) {
750 const size_t buffer_size = 4000;
[email protected]4356f0f2013-04-07 00:58:17751 scoped_ptr<char[]> buffer(new char[buffer_size]);
[email protected]316c1e5e2012-09-12 15:17:44752 FillBuffer(buffer.get(), buffer_size);
753
[email protected]6cdfd7f2013-02-08 20:40:15754 base::FilePath temp_path;
[email protected]03d9afc02013-12-03 17:55:52755 EXPECT_TRUE(base::CreateTemporaryFile(&temp_path));
[email protected]316c1e5e2012-09-12 15:17:44756 GURL temp_url = FilePathToFileURL(temp_path);
[email protected]e5c2a22e2014-03-06 20:42:30757 EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size));
[email protected]316c1e5e2012-09-12 15:17:44758
759 int64 file_size;
[email protected]56285702013-12-04 18:22:49760 EXPECT_TRUE(base::GetFileSize(temp_path, &file_size));
[email protected]316c1e5e2012-09-12 15:17:44761
762 const size_t first_byte_position = 500;
763 const size_t last_byte_position = buffer_size - first_byte_position;
764 const size_t content_length = last_byte_position - first_byte_position + 1;
765 std::string partial_buffer_string(buffer.get() + first_byte_position,
766 buffer.get() + last_byte_position + 1);
767
768 TestDelegate d;
769 {
[email protected]f7022f32014-08-21 16:32:19770 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
771 temp_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:44772
773 HttpRequestHeaders headers;
[email protected]b7572ea2013-11-26 20:16:38774 headers.SetHeader(
775 HttpRequestHeaders::kRange,
[email protected]cba24642014-08-15 20:49:59776 HttpByteRange::Bounded(
[email protected]b7572ea2013-11-26 20:16:38777 first_byte_position, last_byte_position).GetHeaderValue());
[email protected]f7022f32014-08-21 16:32:19778 r->SetExtraRequestHeaders(headers);
779 r->Start();
780 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44781
[email protected]255620da2013-08-19 13:14:29782 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:19783 EXPECT_TRUE(!r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44784 EXPECT_EQ(1, d.response_started_count());
785 EXPECT_FALSE(d.received_data_before_response());
786 EXPECT_EQ(static_cast<int>(content_length), d.bytes_received());
787 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
788 EXPECT_TRUE(partial_buffer_string == d.data_received());
789 }
790
[email protected]dd3aa792013-07-16 19:10:23791 EXPECT_TRUE(base::DeleteFile(temp_path, false));
[email protected]316c1e5e2012-09-12 15:17:44792}
793
794TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) {
795 const size_t buffer_size = 4000;
[email protected]4356f0f2013-04-07 00:58:17796 scoped_ptr<char[]> buffer(new char[buffer_size]);
[email protected]316c1e5e2012-09-12 15:17:44797 FillBuffer(buffer.get(), buffer_size);
798
[email protected]6cdfd7f2013-02-08 20:40:15799 base::FilePath temp_path;
[email protected]03d9afc02013-12-03 17:55:52800 EXPECT_TRUE(base::CreateTemporaryFile(&temp_path));
[email protected]316c1e5e2012-09-12 15:17:44801 GURL temp_url = FilePathToFileURL(temp_path);
[email protected]e5c2a22e2014-03-06 20:42:30802 EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size));
[email protected]316c1e5e2012-09-12 15:17:44803
804 int64 file_size;
[email protected]56285702013-12-04 18:22:49805 EXPECT_TRUE(base::GetFileSize(temp_path, &file_size));
[email protected]316c1e5e2012-09-12 15:17:44806
807 const size_t first_byte_position = 500;
808 const size_t last_byte_position = buffer_size - 1;
809 const size_t content_length = last_byte_position - first_byte_position + 1;
810 std::string partial_buffer_string(buffer.get() + first_byte_position,
811 buffer.get() + last_byte_position + 1);
812
813 TestDelegate d;
814 {
[email protected]f7022f32014-08-21 16:32:19815 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
816 temp_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:44817
818 HttpRequestHeaders headers;
819 headers.SetHeader(HttpRequestHeaders::kRange,
[email protected]cba24642014-08-15 20:49:59820 HttpByteRange::RightUnbounded(
[email protected]b7572ea2013-11-26 20:16:38821 first_byte_position).GetHeaderValue());
[email protected]f7022f32014-08-21 16:32:19822 r->SetExtraRequestHeaders(headers);
823 r->Start();
824 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44825
[email protected]255620da2013-08-19 13:14:29826 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:19827 EXPECT_TRUE(!r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44828 EXPECT_EQ(1, d.response_started_count());
829 EXPECT_FALSE(d.received_data_before_response());
830 EXPECT_EQ(static_cast<int>(content_length), d.bytes_received());
831 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
832 EXPECT_TRUE(partial_buffer_string == d.data_received());
833 }
834
[email protected]dd3aa792013-07-16 19:10:23835 EXPECT_TRUE(base::DeleteFile(temp_path, false));
[email protected]316c1e5e2012-09-12 15:17:44836}
837
838TEST_F(URLRequestTest, FileTestMultipleRanges) {
839 const size_t buffer_size = 400000;
[email protected]4356f0f2013-04-07 00:58:17840 scoped_ptr<char[]> buffer(new char[buffer_size]);
[email protected]316c1e5e2012-09-12 15:17:44841 FillBuffer(buffer.get(), buffer_size);
842
[email protected]6cdfd7f2013-02-08 20:40:15843 base::FilePath temp_path;
[email protected]03d9afc02013-12-03 17:55:52844 EXPECT_TRUE(base::CreateTemporaryFile(&temp_path));
[email protected]316c1e5e2012-09-12 15:17:44845 GURL temp_url = FilePathToFileURL(temp_path);
[email protected]e5c2a22e2014-03-06 20:42:30846 EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size));
[email protected]316c1e5e2012-09-12 15:17:44847
848 int64 file_size;
[email protected]56285702013-12-04 18:22:49849 EXPECT_TRUE(base::GetFileSize(temp_path, &file_size));
[email protected]316c1e5e2012-09-12 15:17:44850
851 TestDelegate d;
852 {
[email protected]f7022f32014-08-21 16:32:19853 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
854 temp_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:44855
856 HttpRequestHeaders headers;
[email protected]b7572ea2013-11-26 20:16:38857 headers.SetHeader(HttpRequestHeaders::kRange, "bytes=0-0,10-200,200-300");
[email protected]f7022f32014-08-21 16:32:19858 r->SetExtraRequestHeaders(headers);
859 r->Start();
860 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44861
[email protected]255620da2013-08-19 13:14:29862 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:44863 EXPECT_TRUE(d.request_failed());
864 }
865
[email protected]dd3aa792013-07-16 19:10:23866 EXPECT_TRUE(base::DeleteFile(temp_path, false));
[email protected]316c1e5e2012-09-12 15:17:44867}
868
[email protected]3ca8b362013-11-11 22:18:07869TEST_F(URLRequestTest, AllowFileURLs) {
870 base::ScopedTempDir temp_dir;
871 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
872 base::FilePath test_file;
[email protected]03d9afc02013-12-03 17:55:52873 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &test_file));
[email protected]3ca8b362013-11-11 22:18:07874 std::string test_data("monkey");
[email protected]e5c2a22e2014-03-06 20:42:30875 base::WriteFile(test_file, test_data.data(), test_data.size());
[email protected]cba24642014-08-15 20:49:59876 GURL test_file_url = FilePathToFileURL(test_file);
[email protected]3ca8b362013-11-11 22:18:07877
878 {
879 TestDelegate d;
880 TestNetworkDelegate network_delegate;
881 network_delegate.set_can_access_files(true);
882 default_context_.set_network_delegate(&network_delegate);
[email protected]f7022f32014-08-21 16:32:19883 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
884 test_file_url, DEFAULT_PRIORITY, &d, NULL));
885 r->Start();
[email protected]3ca8b362013-11-11 22:18:07886 base::RunLoop().Run();
887 EXPECT_FALSE(d.request_failed());
888 EXPECT_EQ(test_data, d.data_received());
889 }
890
891 {
892 TestDelegate d;
893 TestNetworkDelegate network_delegate;
894 network_delegate.set_can_access_files(false);
895 default_context_.set_network_delegate(&network_delegate);
[email protected]f7022f32014-08-21 16:32:19896 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
897 test_file_url, DEFAULT_PRIORITY, &d, NULL));
898 r->Start();
[email protected]3ca8b362013-11-11 22:18:07899 base::RunLoop().Run();
900 EXPECT_TRUE(d.request_failed());
901 EXPECT_EQ("", d.data_received());
902 }
903}
904
[email protected]316c1e5e2012-09-12 15:17:44905
906TEST_F(URLRequestTest, FileDirCancelTest) {
907 // Put in mock resource provider.
908 NetModule::SetResourceProvider(TestNetResourceProvider);
909
910 TestDelegate d;
911 {
[email protected]6cdfd7f2013-02-08 20:40:15912 base::FilePath file_path;
[email protected]316c1e5e2012-09-12 15:17:44913 PathService::Get(base::DIR_SOURCE_ROOT, &file_path);
914 file_path = file_path.Append(FILE_PATH_LITERAL("net"));
915 file_path = file_path.Append(FILE_PATH_LITERAL("data"));
916
[email protected]f7022f32014-08-21 16:32:19917 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
918 FilePathToFileURL(file_path), DEFAULT_PRIORITY, &d, NULL));
919 req->Start();
920 EXPECT_TRUE(req->is_pending());
[email protected]316c1e5e2012-09-12 15:17:44921
922 d.set_cancel_in_received_data_pending(true);
923
[email protected]255620da2013-08-19 13:14:29924 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:44925 }
926
927 // Take out mock resource provider.
928 NetModule::SetResourceProvider(NULL);
929}
930
[email protected]5f9581402013-10-30 13:08:32931TEST_F(URLRequestTest, FileDirOutputSanity) {
932 // Verify the general sanity of the the output of the file:
933 // directory lister by checking for the output of a known existing
934 // file.
935 const char sentinel_name[] = "filedir-sentinel";
936
937 base::FilePath path;
938 PathService::Get(base::DIR_SOURCE_ROOT, &path);
939 path = path.Append(FILE_PATH_LITERAL("net"));
940 path = path.Append(FILE_PATH_LITERAL("data"));
941 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
942
943 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:19944 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
945 FilePathToFileURL(path), DEFAULT_PRIORITY, &d, NULL));
946 req->Start();
[email protected]5f9581402013-10-30 13:08:32947 base::RunLoop().Run();
948
949 // Generate entry for the sentinel file.
950 base::FilePath sentinel_path = path.AppendASCII(sentinel_name);
[email protected]54124ed02014-01-07 10:06:58951 base::File::Info info;
[email protected]9eae4e62013-12-04 20:56:49952 EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info));
[email protected]5f9581402013-10-30 13:08:32953 EXPECT_GT(info.size, 0);
954 std::string sentinel_output = GetDirectoryListingEntry(
955 base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)),
956 std::string(sentinel_name),
957 false /* is_dir */,
958 info.size,
959 info.last_modified);
960
961 ASSERT_LT(0, d.bytes_received());
962 ASSERT_FALSE(d.request_failed());
[email protected]f7022f32014-08-21 16:32:19963 ASSERT_TRUE(req->status().is_success());
[email protected]5f9581402013-10-30 13:08:32964 // Check for the entry generated for the "sentinel" file.
965 const std::string& data = d.data_received();
966 ASSERT_NE(data.find(sentinel_output), std::string::npos);
967}
968
[email protected]316c1e5e2012-09-12 15:17:44969TEST_F(URLRequestTest, FileDirRedirectNoCrash) {
970 // There is an implicit redirect when loading a file path that matches a
971 // directory and does not end with a slash. Ensure that following such
972 // redirects does not crash. See http://crbug.com/18686.
973
[email protected]6cdfd7f2013-02-08 20:40:15974 base::FilePath path;
[email protected]316c1e5e2012-09-12 15:17:44975 PathService::Get(base::DIR_SOURCE_ROOT, &path);
976 path = path.Append(FILE_PATH_LITERAL("net"));
977 path = path.Append(FILE_PATH_LITERAL("data"));
978 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
979
980 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:19981 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
982 FilePathToFileURL(path), DEFAULT_PRIORITY, &d, NULL));
983 req->Start();
[email protected]255620da2013-08-19 13:14:29984 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:44985
986 ASSERT_EQ(1, d.received_redirect_count());
987 ASSERT_LT(0, d.bytes_received());
988 ASSERT_FALSE(d.request_failed());
[email protected]f7022f32014-08-21 16:32:19989 ASSERT_TRUE(req->status().is_success());
[email protected]316c1e5e2012-09-12 15:17:44990}
991
992#if defined(OS_WIN)
993// Don't accept the url "file:///" on windows. See http://crbug.com/1474.
994TEST_F(URLRequestTest, FileDirRedirectSingleSlash) {
995 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:19996 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
997 GURL("file:///"), DEFAULT_PRIORITY, &d, NULL));
998 req->Start();
[email protected]255620da2013-08-19 13:14:29999 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441000
1001 ASSERT_EQ(1, d.received_redirect_count());
[email protected]f7022f32014-08-21 16:32:191002 ASSERT_FALSE(req->status().is_success());
[email protected]316c1e5e2012-09-12 15:17:441003}
[email protected]5ecf7cb282014-05-11 01:49:551004#endif // defined(OS_WIN)
1005
1006#endif // !defined(DISABLE_FILE_SUPPORT)
1007
1008TEST_F(URLRequestTest, InvalidUrlTest) {
1009 TestDelegate d;
1010 {
[email protected]f7022f32014-08-21 16:32:191011 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
1012 GURL("invalid url"), DEFAULT_PRIORITY, &d, NULL));
[email protected]5ecf7cb282014-05-11 01:49:551013
[email protected]f7022f32014-08-21 16:32:191014 r->Start();
1015 EXPECT_TRUE(r->is_pending());
[email protected]5ecf7cb282014-05-11 01:49:551016
1017 base::RunLoop().Run();
1018 EXPECT_TRUE(d.request_failed());
1019 }
1020}
1021
1022#if defined(OS_WIN)
1023TEST_F(URLRequestTest, ResolveShortcutTest) {
1024 base::FilePath app_path;
1025 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
1026 app_path = app_path.AppendASCII("net");
1027 app_path = app_path.AppendASCII("data");
1028 app_path = app_path.AppendASCII("url_request_unittest");
1029 app_path = app_path.AppendASCII("with-headers.html");
1030
1031 std::wstring lnk_path = app_path.value() + L".lnk";
1032
1033 base::win::ScopedCOMInitializer com_initializer;
1034
1035 // Temporarily create a shortcut for test
1036 {
1037 base::win::ScopedComPtr<IShellLink> shell;
1038 ASSERT_TRUE(SUCCEEDED(shell.CreateInstance(CLSID_ShellLink, NULL,
1039 CLSCTX_INPROC_SERVER)));
1040 base::win::ScopedComPtr<IPersistFile> persist;
1041 ASSERT_TRUE(SUCCEEDED(shell.QueryInterface(persist.Receive())));
1042 EXPECT_TRUE(SUCCEEDED(shell->SetPath(app_path.value().c_str())));
1043 EXPECT_TRUE(SUCCEEDED(shell->SetDescription(L"ResolveShortcutTest")));
1044 EXPECT_TRUE(SUCCEEDED(persist->Save(lnk_path.c_str(), TRUE)));
1045 }
1046
1047 TestDelegate d;
1048 {
[email protected]f7022f32014-08-21 16:32:191049 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
1050 FilePathToFileURL(base::FilePath(lnk_path)), DEFAULT_PRIORITY, &d,
1051 NULL));
[email protected]5ecf7cb282014-05-11 01:49:551052
[email protected]f7022f32014-08-21 16:32:191053 r->Start();
1054 EXPECT_TRUE(r->is_pending());
[email protected]5ecf7cb282014-05-11 01:49:551055
1056 base::RunLoop().Run();
1057
1058 WIN32_FILE_ATTRIBUTE_DATA data;
1059 GetFileAttributesEx(app_path.value().c_str(),
1060 GetFileExInfoStandard, &data);
1061 HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ,
1062 FILE_SHARE_READ, NULL, OPEN_EXISTING,
1063 FILE_ATTRIBUTE_NORMAL, NULL);
1064 EXPECT_NE(INVALID_HANDLE_VALUE, file);
1065 scoped_ptr<char[]> buffer(new char[data.nFileSizeLow]);
1066 DWORD read_size;
1067 BOOL result;
1068 result = ReadFile(file, buffer.get(), data.nFileSizeLow,
1069 &read_size, NULL);
1070 std::string content(buffer.get(), read_size);
1071 CloseHandle(file);
1072
[email protected]f7022f32014-08-21 16:32:191073 EXPECT_TRUE(!r->is_pending());
[email protected]5ecf7cb282014-05-11 01:49:551074 EXPECT_EQ(1, d.received_redirect_count());
1075 EXPECT_EQ(content, d.data_received());
1076 }
1077
1078 // Clean the shortcut
1079 DeleteFile(lnk_path.c_str());
1080}
1081#endif // defined(OS_WIN)
[email protected]316c1e5e2012-09-12 15:17:441082
1083// Custom URLRequestJobs for use with interceptor tests
1084class RestartTestJob : public URLRequestTestJob {
1085 public:
1086 RestartTestJob(URLRequest* request, NetworkDelegate* network_delegate)
1087 : URLRequestTestJob(request, network_delegate, true) {}
1088 protected:
[email protected]46fadfd2013-02-06 09:40:161089 virtual void StartAsync() OVERRIDE {
[email protected]316c1e5e2012-09-12 15:17:441090 this->NotifyRestartRequired();
1091 }
1092 private:
[email protected]46fadfd2013-02-06 09:40:161093 virtual ~RestartTestJob() {}
[email protected]316c1e5e2012-09-12 15:17:441094};
1095
1096class CancelTestJob : public URLRequestTestJob {
1097 public:
1098 explicit CancelTestJob(URLRequest* request, NetworkDelegate* network_delegate)
1099 : URLRequestTestJob(request, network_delegate, true) {}
1100 protected:
[email protected]46fadfd2013-02-06 09:40:161101 virtual void StartAsync() OVERRIDE {
[email protected]316c1e5e2012-09-12 15:17:441102 request_->Cancel();
1103 }
1104 private:
[email protected]46fadfd2013-02-06 09:40:161105 virtual ~CancelTestJob() {}
[email protected]316c1e5e2012-09-12 15:17:441106};
1107
1108class CancelThenRestartTestJob : public URLRequestTestJob {
1109 public:
1110 explicit CancelThenRestartTestJob(URLRequest* request,
1111 NetworkDelegate* network_delegate)
1112 : URLRequestTestJob(request, network_delegate, true) {
1113 }
1114 protected:
[email protected]46fadfd2013-02-06 09:40:161115 virtual void StartAsync() OVERRIDE {
[email protected]316c1e5e2012-09-12 15:17:441116 request_->Cancel();
1117 this->NotifyRestartRequired();
1118 }
1119 private:
[email protected]46fadfd2013-02-06 09:40:161120 virtual ~CancelThenRestartTestJob() {}
[email protected]316c1e5e2012-09-12 15:17:441121};
1122
1123// An Interceptor for use with interceptor tests
1124class TestInterceptor : URLRequest::Interceptor {
1125 public:
1126 TestInterceptor()
1127 : intercept_main_request_(false), restart_main_request_(false),
1128 cancel_main_request_(false), cancel_then_restart_main_request_(false),
1129 simulate_main_network_error_(false),
1130 intercept_redirect_(false), cancel_redirect_request_(false),
1131 intercept_final_response_(false), cancel_final_request_(false),
1132 did_intercept_main_(false), did_restart_main_(false),
1133 did_cancel_main_(false), did_cancel_then_restart_main_(false),
1134 did_simulate_error_main_(false),
1135 did_intercept_redirect_(false), did_cancel_redirect_(false),
1136 did_intercept_final_(false), did_cancel_final_(false) {
1137 URLRequest::Deprecated::RegisterRequestInterceptor(this);
1138 }
1139
[email protected]46fadfd2013-02-06 09:40:161140 virtual ~TestInterceptor() {
[email protected]316c1e5e2012-09-12 15:17:441141 URLRequest::Deprecated::UnregisterRequestInterceptor(this);
1142 }
1143
[email protected]46fadfd2013-02-06 09:40:161144 virtual URLRequestJob* MaybeIntercept(
1145 URLRequest* request,
1146 NetworkDelegate* network_delegate) OVERRIDE {
[email protected]316c1e5e2012-09-12 15:17:441147 if (restart_main_request_) {
1148 restart_main_request_ = false;
1149 did_restart_main_ = true;
1150 return new RestartTestJob(request, network_delegate);
1151 }
1152 if (cancel_main_request_) {
1153 cancel_main_request_ = false;
1154 did_cancel_main_ = true;
1155 return new CancelTestJob(request, network_delegate);
1156 }
1157 if (cancel_then_restart_main_request_) {
1158 cancel_then_restart_main_request_ = false;
1159 did_cancel_then_restart_main_ = true;
1160 return new CancelThenRestartTestJob(request, network_delegate);
1161 }
1162 if (simulate_main_network_error_) {
1163 simulate_main_network_error_ = false;
1164 did_simulate_error_main_ = true;
1165 // will error since the requeted url is not one of its canned urls
1166 return new URLRequestTestJob(request, network_delegate, true);
1167 }
1168 if (!intercept_main_request_)
1169 return NULL;
1170 intercept_main_request_ = false;
1171 did_intercept_main_ = true;
[email protected]2bba3252013-04-08 19:50:591172 URLRequestTestJob* job = new URLRequestTestJob(request,
1173 network_delegate,
1174 main_headers_,
1175 main_data_,
1176 true);
1177 job->set_load_timing_info(main_request_load_timing_info_);
1178 return job;
[email protected]316c1e5e2012-09-12 15:17:441179 }
1180
1181 virtual URLRequestJob* MaybeInterceptRedirect(
1182 URLRequest* request,
1183 NetworkDelegate* network_delegate,
[email protected]46fadfd2013-02-06 09:40:161184 const GURL& location) OVERRIDE {
[email protected]316c1e5e2012-09-12 15:17:441185 if (cancel_redirect_request_) {
1186 cancel_redirect_request_ = false;
1187 did_cancel_redirect_ = true;
1188 return new CancelTestJob(request, network_delegate);
1189 }
1190 if (!intercept_redirect_)
1191 return NULL;
1192 intercept_redirect_ = false;
1193 did_intercept_redirect_ = true;
1194 return new URLRequestTestJob(request,
1195 network_delegate,
1196 redirect_headers_,
1197 redirect_data_,
1198 true);
1199 }
1200
1201 virtual URLRequestJob* MaybeInterceptResponse(
[email protected]46fadfd2013-02-06 09:40:161202 URLRequest* request, NetworkDelegate* network_delegate) OVERRIDE {
[email protected]316c1e5e2012-09-12 15:17:441203 if (cancel_final_request_) {
1204 cancel_final_request_ = false;
1205 did_cancel_final_ = true;
1206 return new CancelTestJob(request, network_delegate);
1207 }
1208 if (!intercept_final_response_)
1209 return NULL;
1210 intercept_final_response_ = false;
1211 did_intercept_final_ = true;
1212 return new URLRequestTestJob(request,
1213 network_delegate,
1214 final_headers_,
1215 final_data_,
1216 true);
1217 }
1218
[email protected]2bba3252013-04-08 19:50:591219 // Whether to intercept the main request, and if so the response to return and
1220 // the LoadTimingInfo to use.
[email protected]316c1e5e2012-09-12 15:17:441221 bool intercept_main_request_;
1222 std::string main_headers_;
1223 std::string main_data_;
[email protected]2bba3252013-04-08 19:50:591224 LoadTimingInfo main_request_load_timing_info_;
[email protected]316c1e5e2012-09-12 15:17:441225
1226 // Other actions we take at MaybeIntercept time
1227 bool restart_main_request_;
1228 bool cancel_main_request_;
1229 bool cancel_then_restart_main_request_;
1230 bool simulate_main_network_error_;
1231
1232 // Whether to intercept redirects, and if so the response to return.
1233 bool intercept_redirect_;
1234 std::string redirect_headers_;
1235 std::string redirect_data_;
1236
1237 // Other actions we can take at MaybeInterceptRedirect time
1238 bool cancel_redirect_request_;
1239
1240 // Whether to intercept final response, and if so the response to return.
1241 bool intercept_final_response_;
1242 std::string final_headers_;
1243 std::string final_data_;
1244
1245 // Other actions we can take at MaybeInterceptResponse time
1246 bool cancel_final_request_;
1247
1248 // If we did something or not
1249 bool did_intercept_main_;
1250 bool did_restart_main_;
1251 bool did_cancel_main_;
1252 bool did_cancel_then_restart_main_;
1253 bool did_simulate_error_main_;
1254 bool did_intercept_redirect_;
1255 bool did_cancel_redirect_;
1256 bool did_intercept_final_;
1257 bool did_cancel_final_;
1258
1259 // Static getters for canned response header and data strings
1260
1261 static std::string ok_data() {
1262 return URLRequestTestJob::test_data_1();
1263 }
1264
1265 static std::string ok_headers() {
1266 return URLRequestTestJob::test_headers();
1267 }
1268
1269 static std::string redirect_data() {
1270 return std::string();
1271 }
1272
1273 static std::string redirect_headers() {
1274 return URLRequestTestJob::test_redirect_headers();
1275 }
1276
1277 static std::string error_data() {
1278 return std::string("ohhh nooooo mr. bill!");
1279 }
1280
1281 static std::string error_headers() {
1282 return URLRequestTestJob::test_error_headers();
1283 }
1284};
1285
1286TEST_F(URLRequestTest, Intercept) {
1287 TestInterceptor interceptor;
1288
1289 // intercept the main request and respond with a simple response
1290 interceptor.intercept_main_request_ = true;
1291 interceptor.main_headers_ = TestInterceptor::ok_headers();
1292 interceptor.main_data_ = TestInterceptor::ok_data();
1293
1294 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:191295 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1296 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:441297 base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data();
1298 base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data();
1299 base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data();
[email protected]f7022f32014-08-21 16:32:191300 req->SetUserData(NULL, user_data0);
1301 req->SetUserData(&user_data1, user_data1);
1302 req->SetUserData(&user_data2, user_data2);
1303 req->set_method("GET");
1304 req->Start();
[email protected]255620da2013-08-19 13:14:291305 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441306
1307 // Make sure we can retrieve our specific user data
[email protected]f7022f32014-08-21 16:32:191308 EXPECT_EQ(user_data0, req->GetUserData(NULL));
1309 EXPECT_EQ(user_data1, req->GetUserData(&user_data1));
1310 EXPECT_EQ(user_data2, req->GetUserData(&user_data2));
[email protected]316c1e5e2012-09-12 15:17:441311
1312 // Check the interceptor got called as expected
1313 EXPECT_TRUE(interceptor.did_intercept_main_);
1314
1315 // Check we got one good response
[email protected]f7022f32014-08-21 16:32:191316 EXPECT_TRUE(req->status().is_success());
1317 EXPECT_EQ(200, req->response_headers()->response_code());
[email protected]316c1e5e2012-09-12 15:17:441318 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1319 EXPECT_EQ(1, d.response_started_count());
1320 EXPECT_EQ(0, d.received_redirect_count());
1321}
1322
1323TEST_F(URLRequestTest, InterceptRedirect) {
1324 TestInterceptor interceptor;
1325
1326 // intercept the main request and respond with a redirect
1327 interceptor.intercept_main_request_ = true;
1328 interceptor.main_headers_ = TestInterceptor::redirect_headers();
1329 interceptor.main_data_ = TestInterceptor::redirect_data();
1330
1331 // intercept that redirect and respond a final OK response
1332 interceptor.intercept_redirect_ = true;
1333 interceptor.redirect_headers_ = TestInterceptor::ok_headers();
1334 interceptor.redirect_data_ = TestInterceptor::ok_data();
1335
1336 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:191337 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1338 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL));
1339 req->set_method("GET");
1340 req->Start();
[email protected]255620da2013-08-19 13:14:291341 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441342
1343 // Check the interceptor got called as expected
1344 EXPECT_TRUE(interceptor.did_intercept_main_);
1345 EXPECT_TRUE(interceptor.did_intercept_redirect_);
1346
1347 // Check we got one good response
[email protected]f7022f32014-08-21 16:32:191348 EXPECT_TRUE(req->status().is_success());
1349 if (req->status().is_success()) {
1350 EXPECT_EQ(200, req->response_headers()->response_code());
[email protected]316c1e5e2012-09-12 15:17:441351 }
1352 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1353 EXPECT_EQ(1, d.response_started_count());
1354 EXPECT_EQ(0, d.received_redirect_count());
1355}
1356
1357TEST_F(URLRequestTest, InterceptServerError) {
1358 TestInterceptor interceptor;
1359
1360 // intercept the main request to generate a server error response
1361 interceptor.intercept_main_request_ = true;
1362 interceptor.main_headers_ = TestInterceptor::error_headers();
1363 interceptor.main_data_ = TestInterceptor::error_data();
1364
1365 // intercept that error and respond with an OK response
1366 interceptor.intercept_final_response_ = true;
1367 interceptor.final_headers_ = TestInterceptor::ok_headers();
1368 interceptor.final_data_ = TestInterceptor::ok_data();
1369
1370 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:191371 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1372 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL));
1373 req->set_method("GET");
1374 req->Start();
[email protected]255620da2013-08-19 13:14:291375 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441376
1377 // Check the interceptor got called as expected
1378 EXPECT_TRUE(interceptor.did_intercept_main_);
1379 EXPECT_TRUE(interceptor.did_intercept_final_);
1380
1381 // Check we got one good response
[email protected]f7022f32014-08-21 16:32:191382 EXPECT_TRUE(req->status().is_success());
1383 EXPECT_EQ(200, req->response_headers()->response_code());
[email protected]316c1e5e2012-09-12 15:17:441384 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1385 EXPECT_EQ(1, d.response_started_count());
1386 EXPECT_EQ(0, d.received_redirect_count());
1387}
1388
1389TEST_F(URLRequestTest, InterceptNetworkError) {
1390 TestInterceptor interceptor;
1391
1392 // intercept the main request to simulate a network error
1393 interceptor.simulate_main_network_error_ = true;
1394
1395 // intercept that error and respond with an OK response
1396 interceptor.intercept_final_response_ = true;
1397 interceptor.final_headers_ = TestInterceptor::ok_headers();
1398 interceptor.final_data_ = TestInterceptor::ok_data();
1399
1400 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:191401 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1402 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL));
1403 req->set_method("GET");
1404 req->Start();
[email protected]255620da2013-08-19 13:14:291405 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441406
1407 // Check the interceptor got called as expected
1408 EXPECT_TRUE(interceptor.did_simulate_error_main_);
1409 EXPECT_TRUE(interceptor.did_intercept_final_);
1410
1411 // Check we received one good response
[email protected]f7022f32014-08-21 16:32:191412 EXPECT_TRUE(req->status().is_success());
1413 EXPECT_EQ(200, req->response_headers()->response_code());
[email protected]316c1e5e2012-09-12 15:17:441414 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1415 EXPECT_EQ(1, d.response_started_count());
1416 EXPECT_EQ(0, d.received_redirect_count());
1417}
1418
1419TEST_F(URLRequestTest, InterceptRestartRequired) {
1420 TestInterceptor interceptor;
1421
1422 // restart the main request
1423 interceptor.restart_main_request_ = true;
1424
1425 // then intercept the new main request and respond with an OK response
1426 interceptor.intercept_main_request_ = true;
1427 interceptor.main_headers_ = TestInterceptor::ok_headers();
1428 interceptor.main_data_ = TestInterceptor::ok_data();
1429
1430 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:191431 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1432 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL));
1433 req->set_method("GET");
1434 req->Start();
[email protected]255620da2013-08-19 13:14:291435 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441436
1437 // Check the interceptor got called as expected
1438 EXPECT_TRUE(interceptor.did_restart_main_);
1439 EXPECT_TRUE(interceptor.did_intercept_main_);
1440
1441 // Check we received one good response
[email protected]f7022f32014-08-21 16:32:191442 EXPECT_TRUE(req->status().is_success());
1443 if (req->status().is_success()) {
1444 EXPECT_EQ(200, req->response_headers()->response_code());
[email protected]316c1e5e2012-09-12 15:17:441445 }
1446 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1447 EXPECT_EQ(1, d.response_started_count());
1448 EXPECT_EQ(0, d.received_redirect_count());
1449}
1450
1451TEST_F(URLRequestTest, InterceptRespectsCancelMain) {
1452 TestInterceptor interceptor;
1453
1454 // intercept the main request and cancel from within the restarted job
1455 interceptor.cancel_main_request_ = true;
1456
1457 // setup to intercept final response and override it with an OK response
1458 interceptor.intercept_final_response_ = true;
1459 interceptor.final_headers_ = TestInterceptor::ok_headers();
1460 interceptor.final_data_ = TestInterceptor::ok_data();
1461
1462 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:191463 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1464 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL));
1465 req->set_method("GET");
1466 req->Start();
[email protected]255620da2013-08-19 13:14:291467 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441468
1469 // Check the interceptor got called as expected
1470 EXPECT_TRUE(interceptor.did_cancel_main_);
1471 EXPECT_FALSE(interceptor.did_intercept_final_);
1472
1473 // Check we see a canceled request
[email protected]f7022f32014-08-21 16:32:191474 EXPECT_FALSE(req->status().is_success());
1475 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status());
[email protected]316c1e5e2012-09-12 15:17:441476}
1477
1478TEST_F(URLRequestTest, InterceptRespectsCancelRedirect) {
1479 TestInterceptor interceptor;
1480
1481 // intercept the main request and respond with a redirect
1482 interceptor.intercept_main_request_ = true;
1483 interceptor.main_headers_ = TestInterceptor::redirect_headers();
1484 interceptor.main_data_ = TestInterceptor::redirect_data();
1485
1486 // intercept the redirect and cancel from within that job
1487 interceptor.cancel_redirect_request_ = true;
1488
1489 // setup to intercept final response and override it with an OK response
1490 interceptor.intercept_final_response_ = true;
1491 interceptor.final_headers_ = TestInterceptor::ok_headers();
1492 interceptor.final_data_ = TestInterceptor::ok_data();
1493
1494 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:191495 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1496 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL));
1497 req->set_method("GET");
1498 req->Start();
[email protected]255620da2013-08-19 13:14:291499 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441500
1501 // Check the interceptor got called as expected
1502 EXPECT_TRUE(interceptor.did_intercept_main_);
1503 EXPECT_TRUE(interceptor.did_cancel_redirect_);
1504 EXPECT_FALSE(interceptor.did_intercept_final_);
1505
1506 // Check we see a canceled request
[email protected]f7022f32014-08-21 16:32:191507 EXPECT_FALSE(req->status().is_success());
1508 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status());
[email protected]316c1e5e2012-09-12 15:17:441509}
1510
1511TEST_F(URLRequestTest, InterceptRespectsCancelFinal) {
1512 TestInterceptor interceptor;
1513
1514 // intercept the main request to simulate a network error
1515 interceptor.simulate_main_network_error_ = true;
1516
1517 // setup to intercept final response and cancel from within that job
1518 interceptor.cancel_final_request_ = true;
1519
1520 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:191521 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1522 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL));
1523 req->set_method("GET");
1524 req->Start();
[email protected]255620da2013-08-19 13:14:291525 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441526
1527 // Check the interceptor got called as expected
1528 EXPECT_TRUE(interceptor.did_simulate_error_main_);
1529 EXPECT_TRUE(interceptor.did_cancel_final_);
1530
1531 // Check we see a canceled request
[email protected]f7022f32014-08-21 16:32:191532 EXPECT_FALSE(req->status().is_success());
1533 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status());
[email protected]316c1e5e2012-09-12 15:17:441534}
1535
1536TEST_F(URLRequestTest, InterceptRespectsCancelInRestart) {
1537 TestInterceptor interceptor;
1538
1539 // intercept the main request and cancel then restart from within that job
1540 interceptor.cancel_then_restart_main_request_ = true;
1541
1542 // setup to intercept final response and override it with an OK response
1543 interceptor.intercept_final_response_ = true;
1544 interceptor.final_headers_ = TestInterceptor::ok_headers();
1545 interceptor.final_data_ = TestInterceptor::ok_data();
1546
1547 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:191548 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1549 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL));
1550 req->set_method("GET");
1551 req->Start();
[email protected]255620da2013-08-19 13:14:291552 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441553
1554 // Check the interceptor got called as expected
1555 EXPECT_TRUE(interceptor.did_cancel_then_restart_main_);
1556 EXPECT_FALSE(interceptor.did_intercept_final_);
1557
1558 // Check we see a canceled request
[email protected]f7022f32014-08-21 16:32:191559 EXPECT_FALSE(req->status().is_success());
1560 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status());
[email protected]316c1e5e2012-09-12 15:17:441561}
1562
[email protected]2bba3252013-04-08 19:50:591563LoadTimingInfo RunLoadTimingTest(const LoadTimingInfo& job_load_timing,
1564 URLRequestContext* context) {
1565 TestInterceptor interceptor;
1566 interceptor.intercept_main_request_ = true;
1567 interceptor.main_request_load_timing_info_ = job_load_timing;
1568 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:191569 scoped_ptr<URLRequest> req(context->CreateRequest(
1570 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL));
1571 req->Start();
[email protected]255620da2013-08-19 13:14:291572 base::RunLoop().Run();
[email protected]2bba3252013-04-08 19:50:591573
1574 LoadTimingInfo resulting_load_timing;
[email protected]f7022f32014-08-21 16:32:191575 req->GetLoadTimingInfo(&resulting_load_timing);
[email protected]2bba3252013-04-08 19:50:591576
1577 // None of these should be modified by the URLRequest.
1578 EXPECT_EQ(job_load_timing.socket_reused, resulting_load_timing.socket_reused);
1579 EXPECT_EQ(job_load_timing.socket_log_id, resulting_load_timing.socket_log_id);
1580 EXPECT_EQ(job_load_timing.send_start, resulting_load_timing.send_start);
1581 EXPECT_EQ(job_load_timing.send_end, resulting_load_timing.send_end);
1582 EXPECT_EQ(job_load_timing.receive_headers_end,
1583 resulting_load_timing.receive_headers_end);
1584
1585 return resulting_load_timing;
1586}
1587
1588// "Normal" LoadTimingInfo as returned by a job. Everything is in order, not
1589// reused. |connect_time_flags| is used to indicate if there should be dns
1590// or SSL times, and |used_proxy| is used for proxy times.
1591LoadTimingInfo NormalLoadTimingInfo(base::TimeTicks now,
1592 int connect_time_flags,
1593 bool used_proxy) {
1594 LoadTimingInfo load_timing;
1595 load_timing.socket_log_id = 1;
1596
1597 if (used_proxy) {
1598 load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1);
1599 load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2);
1600 }
1601
1602 LoadTimingInfo::ConnectTiming& connect_timing = load_timing.connect_timing;
1603 if (connect_time_flags & CONNECT_TIMING_HAS_DNS_TIMES) {
1604 connect_timing.dns_start = now + base::TimeDelta::FromDays(3);
1605 connect_timing.dns_end = now + base::TimeDelta::FromDays(4);
1606 }
1607 connect_timing.connect_start = now + base::TimeDelta::FromDays(5);
1608 if (connect_time_flags & CONNECT_TIMING_HAS_SSL_TIMES) {
1609 connect_timing.ssl_start = now + base::TimeDelta::FromDays(6);
1610 connect_timing.ssl_end = now + base::TimeDelta::FromDays(7);
1611 }
1612 connect_timing.connect_end = now + base::TimeDelta::FromDays(8);
1613
1614 load_timing.send_start = now + base::TimeDelta::FromDays(9);
1615 load_timing.send_end = now + base::TimeDelta::FromDays(10);
1616 load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11);
1617 return load_timing;
1618}
1619
1620// Same as above, but in the case of a reused socket.
1621LoadTimingInfo NormalLoadTimingInfoReused(base::TimeTicks now,
1622 bool used_proxy) {
1623 LoadTimingInfo load_timing;
1624 load_timing.socket_log_id = 1;
1625 load_timing.socket_reused = true;
1626
1627 if (used_proxy) {
1628 load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1);
1629 load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2);
1630 }
1631
1632 load_timing.send_start = now + base::TimeDelta::FromDays(9);
1633 load_timing.send_end = now + base::TimeDelta::FromDays(10);
1634 load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11);
1635 return load_timing;
1636}
1637
1638// Basic test that the intercept + load timing tests work.
1639TEST_F(URLRequestTest, InterceptLoadTiming) {
1640 base::TimeTicks now = base::TimeTicks::Now();
1641 LoadTimingInfo job_load_timing =
1642 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, false);
1643
[email protected]2ca01e52013-10-31 22:05:191644 LoadTimingInfo load_timing_result =
1645 RunLoadTimingTest(job_load_timing, &default_context_);
[email protected]2bba3252013-04-08 19:50:591646
1647 // Nothing should have been changed by the URLRequest.
1648 EXPECT_EQ(job_load_timing.proxy_resolve_start,
1649 load_timing_result.proxy_resolve_start);
1650 EXPECT_EQ(job_load_timing.proxy_resolve_end,
1651 load_timing_result.proxy_resolve_end);
1652 EXPECT_EQ(job_load_timing.connect_timing.dns_start,
1653 load_timing_result.connect_timing.dns_start);
1654 EXPECT_EQ(job_load_timing.connect_timing.dns_end,
1655 load_timing_result.connect_timing.dns_end);
1656 EXPECT_EQ(job_load_timing.connect_timing.connect_start,
1657 load_timing_result.connect_timing.connect_start);
1658 EXPECT_EQ(job_load_timing.connect_timing.connect_end,
1659 load_timing_result.connect_timing.connect_end);
1660 EXPECT_EQ(job_load_timing.connect_timing.ssl_start,
1661 load_timing_result.connect_timing.ssl_start);
1662 EXPECT_EQ(job_load_timing.connect_timing.ssl_end,
1663 load_timing_result.connect_timing.ssl_end);
1664
1665 // Redundant sanity check.
1666 TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_DNS_TIMES);
1667}
1668
1669// Another basic test, with proxy and SSL times, but no DNS times.
1670TEST_F(URLRequestTest, InterceptLoadTimingProxy) {
1671 base::TimeTicks now = base::TimeTicks::Now();
1672 LoadTimingInfo job_load_timing =
1673 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, true);
1674
[email protected]2ca01e52013-10-31 22:05:191675 LoadTimingInfo load_timing_result =
1676 RunLoadTimingTest(job_load_timing, &default_context_);
[email protected]2bba3252013-04-08 19:50:591677
1678 // Nothing should have been changed by the URLRequest.
1679 EXPECT_EQ(job_load_timing.proxy_resolve_start,
1680 load_timing_result.proxy_resolve_start);
1681 EXPECT_EQ(job_load_timing.proxy_resolve_end,
1682 load_timing_result.proxy_resolve_end);
1683 EXPECT_EQ(job_load_timing.connect_timing.dns_start,
1684 load_timing_result.connect_timing.dns_start);
1685 EXPECT_EQ(job_load_timing.connect_timing.dns_end,
1686 load_timing_result.connect_timing.dns_end);
1687 EXPECT_EQ(job_load_timing.connect_timing.connect_start,
1688 load_timing_result.connect_timing.connect_start);
1689 EXPECT_EQ(job_load_timing.connect_timing.connect_end,
1690 load_timing_result.connect_timing.connect_end);
1691 EXPECT_EQ(job_load_timing.connect_timing.ssl_start,
1692 load_timing_result.connect_timing.ssl_start);
1693 EXPECT_EQ(job_load_timing.connect_timing.ssl_end,
1694 load_timing_result.connect_timing.ssl_end);
1695
1696 // Redundant sanity check.
1697 TestLoadTimingNotReusedWithProxy(load_timing_result,
1698 CONNECT_TIMING_HAS_SSL_TIMES);
1699}
1700
1701// Make sure that URLRequest correctly adjusts proxy times when they're before
1702// |request_start|, due to already having a connected socket. This happens in
[email protected]cf4cae32014-05-27 00:39:101703// the case of reusing a SPDY session. The connected socket is not considered
1704// reused in this test (May be a preconnect).
[email protected]2bba3252013-04-08 19:50:591705//
1706// To mix things up from the test above, assumes DNS times but no SSL times.
1707TEST_F(URLRequestTest, InterceptLoadTimingEarlyProxyResolution) {
1708 base::TimeTicks now = base::TimeTicks::Now();
1709 LoadTimingInfo job_load_timing =
1710 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, true);
1711 job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(6);
1712 job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(5);
1713 job_load_timing.connect_timing.dns_start = now - base::TimeDelta::FromDays(4);
1714 job_load_timing.connect_timing.dns_end = now - base::TimeDelta::FromDays(3);
1715 job_load_timing.connect_timing.connect_start =
1716 now - base::TimeDelta::FromDays(2);
1717 job_load_timing.connect_timing.connect_end =
1718 now - base::TimeDelta::FromDays(1);
1719
[email protected]2ca01e52013-10-31 22:05:191720 LoadTimingInfo load_timing_result =
1721 RunLoadTimingTest(job_load_timing, &default_context_);
[email protected]2bba3252013-04-08 19:50:591722
1723 // Proxy times, connect times, and DNS times should all be replaced with
1724 // request_start.
1725 EXPECT_EQ(load_timing_result.request_start,
1726 load_timing_result.proxy_resolve_start);
1727 EXPECT_EQ(load_timing_result.request_start,
1728 load_timing_result.proxy_resolve_end);
1729 EXPECT_EQ(load_timing_result.request_start,
1730 load_timing_result.connect_timing.dns_start);
1731 EXPECT_EQ(load_timing_result.request_start,
1732 load_timing_result.connect_timing.dns_end);
1733 EXPECT_EQ(load_timing_result.request_start,
1734 load_timing_result.connect_timing.connect_start);
1735 EXPECT_EQ(load_timing_result.request_start,
1736 load_timing_result.connect_timing.connect_end);
1737
1738 // Other times should have been left null.
1739 TestLoadTimingNotReusedWithProxy(load_timing_result,
1740 CONNECT_TIMING_HAS_DNS_TIMES);
1741}
1742
1743// Same as above, but in the reused case.
1744TEST_F(URLRequestTest, InterceptLoadTimingEarlyProxyResolutionReused) {
1745 base::TimeTicks now = base::TimeTicks::Now();
1746 LoadTimingInfo job_load_timing = NormalLoadTimingInfoReused(now, true);
1747 job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(4);
1748 job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(3);
1749
[email protected]2ca01e52013-10-31 22:05:191750 LoadTimingInfo load_timing_result =
1751 RunLoadTimingTest(job_load_timing, &default_context_);
[email protected]2bba3252013-04-08 19:50:591752
1753 // Proxy times and connect times should all be replaced with request_start.
1754 EXPECT_EQ(load_timing_result.request_start,
1755 load_timing_result.proxy_resolve_start);
1756 EXPECT_EQ(load_timing_result.request_start,
1757 load_timing_result.proxy_resolve_end);
1758
1759 // Other times should have been left null.
1760 TestLoadTimingReusedWithProxy(load_timing_result);
1761}
1762
1763// Make sure that URLRequest correctly adjusts connect times when they're before
1764// |request_start|, due to reusing a connected socket. The connected socket is
1765// not considered reused in this test (May be a preconnect).
1766//
1767// To mix things up, the request has SSL times, but no DNS times.
1768TEST_F(URLRequestTest, InterceptLoadTimingEarlyConnect) {
1769 base::TimeTicks now = base::TimeTicks::Now();
1770 LoadTimingInfo job_load_timing =
1771 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, false);
1772 job_load_timing.connect_timing.connect_start =
1773 now - base::TimeDelta::FromDays(1);
1774 job_load_timing.connect_timing.ssl_start = now - base::TimeDelta::FromDays(2);
1775 job_load_timing.connect_timing.ssl_end = now - base::TimeDelta::FromDays(3);
1776 job_load_timing.connect_timing.connect_end =
1777 now - base::TimeDelta::FromDays(4);
1778
[email protected]2ca01e52013-10-31 22:05:191779 LoadTimingInfo load_timing_result =
1780 RunLoadTimingTest(job_load_timing, &default_context_);
[email protected]2bba3252013-04-08 19:50:591781
1782 // Connect times, and SSL times should be replaced with request_start.
1783 EXPECT_EQ(load_timing_result.request_start,
1784 load_timing_result.connect_timing.connect_start);
1785 EXPECT_EQ(load_timing_result.request_start,
1786 load_timing_result.connect_timing.ssl_start);
1787 EXPECT_EQ(load_timing_result.request_start,
1788 load_timing_result.connect_timing.ssl_end);
1789 EXPECT_EQ(load_timing_result.request_start,
1790 load_timing_result.connect_timing.connect_end);
1791
1792 // Other times should have been left null.
1793 TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_SSL_TIMES);
1794}
1795
1796// Make sure that URLRequest correctly adjusts connect times when they're before
1797// |request_start|, due to reusing a connected socket in the case that there
1798// are also proxy times. The connected socket is not considered reused in this
1799// test (May be a preconnect).
1800//
1801// In this test, there are no SSL or DNS times.
1802TEST_F(URLRequestTest, InterceptLoadTimingEarlyConnectWithProxy) {
1803 base::TimeTicks now = base::TimeTicks::Now();
1804 LoadTimingInfo job_load_timing =
1805 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY, true);
1806 job_load_timing.connect_timing.connect_start =
1807 now - base::TimeDelta::FromDays(1);
1808 job_load_timing.connect_timing.connect_end =
1809 now - base::TimeDelta::FromDays(2);
1810
[email protected]2ca01e52013-10-31 22:05:191811 LoadTimingInfo load_timing_result =
1812 RunLoadTimingTest(job_load_timing, &default_context_);
[email protected]2bba3252013-04-08 19:50:591813
1814 // Connect times should be replaced with proxy_resolve_end.
1815 EXPECT_EQ(load_timing_result.proxy_resolve_end,
1816 load_timing_result.connect_timing.connect_start);
1817 EXPECT_EQ(load_timing_result.proxy_resolve_end,
1818 load_timing_result.connect_timing.connect_end);
1819
1820 // Other times should have been left null.
1821 TestLoadTimingNotReusedWithProxy(load_timing_result,
1822 CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY);
1823}
1824
[email protected]316c1e5e2012-09-12 15:17:441825// Check that two different URL requests have different identifiers.
1826TEST_F(URLRequestTest, Identifiers) {
1827 TestDelegate d;
1828 TestURLRequestContext context;
[email protected]2ca01e52013-10-31 22:05:191829 TestURLRequest req(
1830 GURL("http://example.com"), DEFAULT_PRIORITY, &d, &context);
1831 TestURLRequest other_req(
1832 GURL("http://example.com"), DEFAULT_PRIORITY, &d, &context);
[email protected]316c1e5e2012-09-12 15:17:441833
1834 ASSERT_NE(req.identifier(), other_req.identifier());
1835}
1836
1837// Check that a failure to connect to the proxy is reported to the network
1838// delegate.
1839TEST_F(URLRequestTest, NetworkDelegateProxyError) {
1840 MockHostResolver host_resolver;
1841 host_resolver.rules()->AddSimulatedFailure("*");
1842
[email protected]ceefd7fd2012-11-29 00:36:241843 TestNetworkDelegate network_delegate; // Must outlive URLRequests.
[email protected]316c1e5e2012-09-12 15:17:441844 TestURLRequestContextWithProxy context("myproxy:70", &network_delegate);
1845
1846 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:191847 scoped_ptr<URLRequest> req(context.CreateRequest(
1848 GURL("http://example.com"), DEFAULT_PRIORITY, &d, NULL));
1849 req->set_method("GET");
[email protected]316c1e5e2012-09-12 15:17:441850
[email protected]f7022f32014-08-21 16:32:191851 req->Start();
[email protected]255620da2013-08-19 13:14:291852 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441853
1854 // Check we see a failed request.
[email protected]f7022f32014-08-21 16:32:191855 EXPECT_FALSE(req->status().is_success());
[email protected]d8fc4722014-06-13 13:17:151856 // The proxy server is not set before failure.
[email protected]f7022f32014-08-21 16:32:191857 EXPECT_TRUE(req->proxy_server().IsEmpty());
1858 EXPECT_EQ(URLRequestStatus::FAILED, req->status().status());
1859 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, req->status().error());
[email protected]316c1e5e2012-09-12 15:17:441860
1861 EXPECT_EQ(1, network_delegate.error_count());
1862 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, network_delegate.last_error());
1863 EXPECT_EQ(1, network_delegate.completed_requests());
1864}
1865
[email protected]cba24642014-08-15 20:49:591866// Make sure that NetworkDelegate::NotifyCompleted is called if
[email protected]316c1e5e2012-09-12 15:17:441867// content is empty.
1868TEST_F(URLRequestTest, RequestCompletionForEmptyResponse) {
1869 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:191870 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1871 GURL("data:,"), DEFAULT_PRIORITY, &d, NULL));
1872 req->Start();
[email protected]255620da2013-08-19 13:14:291873 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441874 EXPECT_EQ("", d.data_received());
1875 EXPECT_EQ(1, default_network_delegate_.completed_requests());
1876}
1877
[email protected]5033ab82013-03-22 20:17:461878// Make sure that SetPriority actually sets the URLRequest's priority
1879// correctly, both before and after start.
1880TEST_F(URLRequestTest, SetPriorityBasic) {
1881 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:191882 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1883 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL));
1884 EXPECT_EQ(DEFAULT_PRIORITY, req->priority());
[email protected]5033ab82013-03-22 20:17:461885
[email protected]f7022f32014-08-21 16:32:191886 req->SetPriority(LOW);
1887 EXPECT_EQ(LOW, req->priority());
[email protected]5033ab82013-03-22 20:17:461888
[email protected]f7022f32014-08-21 16:32:191889 req->Start();
1890 EXPECT_EQ(LOW, req->priority());
[email protected]5033ab82013-03-22 20:17:461891
[email protected]f7022f32014-08-21 16:32:191892 req->SetPriority(MEDIUM);
1893 EXPECT_EQ(MEDIUM, req->priority());
[email protected]5033ab82013-03-22 20:17:461894}
1895
1896// Make sure that URLRequest calls SetPriority on a job before calling
1897// Start on it.
1898TEST_F(URLRequestTest, SetJobPriorityBeforeJobStart) {
1899 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:191900 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1901 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL));
1902 EXPECT_EQ(DEFAULT_PRIORITY, req->priority());
[email protected]5033ab82013-03-22 20:17:461903
1904 scoped_refptr<URLRequestTestJob> job =
[email protected]f7022f32014-08-21 16:32:191905 new URLRequestTestJob(req.get(), &default_network_delegate_);
[email protected]90499482013-06-01 00:39:501906 AddTestInterceptor()->set_main_intercept_job(job.get());
[email protected]5033ab82013-03-22 20:17:461907 EXPECT_EQ(DEFAULT_PRIORITY, job->priority());
1908
[email protected]f7022f32014-08-21 16:32:191909 req->SetPriority(LOW);
[email protected]5033ab82013-03-22 20:17:461910
[email protected]f7022f32014-08-21 16:32:191911 req->Start();
[email protected]5033ab82013-03-22 20:17:461912 EXPECT_EQ(LOW, job->priority());
1913}
1914
1915// Make sure that URLRequest passes on its priority updates to its
1916// job.
1917TEST_F(URLRequestTest, SetJobPriority) {
1918 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:191919 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1920 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL));
[email protected]5033ab82013-03-22 20:17:461921
1922 scoped_refptr<URLRequestTestJob> job =
[email protected]f7022f32014-08-21 16:32:191923 new URLRequestTestJob(req.get(), &default_network_delegate_);
[email protected]90499482013-06-01 00:39:501924 AddTestInterceptor()->set_main_intercept_job(job.get());
[email protected]5033ab82013-03-22 20:17:461925
[email protected]f7022f32014-08-21 16:32:191926 req->SetPriority(LOW);
1927 req->Start();
[email protected]5033ab82013-03-22 20:17:461928 EXPECT_EQ(LOW, job->priority());
1929
[email protected]f7022f32014-08-21 16:32:191930 req->SetPriority(MEDIUM);
1931 EXPECT_EQ(MEDIUM, req->priority());
[email protected]5033ab82013-03-22 20:17:461932 EXPECT_EQ(MEDIUM, job->priority());
1933}
1934
[email protected]bb1c4662013-11-14 00:00:071935// Setting the IGNORE_LIMITS load flag should be okay if the priority
1936// is MAXIMUM_PRIORITY.
1937TEST_F(URLRequestTest, PriorityIgnoreLimits) {
1938 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:191939 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1940 GURL("http://test_intercept/foo"), MAXIMUM_PRIORITY, &d, NULL));
1941 EXPECT_EQ(MAXIMUM_PRIORITY, req->priority());
[email protected]bb1c4662013-11-14 00:00:071942
1943 scoped_refptr<URLRequestTestJob> job =
[email protected]f7022f32014-08-21 16:32:191944 new URLRequestTestJob(req.get(), &default_network_delegate_);
[email protected]bb1c4662013-11-14 00:00:071945 AddTestInterceptor()->set_main_intercept_job(job.get());
1946
[email protected]f7022f32014-08-21 16:32:191947 req->SetLoadFlags(LOAD_IGNORE_LIMITS);
1948 EXPECT_EQ(MAXIMUM_PRIORITY, req->priority());
[email protected]bb1c4662013-11-14 00:00:071949
[email protected]f7022f32014-08-21 16:32:191950 req->SetPriority(MAXIMUM_PRIORITY);
1951 EXPECT_EQ(MAXIMUM_PRIORITY, req->priority());
[email protected]bb1c4662013-11-14 00:00:071952
[email protected]f7022f32014-08-21 16:32:191953 req->Start();
1954 EXPECT_EQ(MAXIMUM_PRIORITY, req->priority());
[email protected]bb1c4662013-11-14 00:00:071955 EXPECT_EQ(MAXIMUM_PRIORITY, job->priority());
1956}
1957
[email protected]ce7d0cbc2013-05-03 18:57:221958// TODO(droger): Support SpawnedTestServer on iOS (see http://crbug.com/148666).
[email protected]316c1e5e2012-09-12 15:17:441959#if !defined(OS_IOS)
[email protected]ce7d0cbc2013-05-03 18:57:221960// A subclass of SpawnedTestServer that uses a statically-configured hostname.
1961// This is to work around mysterious failures in chrome_frame_net_tests. See:
[email protected]316c1e5e2012-09-12 15:17:441962// http://crbug.com/114369
[email protected]cd6f2522014-01-16 18:27:351963// TODO(erikwright): remove or update as needed; see http://crbug.com/334634.
[email protected]ce7d0cbc2013-05-03 18:57:221964class LocalHttpTestServer : public SpawnedTestServer {
[email protected]316c1e5e2012-09-12 15:17:441965 public:
[email protected]6cdfd7f2013-02-08 20:40:151966 explicit LocalHttpTestServer(const base::FilePath& document_root)
[email protected]ce7d0cbc2013-05-03 18:57:221967 : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP,
1968 ScopedCustomUrlRequestTestHttpHost::value(),
1969 document_root) {}
[email protected]316c1e5e2012-09-12 15:17:441970 LocalHttpTestServer()
[email protected]ce7d0cbc2013-05-03 18:57:221971 : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP,
1972 ScopedCustomUrlRequestTestHttpHost::value(),
1973 base::FilePath()) {}
[email protected]316c1e5e2012-09-12 15:17:441974};
1975
[email protected]f2f31b32013-01-16 23:24:091976TEST_F(URLRequestTest, DelayedCookieCallback) {
[email protected]316c1e5e2012-09-12 15:17:441977 LocalHttpTestServer test_server;
1978 ASSERT_TRUE(test_server.Start());
1979
1980 TestURLRequestContext context;
1981 scoped_refptr<DelayedCookieMonster> delayed_cm =
1982 new DelayedCookieMonster();
1983 scoped_refptr<CookieStore> cookie_store = delayed_cm;
[email protected]90499482013-06-01 00:39:501984 context.set_cookie_store(delayed_cm.get());
[email protected]316c1e5e2012-09-12 15:17:441985
1986 // Set up a cookie.
1987 {
1988 TestNetworkDelegate network_delegate;
1989 context.set_network_delegate(&network_delegate);
1990 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:191991 scoped_ptr<URLRequest> req(context.CreateRequest(
1992 test_server.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY,
1993 &d, NULL));
1994 req->Start();
[email protected]255620da2013-08-19 13:14:291995 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:441996 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
1997 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
1998 EXPECT_EQ(1, network_delegate.set_cookie_count());
1999 }
2000
2001 // Verify that the cookie is set.
2002 {
2003 TestNetworkDelegate network_delegate;
2004 context.set_network_delegate(&network_delegate);
2005 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192006 scoped_ptr<URLRequest> req(context.CreateRequest(
2007 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL));
2008 req->Start();
[email protected]255620da2013-08-19 13:14:292009 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442010
2011 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2012 != std::string::npos);
2013 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2014 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2015 }
2016}
2017
[email protected]f2f31b32013-01-16 23:24:092018TEST_F(URLRequestTest, DoNotSendCookies) {
[email protected]316c1e5e2012-09-12 15:17:442019 LocalHttpTestServer test_server;
2020 ASSERT_TRUE(test_server.Start());
2021
2022 // Set up a cookie.
2023 {
2024 TestNetworkDelegate network_delegate;
2025 default_context_.set_network_delegate(&network_delegate);
2026 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192027 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2028 test_server.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY,
2029 &d, NULL));
2030 req->Start();
[email protected]255620da2013-08-19 13:14:292031 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442032 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2033 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2034 }
2035
2036 // Verify that the cookie is set.
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("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL));
2043 req->Start();
[email protected]255620da2013-08-19 13:14:292044 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442045
2046 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2047 != std::string::npos);
2048 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2049 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2050 }
2051
2052 // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set.
2053 {
2054 TestNetworkDelegate network_delegate;
2055 default_context_.set_network_delegate(&network_delegate);
2056 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192057 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2058 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL));
2059 req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES);
2060 req->Start();
[email protected]255620da2013-08-19 13:14:292061 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442062
2063 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
2064 == std::string::npos);
2065
2066 // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies.
2067 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2068 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2069 }
2070}
2071
2072TEST_F(URLRequestTest, DoNotSaveCookies) {
2073 LocalHttpTestServer test_server;
2074 ASSERT_TRUE(test_server.Start());
2075
2076 // Set up a cookie.
2077 {
2078 TestNetworkDelegate network_delegate;
2079 default_context_.set_network_delegate(&network_delegate);
2080 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192081 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2082 test_server.GetURL("set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY,
2083 &d, NULL));
2084 req->Start();
[email protected]255620da2013-08-19 13:14:292085 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442086
2087 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2088 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2089 EXPECT_EQ(1, network_delegate.set_cookie_count());
2090 }
2091
2092 // Try to set-up another cookie and update the previous cookie.
2093 {
2094 TestNetworkDelegate network_delegate;
2095 default_context_.set_network_delegate(&network_delegate);
2096 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192097 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
[email protected]316c1e5e2012-09-12 15:17:442098 test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
[email protected]f7022f32014-08-21 16:32:192099 DEFAULT_PRIORITY, &d, NULL));
2100 req->SetLoadFlags(LOAD_DO_NOT_SAVE_COOKIES);
2101 req->Start();
[email protected]316c1e5e2012-09-12 15:17:442102
[email protected]255620da2013-08-19 13:14:292103 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442104
2105 // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie.
2106 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2107 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2108 EXPECT_EQ(0, network_delegate.set_cookie_count());
2109 }
2110
2111 // Verify the cookies weren't saved or updated.
2112 {
2113 TestNetworkDelegate network_delegate;
2114 default_context_.set_network_delegate(&network_delegate);
2115 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192116 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2117 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL));
2118 req->Start();
[email protected]255620da2013-08-19 13:14:292119 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442120
2121 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
2122 == std::string::npos);
2123 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
2124 != std::string::npos);
2125
2126 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2127 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2128 EXPECT_EQ(0, network_delegate.set_cookie_count());
2129 }
2130}
2131
2132TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) {
2133 LocalHttpTestServer test_server;
2134 ASSERT_TRUE(test_server.Start());
2135
2136 // Set up a cookie.
2137 {
2138 TestNetworkDelegate network_delegate;
2139 default_context_.set_network_delegate(&network_delegate);
2140 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192141 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2142 test_server.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY,
2143 &d, NULL));
2144 req->Start();
[email protected]255620da2013-08-19 13:14:292145 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442146
2147 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2148 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2149 }
2150
2151 // Verify that the cookie is set.
2152 {
2153 TestNetworkDelegate network_delegate;
2154 default_context_.set_network_delegate(&network_delegate);
2155 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192156 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2157 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &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_TRUE(d.data_received().find("CookieToNotSend=1")
2162 != std::string::npos);
2163
2164 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2165 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2166 }
2167
2168 // Verify that the cookie isn't sent.
2169 {
2170 TestNetworkDelegate network_delegate;
2171 default_context_.set_network_delegate(&network_delegate);
2172 TestDelegate d;
2173 network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES);
[email protected]f7022f32014-08-21 16:32:192174 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2175 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL));
2176 req->Start();
[email protected]255620da2013-08-19 13:14:292177 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442178
2179 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
2180 == std::string::npos);
2181
[email protected]22e045f2013-09-20 03:54:032182 EXPECT_EQ(1, network_delegate.blocked_get_cookies_count());
[email protected]316c1e5e2012-09-12 15:17:442183 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2184 }
2185}
2186
2187TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) {
2188 LocalHttpTestServer test_server;
2189 ASSERT_TRUE(test_server.Start());
2190
2191 // Set up a cookie.
2192 {
2193 TestNetworkDelegate network_delegate;
2194 default_context_.set_network_delegate(&network_delegate);
2195 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192196 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2197 test_server.GetURL("set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY,
2198 &d, NULL));
2199 req->Start();
[email protected]255620da2013-08-19 13:14:292200 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442201
2202 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2203 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2204 }
2205
2206 // Try to set-up another cookie and update the previous cookie.
2207 {
2208 TestNetworkDelegate network_delegate;
2209 default_context_.set_network_delegate(&network_delegate);
2210 TestDelegate d;
2211 network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE);
[email protected]f7022f32014-08-21 16:32:192212 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
[email protected]316c1e5e2012-09-12 15:17:442213 test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
[email protected]f7022f32014-08-21 16:32:192214 DEFAULT_PRIORITY, &d, NULL));
2215 req->Start();
[email protected]316c1e5e2012-09-12 15:17:442216
[email protected]255620da2013-08-19 13:14:292217 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442218
2219 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2220 EXPECT_EQ(2, network_delegate.blocked_set_cookie_count());
2221 }
2222
2223 // Verify the cookies weren't saved or updated.
2224 {
2225 TestNetworkDelegate network_delegate;
2226 default_context_.set_network_delegate(&network_delegate);
2227 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192228 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2229 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL));
2230 req->Start();
[email protected]255620da2013-08-19 13:14:292231 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442232
2233 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
2234 == std::string::npos);
2235 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
2236 != std::string::npos);
2237
2238 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2239 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2240 }
2241}
2242
2243TEST_F(URLRequestTest, DoNotSaveEmptyCookies) {
2244 LocalHttpTestServer test_server;
2245 ASSERT_TRUE(test_server.Start());
2246
2247 // Set up an empty cookie.
2248 {
2249 TestNetworkDelegate network_delegate;
2250 default_context_.set_network_delegate(&network_delegate);
2251 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192252 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2253 test_server.GetURL("set-cookie"), DEFAULT_PRIORITY, &d, NULL));
2254 req->Start();
[email protected]255620da2013-08-19 13:14:292255 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442256
2257 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2258 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2259 EXPECT_EQ(0, network_delegate.set_cookie_count());
2260 }
2261}
2262
2263TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) {
2264 LocalHttpTestServer test_server;
2265 ASSERT_TRUE(test_server.Start());
2266
2267 // Set up a cookie.
2268 {
2269 TestNetworkDelegate network_delegate;
2270 default_context_.set_network_delegate(&network_delegate);
2271 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192272 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2273 test_server.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY,
2274 &d, NULL));
2275 req->Start();
[email protected]255620da2013-08-19 13:14:292276 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442277
2278 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2279 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2280 }
2281
2282 // Verify that the cookie is set.
2283 {
2284 TestNetworkDelegate network_delegate;
2285 default_context_.set_network_delegate(&network_delegate);
2286 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192287 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2288 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &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_TRUE(d.data_received().find("CookieToNotSend=1")
2293 != std::string::npos);
2294
2295 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2296 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2297 }
2298
2299 // Verify that the cookie isn't sent.
2300 {
2301 TestNetworkDelegate network_delegate;
2302 default_context_.set_network_delegate(&network_delegate);
2303 TestDelegate d;
2304 network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES);
[email protected]f7022f32014-08-21 16:32:192305 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2306 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL));
2307 req->Start();
[email protected]255620da2013-08-19 13:14:292308 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442309
2310 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
2311 == std::string::npos);
2312
[email protected]22e045f2013-09-20 03:54:032313 EXPECT_EQ(1, network_delegate.blocked_get_cookies_count());
[email protected]316c1e5e2012-09-12 15:17:442314 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2315 }
2316}
2317
2318TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) {
2319 LocalHttpTestServer test_server;
2320 ASSERT_TRUE(test_server.Start());
2321
2322 // Set up a cookie.
2323 {
2324 TestNetworkDelegate network_delegate;
2325 default_context_.set_network_delegate(&network_delegate);
2326 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192327 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2328 test_server.GetURL("set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY,
2329 &d, NULL));
2330 req->Start();
[email protected]255620da2013-08-19 13:14:292331 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442332
2333 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2334 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2335 }
2336
2337 // Try to set-up another cookie and update the previous cookie.
2338 {
2339 TestNetworkDelegate network_delegate;
2340 default_context_.set_network_delegate(&network_delegate);
2341 TestDelegate d;
2342 network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE);
[email protected]f7022f32014-08-21 16:32:192343 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
[email protected]316c1e5e2012-09-12 15:17:442344 test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
[email protected]f7022f32014-08-21 16:32:192345 DEFAULT_PRIORITY, &d, NULL));
2346 req->Start();
[email protected]316c1e5e2012-09-12 15:17:442347
[email protected]255620da2013-08-19 13:14:292348 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442349
2350 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2351 EXPECT_EQ(2, network_delegate.blocked_set_cookie_count());
2352 }
2353
2354 // Verify the cookies weren't saved or updated.
2355 {
2356 TestNetworkDelegate network_delegate;
2357 default_context_.set_network_delegate(&network_delegate);
2358 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192359 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2360 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL));
2361 req->Start();
[email protected]255620da2013-08-19 13:14:292362 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442363
2364 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
2365 == std::string::npos);
2366 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
2367 != std::string::npos);
2368
2369 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2370 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2371 }
2372}
2373
[email protected]5095cd72012-11-01 10:29:162374// FixedDateNetworkDelegate swaps out the server's HTTP Date response header
2375// value for the |fixed_date| argument given to the constructor.
2376class FixedDateNetworkDelegate : public TestNetworkDelegate {
2377 public:
2378 explicit FixedDateNetworkDelegate(const std::string& fixed_date)
2379 : fixed_date_(fixed_date) {}
2380 virtual ~FixedDateNetworkDelegate() {}
2381
[email protected]cba24642014-08-15 20:49:592382 // NetworkDelegate implementation
[email protected]5095cd72012-11-01 10:29:162383 virtual int OnHeadersReceived(
[email protected]cba24642014-08-15 20:49:592384 URLRequest* request,
2385 const CompletionCallback& callback,
2386 const HttpResponseHeaders* original_response_headers,
2387 scoped_refptr<HttpResponseHeaders>* override_response_headers,
[email protected]5f714132014-03-26 10:41:162388 GURL* allowed_unsafe_redirect_url) OVERRIDE;
[email protected]5095cd72012-11-01 10:29:162389
2390 private:
2391 std::string fixed_date_;
2392
2393 DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate);
2394};
2395
2396int FixedDateNetworkDelegate::OnHeadersReceived(
[email protected]cba24642014-08-15 20:49:592397 URLRequest* request,
2398 const CompletionCallback& callback,
2399 const HttpResponseHeaders* original_response_headers,
2400 scoped_refptr<HttpResponseHeaders>* override_response_headers,
[email protected]5f714132014-03-26 10:41:162401 GURL* allowed_unsafe_redirect_url) {
[email protected]cba24642014-08-15 20:49:592402 HttpResponseHeaders* new_response_headers =
2403 new HttpResponseHeaders(original_response_headers->raw_headers());
[email protected]5095cd72012-11-01 10:29:162404
2405 new_response_headers->RemoveHeader("Date");
2406 new_response_headers->AddHeader("Date: " + fixed_date_);
2407
2408 *override_response_headers = new_response_headers;
2409 return TestNetworkDelegate::OnHeadersReceived(request,
2410 callback,
2411 original_response_headers,
[email protected]5f714132014-03-26 10:41:162412 override_response_headers,
2413 allowed_unsafe_redirect_url);
[email protected]5095cd72012-11-01 10:29:162414}
2415
2416// Test that cookie expiration times are adjusted for server/client clock
2417// skew and that we handle incorrect timezone specifier "UTC" in HTTP Date
2418// headers by defaulting to GMT. (crbug.com/135131)
2419TEST_F(URLRequestTest, AcceptClockSkewCookieWithWrongDateTimezone) {
2420 LocalHttpTestServer test_server;
2421 ASSERT_TRUE(test_server.Start());
2422
2423 // Set up an expired cookie.
2424 {
2425 TestNetworkDelegate network_delegate;
2426 default_context_.set_network_delegate(&network_delegate);
2427 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192428 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
[email protected]2ca01e52013-10-31 22:05:192429 test_server.GetURL(
2430 "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"),
[email protected]f7022f32014-08-21 16:32:192431 DEFAULT_PRIORITY, &d, NULL));
2432 req->Start();
[email protected]255620da2013-08-19 13:14:292433 base::RunLoop().Run();
[email protected]5095cd72012-11-01 10:29:162434 }
2435 // Verify that the cookie is not set.
2436 {
2437 TestNetworkDelegate network_delegate;
2438 default_context_.set_network_delegate(&network_delegate);
2439 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192440 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2441 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL));
2442 req->Start();
[email protected]255620da2013-08-19 13:14:292443 base::RunLoop().Run();
[email protected]5095cd72012-11-01 10:29:162444
2445 EXPECT_TRUE(d.data_received().find("StillGood=1") == std::string::npos);
2446 }
2447 // Set up a cookie with clock skew and "UTC" HTTP Date timezone specifier.
2448 {
2449 FixedDateNetworkDelegate network_delegate("18-Apr-1977 22:49:13 UTC");
2450 default_context_.set_network_delegate(&network_delegate);
2451 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192452 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
[email protected]2ca01e52013-10-31 22:05:192453 test_server.GetURL(
2454 "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"),
[email protected]f7022f32014-08-21 16:32:192455 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 // Verify that the cookie is set.
2460 {
2461 TestNetworkDelegate network_delegate;
2462 default_context_.set_network_delegate(&network_delegate);
2463 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192464 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2465 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL));
2466 req->Start();
[email protected]255620da2013-08-19 13:14:292467 base::RunLoop().Run();
[email protected]5095cd72012-11-01 10:29:162468
2469 EXPECT_TRUE(d.data_received().find("StillGood=1") != std::string::npos);
2470 }
2471}
2472
2473
[email protected]316c1e5e2012-09-12 15:17:442474// Check that it is impossible to change the referrer in the extra headers of
2475// an URLRequest.
2476TEST_F(URLRequestTest, DoNotOverrideReferrer) {
2477 LocalHttpTestServer test_server;
2478 ASSERT_TRUE(test_server.Start());
2479
2480 // If extra headers contain referer and the request contains a referer,
2481 // only the latter shall be respected.
2482 {
2483 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192484 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2485 test_server.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d, NULL));
2486 req->SetReferrer("http://foo.com/");
[email protected]316c1e5e2012-09-12 15:17:442487
2488 HttpRequestHeaders headers;
2489 headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/");
[email protected]f7022f32014-08-21 16:32:192490 req->SetExtraRequestHeaders(headers);
[email protected]316c1e5e2012-09-12 15:17:442491
[email protected]f7022f32014-08-21 16:32:192492 req->Start();
[email protected]255620da2013-08-19 13:14:292493 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442494
2495 EXPECT_EQ("http://foo.com/", d.data_received());
2496 }
2497
2498 // If extra headers contain a referer but the request does not, no referer
2499 // shall be sent in the header.
2500 {
2501 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192502 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2503 test_server.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:442504
2505 HttpRequestHeaders headers;
2506 headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/");
[email protected]f7022f32014-08-21 16:32:192507 req->SetExtraRequestHeaders(headers);
2508 req->SetLoadFlags(LOAD_VALIDATE_CACHE);
[email protected]316c1e5e2012-09-12 15:17:442509
[email protected]f7022f32014-08-21 16:32:192510 req->Start();
[email protected]255620da2013-08-19 13:14:292511 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:442512
2513 EXPECT_EQ("None", d.data_received());
2514 }
2515}
2516
[email protected]b89290212009-08-14 22:37:352517class URLRequestTestHTTP : public URLRequestTest {
[email protected]95409e12010-08-17 20:07:112518 public:
2519 URLRequestTestHTTP()
[email protected]6cdfd7f2013-02-08 20:40:152520 : test_server_(base::FilePath(FILE_PATH_LITERAL(
[email protected]95409e12010-08-17 20:07:112521 "net/data/url_request_unittest"))) {
2522 }
2523
[email protected]b89290212009-08-14 22:37:352524 protected:
[email protected]21184962011-10-26 00:50:302525 // Requests |redirect_url|, which must return a HTTP 3xx redirect.
2526 // |request_method| is the method to use for the initial request.
2527 // |redirect_method| is the method that is expected to be used for the second
2528 // request, after redirection.
2529 // If |include_data| is true, data is uploaded with the request. The
2530 // response body is expected to match it exactly, if and only if
2531 // |request_method| == |redirect_method|.
2532 void HTTPRedirectMethodTest(const GURL& redirect_url,
2533 const std::string& request_method,
2534 const std::string& redirect_method,
2535 bool include_data) {
2536 static const char kData[] = "hello world";
2537 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192538 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2539 redirect_url, DEFAULT_PRIORITY, &d, NULL));
2540 req->set_method(request_method);
[email protected]21184962011-10-26 00:50:302541 if (include_data) {
[email protected]f7022f32014-08-21 16:32:192542 req->set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
[email protected]21184962011-10-26 00:50:302543 HttpRequestHeaders headers;
2544 headers.SetHeader(HttpRequestHeaders::kContentLength,
2545 base::UintToString(arraysize(kData) - 1));
[email protected]f7022f32014-08-21 16:32:192546 req->SetExtraRequestHeaders(headers);
[email protected]21184962011-10-26 00:50:302547 }
[email protected]f7022f32014-08-21 16:32:192548 req->Start();
[email protected]255620da2013-08-19 13:14:292549 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:192550 EXPECT_EQ(redirect_method, req->method());
2551 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
2552 EXPECT_EQ(OK, req->status().error());
[email protected]21184962011-10-26 00:50:302553 if (include_data) {
2554 if (request_method == redirect_method) {
2555 EXPECT_EQ(kData, d.data_received());
2556 } else {
2557 EXPECT_NE(kData, d.data_received());
2558 }
2559 }
2560 if (HasFailure())
2561 LOG(WARNING) << "Request method was: " << request_method;
2562 }
2563
[email protected]762d2db2010-01-11 19:03:012564 void HTTPUploadDataOperationTest(const std::string& method) {
[email protected]762d2db2010-01-11 19:03:012565 const int kMsgSize = 20000; // multiple of 10
2566 const int kIterations = 50;
[email protected]f43b89f32012-05-01 19:39:482567 char* uploadBytes = new char[kMsgSize+1];
2568 char* ptr = uploadBytes;
[email protected]762d2db2010-01-11 19:03:012569 char marker = 'a';
2570 for (int idx = 0; idx < kMsgSize/10; idx++) {
2571 memcpy(ptr, "----------", 10);
2572 ptr += 10;
2573 if (idx % 100 == 0) {
2574 ptr--;
2575 *ptr++ = marker;
2576 if (++marker > 'z')
2577 marker = 'a';
2578 }
2579 }
2580 uploadBytes[kMsgSize] = '\0';
2581
[email protected]762d2db2010-01-11 19:03:012582 for (int i = 0; i < kIterations; ++i) {
2583 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192584 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
2585 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, NULL));
2586 r->set_method(method.c_str());
[email protected]762d2db2010-01-11 19:03:012587
[email protected]f7022f32014-08-21 16:32:192588 r->set_upload(make_scoped_ptr(CreateSimpleUploadData(uploadBytes)));
[email protected]762d2db2010-01-11 19:03:012589
[email protected]f7022f32014-08-21 16:32:192590 r->Start();
2591 EXPECT_TRUE(r->is_pending());
[email protected]762d2db2010-01-11 19:03:012592
[email protected]255620da2013-08-19 13:14:292593 base::RunLoop().Run();
[email protected]762d2db2010-01-11 19:03:012594
[email protected]329b68b2012-11-14 17:54:272595 ASSERT_EQ(1, d.response_started_count())
[email protected]f7022f32014-08-21 16:32:192596 << "request failed: " << r->status().status()
2597 << ", os error: " << r->status().error();
[email protected]762d2db2010-01-11 19:03:012598
2599 EXPECT_FALSE(d.received_data_before_response());
2600 EXPECT_EQ(uploadBytes, d.data_received());
[email protected]762d2db2010-01-11 19:03:012601 }
2602 delete[] uploadBytes;
2603 }
2604
[email protected]ef2bf422012-05-11 03:27:092605 void AddChunksToUpload(URLRequest* r) {
[email protected]0c9bf872011-03-04 17:53:222606 r->AppendChunkToUpload("a", 1, false);
2607 r->AppendChunkToUpload("bcd", 3, false);
2608 r->AppendChunkToUpload("this is a longer chunk than before.", 35, false);
2609 r->AppendChunkToUpload("\r\n\r\n", 4, false);
2610 r->AppendChunkToUpload("0", 1, false);
2611 r->AppendChunkToUpload("2323", 4, true);
[email protected]699efe602011-01-25 07:17:112612 }
2613
[email protected]ef2bf422012-05-11 03:27:092614 void VerifyReceivedDataMatchesChunks(URLRequest* r, TestDelegate* d) {
[email protected]699efe602011-01-25 07:17:112615 // This should match the chunks sent by AddChunksToUpload().
[email protected]329b68b2012-11-14 17:54:272616 const std::string expected_data =
[email protected]699efe602011-01-25 07:17:112617 "abcdthis is a longer chunk than before.\r\n\r\n02323";
2618
[email protected]329b68b2012-11-14 17:54:272619 ASSERT_EQ(1, d->response_started_count())
2620 << "request failed: " << r->status().status()
2621 << ", os error: " << r->status().error();
[email protected]699efe602011-01-25 07:17:112622
2623 EXPECT_FALSE(d->received_data_before_response());
2624
[email protected]329b68b2012-11-14 17:54:272625 EXPECT_EQ(expected_data.size(), static_cast<size_t>(d->bytes_received()));
2626 EXPECT_EQ(expected_data, d->data_received());
[email protected]699efe602011-01-25 07:17:112627 }
2628
[email protected]ede03212012-09-07 12:52:262629 bool DoManyCookiesRequest(int num_cookies) {
[email protected]263163f2012-06-14 22:40:342630 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:192631 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
2632 test_server_.GetURL("set-many-cookies?" +
2633 base::IntToString(num_cookies)),
2634 DEFAULT_PRIORITY, &d, NULL));
[email protected]263163f2012-06-14 22:40:342635
[email protected]f7022f32014-08-21 16:32:192636 r->Start();
2637 EXPECT_TRUE(r->is_pending());
[email protected]263163f2012-06-14 22:40:342638
[email protected]255620da2013-08-19 13:14:292639 base::RunLoop().Run();
[email protected]263163f2012-06-14 22:40:342640
[email protected]f7022f32014-08-21 16:32:192641 bool is_success = r->status().is_success();
[email protected]263163f2012-06-14 22:40:342642
[email protected]ede03212012-09-07 12:52:262643 if (!is_success) {
[email protected]f7022f32014-08-21 16:32:192644 EXPECT_TRUE(r->status().error() == ERR_RESPONSE_HEADERS_TOO_BIG);
[email protected]263163f2012-06-14 22:40:342645 // The test server appears to be unable to handle subsequent requests
2646 // after this error is triggered. Force it to restart.
2647 EXPECT_TRUE(test_server_.Stop());
2648 EXPECT_TRUE(test_server_.Start());
2649 }
2650
2651 return is_success;
2652 }
2653
[email protected]1700c6a2012-02-22 18:07:072654 LocalHttpTestServer test_server_;
[email protected]b89290212009-08-14 22:37:352655};
2656
[email protected]95409e12010-08-17 20:07:112657// In this unit test, we're using the HTTPTestServer as a proxy server and
2658// issuing a CONNECT request with the magic host name "www.redirect.com".
2659// The HTTPTestServer will return a 302 response, which we should not
2660// follow.
[email protected]f2f31b32013-01-16 23:24:092661TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) {
[email protected]95409e12010-08-17 20:07:112662 ASSERT_TRUE(test_server_.Start());
2663
[email protected]ceefd7fd2012-11-29 00:36:242664 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]d5a4dd62012-05-23 01:41:042665 TestURLRequestContextWithProxy context(
[email protected]2ca01e52013-10-31 22:05:192666 test_server_.host_port_pair().ToString(), &network_delegate);
[email protected]87a09a92011-07-14 15:50:502667
[email protected]d1ec59082009-02-11 02:48:152668 TestDelegate d;
2669 {
[email protected]f7022f32014-08-21 16:32:192670 scoped_ptr<URLRequest> r(context.CreateRequest(
2671 GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, NULL));
2672 r->Start();
2673 EXPECT_TRUE(r->is_pending());
[email protected]d1ec59082009-02-11 02:48:152674
[email protected]255620da2013-08-19 13:14:292675 base::RunLoop().Run();
[email protected]d1ec59082009-02-11 02:48:152676
[email protected]f7022f32014-08-21 16:32:192677 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
[email protected]d8fc4722014-06-13 13:17:152678 // The proxy server is not set before failure.
[email protected]f7022f32014-08-21 16:32:192679 EXPECT_TRUE(r->proxy_server().IsEmpty());
2680 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r->status().error());
[email protected]dc651782009-02-14 01:45:082681 EXPECT_EQ(1, d.response_started_count());
[email protected]d1ec59082009-02-11 02:48:152682 // We should not have followed the redirect.
2683 EXPECT_EQ(0, d.received_redirect_count());
2684 }
2685}
2686
[email protected]8202d0c2011-02-23 08:31:142687// This is the same as the previous test, but checks that the network delegate
2688// registers the error.
[email protected]c044616e2013-02-20 02:01:262689TEST_F(URLRequestTestHTTP, NetworkDelegateTunnelConnectionFailed) {
[email protected]8202d0c2011-02-23 08:31:142690 ASSERT_TRUE(test_server_.Start());
2691
[email protected]ceefd7fd2012-11-29 00:36:242692 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]d5a4dd62012-05-23 01:41:042693 TestURLRequestContextWithProxy context(
[email protected]2ca01e52013-10-31 22:05:192694 test_server_.host_port_pair().ToString(), &network_delegate);
[email protected]87a09a92011-07-14 15:50:502695
[email protected]8202d0c2011-02-23 08:31:142696 TestDelegate d;
2697 {
[email protected]f7022f32014-08-21 16:32:192698 scoped_ptr<URLRequest> r(context.CreateRequest(
2699 GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, NULL));
2700 r->Start();
2701 EXPECT_TRUE(r->is_pending());
[email protected]8202d0c2011-02-23 08:31:142702
[email protected]255620da2013-08-19 13:14:292703 base::RunLoop().Run();
[email protected]8202d0c2011-02-23 08:31:142704
[email protected]f7022f32014-08-21 16:32:192705 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
[email protected]d8fc4722014-06-13 13:17:152706 // The proxy server is not set before failure.
[email protected]f7022f32014-08-21 16:32:192707 EXPECT_TRUE(r->proxy_server().IsEmpty());
2708 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r->status().error());
[email protected]8202d0c2011-02-23 08:31:142709 EXPECT_EQ(1, d.response_started_count());
2710 // We should not have followed the redirect.
2711 EXPECT_EQ(0, d.received_redirect_count());
2712
2713 EXPECT_EQ(1, network_delegate.error_count());
[email protected]d0cc35b2011-09-08 12:02:052714 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, network_delegate.last_error());
[email protected]8202d0c2011-02-23 08:31:142715 }
2716}
2717
[email protected]dc5a5cf2012-09-26 02:49:302718// Tests that we can block and asynchronously return OK in various stages.
2719TEST_F(URLRequestTestHTTP, NetworkDelegateBlockAsynchronously) {
2720 static const BlockingNetworkDelegate::Stage blocking_stages[] = {
2721 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
2722 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
2723 BlockingNetworkDelegate::ON_HEADERS_RECEIVED
2724 };
2725 static const size_t blocking_stages_length = arraysize(blocking_stages);
2726
2727 ASSERT_TRUE(test_server_.Start());
2728
2729 TestDelegate d;
2730 BlockingNetworkDelegate network_delegate(
2731 BlockingNetworkDelegate::USER_CALLBACK);
2732 network_delegate.set_block_on(
2733 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST |
2734 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS |
2735 BlockingNetworkDelegate::ON_HEADERS_RECEIVED);
2736
2737 TestURLRequestContext context(true);
2738 context.set_network_delegate(&network_delegate);
2739 context.Init();
2740
2741 {
[email protected]f7022f32014-08-21 16:32:192742 scoped_ptr<URLRequest> r(context.CreateRequest(
2743 test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d, NULL));
[email protected]dc5a5cf2012-09-26 02:49:302744
[email protected]f7022f32014-08-21 16:32:192745 r->Start();
[email protected]dc5a5cf2012-09-26 02:49:302746 for (size_t i = 0; i < blocking_stages_length; ++i) {
[email protected]255620da2013-08-19 13:14:292747 base::RunLoop().Run();
[email protected]dc5a5cf2012-09-26 02:49:302748 EXPECT_EQ(blocking_stages[i],
2749 network_delegate.stage_blocked_for_callback());
2750 network_delegate.DoCallback(OK);
2751 }
[email protected]255620da2013-08-19 13:14:292752 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:192753 EXPECT_EQ(200, r->GetResponseCode());
2754 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
[email protected]dc5a5cf2012-09-26 02:49:302755 EXPECT_EQ(1, network_delegate.created_requests());
2756 EXPECT_EQ(0, network_delegate.destroyed_requests());
2757 }
2758 EXPECT_EQ(1, network_delegate.destroyed_requests());
2759}
2760
[email protected]4c76d7c2011-04-15 19:14:122761// Tests that the network delegate can block and cancel a request.
2762TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) {
2763 ASSERT_TRUE(test_server_.Start());
2764
2765 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302766 BlockingNetworkDelegate network_delegate(
2767 BlockingNetworkDelegate::AUTO_CALLBACK);
2768 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
2769 network_delegate.set_retval(ERR_EMPTY_RESPONSE);
[email protected]4c76d7c2011-04-15 19:14:122770
[email protected]d5a4dd62012-05-23 01:41:042771 TestURLRequestContextWithProxy context(
[email protected]2ca01e52013-10-31 22:05:192772 test_server_.host_port_pair().ToString(), &network_delegate);
[email protected]87a09a92011-07-14 15:50:502773
[email protected]4c76d7c2011-04-15 19:14:122774 {
[email protected]f7022f32014-08-21 16:32:192775 scoped_ptr<URLRequest> r(context.CreateRequest(
2776 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL));
[email protected]4c76d7c2011-04-15 19:14:122777
[email protected]f7022f32014-08-21 16:32:192778 r->Start();
[email protected]255620da2013-08-19 13:14:292779 base::RunLoop().Run();
[email protected]4c76d7c2011-04-15 19:14:122780
[email protected]f7022f32014-08-21 16:32:192781 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
[email protected]d8fc4722014-06-13 13:17:152782 // The proxy server is not set before cancellation.
[email protected]f7022f32014-08-21 16:32:192783 EXPECT_TRUE(r->proxy_server().IsEmpty());
2784 EXPECT_EQ(ERR_EMPTY_RESPONSE, r->status().error());
[email protected]4c76d7c2011-04-15 19:14:122785 EXPECT_EQ(1, network_delegate.created_requests());
2786 EXPECT_EQ(0, network_delegate.destroyed_requests());
2787 }
2788 EXPECT_EQ(1, network_delegate.destroyed_requests());
2789}
2790
[email protected]b4438d32012-09-27 06:15:302791// Helper function for NetworkDelegateCancelRequestAsynchronously and
2792// NetworkDelegateCancelRequestSynchronously. Sets up a blocking network
2793// delegate operating in |block_mode| and a request for |url|. It blocks the
2794// request in |stage| and cancels it with ERR_BLOCKED_BY_CLIENT.
2795void NetworkDelegateCancelRequest(BlockingNetworkDelegate::BlockMode block_mode,
2796 BlockingNetworkDelegate::Stage stage,
2797 const GURL& url) {
[email protected]3cd384c602011-08-31 16:12:362798 TestDelegate d;
[email protected]b4438d32012-09-27 06:15:302799 BlockingNetworkDelegate network_delegate(block_mode);
2800 network_delegate.set_retval(ERR_BLOCKED_BY_CLIENT);
2801 network_delegate.set_block_on(stage);
[email protected]3cd384c602011-08-31 16:12:362802
[email protected]b4438d32012-09-27 06:15:302803 TestURLRequestContext context(true);
2804 context.set_network_delegate(&network_delegate);
2805 context.Init();
[email protected]3cd384c602011-08-31 16:12:362806
2807 {
[email protected]f7022f32014-08-21 16:32:192808 scoped_ptr<URLRequest> r(context.CreateRequest(
2809 url, DEFAULT_PRIORITY, &d, NULL));
[email protected]3cd384c602011-08-31 16:12:362810
[email protected]f7022f32014-08-21 16:32:192811 r->Start();
[email protected]255620da2013-08-19 13:14:292812 base::RunLoop().Run();
[email protected]3cd384c602011-08-31 16:12:362813
[email protected]f7022f32014-08-21 16:32:192814 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
[email protected]d8fc4722014-06-13 13:17:152815 // The proxy server is not set before cancellation.
[email protected]f7022f32014-08-21 16:32:192816 EXPECT_TRUE(r->proxy_server().IsEmpty());
2817 EXPECT_EQ(ERR_BLOCKED_BY_CLIENT, r->status().error());
[email protected]3cd384c602011-08-31 16:12:362818 EXPECT_EQ(1, network_delegate.created_requests());
2819 EXPECT_EQ(0, network_delegate.destroyed_requests());
2820 }
2821 EXPECT_EQ(1, network_delegate.destroyed_requests());
2822}
2823
[email protected]b4438d32012-09-27 06:15:302824// The following 3 tests check that the network delegate can cancel a request
2825// synchronously in various stages of the request.
2826TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously1) {
2827 ASSERT_TRUE(test_server_.Start());
2828 NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS,
2829 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
[email protected]007b3f82013-04-09 08:46:452830 test_server_.GetURL(std::string()));
[email protected]b4438d32012-09-27 06:15:302831}
2832
2833TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously2) {
2834 ASSERT_TRUE(test_server_.Start());
2835 NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS,
2836 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
[email protected]007b3f82013-04-09 08:46:452837 test_server_.GetURL(std::string()));
[email protected]b4438d32012-09-27 06:15:302838}
2839
2840TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously3) {
2841 ASSERT_TRUE(test_server_.Start());
2842 NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS,
2843 BlockingNetworkDelegate::ON_HEADERS_RECEIVED,
[email protected]007b3f82013-04-09 08:46:452844 test_server_.GetURL(std::string()));
[email protected]b4438d32012-09-27 06:15:302845}
2846
2847// The following 3 tests check that the network delegate can cancel a request
2848// asynchronously in various stages of the request.
2849TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously1) {
2850 ASSERT_TRUE(test_server_.Start());
2851 NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK,
2852 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
[email protected]007b3f82013-04-09 08:46:452853 test_server_.GetURL(std::string()));
[email protected]b4438d32012-09-27 06:15:302854}
2855
2856TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously2) {
2857 ASSERT_TRUE(test_server_.Start());
2858 NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK,
2859 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
[email protected]007b3f82013-04-09 08:46:452860 test_server_.GetURL(std::string()));
[email protected]b4438d32012-09-27 06:15:302861}
2862
2863TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously3) {
2864 ASSERT_TRUE(test_server_.Start());
2865 NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK,
2866 BlockingNetworkDelegate::ON_HEADERS_RECEIVED,
[email protected]007b3f82013-04-09 08:46:452867 test_server_.GetURL(std::string()));
[email protected]b4438d32012-09-27 06:15:302868}
2869
[email protected]4c76d7c2011-04-15 19:14:122870// Tests that the network delegate can block and redirect a request to a new
2871// URL.
2872TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) {
2873 ASSERT_TRUE(test_server_.Start());
2874
2875 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302876 BlockingNetworkDelegate network_delegate(
2877 BlockingNetworkDelegate::AUTO_CALLBACK);
2878 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
[email protected]4c76d7c2011-04-15 19:14:122879 GURL redirect_url(test_server_.GetURL("simple.html"));
2880 network_delegate.set_redirect_url(redirect_url);
2881
[email protected]d5a4dd62012-05-23 01:41:042882 TestURLRequestContextWithProxy context(
[email protected]2ca01e52013-10-31 22:05:192883 test_server_.host_port_pair().ToString(), &network_delegate);
[email protected]87a09a92011-07-14 15:50:502884
[email protected]4c76d7c2011-04-15 19:14:122885 {
[email protected]8f1ac082011-04-19 21:14:132886 GURL original_url(test_server_.GetURL("empty.html"));
[email protected]f7022f32014-08-21 16:32:192887 scoped_ptr<URLRequest> r(context.CreateRequest(
2888 original_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]4c76d7c2011-04-15 19:14:122889
[email protected]6be6fa92014-08-06 23:44:562890 // Quit after hitting the redirect, so can check the headers.
2891 d.set_quit_on_redirect(true);
[email protected]f7022f32014-08-21 16:32:192892 r->Start();
[email protected]255620da2013-08-19 13:14:292893 base::RunLoop().Run();
[email protected]4c76d7c2011-04-15 19:14:122894
[email protected]6be6fa92014-08-06 23:44:562895 // Check headers from URLRequestJob.
[email protected]f7022f32014-08-21 16:32:192896 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
2897 EXPECT_EQ(307, r->GetResponseCode());
2898 EXPECT_EQ(307, r->response_headers()->response_code());
[email protected]6be6fa92014-08-06 23:44:562899 std::string location;
[email protected]f7022f32014-08-21 16:32:192900 ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location",
2901 &location));
[email protected]6be6fa92014-08-06 23:44:562902 EXPECT_EQ(redirect_url, GURL(location));
2903
2904 // Let the request finish.
[email protected]f7022f32014-08-21 16:32:192905 r->FollowDeferredRedirect();
[email protected]6be6fa92014-08-06 23:44:562906 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:192907 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
2908 EXPECT_TRUE(r->proxy_server().Equals(test_server_.host_port_pair()));
[email protected]597a1ab2014-06-26 08:12:272909 EXPECT_EQ(
2910 1, network_delegate.observed_before_proxy_headers_sent_callbacks());
2911 EXPECT_TRUE(
2912 network_delegate.last_observed_proxy().Equals(
2913 test_server_.host_port_pair()));
2914
[email protected]f7022f32014-08-21 16:32:192915 EXPECT_EQ(0, r->status().error());
2916 EXPECT_EQ(redirect_url, r->url());
2917 EXPECT_EQ(original_url, r->original_url());
2918 EXPECT_EQ(2U, r->url_chain().size());
[email protected]4c76d7c2011-04-15 19:14:122919 EXPECT_EQ(1, network_delegate.created_requests());
2920 EXPECT_EQ(0, network_delegate.destroyed_requests());
2921 }
2922 EXPECT_EQ(1, network_delegate.destroyed_requests());
2923}
2924
[email protected]b813ed72012-04-05 08:21:362925// Tests that the network delegate can block and redirect a request to a new
2926// URL by setting a redirect_url and returning in OnBeforeURLRequest directly.
2927TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) {
2928 ASSERT_TRUE(test_server_.Start());
2929
2930 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302931 BlockingNetworkDelegate network_delegate(
2932 BlockingNetworkDelegate::SYNCHRONOUS);
[email protected]b813ed72012-04-05 08:21:362933 GURL redirect_url(test_server_.GetURL("simple.html"));
2934 network_delegate.set_redirect_url(redirect_url);
[email protected]b813ed72012-04-05 08:21:362935
[email protected]d5a4dd62012-05-23 01:41:042936 TestURLRequestContextWithProxy context(
[email protected]2ca01e52013-10-31 22:05:192937 test_server_.host_port_pair().ToString(), &network_delegate);
[email protected]b813ed72012-04-05 08:21:362938
2939 {
2940 GURL original_url(test_server_.GetURL("empty.html"));
[email protected]f7022f32014-08-21 16:32:192941 scoped_ptr<URLRequest> r(context.CreateRequest(
2942 original_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]b813ed72012-04-05 08:21:362943
[email protected]6be6fa92014-08-06 23:44:562944 // Quit after hitting the redirect, so can check the headers.
2945 d.set_quit_on_redirect(true);
[email protected]f7022f32014-08-21 16:32:192946 r->Start();
[email protected]255620da2013-08-19 13:14:292947 base::RunLoop().Run();
[email protected]b813ed72012-04-05 08:21:362948
[email protected]6be6fa92014-08-06 23:44:562949 // Check headers from URLRequestJob.
[email protected]f7022f32014-08-21 16:32:192950 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
2951 EXPECT_EQ(307, r->GetResponseCode());
2952 EXPECT_EQ(307, r->response_headers()->response_code());
[email protected]6be6fa92014-08-06 23:44:562953 std::string location;
[email protected]f7022f32014-08-21 16:32:192954 ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location",
2955 &location));
[email protected]6be6fa92014-08-06 23:44:562956 EXPECT_EQ(redirect_url, GURL(location));
2957
2958 // Let the request finish.
[email protected]f7022f32014-08-21 16:32:192959 r->FollowDeferredRedirect();
[email protected]6be6fa92014-08-06 23:44:562960 base::RunLoop().Run();
2961
[email protected]f7022f32014-08-21 16:32:192962 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
2963 EXPECT_TRUE(r->proxy_server().Equals(test_server_.host_port_pair()));
[email protected]597a1ab2014-06-26 08:12:272964 EXPECT_EQ(
2965 1, network_delegate.observed_before_proxy_headers_sent_callbacks());
2966 EXPECT_TRUE(
2967 network_delegate.last_observed_proxy().Equals(
2968 test_server_.host_port_pair()));
[email protected]f7022f32014-08-21 16:32:192969 EXPECT_EQ(0, r->status().error());
2970 EXPECT_EQ(redirect_url, r->url());
2971 EXPECT_EQ(original_url, r->original_url());
2972 EXPECT_EQ(2U, r->url_chain().size());
[email protected]b813ed72012-04-05 08:21:362973 EXPECT_EQ(1, network_delegate.created_requests());
2974 EXPECT_EQ(0, network_delegate.destroyed_requests());
2975 }
2976 EXPECT_EQ(1, network_delegate.destroyed_requests());
2977}
2978
[email protected]3c5ca8c2011-09-29 01:14:512979// Tests that redirects caused by the network delegate preserve POST data.
2980TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) {
2981 ASSERT_TRUE(test_server_.Start());
2982
2983 const char kData[] = "hello world";
2984
2985 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:302986 BlockingNetworkDelegate network_delegate(
2987 BlockingNetworkDelegate::AUTO_CALLBACK);
2988 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
[email protected]3c5ca8c2011-09-29 01:14:512989 GURL redirect_url(test_server_.GetURL("echo"));
2990 network_delegate.set_redirect_url(redirect_url);
2991
[email protected]ef2bf422012-05-11 03:27:092992 TestURLRequestContext context(true);
2993 context.set_network_delegate(&network_delegate);
2994 context.Init();
[email protected]3c5ca8c2011-09-29 01:14:512995
2996 {
2997 GURL original_url(test_server_.GetURL("empty.html"));
[email protected]f7022f32014-08-21 16:32:192998 scoped_ptr<URLRequest> r(context.CreateRequest(
2999 original_url, DEFAULT_PRIORITY, &d, NULL));
3000 r->set_method("POST");
3001 r->set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
[email protected]3c5ca8c2011-09-29 01:14:513002 HttpRequestHeaders headers;
3003 headers.SetHeader(HttpRequestHeaders::kContentLength,
3004 base::UintToString(arraysize(kData) - 1));
[email protected]f7022f32014-08-21 16:32:193005 r->SetExtraRequestHeaders(headers);
[email protected]6be6fa92014-08-06 23:44:563006
3007 // Quit after hitting the redirect, so can check the headers.
3008 d.set_quit_on_redirect(true);
[email protected]f7022f32014-08-21 16:32:193009 r->Start();
[email protected]255620da2013-08-19 13:14:293010 base::RunLoop().Run();
[email protected]3c5ca8c2011-09-29 01:14:513011
[email protected]6be6fa92014-08-06 23:44:563012 // Check headers from URLRequestJob.
[email protected]f7022f32014-08-21 16:32:193013 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3014 EXPECT_EQ(307, r->GetResponseCode());
3015 EXPECT_EQ(307, r->response_headers()->response_code());
[email protected]6be6fa92014-08-06 23:44:563016 std::string location;
[email protected]f7022f32014-08-21 16:32:193017 ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location",
3018 &location));
[email protected]6be6fa92014-08-06 23:44:563019 EXPECT_EQ(redirect_url, GURL(location));
3020
3021 // Let the request finish.
[email protected]f7022f32014-08-21 16:32:193022 r->FollowDeferredRedirect();
[email protected]6be6fa92014-08-06 23:44:563023 base::RunLoop().Run();
3024
[email protected]f7022f32014-08-21 16:32:193025 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3026 EXPECT_EQ(0, r->status().error());
3027 EXPECT_EQ(redirect_url, r->url());
3028 EXPECT_EQ(original_url, r->original_url());
3029 EXPECT_EQ(2U, r->url_chain().size());
[email protected]3c5ca8c2011-09-29 01:14:513030 EXPECT_EQ(1, network_delegate.created_requests());
3031 EXPECT_EQ(0, network_delegate.destroyed_requests());
[email protected]f7022f32014-08-21 16:32:193032 EXPECT_EQ("POST", r->method());
[email protected]3c5ca8c2011-09-29 01:14:513033 EXPECT_EQ(kData, d.data_received());
3034 }
3035 EXPECT_EQ(1, network_delegate.destroyed_requests());
3036}
3037
[email protected]5f714132014-03-26 10:41:163038// Tests that the network delegate can block and redirect a request to a new
3039// URL during OnHeadersReceived.
3040TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestOnHeadersReceived) {
3041 ASSERT_TRUE(test_server_.Start());
3042
3043 TestDelegate d;
3044 BlockingNetworkDelegate network_delegate(
3045 BlockingNetworkDelegate::AUTO_CALLBACK);
3046 network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED);
3047 GURL redirect_url(test_server_.GetURL("simple.html"));
3048 network_delegate.set_redirect_on_headers_received_url(redirect_url);
3049
3050 TestURLRequestContextWithProxy context(
3051 test_server_.host_port_pair().ToString(), &network_delegate);
3052
3053 {
3054 GURL original_url(test_server_.GetURL("empty.html"));
[email protected]f7022f32014-08-21 16:32:193055 scoped_ptr<URLRequest> r(context.CreateRequest(
3056 original_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]5f714132014-03-26 10:41:163057
[email protected]f7022f32014-08-21 16:32:193058 r->Start();
[email protected]5f714132014-03-26 10:41:163059 base::RunLoop().Run();
3060
[email protected]f7022f32014-08-21 16:32:193061 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3062 EXPECT_TRUE(r->proxy_server().Equals(test_server_.host_port_pair()));
[email protected]597a1ab2014-06-26 08:12:273063 EXPECT_EQ(
3064 2, network_delegate.observed_before_proxy_headers_sent_callbacks());
3065 EXPECT_TRUE(
3066 network_delegate.last_observed_proxy().Equals(
3067 test_server_.host_port_pair()));
[email protected]f7022f32014-08-21 16:32:193068
3069 EXPECT_EQ(OK, r->status().error());
3070 EXPECT_EQ(redirect_url, r->url());
3071 EXPECT_EQ(original_url, r->original_url());
3072 EXPECT_EQ(2U, r->url_chain().size());
[email protected]5f714132014-03-26 10:41:163073 EXPECT_EQ(2, network_delegate.created_requests());
3074 EXPECT_EQ(0, network_delegate.destroyed_requests());
3075 }
3076 EXPECT_EQ(1, network_delegate.destroyed_requests());
3077}
3078
[email protected]c2911d72011-10-03 22:16:363079// Tests that the network delegate can synchronously complete OnAuthRequired
3080// by taking no action. This indicates that the NetworkDelegate does not want to
3081// handle the challenge, and is passing the buck along to the
3082// URLRequest::Delegate.
3083TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) {
3084 ASSERT_TRUE(test_server_.Start());
3085
3086 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303087 BlockingNetworkDelegate network_delegate(
3088 BlockingNetworkDelegate::SYNCHRONOUS);
[email protected]c2911d72011-10-03 22:16:363089
[email protected]ef2bf422012-05-11 03:27:093090 TestURLRequestContext context(true);
3091 context.set_network_delegate(&network_delegate);
3092 context.Init();
[email protected]c2911d72011-10-03 22:16:363093
[email protected]f3cf9802011-10-28 18:44:583094 d.set_credentials(AuthCredentials(kUser, kSecret));
[email protected]c2911d72011-10-03 22:16:363095
3096 {
3097 GURL url(test_server_.GetURL("auth-basic"));
[email protected]f7022f32014-08-21 16:32:193098 scoped_ptr<URLRequest> r(context.CreateRequest(
3099 url, DEFAULT_PRIORITY, &d, NULL));
3100 r->Start();
[email protected]79e1fd62013-06-20 06:50:043101
[email protected]255620da2013-08-19 13:14:293102 base::RunLoop().Run();
[email protected]79e1fd62013-06-20 06:50:043103
[email protected]f7022f32014-08-21 16:32:193104 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3105 EXPECT_EQ(0, r->status().error());
3106 EXPECT_EQ(200, r->GetResponseCode());
[email protected]79e1fd62013-06-20 06:50:043107 EXPECT_TRUE(d.auth_required_called());
3108 EXPECT_EQ(1, network_delegate.created_requests());
3109 EXPECT_EQ(0, network_delegate.destroyed_requests());
3110 }
3111 EXPECT_EQ(1, network_delegate.destroyed_requests());
3112}
3113
3114TEST_F(URLRequestTestHTTP,
3115 NetworkDelegateOnAuthRequiredSyncNoAction_GetFullRequestHeaders) {
3116 ASSERT_TRUE(test_server_.Start());
3117
3118 TestDelegate d;
3119 BlockingNetworkDelegate network_delegate(
3120 BlockingNetworkDelegate::SYNCHRONOUS);
3121
3122 TestURLRequestContext context(true);
3123 context.set_network_delegate(&network_delegate);
3124 context.Init();
3125
3126 d.set_credentials(AuthCredentials(kUser, kSecret));
3127
3128 {
3129 GURL url(test_server_.GetURL("auth-basic"));
[email protected]f7022f32014-08-21 16:32:193130 scoped_ptr<URLRequest> r(context.CreateRequest(
3131 url, DEFAULT_PRIORITY, &d, NULL));
3132 r->Start();
[email protected]79e1fd62013-06-20 06:50:043133
3134 {
3135 HttpRequestHeaders headers;
[email protected]f7022f32014-08-21 16:32:193136 EXPECT_TRUE(r->GetFullRequestHeaders(&headers));
[email protected]79e1fd62013-06-20 06:50:043137 EXPECT_FALSE(headers.HasHeader("Authorization"));
3138 }
3139
[email protected]255620da2013-08-19 13:14:293140 base::RunLoop().Run();
[email protected]c2911d72011-10-03 22:16:363141
[email protected]f7022f32014-08-21 16:32:193142 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3143 EXPECT_EQ(0, r->status().error());
3144 EXPECT_EQ(200, r->GetResponseCode());
[email protected]c2911d72011-10-03 22:16:363145 EXPECT_TRUE(d.auth_required_called());
3146 EXPECT_EQ(1, network_delegate.created_requests());
3147 EXPECT_EQ(0, network_delegate.destroyed_requests());
3148 }
3149 EXPECT_EQ(1, network_delegate.destroyed_requests());
3150}
3151
3152// Tests that the network delegate can synchronously complete OnAuthRequired
[email protected]1e110eae2013-05-10 22:02:403153// by setting credentials.
[email protected]c2911d72011-10-03 22:16:363154TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) {
3155 ASSERT_TRUE(test_server_.Start());
3156
3157 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303158 BlockingNetworkDelegate network_delegate(
3159 BlockingNetworkDelegate::SYNCHRONOUS);
3160 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]c2911d72011-10-03 22:16:363161 network_delegate.set_auth_retval(
3162 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
3163
[email protected]f3cf9802011-10-28 18:44:583164 network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret));
[email protected]c2911d72011-10-03 22:16:363165
[email protected]ef2bf422012-05-11 03:27:093166 TestURLRequestContext context(true);
3167 context.set_network_delegate(&network_delegate);
3168 context.Init();
[email protected]c2911d72011-10-03 22:16:363169
3170 {
3171 GURL url(test_server_.GetURL("auth-basic"));
[email protected]f7022f32014-08-21 16:32:193172 scoped_ptr<URLRequest> r(context.CreateRequest(
3173 url, DEFAULT_PRIORITY, &d, NULL));
3174 r->Start();
[email protected]255620da2013-08-19 13:14:293175 base::RunLoop().Run();
[email protected]c2911d72011-10-03 22:16:363176
[email protected]f7022f32014-08-21 16:32:193177 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3178 EXPECT_EQ(0, r->status().error());
3179 EXPECT_EQ(200, r->GetResponseCode());
[email protected]c2911d72011-10-03 22:16:363180 EXPECT_FALSE(d.auth_required_called());
3181 EXPECT_EQ(1, network_delegate.created_requests());
3182 EXPECT_EQ(0, network_delegate.destroyed_requests());
3183 }
3184 EXPECT_EQ(1, network_delegate.destroyed_requests());
3185}
3186
[email protected]79e1fd62013-06-20 06:50:043187// Same as above, but also tests that GetFullRequestHeaders returns the proper
3188// headers (for the first or second request) when called at the proper times.
3189TEST_F(URLRequestTestHTTP,
3190 NetworkDelegateOnAuthRequiredSyncSetAuth_GetFullRequestHeaders) {
3191 ASSERT_TRUE(test_server_.Start());
3192
3193 TestDelegate d;
3194 BlockingNetworkDelegate network_delegate(
3195 BlockingNetworkDelegate::SYNCHRONOUS);
3196 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
3197 network_delegate.set_auth_retval(
3198 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
3199
3200 network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret));
3201
3202 TestURLRequestContext context(true);
3203 context.set_network_delegate(&network_delegate);
3204 context.Init();
3205
3206 {
3207 GURL url(test_server_.GetURL("auth-basic"));
[email protected]f7022f32014-08-21 16:32:193208 scoped_ptr<URLRequest> r(context.CreateRequest(
3209 url, DEFAULT_PRIORITY, &d, NULL));
3210 r->Start();
[email protected]255620da2013-08-19 13:14:293211 base::RunLoop().Run();
[email protected]79e1fd62013-06-20 06:50:043212
[email protected]f7022f32014-08-21 16:32:193213 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3214 EXPECT_EQ(0, r->status().error());
3215 EXPECT_EQ(200, r->GetResponseCode());
[email protected]79e1fd62013-06-20 06:50:043216 EXPECT_FALSE(d.auth_required_called());
3217 EXPECT_EQ(1, network_delegate.created_requests());
3218 EXPECT_EQ(0, network_delegate.destroyed_requests());
3219
3220 {
3221 HttpRequestHeaders headers;
[email protected]f7022f32014-08-21 16:32:193222 EXPECT_TRUE(r->GetFullRequestHeaders(&headers));
[email protected]79e1fd62013-06-20 06:50:043223 EXPECT_TRUE(headers.HasHeader("Authorization"));
3224 }
3225 }
3226 EXPECT_EQ(1, network_delegate.destroyed_requests());
3227}
3228
[email protected]c2911d72011-10-03 22:16:363229// Tests that the network delegate can synchronously complete OnAuthRequired
3230// by cancelling authentication.
3231TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) {
3232 ASSERT_TRUE(test_server_.Start());
3233
3234 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303235 BlockingNetworkDelegate network_delegate(
3236 BlockingNetworkDelegate::SYNCHRONOUS);
3237 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]c2911d72011-10-03 22:16:363238 network_delegate.set_auth_retval(
3239 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH);
3240
[email protected]ef2bf422012-05-11 03:27:093241 TestURLRequestContext context(true);
3242 context.set_network_delegate(&network_delegate);
3243 context.Init();
[email protected]c2911d72011-10-03 22:16:363244
3245 {
3246 GURL url(test_server_.GetURL("auth-basic"));
[email protected]f7022f32014-08-21 16:32:193247 scoped_ptr<URLRequest> r(context.CreateRequest(
3248 url, DEFAULT_PRIORITY, &d, NULL));
3249 r->Start();
[email protected]255620da2013-08-19 13:14:293250 base::RunLoop().Run();
[email protected]c2911d72011-10-03 22:16:363251
[email protected]f7022f32014-08-21 16:32:193252 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3253 EXPECT_EQ(OK, r->status().error());
3254 EXPECT_EQ(401, r->GetResponseCode());
[email protected]c2911d72011-10-03 22:16:363255 EXPECT_FALSE(d.auth_required_called());
3256 EXPECT_EQ(1, network_delegate.created_requests());
3257 EXPECT_EQ(0, network_delegate.destroyed_requests());
3258 }
3259 EXPECT_EQ(1, network_delegate.destroyed_requests());
3260}
3261
3262// Tests that the network delegate can asynchronously complete OnAuthRequired
3263// by taking no action. This indicates that the NetworkDelegate does not want
3264// to handle the challenge, and is passing the buck along to the
3265// URLRequest::Delegate.
3266TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) {
3267 ASSERT_TRUE(test_server_.Start());
3268
3269 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303270 BlockingNetworkDelegate network_delegate(
3271 BlockingNetworkDelegate::AUTO_CALLBACK);
3272 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]c2911d72011-10-03 22:16:363273
[email protected]ef2bf422012-05-11 03:27:093274 TestURLRequestContext context(true);
3275 context.set_network_delegate(&network_delegate);
3276 context.Init();
[email protected]c2911d72011-10-03 22:16:363277
[email protected]f3cf9802011-10-28 18:44:583278 d.set_credentials(AuthCredentials(kUser, kSecret));
[email protected]c2911d72011-10-03 22:16:363279
3280 {
3281 GURL url(test_server_.GetURL("auth-basic"));
[email protected]f7022f32014-08-21 16:32:193282 scoped_ptr<URLRequest> r(context.CreateRequest(
3283 url, DEFAULT_PRIORITY, &d, NULL));
3284 r->Start();
[email protected]255620da2013-08-19 13:14:293285 base::RunLoop().Run();
[email protected]c2911d72011-10-03 22:16:363286
[email protected]f7022f32014-08-21 16:32:193287 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3288 EXPECT_EQ(0, r->status().error());
3289 EXPECT_EQ(200, r->GetResponseCode());
[email protected]c2911d72011-10-03 22:16:363290 EXPECT_TRUE(d.auth_required_called());
3291 EXPECT_EQ(1, network_delegate.created_requests());
3292 EXPECT_EQ(0, network_delegate.destroyed_requests());
3293 }
3294 EXPECT_EQ(1, network_delegate.destroyed_requests());
3295}
3296
3297// Tests that the network delegate can asynchronously complete OnAuthRequired
3298// by setting credentials.
3299TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) {
3300 ASSERT_TRUE(test_server_.Start());
3301
3302 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303303 BlockingNetworkDelegate network_delegate(
3304 BlockingNetworkDelegate::AUTO_CALLBACK);
3305 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]c2911d72011-10-03 22:16:363306 network_delegate.set_auth_retval(
[email protected]c2911d72011-10-03 22:16:363307 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
3308
[email protected]f3cf9802011-10-28 18:44:583309 AuthCredentials auth_credentials(kUser, kSecret);
[email protected]c2911d72011-10-03 22:16:363310 network_delegate.set_auth_credentials(auth_credentials);
3311
[email protected]ef2bf422012-05-11 03:27:093312 TestURLRequestContext context(true);
3313 context.set_network_delegate(&network_delegate);
3314 context.Init();
[email protected]c2911d72011-10-03 22:16:363315
3316 {
3317 GURL url(test_server_.GetURL("auth-basic"));
[email protected]f7022f32014-08-21 16:32:193318 scoped_ptr<URLRequest> r(context.CreateRequest(
3319 url, DEFAULT_PRIORITY, &d, NULL));
3320 r->Start();
[email protected]255620da2013-08-19 13:14:293321 base::RunLoop().Run();
[email protected]c2911d72011-10-03 22:16:363322
[email protected]f7022f32014-08-21 16:32:193323 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3324 EXPECT_EQ(0, r->status().error());
[email protected]c2911d72011-10-03 22:16:363325
[email protected]f7022f32014-08-21 16:32:193326 EXPECT_EQ(200, r->GetResponseCode());
[email protected]c2911d72011-10-03 22:16:363327 EXPECT_FALSE(d.auth_required_called());
3328 EXPECT_EQ(1, network_delegate.created_requests());
3329 EXPECT_EQ(0, network_delegate.destroyed_requests());
3330 }
3331 EXPECT_EQ(1, network_delegate.destroyed_requests());
3332}
3333
3334// Tests that the network delegate can asynchronously complete OnAuthRequired
3335// by cancelling authentication.
3336TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) {
3337 ASSERT_TRUE(test_server_.Start());
3338
3339 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303340 BlockingNetworkDelegate network_delegate(
3341 BlockingNetworkDelegate::AUTO_CALLBACK);
3342 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]c2911d72011-10-03 22:16:363343 network_delegate.set_auth_retval(
[email protected]c2911d72011-10-03 22:16:363344 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH);
3345
[email protected]ef2bf422012-05-11 03:27:093346 TestURLRequestContext context(true);
3347 context.set_network_delegate(&network_delegate);
3348 context.Init();
[email protected]c2911d72011-10-03 22:16:363349
3350 {
3351 GURL url(test_server_.GetURL("auth-basic"));
[email protected]f7022f32014-08-21 16:32:193352 scoped_ptr<URLRequest> r(context.CreateRequest(
3353 url, DEFAULT_PRIORITY, &d, NULL));
3354 r->Start();
[email protected]255620da2013-08-19 13:14:293355 base::RunLoop().Run();
[email protected]c2911d72011-10-03 22:16:363356
[email protected]f7022f32014-08-21 16:32:193357 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3358 EXPECT_EQ(OK, r->status().error());
3359 EXPECT_EQ(401, r->GetResponseCode());
[email protected]c2911d72011-10-03 22:16:363360 EXPECT_FALSE(d.auth_required_called());
3361 EXPECT_EQ(1, network_delegate.created_requests());
3362 EXPECT_EQ(0, network_delegate.destroyed_requests());
3363 }
3364 EXPECT_EQ(1, network_delegate.destroyed_requests());
3365}
3366
[email protected]9045b8822012-01-13 20:35:353367// Tests that we can handle when a network request was canceled while we were
3368// waiting for the network delegate.
3369// Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback.
3370TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) {
3371 ASSERT_TRUE(test_server_.Start());
3372
3373 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303374 BlockingNetworkDelegate network_delegate(
3375 BlockingNetworkDelegate::USER_CALLBACK);
3376 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
[email protected]9045b8822012-01-13 20:35:353377
[email protected]ef2bf422012-05-11 03:27:093378 TestURLRequestContext context(true);
3379 context.set_network_delegate(&network_delegate);
3380 context.Init();
[email protected]9045b8822012-01-13 20:35:353381
3382 {
[email protected]f7022f32014-08-21 16:32:193383 scoped_ptr<URLRequest> r(context.CreateRequest(
3384 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL));
[email protected]9045b8822012-01-13 20:35:353385
[email protected]f7022f32014-08-21 16:32:193386 r->Start();
[email protected]255620da2013-08-19 13:14:293387 base::RunLoop().Run();
[email protected]dc5a5cf2012-09-26 02:49:303388 EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
3389 network_delegate.stage_blocked_for_callback());
[email protected]9045b8822012-01-13 20:35:353390 EXPECT_EQ(0, network_delegate.completed_requests());
3391 // Cancel before callback.
[email protected]f7022f32014-08-21 16:32:193392 r->Cancel();
[email protected]9045b8822012-01-13 20:35:353393 // Ensure that network delegate is notified.
3394 EXPECT_EQ(1, network_delegate.completed_requests());
[email protected]f7022f32014-08-21 16:32:193395 EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status());
3396 EXPECT_EQ(ERR_ABORTED, r->status().error());
[email protected]9045b8822012-01-13 20:35:353397 EXPECT_EQ(1, network_delegate.created_requests());
3398 EXPECT_EQ(0, network_delegate.destroyed_requests());
3399 }
3400 EXPECT_EQ(1, network_delegate.destroyed_requests());
3401}
3402
3403// Tests that we can handle when a network request was canceled while we were
3404// waiting for the network delegate.
3405// Part 2: Request is cancelled while waiting for OnBeforeSendHeaders callback.
3406TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) {
3407 ASSERT_TRUE(test_server_.Start());
3408
3409 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303410 BlockingNetworkDelegate network_delegate(
3411 BlockingNetworkDelegate::USER_CALLBACK);
3412 network_delegate.set_block_on(
3413 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS);
[email protected]9045b8822012-01-13 20:35:353414
[email protected]ef2bf422012-05-11 03:27:093415 TestURLRequestContext context(true);
3416 context.set_network_delegate(&network_delegate);
3417 context.Init();
[email protected]9045b8822012-01-13 20:35:353418
3419 {
[email protected]f7022f32014-08-21 16:32:193420 scoped_ptr<URLRequest> r(context.CreateRequest(
3421 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL));
[email protected]9045b8822012-01-13 20:35:353422
[email protected]f7022f32014-08-21 16:32:193423 r->Start();
[email protected]255620da2013-08-19 13:14:293424 base::RunLoop().Run();
[email protected]dc5a5cf2012-09-26 02:49:303425 EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
3426 network_delegate.stage_blocked_for_callback());
[email protected]9045b8822012-01-13 20:35:353427 EXPECT_EQ(0, network_delegate.completed_requests());
3428 // Cancel before callback.
[email protected]f7022f32014-08-21 16:32:193429 r->Cancel();
[email protected]9045b8822012-01-13 20:35:353430 // Ensure that network delegate is notified.
3431 EXPECT_EQ(1, network_delegate.completed_requests());
[email protected]f7022f32014-08-21 16:32:193432 EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status());
3433 EXPECT_EQ(ERR_ABORTED, r->status().error());
[email protected]9045b8822012-01-13 20:35:353434 EXPECT_EQ(1, network_delegate.created_requests());
3435 EXPECT_EQ(0, network_delegate.destroyed_requests());
3436 }
3437 EXPECT_EQ(1, network_delegate.destroyed_requests());
3438}
3439
3440// Tests that we can handle when a network request was canceled while we were
3441// waiting for the network delegate.
3442// Part 3: Request is cancelled while waiting for OnHeadersReceived callback.
3443TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) {
3444 ASSERT_TRUE(test_server_.Start());
3445
3446 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303447 BlockingNetworkDelegate network_delegate(
3448 BlockingNetworkDelegate::USER_CALLBACK);
3449 network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED);
[email protected]9045b8822012-01-13 20:35:353450
[email protected]ef2bf422012-05-11 03:27:093451 TestURLRequestContext context(true);
3452 context.set_network_delegate(&network_delegate);
3453 context.Init();
[email protected]9045b8822012-01-13 20:35:353454
3455 {
[email protected]f7022f32014-08-21 16:32:193456 scoped_ptr<URLRequest> r(context.CreateRequest(
3457 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL));
[email protected]9045b8822012-01-13 20:35:353458
[email protected]f7022f32014-08-21 16:32:193459 r->Start();
[email protected]255620da2013-08-19 13:14:293460 base::RunLoop().Run();
[email protected]dc5a5cf2012-09-26 02:49:303461 EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED,
3462 network_delegate.stage_blocked_for_callback());
[email protected]9045b8822012-01-13 20:35:353463 EXPECT_EQ(0, network_delegate.completed_requests());
3464 // Cancel before callback.
[email protected]f7022f32014-08-21 16:32:193465 r->Cancel();
[email protected]9045b8822012-01-13 20:35:353466 // Ensure that network delegate is notified.
3467 EXPECT_EQ(1, network_delegate.completed_requests());
[email protected]f7022f32014-08-21 16:32:193468 EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status());
3469 EXPECT_EQ(ERR_ABORTED, r->status().error());
[email protected]9045b8822012-01-13 20:35:353470 EXPECT_EQ(1, network_delegate.created_requests());
3471 EXPECT_EQ(0, network_delegate.destroyed_requests());
3472 }
3473 EXPECT_EQ(1, network_delegate.destroyed_requests());
3474}
3475
3476// Tests that we can handle when a network request was canceled while we were
3477// waiting for the network delegate.
3478// Part 4: Request is cancelled while waiting for OnAuthRequired callback.
[email protected]bfe8b302013-02-15 12:16:023479TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) {
[email protected]9045b8822012-01-13 20:35:353480 ASSERT_TRUE(test_server_.Start());
3481
3482 TestDelegate d;
[email protected]dc5a5cf2012-09-26 02:49:303483 BlockingNetworkDelegate network_delegate(
3484 BlockingNetworkDelegate::USER_CALLBACK);
3485 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
[email protected]9045b8822012-01-13 20:35:353486
[email protected]ef2bf422012-05-11 03:27:093487 TestURLRequestContext context(true);
3488 context.set_network_delegate(&network_delegate);
3489 context.Init();
[email protected]9045b8822012-01-13 20:35:353490
3491 {
[email protected]f7022f32014-08-21 16:32:193492 scoped_ptr<URLRequest> r(context.CreateRequest(
3493 test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d, NULL));
[email protected]9045b8822012-01-13 20:35:353494
[email protected]f7022f32014-08-21 16:32:193495 r->Start();
[email protected]255620da2013-08-19 13:14:293496 base::RunLoop().Run();
[email protected]dc5a5cf2012-09-26 02:49:303497 EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED,
3498 network_delegate.stage_blocked_for_callback());
[email protected]9045b8822012-01-13 20:35:353499 EXPECT_EQ(0, network_delegate.completed_requests());
3500 // Cancel before callback.
[email protected]f7022f32014-08-21 16:32:193501 r->Cancel();
[email protected]9045b8822012-01-13 20:35:353502 // Ensure that network delegate is notified.
3503 EXPECT_EQ(1, network_delegate.completed_requests());
[email protected]f7022f32014-08-21 16:32:193504 EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status());
3505 EXPECT_EQ(ERR_ABORTED, r->status().error());
[email protected]9045b8822012-01-13 20:35:353506 EXPECT_EQ(1, network_delegate.created_requests());
3507 EXPECT_EQ(0, network_delegate.destroyed_requests());
3508 }
3509 EXPECT_EQ(1, network_delegate.destroyed_requests());
3510}
3511
[email protected]95409e12010-08-17 20:07:113512// In this unit test, we're using the HTTPTestServer as a proxy server and
3513// issuing a CONNECT request with the magic host name "www.server-auth.com".
3514// The HTTPTestServer will return a 401 response, which we should balk at.
[email protected]b89290212009-08-14 22:37:353515TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) {
[email protected]95409e12010-08-17 20:07:113516 ASSERT_TRUE(test_server_.Start());
3517
[email protected]ceefd7fd2012-11-29 00:36:243518 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]d5a4dd62012-05-23 01:41:043519 TestURLRequestContextWithProxy context(
[email protected]2ca01e52013-10-31 22:05:193520 test_server_.host_port_pair().ToString(), &network_delegate);
[email protected]87a09a92011-07-14 15:50:503521
[email protected]dc651782009-02-14 01:45:083522 TestDelegate d;
3523 {
[email protected]f7022f32014-08-21 16:32:193524 scoped_ptr<URLRequest> r(context.CreateRequest(
3525 GURL("https://www.server-auth.com/"), DEFAULT_PRIORITY, &d, NULL));
[email protected]dc651782009-02-14 01:45:083526
[email protected]f7022f32014-08-21 16:32:193527 r->Start();
3528 EXPECT_TRUE(r->is_pending());
[email protected]dc651782009-02-14 01:45:083529
[email protected]255620da2013-08-19 13:14:293530 base::RunLoop().Run();
[email protected]dc651782009-02-14 01:45:083531
[email protected]f7022f32014-08-21 16:32:193532 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
[email protected]d8fc4722014-06-13 13:17:153533 // The proxy server is not set before failure.
[email protected]f7022f32014-08-21 16:32:193534 EXPECT_TRUE(r->proxy_server().IsEmpty());
3535 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r->status().error());
[email protected]dc651782009-02-14 01:45:083536 }
3537}
3538
[email protected]b89290212009-08-14 22:37:353539TEST_F(URLRequestTestHTTP, GetTest_NoCache) {
[email protected]95409e12010-08-17 20:07:113540 ASSERT_TRUE(test_server_.Start());
3541
initial.commit586acc5fe2008-07-26 22:42:523542 TestDelegate d;
3543 {
[email protected]f7022f32014-08-21 16:32:193544 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
3545 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL));
initial.commit586acc5fe2008-07-26 22:42:523546
[email protected]f7022f32014-08-21 16:32:193547 r->Start();
3548 EXPECT_TRUE(r->is_pending());
initial.commit586acc5fe2008-07-26 22:42:523549
[email protected]255620da2013-08-19 13:14:293550 base::RunLoop().Run();
initial.commit586acc5fe2008-07-26 22:42:523551
3552 EXPECT_EQ(1, d.response_started_count());
3553 EXPECT_FALSE(d.received_data_before_response());
3554 EXPECT_NE(0, d.bytes_received());
[email protected]6d81b482011-02-22 19:47:193555 EXPECT_EQ(test_server_.host_port_pair().host(),
[email protected]f7022f32014-08-21 16:32:193556 r->GetSocketAddress().host());
[email protected]6d81b482011-02-22 19:47:193557 EXPECT_EQ(test_server_.host_port_pair().port(),
[email protected]f7022f32014-08-21 16:32:193558 r->GetSocketAddress().port());
[email protected]c31a54592009-09-04 02:36:163559
[email protected]9e743cd2010-03-16 07:03:533560 // TODO(eroman): Add back the NetLog tests...
initial.commit586acc5fe2008-07-26 22:42:523561 }
initial.commit586acc5fe2008-07-26 22:42:523562}
3563
[email protected]263163f2012-06-14 22:40:343564// This test has the server send a large number of cookies to the client.
3565// To ensure that no number of cookies causes a crash, a galloping binary
3566// search is used to estimate that maximum number of cookies that are accepted
3567// by the browser. Beyond the maximum number, the request will fail with
3568// ERR_RESPONSE_HEADERS_TOO_BIG.
[email protected]69dd6fe2013-02-23 23:15:303569#if defined(OS_WIN)
3570// http://crbug.com/177916
3571#define MAYBE_GetTest_ManyCookies DISABLED_GetTest_ManyCookies
3572#else
3573#define MAYBE_GetTest_ManyCookies GetTest_ManyCookies
3574#endif // defined(OS_WIN)
3575TEST_F(URLRequestTestHTTP, MAYBE_GetTest_ManyCookies) {
[email protected]263163f2012-06-14 22:40:343576 ASSERT_TRUE(test_server_.Start());
3577
3578 int lower_bound = 0;
3579 int upper_bound = 1;
3580
3581 // Double the number of cookies until the response header limits are
3582 // exceeded.
3583 while (DoManyCookiesRequest(upper_bound)) {
3584 lower_bound = upper_bound;
3585 upper_bound *= 2;
3586 ASSERT_LT(upper_bound, 1000000);
3587 }
3588
3589 int tolerance = upper_bound * 0.005;
3590 if (tolerance < 2)
3591 tolerance = 2;
3592
3593 // Perform a binary search to find the highest possible number of cookies,
3594 // within the desired tolerance.
3595 while (upper_bound - lower_bound >= tolerance) {
3596 int num_cookies = (lower_bound + upper_bound) / 2;
3597
3598 if (DoManyCookiesRequest(num_cookies))
3599 lower_bound = num_cookies;
3600 else
3601 upper_bound = num_cookies;
3602 }
3603 // Success: the test did not crash.
3604}
3605
[email protected]b89290212009-08-14 22:37:353606TEST_F(URLRequestTestHTTP, GetTest) {
[email protected]95409e12010-08-17 20:07:113607 ASSERT_TRUE(test_server_.Start());
3608
initial.commit586acc5fe2008-07-26 22:42:523609 TestDelegate d;
3610 {
[email protected]f7022f32014-08-21 16:32:193611 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
3612 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL));
initial.commit586acc5fe2008-07-26 22:42:523613
[email protected]f7022f32014-08-21 16:32:193614 r->Start();
3615 EXPECT_TRUE(r->is_pending());
initial.commit586acc5fe2008-07-26 22:42:523616
[email protected]255620da2013-08-19 13:14:293617 base::RunLoop().Run();
initial.commit586acc5fe2008-07-26 22:42:523618
3619 EXPECT_EQ(1, d.response_started_count());
3620 EXPECT_FALSE(d.received_data_before_response());
3621 EXPECT_NE(0, d.bytes_received());
[email protected]6d81b482011-02-22 19:47:193622 EXPECT_EQ(test_server_.host_port_pair().host(),
[email protected]f7022f32014-08-21 16:32:193623 r->GetSocketAddress().host());
[email protected]6d81b482011-02-22 19:47:193624 EXPECT_EQ(test_server_.host_port_pair().port(),
[email protected]f7022f32014-08-21 16:32:193625 r->GetSocketAddress().port());
initial.commit586acc5fe2008-07-26 22:42:523626 }
[email protected]5d7b373e2009-09-02 07:19:033627}
3628
[email protected]79e1fd62013-06-20 06:50:043629TEST_F(URLRequestTestHTTP, GetTest_GetFullRequestHeaders) {
3630 ASSERT_TRUE(test_server_.Start());
3631
3632 TestDelegate d;
3633 {
3634 GURL test_url(test_server_.GetURL(std::string()));
[email protected]f7022f32014-08-21 16:32:193635 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
3636 test_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]79e1fd62013-06-20 06:50:043637
3638 HttpRequestHeaders headers;
[email protected]f7022f32014-08-21 16:32:193639 EXPECT_FALSE(r->GetFullRequestHeaders(&headers));
[email protected]79e1fd62013-06-20 06:50:043640
[email protected]f7022f32014-08-21 16:32:193641 r->Start();
3642 EXPECT_TRUE(r->is_pending());
[email protected]79e1fd62013-06-20 06:50:043643
[email protected]255620da2013-08-19 13:14:293644 base::RunLoop().Run();
[email protected]79e1fd62013-06-20 06:50:043645
3646 EXPECT_EQ(1, d.response_started_count());
3647 EXPECT_FALSE(d.received_data_before_response());
3648 EXPECT_NE(0, d.bytes_received());
3649 EXPECT_EQ(test_server_.host_port_pair().host(),
[email protected]f7022f32014-08-21 16:32:193650 r->GetSocketAddress().host());
[email protected]79e1fd62013-06-20 06:50:043651 EXPECT_EQ(test_server_.host_port_pair().port(),
[email protected]f7022f32014-08-21 16:32:193652 r->GetSocketAddress().port());
[email protected]79e1fd62013-06-20 06:50:043653
3654 EXPECT_TRUE(d.have_full_request_headers());
3655 CheckFullRequestHeaders(d.full_request_headers(), test_url);
3656 }
3657}
3658
[email protected]58e32bb2013-01-21 18:23:253659TEST_F(URLRequestTestHTTP, GetTestLoadTiming) {
3660 ASSERT_TRUE(test_server_.Start());
3661
3662 TestDelegate d;
3663 {
[email protected]f7022f32014-08-21 16:32:193664 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
3665 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL));
[email protected]58e32bb2013-01-21 18:23:253666
[email protected]f7022f32014-08-21 16:32:193667 r->Start();
3668 EXPECT_TRUE(r->is_pending());
[email protected]58e32bb2013-01-21 18:23:253669
[email protected]255620da2013-08-19 13:14:293670 base::RunLoop().Run();
[email protected]58e32bb2013-01-21 18:23:253671
3672 LoadTimingInfo load_timing_info;
[email protected]f7022f32014-08-21 16:32:193673 r->GetLoadTimingInfo(&load_timing_info);
[email protected]58e32bb2013-01-21 18:23:253674 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
3675
3676 EXPECT_EQ(1, d.response_started_count());
3677 EXPECT_FALSE(d.received_data_before_response());
3678 EXPECT_NE(0, d.bytes_received());
3679 EXPECT_EQ(test_server_.host_port_pair().host(),
[email protected]f7022f32014-08-21 16:32:193680 r->GetSocketAddress().host());
[email protected]58e32bb2013-01-21 18:23:253681 EXPECT_EQ(test_server_.host_port_pair().port(),
[email protected]f7022f32014-08-21 16:32:193682 r->GetSocketAddress().port());
[email protected]58e32bb2013-01-21 18:23:253683 }
3684}
3685
[email protected]aad63572011-05-24 20:14:393686TEST_F(URLRequestTestHTTP, GetZippedTest) {
3687 ASSERT_TRUE(test_server_.Start());
3688
3689 // Parameter that specifies the Content-Length field in the response:
3690 // C - Compressed length.
3691 // U - Uncompressed length.
3692 // L - Large length (larger than both C & U).
3693 // M - Medium length (between C & U).
3694 // S - Small length (smaller than both C & U).
3695 const char test_parameters[] = "CULMS";
3696 const int num_tests = arraysize(test_parameters)- 1; // Skip NULL.
3697 // C & U should be OK.
[email protected]f0e2bf42011-07-22 21:21:443698 // L & M are larger than the data sent, and show an error.
[email protected]aad63572011-05-24 20:14:393699 // S has too little data, but we seem to accept it.
3700 const bool test_expect_success[num_tests] =
[email protected]f001bd6a2011-12-08 04:31:373701 { true, true, false, false, true };
[email protected]aad63572011-05-24 20:14:393702
3703 for (int i = 0; i < num_tests ; i++) {
3704 TestDelegate d;
3705 {
3706 std::string test_file =
3707 base::StringPrintf("compressedfiles/BullRunSpeech.txt?%c",
3708 test_parameters[i]);
[email protected]aad63572011-05-24 20:14:393709
[email protected]ceefd7fd2012-11-29 00:36:243710 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]ef2bf422012-05-11 03:27:093711 TestURLRequestContext context(true);
3712 context.set_network_delegate(&network_delegate);
3713 context.Init();
[email protected]87a09a92011-07-14 15:50:503714
[email protected]f7022f32014-08-21 16:32:193715 scoped_ptr<URLRequest> r(context.CreateRequest(
3716 test_server_.GetURL(test_file), DEFAULT_PRIORITY, &d, NULL));
3717 r->Start();
3718 EXPECT_TRUE(r->is_pending());
[email protected]aad63572011-05-24 20:14:393719
[email protected]255620da2013-08-19 13:14:293720 base::RunLoop().Run();
[email protected]aad63572011-05-24 20:14:393721
3722 EXPECT_EQ(1, d.response_started_count());
3723 EXPECT_FALSE(d.received_data_before_response());
3724 VLOG(1) << " Received " << d.bytes_received() << " bytes"
[email protected]f7022f32014-08-21 16:32:193725 << " status = " << r->status().status()
3726 << " error = " << r->status().error();
[email protected]aad63572011-05-24 20:14:393727 if (test_expect_success[i]) {
[email protected]f7022f32014-08-21 16:32:193728 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status())
[email protected]aad63572011-05-24 20:14:393729 << " Parameter = \"" << test_file << "\"";
3730 } else {
[email protected]f7022f32014-08-21 16:32:193731 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
3732 EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, r->status().error())
[email protected]aad63572011-05-24 20:14:393733 << " Parameter = \"" << test_file << "\"";
3734 }
3735 }
3736 }
3737}
3738
[email protected]c044616e2013-02-20 02:01:263739TEST_F(URLRequestTestHTTP, HTTPSToHTTPRedirectNoRefererTest) {
[email protected]95409e12010-08-17 20:07:113740 ASSERT_TRUE(test_server_.Start());
3741
[email protected]ce7d0cbc2013-05-03 18:57:223742 SpawnedTestServer https_test_server(
3743 SpawnedTestServer::TYPE_HTTPS, SpawnedTestServer::kLocalhost,
[email protected]6cdfd7f2013-02-08 20:40:153744 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]95409e12010-08-17 20:07:113745 ASSERT_TRUE(https_test_server.Start());
[email protected]7844480a2009-12-16 21:18:583746
3747 // An https server is sent a request with an https referer,
3748 // and responds with a redirect to an http url. The http
3749 // server should not be sent the referer.
[email protected]007b3f82013-04-09 08:46:453750 GURL http_destination = test_server_.GetURL(std::string());
[email protected]7844480a2009-12-16 21:18:583751 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:193752 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
[email protected]2ca01e52013-10-31 22:05:193753 https_test_server.GetURL("server-redirect?" + http_destination.spec()),
[email protected]f7022f32014-08-21 16:32:193754 DEFAULT_PRIORITY, &d, NULL));
3755 req->SetReferrer("https://www.referrer.com/");
3756 req->Start();
[email protected]255620da2013-08-19 13:14:293757 base::RunLoop().Run();
[email protected]7844480a2009-12-16 21:18:583758
3759 EXPECT_EQ(1, d.response_started_count());
3760 EXPECT_EQ(1, d.received_redirect_count());
[email protected]f7022f32014-08-21 16:32:193761 EXPECT_EQ(http_destination, req->url());
3762 EXPECT_EQ(std::string(), req->referrer());
[email protected]7844480a2009-12-16 21:18:583763}
3764
[email protected]58e32bb2013-01-21 18:23:253765TEST_F(URLRequestTestHTTP, RedirectLoadTiming) {
3766 ASSERT_TRUE(test_server_.Start());
3767
[email protected]007b3f82013-04-09 08:46:453768 GURL destination_url = test_server_.GetURL(std::string());
3769 GURL original_url =
3770 test_server_.GetURL("server-redirect?" + destination_url.spec());
[email protected]58e32bb2013-01-21 18:23:253771 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:193772 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
3773 original_url, DEFAULT_PRIORITY, &d, NULL));
3774 req->Start();
[email protected]255620da2013-08-19 13:14:293775 base::RunLoop().Run();
[email protected]58e32bb2013-01-21 18:23:253776
3777 EXPECT_EQ(1, d.response_started_count());
3778 EXPECT_EQ(1, d.received_redirect_count());
[email protected]f7022f32014-08-21 16:32:193779 EXPECT_EQ(destination_url, req->url());
3780 EXPECT_EQ(original_url, req->original_url());
3781 ASSERT_EQ(2U, req->url_chain().size());
3782 EXPECT_EQ(original_url, req->url_chain()[0]);
3783 EXPECT_EQ(destination_url, req->url_chain()[1]);
[email protected]58e32bb2013-01-21 18:23:253784
3785 LoadTimingInfo load_timing_info_before_redirect;
3786 EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeRedirect(
3787 &load_timing_info_before_redirect));
3788 TestLoadTimingNotReused(load_timing_info_before_redirect,
3789 CONNECT_TIMING_HAS_DNS_TIMES);
3790
3791 LoadTimingInfo load_timing_info;
[email protected]f7022f32014-08-21 16:32:193792 req->GetLoadTimingInfo(&load_timing_info);
[email protected]58e32bb2013-01-21 18:23:253793 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
3794
3795 // Check that a new socket was used on redirect, since the server does not
3796 // supposed keep-alive sockets, and that the times before the redirect are
3797 // before the ones recorded for the second request.
3798 EXPECT_NE(load_timing_info_before_redirect.socket_log_id,
3799 load_timing_info.socket_log_id);
3800 EXPECT_LE(load_timing_info_before_redirect.receive_headers_end,
3801 load_timing_info.connect_timing.connect_start);
3802}
3803
[email protected]8f1ac082011-04-19 21:14:133804TEST_F(URLRequestTestHTTP, MultipleRedirectTest) {
3805 ASSERT_TRUE(test_server_.Start());
3806
[email protected]007b3f82013-04-09 08:46:453807 GURL destination_url = test_server_.GetURL(std::string());
3808 GURL middle_redirect_url =
3809 test_server_.GetURL("server-redirect?" + destination_url.spec());
[email protected]8f1ac082011-04-19 21:14:133810 GURL original_url = test_server_.GetURL(
3811 "server-redirect?" + middle_redirect_url.spec());
3812 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:193813 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
3814 original_url, DEFAULT_PRIORITY, &d, NULL));
3815 req->Start();
[email protected]255620da2013-08-19 13:14:293816 base::RunLoop().Run();
[email protected]8f1ac082011-04-19 21:14:133817
3818 EXPECT_EQ(1, d.response_started_count());
3819 EXPECT_EQ(2, d.received_redirect_count());
[email protected]f7022f32014-08-21 16:32:193820 EXPECT_EQ(destination_url, req->url());
3821 EXPECT_EQ(original_url, req->original_url());
3822 ASSERT_EQ(3U, req->url_chain().size());
3823 EXPECT_EQ(original_url, req->url_chain()[0]);
3824 EXPECT_EQ(middle_redirect_url, req->url_chain()[1]);
3825 EXPECT_EQ(destination_url, req->url_chain()[2]);
[email protected]8f1ac082011-04-19 21:14:133826}
3827
[email protected]abe1c4a2013-10-25 19:28:513828// First and second pieces of information logged by delegates to URLRequests.
3829const char kFirstDelegateInfo[] = "Wonderful delegate";
3830const char kSecondDelegateInfo[] = "Exciting delegate";
3831
3832// Logs delegate information to a URLRequest. The first string is logged
3833// synchronously on Start(), using DELEGATE_INFO_DEBUG_ONLY. The second is
3834// logged asynchronously, using DELEGATE_INFO_DISPLAY_TO_USER. Then
3835// another asynchronous call is used to clear the delegate information
3836// before calling a callback. The object then deletes itself.
3837class AsyncDelegateLogger : public base::RefCounted<AsyncDelegateLogger> {
3838 public:
3839 typedef base::Callback<void()> Callback;
3840
3841 // Each time delegate information is added to the URLRequest, the resulting
3842 // load state is checked. The expected load state after each request is
3843 // passed in as an argument.
3844 static void Run(URLRequest* url_request,
3845 LoadState expected_first_load_state,
3846 LoadState expected_second_load_state,
3847 LoadState expected_third_load_state,
3848 const Callback& callback) {
3849 AsyncDelegateLogger* logger = new AsyncDelegateLogger(
3850 url_request,
3851 expected_first_load_state,
3852 expected_second_load_state,
3853 expected_third_load_state,
3854 callback);
3855 logger->Start();
3856 }
3857
3858 // Checks that the log entries, starting with log_position, contain the
3859 // DELEGATE_INFO NetLog events that an AsyncDelegateLogger should have
3860 // recorded. Returns the index of entry after the expected number of
3861 // events this logged, or entries.size() if there aren't enough entries.
3862 static size_t CheckDelegateInfo(
3863 const CapturingNetLog::CapturedEntryList& entries, size_t log_position) {
3864 // There should be 4 DELEGATE_INFO events: Two begins and two ends.
3865 if (log_position + 3 >= entries.size()) {
3866 ADD_FAILURE() << "Not enough log entries";
3867 return entries.size();
3868 }
3869 std::string delegate_info;
3870 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type);
3871 EXPECT_EQ(NetLog::PHASE_BEGIN, entries[log_position].phase);
3872 EXPECT_TRUE(entries[log_position].GetStringValue("delegate_info",
3873 &delegate_info));
3874 EXPECT_EQ(kFirstDelegateInfo, delegate_info);
3875
3876 ++log_position;
3877 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type);
3878 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
3879
3880 ++log_position;
3881 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type);
3882 EXPECT_EQ(NetLog::PHASE_BEGIN, entries[log_position].phase);
3883 EXPECT_TRUE(entries[log_position].GetStringValue("delegate_info",
3884 &delegate_info));
3885 EXPECT_EQ(kSecondDelegateInfo, delegate_info);
3886
3887 ++log_position;
3888 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type);
3889 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
3890
3891 return log_position + 1;
3892 }
3893
[email protected]1826a402014-01-08 15:40:483894 // Find delegate request begin and end messages for OnBeforeNetworkStart.
3895 // Returns the position of the end message.
3896 static size_t ExpectBeforeNetworkEvents(
3897 const CapturingNetLog::CapturedEntryList& entries,
3898 size_t log_position) {
3899 log_position =
3900 ExpectLogContainsSomewhereAfter(entries,
3901 log_position,
3902 NetLog::TYPE_URL_REQUEST_DELEGATE,
3903 NetLog::PHASE_BEGIN);
3904 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE,
3905 entries[log_position + 1].type);
3906 EXPECT_EQ(NetLog::PHASE_END, entries[log_position + 1].phase);
3907 return log_position + 1;
3908 }
3909
[email protected]abe1c4a2013-10-25 19:28:513910 private:
3911 friend class base::RefCounted<AsyncDelegateLogger>;
3912
3913 AsyncDelegateLogger(URLRequest* url_request,
3914 LoadState expected_first_load_state,
3915 LoadState expected_second_load_state,
3916 LoadState expected_third_load_state,
3917 const Callback& callback)
3918 : url_request_(url_request),
3919 expected_first_load_state_(expected_first_load_state),
3920 expected_second_load_state_(expected_second_load_state),
3921 expected_third_load_state_(expected_third_load_state),
3922 callback_(callback) {
3923 }
3924
3925 ~AsyncDelegateLogger() {}
3926
3927 void Start() {
[email protected]f8fe5cf2013-12-04 20:11:533928 url_request_->LogBlockedBy(kFirstDelegateInfo);
[email protected]abe1c4a2013-10-25 19:28:513929 LoadStateWithParam load_state = url_request_->GetLoadState();
3930 EXPECT_EQ(expected_first_load_state_, load_state.state);
3931 EXPECT_NE(ASCIIToUTF16(kFirstDelegateInfo), load_state.param);
3932 base::MessageLoop::current()->PostTask(
3933 FROM_HERE,
3934 base::Bind(&AsyncDelegateLogger::LogSecondDelegate, this));
3935 }
3936
3937 void LogSecondDelegate() {
[email protected]f8fe5cf2013-12-04 20:11:533938 url_request_->LogAndReportBlockedBy(kSecondDelegateInfo);
[email protected]abe1c4a2013-10-25 19:28:513939 LoadStateWithParam load_state = url_request_->GetLoadState();
3940 EXPECT_EQ(expected_second_load_state_, load_state.state);
3941 if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) {
3942 EXPECT_EQ(ASCIIToUTF16(kSecondDelegateInfo), load_state.param);
3943 } else {
3944 EXPECT_NE(ASCIIToUTF16(kSecondDelegateInfo), load_state.param);
3945 }
3946 base::MessageLoop::current()->PostTask(
3947 FROM_HERE,
3948 base::Bind(&AsyncDelegateLogger::LogComplete, this));
3949 }
3950
3951 void LogComplete() {
[email protected]f8fe5cf2013-12-04 20:11:533952 url_request_->LogUnblocked();
[email protected]abe1c4a2013-10-25 19:28:513953 LoadStateWithParam load_state = url_request_->GetLoadState();
3954 EXPECT_EQ(expected_third_load_state_, load_state.state);
3955 if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE)
[email protected]754bd202013-12-18 08:29:083956 EXPECT_EQ(base::string16(), load_state.param);
[email protected]abe1c4a2013-10-25 19:28:513957 callback_.Run();
3958 }
3959
3960 URLRequest* url_request_;
3961 const int expected_first_load_state_;
3962 const int expected_second_load_state_;
3963 const int expected_third_load_state_;
3964 const Callback callback_;
3965
3966 DISALLOW_COPY_AND_ASSIGN(AsyncDelegateLogger);
3967};
3968
3969// NetworkDelegate that logs delegate information before a request is started,
3970// before headers are sent, when headers are read, and when auth information
3971// is requested. Uses AsyncDelegateLogger.
3972class AsyncLoggingNetworkDelegate : public TestNetworkDelegate {
3973 public:
3974 AsyncLoggingNetworkDelegate() {}
3975 virtual ~AsyncLoggingNetworkDelegate() {}
3976
3977 // NetworkDelegate implementation.
3978 virtual int OnBeforeURLRequest(URLRequest* request,
3979 const CompletionCallback& callback,
3980 GURL* new_url) OVERRIDE {
3981 TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url);
3982 return RunCallbackAsynchronously(request, callback);
3983 }
3984
3985 virtual int OnBeforeSendHeaders(URLRequest* request,
3986 const CompletionCallback& callback,
3987 HttpRequestHeaders* headers) OVERRIDE {
3988 TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers);
3989 return RunCallbackAsynchronously(request, callback);
3990 }
3991
3992 virtual int OnHeadersReceived(
3993 URLRequest* request,
3994 const CompletionCallback& callback,
3995 const HttpResponseHeaders* original_response_headers,
[email protected]5f714132014-03-26 10:41:163996 scoped_refptr<HttpResponseHeaders>* override_response_headers,
3997 GURL* allowed_unsafe_redirect_url) OVERRIDE {
3998 TestNetworkDelegate::OnHeadersReceived(request,
3999 callback,
[email protected]abe1c4a2013-10-25 19:28:514000 original_response_headers,
[email protected]5f714132014-03-26 10:41:164001 override_response_headers,
4002 allowed_unsafe_redirect_url);
[email protected]abe1c4a2013-10-25 19:28:514003 return RunCallbackAsynchronously(request, callback);
4004 }
4005
4006 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired(
4007 URLRequest* request,
4008 const AuthChallengeInfo& auth_info,
4009 const AuthCallback& callback,
4010 AuthCredentials* credentials) OVERRIDE {
4011 AsyncDelegateLogger::Run(
4012 request,
4013 LOAD_STATE_WAITING_FOR_DELEGATE,
4014 LOAD_STATE_WAITING_FOR_DELEGATE,
4015 LOAD_STATE_WAITING_FOR_DELEGATE,
4016 base::Bind(&AsyncLoggingNetworkDelegate::SetAuthAndResume,
4017 callback, credentials));
4018 return AUTH_REQUIRED_RESPONSE_IO_PENDING;
4019 }
4020
4021 private:
4022 static int RunCallbackAsynchronously(
4023 URLRequest* request,
4024 const CompletionCallback& callback) {
4025 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(callback, OK));
4031 return ERR_IO_PENDING;
4032 }
4033
4034 static void SetAuthAndResume(const AuthCallback& callback,
4035 AuthCredentials* credentials) {
4036 *credentials = AuthCredentials(kUser, kSecret);
4037 callback.Run(NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
4038 }
4039
4040 DISALLOW_COPY_AND_ASSIGN(AsyncLoggingNetworkDelegate);
4041};
4042
4043// URLRequest::Delegate that logs delegate information when the headers
4044// are received, when each read completes, and during redirects. Uses
4045// AsyncDelegateLogger. Can optionally cancel a request in any phase.
4046//
4047// Inherits from TestDelegate to reuse the TestDelegate code to handle
4048// advancing to the next step in most cases, as well as cancellation.
4049class AsyncLoggingUrlRequestDelegate : public TestDelegate {
4050 public:
4051 enum CancelStage {
4052 NO_CANCEL = 0,
4053 CANCEL_ON_RECEIVED_REDIRECT,
4054 CANCEL_ON_RESPONSE_STARTED,
4055 CANCEL_ON_READ_COMPLETED
4056 };
4057
4058 explicit AsyncLoggingUrlRequestDelegate(CancelStage cancel_stage)
4059 : cancel_stage_(cancel_stage) {
4060 if (cancel_stage == CANCEL_ON_RECEIVED_REDIRECT)
4061 set_cancel_in_received_redirect(true);
4062 else if (cancel_stage == CANCEL_ON_RESPONSE_STARTED)
4063 set_cancel_in_response_started(true);
4064 else if (cancel_stage == CANCEL_ON_READ_COMPLETED)
4065 set_cancel_in_received_data(true);
4066 }
4067 virtual ~AsyncLoggingUrlRequestDelegate() {}
4068
4069 // URLRequest::Delegate implementation:
4070 void virtual OnReceivedRedirect(URLRequest* request,
[email protected]cba24642014-08-15 20:49:594071 const RedirectInfo& redirect_info,
[email protected]abe1c4a2013-10-25 19:28:514072 bool* defer_redirect) OVERRIDE {
4073 *defer_redirect = true;
4074 AsyncDelegateLogger::Run(
4075 request,
4076 LOAD_STATE_WAITING_FOR_DELEGATE,
4077 LOAD_STATE_WAITING_FOR_DELEGATE,
4078 LOAD_STATE_WAITING_FOR_DELEGATE,
4079 base::Bind(
4080 &AsyncLoggingUrlRequestDelegate::OnReceivedRedirectLoggingComplete,
[email protected]cba24642014-08-15 20:49:594081 base::Unretained(this), request, redirect_info));
[email protected]abe1c4a2013-10-25 19:28:514082 }
4083
4084 virtual void OnResponseStarted(URLRequest* request) OVERRIDE {
4085 AsyncDelegateLogger::Run(
4086 request,
4087 LOAD_STATE_WAITING_FOR_DELEGATE,
4088 LOAD_STATE_WAITING_FOR_DELEGATE,
4089 LOAD_STATE_WAITING_FOR_DELEGATE,
4090 base::Bind(
4091 &AsyncLoggingUrlRequestDelegate::OnResponseStartedLoggingComplete,
4092 base::Unretained(this), request));
4093 }
4094
4095 virtual void OnReadCompleted(URLRequest* request,
4096 int bytes_read) OVERRIDE {
4097 AsyncDelegateLogger::Run(
4098 request,
4099 LOAD_STATE_IDLE,
4100 LOAD_STATE_IDLE,
4101 LOAD_STATE_IDLE,
4102 base::Bind(
4103 &AsyncLoggingUrlRequestDelegate::AfterReadCompletedLoggingComplete,
4104 base::Unretained(this), request, bytes_read));
4105 }
4106
4107 private:
4108 void OnReceivedRedirectLoggingComplete(URLRequest* request,
[email protected]cba24642014-08-15 20:49:594109 const RedirectInfo& redirect_info) {
[email protected]abe1c4a2013-10-25 19:28:514110 bool defer_redirect = false;
[email protected]cba24642014-08-15 20:49:594111 TestDelegate::OnReceivedRedirect(request, redirect_info, &defer_redirect);
[email protected]abe1c4a2013-10-25 19:28:514112 // FollowDeferredRedirect should not be called after cancellation.
4113 if (cancel_stage_ == CANCEL_ON_RECEIVED_REDIRECT)
4114 return;
4115 if (!defer_redirect)
4116 request->FollowDeferredRedirect();
4117 }
4118
4119 void OnResponseStartedLoggingComplete(URLRequest* request) {
4120 // The parent class continues the request.
4121 TestDelegate::OnResponseStarted(request);
4122 }
4123
4124 void AfterReadCompletedLoggingComplete(URLRequest* request, int bytes_read) {
4125 // The parent class continues the request.
4126 TestDelegate::OnReadCompleted(request, bytes_read);
4127 }
4128
4129 const CancelStage cancel_stage_;
4130
4131 DISALLOW_COPY_AND_ASSIGN(AsyncLoggingUrlRequestDelegate);
4132};
4133
4134// Tests handling of delegate info before a request starts.
4135TEST_F(URLRequestTestHTTP, DelegateInfoBeforeStart) {
4136 ASSERT_TRUE(test_server_.Start());
4137
4138 TestDelegate request_delegate;
4139 TestURLRequestContext context(true);
4140 context.set_network_delegate(NULL);
4141 context.set_net_log(&net_log_);
4142 context.Init();
4143
4144 {
[email protected]f7022f32014-08-21 16:32:194145 scoped_ptr<URLRequest> r(context.CreateRequest(
4146 test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &request_delegate,
4147 NULL));
4148 LoadStateWithParam load_state = r->GetLoadState();
[email protected]abe1c4a2013-10-25 19:28:514149 EXPECT_EQ(LOAD_STATE_IDLE, load_state.state);
[email protected]754bd202013-12-18 08:29:084150 EXPECT_EQ(base::string16(), load_state.param);
[email protected]abe1c4a2013-10-25 19:28:514151
4152 AsyncDelegateLogger::Run(
[email protected]f7022f32014-08-21 16:32:194153 r.get(),
[email protected]abe1c4a2013-10-25 19:28:514154 LOAD_STATE_WAITING_FOR_DELEGATE,
4155 LOAD_STATE_WAITING_FOR_DELEGATE,
4156 LOAD_STATE_IDLE,
[email protected]f7022f32014-08-21 16:32:194157 base::Bind(&URLRequest::Start, base::Unretained(r.get())));
[email protected]abe1c4a2013-10-25 19:28:514158
4159 base::RunLoop().Run();
4160
[email protected]f7022f32014-08-21 16:32:194161 EXPECT_EQ(200, r->GetResponseCode());
4162 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
[email protected]abe1c4a2013-10-25 19:28:514163 }
4164
4165 CapturingNetLog::CapturedEntryList entries;
4166 net_log_.GetEntries(&entries);
4167 size_t log_position = ExpectLogContainsSomewhereAfter(
4168 entries,
4169 0,
4170 NetLog::TYPE_DELEGATE_INFO,
4171 NetLog::PHASE_BEGIN);
4172
4173 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, log_position);
4174
4175 // Nothing else should add any delegate info to the request.
4176 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4177 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4178}
4179
4180// Tests handling of delegate info from a network delegate.
4181TEST_F(URLRequestTestHTTP, NetworkDelegateInfo) {
4182 ASSERT_TRUE(test_server_.Start());
4183
4184 TestDelegate request_delegate;
4185 AsyncLoggingNetworkDelegate network_delegate;
4186 TestURLRequestContext context(true);
4187 context.set_network_delegate(&network_delegate);
4188 context.set_net_log(&net_log_);
4189 context.Init();
4190
4191 {
[email protected]f7022f32014-08-21 16:32:194192 scoped_ptr<URLRequest> r(context.CreateRequest(
4193 test_server_.GetURL("simple.html"), DEFAULT_PRIORITY, &request_delegate,
4194 NULL));
4195 LoadStateWithParam load_state = r->GetLoadState();
[email protected]abe1c4a2013-10-25 19:28:514196 EXPECT_EQ(LOAD_STATE_IDLE, load_state.state);
[email protected]754bd202013-12-18 08:29:084197 EXPECT_EQ(base::string16(), load_state.param);
[email protected]abe1c4a2013-10-25 19:28:514198
[email protected]f7022f32014-08-21 16:32:194199 r->Start();
[email protected]abe1c4a2013-10-25 19:28:514200 base::RunLoop().Run();
4201
[email protected]f7022f32014-08-21 16:32:194202 EXPECT_EQ(200, r->GetResponseCode());
4203 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
[email protected]abe1c4a2013-10-25 19:28:514204 EXPECT_EQ(1, network_delegate.created_requests());
4205 EXPECT_EQ(0, network_delegate.destroyed_requests());
4206 }
4207 EXPECT_EQ(1, network_delegate.destroyed_requests());
4208
4209 size_t log_position = 0;
4210 CapturingNetLog::CapturedEntryList entries;
4211 net_log_.GetEntries(&entries);
4212 for (size_t i = 0; i < 3; ++i) {
4213 log_position = ExpectLogContainsSomewhereAfter(
4214 entries,
4215 log_position + 1,
4216 NetLog::TYPE_URL_REQUEST_DELEGATE,
4217 NetLog::PHASE_BEGIN);
4218
4219 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4220 log_position + 1);
4221
4222 ASSERT_LT(log_position, entries.size());
4223 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4224 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
[email protected]1826a402014-01-08 15:40:484225
4226 if (i == 1) {
4227 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4228 entries, log_position + 1);
4229 }
[email protected]abe1c4a2013-10-25 19:28:514230 }
4231
4232 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4233 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4234}
4235
4236// Tests handling of delegate info from a network delegate in the case of an
4237// HTTP redirect.
4238TEST_F(URLRequestTestHTTP, NetworkDelegateInfoRedirect) {
4239 ASSERT_TRUE(test_server_.Start());
4240
4241 TestDelegate request_delegate;
4242 AsyncLoggingNetworkDelegate network_delegate;
4243 TestURLRequestContext context(true);
4244 context.set_network_delegate(&network_delegate);
4245 context.set_net_log(&net_log_);
4246 context.Init();
4247
4248 {
[email protected]f7022f32014-08-21 16:32:194249 scoped_ptr<URLRequest> r(context.CreateRequest(
4250 test_server_.GetURL("server-redirect?simple.html"), DEFAULT_PRIORITY,
4251 &request_delegate, NULL));
4252 LoadStateWithParam load_state = r->GetLoadState();
[email protected]abe1c4a2013-10-25 19:28:514253 EXPECT_EQ(LOAD_STATE_IDLE, load_state.state);
[email protected]754bd202013-12-18 08:29:084254 EXPECT_EQ(base::string16(), load_state.param);
[email protected]abe1c4a2013-10-25 19:28:514255
[email protected]f7022f32014-08-21 16:32:194256 r->Start();
[email protected]abe1c4a2013-10-25 19:28:514257 base::RunLoop().Run();
4258
[email protected]f7022f32014-08-21 16:32:194259 EXPECT_EQ(200, r->GetResponseCode());
4260 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
[email protected]abe1c4a2013-10-25 19:28:514261 EXPECT_EQ(2, network_delegate.created_requests());
4262 EXPECT_EQ(0, network_delegate.destroyed_requests());
4263 }
4264 EXPECT_EQ(1, network_delegate.destroyed_requests());
4265
4266 size_t log_position = 0;
4267 CapturingNetLog::CapturedEntryList entries;
4268 net_log_.GetEntries(&entries);
4269 // The NetworkDelegate logged information in OnBeforeURLRequest,
4270 // OnBeforeSendHeaders, and OnHeadersReceived.
4271 for (size_t i = 0; i < 3; ++i) {
4272 log_position = ExpectLogContainsSomewhereAfter(
4273 entries,
4274 log_position + 1,
4275 NetLog::TYPE_URL_REQUEST_DELEGATE,
4276 NetLog::PHASE_BEGIN);
4277
4278 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4279 log_position + 1);
4280
4281 ASSERT_LT(log_position, entries.size());
4282 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4283 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
[email protected]1826a402014-01-08 15:40:484284
4285 if (i == 1) {
4286 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4287 entries, log_position + 1);
4288 }
[email protected]abe1c4a2013-10-25 19:28:514289 }
4290
4291 // The URLRequest::Delegate then gets informed about the redirect.
4292 log_position = ExpectLogContainsSomewhereAfter(
4293 entries,
4294 log_position + 1,
4295 NetLog::TYPE_URL_REQUEST_DELEGATE,
4296 NetLog::PHASE_BEGIN);
4297
4298 // The NetworkDelegate logged information in the same three events as before.
4299 for (size_t i = 0; i < 3; ++i) {
4300 log_position = ExpectLogContainsSomewhereAfter(
4301 entries,
4302 log_position + 1,
4303 NetLog::TYPE_URL_REQUEST_DELEGATE,
4304 NetLog::PHASE_BEGIN);
4305
4306 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4307 log_position + 1);
4308
4309 ASSERT_LT(log_position, entries.size());
4310 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4311 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4312 }
4313
4314 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4315 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4316}
4317
4318// Tests handling of delegate info from a network delegate in the case of HTTP
4319// AUTH.
4320TEST_F(URLRequestTestHTTP, NetworkDelegateInfoAuth) {
4321 ASSERT_TRUE(test_server_.Start());
4322
4323 TestDelegate request_delegate;
4324 AsyncLoggingNetworkDelegate network_delegate;
4325 TestURLRequestContext context(true);
4326 context.set_network_delegate(&network_delegate);
4327 context.set_net_log(&net_log_);
4328 context.Init();
4329
4330 {
[email protected]f7022f32014-08-21 16:32:194331 scoped_ptr<URLRequest> r(context.CreateRequest(
4332 test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &request_delegate,
4333 NULL));
4334 LoadStateWithParam load_state = r->GetLoadState();
[email protected]abe1c4a2013-10-25 19:28:514335 EXPECT_EQ(LOAD_STATE_IDLE, load_state.state);
[email protected]754bd202013-12-18 08:29:084336 EXPECT_EQ(base::string16(), load_state.param);
[email protected]abe1c4a2013-10-25 19:28:514337
[email protected]f7022f32014-08-21 16:32:194338 r->Start();
[email protected]abe1c4a2013-10-25 19:28:514339 base::RunLoop().Run();
4340
[email protected]f7022f32014-08-21 16:32:194341 EXPECT_EQ(200, r->GetResponseCode());
4342 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
[email protected]abe1c4a2013-10-25 19:28:514343 EXPECT_EQ(1, network_delegate.created_requests());
4344 EXPECT_EQ(0, network_delegate.destroyed_requests());
4345 }
4346 EXPECT_EQ(1, network_delegate.destroyed_requests());
4347
4348 size_t log_position = 0;
4349 CapturingNetLog::CapturedEntryList entries;
4350 net_log_.GetEntries(&entries);
4351 // The NetworkDelegate should have logged information in OnBeforeURLRequest,
4352 // OnBeforeSendHeaders, OnHeadersReceived, OnAuthRequired, and then again in
4353 // OnBeforeURLRequest and OnBeforeSendHeaders.
4354 for (size_t i = 0; i < 6; ++i) {
4355 log_position = ExpectLogContainsSomewhereAfter(
4356 entries,
4357 log_position + 1,
4358 NetLog::TYPE_URL_REQUEST_DELEGATE,
4359 NetLog::PHASE_BEGIN);
4360
4361 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4362 log_position + 1);
4363
4364 ASSERT_LT(log_position, entries.size());
4365 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4366 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
[email protected]1826a402014-01-08 15:40:484367
4368 if (i == 1) {
4369 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4370 entries, log_position + 1);
4371 }
[email protected]abe1c4a2013-10-25 19:28:514372 }
4373
4374 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4375 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4376}
4377
4378// Tests handling of delegate info from a URLRequest::Delegate.
4379TEST_F(URLRequestTestHTTP, URLRequestDelegateInfo) {
4380 ASSERT_TRUE(test_server_.Start());
4381
4382 AsyncLoggingUrlRequestDelegate request_delegate(
4383 AsyncLoggingUrlRequestDelegate::NO_CANCEL);
4384 TestURLRequestContext context(true);
4385 context.set_network_delegate(NULL);
4386 context.set_net_log(&net_log_);
4387 context.Init();
4388
4389 {
4390 // A chunked response with delays between chunks is used to make sure that
4391 // attempts by the URLRequest delegate to log information while reading the
4392 // body are ignored. Since they are ignored, this test is robust against
[email protected]1826a402014-01-08 15:40:484393 // the possibility of multiple reads being combined in the unlikely event
[email protected]abe1c4a2013-10-25 19:28:514394 // that it occurs.
[email protected]f7022f32014-08-21 16:32:194395 scoped_ptr<URLRequest> r(context.CreateRequest(
4396 test_server_.GetURL("chunked?waitBetweenChunks=20"), DEFAULT_PRIORITY,
4397 &request_delegate, NULL));
4398 LoadStateWithParam load_state = r->GetLoadState();
4399 r->Start();
[email protected]abe1c4a2013-10-25 19:28:514400 base::RunLoop().Run();
4401
[email protected]f7022f32014-08-21 16:32:194402 EXPECT_EQ(200, r->GetResponseCode());
4403 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
[email protected]abe1c4a2013-10-25 19:28:514404 }
4405
4406 CapturingNetLog::CapturedEntryList entries;
4407 net_log_.GetEntries(&entries);
4408
[email protected]1826a402014-01-08 15:40:484409 size_t log_position = 0;
4410
4411 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4412 entries, log_position);
4413
[email protected]abe1c4a2013-10-25 19:28:514414 // The delegate info should only have been logged on header complete. Other
4415 // times it should silently be ignored.
[email protected]1826a402014-01-08 15:40:484416 log_position =
4417 ExpectLogContainsSomewhereAfter(entries,
4418 log_position + 1,
4419 NetLog::TYPE_URL_REQUEST_DELEGATE,
4420 NetLog::PHASE_BEGIN);
[email protected]abe1c4a2013-10-25 19:28:514421
4422 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4423 log_position + 1);
4424
4425 ASSERT_LT(log_position, entries.size());
4426 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4427 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4428
4429 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4430 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4431 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4432 entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE));
4433}
4434
4435// Tests handling of delegate info from a URLRequest::Delegate in the case of
4436// an HTTP redirect.
4437TEST_F(URLRequestTestHTTP, URLRequestDelegateInfoOnRedirect) {
4438 ASSERT_TRUE(test_server_.Start());
4439
4440 AsyncLoggingUrlRequestDelegate request_delegate(
4441 AsyncLoggingUrlRequestDelegate::NO_CANCEL);
4442 TestURLRequestContext context(true);
4443 context.set_network_delegate(NULL);
4444 context.set_net_log(&net_log_);
4445 context.Init();
4446
4447 {
[email protected]f7022f32014-08-21 16:32:194448 scoped_ptr<URLRequest> r(context.CreateRequest(
4449 test_server_.GetURL("server-redirect?simple.html"), DEFAULT_PRIORITY,
4450 &request_delegate, NULL));
4451 LoadStateWithParam load_state = r->GetLoadState();
4452 r->Start();
[email protected]abe1c4a2013-10-25 19:28:514453 base::RunLoop().Run();
4454
[email protected]f7022f32014-08-21 16:32:194455 EXPECT_EQ(200, r->GetResponseCode());
4456 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
[email protected]abe1c4a2013-10-25 19:28:514457 }
4458
4459 CapturingNetLog::CapturedEntryList entries;
4460 net_log_.GetEntries(&entries);
4461
4462 // Delegate info should only have been logged in OnReceivedRedirect and
4463 // OnResponseStarted.
4464 size_t log_position = 0;
4465 for (int i = 0; i < 2; ++i) {
[email protected]1826a402014-01-08 15:40:484466 if (i == 0) {
4467 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4468 entries, log_position) + 1;
4469 }
4470
[email protected]abe1c4a2013-10-25 19:28:514471 log_position = ExpectLogContainsSomewhereAfter(
4472 entries,
4473 log_position,
4474 NetLog::TYPE_URL_REQUEST_DELEGATE,
4475 NetLog::PHASE_BEGIN);
4476
4477 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4478 log_position + 1);
4479
4480 ASSERT_LT(log_position, entries.size());
4481 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4482 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4483 }
4484
4485 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4486 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4487 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4488 entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE));
4489}
4490
4491// Tests handling of delegate info from a URLRequest::Delegate in the case of
4492// an HTTP redirect, with cancellation at various points.
4493TEST_F(URLRequestTestHTTP, URLRequestDelegateOnRedirectCancelled) {
4494 ASSERT_TRUE(test_server_.Start());
4495
4496 const AsyncLoggingUrlRequestDelegate::CancelStage kCancelStages[] = {
4497 AsyncLoggingUrlRequestDelegate::CANCEL_ON_RECEIVED_REDIRECT,
4498 AsyncLoggingUrlRequestDelegate::CANCEL_ON_RESPONSE_STARTED,
4499 AsyncLoggingUrlRequestDelegate::CANCEL_ON_READ_COMPLETED,
4500 };
4501
4502 for (size_t test_case = 0; test_case < arraysize(kCancelStages);
4503 ++test_case) {
4504 AsyncLoggingUrlRequestDelegate request_delegate(kCancelStages[test_case]);
4505 TestURLRequestContext context(true);
4506 CapturingNetLog net_log;
4507 context.set_network_delegate(NULL);
4508 context.set_net_log(&net_log);
4509 context.Init();
4510
4511 {
[email protected]f7022f32014-08-21 16:32:194512 scoped_ptr<URLRequest> r(context.CreateRequest(
4513 test_server_.GetURL("server-redirect?simple.html"), DEFAULT_PRIORITY,
4514 &request_delegate, NULL));
4515 LoadStateWithParam load_state = r->GetLoadState();
4516 r->Start();
[email protected]abe1c4a2013-10-25 19:28:514517 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:194518 EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status());
[email protected]abe1c4a2013-10-25 19:28:514519 }
4520
4521 CapturingNetLog::CapturedEntryList entries;
4522 net_log.GetEntries(&entries);
4523
4524 // Delegate info is always logged in both OnReceivedRedirect and
4525 // OnResponseStarted. In the CANCEL_ON_RECEIVED_REDIRECT, the
4526 // OnResponseStarted delegate call is after cancellation, but logging is
4527 // still currently supported in that call.
4528 size_t log_position = 0;
4529 for (int i = 0; i < 2; ++i) {
[email protected]1826a402014-01-08 15:40:484530 if (i == 0) {
4531 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4532 entries, log_position) + 1;
4533 }
4534
[email protected]abe1c4a2013-10-25 19:28:514535 log_position = ExpectLogContainsSomewhereAfter(
4536 entries,
4537 log_position,
4538 NetLog::TYPE_URL_REQUEST_DELEGATE,
4539 NetLog::PHASE_BEGIN);
4540
4541 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4542 log_position + 1);
4543
4544 ASSERT_LT(log_position, entries.size());
4545 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4546 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4547 }
4548
4549 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4550 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4551 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4552 entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE));
4553 }
4554}
4555
[email protected]847c0fa92012-11-06 16:37:424556namespace {
4557
4558const char kExtraHeader[] = "Allow-Snafu";
4559const char kExtraValue[] = "fubar";
4560
4561class RedirectWithAdditionalHeadersDelegate : public TestDelegate {
[email protected]cba24642014-08-15 20:49:594562 virtual void OnReceivedRedirect(URLRequest* request,
4563 const RedirectInfo& redirect_info,
[email protected]46fadfd2013-02-06 09:40:164564 bool* defer_redirect) OVERRIDE {
[email protected]cba24642014-08-15 20:49:594565 TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect);
[email protected]847c0fa92012-11-06 16:37:424566 request->SetExtraRequestHeaderByName(kExtraHeader, kExtraValue, false);
4567 }
4568};
4569
4570} // namespace
4571
4572TEST_F(URLRequestTestHTTP, RedirectWithAdditionalHeadersTest) {
4573 ASSERT_TRUE(test_server_.Start());
4574
4575 GURL destination_url = test_server_.GetURL(
4576 "echoheader?" + std::string(kExtraHeader));
4577 GURL original_url = test_server_.GetURL(
4578 "server-redirect?" + destination_url.spec());
4579 RedirectWithAdditionalHeadersDelegate d;
[email protected]f7022f32014-08-21 16:32:194580 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
4581 original_url, DEFAULT_PRIORITY, &d, NULL));
4582 req->Start();
[email protected]255620da2013-08-19 13:14:294583 base::RunLoop().Run();
[email protected]847c0fa92012-11-06 16:37:424584
4585 std::string value;
[email protected]f7022f32014-08-21 16:32:194586 const HttpRequestHeaders& headers = req->extra_request_headers();
[email protected]847c0fa92012-11-06 16:37:424587 EXPECT_TRUE(headers.GetHeader(kExtraHeader, &value));
4588 EXPECT_EQ(kExtraValue, value);
[email protected]f7022f32014-08-21 16:32:194589 EXPECT_FALSE(req->is_pending());
4590 EXPECT_FALSE(req->is_redirecting());
[email protected]847c0fa92012-11-06 16:37:424591 EXPECT_EQ(kExtraValue, d.data_received());
4592}
4593
[email protected]251a1b92012-11-13 11:01:094594namespace {
4595
4596const char kExtraHeaderToRemove[] = "To-Be-Removed";
4597
4598class RedirectWithHeaderRemovalDelegate : public TestDelegate {
[email protected]cba24642014-08-15 20:49:594599 virtual void OnReceivedRedirect(URLRequest* request,
4600 const RedirectInfo& redirect_info,
4601 bool* defer_redirect) OVERRIDE {
4602 TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect);
[email protected]251a1b92012-11-13 11:01:094603 request->RemoveRequestHeaderByName(kExtraHeaderToRemove);
4604 }
4605};
4606
4607} // namespace
4608
4609TEST_F(URLRequestTestHTTP, RedirectWithHeaderRemovalTest) {
4610 ASSERT_TRUE(test_server_.Start());
4611
4612 GURL destination_url = test_server_.GetURL(
4613 "echoheader?" + std::string(kExtraHeaderToRemove));
4614 GURL original_url = test_server_.GetURL(
4615 "server-redirect?" + destination_url.spec());
4616 RedirectWithHeaderRemovalDelegate d;
[email protected]f7022f32014-08-21 16:32:194617 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
4618 original_url, DEFAULT_PRIORITY, &d, NULL));
4619 req->SetExtraRequestHeaderByName(kExtraHeaderToRemove, "dummy", false);
4620 req->Start();
[email protected]255620da2013-08-19 13:14:294621 base::RunLoop().Run();
[email protected]251a1b92012-11-13 11:01:094622
4623 std::string value;
[email protected]f7022f32014-08-21 16:32:194624 const HttpRequestHeaders& headers = req->extra_request_headers();
[email protected]251a1b92012-11-13 11:01:094625 EXPECT_FALSE(headers.GetHeader(kExtraHeaderToRemove, &value));
[email protected]f7022f32014-08-21 16:32:194626 EXPECT_FALSE(req->is_pending());
4627 EXPECT_FALSE(req->is_redirecting());
[email protected]251a1b92012-11-13 11:01:094628 EXPECT_EQ("None", d.data_received());
4629}
4630
[email protected]316c1e5e2012-09-12 15:17:444631TEST_F(URLRequestTestHTTP, CancelTest) {
4632 TestDelegate d;
4633 {
[email protected]f7022f32014-08-21 16:32:194634 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
4635 GURL("http://www.google.com/"), DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:444636
[email protected]f7022f32014-08-21 16:32:194637 r->Start();
4638 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:444639
[email protected]f7022f32014-08-21 16:32:194640 r->Cancel();
[email protected]316c1e5e2012-09-12 15:17:444641
[email protected]255620da2013-08-19 13:14:294642 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:444643
4644 // We expect to receive OnResponseStarted even though the request has been
4645 // cancelled.
4646 EXPECT_EQ(1, d.response_started_count());
4647 EXPECT_EQ(0, d.bytes_received());
4648 EXPECT_FALSE(d.received_data_before_response());
4649 }
4650}
4651
4652TEST_F(URLRequestTestHTTP, CancelTest2) {
4653 ASSERT_TRUE(test_server_.Start());
4654
4655 TestDelegate d;
4656 {
[email protected]f7022f32014-08-21 16:32:194657 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
4658 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:444659
4660 d.set_cancel_in_response_started(true);
4661
[email protected]f7022f32014-08-21 16:32:194662 r->Start();
4663 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:444664
[email protected]255620da2013-08-19 13:14:294665 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:444666
4667 EXPECT_EQ(1, d.response_started_count());
4668 EXPECT_EQ(0, d.bytes_received());
4669 EXPECT_FALSE(d.received_data_before_response());
[email protected]f7022f32014-08-21 16:32:194670 EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status());
[email protected]316c1e5e2012-09-12 15:17:444671 }
4672}
4673
4674TEST_F(URLRequestTestHTTP, CancelTest3) {
4675 ASSERT_TRUE(test_server_.Start());
4676
4677 TestDelegate d;
4678 {
[email protected]f7022f32014-08-21 16:32:194679 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
4680 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:444681
4682 d.set_cancel_in_received_data(true);
4683
[email protected]f7022f32014-08-21 16:32:194684 r->Start();
4685 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:444686
[email protected]255620da2013-08-19 13:14:294687 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:444688
4689 EXPECT_EQ(1, d.response_started_count());
4690 // There is no guarantee about how much data was received
4691 // before the cancel was issued. It could have been 0 bytes,
4692 // or it could have been all the bytes.
4693 // EXPECT_EQ(0, d.bytes_received());
4694 EXPECT_FALSE(d.received_data_before_response());
[email protected]f7022f32014-08-21 16:32:194695 EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status());
[email protected]316c1e5e2012-09-12 15:17:444696 }
4697}
4698
4699TEST_F(URLRequestTestHTTP, CancelTest4) {
4700 ASSERT_TRUE(test_server_.Start());
4701
4702 TestDelegate d;
4703 {
[email protected]f7022f32014-08-21 16:32:194704 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
4705 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:444706
[email protected]f7022f32014-08-21 16:32:194707 r->Start();
4708 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:444709
4710 // The request will be implicitly canceled when it is destroyed. The
4711 // test delegate must not post a quit message when this happens because
4712 // this test doesn't actually have a message loop. The quit message would
4713 // get put on this thread's message queue and the next test would exit
4714 // early, causing problems.
4715 d.set_quit_on_complete(false);
4716 }
4717 // expect things to just cleanup properly.
4718
4719 // we won't actually get a received reponse here because we've never run the
4720 // message loop
4721 EXPECT_FALSE(d.received_data_before_response());
4722 EXPECT_EQ(0, d.bytes_received());
4723}
4724
4725TEST_F(URLRequestTestHTTP, CancelTest5) {
4726 ASSERT_TRUE(test_server_.Start());
4727
4728 // populate cache
4729 {
4730 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:194731 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
4732 test_server_.GetURL("cachetime"), DEFAULT_PRIORITY, &d, NULL));
4733 r->Start();
[email protected]255620da2013-08-19 13:14:294734 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:194735 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
[email protected]316c1e5e2012-09-12 15:17:444736 }
4737
4738 // cancel read from cache (see bug 990242)
4739 {
4740 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:194741 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
4742 test_server_.GetURL("cachetime"), DEFAULT_PRIORITY, &d, NULL));
4743 r->Start();
4744 r->Cancel();
[email protected]255620da2013-08-19 13:14:294745 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:444746
[email protected]f7022f32014-08-21 16:32:194747 EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status());
[email protected]316c1e5e2012-09-12 15:17:444748 EXPECT_EQ(1, d.response_started_count());
4749 EXPECT_EQ(0, d.bytes_received());
4750 EXPECT_FALSE(d.received_data_before_response());
4751 }
4752}
4753
4754TEST_F(URLRequestTestHTTP, PostTest) {
4755 ASSERT_TRUE(test_server_.Start());
4756 HTTPUploadDataOperationTest("POST");
4757}
4758
4759TEST_F(URLRequestTestHTTP, PutTest) {
4760 ASSERT_TRUE(test_server_.Start());
4761 HTTPUploadDataOperationTest("PUT");
4762}
4763
4764TEST_F(URLRequestTestHTTP, PostEmptyTest) {
4765 ASSERT_TRUE(test_server_.Start());
4766
4767 TestDelegate d;
4768 {
[email protected]f7022f32014-08-21 16:32:194769 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
4770 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, NULL));
4771 r->set_method("POST");
[email protected]316c1e5e2012-09-12 15:17:444772
[email protected]f7022f32014-08-21 16:32:194773 r->Start();
4774 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:444775
[email protected]255620da2013-08-19 13:14:294776 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:444777
[email protected]329b68b2012-11-14 17:54:274778 ASSERT_EQ(1, d.response_started_count())
[email protected]f7022f32014-08-21 16:32:194779 << "request failed: " << r->status().status()
4780 << ", error: " << r->status().error();
[email protected]316c1e5e2012-09-12 15:17:444781
4782 EXPECT_FALSE(d.received_data_before_response());
4783 EXPECT_TRUE(d.data_received().empty());
4784 }
4785}
4786
4787TEST_F(URLRequestTestHTTP, PostFileTest) {
4788 ASSERT_TRUE(test_server_.Start());
4789
4790 TestDelegate d;
4791 {
[email protected]f7022f32014-08-21 16:32:194792 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
4793 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, NULL));
4794 r->set_method("POST");
[email protected]316c1e5e2012-09-12 15:17:444795
[email protected]6cdfd7f2013-02-08 20:40:154796 base::FilePath dir;
[email protected]316c1e5e2012-09-12 15:17:444797 PathService::Get(base::DIR_EXE, &dir);
[email protected]37b3c1992014-03-11 20:59:024798 base::SetCurrentDirectory(dir);
[email protected]316c1e5e2012-09-12 15:17:444799
[email protected]f288ef02012-12-15 20:28:284800 ScopedVector<UploadElementReader> element_readers;
[email protected]316c1e5e2012-09-12 15:17:444801
[email protected]6cdfd7f2013-02-08 20:40:154802 base::FilePath path;
[email protected]316c1e5e2012-09-12 15:17:444803 PathService::Get(base::DIR_SOURCE_ROOT, &path);
4804 path = path.Append(FILE_PATH_LITERAL("net"));
4805 path = path.Append(FILE_PATH_LITERAL("data"));
4806 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
4807 path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
[email protected]cadac622013-06-11 16:46:364808 element_readers.push_back(
4809 new UploadFileElementReader(base::MessageLoopProxy::current().get(),
4810 path,
4811 0,
4812 kuint64max,
4813 base::Time()));
[email protected]f7022f32014-08-21 16:32:194814 r->set_upload(make_scoped_ptr(
[email protected]96c77a72013-09-24 09:49:204815 new UploadDataStream(element_readers.Pass(), 0)));
[email protected]316c1e5e2012-09-12 15:17:444816
[email protected]f7022f32014-08-21 16:32:194817 r->Start();
4818 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:444819
[email protected]255620da2013-08-19 13:14:294820 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:444821
[email protected]329b68b2012-11-14 17:54:274822 int64 size = 0;
[email protected]56285702013-12-04 18:22:494823 ASSERT_EQ(true, base::GetFileSize(path, &size));
[email protected]4356f0f2013-04-07 00:58:174824 scoped_ptr<char[]> buf(new char[size]);
[email protected]316c1e5e2012-09-12 15:17:444825
[email protected]7600d0b2013-12-08 21:43:304826 ASSERT_EQ(size, base::ReadFile(path, buf.get(), size));
[email protected]316c1e5e2012-09-12 15:17:444827
[email protected]329b68b2012-11-14 17:54:274828 ASSERT_EQ(1, d.response_started_count())
[email protected]f7022f32014-08-21 16:32:194829 << "request failed: " << r->status().status()
4830 << ", error: " << r->status().error();
[email protected]316c1e5e2012-09-12 15:17:444831
4832 EXPECT_FALSE(d.received_data_before_response());
4833
[email protected]329b68b2012-11-14 17:54:274834 EXPECT_EQ(size, d.bytes_received());
4835 EXPECT_EQ(std::string(&buf[0], size), d.data_received());
[email protected]316c1e5e2012-09-12 15:17:444836 }
4837}
4838
[email protected]999dd8c2013-11-12 06:45:544839TEST_F(URLRequestTestHTTP, PostUnreadableFileTest) {
4840 ASSERT_TRUE(test_server_.Start());
4841
4842 TestDelegate d;
4843 {
[email protected]f7022f32014-08-21 16:32:194844 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
4845 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, NULL));
4846 r->set_method("POST");
[email protected]999dd8c2013-11-12 06:45:544847
4848 ScopedVector<UploadElementReader> element_readers;
4849
4850 element_readers.push_back(new UploadFileElementReader(
4851 base::MessageLoopProxy::current().get(),
4852 base::FilePath(FILE_PATH_LITERAL(
4853 "c:\\path\\to\\non\\existant\\file.randomness.12345")),
4854 0,
4855 kuint64max,
4856 base::Time()));
[email protected]f7022f32014-08-21 16:32:194857 r->set_upload(make_scoped_ptr(
[email protected]999dd8c2013-11-12 06:45:544858 new UploadDataStream(element_readers.Pass(), 0)));
4859
[email protected]f7022f32014-08-21 16:32:194860 r->Start();
4861 EXPECT_TRUE(r->is_pending());
[email protected]999dd8c2013-11-12 06:45:544862
4863 base::RunLoop().Run();
4864
[email protected]999dd8c2013-11-12 06:45:544865 EXPECT_TRUE(d.request_failed());
4866 EXPECT_FALSE(d.received_data_before_response());
4867 EXPECT_EQ(0, d.bytes_received());
[email protected]f7022f32014-08-21 16:32:194868 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
4869 EXPECT_EQ(ERR_FILE_NOT_FOUND, r->status().error());
[email protected]999dd8c2013-11-12 06:45:544870 }
4871}
4872
[email protected]316c1e5e2012-09-12 15:17:444873TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) {
4874 ASSERT_TRUE(test_server_.Start());
4875
4876 TestDelegate d;
4877 {
[email protected]f7022f32014-08-21 16:32:194878 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
4879 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, NULL));
4880 r->EnableChunkedUpload();
4881 r->set_method("POST");
4882 AddChunksToUpload(r.get());
4883 r->Start();
4884 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:444885
[email protected]255620da2013-08-19 13:14:294886 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:444887
[email protected]f7022f32014-08-21 16:32:194888 VerifyReceivedDataMatchesChunks(r.get(), &d);
[email protected]316c1e5e2012-09-12 15:17:444889 }
4890}
4891
[email protected]329b68b2012-11-14 17:54:274892TEST_F(URLRequestTestHTTP, TestPostChunkedDataJustAfterStart) {
4893 ASSERT_TRUE(test_server_.Start());
4894
4895 TestDelegate d;
4896 {
[email protected]f7022f32014-08-21 16:32:194897 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
4898 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, NULL));
4899 r->EnableChunkedUpload();
4900 r->set_method("POST");
4901 r->Start();
4902 EXPECT_TRUE(r->is_pending());
4903 AddChunksToUpload(r.get());
[email protected]255620da2013-08-19 13:14:294904 base::RunLoop().Run();
[email protected]329b68b2012-11-14 17:54:274905
[email protected]f7022f32014-08-21 16:32:194906 VerifyReceivedDataMatchesChunks(r.get(), &d);
[email protected]329b68b2012-11-14 17:54:274907 }
4908}
4909
[email protected]316c1e5e2012-09-12 15:17:444910TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) {
4911 ASSERT_TRUE(test_server_.Start());
4912
4913 TestDelegate d;
4914 {
[email protected]f7022f32014-08-21 16:32:194915 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
4916 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, NULL));
4917 r->EnableChunkedUpload();
4918 r->set_method("POST");
4919 r->Start();
4920 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:444921
[email protected]255620da2013-08-19 13:14:294922 base::RunLoop().RunUntilIdle();
[email protected]f7022f32014-08-21 16:32:194923 AddChunksToUpload(r.get());
[email protected]255620da2013-08-19 13:14:294924 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:444925
[email protected]f7022f32014-08-21 16:32:194926 VerifyReceivedDataMatchesChunks(r.get(), &d);
[email protected]316c1e5e2012-09-12 15:17:444927 }
4928}
4929
4930TEST_F(URLRequestTestHTTP, ResponseHeadersTest) {
4931 ASSERT_TRUE(test_server_.Start());
4932
4933 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:194934 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
4935 test_server_.GetURL("files/with-headers.html"), DEFAULT_PRIORITY, &d,
4936 NULL));
4937 req->Start();
[email protected]255620da2013-08-19 13:14:294938 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:444939
[email protected]f7022f32014-08-21 16:32:194940 const HttpResponseHeaders* headers = req->response_headers();
[email protected]316c1e5e2012-09-12 15:17:444941
4942 // Simple sanity check that response_info() accesses the same data.
[email protected]f7022f32014-08-21 16:32:194943 EXPECT_EQ(headers, req->response_info().headers.get());
[email protected]316c1e5e2012-09-12 15:17:444944
4945 std::string header;
4946 EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header));
4947 EXPECT_EQ("private", header);
4948
4949 header.clear();
4950 EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header));
4951 EXPECT_EQ("text/html; charset=ISO-8859-1", header);
4952
4953 // The response has two "X-Multiple-Entries" headers.
4954 // This verfies our output has them concatenated together.
4955 header.clear();
4956 EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header));
4957 EXPECT_EQ("a, b", header);
4958}
4959
[email protected]242d8562012-10-30 21:20:464960TEST_F(URLRequestTestHTTP, ProcessSTS) {
[email protected]ce7d0cbc2013-05-03 18:57:224961 SpawnedTestServer::SSLOptions ssl_options;
4962 SpawnedTestServer https_test_server(
4963 SpawnedTestServer::TYPE_HTTPS,
[email protected]242d8562012-10-30 21:20:464964 ssl_options,
[email protected]6cdfd7f2013-02-08 20:40:154965 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
[email protected]242d8562012-10-30 21:20:464966 ASSERT_TRUE(https_test_server.Start());
4967
4968 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:194969 scoped_ptr<URLRequest> request(default_context_.CreateRequest(
4970 https_test_server.GetURL("files/hsts-headers.html"), DEFAULT_PRIORITY, &d,
4971 NULL));
4972 request->Start();
[email protected]255620da2013-08-19 13:14:294973 base::RunLoop().Run();
[email protected]242d8562012-10-30 21:20:464974
4975 TransportSecurityState* security_state =
4976 default_context_.transport_security_state();
[email protected]242d8562012-10-30 21:20:464977 TransportSecurityState::DomainState domain_state;
[email protected]9e6968d2014-05-07 21:46:264978 EXPECT_TRUE(security_state->GetDynamicDomainState(
4979 SpawnedTestServer::kLocalhost, &domain_state));
[email protected]242d8562012-10-30 21:20:464980 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
[email protected]9e6968d2014-05-07 21:46:264981 domain_state.sts.upgrade_mode);
4982 EXPECT_TRUE(domain_state.sts.include_subdomains);
4983 EXPECT_FALSE(domain_state.pkp.include_subdomains);
[email protected]37fd55fb2013-06-29 13:13:274984#if defined(OS_ANDROID)
4985 // Android's CertVerifyProc does not (yet) handle pins.
4986#else
4987 EXPECT_FALSE(domain_state.HasPublicKeyPins());
4988#endif
4989}
4990
4991// Android's CertVerifyProc does not (yet) handle pins. Therefore, it will
4992// reject HPKP headers, and a test setting only HPKP headers will fail (no
4993// DomainState present because header rejected).
4994#if defined(OS_ANDROID)
4995#define MAYBE_ProcessPKP DISABLED_ProcessPKP
4996#else
4997#define MAYBE_ProcessPKP ProcessPKP
4998#endif
4999
5000// Tests that enabling HPKP on a domain does not affect the HSTS
5001// validity/expiration.
5002TEST_F(URLRequestTestHTTP, MAYBE_ProcessPKP) {
5003 SpawnedTestServer::SSLOptions ssl_options;
5004 SpawnedTestServer https_test_server(
5005 SpawnedTestServer::TYPE_HTTPS,
5006 ssl_options,
5007 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
5008 ASSERT_TRUE(https_test_server.Start());
5009
5010 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195011 scoped_ptr<URLRequest> request(default_context_.CreateRequest(
5012 https_test_server.GetURL("files/hpkp-headers.html"), DEFAULT_PRIORITY, &d,
5013 NULL));
5014 request->Start();
[email protected]255620da2013-08-19 13:14:295015 base::RunLoop().Run();
[email protected]37fd55fb2013-06-29 13:13:275016
5017 TransportSecurityState* security_state =
5018 default_context_.transport_security_state();
[email protected]37fd55fb2013-06-29 13:13:275019 TransportSecurityState::DomainState domain_state;
[email protected]9e6968d2014-05-07 21:46:265020 EXPECT_TRUE(security_state->GetDynamicDomainState(
5021 SpawnedTestServer::kLocalhost, &domain_state));
[email protected]37fd55fb2013-06-29 13:13:275022 EXPECT_EQ(TransportSecurityState::DomainState::MODE_DEFAULT,
[email protected]9e6968d2014-05-07 21:46:265023 domain_state.sts.upgrade_mode);
5024 EXPECT_FALSE(domain_state.sts.include_subdomains);
5025 EXPECT_FALSE(domain_state.pkp.include_subdomains);
[email protected]37fd55fb2013-06-29 13:13:275026 EXPECT_TRUE(domain_state.HasPublicKeyPins());
[email protected]9e6968d2014-05-07 21:46:265027 EXPECT_NE(domain_state.sts.expiry, domain_state.pkp.expiry);
[email protected]242d8562012-10-30 21:20:465028}
5029
5030TEST_F(URLRequestTestHTTP, ProcessSTSOnce) {
[email protected]ce7d0cbc2013-05-03 18:57:225031 SpawnedTestServer::SSLOptions ssl_options;
5032 SpawnedTestServer https_test_server(
5033 SpawnedTestServer::TYPE_HTTPS,
[email protected]242d8562012-10-30 21:20:465034 ssl_options,
[email protected]6cdfd7f2013-02-08 20:40:155035 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
[email protected]242d8562012-10-30 21:20:465036 ASSERT_TRUE(https_test_server.Start());
5037
5038 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195039 scoped_ptr<URLRequest> request(default_context_.CreateRequest(
[email protected]242d8562012-10-30 21:20:465040 https_test_server.GetURL("files/hsts-multiple-headers.html"),
[email protected]f7022f32014-08-21 16:32:195041 DEFAULT_PRIORITY, &d, NULL));
5042 request->Start();
[email protected]255620da2013-08-19 13:14:295043 base::RunLoop().Run();
[email protected]242d8562012-10-30 21:20:465044
5045 // We should have set parameters from the first header, not the second.
5046 TransportSecurityState* security_state =
5047 default_context_.transport_security_state();
[email protected]242d8562012-10-30 21:20:465048 TransportSecurityState::DomainState domain_state;
[email protected]9e6968d2014-05-07 21:46:265049 EXPECT_TRUE(security_state->GetDynamicDomainState(
5050 SpawnedTestServer::kLocalhost, &domain_state));
[email protected]242d8562012-10-30 21:20:465051 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
[email protected]9e6968d2014-05-07 21:46:265052 domain_state.sts.upgrade_mode);
5053 EXPECT_FALSE(domain_state.sts.include_subdomains);
5054 EXPECT_FALSE(domain_state.pkp.include_subdomains);
[email protected]242d8562012-10-30 21:20:465055}
5056
[email protected]9f972ec2013-04-10 20:24:365057TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP) {
[email protected]ce7d0cbc2013-05-03 18:57:225058 SpawnedTestServer::SSLOptions ssl_options;
5059 SpawnedTestServer https_test_server(
5060 SpawnedTestServer::TYPE_HTTPS,
[email protected]9f972ec2013-04-10 20:24:365061 ssl_options,
5062 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
5063 ASSERT_TRUE(https_test_server.Start());
5064
5065 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195066 scoped_ptr<URLRequest> request(default_context_.CreateRequest(
[email protected]9f972ec2013-04-10 20:24:365067 https_test_server.GetURL("files/hsts-and-hpkp-headers.html"),
[email protected]f7022f32014-08-21 16:32:195068 DEFAULT_PRIORITY, &d, NULL));
5069 request->Start();
[email protected]255620da2013-08-19 13:14:295070 base::RunLoop().Run();
[email protected]9f972ec2013-04-10 20:24:365071
5072 // We should have set parameters from the first header, not the second.
5073 TransportSecurityState* security_state =
5074 default_context_.transport_security_state();
[email protected]9f972ec2013-04-10 20:24:365075 TransportSecurityState::DomainState domain_state;
[email protected]9e6968d2014-05-07 21:46:265076 EXPECT_TRUE(security_state->GetDynamicDomainState(
5077 SpawnedTestServer::kLocalhost, &domain_state));
[email protected]9f972ec2013-04-10 20:24:365078 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
[email protected]9e6968d2014-05-07 21:46:265079 domain_state.sts.upgrade_mode);
[email protected]9f972ec2013-04-10 20:24:365080#if defined(OS_ANDROID)
5081 // Android's CertVerifyProc does not (yet) handle pins.
5082#else
5083 EXPECT_TRUE(domain_state.HasPublicKeyPins());
5084#endif
[email protected]9e6968d2014-05-07 21:46:265085 EXPECT_NE(domain_state.sts.expiry, domain_state.pkp.expiry);
[email protected]9f972ec2013-04-10 20:24:365086
[email protected]a165f092013-06-12 16:10:055087 // Even though there is an HSTS header asserting includeSubdomains, it is
5088 // the *second* such header, and we MUST process only the first.
[email protected]9e6968d2014-05-07 21:46:265089 EXPECT_FALSE(domain_state.sts.include_subdomains);
[email protected]a165f092013-06-12 16:10:055090 // includeSubdomains does not occur in the test HPKP header.
[email protected]9e6968d2014-05-07 21:46:265091 EXPECT_FALSE(domain_state.pkp.include_subdomains);
[email protected]9f972ec2013-04-10 20:24:365092}
5093
[email protected]37fd55fb2013-06-29 13:13:275094// Tests that when multiple HPKP headers are present, asserting different
5095// policies, that only the first such policy is processed.
5096TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP2) {
5097 SpawnedTestServer::SSLOptions ssl_options;
5098 SpawnedTestServer https_test_server(
5099 SpawnedTestServer::TYPE_HTTPS,
5100 ssl_options,
5101 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
5102 ASSERT_TRUE(https_test_server.Start());
5103
5104 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195105 scoped_ptr<URLRequest> request(default_context_.CreateRequest(
[email protected]37fd55fb2013-06-29 13:13:275106 https_test_server.GetURL("files/hsts-and-hpkp-headers2.html"),
[email protected]f7022f32014-08-21 16:32:195107 DEFAULT_PRIORITY, &d, NULL));
5108 request->Start();
[email protected]255620da2013-08-19 13:14:295109 base::RunLoop().Run();
[email protected]37fd55fb2013-06-29 13:13:275110
5111 TransportSecurityState* security_state =
5112 default_context_.transport_security_state();
[email protected]37fd55fb2013-06-29 13:13:275113 TransportSecurityState::DomainState domain_state;
[email protected]9e6968d2014-05-07 21:46:265114 EXPECT_TRUE(security_state->GetDynamicDomainState(
5115 SpawnedTestServer::kLocalhost, &domain_state));
[email protected]37fd55fb2013-06-29 13:13:275116 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
[email protected]9e6968d2014-05-07 21:46:265117 domain_state.sts.upgrade_mode);
[email protected]37fd55fb2013-06-29 13:13:275118#if defined(OS_ANDROID)
5119 // Android's CertVerifyProc does not (yet) handle pins.
5120#else
5121 EXPECT_TRUE(domain_state.HasPublicKeyPins());
5122#endif
[email protected]9e6968d2014-05-07 21:46:265123 EXPECT_NE(domain_state.sts.expiry, domain_state.pkp.expiry);
[email protected]37fd55fb2013-06-29 13:13:275124
[email protected]9e6968d2014-05-07 21:46:265125 EXPECT_TRUE(domain_state.sts.include_subdomains);
5126 EXPECT_FALSE(domain_state.pkp.include_subdomains);
[email protected]37fd55fb2013-06-29 13:13:275127}
5128
[email protected]316c1e5e2012-09-12 15:17:445129TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) {
5130 ASSERT_TRUE(test_server_.Start());
5131
5132 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195133 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5134 test_server_.GetURL("files/content-type-normalization.html"),
5135 DEFAULT_PRIORITY, &d, NULL));
5136 req->Start();
[email protected]255620da2013-08-19 13:14:295137 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445138
5139 std::string mime_type;
[email protected]f7022f32014-08-21 16:32:195140 req->GetMimeType(&mime_type);
[email protected]316c1e5e2012-09-12 15:17:445141 EXPECT_EQ("text/html", mime_type);
5142
5143 std::string charset;
[email protected]f7022f32014-08-21 16:32:195144 req->GetCharset(&charset);
[email protected]316c1e5e2012-09-12 15:17:445145 EXPECT_EQ("utf-8", charset);
[email protected]f7022f32014-08-21 16:32:195146 req->Cancel();
[email protected]316c1e5e2012-09-12 15:17:445147}
5148
[email protected]02494ec2014-05-07 15:05:295149TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictDataRedirects) {
[email protected]e0f35c92013-05-08 16:04:345150 // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget().
[email protected]e0f35c92013-05-08 16:04:345151 GURL data_url("data:,foo");
[email protected]e0f35c92013-05-08 16:04:345152 DataProtocolHandler data_protocol_handler;
[email protected]588614c22013-08-16 00:09:025153 EXPECT_FALSE(data_protocol_handler.IsSafeRedirectTarget(data_url));
[email protected]e0f35c92013-05-08 16:04:345154
5155 // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget().
[email protected]588614c22013-08-16 00:09:025156 EXPECT_FALSE(job_factory_.IsSafeRedirectTarget(data_url));
[email protected]e0f35c92013-05-08 16:04:345157}
5158
[email protected]02494ec2014-05-07 15:05:295159#if !defined(DISABLE_FILE_SUPPORT)
5160TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictFileRedirects) {
5161 // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget().
5162 GURL file_url("file:///foo.txt");
5163 FileProtocolHandler file_protocol_handler(base::MessageLoopProxy::current());
5164 EXPECT_FALSE(file_protocol_handler.IsSafeRedirectTarget(file_url));
5165
5166 // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget().
5167 EXPECT_FALSE(job_factory_.IsSafeRedirectTarget(file_url));
5168}
5169
[email protected]588614c22013-08-16 00:09:025170TEST_F(URLRequestTestHTTP, RestrictFileRedirects) {
[email protected]316c1e5e2012-09-12 15:17:445171 ASSERT_TRUE(test_server_.Start());
5172
5173 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195174 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5175 test_server_.GetURL("files/redirect-to-file.html"), DEFAULT_PRIORITY, &d,
5176 NULL));
5177 req->Start();
[email protected]255620da2013-08-19 13:14:295178 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445179
[email protected]f7022f32014-08-21 16:32:195180 EXPECT_EQ(URLRequestStatus::FAILED, req->status().status());
5181 EXPECT_EQ(ERR_UNSAFE_REDIRECT, req->status().error());
[email protected]316c1e5e2012-09-12 15:17:445182}
[email protected]02494ec2014-05-07 15:05:295183#endif // !defined(DISABLE_FILE_SUPPORT)
[email protected]316c1e5e2012-09-12 15:17:445184
[email protected]588614c22013-08-16 00:09:025185TEST_F(URLRequestTestHTTP, RestrictDataRedirects) {
5186 ASSERT_TRUE(test_server_.Start());
5187
5188 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195189 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5190 test_server_.GetURL("files/redirect-to-data.html"), DEFAULT_PRIORITY, &d,
5191 NULL));
5192 req->Start();
[email protected]588614c22013-08-16 00:09:025193 base::MessageLoop::current()->Run();
5194
[email protected]f7022f32014-08-21 16:32:195195 EXPECT_EQ(URLRequestStatus::FAILED, req->status().status());
5196 EXPECT_EQ(ERR_UNSAFE_REDIRECT, req->status().error());
[email protected]588614c22013-08-16 00:09:025197}
5198
[email protected]316c1e5e2012-09-12 15:17:445199TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) {
5200 ASSERT_TRUE(test_server_.Start());
5201
5202 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195203 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5204 test_server_.GetURL("files/redirect-to-invalid-url.html"),
5205 DEFAULT_PRIORITY, &d, NULL));
5206 req->Start();
[email protected]255620da2013-08-19 13:14:295207 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445208
[email protected]f7022f32014-08-21 16:32:195209 EXPECT_EQ(URLRequestStatus::FAILED, req->status().status());
5210 EXPECT_EQ(ERR_INVALID_URL, req->status().error());
[email protected]316c1e5e2012-09-12 15:17:445211}
5212
[email protected]e50efea2014-03-24 18:41:005213// Make sure redirects are cached, despite not reading their bodies.
5214TEST_F(URLRequestTestHTTP, CacheRedirect) {
5215 ASSERT_TRUE(test_server_.Start());
5216 GURL redirect_url =
5217 test_server_.GetURL("files/redirect302-to-echo-cacheable");
5218
5219 {
5220 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195221 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5222 redirect_url, DEFAULT_PRIORITY, &d, NULL));
5223 req->Start();
[email protected]e50efea2014-03-24 18:41:005224 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:195225 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
[email protected]e50efea2014-03-24 18:41:005226 EXPECT_EQ(1, d.received_redirect_count());
[email protected]f7022f32014-08-21 16:32:195227 EXPECT_EQ(test_server_.GetURL("echo"), req->url());
[email protected]e50efea2014-03-24 18:41:005228 }
5229
5230 {
5231 TestDelegate d;
5232 d.set_quit_on_redirect(true);
[email protected]f7022f32014-08-21 16:32:195233 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5234 redirect_url, DEFAULT_PRIORITY, &d, NULL));
5235 req->Start();
[email protected]e50efea2014-03-24 18:41:005236 base::RunLoop().Run();
5237
5238 EXPECT_EQ(1, d.received_redirect_count());
5239 EXPECT_EQ(0, d.response_started_count());
[email protected]f7022f32014-08-21 16:32:195240 EXPECT_TRUE(req->was_cached());
[email protected]e50efea2014-03-24 18:41:005241
[email protected]f7022f32014-08-21 16:32:195242 req->FollowDeferredRedirect();
[email protected]e50efea2014-03-24 18:41:005243 base::RunLoop().Run();
5244 EXPECT_EQ(1, d.received_redirect_count());
5245 EXPECT_EQ(1, d.response_started_count());
[email protected]f7022f32014-08-21 16:32:195246 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
5247 EXPECT_EQ(test_server_.GetURL("echo"), req->url());
[email protected]e50efea2014-03-24 18:41:005248 }
5249}
5250
5251// Make sure a request isn't cached when a NetworkDelegate forces a redirect
5252// when the headers are read, since the body won't have been read.
5253TEST_F(URLRequestTestHTTP, NoCacheOnNetworkDelegateRedirect) {
5254 ASSERT_TRUE(test_server_.Start());
5255 // URL that is normally cached.
5256 GURL initial_url = test_server_.GetURL("cachetime");
5257
5258 {
5259 // Set up the TestNetworkDelegate tp force a redirect.
5260 GURL redirect_to_url = test_server_.GetURL("echo");
5261 default_network_delegate_.set_redirect_on_headers_received_url(
5262 redirect_to_url);
5263
5264 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195265 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5266 initial_url, DEFAULT_PRIORITY, &d, NULL));
5267 req->Start();
[email protected]e50efea2014-03-24 18:41:005268 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:195269 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
[email protected]e50efea2014-03-24 18:41:005270 EXPECT_EQ(1, d.received_redirect_count());
[email protected]f7022f32014-08-21 16:32:195271 EXPECT_EQ(redirect_to_url, req->url());
[email protected]e50efea2014-03-24 18:41:005272 }
5273
5274 {
5275 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195276 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5277 initial_url, DEFAULT_PRIORITY, &d, NULL));
5278 req->Start();
[email protected]e50efea2014-03-24 18:41:005279 base::RunLoop().Run();
5280
[email protected]f7022f32014-08-21 16:32:195281 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
5282 EXPECT_FALSE(req->was_cached());
[email protected]e50efea2014-03-24 18:41:005283 EXPECT_EQ(0, d.received_redirect_count());
[email protected]f7022f32014-08-21 16:32:195284 EXPECT_EQ(initial_url, req->url());
[email protected]e50efea2014-03-24 18:41:005285 }
5286}
5287
[email protected]5f714132014-03-26 10:41:165288// Tests that redirection to an unsafe URL is allowed when it has been marked as
5289// safe.
5290TEST_F(URLRequestTestHTTP, UnsafeRedirectToWhitelistedUnsafeURL) {
5291 ASSERT_TRUE(test_server_.Start());
5292
5293 GURL unsafe_url("data:text/html,this-is-considered-an-unsafe-url");
5294 default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url);
5295 default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url);
5296
5297 TestDelegate d;
5298 {
[email protected]f7022f32014-08-21 16:32:195299 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
5300 test_server_.GetURL("whatever"), DEFAULT_PRIORITY, &d, NULL));
[email protected]5f714132014-03-26 10:41:165301
[email protected]f7022f32014-08-21 16:32:195302 r->Start();
[email protected]5f714132014-03-26 10:41:165303 base::RunLoop().Run();
5304
[email protected]f7022f32014-08-21 16:32:195305 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
[email protected]5f714132014-03-26 10:41:165306
[email protected]f7022f32014-08-21 16:32:195307 EXPECT_EQ(2U, r->url_chain().size());
5308 EXPECT_EQ(OK, r->status().error());
5309 EXPECT_EQ(unsafe_url, r->url());
[email protected]5f714132014-03-26 10:41:165310 EXPECT_EQ("this-is-considered-an-unsafe-url", d.data_received());
5311 }
5312}
5313
5314// Tests that a redirect to a different unsafe URL is blocked, even after adding
5315// some other URL to the whitelist.
5316TEST_F(URLRequestTestHTTP, UnsafeRedirectToDifferentUnsafeURL) {
5317 ASSERT_TRUE(test_server_.Start());
5318
5319 GURL unsafe_url("data:text/html,something");
5320 GURL different_unsafe_url("data:text/html,something-else");
5321 default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url);
5322 default_network_delegate_.set_allowed_unsafe_redirect_url(
5323 different_unsafe_url);
5324
5325 TestDelegate d;
5326 {
[email protected]f7022f32014-08-21 16:32:195327 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
5328 test_server_.GetURL("whatever"), DEFAULT_PRIORITY, &d, NULL));
[email protected]5f714132014-03-26 10:41:165329
[email protected]f7022f32014-08-21 16:32:195330 r->Start();
[email protected]5f714132014-03-26 10:41:165331 base::RunLoop().Run();
5332
[email protected]f7022f32014-08-21 16:32:195333 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
5334 EXPECT_EQ(ERR_UNSAFE_REDIRECT, r->status().error());
[email protected]5f714132014-03-26 10:41:165335 }
5336}
5337
[email protected]5f714132014-03-26 10:41:165338// Redirects from an URL with fragment to an unsafe URL with fragment should
5339// be allowed, and the reference fragment of the target URL should be preserved.
5340TEST_F(URLRequestTestHTTP, UnsafeRedirectWithDifferentReferenceFragment) {
5341 ASSERT_TRUE(test_server_.Start());
5342
5343 GURL original_url(test_server_.GetURL("original#fragment1"));
5344 GURL unsafe_url("data:,url-marked-safe-and-used-in-redirect#fragment2");
5345 GURL expected_url("data:,url-marked-safe-and-used-in-redirect#fragment2");
5346
5347 default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url);
5348 default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url);
5349
5350 TestDelegate d;
5351 {
[email protected]f7022f32014-08-21 16:32:195352 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
5353 original_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]5f714132014-03-26 10:41:165354
[email protected]f7022f32014-08-21 16:32:195355 r->Start();
[email protected]5f714132014-03-26 10:41:165356 base::RunLoop().Run();
5357
[email protected]f7022f32014-08-21 16:32:195358 EXPECT_EQ(2U, r->url_chain().size());
5359 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
5360 EXPECT_EQ(OK, r->status().error());
5361 EXPECT_EQ(original_url, r->original_url());
5362 EXPECT_EQ(expected_url, r->url());
[email protected]5f714132014-03-26 10:41:165363 }
5364}
5365
5366// When a delegate has specified a safe redirect URL, but it does not match the
5367// redirect target, then do not prevent the reference fragment from being added.
[email protected]f878230e2014-04-03 15:36:145368TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragmentAndUnrelatedUnsafeUrl) {
[email protected]5f714132014-03-26 10:41:165369 ASSERT_TRUE(test_server_.Start());
5370
5371 GURL original_url(test_server_.GetURL("original#expected-fragment"));
5372 GURL unsafe_url("data:text/html,this-url-does-not-match-redirect-url");
5373 GURL redirect_url(test_server_.GetURL("target"));
5374 GURL expected_redirect_url(test_server_.GetURL("target#expected-fragment"));
5375
5376 default_network_delegate_.set_redirect_on_headers_received_url(redirect_url);
5377 default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url);
5378
5379 TestDelegate d;
5380 {
[email protected]f7022f32014-08-21 16:32:195381 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
5382 original_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]5f714132014-03-26 10:41:165383
[email protected]f7022f32014-08-21 16:32:195384 r->Start();
[email protected]5f714132014-03-26 10:41:165385 base::RunLoop().Run();
5386
[email protected]f7022f32014-08-21 16:32:195387 EXPECT_EQ(2U, r->url_chain().size());
5388 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
5389 EXPECT_EQ(OK, r->status().error());
5390 EXPECT_EQ(original_url, r->original_url());
5391 EXPECT_EQ(expected_redirect_url, r->url());
[email protected]5f714132014-03-26 10:41:165392 }
5393}
5394
[email protected]f878230e2014-04-03 15:36:145395// When a delegate has specified a safe redirect URL, assume that the redirect
5396// URL should not be changed. In particular, the reference fragment should not
5397// be modified.
5398TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragment) {
5399 ASSERT_TRUE(test_server_.Start());
5400
5401 GURL original_url(test_server_.GetURL("original#should-not-be-appended"));
5402 GURL redirect_url("data:text/html,expect-no-reference-fragment");
5403
5404 default_network_delegate_.set_redirect_on_headers_received_url(redirect_url);
5405 default_network_delegate_.set_allowed_unsafe_redirect_url(redirect_url);
5406
5407 TestDelegate d;
5408 {
[email protected]f7022f32014-08-21 16:32:195409 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
5410 original_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]f878230e2014-04-03 15:36:145411
[email protected]f7022f32014-08-21 16:32:195412 r->Start();
[email protected]f878230e2014-04-03 15:36:145413 base::RunLoop().Run();
5414
[email protected]f7022f32014-08-21 16:32:195415 EXPECT_EQ(2U, r->url_chain().size());
5416 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
5417 EXPECT_EQ(OK, r->status().error());
5418 EXPECT_EQ(original_url, r->original_url());
5419 EXPECT_EQ(redirect_url, r->url());
[email protected]f878230e2014-04-03 15:36:145420 }
5421}
5422
5423// When a URLRequestRedirectJob is created, the redirection must be followed and
5424// the reference fragment of the target URL must not be modified.
5425TEST_F(URLRequestTestHTTP, RedirectJobWithReferenceFragment) {
5426 ASSERT_TRUE(test_server_.Start());
5427
5428 GURL original_url(test_server_.GetURL("original#should-not-be-appended"));
5429 GURL redirect_url(test_server_.GetURL("echo"));
5430
5431 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195432 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
5433 original_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]f878230e2014-04-03 15:36:145434
5435 URLRequestRedirectJob* job = new URLRequestRedirectJob(
[email protected]f7022f32014-08-21 16:32:195436 r.get(), &default_network_delegate_, redirect_url,
[email protected]f878230e2014-04-03 15:36:145437 URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason");
5438 AddTestInterceptor()->set_main_intercept_job(job);
5439
[email protected]f7022f32014-08-21 16:32:195440 r->Start();
[email protected]f878230e2014-04-03 15:36:145441 base::RunLoop().Run();
5442
[email protected]f7022f32014-08-21 16:32:195443 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
5444 EXPECT_EQ(OK, r->status().error());
5445 EXPECT_EQ(original_url, r->original_url());
5446 EXPECT_EQ(redirect_url, r->url());
[email protected]f878230e2014-04-03 15:36:145447}
5448
[email protected]316c1e5e2012-09-12 15:17:445449TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) {
5450 ASSERT_TRUE(test_server_.Start());
5451
5452 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195453 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5454 test_server_.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d, NULL));
5455 req->SetReferrer("http://user:[email protected]/");
5456 req->Start();
[email protected]255620da2013-08-19 13:14:295457 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445458
5459 EXPECT_EQ(std::string("http://foo.com/"), d.data_received());
5460}
5461
[email protected]99ecf6e2013-04-10 22:46:135462TEST_F(URLRequestTestHTTP, NoFragmentInReferrer) {
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://foo.com/test#fragment");
5469 req->Start();
[email protected]255620da2013-08-19 13:14:295470 base::RunLoop().Run();
[email protected]99ecf6e2013-04-10 22:46:135471
5472 EXPECT_EQ(std::string("http://foo.com/test"), d.data_received());
5473}
5474
5475TEST_F(URLRequestTestHTTP, EmptyReferrerAfterValidReferrer) {
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->SetReferrer("");
5483 req->Start();
[email protected]255620da2013-08-19 13:14:295484 base::RunLoop().Run();
[email protected]99ecf6e2013-04-10 22:46:135485
5486 EXPECT_EQ(std::string("None"), d.data_received());
5487}
5488
[email protected]1826a402014-01-08 15:40:485489// Defer network start and then resume, checking that the request was a success
5490// and bytes were received.
5491TEST_F(URLRequestTestHTTP, DeferredBeforeNetworkStart) {
5492 ASSERT_TRUE(test_server_.Start());
5493
5494 TestDelegate d;
5495 {
5496 d.set_quit_on_network_start(true);
5497 GURL test_url(test_server_.GetURL("echo"));
[email protected]f7022f32014-08-21 16:32:195498 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5499 test_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]1826a402014-01-08 15:40:485500
[email protected]f7022f32014-08-21 16:32:195501 req->Start();
[email protected]1826a402014-01-08 15:40:485502 base::RunLoop().Run();
5503
5504 EXPECT_EQ(1, d.received_before_network_start_count());
5505 EXPECT_EQ(0, d.response_started_count());
5506
[email protected]f7022f32014-08-21 16:32:195507 req->ResumeNetworkStart();
[email protected]1826a402014-01-08 15:40:485508 base::RunLoop().Run();
5509
5510 EXPECT_EQ(1, d.response_started_count());
5511 EXPECT_NE(0, d.bytes_received());
[email protected]f7022f32014-08-21 16:32:195512 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
[email protected]1826a402014-01-08 15:40:485513 }
5514}
5515
5516// Check that OnBeforeNetworkStart is only called once even if there is a
5517// redirect.
5518TEST_F(URLRequestTestHTTP, BeforeNetworkStartCalledOnce) {
5519 ASSERT_TRUE(test_server_.Start());
5520
5521 TestDelegate d;
5522 {
5523 d.set_quit_on_redirect(true);
5524 d.set_quit_on_network_start(true);
[email protected]f7022f32014-08-21 16:32:195525 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5526 test_server_.GetURL("server-redirect?echo"), DEFAULT_PRIORITY, &d,
5527 NULL));
[email protected]1826a402014-01-08 15:40:485528
[email protected]f7022f32014-08-21 16:32:195529 req->Start();
[email protected]1826a402014-01-08 15:40:485530 base::RunLoop().Run();
5531
5532 EXPECT_EQ(1, d.received_before_network_start_count());
5533 EXPECT_EQ(0, d.response_started_count());
5534 EXPECT_EQ(0, d.received_redirect_count());
5535
[email protected]f7022f32014-08-21 16:32:195536 req->ResumeNetworkStart();
[email protected]1826a402014-01-08 15:40:485537 base::RunLoop().Run();
5538
5539 EXPECT_EQ(1, d.received_redirect_count());
[email protected]f7022f32014-08-21 16:32:195540 req->FollowDeferredRedirect();
[email protected]1826a402014-01-08 15:40:485541 base::RunLoop().Run();
5542
5543 // Check that the redirect's new network transaction does not get propagated
5544 // to a second OnBeforeNetworkStart() notification.
5545 EXPECT_EQ(1, d.received_before_network_start_count());
5546
5547 EXPECT_EQ(1, d.response_started_count());
5548 EXPECT_NE(0, d.bytes_received());
[email protected]f7022f32014-08-21 16:32:195549 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
[email protected]1826a402014-01-08 15:40:485550 }
5551}
5552
5553// Cancel the request after learning that the request would use the network.
5554TEST_F(URLRequestTestHTTP, CancelOnBeforeNetworkStart) {
5555 ASSERT_TRUE(test_server_.Start());
5556
5557 TestDelegate d;
5558 {
5559 d.set_quit_on_network_start(true);
5560 GURL test_url(test_server_.GetURL("echo"));
[email protected]f7022f32014-08-21 16:32:195561 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5562 test_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]1826a402014-01-08 15:40:485563
[email protected]f7022f32014-08-21 16:32:195564 req->Start();
[email protected]1826a402014-01-08 15:40:485565 base::RunLoop().Run();
5566
5567 EXPECT_EQ(1, d.received_before_network_start_count());
5568 EXPECT_EQ(0, d.response_started_count());
5569
[email protected]f7022f32014-08-21 16:32:195570 req->Cancel();
[email protected]1826a402014-01-08 15:40:485571 base::RunLoop().Run();
5572
5573 EXPECT_EQ(1, d.response_started_count());
5574 EXPECT_EQ(0, d.bytes_received());
[email protected]f7022f32014-08-21 16:32:195575 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status());
[email protected]1826a402014-01-08 15:40:485576 }
5577}
5578
[email protected]316c1e5e2012-09-12 15:17:445579TEST_F(URLRequestTestHTTP, CancelRedirect) {
5580 ASSERT_TRUE(test_server_.Start());
5581
5582 TestDelegate d;
5583 {
5584 d.set_cancel_in_received_redirect(true);
[email protected]f7022f32014-08-21 16:32:195585 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5586 test_server_.GetURL("files/redirect-test.html"), DEFAULT_PRIORITY, &d,
5587 NULL));
5588 req->Start();
[email protected]255620da2013-08-19 13:14:295589 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445590
5591 EXPECT_EQ(1, d.response_started_count());
5592 EXPECT_EQ(0, d.bytes_received());
5593 EXPECT_FALSE(d.received_data_before_response());
[email protected]f7022f32014-08-21 16:32:195594 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status());
[email protected]316c1e5e2012-09-12 15:17:445595 }
5596}
5597
5598TEST_F(URLRequestTestHTTP, DeferredRedirect) {
5599 ASSERT_TRUE(test_server_.Start());
5600
5601 TestDelegate d;
5602 {
5603 d.set_quit_on_redirect(true);
[email protected]79e1fd62013-06-20 06:50:045604 GURL test_url(test_server_.GetURL("files/redirect-test.html"));
[email protected]f7022f32014-08-21 16:32:195605 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5606 test_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]79e1fd62013-06-20 06:50:045607
[email protected]f7022f32014-08-21 16:32:195608 req->Start();
[email protected]255620da2013-08-19 13:14:295609 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445610
5611 EXPECT_EQ(1, d.received_redirect_count());
5612
[email protected]f7022f32014-08-21 16:32:195613 req->FollowDeferredRedirect();
[email protected]255620da2013-08-19 13:14:295614 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445615
5616 EXPECT_EQ(1, d.response_started_count());
5617 EXPECT_FALSE(d.received_data_before_response());
[email protected]f7022f32014-08-21 16:32:195618 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
[email protected]316c1e5e2012-09-12 15:17:445619
[email protected]6cdfd7f2013-02-08 20:40:155620 base::FilePath path;
[email protected]316c1e5e2012-09-12 15:17:445621 PathService::Get(base::DIR_SOURCE_ROOT, &path);
5622 path = path.Append(FILE_PATH_LITERAL("net"));
5623 path = path.Append(FILE_PATH_LITERAL("data"));
5624 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
5625 path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
5626
5627 std::string contents;
[email protected]82f84b92013-08-30 18:23:505628 EXPECT_TRUE(base::ReadFileToString(path, &contents));
[email protected]316c1e5e2012-09-12 15:17:445629 EXPECT_EQ(contents, d.data_received());
5630 }
5631}
5632
[email protected]79e1fd62013-06-20 06:50:045633TEST_F(URLRequestTestHTTP, DeferredRedirect_GetFullRequestHeaders) {
5634 ASSERT_TRUE(test_server_.Start());
5635
5636 TestDelegate d;
5637 {
5638 d.set_quit_on_redirect(true);
5639 GURL test_url(test_server_.GetURL("files/redirect-test.html"));
[email protected]f7022f32014-08-21 16:32:195640 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5641 test_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]79e1fd62013-06-20 06:50:045642
5643 EXPECT_FALSE(d.have_full_request_headers());
5644
[email protected]f7022f32014-08-21 16:32:195645 req->Start();
[email protected]255620da2013-08-19 13:14:295646 base::RunLoop().Run();
[email protected]79e1fd62013-06-20 06:50:045647
5648 EXPECT_EQ(1, d.received_redirect_count());
5649 EXPECT_TRUE(d.have_full_request_headers());
5650 CheckFullRequestHeaders(d.full_request_headers(), test_url);
5651 d.ClearFullRequestHeaders();
5652
[email protected]f7022f32014-08-21 16:32:195653 req->FollowDeferredRedirect();
[email protected]255620da2013-08-19 13:14:295654 base::RunLoop().Run();
[email protected]79e1fd62013-06-20 06:50:045655
5656 GURL target_url(test_server_.GetURL("files/with-headers.html"));
5657 EXPECT_EQ(1, d.response_started_count());
5658 EXPECT_TRUE(d.have_full_request_headers());
5659 CheckFullRequestHeaders(d.full_request_headers(), target_url);
5660 EXPECT_FALSE(d.received_data_before_response());
[email protected]f7022f32014-08-21 16:32:195661 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
[email protected]79e1fd62013-06-20 06:50:045662
5663 base::FilePath path;
5664 PathService::Get(base::DIR_SOURCE_ROOT, &path);
5665 path = path.Append(FILE_PATH_LITERAL("net"));
5666 path = path.Append(FILE_PATH_LITERAL("data"));
5667 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
5668 path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
5669
5670 std::string contents;
[email protected]82f84b92013-08-30 18:23:505671 EXPECT_TRUE(base::ReadFileToString(path, &contents));
[email protected]79e1fd62013-06-20 06:50:045672 EXPECT_EQ(contents, d.data_received());
5673 }
5674}
5675
[email protected]316c1e5e2012-09-12 15:17:445676TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) {
5677 ASSERT_TRUE(test_server_.Start());
5678
5679 TestDelegate d;
5680 {
5681 d.set_quit_on_redirect(true);
[email protected]f7022f32014-08-21 16:32:195682 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5683 test_server_.GetURL("files/redirect-test.html"), DEFAULT_PRIORITY, &d,
5684 NULL));
5685 req->Start();
[email protected]255620da2013-08-19 13:14:295686 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445687
5688 EXPECT_EQ(1, d.received_redirect_count());
5689
[email protected]f7022f32014-08-21 16:32:195690 req->Cancel();
[email protected]255620da2013-08-19 13:14:295691 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445692
5693 EXPECT_EQ(1, d.response_started_count());
5694 EXPECT_EQ(0, d.bytes_received());
5695 EXPECT_FALSE(d.received_data_before_response());
[email protected]f7022f32014-08-21 16:32:195696 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status());
[email protected]316c1e5e2012-09-12 15:17:445697 }
5698}
5699
5700TEST_F(URLRequestTestHTTP, VaryHeader) {
5701 ASSERT_TRUE(test_server_.Start());
5702
[email protected]3b23a222013-05-15 21:33:255703 // Populate the cache.
[email protected]316c1e5e2012-09-12 15:17:445704 {
5705 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195706 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5707 test_server_.GetURL("echoheadercache?foo"), DEFAULT_PRIORITY, &d,
5708 NULL));
[email protected]316c1e5e2012-09-12 15:17:445709 HttpRequestHeaders headers;
5710 headers.SetHeader("foo", "1");
[email protected]f7022f32014-08-21 16:32:195711 req->SetExtraRequestHeaders(headers);
5712 req->Start();
[email protected]255620da2013-08-19 13:14:295713 base::RunLoop().Run();
[email protected]3b23a222013-05-15 21:33:255714
5715 LoadTimingInfo load_timing_info;
[email protected]f7022f32014-08-21 16:32:195716 req->GetLoadTimingInfo(&load_timing_info);
[email protected]3b23a222013-05-15 21:33:255717 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
[email protected]316c1e5e2012-09-12 15:17:445718 }
5719
[email protected]3b23a222013-05-15 21:33:255720 // Expect a cache hit.
[email protected]316c1e5e2012-09-12 15:17:445721 {
5722 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195723 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5724 test_server_.GetURL("echoheadercache?foo"), DEFAULT_PRIORITY, &d,
5725 NULL));
[email protected]316c1e5e2012-09-12 15:17:445726 HttpRequestHeaders headers;
5727 headers.SetHeader("foo", "1");
[email protected]f7022f32014-08-21 16:32:195728 req->SetExtraRequestHeaders(headers);
5729 req->Start();
[email protected]255620da2013-08-19 13:14:295730 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445731
[email protected]f7022f32014-08-21 16:32:195732 EXPECT_TRUE(req->was_cached());
[email protected]3b23a222013-05-15 21:33:255733
5734 LoadTimingInfo load_timing_info;
[email protected]f7022f32014-08-21 16:32:195735 req->GetLoadTimingInfo(&load_timing_info);
[email protected]3b23a222013-05-15 21:33:255736 TestLoadTimingCacheHitNoNetwork(load_timing_info);
[email protected]316c1e5e2012-09-12 15:17:445737 }
5738
[email protected]3b23a222013-05-15 21:33:255739 // Expect a cache miss.
[email protected]316c1e5e2012-09-12 15:17:445740 {
5741 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195742 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5743 test_server_.GetURL("echoheadercache?foo"), DEFAULT_PRIORITY, &d,
5744 NULL));
[email protected]316c1e5e2012-09-12 15:17:445745 HttpRequestHeaders headers;
5746 headers.SetHeader("foo", "2");
[email protected]f7022f32014-08-21 16:32:195747 req->SetExtraRequestHeaders(headers);
5748 req->Start();
[email protected]255620da2013-08-19 13:14:295749 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445750
[email protected]f7022f32014-08-21 16:32:195751 EXPECT_FALSE(req->was_cached());
[email protected]3b23a222013-05-15 21:33:255752
5753 LoadTimingInfo load_timing_info;
[email protected]f7022f32014-08-21 16:32:195754 req->GetLoadTimingInfo(&load_timing_info);
[email protected]3b23a222013-05-15 21:33:255755 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
[email protected]316c1e5e2012-09-12 15:17:445756 }
5757}
5758
5759TEST_F(URLRequestTestHTTP, BasicAuth) {
5760 ASSERT_TRUE(test_server_.Start());
5761
5762 // populate the cache
5763 {
5764 TestDelegate d;
5765 d.set_credentials(AuthCredentials(kUser, kSecret));
5766
[email protected]f7022f32014-08-21 16:32:195767 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
5768 test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d, NULL));
5769 r->Start();
[email protected]316c1e5e2012-09-12 15:17:445770
[email protected]255620da2013-08-19 13:14:295771 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445772
5773 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
5774 }
5775
5776 // repeat request with end-to-end validation. since auth-basic results in a
5777 // cachable page, we expect this test to result in a 304. in which case, the
5778 // response should be fetched from the cache.
5779 {
5780 TestDelegate d;
5781 d.set_credentials(AuthCredentials(kUser, kSecret));
5782
[email protected]f7022f32014-08-21 16:32:195783 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
5784 test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d, NULL));
5785 r->SetLoadFlags(LOAD_VALIDATE_CACHE);
5786 r->Start();
[email protected]316c1e5e2012-09-12 15:17:445787
[email protected]255620da2013-08-19 13:14:295788 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445789
5790 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
5791
5792 // Should be the same cached document.
[email protected]f7022f32014-08-21 16:32:195793 EXPECT_TRUE(r->was_cached());
[email protected]316c1e5e2012-09-12 15:17:445794 }
5795}
5796
5797// Check that Set-Cookie headers in 401 responses are respected.
5798// http://crbug.com/6450
5799TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) {
5800 ASSERT_TRUE(test_server_.Start());
5801
5802 GURL url_requiring_auth =
5803 test_server_.GetURL("auth-basic?set-cookie-if-challenged");
5804
5805 // Request a page that will give a 401 containing a Set-Cookie header.
5806 // Verify that when the transaction is restarted, it includes the new cookie.
5807 {
[email protected]ceefd7fd2012-11-29 00:36:245808 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]316c1e5e2012-09-12 15:17:445809 TestURLRequestContext context(true);
5810 context.set_network_delegate(&network_delegate);
5811 context.Init();
5812
5813 TestDelegate d;
5814 d.set_credentials(AuthCredentials(kUser, kSecret));
5815
[email protected]f7022f32014-08-21 16:32:195816 scoped_ptr<URLRequest> r(context.CreateRequest(
5817 url_requiring_auth, DEFAULT_PRIORITY, &d, NULL));
5818 r->Start();
[email protected]316c1e5e2012-09-12 15:17:445819
[email protected]255620da2013-08-19 13:14:295820 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445821
5822 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
5823
5824 // Make sure we sent the cookie in the restarted transaction.
5825 EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true")
5826 != std::string::npos);
5827 }
5828
5829 // Same test as above, except this time the restart is initiated earlier
5830 // (without user intervention since identity is embedded in the URL).
5831 {
[email protected]ceefd7fd2012-11-29 00:36:245832 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]316c1e5e2012-09-12 15:17:445833 TestURLRequestContext context(true);
5834 context.set_network_delegate(&network_delegate);
5835 context.Init();
5836
5837 TestDelegate d;
5838
5839 GURL::Replacements replacements;
5840 std::string username("user2");
5841 std::string password("secret");
5842 replacements.SetUsernameStr(username);
5843 replacements.SetPasswordStr(password);
5844 GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements);
5845
[email protected]f7022f32014-08-21 16:32:195846 scoped_ptr<URLRequest> r(context.CreateRequest(
5847 url_with_identity, DEFAULT_PRIORITY, &d, NULL));
5848 r->Start();
[email protected]316c1e5e2012-09-12 15:17:445849
[email protected]255620da2013-08-19 13:14:295850 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445851
5852 EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos);
5853
5854 // Make sure we sent the cookie in the restarted transaction.
5855 EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true")
5856 != std::string::npos);
5857 }
5858}
5859
[email protected]58e32bb2013-01-21 18:23:255860// Tests that load timing works as expected with auth and the cache.
5861TEST_F(URLRequestTestHTTP, BasicAuthLoadTiming) {
5862 ASSERT_TRUE(test_server_.Start());
5863
5864 // populate the cache
5865 {
5866 TestDelegate d;
5867 d.set_credentials(AuthCredentials(kUser, kSecret));
5868
[email protected]f7022f32014-08-21 16:32:195869 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
5870 test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d, NULL));
5871 r->Start();
[email protected]58e32bb2013-01-21 18:23:255872
[email protected]255620da2013-08-19 13:14:295873 base::RunLoop().Run();
[email protected]58e32bb2013-01-21 18:23:255874
5875 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
5876
5877 LoadTimingInfo load_timing_info_before_auth;
5878 EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeAuth(
5879 &load_timing_info_before_auth));
5880 TestLoadTimingNotReused(load_timing_info_before_auth,
5881 CONNECT_TIMING_HAS_DNS_TIMES);
5882
5883 LoadTimingInfo load_timing_info;
[email protected]f7022f32014-08-21 16:32:195884 r->GetLoadTimingInfo(&load_timing_info);
[email protected]58e32bb2013-01-21 18:23:255885 // The test server does not support keep alive sockets, so the second
5886 // request with auth should use a new socket.
5887 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
5888 EXPECT_NE(load_timing_info_before_auth.socket_log_id,
5889 load_timing_info.socket_log_id);
5890 EXPECT_LE(load_timing_info_before_auth.receive_headers_end,
5891 load_timing_info.connect_timing.connect_start);
5892 }
5893
[email protected]3b23a222013-05-15 21:33:255894 // Repeat request with end-to-end validation. Since auth-basic results in a
5895 // cachable page, we expect this test to result in a 304. In which case, the
[email protected]58e32bb2013-01-21 18:23:255896 // response should be fetched from the cache.
5897 {
5898 TestDelegate d;
5899 d.set_credentials(AuthCredentials(kUser, kSecret));
5900
[email protected]f7022f32014-08-21 16:32:195901 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
5902 test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d, NULL));
5903 r->SetLoadFlags(LOAD_VALIDATE_CACHE);
5904 r->Start();
[email protected]58e32bb2013-01-21 18:23:255905
[email protected]255620da2013-08-19 13:14:295906 base::RunLoop().Run();
[email protected]58e32bb2013-01-21 18:23:255907
5908 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
5909
5910 // Should be the same cached document.
[email protected]f7022f32014-08-21 16:32:195911 EXPECT_TRUE(r->was_cached());
[email protected]58e32bb2013-01-21 18:23:255912
[email protected]3b23a222013-05-15 21:33:255913 // Since there was a request that went over the wire, the load timing
5914 // information should include connection times.
[email protected]58e32bb2013-01-21 18:23:255915 LoadTimingInfo load_timing_info;
[email protected]f7022f32014-08-21 16:32:195916 r->GetLoadTimingInfo(&load_timing_info);
[email protected]3b23a222013-05-15 21:33:255917 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
[email protected]58e32bb2013-01-21 18:23:255918 }
5919}
5920
[email protected]316c1e5e2012-09-12 15:17:445921// In this test, we do a POST which the server will 302 redirect.
5922// The subsequent transaction should use GET, and should not send the
5923// Content-Type header.
5924// http://code.google.com/p/chromium/issues/detail?id=843
5925TEST_F(URLRequestTestHTTP, Post302RedirectGet) {
5926 ASSERT_TRUE(test_server_.Start());
5927
5928 const char kData[] = "hello world";
5929
5930 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:195931 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5932 test_server_.GetURL("files/redirect-to-echoall"), DEFAULT_PRIORITY, &d,
5933 NULL));
5934 req->set_method("POST");
5935 req->set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
[email protected]316c1e5e2012-09-12 15:17:445936
5937 // Set headers (some of which are specific to the POST).
5938 HttpRequestHeaders headers;
5939 headers.AddHeadersFromString(
5940 "Content-Type: multipart/form-data; "
5941 "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n"
5942 "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,"
5943 "text/plain;q=0.8,image/png,*/*;q=0.5\r\n"
5944 "Accept-Language: en-US,en\r\n"
5945 "Accept-Charset: ISO-8859-1,*,utf-8\r\n"
5946 "Content-Length: 11\r\n"
5947 "Origin: http://localhost:1337/");
[email protected]f7022f32014-08-21 16:32:195948 req->SetExtraRequestHeaders(headers);
5949 req->Start();
[email protected]255620da2013-08-19 13:14:295950 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:445951
5952 std::string mime_type;
[email protected]f7022f32014-08-21 16:32:195953 req->GetMimeType(&mime_type);
[email protected]316c1e5e2012-09-12 15:17:445954 EXPECT_EQ("text/html", mime_type);
5955
5956 const std::string& data = d.data_received();
5957
5958 // Check that the post-specific headers were stripped:
5959 EXPECT_FALSE(ContainsString(data, "Content-Length:"));
5960 EXPECT_FALSE(ContainsString(data, "Content-Type:"));
5961 EXPECT_FALSE(ContainsString(data, "Origin:"));
5962
5963 // These extra request headers should not have been stripped.
5964 EXPECT_TRUE(ContainsString(data, "Accept:"));
5965 EXPECT_TRUE(ContainsString(data, "Accept-Language:"));
5966 EXPECT_TRUE(ContainsString(data, "Accept-Charset:"));
5967}
5968
5969// The following tests check that we handle mutating the request method for
5970// HTTP redirects as expected.
5971// See http://crbug.com/56373 and http://crbug.com/102130.
5972
5973TEST_F(URLRequestTestHTTP, Redirect301Tests) {
5974 ASSERT_TRUE(test_server_.Start());
5975
5976 const GURL url = test_server_.GetURL("files/redirect301-to-echo");
5977
5978 HTTPRedirectMethodTest(url, "POST", "GET", true);
5979 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
5980 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
5981}
5982
5983TEST_F(URLRequestTestHTTP, Redirect302Tests) {
5984 ASSERT_TRUE(test_server_.Start());
5985
5986 const GURL url = test_server_.GetURL("files/redirect302-to-echo");
5987
5988 HTTPRedirectMethodTest(url, "POST", "GET", true);
5989 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
5990 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
5991}
5992
5993TEST_F(URLRequestTestHTTP, Redirect303Tests) {
5994 ASSERT_TRUE(test_server_.Start());
5995
5996 const GURL url = test_server_.GetURL("files/redirect303-to-echo");
5997
5998 HTTPRedirectMethodTest(url, "POST", "GET", true);
5999 HTTPRedirectMethodTest(url, "PUT", "GET", true);
6000 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
6001}
6002
6003TEST_F(URLRequestTestHTTP, Redirect307Tests) {
6004 ASSERT_TRUE(test_server_.Start());
6005
6006 const GURL url = test_server_.GetURL("files/redirect307-to-echo");
6007
6008 HTTPRedirectMethodTest(url, "POST", "POST", true);
6009 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
6010 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
6011}
6012
[email protected]0a17aab32014-04-24 03:32:376013TEST_F(URLRequestTestHTTP, Redirect308Tests) {
6014 ASSERT_TRUE(test_server_.Start());
6015
6016 const GURL url = test_server_.GetURL("files/redirect308-to-echo");
6017
6018 HTTPRedirectMethodTest(url, "POST", "POST", true);
6019 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
6020 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
6021}
6022
6023// Make sure that 308 responses without bodies are not treated as redirects.
6024// Certain legacy apis that pre-date the response code expect this behavior
6025// (Like Google Drive).
6026TEST_F(URLRequestTestHTTP, NoRedirectOn308WithoutLocationHeader) {
6027 ASSERT_TRUE(test_server_.Start());
6028
6029 TestDelegate d;
6030 const GURL url = test_server_.GetURL("files/308-without-location-header");
6031
[email protected]f7022f32014-08-21 16:32:196032 scoped_ptr<URLRequest> request(default_context_.CreateRequest(
6033 url, DEFAULT_PRIORITY, &d, NULL));
[email protected]0a17aab32014-04-24 03:32:376034
[email protected]f7022f32014-08-21 16:32:196035 request->Start();
[email protected]0a17aab32014-04-24 03:32:376036 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:196037 EXPECT_EQ(URLRequestStatus::SUCCESS, request->status().status());
6038 EXPECT_EQ(OK, request->status().error());
[email protected]0a17aab32014-04-24 03:32:376039 EXPECT_EQ(0, d.received_redirect_count());
[email protected]f7022f32014-08-21 16:32:196040 EXPECT_EQ(308, request->response_headers()->response_code());
[email protected]0a17aab32014-04-24 03:32:376041 EXPECT_EQ("This is not a redirect.", d.data_received());
6042}
6043
[email protected]f878230e2014-04-03 15:36:146044TEST_F(URLRequestTestHTTP, Redirect302PreserveReferenceFragment) {
6045 ASSERT_TRUE(test_server_.Start());
6046
6047 GURL original_url(test_server_.GetURL("files/redirect302-to-echo#fragment"));
6048 GURL expected_url(test_server_.GetURL("echo#fragment"));
6049
6050 TestDelegate d;
6051 {
[email protected]f7022f32014-08-21 16:32:196052 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
6053 original_url, DEFAULT_PRIORITY, &d, NULL));
[email protected]f878230e2014-04-03 15:36:146054
[email protected]f7022f32014-08-21 16:32:196055 r->Start();
[email protected]f878230e2014-04-03 15:36:146056 base::RunLoop().Run();
6057
[email protected]f7022f32014-08-21 16:32:196058 EXPECT_EQ(2U, r->url_chain().size());
6059 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
6060 EXPECT_EQ(OK, r->status().error());
6061 EXPECT_EQ(original_url, r->original_url());
6062 EXPECT_EQ(expected_url, r->url());
[email protected]f878230e2014-04-03 15:36:146063 }
6064}
6065
[email protected]cba24642014-08-15 20:49:596066TEST_F(URLRequestTestHTTP, RedirectPreserveFirstPartyURL) {
6067 ASSERT_TRUE(test_server_.Start());
6068
6069 GURL url(test_server_.GetURL("files/redirect302-to-echo"));
6070 GURL first_party_url("http://example.com");
6071
6072 TestDelegate d;
6073 {
[email protected]f7022f32014-08-21 16:32:196074 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
6075 url, DEFAULT_PRIORITY, &d, NULL));
6076 r->set_first_party_for_cookies(first_party_url);
[email protected]cba24642014-08-15 20:49:596077
[email protected]f7022f32014-08-21 16:32:196078 r->Start();
[email protected]cba24642014-08-15 20:49:596079 base::RunLoop().Run();
6080
[email protected]f7022f32014-08-21 16:32:196081 EXPECT_EQ(2U, r->url_chain().size());
6082 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
6083 EXPECT_EQ(OK, r->status().error());
6084 EXPECT_EQ(first_party_url, r->first_party_for_cookies());
[email protected]cba24642014-08-15 20:49:596085 }
6086}
6087
6088TEST_F(URLRequestTestHTTP, RedirectUpdateFirstPartyURL) {
6089 ASSERT_TRUE(test_server_.Start());
6090
6091 GURL url(test_server_.GetURL("files/redirect302-to-echo"));
6092 GURL original_first_party_url("http://example.com");
6093 GURL expected_first_party_url(test_server_.GetURL("echo"));
6094
6095 TestDelegate d;
6096 {
[email protected]f7022f32014-08-21 16:32:196097 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
6098 url, DEFAULT_PRIORITY, &d, NULL));
6099 r->set_first_party_for_cookies(original_first_party_url);
6100 r->set_first_party_url_policy(
[email protected]cba24642014-08-15 20:49:596101 URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT);
6102
[email protected]f7022f32014-08-21 16:32:196103 r->Start();
[email protected]cba24642014-08-15 20:49:596104 base::RunLoop().Run();
6105
[email protected]f7022f32014-08-21 16:32:196106 EXPECT_EQ(2U, r->url_chain().size());
6107 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
6108 EXPECT_EQ(OK, r->status().error());
6109 EXPECT_EQ(expected_first_party_url, r->first_party_for_cookies());
[email protected]cba24642014-08-15 20:49:596110 }
6111}
6112
[email protected]316c1e5e2012-09-12 15:17:446113TEST_F(URLRequestTestHTTP, InterceptPost302RedirectGet) {
6114 ASSERT_TRUE(test_server_.Start());
6115
6116 const char kData[] = "hello world";
6117
6118 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196119 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
6120 test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d, NULL));
6121 req->set_method("POST");
6122 req->set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
[email protected]316c1e5e2012-09-12 15:17:446123 HttpRequestHeaders headers;
6124 headers.SetHeader(HttpRequestHeaders::kContentLength,
6125 base::UintToString(arraysize(kData) - 1));
[email protected]f7022f32014-08-21 16:32:196126 req->SetExtraRequestHeaders(headers);
[email protected]316c1e5e2012-09-12 15:17:446127
6128 URLRequestRedirectJob* job = new URLRequestRedirectJob(
[email protected]f7022f32014-08-21 16:32:196129 req.get(), &default_network_delegate_, test_server_.GetURL("echo"),
[email protected]7983c4a2014-03-12 01:47:096130 URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason");
[email protected]316c1e5e2012-09-12 15:17:446131 AddTestInterceptor()->set_main_intercept_job(job);
6132
[email protected]f7022f32014-08-21 16:32:196133 req->Start();
[email protected]255620da2013-08-19 13:14:296134 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:196135 EXPECT_EQ("GET", req->method());
[email protected]316c1e5e2012-09-12 15:17:446136}
6137
6138TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) {
6139 ASSERT_TRUE(test_server_.Start());
6140
6141 const char kData[] = "hello world";
6142
6143 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196144 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
6145 test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d, NULL));
6146 req->set_method("POST");
6147 req->set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
[email protected]316c1e5e2012-09-12 15:17:446148 HttpRequestHeaders headers;
6149 headers.SetHeader(HttpRequestHeaders::kContentLength,
6150 base::UintToString(arraysize(kData) - 1));
[email protected]f7022f32014-08-21 16:32:196151 req->SetExtraRequestHeaders(headers);
[email protected]316c1e5e2012-09-12 15:17:446152
6153 URLRequestRedirectJob* job = new URLRequestRedirectJob(
[email protected]f7022f32014-08-21 16:32:196154 req.get(), &default_network_delegate_, test_server_.GetURL("echo"),
[email protected]7983c4a2014-03-12 01:47:096155 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT,
6156 "Very Good Reason");
[email protected]316c1e5e2012-09-12 15:17:446157 AddTestInterceptor()->set_main_intercept_job(job);
6158
[email protected]f7022f32014-08-21 16:32:196159 req->Start();
[email protected]255620da2013-08-19 13:14:296160 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:196161 EXPECT_EQ("POST", req->method());
[email protected]316c1e5e2012-09-12 15:17:446162 EXPECT_EQ(kData, d.data_received());
6163}
6164
6165// Check that default A-L header is sent.
6166TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) {
6167 ASSERT_TRUE(test_server_.Start());
6168
[email protected]8790210c2013-12-02 05:29:536169 StaticHttpUserAgentSettings settings("en", std::string());
[email protected]ceefd7fd2012-11-29 00:36:246170 TestNetworkDelegate network_delegate; // Must outlive URLRequests.
[email protected]316c1e5e2012-09-12 15:17:446171 TestURLRequestContext context(true);
6172 context.set_network_delegate(&network_delegate);
[email protected]ee4c30d2012-11-07 15:08:436173 context.set_http_user_agent_settings(&settings);
[email protected]316c1e5e2012-09-12 15:17:446174 context.Init();
6175
6176 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196177 scoped_ptr<URLRequest> req(context.CreateRequest(
6178 test_server_.GetURL("echoheader?Accept-Language"), DEFAULT_PRIORITY, &d,
6179 NULL));
6180 req->Start();
[email protected]255620da2013-08-19 13:14:296181 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446182 EXPECT_EQ("en", d.data_received());
6183}
6184
6185// Check that an empty A-L header is not sent. http://crbug.com/77365.
6186TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) {
6187 ASSERT_TRUE(test_server_.Start());
6188
[email protected]8790210c2013-12-02 05:29:536189 std::string empty_string; // Avoid most vexing parse on line below.
6190 StaticHttpUserAgentSettings settings(empty_string, empty_string);
[email protected]ceefd7fd2012-11-29 00:36:246191 TestNetworkDelegate network_delegate; // Must outlive URLRequests.
[email protected]316c1e5e2012-09-12 15:17:446192 TestURLRequestContext context(true);
6193 context.set_network_delegate(&network_delegate);
6194 context.Init();
6195 // We override the language after initialization because empty entries
6196 // get overridden by Init().
[email protected]ee4c30d2012-11-07 15:08:436197 context.set_http_user_agent_settings(&settings);
[email protected]316c1e5e2012-09-12 15:17:446198
6199 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196200 scoped_ptr<URLRequest> req(context.CreateRequest(
6201 test_server_.GetURL("echoheader?Accept-Language"), DEFAULT_PRIORITY, &d,
6202 NULL));
6203 req->Start();
[email protected]255620da2013-08-19 13:14:296204 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446205 EXPECT_EQ("None", d.data_received());
6206}
6207
6208// Check that if request overrides the A-L header, the default is not appended.
6209// See http://crbug.com/20894
6210TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) {
6211 ASSERT_TRUE(test_server_.Start());
6212
6213 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196214 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
6215 test_server_.GetURL("echoheader?Accept-Language"), DEFAULT_PRIORITY, &d,
6216 NULL));
[email protected]316c1e5e2012-09-12 15:17:446217 HttpRequestHeaders headers;
6218 headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru");
[email protected]f7022f32014-08-21 16:32:196219 req->SetExtraRequestHeaders(headers);
6220 req->Start();
[email protected]255620da2013-08-19 13:14:296221 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446222 EXPECT_EQ(std::string("ru"), d.data_received());
6223}
6224
6225// Check that default A-E header is sent.
6226TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) {
6227 ASSERT_TRUE(test_server_.Start());
6228
6229 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196230 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
6231 test_server_.GetURL("echoheader?Accept-Encoding"), DEFAULT_PRIORITY, &d,
6232 NULL));
[email protected]316c1e5e2012-09-12 15:17:446233 HttpRequestHeaders headers;
[email protected]f7022f32014-08-21 16:32:196234 req->SetExtraRequestHeaders(headers);
6235 req->Start();
[email protected]255620da2013-08-19 13:14:296236 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446237 EXPECT_TRUE(ContainsString(d.data_received(), "gzip"));
6238}
6239
6240// Check that if request overrides the A-E header, the default is not appended.
6241// See http://crbug.com/47381
6242TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) {
6243 ASSERT_TRUE(test_server_.Start());
6244
6245 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196246 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
6247 test_server_.GetURL("echoheader?Accept-Encoding"), DEFAULT_PRIORITY, &d,
6248 NULL));
[email protected]316c1e5e2012-09-12 15:17:446249 HttpRequestHeaders headers;
6250 headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, "identity");
[email protected]f7022f32014-08-21 16:32:196251 req->SetExtraRequestHeaders(headers);
6252 req->Start();
[email protected]255620da2013-08-19 13:14:296253 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446254 EXPECT_FALSE(ContainsString(d.data_received(), "gzip"));
6255 EXPECT_TRUE(ContainsString(d.data_received(), "identity"));
6256}
6257
[email protected]84f05432013-03-15 01:00:126258// Check that setting the A-C header sends the proper header.
6259TEST_F(URLRequestTestHTTP, SetAcceptCharset) {
[email protected]316c1e5e2012-09-12 15:17:446260 ASSERT_TRUE(test_server_.Start());
6261
6262 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196263 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
6264 test_server_.GetURL("echoheader?Accept-Charset"), DEFAULT_PRIORITY, &d,
6265 NULL));
[email protected]316c1e5e2012-09-12 15:17:446266 HttpRequestHeaders headers;
6267 headers.SetHeader(HttpRequestHeaders::kAcceptCharset, "koi-8r");
[email protected]f7022f32014-08-21 16:32:196268 req->SetExtraRequestHeaders(headers);
6269 req->Start();
[email protected]255620da2013-08-19 13:14:296270 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446271 EXPECT_EQ(std::string("koi-8r"), d.data_received());
6272}
6273
6274// Check that default User-Agent header is sent.
6275TEST_F(URLRequestTestHTTP, DefaultUserAgent) {
6276 ASSERT_TRUE(test_server_.Start());
6277
6278 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196279 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
6280 test_server_.GetURL("echoheader?User-Agent"), DEFAULT_PRIORITY, &d,
6281 NULL));
6282 req->Start();
[email protected]255620da2013-08-19 13:14:296283 base::RunLoop().Run();
[email protected]f7022f32014-08-21 16:32:196284 EXPECT_EQ(default_context_.http_user_agent_settings()->GetUserAgent(),
[email protected]051a4b32014-01-09 04:02:376285 d.data_received());
[email protected]316c1e5e2012-09-12 15:17:446286}
6287
6288// Check that if request overrides the User-Agent header,
6289// the default is not appended.
6290TEST_F(URLRequestTestHTTP, OverrideUserAgent) {
6291 ASSERT_TRUE(test_server_.Start());
6292
6293 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196294 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
6295 test_server_.GetURL("echoheader?User-Agent"), DEFAULT_PRIORITY, &d,
6296 NULL));
[email protected]316c1e5e2012-09-12 15:17:446297 HttpRequestHeaders headers;
6298 headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)");
[email protected]f7022f32014-08-21 16:32:196299 req->SetExtraRequestHeaders(headers);
6300 req->Start();
[email protected]255620da2013-08-19 13:14:296301 base::RunLoop().Run();
[email protected]cd6f2522014-01-16 18:27:356302 EXPECT_EQ(std::string("Lynx (textmode)"), d.data_received());
[email protected]316c1e5e2012-09-12 15:17:446303}
6304
[email protected]ee4c30d2012-11-07 15:08:436305// Check that a NULL HttpUserAgentSettings causes the corresponding empty
6306// User-Agent header to be sent but does not send the Accept-Language and
6307// Accept-Charset headers.
6308TEST_F(URLRequestTestHTTP, EmptyHttpUserAgentSettings) {
6309 ASSERT_TRUE(test_server_.Start());
6310
[email protected]ceefd7fd2012-11-29 00:36:246311 TestNetworkDelegate network_delegate; // Must outlive URLRequests.
[email protected]ee4c30d2012-11-07 15:08:436312 TestURLRequestContext context(true);
6313 context.set_network_delegate(&network_delegate);
6314 context.Init();
6315 // We override the HttpUserAgentSettings after initialization because empty
6316 // entries get overridden by Init().
6317 context.set_http_user_agent_settings(NULL);
6318
6319 struct {
6320 const char* request;
6321 const char* expected_response;
6322 } tests[] = { { "echoheader?Accept-Language", "None" },
6323 { "echoheader?Accept-Charset", "None" },
6324 { "echoheader?User-Agent", "" } };
6325
6326 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); i++) {
6327 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196328 scoped_ptr<URLRequest> req(context.CreateRequest(
6329 test_server_.GetURL(tests[i].request), DEFAULT_PRIORITY, &d, NULL));
6330 req->Start();
[email protected]255620da2013-08-19 13:14:296331 base::RunLoop().Run();
[email protected]ee4c30d2012-11-07 15:08:436332 EXPECT_EQ(tests[i].expected_response, d.data_received())
6333 << " Request = \"" << tests[i].request << "\"";
6334 }
6335}
6336
[email protected]5033ab82013-03-22 20:17:466337// Make sure that URLRequest passes on its priority updates to
6338// newly-created jobs after the first one.
6339TEST_F(URLRequestTestHTTP, SetSubsequentJobPriority) {
6340 ASSERT_TRUE(test_server_.Start());
6341
6342 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196343 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
6344 test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d, NULL));
6345 EXPECT_EQ(DEFAULT_PRIORITY, req->priority());
[email protected]5033ab82013-03-22 20:17:466346
6347 scoped_refptr<URLRequestRedirectJob> redirect_job =
6348 new URLRequestRedirectJob(
[email protected]f7022f32014-08-21 16:32:196349 req.get(), &default_network_delegate_, test_server_.GetURL("echo"),
[email protected]7983c4a2014-03-12 01:47:096350 URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason");
[email protected]90499482013-06-01 00:39:506351 AddTestInterceptor()->set_main_intercept_job(redirect_job.get());
[email protected]5033ab82013-03-22 20:17:466352
[email protected]f7022f32014-08-21 16:32:196353 req->SetPriority(LOW);
6354 req->Start();
6355 EXPECT_TRUE(req->is_pending());
[email protected]5033ab82013-03-22 20:17:466356
6357 scoped_refptr<URLRequestTestJob> job =
[email protected]f7022f32014-08-21 16:32:196358 new URLRequestTestJob(req.get(), &default_network_delegate_);
[email protected]90499482013-06-01 00:39:506359 AddTestInterceptor()->set_main_intercept_job(job.get());
[email protected]5033ab82013-03-22 20:17:466360
6361 // Should trigger |job| to be started.
[email protected]255620da2013-08-19 13:14:296362 base::RunLoop().Run();
[email protected]5033ab82013-03-22 20:17:466363 EXPECT_EQ(LOW, job->priority());
6364}
6365
[email protected]80abdad2014-03-15 00:20:546366// Check that creating a network request while entering/exiting suspend mode
6367// fails as it should. This is the only case where an HttpTransactionFactory
6368// does not return an HttpTransaction.
6369TEST_F(URLRequestTestHTTP, NetworkSuspendTest) {
6370 // Create a new HttpNetworkLayer that thinks it's suspended.
6371 HttpNetworkSession::Params params;
6372 params.host_resolver = default_context_.host_resolver();
6373 params.cert_verifier = default_context_.cert_verifier();
6374 params.transport_security_state = default_context_.transport_security_state();
6375 params.proxy_service = default_context_.proxy_service();
6376 params.ssl_config_service = default_context_.ssl_config_service();
6377 params.http_auth_handler_factory =
6378 default_context_.http_auth_handler_factory();
6379 params.network_delegate = &default_network_delegate_;
6380 params.http_server_properties = default_context_.http_server_properties();
6381 scoped_ptr<HttpNetworkLayer> network_layer(
6382 new HttpNetworkLayer(new HttpNetworkSession(params)));
6383 network_layer->OnSuspend();
6384
6385 HttpCache http_cache(network_layer.release(), default_context_.net_log(),
6386 HttpCache::DefaultBackend::InMemory(0));
6387
6388 TestURLRequestContext context(true);
6389 context.set_http_transaction_factory(&http_cache);
6390 context.Init();
6391
6392 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196393 scoped_ptr<URLRequest> req(context.CreateRequest(
6394 GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d, NULL));
6395 req->Start();
[email protected]80abdad2014-03-15 00:20:546396 base::RunLoop().Run();
6397
6398 EXPECT_TRUE(d.request_failed());
[email protected]f7022f32014-08-21 16:32:196399 EXPECT_EQ(URLRequestStatus::FAILED, req->status().status());
6400 EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, req->status().error());
[email protected]80abdad2014-03-15 00:20:546401}
6402
6403// Check that creating a network request while entering/exiting suspend mode
6404// fails as it should in the case there is no cache. This is the only case
6405// where an HttpTransactionFactory does not return an HttpTransaction.
6406TEST_F(URLRequestTestHTTP, NetworkSuspendTestNoCache) {
6407 // Create a new HttpNetworkLayer that thinks it's suspended.
6408 HttpNetworkSession::Params params;
6409 params.host_resolver = default_context_.host_resolver();
6410 params.cert_verifier = default_context_.cert_verifier();
6411 params.transport_security_state = default_context_.transport_security_state();
6412 params.proxy_service = default_context_.proxy_service();
6413 params.ssl_config_service = default_context_.ssl_config_service();
6414 params.http_auth_handler_factory =
6415 default_context_.http_auth_handler_factory();
6416 params.network_delegate = &default_network_delegate_;
6417 params.http_server_properties = default_context_.http_server_properties();
6418 HttpNetworkLayer network_layer(new HttpNetworkSession(params));
6419 network_layer.OnSuspend();
6420
6421 TestURLRequestContext context(true);
6422 context.set_http_transaction_factory(&network_layer);
6423 context.Init();
6424
6425 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196426 scoped_ptr<URLRequest> req(context.CreateRequest(
6427 GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d, NULL));
6428 req->Start();
[email protected]80abdad2014-03-15 00:20:546429 base::RunLoop().Run();
6430
6431 EXPECT_TRUE(d.request_failed());
[email protected]f7022f32014-08-21 16:32:196432 EXPECT_EQ(URLRequestStatus::FAILED, req->status().status());
6433 EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, req->status().error());
[email protected]80abdad2014-03-15 00:20:546434}
6435
[email protected]73e0bba2009-02-19 22:57:096436class HTTPSRequestTest : public testing::Test {
[email protected]87a09a92011-07-14 15:50:506437 public:
[email protected]ef2bf422012-05-11 03:27:096438 HTTPSRequestTest() : default_context_(true) {
6439 default_context_.set_network_delegate(&default_network_delegate_);
6440 default_context_.Init();
[email protected]87a09a92011-07-14 15:50:506441 }
6442 virtual ~HTTPSRequestTest() {}
6443
6444 protected:
[email protected]ceefd7fd2012-11-29 00:36:246445 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest.
[email protected]ef2bf422012-05-11 03:27:096446 TestURLRequestContext default_context_;
[email protected]ea224582008-12-07 20:25:466447};
6448
[email protected]c044616e2013-02-20 02:01:266449TEST_F(HTTPSRequestTest, HTTPSGetTest) {
[email protected]ce7d0cbc2013-05-03 18:57:226450 SpawnedTestServer test_server(
6451 SpawnedTestServer::TYPE_HTTPS,
6452 SpawnedTestServer::kLocalhost,
6453 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]95409e12010-08-17 20:07:116454 ASSERT_TRUE(test_server.Start());
[email protected]ea224582008-12-07 20:25:466455
[email protected]ea224582008-12-07 20:25:466456 TestDelegate d;
6457 {
[email protected]f7022f32014-08-21 16:32:196458 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
6459 test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL));
6460 r->Start();
6461 EXPECT_TRUE(r->is_pending());
[email protected]ea224582008-12-07 20:25:466462
[email protected]255620da2013-08-19 13:14:296463 base::RunLoop().Run();
[email protected]ea224582008-12-07 20:25:466464
6465 EXPECT_EQ(1, d.response_started_count());
6466 EXPECT_FALSE(d.received_data_before_response());
6467 EXPECT_NE(0, d.bytes_received());
[email protected]f7022f32014-08-21 16:32:196468 CheckSSLInfo(r->ssl_info());
[email protected]6d81b482011-02-22 19:47:196469 EXPECT_EQ(test_server.host_port_pair().host(),
[email protected]f7022f32014-08-21 16:32:196470 r->GetSocketAddress().host());
[email protected]6d81b482011-02-22 19:47:196471 EXPECT_EQ(test_server.host_port_pair().port(),
[email protected]f7022f32014-08-21 16:32:196472 r->GetSocketAddress().port());
[email protected]ea224582008-12-07 20:25:466473 }
[email protected]ea224582008-12-07 20:25:466474}
6475
[email protected]5774ada2010-07-15 06:30:546476TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) {
[email protected]ce7d0cbc2013-05-03 18:57:226477 SpawnedTestServer::SSLOptions ssl_options(
6478 SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME);
6479 SpawnedTestServer test_server(
6480 SpawnedTestServer::TYPE_HTTPS,
6481 ssl_options,
6482 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]95409e12010-08-17 20:07:116483 ASSERT_TRUE(test_server.Start());
[email protected]bacff652009-03-31 17:50:336484
6485 bool err_allowed = true;
6486 for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) {
6487 TestDelegate d;
6488 {
6489 d.set_allow_certificate_errors(err_allowed);
[email protected]f7022f32014-08-21 16:32:196490 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
6491 test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL));
[email protected]bacff652009-03-31 17:50:336492
[email protected]f7022f32014-08-21 16:32:196493 r->Start();
6494 EXPECT_TRUE(r->is_pending());
[email protected]bacff652009-03-31 17:50:336495
[email protected]255620da2013-08-19 13:14:296496 base::RunLoop().Run();
[email protected]bacff652009-03-31 17:50:336497
6498 EXPECT_EQ(1, d.response_started_count());
6499 EXPECT_FALSE(d.received_data_before_response());
6500 EXPECT_TRUE(d.have_certificate_errors());
[email protected]96adadb2010-08-28 01:16:176501 if (err_allowed) {
[email protected]bacff652009-03-31 17:50:336502 EXPECT_NE(0, d.bytes_received());
[email protected]f7022f32014-08-21 16:32:196503 CheckSSLInfo(r->ssl_info());
[email protected]96adadb2010-08-28 01:16:176504 } else {
[email protected]bacff652009-03-31 17:50:336505 EXPECT_EQ(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:176506 }
[email protected]bacff652009-03-31 17:50:336507 }
6508 }
6509}
6510
[email protected]5774ada2010-07-15 06:30:546511TEST_F(HTTPSRequestTest, HTTPSExpiredTest) {
[email protected]ce7d0cbc2013-05-03 18:57:226512 SpawnedTestServer::SSLOptions ssl_options(
6513 SpawnedTestServer::SSLOptions::CERT_EXPIRED);
6514 SpawnedTestServer test_server(
6515 SpawnedTestServer::TYPE_HTTPS,
6516 ssl_options,
6517 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]95409e12010-08-17 20:07:116518 ASSERT_TRUE(test_server.Start());
[email protected]bacff652009-03-31 17:50:336519
6520 // Iterate from false to true, just so that we do the opposite of the
6521 // previous test in order to increase test coverage.
6522 bool err_allowed = false;
6523 for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) {
6524 TestDelegate d;
6525 {
6526 d.set_allow_certificate_errors(err_allowed);
[email protected]f7022f32014-08-21 16:32:196527 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
6528 test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL));
[email protected]bacff652009-03-31 17:50:336529
[email protected]f7022f32014-08-21 16:32:196530 r->Start();
6531 EXPECT_TRUE(r->is_pending());
[email protected]bacff652009-03-31 17:50:336532
[email protected]255620da2013-08-19 13:14:296533 base::RunLoop().Run();
[email protected]bacff652009-03-31 17:50:336534
6535 EXPECT_EQ(1, d.response_started_count());
6536 EXPECT_FALSE(d.received_data_before_response());
6537 EXPECT_TRUE(d.have_certificate_errors());
[email protected]96adadb2010-08-28 01:16:176538 if (err_allowed) {
[email protected]bacff652009-03-31 17:50:336539 EXPECT_NE(0, d.bytes_received());
[email protected]f7022f32014-08-21 16:32:196540 CheckSSLInfo(r->ssl_info());
[email protected]96adadb2010-08-28 01:16:176541 } else {
[email protected]bacff652009-03-31 17:50:336542 EXPECT_EQ(0, d.bytes_received());
[email protected]96adadb2010-08-28 01:16:176543 }
[email protected]bacff652009-03-31 17:50:336544 }
6545 }
6546}
[email protected]73e0bba2009-02-19 22:57:096547
[email protected]316c1e5e2012-09-12 15:17:446548// This tests that a load of www.google.com with a certificate error sets
6549// the |certificate_errors_are_fatal| flag correctly. This flag will cause
6550// the interstitial to be fatal.
6551TEST_F(HTTPSRequestTest, HTTPSPreloadedHSTSTest) {
[email protected]ce7d0cbc2013-05-03 18:57:226552 SpawnedTestServer::SSLOptions ssl_options(
6553 SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME);
6554 SpawnedTestServer test_server(
6555 SpawnedTestServer::TYPE_HTTPS,
6556 ssl_options,
6557 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:446558 ASSERT_TRUE(test_server.Start());
6559
6560 // We require that the URL be www.google.com in order to pick up the
6561 // preloaded HSTS entries in the TransportSecurityState. This means that we
6562 // have to use a MockHostResolver in order to direct www.google.com to the
[email protected]ceefd7fd2012-11-29 00:36:246563 // testserver. By default, MockHostResolver maps all hosts to 127.0.0.1.
[email protected]316c1e5e2012-09-12 15:17:446564
6565 MockHostResolver host_resolver;
[email protected]ceefd7fd2012-11-29 00:36:246566 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]316c1e5e2012-09-12 15:17:446567 TestURLRequestContext context(true);
6568 context.set_network_delegate(&network_delegate);
6569 context.set_host_resolver(&host_resolver);
6570 TransportSecurityState transport_security_state;
6571 context.set_transport_security_state(&transport_security_state);
6572 context.Init();
6573
6574 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196575 scoped_ptr<URLRequest> r(context.CreateRequest(
6576 GURL(base::StringPrintf("https://www.google.com:%d",
6577 test_server.host_port_pair().port())),
6578 DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:446579
[email protected]f7022f32014-08-21 16:32:196580 r->Start();
6581 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:446582
[email protected]255620da2013-08-19 13:14:296583 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446584
6585 EXPECT_EQ(1, d.response_started_count());
6586 EXPECT_FALSE(d.received_data_before_response());
6587 EXPECT_TRUE(d.have_certificate_errors());
6588 EXPECT_TRUE(d.certificate_errors_are_fatal());
6589}
6590
6591// This tests that cached HTTPS page loads do not cause any updates to the
6592// TransportSecurityState.
6593TEST_F(HTTPSRequestTest, HTTPSErrorsNoClobberTSSTest) {
6594 // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't
6595 // matter. It just has to be any error.
[email protected]ce7d0cbc2013-05-03 18:57:226596 SpawnedTestServer::SSLOptions ssl_options(
6597 SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME);
6598 SpawnedTestServer test_server(
6599 SpawnedTestServer::TYPE_HTTPS,
6600 ssl_options,
6601 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:446602 ASSERT_TRUE(test_server.Start());
6603
[email protected]9e6968d2014-05-07 21:46:266604 // We require that the URL be www.google.com in order to pick up the static
6605 // and dynamic STS and PKP entries in the TransportSecurityState. This means
6606 // that we have to use a MockHostResolver in order to direct www.google.com to
6607 // the testserver. By default, MockHostResolver maps all hosts to 127.0.0.1.
[email protected]316c1e5e2012-09-12 15:17:446608
6609 MockHostResolver host_resolver;
[email protected]ceefd7fd2012-11-29 00:36:246610 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
[email protected]316c1e5e2012-09-12 15:17:446611 TestURLRequestContext context(true);
6612 context.set_network_delegate(&network_delegate);
6613 context.set_host_resolver(&host_resolver);
6614 TransportSecurityState transport_security_state;
[email protected]9e6968d2014-05-07 21:46:266615
6616 TransportSecurityState::DomainState static_domain_state;
6617 EXPECT_TRUE(transport_security_state.GetStaticDomainState(
6618 "www.google.com", true, &static_domain_state));
[email protected]316c1e5e2012-09-12 15:17:446619 context.set_transport_security_state(&transport_security_state);
6620 context.Init();
6621
[email protected]9e6968d2014-05-07 21:46:266622 TransportSecurityState::DomainState dynamic_domain_state;
6623 EXPECT_FALSE(transport_security_state.GetDynamicDomainState(
6624 "www.google.com", &dynamic_domain_state));
6625
[email protected]316c1e5e2012-09-12 15:17:446626 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196627 scoped_ptr<URLRequest> r(context.CreateRequest(
6628 GURL(base::StringPrintf("https://www.google.com:%d",
6629 test_server.host_port_pair().port())),
6630 DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:446631
[email protected]f7022f32014-08-21 16:32:196632 r->Start();
6633 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:446634
[email protected]255620da2013-08-19 13:14:296635 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446636
6637 EXPECT_EQ(1, d.response_started_count());
6638 EXPECT_FALSE(d.received_data_before_response());
6639 EXPECT_TRUE(d.have_certificate_errors());
6640 EXPECT_TRUE(d.certificate_errors_are_fatal());
6641
[email protected]9e6968d2014-05-07 21:46:266642 // Get a fresh copy of the states, and check that they haven't changed.
6643 TransportSecurityState::DomainState new_static_domain_state;
6644 EXPECT_TRUE(transport_security_state.GetStaticDomainState(
6645 "www.google.com", true, &new_static_domain_state));
6646 TransportSecurityState::DomainState new_dynamic_domain_state;
6647 EXPECT_FALSE(transport_security_state.GetDynamicDomainState(
6648 "www.google.com", &new_dynamic_domain_state));
6649
6650 EXPECT_EQ(new_static_domain_state.sts.upgrade_mode,
6651 static_domain_state.sts.upgrade_mode);
6652 EXPECT_EQ(new_static_domain_state.sts.include_subdomains,
6653 static_domain_state.sts.include_subdomains);
6654 EXPECT_EQ(new_static_domain_state.pkp.include_subdomains,
6655 static_domain_state.pkp.include_subdomains);
6656 EXPECT_TRUE(FingerprintsEqual(new_static_domain_state.pkp.spki_hashes,
6657 static_domain_state.pkp.spki_hashes));
6658 EXPECT_TRUE(FingerprintsEqual(new_static_domain_state.pkp.bad_spki_hashes,
6659 static_domain_state.pkp.bad_spki_hashes));
[email protected]316c1e5e2012-09-12 15:17:446660}
6661
[email protected]8ccc69f2012-11-28 19:52:146662// Make sure HSTS preserves a POST request's method and body.
6663TEST_F(HTTPSRequestTest, HSTSPreservesPosts) {
6664 static const char kData[] = "hello world";
6665
[email protected]ce7d0cbc2013-05-03 18:57:226666 SpawnedTestServer::SSLOptions ssl_options(
6667 SpawnedTestServer::SSLOptions::CERT_OK);
6668 SpawnedTestServer test_server(
6669 SpawnedTestServer::TYPE_HTTPS,
6670 ssl_options,
6671 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]8ccc69f2012-11-28 19:52:146672 ASSERT_TRUE(test_server.Start());
6673
6674
6675 // Per spec, TransportSecurityState expects a domain name, rather than an IP
6676 // address, so a MockHostResolver is needed to redirect www.somewhere.com to
[email protected]ce7d0cbc2013-05-03 18:57:226677 // the SpawnedTestServer. By default, MockHostResolver maps all hosts
6678 // to 127.0.0.1.
[email protected]8ccc69f2012-11-28 19:52:146679 MockHostResolver host_resolver;
[email protected]8ccc69f2012-11-28 19:52:146680
6681 // Force https for www.somewhere.com.
6682 TransportSecurityState transport_security_state;
[email protected]474f079e2013-03-02 19:11:206683 base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000);
6684 bool include_subdomains = false;
6685 transport_security_state.AddHSTS("www.somewhere.com", expiry,
6686 include_subdomains);
[email protected]8ccc69f2012-11-28 19:52:146687
6688 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
6689
6690 TestURLRequestContext context(true);
6691 context.set_host_resolver(&host_resolver);
6692 context.set_transport_security_state(&transport_security_state);
6693 context.set_network_delegate(&network_delegate);
6694 context.Init();
6695
6696 TestDelegate d;
6697 // Navigating to https://www.somewhere.com instead of https://127.0.0.1 will
6698 // cause a certificate error. Ignore the error.
6699 d.set_allow_certificate_errors(true);
6700
[email protected]f7022f32014-08-21 16:32:196701 scoped_ptr<URLRequest> req(context.CreateRequest(
6702 GURL(base::StringPrintf("http://www.somewhere.com:%d/echo",
6703 test_server.host_port_pair().port())),
6704 DEFAULT_PRIORITY, &d, NULL));
6705 req->set_method("POST");
6706 req->set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
[email protected]8ccc69f2012-11-28 19:52:146707
[email protected]f7022f32014-08-21 16:32:196708 req->Start();
[email protected]255620da2013-08-19 13:14:296709 base::RunLoop().Run();
[email protected]8ccc69f2012-11-28 19:52:146710
[email protected]f7022f32014-08-21 16:32:196711 EXPECT_EQ("https", req->url().scheme());
6712 EXPECT_EQ("POST", req->method());
[email protected]8ccc69f2012-11-28 19:52:146713 EXPECT_EQ(kData, d.data_received());
[email protected]07d9bfd2013-06-27 14:16:406714
6715 LoadTimingInfo load_timing_info;
6716 network_delegate.GetLoadTimingInfoBeforeRedirect(&load_timing_info);
6717 // LoadTimingInfo of HSTS redirects is similar to that of network cache hits
6718 TestLoadTimingCacheHitNoNetwork(load_timing_info);
[email protected]8ccc69f2012-11-28 19:52:146719}
6720
[email protected]316c1e5e2012-09-12 15:17:446721namespace {
6722
6723class SSLClientAuthTestDelegate : public TestDelegate {
6724 public:
6725 SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) {
6726 }
6727 virtual void OnCertificateRequested(
6728 URLRequest* request,
[email protected]46fadfd2013-02-06 09:40:166729 SSLCertRequestInfo* cert_request_info) OVERRIDE {
[email protected]316c1e5e2012-09-12 15:17:446730 on_certificate_requested_count_++;
[email protected]2da659e2013-05-23 20:51:346731 base::MessageLoop::current()->Quit();
[email protected]316c1e5e2012-09-12 15:17:446732 }
6733 int on_certificate_requested_count() {
6734 return on_certificate_requested_count_;
6735 }
6736 private:
6737 int on_certificate_requested_count_;
6738};
6739
6740} // namespace
6741
6742// TODO(davidben): Test the rest of the code. Specifically,
6743// - Filtering which certificates to select.
6744// - Sending a certificate back.
6745// - Getting a certificate request in an SSL renegotiation sending the
6746// HTTP request.
6747TEST_F(HTTPSRequestTest, ClientAuthTest) {
[email protected]ce7d0cbc2013-05-03 18:57:226748 SpawnedTestServer::SSLOptions ssl_options;
[email protected]316c1e5e2012-09-12 15:17:446749 ssl_options.request_client_certificate = true;
[email protected]ce7d0cbc2013-05-03 18:57:226750 SpawnedTestServer test_server(
6751 SpawnedTestServer::TYPE_HTTPS,
6752 ssl_options,
6753 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:446754 ASSERT_TRUE(test_server.Start());
6755
6756 SSLClientAuthTestDelegate d;
6757 {
[email protected]f7022f32014-08-21 16:32:196758 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
6759 test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:446760
[email protected]f7022f32014-08-21 16:32:196761 r->Start();
6762 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:446763
[email protected]255620da2013-08-19 13:14:296764 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446765
6766 EXPECT_EQ(1, d.on_certificate_requested_count());
6767 EXPECT_FALSE(d.received_data_before_response());
6768 EXPECT_EQ(0, d.bytes_received());
6769
6770 // Send no certificate.
6771 // TODO(davidben): Get temporary client cert import (with keys) working on
6772 // all platforms so we can test sending a cert as well.
[email protected]f7022f32014-08-21 16:32:196773 r->ContinueWithCertificate(NULL);
[email protected]316c1e5e2012-09-12 15:17:446774
[email protected]255620da2013-08-19 13:14:296775 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446776
6777 EXPECT_EQ(1, d.response_started_count());
6778 EXPECT_FALSE(d.received_data_before_response());
6779 EXPECT_NE(0, d.bytes_received());
6780 }
6781}
6782
6783TEST_F(HTTPSRequestTest, ResumeTest) {
6784 // Test that we attempt a session resume when making two connections to the
6785 // same host.
[email protected]ce7d0cbc2013-05-03 18:57:226786 SpawnedTestServer::SSLOptions ssl_options;
[email protected]316c1e5e2012-09-12 15:17:446787 ssl_options.record_resume = true;
[email protected]ce7d0cbc2013-05-03 18:57:226788 SpawnedTestServer test_server(
6789 SpawnedTestServer::TYPE_HTTPS,
6790 ssl_options,
6791 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:446792 ASSERT_TRUE(test_server.Start());
6793
6794 SSLClientSocket::ClearSessionCache();
6795
6796 {
6797 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196798 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
6799 test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:446800
[email protected]f7022f32014-08-21 16:32:196801 r->Start();
6802 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:446803
[email protected]255620da2013-08-19 13:14:296804 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446805
6806 EXPECT_EQ(1, d.response_started_count());
6807 }
6808
6809 reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())->
6810 CloseAllConnections();
6811
6812 {
6813 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196814 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
6815 test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:446816
[email protected]f7022f32014-08-21 16:32:196817 r->Start();
6818 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:446819
[email protected]255620da2013-08-19 13:14:296820 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446821
6822 // The response will look like;
6823 // insert abc
6824 // lookup abc
6825 // insert xyz
6826 //
6827 // With a newline at the end which makes the split think that there are
6828 // four lines.
6829
6830 EXPECT_EQ(1, d.response_started_count());
6831 std::vector<std::string> lines;
6832 base::SplitString(d.data_received(), '\n', &lines);
6833 ASSERT_EQ(4u, lines.size()) << d.data_received();
6834
6835 std::string session_id;
6836
6837 for (size_t i = 0; i < 2; i++) {
6838 std::vector<std::string> parts;
6839 base::SplitString(lines[i], '\t', &parts);
6840 ASSERT_EQ(2u, parts.size());
6841 if (i == 0) {
6842 EXPECT_EQ("insert", parts[0]);
6843 session_id = parts[1];
6844 } else {
6845 EXPECT_EQ("lookup", parts[0]);
6846 EXPECT_EQ(session_id, parts[1]);
6847 }
6848 }
6849 }
6850}
6851
6852TEST_F(HTTPSRequestTest, SSLSessionCacheShardTest) {
6853 // Test that sessions aren't resumed when the value of ssl_session_cache_shard
6854 // differs.
[email protected]ce7d0cbc2013-05-03 18:57:226855 SpawnedTestServer::SSLOptions ssl_options;
[email protected]316c1e5e2012-09-12 15:17:446856 ssl_options.record_resume = true;
[email protected]ce7d0cbc2013-05-03 18:57:226857 SpawnedTestServer test_server(
6858 SpawnedTestServer::TYPE_HTTPS,
6859 ssl_options,
6860 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]316c1e5e2012-09-12 15:17:446861 ASSERT_TRUE(test_server.Start());
6862
6863 SSLClientSocket::ClearSessionCache();
6864
6865 {
6866 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196867 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
6868 test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:446869
[email protected]f7022f32014-08-21 16:32:196870 r->Start();
6871 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:446872
[email protected]255620da2013-08-19 13:14:296873 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446874
6875 EXPECT_EQ(1, d.response_started_count());
6876 }
6877
6878 // Now create a new HttpCache with a different ssl_session_cache_shard value.
6879 HttpNetworkSession::Params params;
6880 params.host_resolver = default_context_.host_resolver();
6881 params.cert_verifier = default_context_.cert_verifier();
[email protected]b1c988b2013-06-13 06:48:116882 params.transport_security_state = default_context_.transport_security_state();
[email protected]316c1e5e2012-09-12 15:17:446883 params.proxy_service = default_context_.proxy_service();
6884 params.ssl_config_service = default_context_.ssl_config_service();
6885 params.http_auth_handler_factory =
6886 default_context_.http_auth_handler_factory();
[email protected]cc05edc2013-03-08 18:04:416887 params.network_delegate = &default_network_delegate_;
[email protected]316c1e5e2012-09-12 15:17:446888 params.http_server_properties = default_context_.http_server_properties();
6889 params.ssl_session_cache_shard = "alternate";
6890
[email protected]cba24642014-08-15 20:49:596891 scoped_ptr<HttpCache> cache(new HttpCache(
6892 new HttpNetworkSession(params),
6893 HttpCache::DefaultBackend::InMemory(0)));
[email protected]316c1e5e2012-09-12 15:17:446894
6895 default_context_.set_http_transaction_factory(cache.get());
6896
6897 {
6898 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:196899 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
6900 test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, NULL));
[email protected]316c1e5e2012-09-12 15:17:446901
[email protected]f7022f32014-08-21 16:32:196902 r->Start();
6903 EXPECT_TRUE(r->is_pending());
[email protected]316c1e5e2012-09-12 15:17:446904
[email protected]255620da2013-08-19 13:14:296905 base::RunLoop().Run();
[email protected]316c1e5e2012-09-12 15:17:446906
6907 // The response will look like;
6908 // insert abc
6909 // insert xyz
6910 //
6911 // With a newline at the end which makes the split think that there are
6912 // three lines.
6913
6914 EXPECT_EQ(1, d.response_started_count());
6915 std::vector<std::string> lines;
6916 base::SplitString(d.data_received(), '\n', &lines);
6917 ASSERT_EQ(3u, lines.size());
6918
6919 std::string session_id;
6920 for (size_t i = 0; i < 2; i++) {
6921 std::vector<std::string> parts;
6922 base::SplitString(lines[i], '\t', &parts);
6923 ASSERT_EQ(2u, parts.size());
6924 EXPECT_EQ("insert", parts[0]);
6925 if (i == 0) {
6926 session_id = parts[1];
6927 } else {
6928 EXPECT_NE(session_id, parts[1]);
6929 }
6930 }
6931 }
6932}
6933
[email protected]48d2b7c52014-06-27 01:16:556934class HTTPSFallbackTest : public testing::Test {
6935 public:
6936 HTTPSFallbackTest() : context_(true) {
6937 context_.Init();
6938 delegate_.set_allow_certificate_errors(true);
6939 }
6940 virtual ~HTTPSFallbackTest() {}
6941
6942 protected:
6943 void DoFallbackTest(const SpawnedTestServer::SSLOptions& ssl_options) {
6944 DCHECK(!request_);
6945 SpawnedTestServer test_server(
6946 SpawnedTestServer::TYPE_HTTPS,
6947 ssl_options,
6948 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6949 ASSERT_TRUE(test_server.Start());
6950
[email protected]f7022f32014-08-21 16:32:196951 request_ = context_.CreateRequest(
6952 test_server.GetURL(std::string()), DEFAULT_PRIORITY, &delegate_, NULL);
[email protected]48d2b7c52014-06-27 01:16:556953 request_->Start();
6954
6955 base::RunLoop().Run();
6956 }
6957
6958 void ExpectConnection(int version) {
6959 EXPECT_EQ(1, delegate_.response_started_count());
6960 EXPECT_NE(0, delegate_.bytes_received());
6961 EXPECT_EQ(version, SSLConnectionStatusToVersion(
6962 request_->ssl_info().connection_status));
6963 EXPECT_TRUE(request_->ssl_info().connection_status &
6964 SSL_CONNECTION_VERSION_FALLBACK);
6965 }
6966
6967 void ExpectFailure(int error) {
6968 EXPECT_EQ(1, delegate_.response_started_count());
6969 EXPECT_FALSE(request_->status().is_success());
6970 EXPECT_EQ(URLRequestStatus::FAILED, request_->status().status());
6971 EXPECT_EQ(error, request_->status().error());
6972 }
6973
6974 private:
6975 TestDelegate delegate_;
6976 TestURLRequestContext context_;
6977 scoped_ptr<URLRequest> request_;
6978};
6979
6980// Tests TLSv1.1 -> TLSv1 fallback. Verifies that we don't fall back more
6981// than necessary.
6982TEST_F(HTTPSFallbackTest, TLSv1Fallback) {
6983 SpawnedTestServer::SSLOptions ssl_options(
6984 SpawnedTestServer::SSLOptions::CERT_OK);
6985 ssl_options.tls_intolerant =
6986 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1;
6987
6988 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
6989 ExpectConnection(SSL_CONNECTION_VERSION_TLS1);
6990}
6991
6992// This test is disabled on Android because the remote test server doesn't cause
6993// a TCP reset.
6994#if !defined(OS_ANDROID)
6995// Tests fallback to TLS 1.0 on connection reset.
6996TEST_F(HTTPSFallbackTest, TLSv1FallbackReset) {
6997 SpawnedTestServer::SSLOptions ssl_options(
6998 SpawnedTestServer::SSLOptions::CERT_OK);
6999 ssl_options.tls_intolerant =
7000 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1;
7001 ssl_options.tls_intolerance_type =
7002 SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_RESET;
7003
7004 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
7005 ExpectConnection(SSL_CONNECTION_VERSION_TLS1);
7006}
7007#endif // !OS_ANDROID
7008
[email protected]12833302014-07-02 01:57:317009// Tests that we don't fallback on handshake failure with servers that implement
7010// TLS_FALLBACK_SCSV. Also ensure that the original error code is reported.
[email protected]48d2b7c52014-06-27 01:16:557011TEST_F(HTTPSFallbackTest, FallbackSCSV) {
[email protected]48d2b7c52014-06-27 01:16:557012 SpawnedTestServer::SSLOptions ssl_options(
7013 SpawnedTestServer::SSLOptions::CERT_OK);
7014 // Configure HTTPS server to be intolerant of TLS >= 1.0 in order to trigger
7015 // a version fallback.
7016 ssl_options.tls_intolerant =
7017 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL;
7018 // Have the server process TLS_FALLBACK_SCSV so that version fallback
7019 // connections are rejected.
7020 ssl_options.fallback_scsv_enabled = true;
7021
7022 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
7023
7024 // ERR_SSL_VERSION_OR_CIPHER_MISMATCH is how the server simulates version
7025 // intolerance. If the fallback SCSV is processed when the original error
7026 // that caused the fallback should be returned, which should be
7027 // ERR_SSL_VERSION_OR_CIPHER_MISMATCH.
7028 ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH);
7029}
7030
[email protected]12833302014-07-02 01:57:317031// Tests that we don't fallback on connection closed with servers that implement
7032// TLS_FALLBACK_SCSV. Also ensure that the original error code is reported.
[email protected]12833302014-07-02 01:57:317033TEST_F(HTTPSFallbackTest, FallbackSCSVClosed) {
[email protected]12833302014-07-02 01:57:317034 SpawnedTestServer::SSLOptions ssl_options(
7035 SpawnedTestServer::SSLOptions::CERT_OK);
7036 // Configure HTTPS server to be intolerant of TLS >= 1.0 in order to trigger
7037 // a version fallback.
7038 ssl_options.tls_intolerant =
7039 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL;
7040 ssl_options.tls_intolerance_type =
7041 SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE;
7042 // Have the server process TLS_FALLBACK_SCSV so that version fallback
7043 // connections are rejected.
7044 ssl_options.fallback_scsv_enabled = true;
7045
7046 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
7047
7048 // The original error should be replayed on rejected fallback.
7049 ExpectFailure(ERR_CONNECTION_CLOSED);
7050}
7051
[email protected]48d2b7c52014-06-27 01:16:557052// Tests that the SSLv3 fallback triggers on alert.
7053TEST_F(HTTPSFallbackTest, SSLv3Fallback) {
7054 SpawnedTestServer::SSLOptions ssl_options(
7055 SpawnedTestServer::SSLOptions::CERT_OK);
7056 ssl_options.tls_intolerant =
7057 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL;
7058
7059 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
7060 ExpectConnection(SSL_CONNECTION_VERSION_SSL3);
7061}
7062
7063// Tests that the SSLv3 fallback triggers on closed connections.
7064TEST_F(HTTPSFallbackTest, SSLv3FallbackClosed) {
7065 SpawnedTestServer::SSLOptions ssl_options(
7066 SpawnedTestServer::SSLOptions::CERT_OK);
7067 ssl_options.tls_intolerant =
7068 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL;
7069 ssl_options.tls_intolerance_type =
7070 SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE;
7071
7072 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
7073 ExpectConnection(SSL_CONNECTION_VERSION_SSL3);
7074}
7075
7076// This test is disabled on Android because the remote test server doesn't cause
[email protected]5aea79182014-07-14 20:43:417077// a TCP reset.
7078#if !defined(OS_ANDROID)
[email protected]48d2b7c52014-06-27 01:16:557079// Tests that a reset connection does not fallback down to SSL3.
7080TEST_F(HTTPSFallbackTest, SSLv3NoFallbackReset) {
7081 SpawnedTestServer::SSLOptions ssl_options(
7082 SpawnedTestServer::SSLOptions::CERT_OK);
7083 ssl_options.tls_intolerant =
7084 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL;
7085 ssl_options.tls_intolerance_type =
7086 SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_RESET;
7087
7088 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
7089 ExpectFailure(ERR_CONNECTION_RESET);
7090}
[email protected]5aea79182014-07-14 20:43:417091#endif // !OS_ANDROID
[email protected]48d2b7c52014-06-27 01:16:557092
[email protected]a8fed1742013-12-27 02:14:247093class HTTPSSessionTest : public testing::Test {
7094 public:
7095 HTTPSSessionTest() : default_context_(true) {
[email protected]cba24642014-08-15 20:49:597096 cert_verifier_.set_default_result(OK);
[email protected]a8fed1742013-12-27 02:14:247097
7098 default_context_.set_network_delegate(&default_network_delegate_);
7099 default_context_.set_cert_verifier(&cert_verifier_);
7100 default_context_.Init();
7101 }
7102 virtual ~HTTPSSessionTest() {}
7103
7104 protected:
7105 MockCertVerifier cert_verifier_;
7106 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest.
7107 TestURLRequestContext default_context_;
7108};
7109
7110// Tests that session resumption is not attempted if an invalid certificate
7111// is presented.
7112TEST_F(HTTPSSessionTest, DontResumeSessionsForInvalidCertificates) {
7113 SpawnedTestServer::SSLOptions ssl_options;
7114 ssl_options.record_resume = true;
7115 SpawnedTestServer test_server(
7116 SpawnedTestServer::TYPE_HTTPS,
7117 ssl_options,
7118 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7119 ASSERT_TRUE(test_server.Start());
7120
7121 SSLClientSocket::ClearSessionCache();
7122
7123 // Simulate the certificate being expired and attempt a connection.
[email protected]cba24642014-08-15 20:49:597124 cert_verifier_.set_default_result(ERR_CERT_DATE_INVALID);
[email protected]a8fed1742013-12-27 02:14:247125 {
7126 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:197127 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
7128 test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, NULL));
[email protected]a8fed1742013-12-27 02:14:247129
[email protected]f7022f32014-08-21 16:32:197130 r->Start();
7131 EXPECT_TRUE(r->is_pending());
[email protected]a8fed1742013-12-27 02:14:247132
7133 base::RunLoop().Run();
7134
7135 EXPECT_EQ(1, d.response_started_count());
7136 }
7137
7138 reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())->
7139 CloseAllConnections();
7140
7141 // Now change the certificate to be acceptable (so that the response is
7142 // loaded), and ensure that no session id is presented to the peer.
[email protected]cba24642014-08-15 20:49:597143 cert_verifier_.set_default_result(OK);
[email protected]a8fed1742013-12-27 02:14:247144 {
7145 TestDelegate d;
[email protected]f7022f32014-08-21 16:32:197146 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
7147 test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, NULL));
[email protected]a8fed1742013-12-27 02:14:247148
[email protected]f7022f32014-08-21 16:32:197149 r->Start();
7150 EXPECT_TRUE(r->is_pending());
[email protected]a8fed1742013-12-27 02:14:247151
7152 base::RunLoop().Run();
7153
7154 // The response will look like;
7155 // insert abc
7156 // insert xyz
7157 //
7158 // With a newline at the end which makes the split think that there are
7159 // three lines.
7160 //
7161 // If a session was presented (eg: a bug), then the response would look
7162 // like;
7163 // insert abc
7164 // lookup abc
7165 // insert xyz
7166
7167 EXPECT_EQ(1, d.response_started_count());
7168 std::vector<std::string> lines;
7169 base::SplitString(d.data_received(), '\n', &lines);
7170 ASSERT_EQ(3u, lines.size()) << d.data_received();
7171
7172 std::string session_id;
7173 for (size_t i = 0; i < 2; i++) {
7174 std::vector<std::string> parts;
7175 base::SplitString(lines[i], '\t', &parts);
7176 ASSERT_EQ(2u, parts.size());
7177 EXPECT_EQ("insert", parts[0]);
7178 if (i == 0) {
7179 session_id = parts[1];
7180 } else {
7181 EXPECT_NE(session_id, parts[1]);
7182 }
7183 }
7184 }
7185}
7186
[email protected]a13234c2012-03-20 21:45:027187class TestSSLConfigService : public SSLConfigService {
[email protected]dffe8242012-03-20 15:14:277188 public:
[email protected]3a86a712013-07-30 07:16:207189 TestSSLConfigService(bool ev_enabled,
7190 bool online_rev_checking,
7191 bool rev_checking_required_local_anchors)
[email protected]a13234c2012-03-20 21:45:027192 : ev_enabled_(ev_enabled),
[email protected]3a86a712013-07-30 07:16:207193 online_rev_checking_(online_rev_checking),
7194 rev_checking_required_local_anchors_(
7195 rev_checking_required_local_anchors) {}
[email protected]a13234c2012-03-20 21:45:027196
[email protected]a9813302012-04-28 09:29:287197 // SSLConfigService:
7198 virtual void GetSSLConfig(SSLConfig* config) OVERRIDE {
[email protected]dffe8242012-03-20 15:14:277199 *config = SSLConfig();
[email protected]a13234c2012-03-20 21:45:027200 config->rev_checking_enabled = online_rev_checking_;
7201 config->verify_ev_cert = ev_enabled_;
[email protected]3a86a712013-07-30 07:16:207202 config->rev_checking_required_local_anchors =
7203 rev_checking_required_local_anchors_;
[email protected]dffe8242012-03-20 15:14:277204 }
[email protected]a13234c2012-03-20 21:45:027205
[email protected]a9813302012-04-28 09:29:287206 protected:
7207 virtual ~TestSSLConfigService() {}
7208
[email protected]a13234c2012-03-20 21:45:027209 private:
7210 const bool ev_enabled_;
7211 const bool online_rev_checking_;
[email protected]3a86a712013-07-30 07:16:207212 const bool rev_checking_required_local_anchors_;
[email protected]dffe8242012-03-20 15:14:277213};
7214
7215// This the fingerprint of the "Testing CA" certificate used by the testserver.
7216// See net/data/ssl/certificates/ocsp-test-root.pem.
[email protected]ede03212012-09-07 12:52:267217static const SHA1HashValue kOCSPTestCertFingerprint =
[email protected]dffe8242012-03-20 15:14:277218 { { 0xf1, 0xad, 0xf6, 0xce, 0x42, 0xac, 0xe7, 0xb4, 0xf4, 0x24,
7219 0xdb, 0x1a, 0xf7, 0xa0, 0x9f, 0x09, 0xa1, 0xea, 0xf1, 0x5c } };
7220
[email protected]51523f52013-07-31 21:57:287221// This is the SHA256, SPKI hash of the "Testing CA" certificate used by the
7222// testserver.
7223static const SHA256HashValue kOCSPTestCertSPKI = { {
7224 0xee, 0xe6, 0x51, 0x2d, 0x4c, 0xfa, 0xf7, 0x3e,
7225 0x6c, 0xd8, 0xca, 0x67, 0xed, 0xb5, 0x5d, 0x49,
7226 0x76, 0xe1, 0x52, 0xa7, 0x6e, 0x0e, 0xa0, 0x74,
7227 0x09, 0x75, 0xe6, 0x23, 0x24, 0xbd, 0x1b, 0x28,
7228} };
7229
[email protected]dffe8242012-03-20 15:14:277230// This is the policy OID contained in the certificates that testserver
7231// generates.
7232static const char kOCSPTestCertPolicy[] = "1.3.6.1.4.1.11129.2.4.1";
7233
7234class HTTPSOCSPTest : public HTTPSRequestTest {
7235 public:
7236 HTTPSOCSPTest()
[email protected]ef2bf422012-05-11 03:27:097237 : context_(true),
[email protected]b6f2de32012-08-17 04:35:087238 ev_test_policy_(
7239 new ScopedTestEVPolicy(EVRootCAMetadata::GetInstance(),
7240 kOCSPTestCertFingerprint,
7241 kOCSPTestCertPolicy)) {
[email protected]a13234c2012-03-20 21:45:027242 }
7243
7244 virtual void SetUp() OVERRIDE {
[email protected]ef2bf422012-05-11 03:27:097245 SetupContext(&context_);
7246 context_.Init();
[email protected]dffe8242012-03-20 15:14:277247
[email protected]cba24642014-08-15 20:49:597248 scoped_refptr<X509Certificate> root_cert =
[email protected]3a86a712013-07-30 07:16:207249 ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem");
dchengc2e01e82014-08-27 00:24:427250 CHECK_NE(static_cast<X509Certificate*>(NULL), root_cert.get());
[email protected]90499482013-06-01 00:39:507251 test_root_.reset(new ScopedTestRoot(root_cert.get()));
[email protected]dffe8242012-03-20 15:14:277252
[email protected]a592c0432012-12-01 18:10:297253#if defined(USE_NSS) || defined(OS_IOS)
[email protected]ef2bf422012-05-11 03:27:097254 SetURLRequestContextForNSSHttpIO(&context_);
[email protected]dffe8242012-03-20 15:14:277255 EnsureNSSHttpIOInit();
7256#endif
7257 }
7258
[email protected]ce7d0cbc2013-05-03 18:57:227259 void DoConnection(const SpawnedTestServer::SSLOptions& ssl_options,
[email protected]dffe8242012-03-20 15:14:277260 CertStatus* out_cert_status) {
[email protected]924e9f92012-12-16 22:00:537261 // We always overwrite out_cert_status.
7262 *out_cert_status = 0;
[email protected]ce7d0cbc2013-05-03 18:57:227263 SpawnedTestServer test_server(
7264 SpawnedTestServer::TYPE_HTTPS,
7265 ssl_options,
7266 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
[email protected]dffe8242012-03-20 15:14:277267 ASSERT_TRUE(test_server.Start());
7268
7269 TestDelegate d;
7270 d.set_allow_certificate_errors(true);
[email protected]f7022f32014-08-21 16:32:197271 scoped_ptr<URLRequest> r(context_.CreateRequest(
7272 test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL));
7273 r->Start();
[email protected]dffe8242012-03-20 15:14:277274
[email protected]255620da2013-08-19 13:14:297275 base::RunLoop().Run();
[email protected]dffe8242012-03-20 15:14:277276
7277 EXPECT_EQ(1, d.response_started_count());
[email protected]f7022f32014-08-21 16:32:197278 *out_cert_status = r->ssl_info().cert_status;
[email protected]dffe8242012-03-20 15:14:277279 }
7280
[email protected]46fadfd2013-02-06 09:40:167281 virtual ~HTTPSOCSPTest() {
[email protected]a592c0432012-12-01 18:10:297282#if defined(USE_NSS) || defined(OS_IOS)
[email protected]dffe8242012-03-20 15:14:277283 ShutdownNSSHttpIO();
7284#endif
7285 }
7286
[email protected]a13234c2012-03-20 21:45:027287 protected:
7288 // SetupContext configures the URLRequestContext that will be used for making
7289 // connetions to testserver. This can be overridden in test subclasses for
7290 // different behaviour.
7291 virtual void SetupContext(URLRequestContext* context) {
7292 context->set_ssl_config_service(
7293 new TestSSLConfigService(true /* check for EV */,
[email protected]3a86a712013-07-30 07:16:207294 true /* online revocation checking */,
7295 false /* require rev. checking for local
7296 anchors */));
[email protected]a13234c2012-03-20 21:45:027297 }
7298
[email protected]dffe8242012-03-20 15:14:277299 scoped_ptr<ScopedTestRoot> test_root_;
[email protected]ef2bf422012-05-11 03:27:097300 TestURLRequestContext context_;
[email protected]b6f2de32012-08-17 04:35:087301 scoped_ptr<ScopedTestEVPolicy> ev_test_policy_;
[email protected]dffe8242012-03-20 15:14:277302};
7303
[email protected]a13234c2012-03-20 21:45:027304static CertStatus ExpectedCertStatusForFailedOnlineRevocationCheck() {
[email protected]05454a432012-03-20 20:04:017305#if defined(OS_WIN)
[email protected]a13234c2012-03-20 21:45:027306 // Windows can return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION but we don't
7307 // have that ability on other platforms.
7308 return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION;
7309#else
7310 return 0;
7311#endif
7312}
7313
[email protected]3a86a712013-07-30 07:16:207314// SystemSupportsHardFailRevocationChecking returns true iff the current
7315// operating system supports revocation checking and can distinguish between
7316// situations where a given certificate lacks any revocation information (eg:
7317// no CRLDistributionPoints and no OCSP Responder AuthorityInfoAccess) and when
7318// revocation information cannot be obtained (eg: the CRL was unreachable).
7319// If it does not, then tests which rely on 'hard fail' behaviour should be
7320// skipped.
7321static bool SystemSupportsHardFailRevocationChecking() {
7322#if defined(OS_WIN) || defined(USE_NSS) || defined(OS_IOS)
7323 return true;
7324#else
7325 return false;
7326#endif
7327}
7328
[email protected]a13234c2012-03-20 21:45:027329// SystemUsesChromiumEVMetadata returns true iff the current operating system
7330// uses Chromium's EV metadata (i.e. EVRootCAMetadata). If it does not, then
7331// several tests are effected because our testing EV certificate won't be
7332// recognised as EV.
7333static bool SystemUsesChromiumEVMetadata() {
[email protected]e1b2d732014-03-28 16:20:327334#if defined(USE_OPENSSL_CERTS) && !defined(OS_ANDROID)
[email protected]5c504192012-03-27 19:00:577335 // http://crbug.com/117478 - OpenSSL does not support EV validation.
7336 return false;
[email protected]e1b2d732014-03-28 16:20:327337#elif (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID)
7338 // On OS X and Android, we use the system to tell us whether a certificate is
7339 // EV or not and the system won't recognise our testing root.
[email protected]05454a432012-03-20 20:04:017340 return false;
7341#else
7342 return true;
7343#endif
7344}
7345
[email protected]b6f2de32012-08-17 04:35:087346static bool SystemSupportsOCSP() {
[email protected]5c504192012-03-27 19:00:577347#if defined(USE_OPENSSL)
7348 // http://crbug.com/117478 - OpenSSL does not support OCSP.
7349 return false;
7350#elif defined(OS_WIN)
[email protected]a13234c2012-03-20 21:45:027351 return base::win::GetVersion() >= base::win::VERSION_VISTA;
7352#elif defined(OS_ANDROID)
7353 // TODO(jnd): http://crbug.com/117478 - EV verification is not yet supported.
7354 return false;
7355#else
7356 return true;
7357#endif
7358}
7359
[email protected]dffe8242012-03-20 15:14:277360TEST_F(HTTPSOCSPTest, Valid) {
7361 if (!SystemSupportsOCSP()) {
7362 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7363 return;
7364 }
7365
[email protected]ce7d0cbc2013-05-03 18:57:227366 SpawnedTestServer::SSLOptions ssl_options(
7367 SpawnedTestServer::SSLOptions::CERT_AUTO);
7368 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK;
[email protected]dffe8242012-03-20 15:14:277369
[email protected]924e9f92012-12-16 22:00:537370 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:127371 DoConnection(ssl_options, &cert_status);
[email protected]dffe8242012-03-20 15:14:277372
7373 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
7374
[email protected]a13234c2012-03-20 21:45:027375 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7376 static_cast<bool>(cert_status & CERT_STATUS_IS_EV));
[email protected]dffe8242012-03-20 15:14:277377
7378 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
7379}
7380
7381TEST_F(HTTPSOCSPTest, Revoked) {
7382 if (!SystemSupportsOCSP()) {
7383 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7384 return;
7385 }
7386
[email protected]ce7d0cbc2013-05-03 18:57:227387 SpawnedTestServer::SSLOptions ssl_options(
7388 SpawnedTestServer::SSLOptions::CERT_AUTO);
7389 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED;
[email protected]dffe8242012-03-20 15:14:277390
7391 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:127392 DoConnection(ssl_options, &cert_status);
[email protected]dffe8242012-03-20 15:14:277393
[email protected]a592c0432012-12-01 18:10:297394#if !(defined(OS_MACOSX) && !defined(OS_IOS))
[email protected]dffe8242012-03-20 15:14:277395 // Doesn't pass on OS X yet for reasons that need to be investigated.
7396 EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS);
7397#endif
7398 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7399 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
7400}
7401
7402TEST_F(HTTPSOCSPTest, Invalid) {
7403 if (!SystemSupportsOCSP()) {
7404 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7405 return;
7406 }
7407
[email protected]ce7d0cbc2013-05-03 18:57:227408 SpawnedTestServer::SSLOptions ssl_options(
7409 SpawnedTestServer::SSLOptions::CERT_AUTO);
7410 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
[email protected]dffe8242012-03-20 15:14:277411
[email protected]924e9f92012-12-16 22:00:537412 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:127413 DoConnection(ssl_options, &cert_status);
[email protected]dffe8242012-03-20 15:14:277414
[email protected]a13234c2012-03-20 21:45:027415 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
[email protected]dffe8242012-03-20 15:14:277416 cert_status & CERT_STATUS_ALL_ERRORS);
[email protected]dffe8242012-03-20 15:14:277417
7418 // Without a positive OCSP response, we shouldn't show the EV status.
7419 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7420 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
7421}
[email protected]a13234c2012-03-20 21:45:027422
[email protected]3a86a712013-07-30 07:16:207423class HTTPSHardFailTest : public HTTPSOCSPTest {
7424 protected:
7425 virtual void SetupContext(URLRequestContext* context) OVERRIDE {
7426 context->set_ssl_config_service(
7427 new TestSSLConfigService(false /* check for EV */,
7428 false /* online revocation checking */,
7429 true /* require rev. checking for local
7430 anchors */));
7431 }
7432};
7433
7434
7435TEST_F(HTTPSHardFailTest, FailsOnOCSPInvalid) {
7436 if (!SystemSupportsOCSP()) {
7437 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7438 return;
7439 }
7440
7441 if (!SystemSupportsHardFailRevocationChecking()) {
7442 LOG(WARNING) << "Skipping test because system doesn't support hard fail "
7443 << "revocation checking";
7444 return;
7445 }
7446
7447 SpawnedTestServer::SSLOptions ssl_options(
7448 SpawnedTestServer::SSLOptions::CERT_AUTO);
7449 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
7450
7451 CertStatus cert_status;
7452 DoConnection(ssl_options, &cert_status);
7453
7454 EXPECT_EQ(CERT_STATUS_REVOKED,
7455 cert_status & CERT_STATUS_REVOKED);
7456
7457 // Without a positive OCSP response, we shouldn't show the EV status.
7458 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
7459}
7460
[email protected]a13234c2012-03-20 21:45:027461class HTTPSEVCRLSetTest : public HTTPSOCSPTest {
7462 protected:
7463 virtual void SetupContext(URLRequestContext* context) OVERRIDE {
7464 context->set_ssl_config_service(
7465 new TestSSLConfigService(true /* check for EV */,
[email protected]3a86a712013-07-30 07:16:207466 false /* online revocation checking */,
7467 false /* require rev. checking for local
7468 anchors */));
[email protected]a13234c2012-03-20 21:45:027469 }
7470};
7471
7472TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndInvalidOCSP) {
7473 if (!SystemSupportsOCSP()) {
7474 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7475 return;
7476 }
7477
[email protected]ce7d0cbc2013-05-03 18:57:227478 SpawnedTestServer::SSLOptions ssl_options(
7479 SpawnedTestServer::SSLOptions::CERT_AUTO);
7480 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
[email protected]a13234c2012-03-20 21:45:027481 SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>());
7482
[email protected]924e9f92012-12-16 22:00:537483 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:127484 DoConnection(ssl_options, &cert_status);
[email protected]a13234c2012-03-20 21:45:027485
7486 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
7487 cert_status & CERT_STATUS_ALL_ERRORS);
7488
7489 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
[email protected]b6f2de32012-08-17 04:35:087490 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7491 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
[email protected]a13234c2012-03-20 21:45:027492}
7493
[email protected]be0fff62013-08-29 23:37:487494TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndRevokedOCSP) {
7495 if (!SystemSupportsOCSP()) {
7496 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7497 return;
7498 }
7499
7500 SpawnedTestServer::SSLOptions ssl_options(
7501 SpawnedTestServer::SSLOptions::CERT_AUTO);
7502 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED;
7503 SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>());
7504
7505 CertStatus cert_status;
7506 DoConnection(ssl_options, &cert_status);
7507
7508 // Currently only works for Windows. When using NSS or OS X, it's not
7509 // possible to determine whether the check failed because of actual
7510 // revocation or because there was an OCSP failure.
7511#if defined(OS_WIN)
7512 EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS);
7513#else
7514 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
7515#endif
7516
7517 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7518 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7519 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
7520}
7521
[email protected]a13234c2012-03-20 21:45:027522TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndGoodOCSP) {
7523 if (!SystemSupportsOCSP()) {
7524 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7525 return;
7526 }
7527
[email protected]ce7d0cbc2013-05-03 18:57:227528 SpawnedTestServer::SSLOptions ssl_options(
7529 SpawnedTestServer::SSLOptions::CERT_AUTO);
7530 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK;
[email protected]a13234c2012-03-20 21:45:027531 SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>());
7532
7533 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:127534 DoConnection(ssl_options, &cert_status);
[email protected]a13234c2012-03-20 21:45:027535
7536 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
7537
7538 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7539 static_cast<bool>(cert_status & CERT_STATUS_IS_EV));
[email protected]b6f2de32012-08-17 04:35:087540 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7541 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
[email protected]a13234c2012-03-20 21:45:027542}
7543
7544TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSet) {
7545 if (!SystemSupportsOCSP()) {
7546 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7547 return;
7548 }
7549
[email protected]ce7d0cbc2013-05-03 18:57:227550 SpawnedTestServer::SSLOptions ssl_options(
7551 SpawnedTestServer::SSLOptions::CERT_AUTO);
7552 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
[email protected]a13234c2012-03-20 21:45:027553 SSLConfigService::SetCRLSet(
7554 scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting()));
7555
7556 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:127557 DoConnection(ssl_options, &cert_status);
[email protected]a13234c2012-03-20 21:45:027558
7559 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
7560 cert_status & CERT_STATUS_ALL_ERRORS);
7561
7562 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
[email protected]b6f2de32012-08-17 04:35:087563 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7564 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
[email protected]a13234c2012-03-20 21:45:027565}
7566
[email protected]51523f52013-07-31 21:57:287567TEST_F(HTTPSEVCRLSetTest, FreshCRLSetCovered) {
7568 if (!SystemSupportsOCSP()) {
7569 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7570 return;
7571 }
7572
7573 SpawnedTestServer::SSLOptions ssl_options(
7574 SpawnedTestServer::SSLOptions::CERT_AUTO);
7575 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
7576 SSLConfigService::SetCRLSet(
7577 scoped_refptr<CRLSet>(CRLSet::ForTesting(
7578 false, &kOCSPTestCertSPKI, "")));
7579
7580 CertStatus cert_status;
7581 DoConnection(ssl_options, &cert_status);
7582
7583 // With a fresh CRLSet that covers the issuing certificate, we shouldn't do a
7584 // revocation check for EV.
7585 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
7586 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7587 static_cast<bool>(cert_status & CERT_STATUS_IS_EV));
7588 EXPECT_FALSE(
7589 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
7590}
7591
7592TEST_F(HTTPSEVCRLSetTest, FreshCRLSetNotCovered) {
7593 if (!SystemSupportsOCSP()) {
7594 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7595 return;
7596 }
7597
[email protected]ce7d0cbc2013-05-03 18:57:227598 SpawnedTestServer::SSLOptions ssl_options(
7599 SpawnedTestServer::SSLOptions::CERT_AUTO);
7600 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
[email protected]a13234c2012-03-20 21:45:027601 SSLConfigService::SetCRLSet(
7602 scoped_refptr<CRLSet>(CRLSet::EmptyCRLSetForTesting()));
7603
[email protected]51523f52013-07-31 21:57:287604 CertStatus cert_status = 0;
[email protected]295e5cd2012-08-23 01:05:127605 DoConnection(ssl_options, &cert_status);
[email protected]a13234c2012-03-20 21:45:027606
[email protected]51523f52013-07-31 21:57:287607 // Even with a fresh CRLSet, we should still do online revocation checks when
7608 // the certificate chain isn't covered by the CRLSet, which it isn't in this
7609 // test.
7610 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
7611 cert_status & CERT_STATUS_ALL_ERRORS);
[email protected]a13234c2012-03-20 21:45:027612
[email protected]51523f52013-07-31 21:57:287613 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
[email protected]a13234c2012-03-20 21:45:027614 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
[email protected]51523f52013-07-31 21:57:287615 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
[email protected]a13234c2012-03-20 21:45:027616}
7617
[email protected]b6f2de32012-08-17 04:35:087618TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSetAndRevokedNonEVCert) {
7619 // Test that when EV verification is requested, but online revocation
7620 // checking is disabled, and the leaf certificate is not in fact EV, that
7621 // no revocation checking actually happens.
7622 if (!SystemSupportsOCSP()) {
7623 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7624 return;
7625 }
7626
7627 // Unmark the certificate's OID as EV, which should disable revocation
7628 // checking (as per the user preference)
7629 ev_test_policy_.reset();
7630
[email protected]ce7d0cbc2013-05-03 18:57:227631 SpawnedTestServer::SSLOptions ssl_options(
7632 SpawnedTestServer::SSLOptions::CERT_AUTO);
7633 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED;
[email protected]b6f2de32012-08-17 04:35:087634 SSLConfigService::SetCRLSet(
7635 scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting()));
7636
7637 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:127638 DoConnection(ssl_options, &cert_status);
[email protected]b6f2de32012-08-17 04:35:087639
7640 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
7641
7642 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7643 EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
7644}
7645
[email protected]a13234c2012-03-20 21:45:027646class HTTPSCRLSetTest : public HTTPSOCSPTest {
7647 protected:
7648 virtual void SetupContext(URLRequestContext* context) OVERRIDE {
7649 context->set_ssl_config_service(
7650 new TestSSLConfigService(false /* check for EV */,
[email protected]3a86a712013-07-30 07:16:207651 false /* online revocation checking */,
7652 false /* require rev. checking for local
7653 anchors */));
[email protected]a13234c2012-03-20 21:45:027654 }
7655};
7656
7657TEST_F(HTTPSCRLSetTest, ExpiredCRLSet) {
[email protected]ce7d0cbc2013-05-03 18:57:227658 SpawnedTestServer::SSLOptions ssl_options(
7659 SpawnedTestServer::SSLOptions::CERT_AUTO);
7660 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
[email protected]a13234c2012-03-20 21:45:027661 SSLConfigService::SetCRLSet(
7662 scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting()));
7663
[email protected]924e9f92012-12-16 22:00:537664 CertStatus cert_status;
[email protected]295e5cd2012-08-23 01:05:127665 DoConnection(ssl_options, &cert_status);
[email protected]a13234c2012-03-20 21:45:027666
7667 // If we're not trying EV verification then, even if the CRLSet has expired,
7668 // we don't fall back to online revocation checks.
7669 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
7670 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7671 EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
7672}
[email protected]51523f52013-07-31 21:57:287673
7674TEST_F(HTTPSCRLSetTest, CRLSetRevoked) {
[email protected]591cffcd2014-08-18 20:02:307675#if defined(OS_ANDROID)
[email protected]51523f52013-07-31 21:57:287676 LOG(WARNING) << "Skipping test because system doesn't support CRLSets";
7677 return;
7678#endif
7679
7680 SpawnedTestServer::SSLOptions ssl_options(
7681 SpawnedTestServer::SSLOptions::CERT_AUTO);
7682 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK;
7683 ssl_options.cert_serial = 10;
7684 SSLConfigService::SetCRLSet(
7685 scoped_refptr<CRLSet>(CRLSet::ForTesting(
7686 false, &kOCSPTestCertSPKI, "\x0a")));
7687
7688 CertStatus cert_status = 0;
7689 DoConnection(ssl_options, &cert_status);
7690
7691 // If the certificate is recorded as revoked in the CRLSet, that should be
7692 // reflected without online revocation checking.
7693 EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS);
7694 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7695 EXPECT_FALSE(
7696 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
7697}
[email protected]316c1e5e2012-09-12 15:17:447698#endif // !defined(OS_IOS)
[email protected]dffe8242012-03-20 15:14:277699
[email protected]316c1e5e2012-09-12 15:17:447700#if !defined(DISABLE_FTP_SUPPORT)
[email protected]b89ca032009-08-31 21:41:317701class URLRequestTestFTP : public URLRequestTest {
[email protected]95409e12010-08-17 20:07:117702 public:
[email protected]d9fca99a2012-02-24 16:16:207703 URLRequestTestFTP()
[email protected]ce7d0cbc2013-05-03 18:57:227704 : test_server_(SpawnedTestServer::TYPE_FTP, SpawnedTestServer::kLocalhost,
[email protected]6cdfd7f2013-02-08 20:40:157705 base::FilePath()) {
[email protected]95409e12010-08-17 20:07:117706 }
7707
[email protected]b89ca032009-08-31 21:41:317708 protected:
[email protected]ce7d0cbc2013-05-03 18:57:227709 SpawnedTestServer test_server_;
[email protected]b89ca032009-08-31 21:41:317710};
7711
[email protected]d2a133182012-08-05 16:44:087712// Make sure an FTP request using an unsafe ports fails.
[email protected]f2f31b32013-01-16 23:24:097713TEST_F(URLRequestTestFTP, UnsafePort) {
[email protected]d2a133182012-08-05 16:44:087714 ASSERT_TRUE(test_server_.Start());
7715
[email protected]9d5730b2012-08-24 17:42:497716 URLRequestJobFactoryImpl job_factory;
[email protected]e0f35c92013-05-08 16:04:347717 FtpNetworkLayer ftp_transaction_factory(default_context_.host_resolver());
[email protected]d2a133182012-08-05 16:44:087718
7719 GURL url("ftp://127.0.0.1:7");
[email protected]d2a133182012-08-05 16:44:087720 job_factory.SetProtocolHandler(
7721 "ftp",
[email protected]e0f35c92013-05-08 16:04:347722 new FtpProtocolHandler(&ftp_transaction_factory));
[email protected]d2a133182012-08-05 16:44:087723 default_context_.set_job_factory(&job_factory);
7724
7725 TestDelegate d;
7726 {
[email protected]f7022f32014-08-21 16:32:197727 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
7728 url, DEFAULT_PRIORITY, &d, NULL));
7729 r->Start();
7730 EXPECT_TRUE(r->is_pending());
[email protected]d2a133182012-08-05 16:44:087731
[email protected]255620da2013-08-19 13:14:297732 base::RunLoop().Run();
[email protected]d2a133182012-08-05 16:44:087733
[email protected]f7022f32014-08-21 16:32:197734 EXPECT_FALSE(r->is_pending());
7735 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
7736 EXPECT_EQ(ERR_UNSAFE_PORT, r->status().error());
[email protected]d2a133182012-08-05 16:44:087737 }
7738}
7739
[email protected]5accf7332009-11-24 03:41:387740// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:127741TEST_F(URLRequestTestFTP, DISABLED_FTPDirectoryListing) {
[email protected]95409e12010-08-17 20:07:117742 ASSERT_TRUE(test_server_.Start());
7743
[email protected]a25e90e2009-09-09 17:05:377744 TestDelegate d;
7745 {
[email protected]f7022f32014-08-21 16:32:197746 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
7747 test_server_.GetURL("/"), DEFAULT_PRIORITY, &d, NULL));
7748 r->Start();
7749 EXPECT_TRUE(r->is_pending());
[email protected]a25e90e2009-09-09 17:05:377750
[email protected]255620da2013-08-19 13:14:297751 base::RunLoop().Run();
[email protected]a25e90e2009-09-09 17:05:377752
[email protected]f7022f32014-08-21 16:32:197753 EXPECT_FALSE(r->is_pending());
[email protected]a25e90e2009-09-09 17:05:377754 EXPECT_EQ(1, d.response_started_count());
7755 EXPECT_FALSE(d.received_data_before_response());
7756 EXPECT_LT(0, d.bytes_received());
[email protected]6d81b482011-02-22 19:47:197757 EXPECT_EQ(test_server_.host_port_pair().host(),
[email protected]f7022f32014-08-21 16:32:197758 r->GetSocketAddress().host());
[email protected]6d81b482011-02-22 19:47:197759 EXPECT_EQ(test_server_.host_port_pair().port(),
[email protected]f7022f32014-08-21 16:32:197760 r->GetSocketAddress().port());
[email protected]a25e90e2009-09-09 17:05:377761 }
7762}
7763
[email protected]7df70012010-02-04 00:09:557764// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:127765TEST_F(URLRequestTestFTP, DISABLED_FTPGetTestAnonymous) {
[email protected]95409e12010-08-17 20:07:117766 ASSERT_TRUE(test_server_.Start());
7767
[email protected]6cdfd7f2013-02-08 20:40:157768 base::FilePath app_path;
[email protected]dd265012009-01-08 20:45:277769 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:027770 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:277771 TestDelegate d;
7772 {
[email protected]f7022f32014-08-21 16:32:197773 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
7774 test_server_.GetURL("/LICENSE"), DEFAULT_PRIORITY, &d, NULL));
7775 r->Start();
7776 EXPECT_TRUE(r->is_pending());
[email protected]dd265012009-01-08 20:45:277777
[email protected]255620da2013-08-19 13:14:297778 base::RunLoop().Run();
[email protected]dd265012009-01-08 20:45:277779
7780 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:497781 base::GetFileSize(app_path, &file_size);
[email protected]dd265012009-01-08 20:45:277782
[email protected]f7022f32014-08-21 16:32:197783 EXPECT_FALSE(r->is_pending());
[email protected]dd265012009-01-08 20:45:277784 EXPECT_EQ(1, d.response_started_count());
7785 EXPECT_FALSE(d.received_data_before_response());
7786 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
[email protected]6d81b482011-02-22 19:47:197787 EXPECT_EQ(test_server_.host_port_pair().host(),
[email protected]f7022f32014-08-21 16:32:197788 r->GetSocketAddress().host());
[email protected]6d81b482011-02-22 19:47:197789 EXPECT_EQ(test_server_.host_port_pair().port(),
[email protected]f7022f32014-08-21 16:32:197790 r->GetSocketAddress().port());
[email protected]dd265012009-01-08 20:45:277791 }
7792}
7793
[email protected]e9ecbd12009-12-20 18:44:407794// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:127795TEST_F(URLRequestTestFTP, DISABLED_FTPGetTest) {
[email protected]95409e12010-08-17 20:07:117796 ASSERT_TRUE(test_server_.Start());
7797
[email protected]6cdfd7f2013-02-08 20:40:157798 base::FilePath app_path;
[email protected]dd265012009-01-08 20:45:277799 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:027800 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:277801 TestDelegate d;
7802 {
[email protected]f7022f32014-08-21 16:32:197803 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
[email protected]95409e12010-08-17 20:07:117804 test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", "chrome"),
[email protected]f7022f32014-08-21 16:32:197805 DEFAULT_PRIORITY, &d, NULL));
7806 r->Start();
7807 EXPECT_TRUE(r->is_pending());
[email protected]dd265012009-01-08 20:45:277808
[email protected]255620da2013-08-19 13:14:297809 base::RunLoop().Run();
[email protected]dd265012009-01-08 20:45:277810
7811 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:497812 base::GetFileSize(app_path, &file_size);
[email protected]dd265012009-01-08 20:45:277813
[email protected]f7022f32014-08-21 16:32:197814 EXPECT_FALSE(r->is_pending());
[email protected]6d81b482011-02-22 19:47:197815 EXPECT_EQ(test_server_.host_port_pair().host(),
[email protected]f7022f32014-08-21 16:32:197816 r->GetSocketAddress().host());
[email protected]6d81b482011-02-22 19:47:197817 EXPECT_EQ(test_server_.host_port_pair().port(),
[email protected]f7022f32014-08-21 16:32:197818 r->GetSocketAddress().port());
[email protected]dd265012009-01-08 20:45:277819 EXPECT_EQ(1, d.response_started_count());
7820 EXPECT_FALSE(d.received_data_before_response());
7821 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
[email protected]58e32bb2013-01-21 18:23:257822
7823 LoadTimingInfo load_timing_info;
[email protected]f7022f32014-08-21 16:32:197824 r->GetLoadTimingInfo(&load_timing_info);
[email protected]3b23a222013-05-15 21:33:257825 TestLoadTimingNoHttpResponse(load_timing_info);
[email protected]dd265012009-01-08 20:45:277826 }
7827}
7828
[email protected]49abd652010-08-05 05:04:537829// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:127830TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPassword) {
[email protected]95409e12010-08-17 20:07:117831 ASSERT_TRUE(test_server_.Start());
7832
[email protected]6cdfd7f2013-02-08 20:40:157833 base::FilePath app_path;
[email protected]dd265012009-01-08 20:45:277834 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:027835 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:277836 TestDelegate d;
7837 {
[email protected]f7022f32014-08-21 16:32:197838 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
7839 test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome",
7840 "wrong_password"),
7841 DEFAULT_PRIORITY, &d, NULL));
7842 r->Start();
7843 EXPECT_TRUE(r->is_pending());
[email protected]dd265012009-01-08 20:45:277844
[email protected]255620da2013-08-19 13:14:297845 base::RunLoop().Run();
[email protected]dd265012009-01-08 20:45:277846
7847 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:497848 base::GetFileSize(app_path, &file_size);
[email protected]dd265012009-01-08 20:45:277849
[email protected]f7022f32014-08-21 16:32:197850 EXPECT_FALSE(r->is_pending());
[email protected]dd265012009-01-08 20:45:277851 EXPECT_EQ(1, d.response_started_count());
7852 EXPECT_FALSE(d.received_data_before_response());
7853 EXPECT_EQ(d.bytes_received(), 0);
7854 }
7855}
7856
[email protected]cde4e80d2009-10-16 19:58:157857// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:127858TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPasswordRestart) {
[email protected]95409e12010-08-17 20:07:117859 ASSERT_TRUE(test_server_.Start());
7860
[email protected]6cdfd7f2013-02-08 20:40:157861 base::FilePath app_path;
[email protected]8b8a197d2009-08-26 15:57:587862 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
7863 app_path = app_path.AppendASCII("LICENSE");
7864 TestDelegate d;
7865 // Set correct login credentials. The delegate will be asked for them when
7866 // the initial login with wrong credentials will fail.
[email protected]f3cf9802011-10-28 18:44:587867 d.set_credentials(AuthCredentials(kChrome, kChrome));
[email protected]8b8a197d2009-08-26 15:57:587868 {
[email protected]f7022f32014-08-21 16:32:197869 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
7870 test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome",
7871 "wrong_password"),
7872 DEFAULT_PRIORITY, &d, NULL));
7873 r->Start();
7874 EXPECT_TRUE(r->is_pending());
[email protected]8b8a197d2009-08-26 15:57:587875
[email protected]255620da2013-08-19 13:14:297876 base::RunLoop().Run();
[email protected]8b8a197d2009-08-26 15:57:587877
7878 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:497879 base::GetFileSize(app_path, &file_size);
[email protected]8b8a197d2009-08-26 15:57:587880
[email protected]f7022f32014-08-21 16:32:197881 EXPECT_FALSE(r->is_pending());
[email protected]8b8a197d2009-08-26 15:57:587882 EXPECT_EQ(1, d.response_started_count());
7883 EXPECT_FALSE(d.received_data_before_response());
7884 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
7885 }
7886}
7887
[email protected]49abd652010-08-05 05:04:537888// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:127889TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUser) {
[email protected]95409e12010-08-17 20:07:117890 ASSERT_TRUE(test_server_.Start());
7891
[email protected]6cdfd7f2013-02-08 20:40:157892 base::FilePath app_path;
[email protected]dd265012009-01-08 20:45:277893 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
[email protected]399b8702009-05-01 20:34:027894 app_path = app_path.AppendASCII("LICENSE");
[email protected]dd265012009-01-08 20:45:277895 TestDelegate d;
7896 {
[email protected]f7022f32014-08-21 16:32:197897 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
7898 test_server_.GetURLWithUserAndPassword("/LICENSE", "wrong_user",
7899 "chrome"),
7900 DEFAULT_PRIORITY, &d, NULL));
7901 r->Start();
7902 EXPECT_TRUE(r->is_pending());
[email protected]dd265012009-01-08 20:45:277903
[email protected]255620da2013-08-19 13:14:297904 base::RunLoop().Run();
[email protected]dd265012009-01-08 20:45:277905
7906 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:497907 base::GetFileSize(app_path, &file_size);
[email protected]dd265012009-01-08 20:45:277908
[email protected]f7022f32014-08-21 16:32:197909 EXPECT_FALSE(r->is_pending());
[email protected]dd265012009-01-08 20:45:277910 EXPECT_EQ(1, d.response_started_count());
7911 EXPECT_FALSE(d.received_data_before_response());
7912 EXPECT_EQ(d.bytes_received(), 0);
7913 }
7914}
[email protected]8b8a197d2009-08-26 15:57:587915
[email protected]cde4e80d2009-10-16 19:58:157916// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:127917TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUserRestart) {
[email protected]95409e12010-08-17 20:07:117918 ASSERT_TRUE(test_server_.Start());
7919
[email protected]6cdfd7f2013-02-08 20:40:157920 base::FilePath app_path;
[email protected]8b8a197d2009-08-26 15:57:587921 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
7922 app_path = app_path.AppendASCII("LICENSE");
7923 TestDelegate d;
7924 // Set correct login credentials. The delegate will be asked for them when
7925 // the initial login with wrong credentials will fail.
[email protected]f3cf9802011-10-28 18:44:587926 d.set_credentials(AuthCredentials(kChrome, kChrome));
[email protected]8b8a197d2009-08-26 15:57:587927 {
[email protected]f7022f32014-08-21 16:32:197928 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
7929 test_server_.GetURLWithUserAndPassword("/LICENSE", "wrong_user",
7930 "chrome"),
7931 DEFAULT_PRIORITY, &d, NULL));
7932 r->Start();
7933 EXPECT_TRUE(r->is_pending());
[email protected]8b8a197d2009-08-26 15:57:587934
[email protected]255620da2013-08-19 13:14:297935 base::RunLoop().Run();
[email protected]8b8a197d2009-08-26 15:57:587936
7937 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:497938 base::GetFileSize(app_path, &file_size);
[email protected]8b8a197d2009-08-26 15:57:587939
[email protected]f7022f32014-08-21 16:32:197940 EXPECT_FALSE(r->is_pending());
[email protected]8b8a197d2009-08-26 15:57:587941 EXPECT_EQ(1, d.response_started_count());
7942 EXPECT_FALSE(d.received_data_before_response());
7943 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
7944 }
7945}
[email protected]60a3df52009-09-22 16:13:247946
[email protected]cde4e80d2009-10-16 19:58:157947// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:127948TEST_F(URLRequestTestFTP, DISABLED_FTPCacheURLCredentials) {
[email protected]95409e12010-08-17 20:07:117949 ASSERT_TRUE(test_server_.Start());
7950
[email protected]6cdfd7f2013-02-08 20:40:157951 base::FilePath app_path;
[email protected]60a3df52009-09-22 16:13:247952 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
7953 app_path = app_path.AppendASCII("LICENSE");
7954
7955 scoped_ptr<TestDelegate> d(new TestDelegate);
7956 {
7957 // Pass correct login identity in the URL.
[email protected]f7022f32014-08-21 16:32:197958 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
[email protected]2ca01e52013-10-31 22:05:197959 test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", "chrome"),
[email protected]f7022f32014-08-21 16:32:197960 DEFAULT_PRIORITY, d.get(), NULL));
7961 r->Start();
7962 EXPECT_TRUE(r->is_pending());
[email protected]60a3df52009-09-22 16:13:247963
[email protected]255620da2013-08-19 13:14:297964 base::RunLoop().Run();
[email protected]60a3df52009-09-22 16:13:247965
7966 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:497967 base::GetFileSize(app_path, &file_size);
[email protected]60a3df52009-09-22 16:13:247968
[email protected]f7022f32014-08-21 16:32:197969 EXPECT_FALSE(r->is_pending());
[email protected]60a3df52009-09-22 16:13:247970 EXPECT_EQ(1, d->response_started_count());
7971 EXPECT_FALSE(d->received_data_before_response());
7972 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
7973 }
7974
7975 d.reset(new TestDelegate);
7976 {
7977 // This request should use cached identity from previous request.
[email protected]f7022f32014-08-21 16:32:197978 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
7979 test_server_.GetURL("/LICENSE"), DEFAULT_PRIORITY, d.get(), NULL));
7980 r->Start();
7981 EXPECT_TRUE(r->is_pending());
[email protected]60a3df52009-09-22 16:13:247982
[email protected]255620da2013-08-19 13:14:297983 base::RunLoop().Run();
[email protected]60a3df52009-09-22 16:13:247984
7985 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:497986 base::GetFileSize(app_path, &file_size);
[email protected]60a3df52009-09-22 16:13:247987
[email protected]f7022f32014-08-21 16:32:197988 EXPECT_FALSE(r->is_pending());
[email protected]60a3df52009-09-22 16:13:247989 EXPECT_EQ(1, d->response_started_count());
7990 EXPECT_FALSE(d->received_data_before_response());
7991 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
7992 }
7993}
7994
[email protected]cde4e80d2009-10-16 19:58:157995// Flaky, see http://crbug.com/25045.
[email protected]9ef1d98e2012-02-14 00:34:127996TEST_F(URLRequestTestFTP, DISABLED_FTPCacheLoginBoxCredentials) {
[email protected]95409e12010-08-17 20:07:117997 ASSERT_TRUE(test_server_.Start());
7998
[email protected]6cdfd7f2013-02-08 20:40:157999 base::FilePath app_path;
[email protected]60a3df52009-09-22 16:13:248000 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
8001 app_path = app_path.AppendASCII("LICENSE");
8002
8003 scoped_ptr<TestDelegate> d(new TestDelegate);
8004 // Set correct login credentials. The delegate will be asked for them when
8005 // the initial login with wrong credentials will fail.
[email protected]f3cf9802011-10-28 18:44:588006 d->set_credentials(AuthCredentials(kChrome, kChrome));
[email protected]60a3df52009-09-22 16:13:248007 {
[email protected]f7022f32014-08-21 16:32:198008 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
8009 test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome",
8010 "wrong_password"),
8011 DEFAULT_PRIORITY, d.get(), NULL));
8012 r->Start();
8013 EXPECT_TRUE(r->is_pending());
[email protected]60a3df52009-09-22 16:13:248014
[email protected]255620da2013-08-19 13:14:298015 base::RunLoop().Run();
[email protected]60a3df52009-09-22 16:13:248016
8017 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:498018 base::GetFileSize(app_path, &file_size);
[email protected]60a3df52009-09-22 16:13:248019
[email protected]f7022f32014-08-21 16:32:198020 EXPECT_FALSE(r->is_pending());
[email protected]60a3df52009-09-22 16:13:248021 EXPECT_EQ(1, d->response_started_count());
8022 EXPECT_FALSE(d->received_data_before_response());
8023 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
8024 }
8025
8026 // Use a new delegate without explicit credentials. The cached ones should be
8027 // used.
8028 d.reset(new TestDelegate);
8029 {
8030 // Don't pass wrong credentials in the URL, they would override valid cached
8031 // ones.
[email protected]f7022f32014-08-21 16:32:198032 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
8033 test_server_.GetURL("/LICENSE"), DEFAULT_PRIORITY, d.get(), NULL));
8034 r->Start();
8035 EXPECT_TRUE(r->is_pending());
[email protected]60a3df52009-09-22 16:13:248036
[email protected]255620da2013-08-19 13:14:298037 base::RunLoop().Run();
[email protected]60a3df52009-09-22 16:13:248038
8039 int64 file_size = 0;
[email protected]56285702013-12-04 18:22:498040 base::GetFileSize(app_path, &file_size);
[email protected]60a3df52009-09-22 16:13:248041
[email protected]f7022f32014-08-21 16:32:198042 EXPECT_FALSE(r->is_pending());
[email protected]60a3df52009-09-22 16:13:248043 EXPECT_EQ(1, d->response_started_count());
8044 EXPECT_FALSE(d->received_data_before_response());
8045 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
8046 }
8047}
[email protected]316c1e5e2012-09-12 15:17:448048#endif // !defined(DISABLE_FTP_SUPPORT)
[email protected]7461a402011-03-24 23:19:518049
8050} // namespace net