OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "build/build_config.h" | 5 #include "build/build_config.h" |
6 | 6 |
7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
8 #include <windows.h> | 8 #include <windows.h> |
9 #include <shlobj.h> | 9 #include <shlobj.h> |
10 #endif | 10 #endif |
11 | 11 |
12 #include <stdint.h> | 12 #include <stdint.h> |
13 | 13 |
14 #include <algorithm> | 14 #include <algorithm> |
15 | 15 |
16 #include "base/basictypes.h" | 16 #include "base/basictypes.h" |
17 #include "base/bind.h" | 17 #include "base/bind.h" |
18 #include "base/compiler_specific.h" | 18 #include "base/compiler_specific.h" |
19 #include "base/files/file_path.h" | 19 #include "base/files/file_path.h" |
20 #include "base/files/file_util.h" | 20 #include "base/files/file_util.h" |
21 #include "base/files/scoped_temp_dir.h" | 21 #include "base/files/scoped_temp_dir.h" |
22 #include "base/format_macros.h" | 22 #include "base/format_macros.h" |
23 #include "base/json/json_reader.h" | 23 #include "base/json/json_reader.h" |
24 #include "base/location.h" | 24 #include "base/location.h" |
25 #include "base/memory/scoped_ptr.h" | 25 #include "base/memory/scoped_ptr.h" |
26 #include "base/memory/weak_ptr.h" | 26 #include "base/memory/weak_ptr.h" |
27 #include "base/message_loop/message_loop.h" | 27 #include "base/message_loop/message_loop.h" |
28 #include "base/path_service.h" | 28 #include "base/path_service.h" |
29 #include "base/power_monitor/power_monitor.h" | |
30 #include "base/power_monitor/power_monitor_source.h" | |
29 #include "base/run_loop.h" | 31 #include "base/run_loop.h" |
30 #include "base/single_thread_task_runner.h" | 32 #include "base/single_thread_task_runner.h" |
31 #include "base/strings/string_number_conversions.h" | 33 #include "base/strings/string_number_conversions.h" |
32 #include "base/strings/string_piece.h" | 34 #include "base/strings/string_piece.h" |
33 #include "base/strings/string_split.h" | 35 #include "base/strings/string_split.h" |
34 #include "base/strings/string_util.h" | 36 #include "base/strings/string_util.h" |
35 #include "base/strings/stringprintf.h" | 37 #include "base/strings/stringprintf.h" |
36 #include "base/strings/utf_string_conversions.h" | 38 #include "base/strings/utf_string_conversions.h" |
37 #include "base/test/histogram_tester.h" | 39 #include "base/test/histogram_tester.h" |
38 #include "base/thread_task_runner_handle.h" | 40 #include "base/thread_task_runner_handle.h" |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
251 ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); | 253 ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
252 | 254 |
253 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); | 255 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
254 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); | 256 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
255 EXPECT_TRUE(load_timing_info.send_start.is_null()); | 257 EXPECT_TRUE(load_timing_info.send_start.is_null()); |
256 EXPECT_TRUE(load_timing_info.send_end.is_null()); | 258 EXPECT_TRUE(load_timing_info.send_end.is_null()); |
257 EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); | 259 EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); |
258 } | 260 } |
259 #endif | 261 #endif |
260 | 262 |
263 // Test power monitor source that can simulate entering suspend mode. Can't use | |
264 // the one in base/ because it insists on bringing its own MessageLoop. | |
265 class TestPowerMonitorSource : public base::PowerMonitorSource { | |
266 public: | |
267 TestPowerMonitorSource() {} | |
268 ~TestPowerMonitorSource() override {} | |
269 | |
270 void Suspend() { ProcessPowerEvent(SUSPEND_EVENT); } | |
271 | |
272 bool IsOnBatteryPowerImpl() override { return false; } | |
273 | |
274 private: | |
275 DISALLOW_COPY_AND_ASSIGN(TestPowerMonitorSource); | |
276 }; | |
277 | |
261 // Do a case-insensitive search through |haystack| for |needle|. | 278 // Do a case-insensitive search through |haystack| for |needle|. |
262 bool ContainsString(const std::string& haystack, const char* needle) { | 279 bool ContainsString(const std::string& haystack, const char* needle) { |
263 std::string::const_iterator it = std::search( | 280 std::string::const_iterator it = std::search( |
264 haystack.begin(), haystack.end(), needle, needle + strlen(needle), | 281 haystack.begin(), haystack.end(), needle, needle + strlen(needle), |
265 base::CaseInsensitiveCompareASCII<char>()); | 282 base::CaseInsensitiveCompareASCII<char>()); |
266 return it != haystack.end(); | 283 return it != haystack.end(); |
267 } | 284 } |
268 | 285 |
269 scoped_ptr<UploadDataStream> CreateSimpleUploadData(const char* data) { | 286 scoped_ptr<UploadDataStream> CreateSimpleUploadData(const char* data) { |
270 scoped_ptr<UploadElementReader> reader( | 287 scoped_ptr<UploadElementReader> reader( |
(...skipping 3128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3399 EXPECT_EQ(1, network_delegate.created_requests()); | 3416 EXPECT_EQ(1, network_delegate.created_requests()); |
3400 EXPECT_EQ(0, network_delegate.destroyed_requests()); | 3417 EXPECT_EQ(0, network_delegate.destroyed_requests()); |
3401 EXPECT_EQ("POST", r->method()); | 3418 EXPECT_EQ("POST", r->method()); |
3402 EXPECT_EQ(kData, d.data_received()); | 3419 EXPECT_EQ(kData, d.data_received()); |
3403 } | 3420 } |
3404 EXPECT_EQ(1, network_delegate.destroyed_requests()); | 3421 EXPECT_EQ(1, network_delegate.destroyed_requests()); |
3405 } | 3422 } |
3406 | 3423 |
3407 // Tests that the network delegate can block and redirect a request to a new | 3424 // Tests that the network delegate can block and redirect a request to a new |
3408 // URL during OnHeadersReceived. | 3425 // URL during OnHeadersReceived. |
3426 TEST_F(URLRequestTestHTTP, NetworkDelegateCanceledOnAsyncOnHeadersReceived) { | |
3427 TestPowerMonitorSource* power_monitor_source = new TestPowerMonitorSource(); | |
3428 base::PowerMonitor power_monitor(make_scoped_ptr(power_monitor_source)); | |
3429 ASSERT_TRUE(test_server_.Start()); | |
3430 | |
3431 TestDelegate d; | |
3432 // Request that won't complete any time soon. | |
3433 GURL url(test_server_.GetURL("slow?600")); | |
3434 scoped_ptr<URLRequest> r( | |
3435 default_context_.CreateRequest(url, DEFAULT_PRIORITY, &d)); | |
3436 r->Start(); | |
3437 | |
3438 power_monitor_source->Suspend(); | |
3439 // Wait for the event to propogated to the request, and the request to fail. | |
3440 base::MessageLoop::current()->Run(); | |
davidben
2015/09/25 21:24:55
Nit: base::RunLoop().Run(); ?
mmenke
2015/09/25 21:37:52
Done (Not really sure...we're waiting on URLReques
| |
3441 EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); | |
3442 EXPECT_TRUE(d.request_failed()); | |
3443 EXPECT_EQ(1, default_network_delegate_.completed_requests()); | |
3444 } | |
3445 | |
3446 // Tests that the network delegate can block and redirect a request to a new | |
3447 // URL during OnHeadersReceived. | |
3409 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestOnHeadersReceived) { | 3448 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestOnHeadersReceived) { |
3410 ASSERT_TRUE(test_server_.Start()); | 3449 ASSERT_TRUE(test_server_.Start()); |
3411 | 3450 |
3412 TestDelegate d; | 3451 TestDelegate d; |
3413 BlockingNetworkDelegate network_delegate( | 3452 BlockingNetworkDelegate network_delegate( |
3414 BlockingNetworkDelegate::AUTO_CALLBACK); | 3453 BlockingNetworkDelegate::AUTO_CALLBACK); |
3415 network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); | 3454 network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
3416 GURL redirect_url(test_server_.GetURL("simple.html")); | 3455 GURL redirect_url(test_server_.GetURL("simple.html")); |
3417 network_delegate.set_redirect_on_headers_received_url(redirect_url); | 3456 network_delegate.set_redirect_on_headers_received_url(redirect_url); |
3418 | 3457 |
(...skipping 6054 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
9473 | 9512 |
9474 req->Start(); | 9513 req->Start(); |
9475 req->Cancel(); | 9514 req->Cancel(); |
9476 job->DetachRequest(); | 9515 job->DetachRequest(); |
9477 base::RunLoop().RunUntilIdle(); | 9516 base::RunLoop().RunUntilIdle(); |
9478 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); | 9517 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
9479 EXPECT_EQ(0, d.received_redirect_count()); | 9518 EXPECT_EQ(0, d.received_redirect_count()); |
9480 } | 9519 } |
9481 | 9520 |
9482 } // namespace net | 9521 } // namespace net |
OLD | NEW |