Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(155)

Side by Side Diff: content/browser/loader/resource_loader_unittest.cc

Issue 2526983002: Refactor ResourceHandler API. (Closed)
Patch Set: Response to comments Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 "content/browser/loader/resource_loader.h" 5 #include "content/browser/loader/resource_loader.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <deque> 10 #include <deque>
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 DISALLOW_COPY_AND_ASSIGN(MockHTTPSURLRequestJob); 236 DISALLOW_COPY_AND_ASSIGN(MockHTTPSURLRequestJob);
237 }; 237 };
238 238
239 const char kRedirectHeaders[] = 239 const char kRedirectHeaders[] =
240 "HTTP/1.1 302 Found\n" 240 "HTTP/1.1 302 Found\n"
241 "Location: https://example.test\n" 241 "Location: https://example.test\n"
242 "\n"; 242 "\n";
243 243
244 class MockHTTPSJobURLRequestInterceptor : public net::URLRequestInterceptor { 244 class MockHTTPSJobURLRequestInterceptor : public net::URLRequestInterceptor {
245 public: 245 public:
246 MockHTTPSJobURLRequestInterceptor(bool redirect) : redirect_(redirect) {} 246 explicit MockHTTPSJobURLRequestInterceptor(bool redirect)
247 : redirect_(redirect) {}
247 ~MockHTTPSJobURLRequestInterceptor() override {} 248 ~MockHTTPSJobURLRequestInterceptor() override {}
248 249
249 // net::URLRequestInterceptor: 250 // net::URLRequestInterceptor:
250 net::URLRequestJob* MaybeInterceptRequest( 251 net::URLRequestJob* MaybeInterceptRequest(
251 net::URLRequest* request, 252 net::URLRequest* request,
252 net::NetworkDelegate* network_delegate) const override { 253 net::NetworkDelegate* network_delegate) const override {
253 std::string headers = 254 std::string headers =
254 redirect_ ? std::string(kRedirectHeaders, arraysize(kRedirectHeaders)) 255 redirect_ ? std::string(kRedirectHeaders, arraysize(kRedirectHeaders))
255 : net::URLRequestTestJob::test_headers(); 256 : net::URLRequestTestJob::test_headers();
256 return new MockHTTPSURLRequestJob(request, network_delegate, headers, 257 return new MockHTTPSURLRequestJob(request, network_delegate, headers,
257 "dummy response", true); 258 "dummy response", true);
258 } 259 }
259 260
260 private: 261 private:
261 bool redirect_; 262 bool redirect_;
262 }; 263 };
263 264
264 // Arbitrary read buffer size.
265 // Test browser client that captures calls to SelectClientCertificates and 265 // Test browser client that captures calls to SelectClientCertificates and
266 // records the arguments of the most recent call for later inspection. 266 // records the arguments of the most recent call for later inspection.
267 class SelectCertificateBrowserClient : public TestContentBrowserClient { 267 class SelectCertificateBrowserClient : public TestContentBrowserClient {
268 public: 268 public:
269 SelectCertificateBrowserClient() : call_count_(0) {} 269 SelectCertificateBrowserClient() : call_count_(0) {}
270 270
271 // Waits until the first call to SelectClientCertificate. 271 // Waits until the first call to SelectClientCertificate.
272 void WaitForSelectCertificate() { 272 void WaitForSelectCertificate() {
273 select_certificate_run_loop_.Run(); 273 select_certificate_run_loop_.Run();
274 // Process any pending messages - just so tests can check if 274 // Process any pending messages - just so tests can check if
(...skipping 1090 matching lines...) Expand 10 before | Expand all | Expand 10 after
1365 net::URLRequestFailedJob::START, net::ERR_IO_PENDING)); 1365 net::URLRequestFailedJob::START, net::ERR_IO_PENDING));
1366 1366
1367 loader_->StartRequest(); 1367 loader_->StartRequest();
1368 base::RunLoop().RunUntilIdle(); 1368 base::RunLoop().RunUntilIdle();
1369 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called()); 1369 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called());
1370 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called()); 1370 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called());
1371 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_started_called()); 1371 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_started_called());
1372 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called()); 1372 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called());
1373 EXPECT_EQ(1, handle_external_protocol_); 1373 EXPECT_EQ(1, handle_external_protocol_);
1374 1374
1375 raw_ptr_resource_handler_->CancelWithError(net::ERR_FAILED); 1375 // Can't cancel through the ResourceHandler, since that depends on
1376 // ResourceDispatachHost, which these tests don't use.
1377 loader_->CancelRequest(false);
1376 raw_ptr_resource_handler_->WaitUntilResponseComplete(); 1378 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1377 1379
1378 EXPECT_EQ(0, did_received_redirect_); 1380 EXPECT_EQ(0, did_received_redirect_);
1379 EXPECT_EQ(0, did_receive_response_); 1381 EXPECT_EQ(0, did_receive_response_);
1380 EXPECT_EQ(1, did_finish_loading_); 1382 EXPECT_EQ(1, did_finish_loading_);
1381 EXPECT_EQ(1, handle_external_protocol_); 1383 EXPECT_EQ(1, handle_external_protocol_);
1382 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called()); 1384 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called());
1383 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called()); 1385 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called());
1384 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_started_called()); 1386 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_started_called());
1385 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called()); 1387 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
1386 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error()); 1388 EXPECT_EQ(net::ERR_ABORTED,
1389 raw_ptr_resource_handler_->final_status().error());
1387 EXPECT_EQ("", raw_ptr_resource_handler_->body()); 1390 EXPECT_EQ("", raw_ptr_resource_handler_->body());
1388 } 1391 }
1389 1392
1390 TEST_F(ResourceLoaderTest, OutOfBandCancelDuringRead) { 1393 TEST_F(ResourceLoaderTest, OutOfBandCancelDuringRead) {
1391 SetUpResourceLoaderForUrl( 1394 SetUpResourceLoaderForUrl(
1392 net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase( 1395 net::URLRequestFailedJob::GetMockHttpUrlWithFailurePhase(
1393 net::URLRequestFailedJob::READ_SYNC, net::ERR_IO_PENDING)); 1396 net::URLRequestFailedJob::READ_SYNC, net::ERR_IO_PENDING));
1394 1397
1395 loader_->StartRequest(); 1398 loader_->StartRequest();
1396 base::RunLoop().RunUntilIdle(); 1399 base::RunLoop().RunUntilIdle();
1397 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called()); 1400 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called());
1398 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called()); 1401 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called());
1399 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_started_called()); 1402 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_started_called());
1400 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called()); 1403 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called());
1401 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called()); 1404 EXPECT_EQ(0, raw_ptr_resource_handler_->on_response_completed_called());
1402 EXPECT_EQ(1, handle_external_protocol_); 1405 EXPECT_EQ(1, handle_external_protocol_);
1403 1406
1404 raw_ptr_resource_handler_->CancelWithError(net::ERR_FAILED); 1407 // Can't cancel through the ResourceHandler, since that depends on
1408 // ResourceDispatachHost, which these tests don't use.
1409 loader_->CancelRequest(false);
1405 raw_ptr_resource_handler_->WaitUntilResponseComplete(); 1410 raw_ptr_resource_handler_->WaitUntilResponseComplete();
1406 EXPECT_EQ(0, did_received_redirect_); 1411 EXPECT_EQ(0, did_received_redirect_);
1407 EXPECT_EQ(1, did_receive_response_); 1412 EXPECT_EQ(1, did_receive_response_);
1408 EXPECT_EQ(1, did_finish_loading_); 1413 EXPECT_EQ(1, did_finish_loading_);
1409 EXPECT_EQ(1, handle_external_protocol_); 1414 EXPECT_EQ(1, handle_external_protocol_);
1410 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called()); 1415 EXPECT_EQ(1, raw_ptr_resource_handler_->on_will_start_called());
1411 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called()); 1416 EXPECT_EQ(0, raw_ptr_resource_handler_->on_request_redirected_called());
1412 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_started_called()); 1417 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_started_called());
1413 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called()); 1418 EXPECT_EQ(0, raw_ptr_resource_handler_->on_read_completed_called());
1414 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called()); 1419 EXPECT_EQ(1, raw_ptr_resource_handler_->on_response_completed_called());
1415 EXPECT_EQ(net::ERR_FAILED, raw_ptr_resource_handler_->final_status().error()); 1420 EXPECT_EQ(net::ERR_ABORTED,
1421 raw_ptr_resource_handler_->final_status().error());
1416 EXPECT_EQ("", raw_ptr_resource_handler_->body()); 1422 EXPECT_EQ("", raw_ptr_resource_handler_->body());
1417 } 1423 }
1418 1424
1419 TEST_F(ResourceLoaderTest, ResumeCanceledRequest) { 1425 TEST_F(ResourceLoaderTest, ResumeCanceledRequest) {
1420 raw_ptr_resource_handler_->set_defer_on_will_start(true); 1426 raw_ptr_resource_handler_->set_defer_on_will_start(true);
1421 1427
1422 loader_->StartRequest(); 1428 loader_->StartRequest();
1429 raw_ptr_resource_handler_->WaitUntilDeferred();
1423 loader_->CancelRequest(true); 1430 loader_->CancelRequest(true);
1424 static_cast<ResourceController*>(loader_.get())->Resume(); 1431 raw_ptr_resource_handler_->Resume();
1425 } 1432 }
1426 1433
1427 class EffectiveConnectionTypeResourceLoaderTest : public ResourceLoaderTest { 1434 class EffectiveConnectionTypeResourceLoaderTest : public ResourceLoaderTest {
1428 public: 1435 public:
1429 void VerifyEffectiveConnectionType( 1436 void VerifyEffectiveConnectionType(
1430 ResourceType resource_type, 1437 ResourceType resource_type,
1431 bool belongs_to_main_frame, 1438 bool belongs_to_main_frame,
1432 net::EffectiveConnectionType set_type, 1439 net::EffectiveConnectionType set_type,
1433 net::EffectiveConnectionType expected_type) { 1440 net::EffectiveConnectionType expected_type) {
1434 network_quality_estimator()->set_effective_connection_type(set_type); 1441 network_quality_estimator()->set_effective_connection_type(set_type);
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
1476 1483
1477 // Tests that the effective connection type is not set on non-main frame 1484 // Tests that the effective connection type is not set on non-main frame
1478 // requests. 1485 // requests.
1479 TEST_F(EffectiveConnectionTypeResourceLoaderTest, DoesNotBelongToMainFrame) { 1486 TEST_F(EffectiveConnectionTypeResourceLoaderTest, DoesNotBelongToMainFrame) {
1480 VerifyEffectiveConnectionType(RESOURCE_TYPE_OBJECT, false, 1487 VerifyEffectiveConnectionType(RESOURCE_TYPE_OBJECT, false,
1481 net::EFFECTIVE_CONNECTION_TYPE_3G, 1488 net::EFFECTIVE_CONNECTION_TYPE_3G,
1482 net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN); 1489 net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN);
1483 } 1490 }
1484 1491
1485 } // namespace content 1492 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/loader/resource_loader.cc ('k') | content/browser/loader/stream_resource_handler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698