Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(102)

Side by Side Diff: content/browser/webui/url_data_manager_backend_unittest.cc

Issue 1273813005: Revert of Fix cancellation of a pair of URLRequestJob subclasses (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/memory/scoped_ptr.h" 5 #include "base/memory/scoped_ptr.h"
6 #include "base/run_loop.h" 6 #include "base/run_loop.h"
7 #include "content/browser/webui/url_data_manager_backend.h" 7 #include "content/browser/webui/url_data_manager_backend.h"
8 #include "content/public/test/mock_resource_context.h" 8 #include "content/public/test/mock_resource_context.h"
9 #include "content/public/test/test_browser_thread_bundle.h" 9 #include "content/public/test/test_browser_thread_bundle.h"
10 #include "net/http/http_response_headers.h" 10 #include "net/http/http_response_headers.h"
11 #include "net/http/http_response_info.h" 11 #include "net/http/http_response_info.h"
12 #include "net/url_request/url_request_context.h" 12 #include "net/url_request/url_request_context.h"
13 #include "net/url_request/url_request_job.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" 14 #include "testing/gtest/include/gtest/gtest.h"
17 15
18 namespace content { 16 namespace content {
19 17
20 class CancelAfterFirstReadURLRequestDelegate : public net::TestDelegate { 18 namespace {
21 public:
22 CancelAfterFirstReadURLRequestDelegate() {}
23 19
24 ~CancelAfterFirstReadURLRequestDelegate() override {} 20 // Create a request for a chrome://resource URL passing the supplied |origin|
25 21 // header and checking that the response header Access-Control-Allow-Origin has
26 void OnResponseStarted(net::URLRequest* request) override { 22 // value |expected_access_control_allow_origin_value|.
27 // net::TestDelegate will start the first read. 23 void RunAccessControlAllowOriginTest(
28 TestDelegate::OnResponseStarted(request); 24 const std::string& origin,
29 request->Cancel(); 25 const std::string& expected_access_control_allow_origin_value) {
30 } 26 TestBrowserThreadBundle thread_bundle;
31 27 MockResourceContext resource_context;
32 void OnReadCompleted(net::URLRequest* request, int bytes_read) override { 28 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler> protocol_handler(
33 // Read should have been cancelled. 29 URLDataManagerBackend::CreateProtocolHandler(&resource_context, false,
34 EXPECT_EQ(-1, bytes_read); 30 nullptr, nullptr));
35 } 31 net::URLRequestContext url_request_context;
36 32 scoped_ptr<net::URLRequest> request = url_request_context.CreateRequest(
37 private: 33 GURL("chrome://resources/polymer/v1_0/polymer/polymer-extracted.js"),
38 DISALLOW_COPY_AND_ASSIGN(CancelAfterFirstReadURLRequestDelegate); 34 net::HIGHEST, nullptr);
39 }; 35 request->SetExtraRequestHeaderByName("Origin", origin, true);
40 36 scoped_refptr<net::URLRequestJob> job =
41 class UrlDataManagerBackendTest : public testing::Test { 37 protocol_handler->MaybeCreateJob(request.get(), nullptr);
42 public: 38 ASSERT_TRUE(job);
43 UrlDataManagerBackendTest() { 39 job->Start();
44 // URLRequestJobFactory takes ownership of the passed in ProtocolHandler.
45 url_request_job_factory_.SetProtocolHandler(
46 "chrome",
47 URLDataManagerBackend::CreateProtocolHandler(
48 &resource_context_, false, nullptr, nullptr));
49 url_request_context_.set_job_factory(&url_request_job_factory_);
50 }
51
52 scoped_ptr<net::URLRequest> CreateRequest(net::URLRequest::Delegate* delegate,
53 const char* origin) {
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();
59 }
60
61 protected:
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_;
67 };
68
69 TEST_F(UrlDataManagerBackendTest, AccessControlAllowOriginChromeUrl) {
70 scoped_ptr<net::URLRequest> request(
71 CreateRequest(&delegate_, "chrome://webui"));
72 request->Start();
73 base::RunLoop().RunUntilIdle(); 40 base::RunLoop().RunUntilIdle();
74 EXPECT_TRUE(request->response_headers()->HasHeaderValue( 41 net::HttpResponseInfo response;
75 "Access-Control-Allow-Origin", "chrome://webui")); 42 job->GetResponseInfo(&response);
43 EXPECT_TRUE(response.headers->HasHeaderValue(
44 "Access-Control-Allow-Origin",
45 expected_access_control_allow_origin_value));
76 } 46 }
77 47
78 TEST_F(UrlDataManagerBackendTest, AccessControlAllowOriginNonChromeUrl) { 48 } // namespace
79 scoped_ptr<net::URLRequest> request( 49
80 CreateRequest(&delegate_, "http://www.example.com")); 50 TEST(UrlDataManagerBackendTest, AccessControlAllowOriginChromeUrl) {
81 request->Start(); 51 RunAccessControlAllowOriginTest("chrome://webui", "chrome://webui");
82 base::RunLoop().RunUntilIdle();
83 EXPECT_TRUE(request->response_headers()->HasHeaderValue(
84 "Access-Control-Allow-Origin", "null"));
85 } 52 }
86 53
87 // Check that the URLRequest isn't passed headers after cancellation. 54 TEST(UrlDataManagerBackendTest, AccessControlAllowOriginNonChromeUrl) {
88 TEST_F(UrlDataManagerBackendTest, CancelBeforeResponseStarts) { 55 RunAccessControlAllowOriginTest("http://www.example.com", "null");
89 scoped_ptr<net::URLRequest> request(
90 CreateRequest(&delegate_, "chrome://webui"));
91 request->Start();
92 request->Cancel();
93 base::RunLoop().RunUntilIdle();
94 EXPECT_EQ(net::URLRequestStatus::CANCELED, request->status().status());
95 EXPECT_EQ(1, delegate_.response_started_count());
96 }
97
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"));
103 request->Start();
104 base::RunLoop().RunUntilIdle();
105
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());
109 } 56 }
110 57
111 } // namespace content 58 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/webui/url_data_manager_backend.cc ('k') | net/url_request/url_request_http_job_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698