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

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

Issue 2476163003: Refactor ResourceHandler API. (Closed)
Patch Set: Minor cleanups, one real fix Created 4 years, 1 month 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/mime_sniffing_resource_handler.h" 5 #include "content/browser/loader/mime_sniffing_resource_handler.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <memory> 9 #include <memory>
10 10
(...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after
366 TestResourceHandler* test_handler = scoped_test_handler.get(); 366 TestResourceHandler* test_handler = scoped_test_handler.get();
367 std::unique_ptr<MimeSniffingResourceHandler> mime_sniffing_handler( 367 std::unique_ptr<MimeSniffingResourceHandler> mime_sniffing_handler(
368 new MimeSniffingResourceHandler(std::move(scoped_test_handler), &host, 368 new MimeSniffingResourceHandler(std::move(scoped_test_handler), &host,
369 &plugin_service, 369 &plugin_service,
370 intercepting_handler.get(), request.get(), 370 intercepting_handler.get(), request.get(),
371 REQUEST_CONTEXT_TYPE_UNSPECIFIED)); 371 REQUEST_CONTEXT_TYPE_UNSPECIFIED));
372 372
373 TestResourceController resource_controller; 373 TestResourceController resource_controller;
374 mime_sniffing_handler->SetController(&resource_controller); 374 mime_sniffing_handler->SetController(&resource_controller);
375 375
376 bool defer = false; 376 bool defer_or_cancel = false;
377 mime_sniffing_handler->OnWillStart(GURL(), &defer); 377 mime_sniffing_handler->OnWillStart(GURL(), &defer_or_cancel);
378 EXPECT_FALSE(defer_or_cancel);
379 EXPECT_EQ(0, resource_controller.cancel_call_count());
378 380
379 // The response should be sniffed. 381 // The response should be sniffed.
380 scoped_refptr<ResourceResponse> response(new ResourceResponse); 382 scoped_refptr<ResourceResponse> response(new ResourceResponse);
381 response->head.mime_type.assign("text/plain"); 383 response->head.mime_type.assign("text/plain");
382 384
383 // Simulate the response starting. The MimeSniffingHandler should start 385 // Simulate the response starting. The MimeSniffingHandler should start
384 // buffering, so the return value should always be true. 386 // buffering, so should not defer.
385 EXPECT_TRUE(mime_sniffing_handler->OnResponseStarted(response.get(), &defer)); 387 mime_sniffing_handler->OnResponseStarted(response.get(), &defer_or_cancel);
386 EXPECT_EQ(0, resource_controller.cancel_call_count()); 388 EXPECT_EQ(0, resource_controller.cancel_call_count());
387 EXPECT_EQ(0, resource_controller.resume_call_count()); 389 EXPECT_EQ(0, resource_controller.resume_call_count());
388 EXPECT_FALSE(defer); 390 EXPECT_FALSE(defer_or_cancel);
389 391
390 // Read some data to sniff the mime type. This will ask the next 392 // Read some data to sniff the mime type. This will ask the next
391 // ResourceHandler for a buffer. 393 // ResourceHandler for a buffer.
392 scoped_refptr<net::IOBuffer> read_buffer; 394 scoped_refptr<net::IOBuffer> read_buffer;
393 int buf_size = 0; 395 int buf_size = 0;
394 EXPECT_EQ(will_read, 396 EXPECT_EQ(will_read,
395 mime_sniffing_handler->OnWillRead(&read_buffer, &buf_size, -1)); 397 mime_sniffing_handler->OnWillRead(&read_buffer, &buf_size, -1));
396 EXPECT_EQ(0, resource_controller.cancel_call_count()); 398 EXPECT_EQ(0, resource_controller.cancel_call_count());
397 399
398 if (!will_read) { 400 if (!will_read) {
399 EXPECT_EQ(1, test_handler->on_will_start_called()); 401 EXPECT_EQ(1, test_handler->on_will_start_called());
400 EXPECT_EQ(0, test_handler->on_request_redirected_called()); 402 EXPECT_EQ(0, test_handler->on_request_redirected_called());
401 EXPECT_EQ(0, test_handler->on_response_started_called()); 403 EXPECT_EQ(0, test_handler->on_response_started_called());
402 EXPECT_EQ(1, test_handler->on_will_read_called()); 404 EXPECT_EQ(1, test_handler->on_will_read_called());
403 EXPECT_EQ(0, test_handler->on_read_completed_called()); 405 EXPECT_EQ(0, test_handler->on_read_completed_called());
404 406
405 // Process all messages to ensure proper test teardown. 407 // Process all messages to ensure proper test teardown.
406 content::RunAllPendingInMessageLoop(); 408 content::RunAllPendingInMessageLoop();
407 return; 409 return;
408 } 410 }
409 411
410 // Simulate an HTML page. The mime sniffer will identify the MimeType and 412 // Simulate an HTML page. The mime sniffer will identify the MimeType and
411 // proceed with replay. 413 // proceed with replay.
412 char data[] = "!DOCTYPE html\n<head>\n<title>Foo</title>\n</head>"; 414 char data[] = "!DOCTYPE html\n<head>\n<title>Foo</title>\n</head>";
413 memcpy(read_buffer->data(), data, sizeof(data)); 415 memcpy(read_buffer->data(), data, sizeof(data));
414 416
415 defer = false; 417 defer_or_cancel = false;
416 bool return_value = 418 mime_sniffing_handler->OnReadCompleted(sizeof(data), &defer_or_cancel);
417 mime_sniffing_handler->OnReadCompleted(sizeof(data), &defer);
418 419
419 // If the next handler cancels the response start, the caller of 420 // If the next handler cancels the response start, the caller of
420 // MimeSniffingHandler::OnReadCompleted should be notified immediately. 421 // MimeSniffingHandler::OnReadCompleted should be notified immediately.
421 if (!response_started) { 422 if (!response_started) {
422 EXPECT_FALSE(defer); 423 EXPECT_TRUE(defer_or_cancel);
423 EXPECT_EQ(response_started, return_value); 424 EXPECT_EQ(1, resource_controller.cancel_call_count());
424 EXPECT_EQ(0, resource_controller.cancel_call_count());
425 425
426 EXPECT_EQ(1, test_handler->on_will_start_called()); 426 EXPECT_EQ(1, test_handler->on_will_start_called());
427 EXPECT_EQ(0, test_handler->on_request_redirected_called()); 427 EXPECT_EQ(0, test_handler->on_request_redirected_called());
428 EXPECT_EQ(1, test_handler->on_response_started_called()); 428 EXPECT_EQ(1, test_handler->on_response_started_called());
429 EXPECT_EQ(1, test_handler->on_will_read_called()); 429 EXPECT_EQ(1, test_handler->on_will_read_called());
430 EXPECT_EQ(0, test_handler->on_read_completed_called()); 430 EXPECT_EQ(0, test_handler->on_read_completed_called());
431 431
432 // Process all messages to ensure proper test teardown. 432 // Process all messages to ensure proper test teardown.
433 content::RunAllPendingInMessageLoop(); 433 content::RunAllPendingInMessageLoop();
434 return; 434 return;
435 } 435 }
436 436
437 // The replay can be deferred both at response started and read replay
438 // stages.
439 EXPECT_EQ(defer, defer_response_started || defer_read_completed);
440 if (defer_response_started) { 437 if (defer_response_started) {
441 EXPECT_TRUE(defer); 438 EXPECT_TRUE(defer_or_cancel);
442 EXPECT_TRUE(return_value); 439 EXPECT_EQ(0, resource_controller.cancel_call_count());
443 EXPECT_EQ(MimeSniffingResourceHandler::STATE_REPLAYING_RESPONSE_RECEIVED, 440 EXPECT_EQ(MimeSniffingResourceHandler::STATE_REPLAYING_RESPONSE_RECEIVED,
444 mime_sniffing_handler->state_); 441 mime_sniffing_handler->state_);
445 mime_sniffing_handler->Resume(); 442 mime_sniffing_handler->Resume();
446 } 443 }
447 444
448 // The body that was sniffed should be transmitted to the next handler. This 445 // The body that was sniffed should be transmitted to the next handler. This
449 // may cancel the request. 446 // may cancel the request.
450 if (!read_completed) { 447 if (!read_completed) {
451 if (defer_response_started) { 448 EXPECT_TRUE(defer_or_cancel);
452 EXPECT_EQ(1, resource_controller.cancel_call_count()); 449 EXPECT_EQ(1, resource_controller.cancel_call_count());
453 } else {
454 EXPECT_EQ(0, resource_controller.cancel_call_count());
455 EXPECT_FALSE(return_value);
456 }
457 // Process all messages to ensure proper test teardown. 450 // Process all messages to ensure proper test teardown.
458 content::RunAllPendingInMessageLoop(); 451 content::RunAllPendingInMessageLoop();
459 return; 452 return;
460 } 453 }
461 454
462 EXPECT_EQ(MimeSniffingResourceHandler::STATE_STREAMING, 455 EXPECT_EQ(MimeSniffingResourceHandler::STATE_STREAMING,
463 mime_sniffing_handler->state_); 456 mime_sniffing_handler->state_);
464 457
465 // The request may be deferred by the next handler once the read is done. 458 // The request may be deferred by the next handler once the read is done.
466 if (defer_read_completed) { 459 if (defer_read_completed) {
467 EXPECT_TRUE(defer); 460 EXPECT_TRUE(defer_or_cancel);
468 mime_sniffing_handler->Resume(); 461 mime_sniffing_handler->Resume();
469 } 462 }
470 463
471 EXPECT_EQ(MimeSniffingResourceHandler::STATE_STREAMING, 464 EXPECT_EQ(MimeSniffingResourceHandler::STATE_STREAMING,
472 mime_sniffing_handler->state_); 465 mime_sniffing_handler->state_);
473 EXPECT_EQ(0, resource_controller.cancel_call_count()); 466 EXPECT_EQ(0, resource_controller.cancel_call_count());
474 467
475 // Even if the next handler defers the request twice, the 468 // Even if the next handler defers the request twice, the
476 // MimeSniffingResourceHandler should only call Resume on its controller 469 // MimeSniffingResourceHandler should only call Resume on its controller
477 // once. 470 // once.
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
533 new MimeSniffingResourceHandler(std::move(scoped_test_handler), &host, 526 new MimeSniffingResourceHandler(std::move(scoped_test_handler), &host,
534 &plugin_service, 527 &plugin_service,
535 intercepting_handler.get(), request.get(), 528 intercepting_handler.get(), request.get(),
536 REQUEST_CONTEXT_TYPE_UNSPECIFIED)); 529 REQUEST_CONTEXT_TYPE_UNSPECIFIED));
537 530
538 TestResourceController resource_controller; 531 TestResourceController resource_controller;
539 mime_sniffing_handler->SetController(&resource_controller); 532 mime_sniffing_handler->SetController(&resource_controller);
540 533
541 int expected_resume_calls = 0; 534 int expected_resume_calls = 0;
542 535
543 bool defer = false; 536 bool defer_or_cancel = false;
544 mime_sniffing_handler->OnWillStart(GURL(), &defer); 537 mime_sniffing_handler->OnWillStart(GURL(), &defer_or_cancel);
538 EXPECT_EQ(0, resource_controller.cancel_call_count());
539 EXPECT_FALSE(defer_or_cancel);
545 540
546 // The response should not be sniffed. 541 // The response should not be sniffed.
547 scoped_refptr<ResourceResponse> response(new ResourceResponse); 542 scoped_refptr<ResourceResponse> response(new ResourceResponse);
548 response->head.mime_type.assign("text/html"); 543 response->head.mime_type.assign("text/html");
549 544
550 // Simulate the response starting. There should be no need for buffering, so 545 // Simulate the response starting. There should be no need for buffering, so
551 // the return value should be that of the next handler. 546 // should transparently pass calls on to the next handler.
552 EXPECT_EQ(response_started, 547 mime_sniffing_handler->OnResponseStarted(response.get(), &defer_or_cancel);
553 mime_sniffing_handler->OnResponseStarted(response.get(), &defer));
554 EXPECT_EQ(0, resource_controller.cancel_call_count());
555 548
556 if (!response_started) { 549 if (!response_started) {
557 EXPECT_FALSE(defer); 550 EXPECT_TRUE(defer_or_cancel);
551 EXPECT_EQ(1, resource_controller.cancel_call_count());
558 552
559 EXPECT_EQ(1, test_handler->on_will_start_called()); 553 EXPECT_EQ(1, test_handler->on_will_start_called());
560 EXPECT_EQ(0, test_handler->on_request_redirected_called()); 554 EXPECT_EQ(0, test_handler->on_request_redirected_called());
561 EXPECT_EQ(1, test_handler->on_response_started_called()); 555 EXPECT_EQ(1, test_handler->on_response_started_called());
562 EXPECT_EQ(0, test_handler->on_will_read_called()); 556 EXPECT_EQ(0, test_handler->on_will_read_called());
563 EXPECT_EQ(0, test_handler->on_read_completed_called()); 557 EXPECT_EQ(0, test_handler->on_read_completed_called());
564 558
565 // Process all messages to ensure proper test teardown. 559 // Process all messages to ensure proper test teardown.
566 content::RunAllPendingInMessageLoop(); 560 content::RunAllPendingInMessageLoop();
567 return; 561 return;
568 } 562 }
569 563
570 EXPECT_EQ(defer_response_started, defer); 564 EXPECT_EQ(0, resource_controller.cancel_call_count());
571 if (defer) { 565 EXPECT_EQ(defer_response_started, defer_or_cancel);
566 if (defer_or_cancel) {
572 EXPECT_EQ(MimeSniffingResourceHandler::STATE_REPLAYING_RESPONSE_RECEIVED, 567 EXPECT_EQ(MimeSniffingResourceHandler::STATE_REPLAYING_RESPONSE_RECEIVED,
573 mime_sniffing_handler->state_); 568 mime_sniffing_handler->state_);
574 expected_resume_calls++; 569 expected_resume_calls++;
575 mime_sniffing_handler->Resume(); 570 mime_sniffing_handler->Resume();
576 } 571 }
577 572
578 EXPECT_EQ(expected_resume_calls, resource_controller.resume_call_count()); 573 EXPECT_EQ(expected_resume_calls, resource_controller.resume_call_count());
579 574
580 // The MimeSniffingResourceHandler should be acting as a pass-through 575 // The MimeSniffingResourceHandler should be acting as a pass-through
581 // ResourceHandler. 576 // ResourceHandler.
582 scoped_refptr<net::IOBuffer> read_buffer; 577 scoped_refptr<net::IOBuffer> read_buffer;
583 int buf_size = 0; 578 int buf_size = 0;
584 EXPECT_EQ(will_read, 579 EXPECT_EQ(will_read,
585 mime_sniffing_handler->OnWillRead(&read_buffer, &buf_size, -1)); 580 mime_sniffing_handler->OnWillRead(&read_buffer, &buf_size, -1));
586 EXPECT_EQ(0, resource_controller.cancel_call_count()); 581 EXPECT_EQ(0, resource_controller.cancel_call_count());
587 582
588 if (!will_read) { 583 if (!will_read) {
589 EXPECT_EQ(1, test_handler->on_will_start_called()); 584 EXPECT_EQ(1, test_handler->on_will_start_called());
590 EXPECT_EQ(0, test_handler->on_request_redirected_called()); 585 EXPECT_EQ(0, test_handler->on_request_redirected_called());
591 EXPECT_EQ(1, test_handler->on_response_started_called()); 586 EXPECT_EQ(1, test_handler->on_response_started_called());
592 EXPECT_EQ(1, test_handler->on_will_read_called()); 587 EXPECT_EQ(1, test_handler->on_will_read_called());
593 EXPECT_EQ(0, test_handler->on_read_completed_called()); 588 EXPECT_EQ(0, test_handler->on_read_completed_called());
594 589
595 // Process all messages to ensure proper test teardown. 590 // Process all messages to ensure proper test teardown.
596 content::RunAllPendingInMessageLoop(); 591 content::RunAllPendingInMessageLoop();
597 return; 592 return;
598 } 593 }
599 594
600 defer = false; 595 defer_or_cancel = false;
601 EXPECT_EQ(read_completed, 596 mime_sniffing_handler->OnReadCompleted(2000, &defer_or_cancel);
602 mime_sniffing_handler->OnReadCompleted(2000, &defer));
603 EXPECT_EQ(0, resource_controller.cancel_call_count());
604 597
605 EXPECT_EQ(1, test_handler->on_will_start_called()); 598 EXPECT_EQ(1, test_handler->on_will_start_called());
606 EXPECT_EQ(0, test_handler->on_request_redirected_called()); 599 EXPECT_EQ(0, test_handler->on_request_redirected_called());
607 EXPECT_EQ(1, test_handler->on_response_started_called()); 600 EXPECT_EQ(1, test_handler->on_response_started_called());
608 EXPECT_EQ(1, test_handler->on_will_read_called()); 601 EXPECT_EQ(1, test_handler->on_will_read_called());
609 EXPECT_EQ(1, test_handler->on_read_completed_called()); 602 EXPECT_EQ(1, test_handler->on_read_completed_called());
610 603
611 if (!read_completed) { 604 if (!read_completed) {
612 EXPECT_FALSE(defer); 605 EXPECT_EQ(1, resource_controller.cancel_call_count());
606 EXPECT_TRUE(defer_or_cancel);
613 607
614 // Process all messages to ensure proper test teardown. 608 // Process all messages to ensure proper test teardown.
615 content::RunAllPendingInMessageLoop(); 609 content::RunAllPendingInMessageLoop();
616 return; 610 return;
617 } 611 }
618 612
619 EXPECT_EQ(defer_read_completed, defer); 613 EXPECT_EQ(0, resource_controller.cancel_call_count());
620 if (defer) { 614 EXPECT_EQ(defer_read_completed, defer_or_cancel);
615 if (defer_or_cancel) {
621 expected_resume_calls++; 616 expected_resume_calls++;
622 mime_sniffing_handler->Resume(); 617 mime_sniffing_handler->Resume();
623 } 618 }
624 EXPECT_EQ(expected_resume_calls, resource_controller.resume_call_count()); 619 EXPECT_EQ(expected_resume_calls, resource_controller.resume_call_count());
625 620
626 // Process all messages to ensure proper test teardown. 621 // Process all messages to ensure proper test teardown.
627 content::RunAllPendingInMessageLoop(); 622 content::RunAllPendingInMessageLoop();
628 } 623 }
629 624
630 // Test that the proper Accept: header is set based on the ResourceType 625 // Test that the proper Accept: header is set based on the ResourceType
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after
974 scoped_test_handler->set_on_response_started_result(false); 969 scoped_test_handler->set_on_response_started_result(false);
975 std::unique_ptr<ResourceHandler> mime_sniffing_handler( 970 std::unique_ptr<ResourceHandler> mime_sniffing_handler(
976 new MimeSniffingResourceHandler(std::move(scoped_test_handler), &host, 971 new MimeSniffingResourceHandler(std::move(scoped_test_handler), &host,
977 &plugin_service, 972 &plugin_service,
978 intercepting_handler.get(), request.get(), 973 intercepting_handler.get(), request.get(),
979 REQUEST_CONTEXT_TYPE_FETCH)); 974 REQUEST_CONTEXT_TYPE_FETCH));
980 975
981 TestResourceController resource_controller; 976 TestResourceController resource_controller;
982 mime_sniffing_handler->SetController(&resource_controller); 977 mime_sniffing_handler->SetController(&resource_controller);
983 978
984 bool defer = false; 979 bool defer_or_cancel = false;
985 mime_sniffing_handler->OnWillStart(GURL(), &defer); 980 mime_sniffing_handler->OnWillStart(GURL(), &defer_or_cancel);
986 ASSERT_FALSE(defer); 981 ASSERT_EQ(0, resource_controller.cancel_call_count());
982 ASSERT_FALSE(defer_or_cancel);
987 983
988 scoped_refptr<ResourceResponse> response(new ResourceResponse); 984 scoped_refptr<ResourceResponse> response(new ResourceResponse);
989 response->head.mime_type = "text/plain"; 985 response->head.mime_type = "text/plain";
990 986
991 // |mime_sniffing_handler->OnResponseStarted| should return false because 987 // |mime_sniffing_handler->OnResponseStarted| should cancel the request
992 // mime sniffing is disabled and the wrapped resource handler returns false 988 // because MIME sniffing is disabled and the wrapped resource handler cancels
993 // on OnResponseStarted. 989 // the request in OnResponseStarted.
994 EXPECT_FALSE( 990 mime_sniffing_handler->OnResponseStarted(response.get(), &defer_or_cancel);
995 mime_sniffing_handler->OnResponseStarted(response.get(), &defer)); 991 EXPECT_EQ(1, resource_controller.cancel_call_count());
992 EXPECT_TRUE(defer_or_cancel);
996 993
997 // Process all messages to ensure proper test teardown. 994 // Process all messages to ensure proper test teardown.
998 content::RunAllPendingInMessageLoop(); 995 content::RunAllPendingInMessageLoop();
999 } 996 }
1000 997
1001 } // namespace content 998 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/loader/mime_sniffing_resource_handler.cc ('k') | content/browser/loader/mojo_async_resource_handler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698