Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / webui / url_data_manager_backend_unittest.cc
blob9a511f1e25d98ccf08655c099363c570ee5ab068
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"
18 namespace content {
20 class CancelAfterFirstReadURLRequestDelegate : public net::TestDelegate {
21 public:
22 CancelAfterFirstReadURLRequestDelegate() {}
24 ~CancelAfterFirstReadURLRequestDelegate() override {}
26 void OnResponseStarted(net::URLRequest* request) override {
27 // net::TestDelegate will start the first read.
28 TestDelegate::OnResponseStarted(request);
29 request->Cancel();
32 void OnReadCompleted(net::URLRequest* request, int bytes_read) override {
33 // Read should have been cancelled.
34 EXPECT_EQ(-1, bytes_read);
37 private:
38 DISALLOW_COPY_AND_ASSIGN(CancelAfterFirstReadURLRequestDelegate);
41 class UrlDataManagerBackendTest : public testing::Test {
42 public:
43 UrlDataManagerBackendTest() {
44 // URLRequestJobFactory takes ownership of the passed in ProtocolHandler.
45 url_request_job_factory_.SetProtocolHandler(
46 "chrome", URLDataManagerBackend::CreateProtocolHandler(
47 &resource_context_, false, nullptr, nullptr));
48 url_request_context_.set_job_factory(&url_request_job_factory_);
51 scoped_ptr<net::URLRequest> CreateRequest(net::URLRequest::Delegate* delegate,
52 const char* origin) {
53 scoped_ptr<net::URLRequest> request = url_request_context_.CreateRequest(
54 GURL("chrome://resources/polymer/v1_0/polymer/polymer-extracted.js"),
55 net::HIGHEST, delegate);
56 request->SetExtraRequestHeaderByName("Origin", origin, true);
57 return request.Pass();
60 protected:
61 TestBrowserThreadBundle thread_bundle_;
62 MockResourceContext resource_context_;
63 net::URLRequestJobFactoryImpl url_request_job_factory_;
64 net::URLRequestContext url_request_context_;
65 net::TestDelegate delegate_;
68 TEST_F(UrlDataManagerBackendTest, AccessControlAllowOriginChromeUrl) {
69 scoped_ptr<net::URLRequest> request(
70 CreateRequest(&delegate_, "chrome://webui"));
71 request->Start();
72 base::RunLoop().RunUntilIdle();
73 EXPECT_TRUE(request->response_headers()->HasHeaderValue(
74 "Access-Control-Allow-Origin", "chrome://webui"));
77 TEST_F(UrlDataManagerBackendTest, AccessControlAllowOriginNonChromeUrl) {
78 scoped_ptr<net::URLRequest> request(
79 CreateRequest(&delegate_, "http://www.example.com"));
80 request->Start();
81 base::RunLoop().RunUntilIdle();
82 EXPECT_TRUE(request->response_headers()->HasHeaderValue(
83 "Access-Control-Allow-Origin", "null"));
86 // Check that the URLRequest isn't passed headers after cancellation.
87 TEST_F(UrlDataManagerBackendTest, CancelBeforeResponseStarts) {
88 scoped_ptr<net::URLRequest> request(
89 CreateRequest(&delegate_, "chrome://webui"));
90 request->Start();
91 request->Cancel();
92 base::RunLoop().RunUntilIdle();
93 EXPECT_EQ(net::URLRequestStatus::CANCELED, request->status().status());
94 EXPECT_EQ(1, delegate_.response_started_count());
97 // Check that the URLRequest isn't passed data after cancellation.
98 TEST_F(UrlDataManagerBackendTest, CancelAfterFirstReadStarted) {
99 CancelAfterFirstReadURLRequestDelegate cancel_delegate;
100 scoped_ptr<net::URLRequest> request(
101 CreateRequest(&cancel_delegate, "chrome://webui"));
102 request->Start();
103 base::RunLoop().RunUntilIdle();
105 EXPECT_EQ(net::URLRequestStatus::CANCELED, request->status().status());
106 EXPECT_EQ(1, cancel_delegate.response_started_count());
107 EXPECT_EQ("", cancel_delegate.data_received());
110 } // namespace content