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
3 // found in the LICENSE file.
5 #include "base/memory/scoped_ptr.h"
6 #include "base/run_loop.h"
7 #include "content/browser/webui/url_data_manager_backend.h"
8 #include "content/public/test/mock_resource_context.h"
9 #include "content/public/test/test_browser_thread_bundle.h"
10 #include "net/http/http_response_headers.h"
11 #include "net/http/http_response_info.h"
12 #include "net/url_request/url_request_context.h"
13 #include "net/url_request/url_request_job.h"
14 #include "net/url_request/url_request_job_factory_impl.h"
15 #include "net/url_request/url_request_test_util.h"
16 #include "testing/gtest/include/gtest/gtest.h"
20 class CancelAfterFirstReadURLRequestDelegate
: public net::TestDelegate
{
22 CancelAfterFirstReadURLRequestDelegate() {}
24 ~CancelAfterFirstReadURLRequestDelegate() override
{}
26 void OnResponseStarted(net::URLRequest
* request
) override
{
27 // net::TestDelegate will start the first read.
28 TestDelegate::OnResponseStarted(request
);
32 void OnReadCompleted(net::URLRequest
* request
, int bytes_read
) override
{
33 // Read should have been cancelled.
34 EXPECT_EQ(-1, bytes_read
);
38 DISALLOW_COPY_AND_ASSIGN(CancelAfterFirstReadURLRequestDelegate
);
41 class UrlDataManagerBackendTest
: public testing::Test
{
43 UrlDataManagerBackendTest() {
44 // URLRequestJobFactory takes ownership of the passed in ProtocolHandler.
45 url_request_job_factory_
.SetProtocolHandler(
47 URLDataManagerBackend::CreateProtocolHandler(
48 &resource_context_
, false, nullptr, nullptr));
49 url_request_context_
.set_job_factory(&url_request_job_factory_
);
52 scoped_ptr
<net::URLRequest
> CreateRequest(net::URLRequest::Delegate
* delegate
,
54 scoped_ptr
<net::URLRequest
> request
= url_request_context_
.CreateRequest(
55 GURL("chrome://resources/polymer/v1_0/polymer/polymer-extracted.js"),
56 net::HIGHEST
, delegate
);
57 request
->SetExtraRequestHeaderByName("Origin", origin
, true);
58 return request
.Pass();
62 TestBrowserThreadBundle thread_bundle_
;
63 MockResourceContext resource_context_
;
64 net::URLRequestJobFactoryImpl url_request_job_factory_
;
65 net::URLRequestContext url_request_context_
;
66 net::TestDelegate delegate_
;
69 TEST_F(UrlDataManagerBackendTest
, AccessControlAllowOriginChromeUrl
) {
70 scoped_ptr
<net::URLRequest
> request(
71 CreateRequest(&delegate_
, "chrome://webui"));
73 base::RunLoop().RunUntilIdle();
74 EXPECT_TRUE(request
->response_headers()->HasHeaderValue(
75 "Access-Control-Allow-Origin", "chrome://webui"));
78 TEST_F(UrlDataManagerBackendTest
, AccessControlAllowOriginNonChromeUrl
) {
79 scoped_ptr
<net::URLRequest
> request(
80 CreateRequest(&delegate_
, "http://www.example.com"));
82 base::RunLoop().RunUntilIdle();
83 EXPECT_TRUE(request
->response_headers()->HasHeaderValue(
84 "Access-Control-Allow-Origin", "null"));
87 // Check that the URLRequest isn't passed headers after cancellation.
88 TEST_F(UrlDataManagerBackendTest
, CancelBeforeResponseStarts
) {
89 scoped_ptr
<net::URLRequest
> request(
90 CreateRequest(&delegate_
, "chrome://webui"));
93 base::RunLoop().RunUntilIdle();
94 EXPECT_EQ(net::URLRequestStatus::CANCELED
, request
->status().status());
95 EXPECT_EQ(1, delegate_
.response_started_count());
98 // Check that the URLRequest isn't passed data after cancellation.
99 TEST_F(UrlDataManagerBackendTest
, CancelAfterFirstReadStarted
) {
100 CancelAfterFirstReadURLRequestDelegate cancel_delegate
;
101 scoped_ptr
<net::URLRequest
> request(
102 CreateRequest(&cancel_delegate
, "chrome://webui"));
104 base::RunLoop().RunUntilIdle();
106 EXPECT_EQ(net::URLRequestStatus::CANCELED
, request
->status().status());
107 EXPECT_EQ(1, cancel_delegate
.response_started_count());
108 EXPECT_EQ("", cancel_delegate
.data_received());
111 } // namespace content