Enables compositing support for webview.
[chromium-blink-merge.git] / net / url_request / url_request_job_factory_impl_unittest.cc
blob232b21ac44c5654751b1ad063dbc0d48113d378c
1 // Copyright (c) 2012 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 "net/url_request/url_request_job_factory_impl.h"
7 #include "base/bind.h"
8 #include "base/memory/weak_ptr.h"
9 #include "net/url_request/url_request.h"
10 #include "net/url_request/url_request_job.h"
11 #include "net/url_request/url_request_test_util.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace net {
16 namespace {
18 class MockURLRequestJob : public URLRequestJob {
19 public:
20 MockURLRequestJob(URLRequest* request,
21 NetworkDelegate* network_delegate,
22 const URLRequestStatus& status)
23 : URLRequestJob(request, network_delegate),
24 status_(status),
25 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {}
27 virtual void Start() OVERRIDE {
28 // Start reading asynchronously so that all error reporting and data
29 // callbacks happen as they would for network requests.
30 MessageLoop::current()->PostTask(
31 FROM_HERE,
32 base::Bind(&MockURLRequestJob::StartAsync,
33 weak_factory_.GetWeakPtr()));
36 protected:
37 virtual ~MockURLRequestJob() {}
39 private:
40 void StartAsync() {
41 SetStatus(status_);
42 NotifyHeadersComplete();
45 URLRequestStatus status_;
46 base::WeakPtrFactory<MockURLRequestJob> weak_factory_;
49 class DummyProtocolHandler : public URLRequestJobFactory::ProtocolHandler {
50 public:
51 virtual URLRequestJob* MaybeCreateJob(
52 URLRequest* request, NetworkDelegate* network_delegate) const OVERRIDE {
53 return new MockURLRequestJob(
54 request,
55 network_delegate,
56 URLRequestStatus(URLRequestStatus::SUCCESS, OK));
60 class DummyInterceptor : public URLRequestJobFactory::Interceptor {
61 public:
62 DummyInterceptor()
63 : did_intercept_(false),
64 handle_all_protocols_(false) {
67 virtual URLRequestJob* MaybeIntercept(
68 URLRequest* request, NetworkDelegate* network_delegate) const OVERRIDE {
69 did_intercept_ = true;
70 return new MockURLRequestJob(
71 request,
72 network_delegate,
73 URLRequestStatus(URLRequestStatus::FAILED, ERR_FAILED));
76 virtual URLRequestJob* MaybeInterceptRedirect(
77 const GURL& /* location */,
78 URLRequest* /* request */,
79 NetworkDelegate* network_delegate /* network delegate */) const OVERRIDE {
80 return NULL;
83 virtual URLRequestJob* MaybeInterceptResponse(
84 URLRequest* /* request */,
85 NetworkDelegate* network_delegate /* network delegate */) const OVERRIDE {
86 return NULL;
89 virtual bool WillHandleProtocol(
90 const std::string& /* protocol */) const OVERRIDE {
91 return handle_all_protocols_;
94 mutable bool did_intercept_;
95 mutable bool handle_all_protocols_;
98 TEST(URLRequestJobFactoryTest, NoProtocolHandler) {
99 TestDelegate delegate;
100 TestURLRequestContext request_context;
101 TestURLRequest request(GURL("foo://bar"), &delegate, &request_context);
102 request.Start();
104 MessageLoop::current()->Run();
105 EXPECT_EQ(URLRequestStatus::FAILED, request.status().status());
106 EXPECT_EQ(ERR_UNKNOWN_URL_SCHEME, request.status().error());
109 TEST(URLRequestJobFactoryTest, BasicProtocolHandler) {
110 TestDelegate delegate;
111 URLRequestJobFactoryImpl job_factory;
112 TestURLRequestContext request_context;
113 request_context.set_job_factory(&job_factory);
114 job_factory.SetProtocolHandler("foo", new DummyProtocolHandler);
115 TestURLRequest request(GURL("foo://bar"), &delegate, &request_context);
116 request.Start();
118 MessageLoop::current()->Run();
119 EXPECT_EQ(URLRequestStatus::SUCCESS, request.status().status());
120 EXPECT_EQ(OK, request.status().error());
123 TEST(URLRequestJobFactoryTest, DeleteProtocolHandler) {
124 URLRequestJobFactoryImpl job_factory;
125 TestURLRequestContext request_context;
126 request_context.set_job_factory(&job_factory);
127 job_factory.SetProtocolHandler("foo", new DummyProtocolHandler);
128 job_factory.SetProtocolHandler("foo", NULL);
131 TEST(URLRequestJobFactoryTest, BasicInterceptor) {
132 TestDelegate delegate;
133 URLRequestJobFactoryImpl job_factory;
134 TestURLRequestContext request_context;
135 request_context.set_job_factory(&job_factory);
136 job_factory.AddInterceptor(new DummyInterceptor);
137 TestURLRequest request(GURL("http://bar"), &delegate, &request_context);
138 request.Start();
140 MessageLoop::current()->Run();
141 EXPECT_EQ(URLRequestStatus::FAILED, request.status().status());
142 EXPECT_EQ(ERR_FAILED, request.status().error());
145 TEST(URLRequestJobFactoryTest, InterceptorNeedsValidSchemeStill) {
146 TestDelegate delegate;
147 URLRequestJobFactoryImpl job_factory;
148 TestURLRequestContext request_context;
149 request_context.set_job_factory(&job_factory);
150 job_factory.AddInterceptor(new DummyInterceptor);
151 TestURLRequest request(GURL("foo://bar"), &delegate, &request_context);
152 request.Start();
154 MessageLoop::current()->Run();
155 EXPECT_EQ(URLRequestStatus::FAILED, request.status().status());
156 EXPECT_EQ(ERR_UNKNOWN_URL_SCHEME, request.status().error());
159 TEST(URLRequestJobFactoryTest, InterceptorOverridesProtocolHandler) {
160 TestDelegate delegate;
161 URLRequestJobFactoryImpl job_factory;
162 TestURLRequestContext request_context;
163 request_context.set_job_factory(&job_factory);
164 job_factory.SetProtocolHandler("foo", new DummyProtocolHandler);
165 job_factory.AddInterceptor(new DummyInterceptor);
166 TestURLRequest request(GURL("foo://bar"), &delegate, &request_context);
167 request.Start();
169 MessageLoop::current()->Run();
170 EXPECT_EQ(URLRequestStatus::FAILED, request.status().status());
171 EXPECT_EQ(ERR_FAILED, request.status().error());
174 TEST(URLRequestJobFactoryTest, InterceptorDoesntInterceptUnknownProtocols) {
175 TestDelegate delegate;
176 URLRequestJobFactoryImpl job_factory;
177 TestURLRequestContext request_context;
178 request_context.set_job_factory(&job_factory);
179 DummyInterceptor* interceptor = new DummyInterceptor;
180 job_factory.AddInterceptor(interceptor);
181 TestURLRequest request(GURL("foo://bar"), &delegate, &request_context);
182 request.Start();
184 MessageLoop::current()->Run();
185 EXPECT_FALSE(interceptor->did_intercept_);
188 TEST(URLRequestJobFactoryTest, InterceptorInterceptsHandledUnknownProtocols) {
189 TestDelegate delegate;
190 URLRequestJobFactoryImpl job_factory;
191 TestURLRequestContext request_context;
192 request_context.set_job_factory(&job_factory);
193 DummyInterceptor* interceptor = new DummyInterceptor;
194 interceptor->handle_all_protocols_ = true;
195 job_factory.AddInterceptor(interceptor);
196 TestURLRequest request(GURL("foo://bar"), &delegate, &request_context);
197 request.Start();
199 MessageLoop::current()->Run();
200 EXPECT_TRUE(interceptor->did_intercept_);
201 EXPECT_EQ(URLRequestStatus::FAILED, request.status().status());
202 EXPECT_EQ(ERR_FAILED, request.status().error());
205 TEST(URLRequestJobFactoryTest, InterceptorAffectsIsHandledProtocol) {
206 DummyInterceptor* interceptor = new DummyInterceptor;
207 URLRequestJobFactoryImpl job_factory;
208 job_factory.AddInterceptor(interceptor);
209 EXPECT_FALSE(interceptor->WillHandleProtocol("anything"));
210 EXPECT_FALSE(job_factory.IsHandledProtocol("anything"));
211 interceptor->handle_all_protocols_ = true;
212 EXPECT_TRUE(interceptor->WillHandleProtocol("anything"));
213 EXPECT_TRUE(job_factory.IsHandledProtocol("anything"));
216 } // namespace
218 } // namespace net