summaryrefslogtreecommitdiff
path: root/chromium/content/browser/loader/mojo_async_resource_handler_unittest.cc
diff options
context:
space:
mode:
Diffstat (limited to 'chromium/content/browser/loader/mojo_async_resource_handler_unittest.cc')
-rw-r--r--chromium/content/browser/loader/mojo_async_resource_handler_unittest.cc155
1 files changed, 144 insertions, 11 deletions
diff --git a/chromium/content/browser/loader/mojo_async_resource_handler_unittest.cc b/chromium/content/browser/loader/mojo_async_resource_handler_unittest.cc
index 5386d6e906f..4ad1ba638fe 100644
--- a/chromium/content/browser/loader/mojo_async_resource_handler_unittest.cc
+++ b/chromium/content/browser/loader/mojo_async_resource_handler_unittest.cc
@@ -33,6 +33,7 @@
#include "content/public/test/test_browser_thread_bundle.h"
#include "mojo/public/c/system/data_pipe.h"
#include "mojo/public/c/system/types.h"
+#include "mojo/public/cpp/bindings/strong_binding.h"
#include "mojo/public/cpp/system/data_pipe.h"
#include "net/base/auth.h"
#include "net/base/net_errors.h"
@@ -98,12 +99,7 @@ class TestResourceDispatcherHostDelegate final
bool HandleExternalProtocol(
const GURL& url,
- int child_id,
- const ResourceRequestInfo::WebContentsGetter& web_contents_getter,
- bool is_main_frame,
- ui::PageTransition page_transition,
- bool has_user_gesture,
- ResourceContext* resource_context) override {
+ ResourceRequestInfo* resource_request_info) override {
ADD_FAILURE() << "HandleExternalProtocol should not be called.";
return false;
}
@@ -189,6 +185,10 @@ class TestResourceController : public ResourceController {
}
void CancelWithError(int error_code) override {
+ // While cancelling more than once is legal, none of these tests should do
+ // it.
+ EXPECT_FALSE(is_cancel_with_error_called_);
+
is_cancel_with_error_called_ = true;
error_ = error_code;
if (quit_closure_)
@@ -228,8 +228,8 @@ class MojoAsyncResourceHandlerWithCustomDataPipeOperations
MojoAsyncResourceHandlerWithCustomDataPipeOperations(
net::URLRequest* request,
ResourceDispatcherHostImpl* rdh,
- mojo::InterfaceRequest<mojom::URLLoader> mojo_request,
- mojom::URLLoaderClientPtr url_loader_client)
+ mojom::URLLoaderAssociatedRequest mojo_request,
+ mojom::URLLoaderClientAssociatedPtr url_loader_client)
: MojoAsyncResourceHandler(request,
rdh,
std::move(mojo_request),
@@ -246,6 +246,7 @@ class MojoAsyncResourceHandlerWithCustomDataPipeOperations
is_end_write_expectation_set_ = true;
end_write_expectation_ = end_write_expectation;
}
+ bool has_received_bad_message() const { return has_received_bad_message_; }
private:
MojoResult BeginWrite(void** data, uint32_t* available) override {
@@ -258,9 +259,13 @@ class MojoAsyncResourceHandlerWithCustomDataPipeOperations
return end_write_expectation_;
return MojoAsyncResourceHandler::EndWrite(written);
}
+ void ReportBadMessage(const std::string& error) override {
+ has_received_bad_message_ = true;
+ }
bool is_begin_write_expectation_set_ = false;
bool is_end_write_expectation_set_ = false;
+ bool has_received_bad_message_ = false;
MojoResult begin_write_expectation_ = MOJO_RESULT_UNKNOWN;
MojoResult end_write_expectation_ = MOJO_RESULT_UNKNOWN;
@@ -268,6 +273,43 @@ class MojoAsyncResourceHandlerWithCustomDataPipeOperations
MojoAsyncResourceHandlerWithCustomDataPipeOperations);
};
+class TestURLLoaderFactory final : public mojom::URLLoaderFactory {
+ public:
+ TestURLLoaderFactory() {}
+ ~TestURLLoaderFactory() override {}
+
+ void CreateLoaderAndStart(
+ mojom::URLLoaderAssociatedRequest request,
+ int32_t routing_id,
+ int32_t request_id,
+ const ResourceRequest& url_request,
+ mojom::URLLoaderClientAssociatedPtrInfo client_ptr_info) override {
+ loader_request_ = std::move(request);
+ client_ptr_info_ = std::move(client_ptr_info);
+ }
+
+ mojom::URLLoaderAssociatedRequest PassLoaderRequest() {
+ return std::move(loader_request_);
+ }
+
+ mojom::URLLoaderClientAssociatedPtrInfo PassClientPtrInfo() {
+ return std::move(client_ptr_info_);
+ }
+
+ void SyncLoad(int32_t routing_id,
+ int32_t request_id,
+ const ResourceRequest& url_request,
+ const SyncLoadCallback& callback) override {
+ NOTREACHED();
+ }
+
+ private:
+ mojom::URLLoaderAssociatedRequest loader_request_;
+ mojom::URLLoaderClientAssociatedPtrInfo client_ptr_info_;
+
+ DISALLOW_COPY_AND_ASSIGN(TestURLLoaderFactory);
+};
+
class MojoAsyncResourceHandlerTestBase {
public:
MojoAsyncResourceHandlerTestBase()
@@ -295,9 +337,28 @@ class MojoAsyncResourceHandlerTestBase {
true, // is_async
false // is_using_lofi
);
+
+ ResourceRequest request;
+ base::WeakPtr<mojo::StrongBinding<mojom::URLLoaderFactory>> weak_binding =
+ mojo::MakeStrongBinding(base::MakeUnique<TestURLLoaderFactory>(),
+ mojo::GetProxy(&url_loader_factory_));
+
+ url_loader_factory_->CreateLoaderAndStart(
+ mojo::GetProxy(&url_loader_proxy_,
+ url_loader_factory_.associated_group()),
+ 0, 0, request, url_loader_client_.CreateRemoteAssociatedPtrInfo(
+ url_loader_factory_.associated_group()));
+
+ url_loader_factory_.FlushForTesting();
+ DCHECK(weak_binding);
+ TestURLLoaderFactory* factory_impl =
+ static_cast<TestURLLoaderFactory*>(weak_binding->impl());
+
+ mojom::URLLoaderClientAssociatedPtr client_ptr;
+ client_ptr.Bind(factory_impl->PassClientPtrInfo());
handler_.reset(new MojoAsyncResourceHandlerWithCustomDataPipeOperations(
- request_.get(), &rdh_, nullptr,
- url_loader_client_.CreateInterfacePtrAndBind()));
+ request_.get(), &rdh_, factory_impl->PassLoaderRequest(),
+ std::move(client_ptr)));
handler_->SetController(&resource_controller_);
}
@@ -349,6 +410,8 @@ class MojoAsyncResourceHandlerTestBase {
TestBrowserThreadBundle thread_bundle_;
TestResourceDispatcherHostDelegate rdh_delegate_;
ResourceDispatcherHostImpl rdh_;
+ mojom::URLLoaderFactoryPtr url_loader_factory_;
+ mojom::URLLoaderAssociatedPtr url_loader_proxy_;
TestURLLoaderClient url_loader_client_;
TestResourceController resource_controller_;
std::unique_ptr<TestBrowserContext> browser_context_;
@@ -781,7 +844,7 @@ TEST_F(MojoAsyncResourceHandlerTest,
url_loader_client_.RunUntilResponseBodyArrived();
ASSERT_TRUE(url_loader_client_.response_body().is_valid());
handler_->ResetBeginWriteExpectation();
- handler_->ResumeForTesting();
+ handler_->OnWritableForTesting();
std::string actual;
while (actual.size() < written) {
@@ -1020,6 +1083,76 @@ TEST_P(MojoAsyncResourceHandlerWithAllocationSizeTest, CancelWhileWaiting) {
EXPECT_EQ(0, resource_controller_.num_resume_calls());
}
+TEST_P(MojoAsyncResourceHandlerWithAllocationSizeTest, RedirectHandling) {
+ rdh_delegate_.set_num_on_response_started_calls_expectation(1);
+ bool defer = false;
+
+ ASSERT_TRUE(handler_->OnWillStart(request_->url(), &defer));
+ ASSERT_FALSE(defer);
+ scoped_refptr<ResourceResponse> response = new ResourceResponse();
+ net::RedirectInfo redirect_info;
+ redirect_info.status_code = 301;
+ ASSERT_TRUE(
+ handler_->OnRequestRedirected(redirect_info, response.get(), &defer));
+ ASSERT_TRUE(defer);
+
+ ASSERT_FALSE(url_loader_client_.has_received_response());
+ ASSERT_FALSE(url_loader_client_.has_received_redirect());
+ url_loader_client_.RunUntilRedirectReceived();
+
+ ASSERT_FALSE(url_loader_client_.has_received_response());
+ ASSERT_TRUE(url_loader_client_.has_received_redirect());
+ EXPECT_EQ(301, url_loader_client_.redirect_info().status_code);
+
+ EXPECT_EQ(0, resource_controller_.num_resume_calls());
+ handler_->FollowRedirect();
+ EXPECT_EQ(1, resource_controller_.num_resume_calls());
+
+ url_loader_client_.ClearHasReceivedRedirect();
+ // Redirect once more.
+ defer = false;
+ redirect_info.status_code = 302;
+ ASSERT_TRUE(
+ handler_->OnRequestRedirected(redirect_info, response.get(), &defer));
+ ASSERT_TRUE(defer);
+
+ ASSERT_FALSE(url_loader_client_.has_received_response());
+ ASSERT_FALSE(url_loader_client_.has_received_redirect());
+ url_loader_client_.RunUntilRedirectReceived();
+
+ ASSERT_FALSE(url_loader_client_.has_received_response());
+ ASSERT_TRUE(url_loader_client_.has_received_redirect());
+ EXPECT_EQ(302, url_loader_client_.redirect_info().status_code);
+
+ EXPECT_EQ(1, resource_controller_.num_resume_calls());
+ handler_->FollowRedirect();
+ EXPECT_EQ(2, resource_controller_.num_resume_calls());
+
+ // Give the final response.
+ defer = false;
+ ASSERT_TRUE(handler_->OnResponseStarted(response.get(), &defer));
+ ASSERT_FALSE(defer);
+
+ net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, net::OK);
+ handler_->OnResponseCompleted(status, &defer);
+ ASSERT_FALSE(defer);
+
+ ASSERT_FALSE(url_loader_client_.has_received_completion());
+ url_loader_client_.RunUntilComplete();
+
+ ASSERT_TRUE(url_loader_client_.has_received_response());
+ ASSERT_TRUE(url_loader_client_.has_received_completion());
+ EXPECT_EQ(net::OK, url_loader_client_.completion_status().error_code);
+}
+
+TEST_P(MojoAsyncResourceHandlerWithAllocationSizeTest,
+ MalformedFollowRedirectRequest) {
+ handler_->FollowRedirect();
+
+ EXPECT_TRUE(handler_->has_received_bad_message());
+ EXPECT_EQ(0, resource_controller_.num_resume_calls());
+}
+
// Typically ResourceHandler methods are called in this order.
TEST_P(
MojoAsyncResourceHandlerWithAllocationSizeTest,