| 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_sniffing_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/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 16 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
| 17 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
| 18 #include "content/browser/loader/intercepting_resource_handler.h" | 18 #include "content/browser/loader/intercepting_resource_handler.h" |
| 19 #include "content/browser/loader/resource_controller.h" | 19 #include "content/browser/loader/mock_resource_loader.h" |
| 20 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 20 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 21 #include "content/browser/loader/test_resource_handler.h" | 21 #include "content/browser/loader/test_resource_handler.h" |
| 22 #include "content/public/browser/resource_dispatcher_host_delegate.h" | 22 #include "content/public/browser/resource_dispatcher_host_delegate.h" |
| 23 #include "content/public/browser/resource_request_info.h" | 23 #include "content/public/browser/resource_request_info.h" |
| 24 #include "content/public/common/resource_response.h" | 24 #include "content/public/common/resource_response.h" |
| 25 #include "content/public/common/webplugininfo.h" | 25 #include "content/public/common/webplugininfo.h" |
| 26 #include "content/public/test/test_browser_thread_bundle.h" | 26 #include "content/public/test/test_browser_thread_bundle.h" |
| 27 #include "content/public/test/test_utils.h" | 27 #include "content/public/test/test_utils.h" |
| 28 #include "content/test/fake_plugin_service.h" | 28 #include "content/test/fake_plugin_service.h" |
| 29 #include "net/url_request/url_request_context.h" | 29 #include "net/url_request/url_request_context.h" |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 FROM_HERE, base::Bind(callback, plugins)); | 146 FROM_HERE, base::Bind(callback, plugins)); |
| 147 } | 147 } |
| 148 | 148 |
| 149 private: | 149 private: |
| 150 const bool plugin_available_; | 150 const bool plugin_available_; |
| 151 bool is_plugin_stale_; | 151 bool is_plugin_stale_; |
| 152 | 152 |
| 153 DISALLOW_COPY_AND_ASSIGN(TestFakePluginService); | 153 DISALLOW_COPY_AND_ASSIGN(TestFakePluginService); |
| 154 }; | 154 }; |
| 155 | 155 |
| 156 class TestResourceController : public ResourceController { | |
| 157 public: | |
| 158 TestResourceController() : cancel_call_count_(0), resume_call_count_(0) {} | |
| 159 | |
| 160 void Cancel() override { cancel_call_count_++; } | |
| 161 | |
| 162 void CancelAndIgnore() override { NOTREACHED(); } | |
| 163 | |
| 164 void CancelWithError(int error_code) override { NOTREACHED(); } | |
| 165 | |
| 166 void Resume() override { resume_call_count_++; } | |
| 167 | |
| 168 int cancel_call_count() const { return cancel_call_count_; } | |
| 169 int resume_call_count() const { return resume_call_count_; } | |
| 170 | |
| 171 private: | |
| 172 int cancel_call_count_; | |
| 173 int resume_call_count_; | |
| 174 }; | |
| 175 | |
| 176 } // namespace | 156 } // namespace |
| 177 | 157 |
| 178 class MimeSniffingResourceHandlerTest : public testing::Test { | 158 class MimeSniffingResourceHandlerTest : public testing::Test { |
| 179 public: | 159 public: |
| 180 MimeSniffingResourceHandlerTest() | 160 MimeSniffingResourceHandlerTest() |
| 181 : stream_has_handler_(false), | 161 : stream_has_handler_(false), |
| 182 plugin_available_(false), | 162 plugin_available_(false), |
| 183 plugin_stale_(false) {} | 163 plugin_stale_(false) {} |
| 184 | 164 |
| 185 // Tests that the MimeSniffingHandler properly sets the accept field in the | 165 // Tests that the MimeSniffingHandler properly sets the accept field in the |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 is_main_frame, // is_main_frame | 230 is_main_frame, // is_main_frame |
| 251 false, // parent_is_main_frame | 231 false, // parent_is_main_frame |
| 252 false, // allow_download | 232 false, // allow_download |
| 253 true, // is_async | 233 true, // is_async |
| 254 false); // is_using_lofi | 234 false); // is_using_lofi |
| 255 | 235 |
| 256 std::unique_ptr<TestResourceHandler> scoped_test_handler( | 236 std::unique_ptr<TestResourceHandler> scoped_test_handler( |
| 257 new TestResourceHandler()); | 237 new TestResourceHandler()); |
| 258 scoped_test_handler->set_on_response_started_result(false); | 238 scoped_test_handler->set_on_response_started_result(false); |
| 259 | 239 |
| 260 std::unique_ptr<ResourceHandler> mime_sniffing_handler( | 240 MimeSniffingResourceHandler mime_sniffing_handler( |
| 261 new MimeSniffingResourceHandler(std::move(scoped_test_handler), nullptr, | 241 std::move(scoped_test_handler), nullptr, nullptr, nullptr, request, |
| 262 nullptr, nullptr, request, | 242 REQUEST_CONTEXT_TYPE_UNSPECIFIED); |
| 263 REQUEST_CONTEXT_TYPE_UNSPECIFIED)); | 243 MockResourceLoader mock_loader(&mime_sniffing_handler); |
| 264 | 244 |
| 265 bool defer = false; | 245 EXPECT_EQ(MockResourceLoader::Status::IDLE, |
| 266 mime_sniffing_handler->OnWillStart(request->url(), &defer); | 246 mock_loader.OnWillStart(request->url())); |
| 267 content::RunAllPendingInMessageLoop(); | |
| 268 | 247 |
| 269 std::string accept_header; | 248 std::string accept_header; |
| 270 request->extra_request_headers().GetHeader("Accept", &accept_header); | 249 request->extra_request_headers().GetHeader("Accept", &accept_header); |
| 271 return accept_header; | 250 return accept_header; |
| 272 } | 251 } |
| 273 | 252 |
| 274 bool MimeSniffingResourceHandlerTest::TestStreamIsIntercepted( | 253 bool MimeSniffingResourceHandlerTest::TestStreamIsIntercepted( |
| 275 bool allow_download, | 254 bool allow_download, |
| 276 bool must_download, | 255 bool must_download, |
| 277 ResourceType request_resource_type) { | 256 ResourceType request_resource_type) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 295 host.SetDelegate(&host_delegate); | 274 host.SetDelegate(&host_delegate); |
| 296 | 275 |
| 297 TestFakePluginService plugin_service(plugin_available_, plugin_stale_); | 276 TestFakePluginService plugin_service(plugin_available_, plugin_stale_); |
| 298 | 277 |
| 299 std::unique_ptr<InterceptingResourceHandler> intercepting_handler( | 278 std::unique_ptr<InterceptingResourceHandler> intercepting_handler( |
| 300 new InterceptingResourceHandler(base::MakeUnique<TestResourceHandler>(), | 279 new InterceptingResourceHandler(base::MakeUnique<TestResourceHandler>(), |
| 301 nullptr)); | 280 nullptr)); |
| 302 std::unique_ptr<TestResourceHandler> scoped_test_handler( | 281 std::unique_ptr<TestResourceHandler> scoped_test_handler( |
| 303 new TestResourceHandler()); | 282 new TestResourceHandler()); |
| 304 scoped_test_handler->set_on_response_started_result(false); | 283 scoped_test_handler->set_on_response_started_result(false); |
| 305 std::unique_ptr<ResourceHandler> mime_handler(new MimeSniffingResourceHandler( | 284 MimeSniffingResourceHandler mime_sniffing_handler( |
| 306 std::unique_ptr<ResourceHandler>(std::move(scoped_test_handler)), &host, | 285 std::unique_ptr<ResourceHandler>(std::move(scoped_test_handler)), &host, |
| 307 &plugin_service, intercepting_handler.get(), request.get(), | 286 &plugin_service, intercepting_handler.get(), request.get(), |
| 308 REQUEST_CONTEXT_TYPE_UNSPECIFIED)); | 287 REQUEST_CONTEXT_TYPE_UNSPECIFIED); |
| 309 | 288 |
| 310 TestResourceController resource_controller; | 289 MockResourceLoader mock_loader(&mime_sniffing_handler); |
| 311 mime_handler->SetController(&resource_controller); | |
| 312 | 290 |
| 313 scoped_refptr<ResourceResponse> response(new ResourceResponse); | 291 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
| 314 // The MIME type isn't important but it shouldn't be empty. | 292 // The MIME type isn't important but it shouldn't be empty. |
| 315 response->head.mime_type = "application/pdf"; | 293 response->head.mime_type = "application/pdf"; |
| 316 | 294 |
| 317 bool defer = false; | 295 EXPECT_EQ(MockResourceLoader::Status::IDLE, |
| 318 mime_handler->OnWillStart(request->url(), &defer); | 296 mock_loader.OnWillStart(request->url())); |
| 319 EXPECT_FALSE(defer); | |
| 320 | 297 |
| 321 mime_handler->OnResponseStarted(response.get(), &defer); | 298 mock_loader.OnResponseStarted(response); |
| 299 mock_loader.WaitUntilIdleOrCanceled(); |
| 300 EXPECT_EQ(MockResourceLoader::Status::CANCELED, mock_loader.status()); |
| 301 EXPECT_EQ(net::ERR_ABORTED, mock_loader.error_code()); |
| 322 | 302 |
| 323 content::RunAllPendingInMessageLoop(); | |
| 324 EXPECT_LT(host.intercepted_as_stream_count(), 2); | 303 EXPECT_LT(host.intercepted_as_stream_count(), 2); |
| 325 if (allow_download) | 304 if (allow_download) |
| 326 EXPECT_TRUE(intercepting_handler->new_handler_for_testing()); | 305 EXPECT_TRUE(intercepting_handler->new_handler_for_testing()); |
| 327 return host.intercepted_as_stream(); | 306 return host.intercepted_as_stream(); |
| 328 } | 307 } |
| 329 | 308 |
| 330 void MimeSniffingResourceHandlerTest::TestHandlerSniffing( | 309 void MimeSniffingResourceHandlerTest::TestHandlerSniffing( |
| 331 bool response_started, | 310 bool response_started, |
| 332 bool defer_response_started, | 311 bool defer_response_started, |
| 333 bool will_read, | 312 bool will_read, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 358 nullptr)); | 337 nullptr)); |
| 359 | 338 |
| 360 std::unique_ptr<TestResourceHandler> scoped_test_handler( | 339 std::unique_ptr<TestResourceHandler> scoped_test_handler( |
| 361 new TestResourceHandler()); | 340 new TestResourceHandler()); |
| 362 scoped_test_handler->set_on_response_started_result(response_started); | 341 scoped_test_handler->set_on_response_started_result(response_started); |
| 363 scoped_test_handler->set_defer_on_response_started(defer_response_started); | 342 scoped_test_handler->set_defer_on_response_started(defer_response_started); |
| 364 scoped_test_handler->set_on_will_read_result(will_read); | 343 scoped_test_handler->set_on_will_read_result(will_read); |
| 365 scoped_test_handler->set_on_read_completed_result(read_completed); | 344 scoped_test_handler->set_on_read_completed_result(read_completed); |
| 366 scoped_test_handler->set_defer_on_read_completed(defer_read_completed); | 345 scoped_test_handler->set_defer_on_read_completed(defer_read_completed); |
| 367 TestResourceHandler* test_handler = scoped_test_handler.get(); | 346 TestResourceHandler* test_handler = scoped_test_handler.get(); |
| 368 std::unique_ptr<MimeSniffingResourceHandler> mime_sniffing_handler( | 347 MimeSniffingResourceHandler mime_sniffing_handler( |
| 369 new MimeSniffingResourceHandler(std::move(scoped_test_handler), &host, | 348 std::move(scoped_test_handler), &host, &plugin_service, |
| 370 &plugin_service, | 349 intercepting_handler.get(), request.get(), |
| 371 intercepting_handler.get(), request.get(), | 350 REQUEST_CONTEXT_TYPE_UNSPECIFIED); |
| 372 REQUEST_CONTEXT_TYPE_UNSPECIFIED)); | |
| 373 | 351 |
| 374 TestResourceController resource_controller; | 352 MockResourceLoader mock_loader(&mime_sniffing_handler); |
| 375 mime_sniffing_handler->SetController(&resource_controller); | |
| 376 | 353 |
| 377 bool defer = false; | 354 ASSERT_EQ(MockResourceLoader::Status::IDLE, |
| 378 mime_sniffing_handler->OnWillStart(GURL(), &defer); | 355 mock_loader.OnWillStart(request->url())); |
| 379 | 356 |
| 380 // The response should be sniffed. | 357 // The response should be sniffed. |
| 381 scoped_refptr<ResourceResponse> response(new ResourceResponse); | 358 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
| 382 response->head.mime_type.assign("text/plain"); | 359 response->head.mime_type.assign("text/plain"); |
| 383 | 360 |
| 384 // Simulate the response starting. The MimeSniffingHandler should start | 361 // Simulate the response starting. The MimeSniffingHandler should start |
| 385 // buffering, so the return value should always be true. | 362 // buffering, so the return value should always be true. |
| 386 EXPECT_TRUE(mime_sniffing_handler->OnResponseStarted(response.get(), &defer)); | 363 ASSERT_EQ(MockResourceLoader::Status::IDLE, |
| 387 EXPECT_EQ(0, resource_controller.cancel_call_count()); | 364 mock_loader.OnResponseStarted(response.get())); |
| 388 EXPECT_EQ(0, resource_controller.resume_call_count()); | |
| 389 EXPECT_FALSE(defer); | |
| 390 | 365 |
| 391 // Read some data to sniff the mime type. This will ask the next | 366 // Read some data to sniff the mime type. This will ask the next |
| 392 // ResourceHandler for a buffer. | 367 // ResourceHandler for a buffer. |
| 393 scoped_refptr<net::IOBuffer> read_buffer; | 368 mock_loader.OnWillRead(-1); |
| 394 int buf_size = 0; | |
| 395 EXPECT_EQ(will_read, | |
| 396 mime_sniffing_handler->OnWillRead(&read_buffer, &buf_size, -1)); | |
| 397 EXPECT_EQ(0, resource_controller.cancel_call_count()); | |
| 398 | 369 |
| 399 if (!will_read) { | 370 if (!will_read) { |
| 371 EXPECT_EQ(MockResourceLoader::Status::CANCELED, mock_loader.status()); |
| 372 EXPECT_EQ(net::ERR_ABORTED, mock_loader.error_code()); |
| 373 |
| 400 EXPECT_EQ(1, test_handler->on_will_start_called()); | 374 EXPECT_EQ(1, test_handler->on_will_start_called()); |
| 401 EXPECT_EQ(0, test_handler->on_request_redirected_called()); | 375 EXPECT_EQ(0, test_handler->on_request_redirected_called()); |
| 402 EXPECT_EQ(0, test_handler->on_response_started_called()); | 376 EXPECT_EQ(0, test_handler->on_response_started_called()); |
| 403 EXPECT_EQ(1, test_handler->on_will_read_called()); | 377 EXPECT_EQ(1, test_handler->on_will_read_called()); |
| 404 EXPECT_EQ(0, test_handler->on_read_completed_called()); | 378 EXPECT_EQ(0, test_handler->on_read_completed_called()); |
| 405 | 379 |
| 406 // Process all messages to ensure proper test teardown. | 380 // Process all messages to ensure proper test teardown. |
| 407 content::RunAllPendingInMessageLoop(); | 381 content::RunAllPendingInMessageLoop(); |
| 408 return; | 382 return; |
| 409 } | 383 } |
| 410 | 384 |
| 385 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader.status()); |
| 386 |
| 411 // Simulate an HTML page. The mime sniffer will identify the MimeType and | 387 // Simulate an HTML page. The mime sniffer will identify the MimeType and |
| 412 // proceed with replay. | 388 // proceed with replay. |
| 413 char data[] = "!DOCTYPE html\n<head>\n<title>Foo</title>\n</head>"; | 389 const char kData[] = "!DOCTYPE html\n<head>\n<title>Foo</title>\n</head>"; |
| 414 memcpy(read_buffer->data(), data, sizeof(data)); | 390 // Construct StringPiece manually, as the terminal null needs to be included, |
| 415 | 391 // so it's sniffed as binary (Not important that it's sniffed as binary, but |
| 416 defer = false; | 392 // this gaurantees it's sniffed as something, without waiting for more data). |
| 417 bool return_value = | 393 mock_loader.OnReadCompleted(base::StringPiece(kData, sizeof(kData))); |
| 418 mime_sniffing_handler->OnReadCompleted(sizeof(data), &defer); | |
| 419 | 394 |
| 420 // If the next handler cancels the response start, the caller of | 395 // If the next handler cancels the response start, the caller of |
| 421 // MimeSniffingHandler::OnReadCompleted should be notified immediately. | 396 // MimeSniffingHandler::OnReadCompleted should be notified immediately. |
| 422 if (!response_started) { | 397 if (!response_started) { |
| 423 EXPECT_FALSE(defer); | 398 EXPECT_EQ(MockResourceLoader::Status::CANCELED, mock_loader.status()); |
| 424 EXPECT_EQ(response_started, return_value); | 399 EXPECT_EQ(net::ERR_ABORTED, mock_loader.error_code()); |
| 425 EXPECT_EQ(0, resource_controller.cancel_call_count()); | |
| 426 | 400 |
| 427 EXPECT_EQ(1, test_handler->on_will_start_called()); | 401 EXPECT_EQ(1, test_handler->on_will_start_called()); |
| 428 EXPECT_EQ(0, test_handler->on_request_redirected_called()); | 402 EXPECT_EQ(0, test_handler->on_request_redirected_called()); |
| 429 EXPECT_EQ(1, test_handler->on_response_started_called()); | 403 EXPECT_EQ(1, test_handler->on_response_started_called()); |
| 430 EXPECT_EQ(1, test_handler->on_will_read_called()); | 404 EXPECT_EQ(1, test_handler->on_will_read_called()); |
| 431 EXPECT_EQ(0, test_handler->on_read_completed_called()); | 405 EXPECT_EQ(0, test_handler->on_read_completed_called()); |
| 432 | 406 |
| 433 // Process all messages to ensure proper test teardown. | 407 // Process all messages to ensure proper test teardown. |
| 434 content::RunAllPendingInMessageLoop(); | 408 content::RunAllPendingInMessageLoop(); |
| 435 return; | 409 return; |
| 436 } | 410 } |
| 437 | 411 |
| 438 // The replay can be deferred both at response started and read replay | |
| 439 // stages. | |
| 440 EXPECT_EQ(defer, defer_response_started || defer_read_completed); | |
| 441 if (defer_response_started) { | 412 if (defer_response_started) { |
| 442 EXPECT_TRUE(defer); | 413 ASSERT_EQ(MockResourceLoader::Status::CALLBACK_PENDING, |
| 443 EXPECT_TRUE(return_value); | 414 mock_loader.status()); |
| 444 EXPECT_EQ(MimeSniffingResourceHandler::STATE_REPLAYING_RESPONSE_RECEIVED, | 415 EXPECT_EQ(MimeSniffingResourceHandler::STATE_REPLAYING_RESPONSE_RECEIVED, |
| 445 mime_sniffing_handler->state_); | 416 mime_sniffing_handler.state_); |
| 446 mime_sniffing_handler->Resume(); | 417 mime_sniffing_handler.Resume(); |
| 447 } | 418 } |
| 448 | 419 |
| 449 // The body that was sniffed should be transmitted to the next handler. This | 420 // The body that was sniffed should be transmitted to the next handler. This |
| 450 // may cancel the request. | 421 // may cancel the request. |
| 451 if (!read_completed) { | 422 if (!read_completed) { |
| 452 if (defer_response_started) { | 423 EXPECT_EQ(MockResourceLoader::Status::CANCELED, mock_loader.status()); |
| 453 EXPECT_EQ(1, resource_controller.cancel_call_count()); | 424 EXPECT_EQ(net::ERR_ABORTED, mock_loader.error_code()); |
| 454 } else { | 425 |
| 455 EXPECT_EQ(0, resource_controller.cancel_call_count()); | |
| 456 EXPECT_FALSE(return_value); | |
| 457 } | |
| 458 // Process all messages to ensure proper test teardown. | 426 // Process all messages to ensure proper test teardown. |
| 459 content::RunAllPendingInMessageLoop(); | 427 content::RunAllPendingInMessageLoop(); |
| 460 return; | 428 return; |
| 461 } | 429 } |
| 462 | 430 |
| 463 EXPECT_EQ(MimeSniffingResourceHandler::STATE_STREAMING, | 431 EXPECT_EQ(MimeSniffingResourceHandler::STATE_STREAMING, |
| 464 mime_sniffing_handler->state_); | 432 mime_sniffing_handler.state_); |
| 465 | 433 |
| 466 // The request may be deferred by the next handler once the read is done. | 434 // The request may be deferred by the next handler once the read is done. |
| 467 if (defer_read_completed) { | 435 if (defer_read_completed) { |
| 468 EXPECT_TRUE(defer); | 436 ASSERT_EQ(MockResourceLoader::Status::CALLBACK_PENDING, |
| 469 mime_sniffing_handler->Resume(); | 437 mock_loader.status()); |
| 438 mime_sniffing_handler.Resume(); |
| 470 } | 439 } |
| 471 | 440 |
| 441 EXPECT_EQ(MockResourceLoader::Status::IDLE, mock_loader.status()); |
| 472 EXPECT_EQ(MimeSniffingResourceHandler::STATE_STREAMING, | 442 EXPECT_EQ(MimeSniffingResourceHandler::STATE_STREAMING, |
| 473 mime_sniffing_handler->state_); | 443 mime_sniffing_handler.state_); |
| 474 EXPECT_EQ(0, resource_controller.cancel_call_count()); | |
| 475 | |
| 476 // Even if the next handler defers the request twice, the | |
| 477 // MimeSniffingResourceHandler should only call Resume on its controller | |
| 478 // once. | |
| 479 if (defer_response_started || defer_read_completed) { | |
| 480 EXPECT_EQ(1, resource_controller.resume_call_count()); | |
| 481 } else { | |
| 482 EXPECT_EQ(0, resource_controller.resume_call_count()); | |
| 483 } | |
| 484 | 444 |
| 485 EXPECT_EQ(1, test_handler->on_will_start_called()); | 445 EXPECT_EQ(1, test_handler->on_will_start_called()); |
| 486 EXPECT_EQ(0, test_handler->on_request_redirected_called()); | 446 EXPECT_EQ(0, test_handler->on_request_redirected_called()); |
| 487 EXPECT_EQ(1, test_handler->on_response_started_called()); | 447 EXPECT_EQ(1, test_handler->on_response_started_called()); |
| 488 EXPECT_EQ(1, test_handler->on_will_read_called()); | 448 EXPECT_EQ(1, test_handler->on_will_read_called()); |
| 489 EXPECT_EQ(1, test_handler->on_read_completed_called()); | 449 EXPECT_EQ(1, test_handler->on_read_completed_called()); |
| 490 | 450 |
| 491 // Process all messages to ensure proper test teardown. | 451 // Process all messages to ensure proper test teardown. |
| 492 content::RunAllPendingInMessageLoop(); | 452 content::RunAllPendingInMessageLoop(); |
| 493 } | 453 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 523 nullptr)); | 483 nullptr)); |
| 524 | 484 |
| 525 std::unique_ptr<TestResourceHandler> scoped_test_handler( | 485 std::unique_ptr<TestResourceHandler> scoped_test_handler( |
| 526 new TestResourceHandler()); | 486 new TestResourceHandler()); |
| 527 scoped_test_handler->set_on_response_started_result(response_started); | 487 scoped_test_handler->set_on_response_started_result(response_started); |
| 528 scoped_test_handler->set_defer_on_response_started(defer_response_started); | 488 scoped_test_handler->set_defer_on_response_started(defer_response_started); |
| 529 scoped_test_handler->set_on_will_read_result(will_read); | 489 scoped_test_handler->set_on_will_read_result(will_read); |
| 530 scoped_test_handler->set_on_read_completed_result(read_completed); | 490 scoped_test_handler->set_on_read_completed_result(read_completed); |
| 531 scoped_test_handler->set_defer_on_read_completed(defer_read_completed); | 491 scoped_test_handler->set_defer_on_read_completed(defer_read_completed); |
| 532 TestResourceHandler* test_handler = scoped_test_handler.get(); | 492 TestResourceHandler* test_handler = scoped_test_handler.get(); |
| 533 std::unique_ptr<MimeSniffingResourceHandler> mime_sniffing_handler( | 493 MimeSniffingResourceHandler mime_sniffing_handler( |
| 534 new MimeSniffingResourceHandler(std::move(scoped_test_handler), &host, | 494 std::move(scoped_test_handler), &host, &plugin_service, |
| 535 &plugin_service, | 495 intercepting_handler.get(), request.get(), |
| 536 intercepting_handler.get(), request.get(), | 496 REQUEST_CONTEXT_TYPE_UNSPECIFIED); |
| 537 REQUEST_CONTEXT_TYPE_UNSPECIFIED)); | |
| 538 | 497 |
| 539 TestResourceController resource_controller; | 498 MockResourceLoader mock_loader(&mime_sniffing_handler); |
| 540 mime_sniffing_handler->SetController(&resource_controller); | |
| 541 | 499 |
| 542 int expected_resume_calls = 0; | 500 ASSERT_EQ(MockResourceLoader::Status::IDLE, |
| 543 | 501 mock_loader.OnWillStart(request->url())); |
| 544 bool defer = false; | |
| 545 mime_sniffing_handler->OnWillStart(GURL(), &defer); | |
| 546 | 502 |
| 547 // The response should not be sniffed. | 503 // The response should not be sniffed. |
| 548 scoped_refptr<ResourceResponse> response(new ResourceResponse); | 504 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
| 549 response->head.mime_type.assign("text/html"); | 505 response->head.mime_type.assign("text/html"); |
| 550 | 506 |
| 551 // Simulate the response starting. There should be no need for buffering, so | 507 // Simulate the response starting. There should be no need for buffering, so |
| 552 // the return value should be that of the next handler. | 508 // the return value should be that of the next handler. |
| 553 EXPECT_EQ(response_started, | 509 mock_loader.OnResponseStarted(response); |
| 554 mime_sniffing_handler->OnResponseStarted(response.get(), &defer)); | |
| 555 EXPECT_EQ(0, resource_controller.cancel_call_count()); | |
| 556 | 510 |
| 557 if (!response_started) { | 511 if (!response_started) { |
| 558 EXPECT_FALSE(defer); | 512 EXPECT_EQ(MockResourceLoader::Status::CANCELED, mock_loader.status()); |
| 513 EXPECT_EQ(net::ERR_ABORTED, mock_loader.error_code()); |
| 559 | 514 |
| 560 EXPECT_EQ(1, test_handler->on_will_start_called()); | 515 EXPECT_EQ(1, test_handler->on_will_start_called()); |
| 561 EXPECT_EQ(0, test_handler->on_request_redirected_called()); | 516 EXPECT_EQ(0, test_handler->on_request_redirected_called()); |
| 562 EXPECT_EQ(1, test_handler->on_response_started_called()); | 517 EXPECT_EQ(1, test_handler->on_response_started_called()); |
| 563 EXPECT_EQ(0, test_handler->on_will_read_called()); | 518 EXPECT_EQ(0, test_handler->on_will_read_called()); |
| 564 EXPECT_EQ(0, test_handler->on_read_completed_called()); | 519 EXPECT_EQ(0, test_handler->on_read_completed_called()); |
| 565 | 520 |
| 566 // Process all messages to ensure proper test teardown. | 521 // Process all messages to ensure proper test teardown. |
| 567 content::RunAllPendingInMessageLoop(); | 522 content::RunAllPendingInMessageLoop(); |
| 568 return; | 523 return; |
| 569 } | 524 } |
| 570 | 525 |
| 571 EXPECT_EQ(defer_response_started, defer); | 526 if (defer_response_started) { |
| 572 if (defer) { | 527 ASSERT_EQ(MockResourceLoader::Status::CALLBACK_PENDING, |
| 528 mock_loader.status()); |
| 573 EXPECT_EQ(MimeSniffingResourceHandler::STATE_REPLAYING_RESPONSE_RECEIVED, | 529 EXPECT_EQ(MimeSniffingResourceHandler::STATE_REPLAYING_RESPONSE_RECEIVED, |
| 574 mime_sniffing_handler->state_); | 530 mime_sniffing_handler.state_); |
| 575 expected_resume_calls++; | 531 mime_sniffing_handler.Resume(); |
| 576 mime_sniffing_handler->Resume(); | |
| 577 } | 532 } |
| 578 | 533 |
| 579 EXPECT_EQ(expected_resume_calls, resource_controller.resume_call_count()); | 534 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader.status()); |
| 580 | 535 |
| 581 // The MimeSniffingResourceHandler should be acting as a pass-through | 536 // The MimeSniffingResourceHandler should be acting as a pass-through |
| 582 // ResourceHandler. | 537 // ResourceHandler. |
| 583 scoped_refptr<net::IOBuffer> read_buffer; | 538 mock_loader.OnWillRead(-1); |
| 584 int buf_size = 0; | |
| 585 EXPECT_EQ(will_read, | |
| 586 mime_sniffing_handler->OnWillRead(&read_buffer, &buf_size, -1)); | |
| 587 EXPECT_EQ(0, resource_controller.cancel_call_count()); | |
| 588 | 539 |
| 589 if (!will_read) { | 540 if (!will_read) { |
| 541 EXPECT_EQ(MockResourceLoader::Status::CANCELED, mock_loader.status()); |
| 542 EXPECT_EQ(net::ERR_ABORTED, mock_loader.error_code()); |
| 543 |
| 590 EXPECT_EQ(1, test_handler->on_will_start_called()); | 544 EXPECT_EQ(1, test_handler->on_will_start_called()); |
| 591 EXPECT_EQ(0, test_handler->on_request_redirected_called()); | 545 EXPECT_EQ(0, test_handler->on_request_redirected_called()); |
| 592 EXPECT_EQ(1, test_handler->on_response_started_called()); | 546 EXPECT_EQ(1, test_handler->on_response_started_called()); |
| 593 EXPECT_EQ(1, test_handler->on_will_read_called()); | 547 EXPECT_EQ(1, test_handler->on_will_read_called()); |
| 594 EXPECT_EQ(0, test_handler->on_read_completed_called()); | 548 EXPECT_EQ(0, test_handler->on_read_completed_called()); |
| 595 | 549 |
| 596 // Process all messages to ensure proper test teardown. | 550 // Process all messages to ensure proper test teardown. |
| 597 content::RunAllPendingInMessageLoop(); | 551 content::RunAllPendingInMessageLoop(); |
| 598 return; | 552 return; |
| 599 } | 553 } |
| 600 | 554 |
| 601 defer = false; | 555 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader.status()); |
| 602 EXPECT_EQ(read_completed, | 556 |
| 603 mime_sniffing_handler->OnReadCompleted(2000, &defer)); | 557 mock_loader.OnReadCompleted(std::string(2000, 'a')); |
| 604 EXPECT_EQ(0, resource_controller.cancel_call_count()); | |
| 605 | 558 |
| 606 EXPECT_EQ(1, test_handler->on_will_start_called()); | 559 EXPECT_EQ(1, test_handler->on_will_start_called()); |
| 607 EXPECT_EQ(0, test_handler->on_request_redirected_called()); | 560 EXPECT_EQ(0, test_handler->on_request_redirected_called()); |
| 608 EXPECT_EQ(1, test_handler->on_response_started_called()); | 561 EXPECT_EQ(1, test_handler->on_response_started_called()); |
| 609 EXPECT_EQ(1, test_handler->on_will_read_called()); | 562 EXPECT_EQ(1, test_handler->on_will_read_called()); |
| 610 EXPECT_EQ(1, test_handler->on_read_completed_called()); | 563 EXPECT_EQ(1, test_handler->on_read_completed_called()); |
| 611 | 564 |
| 612 if (!read_completed) { | 565 if (!read_completed) { |
| 613 EXPECT_FALSE(defer); | 566 EXPECT_EQ(MockResourceLoader::Status::CANCELED, mock_loader.status()); |
| 567 EXPECT_EQ(net::ERR_ABORTED, mock_loader.error_code()); |
| 614 | 568 |
| 615 // Process all messages to ensure proper test teardown. | 569 // Process all messages to ensure proper test teardown. |
| 616 content::RunAllPendingInMessageLoop(); | 570 content::RunAllPendingInMessageLoop(); |
| 617 return; | 571 return; |
| 618 } | 572 } |
| 619 | 573 |
| 620 EXPECT_EQ(defer_read_completed, defer); | 574 if (mock_loader.status() == MockResourceLoader::Status::CALLBACK_PENDING) |
| 621 if (defer) { | 575 mime_sniffing_handler.Resume(); |
| 622 expected_resume_calls++; | 576 |
| 623 mime_sniffing_handler->Resume(); | 577 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader.status()); |
| 624 } | |
| 625 EXPECT_EQ(expected_resume_calls, resource_controller.resume_call_count()); | |
| 626 | 578 |
| 627 // Process all messages to ensure proper test teardown. | 579 // Process all messages to ensure proper test teardown. |
| 628 content::RunAllPendingInMessageLoop(); | 580 content::RunAllPendingInMessageLoop(); |
| 629 } | 581 } |
| 630 | 582 |
| 631 // Test that the proper Accept: header is set based on the ResourceType | 583 // Test that the proper Accept: header is set based on the ResourceType |
| 632 TEST_F(MimeSniffingResourceHandlerTest, AcceptHeaders) { | 584 TEST_F(MimeSniffingResourceHandlerTest, AcceptHeaders) { |
| 633 EXPECT_EQ( | 585 EXPECT_EQ( |
| 634 "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp," | 586 "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp," |
| 635 "*/*;q=0.8", | 587 "*/*;q=0.8", |
| (...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 911 false); // is_using_lofi | 863 false); // is_using_lofi |
| 912 | 864 |
| 913 TestResourceDispatcherHost host(false); | 865 TestResourceDispatcherHost host(false); |
| 914 TestResourceDispatcherHostDelegate host_delegate(false); | 866 TestResourceDispatcherHostDelegate host_delegate(false); |
| 915 host.SetDelegate(&host_delegate); | 867 host.SetDelegate(&host_delegate); |
| 916 | 868 |
| 917 TestFakePluginService plugin_service(false, false); | 869 TestFakePluginService plugin_service(false, false); |
| 918 std::unique_ptr<ResourceHandler> intercepting_handler( | 870 std::unique_ptr<ResourceHandler> intercepting_handler( |
| 919 new InterceptingResourceHandler(base::MakeUnique<TestResourceHandler>(), | 871 new InterceptingResourceHandler(base::MakeUnique<TestResourceHandler>(), |
| 920 nullptr)); | 872 nullptr)); |
| 921 std::unique_ptr<ResourceHandler> mime_handler(new MimeSniffingResourceHandler( | 873 MimeSniffingResourceHandler mime_sniffing_handler( |
| 922 std::unique_ptr<ResourceHandler>(new TestResourceHandler()), &host, | 874 std::unique_ptr<ResourceHandler>(new TestResourceHandler()), &host, |
| 923 &plugin_service, | 875 &plugin_service, |
| 924 static_cast<InterceptingResourceHandler*>(intercepting_handler.get()), | 876 static_cast<InterceptingResourceHandler*>(intercepting_handler.get()), |
| 925 request.get(), REQUEST_CONTEXT_TYPE_UNSPECIFIED)); | 877 request.get(), REQUEST_CONTEXT_TYPE_UNSPECIFIED); |
| 926 | 878 |
| 927 TestResourceController resource_controller; | 879 MockResourceLoader mock_loader(&mime_sniffing_handler); |
| 928 mime_handler->SetController(&resource_controller); | |
| 929 | 880 |
| 930 // Request starts. | 881 // Request starts. |
| 931 bool defer = false; | 882 EXPECT_EQ(MockResourceLoader::Status::IDLE, |
| 932 mime_handler->OnWillStart(request->url(), &defer); | 883 mock_loader.OnWillStart(request->url())); |
| 933 EXPECT_FALSE(defer); | |
| 934 | 884 |
| 935 // Simulate a 304 response. | 885 // Simulate a 304 response. |
| 936 scoped_refptr<ResourceResponse> response(new ResourceResponse); | 886 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
| 937 // The MIME type isn't important but it shouldn't be empty. | 887 // The MIME type isn't important but it shouldn't be empty. |
| 938 response->head.mime_type = "application/pdf"; | 888 response->head.mime_type = "application/pdf"; |
| 939 response->head.headers = new net::HttpResponseHeaders("HTTP/1.x 304 OK"); | 889 response->head.headers = new net::HttpResponseHeaders("HTTP/1.x 304 OK"); |
| 940 | 890 |
| 941 // The response is received. No new ResourceHandler should be created to | 891 // The response is received. No new ResourceHandler should be created to |
| 942 // handle the download. | 892 // handle the download. |
| 943 mime_handler->OnResponseStarted(response.get(), &defer); | 893 EXPECT_EQ(MockResourceLoader::Status::IDLE, |
| 944 EXPECT_FALSE(defer); | 894 mock_loader.OnResponseStarted(response.get())); |
| 945 EXPECT_FALSE(host.new_resource_handler()); | 895 EXPECT_FALSE(host.new_resource_handler()); |
| 946 | 896 |
| 947 content::RunAllPendingInMessageLoop(); | 897 content::RunAllPendingInMessageLoop(); |
| 948 } | 898 } |
| 949 | 899 |
| 950 TEST_F(MimeSniffingResourceHandlerTest, FetchShouldDisableMimeSniffing) { | 900 TEST_F(MimeSniffingResourceHandlerTest, FetchShouldDisableMimeSniffing) { |
| 951 net::URLRequestContext context; | 901 net::URLRequestContext context; |
| 952 std::unique_ptr<net::URLRequest> request(context.CreateRequest( | 902 std::unique_ptr<net::URLRequest> request(context.CreateRequest( |
| 953 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); | 903 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); |
| 954 ResourceRequestInfo::AllocateForTesting(request.get(), | 904 ResourceRequestInfo::AllocateForTesting(request.get(), |
| (...skipping 11 matching lines...) Expand all Loading... |
| 966 TestResourceDispatcherHost host(false); | 916 TestResourceDispatcherHost host(false); |
| 967 | 917 |
| 968 TestFakePluginService plugin_service(false, false); | 918 TestFakePluginService plugin_service(false, false); |
| 969 std::unique_ptr<InterceptingResourceHandler> intercepting_handler( | 919 std::unique_ptr<InterceptingResourceHandler> intercepting_handler( |
| 970 new InterceptingResourceHandler(base::MakeUnique<TestResourceHandler>(), | 920 new InterceptingResourceHandler(base::MakeUnique<TestResourceHandler>(), |
| 971 nullptr)); | 921 nullptr)); |
| 972 | 922 |
| 973 std::unique_ptr<TestResourceHandler> scoped_test_handler( | 923 std::unique_ptr<TestResourceHandler> scoped_test_handler( |
| 974 new TestResourceHandler()); | 924 new TestResourceHandler()); |
| 975 scoped_test_handler->set_on_response_started_result(false); | 925 scoped_test_handler->set_on_response_started_result(false); |
| 976 std::unique_ptr<ResourceHandler> mime_sniffing_handler( | 926 MimeSniffingResourceHandler mime_sniffing_handler( |
| 977 new MimeSniffingResourceHandler(std::move(scoped_test_handler), &host, | 927 std::move(scoped_test_handler), &host, &plugin_service, |
| 978 &plugin_service, | 928 intercepting_handler.get(), request.get(), REQUEST_CONTEXT_TYPE_FETCH); |
| 979 intercepting_handler.get(), request.get(), | |
| 980 REQUEST_CONTEXT_TYPE_FETCH)); | |
| 981 | 929 |
| 982 TestResourceController resource_controller; | 930 MockResourceLoader mock_loader(&mime_sniffing_handler); |
| 983 mime_sniffing_handler->SetController(&resource_controller); | |
| 984 | 931 |
| 985 bool defer = false; | 932 // Request starts. |
| 986 mime_sniffing_handler->OnWillStart(GURL(), &defer); | 933 EXPECT_EQ(MockResourceLoader::Status::IDLE, |
| 987 ASSERT_FALSE(defer); | 934 mock_loader.OnWillStart(request->url())); |
| 988 | 935 |
| 989 scoped_refptr<ResourceResponse> response(new ResourceResponse); | 936 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
| 990 response->head.mime_type = "text/plain"; | 937 response->head.mime_type = "text/plain"; |
| 991 | 938 |
| 992 // |mime_sniffing_handler->OnResponseStarted| should return false because | 939 // |mime_sniffing_handler->OnResponseStarted| should return false because |
| 993 // mime sniffing is disabled and the wrapped resource handler returns false | 940 // mime sniffing is disabled and the wrapped resource handler returns false |
| 994 // on OnResponseStarted. | 941 // on OnResponseStarted. |
| 995 EXPECT_FALSE( | 942 EXPECT_EQ(MockResourceLoader::Status::CANCELED, |
| 996 mime_sniffing_handler->OnResponseStarted(response.get(), &defer)); | 943 mock_loader.OnResponseStarted(response)); |
| 944 EXPECT_EQ(net::ERR_ABORTED, mock_loader.error_code()); |
| 997 | 945 |
| 998 // Process all messages to ensure proper test teardown. | 946 // Process all messages to ensure proper test teardown. |
| 999 content::RunAllPendingInMessageLoop(); | 947 content::RunAllPendingInMessageLoop(); |
| 1000 } | 948 } |
| 1001 | 949 |
| 1002 } // namespace content | 950 } // namespace content |
| OLD | NEW |