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

Unified Diff: content/browser/loader/mime_sniffing_resource_handler_unittest.cc

Issue 2366753002: Have MimeSniffingResourceHandler call WillStartRequest on new Handlers. (Closed)
Patch Set: Fix Created 4 years, 3 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « content/browser/loader/mime_sniffing_resource_handler.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/browser/loader/mime_sniffing_resource_handler_unittest.cc
diff --git a/content/browser/loader/mime_sniffing_resource_handler_unittest.cc b/content/browser/loader/mime_sniffing_resource_handler_unittest.cc
index ed407eb1d68106b55ac9160c73f8f61f9bc26413..346a16f47e31e521e5e2d45627152161015fd224 100644
--- a/content/browser/loader/mime_sniffing_resource_handler_unittest.cc
+++ b/content/browser/loader/mime_sniffing_resource_handler_unittest.cc
@@ -34,13 +34,19 @@ namespace {
class TestResourceHandler : public ResourceHandler {
public:
- TestResourceHandler(bool response_started_succeeds,
+ // TODO(mmenke): This constructor is a bit crazy. Switch to taking a bitfield
+ // instead, or maybe use setters?
+ TestResourceHandler(bool will_start_succeeds,
+ bool defer_on_will_start,
+ bool response_started_succeeds,
bool defer_on_response_started,
bool will_read_succeeds,
bool read_completed_succeeds,
bool defer_on_read_completed)
: ResourceHandler(nullptr),
buffer_(new net::IOBuffer(2048)),
+ will_start_succeeds_(will_start_succeeds),
+ defer_on_will_start_(defer_on_will_start),
response_started_succeeds_(response_started_succeeds),
defer_on_response_started_(defer_on_response_started),
will_read_succeeds_(will_read_succeeds),
@@ -52,8 +58,6 @@ class TestResourceHandler : public ResourceHandler {
on_will_read_called_(0),
on_read_completed_called_(0) {}
- void SetController(ResourceController* controller) override {}
-
bool OnRequestRedirected(const net::RedirectInfo& redirect_info,
ResourceResponse* response,
bool* defer) override {
@@ -63,6 +67,8 @@ class TestResourceHandler : public ResourceHandler {
}
bool OnResponseStarted(ResourceResponse* response, bool* defer) override {
+ EXPECT_TRUE(on_will_start_called_);
+
on_response_started_called_++;
if (defer_on_response_started_)
*defer = true;
@@ -71,7 +77,9 @@ class TestResourceHandler : public ResourceHandler {
bool OnWillStart(const GURL& url, bool* defer) override {
on_will_start_called_++;
- return false;
+
+ *defer = defer_on_will_start_;
+ return will_start_succeeds_;
}
bool OnWillRead(scoped_refptr<net::IOBuffer>* buf,
@@ -84,6 +92,9 @@ class TestResourceHandler : public ResourceHandler {
}
bool OnReadCompleted(int bytes_read, bool* defer) override {
+ EXPECT_TRUE(on_will_start_called_);
+ EXPECT_TRUE(on_response_started_called_);
+
DCHECK_LT(bytes_read, 2048);
on_read_completed_called_++;
if (defer_on_read_completed_)
@@ -92,10 +103,20 @@ class TestResourceHandler : public ResourceHandler {
}
void OnResponseCompleted(const net::URLRequestStatus& status,
- bool* defer) override {}
+ bool* defer) override {
+ // If the request succeeded, OnWillRead and OnResponseStarted must have been
+ // called.
+ if (status.is_success()) {
+ EXPECT_TRUE(on_will_start_called_);
+ EXPECT_TRUE(on_response_started_called_);
+ }
+ }
void OnDataDownloaded(int bytes_downloaded) override { NOTREACHED(); }
+ // Give text fixture access to ResourceController, for resuming.
+ using ResourceHandler::controller;
+
scoped_refptr<net::IOBuffer> buffer() { return buffer_; }
int on_will_start_called() const { return on_will_start_called_; }
@@ -108,6 +129,9 @@ class TestResourceHandler : public ResourceHandler {
private:
scoped_refptr<net::IOBuffer> buffer_;
+
+ bool will_start_succeeds_;
+ bool defer_on_will_start_;
bool response_started_succeeds_;
bool defer_on_response_started_;
bool will_read_succeeds_;
@@ -142,6 +166,8 @@ class TestResourceDispatcherHost : public ResourceDispatcherHostImpl {
public:
explicit TestResourceDispatcherHost(bool stream_has_handler)
: stream_has_handler_(stream_has_handler),
+ new_handler_will_start_succeeds_(true),
+ new_handler_defer_on_will_start_(false),
intercepted_as_stream_(false),
intercepted_as_stream_count_(0),
new_resource_handler_(nullptr) {}
@@ -166,6 +192,16 @@ class TestResourceDispatcherHost : public ResourceDispatcherHostImpl {
return CreateNewResourceHandler();
}
+ void set_new_handler_will_start_succeeds(
+ bool new_handler_will_start_succeeds) {
+ new_handler_will_start_succeeds_ = new_handler_will_start_succeeds;
+ }
+
+ void set_new_handler_defer_on_will_start(
+ bool new_handler_defer_on_will_start) {
+ new_handler_defer_on_will_start_ = new_handler_defer_on_will_start;
+ }
+
int intercepted_as_stream_count() const {
return intercepted_as_stream_count_;
}
@@ -177,7 +213,12 @@ class TestResourceDispatcherHost : public ResourceDispatcherHostImpl {
private:
std::unique_ptr<ResourceHandler> CreateNewResourceHandler() {
std::unique_ptr<TestResourceHandler> new_resource_handler(
- new TestResourceHandler(false, false, true, true, false));
+ new TestResourceHandler(
+ new_handler_will_start_succeeds_, new_handler_defer_on_will_start_,
+ false /* response_started_succeeds */,
+ false /* defer_on_response_started */,
+ true /* will_read_succeeds */, true /* read_completed_succeeds */,
+ false /* defer_on_read_completed */));
new_resource_handler_ = new_resource_handler.get();
return std::move(new_resource_handler);
}
@@ -185,6 +226,10 @@ class TestResourceDispatcherHost : public ResourceDispatcherHostImpl {
// Whether the URL request should be intercepted as a stream.
bool stream_has_handler_;
+ // Behavior of the OnWillStart method of any created ResourceHandlers.
+ bool new_handler_will_start_succeeds_;
+ bool new_handler_defer_on_will_start_;
+
// Whether the URL request has been intercepted as a stream.
bool intercepted_as_stream_;
@@ -286,7 +331,9 @@ class MimeSniffingResourceHandlerTest : public testing::Test {
bool TestStreamIsIntercepted(bool allow_download,
bool must_download,
- ResourceType request_resource_type);
+ ResourceType request_resource_type,
+ bool new_handler_will_start_succeeds,
+ bool new_handler_defer_on_will_start);
// Tests the operation of the MimeSniffingHandler when it needs to buffer
// data (example case: the response is text/plain).
@@ -340,8 +387,8 @@ MimeSniffingResourceHandlerTest::TestAcceptHeaderSettingWithURLRequest(
std::unique_ptr<ResourceHandler> mime_sniffing_handler(
new MimeSniffingResourceHandler(
- std::unique_ptr<ResourceHandler>(
- new TestResourceHandler(false, false, false, false, false)),
+ std::unique_ptr<ResourceHandler>(new TestResourceHandler(
+ true, false, false, false, false, false, false)),
nullptr, nullptr, nullptr, request,
REQUEST_CONTEXT_TYPE_UNSPECIFIED));
@@ -357,7 +404,9 @@ MimeSniffingResourceHandlerTest::TestAcceptHeaderSettingWithURLRequest(
bool MimeSniffingResourceHandlerTest::TestStreamIsIntercepted(
bool allow_download,
bool must_download,
- ResourceType request_resource_type) {
+ ResourceType request_resource_type,
+ bool new_handler_will_start_succeeds,
+ bool new_handler_defer_on_will_start) {
net::URLRequestContext context;
std::unique_ptr<net::URLRequest> request(context.CreateRequest(
GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr));
@@ -374,18 +423,23 @@ bool MimeSniffingResourceHandlerTest::TestStreamIsIntercepted(
false); // is_using_lofi
TestResourceDispatcherHost host(stream_has_handler_);
+ host.set_new_handler_will_start_succeeds(new_handler_will_start_succeeds);
+ host.set_new_handler_defer_on_will_start(new_handler_defer_on_will_start);
+
TestResourceDispatcherHostDelegate host_delegate(must_download);
host.SetDelegate(&host_delegate);
TestFakePluginService plugin_service(plugin_available_, plugin_stale_);
- std::unique_ptr<InterceptingResourceHandler> intercepting_handler(
- new InterceptingResourceHandler(std::unique_ptr<ResourceHandler>(),
- nullptr));
- std::unique_ptr<ResourceHandler> mime_handler(new MimeSniffingResourceHandler(
- std::unique_ptr<ResourceHandler>(
- new TestResourceHandler(false, false, false, false, false)),
- &host, &plugin_service, intercepting_handler.get(), request.get(),
- REQUEST_CONTEXT_TYPE_UNSPECIFIED));
+ InterceptingResourceHandler* intercepting_handler =
+ new InterceptingResourceHandler(
+ base::MakeUnique<TestResourceHandler>(true, false, false, false, true,
+ false, false),
+ nullptr);
+ std::unique_ptr<MimeSniffingResourceHandler> mime_handler(
+ new MimeSniffingResourceHandler(base::WrapUnique(intercepting_handler),
+ &host, &plugin_service,
+ intercepting_handler, request.get(),
+ REQUEST_CONTEXT_TYPE_UNSPECIFIED));
TestResourceController resource_controller;
mime_handler->SetController(&resource_controller);
@@ -395,12 +449,43 @@ bool MimeSniffingResourceHandlerTest::TestStreamIsIntercepted(
response->head.mime_type = "application/pdf";
bool defer = false;
- mime_handler->OnResponseStarted(response.get(), &defer);
+ EXPECT_TRUE(mime_handler->OnWillStart(request->url(), &defer));
+ EXPECT_FALSE(defer);
+ bool result = mime_handler->OnResponseStarted(response.get(), &defer);
+
+ // Exit early if the new ResourceHandler should have failed in OnWillStart.
+ if (!new_handler_will_start_succeeds) {
+ EXPECT_FALSE(result);
+ EXPECT_TRUE(host.new_resource_handler()->on_will_start_called());
+ EXPECT_FALSE(host.new_resource_handler()->on_response_started_called());
+ return host.intercepted_as_stream();
+ }
+
+ // If the new ResourceHandler's OnWillStart method should have deferred the
+ // request, check state and resume it.
+ if (new_handler_defer_on_will_start) {
+ EXPECT_TRUE(defer);
+ EXPECT_TRUE(result);
+ EXPECT_TRUE(host.new_resource_handler()->on_will_start_called());
+ EXPECT_FALSE(host.new_resource_handler()->on_response_started_called());
+ host.new_resource_handler()->controller()->Resume();
+ }
content::RunAllPendingInMessageLoop();
EXPECT_LT(host.intercepted_as_stream_count(), 2);
+
if (allow_download)
- EXPECT_TRUE(intercepting_handler->new_handler_for_testing());
+ EXPECT_TRUE(host.new_resource_handler());
+
+ // If a new resource handler was created, make sure it was successfully
+ // swapped in, and its OnWillStart and OnResponseStarted methods were called.
+ if (host.new_resource_handler()) {
+ EXPECT_EQ(host.new_resource_handler(),
+ intercepting_handler->next_handler_for_testing());
+ EXPECT_TRUE(host.new_resource_handler()->on_will_start_called());
+ EXPECT_TRUE(host.new_resource_handler()->on_response_started_called());
+ }
+
return host.intercepted_as_stream();
}
@@ -435,8 +520,8 @@ void MimeSniffingResourceHandlerTest::TestHandlerSniffing(
nullptr));
std::unique_ptr<TestResourceHandler> scoped_test_handler =
std::unique_ptr<TestResourceHandler>(new TestResourceHandler(
- response_started, defer_response_started, will_read, read_completed,
- defer_read_completed));
+ true, false, response_started, defer_response_started, will_read,
+ read_completed, defer_read_completed));
TestResourceHandler* test_handler = scoped_test_handler.get();
std::unique_ptr<MimeSniffingResourceHandler> mime_sniffing_handler(
new MimeSniffingResourceHandler(std::move(scoped_test_handler), &host,
@@ -514,7 +599,7 @@ void MimeSniffingResourceHandlerTest::TestHandlerSniffing(
if (defer_response_started) {
EXPECT_TRUE(defer);
EXPECT_TRUE(return_value);
- EXPECT_EQ(MimeSniffingResourceHandler::STATE_REPLAYING_RESPONSE_RECEIVED,
+ EXPECT_EQ(MimeSniffingResourceHandler::STATE_REPLAYING_RECEIVED_RESPONSE,
mime_sniffing_handler->state_);
mime_sniffing_handler->Resume();
}
@@ -597,8 +682,8 @@ void MimeSniffingResourceHandlerTest::TestHandlerNoSniffing(
std::unique_ptr<TestResourceHandler> scoped_test_handler =
std::unique_ptr<TestResourceHandler>(new TestResourceHandler(
- response_started, defer_response_started, will_read, read_completed,
- defer_read_completed));
+ true, false, response_started, defer_response_started, will_read,
+ read_completed, defer_read_completed));
TestResourceHandler* test_handler = scoped_test_handler.get();
std::unique_ptr<MimeSniffingResourceHandler> mime_sniffing_handler(
new MimeSniffingResourceHandler(std::move(scoped_test_handler), &host,
@@ -640,7 +725,7 @@ void MimeSniffingResourceHandlerTest::TestHandlerNoSniffing(
EXPECT_EQ(defer_response_started, defer);
if (defer) {
- EXPECT_EQ(MimeSniffingResourceHandler::STATE_REPLAYING_RESPONSE_RECEIVED,
+ EXPECT_EQ(MimeSniffingResourceHandler::STATE_REPLAYING_RECEIVED_RESPONSE,
mime_sniffing_handler->state_);
expected_resume_calls++;
mime_sniffing_handler->Resume();
@@ -744,6 +829,8 @@ TEST_F(MimeSniffingResourceHandlerTest, StreamHandling) {
bool allow_download;
bool must_download;
ResourceType resource_type;
+ bool new_handler_will_start_succeeds;
+ bool new_handler_defer_on_will_start;
// Ensure the stream is handled by MaybeInterceptAsStream in the
// ResourceDispatcherHost.
@@ -755,45 +842,86 @@ TEST_F(MimeSniffingResourceHandlerTest, StreamHandling) {
allow_download = false;
must_download = false;
resource_type = RESOURCE_TYPE_MAIN_FRAME;
- EXPECT_FALSE(
- TestStreamIsIntercepted(allow_download, must_download, resource_type));
+ new_handler_will_start_succeeds = true;
+ new_handler_defer_on_will_start = false;
+ EXPECT_FALSE(TestStreamIsIntercepted(
+ false /* allow_download */, false /* must_download */,
+ RESOURCE_TYPE_MAIN_FRAME, true /* new_handler_will_start_succeeds */,
+ false /* new_handler_defer_on_will_start */));
// Main frame request with download allowed. Stream should be intercepted.
allow_download = true;
must_download = false;
resource_type = RESOURCE_TYPE_MAIN_FRAME;
- EXPECT_TRUE(
- TestStreamIsIntercepted(allow_download, must_download, resource_type));
+ new_handler_will_start_succeeds = true;
+ new_handler_defer_on_will_start = false;
+ EXPECT_TRUE(TestStreamIsIntercepted(
+ allow_download, must_download, resource_type,
+ new_handler_will_start_succeeds, new_handler_defer_on_will_start));
+
+ // Main frame request with download allowed, but OnWillStart for the new
+ // handler succeeds asynchronously. Stream should be intercepted.
+ allow_download = true;
+ must_download = false;
+ resource_type = RESOURCE_TYPE_MAIN_FRAME;
+ new_handler_will_start_succeeds = true;
+ new_handler_defer_on_will_start = true;
+ EXPECT_TRUE(TestStreamIsIntercepted(
+ allow_download, must_download, resource_type,
+ new_handler_will_start_succeeds, new_handler_defer_on_will_start));
+
+ // Main frame request with download allowed, but OnWillStart of new handler
+ // fails.
+ allow_download = true;
+ must_download = false;
+ resource_type = RESOURCE_TYPE_MAIN_FRAME;
+ new_handler_will_start_succeeds = false;
+ new_handler_defer_on_will_start = false;
+ EXPECT_TRUE(TestStreamIsIntercepted(
+ allow_download, must_download, resource_type,
+ new_handler_will_start_succeeds, new_handler_defer_on_will_start));
// Main frame request with download forced. Stream shouldn't be intercepted.
allow_download = true;
must_download = true;
resource_type = RESOURCE_TYPE_MAIN_FRAME;
- EXPECT_FALSE(
- TestStreamIsIntercepted(allow_download, must_download, resource_type));
+ new_handler_will_start_succeeds = true;
+ new_handler_defer_on_will_start = false;
+ EXPECT_FALSE(TestStreamIsIntercepted(
+ allow_download, must_download, resource_type,
+ new_handler_will_start_succeeds, new_handler_defer_on_will_start));
// Sub-resource request with download not allowed. Stream shouldn't be
// intercepted.
allow_download = false;
must_download = false;
resource_type = RESOURCE_TYPE_SUB_RESOURCE;
- EXPECT_FALSE(
- TestStreamIsIntercepted(allow_download, must_download, resource_type));
+ new_handler_will_start_succeeds = true;
+ new_handler_defer_on_will_start = false;
+ EXPECT_FALSE(TestStreamIsIntercepted(
+ allow_download, must_download, resource_type,
+ new_handler_will_start_succeeds, new_handler_defer_on_will_start));
// Plugin resource request with download not allowed. Stream shouldn't be
// intercepted.
allow_download = false;
must_download = false;
resource_type = RESOURCE_TYPE_PLUGIN_RESOURCE;
- EXPECT_FALSE(
- TestStreamIsIntercepted(allow_download, must_download, resource_type));
+ new_handler_will_start_succeeds = true;
+ new_handler_defer_on_will_start = false;
+ EXPECT_FALSE(TestStreamIsIntercepted(
+ allow_download, must_download, resource_type,
+ new_handler_will_start_succeeds, new_handler_defer_on_will_start));
// Object request with download not allowed. Stream should be intercepted.
allow_download = false;
must_download = false;
resource_type = RESOURCE_TYPE_OBJECT;
- EXPECT_TRUE(
- TestStreamIsIntercepted(allow_download, must_download, resource_type));
+ new_handler_will_start_succeeds = true;
+ new_handler_defer_on_will_start = false;
+ EXPECT_TRUE(TestStreamIsIntercepted(
+ allow_download, must_download, resource_type,
+ new_handler_will_start_succeeds, new_handler_defer_on_will_start));
// Test the cases where the stream isn't handled by MaybeInterceptAsStream
// in the ResourceDispatcherHost.
@@ -801,8 +929,11 @@ TEST_F(MimeSniffingResourceHandlerTest, StreamHandling) {
allow_download = false;
must_download = false;
resource_type = RESOURCE_TYPE_OBJECT;
- EXPECT_FALSE(
- TestStreamIsIntercepted(allow_download, must_download, resource_type));
+ new_handler_will_start_succeeds = true;
+ new_handler_defer_on_will_start = false;
+ EXPECT_FALSE(TestStreamIsIntercepted(
+ allow_download, must_download, resource_type,
+ new_handler_will_start_succeeds, new_handler_defer_on_will_start));
// Test the cases where the stream handled by MaybeInterceptAsStream
// with plugin not available. This is the case when intercepting streams for
@@ -812,8 +943,11 @@ TEST_F(MimeSniffingResourceHandlerTest, StreamHandling) {
allow_download = false;
must_download = false;
resource_type = RESOURCE_TYPE_OBJECT;
- EXPECT_TRUE(
- TestStreamIsIntercepted(allow_download, must_download, resource_type));
+ new_handler_will_start_succeeds = true;
+ new_handler_defer_on_will_start = false;
+ EXPECT_TRUE(TestStreamIsIntercepted(
+ allow_download, must_download, resource_type,
+ new_handler_will_start_succeeds, new_handler_defer_on_will_start));
// Test the cases where the stream handled by MaybeInterceptAsStream
// with plugin not available. This is the case when intercepting streams for
@@ -822,8 +956,11 @@ TEST_F(MimeSniffingResourceHandlerTest, StreamHandling) {
allow_download = false;
must_download = false;
resource_type = RESOURCE_TYPE_OBJECT;
- EXPECT_TRUE(
- TestStreamIsIntercepted(allow_download, must_download, resource_type));
+ new_handler_will_start_succeeds = true;
+ new_handler_defer_on_will_start = false;
+ EXPECT_TRUE(TestStreamIsIntercepted(
+ allow_download, must_download, resource_type,
+ new_handler_will_start_succeeds, new_handler_defer_on_will_start));
}
#endif
@@ -990,7 +1127,7 @@ TEST_F(MimeSniffingResourceHandlerTest, 304Handling) {
nullptr));
std::unique_ptr<ResourceHandler> mime_handler(new MimeSniffingResourceHandler(
std::unique_ptr<ResourceHandler>(
- new TestResourceHandler(true, false, true, true, false)),
+ new TestResourceHandler(true, false, true, false, true, true, false)),
&host, &plugin_service,
static_cast<InterceptingResourceHandler*>(intercepting_handler.get()),
request.get(), REQUEST_CONTEXT_TYPE_UNSPECIFIED));
@@ -1007,7 +1144,10 @@ TEST_F(MimeSniffingResourceHandlerTest, 304Handling) {
// The response is received. No new ResourceHandler should be created to
// handle the download.
bool defer = false;
- mime_handler->OnResponseStarted(response.get(), &defer);
+ ASSERT_TRUE(mime_handler->OnWillStart(request->url(), &defer));
+ ASSERT_FALSE(defer);
+
+ ASSERT_TRUE(mime_handler->OnResponseStarted(response.get(), &defer));
EXPECT_FALSE(defer);
EXPECT_FALSE(host.new_resource_handler());
@@ -1037,10 +1177,12 @@ TEST_F(MimeSniffingResourceHandlerTest, FetchShouldDisableMimeSniffing) {
new InterceptingResourceHandler(nullptr, nullptr));
std::unique_ptr<TestResourceHandler> scoped_test_handler(
- new TestResourceHandler(false, // response_started
- false, // defer_response_started
- true, // will_read,
- true, // read_completed,
+ new TestResourceHandler(true, // will_start_succeeds
+ false, // defer_on_will_start
+ false, // response_started_succeeds
+ false, // defer_on_response_started
+ true, // will_read_succeeds
+ true, // read_completed_succeeds
false)); // defer_read_completed
std::unique_ptr<ResourceHandler> mime_sniffing_handler(
new MimeSniffingResourceHandler(std::move(scoped_test_handler), &host,
« no previous file with comments | « content/browser/loader/mime_sniffing_resource_handler.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698