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

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

Powered by Google App Engine
This is Rietveld 408576698