Commit 09bdfef5 authored by Takashi Toyoshima's avatar Takashi Toyoshima Committed by Commit Bot

CORSURLLoaderTest: submit remaining tests that the original CL had

This patch imports all remaining tests that the original CL below had.
https://chromium-review.googlesource.com/c/chromium/src/+/558226

Bug: 736308
Change-Id: I9cfad08f695d63a440f395bdf51c9b072fc812ba
Reviewed-on: https://chromium-review.googlesource.com/763148
Commit-Queue: Takashi Toyoshima <toyoshim@chromium.org>
Reviewed-by: default avatarTakeshi Yoshino <tyoshino@chromium.org>
Reviewed-by: default avatarKinuko Yasuda <kinuko@chromium.org>
Cr-Commit-Position: refs/heads/master@{#517065}
parent 6f1c8d9e
...@@ -15,9 +15,10 @@ ...@@ -15,9 +15,10 @@
namespace content { namespace content {
// Wrapper class that adds cross-origin resource sharing capabilities // Wrapper class that adds cross-origin resource sharing capabilities
// (https://www.w3.org/TR/cors/), delegating requests as well as potential // (https://fetch.spec.whatwg.org/#http-cors-protocol), delegating requests as
// preflight requests to the supplied |network_loader_factory|. It is owned by // well as potential preflight requests to the supplied
// the CORSURLLoaderFactory that created it. // |network_loader_factory|. It is owned by the CORSURLLoaderFactory that
// created it.
class CONTENT_EXPORT CORSURLLoader : public mojom::URLLoader, class CONTENT_EXPORT CORSURLLoader : public mojom::URLLoader,
public mojom::URLLoaderClient { public mojom::URLLoaderClient {
public: public:
......
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h" #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
using network::mojom::FetchRequestMode;
namespace content { namespace content {
namespace { namespace {
...@@ -24,12 +26,14 @@ class TestURLLoaderFactory : public mojom::URLLoaderFactory { ...@@ -24,12 +26,14 @@ class TestURLLoaderFactory : public mojom::URLLoaderFactory {
TestURLLoaderFactory() = default; TestURLLoaderFactory() = default;
~TestURLLoaderFactory() override = default; ~TestURLLoaderFactory() override = default;
void NotifyClientOnReceiveResponse() { void NotifyClientOnReceiveResponse(const std::string& extra_header) {
DCHECK(client_ptr_); DCHECK(client_ptr_);
ResourceResponseHead response; ResourceResponseHead response;
response.headers = new net::HttpResponseHeaders( response.headers = new net::HttpResponseHeaders(
"HTTP/1.1 200 OK\n" "HTTP/1.1 200 OK\n"
"Content-Type: text/html; charset=utf-8\n"); "Content-Type: image/png\n");
if (!extra_header.empty())
response.headers->AddHeader(extra_header);
client_ptr_->OnReceiveResponse(response, base::nullopt /* ssl_info */, client_ptr_->OnReceiveResponse(response, base::nullopt /* ssl_info */,
nullptr /* downloaded_file */); nullptr /* downloaded_file */);
...@@ -69,10 +73,9 @@ class CORSURLLoaderTest : public testing::Test { ...@@ -69,10 +73,9 @@ class CORSURLLoaderTest : public testing::Test {
: test_network_factory_binding_(&test_network_loader_factory_) {} : test_network_factory_binding_(&test_network_loader_factory_) {}
protected: protected:
void CreateLoaderAndStart( void CreateLoaderAndStart(const GURL& origin,
const GURL& origin, const GURL& url,
const GURL& url, FetchRequestMode fetch_request_mode) {
network::mojom::FetchRequestMode fetch_request_mode) {
mojom::URLLoaderFactoryPtr network_factory_ptr; mojom::URLLoaderFactoryPtr network_factory_ptr;
test_network_factory_binding_.Bind(mojo::MakeRequest(&network_factory_ptr)); test_network_factory_binding_.Bind(mojo::MakeRequest(&network_factory_ptr));
...@@ -103,8 +106,9 @@ class CORSURLLoaderTest : public testing::Test { ...@@ -103,8 +106,9 @@ class CORSURLLoaderTest : public testing::Test {
return test_network_loader_factory_.IsCreateLoaderAndStartCalled(); return test_network_loader_factory_.IsCreateLoaderAndStartCalled();
} }
void NotifyLoaderClientOnReceiveResponse() { void NotifyLoaderClientOnReceiveResponse(
test_network_loader_factory_.NotifyClientOnReceiveResponse(); const std::string& extra_header = std::string()) {
test_network_loader_factory_.NotifyClientOnReceiveResponse(extra_header);
} }
void NotifyLoaderClientOnComplete(int error_code) { void NotifyLoaderClientOnComplete(int error_code) {
...@@ -134,8 +138,24 @@ class CORSURLLoaderTest : public testing::Test { ...@@ -134,8 +138,24 @@ class CORSURLLoaderTest : public testing::Test {
TEST_F(CORSURLLoaderTest, SameOriginRequest) { TEST_F(CORSURLLoaderTest, SameOriginRequest) {
const GURL url("http://example.com/foo.png"); const GURL url("http://example.com/foo.png");
CreateLoaderAndStart(url.GetOrigin(), url, CreateLoaderAndStart(url.GetOrigin(), url, FetchRequestMode::kSameOrigin);
network::mojom::FetchRequestMode::kSameOrigin);
NotifyLoaderClientOnReceiveResponse();
NotifyLoaderClientOnComplete(net::OK);
RunUntilComplete();
EXPECT_TRUE(IsNetworkLoaderStarted());
EXPECT_FALSE(client().has_received_redirect());
EXPECT_TRUE(client().has_received_response());
EXPECT_TRUE(client().has_received_completion());
EXPECT_EQ(net::OK, client().status().error_code);
}
TEST_F(CORSURLLoaderTest, CrossOriginRequestWithNoCORSMode) {
const GURL origin("http://example.com");
const GURL url("http://other.com/foo.png");
CreateLoaderAndStart(origin, url, FetchRequestMode::kNoCORS);
NotifyLoaderClientOnReceiveResponse(); NotifyLoaderClientOnReceiveResponse();
NotifyLoaderClientOnComplete(net::OK); NotifyLoaderClientOnComplete(net::OK);
...@@ -171,7 +191,7 @@ TEST_F(CORSURLLoaderTest, CrossOriginRequestFetchRequestModeSameOrigin) { ...@@ -171,7 +191,7 @@ TEST_F(CORSURLLoaderTest, CrossOriginRequestFetchRequestModeSameOrigin) {
TEST_F(CORSURLLoaderTest, CrossOriginRequestWithCORSModeButMissingCORSHeader) { TEST_F(CORSURLLoaderTest, CrossOriginRequestWithCORSModeButMissingCORSHeader) {
const GURL origin("http://example.com"); const GURL origin("http://example.com");
const GURL url("http://other.com/foo.png"); const GURL url("http://other.com/foo.png");
CreateLoaderAndStart(origin, url, network::mojom::FetchRequestMode::kCORS); CreateLoaderAndStart(origin, url, FetchRequestMode::kCORS);
NotifyLoaderClientOnReceiveResponse(); NotifyLoaderClientOnReceiveResponse();
NotifyLoaderClientOnComplete(net::OK); NotifyLoaderClientOnComplete(net::OK);
...@@ -187,5 +207,44 @@ TEST_F(CORSURLLoaderTest, CrossOriginRequestWithCORSModeButMissingCORSHeader) { ...@@ -187,5 +207,44 @@ TEST_F(CORSURLLoaderTest, CrossOriginRequestWithCORSModeButMissingCORSHeader) {
*client().status().cors_error); *client().status().cors_error);
} }
TEST_F(CORSURLLoaderTest, CrossOriginRequestWithCORSMode) {
const GURL origin("http://example.com");
const GURL url("http://other.com/foo.png");
CreateLoaderAndStart(origin, url, FetchRequestMode::kCORS);
NotifyLoaderClientOnReceiveResponse(
"Access-Control-Allow-Origin: http://example.com");
NotifyLoaderClientOnComplete(net::OK);
RunUntilComplete();
EXPECT_TRUE(IsNetworkLoaderStarted());
EXPECT_FALSE(client().has_received_redirect());
EXPECT_TRUE(client().has_received_response());
EXPECT_TRUE(client().has_received_completion());
EXPECT_EQ(net::OK, client().status().error_code);
}
TEST_F(CORSURLLoaderTest,
CrossOriginRequestFetchRequestWithCORSModeButMismatchedCORSHeader) {
const GURL origin("http://example.com");
const GURL url("http://other.com/foo.png");
CreateLoaderAndStart(origin, url, FetchRequestMode::kCORS);
NotifyLoaderClientOnReceiveResponse(
"Access-Control-Allow-Origin: http://some-other-domain.com");
NotifyLoaderClientOnComplete(net::OK);
RunUntilComplete();
EXPECT_TRUE(IsNetworkLoaderStarted());
EXPECT_FALSE(client().has_received_redirect());
EXPECT_FALSE(client().has_received_response());
EXPECT_EQ(net::ERR_FAILED, client().status().error_code);
ASSERT_TRUE(client().status().cors_error);
EXPECT_EQ(network::mojom::CORSError::kAllowOriginMismatch,
*client().status().cors_error);
}
} // namespace } // namespace
} // namespace content } // namespace content
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment