OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "net/url_request/url_request_job_factory_impl.h" | 5 #include "net/url_request/url_request_job_factory_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
9 #include "net/url_request/url_request.h" | 9 #include "net/url_request/url_request.h" |
10 #include "net/url_request/url_request_job.h" | 10 #include "net/url_request/url_request_job.h" |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 public: | 50 public: |
51 virtual URLRequestJob* MaybeCreateJob( | 51 virtual URLRequestJob* MaybeCreateJob( |
52 URLRequest* request, NetworkDelegate* network_delegate) const OVERRIDE { | 52 URLRequest* request, NetworkDelegate* network_delegate) const OVERRIDE { |
53 return new MockURLRequestJob( | 53 return new MockURLRequestJob( |
54 request, | 54 request, |
55 network_delegate, | 55 network_delegate, |
56 URLRequestStatus(URLRequestStatus::SUCCESS, OK)); | 56 URLRequestStatus(URLRequestStatus::SUCCESS, OK)); |
57 } | 57 } |
58 }; | 58 }; |
59 | 59 |
60 class DummyInterceptor : public URLRequestJobFactory::Interceptor { | |
61 public: | |
62 DummyInterceptor() | |
63 : did_intercept_(false), | |
64 handle_all_protocols_(false) { | |
65 } | |
66 | |
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)); | |
74 } | |
75 | |
76 virtual URLRequestJob* MaybeInterceptRedirect( | |
77 const GURL& /* location */, | |
78 URLRequest* /* request */, | |
79 NetworkDelegate* network_delegate /* network delegate */) const OVERRIDE { | |
80 return NULL; | |
81 } | |
82 | |
83 virtual URLRequestJob* MaybeInterceptResponse( | |
84 URLRequest* /* request */, | |
85 NetworkDelegate* network_delegate /* network delegate */) const OVERRIDE { | |
86 return NULL; | |
87 } | |
88 | |
89 virtual bool WillHandleProtocol( | |
90 const std::string& /* protocol */) const OVERRIDE { | |
91 return handle_all_protocols_; | |
92 } | |
93 | |
94 mutable bool did_intercept_; | |
95 mutable bool handle_all_protocols_; | |
96 }; | |
97 | |
98 TEST(URLRequestJobFactoryTest, NoProtocolHandler) { | 60 TEST(URLRequestJobFactoryTest, NoProtocolHandler) { |
99 TestDelegate delegate; | 61 TestDelegate delegate; |
100 TestURLRequestContext request_context; | 62 TestURLRequestContext request_context; |
101 TestURLRequest request(GURL("foo://bar"), &delegate, &request_context); | 63 TestURLRequest request(GURL("foo://bar"), &delegate, &request_context); |
102 request.Start(); | 64 request.Start(); |
103 | 65 |
104 MessageLoop::current()->Run(); | 66 MessageLoop::current()->Run(); |
105 EXPECT_EQ(URLRequestStatus::FAILED, request.status().status()); | 67 EXPECT_EQ(URLRequestStatus::FAILED, request.status().status()); |
106 EXPECT_EQ(ERR_UNKNOWN_URL_SCHEME, request.status().error()); | 68 EXPECT_EQ(ERR_UNKNOWN_URL_SCHEME, request.status().error()); |
107 } | 69 } |
(...skipping 13 matching lines...) Expand all Loading... |
121 } | 83 } |
122 | 84 |
123 TEST(URLRequestJobFactoryTest, DeleteProtocolHandler) { | 85 TEST(URLRequestJobFactoryTest, DeleteProtocolHandler) { |
124 URLRequestJobFactoryImpl job_factory; | 86 URLRequestJobFactoryImpl job_factory; |
125 TestURLRequestContext request_context; | 87 TestURLRequestContext request_context; |
126 request_context.set_job_factory(&job_factory); | 88 request_context.set_job_factory(&job_factory); |
127 job_factory.SetProtocolHandler("foo", new DummyProtocolHandler); | 89 job_factory.SetProtocolHandler("foo", new DummyProtocolHandler); |
128 job_factory.SetProtocolHandler("foo", NULL); | 90 job_factory.SetProtocolHandler("foo", NULL); |
129 } | 91 } |
130 | 92 |
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(); | |
139 | |
140 MessageLoop::current()->Run(); | |
141 EXPECT_EQ(URLRequestStatus::FAILED, request.status().status()); | |
142 EXPECT_EQ(ERR_FAILED, request.status().error()); | |
143 } | |
144 | |
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(); | |
153 | |
154 MessageLoop::current()->Run(); | |
155 EXPECT_EQ(URLRequestStatus::FAILED, request.status().status()); | |
156 EXPECT_EQ(ERR_UNKNOWN_URL_SCHEME, request.status().error()); | |
157 } | |
158 | |
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(); | |
168 | |
169 MessageLoop::current()->Run(); | |
170 EXPECT_EQ(URLRequestStatus::FAILED, request.status().status()); | |
171 EXPECT_EQ(ERR_FAILED, request.status().error()); | |
172 } | |
173 | |
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(); | |
183 | |
184 MessageLoop::current()->Run(); | |
185 EXPECT_FALSE(interceptor->did_intercept_); | |
186 } | |
187 | |
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(); | |
198 | |
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()); | |
203 } | |
204 | |
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")); | |
214 } | |
215 | |
216 } // namespace | 93 } // namespace |
217 | 94 |
218 } // namespace net | 95 } // namespace net |
OLD | NEW |