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 @@
namespace content {
// Wrapper class that adds cross-origin resource sharing capabilities
// (https://www.w3.org/TR/cors/), delegating requests as well as potential
// preflight requests to the supplied |network_loader_factory|. It is owned by
// the CORSURLLoaderFactory that created it.
// (https://fetch.spec.whatwg.org/#http-cors-protocol), delegating requests as
// well as potential preflight requests to the supplied
// |network_loader_factory|. It is owned by the CORSURLLoaderFactory that
// created it.
class CONTENT_EXPORT CORSURLLoader : public mojom::URLLoader,
public mojom::URLLoaderClient {
public:
......
......@@ -16,6 +16,8 @@
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "testing/gtest/include/gtest/gtest.h"
using network::mojom::FetchRequestMode;
namespace content {
namespace {
......@@ -24,12 +26,14 @@ class TestURLLoaderFactory : public mojom::URLLoaderFactory {
TestURLLoaderFactory() = default;
~TestURLLoaderFactory() override = default;
void NotifyClientOnReceiveResponse() {
void NotifyClientOnReceiveResponse(const std::string& extra_header) {
DCHECK(client_ptr_);
ResourceResponseHead response;
response.headers = new net::HttpResponseHeaders(
"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 */,
nullptr /* downloaded_file */);
......@@ -69,10 +73,9 @@ class CORSURLLoaderTest : public testing::Test {
: test_network_factory_binding_(&test_network_loader_factory_) {}
protected:
void CreateLoaderAndStart(
const GURL& origin,
const GURL& url,
network::mojom::FetchRequestMode fetch_request_mode) {
void CreateLoaderAndStart(const GURL& origin,
const GURL& url,
FetchRequestMode fetch_request_mode) {
mojom::URLLoaderFactoryPtr network_factory_ptr;
test_network_factory_binding_.Bind(mojo::MakeRequest(&network_factory_ptr));
......@@ -103,8 +106,9 @@ class CORSURLLoaderTest : public testing::Test {
return test_network_loader_factory_.IsCreateLoaderAndStartCalled();
}
void NotifyLoaderClientOnReceiveResponse() {
test_network_loader_factory_.NotifyClientOnReceiveResponse();
void NotifyLoaderClientOnReceiveResponse(
const std::string& extra_header = std::string()) {
test_network_loader_factory_.NotifyClientOnReceiveResponse(extra_header);
}
void NotifyLoaderClientOnComplete(int error_code) {
......@@ -134,8 +138,24 @@ class CORSURLLoaderTest : public testing::Test {
TEST_F(CORSURLLoaderTest, SameOriginRequest) {
const GURL url("http://example.com/foo.png");
CreateLoaderAndStart(url.GetOrigin(), url,
network::mojom::FetchRequestMode::kSameOrigin);
CreateLoaderAndStart(url.GetOrigin(), url, 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();
NotifyLoaderClientOnComplete(net::OK);
......@@ -171,7 +191,7 @@ TEST_F(CORSURLLoaderTest, CrossOriginRequestFetchRequestModeSameOrigin) {
TEST_F(CORSURLLoaderTest, CrossOriginRequestWithCORSModeButMissingCORSHeader) {
const GURL origin("http://example.com");
const GURL url("http://other.com/foo.png");
CreateLoaderAndStart(origin, url, network::mojom::FetchRequestMode::kCORS);
CreateLoaderAndStart(origin, url, FetchRequestMode::kCORS);
NotifyLoaderClientOnReceiveResponse();
NotifyLoaderClientOnComplete(net::OK);
......@@ -187,5 +207,44 @@ TEST_F(CORSURLLoaderTest, CrossOriginRequestWithCORSModeButMissingCORSHeader) {
*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 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