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

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

Issue 2005273002: Move MimeTypeResourceHandler before ThrottlingResourceHandler (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments 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 unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "content/browser/loader/mime_sniffing_resource_handler.h"
6
7 #include <stdint.h>
8
9 #include <memory>
10
11 #include "base/files/file_path.h"
12 #include "base/location.h"
13 #include "base/logging.h"
14 #include "base/macros.h"
15 #include "base/single_thread_task_runner.h"
16 #include "base/threading/thread_task_runner_handle.h"
17 #include "content/browser/loader/intercepting_resource_handler.h"
18 #include "content/browser/loader/resource_dispatcher_host_impl.h"
19 #include "content/public/browser/resource_controller.h"
20 #include "content/public/browser/resource_dispatcher_host_delegate.h"
21 #include "content/public/browser/resource_request_info.h"
22 #include "content/public/common/resource_response.h"
23 #include "content/public/common/webplugininfo.h"
24 #include "content/public/test/test_browser_thread_bundle.h"
25 #include "content/public/test/test_utils.h"
26 #include "content/test/fake_plugin_service.h"
27 #include "net/url_request/url_request_context.h"
28 #include "testing/gtest/include/gtest/gtest.h"
29 #include "url/gurl.h"
30
31 namespace content {
32
33 namespace {
34
35 class TestResourceHandler : public ResourceHandler {
36 public:
37 TestResourceHandler(bool response_started_succeeds,
38 bool defer_on_response_started,
39 bool will_read_succeeds,
40 bool read_completed_succeeds,
41 bool defer_on_read_completed)
42 : ResourceHandler(nullptr),
43 buffer_(new net::IOBuffer(2048)),
44 response_started_succeeds_(response_started_succeeds),
45 defer_on_response_started_(defer_on_response_started),
46 will_read_succeeds_(will_read_succeeds),
47 read_completed_succeeds_(read_completed_succeeds),
48 defer_on_read_completed_(defer_on_read_completed),
49 on_will_start_called_(0),
50 on_request_redirected_called_(0),
51 on_response_started_called_(0),
52 on_will_read_called_(0),
53 on_read_completed_called_(0) {}
54
55 void SetController(ResourceController* controller) override {}
56
57 bool OnRequestRedirected(const net::RedirectInfo& redirect_info,
58 ResourceResponse* response,
59 bool* defer) override {
60 on_request_redirected_called_++;
61 NOTREACHED();
62 return false;
63 }
64
65 bool OnResponseStarted(ResourceResponse* response, bool* defer) override {
66 on_response_started_called_++;
67 if (defer_on_response_started_)
68 *defer = true;
69 return response_started_succeeds_;
70 }
71
72 bool OnWillStart(const GURL& url, bool* defer) override {
73 on_will_start_called_++;
74 return false;
75 }
76
77 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf,
78 int* buf_size,
79 int min_size) override {
80 on_will_read_called_++;
81 *buf = buffer_;
82 *buf_size = 2048;
83 return will_read_succeeds_;
84 }
85
86 bool OnReadCompleted(int bytes_read, bool* defer) override {
87 DCHECK_LT(bytes_read, 2048);
88 on_read_completed_called_++;
89 if (defer_on_read_completed_)
90 *defer = true;
91 return read_completed_succeeds_;
92 }
93
94 void OnResponseCompleted(const net::URLRequestStatus& status,
95 const std::string& security_info,
96 bool* defer) override {}
97
98 void OnDataDownloaded(int bytes_downloaded) override { NOTREACHED(); }
99
100 scoped_refptr<net::IOBuffer> buffer() { return buffer_; }
101
102 int on_will_start_called() const { return on_will_start_called_; }
103 int on_request_redirected_called() const {
104 return on_request_redirected_called_;
105 }
106 int on_response_started_called() const { return on_response_started_called_; }
107 int on_will_read_called() const { return on_will_read_called_; }
108 int on_read_completed_called() const { return on_read_completed_called_; }
109
110 private:
111 scoped_refptr<net::IOBuffer> buffer_;
112 bool response_started_succeeds_;
113 bool defer_on_response_started_;
114 bool will_read_succeeds_;
115 bool read_completed_succeeds_;
116 bool defer_on_read_completed_;
117
118 int on_will_start_called_;
119 int on_request_redirected_called_;
120 int on_response_started_called_;
121 int on_will_read_called_;
122 int on_read_completed_called_;
123
124 DISALLOW_COPY_AND_ASSIGN(TestResourceHandler);
125 };
126
127 class TestResourceDispatcherHostDelegate
128 : public ResourceDispatcherHostDelegate {
129 public:
130 explicit TestResourceDispatcherHostDelegate(bool must_download)
131 : must_download_(must_download) {}
132
133 bool ShouldForceDownloadResource(const GURL& url,
134 const std::string& mime_type) override {
135 return must_download_;
136 }
137
138 private:
139 const bool must_download_;
140 };
141
142 class TestResourceDispatcherHost : public ResourceDispatcherHostImpl {
143 public:
144 explicit TestResourceDispatcherHost(bool stream_has_handler)
145 : stream_has_handler_(stream_has_handler),
146 intercepted_as_stream_(false),
147 intercepted_as_stream_count_(0),
148 new_resource_handler_(nullptr) {}
149
150 bool intercepted_as_stream() const { return intercepted_as_stream_; }
151
152 std::unique_ptr<ResourceHandler> CreateResourceHandlerForDownload(
153 net::URLRequest* request,
154 bool is_content_initiated,
155 bool must_download) override {
156 return CreateNewResourceHandler();
157 }
158
159 std::unique_ptr<ResourceHandler> MaybeInterceptAsStream(
160 const base::FilePath& plugin_path,
161 net::URLRequest* request,
162 ResourceResponse* response,
163 std::string* payload) override {
164 intercepted_as_stream_count_++;
165 if (stream_has_handler_)
166 intercepted_as_stream_ = true;
167 return CreateNewResourceHandler();
168 }
169
170 int intercepted_as_stream_count() const {
171 return intercepted_as_stream_count_;
172 }
173
174 TestResourceHandler* new_resource_handler() const {
175 return new_resource_handler_;
176 }
177
178 private:
179 std::unique_ptr<ResourceHandler> CreateNewResourceHandler() {
180 std::unique_ptr<TestResourceHandler> new_resource_handler(
181 new TestResourceHandler(false, false, true, true, false));
182 new_resource_handler_ = new_resource_handler.get();
183 return std::move(new_resource_handler);
184 }
185
186 // Whether the URL request should be intercepted as a stream.
187 bool stream_has_handler_;
188
189 // Whether the URL request has been intercepted as a stream.
190 bool intercepted_as_stream_;
191
192 // Count of number of times MaybeInterceptAsStream function get called in a
193 // test.
194 int intercepted_as_stream_count_;
195
196 // The last alternative TestResourceHandler created by this
197 // TestResourceDispatcherHost.
198 TestResourceHandler* new_resource_handler_;
199 };
200
201 class TestFakePluginService : public FakePluginService {
202 public:
203 // If |is_plugin_stale| is true, GetPluginInfo will indicate the plugins are
204 // stale until GetPlugins is called.
205 TestFakePluginService(bool plugin_available, bool is_plugin_stale)
206 : plugin_available_(plugin_available),
207 is_plugin_stale_(is_plugin_stale) {}
208
209 bool GetPluginInfo(int render_process_id,
210 int render_frame_id,
211 ResourceContext* context,
212 const GURL& url,
213 const GURL& page_url,
214 const std::string& mime_type,
215 bool allow_wildcard,
216 bool* is_stale,
217 WebPluginInfo* info,
218 std::string* actual_mime_type) override {
219 *is_stale = is_plugin_stale_;
220 if (!is_plugin_stale_ || !plugin_available_)
221 return false;
222 info->type = WebPluginInfo::PLUGIN_TYPE_BROWSER_PLUGIN;
223 info->path = base::FilePath::FromUTF8Unsafe(
224 std::string("chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/"));
225 return true;
226 }
227
228 void GetPlugins(const GetPluginsCallback& callback) override {
229 is_plugin_stale_ = false;
230 std::vector<WebPluginInfo> plugins;
231 base::ThreadTaskRunnerHandle::Get()->PostTask(
232 FROM_HERE, base::Bind(callback, plugins));
233 }
234
235 private:
236 const bool plugin_available_;
237 bool is_plugin_stale_;
238
239 DISALLOW_COPY_AND_ASSIGN(TestFakePluginService);
240 };
241
242 class TestResourceController : public ResourceController {
243 public:
244 TestResourceController() : cancel_call_count_(0), resume_call_count_(0) {}
245
246 void Cancel() override { cancel_call_count_++; }
247
248 void CancelAndIgnore() override { NOTREACHED(); }
249
250 void CancelWithError(int error_code) override { NOTREACHED(); }
251
252 void Resume() override { resume_call_count_++; }
253
254 int cancel_call_count() const { return cancel_call_count_; }
255 int resume_call_count() const { return resume_call_count_; }
256
257 private:
258 int cancel_call_count_;
259 int resume_call_count_;
260 };
261
262 } // namespace
263
264 class MimeSniffingResourceHandlerTest : public testing::Test {
265 public:
266 MimeSniffingResourceHandlerTest()
267 : stream_has_handler_(false),
268 plugin_available_(false),
269 plugin_stale_(false) {}
270
271 // Tests that the MimeSniffingHandler properly sets the accept field in the
272 // header. Returns the accept header value.
273 std::string TestAcceptHeaderSetting(ResourceType request_resource_type);
274 std::string TestAcceptHeaderSettingWithURLRequest(
275 ResourceType request_resource_type,
276 net::URLRequest* request);
277
278 void set_stream_has_handler(bool stream_has_handler) {
279 stream_has_handler_ = stream_has_handler;
280 }
281
282 void set_plugin_available(bool plugin_available) {
283 plugin_available_ = plugin_available;
284 }
285
286 void set_plugin_stale(bool plugin_stale) { plugin_stale_ = plugin_stale; }
287
288 bool TestStreamIsIntercepted(bool allow_download,
289 bool must_download,
290 ResourceType request_resource_type);
291
292 // Tests the operation of the MimeSniffingHandler when it needs to buffer
293 // data (example case: the response is text/plain).
294 void TestHandlerSniffing(bool response_started,
295 bool defer_response_started,
296 bool will_read,
297 bool read_completed,
298 bool defer_read_completed);
299
300 // Tests the operation of the MimeSniffingHandler when it doesn't buffer
301 // data (example case: the response is text/html).
302 void TestHandlerNoSniffing(bool response_started,
303 bool defer_response_started,
304 bool will_read,
305 bool read_completed,
306 bool defer_read_completed);
307
308 private:
309 // Whether the URL request should be intercepted as a stream.
310 bool stream_has_handler_;
311 bool plugin_available_;
312 bool plugin_stale_;
313
314 TestBrowserThreadBundle thread_bundle_;
315 };
316
317 std::string MimeSniffingResourceHandlerTest::TestAcceptHeaderSetting(
318 ResourceType request_resource_type) {
319 net::URLRequestContext context;
320 std::unique_ptr<net::URLRequest> request(context.CreateRequest(
321 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr));
322 return TestAcceptHeaderSettingWithURLRequest(request_resource_type,
323 request.get());
324 }
325
326 std::string
327 MimeSniffingResourceHandlerTest::TestAcceptHeaderSettingWithURLRequest(
328 ResourceType request_resource_type,
329 net::URLRequest* request) {
330 bool is_main_frame = request_resource_type == RESOURCE_TYPE_MAIN_FRAME;
331 ResourceRequestInfo::AllocateForTesting(request, request_resource_type,
332 nullptr, // context
333 0, // render_process_id
334 0, // render_view_id
335 0, // render_frame_id
336 is_main_frame, // is_main_frame
337 false, // parent_is_main_frame
338 false, // allow_download
339 true, // is_async
340 false); // is_using_lofi
341
342 std::unique_ptr<ResourceHandler> mime_sniffing_handler(
343 new MimeSniffingResourceHandler(
344 std::unique_ptr<ResourceHandler>(
345 new TestResourceHandler(false, false, false, false, false)),
346 nullptr, nullptr, nullptr, request));
347
348 bool defer = false;
349 mime_sniffing_handler->OnWillStart(request->url(), &defer);
350 content::RunAllPendingInMessageLoop();
351
352 std::string accept_header;
353 request->extra_request_headers().GetHeader("Accept", &accept_header);
354 return accept_header;
355 }
356
357 bool MimeSniffingResourceHandlerTest::TestStreamIsIntercepted(
358 bool allow_download,
359 bool must_download,
360 ResourceType request_resource_type) {
361 net::URLRequestContext context;
362 std::unique_ptr<net::URLRequest> request(context.CreateRequest(
363 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr));
364 bool is_main_frame = request_resource_type == RESOURCE_TYPE_MAIN_FRAME;
365 ResourceRequestInfo::AllocateForTesting(request.get(), request_resource_type,
366 nullptr, // context
367 0, // render_process_id
368 0, // render_view_id
369 0, // render_frame_id
370 is_main_frame, // is_main_frame
371 false, // parent_is_main_frame
372 allow_download, // allow_download
373 true, // is_async
374 false); // is_using_lofi
375
376 TestResourceDispatcherHost host(stream_has_handler_);
377 TestResourceDispatcherHostDelegate host_delegate(must_download);
378 host.SetDelegate(&host_delegate);
379
380 TestFakePluginService plugin_service(plugin_available_, plugin_stale_);
381 std::unique_ptr<InterceptingResourceHandler> intercepting_handler(
382 new InterceptingResourceHandler(std::unique_ptr<ResourceHandler>(),
383 nullptr));
384 std::unique_ptr<ResourceHandler> mime_handler(new MimeSniffingResourceHandler(
385 std::unique_ptr<ResourceHandler>(
386 new TestResourceHandler(false, false, false, false, false)),
387 &host, &plugin_service, intercepting_handler.get(), request.get()));
388
389 TestResourceController resource_controller;
390 mime_handler->SetController(&resource_controller);
391
392 scoped_refptr<ResourceResponse> response(new ResourceResponse);
393 // The MIME type isn't important but it shouldn't be empty.
394 response->head.mime_type = "application/pdf";
395
396 bool defer = false;
397 mime_handler->OnResponseStarted(response.get(), &defer);
398
399 content::RunAllPendingInMessageLoop();
400 EXPECT_LT(host.intercepted_as_stream_count(), 2);
401 if (allow_download)
402 EXPECT_TRUE(intercepting_handler->new_handler_for_testing());
403 return host.intercepted_as_stream();
404 }
405
406 void MimeSniffingResourceHandlerTest::TestHandlerSniffing(
407 bool response_started,
408 bool defer_response_started,
409 bool will_read,
410 bool read_completed,
411 bool defer_read_completed) {
412 net::URLRequestContext context;
413 std::unique_ptr<net::URLRequest> request(context.CreateRequest(
414 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr));
415 ResourceRequestInfo::AllocateForTesting(request.get(),
416 RESOURCE_TYPE_MAIN_FRAME,
417 nullptr, // context
418 0, // render_process_id
419 0, // render_view_id
420 0, // render_frame_id
421 true, // is_main_frame
422 false, // parent_is_main_frame
423 false, // allow_download
424 true, // is_async
425 false); // is_using_lofi
426
427 TestResourceDispatcherHost host(false);
428 TestResourceDispatcherHostDelegate host_delegate(false);
429 host.SetDelegate(&host_delegate);
430
431 TestFakePluginService plugin_service(plugin_available_, plugin_stale_);
432 std::unique_ptr<InterceptingResourceHandler> intercepting_handler(
433 new InterceptingResourceHandler(std::unique_ptr<ResourceHandler>(),
434 nullptr));
435 std::unique_ptr<TestResourceHandler> scoped_test_handler =
436 std::unique_ptr<TestResourceHandler>(new TestResourceHandler(
437 response_started, defer_response_started, will_read, read_completed,
438 defer_read_completed));
439 TestResourceHandler* test_handler = scoped_test_handler.get();
440 std::unique_ptr<MimeSniffingResourceHandler> mime_sniffing_handler(
441 new MimeSniffingResourceHandler(
442 std::move(scoped_test_handler), &host, &plugin_service,
443 intercepting_handler.get(), request.get()));
444
445 TestResourceController resource_controller;
446 mime_sniffing_handler->SetController(&resource_controller);
447
448 bool defer = false;
449 mime_sniffing_handler->OnWillStart(GURL(), &defer);
450
451 // The response should be sniffed.
452 scoped_refptr<ResourceResponse> response(new ResourceResponse);
453 response->head.mime_type.assign("text/plain");
454
455 // Simulate the response starting. The MimeSniffingHandler should start
456 // buffering, so the return value should always be true.
457 EXPECT_TRUE(mime_sniffing_handler->OnResponseStarted(response.get(), &defer));
458 EXPECT_EQ(0, resource_controller.cancel_call_count());
459 EXPECT_EQ(0, resource_controller.resume_call_count());
460 EXPECT_FALSE(defer);
461
462 // Read some data to sniff the mime type. This will ask the next
463 // ResourceHandler for a buffer.
464 scoped_refptr<net::IOBuffer> read_buffer;
465 int buf_size = 0;
466 EXPECT_EQ(will_read,
467 mime_sniffing_handler->OnWillRead(&read_buffer, &buf_size, -1));
468 EXPECT_EQ(0, resource_controller.cancel_call_count());
469
470 if (!will_read) {
471 EXPECT_EQ(1, test_handler->on_will_start_called());
472 EXPECT_EQ(0, test_handler->on_request_redirected_called());
473 EXPECT_EQ(0, test_handler->on_response_started_called());
474 EXPECT_EQ(1, test_handler->on_will_read_called());
475 EXPECT_EQ(0, test_handler->on_read_completed_called());
476
477 // Process all messages to ensure proper test teardown.
478 content::RunAllPendingInMessageLoop();
479 return;
480 }
481
482 // Simulate an HTML page. The mime sniffer will identify the MimeType and
483 // proceed with replay.
484 char data[] = "!DOCTYPE html\n<head>\n<title>Foo</title>\n</head>";
485 memcpy(read_buffer->data(), data, sizeof(data));
486
487 defer = false;
488 bool return_value =
489 mime_sniffing_handler->OnReadCompleted(sizeof(data), &defer);
490
491 // If the next handler cancels the response start, the caller of
492 // MimeSniffingHandler::OnReadCompleted should be notified immediately.
493 if (!response_started) {
494 EXPECT_FALSE(defer);
495 EXPECT_EQ(response_started, return_value);
496 EXPECT_EQ(0, resource_controller.cancel_call_count());
497
498 EXPECT_EQ(1, test_handler->on_will_start_called());
499 EXPECT_EQ(0, test_handler->on_request_redirected_called());
500 EXPECT_EQ(1, test_handler->on_response_started_called());
501 EXPECT_EQ(1, test_handler->on_will_read_called());
502 EXPECT_EQ(0, test_handler->on_read_completed_called());
503
504 // Process all messages to ensure proper test teardown.
505 content::RunAllPendingInMessageLoop();
506 return;
507 }
508
509 // The replay can be deferred both at response started and read replay
510 // stages.
511 EXPECT_EQ(defer, defer_response_started || defer_read_completed);
512 if (defer_response_started) {
513 EXPECT_TRUE(defer);
514 EXPECT_TRUE(return_value);
515 EXPECT_EQ(MimeSniffingResourceHandler::STATE_REPLAYING_RESPONSE_RECEIVED,
516 mime_sniffing_handler->state_);
517 mime_sniffing_handler->Resume();
518 }
519
520 // The body that was sniffed should be transmitted to the next handler. This
521 // may cancel the request.
522 if (!read_completed) {
523 if (defer_response_started) {
524 EXPECT_EQ(1, resource_controller.cancel_call_count());
525 } else {
526 EXPECT_EQ(0, resource_controller.cancel_call_count());
527 EXPECT_FALSE(return_value);
528 }
529 // Process all messages to ensure proper test teardown.
530 content::RunAllPendingInMessageLoop();
531 return;
532 }
533
534 EXPECT_EQ(MimeSniffingResourceHandler::STATE_STREAMING,
535 mime_sniffing_handler->state_);
536
537 // The request may be deferred by the next handler once the read is done.
538 if (defer_read_completed) {
539 EXPECT_TRUE(defer);
540 mime_sniffing_handler->Resume();
541 }
542
543 EXPECT_EQ(MimeSniffingResourceHandler::STATE_STREAMING,
544 mime_sniffing_handler->state_);
545 EXPECT_EQ(0, resource_controller.cancel_call_count());
546
547 // Even if the next handler defers the request twice, the
548 // MimeSniffingResourceHandler should only call Resume on its controller
549 // once.
550 if (defer_response_started || defer_read_completed) {
551 EXPECT_EQ(1, resource_controller.resume_call_count());
552 } else {
553 EXPECT_EQ(0, resource_controller.resume_call_count());
554 }
555
556 EXPECT_EQ(1, test_handler->on_will_start_called());
557 EXPECT_EQ(0, test_handler->on_request_redirected_called());
558 EXPECT_EQ(1, test_handler->on_response_started_called());
559 EXPECT_EQ(1, test_handler->on_will_read_called());
560 EXPECT_EQ(1, test_handler->on_read_completed_called());
561
562 // Process all messages to ensure proper test teardown.
563 content::RunAllPendingInMessageLoop();
564 }
565
566 void MimeSniffingResourceHandlerTest::TestHandlerNoSniffing(
567 bool response_started,
568 bool defer_response_started,
569 bool will_read,
570 bool read_completed,
571 bool defer_read_completed) {
572 net::URLRequestContext context;
573 std::unique_ptr<net::URLRequest> request(context.CreateRequest(
574 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr));
575 ResourceRequestInfo::AllocateForTesting(request.get(),
576 RESOURCE_TYPE_MAIN_FRAME,
577 nullptr, // context
578 0, // render_process_id
579 0, // render_view_id
580 0, // render_frame_id
581 true, // is_main_frame
582 false, // parent_is_main_frame
583 false, // allow_download
584 true, // is_async
585 false); // is_using_lofi
586
587 TestResourceDispatcherHost host(false);
588 TestResourceDispatcherHostDelegate host_delegate(false);
589 host.SetDelegate(&host_delegate);
590
591 TestFakePluginService plugin_service(plugin_available_, plugin_stale_);
592 std::unique_ptr<InterceptingResourceHandler> intercepting_handler(
593 new InterceptingResourceHandler(std::unique_ptr<ResourceHandler>(),
594 nullptr));
595
596 std::unique_ptr<TestResourceHandler> scoped_test_handler =
597 std::unique_ptr<TestResourceHandler>(new TestResourceHandler(
598 response_started, defer_response_started, will_read, read_completed,
599 defer_read_completed));
600 TestResourceHandler* test_handler = scoped_test_handler.get();
601 std::unique_ptr<MimeSniffingResourceHandler> mime_sniffing_handler(
602 new MimeSniffingResourceHandler(
603 std::move(scoped_test_handler), &host, &plugin_service,
604 intercepting_handler.get(), request.get()));
605
606 TestResourceController resource_controller;
607 mime_sniffing_handler->SetController(&resource_controller);
608
609 int expected_resume_calls = 0;
610
611 bool defer = false;
612 mime_sniffing_handler->OnWillStart(GURL(), &defer);
613
614 // The response should not be sniffed.
615 scoped_refptr<ResourceResponse> response(new ResourceResponse);
616 response->head.mime_type.assign("text/html");
617
618 // Simulate the response starting. There should be no need for buffering, so
619 // the return value should be that of the next handler.
620 EXPECT_EQ(response_started,
621 mime_sniffing_handler->OnResponseStarted(response.get(), &defer));
622 EXPECT_EQ(0, resource_controller.cancel_call_count());
623
624 if (!response_started) {
625 EXPECT_FALSE(defer);
626
627 EXPECT_EQ(1, test_handler->on_will_start_called());
628 EXPECT_EQ(0, test_handler->on_request_redirected_called());
629 EXPECT_EQ(1, test_handler->on_response_started_called());
630 EXPECT_EQ(0, test_handler->on_will_read_called());
631 EXPECT_EQ(0, test_handler->on_read_completed_called());
632
633 // Process all messages to ensure proper test teardown.
634 content::RunAllPendingInMessageLoop();
635 return;
636 }
637
638 EXPECT_EQ(defer_response_started, defer);
639 if (defer) {
640 EXPECT_EQ(MimeSniffingResourceHandler::STATE_REPLAYING_RESPONSE_RECEIVED,
641 mime_sniffing_handler->state_);
642 expected_resume_calls++;
643 mime_sniffing_handler->Resume();
644 }
645
646 EXPECT_EQ(expected_resume_calls, resource_controller.resume_call_count());
647
648 // The MimeSniffingResourceHandler should be acting as a pass-through
649 // ResourceHandler.
650 scoped_refptr<net::IOBuffer> read_buffer;
651 int buf_size = 0;
652 EXPECT_EQ(will_read,
653 mime_sniffing_handler->OnWillRead(&read_buffer, &buf_size, -1));
654 EXPECT_EQ(0, resource_controller.cancel_call_count());
655
656 if (!will_read) {
657 EXPECT_EQ(1, test_handler->on_will_start_called());
658 EXPECT_EQ(0, test_handler->on_request_redirected_called());
659 EXPECT_EQ(1, test_handler->on_response_started_called());
660 EXPECT_EQ(1, test_handler->on_will_read_called());
661 EXPECT_EQ(0, test_handler->on_read_completed_called());
662
663 // Process all messages to ensure proper test teardown.
664 content::RunAllPendingInMessageLoop();
665 return;
666 }
667
668 defer = false;
669 EXPECT_EQ(read_completed,
670 mime_sniffing_handler->OnReadCompleted(2000, &defer));
671 EXPECT_EQ(0, resource_controller.cancel_call_count());
672
673 EXPECT_EQ(1, test_handler->on_will_start_called());
674 EXPECT_EQ(0, test_handler->on_request_redirected_called());
675 EXPECT_EQ(1, test_handler->on_response_started_called());
676 EXPECT_EQ(1, test_handler->on_will_read_called());
677 EXPECT_EQ(1, test_handler->on_read_completed_called());
678
679 if (!read_completed) {
680 EXPECT_FALSE(defer);
681
682 // Process all messages to ensure proper test teardown.
683 content::RunAllPendingInMessageLoop();
684 return;
685 }
686
687 EXPECT_EQ(defer_read_completed, defer);
688 if (defer) {
689 expected_resume_calls++;
690 mime_sniffing_handler->Resume();
691 }
692 EXPECT_EQ(expected_resume_calls, resource_controller.resume_call_count());
693
694 // Process all messages to ensure proper test teardown.
695 content::RunAllPendingInMessageLoop();
696 }
697
698 // Test that the proper Accept: header is set based on the ResourceType
699 TEST_F(MimeSniffingResourceHandlerTest, AcceptHeaders) {
700 EXPECT_EQ(
701 "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,"
702 "*/*;q=0.8",
703 TestAcceptHeaderSetting(RESOURCE_TYPE_MAIN_FRAME));
704 EXPECT_EQ(
705 "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,"
706 "*/*;q=0.8",
707 TestAcceptHeaderSetting(RESOURCE_TYPE_SUB_FRAME));
708 EXPECT_EQ("text/css,*/*;q=0.1",
709 TestAcceptHeaderSetting(RESOURCE_TYPE_STYLESHEET));
710 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_SCRIPT));
711 EXPECT_EQ("image/webp,image/*,*/*;q=0.8",
712 TestAcceptHeaderSetting(RESOURCE_TYPE_IMAGE));
713 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_FONT_RESOURCE));
714 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_SUB_RESOURCE));
715 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_OBJECT));
716 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_MEDIA));
717 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_WORKER));
718 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_SHARED_WORKER));
719 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_PREFETCH));
720 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_FAVICON));
721 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_XHR));
722 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_PING));
723 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_SERVICE_WORKER));
724 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_CSP_REPORT));
725 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_PLUGIN_RESOURCE));
726
727 // Ensure that if an Accept header is already set, it is not overwritten.
728 net::URLRequestContext context;
729 std::unique_ptr<net::URLRequest> request(context.CreateRequest(
730 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr));
731 request->SetExtraRequestHeaderByName("Accept", "*", true);
732 EXPECT_EQ("*", TestAcceptHeaderSettingWithURLRequest(RESOURCE_TYPE_XHR,
733 request.get()));
734 }
735
736 // Test that stream requests are correctly intercepted under the right
737 // circumstances. Test is not relevent when plugins are disabled.
738 #if defined(ENABLE_PLUGINS)
739 TEST_F(MimeSniffingResourceHandlerTest, StreamHandling) {
740 bool allow_download;
741 bool must_download;
742 ResourceType resource_type;
743
744 // Ensure the stream is handled by MaybeInterceptAsStream in the
745 // ResourceDispatcherHost.
746 set_stream_has_handler(true);
747 set_plugin_available(true);
748
749 // Main frame request with no download allowed. Stream shouldn't be
750 // intercepted.
751 allow_download = false;
752 must_download = false;
753 resource_type = RESOURCE_TYPE_MAIN_FRAME;
754 EXPECT_FALSE(
755 TestStreamIsIntercepted(allow_download, must_download, resource_type));
756
757 // Main frame request with download allowed. Stream should be intercepted.
758 allow_download = true;
759 must_download = false;
760 resource_type = RESOURCE_TYPE_MAIN_FRAME;
761 EXPECT_TRUE(
762 TestStreamIsIntercepted(allow_download, must_download, resource_type));
763
764 // Main frame request with download forced. Stream shouldn't be intercepted.
765 allow_download = true;
766 must_download = true;
767 resource_type = RESOURCE_TYPE_MAIN_FRAME;
768 EXPECT_FALSE(
769 TestStreamIsIntercepted(allow_download, must_download, resource_type));
770
771 // Sub-resource request with download not allowed. Stream shouldn't be
772 // intercepted.
773 allow_download = false;
774 must_download = false;
775 resource_type = RESOURCE_TYPE_SUB_RESOURCE;
776 EXPECT_FALSE(
777 TestStreamIsIntercepted(allow_download, must_download, resource_type));
778
779 // Plugin resource request with download not allowed. Stream shouldn't be
780 // intercepted.
781 allow_download = false;
782 must_download = false;
783 resource_type = RESOURCE_TYPE_PLUGIN_RESOURCE;
784 EXPECT_FALSE(
785 TestStreamIsIntercepted(allow_download, must_download, resource_type));
786
787 // Object request with download not allowed. Stream should be intercepted.
788 allow_download = false;
789 must_download = false;
790 resource_type = RESOURCE_TYPE_OBJECT;
791 EXPECT_TRUE(
792 TestStreamIsIntercepted(allow_download, must_download, resource_type));
793
794 // Test the cases where the stream isn't handled by MaybeInterceptAsStream
795 // in the ResourceDispatcherHost.
796 set_stream_has_handler(false);
797 allow_download = false;
798 must_download = false;
799 resource_type = RESOURCE_TYPE_OBJECT;
800 EXPECT_FALSE(
801 TestStreamIsIntercepted(allow_download, must_download, resource_type));
802
803 // Test the cases where the stream handled by MaybeInterceptAsStream
804 // with plugin not available. This is the case when intercepting streams for
805 // the streamsPrivate extensions API.
806 set_stream_has_handler(true);
807 set_plugin_available(false);
808 allow_download = false;
809 must_download = false;
810 resource_type = RESOURCE_TYPE_OBJECT;
811 EXPECT_TRUE(
812 TestStreamIsIntercepted(allow_download, must_download, resource_type));
813
814 // Test the cases where the stream handled by MaybeInterceptAsStream
815 // with plugin not available. This is the case when intercepting streams for
816 // the streamsPrivate extensions API with stale plugin.
817 set_plugin_stale(true);
818 allow_download = false;
819 must_download = false;
820 resource_type = RESOURCE_TYPE_OBJECT;
821 EXPECT_TRUE(
822 TestStreamIsIntercepted(allow_download, must_download, resource_type));
823 }
824 #endif
825
826 // Test that the MimeSniffingHandler operates properly when it doesn't sniff
827 // resources.
828 TEST_F(MimeSniffingResourceHandlerTest, NoSniffing) {
829 // Test simple case.
830 TestHandlerNoSniffing(
831 true /* response_started_succeeds */,
832 false /* defer_response_started */,
833 true /* will_read_succeeds */,
834 true /* read_completed_succeeds */,
835 false /* defer_read_completed */);
836
837 // Test deferral in OnResponseStarted and/or in OnReadCompleted.
838 TestHandlerNoSniffing(
839 true /* response_started_succeeds */,
840 true /* defer_response_started */,
841 true /* will_read_succeeds */,
842 true /* read_completed_succeeds */,
843 false /* defer_read_completed */);
844 TestHandlerNoSniffing(
845 true /* response_started_succeeds */,
846 false /* defer_response_started */,
847 true /* will_read_succeeds */,
848 true /* read_completed_succeeds */,
849 true /* defer_read_completed */);
850 TestHandlerNoSniffing(
851 true /* response_started_succeeds */,
852 true /* defer_response_started */,
853 true /* will_read_succeeds */,
854 true /* read_completed_succeeds */,
855 true /* defer_read_completed */);
856
857 // Test cancel in OnResponseStarted, OnWillRead, OnReadCompleted.
858 TestHandlerNoSniffing(
859 false /* response_started_succeeds */,
860 false /* defer_response_started */,
861 false /* will_read_succeeds */,
862 false /* read_completed_succeeds */,
863 false /* defer_read_completed */);
864 TestHandlerNoSniffing(
865 true /* response_started_succeeds */,
866 false /* defer_response_started */,
867 false /* will_read_succeeds */,
868 false /* read_completed_succeeds */,
869 false /* defer_read_completed */);
870 TestHandlerNoSniffing(
871 true /* response_started_succeeds */,
872 false /* defer_response_started */,
873 true /* will_read_succeeds */,
874 false /* read_completed_succeeds */,
875 false /* defer_read_completed */);
876
877 // Test cancel after OnResponseStarted deferral.
878 TestHandlerNoSniffing(
879 true /* response_started_succeeds */,
880 true /* defer_response_started */,
881 false /* will_read_succeeds */,
882 false /* read_completed_succeeds */,
883 false /* defer_read_completed */);
884 TestHandlerNoSniffing(
885 true /* response_started_succeeds */,
886 true /* defer_response_started */,
887 true /* will_read_succeeds */,
888 false /* read_completed_succeeds */,
889 false /* defer_read_completed */);
890
891 content::RunAllPendingInMessageLoop();
892 }
893
894 // Test that the MimeSniffingHandler operates properly when it sniffs
895 // resources.
896 TEST_F(MimeSniffingResourceHandlerTest, Sniffing) {
897 // Test simple case.
898 TestHandlerSniffing(
899 true /* response_started_succeeds */,
900 false /* defer_response_started */,
901 true /* will_read_succeeds */,
902 true /* read_completed_succeeds */,
903 false /* defer_read_completed */);
904
905 // Test deferral in OnResponseStarted and/or in OnReadCompleted.
906 TestHandlerSniffing(
907 true /* response_started_succeeds */,
908 true /* defer_response_started */,
909 true /* will_read_succeeds */,
910 true /* read_completed_succeeds */,
911 false /* defer_read_completed */);
912 TestHandlerSniffing(
913 true /* response_started_succeeds */,
914 false /* defer_response_started */,
915 true /* will_read_succeeds */,
916 true /* read_completed_succeeds */,
917 true /* defer_read_completed */);
918 TestHandlerSniffing(
919 true /* response_started_succeeds */,
920 true /* defer_response_started */,
921 true /* will_read_succeeds */,
922 true /* read_completed_succeeds */,
923 true /* defer_read_completed */);
924
925 // Test cancel in OnResponseStarted, OnWillRead, OnReadCompleted.
926 TestHandlerSniffing(
927 false /* response_started_succeeds */,
928 false /* defer_response_started */,
929 false /* will_read_succeeds */,
930 false /* read_completed_succeeds */,
931 false /* defer_read_completed */);
932 TestHandlerSniffing(
933 true /* response_started_succeeds */,
934 false /* defer_response_started */,
935 false /* will_read_succeeds */,
936 false /* read_completed_succeeds */,
937 false /* defer_read_completed */);
938 TestHandlerSniffing(
939 true /* response_started_succeeds */,
940 false /* defer_response_started */,
941 true /* will_read_succeeds */,
942 false /* read_completed_succeeds */,
943 false /* defer_read_completed */);
944
945 // Test cancel after OnResponseStarted deferral.
946 TestHandlerSniffing(
947 true /* response_started_succeeds */,
948 true /* defer_response_started */,
949 false /* will_read_succeeds */,
950 false /* read_completed_succeeds */,
951 false /* defer_read_completed */);
952 TestHandlerSniffing(
953 true /* response_started_succeeds */,
954 true /* defer_response_started */,
955 true /* will_read_succeeds */,
956 false /* read_completed_succeeds */,
957 false /* defer_read_completed */);
958
959 content::RunAllPendingInMessageLoop();
960 }
961
962 // Tests that 304s do not trigger a change in handlers.
963 TEST_F(MimeSniffingResourceHandlerTest, 304Handling) {
964 net::URLRequestContext context;
965 std::unique_ptr<net::URLRequest> request(context.CreateRequest(
966 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr));
967 ResourceRequestInfo::AllocateForTesting(request.get(),
968 RESOURCE_TYPE_MAIN_FRAME,
969 nullptr, // context
970 0, // render_process_id
971 0, // render_view_id
972 0, // render_frame_id
973 true, // is_main_frame
974 false, // parent_is_main_frame
975 true, // allow_download
976 true, // is_async
977 false); // is_using_lofi
978
979 TestResourceDispatcherHost host(false);
980 TestResourceDispatcherHostDelegate host_delegate(false);
981 host.SetDelegate(&host_delegate);
982
983 TestFakePluginService plugin_service(false, false);
984 std::unique_ptr<ResourceHandler> intercepting_handler(
985 new InterceptingResourceHandler(std::unique_ptr<ResourceHandler>(),
986 nullptr));
987 std::unique_ptr<ResourceHandler> mime_handler(new MimeSniffingResourceHandler(
988 std::unique_ptr<ResourceHandler>(
989 new TestResourceHandler(true, false, true, true, false)),
990 &host, &plugin_service,
991 static_cast<InterceptingResourceHandler*>(intercepting_handler.get()),
992 request.get()));
993
994 TestResourceController resource_controller;
995 mime_handler->SetController(&resource_controller);
996
997 // Simulate a 304 response.
998 scoped_refptr<ResourceResponse> response(new ResourceResponse);
999 // The MIME type isn't important but it shouldn't be empty.
1000 response->head.mime_type = "application/pdf";
1001 response->head.headers = new net::HttpResponseHeaders("HTTP/1.x 304 OK");
1002
1003 // The response is received. No new ResourceHandler should be created to
1004 // handle the download.
1005 bool defer = false;
1006 mime_handler->OnResponseStarted(response.get(), &defer);
1007 EXPECT_FALSE(defer);
1008 EXPECT_FALSE(host.new_resource_handler());
1009
1010 content::RunAllPendingInMessageLoop();
1011 }
1012
1013 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/loader/mime_sniffing_resource_handler.cc ('k') | content/browser/loader/mime_type_resource_handler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698