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_type_resource_handler.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 27 matching lines...) Expand all Loading... |
38 bool* defer) override { | 38 bool* defer) override { |
39 NOTREACHED(); | 39 NOTREACHED(); |
40 return false; | 40 return false; |
41 } | 41 } |
42 | 42 |
43 bool OnResponseStarted(ResourceResponse* response, bool* defer) override { | 43 bool OnResponseStarted(ResourceResponse* response, bool* defer) override { |
44 return false; | 44 return false; |
45 } | 45 } |
46 | 46 |
47 bool OnWillStart(const GURL& url, bool* defer) override { | 47 bool OnWillStart(const GURL& url, bool* defer) override { |
48 NOTREACHED(); | |
49 return false; | 48 return false; |
50 } | 49 } |
51 | 50 |
52 bool OnBeforeNetworkStart(const GURL& url, bool* defer) override { | 51 bool OnBeforeNetworkStart(const GURL& url, bool* defer) override { |
53 NOTREACHED(); | 52 NOTREACHED(); |
54 return false; | 53 return false; |
55 } | 54 } |
56 | 55 |
57 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 56 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
58 int* buf_size, | 57 int* buf_size, |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 void set_plugin_available(bool plugin_available) { | 212 void set_plugin_available(bool plugin_available) { |
214 plugin_available_ = plugin_available; | 213 plugin_available_ = plugin_available; |
215 } | 214 } |
216 | 215 |
217 void set_plugin_stale(bool plugin_stale) { plugin_stale_ = plugin_stale; } | 216 void set_plugin_stale(bool plugin_stale) { plugin_stale_ = plugin_stale; } |
218 | 217 |
219 bool TestStreamIsIntercepted(bool allow_download, | 218 bool TestStreamIsIntercepted(bool allow_download, |
220 bool must_download, | 219 bool must_download, |
221 ResourceType request_resource_type); | 220 ResourceType request_resource_type); |
222 | 221 |
| 222 std::string TestAcceptHeaderSetting(ResourceType request_resource_type); |
| 223 std::string TestAcceptHeaderSettingWithURLRequest( |
| 224 ResourceType request_resource_type, |
| 225 net::URLRequest* request); |
| 226 |
223 private: | 227 private: |
224 // Whether the URL request should be intercepted as a stream. | 228 // Whether the URL request should be intercepted as a stream. |
225 bool stream_has_handler_; | 229 bool stream_has_handler_; |
226 bool plugin_available_; | 230 bool plugin_available_; |
227 bool plugin_stale_; | 231 bool plugin_stale_; |
228 | 232 |
229 TestBrowserThreadBundle thread_bundle_; | 233 TestBrowserThreadBundle thread_bundle_; |
230 }; | 234 }; |
231 | 235 |
232 bool MimeTypeResourceHandlerTest::TestStreamIsIntercepted( | 236 bool MimeTypeResourceHandlerTest::TestStreamIsIntercepted( |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 response->head.mime_type = "application/pdf"; | 270 response->head.mime_type = "application/pdf"; |
267 | 271 |
268 bool defer = false; | 272 bool defer = false; |
269 mime_sniffing_handler->OnResponseStarted(response.get(), &defer); | 273 mime_sniffing_handler->OnResponseStarted(response.get(), &defer); |
270 | 274 |
271 content::RunAllPendingInMessageLoop(); | 275 content::RunAllPendingInMessageLoop(); |
272 EXPECT_LT(host.intercepted_as_stream_count(), 2); | 276 EXPECT_LT(host.intercepted_as_stream_count(), 2); |
273 return host.intercepted_as_stream(); | 277 return host.intercepted_as_stream(); |
274 } | 278 } |
275 | 279 |
| 280 std::string MimeTypeResourceHandlerTest::TestAcceptHeaderSetting( |
| 281 ResourceType request_resource_type) { |
| 282 net::URLRequestContext context; |
| 283 scoped_ptr<net::URLRequest> request(context.CreateRequest( |
| 284 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); |
| 285 return TestAcceptHeaderSettingWithURLRequest( |
| 286 request_resource_type, request.get()); |
| 287 } |
| 288 |
| 289 std::string MimeTypeResourceHandlerTest::TestAcceptHeaderSettingWithURLRequest( |
| 290 ResourceType request_resource_type, |
| 291 net::URLRequest* request) { |
| 292 bool is_main_frame = request_resource_type == RESOURCE_TYPE_MAIN_FRAME; |
| 293 ResourceRequestInfo::AllocateForTesting( |
| 294 request, |
| 295 request_resource_type, |
| 296 nullptr, // context |
| 297 0, // render_process_id |
| 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); |
| 308 host.SetDelegate(&host_delegate); |
| 309 |
| 310 scoped_ptr<ResourceHandler> mime_sniffing_handler(new MimeTypeResourceHandler( |
| 311 scoped_ptr<ResourceHandler>(new TestResourceHandler()), &host, |
| 312 nullptr, request)); |
| 313 |
| 314 bool defer = false; |
| 315 mime_sniffing_handler->OnWillStart(request->url(), &defer); |
| 316 content::RunAllPendingInMessageLoop(); |
| 317 |
| 318 std::string accept_header; |
| 319 request->extra_request_headers().GetHeader("Accept", &accept_header); |
| 320 return accept_header; |
| 321 } |
| 322 |
| 323 // Test that the proper Accept: header is set based on the ResourceType |
| 324 TEST_F(MimeTypeResourceHandlerTest, AcceptHeaders) { |
| 325 EXPECT_EQ( |
| 326 "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp," |
| 327 "*/*;q=0.8", |
| 328 TestAcceptHeaderSetting(RESOURCE_TYPE_MAIN_FRAME)); |
| 329 EXPECT_EQ( |
| 330 "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp," |
| 331 "*/*;q=0.8", |
| 332 TestAcceptHeaderSetting(RESOURCE_TYPE_SUB_FRAME)); |
| 333 EXPECT_EQ("text/css,*/*;q=0.1", |
| 334 TestAcceptHeaderSetting(RESOURCE_TYPE_STYLESHEET)); |
| 335 EXPECT_EQ("*/*", |
| 336 TestAcceptHeaderSetting(RESOURCE_TYPE_SCRIPT)); |
| 337 EXPECT_EQ("image/webp,image/*,*/*;q=0.8", |
| 338 TestAcceptHeaderSetting(RESOURCE_TYPE_IMAGE)); |
| 339 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_FONT_RESOURCE)); |
| 340 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_SUB_RESOURCE)); |
| 341 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_OBJECT)); |
| 342 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_MEDIA)); |
| 343 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_WORKER)); |
| 344 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_SHARED_WORKER)); |
| 345 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_PREFETCH)); |
| 346 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_FAVICON)); |
| 347 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_XHR)); |
| 348 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_PING)); |
| 349 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_SERVICE_WORKER)); |
| 350 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_CSP_REPORT)); |
| 351 EXPECT_EQ("*/*", TestAcceptHeaderSetting(RESOURCE_TYPE_PLUGIN_RESOURCE)); |
| 352 |
| 353 // Ensure that if an Accept header is already set, it is not overwritten. |
| 354 net::URLRequestContext context; |
| 355 scoped_ptr<net::URLRequest> request(context.CreateRequest( |
| 356 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); |
| 357 request->SetExtraRequestHeaderByName("Accept", "*", true); |
| 358 EXPECT_EQ("*", |
| 359 TestAcceptHeaderSettingWithURLRequest(RESOURCE_TYPE_XHR, request.get())); |
| 360 } |
| 361 |
276 // Test that stream requests are correctly intercepted under the right | 362 // Test that stream requests are correctly intercepted under the right |
277 // circumstances. Test is not relevent when plugins are disabled. | 363 // circumstances. Test is not relevent when plugins are disabled. |
278 #if defined(ENABLE_PLUGINS) | 364 #if defined(ENABLE_PLUGINS) |
279 TEST_F(MimeTypeResourceHandlerTest, StreamHandling) { | 365 TEST_F(MimeTypeResourceHandlerTest, StreamHandling) { |
280 bool allow_download; | 366 bool allow_download; |
281 bool must_download; | 367 bool must_download; |
282 ResourceType resource_type; | 368 ResourceType resource_type; |
283 | 369 |
284 // Ensure the stream is handled by MaybeInterceptAsStream in the | 370 // Ensure the stream is handled by MaybeInterceptAsStream in the |
285 // ResourceDispatcherHost. | 371 // ResourceDispatcherHost. |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 must_download = false; | 451 must_download = false; |
366 resource_type = RESOURCE_TYPE_OBJECT; | 452 resource_type = RESOURCE_TYPE_OBJECT; |
367 EXPECT_TRUE( | 453 EXPECT_TRUE( |
368 TestStreamIsIntercepted(allow_download, must_download, resource_type)); | 454 TestStreamIsIntercepted(allow_download, must_download, resource_type)); |
369 } | 455 } |
370 #endif | 456 #endif |
371 | 457 |
372 } // namespace | 458 } // namespace |
373 | 459 |
374 } // namespace content | 460 } // namespace content |
OLD | NEW |