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

Side by Side Diff: content/browser/renderer_host/resource_dispatcher_host_unittest.cc

Issue 10392111: Use ByteStream in downloads system to decouple source and sink. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Sync'd to LKGR. Created 8 years, 6 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 | Annotate | Revision Log
OLDNEW
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 "content/browser/renderer_host/resource_dispatcher_host_impl.h" 5 #include "content/browser/renderer_host/resource_dispatcher_host_impl.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/file_path.h" 10 #include "base/file_path.h"
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 list_head_ = this; 258 list_head_ = this;
259 } 259 }
260 260
261 static URLRequestTestDelayedStartJob* list_head_; 261 static URLRequestTestDelayedStartJob* list_head_;
262 URLRequestTestDelayedStartJob* next_; 262 URLRequestTestDelayedStartJob* next_;
263 }; 263 };
264 264
265 URLRequestTestDelayedStartJob* 265 URLRequestTestDelayedStartJob*
266 URLRequestTestDelayedStartJob::list_head_ = NULL; 266 URLRequestTestDelayedStartJob::list_head_ = NULL;
267 267
268 // This class is a variation on URLRequestTestJob in that it
269 // returns IO_pending errors before every read, not just the first one.
270 class URLRequestTestDelayedCompletionJob : public net::URLRequestTestJob {
271 public:
272 explicit URLRequestTestDelayedCompletionJob(net::URLRequest* request)
273 : net::URLRequestTestJob(request) {}
274 URLRequestTestDelayedCompletionJob(net::URLRequest* request,
275 bool auto_advance)
276 : net::URLRequestTestJob(request, auto_advance) {}
277 URLRequestTestDelayedCompletionJob(net::URLRequest* request,
278 const std::string& response_headers,
279 const std::string& response_data,
280 bool auto_advance)
281 : net::URLRequestTestJob(request, response_headers,
282 response_data, auto_advance) {}
283
284 protected:
285 ~URLRequestTestDelayedCompletionJob() {}
286
287 private:
288 virtual bool NextReadAsync() OVERRIDE { return true; }
289 };
290
291
292
268 // Associated with an URLRequest to determine if the URLRequest gets deleted. 293 // Associated with an URLRequest to determine if the URLRequest gets deleted.
269 class TestUserData : public base::SupportsUserData::Data { 294 class TestUserData : public base::SupportsUserData::Data {
270 public: 295 public:
271 explicit TestUserData(bool* was_deleted) 296 explicit TestUserData(bool* was_deleted)
272 : was_deleted_(was_deleted) { 297 : was_deleted_(was_deleted) {
273 } 298 }
274 299
275 ~TestUserData() { 300 ~TestUserData() {
276 *was_deleted_ = true; 301 *was_deleted_ = true;
277 } 302 }
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
371 // testing::Test 396 // testing::Test
372 virtual void SetUp() { 397 virtual void SetUp() {
373 DCHECK(!test_fixture_); 398 DCHECK(!test_fixture_);
374 test_fixture_ = this; 399 test_fixture_ = this;
375 ChildProcessSecurityPolicyImpl::GetInstance()->Add(0); 400 ChildProcessSecurityPolicyImpl::GetInstance()->Add(0);
376 net::URLRequest::Deprecated::RegisterProtocolFactory( 401 net::URLRequest::Deprecated::RegisterProtocolFactory(
377 "test", 402 "test",
378 &ResourceDispatcherHostTest::Factory); 403 &ResourceDispatcherHostTest::Factory);
379 EnsureTestSchemeIsAllowed(); 404 EnsureTestSchemeIsAllowed();
380 delay_start_ = false; 405 delay_start_ = false;
406 delay_complete_ = false;
381 } 407 }
382 408
383 virtual void TearDown() { 409 virtual void TearDown() {
384 net::URLRequest::Deprecated::RegisterProtocolFactory("test", NULL); 410 net::URLRequest::Deprecated::RegisterProtocolFactory("test", NULL);
385 if (!scheme_.empty()) 411 if (!scheme_.empty())
386 net::URLRequest::Deprecated::RegisterProtocolFactory( 412 net::URLRequest::Deprecated::RegisterProtocolFactory(
387 scheme_, old_factory_); 413 scheme_, old_factory_);
388 414
389 EXPECT_TRUE(URLRequestTestDelayedStartJob::DelayedStartQueueEmpty()); 415 EXPECT_TRUE(URLRequestTestDelayedStartJob::DelayedStartQueueEmpty());
390 URLRequestTestDelayedStartJob::ClearQueue(); 416 URLRequestTestDelayedStartJob::ClearQueue();
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
448 old_factory_ = net::URLRequest::Deprecated::RegisterProtocolFactory( 474 old_factory_ = net::URLRequest::Deprecated::RegisterProtocolFactory(
449 scheme_, &ResourceDispatcherHostTest::Factory); 475 scheme_, &ResourceDispatcherHostTest::Factory);
450 } 476 }
451 477
452 // Our own net::URLRequestJob factory. 478 // Our own net::URLRequestJob factory.
453 static net::URLRequestJob* Factory(net::URLRequest* request, 479 static net::URLRequestJob* Factory(net::URLRequest* request,
454 const std::string& scheme) { 480 const std::string& scheme) {
455 if (test_fixture_->response_headers_.empty()) { 481 if (test_fixture_->response_headers_.empty()) {
456 if (delay_start_) { 482 if (delay_start_) {
457 return new URLRequestTestDelayedStartJob(request); 483 return new URLRequestTestDelayedStartJob(request);
484 } else if (delay_complete_) {
485 return new URLRequestTestDelayedCompletionJob(request);
458 } else { 486 } else {
459 return new net::URLRequestTestJob(request); 487 return new net::URLRequestTestJob(request);
460 } 488 }
461 } else { 489 } else {
462 if (delay_start_) { 490 if (delay_start_) {
463 return new URLRequestTestDelayedStartJob( 491 return new URLRequestTestDelayedStartJob(
464 request, test_fixture_->response_headers_, 492 request, test_fixture_->response_headers_,
465 test_fixture_->response_data_, false); 493 test_fixture_->response_data_, false);
494 } else if (delay_complete_) {
495 return new URLRequestTestDelayedCompletionJob(
496 request, test_fixture_->response_headers_,
497 test_fixture_->response_data_, false);
466 } else { 498 } else {
467 return new net::URLRequestTestJob(request, 499 return new net::URLRequestTestJob(request,
468 test_fixture_->response_headers_, 500 test_fixture_->response_headers_,
469 test_fixture_->response_data_, 501 test_fixture_->response_data_,
470 false); 502 false);
471 } 503 }
472 } 504 }
473 } 505 }
474 506
475 void SetDelayedStartJobGeneration(bool delay_job_start) { 507 void SetDelayedStartJobGeneration(bool delay_job_start) {
476 delay_start_ = delay_job_start; 508 delay_start_ = delay_job_start;
477 } 509 }
478 510
511 void SetDelayedCompleteJobGeneration(bool delay_job_complete) {
512 delay_complete_ = delay_job_complete;
513 }
514
479 MessageLoopForIO message_loop_; 515 MessageLoopForIO message_loop_;
480 BrowserThreadImpl ui_thread_; 516 BrowserThreadImpl ui_thread_;
481 BrowserThreadImpl file_thread_; 517 BrowserThreadImpl file_thread_;
482 BrowserThreadImpl cache_thread_; 518 BrowserThreadImpl cache_thread_;
483 BrowserThreadImpl io_thread_; 519 BrowserThreadImpl io_thread_;
484 scoped_ptr<content::TestBrowserContext> browser_context_; 520 scoped_ptr<content::TestBrowserContext> browser_context_;
485 scoped_refptr<ForwardingFilter> filter_; 521 scoped_refptr<ForwardingFilter> filter_;
486 ResourceDispatcherHostImpl host_; 522 ResourceDispatcherHostImpl host_;
487 ResourceIPCAccumulator accum_; 523 ResourceIPCAccumulator accum_;
488 std::string response_headers_; 524 std::string response_headers_;
489 std::string response_data_; 525 std::string response_data_;
490 std::string scheme_; 526 std::string scheme_;
491 net::URLRequest::ProtocolFactory* old_factory_; 527 net::URLRequest::ProtocolFactory* old_factory_;
492 ResourceType::Type resource_type_; 528 ResourceType::Type resource_type_;
493 static ResourceDispatcherHostTest* test_fixture_; 529 static ResourceDispatcherHostTest* test_fixture_;
494 static bool delay_start_; 530 static bool delay_start_;
531 static bool delay_complete_;
495 }; 532 };
496 // Static. 533 // Static.
497 ResourceDispatcherHostTest* ResourceDispatcherHostTest::test_fixture_ = NULL; 534 ResourceDispatcherHostTest* ResourceDispatcherHostTest::test_fixture_ = NULL;
498 bool ResourceDispatcherHostTest::delay_start_ = false; 535 bool ResourceDispatcherHostTest::delay_start_ = false;
536 bool ResourceDispatcherHostTest::delay_complete_ = false;
499 537
500 void ResourceDispatcherHostTest::MakeTestRequest(int render_view_id, 538 void ResourceDispatcherHostTest::MakeTestRequest(int render_view_id,
501 int request_id, 539 int request_id,
502 const GURL& url) { 540 const GURL& url) {
503 MakeTestRequest(filter_.get(), render_view_id, request_id, url); 541 MakeTestRequest(filter_.get(), render_view_id, request_id, url);
504 } 542 }
505 543
506 void ResourceDispatcherHostTest::MakeTestRequest( 544 void ResourceDispatcherHostTest::MakeTestRequest(
507 ResourceMessageFilter* filter, 545 ResourceMessageFilter* filter,
508 int render_view_id, 546 int render_view_id,
(...skipping 729 matching lines...) Expand 10 before | Expand all | Expand 10 after
1238 1276
1239 int render_view_id = 0; 1277 int render_view_id = 0;
1240 int request_id = 1; 1278 int request_id = 1;
1241 1279
1242 std::string response("HTTP\n" 1280 std::string response("HTTP\n"
1243 "Content-disposition: attachment; filename=foo\n\n"); 1281 "Content-disposition: attachment; filename=foo\n\n");
1244 std::string raw_headers(net::HttpUtil::AssembleRawHeaders(response.data(), 1282 std::string raw_headers(net::HttpUtil::AssembleRawHeaders(response.data(),
1245 response.size())); 1283 response.size()));
1246 std::string response_data("01234567890123456789\x01foobar"); 1284 std::string response_data("01234567890123456789\x01foobar");
1247 1285
1286 // Get past sniffing metrics in the BufferedResourceHandler. Note that
1287 // if we don't get past the sniffing metrics, the result will be that
1288 // the BufferedResourceHandler won't have figured out that it's a download,
1289 // won't have constructed a DownloadResourceHandler, and and the request
1290 // will be successfully canceled below, failing the test.
1291 response_data.resize(1025, ' ');
1292
1248 SetResponse(raw_headers, response_data); 1293 SetResponse(raw_headers, response_data);
1249 SetResourceType(ResourceType::MAIN_FRAME); 1294 SetResourceType(ResourceType::MAIN_FRAME);
1295 SetDelayedCompleteJobGeneration(true);
1250 HandleScheme("http"); 1296 HandleScheme("http");
1251 1297
1252 MakeTestRequest(render_view_id, request_id, GURL("http://example.com/blah")); 1298 MakeTestRequest(render_view_id, request_id, GURL("http://example.com/blah"));
1253 // Return some data so that the request is identified as a download 1299 // Return some data so that the request is identified as a download
1254 // and the proper resource handlers are created. 1300 // and the proper resource handlers are created.
1255 EXPECT_TRUE(net::URLRequestTestJob::ProcessOnePendingMessage()); 1301 EXPECT_TRUE(net::URLRequestTestJob::ProcessOnePendingMessage());
1256 1302
1257 // And now simulate a cancellation coming from the renderer. 1303 // And now simulate a cancellation coming from the renderer.
1258 ResourceHostMsg_CancelRequest msg(filter_->child_id(), request_id); 1304 ResourceHostMsg_CancelRequest msg(filter_->child_id(), request_id);
1259 bool msg_was_ok; 1305 bool msg_was_ok;
(...skipping 12 matching lines...) Expand all
1272 EXPECT_EQ(0, host_.pending_requests()); 1318 EXPECT_EQ(0, host_.pending_requests());
1273 1319
1274 int render_view_id = 0; 1320 int render_view_id = 0;
1275 int request_id = 1; 1321 int request_id = 1;
1276 1322
1277 std::string response("HTTP\n" 1323 std::string response("HTTP\n"
1278 "Content-disposition: attachment; filename=foo\n\n"); 1324 "Content-disposition: attachment; filename=foo\n\n");
1279 std::string raw_headers(net::HttpUtil::AssembleRawHeaders(response.data(), 1325 std::string raw_headers(net::HttpUtil::AssembleRawHeaders(response.data(),
1280 response.size())); 1326 response.size()));
1281 std::string response_data("01234567890123456789\x01foobar"); 1327 std::string response_data("01234567890123456789\x01foobar");
1328 // Get past sniffing metrics.
1329 response_data.resize(1025, ' ');
1282 1330
1283 SetResponse(raw_headers, response_data); 1331 SetResponse(raw_headers, response_data);
1284 SetResourceType(ResourceType::MAIN_FRAME); 1332 SetResourceType(ResourceType::MAIN_FRAME);
1333 SetDelayedCompleteJobGeneration(true);
1285 HandleScheme("http"); 1334 HandleScheme("http");
1286 1335
1287 MakeTestRequest(render_view_id, request_id, GURL("http://example.com/blah")); 1336 MakeTestRequest(render_view_id, request_id, GURL("http://example.com/blah"));
1288 // Return some data so that the request is identified as a download 1337 // Return some data so that the request is identified as a download
1289 // and the proper resource handlers are created. 1338 // and the proper resource handlers are created.
1290 EXPECT_TRUE(net::URLRequestTestJob::ProcessOnePendingMessage()); 1339 EXPECT_TRUE(net::URLRequestTestJob::ProcessOnePendingMessage());
1291 1340
1292 // And now simulate a cancellation coming from the renderer. 1341 // And now simulate a cancellation coming from the renderer.
1293 ResourceHostMsg_CancelRequest msg(filter_->child_id(), request_id); 1342 ResourceHostMsg_CancelRequest msg(filter_->child_id(), request_id);
1294 bool msg_was_ok; 1343 bool msg_was_ok;
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
1377 PickleIterator iter(msgs[0][0]); 1426 PickleIterator iter(msgs[0][0]);
1378 EXPECT_TRUE(IPC::ReadParam(&msgs[0][0], &iter, &request_id)); 1427 EXPECT_TRUE(IPC::ReadParam(&msgs[0][0], &iter, &request_id));
1379 EXPECT_TRUE(IPC::ReadParam(&msgs[0][0], &iter, &status)); 1428 EXPECT_TRUE(IPC::ReadParam(&msgs[0][0], &iter, &status));
1380 1429
1381 EXPECT_EQ(1, request_id); 1430 EXPECT_EQ(1, request_id);
1382 EXPECT_EQ(net::URLRequestStatus::FAILED, status.status()); 1431 EXPECT_EQ(net::URLRequestStatus::FAILED, status.status());
1383 EXPECT_EQ(net::ERR_UNKNOWN_URL_SCHEME, status.error()); 1432 EXPECT_EQ(net::ERR_UNKNOWN_URL_SCHEME, status.error());
1384 } 1433 }
1385 1434
1386 } // namespace content 1435 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698