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

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

Powered by Google App Engine
This is Rietveld 408576698