OLD | NEW |
---|---|
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, |
38 bool defer_response_started, | |
39 bool will_read, | |
40 bool read_completed, | |
41 bool defer_read_completed) | |
mmenke
2016/07/25 17:39:32
Think these names can be clearer. Maybe:
respons
clamy
2016/07/26 16:24:16
Done.
| |
42 : ResourceHandler(nullptr), | |
43 buffer_(new net::IOBuffer(2048)), | |
44 response_started_(response_started), | |
45 defer_response_started_(defer_response_started), | |
46 will_read_(will_read), | |
47 read_completed_(read_completed), | |
48 defer_read_completed_(defer_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_response_started_) |
61 *defer = true; | |
62 return response_started_; | |
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_; | |
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_read_completed_) |
78 *defer = true; | |
79 return read_completed_; | |
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_; | |
93 bool defer_response_started_; | |
94 bool will_read_; | |
95 bool read_completed_; | |
96 bool defer_read_completed_; | |
97 DISALLOW_COPY_AND_ASSIGN(TestResourceHandler); | |
mmenke
2016/07/25 17:39:32
nit: Blank line before disallow_copy_and_assign
clamy
2016/07/26 16:24:15
Done.
| |
98 }; | |
99 | |
100 class TestResourceDispatcherHostDelegate | |
101 : public ResourceDispatcherHostDelegate { | |
102 public: | |
103 TestResourceDispatcherHostDelegate(bool must_download) | |
mmenke
2016/07/25 17:39:31
explicit
clamy
2016/07/26 16:24:15
Done.
| |
104 : must_download_(must_download) {} | |
105 | |
106 bool ShouldForceDownloadResource(const GURL& url, | |
107 const std::string& mime_type) override { | |
108 return must_download_; | |
74 } | 109 } |
75 | 110 |
76 private: | 111 private: |
77 DISALLOW_COPY_AND_ASSIGN(TestResourceHandler); | 112 const bool must_download_; |
78 }; | 113 }; |
79 | 114 |
80 class TestResourceDispatcherHost : public ResourceDispatcherHostImpl { | 115 class TestResourceDispatcherHost : public ResourceDispatcherHostImpl { |
81 public: | 116 public: |
82 explicit TestResourceDispatcherHost(bool stream_has_handler) | 117 TestResourceDispatcherHost(bool stream_has_handler) |
mmenke
2016/07/25 17:39:31
explicit
clamy
2016/07/26 16:24:15
Done.
| |
83 : stream_has_handler_(stream_has_handler), | 118 : stream_has_handler_(stream_has_handler), |
84 intercepted_as_stream_(false), | 119 intercepted_as_stream_(false), |
85 intercepted_as_stream_count_(0) {} | 120 intercepted_as_stream_count_(0), |
121 last_resource_handler_(nullptr) {} | |
86 | 122 |
87 bool intercepted_as_stream() const { return intercepted_as_stream_; } | 123 bool intercepted_as_stream() const { return intercepted_as_stream_; } |
88 | 124 |
89 std::unique_ptr<ResourceHandler> CreateResourceHandlerForDownload( | 125 std::unique_ptr<ResourceHandler> CreateResourceHandlerForDownload( |
90 net::URLRequest* request, | 126 net::URLRequest* request, |
91 bool is_content_initiated, | 127 bool is_content_initiated, |
92 bool must_download) override { | 128 bool must_download) override { |
93 return std::unique_ptr<ResourceHandler>(new TestResourceHandler); | 129 return CreateNewResourceHandler(); |
94 } | 130 } |
95 | 131 |
96 std::unique_ptr<ResourceHandler> MaybeInterceptAsStream( | 132 std::unique_ptr<ResourceHandler> MaybeInterceptAsStream( |
97 const base::FilePath& plugin_path, | 133 const base::FilePath& plugin_path, |
98 net::URLRequest* request, | 134 net::URLRequest* request, |
99 ResourceResponse* response, | 135 ResourceResponse* response, |
100 std::string* payload) override { | 136 std::string* payload) override { |
101 intercepted_as_stream_count_++; | 137 intercepted_as_stream_count_++; |
102 if (stream_has_handler_) { | 138 if (stream_has_handler_) |
103 intercepted_as_stream_ = true; | 139 intercepted_as_stream_ = true; |
104 return std::unique_ptr<ResourceHandler>(new TestResourceHandler); | 140 return CreateNewResourceHandler(); |
105 } else { | |
106 return std::unique_ptr<ResourceHandler>(); | |
107 } | |
108 } | 141 } |
109 | 142 |
110 int intercepted_as_stream_count() const { | 143 int intercepted_as_stream_count() const { |
111 return intercepted_as_stream_count_; | 144 return intercepted_as_stream_count_; |
112 } | 145 } |
113 | 146 |
147 TestResourceHandler* last_resource_handler() const { | |
148 return last_resource_handler_; | |
149 } | |
150 | |
114 private: | 151 private: |
152 std::unique_ptr<ResourceHandler> CreateNewResourceHandler() { | |
153 std::unique_ptr<ResourceHandler> new_resource_handler; | |
154 new_resource_handler.reset( | |
155 new TestResourceHandler(false, false, true, true, false)); | |
mmenke
2016/07/25 17:39:32
Should merge the above two lines.
clamy
2016/07/26 16:24:15
Done.
| |
156 last_resource_handler_ = | |
157 static_cast<TestResourceHandler*>(new_resource_handler.get()); | |
mmenke
2016/07/25 17:39:32
Rather than a static_cast, I think it's better to
clamy
2016/07/26 16:24:15
Done.
| |
158 return new_resource_handler; | |
159 } | |
160 | |
115 // Whether the URL request should be intercepted as a stream. | 161 // Whether the URL request should be intercepted as a stream. |
116 bool stream_has_handler_; | 162 bool stream_has_handler_; |
117 | 163 |
118 // Whether the URL request has been intercepted as a stream. | 164 // Whether the URL request has been intercepted as a stream. |
119 bool intercepted_as_stream_; | 165 bool intercepted_as_stream_; |
120 | 166 |
121 // Count of number of times MaybeInterceptAsStream function get called in a | 167 // Count of number of times MaybeInterceptAsStream function get called in a |
122 // test. | 168 // test. |
123 int intercepted_as_stream_count_; | 169 int intercepted_as_stream_count_; |
124 }; | |
125 | 170 |
126 class TestResourceDispatcherHostDelegate | 171 // The last TestResourceHandler created by this TestResourceDispatcherHost. |
127 : public ResourceDispatcherHostDelegate { | 172 TestResourceHandler* last_resource_handler_; |
mmenke
2016/07/25 17:39:31
Maybe rename this to new_resource_handler_, and ma
clamy
2016/07/26 16:24:16
Done.
| |
128 public: | |
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 Loading... | |
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 std::string TestAcceptHeaderSetting(ResourceType request_resource_type); | |
246 std::string TestAcceptHeaderSettingWithURLRequest( | |
247 ResourceType request_resource_type, | |
248 net::URLRequest* request); | |
mmenke
2016/07/25 17:39:31
These need comments (What they do, what the return
clamy
2016/07/26 16:24:16
Done.
| |
249 | |
207 void set_stream_has_handler(bool stream_has_handler) { | 250 void set_stream_has_handler(bool stream_has_handler) { |
208 stream_has_handler_ = stream_has_handler; | 251 stream_has_handler_ = stream_has_handler; |
209 } | 252 } |
210 | 253 |
211 void set_plugin_available(bool plugin_available) { | 254 void set_plugin_available(bool plugin_available) { |
212 plugin_available_ = plugin_available; | 255 plugin_available_ = plugin_available; |
213 } | 256 } |
214 | 257 |
215 void set_plugin_stale(bool plugin_stale) { plugin_stale_ = plugin_stale; } | 258 void set_plugin_stale(bool plugin_stale) { plugin_stale_ = plugin_stale; } |
216 | 259 |
217 bool TestStreamIsIntercepted(bool allow_download, | 260 bool TestStreamIsIntercepted(bool allow_download, |
218 bool must_download, | 261 bool must_download, |
219 ResourceType request_resource_type); | 262 ResourceType request_resource_type); |
mmenke
2016/07/25 17:39:31
Blank line below method.
clamy
2016/07/26 16:24:16
Done.
| |
263 // Tests the operation of the MimeSniffingHandler when it needs to buffer | |
264 // data. | |
265 void TestHandlerSniffing(bool response_started, | |
266 bool defer_response_started, | |
267 bool will_read, | |
268 bool read_completed, | |
269 bool defer_read_completed); | |
220 | 270 |
221 std::string TestAcceptHeaderSetting(ResourceType request_resource_type); | 271 // Tests the operation of the MimeSniffingHandler when it doesn't buffer |
222 std::string TestAcceptHeaderSettingWithURLRequest( | 272 // data. |
223 ResourceType request_resource_type, | 273 void TestHandlerNoSniffing(bool response_started, |
224 net::URLRequest* request); | 274 bool defer_response_started, |
275 bool will_read, | |
276 bool read_completed, | |
277 bool defer_read_completed); | |
225 | 278 |
226 private: | 279 private: |
227 // Whether the URL request should be intercepted as a stream. | 280 // Whether the URL request should be intercepted as a stream. |
228 bool stream_has_handler_; | 281 bool stream_has_handler_; |
229 bool plugin_available_; | 282 bool plugin_available_; |
230 bool plugin_stale_; | 283 bool plugin_stale_; |
231 | 284 |
232 TestBrowserThreadBundle thread_bundle_; | 285 TestBrowserThreadBundle thread_bundle_; |
233 }; | 286 }; |
234 | 287 |
235 bool MimeTypeResourceHandlerTest::TestStreamIsIntercepted( | 288 std::string MimeSniffingResourceHandlerTest::TestAcceptHeaderSetting( |
289 ResourceType request_resource_type) { | |
290 net::URLRequestContext context; | |
291 std::unique_ptr<net::URLRequest> request(context.CreateRequest( | |
292 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); | |
293 return TestAcceptHeaderSettingWithURLRequest(request_resource_type, | |
294 request.get()); | |
295 } | |
296 | |
297 std::string | |
298 MimeSniffingResourceHandlerTest::TestAcceptHeaderSettingWithURLRequest( | |
299 ResourceType request_resource_type, | |
300 net::URLRequest* request) { | |
301 bool is_main_frame = request_resource_type == RESOURCE_TYPE_MAIN_FRAME; | |
302 ResourceRequestInfo::AllocateForTesting(request, request_resource_type, | |
303 nullptr, // context | |
304 0, // render_process_id | |
305 0, // render_view_id | |
306 0, // render_frame_id | |
307 is_main_frame, // is_main_frame | |
308 false, // parent_is_main_frame | |
309 false, // allow_download | |
310 true, // is_async | |
311 false); // is_using_lofi | |
312 | |
313 std::unique_ptr<ResourceHandler> mime_sniffing_handler( | |
314 new MimeSniffingResourceHandler( | |
315 std::unique_ptr<ResourceHandler>( | |
316 new TestResourceHandler(false, false, false, false, false)), | |
317 nullptr, nullptr, nullptr, request)); | |
318 | |
319 bool defer = false; | |
320 mime_sniffing_handler->OnWillStart(request->url(), &defer); | |
321 content::RunAllPendingInMessageLoop(); | |
322 | |
323 std::string accept_header; | |
324 request->extra_request_headers().GetHeader("Accept", &accept_header); | |
325 return accept_header; | |
326 } | |
327 | |
328 bool MimeSniffingResourceHandlerTest::TestStreamIsIntercepted( | |
236 bool allow_download, | 329 bool allow_download, |
237 bool must_download, | 330 bool must_download, |
238 ResourceType request_resource_type) { | 331 ResourceType request_resource_type) { |
239 net::URLRequestContext context; | 332 net::URLRequestContext context; |
240 std::unique_ptr<net::URLRequest> request(context.CreateRequest( | 333 std::unique_ptr<net::URLRequest> request(context.CreateRequest( |
241 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); | 334 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); |
242 bool is_main_frame = request_resource_type == RESOURCE_TYPE_MAIN_FRAME; | 335 bool is_main_frame = request_resource_type == RESOURCE_TYPE_MAIN_FRAME; |
243 ResourceRequestInfo::AllocateForTesting( | 336 ResourceRequestInfo::AllocateForTesting(request.get(), request_resource_type, |
244 request.get(), | 337 nullptr, // context |
245 request_resource_type, | 338 0, // render_process_id |
246 nullptr, // context | 339 0, // render_view_id |
247 0, // render_process_id | 340 0, // render_frame_id |
248 0, // render_view_id | 341 is_main_frame, // is_main_frame |
249 0, // render_frame_id | 342 false, // parent_is_main_frame |
250 is_main_frame, // is_main_frame | 343 allow_download, // allow_download |
251 false, // parent_is_main_frame | 344 true, // is_async |
252 allow_download, // allow_download | 345 false); // is_using_lofi |
253 true, // is_async | |
254 false); // is_using_lofi | |
255 | 346 |
256 TestResourceDispatcherHost host(stream_has_handler_); | 347 TestResourceDispatcherHost host(stream_has_handler_); |
257 TestResourceDispatcherHostDelegate host_delegate(must_download); | 348 TestResourceDispatcherHostDelegate host_delegate(must_download); |
258 host.SetDelegate(&host_delegate); | 349 host.SetDelegate(&host_delegate); |
259 | 350 |
260 TestFakePluginService plugin_service(plugin_available_, plugin_stale_); | 351 TestFakePluginService plugin_service(plugin_available_, plugin_stale_); |
261 std::unique_ptr<ResourceHandler> mime_sniffing_handler( | 352 std::unique_ptr<InterceptingResourceHandler> intercepting_handler( |
262 new MimeTypeResourceHandler( | 353 new InterceptingResourceHandler(std::unique_ptr<ResourceHandler>(), |
263 std::unique_ptr<ResourceHandler>(new TestResourceHandler()), &host, | 354 nullptr)); |
264 &plugin_service, request.get())); | 355 std::unique_ptr<ResourceHandler> mime_handler(new MimeSniffingResourceHandler( |
356 std::unique_ptr<ResourceHandler>( | |
357 new TestResourceHandler(false, false, false, false, false)), | |
358 &host, &plugin_service, intercepting_handler.get(), request.get())); | |
359 | |
265 TestResourceController resource_controller; | 360 TestResourceController resource_controller; |
266 mime_sniffing_handler->SetController(&resource_controller); | 361 mime_handler->SetController(&resource_controller); |
267 | 362 |
268 scoped_refptr<ResourceResponse> response(new ResourceResponse); | 363 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
269 // The MIME type isn't important but it shouldn't be empty. | 364 // The MIME type isn't important but it shouldn't be empty. |
270 response->head.mime_type = "application/pdf"; | 365 response->head.mime_type = "application/pdf"; |
271 | 366 |
272 bool defer = false; | 367 bool defer = false; |
273 mime_sniffing_handler->OnResponseStarted(response.get(), &defer); | 368 mime_handler->OnResponseStarted(response.get(), &defer); |
274 | 369 |
275 content::RunAllPendingInMessageLoop(); | 370 content::RunAllPendingInMessageLoop(); |
276 EXPECT_LT(host.intercepted_as_stream_count(), 2); | 371 EXPECT_LT(host.intercepted_as_stream_count(), 2); |
372 if (allow_download) | |
373 EXPECT_TRUE(intercepting_handler->new_handler_for_testing()); | |
277 return host.intercepted_as_stream(); | 374 return host.intercepted_as_stream(); |
278 } | 375 } |
279 | 376 |
280 std::string MimeTypeResourceHandlerTest::TestAcceptHeaderSetting( | 377 void MimeSniffingResourceHandlerTest::TestHandlerSniffing( |
281 ResourceType request_resource_type) { | 378 bool response_started, |
379 bool defer_response_started, | |
380 bool will_read, | |
381 bool read_completed, | |
382 bool defer_read_completed) { | |
282 net::URLRequestContext context; | 383 net::URLRequestContext context; |
283 std::unique_ptr<net::URLRequest> request(context.CreateRequest( | 384 std::unique_ptr<net::URLRequest> request(context.CreateRequest( |
284 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); | 385 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); |
285 return TestAcceptHeaderSettingWithURLRequest( | 386 ResourceRequestInfo::AllocateForTesting(request.get(), |
286 request_resource_type, request.get()); | 387 RESOURCE_TYPE_MAIN_FRAME, |
287 } | 388 nullptr, // context |
288 | 389 0, // render_process_id |
289 std::string MimeTypeResourceHandlerTest::TestAcceptHeaderSettingWithURLRequest( | 390 0, // render_view_id |
290 ResourceType request_resource_type, | 391 0, // render_frame_id |
291 net::URLRequest* request) { | 392 true, // is_main_frame |
292 bool is_main_frame = request_resource_type == RESOURCE_TYPE_MAIN_FRAME; | 393 false, // parent_is_main_frame |
293 ResourceRequestInfo::AllocateForTesting( | 394 false, // allow_download |
294 request, | 395 true, // is_async |
295 request_resource_type, | 396 false); // is_using_lofi |
296 nullptr, // context | 397 |
297 0, // render_process_id | 398 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); | 399 TestResourceDispatcherHostDelegate host_delegate(false); |
308 host.SetDelegate(&host_delegate); | 400 host.SetDelegate(&host_delegate); |
309 | 401 |
310 std::unique_ptr<ResourceHandler> mime_sniffing_handler( | 402 TestFakePluginService plugin_service(plugin_available_, plugin_stale_); |
311 new MimeTypeResourceHandler( | 403 std::unique_ptr<ResourceHandler> intercepting_handler( |
312 std::unique_ptr<ResourceHandler>(new TestResourceHandler()), &host, | 404 new InterceptingResourceHandler(std::unique_ptr<ResourceHandler>(), |
313 nullptr, request)); | 405 nullptr)); |
314 | 406 std::unique_ptr<ResourceHandler> handler(new MimeSniffingResourceHandler( |
407 std::unique_ptr<ResourceHandler>(new TestResourceHandler( | |
408 response_started, defer_response_started, will_read, read_completed, | |
409 defer_read_completed)), | |
410 &host, &plugin_service, | |
411 static_cast<InterceptingResourceHandler*>(intercepting_handler.get()), | |
412 request.get())); | |
413 MimeSniffingResourceHandler* mime_sniffing_handler = | |
414 static_cast<MimeSniffingResourceHandler*>(handler.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 CHECK_EQ(MimeSniffingResourceHandler::STATE_STARTING, | |
424 mime_sniffing_handler->state_); | |
425 | |
426 // Simulate the response starting. We should start buffering, so the return | |
427 // value should always be true. | |
315 bool defer = false; | 428 bool defer = false; |
316 mime_sniffing_handler->OnWillStart(request->url(), &defer); | 429 CHECK_EQ(true, |
430 mime_sniffing_handler->OnResponseStarted(response.get(), &defer)); | |
431 CHECK_EQ(0, resource_controller.cancel_call_count()); | |
432 CHECK_EQ(0, resource_controller.resume_call_count()); | |
433 CHECK_EQ(false, defer); | |
434 CHECK_EQ(MimeSniffingResourceHandler::STATE_BUFFERING, | |
435 mime_sniffing_handler->state_); | |
436 | |
437 // Read some data to sniff the mime type. This will ask the next | |
438 // ResourceHandler for a buffer. | |
439 scoped_refptr<net::IOBuffer> read_buffer; | |
440 int buf_size = 0; | |
441 CHECK_EQ(will_read, | |
442 mime_sniffing_handler->OnWillRead(&read_buffer, &buf_size, -1)); | |
443 CHECK_EQ(0, resource_controller.cancel_call_count()); | |
444 CHECK_EQ(MimeSniffingResourceHandler::STATE_BUFFERING, | |
445 mime_sniffing_handler->state_); | |
446 | |
447 if (!will_read) { | |
448 content::RunAllPendingInMessageLoop(); | |
449 return; | |
450 } | |
451 | |
452 // Simulate an HTML page. The mime sniffer will identify the MimeType and | |
453 // proceed with replay. | |
454 char data[] = "!DOCTYPE html\n<head>\n<title>Foo</title>\n</head>"; | |
455 memcpy(read_buffer->data(), data, sizeof(data)); | |
456 | |
457 defer = false; | |
458 bool return_value = | |
459 mime_sniffing_handler->OnReadCompleted(sizeof(data), &defer); | |
460 | |
461 // If the next handler cancels the response start, we hsould be notified | |
462 // immediately. | |
463 if (!response_started) { | |
464 CHECK_EQ(response_started, return_value); | |
465 CHECK_EQ(0, resource_controller.cancel_call_count()); | |
466 content::RunAllPendingInMessageLoop(); | |
467 return; | |
468 } | |
469 | |
470 // The replay can be deferred both at response started and read replay | |
471 // stages. | |
472 CHECK_EQ(defer, defer_response_started || defer_read_completed); | |
473 if (defer_response_started) { | |
474 CHECK_EQ(true, defer); | |
475 CHECK_EQ(true, return_value); | |
476 CHECK_EQ(MimeSniffingResourceHandler::STATE_REPLAYING_RESPONSE_RECEIVED, | |
477 mime_sniffing_handler->state_); | |
478 mime_sniffing_handler->Resume(); | |
479 } | |
480 | |
481 // The body that was sniffed should be transmitted to the next handler. This | |
482 // may cancel the request. | |
483 if (!read_completed) { | |
484 if (defer_response_started) { | |
485 CHECK_EQ(1, resource_controller.cancel_call_count()); | |
486 } else { | |
487 CHECK_EQ(0, resource_controller.cancel_call_count()); | |
488 CHECK_EQ(false, return_value); | |
489 } | |
490 content::RunAllPendingInMessageLoop(); | |
491 return; | |
492 } | |
493 | |
494 CHECK_EQ(MimeSniffingResourceHandler::STATE_STREAMING, | |
495 mime_sniffing_handler->state_); | |
496 | |
497 // The request may be deferred by the next handler once the read is done. | |
498 if (defer_read_completed) { | |
499 CHECK_EQ(true, defer); | |
500 mime_sniffing_handler->Resume(); | |
501 } | |
502 | |
503 CHECK_EQ(MimeSniffingResourceHandler::STATE_STREAMING, | |
504 mime_sniffing_handler->state_); | |
505 CHECK_EQ(0, resource_controller.cancel_call_count()); | |
506 | |
507 // Even if the next handler defers the request twice, the | |
508 // MimeSniffingResourceHandler should only call Resume on its controller | |
509 // once. | |
510 if (defer_response_started || defer_read_completed) | |
511 CHECK_EQ(1, resource_controller.resume_call_count()); | |
512 else | |
513 CHECK_EQ(0, resource_controller.resume_call_count()); | |
514 | |
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<ResourceHandler> intercepting_handler( | |
mmenke
2016/07/25 17:39:32
Can make this an InterceptingResourceHandler and g
clamy
2016/07/26 16:24:15
Done.
| |
545 new InterceptingResourceHandler(std::unique_ptr<ResourceHandler>(), | |
546 nullptr)); | |
547 | |
548 std::unique_ptr<ResourceHandler> handler(new MimeSniffingResourceHandler( | |
mmenke
2016/07/25 17:39:32
Again, can make this a MimeSniffingResourceHandler
clamy
2016/07/26 16:24:15
Yes. I'm not sure why this was a std::unique_ptr<R
| |
549 std::unique_ptr<ResourceHandler>(new TestResourceHandler( | |
550 response_started, defer_response_started, will_read, read_completed, | |
551 defer_read_completed)), | |
552 &host, &plugin_service, | |
553 static_cast<InterceptingResourceHandler*>(intercepting_handler.get()), | |
554 request.get())); | |
555 MimeSniffingResourceHandler* mime_sniffing_handler = | |
556 static_cast<MimeSniffingResourceHandler*>(handler.get()); | |
557 | |
558 TestResourceController resource_controller; | |
559 mime_sniffing_handler->SetController(&resource_controller); | |
560 int expected_resume_calls = 0; | |
561 | |
562 // The response should not be sniffed. | |
563 scoped_refptr<ResourceResponse> response(new ResourceResponse); | |
564 response->head.mime_type.assign("text/html"); | |
mmenke
2016/07/25 17:39:32
I think this behavior is worth noting in the comme
clamy
2016/07/26 16:24:15
Done.
| |
565 | |
566 CHECK_EQ(MimeSniffingResourceHandler::STATE_STARTING, | |
567 mime_sniffing_handler->state_); | |
mmenke
2016/07/25 17:39:32
Do we really get anything out of testing the inter
clamy
2016/07/26 16:24:15
Done.
| |
568 | |
569 // Simulate the response starting. There should be no need for buffering, so | |
570 // the return value should be that of the next handler. | |
571 bool defer = false; | |
572 CHECK_EQ(response_started, | |
573 mime_sniffing_handler->OnResponseStarted(response.get(), &defer)); | |
574 CHECK_EQ(0, resource_controller.cancel_call_count()); | |
mmenke
2016/07/25 17:39:32
EXPECT_EQ (Goes for most of this function. Some c
clamy
2016/07/26 16:24:16
Done. I had put CHECKs since I remembered there we
| |
575 | |
576 if (!response_started) { | |
mmenke
2016/07/25 17:39:32
EXPECT_FALSE(defer)?
clamy
2016/07/26 16:24:16
Done.
| |
577 content::RunAllPendingInMessageLoop(); | |
mmenke
2016/07/25 17:39:31
Think these calls need comments as to why they're
clamy
2016/07/26 16:24:15
Done.
| |
578 return; | |
579 } | |
580 | |
581 CHECK_EQ(defer_response_started, defer); | |
582 if (defer) { | |
583 CHECK_EQ(MimeSniffingResourceHandler::STATE_REPLAYING_RESPONSE_RECEIVED, | |
584 mime_sniffing_handler->state_); | |
585 expected_resume_calls++; | |
586 mime_sniffing_handler->Resume(); | |
587 } | |
588 | |
589 CHECK_EQ(expected_resume_calls, resource_controller.resume_call_count()); | |
590 CHECK_EQ(MimeSniffingResourceHandler::STATE_STREAMING, | |
591 mime_sniffing_handler->state_); | |
592 | |
593 // The MimeSniffingResourceHandler should be acting as a pass-through | |
594 // ResourceHandler. | |
595 scoped_refptr<net::IOBuffer> read_buffer; | |
596 int buf_size = 0; | |
597 CHECK_EQ(will_read, | |
598 mime_sniffing_handler->OnWillRead(&read_buffer, &buf_size, -1)); | |
599 CHECK_EQ(0, resource_controller.cancel_call_count()); | |
600 CHECK_EQ(MimeSniffingResourceHandler::STATE_STREAMING, | |
601 mime_sniffing_handler->state_); | |
602 | |
603 if (!will_read) { | |
mmenke
2016/07/25 17:39:32
EXPECT_FALSE(defer)?
clamy
2016/07/26 16:24:15
We don't use defer in OnWillRead, so I added it to
| |
604 content::RunAllPendingInMessageLoop(); | |
605 return; | |
606 } | |
607 | |
608 defer = false; | |
609 CHECK_EQ(read_completed, | |
610 mime_sniffing_handler->OnReadCompleted(2000, &defer)); | |
611 CHECK_EQ(0, resource_controller.cancel_call_count()); | |
612 CHECK_EQ(MimeSniffingResourceHandler::STATE_STREAMING, | |
613 mime_sniffing_handler->state_); | |
614 | |
615 if (!read_completed) { | |
616 content::RunAllPendingInMessageLoop(); | |
617 return; | |
618 } | |
619 | |
620 CHECK_EQ(defer_read_completed, defer); | |
621 if (defer) { | |
622 expected_resume_calls++; | |
623 mime_sniffing_handler->Resume(); | |
624 } | |
625 CHECK_EQ(expected_resume_calls, resource_controller.resume_call_count()); | |
626 | |
627 content::RunAllPendingInMessageLoop(); | |
628 } | |
629 | |
324 // Test that the proper Accept: header is set based on the ResourceType | 630 // Test that the proper Accept: header is set based on the ResourceType |
325 TEST_F(MimeTypeResourceHandlerTest, AcceptHeaders) { | 631 TEST_F(MimeSniffingResourceHandlerTest, AcceptHeaders) { |
326 EXPECT_EQ( | 632 EXPECT_EQ( |
327 "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," |
328 "*/*;q=0.8", | 634 "*/*;q=0.8", |
329 TestAcceptHeaderSetting(RESOURCE_TYPE_MAIN_FRAME)); | 635 TestAcceptHeaderSetting(RESOURCE_TYPE_MAIN_FRAME)); |
330 EXPECT_EQ( | 636 EXPECT_EQ( |
331 "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp," | 637 "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp," |
332 "*/*;q=0.8", | 638 "*/*;q=0.8", |
333 TestAcceptHeaderSetting(RESOURCE_TYPE_SUB_FRAME)); | 639 TestAcceptHeaderSetting(RESOURCE_TYPE_SUB_FRAME)); |
334 EXPECT_EQ("text/css,*/*;q=0.1", | 640 EXPECT_EQ("text/css,*/*;q=0.1", |
335 TestAcceptHeaderSetting(RESOURCE_TYPE_STYLESHEET)); | 641 TestAcceptHeaderSetting(RESOURCE_TYPE_STYLESHEET)); |
336 EXPECT_EQ("*/*", | 642 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_SCRIPT)); |
337 TestAcceptHeaderSetting(RESOURCE_TYPE_SCRIPT)); | |
338 EXPECT_EQ("image/webp,image/*,*/*;q=0.8", | 643 EXPECT_EQ("image/webp,image/*,*/*;q=0.8", |
339 TestAcceptHeaderSetting(RESOURCE_TYPE_IMAGE)); | 644 TestAcceptHeaderSetting(RESOURCE_TYPE_IMAGE)); |
340 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_FONT_RESOURCE)); | 645 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_FONT_RESOURCE)); |
341 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_SUB_RESOURCE)); | 646 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_SUB_RESOURCE)); |
342 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_OBJECT)); | 647 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_OBJECT)); |
343 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_MEDIA)); | 648 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_MEDIA)); |
344 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_WORKER)); | 649 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_WORKER)); |
345 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_SHARED_WORKER)); | 650 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_SHARED_WORKER)); |
346 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_PREFETCH)); | 651 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_PREFETCH)); |
347 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_FAVICON)); | 652 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_FAVICON)); |
348 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_XHR)); | 653 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_XHR)); |
349 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_PING)); | 654 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_PING)); |
350 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_SERVICE_WORKER)); | 655 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_SERVICE_WORKER)); |
351 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_CSP_REPORT)); | 656 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_CSP_REPORT)); |
352 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_PLUGIN_RESOURCE)); | 657 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_PLUGIN_RESOURCE)); |
353 | 658 |
354 // Ensure that if an Accept header is already set, it is not overwritten. | 659 // Ensure that if an Accept header is already set, it is not overwritten. |
355 net::URLRequestContext context; | 660 net::URLRequestContext context; |
356 std::unique_ptr<net::URLRequest> request(context.CreateRequest( | 661 std::unique_ptr<net::URLRequest> request(context.CreateRequest( |
357 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); | 662 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); |
358 request->SetExtraRequestHeaderByName("Accept", "*", true); | 663 request->SetExtraRequestHeaderByName("Accept", "*", true); |
359 EXPECT_EQ("*", | 664 EXPECT_EQ("*", TestAcceptHeaderSettingWithURLRequest(RESOURCE_TYPE_XHR, |
360 TestAcceptHeaderSettingWithURLRequest(RESOURCE_TYPE_XHR, request.get())); | 665 request.get())); |
361 } | 666 } |
362 | 667 |
363 // Test that stream requests are correctly intercepted under the right | 668 // Test that stream requests are correctly intercepted under the right |
364 // circumstances. Test is not relevent when plugins are disabled. | 669 // circumstances. Test is not relevent when plugins are disabled. |
365 #if defined(ENABLE_PLUGINS) | 670 #if defined(ENABLE_PLUGINS) |
366 TEST_F(MimeTypeResourceHandlerTest, StreamHandling) { | 671 TEST_F(MimeSniffingResourceHandlerTest, StreamHandling) { |
367 bool allow_download; | 672 bool allow_download; |
368 bool must_download; | 673 bool must_download; |
369 ResourceType resource_type; | 674 ResourceType resource_type; |
370 | 675 |
371 // Ensure the stream is handled by MaybeInterceptAsStream in the | 676 // Ensure the stream is handled by MaybeInterceptAsStream in the |
372 // ResourceDispatcherHost. | 677 // ResourceDispatcherHost. |
373 set_stream_has_handler(true); | 678 set_stream_has_handler(true); |
374 set_plugin_available(true); | 679 set_plugin_available(true); |
375 | 680 |
376 // Main frame request with no download allowed. Stream shouldn't be | 681 // Main frame request with no download allowed. Stream shouldn't be |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
419 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | 724 TestStreamIsIntercepted(allow_download, must_download, resource_type)); |
420 | 725 |
421 // Test the cases where the stream isn't handled by MaybeInterceptAsStream | 726 // Test the cases where the stream isn't handled by MaybeInterceptAsStream |
422 // in the ResourceDispatcherHost. | 727 // in the ResourceDispatcherHost. |
423 set_stream_has_handler(false); | 728 set_stream_has_handler(false); |
424 allow_download = false; | 729 allow_download = false; |
425 must_download = false; | 730 must_download = false; |
426 resource_type = RESOURCE_TYPE_OBJECT; | 731 resource_type = RESOURCE_TYPE_OBJECT; |
427 EXPECT_FALSE( | 732 EXPECT_FALSE( |
428 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | 733 TestStreamIsIntercepted(allow_download, must_download, resource_type)); |
429 | 734 |
mmenke
2016/07/25 17:39:32
Why did you remove a test case here?
clamy
2016/07/26 16:24:15
That wasn't intentional. It's now back.
| |
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 | 735 // Test the cases where the stream handled by MaybeInterceptAsStream |
437 // with plugin not available. This is the case when intercepting streams for | 736 // with plugin not available. This is the case when intercepting streams for |
438 // the streamsPrivate extensions API. | 737 // the streamsPrivate extensions API. |
439 set_stream_has_handler(true); | 738 set_stream_has_handler(true); |
440 set_plugin_available(false); | 739 set_plugin_available(false); |
441 allow_download = false; | 740 allow_download = false; |
442 must_download = false; | 741 must_download = false; |
443 resource_type = RESOURCE_TYPE_OBJECT; | 742 resource_type = RESOURCE_TYPE_OBJECT; |
444 EXPECT_TRUE( | 743 EXPECT_TRUE( |
445 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | 744 TestStreamIsIntercepted(allow_download, must_download, resource_type)); |
446 | 745 |
447 // Test the cases where the stream handled by MaybeInterceptAsStream | 746 // Test the cases where the stream handled by MaybeInterceptAsStream |
448 // with plugin not available. This is the case when intercepting streams for | 747 // with plugin not available. This is the case when intercepting streams for |
449 // the streamsPrivate extensions API with stale plugin. | 748 // the streamsPrivate extensions API with stale plugin. |
450 set_plugin_stale(true); | 749 set_plugin_stale(true); |
451 allow_download = false; | 750 allow_download = false; |
452 must_download = false; | 751 must_download = false; |
453 resource_type = RESOURCE_TYPE_OBJECT; | 752 resource_type = RESOURCE_TYPE_OBJECT; |
454 EXPECT_TRUE( | 753 EXPECT_TRUE( |
455 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | 754 TestStreamIsIntercepted(allow_download, must_download, resource_type)); |
456 } | 755 } |
457 #endif | 756 #endif |
458 | 757 |
459 } // namespace | 758 // Test that the MimeSniffingHandler operates properly when it doesn't sniff |
759 // resources. | |
760 TEST_F(MimeSniffingResourceHandlerTest, NoSniffing) { | |
761 // Test simple case. | |
762 TestHandlerNoSniffing(true, false, true, true, false); | |
mmenke
2016/07/25 17:39:32
All of these argument need labels. As-is, it's ju
clamy
2016/07/26 16:24:16
Done.
| |
763 | |
764 // Test deferral in OnResponseStarted and/or in OnReadCompleted. | |
765 TestHandlerNoSniffing(true, true, true, true, false); | |
766 TestHandlerNoSniffing(true, false, true, true, true); | |
767 TestHandlerNoSniffing(true, true, true, true, true); | |
768 | |
769 // Test cancel in OnResponseStarted, OnWillRead, OnReadCompleted. | |
770 TestHandlerNoSniffing(false, false, false, false, false); | |
771 TestHandlerNoSniffing(true, false, false, false, false); | |
772 TestHandlerNoSniffing(true, false, true, false, false); | |
773 | |
774 // Test cancel after OnResponseStarted deferral. | |
775 TestHandlerNoSniffing(true, true, false, false, false); | |
776 TestHandlerNoSniffing(true, true, true, false, false); | |
777 | |
778 content::RunAllPendingInMessageLoop(); | |
779 } | |
780 | |
781 // Test that the MimeSniffingHandler operates properly when it sniffs | |
782 // resources. | |
783 TEST_F(MimeSniffingResourceHandlerTest, Sniffing) { | |
784 // Test simple case. | |
785 TestHandlerSniffing(true, false, true, true, false); | |
786 | |
787 // Test deferral in OnResponseStarted and/or in OnReadCompleted. | |
788 TestHandlerSniffing(true, true, true, true, false); | |
789 TestHandlerSniffing(true, false, true, true, true); | |
790 TestHandlerSniffing(true, true, true, true, true); | |
791 | |
792 // Test cancel in OnResponseStarted, OnWillRead, OnReadCompleted. | |
793 TestHandlerSniffing(false, false, false, false, false); | |
794 TestHandlerSniffing(true, false, false, false, false); | |
795 TestHandlerSniffing(true, false, true, false, false); | |
796 | |
797 // Test cancel after OnResponseStarted deferral. | |
798 TestHandlerSniffing(true, true, false, false, false); | |
799 TestHandlerSniffing(true, true, true, false, false); | |
800 | |
801 content::RunAllPendingInMessageLoop(); | |
802 } | |
803 | |
804 // Tests that 304s do not trigger a change in handlers. | |
805 TEST_F(MimeSniffingResourceHandlerTest, 304Handling) { | |
806 net::URLRequestContext context; | |
807 std::unique_ptr<net::URLRequest> request(context.CreateRequest( | |
808 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); | |
809 ResourceRequestInfo::AllocateForTesting(request.get(), | |
810 RESOURCE_TYPE_MAIN_FRAME, | |
811 nullptr, // context | |
812 0, // render_process_id | |
813 0, // render_view_id | |
814 0, // render_frame_id | |
815 true, // is_main_frame | |
816 false, // parent_is_main_frame | |
817 true, // allow_download | |
818 true, // is_async | |
819 false); // is_using_lofi | |
820 | |
821 TestResourceDispatcherHost host(false); | |
822 TestResourceDispatcherHostDelegate host_delegate(false); | |
823 host.SetDelegate(&host_delegate); | |
824 | |
825 TestFakePluginService plugin_service(false, false); | |
826 std::unique_ptr<ResourceHandler> intercepting_handler( | |
827 new InterceptingResourceHandler(std::unique_ptr<ResourceHandler>(), | |
828 nullptr)); | |
829 std::unique_ptr<ResourceHandler> mime_handler(new MimeSniffingResourceHandler( | |
830 std::unique_ptr<ResourceHandler>( | |
831 new TestResourceHandler(true, false, true, true, false)), | |
832 &host, &plugin_service, | |
833 static_cast<InterceptingResourceHandler*>(intercepting_handler.get()), | |
834 request.get())); | |
835 | |
836 TestResourceController resource_controller; | |
837 mime_handler->SetController(&resource_controller); | |
838 | |
839 // Simulate a 304 response. | |
840 scoped_refptr<ResourceResponse> response(new ResourceResponse); | |
841 // The MIME type isn't important but it shouldn't be empty. | |
842 response->head.mime_type = "application/pdf"; | |
843 response->head.headers = new net::HttpResponseHeaders("HTTP/1.x 304 OK"); | |
844 | |
845 // The response is received. No new ResourceHandler should be created to | |
846 // handle the download. | |
847 bool defer = false; | |
848 mime_handler->OnResponseStarted(response.get(), &defer); | |
849 EXPECT_FALSE(defer); | |
850 TestResourceHandler* new_handler = host.last_resource_handler(); | |
851 EXPECT_TRUE(!new_handler); | |
852 | |
853 content::RunAllPendingInMessageLoop(); | |
854 } | |
460 | 855 |
461 } // namespace content | 856 } // namespace content |
OLD | NEW |