[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 5 | #include "build/build_config.h" |
| 6 | |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 7 | #if defined(OS_WIN) |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 8 | #include <windows.h> |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 9 | #include <shlobj.h> |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 10 | #endif |
| 11 | |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 12 | #include <stdint.h> |
| 13 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 14 | #include <algorithm> |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame^] | 15 | #include <limits> |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 16 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 17 | #include "base/bind.h" |
[email protected] | 8523ba5 | 2011-05-22 19:00:58 | [diff] [blame] | 18 | #include "base/compiler_specific.h" |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 19 | #include "base/files/file_path.h" |
thestig | d8df033 | 2014-09-04 06:33:29 | [diff] [blame] | 20 | #include "base/files/file_util.h" |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 21 | #include "base/files/scoped_temp_dir.h" |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 22 | #include "base/format_macros.h" |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 23 | #include "base/json/json_reader.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 24 | #include "base/location.h" |
mmenke | 19378d2 | 2014-09-09 04:12:59 | [diff] [blame] | 25 | #include "base/memory/scoped_ptr.h" |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 26 | #include "base/memory/weak_ptr.h" |
[email protected] | 7f86564d | 2013-07-18 00:41:22 | [diff] [blame] | 27 | #include "base/message_loop/message_loop.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 28 | #include "base/path_service.h" |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 29 | #include "base/power_monitor/power_monitor.h" |
| 30 | #include "base/power_monitor/power_monitor_source.h" |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 31 | #include "base/run_loop.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 32 | #include "base/single_thread_task_runner.h" |
[email protected] | 4dc3ad4f | 2013-06-11 07:15:50 | [diff] [blame] | 33 | #include "base/strings/string_number_conversions.h" |
[email protected] | d069c11a | 2013-04-13 00:01:55 | [diff] [blame] | 34 | #include "base/strings/string_piece.h" |
[email protected] | d778e042 | 2013-03-06 18:10:22 | [diff] [blame] | 35 | #include "base/strings/string_split.h" |
[email protected] | 7f86564d | 2013-07-18 00:41:22 | [diff] [blame] | 36 | #include "base/strings/string_util.h" |
| 37 | #include "base/strings/stringprintf.h" |
[email protected] | 750b2f3c | 2013-06-07 18:41:05 | [diff] [blame] | 38 | #include "base/strings/utf_string_conversions.h" |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 39 | #include "base/test/histogram_tester.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 40 | #include "base/thread_task_runner_handle.h" |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 41 | #include "base/values.h" |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 42 | #include "net/base/chunked_upload_data_stream.h" |
| 43 | #include "net/base/elements_upload_data_stream.h" |
tbansal | 1c92d5b | 2015-08-14 20:14:43 | [diff] [blame] | 44 | #include "net/base/external_estimate_provider.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 45 | #include "net/base/load_flags.h" |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 46 | #include "net/base/load_timing_info.h" |
| 47 | #include "net/base/load_timing_info_test_util.h" |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 48 | #include "net/base/net_errors.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 49 | #include "net/base/net_module.h" |
| 50 | #include "net/base/net_util.h" |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 51 | #include "net/base/network_quality_estimator.h" |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 52 | #include "net/base/request_priority.h" |
[email protected] | 42fdb45 | 2012-11-01 12:44:40 | [diff] [blame] | 53 | #include "net/base/test_data_directory.h" |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 54 | #include "net/base/upload_bytes_element_reader.h" |
| 55 | #include "net/base/upload_data_stream.h" |
| 56 | #include "net/base/upload_file_element_reader.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 57 | #include "net/cert/ev_root_ca_metadata.h" |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 58 | #include "net/cert/mock_cert_verifier.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 59 | #include "net/cert/test_root_certs.h" |
eroman | ed744f3 | 2015-04-09 06:35:49 | [diff] [blame] | 60 | #include "net/cert_net/nss_ocsp.h" |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 61 | #include "net/cookies/cookie_monster.h" |
| 62 | #include "net/cookies/cookie_store_test_helpers.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 63 | #include "net/disk_cache/disk_cache.h" |
[email protected] | f2cb3cf | 2013-03-21 01:40:53 | [diff] [blame] | 64 | #include "net/dns/mock_host_resolver.h" |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 65 | #include "net/http/http_byte_range.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 66 | #include "net/http/http_cache.h" |
| 67 | #include "net/http/http_network_layer.h" |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 68 | #include "net/http/http_network_session.h" |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 69 | #include "net/http/http_request_headers.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 70 | #include "net/http/http_response_headers.h" |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 71 | #include "net/http/http_util.h" |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 72 | #include "net/log/net_log.h" |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 73 | #include "net/log/test_net_log.h" |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 74 | #include "net/log/test_net_log_entry.h" |
| 75 | #include "net/log/test_net_log_util.h" |
[email protected] | 63de95b | 2008-12-10 04:11:27 | [diff] [blame] | 76 | #include "net/proxy/proxy_service.h" |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 77 | #include "net/socket/ssl_client_socket.h" |
davidben | 8ecc307 | 2014-09-03 23:19:09 | [diff] [blame] | 78 | #include "net/ssl/ssl_cipher_suite_names.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 79 | #include "net/ssl/ssl_connection_status_flags.h" |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 80 | #include "net/ssl/ssl_server_config.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 81 | #include "net/test/cert_test_util.h" |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 82 | #include "net/test/embedded_test_server/embedded_test_server.h" |
| 83 | #include "net/test/embedded_test_server/http_request.h" |
| 84 | #include "net/test/embedded_test_server/http_response.h" |
[email protected] | 89b3252 | 2013-05-07 20:04:21 | [diff] [blame] | 85 | #include "net/test/spawned_test_server/spawned_test_server.h" |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 86 | #include "net/test/url_request/url_request_failed_job.h" |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 87 | #include "net/url_request/data_protocol_handler.h" |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 88 | #include "net/url_request/static_http_user_agent_settings.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 89 | #include "net/url_request/url_request.h" |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 90 | #include "net/url_request/url_request_filter.h" |
[email protected] | bcb84f8b | 2009-08-31 16:20:14 | [diff] [blame] | 91 | #include "net/url_request/url_request_http_job.h" |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 92 | #include "net/url_request/url_request_intercepting_job_factory.h" |
| 93 | #include "net/url_request/url_request_interceptor.h" |
[email protected] | 9d5730b | 2012-08-24 17:42:49 | [diff] [blame] | 94 | #include "net/url_request/url_request_job_factory_impl.h" |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 95 | #include "net/url_request/url_request_redirect_job.h" |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 96 | #include "net/url_request/url_request_test_job.h" |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 97 | #include "net/url_request/url_request_test_util.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 98 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 99 | #include "testing/platform_test.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 100 | |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 101 | #if !defined(DISABLE_FILE_SUPPORT) |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 102 | #include "net/base/filename_util.h" |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 103 | #include "net/url_request/file_protocol_handler.h" |
| 104 | #include "net/url_request/url_request_file_dir_job.h" |
| 105 | #endif |
| 106 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 107 | #if !defined(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) |
| 108 | #include "net/ftp/ftp_network_layer.h" |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 109 | #include "net/url_request/ftp_protocol_handler.h" |
| 110 | #endif |
| 111 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 112 | #if defined(OS_WIN) |
[email protected] | 451fd90 | 2012-10-03 17:14:48 | [diff] [blame] | 113 | #include "base/win/scoped_com_initializer.h" |
[email protected] | aed9efb | 2013-04-13 01:20:56 | [diff] [blame] | 114 | #include "base/win/scoped_comptr.h" |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 115 | #include "base/win/windows_version.h" |
| 116 | #endif |
| 117 | |
[email protected] | ad65a3e | 2013-12-25 18:18:01 | [diff] [blame] | 118 | using base::ASCIIToUTF16; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 119 | using base::Time; |
halton.huo | e4e4574 | 2014-12-08 07:55:46 | [diff] [blame] | 120 | using std::string; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 121 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 122 | namespace net { |
| 123 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 124 | namespace { |
| 125 | |
[email protected] | 42cba2fb | 2013-03-29 19:58:57 | [diff] [blame] | 126 | const base::string16 kChrome(ASCIIToUTF16("chrome")); |
| 127 | const base::string16 kSecret(ASCIIToUTF16("secret")); |
| 128 | const base::string16 kUser(ASCIIToUTF16("user")); |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 129 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 130 | const base::FilePath::CharType kTestFilePath[] = |
| 131 | FILE_PATH_LITERAL("net/data/url_request_unittest"); |
| 132 | |
| 133 | #if !defined(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) |
| 134 | // Test file used in most FTP tests. |
| 135 | const char kFtpTestFile[] = "BullRunSpeech.txt"; |
| 136 | #endif |
| 137 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 138 | // Tests load timing information in the case a fresh connection was used, with |
| 139 | // no proxy. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 140 | void TestLoadTimingNotReused(const LoadTimingInfo& load_timing_info, |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 141 | int connect_timing_flags) { |
| 142 | EXPECT_FALSE(load_timing_info.socket_reused); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 143 | EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 144 | |
| 145 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 146 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 147 | |
| 148 | EXPECT_LE(load_timing_info.request_start, |
| 149 | load_timing_info.connect_timing.connect_start); |
| 150 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 151 | connect_timing_flags); |
| 152 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 153 | load_timing_info.send_start); |
| 154 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 155 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 156 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 157 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 158 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 159 | } |
| 160 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 161 | // Same as above, but with proxy times. |
| 162 | void TestLoadTimingNotReusedWithProxy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 163 | const LoadTimingInfo& load_timing_info, |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 164 | int connect_timing_flags) { |
| 165 | EXPECT_FALSE(load_timing_info.socket_reused); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 166 | EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 167 | |
| 168 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 169 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 170 | |
| 171 | EXPECT_LE(load_timing_info.request_start, |
| 172 | load_timing_info.proxy_resolve_start); |
| 173 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 174 | load_timing_info.proxy_resolve_end); |
| 175 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 176 | load_timing_info.connect_timing.connect_start); |
| 177 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 178 | connect_timing_flags); |
| 179 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 180 | load_timing_info.send_start); |
| 181 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 182 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 183 | } |
| 184 | |
| 185 | // Same as above, but with a reused socket and proxy times. |
| 186 | void TestLoadTimingReusedWithProxy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 187 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 188 | EXPECT_TRUE(load_timing_info.socket_reused); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 189 | EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 190 | |
| 191 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 192 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 193 | |
| 194 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 195 | |
| 196 | EXPECT_LE(load_timing_info.request_start, |
| 197 | load_timing_info.proxy_resolve_start); |
| 198 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 199 | load_timing_info.proxy_resolve_end); |
| 200 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 201 | load_timing_info.send_start); |
| 202 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 203 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 204 | } |
| 205 | |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 206 | #if !defined(DISABLE_FILE_SUPPORT) |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 207 | // Tests load timing information in the case of a cache hit, when no cache |
| 208 | // validation request was sent over the wire. |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 209 | base::StringPiece TestNetResourceProvider(int key) { |
| 210 | return "header"; |
| 211 | } |
| 212 | |
| 213 | void FillBuffer(char* buffer, size_t len) { |
| 214 | static bool called = false; |
| 215 | if (!called) { |
| 216 | called = true; |
| 217 | int seed = static_cast<int>(Time::Now().ToInternalValue()); |
| 218 | srand(seed); |
| 219 | } |
| 220 | |
| 221 | for (size_t i = 0; i < len; i++) { |
| 222 | buffer[i] = static_cast<char>(rand()); |
| 223 | if (!buffer[i]) |
| 224 | buffer[i] = 'g'; |
| 225 | } |
| 226 | } |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 227 | #endif |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 228 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 229 | void TestLoadTimingCacheHitNoNetwork( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 230 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 231 | EXPECT_FALSE(load_timing_info.socket_reused); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 232 | EXPECT_EQ(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 233 | |
| 234 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 235 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 236 | |
| 237 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 238 | EXPECT_LE(load_timing_info.request_start, load_timing_info.send_start); |
| 239 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 240 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 241 | |
| 242 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 243 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 244 | } |
| 245 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 246 | #if !defined(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 247 | // Tests load timing in the case that there is no HTTP response. This can be |
| 248 | // used to test in the case of errors or non-HTTP requests. |
| 249 | void TestLoadTimingNoHttpResponse( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 250 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 251 | EXPECT_FALSE(load_timing_info.socket_reused); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 252 | EXPECT_EQ(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 253 | |
| 254 | // Only the request times should be non-null. |
| 255 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 256 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 257 | |
| 258 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 259 | |
| 260 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 261 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 262 | EXPECT_TRUE(load_timing_info.send_start.is_null()); |
| 263 | EXPECT_TRUE(load_timing_info.send_end.is_null()); |
| 264 | EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); |
| 265 | } |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 266 | #endif |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 267 | |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 268 | // Test power monitor source that can simulate entering suspend mode. Can't use |
| 269 | // the one in base/ because it insists on bringing its own MessageLoop. |
| 270 | class TestPowerMonitorSource : public base::PowerMonitorSource { |
| 271 | public: |
| 272 | TestPowerMonitorSource() {} |
| 273 | ~TestPowerMonitorSource() override {} |
| 274 | |
| 275 | void Suspend() { ProcessPowerEvent(SUSPEND_EVENT); } |
| 276 | |
| 277 | void Resume() { ProcessPowerEvent(RESUME_EVENT); } |
| 278 | |
| 279 | bool IsOnBatteryPowerImpl() override { return false; } |
| 280 | |
| 281 | private: |
| 282 | DISALLOW_COPY_AND_ASSIGN(TestPowerMonitorSource); |
| 283 | }; |
| 284 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 285 | // Job that allows monitoring of its priority. |
| 286 | class PriorityMonitoringURLRequestJob : public URLRequestTestJob { |
| 287 | public: |
| 288 | // The latest priority of the job is always written to |request_priority_|. |
| 289 | PriorityMonitoringURLRequestJob(URLRequest* request, |
| 290 | NetworkDelegate* network_delegate, |
| 291 | RequestPriority* request_priority) |
| 292 | : URLRequestTestJob(request, network_delegate), |
| 293 | request_priority_(request_priority) { |
| 294 | *request_priority_ = DEFAULT_PRIORITY; |
| 295 | } |
| 296 | |
| 297 | void SetPriority(RequestPriority priority) override { |
| 298 | *request_priority_ = priority; |
| 299 | URLRequestTestJob::SetPriority(priority); |
| 300 | } |
| 301 | |
| 302 | private: |
| 303 | RequestPriority* const request_priority_; |
| 304 | }; |
| 305 | |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 306 | // Do a case-insensitive search through |haystack| for |needle|. |
| 307 | bool ContainsString(const std::string& haystack, const char* needle) { |
brettw | a2027fb | 2015-07-14 02:24:50 | [diff] [blame] | 308 | std::string::const_iterator it = std::search( |
| 309 | haystack.begin(), haystack.end(), needle, needle + strlen(needle), |
| 310 | base::CaseInsensitiveCompareASCII<char>()); |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 311 | return it != haystack.end(); |
| 312 | } |
| 313 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 314 | scoped_ptr<UploadDataStream> CreateSimpleUploadData(const char* data) { |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 315 | scoped_ptr<UploadElementReader> reader( |
| 316 | new UploadBytesElementReader(data, strlen(data))); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 317 | return ElementsUploadDataStream::CreateWithReader(reader.Pass(), 0); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 318 | } |
| 319 | |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 320 | // Verify that the SSLInfo of a successful SSL connection has valid values. |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 321 | void CheckSSLInfo(const SSLInfo& ssl_info) { |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 322 | // -1 means unknown. 0 means no encryption. |
| 323 | EXPECT_GT(ssl_info.security_bits, 0); |
| 324 | |
| 325 | // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated. |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame^] | 326 | uint16_t cipher_suite = |
| 327 | SSLConnectionStatusToCipherSuite(ssl_info.connection_status); |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 328 | EXPECT_NE(0U, cipher_suite); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 329 | } |
| 330 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 331 | void CheckFullRequestHeaders(const HttpRequestHeaders& headers, |
| 332 | const GURL& host_url) { |
| 333 | std::string sent_value; |
| 334 | |
| 335 | EXPECT_TRUE(headers.GetHeader("Host", &sent_value)); |
| 336 | EXPECT_EQ(GetHostAndOptionalPort(host_url), sent_value); |
| 337 | |
| 338 | EXPECT_TRUE(headers.GetHeader("Connection", &sent_value)); |
| 339 | EXPECT_EQ("keep-alive", sent_value); |
| 340 | } |
| 341 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 342 | #if !defined(OS_IOS) |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 343 | bool FingerprintsEqual(const HashValueVector& a, const HashValueVector& b) { |
[email protected] | 69d7ff44 | 2012-02-13 22:41:27 | [diff] [blame] | 344 | size_t size = a.size(); |
| 345 | |
| 346 | if (size != b.size()) |
| 347 | return false; |
| 348 | |
| 349 | for (size_t i = 0; i < size; ++i) { |
| 350 | if (!a[i].Equals(b[i])) |
| 351 | return false; |
| 352 | } |
| 353 | |
| 354 | return true; |
| 355 | } |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 356 | #endif // !defined(OS_IOS) |
[email protected] | 69d7ff44 | 2012-02-13 22:41:27 | [diff] [blame] | 357 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 358 | // A network delegate that allows the user to choose a subset of request stages |
| 359 | // to block in. When blocking, the delegate can do one of the following: |
| 360 | // * synchronously return a pre-specified error code, or |
| 361 | // * asynchronously return that value via an automatically called callback, |
| 362 | // or |
| 363 | // * block and wait for the user to do a callback. |
| 364 | // Additionally, the user may also specify a redirect URL -- then each request |
| 365 | // with the current URL different from the redirect target will be redirected |
| 366 | // to that target, in the on-before-URL-request stage, independent of whether |
| 367 | // the delegate blocks in ON_BEFORE_URL_REQUEST or not. |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 368 | class BlockingNetworkDelegate : public TestNetworkDelegate { |
| 369 | public: |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 370 | // Stages in which the delegate can block. |
| 371 | enum Stage { |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 372 | NOT_BLOCKED = 0, |
| 373 | ON_BEFORE_URL_REQUEST = 1 << 0, |
| 374 | ON_BEFORE_SEND_HEADERS = 1 << 1, |
| 375 | ON_HEADERS_RECEIVED = 1 << 2, |
| 376 | ON_AUTH_REQUIRED = 1 << 3 |
| 377 | }; |
| 378 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 379 | // Behavior during blocked stages. During other stages, just |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 380 | // returns OK or NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 381 | enum BlockMode { |
| 382 | SYNCHRONOUS, // No callback, returns specified return values. |
| 383 | AUTO_CALLBACK, // |this| posts a task to run the callback using the |
| 384 | // specified return codes. |
| 385 | USER_CALLBACK, // User takes care of doing a callback. |retval_| and |
| 386 | // |auth_retval_| are ignored. In every blocking stage the |
| 387 | // message loop is quit. |
| 388 | }; |
| 389 | |
| 390 | // Creates a delegate which does not block at all. |
| 391 | explicit BlockingNetworkDelegate(BlockMode block_mode); |
| 392 | |
| 393 | // For users to trigger a callback returning |response|. |
| 394 | // Side-effects: resets |stage_blocked_for_callback_| and stored callbacks. |
| 395 | // Only call if |block_mode_| == USER_CALLBACK. |
| 396 | void DoCallback(int response); |
| 397 | void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response); |
| 398 | |
| 399 | // Setters. |
| 400 | void set_retval(int retval) { |
| 401 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 402 | ASSERT_NE(ERR_IO_PENDING, retval); |
| 403 | ASSERT_NE(OK, retval); |
| 404 | retval_ = retval; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 405 | } |
| 406 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 407 | // If |auth_retval| == AUTH_REQUIRED_RESPONSE_SET_AUTH, then |
| 408 | // |auth_credentials_| will be passed with the response. |
| 409 | void set_auth_retval(AuthRequiredResponse auth_retval) { |
| 410 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 411 | ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, auth_retval); |
| 412 | auth_retval_ = auth_retval; |
| 413 | } |
| 414 | void set_auth_credentials(const AuthCredentials& auth_credentials) { |
| 415 | auth_credentials_ = auth_credentials; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 416 | } |
| 417 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 418 | void set_redirect_url(const GURL& url) { |
| 419 | redirect_url_ = url; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 420 | } |
| 421 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 422 | void set_block_on(int block_on) { |
| 423 | block_on_ = block_on; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 424 | } |
| 425 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 426 | // Allows the user to check in which state did we block. |
| 427 | Stage stage_blocked_for_callback() const { |
| 428 | EXPECT_EQ(USER_CALLBACK, block_mode_); |
| 429 | return stage_blocked_for_callback_; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | private: |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 433 | void RunCallback(int response, const CompletionCallback& callback); |
| 434 | void RunAuthCallback(AuthRequiredResponse response, |
| 435 | const AuthCallback& callback); |
| 436 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 437 | // TestNetworkDelegate implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 438 | int OnBeforeURLRequest(URLRequest* request, |
| 439 | const CompletionCallback& callback, |
| 440 | GURL* new_url) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 441 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 442 | int OnBeforeSendHeaders(URLRequest* request, |
| 443 | const CompletionCallback& callback, |
| 444 | HttpRequestHeaders* headers) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 445 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 446 | int OnHeadersReceived( |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 447 | URLRequest* request, |
| 448 | const CompletionCallback& callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 449 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 450 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 451 | GURL* allowed_unsafe_redirect_url) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 452 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 453 | NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 454 | URLRequest* request, |
| 455 | const AuthChallengeInfo& auth_info, |
| 456 | const AuthCallback& callback, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 457 | AuthCredentials* credentials) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 458 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 459 | // Resets the callbacks and |stage_blocked_for_callback_|. |
| 460 | void Reset(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 461 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 462 | // Checks whether we should block in |stage|. If yes, returns an error code |
| 463 | // and optionally sets up callback based on |block_mode_|. If no, returns OK. |
| 464 | int MaybeBlockStage(Stage stage, const CompletionCallback& callback); |
| 465 | |
| 466 | // Configuration parameters, can be adjusted by public methods: |
| 467 | const BlockMode block_mode_; |
| 468 | |
| 469 | // Values returned on blocking stages when mode is SYNCHRONOUS or |
| 470 | // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING. |
| 471 | int retval_; // To be returned in non-auth stages. |
| 472 | AuthRequiredResponse auth_retval_; |
| 473 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 474 | GURL redirect_url_; // Used if non-empty during OnBeforeURLRequest. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 475 | int block_on_; // Bit mask: in which stages to block. |
| 476 | |
| 477 | // |auth_credentials_| will be copied to |*target_auth_credential_| on |
| 478 | // callback. |
| 479 | AuthCredentials auth_credentials_; |
| 480 | AuthCredentials* target_auth_credentials_; |
| 481 | |
| 482 | // Internal variables, not set by not the user: |
| 483 | // Last blocked stage waiting for user callback (unused if |block_mode_| != |
| 484 | // USER_CALLBACK). |
| 485 | Stage stage_blocked_for_callback_; |
| 486 | |
| 487 | // Callback objects stored during blocking stages. |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 488 | CompletionCallback callback_; |
| 489 | AuthCallback auth_callback_; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 490 | |
| 491 | base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_; |
| 492 | |
| 493 | DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 494 | }; |
| 495 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 496 | BlockingNetworkDelegate::BlockingNetworkDelegate(BlockMode block_mode) |
| 497 | : block_mode_(block_mode), |
| 498 | retval_(OK), |
| 499 | auth_retval_(AUTH_REQUIRED_RESPONSE_NO_ACTION), |
| 500 | block_on_(0), |
| 501 | target_auth_credentials_(NULL), |
| 502 | stage_blocked_for_callback_(NOT_BLOCKED), |
[email protected] | aa249b5 | 2013-04-30 01:04:32 | [diff] [blame] | 503 | weak_factory_(this) { |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | void BlockingNetworkDelegate::DoCallback(int response) { |
| 507 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 508 | ASSERT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 509 | ASSERT_NE(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
| 510 | CompletionCallback callback = callback_; |
| 511 | Reset(); |
| 512 | RunCallback(response, callback); |
| 513 | } |
| 514 | |
| 515 | void BlockingNetworkDelegate::DoAuthCallback( |
| 516 | NetworkDelegate::AuthRequiredResponse response) { |
| 517 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 518 | ASSERT_EQ(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
| 519 | AuthCallback auth_callback = auth_callback_; |
| 520 | Reset(); |
| 521 | RunAuthCallback(response, auth_callback); |
| 522 | } |
| 523 | |
| 524 | void BlockingNetworkDelegate::RunCallback(int response, |
| 525 | const CompletionCallback& callback) { |
| 526 | callback.Run(response); |
| 527 | } |
| 528 | |
| 529 | void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response, |
| 530 | const AuthCallback& callback) { |
| 531 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) { |
| 532 | ASSERT_TRUE(target_auth_credentials_ != NULL); |
| 533 | *target_auth_credentials_ = auth_credentials_; |
| 534 | } |
| 535 | callback.Run(response); |
| 536 | } |
| 537 | |
| 538 | int BlockingNetworkDelegate::OnBeforeURLRequest( |
| 539 | URLRequest* request, |
| 540 | const CompletionCallback& callback, |
| 541 | GURL* new_url) { |
| 542 | if (redirect_url_ == request->url()) |
| 543 | return OK; // We've already seen this request and redirected elsewhere. |
| 544 | |
| 545 | TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); |
| 546 | |
| 547 | if (!redirect_url_.is_empty()) |
| 548 | *new_url = redirect_url_; |
| 549 | |
| 550 | return MaybeBlockStage(ON_BEFORE_URL_REQUEST, callback); |
| 551 | } |
| 552 | |
| 553 | int BlockingNetworkDelegate::OnBeforeSendHeaders( |
| 554 | URLRequest* request, |
| 555 | const CompletionCallback& callback, |
| 556 | HttpRequestHeaders* headers) { |
| 557 | TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers); |
| 558 | |
| 559 | return MaybeBlockStage(ON_BEFORE_SEND_HEADERS, callback); |
| 560 | } |
| 561 | |
| 562 | int BlockingNetworkDelegate::OnHeadersReceived( |
| 563 | URLRequest* request, |
| 564 | const CompletionCallback& callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 565 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 566 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
| 567 | GURL* allowed_unsafe_redirect_url) { |
| 568 | TestNetworkDelegate::OnHeadersReceived(request, |
| 569 | callback, |
| 570 | original_response_headers, |
| 571 | override_response_headers, |
| 572 | allowed_unsafe_redirect_url); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 573 | |
| 574 | return MaybeBlockStage(ON_HEADERS_RECEIVED, callback); |
| 575 | } |
| 576 | |
| 577 | NetworkDelegate::AuthRequiredResponse BlockingNetworkDelegate::OnAuthRequired( |
| 578 | URLRequest* request, |
| 579 | const AuthChallengeInfo& auth_info, |
| 580 | const AuthCallback& callback, |
| 581 | AuthCredentials* credentials) { |
| 582 | TestNetworkDelegate::OnAuthRequired(request, auth_info, callback, |
| 583 | credentials); |
| 584 | // Check that the user has provided callback for the previous blocked stage. |
| 585 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 586 | |
| 587 | if ((block_on_ & ON_AUTH_REQUIRED) == 0) { |
| 588 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; |
| 589 | } |
| 590 | |
| 591 | target_auth_credentials_ = credentials; |
| 592 | |
| 593 | switch (block_mode_) { |
| 594 | case SYNCHRONOUS: |
| 595 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) |
| 596 | *target_auth_credentials_ = auth_credentials_; |
| 597 | return auth_retval_; |
| 598 | |
| 599 | case AUTO_CALLBACK: |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 600 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 601 | FROM_HERE, |
| 602 | base::Bind(&BlockingNetworkDelegate::RunAuthCallback, |
| 603 | weak_factory_.GetWeakPtr(), auth_retval_, callback)); |
| 604 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 605 | |
| 606 | case USER_CALLBACK: |
| 607 | auth_callback_ = callback; |
| 608 | stage_blocked_for_callback_ = ON_AUTH_REQUIRED; |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 609 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
ki.stfu | 375812e | 2015-10-09 20:23:17 | [diff] [blame] | 610 | FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 611 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 612 | } |
| 613 | NOTREACHED(); |
| 614 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; // Dummy value. |
| 615 | } |
| 616 | |
| 617 | void BlockingNetworkDelegate::Reset() { |
| 618 | EXPECT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 619 | stage_blocked_for_callback_ = NOT_BLOCKED; |
| 620 | callback_.Reset(); |
| 621 | auth_callback_.Reset(); |
| 622 | } |
| 623 | |
| 624 | int BlockingNetworkDelegate::MaybeBlockStage( |
| 625 | BlockingNetworkDelegate::Stage stage, |
| 626 | const CompletionCallback& callback) { |
| 627 | // Check that the user has provided callback for the previous blocked stage. |
| 628 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 629 | |
| 630 | if ((block_on_ & stage) == 0) { |
| 631 | return OK; |
| 632 | } |
| 633 | |
| 634 | switch (block_mode_) { |
| 635 | case SYNCHRONOUS: |
| 636 | EXPECT_NE(OK, retval_); |
| 637 | return retval_; |
| 638 | |
| 639 | case AUTO_CALLBACK: |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 640 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 641 | FROM_HERE, base::Bind(&BlockingNetworkDelegate::RunCallback, |
| 642 | weak_factory_.GetWeakPtr(), retval_, callback)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 643 | return ERR_IO_PENDING; |
| 644 | |
| 645 | case USER_CALLBACK: |
| 646 | callback_ = callback; |
| 647 | stage_blocked_for_callback_ = stage; |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 648 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
ki.stfu | 375812e | 2015-10-09 20:23:17 | [diff] [blame] | 649 | FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 650 | return ERR_IO_PENDING; |
| 651 | } |
| 652 | NOTREACHED(); |
| 653 | return 0; |
| 654 | } |
| 655 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 656 | class TestURLRequestContextWithProxy : public TestURLRequestContext { |
| 657 | public: |
| 658 | // Does not own |delegate|. |
| 659 | TestURLRequestContextWithProxy(const std::string& proxy, |
| 660 | NetworkDelegate* delegate) |
| 661 | : TestURLRequestContext(true) { |
| 662 | context_storage_.set_proxy_service(ProxyService::CreateFixed(proxy)); |
| 663 | set_network_delegate(delegate); |
| 664 | Init(); |
| 665 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 666 | ~TestURLRequestContextWithProxy() override {} |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 667 | }; |
| 668 | |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 669 | // A mock ReportSender that just remembers the latest report |
| 670 | // URI and report to be sent. |
| 671 | class MockCertificateReportSender |
| 672 | : public TransportSecurityState::ReportSender { |
| 673 | public: |
| 674 | MockCertificateReportSender() {} |
| 675 | ~MockCertificateReportSender() override {} |
| 676 | |
| 677 | void Send(const GURL& report_uri, const std::string& report) override { |
| 678 | latest_report_uri_ = report_uri; |
| 679 | latest_report_ = report; |
| 680 | } |
| 681 | |
| 682 | const GURL& latest_report_uri() { return latest_report_uri_; } |
| 683 | const std::string& latest_report() { return latest_report_; } |
| 684 | |
| 685 | private: |
| 686 | GURL latest_report_uri_; |
| 687 | std::string latest_report_; |
| 688 | }; |
| 689 | |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 690 | class TestExperimentalFeaturesNetworkDelegate : public TestNetworkDelegate { |
| 691 | public: |
| 692 | bool OnAreExperimentalCookieFeaturesEnabled() const override { return true; } |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 693 | bool OnAreStrictSecureCookiesEnabled() const override { return true; } |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 694 | }; |
| 695 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 696 | } // namespace |
| 697 | |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 698 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 699 | class URLRequestTest : public PlatformTest { |
[email protected] | abb2609 | 2010-11-11 22:19:00 | [diff] [blame] | 700 | public: |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 701 | URLRequestTest() : default_context_(true) { |
| 702 | default_context_.set_network_delegate(&default_network_delegate_); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 703 | default_context_.set_net_log(&net_log_); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 704 | job_factory_impl_ = new URLRequestJobFactoryImpl(); |
| 705 | job_factory_.reset(job_factory_impl_); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 706 | } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 707 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 708 | ~URLRequestTest() override { |
[email protected] | e4034ad | 2013-09-20 08:36:18 | [diff] [blame] | 709 | // URLRequestJobs may post clean-up tasks on destruction. |
| 710 | base::RunLoop().RunUntilIdle(); |
| 711 | } |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 712 | |
dcheng | 2339883c | 2014-12-23 00:23:05 | [diff] [blame] | 713 | void SetUp() override { |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 714 | SetUpFactory(); |
| 715 | default_context_.set_job_factory(job_factory_.get()); |
| 716 | default_context_.Init(); |
| 717 | PlatformTest::SetUp(); |
| 718 | } |
| 719 | |
| 720 | virtual void SetUpFactory() { |
svaldez | 5d58c9e | 2015-08-24 21:36:20 | [diff] [blame] | 721 | job_factory_impl_->SetProtocolHandler( |
| 722 | "data", make_scoped_ptr(new DataProtocolHandler)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 723 | #if !defined(DISABLE_FILE_SUPPORT) |
| 724 | job_factory_impl_->SetProtocolHandler( |
svaldez | 5d58c9e | 2015-08-24 21:36:20 | [diff] [blame] | 725 | "file", make_scoped_ptr(new FileProtocolHandler( |
| 726 | base::ThreadTaskRunnerHandle::Get()))); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 727 | #endif |
| 728 | } |
| 729 | |
| 730 | TestNetworkDelegate* default_network_delegate() { |
| 731 | return &default_network_delegate_; |
| 732 | } |
| 733 | |
| 734 | const TestURLRequestContext& default_context() const { |
| 735 | return default_context_; |
| 736 | } |
| 737 | |
| 738 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 739 | // Adds the TestJobInterceptor to the default context. |
| 740 | TestJobInterceptor* AddTestInterceptor() { |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 741 | TestJobInterceptor* protocol_handler_ = new TestJobInterceptor(); |
svaldez | 5d58c9e | 2015-08-24 21:36:20 | [diff] [blame] | 742 | job_factory_impl_->SetProtocolHandler("http", nullptr); |
| 743 | job_factory_impl_->SetProtocolHandler("http", |
| 744 | make_scoped_ptr(protocol_handler_)); |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 745 | return protocol_handler_; |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 746 | } |
| 747 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 748 | protected: |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 749 | TestNetLog net_log_; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 750 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 751 | URLRequestJobFactoryImpl* job_factory_impl_; |
| 752 | scoped_ptr<URLRequestJobFactory> job_factory_; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 753 | TestURLRequestContext default_context_; |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 754 | }; |
| 755 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 756 | TEST_F(URLRequestTest, AboutBlankTest) { |
| 757 | TestDelegate d; |
| 758 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 759 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 760 | GURL("about:blank"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 761 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 762 | r->Start(); |
| 763 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 764 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 765 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 766 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 767 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 768 | EXPECT_FALSE(d.received_data_before_response()); |
| 769 | EXPECT_EQ(d.bytes_received(), 0); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 770 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 771 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 772 | |
| 773 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 774 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 775 | } |
| 776 | } |
| 777 | |
| 778 | TEST_F(URLRequestTest, DataURLImageTest) { |
| 779 | TestDelegate d; |
| 780 | { |
| 781 | // Use our nice little Chrome logo. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 782 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 783 | GURL( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 784 | "data:image/png;base64," |
| 785 | "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUB" |
| 786 | "BjG3w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQ" |
| 787 | "xIEVxitD5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI" |
| 788 | "5MpCVdW0gO7tvNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKz" |
| 789 | "Mx1+fg9bAgK6zHq9cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7Ck" |
| 790 | "IMDxQpF7r/MWq12UctI1dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0" |
| 791 | "WYUqt57yWf2McHTObYPbVD+ZwbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2H" |
| 792 | "QQcm43XwmtoYM6vVKleh0uoWvnzW3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nk" |
| 793 | "DFHF9ZS+uYVjRUasMeHUmyLYtcklTvzWGFZnNOXczThvpKIzjcahSqIzkvDLayDq6" |
| 794 | "D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTbxXX1T5xsV9tm9r4TQwHLiZw/pdDZJ" |
| 795 | "ea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5MrVvMzNaI3+ERHfrFzPKQukrQ" |
| 796 | "GI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gVMd6q5c8GdosynKmSeRuGz" |
| 797 | "pjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq/uxxZKzNiZFGD0wRC" |
| 798 | "3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7FUx1qLnV7MGF40" |
| 799 | "smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufBJbsysjjW4" |
| 800 | "kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJMnlm2O" |
| 801 | "34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfmm" |
| 802 | "oRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV" |
| 803 | "5EnhORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="), |
| 804 | DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 805 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 806 | r->Start(); |
| 807 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 808 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 809 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 810 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 811 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 812 | EXPECT_FALSE(d.received_data_before_response()); |
| 813 | EXPECT_EQ(d.bytes_received(), 911); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 814 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 815 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 816 | |
| 817 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 818 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 819 | } |
| 820 | } |
| 821 | |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 822 | #if !defined(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 823 | TEST_F(URLRequestTest, FileTest) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 824 | base::FilePath app_path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 825 | PathService::Get(base::FILE_EXE, &app_path); |
| 826 | GURL app_url = FilePathToFileURL(app_path); |
| 827 | |
| 828 | TestDelegate d; |
| 829 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 830 | scoped_ptr<URLRequest> r( |
| 831 | default_context_.CreateRequest(app_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 832 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 833 | r->Start(); |
| 834 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 835 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 836 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 837 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame^] | 838 | int64_t file_size = -1; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 839 | EXPECT_TRUE(base::GetFileSize(app_path, &file_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 840 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 841 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 842 | EXPECT_EQ(1, d.response_started_count()); |
| 843 | EXPECT_FALSE(d.received_data_before_response()); |
| 844 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 845 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 846 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 847 | |
| 848 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 849 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 850 | } |
| 851 | } |
| 852 | |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 853 | TEST_F(URLRequestTest, FileTestCancel) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 854 | base::FilePath app_path; |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 855 | PathService::Get(base::FILE_EXE, &app_path); |
| 856 | GURL app_url = FilePathToFileURL(app_path); |
| 857 | |
| 858 | TestDelegate d; |
| 859 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 860 | scoped_ptr<URLRequest> r( |
| 861 | default_context_.CreateRequest(app_url, DEFAULT_PRIORITY, &d)); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 862 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 863 | r->Start(); |
| 864 | EXPECT_TRUE(r->is_pending()); |
| 865 | r->Cancel(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 866 | } |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 867 | // Async cancellation should be safe even when URLRequest has been already |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 868 | // destroyed. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 869 | base::RunLoop().RunUntilIdle(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 870 | } |
| 871 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 872 | TEST_F(URLRequestTest, FileTestFullSpecifiedRange) { |
| 873 | const size_t buffer_size = 4000; |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 874 | scoped_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 875 | FillBuffer(buffer.get(), buffer_size); |
| 876 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 877 | base::FilePath temp_path; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 878 | EXPECT_TRUE(base::CreateTemporaryFile(&temp_path)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 879 | GURL temp_url = FilePathToFileURL(temp_path); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 880 | EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 881 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame^] | 882 | int64_t file_size; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 883 | EXPECT_TRUE(base::GetFileSize(temp_path, &file_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 884 | |
| 885 | const size_t first_byte_position = 500; |
| 886 | const size_t last_byte_position = buffer_size - first_byte_position; |
| 887 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 888 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 889 | buffer.get() + last_byte_position + 1); |
| 890 | |
| 891 | TestDelegate d; |
| 892 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 893 | scoped_ptr<URLRequest> r( |
| 894 | default_context_.CreateRequest(temp_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 895 | |
| 896 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 897 | headers.SetHeader( |
| 898 | HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 899 | HttpByteRange::Bounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 900 | first_byte_position, last_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 901 | r->SetExtraRequestHeaders(headers); |
| 902 | r->Start(); |
| 903 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 904 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 905 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 906 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 907 | EXPECT_EQ(1, d.response_started_count()); |
| 908 | EXPECT_FALSE(d.received_data_before_response()); |
| 909 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 910 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 911 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 912 | } |
| 913 | |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 914 | EXPECT_TRUE(base::DeleteFile(temp_path, false)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) { |
| 918 | const size_t buffer_size = 4000; |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 919 | scoped_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 920 | FillBuffer(buffer.get(), buffer_size); |
| 921 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 922 | base::FilePath temp_path; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 923 | EXPECT_TRUE(base::CreateTemporaryFile(&temp_path)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 924 | GURL temp_url = FilePathToFileURL(temp_path); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 925 | EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 926 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame^] | 927 | int64_t file_size; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 928 | EXPECT_TRUE(base::GetFileSize(temp_path, &file_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 929 | |
| 930 | const size_t first_byte_position = 500; |
| 931 | const size_t last_byte_position = buffer_size - 1; |
| 932 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 933 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 934 | buffer.get() + last_byte_position + 1); |
| 935 | |
| 936 | TestDelegate d; |
| 937 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 938 | scoped_ptr<URLRequest> r( |
| 939 | default_context_.CreateRequest(temp_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 940 | |
| 941 | HttpRequestHeaders headers; |
| 942 | headers.SetHeader(HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 943 | HttpByteRange::RightUnbounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 944 | first_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 945 | r->SetExtraRequestHeaders(headers); |
| 946 | r->Start(); |
| 947 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 948 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 949 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 950 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 951 | EXPECT_EQ(1, d.response_started_count()); |
| 952 | EXPECT_FALSE(d.received_data_before_response()); |
| 953 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 954 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 955 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 956 | } |
| 957 | |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 958 | EXPECT_TRUE(base::DeleteFile(temp_path, false)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | TEST_F(URLRequestTest, FileTestMultipleRanges) { |
| 962 | const size_t buffer_size = 400000; |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 963 | scoped_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 964 | FillBuffer(buffer.get(), buffer_size); |
| 965 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 966 | base::FilePath temp_path; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 967 | EXPECT_TRUE(base::CreateTemporaryFile(&temp_path)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 968 | GURL temp_url = FilePathToFileURL(temp_path); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 969 | EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 970 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame^] | 971 | int64_t file_size; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 972 | EXPECT_TRUE(base::GetFileSize(temp_path, &file_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 973 | |
| 974 | TestDelegate d; |
| 975 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 976 | scoped_ptr<URLRequest> r( |
| 977 | default_context_.CreateRequest(temp_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 978 | |
| 979 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 980 | headers.SetHeader(HttpRequestHeaders::kRange, "bytes=0-0,10-200,200-300"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 981 | r->SetExtraRequestHeaders(headers); |
| 982 | r->Start(); |
| 983 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 984 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 985 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 986 | EXPECT_TRUE(d.request_failed()); |
| 987 | } |
| 988 | |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 989 | EXPECT_TRUE(base::DeleteFile(temp_path, false)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 990 | } |
| 991 | |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 992 | TEST_F(URLRequestTest, AllowFileURLs) { |
| 993 | base::ScopedTempDir temp_dir; |
| 994 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 995 | base::FilePath test_file; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 996 | ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &test_file)); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 997 | std::string test_data("monkey"); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 998 | base::WriteFile(test_file, test_data.data(), test_data.size()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 999 | GURL test_file_url = FilePathToFileURL(test_file); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1000 | |
| 1001 | { |
| 1002 | TestDelegate d; |
| 1003 | TestNetworkDelegate network_delegate; |
| 1004 | network_delegate.set_can_access_files(true); |
| 1005 | default_context_.set_network_delegate(&network_delegate); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1006 | scoped_ptr<URLRequest> r( |
| 1007 | default_context_.CreateRequest(test_file_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1008 | r->Start(); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1009 | base::RunLoop().Run(); |
| 1010 | EXPECT_FALSE(d.request_failed()); |
| 1011 | EXPECT_EQ(test_data, d.data_received()); |
| 1012 | } |
| 1013 | |
| 1014 | { |
| 1015 | TestDelegate d; |
| 1016 | TestNetworkDelegate network_delegate; |
| 1017 | network_delegate.set_can_access_files(false); |
| 1018 | default_context_.set_network_delegate(&network_delegate); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1019 | scoped_ptr<URLRequest> r( |
| 1020 | default_context_.CreateRequest(test_file_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1021 | r->Start(); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 1022 | base::RunLoop().Run(); |
| 1023 | EXPECT_TRUE(d.request_failed()); |
| 1024 | EXPECT_EQ("", d.data_received()); |
| 1025 | } |
| 1026 | } |
| 1027 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1028 | |
| 1029 | TEST_F(URLRequestTest, FileDirCancelTest) { |
| 1030 | // Put in mock resource provider. |
| 1031 | NetModule::SetResourceProvider(TestNetResourceProvider); |
| 1032 | |
| 1033 | TestDelegate d; |
| 1034 | { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1035 | base::FilePath file_path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1036 | PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
| 1037 | file_path = file_path.Append(FILE_PATH_LITERAL("net")); |
| 1038 | file_path = file_path.Append(FILE_PATH_LITERAL("data")); |
| 1039 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1040 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1041 | FilePathToFileURL(file_path), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1042 | req->Start(); |
| 1043 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1044 | |
| 1045 | d.set_cancel_in_received_data_pending(true); |
| 1046 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1047 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | // Take out mock resource provider. |
| 1051 | NetModule::SetResourceProvider(NULL); |
| 1052 | } |
| 1053 | |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1054 | TEST_F(URLRequestTest, FileDirOutputSanity) { |
| 1055 | // Verify the general sanity of the the output of the file: |
| 1056 | // directory lister by checking for the output of a known existing |
| 1057 | // file. |
| 1058 | const char sentinel_name[] = "filedir-sentinel"; |
| 1059 | |
| 1060 | base::FilePath path; |
| 1061 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1062 | path = path.Append(kTestFilePath); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1063 | |
| 1064 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1065 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1066 | FilePathToFileURL(path), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1067 | req->Start(); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1068 | base::RunLoop().Run(); |
| 1069 | |
| 1070 | // Generate entry for the sentinel file. |
| 1071 | base::FilePath sentinel_path = path.AppendASCII(sentinel_name); |
[email protected] | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 1072 | base::File::Info info; |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 1073 | EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info)); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1074 | EXPECT_GT(info.size, 0); |
| 1075 | std::string sentinel_output = GetDirectoryListingEntry( |
| 1076 | base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)), |
| 1077 | std::string(sentinel_name), |
| 1078 | false /* is_dir */, |
| 1079 | info.size, |
| 1080 | info.last_modified); |
| 1081 | |
| 1082 | ASSERT_LT(0, d.bytes_received()); |
| 1083 | ASSERT_FALSE(d.request_failed()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1084 | ASSERT_TRUE(req->status().is_success()); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1085 | // Check for the entry generated for the "sentinel" file. |
| 1086 | const std::string& data = d.data_received(); |
| 1087 | ASSERT_NE(data.find(sentinel_output), std::string::npos); |
| 1088 | } |
| 1089 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1090 | TEST_F(URLRequestTest, FileDirRedirectNoCrash) { |
| 1091 | // There is an implicit redirect when loading a file path that matches a |
| 1092 | // directory and does not end with a slash. Ensure that following such |
| 1093 | // redirects does not crash. See http://crbug.com/18686. |
| 1094 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1095 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1096 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1097 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1098 | |
| 1099 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1100 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1101 | FilePathToFileURL(path), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1102 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1103 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1104 | |
| 1105 | ASSERT_EQ(1, d.received_redirect_count()); |
| 1106 | ASSERT_LT(0, d.bytes_received()); |
| 1107 | ASSERT_FALSE(d.request_failed()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1108 | ASSERT_TRUE(req->status().is_success()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | #if defined(OS_WIN) |
| 1112 | // Don't accept the url "file:///" on windows. See http://crbug.com/1474. |
| 1113 | TEST_F(URLRequestTest, FileDirRedirectSingleSlash) { |
| 1114 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1115 | scoped_ptr<URLRequest> req( |
| 1116 | default_context_.CreateRequest(GURL("file:///"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1117 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1118 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1119 | |
| 1120 | ASSERT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1121 | ASSERT_FALSE(req->status().is_success()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1122 | } |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1123 | #endif // defined(OS_WIN) |
| 1124 | |
| 1125 | #endif // !defined(DISABLE_FILE_SUPPORT) |
| 1126 | |
| 1127 | TEST_F(URLRequestTest, InvalidUrlTest) { |
| 1128 | TestDelegate d; |
| 1129 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1130 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1131 | GURL("invalid url"), DEFAULT_PRIORITY, &d)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1132 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1133 | r->Start(); |
| 1134 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1135 | |
| 1136 | base::RunLoop().Run(); |
| 1137 | EXPECT_TRUE(d.request_failed()); |
| 1138 | } |
| 1139 | } |
| 1140 | |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1141 | TEST_F(URLRequestTest, InvalidReferrerTest) { |
| 1142 | TestURLRequestContext context; |
| 1143 | TestNetworkDelegate network_delegate; |
| 1144 | network_delegate.set_cancel_request_with_policy_violating_referrer(true); |
| 1145 | context.set_network_delegate(&network_delegate); |
| 1146 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1147 | scoped_ptr<URLRequest> req( |
| 1148 | context.CreateRequest(GURL("http://localhost/"), DEFAULT_PRIORITY, &d)); |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1149 | req->SetReferrer("https://somewhere.com/"); |
| 1150 | |
| 1151 | req->Start(); |
| 1152 | base::RunLoop().Run(); |
| 1153 | EXPECT_TRUE(d.request_failed()); |
| 1154 | } |
| 1155 | |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1156 | #if defined(OS_WIN) |
| 1157 | TEST_F(URLRequestTest, ResolveShortcutTest) { |
| 1158 | base::FilePath app_path; |
| 1159 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 1160 | app_path = app_path.Append(kTestFilePath); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1161 | app_path = app_path.AppendASCII("with-headers.html"); |
| 1162 | |
| 1163 | std::wstring lnk_path = app_path.value() + L".lnk"; |
| 1164 | |
| 1165 | base::win::ScopedCOMInitializer com_initializer; |
| 1166 | |
| 1167 | // Temporarily create a shortcut for test |
| 1168 | { |
| 1169 | base::win::ScopedComPtr<IShellLink> shell; |
| 1170 | ASSERT_TRUE(SUCCEEDED(shell.CreateInstance(CLSID_ShellLink, NULL, |
| 1171 | CLSCTX_INPROC_SERVER))); |
| 1172 | base::win::ScopedComPtr<IPersistFile> persist; |
| 1173 | ASSERT_TRUE(SUCCEEDED(shell.QueryInterface(persist.Receive()))); |
| 1174 | EXPECT_TRUE(SUCCEEDED(shell->SetPath(app_path.value().c_str()))); |
| 1175 | EXPECT_TRUE(SUCCEEDED(shell->SetDescription(L"ResolveShortcutTest"))); |
| 1176 | EXPECT_TRUE(SUCCEEDED(persist->Save(lnk_path.c_str(), TRUE))); |
| 1177 | } |
| 1178 | |
| 1179 | TestDelegate d; |
| 1180 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1181 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1182 | FilePathToFileURL(base::FilePath(lnk_path)), DEFAULT_PRIORITY, &d)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1183 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1184 | r->Start(); |
| 1185 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1186 | |
| 1187 | base::RunLoop().Run(); |
| 1188 | |
| 1189 | WIN32_FILE_ATTRIBUTE_DATA data; |
| 1190 | GetFileAttributesEx(app_path.value().c_str(), |
| 1191 | GetFileExInfoStandard, &data); |
| 1192 | HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ, |
| 1193 | FILE_SHARE_READ, NULL, OPEN_EXISTING, |
| 1194 | FILE_ATTRIBUTE_NORMAL, NULL); |
| 1195 | EXPECT_NE(INVALID_HANDLE_VALUE, file); |
| 1196 | scoped_ptr<char[]> buffer(new char[data.nFileSizeLow]); |
| 1197 | DWORD read_size; |
| 1198 | BOOL result; |
| 1199 | result = ReadFile(file, buffer.get(), data.nFileSizeLow, |
| 1200 | &read_size, NULL); |
| 1201 | std::string content(buffer.get(), read_size); |
| 1202 | CloseHandle(file); |
| 1203 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1204 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1205 | EXPECT_EQ(1, d.received_redirect_count()); |
| 1206 | EXPECT_EQ(content, d.data_received()); |
| 1207 | } |
| 1208 | |
| 1209 | // Clean the shortcut |
| 1210 | DeleteFile(lnk_path.c_str()); |
| 1211 | } |
| 1212 | #endif // defined(OS_WIN) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1213 | |
| 1214 | // Custom URLRequestJobs for use with interceptor tests |
| 1215 | class RestartTestJob : public URLRequestTestJob { |
| 1216 | public: |
| 1217 | RestartTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1218 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1219 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1220 | void StartAsync() override { this->NotifyRestartRequired(); } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1221 | private: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1222 | ~RestartTestJob() override {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1223 | }; |
| 1224 | |
| 1225 | class CancelTestJob : public URLRequestTestJob { |
| 1226 | public: |
| 1227 | explicit CancelTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1228 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1229 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1230 | void StartAsync() override { request_->Cancel(); } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1231 | private: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1232 | ~CancelTestJob() override {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1233 | }; |
| 1234 | |
| 1235 | class CancelThenRestartTestJob : public URLRequestTestJob { |
| 1236 | public: |
| 1237 | explicit CancelThenRestartTestJob(URLRequest* request, |
| 1238 | NetworkDelegate* network_delegate) |
| 1239 | : URLRequestTestJob(request, network_delegate, true) { |
| 1240 | } |
| 1241 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1242 | void StartAsync() override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1243 | request_->Cancel(); |
| 1244 | this->NotifyRestartRequired(); |
| 1245 | } |
| 1246 | private: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1247 | ~CancelThenRestartTestJob() override {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1248 | }; |
| 1249 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1250 | // An Interceptor for use with interceptor tests. |
| 1251 | class MockURLRequestInterceptor : public URLRequestInterceptor { |
| 1252 | public: |
| 1253 | // Static getters for canned response header and data strings. |
| 1254 | static std::string ok_data() { |
| 1255 | return URLRequestTestJob::test_data_1(); |
| 1256 | } |
| 1257 | |
| 1258 | static std::string ok_headers() { |
| 1259 | return URLRequestTestJob::test_headers(); |
| 1260 | } |
| 1261 | |
| 1262 | static std::string redirect_data() { |
| 1263 | return std::string(); |
| 1264 | } |
| 1265 | |
| 1266 | static std::string redirect_headers() { |
| 1267 | return URLRequestTestJob::test_redirect_headers(); |
| 1268 | } |
| 1269 | |
| 1270 | static std::string error_data() { |
| 1271 | return std::string("ohhh nooooo mr. bill!"); |
| 1272 | } |
| 1273 | |
| 1274 | static std::string error_headers() { |
| 1275 | return URLRequestTestJob::test_error_headers(); |
| 1276 | } |
| 1277 | |
| 1278 | MockURLRequestInterceptor() |
| 1279 | : intercept_main_request_(false), restart_main_request_(false), |
| 1280 | cancel_main_request_(false), cancel_then_restart_main_request_(false), |
| 1281 | simulate_main_network_error_(false), |
| 1282 | intercept_redirect_(false), cancel_redirect_request_(false), |
| 1283 | intercept_final_response_(false), cancel_final_request_(false), |
| 1284 | use_url_request_http_job_(false), |
| 1285 | did_intercept_main_(false), did_restart_main_(false), |
| 1286 | did_cancel_main_(false), did_cancel_then_restart_main_(false), |
| 1287 | did_simulate_error_main_(false), |
| 1288 | did_intercept_redirect_(false), did_cancel_redirect_(false), |
| 1289 | did_intercept_final_(false), did_cancel_final_(false) { |
| 1290 | } |
| 1291 | |
| 1292 | ~MockURLRequestInterceptor() override { |
| 1293 | } |
| 1294 | |
| 1295 | // URLRequestInterceptor implementation: |
| 1296 | URLRequestJob* MaybeInterceptRequest( |
| 1297 | URLRequest* request, |
| 1298 | NetworkDelegate* network_delegate) const override { |
| 1299 | if (restart_main_request_) { |
| 1300 | restart_main_request_ = false; |
| 1301 | did_restart_main_ = true; |
| 1302 | return new RestartTestJob(request, network_delegate); |
| 1303 | } |
| 1304 | if (cancel_main_request_) { |
| 1305 | cancel_main_request_ = false; |
| 1306 | did_cancel_main_ = true; |
| 1307 | return new CancelTestJob(request, network_delegate); |
| 1308 | } |
| 1309 | if (cancel_then_restart_main_request_) { |
| 1310 | cancel_then_restart_main_request_ = false; |
| 1311 | did_cancel_then_restart_main_ = true; |
| 1312 | return new CancelThenRestartTestJob(request, network_delegate); |
| 1313 | } |
| 1314 | if (simulate_main_network_error_) { |
| 1315 | simulate_main_network_error_ = false; |
| 1316 | did_simulate_error_main_ = true; |
| 1317 | if (use_url_request_http_job_) { |
| 1318 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1319 | } |
| 1320 | // This job will result in error since the requested URL is not one of the |
| 1321 | // URLs supported by these tests. |
| 1322 | return new URLRequestTestJob(request, network_delegate, true); |
| 1323 | } |
| 1324 | if (!intercept_main_request_) |
| 1325 | return nullptr; |
| 1326 | intercept_main_request_ = false; |
| 1327 | did_intercept_main_ = true; |
| 1328 | URLRequestTestJob* job = new URLRequestTestJob(request, |
| 1329 | network_delegate, |
| 1330 | main_headers_, |
| 1331 | main_data_, |
| 1332 | true); |
| 1333 | job->set_load_timing_info(main_request_load_timing_info_); |
| 1334 | return job; |
| 1335 | } |
| 1336 | |
| 1337 | URLRequestJob* MaybeInterceptRedirect(URLRequest* request, |
| 1338 | NetworkDelegate* network_delegate, |
| 1339 | const GURL& location) const override { |
| 1340 | if (cancel_redirect_request_) { |
| 1341 | cancel_redirect_request_ = false; |
| 1342 | did_cancel_redirect_ = true; |
| 1343 | return new CancelTestJob(request, network_delegate); |
| 1344 | } |
| 1345 | if (!intercept_redirect_) |
| 1346 | return nullptr; |
| 1347 | intercept_redirect_ = false; |
| 1348 | did_intercept_redirect_ = true; |
| 1349 | if (use_url_request_http_job_) { |
| 1350 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1351 | } |
| 1352 | return new URLRequestTestJob(request, |
| 1353 | network_delegate, |
| 1354 | redirect_headers_, |
| 1355 | redirect_data_, |
| 1356 | true); |
| 1357 | } |
| 1358 | |
| 1359 | URLRequestJob* MaybeInterceptResponse( |
| 1360 | URLRequest* request, |
| 1361 | NetworkDelegate* network_delegate) const override { |
| 1362 | if (cancel_final_request_) { |
| 1363 | cancel_final_request_ = false; |
| 1364 | did_cancel_final_ = true; |
| 1365 | return new CancelTestJob(request, network_delegate); |
| 1366 | } |
| 1367 | if (!intercept_final_response_) |
| 1368 | return nullptr; |
| 1369 | intercept_final_response_ = false; |
| 1370 | did_intercept_final_ = true; |
| 1371 | if (use_url_request_http_job_) { |
| 1372 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1373 | } |
| 1374 | return new URLRequestTestJob(request, |
| 1375 | network_delegate, |
| 1376 | final_headers_, |
| 1377 | final_data_, |
| 1378 | true); |
| 1379 | } |
| 1380 | |
| 1381 | void set_intercept_main_request(bool intercept_main_request) { |
| 1382 | intercept_main_request_ = intercept_main_request; |
| 1383 | } |
| 1384 | |
| 1385 | void set_main_headers(const std::string& main_headers) { |
| 1386 | main_headers_ = main_headers; |
| 1387 | } |
| 1388 | |
| 1389 | void set_main_data(const std::string& main_data) { |
| 1390 | main_data_ = main_data; |
| 1391 | } |
| 1392 | |
| 1393 | void set_main_request_load_timing_info( |
| 1394 | const LoadTimingInfo& main_request_load_timing_info) { |
| 1395 | main_request_load_timing_info_ = main_request_load_timing_info; |
| 1396 | } |
| 1397 | |
| 1398 | void set_restart_main_request(bool restart_main_request) { |
| 1399 | restart_main_request_ = restart_main_request; |
| 1400 | } |
| 1401 | |
| 1402 | void set_cancel_main_request(bool cancel_main_request) { |
| 1403 | cancel_main_request_ = cancel_main_request; |
| 1404 | } |
| 1405 | |
| 1406 | void set_cancel_then_restart_main_request( |
| 1407 | bool cancel_then_restart_main_request) { |
| 1408 | cancel_then_restart_main_request_ = cancel_then_restart_main_request; |
| 1409 | } |
| 1410 | |
| 1411 | void set_simulate_main_network_error(bool simulate_main_network_error) { |
| 1412 | simulate_main_network_error_ = simulate_main_network_error; |
| 1413 | } |
| 1414 | |
| 1415 | void set_intercept_redirect(bool intercept_redirect) { |
| 1416 | intercept_redirect_ = intercept_redirect; |
| 1417 | } |
| 1418 | |
| 1419 | void set_redirect_headers(const std::string& redirect_headers) { |
| 1420 | redirect_headers_ = redirect_headers; |
| 1421 | } |
| 1422 | |
| 1423 | void set_redirect_data(const std::string& redirect_data) { |
| 1424 | redirect_data_ = redirect_data; |
| 1425 | } |
| 1426 | |
| 1427 | void set_cancel_redirect_request(bool cancel_redirect_request) { |
| 1428 | cancel_redirect_request_ = cancel_redirect_request; |
| 1429 | } |
| 1430 | |
| 1431 | void set_intercept_final_response(bool intercept_final_response) { |
| 1432 | intercept_final_response_ = intercept_final_response; |
| 1433 | } |
| 1434 | |
| 1435 | void set_final_headers(const std::string& final_headers) { |
| 1436 | final_headers_ = final_headers; |
| 1437 | } |
| 1438 | |
| 1439 | void set_final_data(const std::string& final_data) { |
| 1440 | final_data_ = final_data; |
| 1441 | } |
| 1442 | |
| 1443 | void set_cancel_final_request(bool cancel_final_request) { |
| 1444 | cancel_final_request_ = cancel_final_request; |
| 1445 | } |
| 1446 | |
| 1447 | void set_use_url_request_http_job(bool use_url_request_http_job) { |
| 1448 | use_url_request_http_job_ = use_url_request_http_job; |
| 1449 | } |
| 1450 | |
| 1451 | bool did_intercept_main() const { |
| 1452 | return did_intercept_main_; |
| 1453 | } |
| 1454 | |
| 1455 | bool did_restart_main() const { |
| 1456 | return did_restart_main_; |
| 1457 | } |
| 1458 | |
| 1459 | bool did_cancel_main() const { |
| 1460 | return did_cancel_main_; |
| 1461 | } |
| 1462 | |
| 1463 | bool did_cancel_then_restart_main() const { |
| 1464 | return did_cancel_then_restart_main_; |
| 1465 | } |
| 1466 | |
| 1467 | bool did_simulate_error_main() const { |
| 1468 | return did_simulate_error_main_; |
| 1469 | } |
| 1470 | |
| 1471 | bool did_intercept_redirect() const { |
| 1472 | return did_intercept_redirect_; |
| 1473 | } |
| 1474 | |
| 1475 | bool did_cancel_redirect() const { |
| 1476 | return did_cancel_redirect_; |
| 1477 | } |
| 1478 | |
| 1479 | bool did_intercept_final() const { |
| 1480 | return did_intercept_final_; |
| 1481 | } |
| 1482 | |
| 1483 | bool did_cancel_final() const { |
| 1484 | return did_cancel_final_; |
| 1485 | } |
| 1486 | |
| 1487 | private: |
| 1488 | // Indicate whether to intercept the main request, and if so specify the |
| 1489 | // response to return and the LoadTimingInfo to use. |
| 1490 | mutable bool intercept_main_request_; |
| 1491 | mutable std::string main_headers_; |
| 1492 | mutable std::string main_data_; |
| 1493 | mutable LoadTimingInfo main_request_load_timing_info_; |
| 1494 | |
| 1495 | // These indicate actions that can be taken within MaybeInterceptRequest. |
| 1496 | mutable bool restart_main_request_; |
| 1497 | mutable bool cancel_main_request_; |
| 1498 | mutable bool cancel_then_restart_main_request_; |
| 1499 | mutable bool simulate_main_network_error_; |
| 1500 | |
| 1501 | // Indicate whether to intercept redirects, and if so specify the response to |
| 1502 | // return. |
| 1503 | mutable bool intercept_redirect_; |
| 1504 | mutable std::string redirect_headers_; |
| 1505 | mutable std::string redirect_data_; |
| 1506 | |
| 1507 | // Cancel the request within MaybeInterceptRedirect. |
| 1508 | mutable bool cancel_redirect_request_; |
| 1509 | |
| 1510 | // Indicate whether to intercept the final response, and if so specify the |
| 1511 | // response to return. |
| 1512 | mutable bool intercept_final_response_; |
| 1513 | mutable std::string final_headers_; |
| 1514 | mutable std::string final_data_; |
| 1515 | |
| 1516 | // Cancel the final request within MaybeInterceptResponse. |
| 1517 | mutable bool cancel_final_request_; |
| 1518 | |
| 1519 | // Instruct the interceptor to use a real URLRequestHTTPJob. |
| 1520 | mutable bool use_url_request_http_job_; |
| 1521 | |
| 1522 | // These indicate if the interceptor did something or not. |
| 1523 | mutable bool did_intercept_main_; |
| 1524 | mutable bool did_restart_main_; |
| 1525 | mutable bool did_cancel_main_; |
| 1526 | mutable bool did_cancel_then_restart_main_; |
| 1527 | mutable bool did_simulate_error_main_; |
| 1528 | mutable bool did_intercept_redirect_; |
| 1529 | mutable bool did_cancel_redirect_; |
| 1530 | mutable bool did_intercept_final_; |
| 1531 | mutable bool did_cancel_final_; |
| 1532 | }; |
| 1533 | |
| 1534 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
| 1535 | class URLRequestInterceptorTest : public URLRequestTest { |
| 1536 | public: |
| 1537 | URLRequestInterceptorTest() : URLRequestTest(), interceptor_(NULL) { |
| 1538 | } |
| 1539 | |
| 1540 | ~URLRequestInterceptorTest() override { |
| 1541 | // URLRequestJobs may post clean-up tasks on destruction. |
| 1542 | base::RunLoop().RunUntilIdle(); |
| 1543 | } |
| 1544 | |
| 1545 | void SetUpFactory() override { |
| 1546 | interceptor_ = new MockURLRequestInterceptor(); |
| 1547 | job_factory_.reset(new URLRequestInterceptingJobFactory( |
| 1548 | job_factory_.Pass(), make_scoped_ptr(interceptor_))); |
| 1549 | } |
| 1550 | |
| 1551 | MockURLRequestInterceptor* interceptor() const { |
| 1552 | return interceptor_; |
| 1553 | } |
| 1554 | |
| 1555 | private: |
| 1556 | MockURLRequestInterceptor* interceptor_; |
| 1557 | }; |
| 1558 | |
| 1559 | TEST_F(URLRequestInterceptorTest, Intercept) { |
| 1560 | // Intercept the main request and respond with a simple response. |
| 1561 | interceptor()->set_intercept_main_request(true); |
| 1562 | interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers()); |
| 1563 | interceptor()->set_main_data(MockURLRequestInterceptor::ok_data()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1564 | TestDelegate d; |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1565 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1566 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1567 | base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data(); |
| 1568 | base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data(); |
| 1569 | base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data(); |
| 1570 | req->SetUserData(nullptr, user_data0); |
| 1571 | req->SetUserData(&user_data1, user_data1); |
| 1572 | req->SetUserData(&user_data2, user_data2); |
| 1573 | req->set_method("GET"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1574 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1575 | base::RunLoop().Run(); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1576 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1577 | // Make sure we can retrieve our specific user data. |
| 1578 | EXPECT_EQ(user_data0, req->GetUserData(nullptr)); |
| 1579 | EXPECT_EQ(user_data1, req->GetUserData(&user_data1)); |
| 1580 | EXPECT_EQ(user_data2, req->GetUserData(&user_data2)); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1581 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1582 | // Check that we got one good response. |
| 1583 | EXPECT_TRUE(req->status().is_success()); |
| 1584 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1585 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1586 | EXPECT_EQ(1, d.response_started_count()); |
| 1587 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1588 | } |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1589 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1590 | TEST_F(URLRequestInterceptorTest, InterceptRedirect) { |
| 1591 | // Intercept the main request and respond with a redirect. |
| 1592 | interceptor()->set_intercept_main_request(true); |
| 1593 | interceptor()->set_main_headers( |
| 1594 | MockURLRequestInterceptor::redirect_headers()); |
| 1595 | interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data()); |
| 1596 | |
| 1597 | // Intercept that redirect and respond with a final OK response. |
| 1598 | interceptor()->set_intercept_redirect(true); |
| 1599 | interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers()); |
| 1600 | interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data()); |
| 1601 | |
| 1602 | TestDelegate d; |
| 1603 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1604 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1605 | req->set_method("GET"); |
| 1606 | req->Start(); |
| 1607 | base::RunLoop().Run(); |
| 1608 | |
| 1609 | // Check that the interceptor got called as expected. |
| 1610 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1611 | EXPECT_TRUE(interceptor()->did_intercept_redirect()); |
| 1612 | |
| 1613 | // Check that we got one good response. |
| 1614 | EXPECT_TRUE(req->status().is_success()); |
| 1615 | if (req->status().is_success()) |
| 1616 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1617 | |
| 1618 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1619 | EXPECT_EQ(1, d.response_started_count()); |
| 1620 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1621 | } |
| 1622 | |
| 1623 | TEST_F(URLRequestInterceptorTest, InterceptServerError) { |
| 1624 | // Intercept the main request to generate a server error response. |
| 1625 | interceptor()->set_intercept_main_request(true); |
| 1626 | interceptor()->set_main_headers(MockURLRequestInterceptor::error_headers()); |
| 1627 | interceptor()->set_main_data(MockURLRequestInterceptor::error_data()); |
| 1628 | |
| 1629 | // Intercept that error and respond with an OK response. |
| 1630 | interceptor()->set_intercept_final_response(true); |
| 1631 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1632 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1633 | |
| 1634 | TestDelegate d; |
| 1635 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1636 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1637 | req->set_method("GET"); |
| 1638 | req->Start(); |
| 1639 | base::RunLoop().Run(); |
| 1640 | |
| 1641 | // Check that the interceptor got called as expected. |
| 1642 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1643 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 1644 | |
| 1645 | // Check that we got one good response. |
| 1646 | EXPECT_TRUE(req->status().is_success()); |
| 1647 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1648 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1649 | EXPECT_EQ(1, d.response_started_count()); |
| 1650 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1651 | } |
| 1652 | |
| 1653 | TEST_F(URLRequestInterceptorTest, InterceptNetworkError) { |
| 1654 | // Intercept the main request to simulate a network error. |
| 1655 | interceptor()->set_simulate_main_network_error(true); |
| 1656 | |
| 1657 | // Intercept that error and respond with an OK response. |
| 1658 | interceptor()->set_intercept_final_response(true); |
| 1659 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1660 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1661 | |
| 1662 | TestDelegate d; |
| 1663 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1664 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1665 | req->set_method("GET"); |
| 1666 | req->Start(); |
| 1667 | base::RunLoop().Run(); |
| 1668 | |
| 1669 | // Check that the interceptor got called as expected. |
| 1670 | EXPECT_TRUE(interceptor()->did_simulate_error_main()); |
| 1671 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 1672 | |
| 1673 | // Check that we received one good response. |
| 1674 | EXPECT_TRUE(req->status().is_success()); |
| 1675 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1676 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1677 | EXPECT_EQ(1, d.response_started_count()); |
| 1678 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1679 | } |
| 1680 | |
| 1681 | TEST_F(URLRequestInterceptorTest, InterceptRestartRequired) { |
| 1682 | // Restart the main request. |
| 1683 | interceptor()->set_restart_main_request(true); |
| 1684 | |
| 1685 | // then intercept the new main request and respond with an OK response |
| 1686 | interceptor()->set_intercept_main_request(true); |
| 1687 | interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers()); |
| 1688 | interceptor()->set_main_data(MockURLRequestInterceptor::ok_data()); |
| 1689 | |
| 1690 | TestDelegate d; |
| 1691 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1692 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1693 | req->set_method("GET"); |
| 1694 | req->Start(); |
| 1695 | base::RunLoop().Run(); |
| 1696 | |
| 1697 | // Check that the interceptor got called as expected. |
| 1698 | EXPECT_TRUE(interceptor()->did_restart_main()); |
| 1699 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1700 | |
| 1701 | // Check that we received one good response. |
| 1702 | EXPECT_TRUE(req->status().is_success()); |
| 1703 | if (req->status().is_success()) |
| 1704 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1705 | |
| 1706 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1707 | EXPECT_EQ(1, d.response_started_count()); |
| 1708 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1709 | } |
| 1710 | |
| 1711 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelMain) { |
| 1712 | // Intercept the main request and cancel from within the restarted job. |
| 1713 | interceptor()->set_cancel_main_request(true); |
| 1714 | |
| 1715 | // Set up to intercept the final response and override it with an OK response. |
| 1716 | interceptor()->set_intercept_final_response(true); |
| 1717 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1718 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1719 | |
| 1720 | TestDelegate d; |
| 1721 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1722 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1723 | req->set_method("GET"); |
| 1724 | req->Start(); |
| 1725 | base::RunLoop().Run(); |
| 1726 | |
| 1727 | // Check that the interceptor got called as expected. |
| 1728 | EXPECT_TRUE(interceptor()->did_cancel_main()); |
| 1729 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 1730 | |
| 1731 | // Check that we see a canceled request. |
| 1732 | EXPECT_FALSE(req->status().is_success()); |
| 1733 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
| 1734 | } |
| 1735 | |
| 1736 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelRedirect) { |
| 1737 | // Intercept the main request and respond with a redirect. |
| 1738 | interceptor()->set_intercept_main_request(true); |
| 1739 | interceptor()->set_main_headers( |
| 1740 | MockURLRequestInterceptor::redirect_headers()); |
| 1741 | interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data()); |
| 1742 | |
| 1743 | // Intercept the redirect and cancel from within that job. |
| 1744 | interceptor()->set_cancel_redirect_request(true); |
| 1745 | |
| 1746 | // Set up to intercept the final response and override it with an OK response. |
| 1747 | interceptor()->set_intercept_final_response(true); |
| 1748 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1749 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1750 | |
| 1751 | TestDelegate d; |
| 1752 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1753 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1754 | req->set_method("GET"); |
| 1755 | req->Start(); |
| 1756 | base::RunLoop().Run(); |
| 1757 | |
| 1758 | // Check that the interceptor got called as expected. |
| 1759 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1760 | EXPECT_TRUE(interceptor()->did_cancel_redirect()); |
| 1761 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 1762 | |
| 1763 | // Check that we see a canceled request. |
| 1764 | EXPECT_FALSE(req->status().is_success()); |
| 1765 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
| 1766 | } |
| 1767 | |
| 1768 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelFinal) { |
| 1769 | // Intercept the main request to simulate a network error. |
| 1770 | interceptor()->set_simulate_main_network_error(true); |
| 1771 | |
| 1772 | // Set up to intercept final the response and cancel from within that job. |
| 1773 | interceptor()->set_cancel_final_request(true); |
| 1774 | |
| 1775 | TestDelegate d; |
| 1776 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1777 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1778 | req->set_method("GET"); |
| 1779 | req->Start(); |
| 1780 | base::RunLoop().Run(); |
| 1781 | |
| 1782 | // Check that the interceptor got called as expected. |
| 1783 | EXPECT_TRUE(interceptor()->did_simulate_error_main()); |
| 1784 | EXPECT_TRUE(interceptor()->did_cancel_final()); |
| 1785 | |
| 1786 | // Check that we see a canceled request. |
| 1787 | EXPECT_FALSE(req->status().is_success()); |
| 1788 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
| 1789 | } |
| 1790 | |
| 1791 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelInRestart) { |
| 1792 | // Intercept the main request and cancel then restart from within that job. |
| 1793 | interceptor()->set_cancel_then_restart_main_request(true); |
| 1794 | |
| 1795 | // Set up to intercept the final response and override it with an OK response. |
| 1796 | interceptor()->set_intercept_final_response(true); |
bengr | b50d631e | 2014-11-17 22:50:50 | [diff] [blame] | 1797 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1798 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1799 | |
| 1800 | TestDelegate d; |
| 1801 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1802 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1803 | req->set_method("GET"); |
| 1804 | req->Start(); |
| 1805 | base::RunLoop().Run(); |
| 1806 | |
| 1807 | // Check that the interceptor got called as expected. |
| 1808 | EXPECT_TRUE(interceptor()->did_cancel_then_restart_main()); |
| 1809 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 1810 | |
| 1811 | // Check that we see a canceled request. |
| 1812 | EXPECT_FALSE(req->status().is_success()); |
| 1813 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | // "Normal" LoadTimingInfo as returned by a job. Everything is in order, not |
| 1817 | // reused. |connect_time_flags| is used to indicate if there should be dns |
| 1818 | // or SSL times, and |used_proxy| is used for proxy times. |
| 1819 | LoadTimingInfo NormalLoadTimingInfo(base::TimeTicks now, |
| 1820 | int connect_time_flags, |
| 1821 | bool used_proxy) { |
| 1822 | LoadTimingInfo load_timing; |
| 1823 | load_timing.socket_log_id = 1; |
| 1824 | |
| 1825 | if (used_proxy) { |
| 1826 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 1827 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 1828 | } |
| 1829 | |
| 1830 | LoadTimingInfo::ConnectTiming& connect_timing = load_timing.connect_timing; |
| 1831 | if (connect_time_flags & CONNECT_TIMING_HAS_DNS_TIMES) { |
| 1832 | connect_timing.dns_start = now + base::TimeDelta::FromDays(3); |
| 1833 | connect_timing.dns_end = now + base::TimeDelta::FromDays(4); |
| 1834 | } |
| 1835 | connect_timing.connect_start = now + base::TimeDelta::FromDays(5); |
| 1836 | if (connect_time_flags & CONNECT_TIMING_HAS_SSL_TIMES) { |
| 1837 | connect_timing.ssl_start = now + base::TimeDelta::FromDays(6); |
| 1838 | connect_timing.ssl_end = now + base::TimeDelta::FromDays(7); |
| 1839 | } |
| 1840 | connect_timing.connect_end = now + base::TimeDelta::FromDays(8); |
| 1841 | |
| 1842 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 1843 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
| 1844 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11); |
| 1845 | return load_timing; |
| 1846 | } |
| 1847 | |
| 1848 | // Same as above, but in the case of a reused socket. |
| 1849 | LoadTimingInfo NormalLoadTimingInfoReused(base::TimeTicks now, |
| 1850 | bool used_proxy) { |
| 1851 | LoadTimingInfo load_timing; |
| 1852 | load_timing.socket_log_id = 1; |
| 1853 | load_timing.socket_reused = true; |
| 1854 | |
| 1855 | if (used_proxy) { |
| 1856 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 1857 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 1858 | } |
| 1859 | |
| 1860 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 1861 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
| 1862 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11); |
| 1863 | return load_timing; |
| 1864 | } |
| 1865 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1866 | LoadTimingInfo RunURLRequestInterceptorLoadTimingTest( |
| 1867 | const LoadTimingInfo& job_load_timing, |
| 1868 | const URLRequestContext& context, |
| 1869 | MockURLRequestInterceptor* interceptor) { |
| 1870 | interceptor->set_intercept_main_request(true); |
| 1871 | interceptor->set_main_request_load_timing_info(job_load_timing); |
| 1872 | TestDelegate d; |
| 1873 | scoped_ptr<URLRequest> req(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1874 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1875 | req->Start(); |
| 1876 | base::RunLoop().Run(); |
| 1877 | |
| 1878 | LoadTimingInfo resulting_load_timing; |
| 1879 | req->GetLoadTimingInfo(&resulting_load_timing); |
| 1880 | |
| 1881 | // None of these should be modified by the URLRequest. |
| 1882 | EXPECT_EQ(job_load_timing.socket_reused, resulting_load_timing.socket_reused); |
| 1883 | EXPECT_EQ(job_load_timing.socket_log_id, resulting_load_timing.socket_log_id); |
| 1884 | EXPECT_EQ(job_load_timing.send_start, resulting_load_timing.send_start); |
| 1885 | EXPECT_EQ(job_load_timing.send_end, resulting_load_timing.send_end); |
| 1886 | EXPECT_EQ(job_load_timing.receive_headers_end, |
| 1887 | resulting_load_timing.receive_headers_end); |
| 1888 | |
| 1889 | return resulting_load_timing; |
| 1890 | } |
| 1891 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1892 | // Basic test that the intercept + load timing tests work. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1893 | TEST_F(URLRequestInterceptorTest, InterceptLoadTiming) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1894 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1895 | LoadTimingInfo job_load_timing = |
| 1896 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, false); |
| 1897 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1898 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1899 | RunURLRequestInterceptorLoadTimingTest( |
| 1900 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1901 | |
| 1902 | // Nothing should have been changed by the URLRequest. |
| 1903 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 1904 | load_timing_result.proxy_resolve_start); |
| 1905 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 1906 | load_timing_result.proxy_resolve_end); |
| 1907 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 1908 | load_timing_result.connect_timing.dns_start); |
| 1909 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 1910 | load_timing_result.connect_timing.dns_end); |
| 1911 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 1912 | load_timing_result.connect_timing.connect_start); |
| 1913 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 1914 | load_timing_result.connect_timing.connect_end); |
| 1915 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 1916 | load_timing_result.connect_timing.ssl_start); |
| 1917 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 1918 | load_timing_result.connect_timing.ssl_end); |
| 1919 | |
| 1920 | // Redundant sanity check. |
| 1921 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_DNS_TIMES); |
| 1922 | } |
| 1923 | |
| 1924 | // Another basic test, with proxy and SSL times, but no DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1925 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingProxy) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1926 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1927 | LoadTimingInfo job_load_timing = |
| 1928 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, true); |
| 1929 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1930 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1931 | RunURLRequestInterceptorLoadTimingTest( |
| 1932 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1933 | |
| 1934 | // Nothing should have been changed by the URLRequest. |
| 1935 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 1936 | load_timing_result.proxy_resolve_start); |
| 1937 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 1938 | load_timing_result.proxy_resolve_end); |
| 1939 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 1940 | load_timing_result.connect_timing.dns_start); |
| 1941 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 1942 | load_timing_result.connect_timing.dns_end); |
| 1943 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 1944 | load_timing_result.connect_timing.connect_start); |
| 1945 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 1946 | load_timing_result.connect_timing.connect_end); |
| 1947 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 1948 | load_timing_result.connect_timing.ssl_start); |
| 1949 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 1950 | load_timing_result.connect_timing.ssl_end); |
| 1951 | |
| 1952 | // Redundant sanity check. |
| 1953 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 1954 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 1955 | } |
| 1956 | |
| 1957 | // Make sure that URLRequest correctly adjusts proxy times when they're before |
| 1958 | // |request_start|, due to already having a connected socket. This happens in |
[email protected] | cf4cae3 | 2014-05-27 00:39:10 | [diff] [blame] | 1959 | // the case of reusing a SPDY session. The connected socket is not considered |
| 1960 | // reused in this test (May be a preconnect). |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1961 | // |
| 1962 | // To mix things up from the test above, assumes DNS times but no SSL times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1963 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyProxyResolution) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1964 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1965 | LoadTimingInfo job_load_timing = |
| 1966 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, true); |
| 1967 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(6); |
| 1968 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(5); |
| 1969 | job_load_timing.connect_timing.dns_start = now - base::TimeDelta::FromDays(4); |
| 1970 | job_load_timing.connect_timing.dns_end = now - base::TimeDelta::FromDays(3); |
| 1971 | job_load_timing.connect_timing.connect_start = |
| 1972 | now - base::TimeDelta::FromDays(2); |
| 1973 | job_load_timing.connect_timing.connect_end = |
| 1974 | now - base::TimeDelta::FromDays(1); |
| 1975 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1976 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1977 | RunURLRequestInterceptorLoadTimingTest( |
| 1978 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1979 | |
| 1980 | // Proxy times, connect times, and DNS times should all be replaced with |
| 1981 | // request_start. |
| 1982 | EXPECT_EQ(load_timing_result.request_start, |
| 1983 | load_timing_result.proxy_resolve_start); |
| 1984 | EXPECT_EQ(load_timing_result.request_start, |
| 1985 | load_timing_result.proxy_resolve_end); |
| 1986 | EXPECT_EQ(load_timing_result.request_start, |
| 1987 | load_timing_result.connect_timing.dns_start); |
| 1988 | EXPECT_EQ(load_timing_result.request_start, |
| 1989 | load_timing_result.connect_timing.dns_end); |
| 1990 | EXPECT_EQ(load_timing_result.request_start, |
| 1991 | load_timing_result.connect_timing.connect_start); |
| 1992 | EXPECT_EQ(load_timing_result.request_start, |
| 1993 | load_timing_result.connect_timing.connect_end); |
| 1994 | |
| 1995 | // Other times should have been left null. |
| 1996 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 1997 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 1998 | } |
| 1999 | |
| 2000 | // Same as above, but in the reused case. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2001 | TEST_F(URLRequestInterceptorTest, |
| 2002 | InterceptLoadTimingEarlyProxyResolutionReused) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2003 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2004 | LoadTimingInfo job_load_timing = NormalLoadTimingInfoReused(now, true); |
| 2005 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(4); |
| 2006 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(3); |
| 2007 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2008 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2009 | RunURLRequestInterceptorLoadTimingTest( |
| 2010 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2011 | |
| 2012 | // Proxy times and connect times should all be replaced with request_start. |
| 2013 | EXPECT_EQ(load_timing_result.request_start, |
| 2014 | load_timing_result.proxy_resolve_start); |
| 2015 | EXPECT_EQ(load_timing_result.request_start, |
| 2016 | load_timing_result.proxy_resolve_end); |
| 2017 | |
| 2018 | // Other times should have been left null. |
| 2019 | TestLoadTimingReusedWithProxy(load_timing_result); |
| 2020 | } |
| 2021 | |
| 2022 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 2023 | // |request_start|, due to reusing a connected socket. The connected socket is |
| 2024 | // not considered reused in this test (May be a preconnect). |
| 2025 | // |
| 2026 | // To mix things up, the request has SSL times, but no DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2027 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnect) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2028 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2029 | LoadTimingInfo job_load_timing = |
| 2030 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, false); |
| 2031 | job_load_timing.connect_timing.connect_start = |
| 2032 | now - base::TimeDelta::FromDays(1); |
| 2033 | job_load_timing.connect_timing.ssl_start = now - base::TimeDelta::FromDays(2); |
| 2034 | job_load_timing.connect_timing.ssl_end = now - base::TimeDelta::FromDays(3); |
| 2035 | job_load_timing.connect_timing.connect_end = |
| 2036 | now - base::TimeDelta::FromDays(4); |
| 2037 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2038 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2039 | RunURLRequestInterceptorLoadTimingTest( |
| 2040 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2041 | |
| 2042 | // Connect times, and SSL times should be replaced with request_start. |
| 2043 | EXPECT_EQ(load_timing_result.request_start, |
| 2044 | load_timing_result.connect_timing.connect_start); |
| 2045 | EXPECT_EQ(load_timing_result.request_start, |
| 2046 | load_timing_result.connect_timing.ssl_start); |
| 2047 | EXPECT_EQ(load_timing_result.request_start, |
| 2048 | load_timing_result.connect_timing.ssl_end); |
| 2049 | EXPECT_EQ(load_timing_result.request_start, |
| 2050 | load_timing_result.connect_timing.connect_end); |
| 2051 | |
| 2052 | // Other times should have been left null. |
| 2053 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_SSL_TIMES); |
| 2054 | } |
| 2055 | |
| 2056 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 2057 | // |request_start|, due to reusing a connected socket in the case that there |
| 2058 | // are also proxy times. The connected socket is not considered reused in this |
| 2059 | // test (May be a preconnect). |
| 2060 | // |
| 2061 | // In this test, there are no SSL or DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2062 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnectWithProxy) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2063 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2064 | LoadTimingInfo job_load_timing = |
| 2065 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY, true); |
| 2066 | job_load_timing.connect_timing.connect_start = |
| 2067 | now - base::TimeDelta::FromDays(1); |
| 2068 | job_load_timing.connect_timing.connect_end = |
| 2069 | now - base::TimeDelta::FromDays(2); |
| 2070 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2071 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2072 | RunURLRequestInterceptorLoadTimingTest( |
| 2073 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 2074 | |
| 2075 | // Connect times should be replaced with proxy_resolve_end. |
| 2076 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 2077 | load_timing_result.connect_timing.connect_start); |
| 2078 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 2079 | load_timing_result.connect_timing.connect_end); |
| 2080 | |
| 2081 | // Other times should have been left null. |
| 2082 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2083 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 2084 | } |
| 2085 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2086 | // Check that two different URL requests have different identifiers. |
| 2087 | TEST_F(URLRequestTest, Identifiers) { |
| 2088 | TestDelegate d; |
| 2089 | TestURLRequestContext context; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2090 | scoped_ptr<URLRequest> req( |
| 2091 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d)); |
| 2092 | scoped_ptr<URLRequest> other_req( |
| 2093 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2094 | |
mmenke | 19378d2 | 2014-09-09 04:12:59 | [diff] [blame] | 2095 | ASSERT_NE(req->identifier(), other_req->identifier()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2096 | } |
| 2097 | |
| 2098 | // Check that a failure to connect to the proxy is reported to the network |
| 2099 | // delegate. |
| 2100 | TEST_F(URLRequestTest, NetworkDelegateProxyError) { |
| 2101 | MockHostResolver host_resolver; |
| 2102 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 2103 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 2104 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2105 | TestURLRequestContextWithProxy context("myproxy:70", &network_delegate); |
| 2106 | |
| 2107 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2108 | scoped_ptr<URLRequest> req( |
| 2109 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2110 | req->set_method("GET"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2111 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2112 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2113 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2114 | |
| 2115 | // Check we see a failed request. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2116 | EXPECT_FALSE(req->status().is_success()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 2117 | // The proxy server is not set before failure. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2118 | EXPECT_TRUE(req->proxy_server().IsEmpty()); |
| 2119 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
| 2120 | EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, req->status().error()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2121 | |
| 2122 | EXPECT_EQ(1, network_delegate.error_count()); |
| 2123 | EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, network_delegate.last_error()); |
| 2124 | EXPECT_EQ(1, network_delegate.completed_requests()); |
| 2125 | } |
| 2126 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 2127 | // Make sure that NetworkDelegate::NotifyCompleted is called if |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2128 | // content is empty. |
| 2129 | TEST_F(URLRequestTest, RequestCompletionForEmptyResponse) { |
| 2130 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2131 | scoped_ptr<URLRequest> req( |
| 2132 | default_context_.CreateRequest(GURL("data:,"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2133 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2134 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2135 | EXPECT_EQ("", d.data_received()); |
| 2136 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 2137 | } |
| 2138 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2139 | // Make sure that SetPriority actually sets the URLRequest's priority |
| 2140 | // correctly, both before and after start. |
| 2141 | TEST_F(URLRequestTest, SetPriorityBasic) { |
| 2142 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2143 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2144 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2145 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2146 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2147 | req->SetPriority(LOW); |
| 2148 | EXPECT_EQ(LOW, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2149 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2150 | req->Start(); |
| 2151 | EXPECT_EQ(LOW, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2152 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2153 | req->SetPriority(MEDIUM); |
| 2154 | EXPECT_EQ(MEDIUM, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2155 | } |
| 2156 | |
| 2157 | // Make sure that URLRequest calls SetPriority on a job before calling |
| 2158 | // Start on it. |
| 2159 | TEST_F(URLRequestTest, SetJobPriorityBeforeJobStart) { |
| 2160 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2161 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2162 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2163 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2164 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2165 | RequestPriority job_priority; |
| 2166 | scoped_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
| 2167 | req.get(), &default_network_delegate_, &job_priority)); |
| 2168 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
| 2169 | EXPECT_EQ(DEFAULT_PRIORITY, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2170 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2171 | req->SetPriority(LOW); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2172 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2173 | req->Start(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2174 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2175 | } |
| 2176 | |
| 2177 | // Make sure that URLRequest passes on its priority updates to its |
| 2178 | // job. |
| 2179 | TEST_F(URLRequestTest, SetJobPriority) { |
| 2180 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2181 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2182 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2183 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2184 | RequestPriority job_priority; |
| 2185 | scoped_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
| 2186 | req.get(), &default_network_delegate_, &job_priority)); |
| 2187 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2188 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2189 | req->SetPriority(LOW); |
| 2190 | req->Start(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2191 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2192 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2193 | req->SetPriority(MEDIUM); |
| 2194 | EXPECT_EQ(MEDIUM, req->priority()); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2195 | EXPECT_EQ(MEDIUM, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2196 | } |
| 2197 | |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2198 | // Setting the IGNORE_LIMITS load flag should be okay if the priority |
| 2199 | // is MAXIMUM_PRIORITY. |
| 2200 | TEST_F(URLRequestTest, PriorityIgnoreLimits) { |
| 2201 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2202 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2203 | GURL("http://test_intercept/foo"), MAXIMUM_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2204 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2205 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2206 | RequestPriority job_priority; |
| 2207 | scoped_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
| 2208 | req.get(), &default_network_delegate_, &job_priority)); |
| 2209 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2210 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2211 | req->SetLoadFlags(LOAD_IGNORE_LIMITS); |
| 2212 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2213 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2214 | req->SetPriority(MAXIMUM_PRIORITY); |
| 2215 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2216 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2217 | req->Start(); |
| 2218 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 2219 | EXPECT_EQ(MAXIMUM_PRIORITY, job_priority); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2220 | } |
| 2221 | |
nick | 5d570de9 | 2015-05-04 20:16:16 | [diff] [blame] | 2222 | namespace { |
| 2223 | |
| 2224 | // Less verbose way of running a simple testserver for the tests below. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2225 | class LocalHttpTestServer : public EmbeddedTestServer { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2226 | public: |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2227 | explicit LocalHttpTestServer(const base::FilePath& document_root) { |
| 2228 | AddDefaultHandlers(document_root); |
tommycli | eae5f75f | 2015-11-05 19:07:27 | [diff] [blame] | 2229 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2230 | |
| 2231 | LocalHttpTestServer() { AddDefaultHandlers(base::FilePath()); } |
| 2232 | }; |
tommycli | eae5f75f | 2015-11-05 19:07:27 | [diff] [blame] | 2233 | |
nick | 5d570de9 | 2015-05-04 20:16:16 | [diff] [blame] | 2234 | } // namespace |
| 2235 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2236 | TEST_F(URLRequestTest, DelayedCookieCallback) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2237 | LocalHttpTestServer test_server; |
| 2238 | ASSERT_TRUE(test_server.Start()); |
| 2239 | |
| 2240 | TestURLRequestContext context; |
| 2241 | scoped_refptr<DelayedCookieMonster> delayed_cm = |
| 2242 | new DelayedCookieMonster(); |
| 2243 | scoped_refptr<CookieStore> cookie_store = delayed_cm; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 2244 | context.set_cookie_store(delayed_cm.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2245 | |
| 2246 | // Set up a cookie. |
| 2247 | { |
| 2248 | TestNetworkDelegate network_delegate; |
| 2249 | context.set_network_delegate(&network_delegate); |
| 2250 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2251 | scoped_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2252 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2253 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2254 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2255 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2256 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2257 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2258 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2259 | } |
| 2260 | |
| 2261 | // Verify that the cookie is set. |
| 2262 | { |
| 2263 | TestNetworkDelegate network_delegate; |
| 2264 | context.set_network_delegate(&network_delegate); |
| 2265 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2266 | scoped_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2267 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2268 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2269 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2270 | |
| 2271 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2272 | != std::string::npos); |
| 2273 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2274 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2275 | } |
| 2276 | } |
| 2277 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2278 | TEST_F(URLRequestTest, DoNotSendCookies) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2279 | LocalHttpTestServer test_server; |
| 2280 | ASSERT_TRUE(test_server.Start()); |
| 2281 | |
| 2282 | // Set up a cookie. |
| 2283 | { |
| 2284 | TestNetworkDelegate network_delegate; |
| 2285 | default_context_.set_network_delegate(&network_delegate); |
| 2286 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2287 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2288 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2289 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2290 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2291 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2292 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2293 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2294 | } |
| 2295 | |
| 2296 | // Verify that the cookie is set. |
| 2297 | { |
| 2298 | TestNetworkDelegate network_delegate; |
| 2299 | default_context_.set_network_delegate(&network_delegate); |
| 2300 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2301 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2302 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2303 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2304 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2305 | |
| 2306 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2307 | != std::string::npos); |
| 2308 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2309 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2310 | } |
| 2311 | |
| 2312 | // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set. |
| 2313 | { |
| 2314 | TestNetworkDelegate network_delegate; |
| 2315 | default_context_.set_network_delegate(&network_delegate); |
| 2316 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2317 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2318 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2319 | req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES); |
| 2320 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2321 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2322 | |
| 2323 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2324 | == std::string::npos); |
| 2325 | |
| 2326 | // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies. |
| 2327 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2328 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2329 | } |
| 2330 | } |
| 2331 | |
| 2332 | TEST_F(URLRequestTest, DoNotSaveCookies) { |
| 2333 | LocalHttpTestServer test_server; |
| 2334 | ASSERT_TRUE(test_server.Start()); |
| 2335 | |
| 2336 | // Set up a cookie. |
| 2337 | { |
| 2338 | TestNetworkDelegate network_delegate; |
| 2339 | default_context_.set_network_delegate(&network_delegate); |
| 2340 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2341 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2342 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2343 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2344 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2345 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2346 | |
| 2347 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2348 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2349 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2350 | } |
| 2351 | |
| 2352 | // Try to set-up another cookie and update the previous cookie. |
| 2353 | { |
| 2354 | TestNetworkDelegate network_delegate; |
| 2355 | default_context_.set_network_delegate(&network_delegate); |
| 2356 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2357 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2358 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2359 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2360 | req->SetLoadFlags(LOAD_DO_NOT_SAVE_COOKIES); |
| 2361 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2362 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2363 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2364 | |
| 2365 | // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie. |
| 2366 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2367 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2368 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2369 | } |
| 2370 | |
| 2371 | // Verify the cookies weren't saved or updated. |
| 2372 | { |
| 2373 | TestNetworkDelegate network_delegate; |
| 2374 | default_context_.set_network_delegate(&network_delegate); |
| 2375 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2376 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2377 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2378 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2379 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2380 | |
| 2381 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2382 | == std::string::npos); |
| 2383 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2384 | != std::string::npos); |
| 2385 | |
| 2386 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2387 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2388 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2389 | } |
| 2390 | } |
| 2391 | |
| 2392 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) { |
| 2393 | LocalHttpTestServer test_server; |
| 2394 | ASSERT_TRUE(test_server.Start()); |
| 2395 | |
| 2396 | // Set up a cookie. |
| 2397 | { |
| 2398 | TestNetworkDelegate network_delegate; |
| 2399 | default_context_.set_network_delegate(&network_delegate); |
| 2400 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2401 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2402 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2403 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2404 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2405 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2406 | |
| 2407 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2408 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2409 | } |
| 2410 | |
| 2411 | // Verify that the cookie is set. |
| 2412 | { |
| 2413 | TestNetworkDelegate network_delegate; |
| 2414 | default_context_.set_network_delegate(&network_delegate); |
| 2415 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2416 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2417 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2418 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2419 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2420 | |
| 2421 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2422 | != std::string::npos); |
| 2423 | |
| 2424 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2425 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2426 | } |
| 2427 | |
| 2428 | // Verify that the cookie isn't sent. |
| 2429 | { |
| 2430 | TestNetworkDelegate network_delegate; |
| 2431 | default_context_.set_network_delegate(&network_delegate); |
| 2432 | TestDelegate d; |
| 2433 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2434 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2435 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2436 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2437 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2438 | |
| 2439 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2440 | == std::string::npos); |
| 2441 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 2442 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2443 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2444 | } |
| 2445 | } |
| 2446 | |
marq | f14fff8d | 2015-12-02 15:52:29 | [diff] [blame] | 2447 | // TODO(crbug.com/564656) This test is flaky on iOS. |
| 2448 | #if defined(OS_IOS) |
| 2449 | #define MAYBE_DoNotSaveCookies_ViaPolicy FLAKY_DoNotSaveCookies_ViaPolicy |
| 2450 | #else |
| 2451 | #define MAYBE_DoNotSaveCookies_ViaPolicy DoNotSaveCookies_ViaPolicy |
| 2452 | #endif |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2453 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) { |
| 2454 | LocalHttpTestServer test_server; |
| 2455 | ASSERT_TRUE(test_server.Start()); |
| 2456 | |
| 2457 | // Set up a cookie. |
| 2458 | { |
| 2459 | TestNetworkDelegate network_delegate; |
| 2460 | default_context_.set_network_delegate(&network_delegate); |
| 2461 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2462 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2463 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2464 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2465 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2466 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2467 | |
| 2468 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2469 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2470 | } |
| 2471 | |
| 2472 | // Try to set-up another cookie and update the previous cookie. |
| 2473 | { |
| 2474 | TestNetworkDelegate network_delegate; |
| 2475 | default_context_.set_network_delegate(&network_delegate); |
| 2476 | TestDelegate d; |
| 2477 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2478 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2479 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2480 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2481 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2482 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2483 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2484 | |
| 2485 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2486 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
| 2487 | } |
| 2488 | |
| 2489 | // Verify the cookies weren't saved or updated. |
| 2490 | { |
| 2491 | TestNetworkDelegate network_delegate; |
| 2492 | default_context_.set_network_delegate(&network_delegate); |
| 2493 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2494 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2495 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2496 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2497 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2498 | |
| 2499 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2500 | == std::string::npos); |
| 2501 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2502 | != std::string::npos); |
| 2503 | |
| 2504 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2505 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2506 | } |
| 2507 | } |
| 2508 | |
| 2509 | TEST_F(URLRequestTest, DoNotSaveEmptyCookies) { |
| 2510 | LocalHttpTestServer test_server; |
| 2511 | ASSERT_TRUE(test_server.Start()); |
| 2512 | |
| 2513 | // Set up an empty cookie. |
| 2514 | { |
| 2515 | TestNetworkDelegate network_delegate; |
| 2516 | default_context_.set_network_delegate(&network_delegate); |
| 2517 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2518 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2519 | test_server.GetURL("/set-cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2520 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2521 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2522 | |
| 2523 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2524 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2525 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2526 | } |
| 2527 | } |
| 2528 | |
| 2529 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) { |
| 2530 | LocalHttpTestServer test_server; |
| 2531 | ASSERT_TRUE(test_server.Start()); |
| 2532 | |
| 2533 | // Set up a cookie. |
| 2534 | { |
| 2535 | TestNetworkDelegate network_delegate; |
| 2536 | default_context_.set_network_delegate(&network_delegate); |
| 2537 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2538 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2539 | test_server.GetURL("/set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2540 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2541 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2542 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2543 | |
| 2544 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2545 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2546 | } |
| 2547 | |
| 2548 | // Verify that the cookie is set. |
| 2549 | { |
| 2550 | TestNetworkDelegate network_delegate; |
| 2551 | default_context_.set_network_delegate(&network_delegate); |
| 2552 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2553 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2554 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2555 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2556 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2557 | |
| 2558 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2559 | != std::string::npos); |
| 2560 | |
| 2561 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2562 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2563 | } |
| 2564 | |
| 2565 | // Verify that the cookie isn't sent. |
| 2566 | { |
| 2567 | TestNetworkDelegate network_delegate; |
| 2568 | default_context_.set_network_delegate(&network_delegate); |
| 2569 | TestDelegate d; |
| 2570 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2571 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2572 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2573 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2574 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2575 | |
| 2576 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2577 | == std::string::npos); |
| 2578 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 2579 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2580 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2581 | } |
| 2582 | } |
| 2583 | |
| 2584 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) { |
| 2585 | LocalHttpTestServer test_server; |
| 2586 | ASSERT_TRUE(test_server.Start()); |
| 2587 | |
| 2588 | // Set up a cookie. |
| 2589 | { |
| 2590 | TestNetworkDelegate network_delegate; |
| 2591 | default_context_.set_network_delegate(&network_delegate); |
| 2592 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2593 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2594 | test_server.GetURL("/set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2595 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2596 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2597 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2598 | |
| 2599 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2600 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2601 | } |
| 2602 | |
| 2603 | // Try to set-up another cookie and update the previous cookie. |
| 2604 | { |
| 2605 | TestNetworkDelegate network_delegate; |
| 2606 | default_context_.set_network_delegate(&network_delegate); |
| 2607 | TestDelegate d; |
| 2608 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2609 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2610 | test_server.GetURL("/set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2611 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2612 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2613 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2614 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2615 | |
| 2616 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2617 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
| 2618 | } |
| 2619 | |
| 2620 | // Verify the cookies weren't saved or updated. |
| 2621 | { |
| 2622 | TestNetworkDelegate network_delegate; |
| 2623 | default_context_.set_network_delegate(&network_delegate); |
| 2624 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2625 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2626 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2627 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2628 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2629 | |
| 2630 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2631 | == std::string::npos); |
| 2632 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2633 | != std::string::npos); |
| 2634 | |
| 2635 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2636 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2637 | } |
| 2638 | } |
| 2639 | |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2640 | TEST_F(URLRequestTest, FirstPartyOnlyCookiesEnabled) { |
| 2641 | LocalHttpTestServer test_server; |
| 2642 | ASSERT_TRUE(test_server.Start()); |
| 2643 | |
| 2644 | // Set up a 'First-Party-Only' cookie (on '127.0.0.1', as that's where |
| 2645 | // LocalHttpTestServer points). |
| 2646 | { |
| 2647 | TestNetworkDelegate network_delegate; |
estark | 7625d81 | 2015-10-12 20:10:41 | [diff] [blame] | 2648 | network_delegate.set_experimental_cookie_features_enabled(true); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2649 | default_context_.set_network_delegate(&network_delegate); |
| 2650 | |
| 2651 | TestDelegate d; |
| 2652 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2653 | test_server.GetURL( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2654 | "/set-cookie?FirstPartyCookieToSet=1;First-Party-Only"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2655 | DEFAULT_PRIORITY, &d)); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2656 | req->Start(); |
| 2657 | base::RunLoop().Run(); |
| 2658 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2659 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2660 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2661 | } |
| 2662 | |
| 2663 | // Verify that the cookie is sent for first-party requests. |
| 2664 | { |
| 2665 | TestNetworkDelegate network_delegate; |
estark | 7625d81 | 2015-10-12 20:10:41 | [diff] [blame] | 2666 | network_delegate.set_experimental_cookie_features_enabled(true); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2667 | default_context_.set_network_delegate(&network_delegate); |
| 2668 | TestDelegate d; |
| 2669 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2670 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
| 2671 | req->set_first_party_for_cookies(test_server.GetURL("/")); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2672 | req->Start(); |
| 2673 | base::RunLoop().Run(); |
| 2674 | |
| 2675 | EXPECT_TRUE(d.data_received().find("FirstPartyCookieToSet=1") != |
| 2676 | std::string::npos); |
| 2677 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2678 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2679 | } |
| 2680 | |
| 2681 | // Verify that the cookie is not-sent for non-first-party requests. |
| 2682 | { |
| 2683 | TestNetworkDelegate network_delegate; |
estark | 7625d81 | 2015-10-12 20:10:41 | [diff] [blame] | 2684 | network_delegate.set_experimental_cookie_features_enabled(true); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2685 | default_context_.set_network_delegate(&network_delegate); |
| 2686 | TestDelegate d; |
| 2687 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2688 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2689 | req->set_first_party_for_cookies(GURL("http://third-party.test/")); |
| 2690 | req->Start(); |
| 2691 | base::RunLoop().Run(); |
| 2692 | |
| 2693 | EXPECT_TRUE(d.data_received().find("FirstPartyCookieToSet=1") == |
| 2694 | std::string::npos); |
| 2695 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2696 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2697 | } |
| 2698 | } |
| 2699 | |
mkwst | 0513c9d | 2015-04-01 05:53:15 | [diff] [blame] | 2700 | TEST_F(URLRequestTest, FirstPartyOnlyCookiesDisabled) { |
| 2701 | LocalHttpTestServer test_server; |
| 2702 | ASSERT_TRUE(test_server.Start()); |
| 2703 | |
| 2704 | // Set up a 'First-Party-Only' cookie (on '127.0.0.1', as that's where |
| 2705 | // LocalHttpTestServer points). |
| 2706 | { |
| 2707 | TestNetworkDelegate network_delegate; |
estark | 7625d81 | 2015-10-12 20:10:41 | [diff] [blame] | 2708 | network_delegate.set_experimental_cookie_features_enabled(false); |
mkwst | 0513c9d | 2015-04-01 05:53:15 | [diff] [blame] | 2709 | default_context_.set_network_delegate(&network_delegate); |
| 2710 | |
| 2711 | TestDelegate d; |
| 2712 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2713 | test_server.GetURL( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2714 | "/set-cookie?FirstPartyCookieToSet=1;First-Party-Only"), |
mkwst | 0513c9d | 2015-04-01 05:53:15 | [diff] [blame] | 2715 | DEFAULT_PRIORITY, &d)); |
| 2716 | req->Start(); |
| 2717 | base::RunLoop().Run(); |
| 2718 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2719 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2720 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2721 | } |
| 2722 | |
| 2723 | // Verify that the cookie is sent for first-party requests. |
| 2724 | { |
| 2725 | TestNetworkDelegate network_delegate; |
estark | 7625d81 | 2015-10-12 20:10:41 | [diff] [blame] | 2726 | network_delegate.set_experimental_cookie_features_enabled(false); |
mkwst | 0513c9d | 2015-04-01 05:53:15 | [diff] [blame] | 2727 | default_context_.set_network_delegate(&network_delegate); |
| 2728 | TestDelegate d; |
| 2729 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2730 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
| 2731 | req->set_first_party_for_cookies(test_server.GetURL("/")); |
mkwst | 0513c9d | 2015-04-01 05:53:15 | [diff] [blame] | 2732 | req->Start(); |
| 2733 | base::RunLoop().Run(); |
| 2734 | |
| 2735 | EXPECT_TRUE(d.data_received().find("FirstPartyCookieToSet=1") != |
| 2736 | std::string::npos); |
| 2737 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2738 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2739 | } |
| 2740 | |
| 2741 | // Verify that the cookie is also sent for non-first-party requests. |
| 2742 | { |
| 2743 | TestNetworkDelegate network_delegate; |
estark | 7625d81 | 2015-10-12 20:10:41 | [diff] [blame] | 2744 | network_delegate.set_experimental_cookie_features_enabled(false); |
mkwst | 0513c9d | 2015-04-01 05:53:15 | [diff] [blame] | 2745 | default_context_.set_network_delegate(&network_delegate); |
| 2746 | TestDelegate d; |
| 2747 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2748 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
mkwst | 0513c9d | 2015-04-01 05:53:15 | [diff] [blame] | 2749 | req->set_first_party_for_cookies(GURL("http://third-party.test/")); |
| 2750 | req->Start(); |
| 2751 | base::RunLoop().Run(); |
| 2752 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 2753 | EXPECT_NE(d.data_received().find("FirstPartyCookieToSet=1"), |
| 2754 | std::string::npos); |
mkwst | 0513c9d | 2015-04-01 05:53:15 | [diff] [blame] | 2755 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2756 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2757 | } |
| 2758 | } |
| 2759 | |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 2760 | // Tests that __Secure- cookies can't be set on non-secure origins. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2761 | TEST_F(URLRequestTest, SecureCookiePrefixOnNonsecureOrigin) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2762 | EmbeddedTestServer http_server; |
| 2763 | http_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2764 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2765 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 2766 | https_server.AddDefaultHandlers( |
| 2767 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 2768 | ASSERT_TRUE(http_server.Start()); |
| 2769 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2770 | |
| 2771 | TestExperimentalFeaturesNetworkDelegate network_delegate; |
| 2772 | TestURLRequestContext context(true); |
| 2773 | context.set_network_delegate(&network_delegate); |
| 2774 | context.Init(); |
| 2775 | |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 2776 | // Try to set a Secure __Secure- cookie, with experimental features |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2777 | // enabled. |
| 2778 | { |
| 2779 | TestDelegate d; |
| 2780 | scoped_ptr<URLRequest> req(context.CreateRequest( |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 2781 | http_server.GetURL("/set-cookie?__Secure-nonsecure-origin=1;Secure"), |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2782 | DEFAULT_PRIORITY, &d)); |
| 2783 | req->Start(); |
| 2784 | base::RunLoop().Run(); |
| 2785 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2786 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2787 | } |
| 2788 | |
| 2789 | // Verify that the cookie is not set. |
| 2790 | { |
| 2791 | TestDelegate d; |
| 2792 | scoped_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2793 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2794 | req->Start(); |
| 2795 | base::RunLoop().Run(); |
| 2796 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 2797 | EXPECT_EQ(d.data_received().find("__Secure-nonsecure-origin=1"), |
| 2798 | std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2799 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2800 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2801 | } |
| 2802 | } |
| 2803 | |
| 2804 | TEST_F(URLRequestTest, SecureCookiePrefixNonexperimental) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2805 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 2806 | https_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2807 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2808 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2809 | |
| 2810 | TestNetworkDelegate network_delegate; |
| 2811 | TestURLRequestContext context(true); |
| 2812 | context.set_network_delegate(&network_delegate); |
| 2813 | context.Init(); |
| 2814 | |
| 2815 | // Without experimental features, there should be no restrictions on |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 2816 | // __Secure- cookies. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2817 | |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 2818 | // Set a non-Secure cookie with the __Secure- prefix. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2819 | { |
| 2820 | TestDelegate d; |
| 2821 | scoped_ptr<URLRequest> req(context.CreateRequest( |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 2822 | https_server.GetURL( |
| 2823 | "/set-cookie?__Secure-nonsecure-not-experimental=1"), |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2824 | DEFAULT_PRIORITY, &d)); |
| 2825 | req->Start(); |
| 2826 | base::RunLoop().Run(); |
| 2827 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2828 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2829 | } |
| 2830 | |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 2831 | // Set a Secure cookie with the __Secure- prefix. |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2832 | { |
| 2833 | TestDelegate d; |
| 2834 | scoped_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2835 | https_server.GetURL( |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 2836 | "/set-cookie?__Secure-secure-not-experimental=1;Secure"), |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2837 | DEFAULT_PRIORITY, &d)); |
| 2838 | req->Start(); |
| 2839 | base::RunLoop().Run(); |
| 2840 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2841 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2842 | } |
| 2843 | |
| 2844 | // Verify that the cookies are set. Neither should have any |
| 2845 | // restrictions because the experimental flag is off. |
| 2846 | { |
| 2847 | TestDelegate d; |
| 2848 | scoped_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2849 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2850 | req->Start(); |
| 2851 | base::RunLoop().Run(); |
| 2852 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 2853 | EXPECT_NE(d.data_received().find("__Secure-secure-not-experimental=1"), |
| 2854 | std::string::npos); |
| 2855 | EXPECT_NE(d.data_received().find("__Secure-nonsecure-not-experimental=1"), |
| 2856 | std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2857 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2858 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2859 | } |
| 2860 | } |
| 2861 | |
| 2862 | TEST_F(URLRequestTest, SecureCookiePrefixExperimentalNonsecure) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2863 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 2864 | https_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2865 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2866 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2867 | |
| 2868 | TestExperimentalFeaturesNetworkDelegate network_delegate; |
| 2869 | TestURLRequestContext context(true); |
| 2870 | context.set_network_delegate(&network_delegate); |
| 2871 | context.Init(); |
| 2872 | |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 2873 | // Try to set a non-Secure __Secure- cookie, with experimental features |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2874 | // enabled. |
| 2875 | { |
| 2876 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2877 | scoped_ptr<URLRequest> req( |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 2878 | context.CreateRequest(https_server.GetURL("/set-cookie?__Secure-foo=1"), |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2879 | DEFAULT_PRIORITY, &d)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2880 | req->Start(); |
| 2881 | base::RunLoop().Run(); |
| 2882 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2883 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2884 | } |
| 2885 | |
| 2886 | // Verify that the cookie is not set. |
| 2887 | { |
| 2888 | TestDelegate d; |
| 2889 | scoped_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2890 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2891 | req->Start(); |
| 2892 | base::RunLoop().Run(); |
| 2893 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 2894 | EXPECT_EQ(d.data_received().find("__Secure-foo=1"), std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2895 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2896 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2897 | } |
| 2898 | } |
| 2899 | |
| 2900 | TEST_F(URLRequestTest, SecureCookiePrefixExperimentalSecure) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2901 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 2902 | https_server.AddDefaultHandlers( |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2903 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2904 | ASSERT_TRUE(https_server.Start()); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2905 | |
| 2906 | TestExperimentalFeaturesNetworkDelegate network_delegate; |
| 2907 | TestURLRequestContext context(true); |
| 2908 | context.set_network_delegate(&network_delegate); |
| 2909 | context.Init(); |
| 2910 | |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 2911 | // Try to set a Secure __Secure- cookie, with experimental features |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2912 | // enabled. |
| 2913 | { |
| 2914 | TestDelegate d; |
| 2915 | scoped_ptr<URLRequest> req(context.CreateRequest( |
estark | 557a5eb8 | 2015-12-01 22:57:10 | [diff] [blame] | 2916 | https_server.GetURL("/set-cookie?__Secure-bar=1;Secure"), |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2917 | DEFAULT_PRIORITY, &d)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2918 | req->Start(); |
| 2919 | base::RunLoop().Run(); |
| 2920 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2921 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2922 | } |
| 2923 | |
| 2924 | // Verify that the cookie is set. |
| 2925 | { |
| 2926 | TestDelegate d; |
| 2927 | scoped_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 2928 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 2929 | req->Start(); |
| 2930 | base::RunLoop().Run(); |
| 2931 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 2932 | EXPECT_NE(d.data_received().find("__Secure-bar=1"), std::string::npos); |
| 2933 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2934 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2935 | } |
| 2936 | } |
| 2937 | |
| 2938 | // Tests that secure cookies can't be set on non-secure origins if strict secure |
| 2939 | // cookies are enabled. |
| 2940 | TEST_F(URLRequestTest, StrictSecureCookiesOnNonsecureOrigin) { |
| 2941 | EmbeddedTestServer http_server; |
| 2942 | http_server.AddDefaultHandlers( |
| 2943 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 2944 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 2945 | https_server.AddDefaultHandlers( |
| 2946 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 2947 | ASSERT_TRUE(http_server.Start()); |
| 2948 | ASSERT_TRUE(https_server.Start()); |
| 2949 | |
| 2950 | TestExperimentalFeaturesNetworkDelegate network_delegate; |
| 2951 | TestURLRequestContext context(true); |
| 2952 | context.set_network_delegate(&network_delegate); |
| 2953 | context.Init(); |
| 2954 | |
| 2955 | // Try to set a Secure cookie, with experimental features enabled. |
| 2956 | { |
| 2957 | TestDelegate d; |
| 2958 | scoped_ptr<URLRequest> req(context.CreateRequest( |
| 2959 | http_server.GetURL("/set-cookie?nonsecure-origin=1;Secure"), |
| 2960 | DEFAULT_PRIORITY, &d)); |
| 2961 | req->Start(); |
| 2962 | base::RunLoop().Run(); |
| 2963 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2964 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2965 | } |
| 2966 | |
| 2967 | // Verify that the cookie is not set. |
| 2968 | { |
| 2969 | TestDelegate d; |
| 2970 | scoped_ptr<URLRequest> req(context.CreateRequest( |
| 2971 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
| 2972 | req->Start(); |
| 2973 | base::RunLoop().Run(); |
| 2974 | |
| 2975 | EXPECT_EQ(d.data_received().find("nonsecure-origin=1"), std::string::npos); |
| 2976 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2977 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2978 | } |
| 2979 | } |
| 2980 | |
| 2981 | // Tests that secure cookies can be set on secure origins even if strict secure |
| 2982 | // cookies are enabled. |
| 2983 | TEST_F(URLRequestTest, StrictSecureCookiesOnSecureOrigin) { |
| 2984 | EmbeddedTestServer https_server(EmbeddedTestServer::TYPE_HTTPS); |
| 2985 | https_server.AddDefaultHandlers( |
| 2986 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 2987 | ASSERT_TRUE(https_server.Start()); |
| 2988 | |
| 2989 | TestExperimentalFeaturesNetworkDelegate network_delegate; |
| 2990 | TestURLRequestContext context(true); |
| 2991 | context.set_network_delegate(&network_delegate); |
| 2992 | context.Init(); |
| 2993 | |
| 2994 | // Try to set a Secure cookie, with experimental features enabled. |
| 2995 | { |
| 2996 | TestDelegate d; |
| 2997 | scoped_ptr<URLRequest> req(context.CreateRequest( |
| 2998 | https_server.GetURL("/set-cookie?secure-origin=1;Secure"), |
| 2999 | DEFAULT_PRIORITY, &d)); |
| 3000 | req->Start(); |
| 3001 | base::RunLoop().Run(); |
| 3002 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3003 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3004 | } |
| 3005 | |
| 3006 | // Verify that the cookie is not set. |
| 3007 | { |
| 3008 | TestDelegate d; |
| 3009 | scoped_ptr<URLRequest> req(context.CreateRequest( |
| 3010 | https_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
| 3011 | req->Start(); |
| 3012 | base::RunLoop().Run(); |
| 3013 | |
| 3014 | EXPECT_NE(d.data_received().find("secure-origin=1"), std::string::npos); |
estark | cd39c11f | 2015-10-19 19:46:36 | [diff] [blame] | 3015 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 3016 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 3017 | } |
| 3018 | } |
| 3019 | |
mmenke | fb18c77 | 2015-09-30 22:22:50 | [diff] [blame] | 3020 | // Tests that a request is cancelled while entering suspend mode. Uses mocks |
| 3021 | // rather than a spawned test server because the connection used to talk to |
| 3022 | // the test server is affected by entering suspend mode on Android. |
| 3023 | TEST_F(URLRequestTest, CancelOnSuspend) { |
| 3024 | TestPowerMonitorSource* power_monitor_source = new TestPowerMonitorSource(); |
| 3025 | base::PowerMonitor power_monitor(make_scoped_ptr(power_monitor_source)); |
| 3026 | |
| 3027 | URLRequestFailedJob::AddUrlHandler(); |
| 3028 | |
| 3029 | TestDelegate d; |
| 3030 | // Request that just hangs. |
| 3031 | GURL url(URLRequestFailedJob::GetMockHttpUrl(ERR_IO_PENDING)); |
| 3032 | scoped_ptr<URLRequest> r( |
| 3033 | default_context_.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
| 3034 | r->Start(); |
| 3035 | |
| 3036 | power_monitor_source->Suspend(); |
| 3037 | // Wait for the suspend notification to cause the request to fail. |
| 3038 | base::RunLoop().Run(); |
| 3039 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
| 3040 | EXPECT_TRUE(d.request_failed()); |
| 3041 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 3042 | |
| 3043 | URLRequestFilter::GetInstance()->ClearHandlers(); |
| 3044 | |
| 3045 | // Shouldn't be needed, but just in case. |
| 3046 | power_monitor_source->Resume(); |
| 3047 | } |
| 3048 | |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3049 | // FixedDateNetworkDelegate swaps out the server's HTTP Date response header |
| 3050 | // value for the |fixed_date| argument given to the constructor. |
| 3051 | class FixedDateNetworkDelegate : public TestNetworkDelegate { |
| 3052 | public: |
| 3053 | explicit FixedDateNetworkDelegate(const std::string& fixed_date) |
| 3054 | : fixed_date_(fixed_date) {} |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 3055 | ~FixedDateNetworkDelegate() override {} |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3056 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3057 | // NetworkDelegate implementation |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 3058 | int OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3059 | URLRequest* request, |
| 3060 | const CompletionCallback& callback, |
| 3061 | const HttpResponseHeaders* original_response_headers, |
| 3062 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 3063 | GURL* allowed_unsafe_redirect_url) override; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3064 | |
| 3065 | private: |
| 3066 | std::string fixed_date_; |
| 3067 | |
| 3068 | DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate); |
| 3069 | }; |
| 3070 | |
| 3071 | int FixedDateNetworkDelegate::OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3072 | URLRequest* request, |
| 3073 | const CompletionCallback& callback, |
| 3074 | const HttpResponseHeaders* original_response_headers, |
| 3075 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3076 | GURL* allowed_unsafe_redirect_url) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 3077 | HttpResponseHeaders* new_response_headers = |
| 3078 | new HttpResponseHeaders(original_response_headers->raw_headers()); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3079 | |
| 3080 | new_response_headers->RemoveHeader("Date"); |
| 3081 | new_response_headers->AddHeader("Date: " + fixed_date_); |
| 3082 | |
| 3083 | *override_response_headers = new_response_headers; |
| 3084 | return TestNetworkDelegate::OnHeadersReceived(request, |
| 3085 | callback, |
| 3086 | original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3087 | override_response_headers, |
| 3088 | allowed_unsafe_redirect_url); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3089 | } |
| 3090 | |
| 3091 | // Test that cookie expiration times are adjusted for server/client clock |
| 3092 | // skew and that we handle incorrect timezone specifier "UTC" in HTTP Date |
| 3093 | // headers by defaulting to GMT. (crbug.com/135131) |
| 3094 | TEST_F(URLRequestTest, AcceptClockSkewCookieWithWrongDateTimezone) { |
| 3095 | LocalHttpTestServer test_server; |
| 3096 | ASSERT_TRUE(test_server.Start()); |
| 3097 | |
| 3098 | // Set up an expired cookie. |
| 3099 | { |
| 3100 | TestNetworkDelegate network_delegate; |
| 3101 | default_context_.set_network_delegate(&network_delegate); |
| 3102 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3103 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3104 | test_server.GetURL( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3105 | "/set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3106 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3107 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3108 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3109 | } |
| 3110 | // Verify that the cookie is not set. |
| 3111 | { |
| 3112 | TestNetworkDelegate network_delegate; |
| 3113 | default_context_.set_network_delegate(&network_delegate); |
| 3114 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3115 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3116 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3117 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3118 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3119 | |
| 3120 | EXPECT_TRUE(d.data_received().find("StillGood=1") == std::string::npos); |
| 3121 | } |
| 3122 | // Set up a cookie with clock skew and "UTC" HTTP Date timezone specifier. |
| 3123 | { |
| 3124 | FixedDateNetworkDelegate network_delegate("18-Apr-1977 22:49:13 UTC"); |
| 3125 | default_context_.set_network_delegate(&network_delegate); |
| 3126 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3127 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3128 | test_server.GetURL( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3129 | "/set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3130 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3131 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3132 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3133 | } |
| 3134 | // Verify that the cookie is set. |
| 3135 | { |
| 3136 | TestNetworkDelegate network_delegate; |
| 3137 | default_context_.set_network_delegate(&network_delegate); |
| 3138 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3139 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3140 | test_server.GetURL("/echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3141 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3142 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 3143 | |
| 3144 | EXPECT_TRUE(d.data_received().find("StillGood=1") != std::string::npos); |
| 3145 | } |
| 3146 | } |
| 3147 | |
| 3148 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3149 | // Check that it is impossible to change the referrer in the extra headers of |
| 3150 | // an URLRequest. |
| 3151 | TEST_F(URLRequestTest, DoNotOverrideReferrer) { |
| 3152 | LocalHttpTestServer test_server; |
| 3153 | ASSERT_TRUE(test_server.Start()); |
| 3154 | |
| 3155 | // If extra headers contain referer and the request contains a referer, |
| 3156 | // only the latter shall be respected. |
| 3157 | { |
| 3158 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3159 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3160 | test_server.GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3161 | req->SetReferrer("http://foo.com/"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3162 | |
| 3163 | HttpRequestHeaders headers; |
| 3164 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3165 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3166 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3167 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3168 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3169 | |
| 3170 | EXPECT_EQ("http://foo.com/", d.data_received()); |
| 3171 | } |
| 3172 | |
| 3173 | // If extra headers contain a referer but the request does not, no referer |
| 3174 | // shall be sent in the header. |
| 3175 | { |
| 3176 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3177 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3178 | test_server.GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3179 | |
| 3180 | HttpRequestHeaders headers; |
| 3181 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3182 | req->SetExtraRequestHeaders(headers); |
| 3183 | req->SetLoadFlags(LOAD_VALIDATE_CACHE); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3184 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3185 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3186 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3187 | |
| 3188 | EXPECT_EQ("None", d.data_received()); |
| 3189 | } |
| 3190 | } |
| 3191 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3192 | class URLRequestTestHTTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3193 | public: |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 3194 | URLRequestTestHTTP() : test_server_(base::FilePath(kTestFilePath)) {} |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3195 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3196 | protected: |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3197 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. |
| 3198 | // |request_method| is the method to use for the initial request. |
| 3199 | // |redirect_method| is the method that is expected to be used for the second |
| 3200 | // request, after redirection. |
| 3201 | // If |include_data| is true, data is uploaded with the request. The |
| 3202 | // response body is expected to match it exactly, if and only if |
| 3203 | // |request_method| == |redirect_method|. |
| 3204 | void HTTPRedirectMethodTest(const GURL& redirect_url, |
| 3205 | const std::string& request_method, |
| 3206 | const std::string& redirect_method, |
| 3207 | bool include_data) { |
| 3208 | static const char kData[] = "hello world"; |
| 3209 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3210 | scoped_ptr<URLRequest> req( |
| 3211 | default_context_.CreateRequest(redirect_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3212 | req->set_method(request_method); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3213 | if (include_data) { |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 3214 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3215 | HttpRequestHeaders headers; |
| 3216 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
ricea | c912aec | 2015-07-09 07:26:51 | [diff] [blame] | 3217 | base::SizeTToString(arraysize(kData) - 1)); |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 3218 | headers.SetHeader(HttpRequestHeaders::kContentType, "text/plain"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3219 | req->SetExtraRequestHeaders(headers); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3220 | } |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3221 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3222 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3223 | EXPECT_EQ(redirect_method, req->method()); |
| 3224 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
| 3225 | EXPECT_EQ(OK, req->status().error()); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3226 | if (include_data) { |
| 3227 | if (request_method == redirect_method) { |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 3228 | EXPECT_TRUE(req->extra_request_headers().HasHeader( |
| 3229 | HttpRequestHeaders::kContentLength)); |
| 3230 | EXPECT_TRUE(req->extra_request_headers().HasHeader( |
| 3231 | HttpRequestHeaders::kContentType)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3232 | EXPECT_EQ(kData, d.data_received()); |
| 3233 | } else { |
svaldez | 5b3a897 | 2015-10-09 23:23:54 | [diff] [blame] | 3234 | EXPECT_FALSE(req->extra_request_headers().HasHeader( |
| 3235 | HttpRequestHeaders::kContentLength)); |
| 3236 | EXPECT_FALSE(req->extra_request_headers().HasHeader( |
| 3237 | HttpRequestHeaders::kContentType)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3238 | EXPECT_NE(kData, d.data_received()); |
| 3239 | } |
| 3240 | } |
| 3241 | if (HasFailure()) |
| 3242 | LOG(WARNING) << "Request method was: " << request_method; |
| 3243 | } |
| 3244 | |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 3245 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. |
| 3246 | // |request_method| is the method to use for the initial request. |
| 3247 | // |redirect_method| is the method that is expected to be used for the second |
| 3248 | // request, after redirection. |
| 3249 | // |origin_value| is the expected value for the Origin header after |
| 3250 | // redirection. If empty, expects that there will be no Origin header. |
| 3251 | void HTTPRedirectOriginHeaderTest(const GURL& redirect_url, |
| 3252 | const std::string& request_method, |
| 3253 | const std::string& redirect_method, |
| 3254 | const std::string& origin_value) { |
| 3255 | TestDelegate d; |
| 3256 | scoped_ptr<URLRequest> req( |
| 3257 | default_context_.CreateRequest(redirect_url, DEFAULT_PRIORITY, &d)); |
| 3258 | req->set_method(request_method); |
| 3259 | req->SetExtraRequestHeaderByName(HttpRequestHeaders::kOrigin, |
| 3260 | redirect_url.GetOrigin().spec(), false); |
| 3261 | req->Start(); |
| 3262 | |
| 3263 | base::RunLoop().Run(); |
| 3264 | |
| 3265 | EXPECT_EQ(redirect_method, req->method()); |
| 3266 | // Note that there is no check for request success here because, for |
| 3267 | // purposes of testing, the request very well may fail. For example, if the |
| 3268 | // test redirects to an HTTPS server from an HTTP origin, thus it is cross |
| 3269 | // origin, there is not an HTTPS server in this unit test framework, so the |
| 3270 | // request would fail. However, that's fine, as long as the request headers |
| 3271 | // are in order and pass the checks below. |
| 3272 | if (origin_value.empty()) { |
| 3273 | EXPECT_FALSE( |
| 3274 | req->extra_request_headers().HasHeader(HttpRequestHeaders::kOrigin)); |
| 3275 | } else { |
| 3276 | std::string origin_header; |
| 3277 | EXPECT_TRUE(req->extra_request_headers().GetHeader( |
| 3278 | HttpRequestHeaders::kOrigin, &origin_header)); |
| 3279 | EXPECT_EQ(origin_value, origin_header); |
| 3280 | } |
| 3281 | } |
| 3282 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3283 | void HTTPUploadDataOperationTest(const std::string& method) { |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3284 | const int kMsgSize = 20000; // multiple of 10 |
| 3285 | const int kIterations = 50; |
[email protected] | f43b89f3 | 2012-05-01 19:39:48 | [diff] [blame] | 3286 | char* uploadBytes = new char[kMsgSize+1]; |
| 3287 | char* ptr = uploadBytes; |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3288 | char marker = 'a'; |
| 3289 | for (int idx = 0; idx < kMsgSize/10; idx++) { |
| 3290 | memcpy(ptr, "----------", 10); |
| 3291 | ptr += 10; |
| 3292 | if (idx % 100 == 0) { |
| 3293 | ptr--; |
| 3294 | *ptr++ = marker; |
| 3295 | if (++marker > 'z') |
| 3296 | marker = 'a'; |
| 3297 | } |
| 3298 | } |
| 3299 | uploadBytes[kMsgSize] = '\0'; |
| 3300 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3301 | for (int i = 0; i < kIterations; ++i) { |
| 3302 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3303 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3304 | test_server_.GetURL("/echo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3305 | r->set_method(method.c_str()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3306 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 3307 | r->set_upload(CreateSimpleUploadData(uploadBytes)); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3308 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3309 | r->Start(); |
| 3310 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3311 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3312 | base::RunLoop().Run(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3313 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 3314 | ASSERT_EQ(1, d.response_started_count()) |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3315 | << "request failed: " << r->status().status() |
| 3316 | << ", os error: " << r->status().error(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3317 | |
| 3318 | EXPECT_FALSE(d.received_data_before_response()); |
| 3319 | EXPECT_EQ(uploadBytes, d.data_received()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 3320 | } |
| 3321 | delete[] uploadBytes; |
| 3322 | } |
| 3323 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3324 | void AddChunksToUpload(URLRequest* r) { |
[email protected] | 0c9bf87 | 2011-03-04 17:53:22 | [diff] [blame] | 3325 | r->AppendChunkToUpload("a", 1, false); |
| 3326 | r->AppendChunkToUpload("bcd", 3, false); |
| 3327 | r->AppendChunkToUpload("this is a longer chunk than before.", 35, false); |
| 3328 | r->AppendChunkToUpload("\r\n\r\n", 4, false); |
| 3329 | r->AppendChunkToUpload("0", 1, false); |
| 3330 | r->AppendChunkToUpload("2323", 4, true); |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 3331 | } |
| 3332 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3333 | void VerifyReceivedDataMatchesChunks(URLRequest* r, TestDelegate* d) { |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 3334 | // This should match the chunks sent by AddChunksToUpload(). |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 3335 | const std::string expected_data = |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 3336 | "abcdthis is a longer chunk than before.\r\n\r\n02323"; |
| 3337 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 3338 | ASSERT_EQ(1, d->response_started_count()) |
| 3339 | << "request failed: " << r->status().status() |
| 3340 | << ", os error: " << r->status().error(); |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 3341 | |
| 3342 | EXPECT_FALSE(d->received_data_before_response()); |
| 3343 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 3344 | EXPECT_EQ(expected_data.size(), static_cast<size_t>(d->bytes_received())); |
| 3345 | EXPECT_EQ(expected_data, d->data_received()); |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 3346 | } |
| 3347 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 3348 | bool DoManyCookiesRequest(int num_cookies) { |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3349 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3350 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3351 | test_server_.GetURL("/set-many-cookies?" + |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3352 | base::IntToString(num_cookies)), |
| 3353 | DEFAULT_PRIORITY, &d)); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3354 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3355 | r->Start(); |
| 3356 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3357 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3358 | base::RunLoop().Run(); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3359 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3360 | bool is_success = r->status().is_success(); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3361 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3362 | if (!is_success) |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3363 | EXPECT_TRUE(r->status().error() == ERR_RESPONSE_HEADERS_TOO_BIG); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3364 | |
| 3365 | return is_success; |
| 3366 | } |
| 3367 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3368 | LocalHttpTestServer* http_test_server() { return &test_server_; } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 3369 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3370 | private: |
[email protected] | 1700c6a | 2012-02-22 18:07:07 | [diff] [blame] | 3371 | LocalHttpTestServer test_server_; |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3372 | }; |
| 3373 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3374 | namespace { |
| 3375 | |
| 3376 | scoped_ptr<test_server::HttpResponse> HandleRedirectConnect( |
| 3377 | const test_server::HttpRequest& request) { |
| 3378 | if (request.headers.find("Host") == request.headers.end() || |
| 3379 | request.headers.at("Host") != "www.redirect.com" || |
| 3380 | request.method != test_server::METHOD_CONNECT) { |
| 3381 | return nullptr; |
| 3382 | } |
| 3383 | |
| 3384 | scoped_ptr<test_server::BasicHttpResponse> http_response( |
| 3385 | new test_server::BasicHttpResponse); |
| 3386 | http_response->set_code(HTTP_FOUND); |
| 3387 | http_response->AddCustomHeader("Location", |
| 3388 | "http://www.destination.com/foo.js"); |
| 3389 | return http_response.Pass(); |
| 3390 | } |
| 3391 | |
| 3392 | } // namespace |
| 3393 | |
| 3394 | // In this unit test, we're using the EmbeddedTestServer as a proxy server and |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3395 | // issuing a CONNECT request with the magic host name "www.redirect.com". |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3396 | // The EmbeddedTestServer will return a 302 response, which we should not |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3397 | // follow. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 3398 | TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3399 | http_test_server()->RegisterRequestHandler( |
| 3400 | base::Bind(&HandleRedirectConnect)); |
| 3401 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3402 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 3403 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3404 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3405 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3406 | |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3407 | TestDelegate d; |
| 3408 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3409 | scoped_ptr<URLRequest> r(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3410 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3411 | r->Start(); |
| 3412 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3413 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3414 | base::RunLoop().Run(); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3415 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3416 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 3417 | // The proxy server is not set before failure. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3418 | EXPECT_TRUE(r->proxy_server().IsEmpty()); |
| 3419 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r->status().error()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 3420 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3421 | // We should not have followed the redirect. |
| 3422 | EXPECT_EQ(0, d.received_redirect_count()); |
| 3423 | } |
| 3424 | } |
| 3425 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3426 | // This is the same as the previous test, but checks that the network delegate |
| 3427 | // registers the error. |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 3428 | TEST_F(URLRequestTestHTTP, NetworkDelegateTunnelConnectionFailed) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3429 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3430 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 3431 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3432 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3433 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3434 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3435 | TestDelegate d; |
| 3436 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3437 | scoped_ptr<URLRequest> r(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3438 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3439 | r->Start(); |
| 3440 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3441 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3442 | base::RunLoop().Run(); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3443 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3444 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 3445 | // The proxy server is not set before failure. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3446 | EXPECT_TRUE(r->proxy_server().IsEmpty()); |
| 3447 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r->status().error()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3448 | EXPECT_EQ(1, d.response_started_count()); |
| 3449 | // We should not have followed the redirect. |
| 3450 | EXPECT_EQ(0, d.received_redirect_count()); |
| 3451 | |
| 3452 | EXPECT_EQ(1, network_delegate.error_count()); |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 3453 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, network_delegate.last_error()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3454 | } |
| 3455 | } |
| 3456 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3457 | // Tests that we can block and asynchronously return OK in various stages. |
| 3458 | TEST_F(URLRequestTestHTTP, NetworkDelegateBlockAsynchronously) { |
| 3459 | static const BlockingNetworkDelegate::Stage blocking_stages[] = { |
| 3460 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 3461 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 3462 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED |
| 3463 | }; |
| 3464 | static const size_t blocking_stages_length = arraysize(blocking_stages); |
| 3465 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3466 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3467 | |
| 3468 | TestDelegate d; |
| 3469 | BlockingNetworkDelegate network_delegate( |
| 3470 | BlockingNetworkDelegate::USER_CALLBACK); |
| 3471 | network_delegate.set_block_on( |
| 3472 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST | |
| 3473 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS | |
| 3474 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 3475 | |
| 3476 | TestURLRequestContext context(true); |
| 3477 | context.set_network_delegate(&network_delegate); |
| 3478 | context.Init(); |
| 3479 | |
| 3480 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3481 | scoped_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3482 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3483 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3484 | r->Start(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3485 | for (size_t i = 0; i < blocking_stages_length; ++i) { |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3486 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3487 | EXPECT_EQ(blocking_stages[i], |
| 3488 | network_delegate.stage_blocked_for_callback()); |
| 3489 | network_delegate.DoCallback(OK); |
| 3490 | } |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3491 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3492 | EXPECT_EQ(200, r->GetResponseCode()); |
| 3493 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3494 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3495 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3496 | } |
| 3497 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3498 | } |
| 3499 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3500 | // Tests that the network delegate can block and cancel a request. |
| 3501 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3502 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3503 | |
| 3504 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3505 | BlockingNetworkDelegate network_delegate( |
| 3506 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3507 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
| 3508 | network_delegate.set_retval(ERR_EMPTY_RESPONSE); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3509 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3510 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3511 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3512 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3513 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3514 | scoped_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3515 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3516 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3517 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3518 | base::RunLoop().Run(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3519 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3520 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 3521 | // The proxy server is not set before cancellation. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3522 | EXPECT_TRUE(r->proxy_server().IsEmpty()); |
| 3523 | EXPECT_EQ(ERR_EMPTY_RESPONSE, r->status().error()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3524 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3525 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3526 | } |
| 3527 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3528 | } |
| 3529 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3530 | // Helper function for NetworkDelegateCancelRequestAsynchronously and |
| 3531 | // NetworkDelegateCancelRequestSynchronously. Sets up a blocking network |
| 3532 | // delegate operating in |block_mode| and a request for |url|. It blocks the |
| 3533 | // request in |stage| and cancels it with ERR_BLOCKED_BY_CLIENT. |
| 3534 | void NetworkDelegateCancelRequest(BlockingNetworkDelegate::BlockMode block_mode, |
| 3535 | BlockingNetworkDelegate::Stage stage, |
| 3536 | const GURL& url) { |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 3537 | TestDelegate d; |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3538 | BlockingNetworkDelegate network_delegate(block_mode); |
| 3539 | network_delegate.set_retval(ERR_BLOCKED_BY_CLIENT); |
| 3540 | network_delegate.set_block_on(stage); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 3541 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3542 | TestURLRequestContext context(true); |
| 3543 | context.set_network_delegate(&network_delegate); |
| 3544 | context.Init(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 3545 | |
| 3546 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3547 | scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 3548 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3549 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3550 | base::RunLoop().Run(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 3551 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3552 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 3553 | // The proxy server is not set before cancellation. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3554 | EXPECT_TRUE(r->proxy_server().IsEmpty()); |
| 3555 | EXPECT_EQ(ERR_BLOCKED_BY_CLIENT, r->status().error()); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 3556 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3557 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3558 | } |
| 3559 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3560 | } |
| 3561 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3562 | // The following 3 tests check that the network delegate can cancel a request |
| 3563 | // synchronously in various stages of the request. |
| 3564 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3565 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3566 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 3567 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3568 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3569 | } |
| 3570 | |
| 3571 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3572 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3573 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 3574 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3575 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3576 | } |
| 3577 | |
| 3578 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3579 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3580 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 3581 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3582 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3583 | } |
| 3584 | |
| 3585 | // The following 3 tests check that the network delegate can cancel a request |
| 3586 | // asynchronously in various stages of the request. |
| 3587 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3588 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3589 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 3590 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3591 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3592 | } |
| 3593 | |
| 3594 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3595 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3596 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 3597 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3598 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3599 | } |
| 3600 | |
| 3601 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3602 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3603 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 3604 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3605 | http_test_server()->GetURL("/")); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3606 | } |
| 3607 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3608 | // Tests that the network delegate can block and redirect a request to a new |
| 3609 | // URL. |
| 3610 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3611 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3612 | |
| 3613 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3614 | BlockingNetworkDelegate network_delegate( |
| 3615 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3616 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3617 | GURL redirect_url(http_test_server()->GetURL("/simple.html")); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3618 | network_delegate.set_redirect_url(redirect_url); |
| 3619 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3620 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3621 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3622 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3623 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3624 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3625 | scoped_ptr<URLRequest> r( |
| 3626 | context.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3627 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3628 | // Quit after hitting the redirect, so can check the headers. |
| 3629 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3630 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3631 | base::RunLoop().Run(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3632 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3633 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3634 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3635 | EXPECT_EQ(307, r->GetResponseCode()); |
| 3636 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3637 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3638 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 3639 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3640 | EXPECT_EQ(redirect_url, GURL(location)); |
| 3641 | |
| 3642 | // Let the request finish. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3643 | r->FollowDeferredRedirect(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3644 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3645 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3646 | EXPECT_TRUE(r->proxy_server().Equals(http_test_server()->host_port_pair())); |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 3647 | EXPECT_EQ( |
| 3648 | 1, network_delegate.observed_before_proxy_headers_sent_callbacks()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3649 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 3650 | http_test_server()->host_port_pair())); |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 3651 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3652 | EXPECT_EQ(0, r->status().error()); |
| 3653 | EXPECT_EQ(redirect_url, r->url()); |
| 3654 | EXPECT_EQ(original_url, r->original_url()); |
| 3655 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3656 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3657 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3658 | } |
| 3659 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3660 | } |
| 3661 | |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3662 | // Tests that the network delegate can block and redirect a request to a new |
| 3663 | // URL by setting a redirect_url and returning in OnBeforeURLRequest directly. |
| 3664 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3665 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3666 | |
| 3667 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3668 | BlockingNetworkDelegate network_delegate( |
| 3669 | BlockingNetworkDelegate::SYNCHRONOUS); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3670 | GURL redirect_url(http_test_server()->GetURL("/simple.html")); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3671 | network_delegate.set_redirect_url(redirect_url); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3672 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3673 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3674 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3675 | |
| 3676 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3677 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3678 | scoped_ptr<URLRequest> r( |
| 3679 | context.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3680 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3681 | // Quit after hitting the redirect, so can check the headers. |
| 3682 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3683 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3684 | base::RunLoop().Run(); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3685 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3686 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3687 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3688 | EXPECT_EQ(307, r->GetResponseCode()); |
| 3689 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3690 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3691 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 3692 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3693 | EXPECT_EQ(redirect_url, GURL(location)); |
| 3694 | |
| 3695 | // Let the request finish. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3696 | r->FollowDeferredRedirect(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3697 | base::RunLoop().Run(); |
| 3698 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3699 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3700 | EXPECT_TRUE(r->proxy_server().Equals(http_test_server()->host_port_pair())); |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 3701 | EXPECT_EQ( |
| 3702 | 1, network_delegate.observed_before_proxy_headers_sent_callbacks()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3703 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 3704 | http_test_server()->host_port_pair())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3705 | EXPECT_EQ(0, r->status().error()); |
| 3706 | EXPECT_EQ(redirect_url, r->url()); |
| 3707 | EXPECT_EQ(original_url, r->original_url()); |
| 3708 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3709 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3710 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3711 | } |
| 3712 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3713 | } |
| 3714 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3715 | // Tests that redirects caused by the network delegate preserve POST data. |
| 3716 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3717 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3718 | |
| 3719 | const char kData[] = "hello world"; |
| 3720 | |
| 3721 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3722 | BlockingNetworkDelegate network_delegate( |
| 3723 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3724 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3725 | GURL redirect_url(http_test_server()->GetURL("/echo")); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3726 | network_delegate.set_redirect_url(redirect_url); |
| 3727 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3728 | TestURLRequestContext context(true); |
| 3729 | context.set_network_delegate(&network_delegate); |
| 3730 | context.Init(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3731 | |
| 3732 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3733 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3734 | scoped_ptr<URLRequest> r( |
| 3735 | context.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3736 | r->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 3737 | r->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3738 | HttpRequestHeaders headers; |
| 3739 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
ricea | c912aec | 2015-07-09 07:26:51 | [diff] [blame] | 3740 | base::SizeTToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3741 | r->SetExtraRequestHeaders(headers); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3742 | |
| 3743 | // Quit after hitting the redirect, so can check the headers. |
| 3744 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3745 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3746 | base::RunLoop().Run(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3747 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3748 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3749 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3750 | EXPECT_EQ(307, r->GetResponseCode()); |
| 3751 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3752 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3753 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 3754 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3755 | EXPECT_EQ(redirect_url, GURL(location)); |
| 3756 | |
| 3757 | // Let the request finish. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3758 | r->FollowDeferredRedirect(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3759 | base::RunLoop().Run(); |
| 3760 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3761 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3762 | EXPECT_EQ(0, r->status().error()); |
| 3763 | EXPECT_EQ(redirect_url, r->url()); |
| 3764 | EXPECT_EQ(original_url, r->original_url()); |
| 3765 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3766 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3767 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3768 | EXPECT_EQ("POST", r->method()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3769 | EXPECT_EQ(kData, d.data_received()); |
| 3770 | } |
| 3771 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3772 | } |
| 3773 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3774 | // Tests that the network delegate can block and redirect a request to a new |
| 3775 | // URL during OnHeadersReceived. |
| 3776 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestOnHeadersReceived) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3777 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3778 | |
| 3779 | TestDelegate d; |
| 3780 | BlockingNetworkDelegate network_delegate( |
| 3781 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3782 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3783 | GURL redirect_url(http_test_server()->GetURL("/simple.html")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3784 | network_delegate.set_redirect_on_headers_received_url(redirect_url); |
| 3785 | |
| 3786 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3787 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3788 | |
| 3789 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3790 | GURL original_url(http_test_server()->GetURL("/defaultresponse")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3791 | scoped_ptr<URLRequest> r( |
| 3792 | context.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3793 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3794 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3795 | base::RunLoop().Run(); |
| 3796 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3797 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3798 | EXPECT_TRUE(r->proxy_server().Equals(http_test_server()->host_port_pair())); |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 3799 | EXPECT_EQ( |
| 3800 | 2, network_delegate.observed_before_proxy_headers_sent_callbacks()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3801 | EXPECT_TRUE(network_delegate.last_observed_proxy().Equals( |
| 3802 | http_test_server()->host_port_pair())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3803 | |
| 3804 | EXPECT_EQ(OK, r->status().error()); |
| 3805 | EXPECT_EQ(redirect_url, r->url()); |
| 3806 | EXPECT_EQ(original_url, r->original_url()); |
| 3807 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3808 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 3809 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3810 | } |
| 3811 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3812 | } |
| 3813 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3814 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 3815 | // by taking no action. This indicates that the NetworkDelegate does not want to |
| 3816 | // handle the challenge, and is passing the buck along to the |
| 3817 | // URLRequest::Delegate. |
| 3818 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3819 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3820 | |
| 3821 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3822 | BlockingNetworkDelegate network_delegate( |
| 3823 | BlockingNetworkDelegate::SYNCHRONOUS); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3824 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3825 | TestURLRequestContext context(true); |
| 3826 | context.set_network_delegate(&network_delegate); |
| 3827 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3828 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3829 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3830 | |
| 3831 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3832 | GURL url(http_test_server()->GetURL("/auth-basic")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3833 | scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3834 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3835 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3836 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3837 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3838 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3839 | EXPECT_EQ(0, r->status().error()); |
| 3840 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3841 | EXPECT_TRUE(d.auth_required_called()); |
| 3842 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3843 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3844 | } |
| 3845 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3846 | } |
| 3847 | |
| 3848 | TEST_F(URLRequestTestHTTP, |
| 3849 | NetworkDelegateOnAuthRequiredSyncNoAction_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3850 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3851 | |
| 3852 | TestDelegate d; |
| 3853 | BlockingNetworkDelegate network_delegate( |
| 3854 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 3855 | |
| 3856 | TestURLRequestContext context(true); |
| 3857 | context.set_network_delegate(&network_delegate); |
| 3858 | context.Init(); |
| 3859 | |
| 3860 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 3861 | |
| 3862 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3863 | GURL url(http_test_server()->GetURL("/auth-basic")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3864 | scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3865 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3866 | |
| 3867 | { |
| 3868 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3869 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3870 | EXPECT_FALSE(headers.HasHeader("Authorization")); |
| 3871 | } |
| 3872 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3873 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3874 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3875 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3876 | EXPECT_EQ(0, r->status().error()); |
| 3877 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3878 | EXPECT_TRUE(d.auth_required_called()); |
| 3879 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3880 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3881 | } |
| 3882 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3883 | } |
| 3884 | |
| 3885 | // Tests that the network delegate can synchronously complete OnAuthRequired |
[email protected] | 1e110eae | 2013-05-10 22:02:40 | [diff] [blame] | 3886 | // by setting credentials. |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3887 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3888 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3889 | |
| 3890 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3891 | BlockingNetworkDelegate network_delegate( |
| 3892 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 3893 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3894 | network_delegate.set_auth_retval( |
| 3895 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 3896 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3897 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3898 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3899 | TestURLRequestContext context(true); |
| 3900 | context.set_network_delegate(&network_delegate); |
| 3901 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3902 | |
| 3903 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3904 | GURL url(http_test_server()->GetURL("/auth-basic")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3905 | scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3906 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3907 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3908 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3909 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3910 | EXPECT_EQ(0, r->status().error()); |
| 3911 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3912 | EXPECT_FALSE(d.auth_required_called()); |
| 3913 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3914 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3915 | } |
| 3916 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3917 | } |
| 3918 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3919 | // Same as above, but also tests that GetFullRequestHeaders returns the proper |
| 3920 | // headers (for the first or second request) when called at the proper times. |
| 3921 | TEST_F(URLRequestTestHTTP, |
| 3922 | NetworkDelegateOnAuthRequiredSyncSetAuth_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3923 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3924 | |
| 3925 | TestDelegate d; |
| 3926 | BlockingNetworkDelegate network_delegate( |
| 3927 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 3928 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
| 3929 | network_delegate.set_auth_retval( |
| 3930 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 3931 | |
| 3932 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
| 3933 | |
| 3934 | TestURLRequestContext context(true); |
| 3935 | context.set_network_delegate(&network_delegate); |
| 3936 | context.Init(); |
| 3937 | |
| 3938 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3939 | GURL url(http_test_server()->GetURL("/auth-basic")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3940 | scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3941 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3942 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3943 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3944 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3945 | EXPECT_EQ(0, r->status().error()); |
| 3946 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3947 | EXPECT_FALSE(d.auth_required_called()); |
| 3948 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3949 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3950 | |
| 3951 | { |
| 3952 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3953 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3954 | EXPECT_TRUE(headers.HasHeader("Authorization")); |
| 3955 | } |
| 3956 | } |
| 3957 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3958 | } |
| 3959 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3960 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 3961 | // by cancelling authentication. |
| 3962 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3963 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3964 | |
| 3965 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3966 | BlockingNetworkDelegate network_delegate( |
| 3967 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 3968 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3969 | network_delegate.set_auth_retval( |
| 3970 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 3971 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3972 | TestURLRequestContext context(true); |
| 3973 | context.set_network_delegate(&network_delegate); |
| 3974 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3975 | |
| 3976 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3977 | GURL url(http_test_server()->GetURL("/auth-basic")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3978 | scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3979 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3980 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3981 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3982 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3983 | EXPECT_EQ(OK, r->status().error()); |
| 3984 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3985 | EXPECT_FALSE(d.auth_required_called()); |
| 3986 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3987 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3988 | } |
| 3989 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3990 | } |
| 3991 | |
| 3992 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 3993 | // by taking no action. This indicates that the NetworkDelegate does not want |
| 3994 | // to handle the challenge, and is passing the buck along to the |
| 3995 | // URLRequest::Delegate. |
| 3996 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 3997 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3998 | |
| 3999 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4000 | BlockingNetworkDelegate network_delegate( |
| 4001 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4002 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4003 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4004 | TestURLRequestContext context(true); |
| 4005 | context.set_network_delegate(&network_delegate); |
| 4006 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4007 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4008 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4009 | |
| 4010 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4011 | GURL url(http_test_server()->GetURL("/auth-basic")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4012 | scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4013 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4014 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4015 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4016 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 4017 | EXPECT_EQ(0, r->status().error()); |
| 4018 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4019 | EXPECT_TRUE(d.auth_required_called()); |
| 4020 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4021 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4022 | } |
| 4023 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4024 | } |
| 4025 | |
| 4026 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 4027 | // by setting credentials. |
| 4028 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4029 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4030 | |
| 4031 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4032 | BlockingNetworkDelegate network_delegate( |
| 4033 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4034 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4035 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4036 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4037 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 4038 | AuthCredentials auth_credentials(kUser, kSecret); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4039 | network_delegate.set_auth_credentials(auth_credentials); |
| 4040 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4041 | TestURLRequestContext context(true); |
| 4042 | context.set_network_delegate(&network_delegate); |
| 4043 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4044 | |
| 4045 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4046 | GURL url(http_test_server()->GetURL("/auth-basic")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4047 | scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4048 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4049 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4050 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4051 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 4052 | EXPECT_EQ(0, r->status().error()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4053 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4054 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4055 | EXPECT_FALSE(d.auth_required_called()); |
| 4056 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4057 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4058 | } |
| 4059 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4060 | } |
| 4061 | |
| 4062 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 4063 | // by cancelling authentication. |
| 4064 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4065 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4066 | |
| 4067 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4068 | BlockingNetworkDelegate network_delegate( |
| 4069 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 4070 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4071 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4072 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 4073 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4074 | TestURLRequestContext context(true); |
| 4075 | context.set_network_delegate(&network_delegate); |
| 4076 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4077 | |
| 4078 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4079 | GURL url(http_test_server()->GetURL("/auth-basic")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4080 | scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4081 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4082 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4083 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4084 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 4085 | EXPECT_EQ(OK, r->status().error()); |
| 4086 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 4087 | EXPECT_FALSE(d.auth_required_called()); |
| 4088 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4089 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4090 | } |
| 4091 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4092 | } |
| 4093 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4094 | // Tests that we can handle when a network request was canceled while we were |
| 4095 | // waiting for the network delegate. |
| 4096 | // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback. |
| 4097 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4098 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4099 | |
| 4100 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4101 | BlockingNetworkDelegate network_delegate( |
| 4102 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4103 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4104 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4105 | TestURLRequestContext context(true); |
| 4106 | context.set_network_delegate(&network_delegate); |
| 4107 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4108 | |
| 4109 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4110 | scoped_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4111 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4112 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4113 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4114 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4115 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 4116 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4117 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 4118 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4119 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4120 | // Ensure that network delegate is notified. |
| 4121 | EXPECT_EQ(1, network_delegate.completed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4122 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
| 4123 | EXPECT_EQ(ERR_ABORTED, r->status().error()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4124 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4125 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4126 | } |
| 4127 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4128 | } |
| 4129 | |
| 4130 | // Tests that we can handle when a network request was canceled while we were |
| 4131 | // waiting for the network delegate. |
| 4132 | // Part 2: Request is cancelled while waiting for OnBeforeSendHeaders callback. |
| 4133 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4134 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4135 | |
| 4136 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4137 | BlockingNetworkDelegate network_delegate( |
| 4138 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4139 | network_delegate.set_block_on( |
| 4140 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4141 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4142 | TestURLRequestContext context(true); |
| 4143 | context.set_network_delegate(&network_delegate); |
| 4144 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4145 | |
| 4146 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4147 | scoped_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4148 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4149 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4150 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4151 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4152 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 4153 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4154 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 4155 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4156 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4157 | // Ensure that network delegate is notified. |
| 4158 | EXPECT_EQ(1, network_delegate.completed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4159 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
| 4160 | EXPECT_EQ(ERR_ABORTED, r->status().error()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4161 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4162 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4163 | } |
| 4164 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4165 | } |
| 4166 | |
| 4167 | // Tests that we can handle when a network request was canceled while we were |
| 4168 | // waiting for the network delegate. |
| 4169 | // Part 3: Request is cancelled while waiting for OnHeadersReceived callback. |
| 4170 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4171 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4172 | |
| 4173 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4174 | BlockingNetworkDelegate network_delegate( |
| 4175 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4176 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4177 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4178 | TestURLRequestContext context(true); |
| 4179 | context.set_network_delegate(&network_delegate); |
| 4180 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4181 | |
| 4182 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4183 | scoped_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4184 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4185 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4186 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4187 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4188 | EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
| 4189 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4190 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 4191 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4192 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4193 | // Ensure that network delegate is notified. |
| 4194 | EXPECT_EQ(1, network_delegate.completed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4195 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
| 4196 | EXPECT_EQ(ERR_ABORTED, r->status().error()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4197 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4198 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4199 | } |
| 4200 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4201 | } |
| 4202 | |
| 4203 | // Tests that we can handle when a network request was canceled while we were |
| 4204 | // waiting for the network delegate. |
| 4205 | // Part 4: Request is cancelled while waiting for OnAuthRequired callback. |
[email protected] | bfe8b30 | 2013-02-15 12:16:02 | [diff] [blame] | 4206 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4207 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4208 | |
| 4209 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4210 | BlockingNetworkDelegate network_delegate( |
| 4211 | BlockingNetworkDelegate::USER_CALLBACK); |
| 4212 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4213 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4214 | TestURLRequestContext context(true); |
| 4215 | context.set_network_delegate(&network_delegate); |
| 4216 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4217 | |
| 4218 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4219 | scoped_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4220 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4221 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4222 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4223 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 4224 | EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED, |
| 4225 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4226 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 4227 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4228 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4229 | // Ensure that network delegate is notified. |
| 4230 | EXPECT_EQ(1, network_delegate.completed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4231 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
| 4232 | EXPECT_EQ(ERR_ABORTED, r->status().error()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 4233 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4234 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4235 | } |
| 4236 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4237 | } |
| 4238 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4239 | namespace { |
| 4240 | |
| 4241 | scoped_ptr<test_server::HttpResponse> HandleServerAuthConnect( |
| 4242 | const test_server::HttpRequest& request) { |
| 4243 | if (request.headers.find("Host") == request.headers.end() || |
| 4244 | request.headers.at("Host") != "www.server-auth.com" || |
| 4245 | request.method != test_server::METHOD_CONNECT) { |
| 4246 | return nullptr; |
| 4247 | } |
| 4248 | |
| 4249 | scoped_ptr<test_server::BasicHttpResponse> http_response( |
| 4250 | new test_server::BasicHttpResponse); |
| 4251 | http_response->set_code(HTTP_UNAUTHORIZED); |
| 4252 | http_response->AddCustomHeader("WWW-Authenticate", |
| 4253 | "Basic realm=\"WallyWorld\""); |
| 4254 | return http_response.Pass(); |
| 4255 | } |
| 4256 | |
| 4257 | } // namespace |
| 4258 | |
| 4259 | // In this unit test, we're using the EmbeddedTestServer as a proxy server and |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4260 | // issuing a CONNECT request with the magic host name "www.server-auth.com". |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4261 | // The EmbeddedTestServer will return a 401 response, which we should balk at. |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4262 | TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4263 | http_test_server()->RegisterRequestHandler( |
| 4264 | base::Bind(&HandleServerAuthConnect)); |
| 4265 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4266 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4267 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 4268 | TestURLRequestContextWithProxy context( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4269 | http_test_server()->host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4270 | |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4271 | TestDelegate d; |
| 4272 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4273 | scoped_ptr<URLRequest> r(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4274 | GURL("https://www.server-auth.com/"), DEFAULT_PRIORITY, &d)); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4275 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4276 | r->Start(); |
| 4277 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4278 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4279 | base::RunLoop().Run(); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4280 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4281 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 4282 | // The proxy server is not set before failure. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4283 | EXPECT_TRUE(r->proxy_server().IsEmpty()); |
| 4284 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r->status().error()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 4285 | } |
| 4286 | } |
| 4287 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4288 | TEST_F(URLRequestTestHTTP, GetTest_NoCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4289 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4290 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4291 | TestDelegate d; |
| 4292 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4293 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4294 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4295 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4296 | r->Start(); |
| 4297 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4298 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4299 | base::RunLoop().Run(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4300 | |
| 4301 | EXPECT_EQ(1, d.response_started_count()); |
| 4302 | EXPECT_FALSE(d.received_data_before_response()); |
| 4303 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4304 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4305 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4306 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4307 | r->GetSocketAddress().port()); |
[email protected] | c31a5459 | 2009-09-04 02:36:16 | [diff] [blame] | 4308 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 4309 | // TODO(eroman): Add back the NetLog tests... |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4310 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4311 | } |
| 4312 | |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4313 | // This test has the server send a large number of cookies to the client. |
| 4314 | // To ensure that no number of cookies causes a crash, a galloping binary |
| 4315 | // search is used to estimate that maximum number of cookies that are accepted |
| 4316 | // by the browser. Beyond the maximum number, the request will fail with |
| 4317 | // ERR_RESPONSE_HEADERS_TOO_BIG. |
[email protected] | 69dd6fe | 2013-02-23 23:15:30 | [diff] [blame] | 4318 | #if defined(OS_WIN) |
| 4319 | // http://crbug.com/177916 |
| 4320 | #define MAYBE_GetTest_ManyCookies DISABLED_GetTest_ManyCookies |
| 4321 | #else |
| 4322 | #define MAYBE_GetTest_ManyCookies GetTest_ManyCookies |
| 4323 | #endif // defined(OS_WIN) |
| 4324 | TEST_F(URLRequestTestHTTP, MAYBE_GetTest_ManyCookies) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4325 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4326 | |
| 4327 | int lower_bound = 0; |
| 4328 | int upper_bound = 1; |
| 4329 | |
| 4330 | // Double the number of cookies until the response header limits are |
| 4331 | // exceeded. |
| 4332 | while (DoManyCookiesRequest(upper_bound)) { |
| 4333 | lower_bound = upper_bound; |
| 4334 | upper_bound *= 2; |
| 4335 | ASSERT_LT(upper_bound, 1000000); |
| 4336 | } |
| 4337 | |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 4338 | int tolerance = static_cast<int>(upper_bound * 0.005); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 4339 | if (tolerance < 2) |
| 4340 | tolerance = 2; |
| 4341 | |
| 4342 | // Perform a binary search to find the highest possible number of cookies, |
| 4343 | // within the desired tolerance. |
| 4344 | while (upper_bound - lower_bound >= tolerance) { |
| 4345 | int num_cookies = (lower_bound + upper_bound) / 2; |
| 4346 | |
| 4347 | if (DoManyCookiesRequest(num_cookies)) |
| 4348 | lower_bound = num_cookies; |
| 4349 | else |
| 4350 | upper_bound = num_cookies; |
| 4351 | } |
| 4352 | // Success: the test did not crash. |
| 4353 | } |
| 4354 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 4355 | TEST_F(URLRequestTestHTTP, GetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4356 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4357 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4358 | TestDelegate d; |
| 4359 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4360 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4361 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4362 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4363 | r->Start(); |
| 4364 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4365 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4366 | base::RunLoop().Run(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4367 | |
| 4368 | EXPECT_EQ(1, d.response_started_count()); |
| 4369 | EXPECT_FALSE(d.received_data_before_response()); |
| 4370 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4371 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4372 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4373 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4374 | r->GetSocketAddress().port()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4375 | } |
[email protected] | 5d7b373e | 2009-09-02 07:19:03 | [diff] [blame] | 4376 | } |
| 4377 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4378 | TEST_F(URLRequestTestHTTP, GetTest_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4379 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4380 | |
| 4381 | TestDelegate d; |
| 4382 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4383 | GURL test_url(http_test_server()->GetURL("/defaultresponse")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4384 | scoped_ptr<URLRequest> r( |
| 4385 | default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4386 | |
| 4387 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4388 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4389 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4390 | r->Start(); |
| 4391 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4392 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4393 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4394 | |
| 4395 | EXPECT_EQ(1, d.response_started_count()); |
| 4396 | EXPECT_FALSE(d.received_data_before_response()); |
| 4397 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4398 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4399 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4400 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4401 | r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 4402 | |
| 4403 | EXPECT_TRUE(d.have_full_request_headers()); |
| 4404 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 4405 | } |
| 4406 | } |
| 4407 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4408 | TEST_F(URLRequestTestHTTP, GetTestLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4409 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4410 | |
| 4411 | TestDelegate d; |
| 4412 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4413 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4414 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4415 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4416 | r->Start(); |
| 4417 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4418 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4419 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4420 | |
| 4421 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4422 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4423 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 4424 | |
| 4425 | EXPECT_EQ(1, d.response_started_count()); |
| 4426 | EXPECT_FALSE(d.received_data_before_response()); |
| 4427 | EXPECT_NE(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4428 | EXPECT_EQ(http_test_server()->host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4429 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4430 | EXPECT_EQ(http_test_server()->host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4431 | r->GetSocketAddress().port()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4432 | } |
| 4433 | } |
| 4434 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4435 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 4436 | #if !defined(OS_IOS) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4437 | TEST_F(URLRequestTestHTTP, GetZippedTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4438 | SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
| 4439 | SpawnedTestServer::kLocalhost, |
| 4440 | base::FilePath(kTestFilePath)); |
| 4441 | |
| 4442 | ASSERT_TRUE(test_server.Start()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4443 | |
| 4444 | // Parameter that specifies the Content-Length field in the response: |
| 4445 | // C - Compressed length. |
| 4446 | // U - Uncompressed length. |
| 4447 | // L - Large length (larger than both C & U). |
| 4448 | // M - Medium length (between C & U). |
| 4449 | // S - Small length (smaller than both C & U). |
| 4450 | const char test_parameters[] = "CULMS"; |
| 4451 | const int num_tests = arraysize(test_parameters)- 1; // Skip NULL. |
| 4452 | // C & U should be OK. |
[email protected] | f0e2bf4 | 2011-07-22 21:21:44 | [diff] [blame] | 4453 | // L & M are larger than the data sent, and show an error. |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4454 | // S has too little data, but we seem to accept it. |
| 4455 | const bool test_expect_success[num_tests] = |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 4456 | { true, true, false, false, true }; |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4457 | |
| 4458 | for (int i = 0; i < num_tests ; i++) { |
| 4459 | TestDelegate d; |
| 4460 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4461 | std::string test_file = base::StringPrintf( |
| 4462 | "compressedfiles/BullRunSpeech.txt?%c", test_parameters[i]); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4463 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4464 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4465 | TestURLRequestContext context(true); |
| 4466 | context.set_network_delegate(&network_delegate); |
| 4467 | context.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4468 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4469 | scoped_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4470 | test_server.GetURL(test_file), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4471 | r->Start(); |
| 4472 | EXPECT_TRUE(r->is_pending()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4473 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4474 | base::RunLoop().Run(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4475 | |
| 4476 | EXPECT_EQ(1, d.response_started_count()); |
| 4477 | EXPECT_FALSE(d.received_data_before_response()); |
| 4478 | VLOG(1) << " Received " << d.bytes_received() << " bytes" |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4479 | << " status = " << r->status().status() |
| 4480 | << " error = " << r->status().error(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4481 | if (test_expect_success[i]) { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4482 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4483 | << " Parameter = \"" << test_file << "\""; |
| 4484 | } else { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4485 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
| 4486 | EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, r->status().error()) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4487 | << " Parameter = \"" << test_file << "\""; |
| 4488 | } |
| 4489 | } |
| 4490 | } |
| 4491 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4492 | #endif // !defined(OS_IOS) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4493 | |
tbansal | a1caf961 | 2015-06-01 20:24:18 | [diff] [blame] | 4494 | TEST_F(URLRequestTestHTTP, NetworkQualityEstimator) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4495 | ASSERT_TRUE(http_test_server()->Start()); |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 4496 | // Enable requests to local host to be used for network quality estimation. |
tbansal | b177b539 | 2015-06-25 11:13:02 | [diff] [blame] | 4497 | std::map<std::string, std::string> variation_params; |
tbansal | 1c92d5b | 2015-08-14 20:14:43 | [diff] [blame] | 4498 | NetworkQualityEstimator estimator(scoped_ptr<net::ExternalEstimateProvider>(), |
| 4499 | variation_params, true, true); |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 4500 | |
| 4501 | TestDelegate d; |
| 4502 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 4503 | TestURLRequestContext context(true); |
| 4504 | context.set_network_quality_estimator(&estimator); |
| 4505 | context.set_network_delegate(&network_delegate); |
| 4506 | context.Init(); |
| 4507 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4508 | std::string url = "/defaultresponse"; |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 4509 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4510 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 4511 | http_test_server()->GetURL(url), DEFAULT_PRIORITY, &d)); |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 4512 | r->Start(); |
| 4513 | |
| 4514 | base::RunLoop().Run(); |
| 4515 | |
tbansal | 9533b6f | 2015-08-11 22:06:08 | [diff] [blame] | 4516 | base::TimeDelta rtt; |
| 4517 | int32_t kbps; |
| 4518 | EXPECT_TRUE(estimator.GetRTTEstimate(&rtt)); |
| 4519 | EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps)); |
| 4520 | EXPECT_GE(rtt, base::TimeDelta()); |
| 4521 | EXPECT_LT(rtt, base::TimeDelta::Max()); |
| 4522 | EXPECT_GT(kbps, 0); |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 4523 | |
| 4524 | // Verify that histograms are not populated. They should populate only when |
| 4525 | // there is a change in ConnectionType. |
| 4526 | base::HistogramTester histogram_tester; |
| 4527 | histogram_tester.ExpectTotalCount("NQE.PeakKbps.Unknown", 0); |
| 4528 | histogram_tester.ExpectTotalCount("NQE.FastestRTT.Unknown", 0); |
| 4529 | |
| 4530 | NetworkChangeNotifier::NotifyObserversOfConnectionTypeChangeForTests( |
| 4531 | NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI); |
| 4532 | base::MessageLoop::current()->RunUntilIdle(); |
| 4533 | histogram_tester.ExpectTotalCount("NQE.PeakKbps.Unknown", 1); |
| 4534 | histogram_tester.ExpectTotalCount("NQE.FastestRTT.Unknown", 1); |
| 4535 | } |
| 4536 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4537 | TEST_F(URLRequestTestHTTP, RedirectLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4538 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4539 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4540 | GURL destination_url = http_test_server()->GetURL("/"); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 4541 | GURL original_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4542 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4543 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4544 | scoped_ptr<URLRequest> req( |
| 4545 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4546 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4547 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4548 | |
| 4549 | EXPECT_EQ(1, d.response_started_count()); |
| 4550 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4551 | EXPECT_EQ(destination_url, req->url()); |
| 4552 | EXPECT_EQ(original_url, req->original_url()); |
| 4553 | ASSERT_EQ(2U, req->url_chain().size()); |
| 4554 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 4555 | EXPECT_EQ(destination_url, req->url_chain()[1]); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4556 | |
| 4557 | LoadTimingInfo load_timing_info_before_redirect; |
| 4558 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeRedirect( |
| 4559 | &load_timing_info_before_redirect)); |
| 4560 | TestLoadTimingNotReused(load_timing_info_before_redirect, |
| 4561 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 4562 | |
| 4563 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4564 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4565 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 4566 | |
| 4567 | // Check that a new socket was used on redirect, since the server does not |
| 4568 | // supposed keep-alive sockets, and that the times before the redirect are |
| 4569 | // before the ones recorded for the second request. |
| 4570 | EXPECT_NE(load_timing_info_before_redirect.socket_log_id, |
| 4571 | load_timing_info.socket_log_id); |
| 4572 | EXPECT_LE(load_timing_info_before_redirect.receive_headers_end, |
| 4573 | load_timing_info.connect_timing.connect_start); |
| 4574 | } |
| 4575 | |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 4576 | TEST_F(URLRequestTestHTTP, MultipleRedirectTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4577 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 4578 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4579 | GURL destination_url = http_test_server()->GetURL("/"); |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 4580 | GURL middle_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4581 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
| 4582 | GURL original_url = http_test_server()->GetURL("/server-redirect?" + |
| 4583 | middle_redirect_url.spec()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 4584 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4585 | scoped_ptr<URLRequest> req( |
| 4586 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4587 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4588 | base::RunLoop().Run(); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 4589 | |
| 4590 | EXPECT_EQ(1, d.response_started_count()); |
| 4591 | EXPECT_EQ(2, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4592 | EXPECT_EQ(destination_url, req->url()); |
| 4593 | EXPECT_EQ(original_url, req->original_url()); |
| 4594 | ASSERT_EQ(3U, req->url_chain().size()); |
| 4595 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 4596 | EXPECT_EQ(middle_redirect_url, req->url_chain()[1]); |
| 4597 | EXPECT_EQ(destination_url, req->url_chain()[2]); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 4598 | } |
| 4599 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4600 | // First and second pieces of information logged by delegates to URLRequests. |
| 4601 | const char kFirstDelegateInfo[] = "Wonderful delegate"; |
| 4602 | const char kSecondDelegateInfo[] = "Exciting delegate"; |
| 4603 | |
| 4604 | // Logs delegate information to a URLRequest. The first string is logged |
| 4605 | // synchronously on Start(), using DELEGATE_INFO_DEBUG_ONLY. The second is |
| 4606 | // logged asynchronously, using DELEGATE_INFO_DISPLAY_TO_USER. Then |
| 4607 | // another asynchronous call is used to clear the delegate information |
| 4608 | // before calling a callback. The object then deletes itself. |
| 4609 | class AsyncDelegateLogger : public base::RefCounted<AsyncDelegateLogger> { |
| 4610 | public: |
| 4611 | typedef base::Callback<void()> Callback; |
| 4612 | |
| 4613 | // Each time delegate information is added to the URLRequest, the resulting |
| 4614 | // load state is checked. The expected load state after each request is |
| 4615 | // passed in as an argument. |
| 4616 | static void Run(URLRequest* url_request, |
| 4617 | LoadState expected_first_load_state, |
| 4618 | LoadState expected_second_load_state, |
| 4619 | LoadState expected_third_load_state, |
| 4620 | const Callback& callback) { |
| 4621 | AsyncDelegateLogger* logger = new AsyncDelegateLogger( |
| 4622 | url_request, |
| 4623 | expected_first_load_state, |
| 4624 | expected_second_load_state, |
| 4625 | expected_third_load_state, |
| 4626 | callback); |
| 4627 | logger->Start(); |
| 4628 | } |
| 4629 | |
| 4630 | // Checks that the log entries, starting with log_position, contain the |
| 4631 | // DELEGATE_INFO NetLog events that an AsyncDelegateLogger should have |
| 4632 | // recorded. Returns the index of entry after the expected number of |
| 4633 | // events this logged, or entries.size() if there aren't enough entries. |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 4634 | static size_t CheckDelegateInfo(const TestNetLogEntry::List& entries, |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 4635 | size_t log_position) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4636 | // There should be 4 DELEGATE_INFO events: Two begins and two ends. |
| 4637 | if (log_position + 3 >= entries.size()) { |
| 4638 | ADD_FAILURE() << "Not enough log entries"; |
| 4639 | return entries.size(); |
| 4640 | } |
| 4641 | std::string delegate_info; |
| 4642 | EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); |
| 4643 | EXPECT_EQ(NetLog::PHASE_BEGIN, entries[log_position].phase); |
| 4644 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_info", |
| 4645 | &delegate_info)); |
| 4646 | EXPECT_EQ(kFirstDelegateInfo, delegate_info); |
| 4647 | |
| 4648 | ++log_position; |
| 4649 | EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); |
| 4650 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 4651 | |
| 4652 | ++log_position; |
| 4653 | EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); |
| 4654 | EXPECT_EQ(NetLog::PHASE_BEGIN, entries[log_position].phase); |
| 4655 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_info", |
| 4656 | &delegate_info)); |
| 4657 | EXPECT_EQ(kSecondDelegateInfo, delegate_info); |
| 4658 | |
| 4659 | ++log_position; |
| 4660 | EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); |
| 4661 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 4662 | |
| 4663 | return log_position + 1; |
| 4664 | } |
| 4665 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4666 | // Find delegate request begin and end messages for OnBeforeNetworkStart. |
| 4667 | // Returns the position of the end message. |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 4668 | static size_t ExpectBeforeNetworkEvents(const TestNetLogEntry::List& entries, |
| 4669 | size_t log_position) { |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4670 | log_position = |
| 4671 | ExpectLogContainsSomewhereAfter(entries, |
| 4672 | log_position, |
| 4673 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4674 | NetLog::PHASE_BEGIN); |
| 4675 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4676 | entries[log_position + 1].type); |
| 4677 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position + 1].phase); |
| 4678 | return log_position + 1; |
| 4679 | } |
| 4680 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4681 | private: |
| 4682 | friend class base::RefCounted<AsyncDelegateLogger>; |
| 4683 | |
| 4684 | AsyncDelegateLogger(URLRequest* url_request, |
| 4685 | LoadState expected_first_load_state, |
| 4686 | LoadState expected_second_load_state, |
| 4687 | LoadState expected_third_load_state, |
| 4688 | const Callback& callback) |
| 4689 | : url_request_(url_request), |
| 4690 | expected_first_load_state_(expected_first_load_state), |
| 4691 | expected_second_load_state_(expected_second_load_state), |
| 4692 | expected_third_load_state_(expected_third_load_state), |
| 4693 | callback_(callback) { |
| 4694 | } |
| 4695 | |
| 4696 | ~AsyncDelegateLogger() {} |
| 4697 | |
| 4698 | void Start() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 4699 | url_request_->LogBlockedBy(kFirstDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4700 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 4701 | EXPECT_EQ(expected_first_load_state_, load_state.state); |
| 4702 | EXPECT_NE(ASCIIToUTF16(kFirstDelegateInfo), load_state.param); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 4703 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 4704 | FROM_HERE, base::Bind(&AsyncDelegateLogger::LogSecondDelegate, this)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4705 | } |
| 4706 | |
| 4707 | void LogSecondDelegate() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 4708 | url_request_->LogAndReportBlockedBy(kSecondDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4709 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 4710 | EXPECT_EQ(expected_second_load_state_, load_state.state); |
| 4711 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) { |
| 4712 | EXPECT_EQ(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 4713 | } else { |
| 4714 | EXPECT_NE(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 4715 | } |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 4716 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 4717 | FROM_HERE, base::Bind(&AsyncDelegateLogger::LogComplete, this)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4718 | } |
| 4719 | |
| 4720 | void LogComplete() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 4721 | url_request_->LogUnblocked(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4722 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 4723 | EXPECT_EQ(expected_third_load_state_, load_state.state); |
| 4724 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 4725 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4726 | callback_.Run(); |
| 4727 | } |
| 4728 | |
| 4729 | URLRequest* url_request_; |
| 4730 | const int expected_first_load_state_; |
| 4731 | const int expected_second_load_state_; |
| 4732 | const int expected_third_load_state_; |
| 4733 | const Callback callback_; |
| 4734 | |
| 4735 | DISALLOW_COPY_AND_ASSIGN(AsyncDelegateLogger); |
| 4736 | }; |
| 4737 | |
| 4738 | // NetworkDelegate that logs delegate information before a request is started, |
| 4739 | // before headers are sent, when headers are read, and when auth information |
| 4740 | // is requested. Uses AsyncDelegateLogger. |
| 4741 | class AsyncLoggingNetworkDelegate : public TestNetworkDelegate { |
| 4742 | public: |
| 4743 | AsyncLoggingNetworkDelegate() {} |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4744 | ~AsyncLoggingNetworkDelegate() override {} |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4745 | |
| 4746 | // NetworkDelegate implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4747 | int OnBeforeURLRequest(URLRequest* request, |
| 4748 | const CompletionCallback& callback, |
| 4749 | GURL* new_url) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4750 | TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); |
| 4751 | return RunCallbackAsynchronously(request, callback); |
| 4752 | } |
| 4753 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4754 | int OnBeforeSendHeaders(URLRequest* request, |
| 4755 | const CompletionCallback& callback, |
| 4756 | HttpRequestHeaders* headers) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4757 | TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers); |
| 4758 | return RunCallbackAsynchronously(request, callback); |
| 4759 | } |
| 4760 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4761 | int OnHeadersReceived( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4762 | URLRequest* request, |
| 4763 | const CompletionCallback& callback, |
| 4764 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4765 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 4766 | GURL* allowed_unsafe_redirect_url) override { |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4767 | TestNetworkDelegate::OnHeadersReceived(request, |
| 4768 | callback, |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4769 | original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4770 | override_response_headers, |
| 4771 | allowed_unsafe_redirect_url); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4772 | return RunCallbackAsynchronously(request, callback); |
| 4773 | } |
| 4774 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4775 | NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4776 | URLRequest* request, |
| 4777 | const AuthChallengeInfo& auth_info, |
| 4778 | const AuthCallback& callback, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 4779 | AuthCredentials* credentials) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4780 | AsyncDelegateLogger::Run( |
| 4781 | request, |
| 4782 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4783 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4784 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4785 | base::Bind(&AsyncLoggingNetworkDelegate::SetAuthAndResume, |
| 4786 | callback, credentials)); |
| 4787 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 4788 | } |
| 4789 | |
| 4790 | private: |
| 4791 | static int RunCallbackAsynchronously( |
| 4792 | URLRequest* request, |
| 4793 | const CompletionCallback& callback) { |
| 4794 | AsyncDelegateLogger::Run( |
| 4795 | request, |
| 4796 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4797 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4798 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4799 | base::Bind(callback, OK)); |
| 4800 | return ERR_IO_PENDING; |
| 4801 | } |
| 4802 | |
| 4803 | static void SetAuthAndResume(const AuthCallback& callback, |
| 4804 | AuthCredentials* credentials) { |
| 4805 | *credentials = AuthCredentials(kUser, kSecret); |
| 4806 | callback.Run(NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4807 | } |
| 4808 | |
| 4809 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingNetworkDelegate); |
| 4810 | }; |
| 4811 | |
| 4812 | // URLRequest::Delegate that logs delegate information when the headers |
| 4813 | // are received, when each read completes, and during redirects. Uses |
| 4814 | // AsyncDelegateLogger. Can optionally cancel a request in any phase. |
| 4815 | // |
| 4816 | // Inherits from TestDelegate to reuse the TestDelegate code to handle |
| 4817 | // advancing to the next step in most cases, as well as cancellation. |
| 4818 | class AsyncLoggingUrlRequestDelegate : public TestDelegate { |
| 4819 | public: |
| 4820 | enum CancelStage { |
| 4821 | NO_CANCEL = 0, |
| 4822 | CANCEL_ON_RECEIVED_REDIRECT, |
| 4823 | CANCEL_ON_RESPONSE_STARTED, |
| 4824 | CANCEL_ON_READ_COMPLETED |
| 4825 | }; |
| 4826 | |
| 4827 | explicit AsyncLoggingUrlRequestDelegate(CancelStage cancel_stage) |
| 4828 | : cancel_stage_(cancel_stage) { |
| 4829 | if (cancel_stage == CANCEL_ON_RECEIVED_REDIRECT) |
| 4830 | set_cancel_in_received_redirect(true); |
| 4831 | else if (cancel_stage == CANCEL_ON_RESPONSE_STARTED) |
| 4832 | set_cancel_in_response_started(true); |
| 4833 | else if (cancel_stage == CANCEL_ON_READ_COMPLETED) |
| 4834 | set_cancel_in_received_data(true); |
| 4835 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4836 | ~AsyncLoggingUrlRequestDelegate() override {} |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4837 | |
| 4838 | // URLRequest::Delegate implementation: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4839 | void OnReceivedRedirect(URLRequest* request, |
| 4840 | const RedirectInfo& redirect_info, |
| 4841 | bool* defer_redirect) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4842 | *defer_redirect = true; |
| 4843 | AsyncDelegateLogger::Run( |
| 4844 | request, |
| 4845 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4846 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4847 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4848 | base::Bind( |
| 4849 | &AsyncLoggingUrlRequestDelegate::OnReceivedRedirectLoggingComplete, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4850 | base::Unretained(this), request, redirect_info)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4851 | } |
| 4852 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4853 | void OnResponseStarted(URLRequest* request) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4854 | AsyncDelegateLogger::Run( |
| 4855 | request, |
| 4856 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4857 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4858 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4859 | base::Bind( |
| 4860 | &AsyncLoggingUrlRequestDelegate::OnResponseStartedLoggingComplete, |
| 4861 | base::Unretained(this), request)); |
| 4862 | } |
| 4863 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4864 | void OnReadCompleted(URLRequest* request, int bytes_read) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4865 | AsyncDelegateLogger::Run( |
| 4866 | request, |
| 4867 | LOAD_STATE_IDLE, |
| 4868 | LOAD_STATE_IDLE, |
| 4869 | LOAD_STATE_IDLE, |
| 4870 | base::Bind( |
| 4871 | &AsyncLoggingUrlRequestDelegate::AfterReadCompletedLoggingComplete, |
| 4872 | base::Unretained(this), request, bytes_read)); |
| 4873 | } |
| 4874 | |
| 4875 | private: |
| 4876 | void OnReceivedRedirectLoggingComplete(URLRequest* request, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4877 | const RedirectInfo& redirect_info) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4878 | bool defer_redirect = false; |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4879 | TestDelegate::OnReceivedRedirect(request, redirect_info, &defer_redirect); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4880 | // FollowDeferredRedirect should not be called after cancellation. |
| 4881 | if (cancel_stage_ == CANCEL_ON_RECEIVED_REDIRECT) |
| 4882 | return; |
| 4883 | if (!defer_redirect) |
| 4884 | request->FollowDeferredRedirect(); |
| 4885 | } |
| 4886 | |
| 4887 | void OnResponseStartedLoggingComplete(URLRequest* request) { |
| 4888 | // The parent class continues the request. |
| 4889 | TestDelegate::OnResponseStarted(request); |
| 4890 | } |
| 4891 | |
| 4892 | void AfterReadCompletedLoggingComplete(URLRequest* request, int bytes_read) { |
| 4893 | // The parent class continues the request. |
| 4894 | TestDelegate::OnReadCompleted(request, bytes_read); |
| 4895 | } |
| 4896 | |
| 4897 | const CancelStage cancel_stage_; |
| 4898 | |
| 4899 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingUrlRequestDelegate); |
| 4900 | }; |
| 4901 | |
| 4902 | // Tests handling of delegate info before a request starts. |
| 4903 | TEST_F(URLRequestTestHTTP, DelegateInfoBeforeStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4904 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4905 | |
| 4906 | TestDelegate request_delegate; |
| 4907 | TestURLRequestContext context(true); |
| 4908 | context.set_network_delegate(NULL); |
| 4909 | context.set_net_log(&net_log_); |
| 4910 | context.Init(); |
| 4911 | |
| 4912 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4913 | scoped_ptr<URLRequest> r( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4914 | context.CreateRequest(http_test_server()->GetURL("/defaultresponse"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4915 | DEFAULT_PRIORITY, &request_delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4916 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4917 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 4918 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4919 | |
| 4920 | AsyncDelegateLogger::Run( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4921 | r.get(), |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4922 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4923 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4924 | LOAD_STATE_IDLE, |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4925 | base::Bind(&URLRequest::Start, base::Unretained(r.get()))); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4926 | |
| 4927 | base::RunLoop().Run(); |
| 4928 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4929 | EXPECT_EQ(200, r->GetResponseCode()); |
| 4930 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4931 | } |
| 4932 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 4933 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4934 | net_log_.GetEntries(&entries); |
| 4935 | size_t log_position = ExpectLogContainsSomewhereAfter( |
| 4936 | entries, |
| 4937 | 0, |
| 4938 | NetLog::TYPE_DELEGATE_INFO, |
| 4939 | NetLog::PHASE_BEGIN); |
| 4940 | |
| 4941 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, log_position); |
| 4942 | |
| 4943 | // Nothing else should add any delegate info to the request. |
| 4944 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4945 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4946 | } |
| 4947 | |
| 4948 | // Tests handling of delegate info from a network delegate. |
| 4949 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfo) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4950 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4951 | |
| 4952 | TestDelegate request_delegate; |
| 4953 | AsyncLoggingNetworkDelegate network_delegate; |
| 4954 | TestURLRequestContext context(true); |
| 4955 | context.set_network_delegate(&network_delegate); |
| 4956 | context.set_net_log(&net_log_); |
| 4957 | context.Init(); |
| 4958 | |
| 4959 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4960 | scoped_ptr<URLRequest> r( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 4961 | context.CreateRequest(http_test_server()->GetURL("/simple.html"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4962 | DEFAULT_PRIORITY, &request_delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4963 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4964 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 4965 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4966 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4967 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4968 | base::RunLoop().Run(); |
| 4969 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4970 | EXPECT_EQ(200, r->GetResponseCode()); |
| 4971 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4972 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4973 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4974 | } |
| 4975 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4976 | |
| 4977 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 4978 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4979 | net_log_.GetEntries(&entries); |
| 4980 | for (size_t i = 0; i < 3; ++i) { |
| 4981 | log_position = ExpectLogContainsSomewhereAfter( |
| 4982 | entries, |
| 4983 | log_position + 1, |
| 4984 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4985 | NetLog::PHASE_BEGIN); |
| 4986 | |
| 4987 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4988 | log_position + 1); |
| 4989 | |
| 4990 | ASSERT_LT(log_position, entries.size()); |
| 4991 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4992 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4993 | |
| 4994 | if (i == 1) { |
| 4995 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 4996 | entries, log_position + 1); |
| 4997 | } |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4998 | } |
| 4999 | |
| 5000 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 5001 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 5002 | } |
| 5003 | |
| 5004 | // Tests handling of delegate info from a network delegate in the case of an |
| 5005 | // HTTP redirect. |
| 5006 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5007 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5008 | |
| 5009 | TestDelegate request_delegate; |
| 5010 | AsyncLoggingNetworkDelegate network_delegate; |
| 5011 | TestURLRequestContext context(true); |
| 5012 | context.set_network_delegate(&network_delegate); |
| 5013 | context.set_net_log(&net_log_); |
| 5014 | context.Init(); |
| 5015 | |
| 5016 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5017 | scoped_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5018 | http_test_server()->GetURL("/server-redirect?simple.html"), |
| 5019 | DEFAULT_PRIORITY, &request_delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5020 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5021 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5022 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5023 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5024 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5025 | base::RunLoop().Run(); |
| 5026 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5027 | EXPECT_EQ(200, r->GetResponseCode()); |
| 5028 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5029 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 5030 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5031 | } |
| 5032 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5033 | |
| 5034 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5035 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5036 | net_log_.GetEntries(&entries); |
| 5037 | // The NetworkDelegate logged information in OnBeforeURLRequest, |
| 5038 | // OnBeforeSendHeaders, and OnHeadersReceived. |
| 5039 | for (size_t i = 0; i < 3; ++i) { |
| 5040 | log_position = ExpectLogContainsSomewhereAfter( |
| 5041 | entries, |
| 5042 | log_position + 1, |
| 5043 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 5044 | NetLog::PHASE_BEGIN); |
| 5045 | |
| 5046 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5047 | log_position + 1); |
| 5048 | |
| 5049 | ASSERT_LT(log_position, entries.size()); |
| 5050 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 5051 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5052 | |
| 5053 | if (i == 1) { |
| 5054 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 5055 | entries, log_position + 1); |
| 5056 | } |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5057 | } |
| 5058 | |
| 5059 | // The URLRequest::Delegate then gets informed about the redirect. |
| 5060 | log_position = ExpectLogContainsSomewhereAfter( |
| 5061 | entries, |
| 5062 | log_position + 1, |
| 5063 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 5064 | NetLog::PHASE_BEGIN); |
| 5065 | |
| 5066 | // The NetworkDelegate logged information in the same three events as before. |
| 5067 | for (size_t i = 0; i < 3; ++i) { |
| 5068 | log_position = ExpectLogContainsSomewhereAfter( |
| 5069 | entries, |
| 5070 | log_position + 1, |
| 5071 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 5072 | NetLog::PHASE_BEGIN); |
| 5073 | |
| 5074 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5075 | log_position + 1); |
| 5076 | |
| 5077 | ASSERT_LT(log_position, entries.size()); |
| 5078 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 5079 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 5080 | } |
| 5081 | |
| 5082 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 5083 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 5084 | } |
| 5085 | |
| 5086 | // Tests handling of delegate info from a network delegate in the case of HTTP |
| 5087 | // AUTH. |
| 5088 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5089 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5090 | |
| 5091 | TestDelegate request_delegate; |
| 5092 | AsyncLoggingNetworkDelegate network_delegate; |
| 5093 | TestURLRequestContext context(true); |
| 5094 | context.set_network_delegate(&network_delegate); |
| 5095 | context.set_net_log(&net_log_); |
| 5096 | context.Init(); |
| 5097 | |
| 5098 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5099 | scoped_ptr<URLRequest> r( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5100 | context.CreateRequest(http_test_server()->GetURL("/auth-basic"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5101 | DEFAULT_PRIORITY, &request_delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5102 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5103 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 5104 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5105 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5106 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5107 | base::RunLoop().Run(); |
| 5108 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5109 | EXPECT_EQ(200, r->GetResponseCode()); |
| 5110 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5111 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 5112 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 5113 | } |
| 5114 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 5115 | |
| 5116 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5117 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5118 | net_log_.GetEntries(&entries); |
| 5119 | // The NetworkDelegate should have logged information in OnBeforeURLRequest, |
| 5120 | // OnBeforeSendHeaders, OnHeadersReceived, OnAuthRequired, and then again in |
| 5121 | // OnBeforeURLRequest and OnBeforeSendHeaders. |
| 5122 | for (size_t i = 0; i < 6; ++i) { |
| 5123 | log_position = ExpectLogContainsSomewhereAfter( |
| 5124 | entries, |
| 5125 | log_position + 1, |
| 5126 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 5127 | NetLog::PHASE_BEGIN); |
| 5128 | |
| 5129 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5130 | log_position + 1); |
| 5131 | |
| 5132 | ASSERT_LT(log_position, entries.size()); |
| 5133 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 5134 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5135 | |
| 5136 | if (i == 1) { |
| 5137 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 5138 | entries, log_position + 1); |
| 5139 | } |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5140 | } |
| 5141 | |
| 5142 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 5143 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 5144 | } |
| 5145 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5146 | // TODO(svaldez): Update tests to use EmbeddedTestServer. |
| 5147 | #if !defined(OS_IOS) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5148 | // Tests handling of delegate info from a URLRequest::Delegate. |
| 5149 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfo) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5150 | SpawnedTestServer test_server(SpawnedTestServer::TYPE_HTTP, |
| 5151 | SpawnedTestServer::kLocalhost, |
| 5152 | base::FilePath(kTestFilePath)); |
| 5153 | |
| 5154 | ASSERT_TRUE(test_server.Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5155 | |
| 5156 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 5157 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 5158 | TestURLRequestContext context(true); |
| 5159 | context.set_network_delegate(NULL); |
| 5160 | context.set_net_log(&net_log_); |
| 5161 | context.Init(); |
| 5162 | |
| 5163 | { |
| 5164 | // A chunked response with delays between chunks is used to make sure that |
| 5165 | // attempts by the URLRequest delegate to log information while reading the |
| 5166 | // body are ignored. Since they are ignored, this test is robust against |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5167 | // the possibility of multiple reads being combined in the unlikely event |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5168 | // that it occurs. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5169 | scoped_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5170 | test_server.GetURL("/chunked?waitBetweenChunks=20"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5171 | &request_delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5172 | LoadStateWithParam load_state = r->GetLoadState(); |
| 5173 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5174 | base::RunLoop().Run(); |
| 5175 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5176 | EXPECT_EQ(200, r->GetResponseCode()); |
| 5177 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5178 | } |
| 5179 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5180 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5181 | net_log_.GetEntries(&entries); |
| 5182 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5183 | size_t log_position = 0; |
| 5184 | |
| 5185 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 5186 | entries, log_position); |
| 5187 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5188 | // The delegate info should only have been logged on header complete. Other |
| 5189 | // times it should silently be ignored. |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5190 | log_position = |
| 5191 | ExpectLogContainsSomewhereAfter(entries, |
| 5192 | log_position + 1, |
| 5193 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 5194 | NetLog::PHASE_BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5195 | |
| 5196 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5197 | log_position + 1); |
| 5198 | |
| 5199 | ASSERT_LT(log_position, entries.size()); |
| 5200 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 5201 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 5202 | |
| 5203 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 5204 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 5205 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 5206 | entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE)); |
| 5207 | } |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5208 | #endif // !defined(OS_IOS) |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5209 | |
| 5210 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 5211 | // an HTTP redirect. |
| 5212 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfoOnRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5213 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5214 | |
| 5215 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 5216 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 5217 | TestURLRequestContext context(true); |
| 5218 | context.set_network_delegate(NULL); |
| 5219 | context.set_net_log(&net_log_); |
| 5220 | context.Init(); |
| 5221 | |
| 5222 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5223 | scoped_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5224 | http_test_server()->GetURL("/server-redirect?simple.html"), |
| 5225 | DEFAULT_PRIORITY, &request_delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5226 | LoadStateWithParam load_state = r->GetLoadState(); |
| 5227 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5228 | base::RunLoop().Run(); |
| 5229 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5230 | EXPECT_EQ(200, r->GetResponseCode()); |
| 5231 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5232 | } |
| 5233 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5234 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5235 | net_log_.GetEntries(&entries); |
| 5236 | |
| 5237 | // Delegate info should only have been logged in OnReceivedRedirect and |
| 5238 | // OnResponseStarted. |
| 5239 | size_t log_position = 0; |
| 5240 | for (int i = 0; i < 2; ++i) { |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5241 | if (i == 0) { |
| 5242 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 5243 | entries, log_position) + 1; |
| 5244 | } |
| 5245 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5246 | log_position = ExpectLogContainsSomewhereAfter( |
| 5247 | entries, |
| 5248 | log_position, |
| 5249 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 5250 | NetLog::PHASE_BEGIN); |
| 5251 | |
| 5252 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5253 | log_position + 1); |
| 5254 | |
| 5255 | ASSERT_LT(log_position, entries.size()); |
| 5256 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 5257 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 5258 | } |
| 5259 | |
| 5260 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 5261 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 5262 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 5263 | entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE)); |
| 5264 | } |
| 5265 | |
| 5266 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 5267 | // an HTTP redirect, with cancellation at various points. |
| 5268 | TEST_F(URLRequestTestHTTP, URLRequestDelegateOnRedirectCancelled) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5269 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5270 | |
| 5271 | const AsyncLoggingUrlRequestDelegate::CancelStage kCancelStages[] = { |
| 5272 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RECEIVED_REDIRECT, |
| 5273 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RESPONSE_STARTED, |
| 5274 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_READ_COMPLETED, |
| 5275 | }; |
| 5276 | |
| 5277 | for (size_t test_case = 0; test_case < arraysize(kCancelStages); |
| 5278 | ++test_case) { |
| 5279 | AsyncLoggingUrlRequestDelegate request_delegate(kCancelStages[test_case]); |
| 5280 | TestURLRequestContext context(true); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 5281 | TestNetLog net_log; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5282 | context.set_network_delegate(NULL); |
| 5283 | context.set_net_log(&net_log); |
| 5284 | context.Init(); |
| 5285 | |
| 5286 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5287 | scoped_ptr<URLRequest> r(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5288 | http_test_server()->GetURL("/server-redirect?simple.html"), |
| 5289 | DEFAULT_PRIORITY, &request_delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5290 | LoadStateWithParam load_state = r->GetLoadState(); |
| 5291 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5292 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5293 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5294 | } |
| 5295 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 5296 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5297 | net_log.GetEntries(&entries); |
| 5298 | |
| 5299 | // Delegate info is always logged in both OnReceivedRedirect and |
| 5300 | // OnResponseStarted. In the CANCEL_ON_RECEIVED_REDIRECT, the |
| 5301 | // OnResponseStarted delegate call is after cancellation, but logging is |
| 5302 | // still currently supported in that call. |
| 5303 | size_t log_position = 0; |
| 5304 | for (int i = 0; i < 2; ++i) { |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5305 | if (i == 0) { |
| 5306 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 5307 | entries, log_position) + 1; |
| 5308 | } |
| 5309 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 5310 | log_position = ExpectLogContainsSomewhereAfter( |
| 5311 | entries, |
| 5312 | log_position, |
| 5313 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 5314 | NetLog::PHASE_BEGIN); |
| 5315 | |
| 5316 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 5317 | log_position + 1); |
| 5318 | |
| 5319 | ASSERT_LT(log_position, entries.size()); |
| 5320 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 5321 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 5322 | } |
| 5323 | |
| 5324 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 5325 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 5326 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 5327 | entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE)); |
| 5328 | } |
| 5329 | } |
| 5330 | |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5331 | namespace { |
| 5332 | |
| 5333 | const char kExtraHeader[] = "Allow-Snafu"; |
| 5334 | const char kExtraValue[] = "fubar"; |
| 5335 | |
| 5336 | class RedirectWithAdditionalHeadersDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5337 | void OnReceivedRedirect(URLRequest* request, |
| 5338 | const RedirectInfo& redirect_info, |
| 5339 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5340 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5341 | request->SetExtraRequestHeaderByName(kExtraHeader, kExtraValue, false); |
| 5342 | } |
| 5343 | }; |
| 5344 | |
| 5345 | } // namespace |
| 5346 | |
| 5347 | TEST_F(URLRequestTestHTTP, RedirectWithAdditionalHeadersTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5348 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5349 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5350 | GURL destination_url = |
| 5351 | http_test_server()->GetURL("/echoheader?" + std::string(kExtraHeader)); |
| 5352 | GURL original_url = |
| 5353 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5354 | RedirectWithAdditionalHeadersDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5355 | scoped_ptr<URLRequest> req( |
| 5356 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5357 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5358 | base::RunLoop().Run(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5359 | |
| 5360 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5361 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5362 | EXPECT_TRUE(headers.GetHeader(kExtraHeader, &value)); |
| 5363 | EXPECT_EQ(kExtraValue, value); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5364 | EXPECT_FALSE(req->is_pending()); |
| 5365 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 5366 | EXPECT_EQ(kExtraValue, d.data_received()); |
| 5367 | } |
| 5368 | |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5369 | namespace { |
| 5370 | |
| 5371 | const char kExtraHeaderToRemove[] = "To-Be-Removed"; |
| 5372 | |
| 5373 | class RedirectWithHeaderRemovalDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 5374 | void OnReceivedRedirect(URLRequest* request, |
| 5375 | const RedirectInfo& redirect_info, |
| 5376 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 5377 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5378 | request->RemoveRequestHeaderByName(kExtraHeaderToRemove); |
| 5379 | } |
| 5380 | }; |
| 5381 | |
| 5382 | } // namespace |
| 5383 | |
| 5384 | TEST_F(URLRequestTestHTTP, RedirectWithHeaderRemovalTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5385 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5386 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5387 | GURL destination_url = http_test_server()->GetURL( |
| 5388 | "/echoheader?" + std::string(kExtraHeaderToRemove)); |
| 5389 | GURL original_url = |
| 5390 | http_test_server()->GetURL("/server-redirect?" + destination_url.spec()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5391 | RedirectWithHeaderRemovalDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5392 | scoped_ptr<URLRequest> req( |
| 5393 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5394 | req->SetExtraRequestHeaderByName(kExtraHeaderToRemove, "dummy", false); |
| 5395 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5396 | base::RunLoop().Run(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5397 | |
| 5398 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5399 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5400 | EXPECT_FALSE(headers.GetHeader(kExtraHeaderToRemove, &value)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5401 | EXPECT_FALSE(req->is_pending()); |
| 5402 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 5403 | EXPECT_EQ("None", d.data_received()); |
| 5404 | } |
| 5405 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5406 | TEST_F(URLRequestTestHTTP, CancelTest) { |
| 5407 | TestDelegate d; |
| 5408 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5409 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5410 | GURL("http://www.google.com/"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5411 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5412 | r->Start(); |
| 5413 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5414 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5415 | r->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5416 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5417 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5418 | |
| 5419 | // We expect to receive OnResponseStarted even though the request has been |
| 5420 | // cancelled. |
| 5421 | EXPECT_EQ(1, d.response_started_count()); |
| 5422 | EXPECT_EQ(0, d.bytes_received()); |
| 5423 | EXPECT_FALSE(d.received_data_before_response()); |
| 5424 | } |
| 5425 | } |
| 5426 | |
| 5427 | TEST_F(URLRequestTestHTTP, CancelTest2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5428 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5429 | |
| 5430 | TestDelegate d; |
| 5431 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5432 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5433 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5434 | |
| 5435 | d.set_cancel_in_response_started(true); |
| 5436 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5437 | r->Start(); |
| 5438 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5439 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5440 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5441 | |
| 5442 | EXPECT_EQ(1, d.response_started_count()); |
| 5443 | EXPECT_EQ(0, d.bytes_received()); |
| 5444 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5445 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5446 | } |
| 5447 | } |
| 5448 | |
| 5449 | TEST_F(URLRequestTestHTTP, CancelTest3) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5450 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5451 | |
| 5452 | TestDelegate d; |
| 5453 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5454 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5455 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5456 | |
| 5457 | d.set_cancel_in_received_data(true); |
| 5458 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5459 | r->Start(); |
| 5460 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5461 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5462 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5463 | |
| 5464 | EXPECT_EQ(1, d.response_started_count()); |
| 5465 | // There is no guarantee about how much data was received |
| 5466 | // before the cancel was issued. It could have been 0 bytes, |
| 5467 | // or it could have been all the bytes. |
| 5468 | // EXPECT_EQ(0, d.bytes_received()); |
| 5469 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5470 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5471 | } |
| 5472 | } |
| 5473 | |
| 5474 | TEST_F(URLRequestTestHTTP, CancelTest4) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5475 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5476 | |
| 5477 | TestDelegate d; |
| 5478 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5479 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5480 | http_test_server()->GetURL("/"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5481 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5482 | r->Start(); |
| 5483 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5484 | |
| 5485 | // The request will be implicitly canceled when it is destroyed. The |
| 5486 | // test delegate must not post a quit message when this happens because |
| 5487 | // this test doesn't actually have a message loop. The quit message would |
| 5488 | // get put on this thread's message queue and the next test would exit |
| 5489 | // early, causing problems. |
| 5490 | d.set_quit_on_complete(false); |
| 5491 | } |
| 5492 | // expect things to just cleanup properly. |
| 5493 | |
| 5494 | // we won't actually get a received reponse here because we've never run the |
| 5495 | // message loop |
| 5496 | EXPECT_FALSE(d.received_data_before_response()); |
| 5497 | EXPECT_EQ(0, d.bytes_received()); |
| 5498 | } |
| 5499 | |
| 5500 | TEST_F(URLRequestTestHTTP, CancelTest5) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5501 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5502 | |
| 5503 | // populate cache |
| 5504 | { |
| 5505 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5506 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5507 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5508 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5509 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5510 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5511 | } |
| 5512 | |
| 5513 | // cancel read from cache (see bug 990242) |
| 5514 | { |
| 5515 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5516 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5517 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5518 | r->Start(); |
| 5519 | r->Cancel(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5520 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5521 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5522 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5523 | EXPECT_EQ(1, d.response_started_count()); |
| 5524 | EXPECT_EQ(0, d.bytes_received()); |
| 5525 | EXPECT_FALSE(d.received_data_before_response()); |
| 5526 | } |
| 5527 | } |
| 5528 | |
| 5529 | TEST_F(URLRequestTestHTTP, PostTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5530 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5531 | HTTPUploadDataOperationTest("POST"); |
| 5532 | } |
| 5533 | |
| 5534 | TEST_F(URLRequestTestHTTP, PutTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5535 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5536 | HTTPUploadDataOperationTest("PUT"); |
| 5537 | } |
| 5538 | |
| 5539 | TEST_F(URLRequestTestHTTP, PostEmptyTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5540 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5541 | |
| 5542 | TestDelegate d; |
| 5543 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5544 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5545 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5546 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5547 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5548 | r->Start(); |
| 5549 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5550 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5551 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5552 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 5553 | ASSERT_EQ(1, d.response_started_count()) |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5554 | << "request failed: " << r->status().status() |
| 5555 | << ", error: " << r->status().error(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5556 | |
| 5557 | EXPECT_FALSE(d.received_data_before_response()); |
| 5558 | EXPECT_TRUE(d.data_received().empty()); |
| 5559 | } |
| 5560 | } |
| 5561 | |
| 5562 | TEST_F(URLRequestTestHTTP, PostFileTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5563 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5564 | |
| 5565 | TestDelegate d; |
| 5566 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5567 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5568 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5569 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5570 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 5571 | base::FilePath dir; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5572 | PathService::Get(base::DIR_EXE, &dir); |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 5573 | base::SetCurrentDirectory(dir); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5574 | |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 5575 | std::vector<scoped_ptr<UploadElementReader>> element_readers; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5576 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 5577 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5578 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 5579 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5580 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame^] | 5581 | element_readers.push_back(make_scoped_ptr(new UploadFileElementReader( |
| 5582 | base::ThreadTaskRunnerHandle::Get().get(), path, 0, |
| 5583 | std::numeric_limits<uint64_t>::max(), base::Time()))); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 5584 | r->set_upload(make_scoped_ptr<UploadDataStream>( |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 5585 | new ElementsUploadDataStream(std::move(element_readers), 0))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5586 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5587 | r->Start(); |
| 5588 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5589 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5590 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5591 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame^] | 5592 | int64_t size64 = 0; |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 5593 | ASSERT_EQ(true, base::GetFileSize(path, &size64)); |
| 5594 | ASSERT_LE(size64, std::numeric_limits<int>::max()); |
| 5595 | int size = static_cast<int>(size64); |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 5596 | scoped_ptr<char[]> buf(new char[size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5597 | |
[email protected] | 7600d0b | 2013-12-08 21:43:30 | [diff] [blame] | 5598 | ASSERT_EQ(size, base::ReadFile(path, buf.get(), size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5599 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 5600 | ASSERT_EQ(1, d.response_started_count()) |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5601 | << "request failed: " << r->status().status() |
| 5602 | << ", error: " << r->status().error(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5603 | |
| 5604 | EXPECT_FALSE(d.received_data_before_response()); |
| 5605 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 5606 | EXPECT_EQ(size, d.bytes_received()); |
| 5607 | EXPECT_EQ(std::string(&buf[0], size), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5608 | } |
| 5609 | } |
| 5610 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5611 | TEST_F(URLRequestTestHTTP, PostUnreadableFileTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5612 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5613 | |
| 5614 | TestDelegate d; |
| 5615 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5616 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5617 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5618 | r->set_method("POST"); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5619 | |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 5620 | std::vector<scoped_ptr<UploadElementReader>> element_readers; |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5621 | |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 5622 | element_readers.push_back(make_scoped_ptr(new UploadFileElementReader( |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 5623 | base::ThreadTaskRunnerHandle::Get().get(), |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5624 | base::FilePath(FILE_PATH_LITERAL( |
| 5625 | "c:\\path\\to\\non\\existant\\file.randomness.12345")), |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame^] | 5626 | 0, std::numeric_limits<uint64_t>::max(), base::Time()))); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 5627 | r->set_upload(make_scoped_ptr<UploadDataStream>( |
olli.raula | 6df48b2a | 2015-11-26 07:40:22 | [diff] [blame] | 5628 | new ElementsUploadDataStream(std::move(element_readers), 0))); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5629 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5630 | r->Start(); |
| 5631 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5632 | |
| 5633 | base::RunLoop().Run(); |
| 5634 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5635 | EXPECT_TRUE(d.request_failed()); |
| 5636 | EXPECT_FALSE(d.received_data_before_response()); |
| 5637 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5638 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
| 5639 | EXPECT_EQ(ERR_FILE_NOT_FOUND, r->status().error()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5640 | } |
| 5641 | } |
| 5642 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5643 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5644 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5645 | |
| 5646 | TestDelegate d; |
| 5647 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5648 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5649 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5650 | r->EnableChunkedUpload(); |
| 5651 | r->set_method("POST"); |
| 5652 | AddChunksToUpload(r.get()); |
| 5653 | r->Start(); |
| 5654 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5655 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5656 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5657 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5658 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5659 | } |
| 5660 | } |
| 5661 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 5662 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataJustAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5663 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 5664 | |
| 5665 | TestDelegate d; |
| 5666 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5667 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5668 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5669 | r->EnableChunkedUpload(); |
| 5670 | r->set_method("POST"); |
| 5671 | r->Start(); |
| 5672 | EXPECT_TRUE(r->is_pending()); |
| 5673 | AddChunksToUpload(r.get()); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5674 | base::RunLoop().Run(); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 5675 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5676 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 5677 | } |
| 5678 | } |
| 5679 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5680 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5681 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5682 | |
| 5683 | TestDelegate d; |
| 5684 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5685 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5686 | http_test_server()->GetURL("/echo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5687 | r->EnableChunkedUpload(); |
| 5688 | r->set_method("POST"); |
| 5689 | r->Start(); |
| 5690 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5691 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5692 | base::RunLoop().RunUntilIdle(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5693 | AddChunksToUpload(r.get()); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5694 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5695 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5696 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5697 | } |
| 5698 | } |
| 5699 | |
| 5700 | TEST_F(URLRequestTestHTTP, ResponseHeadersTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5701 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5702 | |
| 5703 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5704 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5705 | http_test_server()->GetURL("/with-headers.html"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5706 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5707 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5708 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5709 | const HttpResponseHeaders* headers = req->response_headers(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5710 | |
| 5711 | // Simple sanity check that response_info() accesses the same data. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5712 | EXPECT_EQ(headers, req->response_info().headers.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5713 | |
| 5714 | std::string header; |
| 5715 | EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header)); |
| 5716 | EXPECT_EQ("private", header); |
| 5717 | |
| 5718 | header.clear(); |
| 5719 | EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header)); |
| 5720 | EXPECT_EQ("text/html; charset=ISO-8859-1", header); |
| 5721 | |
| 5722 | // The response has two "X-Multiple-Entries" headers. |
| 5723 | // This verfies our output has them concatenated together. |
| 5724 | header.clear(); |
| 5725 | EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header)); |
| 5726 | EXPECT_EQ("a, b", header); |
| 5727 | } |
| 5728 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5729 | // TODO(svaldez): iOS tests are flaky with EmbeddedTestServer and transport |
| 5730 | // security state. (see http://crbug.com/550977). |
| 5731 | #if !defined(OS_IOS) |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5732 | TEST_F(URLRequestTestHTTP, ProcessSTS) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5733 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 5734 | https_test_server.SetSSLConfig( |
| 5735 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 5736 | https_test_server.ServeFilesFromSourceDirectory( |
| 5737 | base::FilePath(kTestFilePath)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5738 | ASSERT_TRUE(https_test_server.Start()); |
| 5739 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5740 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5741 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5742 | scoped_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5743 | https_test_server.GetURL("/hsts-headers.html"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5744 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5745 | base::RunLoop().Run(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5746 | |
| 5747 | TransportSecurityState* security_state = |
| 5748 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 5749 | TransportSecurityState::STSState sts_state; |
| 5750 | TransportSecurityState::PKPState pkp_state; |
| 5751 | EXPECT_TRUE( |
| 5752 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 5753 | EXPECT_FALSE( |
| 5754 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 5755 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 5756 | sts_state.upgrade_mode); |
| 5757 | EXPECT_TRUE(sts_state.include_subdomains); |
| 5758 | EXPECT_FALSE(pkp_state.include_subdomains); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 5759 | #if defined(OS_ANDROID) |
| 5760 | // Android's CertVerifyProc does not (yet) handle pins. |
| 5761 | #else |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 5762 | EXPECT_FALSE(pkp_state.HasPublicKeyPins()); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 5763 | #endif |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5764 | } |
| 5765 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5766 | TEST_F(URLRequestTestHTTP, STSNotProcessedOnIP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5767 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 5768 | https_test_server.ServeFilesFromSourceDirectory( |
| 5769 | base::FilePath(kTestFilePath)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5770 | ASSERT_TRUE(https_test_server.Start()); |
| 5771 | // Make sure this test fails if the test server is changed to not |
| 5772 | // listen on an IP by default. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5773 | ASSERT_TRUE(https_test_server.GetURL("/").HostIsIPAddress()); |
| 5774 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5775 | |
| 5776 | TestDelegate d; |
| 5777 | scoped_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5778 | https_test_server.GetURL("/hsts-headers.html"), DEFAULT_PRIORITY, &d)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5779 | request->Start(); |
| 5780 | base::RunLoop().Run(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5781 | TransportSecurityState* security_state = |
| 5782 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 5783 | TransportSecurityState::STSState sts_state; |
| 5784 | EXPECT_FALSE( |
| 5785 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5786 | } |
| 5787 | |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 5788 | // Android's CertVerifyProc does not (yet) handle pins. Therefore, it will |
| 5789 | // reject HPKP headers, and a test setting only HPKP headers will fail (no |
| 5790 | // PKPState present because header rejected). |
| 5791 | #if defined(OS_ANDROID) |
| 5792 | #define MAYBE_ProcessPKP DISABLED_ProcessPKP |
| 5793 | #define MAYBE_ProcessPKPAndSendReport DISABLED_ProcessPKPAndSendReport |
| 5794 | #define MAYBE_ProcessPKPReportOnly DISABLED_ProcessPKPReportOnly |
| 5795 | #define MAYBE_ProcessPKPReportOnlyWithNoViolation \ |
| 5796 | DISABLED_ProcessPKPReportOnlyWithNoViolation |
| 5797 | #else |
| 5798 | #define MAYBE_ProcessPKP ProcessPKP |
| 5799 | #define MAYBE_ProcessPKPAndSendReport ProcessPKPAndSendReport |
| 5800 | #define MAYBE_ProcessPKPReportOnly ProcessPKPReportOnly |
| 5801 | #define MAYBE_ProcessPKPReportOnlyWithNoViolation \ |
| 5802 | ProcessPKPReportOnlyWithNoViolation |
| 5803 | #endif |
| 5804 | |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 5805 | namespace { |
| 5806 | const char kHPKPReportUri[] = "https://hpkp-report.test"; |
| 5807 | } // namespace |
| 5808 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5809 | // Tests that enabling HPKP on a domain does not affect the HSTS |
| 5810 | // validity/expiration. |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 5811 | TEST_F(URLRequestTestHTTP, MAYBE_ProcessPKP) { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 5812 | GURL report_uri(kHPKPReportUri); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5813 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 5814 | https_test_server.SetSSLConfig( |
| 5815 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 5816 | https_test_server.ServeFilesFromSourceDirectory( |
| 5817 | base::FilePath(kTestFilePath)); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5818 | ASSERT_TRUE(https_test_server.Start()); |
| 5819 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5820 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5821 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5822 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5823 | scoped_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5824 | https_test_server.GetURL("/hpkp-headers.html"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5825 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5826 | base::RunLoop().Run(); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5827 | TransportSecurityState* security_state = |
| 5828 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 5829 | TransportSecurityState::STSState sts_state; |
| 5830 | TransportSecurityState::PKPState pkp_state; |
| 5831 | EXPECT_FALSE( |
| 5832 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 5833 | EXPECT_TRUE( |
| 5834 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 5835 | EXPECT_EQ(TransportSecurityState::STSState::MODE_DEFAULT, |
| 5836 | sts_state.upgrade_mode); |
| 5837 | EXPECT_FALSE(sts_state.include_subdomains); |
| 5838 | EXPECT_FALSE(pkp_state.include_subdomains); |
| 5839 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 5840 | EXPECT_EQ(report_uri, pkp_state.report_uri); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 5841 | EXPECT_NE(sts_state.expiry, pkp_state.expiry); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5842 | } |
| 5843 | |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 5844 | // Tests that reports get sent on HPKP violations when a report-uri is set. |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 5845 | TEST_F(URLRequestTestHTTP, MAYBE_ProcessPKPAndSendReport) { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 5846 | GURL report_uri(kHPKPReportUri); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5847 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 5848 | https_test_server.SetSSLConfig( |
| 5849 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 5850 | https_test_server.ServeFilesFromSourceDirectory( |
| 5851 | base::FilePath(kTestFilePath)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 5852 | ASSERT_TRUE(https_test_server.Start()); |
| 5853 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5854 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 5855 | |
| 5856 | // Set up a pin for |test_server_hostname|. |
| 5857 | TransportSecurityState security_state; |
| 5858 | const base::Time current_time(base::Time::Now()); |
| 5859 | const base::Time expiry = current_time + base::TimeDelta::FromSeconds(1000); |
| 5860 | HashValueVector hashes; |
| 5861 | HashValue hash1; |
| 5862 | HashValue hash2; |
| 5863 | // The values here don't matter, as long as they are different from |
| 5864 | // the mocked CertVerifyResult below. |
svaldez | 35d0dca | 2015-08-24 16:12:44 | [diff] [blame] | 5865 | ASSERT_TRUE( |
| 5866 | hash1.FromString("sha256/1111111111111111111111111111111111111111111=")); |
| 5867 | ASSERT_TRUE( |
| 5868 | hash2.FromString("sha256/2222222222222222222222222222222222222222222=")); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 5869 | hashes.push_back(hash1); |
| 5870 | hashes.push_back(hash2); |
| 5871 | security_state.AddHPKP(test_server_hostname, expiry, |
| 5872 | false, /* include subdomains */ |
| 5873 | hashes, report_uri); |
| 5874 | |
| 5875 | MockCertificateReportSender mock_report_sender; |
| 5876 | security_state.SetReportSender(&mock_report_sender); |
| 5877 | |
| 5878 | // Set up a MockCertVerifier to trigger a violation of the previously |
| 5879 | // set pin. |
| 5880 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 5881 | ASSERT_TRUE(cert); |
| 5882 | |
| 5883 | MockCertVerifier cert_verifier; |
| 5884 | CertVerifyResult verify_result; |
| 5885 | verify_result.verified_cert = cert; |
| 5886 | verify_result.is_issued_by_known_root = true; |
| 5887 | HashValue hash3; |
svaldez | 35d0dca | 2015-08-24 16:12:44 | [diff] [blame] | 5888 | ASSERT_TRUE( |
| 5889 | hash3.FromString("sha256/3333333333333333333333333333333333333333333=")); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 5890 | verify_result.public_key_hashes.push_back(hash3); |
| 5891 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 5892 | |
| 5893 | TestNetworkDelegate network_delegate; |
| 5894 | TestURLRequestContext context(true); |
| 5895 | context.set_transport_security_state(&security_state); |
| 5896 | context.set_network_delegate(&network_delegate); |
| 5897 | context.set_cert_verifier(&cert_verifier); |
| 5898 | context.Init(); |
| 5899 | |
| 5900 | // Now send a request to trigger the violation. |
| 5901 | TestDelegate d; |
| 5902 | scoped_ptr<URLRequest> violating_request(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5903 | https_test_server.GetURL("/simple.html"), DEFAULT_PRIORITY, &d)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 5904 | violating_request->Start(); |
| 5905 | base::RunLoop().Run(); |
| 5906 | |
| 5907 | // Check that a report was sent. |
| 5908 | EXPECT_EQ(report_uri, mock_report_sender.latest_report_uri()); |
| 5909 | ASSERT_FALSE(mock_report_sender.latest_report().empty()); |
| 5910 | scoped_ptr<base::Value> value( |
| 5911 | base::JSONReader::Read(mock_report_sender.latest_report())); |
| 5912 | ASSERT_TRUE(value); |
| 5913 | ASSERT_TRUE(value->IsType(base::Value::TYPE_DICTIONARY)); |
| 5914 | base::DictionaryValue* report_dict; |
| 5915 | ASSERT_TRUE(value->GetAsDictionary(&report_dict)); |
| 5916 | std::string report_hostname; |
| 5917 | EXPECT_TRUE(report_dict->GetString("hostname", &report_hostname)); |
| 5918 | EXPECT_EQ(test_server_hostname, report_hostname); |
| 5919 | } |
| 5920 | |
| 5921 | // Tests that reports get sent on requests with |
| 5922 | // Public-Key-Pins-Report-Only headers. |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 5923 | TEST_F(URLRequestTestHTTP, MAYBE_ProcessPKPReportOnly) { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 5924 | GURL report_uri(kHPKPReportUri); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5925 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 5926 | https_test_server.SetSSLConfig( |
| 5927 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 5928 | https_test_server.ServeFilesFromSourceDirectory( |
| 5929 | base::FilePath(kTestFilePath)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 5930 | ASSERT_TRUE(https_test_server.Start()); |
| 5931 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5932 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 5933 | |
| 5934 | TransportSecurityState security_state; |
| 5935 | MockCertificateReportSender mock_report_sender; |
| 5936 | security_state.SetReportSender(&mock_report_sender); |
| 5937 | |
| 5938 | // Set up a MockCertVerifier to violate the pin in the Report-Only |
| 5939 | // header. |
| 5940 | scoped_refptr<X509Certificate> cert = https_test_server.GetCertificate(); |
| 5941 | ASSERT_TRUE(cert); |
| 5942 | |
| 5943 | MockCertVerifier cert_verifier; |
| 5944 | CertVerifyResult verify_result; |
| 5945 | verify_result.verified_cert = cert; |
| 5946 | verify_result.is_issued_by_known_root = true; |
| 5947 | HashValue hash; |
| 5948 | // This value doesn't matter, as long as it is different from the pins |
| 5949 | // for the request to hpkp-headers-report-only.html. |
svaldez | 35d0dca | 2015-08-24 16:12:44 | [diff] [blame] | 5950 | ASSERT_TRUE( |
| 5951 | hash.FromString("sha256/1111111111111111111111111111111111111111111=")); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 5952 | verify_result.public_key_hashes.push_back(hash); |
| 5953 | cert_verifier.AddResultForCert(cert.get(), verify_result, OK); |
| 5954 | |
| 5955 | TestNetworkDelegate network_delegate; |
| 5956 | TestURLRequestContext context(true); |
| 5957 | context.set_transport_security_state(&security_state); |
| 5958 | context.set_network_delegate(&network_delegate); |
| 5959 | context.set_cert_verifier(&cert_verifier); |
| 5960 | context.Init(); |
| 5961 | |
| 5962 | // Now send a request to trigger the violation. |
| 5963 | TestDelegate d; |
| 5964 | scoped_ptr<URLRequest> violating_request(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5965 | https_test_server.GetURL("/hpkp-headers-report-only.html"), |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 5966 | DEFAULT_PRIORITY, &d)); |
| 5967 | violating_request->Start(); |
| 5968 | base::RunLoop().Run(); |
| 5969 | |
| 5970 | // Check that a report was sent. |
| 5971 | EXPECT_EQ(report_uri, mock_report_sender.latest_report_uri()); |
| 5972 | ASSERT_FALSE(mock_report_sender.latest_report().empty()); |
| 5973 | scoped_ptr<base::Value> value( |
| 5974 | base::JSONReader::Read(mock_report_sender.latest_report())); |
| 5975 | ASSERT_TRUE(value); |
| 5976 | ASSERT_TRUE(value->IsType(base::Value::TYPE_DICTIONARY)); |
| 5977 | base::DictionaryValue* report_dict; |
| 5978 | ASSERT_TRUE(value->GetAsDictionary(&report_dict)); |
| 5979 | std::string report_hostname; |
| 5980 | EXPECT_TRUE(report_dict->GetString("hostname", &report_hostname)); |
| 5981 | EXPECT_EQ(test_server_hostname, report_hostname); |
| 5982 | } |
| 5983 | |
| 5984 | // Tests that reports do not get sent on requests with |
| 5985 | // Public-Key-Pins-Report-Only headers that don't have pin violations. |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 5986 | TEST_F(URLRequestTestHTTP, MAYBE_ProcessPKPReportOnlyWithNoViolation) { |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 5987 | GURL report_uri(kHPKPReportUri); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5988 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 5989 | https_test_server.SetSSLConfig( |
| 5990 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 5991 | https_test_server.ServeFilesFromSourceDirectory( |
| 5992 | base::FilePath(kTestFilePath)); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 5993 | ASSERT_TRUE(https_test_server.Start()); |
| 5994 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 5995 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 5996 | |
| 5997 | TransportSecurityState security_state; |
| 5998 | MockCertificateReportSender mock_report_sender; |
| 5999 | security_state.SetReportSender(&mock_report_sender); |
| 6000 | |
| 6001 | TestNetworkDelegate network_delegate; |
| 6002 | MockCertVerifier mock_cert_verifier; |
| 6003 | TestURLRequestContext context(true); |
| 6004 | context.set_transport_security_state(&security_state); |
| 6005 | context.set_network_delegate(&network_delegate); |
| 6006 | context.set_cert_verifier(&mock_cert_verifier); |
| 6007 | mock_cert_verifier.set_default_result(OK); |
| 6008 | context.Init(); |
| 6009 | |
| 6010 | // Now send a request that does not trigger the violation. |
| 6011 | TestDelegate d; |
| 6012 | scoped_ptr<URLRequest> request(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6013 | https_test_server.GetURL("/hpkp-headers-report-only.html"), |
estark | 06e0dac | 2015-08-07 21:56:01 | [diff] [blame] | 6014 | DEFAULT_PRIORITY, &d)); |
| 6015 | request->Start(); |
| 6016 | base::RunLoop().Run(); |
| 6017 | |
| 6018 | // Check that a report was not sent. |
| 6019 | EXPECT_EQ(GURL(), mock_report_sender.latest_report_uri()); |
| 6020 | EXPECT_EQ(std::string(), mock_report_sender.latest_report()); |
| 6021 | } |
| 6022 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6023 | TEST_F(URLRequestTestHTTP, PKPNotProcessedOnIP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6024 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6025 | https_test_server.ServeFilesFromSourceDirectory( |
| 6026 | base::FilePath(kTestFilePath)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6027 | ASSERT_TRUE(https_test_server.Start()); |
| 6028 | // Make sure this test fails if the test server is changed to not |
| 6029 | // listen on an IP by default. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6030 | ASSERT_TRUE(https_test_server.GetURL("/").HostIsIPAddress()); |
| 6031 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6032 | |
| 6033 | TestDelegate d; |
| 6034 | scoped_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6035 | https_test_server.GetURL("/hpkp-headers.html"), DEFAULT_PRIORITY, &d)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6036 | request->Start(); |
| 6037 | base::RunLoop().Run(); |
| 6038 | |
| 6039 | TransportSecurityState* security_state = |
| 6040 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6041 | TransportSecurityState::PKPState pkp_state; |
| 6042 | EXPECT_FALSE( |
| 6043 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6044 | } |
| 6045 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6046 | TEST_F(URLRequestTestHTTP, ProcessSTSOnce) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6047 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6048 | https_test_server.SetSSLConfig( |
| 6049 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6050 | https_test_server.ServeFilesFromSourceDirectory( |
| 6051 | base::FilePath(kTestFilePath)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6052 | ASSERT_TRUE(https_test_server.Start()); |
| 6053 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6054 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6055 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6056 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6057 | scoped_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6058 | https_test_server.GetURL("/hsts-multiple-headers.html"), DEFAULT_PRIORITY, |
| 6059 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6060 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6061 | base::RunLoop().Run(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6062 | |
| 6063 | // We should have set parameters from the first header, not the second. |
| 6064 | TransportSecurityState* security_state = |
| 6065 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6066 | TransportSecurityState::STSState sts_state; |
| 6067 | EXPECT_TRUE( |
| 6068 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6069 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6070 | sts_state.upgrade_mode); |
| 6071 | EXPECT_FALSE(sts_state.include_subdomains); |
| 6072 | EXPECT_FALSE(sts_state.include_subdomains); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 6073 | } |
| 6074 | |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6075 | TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6076 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6077 | https_test_server.SetSSLConfig( |
| 6078 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6079 | https_test_server.ServeFilesFromSourceDirectory( |
| 6080 | base::FilePath(kTestFilePath)); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6081 | ASSERT_TRUE(https_test_server.Start()); |
| 6082 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6083 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6084 | |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6085 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6086 | scoped_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6087 | https_test_server.GetURL("/hsts-and-hpkp-headers.html"), DEFAULT_PRIORITY, |
| 6088 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6089 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6090 | base::RunLoop().Run(); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6091 | |
| 6092 | // We should have set parameters from the first header, not the second. |
| 6093 | TransportSecurityState* security_state = |
| 6094 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6095 | TransportSecurityState::STSState sts_state; |
| 6096 | TransportSecurityState::PKPState pkp_state; |
| 6097 | EXPECT_TRUE( |
| 6098 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6099 | EXPECT_TRUE( |
| 6100 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6101 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6102 | sts_state.upgrade_mode); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6103 | #if defined(OS_ANDROID) |
| 6104 | // Android's CertVerifyProc does not (yet) handle pins. |
| 6105 | #else |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6106 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6107 | #endif |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6108 | EXPECT_NE(sts_state.expiry, pkp_state.expiry); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6109 | |
[email protected] | a165f09 | 2013-06-12 16:10:05 | [diff] [blame] | 6110 | // Even though there is an HSTS header asserting includeSubdomains, it is |
| 6111 | // the *second* such header, and we MUST process only the first. |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6112 | EXPECT_FALSE(sts_state.include_subdomains); |
[email protected] | a165f09 | 2013-06-12 16:10:05 | [diff] [blame] | 6113 | // includeSubdomains does not occur in the test HPKP header. |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6114 | EXPECT_FALSE(pkp_state.include_subdomains); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 6115 | } |
| 6116 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6117 | // Tests that when multiple HPKP headers are present, asserting different |
| 6118 | // policies, that only the first such policy is processed. |
| 6119 | TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP2) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6120 | EmbeddedTestServer https_test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 6121 | https_test_server.SetSSLConfig( |
| 6122 | net::EmbeddedTestServer::CERT_COMMON_NAME_IS_DOMAIN); |
| 6123 | https_test_server.ServeFilesFromSourceDirectory( |
| 6124 | base::FilePath(kTestFilePath)); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6125 | ASSERT_TRUE(https_test_server.Start()); |
| 6126 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6127 | std::string test_server_hostname = https_test_server.GetURL("/").host(); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 6128 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6129 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6130 | scoped_ptr<URLRequest> request(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6131 | https_test_server.GetURL("/hsts-and-hpkp-headers2.html"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6132 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6133 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6134 | base::RunLoop().Run(); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6135 | |
| 6136 | TransportSecurityState* security_state = |
| 6137 | default_context_.transport_security_state(); |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6138 | TransportSecurityState::STSState sts_state; |
| 6139 | TransportSecurityState::PKPState pkp_state; |
| 6140 | EXPECT_TRUE( |
| 6141 | security_state->GetDynamicSTSState(test_server_hostname, &sts_state)); |
| 6142 | EXPECT_TRUE( |
| 6143 | security_state->GetDynamicPKPState(test_server_hostname, &pkp_state)); |
| 6144 | EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS, |
| 6145 | sts_state.upgrade_mode); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6146 | #if defined(OS_ANDROID) |
| 6147 | // Android's CertVerifyProc does not (yet) handle pins. |
| 6148 | #else |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6149 | EXPECT_TRUE(pkp_state.HasPublicKeyPins()); |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 6150 | #endif |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6151 | EXPECT_NE(sts_state.expiry, pkp_state.expiry); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6152 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 6153 | EXPECT_TRUE(sts_state.include_subdomains); |
| 6154 | EXPECT_FALSE(pkp_state.include_subdomains); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 6155 | } |
| 6156 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6157 | #endif // !defined(OS_IOS) |
| 6158 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6159 | TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6160 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6161 | |
| 6162 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6163 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6164 | http_test_server()->GetURL("/content-type-normalization.html"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6165 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6166 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6167 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6168 | |
| 6169 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6170 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6171 | EXPECT_EQ("text/html", mime_type); |
| 6172 | |
| 6173 | std::string charset; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6174 | req->GetCharset(&charset); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6175 | EXPECT_EQ("utf-8", charset); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6176 | req->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6177 | } |
| 6178 | |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 6179 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictDataRedirects) { |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 6180 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 6181 | GURL data_url("data:,foo"); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 6182 | DataProtocolHandler data_protocol_handler; |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 6183 | EXPECT_FALSE(data_protocol_handler.IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 6184 | |
| 6185 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 6186 | EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 6187 | } |
| 6188 | |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 6189 | #if !defined(DISABLE_FILE_SUPPORT) |
| 6190 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictFileRedirects) { |
| 6191 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
| 6192 | GURL file_url("file:///foo.txt"); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 6193 | FileProtocolHandler file_protocol_handler( |
| 6194 | base::ThreadTaskRunnerHandle::Get()); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 6195 | EXPECT_FALSE(file_protocol_handler.IsSafeRedirectTarget(file_url)); |
| 6196 | |
| 6197 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 6198 | EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(file_url)); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 6199 | } |
| 6200 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 6201 | TEST_F(URLRequestTestHTTP, RestrictFileRedirects) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6202 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6203 | |
| 6204 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6205 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6206 | http_test_server()->GetURL("/redirect-to-file.html"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6207 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6208 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6209 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6210 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6211 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
| 6212 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, req->status().error()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6213 | } |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 6214 | #endif // !defined(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6215 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 6216 | TEST_F(URLRequestTestHTTP, RestrictDataRedirects) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6217 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 6218 | |
| 6219 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6220 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6221 | http_test_server()->GetURL("/redirect-to-data.html"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6222 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6223 | req->Start(); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 6224 | base::MessageLoop::current()->Run(); |
| 6225 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6226 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
| 6227 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, req->status().error()); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 6228 | } |
| 6229 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6230 | TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6231 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6232 | |
| 6233 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6234 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6235 | http_test_server()->GetURL("/redirect-to-invalid-url.html"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6236 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6237 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6238 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6239 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6240 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
| 6241 | EXPECT_EQ(ERR_INVALID_URL, req->status().error()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6242 | } |
| 6243 | |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6244 | // Make sure redirects are cached, despite not reading their bodies. |
| 6245 | TEST_F(URLRequestTestHTTP, CacheRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6246 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6247 | GURL redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6248 | http_test_server()->GetURL("/redirect302-to-echo-cacheable"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6249 | |
| 6250 | { |
| 6251 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6252 | scoped_ptr<URLRequest> req( |
| 6253 | default_context_.CreateRequest(redirect_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6254 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6255 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6256 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6257 | EXPECT_EQ(1, d.received_redirect_count()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6258 | EXPECT_EQ(http_test_server()->GetURL("/echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6259 | } |
| 6260 | |
| 6261 | { |
| 6262 | TestDelegate d; |
| 6263 | d.set_quit_on_redirect(true); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6264 | scoped_ptr<URLRequest> req( |
| 6265 | default_context_.CreateRequest(redirect_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6266 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6267 | base::RunLoop().Run(); |
| 6268 | |
| 6269 | EXPECT_EQ(1, d.received_redirect_count()); |
| 6270 | EXPECT_EQ(0, d.response_started_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6271 | EXPECT_TRUE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6272 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6273 | req->FollowDeferredRedirect(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6274 | base::RunLoop().Run(); |
| 6275 | EXPECT_EQ(1, d.received_redirect_count()); |
| 6276 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6277 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6278 | EXPECT_EQ(http_test_server()->GetURL("/echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6279 | } |
| 6280 | } |
| 6281 | |
| 6282 | // Make sure a request isn't cached when a NetworkDelegate forces a redirect |
| 6283 | // when the headers are read, since the body won't have been read. |
| 6284 | TEST_F(URLRequestTestHTTP, NoCacheOnNetworkDelegateRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6285 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6286 | // URL that is normally cached. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6287 | GURL initial_url = http_test_server()->GetURL("/cachetime"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6288 | |
| 6289 | { |
| 6290 | // Set up the TestNetworkDelegate tp force a redirect. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6291 | GURL redirect_to_url = http_test_server()->GetURL("/echo"); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6292 | default_network_delegate_.set_redirect_on_headers_received_url( |
| 6293 | redirect_to_url); |
| 6294 | |
| 6295 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6296 | scoped_ptr<URLRequest> req( |
| 6297 | default_context_.CreateRequest(initial_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6298 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6299 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6300 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6301 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6302 | EXPECT_EQ(redirect_to_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6303 | } |
| 6304 | |
| 6305 | { |
| 6306 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6307 | scoped_ptr<URLRequest> req( |
| 6308 | default_context_.CreateRequest(initial_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6309 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6310 | base::RunLoop().Run(); |
| 6311 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6312 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
| 6313 | EXPECT_FALSE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6314 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6315 | EXPECT_EQ(initial_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 6316 | } |
| 6317 | } |
| 6318 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6319 | // Tests that redirection to an unsafe URL is allowed when it has been marked as |
| 6320 | // safe. |
| 6321 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToWhitelistedUnsafeURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6322 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6323 | |
| 6324 | GURL unsafe_url("data:text/html,this-is-considered-an-unsafe-url"); |
| 6325 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 6326 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 6327 | |
| 6328 | TestDelegate d; |
| 6329 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6330 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6331 | http_test_server()->GetURL("/whatever"), DEFAULT_PRIORITY, &d)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6332 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6333 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6334 | base::RunLoop().Run(); |
| 6335 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6336 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6337 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6338 | EXPECT_EQ(2U, r->url_chain().size()); |
| 6339 | EXPECT_EQ(OK, r->status().error()); |
| 6340 | EXPECT_EQ(unsafe_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6341 | EXPECT_EQ("this-is-considered-an-unsafe-url", d.data_received()); |
| 6342 | } |
| 6343 | } |
| 6344 | |
| 6345 | // Tests that a redirect to a different unsafe URL is blocked, even after adding |
| 6346 | // some other URL to the whitelist. |
| 6347 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToDifferentUnsafeURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6348 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6349 | |
| 6350 | GURL unsafe_url("data:text/html,something"); |
| 6351 | GURL different_unsafe_url("data:text/html,something-else"); |
| 6352 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 6353 | default_network_delegate_.set_allowed_unsafe_redirect_url( |
| 6354 | different_unsafe_url); |
| 6355 | |
| 6356 | TestDelegate d; |
| 6357 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6358 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6359 | http_test_server()->GetURL("/whatever"), DEFAULT_PRIORITY, &d)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6360 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6361 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6362 | base::RunLoop().Run(); |
| 6363 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6364 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
| 6365 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, r->status().error()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6366 | } |
| 6367 | } |
| 6368 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6369 | // Redirects from an URL with fragment to an unsafe URL with fragment should |
| 6370 | // be allowed, and the reference fragment of the target URL should be preserved. |
| 6371 | TEST_F(URLRequestTestHTTP, UnsafeRedirectWithDifferentReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6372 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6373 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6374 | GURL original_url(http_test_server()->GetURL("/original#fragment1")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6375 | GURL unsafe_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 6376 | GURL expected_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 6377 | |
| 6378 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 6379 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 6380 | |
| 6381 | TestDelegate d; |
| 6382 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6383 | scoped_ptr<URLRequest> r( |
| 6384 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6385 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6386 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6387 | base::RunLoop().Run(); |
| 6388 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6389 | EXPECT_EQ(2U, r->url_chain().size()); |
| 6390 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 6391 | EXPECT_EQ(OK, r->status().error()); |
| 6392 | EXPECT_EQ(original_url, r->original_url()); |
| 6393 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6394 | } |
| 6395 | } |
| 6396 | |
| 6397 | // When a delegate has specified a safe redirect URL, but it does not match the |
| 6398 | // redirect target, then do not prevent the reference fragment from being added. |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6399 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragmentAndUnrelatedUnsafeUrl) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6400 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6401 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6402 | GURL original_url(http_test_server()->GetURL("/original#expected-fragment")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6403 | GURL unsafe_url("data:text/html,this-url-does-not-match-redirect-url"); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6404 | GURL redirect_url(http_test_server()->GetURL("/target")); |
| 6405 | GURL expected_redirect_url( |
| 6406 | http_test_server()->GetURL("/target#expected-fragment")); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6407 | |
| 6408 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 6409 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 6410 | |
| 6411 | TestDelegate d; |
| 6412 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6413 | scoped_ptr<URLRequest> r( |
| 6414 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6415 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6416 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6417 | base::RunLoop().Run(); |
| 6418 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6419 | EXPECT_EQ(2U, r->url_chain().size()); |
| 6420 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 6421 | EXPECT_EQ(OK, r->status().error()); |
| 6422 | EXPECT_EQ(original_url, r->original_url()); |
| 6423 | EXPECT_EQ(expected_redirect_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 6424 | } |
| 6425 | } |
| 6426 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6427 | // When a delegate has specified a safe redirect URL, assume that the redirect |
| 6428 | // URL should not be changed. In particular, the reference fragment should not |
| 6429 | // be modified. |
| 6430 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6431 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6432 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6433 | GURL original_url( |
| 6434 | http_test_server()->GetURL("/original#should-not-be-appended")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6435 | GURL redirect_url("data:text/html,expect-no-reference-fragment"); |
| 6436 | |
| 6437 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 6438 | default_network_delegate_.set_allowed_unsafe_redirect_url(redirect_url); |
| 6439 | |
| 6440 | TestDelegate d; |
| 6441 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6442 | scoped_ptr<URLRequest> r( |
| 6443 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6444 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6445 | r->Start(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6446 | base::RunLoop().Run(); |
| 6447 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6448 | EXPECT_EQ(2U, r->url_chain().size()); |
| 6449 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 6450 | EXPECT_EQ(OK, r->status().error()); |
| 6451 | EXPECT_EQ(original_url, r->original_url()); |
| 6452 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6453 | } |
| 6454 | } |
| 6455 | |
| 6456 | // When a URLRequestRedirectJob is created, the redirection must be followed and |
| 6457 | // the reference fragment of the target URL must not be modified. |
| 6458 | TEST_F(URLRequestTestHTTP, RedirectJobWithReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6459 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6460 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6461 | GURL original_url( |
| 6462 | http_test_server()->GetURL("/original#should-not-be-appended")); |
| 6463 | GURL redirect_url(http_test_server()->GetURL("/echo")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6464 | |
| 6465 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6466 | scoped_ptr<URLRequest> r( |
| 6467 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6468 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 6469 | scoped_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6470 | r.get(), &default_network_delegate_, redirect_url, |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 6471 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 6472 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6473 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6474 | r->Start(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6475 | base::RunLoop().Run(); |
| 6476 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6477 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 6478 | EXPECT_EQ(OK, r->status().error()); |
| 6479 | EXPECT_EQ(original_url, r->original_url()); |
| 6480 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6481 | } |
| 6482 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6483 | TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6484 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6485 | |
| 6486 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6487 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6488 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6489 | req->SetReferrer("http://user:[email protected]/"); |
| 6490 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6491 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6492 | |
| 6493 | EXPECT_EQ(std::string("http://foo.com/"), d.data_received()); |
| 6494 | } |
| 6495 | |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 6496 | TEST_F(URLRequestTestHTTP, NoFragmentInReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6497 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 6498 | |
| 6499 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6500 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6501 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6502 | req->SetReferrer("http://foo.com/test#fragment"); |
| 6503 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6504 | base::RunLoop().Run(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 6505 | |
| 6506 | EXPECT_EQ(std::string("http://foo.com/test"), d.data_received()); |
| 6507 | } |
| 6508 | |
| 6509 | TEST_F(URLRequestTestHTTP, EmptyReferrerAfterValidReferrer) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6510 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 6511 | |
| 6512 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6513 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6514 | http_test_server()->GetURL("/echoheader?Referer"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6515 | req->SetReferrer("http://foo.com/test#fragment"); |
| 6516 | req->SetReferrer(""); |
| 6517 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6518 | base::RunLoop().Run(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 6519 | |
| 6520 | EXPECT_EQ(std::string("None"), d.data_received()); |
| 6521 | } |
| 6522 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6523 | // Defer network start and then resume, checking that the request was a success |
| 6524 | // and bytes were received. |
| 6525 | TEST_F(URLRequestTestHTTP, DeferredBeforeNetworkStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6526 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6527 | |
| 6528 | TestDelegate d; |
| 6529 | { |
| 6530 | d.set_quit_on_network_start(true); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6531 | GURL test_url(http_test_server()->GetURL("/echo")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6532 | scoped_ptr<URLRequest> req( |
| 6533 | default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6534 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6535 | req->Start(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6536 | base::RunLoop().Run(); |
| 6537 | |
| 6538 | EXPECT_EQ(1, d.received_before_network_start_count()); |
| 6539 | EXPECT_EQ(0, d.response_started_count()); |
| 6540 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6541 | req->ResumeNetworkStart(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6542 | base::RunLoop().Run(); |
| 6543 | |
| 6544 | EXPECT_EQ(1, d.response_started_count()); |
| 6545 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6546 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6547 | } |
| 6548 | } |
| 6549 | |
| 6550 | // Check that OnBeforeNetworkStart is only called once even if there is a |
| 6551 | // redirect. |
| 6552 | TEST_F(URLRequestTestHTTP, BeforeNetworkStartCalledOnce) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6553 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6554 | |
| 6555 | TestDelegate d; |
| 6556 | { |
| 6557 | d.set_quit_on_redirect(true); |
| 6558 | d.set_quit_on_network_start(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6559 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6560 | http_test_server()->GetURL("/server-redirect?echo"), DEFAULT_PRIORITY, |
| 6561 | &d)); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6562 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6563 | req->Start(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6564 | base::RunLoop().Run(); |
| 6565 | |
| 6566 | EXPECT_EQ(1, d.received_before_network_start_count()); |
| 6567 | EXPECT_EQ(0, d.response_started_count()); |
| 6568 | EXPECT_EQ(0, d.received_redirect_count()); |
| 6569 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6570 | req->ResumeNetworkStart(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6571 | base::RunLoop().Run(); |
| 6572 | |
| 6573 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6574 | req->FollowDeferredRedirect(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6575 | base::RunLoop().Run(); |
| 6576 | |
| 6577 | // Check that the redirect's new network transaction does not get propagated |
| 6578 | // to a second OnBeforeNetworkStart() notification. |
| 6579 | EXPECT_EQ(1, d.received_before_network_start_count()); |
| 6580 | |
| 6581 | EXPECT_EQ(1, d.response_started_count()); |
| 6582 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6583 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6584 | } |
| 6585 | } |
| 6586 | |
| 6587 | // Cancel the request after learning that the request would use the network. |
| 6588 | TEST_F(URLRequestTestHTTP, CancelOnBeforeNetworkStart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6589 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6590 | |
| 6591 | TestDelegate d; |
| 6592 | { |
| 6593 | d.set_quit_on_network_start(true); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6594 | GURL test_url(http_test_server()->GetURL("/echo")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6595 | scoped_ptr<URLRequest> req( |
| 6596 | default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6597 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6598 | req->Start(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6599 | base::RunLoop().Run(); |
| 6600 | |
| 6601 | EXPECT_EQ(1, d.received_before_network_start_count()); |
| 6602 | EXPECT_EQ(0, d.response_started_count()); |
| 6603 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6604 | req->Cancel(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6605 | base::RunLoop().Run(); |
| 6606 | |
| 6607 | EXPECT_EQ(1, d.response_started_count()); |
| 6608 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6609 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 6610 | } |
| 6611 | } |
| 6612 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6613 | TEST_F(URLRequestTestHTTP, CancelRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6614 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6615 | |
| 6616 | TestDelegate d; |
| 6617 | { |
| 6618 | d.set_cancel_in_received_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6619 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6620 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, |
| 6621 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6622 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6623 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6624 | |
| 6625 | EXPECT_EQ(1, d.response_started_count()); |
| 6626 | EXPECT_EQ(0, d.bytes_received()); |
| 6627 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6628 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6629 | } |
| 6630 | } |
| 6631 | |
| 6632 | TEST_F(URLRequestTestHTTP, DeferredRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6633 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6634 | |
| 6635 | TestDelegate d; |
| 6636 | { |
| 6637 | d.set_quit_on_redirect(true); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6638 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6639 | scoped_ptr<URLRequest> req( |
| 6640 | default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 6641 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6642 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6643 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6644 | |
| 6645 | EXPECT_EQ(1, d.received_redirect_count()); |
| 6646 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6647 | req->FollowDeferredRedirect(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6648 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6649 | |
| 6650 | EXPECT_EQ(1, d.response_started_count()); |
| 6651 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6652 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6653 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 6654 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6655 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 6656 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6657 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 6658 | |
| 6659 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 6660 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6661 | EXPECT_EQ(contents, d.data_received()); |
| 6662 | } |
| 6663 | } |
| 6664 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 6665 | TEST_F(URLRequestTestHTTP, DeferredRedirect_GetFullRequestHeaders) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6666 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 6667 | |
| 6668 | TestDelegate d; |
| 6669 | { |
| 6670 | d.set_quit_on_redirect(true); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6671 | GURL test_url(http_test_server()->GetURL("/redirect-test.html")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6672 | scoped_ptr<URLRequest> req( |
| 6673 | default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 6674 | |
| 6675 | EXPECT_FALSE(d.have_full_request_headers()); |
| 6676 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6677 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6678 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 6679 | |
| 6680 | EXPECT_EQ(1, d.received_redirect_count()); |
| 6681 | EXPECT_TRUE(d.have_full_request_headers()); |
| 6682 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 6683 | d.ClearFullRequestHeaders(); |
| 6684 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6685 | req->FollowDeferredRedirect(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6686 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 6687 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6688 | GURL target_url(http_test_server()->GetURL("/with-headers.html")); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 6689 | EXPECT_EQ(1, d.response_started_count()); |
| 6690 | EXPECT_TRUE(d.have_full_request_headers()); |
| 6691 | CheckFullRequestHeaders(d.full_request_headers(), target_url); |
| 6692 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6693 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 6694 | |
| 6695 | base::FilePath path; |
| 6696 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 6697 | path = path.Append(kTestFilePath); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 6698 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 6699 | |
| 6700 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 6701 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 6702 | EXPECT_EQ(contents, d.data_received()); |
| 6703 | } |
| 6704 | } |
| 6705 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6706 | TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6707 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6708 | |
| 6709 | TestDelegate d; |
| 6710 | { |
| 6711 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6712 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6713 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, |
| 6714 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6715 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6716 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6717 | |
| 6718 | EXPECT_EQ(1, d.received_redirect_count()); |
| 6719 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6720 | req->Cancel(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6721 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6722 | |
| 6723 | EXPECT_EQ(1, d.response_started_count()); |
| 6724 | EXPECT_EQ(0, d.bytes_received()); |
| 6725 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6726 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6727 | } |
| 6728 | } |
| 6729 | |
| 6730 | TEST_F(URLRequestTestHTTP, VaryHeader) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6731 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6732 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6733 | // Populate the cache. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6734 | { |
| 6735 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6736 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6737 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
| 6738 | &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6739 | HttpRequestHeaders headers; |
| 6740 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6741 | req->SetExtraRequestHeaders(headers); |
| 6742 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6743 | base::RunLoop().Run(); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6744 | |
| 6745 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6746 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6747 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6748 | } |
| 6749 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6750 | // Expect a cache hit. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6751 | { |
| 6752 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6753 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6754 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
| 6755 | &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6756 | HttpRequestHeaders headers; |
| 6757 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6758 | req->SetExtraRequestHeaders(headers); |
| 6759 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6760 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6761 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6762 | EXPECT_TRUE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6763 | |
| 6764 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6765 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6766 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6767 | } |
| 6768 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6769 | // Expect a cache miss. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6770 | { |
| 6771 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6772 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6773 | http_test_server()->GetURL("/echoheadercache?foo"), DEFAULT_PRIORITY, |
| 6774 | &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6775 | HttpRequestHeaders headers; |
| 6776 | headers.SetHeader("foo", "2"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6777 | req->SetExtraRequestHeaders(headers); |
| 6778 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6779 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6780 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6781 | EXPECT_FALSE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6782 | |
| 6783 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6784 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6785 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6786 | } |
| 6787 | } |
| 6788 | |
| 6789 | TEST_F(URLRequestTestHTTP, BasicAuth) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6790 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6791 | |
| 6792 | // populate the cache |
| 6793 | { |
| 6794 | TestDelegate d; |
| 6795 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 6796 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6797 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6798 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6799 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6800 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6801 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6802 | |
| 6803 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 6804 | } |
| 6805 | |
| 6806 | // repeat request with end-to-end validation. since auth-basic results in a |
| 6807 | // cachable page, we expect this test to result in a 304. in which case, the |
| 6808 | // response should be fetched from the cache. |
| 6809 | { |
| 6810 | TestDelegate d; |
| 6811 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 6812 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6813 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6814 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6815 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 6816 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6817 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6818 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6819 | |
| 6820 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 6821 | |
| 6822 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6823 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6824 | } |
| 6825 | } |
| 6826 | |
| 6827 | // Check that Set-Cookie headers in 401 responses are respected. |
| 6828 | // http://crbug.com/6450 |
| 6829 | TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6830 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6831 | |
| 6832 | GURL url_requiring_auth = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6833 | http_test_server()->GetURL("/auth-basic?set-cookie-if-challenged"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6834 | |
| 6835 | // Request a page that will give a 401 containing a Set-Cookie header. |
| 6836 | // Verify that when the transaction is restarted, it includes the new cookie. |
| 6837 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 6838 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6839 | TestURLRequestContext context(true); |
| 6840 | context.set_network_delegate(&network_delegate); |
| 6841 | context.Init(); |
| 6842 | |
| 6843 | TestDelegate d; |
| 6844 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 6845 | |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6846 | scoped_ptr<URLRequest> r( |
| 6847 | context.CreateRequest(url_requiring_auth, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6848 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6849 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6850 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6851 | |
| 6852 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 6853 | |
| 6854 | // Make sure we sent the cookie in the restarted transaction. |
| 6855 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 6856 | != std::string::npos); |
| 6857 | } |
| 6858 | |
| 6859 | // Same test as above, except this time the restart is initiated earlier |
| 6860 | // (without user intervention since identity is embedded in the URL). |
| 6861 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 6862 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6863 | TestURLRequestContext context(true); |
| 6864 | context.set_network_delegate(&network_delegate); |
| 6865 | context.Init(); |
| 6866 | |
| 6867 | TestDelegate d; |
| 6868 | |
| 6869 | GURL::Replacements replacements; |
mgiuca | 77752c3 | 2015-02-05 07:31:18 | [diff] [blame] | 6870 | replacements.SetUsernameStr("user2"); |
| 6871 | replacements.SetPasswordStr("secret"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6872 | GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements); |
| 6873 | |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6874 | scoped_ptr<URLRequest> r( |
| 6875 | context.CreateRequest(url_with_identity, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6876 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6877 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6878 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6879 | |
| 6880 | EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos); |
| 6881 | |
| 6882 | // Make sure we sent the cookie in the restarted transaction. |
| 6883 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 6884 | != std::string::npos); |
| 6885 | } |
| 6886 | } |
| 6887 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6888 | // Tests that load timing works as expected with auth and the cache. |
| 6889 | TEST_F(URLRequestTestHTTP, BasicAuthLoadTiming) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6890 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6891 | |
| 6892 | // populate the cache |
| 6893 | { |
| 6894 | TestDelegate d; |
| 6895 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 6896 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6897 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6898 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6899 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6900 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6901 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6902 | |
| 6903 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 6904 | |
| 6905 | LoadTimingInfo load_timing_info_before_auth; |
| 6906 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeAuth( |
| 6907 | &load_timing_info_before_auth)); |
| 6908 | TestLoadTimingNotReused(load_timing_info_before_auth, |
| 6909 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 6910 | |
| 6911 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6912 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6913 | // The test server does not support keep alive sockets, so the second |
| 6914 | // request with auth should use a new socket. |
| 6915 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 6916 | EXPECT_NE(load_timing_info_before_auth.socket_log_id, |
| 6917 | load_timing_info.socket_log_id); |
| 6918 | EXPECT_LE(load_timing_info_before_auth.receive_headers_end, |
| 6919 | load_timing_info.connect_timing.connect_start); |
| 6920 | } |
| 6921 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6922 | // Repeat request with end-to-end validation. Since auth-basic results in a |
| 6923 | // cachable page, we expect this test to result in a 304. In which case, the |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6924 | // response should be fetched from the cache. |
| 6925 | { |
| 6926 | TestDelegate d; |
| 6927 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 6928 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6929 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6930 | http_test_server()->GetURL("/auth-basic"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6931 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 6932 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6933 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6934 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6935 | |
| 6936 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 6937 | |
| 6938 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6939 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6940 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6941 | // Since there was a request that went over the wire, the load timing |
| 6942 | // information should include connection times. |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6943 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6944 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6945 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6946 | } |
| 6947 | } |
| 6948 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6949 | // In this test, we do a POST which the server will 302 redirect. |
| 6950 | // The subsequent transaction should use GET, and should not send the |
| 6951 | // Content-Type header. |
| 6952 | // http://code.google.com/p/chromium/issues/detail?id=843 |
| 6953 | TEST_F(URLRequestTestHTTP, Post302RedirectGet) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6954 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6955 | |
| 6956 | const char kData[] = "hello world"; |
| 6957 | |
| 6958 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6959 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 6960 | http_test_server()->GetURL("/redirect-to-echoall"), DEFAULT_PRIORITY, |
| 6961 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6962 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 6963 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6964 | |
| 6965 | // Set headers (some of which are specific to the POST). |
| 6966 | HttpRequestHeaders headers; |
| 6967 | headers.AddHeadersFromString( |
| 6968 | "Content-Type: multipart/form-data; " |
| 6969 | "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n" |
| 6970 | "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9," |
| 6971 | "text/plain;q=0.8,image/png,*/*;q=0.5\r\n" |
| 6972 | "Accept-Language: en-US,en\r\n" |
| 6973 | "Accept-Charset: ISO-8859-1,*,utf-8\r\n" |
| 6974 | "Content-Length: 11\r\n" |
| 6975 | "Origin: http://localhost:1337/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6976 | req->SetExtraRequestHeaders(headers); |
| 6977 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6978 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6979 | |
| 6980 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6981 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6982 | EXPECT_EQ("text/html", mime_type); |
| 6983 | |
| 6984 | const std::string& data = d.data_received(); |
| 6985 | |
| 6986 | // Check that the post-specific headers were stripped: |
| 6987 | EXPECT_FALSE(ContainsString(data, "Content-Length:")); |
| 6988 | EXPECT_FALSE(ContainsString(data, "Content-Type:")); |
| 6989 | EXPECT_FALSE(ContainsString(data, "Origin:")); |
| 6990 | |
| 6991 | // These extra request headers should not have been stripped. |
| 6992 | EXPECT_TRUE(ContainsString(data, "Accept:")); |
| 6993 | EXPECT_TRUE(ContainsString(data, "Accept-Language:")); |
| 6994 | EXPECT_TRUE(ContainsString(data, "Accept-Charset:")); |
| 6995 | } |
| 6996 | |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 6997 | // The following tests check that we handle mutating the request for HTTP |
| 6998 | // redirects as expected. |
| 6999 | // See https://crbug.com/56373, https://crbug.com/102130, and |
| 7000 | // https://crbug.com/465517. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7001 | |
| 7002 | TEST_F(URLRequestTestHTTP, Redirect301Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7003 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7004 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7005 | const GURL url = http_test_server()->GetURL("/redirect301-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7006 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7007 | http_test_server()->GetURL("/redirect301-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7008 | |
| 7009 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 7010 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 7011 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7012 | |
| 7013 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 7014 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 7015 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 7016 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 7017 | std::string()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7018 | } |
| 7019 | |
| 7020 | TEST_F(URLRequestTestHTTP, Redirect302Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7021 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7022 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7023 | const GURL url = http_test_server()->GetURL("/redirect302-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7024 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7025 | http_test_server()->GetURL("/redirect302-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7026 | |
| 7027 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 7028 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 7029 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7030 | |
| 7031 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 7032 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 7033 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 7034 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 7035 | std::string()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7036 | } |
| 7037 | |
| 7038 | TEST_F(URLRequestTestHTTP, Redirect303Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7039 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7040 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7041 | const GURL url = http_test_server()->GetURL("/redirect303-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7042 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7043 | http_test_server()->GetURL("/redirect303-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7044 | |
| 7045 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 7046 | HTTPRedirectMethodTest(url, "PUT", "GET", true); |
| 7047 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7048 | |
| 7049 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 7050 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 7051 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 7052 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 7053 | std::string()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7054 | } |
| 7055 | |
| 7056 | TEST_F(URLRequestTestHTTP, Redirect307Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7057 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7058 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7059 | const GURL url = http_test_server()->GetURL("/redirect307-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7060 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7061 | http_test_server()->GetURL("/redirect307-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7062 | |
| 7063 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 7064 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 7065 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7066 | |
| 7067 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 7068 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 7069 | HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec()); |
| 7070 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7071 | } |
| 7072 | |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 7073 | TEST_F(URLRequestTestHTTP, Redirect308Tests) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7074 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 7075 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7076 | const GURL url = http_test_server()->GetURL("/redirect308-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7077 | const GURL https_redirect_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7078 | http_test_server()->GetURL("/redirect308-to-https"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 7079 | |
| 7080 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 7081 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 7082 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 7083 | |
| 7084 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 7085 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 7086 | HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec()); |
| 7087 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 7088 | } |
| 7089 | |
| 7090 | // Make sure that 308 responses without bodies are not treated as redirects. |
| 7091 | // Certain legacy apis that pre-date the response code expect this behavior |
| 7092 | // (Like Google Drive). |
| 7093 | TEST_F(URLRequestTestHTTP, NoRedirectOn308WithoutLocationHeader) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7094 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 7095 | |
| 7096 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7097 | const GURL url = http_test_server()->GetURL("/308-without-location-header"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 7098 | |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7099 | scoped_ptr<URLRequest> request( |
| 7100 | default_context_.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 7101 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7102 | request->Start(); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 7103 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7104 | EXPECT_EQ(URLRequestStatus::SUCCESS, request->status().status()); |
| 7105 | EXPECT_EQ(OK, request->status().error()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 7106 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7107 | EXPECT_EQ(308, request->response_headers()->response_code()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 7108 | EXPECT_EQ("This is not a redirect.", d.data_received()); |
| 7109 | } |
| 7110 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7111 | TEST_F(URLRequestTestHTTP, Redirect302PreserveReferenceFragment) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7112 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7113 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7114 | GURL original_url( |
| 7115 | http_test_server()->GetURL("/redirect302-to-echo#fragment")); |
| 7116 | GURL expected_url(http_test_server()->GetURL("/echo#fragment")); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7117 | |
| 7118 | TestDelegate d; |
| 7119 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7120 | scoped_ptr<URLRequest> r( |
| 7121 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7122 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7123 | r->Start(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7124 | base::RunLoop().Run(); |
| 7125 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7126 | EXPECT_EQ(2U, r->url_chain().size()); |
| 7127 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 7128 | EXPECT_EQ(OK, r->status().error()); |
| 7129 | EXPECT_EQ(original_url, r->original_url()); |
| 7130 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 7131 | } |
| 7132 | } |
| 7133 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7134 | TEST_F(URLRequestTestHTTP, RedirectPreserveFirstPartyURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7135 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7136 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7137 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7138 | GURL first_party_url("http://example.com"); |
| 7139 | |
| 7140 | TestDelegate d; |
| 7141 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7142 | scoped_ptr<URLRequest> r( |
| 7143 | default_context_.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7144 | r->set_first_party_for_cookies(first_party_url); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7145 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7146 | r->Start(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7147 | base::RunLoop().Run(); |
| 7148 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7149 | EXPECT_EQ(2U, r->url_chain().size()); |
| 7150 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 7151 | EXPECT_EQ(OK, r->status().error()); |
| 7152 | EXPECT_EQ(first_party_url, r->first_party_for_cookies()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7153 | } |
| 7154 | } |
| 7155 | |
| 7156 | TEST_F(URLRequestTestHTTP, RedirectUpdateFirstPartyURL) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7157 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7158 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7159 | GURL url(http_test_server()->GetURL("/redirect302-to-echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7160 | GURL original_first_party_url("http://example.com"); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7161 | GURL expected_first_party_url(http_test_server()->GetURL("/echo")); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7162 | |
| 7163 | TestDelegate d; |
| 7164 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7165 | scoped_ptr<URLRequest> r( |
| 7166 | default_context_.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7167 | r->set_first_party_for_cookies(original_first_party_url); |
| 7168 | r->set_first_party_url_policy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7169 | URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 7170 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7171 | r->Start(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7172 | base::RunLoop().Run(); |
| 7173 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7174 | EXPECT_EQ(2U, r->url_chain().size()); |
| 7175 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 7176 | EXPECT_EQ(OK, r->status().error()); |
| 7177 | EXPECT_EQ(expected_first_party_url, r->first_party_for_cookies()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7178 | } |
| 7179 | } |
| 7180 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7181 | TEST_F(URLRequestTestHTTP, InterceptPost302RedirectGet) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7182 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7183 | |
| 7184 | const char kData[] = "hello world"; |
| 7185 | |
| 7186 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7187 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7188 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7189 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 7190 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7191 | HttpRequestHeaders headers; |
| 7192 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
ricea | c912aec | 2015-07-09 07:26:51 | [diff] [blame] | 7193 | base::SizeTToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7194 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7195 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 7196 | scoped_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7197 | req.get(), &default_network_delegate_, |
| 7198 | http_test_server()->GetURL("/echo"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 7199 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 7200 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7201 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7202 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7203 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7204 | EXPECT_EQ("GET", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7205 | } |
| 7206 | |
| 7207 | TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7208 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7209 | |
| 7210 | const char kData[] = "hello world"; |
| 7211 | |
| 7212 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7213 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7214 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7215 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 7216 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7217 | HttpRequestHeaders headers; |
| 7218 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
ricea | c912aec | 2015-07-09 07:26:51 | [diff] [blame] | 7219 | base::SizeTToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7220 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7221 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 7222 | scoped_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7223 | req.get(), &default_network_delegate_, |
| 7224 | http_test_server()->GetURL("/echo"), |
[email protected] | 7983c4a | 2014-03-12 01:47:09 | [diff] [blame] | 7225 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 7226 | "Very Good Reason")); |
| 7227 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7228 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7229 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7230 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7231 | EXPECT_EQ("POST", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7232 | EXPECT_EQ(kData, d.data_received()); |
| 7233 | } |
| 7234 | |
| 7235 | // Check that default A-L header is sent. |
| 7236 | TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7237 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7238 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 7239 | StaticHttpUserAgentSettings settings("en", std::string()); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 7240 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7241 | TestURLRequestContext context(true); |
| 7242 | context.set_network_delegate(&network_delegate); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 7243 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7244 | context.Init(); |
| 7245 | |
| 7246 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7247 | scoped_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7248 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
| 7249 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7250 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7251 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7252 | EXPECT_EQ("en", d.data_received()); |
| 7253 | } |
| 7254 | |
| 7255 | // Check that an empty A-L header is not sent. http://crbug.com/77365. |
| 7256 | TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7257 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7258 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 7259 | std::string empty_string; // Avoid most vexing parse on line below. |
| 7260 | StaticHttpUserAgentSettings settings(empty_string, empty_string); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 7261 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7262 | TestURLRequestContext context(true); |
| 7263 | context.set_network_delegate(&network_delegate); |
| 7264 | context.Init(); |
| 7265 | // We override the language after initialization because empty entries |
| 7266 | // get overridden by Init(). |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 7267 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7268 | |
| 7269 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7270 | scoped_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7271 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
| 7272 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7273 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7274 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7275 | EXPECT_EQ("None", d.data_received()); |
| 7276 | } |
| 7277 | |
| 7278 | // Check that if request overrides the A-L header, the default is not appended. |
| 7279 | // See http://crbug.com/20894 |
| 7280 | TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7281 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7282 | |
| 7283 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7284 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7285 | http_test_server()->GetURL("/echoheader?Accept-Language"), |
| 7286 | DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7287 | HttpRequestHeaders headers; |
| 7288 | headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7289 | req->SetExtraRequestHeaders(headers); |
| 7290 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7291 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7292 | EXPECT_EQ(std::string("ru"), d.data_received()); |
| 7293 | } |
| 7294 | |
| 7295 | // Check that default A-E header is sent. |
| 7296 | TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7297 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7298 | |
| 7299 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7300 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7301 | http_test_server()->GetURL("/echoheader?Accept-Encoding"), |
| 7302 | DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7303 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7304 | req->SetExtraRequestHeaders(headers); |
| 7305 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7306 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7307 | EXPECT_TRUE(ContainsString(d.data_received(), "gzip")); |
| 7308 | } |
| 7309 | |
| 7310 | // Check that if request overrides the A-E header, the default is not appended. |
| 7311 | // See http://crbug.com/47381 |
| 7312 | TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7313 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7314 | |
| 7315 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7316 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7317 | http_test_server()->GetURL("/echoheader?Accept-Encoding"), |
| 7318 | DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7319 | HttpRequestHeaders headers; |
| 7320 | headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, "identity"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7321 | req->SetExtraRequestHeaders(headers); |
| 7322 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7323 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7324 | EXPECT_FALSE(ContainsString(d.data_received(), "gzip")); |
| 7325 | EXPECT_TRUE(ContainsString(d.data_received(), "identity")); |
| 7326 | } |
| 7327 | |
[email protected] | 84f0543 | 2013-03-15 01:00:12 | [diff] [blame] | 7328 | // Check that setting the A-C header sends the proper header. |
| 7329 | TEST_F(URLRequestTestHTTP, SetAcceptCharset) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7330 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7331 | |
| 7332 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7333 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7334 | http_test_server()->GetURL("/echoheader?Accept-Charset"), |
| 7335 | DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7336 | HttpRequestHeaders headers; |
| 7337 | headers.SetHeader(HttpRequestHeaders::kAcceptCharset, "koi-8r"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7338 | req->SetExtraRequestHeaders(headers); |
| 7339 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7340 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7341 | EXPECT_EQ(std::string("koi-8r"), d.data_received()); |
| 7342 | } |
| 7343 | |
| 7344 | // Check that default User-Agent header is sent. |
| 7345 | TEST_F(URLRequestTestHTTP, DefaultUserAgent) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7346 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7347 | |
| 7348 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7349 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7350 | http_test_server()->GetURL("/echoheader?User-Agent"), DEFAULT_PRIORITY, |
| 7351 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7352 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7353 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7354 | EXPECT_EQ(default_context_.http_user_agent_settings()->GetUserAgent(), |
[email protected] | 051a4b3 | 2014-01-09 04:02:37 | [diff] [blame] | 7355 | d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7356 | } |
| 7357 | |
| 7358 | // Check that if request overrides the User-Agent header, |
| 7359 | // the default is not appended. |
marq | f14fff8d | 2015-12-02 15:52:29 | [diff] [blame] | 7360 | // TODO(crbug.com/564656) This test is flaky on iOS. |
| 7361 | #if defined(OS_IOS) |
| 7362 | #define MAYBE_OverrideUserAgent FLAKY_OverrideUserAgent |
| 7363 | #else |
| 7364 | #define MAYBE_OverrideUserAgent OverrideUserAgent |
| 7365 | #endif |
| 7366 | TEST_F(URLRequestTestHTTP, MAYBE_OverrideUserAgent) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7367 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7368 | |
| 7369 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7370 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7371 | http_test_server()->GetURL("/echoheader?User-Agent"), DEFAULT_PRIORITY, |
| 7372 | &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7373 | HttpRequestHeaders headers; |
| 7374 | headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7375 | req->SetExtraRequestHeaders(headers); |
| 7376 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7377 | base::RunLoop().Run(); |
[email protected] | cd6f252 | 2014-01-16 18:27:35 | [diff] [blame] | 7378 | EXPECT_EQ(std::string("Lynx (textmode)"), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7379 | } |
| 7380 | |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 7381 | // Check that a NULL HttpUserAgentSettings causes the corresponding empty |
| 7382 | // User-Agent header to be sent but does not send the Accept-Language and |
| 7383 | // Accept-Charset headers. |
| 7384 | TEST_F(URLRequestTestHTTP, EmptyHttpUserAgentSettings) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7385 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 7386 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 7387 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 7388 | TestURLRequestContext context(true); |
| 7389 | context.set_network_delegate(&network_delegate); |
| 7390 | context.Init(); |
| 7391 | // We override the HttpUserAgentSettings after initialization because empty |
| 7392 | // entries get overridden by Init(). |
| 7393 | context.set_http_user_agent_settings(NULL); |
| 7394 | |
| 7395 | struct { |
| 7396 | const char* request; |
| 7397 | const char* expected_response; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7398 | } tests[] = {{"/echoheader?Accept-Language", "None"}, |
| 7399 | {"/echoheader?Accept-Charset", "None"}, |
| 7400 | {"/echoheader?User-Agent", ""}}; |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 7401 | |
viettrungluu | e4a8b88 | 2014-10-16 06:17:38 | [diff] [blame] | 7402 | for (size_t i = 0; i < arraysize(tests); i++) { |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 7403 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7404 | scoped_ptr<URLRequest> req(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7405 | http_test_server()->GetURL(tests[i].request), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7406 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7407 | base::RunLoop().Run(); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 7408 | EXPECT_EQ(tests[i].expected_response, d.data_received()) |
| 7409 | << " Request = \"" << tests[i].request << "\""; |
| 7410 | } |
| 7411 | } |
| 7412 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7413 | // Make sure that URLRequest passes on its priority updates to |
| 7414 | // newly-created jobs after the first one. |
| 7415 | TEST_F(URLRequestTestHTTP, SetSubsequentJobPriority) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7416 | ASSERT_TRUE(http_test_server()->Start()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7417 | |
| 7418 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7419 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7420 | http_test_server()->GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7421 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7422 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 7423 | scoped_ptr<URLRequestRedirectJob> redirect_job(new URLRequestRedirectJob( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7424 | req.get(), &default_network_delegate_, |
| 7425 | http_test_server()->GetURL("/echo"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 7426 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason")); |
| 7427 | AddTestInterceptor()->set_main_intercept_job(std::move(redirect_job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7428 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7429 | req->SetPriority(LOW); |
| 7430 | req->Start(); |
| 7431 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7432 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 7433 | RequestPriority job_priority; |
| 7434 | scoped_ptr<URLRequestJob> job(new PriorityMonitoringURLRequestJob( |
| 7435 | req.get(), &default_network_delegate_, &job_priority)); |
| 7436 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7437 | |
| 7438 | // Should trigger |job| to be started. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7439 | base::RunLoop().Run(); |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 7440 | EXPECT_EQ(LOW, job_priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 7441 | } |
| 7442 | |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 7443 | // Check that creating a network request while entering/exiting suspend mode |
| 7444 | // fails as it should. This is the only case where an HttpTransactionFactory |
| 7445 | // does not return an HttpTransaction. |
| 7446 | TEST_F(URLRequestTestHTTP, NetworkSuspendTest) { |
| 7447 | // Create a new HttpNetworkLayer that thinks it's suspended. |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 7448 | scoped_ptr<HttpNetworkLayer> network_layer(new HttpNetworkLayer( |
| 7449 | default_context_.http_transaction_factory()->GetSession())); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 7450 | network_layer->OnSuspend(); |
| 7451 | |
mmenke | bc31a2c | 2015-10-29 13:44:45 | [diff] [blame] | 7452 | HttpCache http_cache(network_layer.Pass(), |
mmenke | e65e7af | 2015-10-13 17:16:42 | [diff] [blame] | 7453 | HttpCache::DefaultBackend::InMemory(0), true); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 7454 | |
| 7455 | TestURLRequestContext context(true); |
| 7456 | context.set_http_transaction_factory(&http_cache); |
| 7457 | context.Init(); |
| 7458 | |
| 7459 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7460 | scoped_ptr<URLRequest> req( |
| 7461 | context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7462 | req->Start(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 7463 | base::RunLoop().Run(); |
| 7464 | |
| 7465 | EXPECT_TRUE(d.request_failed()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7466 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
| 7467 | EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, req->status().error()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 7468 | } |
| 7469 | |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 7470 | namespace { |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 7471 | |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 7472 | // HttpTransactionFactory that synchronously fails to create transactions. |
| 7473 | class FailingHttpTransactionFactory : public HttpTransactionFactory { |
| 7474 | public: |
| 7475 | explicit FailingHttpTransactionFactory(HttpNetworkSession* network_session) |
| 7476 | : network_session_(network_session) {} |
| 7477 | |
| 7478 | ~FailingHttpTransactionFactory() override {} |
| 7479 | |
| 7480 | // HttpTransactionFactory methods: |
| 7481 | int CreateTransaction(RequestPriority priority, |
| 7482 | scoped_ptr<HttpTransaction>* trans) override { |
| 7483 | return ERR_FAILED; |
| 7484 | } |
| 7485 | |
| 7486 | HttpCache* GetCache() override { return nullptr; } |
| 7487 | |
| 7488 | HttpNetworkSession* GetSession() override { return network_session_; } |
| 7489 | |
| 7490 | private: |
| 7491 | HttpNetworkSession* network_session_; |
| 7492 | |
| 7493 | DISALLOW_COPY_AND_ASSIGN(FailingHttpTransactionFactory); |
| 7494 | }; |
| 7495 | |
| 7496 | } // namespace |
| 7497 | |
| 7498 | // Check that when a request that fails to create an HttpTransaction can be |
| 7499 | // cancelled while the failure notification is pending, and doesn't send two |
| 7500 | // failure notifications. |
| 7501 | // |
| 7502 | // This currently only happens when in suspend mode and there's no cache, but |
| 7503 | // just use a special HttpTransactionFactory, to avoid depending on those |
| 7504 | // behaviors. |
| 7505 | TEST_F(URLRequestTestHTTP, NetworkCancelAfterCreateTransactionFailsTest) { |
| 7506 | FailingHttpTransactionFactory http_transaction_factory( |
| 7507 | default_context_.http_transaction_factory()->GetSession()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 7508 | TestURLRequestContext context(true); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 7509 | context.set_http_transaction_factory(&http_transaction_factory); |
| 7510 | context.set_network_delegate(default_network_delegate()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 7511 | context.Init(); |
| 7512 | |
| 7513 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7514 | scoped_ptr<URLRequest> req( |
| 7515 | context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d)); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 7516 | // Don't send cookies (Collecting cookies is asynchronous, and need request to |
| 7517 | // try to create an HttpNetworkTransaction synchronously on start). |
| 7518 | req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7519 | req->Start(); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 7520 | req->Cancel(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 7521 | base::RunLoop().Run(); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 7522 | // Run pending error task, if there is one. |
| 7523 | base::RunLoop().RunUntilIdle(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 7524 | |
| 7525 | EXPECT_TRUE(d.request_failed()); |
mmenke | 2281f376 | 2015-11-02 20:38:17 | [diff] [blame] | 7526 | EXPECT_EQ(1, d.response_started_count()); |
| 7527 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
| 7528 | |
| 7529 | // NetworkDelegate should see the cancellation, but not the error. |
| 7530 | EXPECT_EQ(1, default_network_delegate()->canceled_requests()); |
| 7531 | EXPECT_EQ(0, default_network_delegate()->error_count()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 7532 | } |
| 7533 | |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 7534 | TEST_F(URLRequestTestHTTP, NetworkAccessedSetOnNetworkRequest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7535 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 7536 | |
| 7537 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7538 | GURL test_url(http_test_server()->GetURL("/")); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 7539 | scoped_ptr<URLRequest> req( |
| 7540 | default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d)); |
| 7541 | |
| 7542 | req->Start(); |
| 7543 | base::RunLoop().Run(); |
| 7544 | |
| 7545 | EXPECT_TRUE(req->response_info().network_accessed); |
| 7546 | } |
| 7547 | |
| 7548 | TEST_F(URLRequestTestHTTP, NetworkAccessedClearOnCachedResponse) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7549 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 7550 | |
| 7551 | // Populate the cache. |
| 7552 | TestDelegate d; |
| 7553 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7554 | http_test_server()->GetURL("/cachetime"), DEFAULT_PRIORITY, &d)); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 7555 | req->Start(); |
| 7556 | base::RunLoop().Run(); |
| 7557 | |
| 7558 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
| 7559 | EXPECT_TRUE(req->response_info().network_accessed); |
| 7560 | EXPECT_FALSE(req->response_info().was_cached); |
| 7561 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7562 | req = default_context_.CreateRequest(http_test_server()->GetURL("/cachetime"), |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 7563 | DEFAULT_PRIORITY, &d); |
| 7564 | req->Start(); |
| 7565 | base::RunLoop().Run(); |
| 7566 | |
| 7567 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
| 7568 | EXPECT_FALSE(req->response_info().network_accessed); |
| 7569 | EXPECT_TRUE(req->response_info().was_cached); |
| 7570 | } |
| 7571 | |
| 7572 | TEST_F(URLRequestTestHTTP, NetworkAccessedClearOnLoadOnlyFromCache) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7573 | ASSERT_TRUE(http_test_server()->Start()); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 7574 | |
| 7575 | TestDelegate d; |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7576 | GURL test_url(http_test_server()->GetURL("/")); |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 7577 | scoped_ptr<URLRequest> req( |
| 7578 | default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d)); |
| 7579 | req->SetLoadFlags(LOAD_ONLY_FROM_CACHE); |
| 7580 | |
| 7581 | req->Start(); |
| 7582 | base::RunLoop().Run(); |
| 7583 | |
| 7584 | EXPECT_FALSE(req->response_info().network_accessed); |
| 7585 | } |
| 7586 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7587 | class URLRequestInterceptorTestHTTP : public URLRequestTestHTTP { |
| 7588 | public: |
| 7589 | // TODO(bengr): Merge this with the URLRequestInterceptorHTTPTest fixture, |
| 7590 | // ideally remove the dependency on URLRequestTestJob, and maybe move these |
| 7591 | // tests into the factory tests. |
| 7592 | URLRequestInterceptorTestHTTP() : URLRequestTestHTTP(), interceptor_(NULL) { |
| 7593 | } |
| 7594 | |
| 7595 | void SetUpFactory() override { |
| 7596 | interceptor_ = new MockURLRequestInterceptor(); |
| 7597 | job_factory_.reset(new URLRequestInterceptingJobFactory( |
| 7598 | job_factory_.Pass(), make_scoped_ptr(interceptor_))); |
| 7599 | } |
| 7600 | |
| 7601 | MockURLRequestInterceptor* interceptor() const { |
| 7602 | return interceptor_; |
| 7603 | } |
| 7604 | |
| 7605 | private: |
| 7606 | MockURLRequestInterceptor* interceptor_; |
| 7607 | }; |
| 7608 | |
| 7609 | TEST_F(URLRequestInterceptorTestHTTP, |
| 7610 | NetworkDelegateNotificationOnRedirectIntercept) { |
| 7611 | interceptor()->set_intercept_redirect(true); |
| 7612 | interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers()); |
| 7613 | interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data()); |
| 7614 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7615 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7616 | |
| 7617 | TestDelegate d; |
| 7618 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7619 | http_test_server()->GetURL("/redirect-test.html"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7620 | req->Start(); |
| 7621 | base::RunLoop().Run(); |
| 7622 | |
| 7623 | EXPECT_TRUE(interceptor()->did_intercept_redirect()); |
| 7624 | // Check we got one good response |
| 7625 | EXPECT_TRUE(req->status().is_success()); |
| 7626 | if (req->status().is_success()) |
| 7627 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 7628 | |
| 7629 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 7630 | EXPECT_EQ(1, d.response_started_count()); |
| 7631 | EXPECT_EQ(0, d.received_redirect_count()); |
| 7632 | |
| 7633 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
| 7634 | EXPECT_EQ(1, default_network_delegate()->before_send_headers_count()); |
| 7635 | EXPECT_EQ(1, default_network_delegate()->headers_received_count()); |
| 7636 | } |
| 7637 | |
| 7638 | TEST_F(URLRequestInterceptorTestHTTP, |
| 7639 | NetworkDelegateNotificationOnErrorIntercept) { |
| 7640 | // Intercept that error and respond with an OK response. |
| 7641 | interceptor()->set_intercept_final_response(true); |
| 7642 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 7643 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 7644 | default_network_delegate()->set_can_be_intercepted_on_error(true); |
| 7645 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7646 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7647 | |
| 7648 | TestDelegate d; |
| 7649 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7650 | http_test_server()->GetURL("/two-content-lengths.html"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7651 | &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7652 | req->set_method("GET"); |
| 7653 | req->Start(); |
| 7654 | base::RunLoop().Run(); |
| 7655 | |
| 7656 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 7657 | |
| 7658 | // Check we received one good response. |
| 7659 | EXPECT_TRUE(req->status().is_success()); |
| 7660 | if (req->status().is_success()) |
| 7661 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 7662 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 7663 | EXPECT_EQ(1, d.response_started_count()); |
| 7664 | EXPECT_EQ(0, d.received_redirect_count()); |
| 7665 | |
| 7666 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
| 7667 | EXPECT_EQ(1, default_network_delegate()->before_send_headers_count()); |
| 7668 | EXPECT_EQ(0, default_network_delegate()->headers_received_count()); |
| 7669 | } |
| 7670 | |
| 7671 | TEST_F(URLRequestInterceptorTestHTTP, |
| 7672 | NetworkDelegateNotificationOnResponseIntercept) { |
| 7673 | // Intercept that error and respond with an OK response. |
| 7674 | interceptor()->set_intercept_final_response(true); |
| 7675 | |
| 7676 | // Intercept with a real URLRequestHttpJob. |
| 7677 | interceptor()->set_use_url_request_http_job(true); |
| 7678 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7679 | ASSERT_TRUE(http_test_server()->Start()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7680 | |
| 7681 | TestDelegate d; |
| 7682 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7683 | http_test_server()->GetURL("/simple.html"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7684 | req->set_method("GET"); |
| 7685 | req->Start(); |
| 7686 | base::RunLoop().Run(); |
| 7687 | |
| 7688 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 7689 | |
| 7690 | // Check we received one good response. |
| 7691 | EXPECT_TRUE(req->status().is_success()); |
| 7692 | if (req->status().is_success()) |
| 7693 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 7694 | EXPECT_EQ("hello", d.data_received()); |
| 7695 | EXPECT_EQ(1, d.response_started_count()); |
| 7696 | EXPECT_EQ(0, d.received_redirect_count()); |
| 7697 | |
| 7698 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
| 7699 | EXPECT_EQ(2, default_network_delegate()->before_send_headers_count()); |
| 7700 | EXPECT_EQ(2, default_network_delegate()->headers_received_count()); |
| 7701 | } |
| 7702 | |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7703 | class URLRequestTestReferrerPolicy : public URLRequestTest { |
| 7704 | public: |
| 7705 | URLRequestTestReferrerPolicy() {} |
| 7706 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7707 | void InstantiateSameOriginServers(net::EmbeddedTestServer::Type type) { |
| 7708 | origin_server_.reset(new EmbeddedTestServer(type)); |
| 7709 | if (type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 7710 | origin_server_->AddDefaultHandlers( |
| 7711 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 7712 | } else { |
| 7713 | origin_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 7714 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7715 | ASSERT_TRUE(origin_server_->Start()); |
| 7716 | } |
| 7717 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7718 | void InstantiateCrossOriginServers(net::EmbeddedTestServer::Type origin_type, |
| 7719 | net::EmbeddedTestServer::Type dest_type) { |
| 7720 | origin_server_.reset(new EmbeddedTestServer(origin_type)); |
| 7721 | if (origin_type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 7722 | origin_server_->AddDefaultHandlers( |
| 7723 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 7724 | } else { |
| 7725 | origin_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 7726 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7727 | ASSERT_TRUE(origin_server_->Start()); |
| 7728 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7729 | destination_server_.reset(new EmbeddedTestServer(dest_type)); |
| 7730 | if (dest_type == net::EmbeddedTestServer::TYPE_HTTPS) { |
| 7731 | destination_server_->AddDefaultHandlers( |
| 7732 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 7733 | } else { |
| 7734 | destination_server_->AddDefaultHandlers(base::FilePath(kTestFilePath)); |
| 7735 | } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7736 | ASSERT_TRUE(destination_server_->Start()); |
| 7737 | } |
| 7738 | |
| 7739 | void VerifyReferrerAfterRedirect(URLRequest::ReferrerPolicy policy, |
| 7740 | const GURL& referrer, |
| 7741 | const GURL& expected) { |
| 7742 | // Create and execute the request: we'll only have a |destination_server_| |
| 7743 | // if the origins are meant to be distinct. Otherwise, we'll use the |
| 7744 | // |origin_server_| for both endpoints. |
| 7745 | GURL destination_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7746 | destination_server_ ? destination_server_->GetURL("/echoheader?Referer") |
| 7747 | : origin_server_->GetURL("/echoheader?Referer"); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7748 | GURL origin_url = |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7749 | origin_server_->GetURL("/server-redirect?" + destination_url.spec()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7750 | |
| 7751 | TestDelegate d; |
| 7752 | scoped_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7753 | default_context_.CreateRequest(origin_url, DEFAULT_PRIORITY, &d)); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7754 | req->set_referrer_policy(policy); |
| 7755 | req->SetReferrer(referrer.spec()); |
| 7756 | req->Start(); |
| 7757 | base::RunLoop().Run(); |
| 7758 | |
| 7759 | EXPECT_EQ(1, d.response_started_count()); |
| 7760 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7761 | EXPECT_EQ(destination_url, req->url()); |
| 7762 | EXPECT_TRUE(req->status().is_success()); |
| 7763 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 7764 | |
| 7765 | EXPECT_EQ(expected.spec(), req->referrer()); |
| 7766 | if (expected.is_empty()) |
| 7767 | EXPECT_EQ("None", d.data_received()); |
| 7768 | else |
| 7769 | EXPECT_EQ(expected.spec(), d.data_received()); |
| 7770 | } |
| 7771 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7772 | EmbeddedTestServer* origin_server() const { return origin_server_.get(); } |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7773 | |
| 7774 | private: |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7775 | scoped_ptr<EmbeddedTestServer> origin_server_; |
| 7776 | scoped_ptr<EmbeddedTestServer> destination_server_; |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7777 | }; |
| 7778 | |
| 7779 | TEST_F(URLRequestTestReferrerPolicy, HTTPToSameOriginHTTP) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7780 | InstantiateSameOriginServers(net::EmbeddedTestServer::TYPE_HTTP); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7781 | |
| 7782 | VerifyReferrerAfterRedirect( |
| 7783 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7784 | origin_server()->GetURL("/path/to/file.html"), |
| 7785 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7786 | |
| 7787 | VerifyReferrerAfterRedirect( |
| 7788 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7789 | origin_server()->GetURL("/path/to/file.html"), |
| 7790 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7791 | |
| 7792 | VerifyReferrerAfterRedirect( |
| 7793 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7794 | origin_server()->GetURL("/path/to/file.html"), |
| 7795 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7796 | |
| 7797 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7798 | origin_server()->GetURL("/path/to/file.html"), |
| 7799 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7800 | } |
| 7801 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7802 | TEST_F(URLRequestTestReferrerPolicy, HTTPToCrossOriginHTTP) { |
| 7803 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTP, |
| 7804 | net::EmbeddedTestServer::TYPE_HTTP); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7805 | |
| 7806 | VerifyReferrerAfterRedirect( |
| 7807 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7808 | origin_server()->GetURL("/path/to/file.html"), |
| 7809 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7810 | |
| 7811 | VerifyReferrerAfterRedirect( |
| 7812 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7813 | origin_server()->GetURL("/path/to/file.html"), |
| 7814 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7815 | |
| 7816 | VerifyReferrerAfterRedirect( |
| 7817 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7818 | origin_server()->GetURL("/path/to/file.html"), |
| 7819 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7820 | |
| 7821 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7822 | origin_server()->GetURL("/path/to/file.html"), |
| 7823 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7824 | } |
| 7825 | |
| 7826 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToSameOriginHTTPS) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7827 | InstantiateSameOriginServers(net::EmbeddedTestServer::TYPE_HTTPS); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7828 | |
| 7829 | VerifyReferrerAfterRedirect( |
| 7830 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7831 | origin_server()->GetURL("/path/to/file.html"), |
| 7832 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7833 | |
| 7834 | VerifyReferrerAfterRedirect( |
| 7835 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7836 | origin_server()->GetURL("/path/to/file.html"), |
| 7837 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7838 | |
| 7839 | VerifyReferrerAfterRedirect( |
| 7840 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7841 | origin_server()->GetURL("/path/to/file.html"), |
| 7842 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7843 | |
| 7844 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7845 | origin_server()->GetURL("/path/to/file.html"), |
| 7846 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7847 | } |
| 7848 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7849 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToCrossOriginHTTPS) { |
| 7850 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTPS, |
| 7851 | net::EmbeddedTestServer::TYPE_HTTPS); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7852 | |
| 7853 | VerifyReferrerAfterRedirect( |
| 7854 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7855 | origin_server()->GetURL("/path/to/file.html"), |
| 7856 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7857 | |
| 7858 | VerifyReferrerAfterRedirect( |
| 7859 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7860 | origin_server()->GetURL("/path/to/file.html"), |
| 7861 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7862 | |
| 7863 | VerifyReferrerAfterRedirect( |
| 7864 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7865 | origin_server()->GetURL("/path/to/file.html"), |
| 7866 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7867 | |
| 7868 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7869 | origin_server()->GetURL("/path/to/file.html"), |
| 7870 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7871 | } |
| 7872 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7873 | TEST_F(URLRequestTestReferrerPolicy, HTTPToHTTPS) { |
| 7874 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTP, |
| 7875 | net::EmbeddedTestServer::TYPE_HTTPS); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7876 | |
| 7877 | VerifyReferrerAfterRedirect( |
| 7878 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7879 | origin_server()->GetURL("/path/to/file.html"), |
| 7880 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7881 | |
| 7882 | VerifyReferrerAfterRedirect( |
| 7883 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7884 | origin_server()->GetURL("/path/to/file.html"), |
| 7885 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7886 | |
| 7887 | VerifyReferrerAfterRedirect( |
| 7888 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7889 | origin_server()->GetURL("/path/to/file.html"), |
| 7890 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7891 | |
| 7892 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7893 | origin_server()->GetURL("/path/to/file.html"), |
| 7894 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7895 | } |
| 7896 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7897 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToHTTP) { |
| 7898 | InstantiateCrossOriginServers(net::EmbeddedTestServer::TYPE_HTTPS, |
| 7899 | net::EmbeddedTestServer::TYPE_HTTP); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7900 | |
| 7901 | VerifyReferrerAfterRedirect( |
| 7902 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7903 | origin_server()->GetURL("/path/to/file.html"), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7904 | |
| 7905 | VerifyReferrerAfterRedirect( |
| 7906 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7907 | origin_server()->GetURL("/path/to/file.html"), GURL()); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7908 | |
| 7909 | VerifyReferrerAfterRedirect( |
| 7910 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7911 | origin_server()->GetURL("/path/to/file.html"), |
| 7912 | origin_server()->GetURL("/")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7913 | |
| 7914 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7915 | origin_server()->GetURL("/path/to/file.html"), |
| 7916 | origin_server()->GetURL("/path/to/file.html")); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7917 | } |
| 7918 | |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 7919 | class HTTPSRequestTest : public testing::Test { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 7920 | public: |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 7921 | HTTPSRequestTest() : default_context_(true) { |
| 7922 | default_context_.set_network_delegate(&default_network_delegate_); |
| 7923 | default_context_.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 7924 | } |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 7925 | ~HTTPSRequestTest() override {} |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 7926 | |
| 7927 | protected: |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 7928 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 7929 | TestURLRequestContext default_context_; |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 7930 | }; |
| 7931 | |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 7932 | TEST_F(HTTPSRequestTest, HTTPSGetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7933 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7934 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7935 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7936 | ASSERT_TRUE(test_server.Start()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 7937 | |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 7938 | TestDelegate d; |
| 7939 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7940 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7941 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7942 | r->Start(); |
| 7943 | EXPECT_TRUE(r->is_pending()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 7944 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7945 | base::RunLoop().Run(); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 7946 | |
| 7947 | EXPECT_EQ(1, d.response_started_count()); |
| 7948 | EXPECT_FALSE(d.received_data_before_response()); |
| 7949 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7950 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 7951 | EXPECT_EQ(test_server.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7952 | r->GetSocketAddress().host()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 7953 | EXPECT_EQ(test_server.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7954 | r->GetSocketAddress().port()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 7955 | } |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 7956 | } |
| 7957 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 7958 | TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7959 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7960 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 7961 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7962 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7963 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7964 | |
| 7965 | bool err_allowed = true; |
| 7966 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 7967 | TestDelegate d; |
| 7968 | { |
| 7969 | d.set_allow_certificate_errors(err_allowed); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7970 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7971 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7972 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7973 | r->Start(); |
| 7974 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7975 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7976 | base::RunLoop().Run(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7977 | |
| 7978 | EXPECT_EQ(1, d.response_started_count()); |
| 7979 | EXPECT_FALSE(d.received_data_before_response()); |
| 7980 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 7981 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7982 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7983 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 7984 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7985 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 7986 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7987 | } |
| 7988 | } |
| 7989 | } |
| 7990 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 7991 | TEST_F(HTTPSRequestTest, HTTPSExpiredTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 7992 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 7993 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_EXPIRED); |
| 7994 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7995 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7996 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7997 | |
| 7998 | // Iterate from false to true, just so that we do the opposite of the |
| 7999 | // previous test in order to increase test coverage. |
| 8000 | bool err_allowed = false; |
| 8001 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 8002 | TestDelegate d; |
| 8003 | { |
| 8004 | d.set_allow_certificate_errors(err_allowed); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8005 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8006 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 8007 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8008 | r->Start(); |
| 8009 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 8010 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8011 | base::RunLoop().Run(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 8012 | |
| 8013 | EXPECT_EQ(1, d.response_started_count()); |
| 8014 | EXPECT_FALSE(d.received_data_before_response()); |
| 8015 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 8016 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 8017 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8018 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 8019 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 8020 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 8021 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 8022 | } |
| 8023 | } |
| 8024 | } |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 8025 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8026 | // TODO(svaldez): iOS tests are flaky with EmbeddedTestServer and transport |
| 8027 | // security state. (see http://crbug.com/550977). |
| 8028 | #if !defined(OS_IOS) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8029 | // This tests that a load of www.google.com with a certificate error sets |
| 8030 | // the |certificate_errors_are_fatal| flag correctly. This flag will cause |
| 8031 | // the interstitial to be fatal. |
| 8032 | TEST_F(HTTPSRequestTest, HTTPSPreloadedHSTSTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8033 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 8034 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 8035 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8036 | ASSERT_TRUE(test_server.Start()); |
| 8037 | |
| 8038 | // We require that the URL be www.google.com in order to pick up the |
| 8039 | // preloaded HSTS entries in the TransportSecurityState. This means that we |
| 8040 | // have to use a MockHostResolver in order to direct www.google.com to the |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8041 | // testserver. By default, MockHostResolver maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8042 | |
| 8043 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8044 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8045 | TestURLRequestContext context(true); |
| 8046 | context.set_network_delegate(&network_delegate); |
| 8047 | context.set_host_resolver(&host_resolver); |
| 8048 | TransportSecurityState transport_security_state; |
| 8049 | context.set_transport_security_state(&transport_security_state); |
| 8050 | context.Init(); |
| 8051 | |
| 8052 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8053 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 8054 | GURL(base::StringPrintf("https://www.google.com:%d", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8055 | test_server.host_port_pair().port())), |
| 8056 | DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8057 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8058 | r->Start(); |
| 8059 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8060 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8061 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8062 | |
| 8063 | EXPECT_EQ(1, d.response_started_count()); |
| 8064 | EXPECT_FALSE(d.received_data_before_response()); |
| 8065 | EXPECT_TRUE(d.have_certificate_errors()); |
| 8066 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 8067 | } |
| 8068 | |
| 8069 | // This tests that cached HTTPS page loads do not cause any updates to the |
| 8070 | // TransportSecurityState. |
| 8071 | TEST_F(HTTPSRequestTest, HTTPSErrorsNoClobberTSSTest) { |
| 8072 | // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't |
| 8073 | // matter. It just has to be any error. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8074 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 8075 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_MISMATCHED_NAME); |
| 8076 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8077 | ASSERT_TRUE(test_server.Start()); |
| 8078 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 8079 | // We require that the URL be www.google.com in order to pick up the static |
| 8080 | // and dynamic STS and PKP entries in the TransportSecurityState. This means |
| 8081 | // that we have to use a MockHostResolver in order to direct www.google.com to |
| 8082 | // the testserver. By default, MockHostResolver maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8083 | |
| 8084 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 8085 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8086 | TestURLRequestContext context(true); |
| 8087 | context.set_network_delegate(&network_delegate); |
| 8088 | context.set_host_resolver(&host_resolver); |
| 8089 | TransportSecurityState transport_security_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 8090 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 8091 | TransportSecurityState::STSState static_sts_state; |
| 8092 | TransportSecurityState::PKPState static_pkp_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 8093 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 8094 | "www.google.com", &static_sts_state, &static_pkp_state)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8095 | context.set_transport_security_state(&transport_security_state); |
| 8096 | context.Init(); |
| 8097 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 8098 | TransportSecurityState::STSState dynamic_sts_state; |
| 8099 | TransportSecurityState::PKPState dynamic_pkp_state; |
| 8100 | EXPECT_FALSE(transport_security_state.GetDynamicSTSState("www.google.com", |
| 8101 | &dynamic_sts_state)); |
| 8102 | EXPECT_FALSE(transport_security_state.GetDynamicPKPState("www.google.com", |
| 8103 | &dynamic_pkp_state)); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 8104 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8105 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8106 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 8107 | GURL(base::StringPrintf("https://www.google.com:%d", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8108 | test_server.host_port_pair().port())), |
| 8109 | DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8110 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8111 | r->Start(); |
| 8112 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8113 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8114 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8115 | |
| 8116 | EXPECT_EQ(1, d.response_started_count()); |
| 8117 | EXPECT_FALSE(d.received_data_before_response()); |
| 8118 | EXPECT_TRUE(d.have_certificate_errors()); |
| 8119 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 8120 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 8121 | // Get a fresh copy of the states, and check that they haven't changed. |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 8122 | TransportSecurityState::STSState new_static_sts_state; |
| 8123 | TransportSecurityState::PKPState new_static_pkp_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 8124 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 8125 | "www.google.com", &new_static_sts_state, &new_static_pkp_state)); |
| 8126 | TransportSecurityState::STSState new_dynamic_sts_state; |
| 8127 | TransportSecurityState::PKPState new_dynamic_pkp_state; |
| 8128 | EXPECT_FALSE(transport_security_state.GetDynamicSTSState( |
| 8129 | "www.google.com", &new_dynamic_sts_state)); |
| 8130 | EXPECT_FALSE(transport_security_state.GetDynamicPKPState( |
| 8131 | "www.google.com", &new_dynamic_pkp_state)); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 8132 | |
martijn | c0d6b62 | 2015-06-30 19:14:40 | [diff] [blame] | 8133 | EXPECT_EQ(new_static_sts_state.upgrade_mode, static_sts_state.upgrade_mode); |
| 8134 | EXPECT_EQ(new_static_sts_state.include_subdomains, |
| 8135 | static_sts_state.include_subdomains); |
| 8136 | EXPECT_EQ(new_static_pkp_state.include_subdomains, |
| 8137 | static_pkp_state.include_subdomains); |
| 8138 | EXPECT_TRUE(FingerprintsEqual(new_static_pkp_state.spki_hashes, |
| 8139 | static_pkp_state.spki_hashes)); |
| 8140 | EXPECT_TRUE(FingerprintsEqual(new_static_pkp_state.bad_spki_hashes, |
| 8141 | static_pkp_state.bad_spki_hashes)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8142 | } |
| 8143 | |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 8144 | // Make sure HSTS preserves a POST request's method and body. |
| 8145 | TEST_F(HTTPSRequestTest, HSTSPreservesPosts) { |
| 8146 | static const char kData[] = "hello world"; |
| 8147 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8148 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 8149 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8150 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 8151 | ASSERT_TRUE(test_server.Start()); |
| 8152 | |
| 8153 | |
| 8154 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 8155 | // address, so a MockHostResolver is needed to redirect www.somewhere.com to |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8156 | // the EmbeddedTestServer. By default, MockHostResolver maps all hosts |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8157 | // to 127.0.0.1. |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 8158 | MockHostResolver host_resolver; |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 8159 | |
| 8160 | // Force https for www.somewhere.com. |
| 8161 | TransportSecurityState transport_security_state; |
[email protected] | 474f079e | 2013-03-02 19:11:20 | [diff] [blame] | 8162 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000); |
| 8163 | bool include_subdomains = false; |
| 8164 | transport_security_state.AddHSTS("www.somewhere.com", expiry, |
| 8165 | include_subdomains); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 8166 | |
| 8167 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 8168 | |
| 8169 | TestURLRequestContext context(true); |
| 8170 | context.set_host_resolver(&host_resolver); |
| 8171 | context.set_transport_security_state(&transport_security_state); |
| 8172 | context.set_network_delegate(&network_delegate); |
| 8173 | context.Init(); |
| 8174 | |
| 8175 | TestDelegate d; |
| 8176 | // Navigating to https://www.somewhere.com instead of https://127.0.0.1 will |
| 8177 | // cause a certificate error. Ignore the error. |
| 8178 | d.set_allow_certificate_errors(true); |
| 8179 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8180 | scoped_ptr<URLRequest> req(context.CreateRequest( |
| 8181 | GURL(base::StringPrintf("http://www.somewhere.com:%d/echo", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8182 | test_server.host_port_pair().port())), |
| 8183 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8184 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 8185 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 8186 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8187 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8188 | base::RunLoop().Run(); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 8189 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8190 | EXPECT_EQ("https", req->url().scheme()); |
| 8191 | EXPECT_EQ("POST", req->method()); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 8192 | EXPECT_EQ(kData, d.data_received()); |
[email protected] | 07d9bfd | 2013-06-27 14:16:40 | [diff] [blame] | 8193 | |
| 8194 | LoadTimingInfo load_timing_info; |
| 8195 | network_delegate.GetLoadTimingInfoBeforeRedirect(&load_timing_info); |
| 8196 | // LoadTimingInfo of HSTS redirects is similar to that of network cache hits |
| 8197 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 8198 | } |
| 8199 | |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 8200 | // Make sure that the CORS headers are added to cross-origin HSTS redirects. |
| 8201 | TEST_F(HTTPSRequestTest, HSTSCrossOriginAddHeaders) { |
| 8202 | static const char kOriginHeaderValue[] = "http://www.example.com"; |
| 8203 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8204 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 8205 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 8206 | ASSERT_TRUE(test_server.Start()); |
| 8207 | |
| 8208 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 8209 | // address, so a MockHostResolver is needed to redirect example.net to the |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8210 | // EmbeddedTestServer. MockHostResolver maps all hosts to 127.0.0.1 by |
| 8211 | // default. |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 8212 | MockHostResolver host_resolver; |
| 8213 | |
| 8214 | TransportSecurityState transport_security_state; |
| 8215 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1); |
| 8216 | bool include_subdomains = false; |
| 8217 | transport_security_state.AddHSTS("example.net", expiry, include_subdomains); |
| 8218 | |
| 8219 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 8220 | |
| 8221 | MockCertVerifier cert_verifier; |
| 8222 | cert_verifier.set_default_result(OK); |
| 8223 | |
| 8224 | TestURLRequestContext context(true); |
| 8225 | context.set_host_resolver(&host_resolver); |
| 8226 | context.set_transport_security_state(&transport_security_state); |
| 8227 | context.set_network_delegate(&network_delegate); |
| 8228 | context.set_cert_verifier(&cert_verifier); |
| 8229 | context.Init(); |
| 8230 | |
| 8231 | GURL hsts_http_url(base::StringPrintf("http://example.net:%d/somehstssite", |
| 8232 | test_server.host_port_pair().port())); |
| 8233 | url::Replacements<char> replacements; |
| 8234 | const char kNewScheme[] = "https"; |
| 8235 | replacements.SetScheme(kNewScheme, url::Component(0, strlen(kNewScheme))); |
| 8236 | GURL hsts_https_url = hsts_http_url.ReplaceComponents(replacements); |
| 8237 | |
| 8238 | TestDelegate d; |
| 8239 | // Quit on redirect to allow response header inspection upon redirect. |
| 8240 | d.set_quit_on_redirect(true); |
| 8241 | |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8242 | scoped_ptr<URLRequest> req( |
| 8243 | context.CreateRequest(hsts_http_url, DEFAULT_PRIORITY, &d)); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 8244 | // Set Origin header to simulate a cross-origin request. |
| 8245 | HttpRequestHeaders request_headers; |
| 8246 | request_headers.SetHeader("Origin", kOriginHeaderValue); |
| 8247 | req->SetExtraRequestHeaders(request_headers); |
| 8248 | |
| 8249 | req->Start(); |
| 8250 | base::RunLoop().Run(); |
| 8251 | |
| 8252 | EXPECT_EQ(1, d.received_redirect_count()); |
| 8253 | |
| 8254 | const HttpResponseHeaders* headers = req->response_headers(); |
| 8255 | std::string redirect_location; |
| 8256 | EXPECT_TRUE(headers->EnumerateHeader(NULL, "Location", &redirect_location)); |
| 8257 | EXPECT_EQ(hsts_https_url.spec(), redirect_location); |
| 8258 | |
| 8259 | std::string received_cors_header; |
| 8260 | EXPECT_TRUE(headers->EnumerateHeader(NULL, "Access-Control-Allow-Origin", |
| 8261 | &received_cors_header)); |
| 8262 | EXPECT_EQ(kOriginHeaderValue, received_cors_header); |
| 8263 | } |
| 8264 | |
Adam Rice | cb76ac6 | 2015-02-20 05:33:25 | [diff] [blame] | 8265 | // This just tests the behaviour of GetHSTSRedirect(). End-to-end tests of HSTS |
| 8266 | // are performed in net/websockets/websocket_end_to_end_test.cc. |
| 8267 | TEST(WebSocketURLRequestTest, HSTSApplied) { |
| 8268 | TestNetworkDelegate network_delegate; |
| 8269 | TransportSecurityState transport_security_state; |
| 8270 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1); |
| 8271 | bool include_subdomains = false; |
| 8272 | transport_security_state.AddHSTS("example.net", expiry, include_subdomains); |
| 8273 | TestURLRequestContext context(true); |
| 8274 | context.set_transport_security_state(&transport_security_state); |
| 8275 | context.set_network_delegate(&network_delegate); |
| 8276 | context.Init(); |
| 8277 | GURL ws_url("ws://example.net/echo"); |
| 8278 | TestDelegate delegate; |
| 8279 | scoped_ptr<URLRequest> request( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8280 | context.CreateRequest(ws_url, DEFAULT_PRIORITY, &delegate)); |
Adam Rice | cb76ac6 | 2015-02-20 05:33:25 | [diff] [blame] | 8281 | EXPECT_TRUE(request->GetHSTSRedirect(&ws_url)); |
| 8282 | EXPECT_TRUE(ws_url.SchemeIs("wss")); |
| 8283 | } |
| 8284 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8285 | namespace { |
| 8286 | |
| 8287 | class SSLClientAuthTestDelegate : public TestDelegate { |
| 8288 | public: |
| 8289 | SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) { |
| 8290 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 8291 | void OnCertificateRequested(URLRequest* request, |
| 8292 | SSLCertRequestInfo* cert_request_info) override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8293 | on_certificate_requested_count_++; |
ki.stfu | 375812e | 2015-10-09 20:23:17 | [diff] [blame] | 8294 | base::MessageLoop::current()->QuitWhenIdle(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8295 | } |
| 8296 | int on_certificate_requested_count() { |
| 8297 | return on_certificate_requested_count_; |
| 8298 | } |
| 8299 | private: |
| 8300 | int on_certificate_requested_count_; |
| 8301 | }; |
| 8302 | |
| 8303 | } // namespace |
| 8304 | |
| 8305 | // TODO(davidben): Test the rest of the code. Specifically, |
| 8306 | // - Filtering which certificates to select. |
| 8307 | // - Sending a certificate back. |
| 8308 | // - Getting a certificate request in an SSL renegotiation sending the |
| 8309 | // HTTP request. |
| 8310 | TEST_F(HTTPSRequestTest, ClientAuthTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8311 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 8312 | net::SSLServerConfig ssl_config; |
| 8313 | ssl_config.require_client_cert = true; |
| 8314 | test_server.SetSSLConfig(net::EmbeddedTestServer::CERT_OK, ssl_config); |
| 8315 | test_server.AddDefaultHandlers( |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8316 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8317 | ASSERT_TRUE(test_server.Start()); |
| 8318 | |
| 8319 | SSLClientAuthTestDelegate d; |
| 8320 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8321 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8322 | test_server.GetURL("/defaultresponse"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8323 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8324 | r->Start(); |
| 8325 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8326 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8327 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8328 | |
| 8329 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 8330 | EXPECT_FALSE(d.received_data_before_response()); |
| 8331 | EXPECT_EQ(0, d.bytes_received()); |
| 8332 | |
| 8333 | // Send no certificate. |
| 8334 | // TODO(davidben): Get temporary client cert import (with keys) working on |
| 8335 | // all platforms so we can test sending a cert as well. |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 8336 | r->ContinueWithCertificate(nullptr, nullptr); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8337 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8338 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8339 | |
| 8340 | EXPECT_EQ(1, d.response_started_count()); |
| 8341 | EXPECT_FALSE(d.received_data_before_response()); |
| 8342 | EXPECT_NE(0, d.bytes_received()); |
| 8343 | } |
| 8344 | } |
| 8345 | |
| 8346 | TEST_F(HTTPSRequestTest, ResumeTest) { |
| 8347 | // Test that we attempt a session resume when making two connections to the |
| 8348 | // same host. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8349 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8350 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8351 | SpawnedTestServer test_server( |
| 8352 | SpawnedTestServer::TYPE_HTTPS, |
| 8353 | ssl_options, |
| 8354 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8355 | ASSERT_TRUE(test_server.Start()); |
| 8356 | |
| 8357 | SSLClientSocket::ClearSessionCache(); |
| 8358 | |
| 8359 | { |
| 8360 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8361 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8362 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8363 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8364 | r->Start(); |
| 8365 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8366 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8367 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8368 | |
| 8369 | EXPECT_EQ(1, d.response_started_count()); |
| 8370 | } |
| 8371 | |
| 8372 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 8373 | CloseAllConnections(); |
| 8374 | |
| 8375 | { |
| 8376 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8377 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8378 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8379 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8380 | r->Start(); |
| 8381 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8382 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8383 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8384 | |
| 8385 | // The response will look like; |
| 8386 | // insert abc |
| 8387 | // lookup abc |
| 8388 | // insert xyz |
| 8389 | // |
| 8390 | // With a newline at the end which makes the split think that there are |
| 8391 | // four lines. |
| 8392 | |
| 8393 | EXPECT_EQ(1, d.response_started_count()); |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 8394 | std::vector<std::string> lines = base::SplitString( |
| 8395 | d.data_received(), "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8396 | ASSERT_EQ(4u, lines.size()) << d.data_received(); |
| 8397 | |
| 8398 | std::string session_id; |
| 8399 | |
| 8400 | for (size_t i = 0; i < 2; i++) { |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 8401 | std::vector<std::string> parts = base::SplitString( |
| 8402 | lines[i], "\t", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8403 | ASSERT_EQ(2u, parts.size()); |
| 8404 | if (i == 0) { |
| 8405 | EXPECT_EQ("insert", parts[0]); |
| 8406 | session_id = parts[1]; |
| 8407 | } else { |
| 8408 | EXPECT_EQ("lookup", parts[0]); |
| 8409 | EXPECT_EQ(session_id, parts[1]); |
| 8410 | } |
| 8411 | } |
| 8412 | } |
| 8413 | } |
| 8414 | |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8415 | // AssertTwoDistinctSessionsInserted checks that |session_info|, which must be |
| 8416 | // the result of fetching "ssl-session-cache" from the test server, indicates |
| 8417 | // that exactly two different sessions were inserted, with no lookups etc. |
| 8418 | static void AssertTwoDistinctSessionsInserted(const string& session_info) { |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 8419 | std::vector<std::string> lines = base::SplitString( |
| 8420 | session_info, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8421 | ASSERT_EQ(3u, lines.size()) << session_info; |
| 8422 | |
| 8423 | std::string session_id; |
| 8424 | for (size_t i = 0; i < 2; i++) { |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 8425 | std::vector<std::string> parts = base::SplitString( |
| 8426 | lines[i], "\t", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8427 | ASSERT_EQ(2u, parts.size()); |
| 8428 | EXPECT_EQ("insert", parts[0]); |
| 8429 | if (i == 0) { |
| 8430 | session_id = parts[1]; |
| 8431 | } else { |
| 8432 | EXPECT_NE(session_id, parts[1]); |
| 8433 | } |
| 8434 | } |
| 8435 | } |
| 8436 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8437 | TEST_F(HTTPSRequestTest, SSLSessionCacheShardTest) { |
| 8438 | // Test that sessions aren't resumed when the value of ssl_session_cache_shard |
| 8439 | // differs. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8440 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8441 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8442 | SpawnedTestServer test_server( |
| 8443 | SpawnedTestServer::TYPE_HTTPS, |
| 8444 | ssl_options, |
| 8445 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8446 | ASSERT_TRUE(test_server.Start()); |
| 8447 | |
| 8448 | SSLClientSocket::ClearSessionCache(); |
| 8449 | |
| 8450 | { |
| 8451 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8452 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8453 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8454 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8455 | r->Start(); |
| 8456 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8457 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8458 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8459 | |
| 8460 | EXPECT_EQ(1, d.response_started_count()); |
| 8461 | } |
| 8462 | |
| 8463 | // Now create a new HttpCache with a different ssl_session_cache_shard value. |
| 8464 | HttpNetworkSession::Params params; |
| 8465 | params.host_resolver = default_context_.host_resolver(); |
| 8466 | params.cert_verifier = default_context_.cert_verifier(); |
[email protected] | b1c988b | 2013-06-13 06:48:11 | [diff] [blame] | 8467 | params.transport_security_state = default_context_.transport_security_state(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8468 | params.proxy_service = default_context_.proxy_service(); |
| 8469 | params.ssl_config_service = default_context_.ssl_config_service(); |
| 8470 | params.http_auth_handler_factory = |
| 8471 | default_context_.http_auth_handler_factory(); |
[email protected] | cc05edc | 2013-03-08 18:04:41 | [diff] [blame] | 8472 | params.network_delegate = &default_network_delegate_; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8473 | params.http_server_properties = default_context_.http_server_properties(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8474 | |
mmenke | e65e7af | 2015-10-13 17:16:42 | [diff] [blame] | 8475 | HttpNetworkSession network_session(params); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8476 | scoped_ptr<HttpCache> cache(new HttpCache( |
mmenke | e65e7af | 2015-10-13 17:16:42 | [diff] [blame] | 8477 | &network_session, HttpCache::DefaultBackend::InMemory(0), false)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8478 | |
| 8479 | default_context_.set_http_transaction_factory(cache.get()); |
| 8480 | |
| 8481 | { |
| 8482 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8483 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8484 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8485 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8486 | r->Start(); |
| 8487 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8488 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8489 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8490 | |
| 8491 | // The response will look like; |
| 8492 | // insert abc |
| 8493 | // insert xyz |
| 8494 | // |
| 8495 | // With a newline at the end which makes the split think that there are |
| 8496 | // three lines. |
| 8497 | |
| 8498 | EXPECT_EQ(1, d.response_started_count()); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8499 | AssertTwoDistinctSessionsInserted(d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8500 | } |
| 8501 | } |
| 8502 | |
davidben | 8ecc307 | 2014-09-03 23:19:09 | [diff] [blame] | 8503 | #if defined(OS_WIN) |
| 8504 | |
| 8505 | namespace { |
| 8506 | |
| 8507 | bool IsECDSACipherSuite(uint16_t cipher_suite) { |
| 8508 | const char* key_exchange; |
| 8509 | const char* cipher; |
| 8510 | const char* mac; |
| 8511 | bool is_aead; |
| 8512 | SSLCipherSuiteToStrings(&key_exchange, &cipher, &mac, &is_aead, cipher_suite); |
| 8513 | return std::string(key_exchange).find("ECDSA") != std::string::npos; |
| 8514 | } |
| 8515 | |
| 8516 | } // namespace |
| 8517 | |
| 8518 | // Test that ECDSA is disabled on Windows XP, where ECDSA certificates cannot be |
| 8519 | // verified. |
davidben | 9399c95 | 2014-10-07 04:09:11 | [diff] [blame] | 8520 | TEST_F(HTTPSRequestTest, DisableECDSAOnXP) { |
davidben | 8ecc307 | 2014-09-03 23:19:09 | [diff] [blame] | 8521 | if (base::win::GetVersion() >= base::win::VERSION_VISTA) { |
| 8522 | LOG(INFO) << "Skipping test on this version."; |
| 8523 | return; |
| 8524 | } |
| 8525 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8526 | EmbeddedTestServer test_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 8527 | test_server.ServeFilesFromSourceDirectory("net/data/ssl"); |
davidben | 8ecc307 | 2014-09-03 23:19:09 | [diff] [blame] | 8528 | ASSERT_TRUE(test_server.Start()); |
| 8529 | |
| 8530 | TestDelegate d; |
| 8531 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8532 | test_server.GetURL("/client-cipher-list"), DEFAULT_PRIORITY, &d)); |
davidben | 8ecc307 | 2014-09-03 23:19:09 | [diff] [blame] | 8533 | r->Start(); |
| 8534 | EXPECT_TRUE(r->is_pending()); |
| 8535 | |
| 8536 | base::RunLoop().Run(); |
| 8537 | |
| 8538 | EXPECT_EQ(1, d.response_started_count()); |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 8539 | std::vector<std::string> lines = base::SplitString( |
| 8540 | d.data_received(), "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
davidben | 8ecc307 | 2014-09-03 23:19:09 | [diff] [blame] | 8541 | |
| 8542 | for (size_t i = 0; i < lines.size(); i++) { |
| 8543 | int cipher_suite; |
| 8544 | ASSERT_TRUE(base::StringToInt(lines[i], &cipher_suite)); |
| 8545 | EXPECT_FALSE(IsECDSACipherSuite(cipher_suite)) |
| 8546 | << "ClientHello advertised " << cipher_suite; |
| 8547 | } |
| 8548 | } |
| 8549 | |
| 8550 | #endif // OS_WIN |
| 8551 | |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8552 | class TestSSLConfigService : public SSLConfigService { |
| 8553 | public: |
| 8554 | TestSSLConfigService(bool ev_enabled, |
| 8555 | bool online_rev_checking, |
| 8556 | bool rev_checking_required_local_anchors) |
| 8557 | : ev_enabled_(ev_enabled), |
| 8558 | online_rev_checking_(online_rev_checking), |
| 8559 | rev_checking_required_local_anchors_( |
| 8560 | rev_checking_required_local_anchors), |
Adam Langley | ac6f24b4 | 2014-10-31 20:24:02 | [diff] [blame] | 8561 | min_version_(kDefaultSSLVersionMin), |
| 8562 | fallback_min_version_(kDefaultSSLVersionFallbackMin) {} |
| 8563 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame^] | 8564 | void set_min_version(uint16_t version) { min_version_ = version; } |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8565 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame^] | 8566 | void set_fallback_min_version(uint16_t version) { |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8567 | fallback_min_version_ = version; |
| 8568 | } |
| 8569 | |
| 8570 | // SSLConfigService: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 8571 | void GetSSLConfig(SSLConfig* config) override { |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8572 | *config = SSLConfig(); |
| 8573 | config->rev_checking_enabled = online_rev_checking_; |
| 8574 | config->verify_ev_cert = ev_enabled_; |
| 8575 | config->rev_checking_required_local_anchors = |
| 8576 | rev_checking_required_local_anchors_; |
| 8577 | if (fallback_min_version_) { |
| 8578 | config->version_fallback_min = fallback_min_version_; |
| 8579 | } |
Adam Langley | ac6f24b4 | 2014-10-31 20:24:02 | [diff] [blame] | 8580 | if (min_version_) { |
| 8581 | config->version_min = min_version_; |
| 8582 | } |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8583 | } |
| 8584 | |
| 8585 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 8586 | ~TestSSLConfigService() override {} |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8587 | |
| 8588 | private: |
| 8589 | const bool ev_enabled_; |
| 8590 | const bool online_rev_checking_; |
| 8591 | const bool rev_checking_required_local_anchors_; |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame^] | 8592 | uint16_t min_version_; |
| 8593 | uint16_t fallback_min_version_; |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8594 | }; |
| 8595 | |
| 8596 | class FallbackTestURLRequestContext : public TestURLRequestContext { |
| 8597 | public: |
| 8598 | explicit FallbackTestURLRequestContext(bool delay_initialization) |
| 8599 | : TestURLRequestContext(delay_initialization) {} |
| 8600 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame^] | 8601 | void set_fallback_min_version(uint16_t version) { |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8602 | TestSSLConfigService *ssl_config_service = |
| 8603 | new TestSSLConfigService(true /* check for EV */, |
| 8604 | false /* online revocation checking */, |
| 8605 | false /* require rev. checking for local |
| 8606 | anchors */); |
| 8607 | ssl_config_service->set_fallback_min_version(version); |
| 8608 | set_ssl_config_service(ssl_config_service); |
| 8609 | } |
| 8610 | }; |
| 8611 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8612 | class HTTPSFallbackTest : public testing::Test { |
| 8613 | public: |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8614 | HTTPSFallbackTest() : context_(true) {} |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 8615 | ~HTTPSFallbackTest() override {} |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8616 | |
| 8617 | protected: |
| 8618 | void DoFallbackTest(const SpawnedTestServer::SSLOptions& ssl_options) { |
| 8619 | DCHECK(!request_); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8620 | context_.Init(); |
| 8621 | delegate_.set_allow_certificate_errors(true); |
| 8622 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8623 | SpawnedTestServer test_server( |
| 8624 | SpawnedTestServer::TYPE_HTTPS, |
| 8625 | ssl_options, |
| 8626 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 8627 | ASSERT_TRUE(test_server.Start()); |
| 8628 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8629 | request_ = context_.CreateRequest(test_server.GetURL("/"), DEFAULT_PRIORITY, |
| 8630 | &delegate_); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8631 | request_->Start(); |
| 8632 | |
| 8633 | base::RunLoop().Run(); |
| 8634 | } |
| 8635 | |
avi | bf0746c | 2015-12-09 19:53:14 | [diff] [blame^] | 8636 | void set_fallback_min_version(uint16_t version) { |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8637 | context_.set_fallback_min_version(version); |
| 8638 | } |
| 8639 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8640 | void ExpectConnection(int version) { |
| 8641 | EXPECT_EQ(1, delegate_.response_started_count()); |
| 8642 | EXPECT_NE(0, delegate_.bytes_received()); |
| 8643 | EXPECT_EQ(version, SSLConnectionStatusToVersion( |
| 8644 | request_->ssl_info().connection_status)); |
| 8645 | EXPECT_TRUE(request_->ssl_info().connection_status & |
| 8646 | SSL_CONNECTION_VERSION_FALLBACK); |
| 8647 | } |
| 8648 | |
| 8649 | void ExpectFailure(int error) { |
| 8650 | EXPECT_EQ(1, delegate_.response_started_count()); |
| 8651 | EXPECT_FALSE(request_->status().is_success()); |
| 8652 | EXPECT_EQ(URLRequestStatus::FAILED, request_->status().status()); |
| 8653 | EXPECT_EQ(error, request_->status().error()); |
| 8654 | } |
| 8655 | |
| 8656 | private: |
| 8657 | TestDelegate delegate_; |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8658 | FallbackTestURLRequestContext context_; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8659 | scoped_ptr<URLRequest> request_; |
| 8660 | }; |
| 8661 | |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 8662 | // Tests the TLS 1.0 fallback doesn't happen. |
| 8663 | TEST_F(HTTPSFallbackTest, TLSv1NoFallback) { |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8664 | SpawnedTestServer::SSLOptions ssl_options( |
| 8665 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 8666 | ssl_options.tls_intolerant = |
| 8667 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
| 8668 | |
| 8669 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 8670 | ExpectFailure(ERR_SSL_FALLBACK_BEYOND_MINIMUM_VERSION); |
| 8671 | } |
| 8672 | |
| 8673 | // Tests the TLS 1.1 fallback. |
| 8674 | TEST_F(HTTPSFallbackTest, TLSv1_1Fallback) { |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 8675 | SpawnedTestServer::SSLOptions ssl_options( |
| 8676 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 8677 | ssl_options.tls_intolerant = |
| 8678 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_2; |
| 8679 | |
| 8680 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 8681 | ExpectConnection(SSL_CONNECTION_VERSION_TLS1_1); |
| 8682 | } |
| 8683 | |
| 8684 | // Tests that the TLS 1.1 fallback triggers on closed connections. |
| 8685 | TEST_F(HTTPSFallbackTest, TLSv1_1FallbackClosed) { |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 8686 | SpawnedTestServer::SSLOptions ssl_options( |
| 8687 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 8688 | ssl_options.tls_intolerant = |
| 8689 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_2; |
| 8690 | ssl_options.tls_intolerance_type = |
| 8691 | SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE; |
| 8692 | |
| 8693 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 8694 | ExpectConnection(SSL_CONNECTION_VERSION_TLS1_1); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8695 | } |
| 8696 | |
| 8697 | // This test is disabled on Android because the remote test server doesn't cause |
| 8698 | // a TCP reset. |
| 8699 | #if !defined(OS_ANDROID) |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 8700 | // Tests fallback to TLS 1.1 on connection reset. |
| 8701 | TEST_F(HTTPSFallbackTest, TLSv1_1FallbackReset) { |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8702 | SpawnedTestServer::SSLOptions ssl_options( |
| 8703 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 8704 | ssl_options.tls_intolerant = |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 8705 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_2; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8706 | ssl_options.tls_intolerance_type = |
| 8707 | SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_RESET; |
| 8708 | |
| 8709 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
davidben | b127ca8 | 2015-06-15 19:05:42 | [diff] [blame] | 8710 | ExpectConnection(SSL_CONNECTION_VERSION_TLS1_1); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8711 | } |
| 8712 | #endif // !OS_ANDROID |
| 8713 | |
[email protected] | 1283330 | 2014-07-02 01:57:31 | [diff] [blame] | 8714 | // Tests that we don't fallback on handshake failure with servers that implement |
| 8715 | // TLS_FALLBACK_SCSV. Also ensure that the original error code is reported. |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8716 | TEST_F(HTTPSFallbackTest, FallbackSCSV) { |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8717 | SpawnedTestServer::SSLOptions ssl_options( |
| 8718 | SpawnedTestServer::SSLOptions::CERT_OK); |
davidben | c8dc58c | 2015-01-06 21:08:43 | [diff] [blame] | 8719 | // Configure HTTPS server to be intolerant of TLS >= 1.1 in order to trigger |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8720 | // a version fallback. |
| 8721 | ssl_options.tls_intolerant = |
davidben | c8dc58c | 2015-01-06 21:08:43 | [diff] [blame] | 8722 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8723 | // Have the server process TLS_FALLBACK_SCSV so that version fallback |
| 8724 | // connections are rejected. |
| 8725 | ssl_options.fallback_scsv_enabled = true; |
| 8726 | |
| 8727 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 8728 | |
| 8729 | // ERR_SSL_VERSION_OR_CIPHER_MISMATCH is how the server simulates version |
| 8730 | // intolerance. If the fallback SCSV is processed when the original error |
| 8731 | // that caused the fallback should be returned, which should be |
| 8732 | // ERR_SSL_VERSION_OR_CIPHER_MISMATCH. |
| 8733 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
| 8734 | } |
| 8735 | |
[email protected] | 1283330 | 2014-07-02 01:57:31 | [diff] [blame] | 8736 | // Tests that we don't fallback on connection closed with servers that implement |
| 8737 | // TLS_FALLBACK_SCSV. Also ensure that the original error code is reported. |
[email protected] | 1283330 | 2014-07-02 01:57:31 | [diff] [blame] | 8738 | TEST_F(HTTPSFallbackTest, FallbackSCSVClosed) { |
[email protected] | 1283330 | 2014-07-02 01:57:31 | [diff] [blame] | 8739 | SpawnedTestServer::SSLOptions ssl_options( |
| 8740 | SpawnedTestServer::SSLOptions::CERT_OK); |
davidben | c8dc58c | 2015-01-06 21:08:43 | [diff] [blame] | 8741 | // Configure HTTPS server to be intolerant of TLS >= 1.1 in order to trigger |
[email protected] | 1283330 | 2014-07-02 01:57:31 | [diff] [blame] | 8742 | // a version fallback. |
| 8743 | ssl_options.tls_intolerant = |
davidben | c8dc58c | 2015-01-06 21:08:43 | [diff] [blame] | 8744 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
[email protected] | 1283330 | 2014-07-02 01:57:31 | [diff] [blame] | 8745 | ssl_options.tls_intolerance_type = |
| 8746 | SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE; |
| 8747 | // Have the server process TLS_FALLBACK_SCSV so that version fallback |
| 8748 | // connections are rejected. |
| 8749 | ssl_options.fallback_scsv_enabled = true; |
| 8750 | |
| 8751 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 8752 | |
| 8753 | // The original error should be replayed on rejected fallback. |
| 8754 | ExpectFailure(ERR_CONNECTION_CLOSED); |
| 8755 | } |
| 8756 | |
davidben | b937d6c | 2015-05-14 04:53:42 | [diff] [blame] | 8757 | // Test that fallback probe connections don't cause sessions to be cached. |
| 8758 | TEST_F(HTTPSRequestTest, FallbackProbeNoCache) { |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8759 | SpawnedTestServer::SSLOptions ssl_options( |
| 8760 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 8761 | ssl_options.tls_intolerant = |
davidben | b937d6c | 2015-05-14 04:53:42 | [diff] [blame] | 8762 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8763 | ssl_options.tls_intolerance_type = |
| 8764 | SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE; |
| 8765 | ssl_options.record_resume = true; |
| 8766 | |
| 8767 | SpawnedTestServer test_server( |
| 8768 | SpawnedTestServer::TYPE_HTTPS, |
| 8769 | ssl_options, |
| 8770 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 8771 | ASSERT_TRUE(test_server.Start()); |
| 8772 | |
| 8773 | SSLClientSocket::ClearSessionCache(); |
| 8774 | |
davidben | b937d6c | 2015-05-14 04:53:42 | [diff] [blame] | 8775 | // Make a connection that does a probe fallback to TLSv1 but fails because |
| 8776 | // TLSv1 fallback is disabled. We don't wish a session for this connection to |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8777 | // be inserted locally. |
| 8778 | { |
| 8779 | TestDelegate delegate; |
| 8780 | FallbackTestURLRequestContext context(true); |
| 8781 | |
davidben | b937d6c | 2015-05-14 04:53:42 | [diff] [blame] | 8782 | context.set_fallback_min_version(SSL_PROTOCOL_VERSION_TLS1_2); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8783 | context.Init(); |
| 8784 | scoped_ptr<URLRequest> request(context.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8785 | test_server.GetURL("/"), DEFAULT_PRIORITY, &delegate)); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8786 | request->Start(); |
| 8787 | |
| 8788 | base::RunLoop().Run(); |
| 8789 | |
| 8790 | EXPECT_EQ(1, delegate.response_started_count()); |
| 8791 | EXPECT_FALSE(request->status().is_success()); |
| 8792 | EXPECT_EQ(URLRequestStatus::FAILED, request->status().status()); |
| 8793 | EXPECT_EQ(ERR_SSL_FALLBACK_BEYOND_MINIMUM_VERSION, |
| 8794 | request->status().error()); |
| 8795 | } |
| 8796 | |
davidben | b937d6c | 2015-05-14 04:53:42 | [diff] [blame] | 8797 | // Now allow TLSv1 fallback connections and request the session cache log. |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8798 | { |
| 8799 | TestDelegate delegate; |
| 8800 | FallbackTestURLRequestContext context(true); |
davidben | b937d6c | 2015-05-14 04:53:42 | [diff] [blame] | 8801 | context.set_fallback_min_version(SSL_PROTOCOL_VERSION_TLS1); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8802 | |
| 8803 | context.Init(); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8804 | scoped_ptr<URLRequest> request(context.CreateRequest( |
| 8805 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &delegate)); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8806 | request->Start(); |
| 8807 | |
| 8808 | base::RunLoop().Run(); |
| 8809 | |
| 8810 | EXPECT_EQ(1, delegate.response_started_count()); |
| 8811 | EXPECT_NE(0, delegate.bytes_received()); |
davidben | b937d6c | 2015-05-14 04:53:42 | [diff] [blame] | 8812 | EXPECT_EQ( |
| 8813 | SSL_CONNECTION_VERSION_TLS1, |
| 8814 | SSLConnectionStatusToVersion(request->ssl_info().connection_status)); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8815 | EXPECT_TRUE(request->ssl_info().connection_status & |
| 8816 | SSL_CONNECTION_VERSION_FALLBACK); |
| 8817 | |
| 8818 | std::vector<std::string> lines; |
| 8819 | // If no sessions were cached then the server should have seen two sessions |
| 8820 | // inserted with no lookups. |
| 8821 | AssertTwoDistinctSessionsInserted(delegate.data_received()); |
| 8822 | } |
| 8823 | } |
| 8824 | |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8825 | class HTTPSSessionTest : public testing::Test { |
| 8826 | public: |
| 8827 | HTTPSSessionTest() : default_context_(true) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8828 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8829 | |
| 8830 | default_context_.set_network_delegate(&default_network_delegate_); |
| 8831 | default_context_.set_cert_verifier(&cert_verifier_); |
| 8832 | default_context_.Init(); |
| 8833 | } |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 8834 | ~HTTPSSessionTest() override {} |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8835 | |
| 8836 | protected: |
| 8837 | MockCertVerifier cert_verifier_; |
| 8838 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
| 8839 | TestURLRequestContext default_context_; |
| 8840 | }; |
| 8841 | |
| 8842 | // Tests that session resumption is not attempted if an invalid certificate |
| 8843 | // is presented. |
| 8844 | TEST_F(HTTPSSessionTest, DontResumeSessionsForInvalidCertificates) { |
| 8845 | SpawnedTestServer::SSLOptions ssl_options; |
| 8846 | ssl_options.record_resume = true; |
| 8847 | SpawnedTestServer test_server( |
| 8848 | SpawnedTestServer::TYPE_HTTPS, |
| 8849 | ssl_options, |
| 8850 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 8851 | ASSERT_TRUE(test_server.Start()); |
| 8852 | |
| 8853 | SSLClientSocket::ClearSessionCache(); |
| 8854 | |
| 8855 | // Simulate the certificate being expired and attempt a connection. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8856 | cert_verifier_.set_default_result(ERR_CERT_DATE_INVALID); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8857 | { |
| 8858 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8859 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8860 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8861 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8862 | r->Start(); |
| 8863 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8864 | |
| 8865 | base::RunLoop().Run(); |
| 8866 | |
| 8867 | EXPECT_EQ(1, d.response_started_count()); |
| 8868 | } |
| 8869 | |
| 8870 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 8871 | CloseAllConnections(); |
| 8872 | |
| 8873 | // Now change the certificate to be acceptable (so that the response is |
| 8874 | // loaded), and ensure that no session id is presented to the peer. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8875 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8876 | { |
| 8877 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8878 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8879 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8880 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8881 | r->Start(); |
| 8882 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8883 | |
| 8884 | base::RunLoop().Run(); |
| 8885 | |
| 8886 | // The response will look like; |
| 8887 | // insert abc |
| 8888 | // insert xyz |
| 8889 | // |
| 8890 | // With a newline at the end which makes the split think that there are |
| 8891 | // three lines. |
| 8892 | // |
| 8893 | // If a session was presented (eg: a bug), then the response would look |
| 8894 | // like; |
| 8895 | // insert abc |
| 8896 | // lookup abc |
| 8897 | // insert xyz |
| 8898 | |
| 8899 | EXPECT_EQ(1, d.response_started_count()); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8900 | AssertTwoDistinctSessionsInserted(d.data_received()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8901 | } |
| 8902 | } |
| 8903 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8904 | // This the fingerprint of the "Testing CA" certificate used by the testserver. |
| 8905 | // See net/data/ssl/certificates/ocsp-test-root.pem. |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 8906 | static const SHA1HashValue kOCSPTestCertFingerprint = |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8907 | { { 0xf1, 0xad, 0xf6, 0xce, 0x42, 0xac, 0xe7, 0xb4, 0xf4, 0x24, |
| 8908 | 0xdb, 0x1a, 0xf7, 0xa0, 0x9f, 0x09, 0xa1, 0xea, 0xf1, 0x5c } }; |
| 8909 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 8910 | // This is the SHA256, SPKI hash of the "Testing CA" certificate used by the |
| 8911 | // testserver. |
| 8912 | static const SHA256HashValue kOCSPTestCertSPKI = { { |
| 8913 | 0xee, 0xe6, 0x51, 0x2d, 0x4c, 0xfa, 0xf7, 0x3e, |
| 8914 | 0x6c, 0xd8, 0xca, 0x67, 0xed, 0xb5, 0x5d, 0x49, |
| 8915 | 0x76, 0xe1, 0x52, 0xa7, 0x6e, 0x0e, 0xa0, 0x74, |
| 8916 | 0x09, 0x75, 0xe6, 0x23, 0x24, 0xbd, 0x1b, 0x28, |
| 8917 | } }; |
| 8918 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8919 | // This is the policy OID contained in the certificates that testserver |
| 8920 | // generates. |
| 8921 | static const char kOCSPTestCertPolicy[] = "1.3.6.1.4.1.11129.2.4.1"; |
| 8922 | |
| 8923 | class HTTPSOCSPTest : public HTTPSRequestTest { |
| 8924 | public: |
| 8925 | HTTPSOCSPTest() |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 8926 | : context_(true), |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 8927 | ev_test_policy_( |
| 8928 | new ScopedTestEVPolicy(EVRootCAMetadata::GetInstance(), |
| 8929 | kOCSPTestCertFingerprint, |
| 8930 | kOCSPTestCertPolicy)) { |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8931 | } |
| 8932 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 8933 | void SetUp() override { |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 8934 | SetupContext(&context_); |
| 8935 | context_.Init(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8936 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8937 | scoped_refptr<X509Certificate> root_cert = |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 8938 | ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem"); |
dcheng | c2e01e8 | 2014-08-27 00:24:42 | [diff] [blame] | 8939 | CHECK_NE(static_cast<X509Certificate*>(NULL), root_cert.get()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 8940 | test_root_.reset(new ScopedTestRoot(root_cert.get())); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8941 | |
davidben | 71f35ff | 2015-04-17 20:54:48 | [diff] [blame] | 8942 | #if defined(USE_NSS_CERTS) || defined(OS_IOS) |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 8943 | SetURLRequestContextForNSSHttpIO(&context_); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8944 | EnsureNSSHttpIOInit(); |
| 8945 | #endif |
| 8946 | } |
| 8947 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8948 | void DoConnection(const SpawnedTestServer::SSLOptions& ssl_options, |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8949 | CertStatus* out_cert_status) { |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 8950 | // We always overwrite out_cert_status. |
| 8951 | *out_cert_status = 0; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8952 | SpawnedTestServer test_server( |
| 8953 | SpawnedTestServer::TYPE_HTTPS, |
| 8954 | ssl_options, |
| 8955 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8956 | ASSERT_TRUE(test_server.Start()); |
| 8957 | |
| 8958 | TestDelegate d; |
| 8959 | d.set_allow_certificate_errors(true); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 8960 | scoped_ptr<URLRequest> r( |
| 8961 | context_.CreateRequest(test_server.GetURL("/"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8962 | r->Start(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8963 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8964 | base::RunLoop().Run(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8965 | |
| 8966 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8967 | *out_cert_status = r->ssl_info().cert_status; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8968 | } |
| 8969 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 8970 | ~HTTPSOCSPTest() override { |
davidben | 71f35ff | 2015-04-17 20:54:48 | [diff] [blame] | 8971 | #if defined(USE_NSS_CERTS) || defined(OS_IOS) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8972 | ShutdownNSSHttpIO(); |
| 8973 | #endif |
| 8974 | } |
| 8975 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8976 | protected: |
| 8977 | // SetupContext configures the URLRequestContext that will be used for making |
| 8978 | // connetions to testserver. This can be overridden in test subclasses for |
| 8979 | // different behaviour. |
| 8980 | virtual void SetupContext(URLRequestContext* context) { |
| 8981 | context->set_ssl_config_service( |
| 8982 | new TestSSLConfigService(true /* check for EV */, |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 8983 | true /* online revocation checking */, |
| 8984 | false /* require rev. checking for local |
| 8985 | anchors */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8986 | } |
| 8987 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8988 | scoped_ptr<ScopedTestRoot> test_root_; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 8989 | TestURLRequestContext context_; |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 8990 | scoped_ptr<ScopedTestEVPolicy> ev_test_policy_; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8991 | }; |
| 8992 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8993 | static CertStatus ExpectedCertStatusForFailedOnlineRevocationCheck() { |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 8994 | #if defined(OS_WIN) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8995 | // Windows can return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION but we don't |
| 8996 | // have that ability on other platforms. |
| 8997 | return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION; |
| 8998 | #else |
| 8999 | return 0; |
| 9000 | #endif |
| 9001 | } |
| 9002 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 9003 | // SystemSupportsHardFailRevocationChecking returns true iff the current |
| 9004 | // operating system supports revocation checking and can distinguish between |
| 9005 | // situations where a given certificate lacks any revocation information (eg: |
| 9006 | // no CRLDistributionPoints and no OCSP Responder AuthorityInfoAccess) and when |
| 9007 | // revocation information cannot be obtained (eg: the CRL was unreachable). |
| 9008 | // If it does not, then tests which rely on 'hard fail' behaviour should be |
| 9009 | // skipped. |
| 9010 | static bool SystemSupportsHardFailRevocationChecking() { |
davidben | 71f35ff | 2015-04-17 20:54:48 | [diff] [blame] | 9011 | #if defined(OS_WIN) || defined(USE_NSS_CERTS) || defined(OS_IOS) |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 9012 | return true; |
| 9013 | #else |
| 9014 | return false; |
| 9015 | #endif |
| 9016 | } |
| 9017 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9018 | // SystemUsesChromiumEVMetadata returns true iff the current operating system |
| 9019 | // uses Chromium's EV metadata (i.e. EVRootCAMetadata). If it does not, then |
| 9020 | // several tests are effected because our testing EV certificate won't be |
| 9021 | // recognised as EV. |
| 9022 | static bool SystemUsesChromiumEVMetadata() { |
[email protected] | e1b2d73 | 2014-03-28 16:20:32 | [diff] [blame] | 9023 | #if defined(USE_OPENSSL_CERTS) && !defined(OS_ANDROID) |
[email protected] | 5c50419 | 2012-03-27 19:00:57 | [diff] [blame] | 9024 | // http://crbug.com/117478 - OpenSSL does not support EV validation. |
| 9025 | return false; |
[email protected] | e1b2d73 | 2014-03-28 16:20:32 | [diff] [blame] | 9026 | #elif (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID) |
| 9027 | // On OS X and Android, we use the system to tell us whether a certificate is |
| 9028 | // EV or not and the system won't recognise our testing root. |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 9029 | return false; |
| 9030 | #else |
| 9031 | return true; |
| 9032 | #endif |
| 9033 | } |
| 9034 | |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 9035 | static bool SystemSupportsOCSP() { |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 9036 | #if defined(USE_OPENSSL_CERTS) |
[email protected] | 5c50419 | 2012-03-27 19:00:57 | [diff] [blame] | 9037 | // http://crbug.com/117478 - OpenSSL does not support OCSP. |
| 9038 | return false; |
| 9039 | #elif defined(OS_WIN) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9040 | return base::win::GetVersion() >= base::win::VERSION_VISTA; |
| 9041 | #elif defined(OS_ANDROID) |
| 9042 | // TODO(jnd): http://crbug.com/117478 - EV verification is not yet supported. |
| 9043 | return false; |
| 9044 | #else |
| 9045 | return true; |
| 9046 | #endif |
| 9047 | } |
| 9048 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 9049 | static bool SystemSupportsOCSPStapling() { |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 9050 | #if defined(USE_NSS_CERTS) || defined(OS_IOS) |
| 9051 | return true; |
| 9052 | #elif defined(OS_WIN) |
| 9053 | return base::win::GetVersion() >= base::win::VERSION_VISTA; |
| 9054 | #else |
| 9055 | return false; |
| 9056 | #endif |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 9057 | } |
| 9058 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9059 | TEST_F(HTTPSOCSPTest, Valid) { |
| 9060 | if (!SystemSupportsOCSP()) { |
| 9061 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 9062 | return; |
| 9063 | } |
| 9064 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9065 | SpawnedTestServer::SSLOptions ssl_options( |
| 9066 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9067 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9068 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 9069 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 9070 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9071 | |
| 9072 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 9073 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9074 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 9075 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9076 | |
| 9077 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 9078 | } |
| 9079 | |
| 9080 | TEST_F(HTTPSOCSPTest, Revoked) { |
| 9081 | if (!SystemSupportsOCSP()) { |
| 9082 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 9083 | return; |
| 9084 | } |
| 9085 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9086 | SpawnedTestServer::SSLOptions ssl_options( |
| 9087 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9088 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9089 | |
| 9090 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 9091 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9092 | |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 9093 | #if !(defined(OS_MACOSX) && !defined(OS_IOS)) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9094 | // Doesn't pass on OS X yet for reasons that need to be investigated. |
| 9095 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 9096 | #endif |
| 9097 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 9098 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 9099 | } |
| 9100 | |
| 9101 | TEST_F(HTTPSOCSPTest, Invalid) { |
| 9102 | if (!SystemSupportsOCSP()) { |
| 9103 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 9104 | return; |
| 9105 | } |
| 9106 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9107 | SpawnedTestServer::SSLOptions ssl_options( |
| 9108 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9109 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9110 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 9111 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 9112 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9113 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9114 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9115 | cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9116 | |
| 9117 | // Without a positive OCSP response, we shouldn't show the EV status. |
| 9118 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 9119 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 9120 | } |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9121 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 9122 | TEST_F(HTTPSOCSPTest, ValidStapled) { |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 9123 | if (!SystemSupportsOCSPStapling()) { |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 9124 | LOG(WARNING) |
| 9125 | << "Skipping test because system doesn't support OCSP stapling"; |
| 9126 | return; |
| 9127 | } |
| 9128 | |
| 9129 | SpawnedTestServer::SSLOptions ssl_options( |
| 9130 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9131 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 9132 | ssl_options.staple_ocsp_response = true; |
| 9133 | ssl_options.ocsp_server_unavailable = true; |
| 9134 | |
| 9135 | CertStatus cert_status; |
| 9136 | DoConnection(ssl_options, &cert_status); |
| 9137 | |
| 9138 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 9139 | |
| 9140 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 9141 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 9142 | |
| 9143 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 9144 | } |
| 9145 | |
davidben | 6c3191b | 2014-11-21 22:38:04 | [diff] [blame] | 9146 | // Disabled on NSS ports. See https://crbug.com/431716. |
davidben | 71f35ff | 2015-04-17 20:54:48 | [diff] [blame] | 9147 | #if defined(USE_NSS_CERTS) |
davidben | 6c3191b | 2014-11-21 22:38:04 | [diff] [blame] | 9148 | #define MAYBE_RevokedStapled DISABLED_RevokedStapled |
| 9149 | #else |
| 9150 | #define MAYBE_RevokedStapled RevokedStapled |
| 9151 | #endif |
| 9152 | TEST_F(HTTPSOCSPTest, MAYBE_RevokedStapled) { |
mathp | c992e60 | 2015-10-21 20:34:03 | [diff] [blame] | 9153 | if (!SystemSupportsOCSPStapling()) { |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 9154 | LOG(WARNING) |
| 9155 | << "Skipping test because system doesn't support OCSP stapling"; |
| 9156 | return; |
| 9157 | } |
| 9158 | |
| 9159 | SpawnedTestServer::SSLOptions ssl_options( |
| 9160 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9161 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 9162 | ssl_options.staple_ocsp_response = true; |
| 9163 | ssl_options.ocsp_server_unavailable = true; |
| 9164 | |
| 9165 | CertStatus cert_status; |
| 9166 | DoConnection(ssl_options, &cert_status); |
| 9167 | |
| 9168 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 9169 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 9170 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 9171 | } |
| 9172 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 9173 | class HTTPSHardFailTest : public HTTPSOCSPTest { |
| 9174 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 9175 | void SetupContext(URLRequestContext* context) override { |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 9176 | context->set_ssl_config_service( |
| 9177 | new TestSSLConfigService(false /* check for EV */, |
| 9178 | false /* online revocation checking */, |
| 9179 | true /* require rev. checking for local |
| 9180 | anchors */)); |
| 9181 | } |
| 9182 | }; |
| 9183 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 9184 | TEST_F(HTTPSHardFailTest, FailsOnOCSPInvalid) { |
| 9185 | if (!SystemSupportsOCSP()) { |
| 9186 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 9187 | return; |
| 9188 | } |
| 9189 | |
| 9190 | if (!SystemSupportsHardFailRevocationChecking()) { |
| 9191 | LOG(WARNING) << "Skipping test because system doesn't support hard fail " |
| 9192 | << "revocation checking"; |
| 9193 | return; |
| 9194 | } |
| 9195 | |
| 9196 | SpawnedTestServer::SSLOptions ssl_options( |
| 9197 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9198 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
| 9199 | |
| 9200 | CertStatus cert_status; |
| 9201 | DoConnection(ssl_options, &cert_status); |
| 9202 | |
| 9203 | EXPECT_EQ(CERT_STATUS_REVOKED, |
| 9204 | cert_status & CERT_STATUS_REVOKED); |
| 9205 | |
| 9206 | // Without a positive OCSP response, we shouldn't show the EV status. |
| 9207 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 9208 | } |
| 9209 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9210 | class HTTPSEVCRLSetTest : public HTTPSOCSPTest { |
| 9211 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 9212 | void SetupContext(URLRequestContext* context) override { |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9213 | context->set_ssl_config_service( |
| 9214 | new TestSSLConfigService(true /* check for EV */, |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 9215 | false /* online revocation checking */, |
| 9216 | false /* require rev. checking for local |
| 9217 | anchors */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9218 | } |
| 9219 | }; |
| 9220 | |
| 9221 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndInvalidOCSP) { |
| 9222 | if (!SystemSupportsOCSP()) { |
| 9223 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 9224 | return; |
| 9225 | } |
| 9226 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9227 | SpawnedTestServer::SSLOptions ssl_options( |
| 9228 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9229 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9230 | SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>()); |
| 9231 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 9232 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 9233 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9234 | |
| 9235 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
| 9236 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 9237 | |
| 9238 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 9239 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 9240 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9241 | } |
| 9242 | |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 9243 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndRevokedOCSP) { |
| 9244 | if (!SystemSupportsOCSP()) { |
| 9245 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 9246 | return; |
| 9247 | } |
| 9248 | |
| 9249 | SpawnedTestServer::SSLOptions ssl_options( |
| 9250 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9251 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 9252 | SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>()); |
| 9253 | |
| 9254 | CertStatus cert_status; |
| 9255 | DoConnection(ssl_options, &cert_status); |
| 9256 | |
| 9257 | // Currently only works for Windows. When using NSS or OS X, it's not |
| 9258 | // possible to determine whether the check failed because of actual |
| 9259 | // revocation or because there was an OCSP failure. |
| 9260 | #if defined(OS_WIN) |
| 9261 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 9262 | #else |
| 9263 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 9264 | #endif |
| 9265 | |
| 9266 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 9267 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 9268 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 9269 | } |
| 9270 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9271 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndGoodOCSP) { |
| 9272 | if (!SystemSupportsOCSP()) { |
| 9273 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 9274 | return; |
| 9275 | } |
| 9276 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9277 | SpawnedTestServer::SSLOptions ssl_options( |
| 9278 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9279 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9280 | SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>()); |
| 9281 | |
| 9282 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 9283 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9284 | |
| 9285 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 9286 | |
| 9287 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 9288 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 9289 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 9290 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9291 | } |
| 9292 | |
| 9293 | TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSet) { |
| 9294 | if (!SystemSupportsOCSP()) { |
| 9295 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 9296 | return; |
| 9297 | } |
| 9298 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9299 | SpawnedTestServer::SSLOptions ssl_options( |
| 9300 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9301 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9302 | SSLConfigService::SetCRLSet( |
| 9303 | scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); |
| 9304 | |
| 9305 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 9306 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9307 | |
| 9308 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
| 9309 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 9310 | |
| 9311 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 9312 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 9313 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9314 | } |
| 9315 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 9316 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetCovered) { |
| 9317 | if (!SystemSupportsOCSP()) { |
| 9318 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 9319 | return; |
| 9320 | } |
| 9321 | |
| 9322 | SpawnedTestServer::SSLOptions ssl_options( |
| 9323 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9324 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
| 9325 | SSLConfigService::SetCRLSet( |
| 9326 | scoped_refptr<CRLSet>(CRLSet::ForTesting( |
| 9327 | false, &kOCSPTestCertSPKI, ""))); |
| 9328 | |
| 9329 | CertStatus cert_status; |
| 9330 | DoConnection(ssl_options, &cert_status); |
| 9331 | |
| 9332 | // With a fresh CRLSet that covers the issuing certificate, we shouldn't do a |
| 9333 | // revocation check for EV. |
| 9334 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 9335 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 9336 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 9337 | EXPECT_FALSE( |
| 9338 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 9339 | } |
| 9340 | |
| 9341 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetNotCovered) { |
| 9342 | if (!SystemSupportsOCSP()) { |
| 9343 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 9344 | return; |
| 9345 | } |
| 9346 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9347 | SpawnedTestServer::SSLOptions ssl_options( |
| 9348 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9349 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9350 | SSLConfigService::SetCRLSet( |
| 9351 | scoped_refptr<CRLSet>(CRLSet::EmptyCRLSetForTesting())); |
| 9352 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 9353 | CertStatus cert_status = 0; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 9354 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9355 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 9356 | // Even with a fresh CRLSet, we should still do online revocation checks when |
| 9357 | // the certificate chain isn't covered by the CRLSet, which it isn't in this |
| 9358 | // test. |
| 9359 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
| 9360 | cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9361 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 9362 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9363 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 9364 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9365 | } |
| 9366 | |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 9367 | TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSetAndRevokedNonEVCert) { |
| 9368 | // Test that when EV verification is requested, but online revocation |
| 9369 | // checking is disabled, and the leaf certificate is not in fact EV, that |
| 9370 | // no revocation checking actually happens. |
| 9371 | if (!SystemSupportsOCSP()) { |
| 9372 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 9373 | return; |
| 9374 | } |
| 9375 | |
| 9376 | // Unmark the certificate's OID as EV, which should disable revocation |
| 9377 | // checking (as per the user preference) |
| 9378 | ev_test_policy_.reset(); |
| 9379 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9380 | SpawnedTestServer::SSLOptions ssl_options( |
| 9381 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9382 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 9383 | SSLConfigService::SetCRLSet( |
| 9384 | scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); |
| 9385 | |
| 9386 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 9387 | DoConnection(ssl_options, &cert_status); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 9388 | |
| 9389 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 9390 | |
| 9391 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 9392 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 9393 | } |
| 9394 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9395 | class HTTPSCRLSetTest : public HTTPSOCSPTest { |
| 9396 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 9397 | void SetupContext(URLRequestContext* context) override { |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9398 | context->set_ssl_config_service( |
| 9399 | new TestSSLConfigService(false /* check for EV */, |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 9400 | false /* online revocation checking */, |
| 9401 | false /* require rev. checking for local |
| 9402 | anchors */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9403 | } |
| 9404 | }; |
| 9405 | |
| 9406 | TEST_F(HTTPSCRLSetTest, ExpiredCRLSet) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 9407 | SpawnedTestServer::SSLOptions ssl_options( |
| 9408 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9409 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9410 | SSLConfigService::SetCRLSet( |
| 9411 | scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); |
| 9412 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 9413 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 9414 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 9415 | |
| 9416 | // If we're not trying EV verification then, even if the CRLSet has expired, |
| 9417 | // we don't fall back to online revocation checks. |
| 9418 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 9419 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 9420 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 9421 | } |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 9422 | |
| 9423 | TEST_F(HTTPSCRLSetTest, CRLSetRevoked) { |
[email protected] | 591cffcd | 2014-08-18 20:02:30 | [diff] [blame] | 9424 | #if defined(OS_ANDROID) |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 9425 | LOG(WARNING) << "Skipping test because system doesn't support CRLSets"; |
| 9426 | return; |
| 9427 | #endif |
| 9428 | |
| 9429 | SpawnedTestServer::SSLOptions ssl_options( |
| 9430 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 9431 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 9432 | ssl_options.cert_serial = 10; |
| 9433 | SSLConfigService::SetCRLSet( |
| 9434 | scoped_refptr<CRLSet>(CRLSet::ForTesting( |
| 9435 | false, &kOCSPTestCertSPKI, "\x0a"))); |
| 9436 | |
| 9437 | CertStatus cert_status = 0; |
| 9438 | DoConnection(ssl_options, &cert_status); |
| 9439 | |
| 9440 | // If the certificate is recorded as revoked in the CRLSet, that should be |
| 9441 | // reflected without online revocation checking. |
| 9442 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 9443 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 9444 | EXPECT_FALSE( |
| 9445 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 9446 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9447 | #endif // !defined(OS_IOS) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 9448 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9449 | #if !defined(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) |
| 9450 | // These tests aren't passing on Android. Either the RemoteTestServer isn't |
| 9451 | // starting up successfully, or it can't access the test files. |
| 9452 | // TODO(mmenke): Fix this. See http://crbug.com/495220 |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 9453 | class URLRequestTestFTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9454 | public: |
[email protected] | d9fca99a | 2012-02-24 16:16:20 | [diff] [blame] | 9455 | URLRequestTestFTP() |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9456 | : ftp_transaction_factory_(&host_resolver_), |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9457 | ftp_test_server_(SpawnedTestServer::TYPE_FTP, |
| 9458 | SpawnedTestServer::kLocalhost, |
| 9459 | base::FilePath(kTestFilePath)) { |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9460 | // Can't use |default_context_|'s HostResolver to set up the |
| 9461 | // FTPTransactionFactory because it hasn't been created yet. |
| 9462 | default_context_.set_host_resolver(&host_resolver_); |
| 9463 | } |
| 9464 | |
| 9465 | // URLRequestTest interface: |
| 9466 | void SetUpFactory() override { |
| 9467 | // Add FTP support to the default URLRequestContext. |
| 9468 | job_factory_impl_->SetProtocolHandler( |
svaldez | 5d58c9e | 2015-08-24 21:36:20 | [diff] [blame] | 9469 | "ftp", |
| 9470 | make_scoped_ptr(new FtpProtocolHandler(&ftp_transaction_factory_))); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9471 | } |
| 9472 | |
| 9473 | std::string GetTestFileContents() { |
| 9474 | base::FilePath path; |
| 9475 | EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &path)); |
| 9476 | path = path.Append(kTestFilePath); |
| 9477 | path = path.AppendASCII(kFtpTestFile); |
| 9478 | std::string contents; |
| 9479 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
| 9480 | return contents; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9481 | } |
| 9482 | |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 9483 | protected: |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9484 | MockHostResolver host_resolver_; |
| 9485 | FtpNetworkLayer ftp_transaction_factory_; |
| 9486 | |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9487 | SpawnedTestServer ftp_test_server_; |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 9488 | }; |
| 9489 | |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 9490 | // Make sure an FTP request using an unsafe ports fails. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 9491 | TEST_F(URLRequestTestFTP, UnsafePort) { |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 9492 | GURL url("ftp://127.0.0.1:7"); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 9493 | |
| 9494 | TestDelegate d; |
| 9495 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9496 | scoped_ptr<URLRequest> r( |
| 9497 | default_context_.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9498 | r->Start(); |
| 9499 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 9500 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9501 | base::RunLoop().Run(); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 9502 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9503 | EXPECT_FALSE(r->is_pending()); |
| 9504 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
| 9505 | EXPECT_EQ(ERR_UNSAFE_PORT, r->status().error()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 9506 | } |
| 9507 | } |
| 9508 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9509 | TEST_F(URLRequestTestFTP, FTPDirectoryListing) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9510 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9511 | |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 9512 | TestDelegate d; |
| 9513 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9514 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9515 | ftp_test_server_.GetURL("/"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9516 | r->Start(); |
| 9517 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 9518 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9519 | base::RunLoop().Run(); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 9520 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9521 | EXPECT_FALSE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 9522 | EXPECT_EQ(1, d.response_started_count()); |
| 9523 | EXPECT_FALSE(d.received_data_before_response()); |
| 9524 | EXPECT_LT(0, d.bytes_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9525 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9526 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9527 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9528 | r->GetSocketAddress().port()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 9529 | } |
| 9530 | } |
| 9531 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9532 | TEST_F(URLRequestTestFTP, FTPGetTestAnonymous) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9533 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9534 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9535 | TestDelegate d; |
| 9536 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9537 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9538 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9539 | r->Start(); |
| 9540 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9541 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9542 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9543 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9544 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9545 | EXPECT_EQ(1, d.response_started_count()); |
| 9546 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9547 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9548 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9549 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9550 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9551 | r->GetSocketAddress().port()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9552 | } |
| 9553 | } |
| 9554 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9555 | TEST_F(URLRequestTestFTP, FTPGetTest) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9556 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9557 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9558 | TestDelegate d; |
| 9559 | { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9560 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 9561 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 9562 | "chrome"), |
| 9563 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9564 | r->Start(); |
| 9565 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9566 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9567 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9568 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9569 | EXPECT_FALSE(r->is_pending()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9570 | EXPECT_EQ(1, d.response_started_count()); |
| 9571 | EXPECT_FALSE(d.received_data_before_response()); |
| 9572 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9573 | EXPECT_EQ(ftp_test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9574 | r->GetSocketAddress().host()); |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9575 | EXPECT_EQ(ftp_test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9576 | r->GetSocketAddress().port()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 9577 | |
| 9578 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9579 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 9580 | TestLoadTimingNoHttpResponse(load_timing_info); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9581 | } |
| 9582 | } |
| 9583 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9584 | TEST_F(URLRequestTestFTP, FTPCheckWrongPassword) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9585 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9586 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9587 | TestDelegate d; |
| 9588 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9589 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9590 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 9591 | "wrong_password"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9592 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9593 | r->Start(); |
| 9594 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9595 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9596 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9597 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9598 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9599 | EXPECT_EQ(1, d.response_started_count()); |
| 9600 | EXPECT_FALSE(d.received_data_before_response()); |
| 9601 | EXPECT_EQ(d.bytes_received(), 0); |
| 9602 | } |
| 9603 | } |
| 9604 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9605 | TEST_F(URLRequestTestFTP, FTPCheckWrongPasswordRestart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9606 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9607 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9608 | TestDelegate d; |
| 9609 | // Set correct login credentials. The delegate will be asked for them when |
| 9610 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 9611 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9612 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9613 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9614 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 9615 | "wrong_password"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9616 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9617 | r->Start(); |
| 9618 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9619 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9620 | base::RunLoop().Run(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9621 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9622 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9623 | EXPECT_EQ(1, d.response_started_count()); |
| 9624 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9625 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9626 | } |
| 9627 | } |
| 9628 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9629 | TEST_F(URLRequestTestFTP, FTPCheckWrongUser) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9630 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9631 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9632 | TestDelegate d; |
| 9633 | { |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9634 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9635 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "wrong_user", |
| 9636 | "chrome"), |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9637 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9638 | r->Start(); |
| 9639 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9640 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9641 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9642 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9643 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9644 | EXPECT_EQ(1, d.response_started_count()); |
| 9645 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9646 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9647 | } |
| 9648 | } |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9649 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9650 | TEST_F(URLRequestTestFTP, FTPCheckWrongUserRestart) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9651 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9652 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9653 | TestDelegate d; |
| 9654 | // Set correct login credentials. The delegate will be asked for them when |
| 9655 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 9656 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9657 | { |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9658 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9659 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "wrong_user", |
| 9660 | "chrome"), |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9661 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9662 | r->Start(); |
| 9663 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9664 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9665 | base::RunLoop().Run(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9666 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9667 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9668 | EXPECT_EQ(1, d.response_started_count()); |
| 9669 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9670 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9671 | } |
| 9672 | } |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9673 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9674 | TEST_F(URLRequestTestFTP, FTPCacheURLCredentials) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9675 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9676 | |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9677 | scoped_ptr<TestDelegate> d(new TestDelegate); |
| 9678 | { |
| 9679 | // Pass correct login identity in the URL. |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9680 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 9681 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 9682 | "chrome"), |
| 9683 | DEFAULT_PRIORITY, d.get())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9684 | r->Start(); |
| 9685 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9686 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9687 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9688 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9689 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9690 | EXPECT_EQ(1, d->response_started_count()); |
| 9691 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9692 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9693 | } |
| 9694 | |
| 9695 | d.reset(new TestDelegate); |
| 9696 | { |
| 9697 | // This request should use cached identity from previous request. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9698 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9699 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, d.get())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9700 | r->Start(); |
| 9701 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9702 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9703 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9704 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9705 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9706 | EXPECT_EQ(1, d->response_started_count()); |
| 9707 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9708 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9709 | } |
| 9710 | } |
| 9711 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9712 | TEST_F(URLRequestTestFTP, FTPCacheLoginBoxCredentials) { |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9713 | ASSERT_TRUE(ftp_test_server_.Start()); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9714 | |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9715 | scoped_ptr<TestDelegate> d(new TestDelegate); |
| 9716 | // Set correct login credentials. The delegate will be asked for them when |
| 9717 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 9718 | d->set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9719 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9720 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9721 | ftp_test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
| 9722 | "wrong_password"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9723 | DEFAULT_PRIORITY, d.get())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9724 | r->Start(); |
| 9725 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9726 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9727 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9728 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9729 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9730 | EXPECT_EQ(1, d->response_started_count()); |
| 9731 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9732 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9733 | } |
| 9734 | |
| 9735 | // Use a new delegate without explicit credentials. The cached ones should be |
| 9736 | // used. |
| 9737 | d.reset(new TestDelegate); |
| 9738 | { |
| 9739 | // Don't pass wrong credentials in the URL, they would override valid cached |
| 9740 | // ones. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9741 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
tommycli | 59a6343 | 2015-11-06 00:10:55 | [diff] [blame] | 9742 | ftp_test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, d.get())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9743 | r->Start(); |
| 9744 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9745 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9746 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9747 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9748 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9749 | EXPECT_EQ(1, d->response_started_count()); |
| 9750 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame] | 9751 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9752 | } |
| 9753 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9754 | #endif // !defined(DISABLE_FTP_SUPPORT) |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 9755 | |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9756 | TEST_F(URLRequestTest, NetworkAccessedClearBeforeNetworkStart) { |
| 9757 | TestDelegate d; |
| 9758 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 9759 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
| 9760 | d.set_quit_on_network_start(true); |
| 9761 | |
| 9762 | EXPECT_FALSE(req->response_info().network_accessed); |
| 9763 | |
| 9764 | req->Start(); |
| 9765 | base::RunLoop().Run(); |
| 9766 | |
| 9767 | EXPECT_EQ(1, d.received_before_network_start_count()); |
| 9768 | EXPECT_EQ(0, d.response_started_count()); |
| 9769 | EXPECT_FALSE(req->response_info().network_accessed); |
| 9770 | |
| 9771 | req->ResumeNetworkStart(); |
| 9772 | base::RunLoop().Run(); |
| 9773 | } |
| 9774 | |
| 9775 | TEST_F(URLRequestTest, NetworkAccessedClearOnDataRequest) { |
| 9776 | TestDelegate d; |
| 9777 | scoped_ptr<URLRequest> req( |
| 9778 | default_context_.CreateRequest(GURL("data:,"), DEFAULT_PRIORITY, &d)); |
| 9779 | |
| 9780 | EXPECT_FALSE(req->response_info().network_accessed); |
| 9781 | |
| 9782 | req->Start(); |
| 9783 | base::RunLoop().Run(); |
| 9784 | |
| 9785 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 9786 | EXPECT_FALSE(req->response_info().network_accessed); |
| 9787 | } |
| 9788 | |
| 9789 | TEST_F(URLRequestTest, NetworkAccessedSetOnHostResolutionFailure) { |
| 9790 | MockHostResolver host_resolver; |
| 9791 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 9792 | TestURLRequestContext context(true); |
| 9793 | context.set_network_delegate(&network_delegate); |
| 9794 | context.set_host_resolver(&host_resolver); |
| 9795 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 9796 | context.Init(); |
| 9797 | |
| 9798 | TestDelegate d; |
| 9799 | scoped_ptr<URLRequest> req(context.CreateRequest( |
| 9800 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
| 9801 | |
| 9802 | EXPECT_FALSE(req->response_info().network_accessed); |
| 9803 | |
| 9804 | req->Start(); |
| 9805 | base::RunLoop().Run(); |
| 9806 | EXPECT_TRUE(req->response_info().network_accessed); |
| 9807 | } |
| 9808 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9809 | // Test that URLRequest is canceled correctly. |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 9810 | // See http://crbug.com/508900 |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9811 | TEST_F(URLRequestTest, URLRequestRedirectJobCancelRequest) { |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 9812 | TestDelegate d; |
| 9813 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 9814 | GURL("http://not-a-real-domain/"), DEFAULT_PRIORITY, &d)); |
| 9815 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9816 | scoped_ptr<URLRequestRedirectJob> job(new URLRequestRedirectJob( |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 9817 | req.get(), &default_network_delegate_, |
| 9818 | GURL("http://this-should-never-be-navigated-to/"), |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 9819 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, "Jumbo shrimp")); |
| 9820 | AddTestInterceptor()->set_main_intercept_job(std::move(job)); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 9821 | |
| 9822 | req->Start(); |
| 9823 | req->Cancel(); |
alexanderk | cd904b5 | 2015-07-24 18:57:22 | [diff] [blame] | 9824 | base::RunLoop().RunUntilIdle(); |
| 9825 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
| 9826 | EXPECT_EQ(0, d.received_redirect_count()); |
| 9827 | } |
| 9828 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 9829 | } // namespace net |