OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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.h" | 5 #include "net/url_request/url_request_job_factory.h" |
6 | 6 |
7 #include "base/task.h" | 7 #include "base/task.h" |
8 #include "net/url_request/url_request_job.h" | 8 #include "net/url_request/url_request_job.h" |
9 #include "net/url_request/url_request_test_util.h" | 9 #include "net/url_request/url_request_test_util.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 30 matching lines...) Expand all Loading... | |
41 class DummyProtocolHandler : public URLRequestJobFactory::ProtocolHandler { | 41 class DummyProtocolHandler : public URLRequestJobFactory::ProtocolHandler { |
42 public: | 42 public: |
43 virtual URLRequestJob* MaybeCreateJob(URLRequest* request) const { | 43 virtual URLRequestJob* MaybeCreateJob(URLRequest* request) const { |
44 return new MockURLRequestJob( | 44 return new MockURLRequestJob( |
45 request, URLRequestStatus(URLRequestStatus::SUCCESS, OK)); | 45 request, URLRequestStatus(URLRequestStatus::SUCCESS, OK)); |
46 } | 46 } |
47 }; | 47 }; |
48 | 48 |
49 class DummyInterceptor : public URLRequestJobFactory::Interceptor { | 49 class DummyInterceptor : public URLRequestJobFactory::Interceptor { |
50 public: | 50 public: |
51 DummyInterceptor() | |
52 : did_intercept_(false), | |
53 handle_all_protocols_(false) { } | |
54 | |
51 virtual URLRequestJob* MaybeIntercept(URLRequest* request) const { | 55 virtual URLRequestJob* MaybeIntercept(URLRequest* request) const { |
56 did_intercept_ = true; | |
52 return new MockURLRequestJob( | 57 return new MockURLRequestJob( |
53 request, | 58 request, |
54 URLRequestStatus(URLRequestStatus::FAILED, ERR_FAILED)); | 59 URLRequestStatus(URLRequestStatus::FAILED, ERR_FAILED)); |
55 } | 60 } |
56 | 61 |
57 virtual URLRequestJob* MaybeInterceptRedirect( | 62 virtual URLRequestJob* MaybeInterceptRedirect( |
58 const GURL& /* location */, | 63 const GURL& /* location */, |
59 URLRequest* /* request */) const { | 64 URLRequest* /* request */) const { |
60 return NULL; | 65 return NULL; |
61 } | 66 } |
62 | 67 |
63 virtual URLRequestJob* MaybeInterceptResponse( | 68 virtual URLRequestJob* MaybeInterceptResponse( |
64 URLRequest* /* request */) const { | 69 URLRequest* /* request */) const { |
65 return NULL; | 70 return NULL; |
66 } | 71 } |
72 | |
73 virtual bool WillHandleProtocol( | |
74 const std::string& /* protocol */) const { | |
75 return handle_all_protocols_; | |
76 } | |
77 | |
78 mutable bool did_intercept_; | |
79 mutable bool handle_all_protocols_; | |
67 }; | 80 }; |
68 | 81 |
69 TEST(URLRequestJobFactoryTest, NoProtocolHandler) { | 82 TEST(URLRequestJobFactoryTest, NoProtocolHandler) { |
70 TestDelegate delegate; | 83 TestDelegate delegate; |
71 scoped_refptr<URLRequestContext> request_context(new TestURLRequestContext); | 84 scoped_refptr<URLRequestContext> request_context(new TestURLRequestContext); |
72 TestURLRequest request(GURL("foo://bar"), &delegate); | 85 TestURLRequest request(GURL("foo://bar"), &delegate); |
73 request.set_context(request_context); | 86 request.set_context(request_context); |
74 request.Start(); | 87 request.Start(); |
75 | 88 |
76 MessageLoop::current()->Run(); | 89 MessageLoop::current()->Run(); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
140 job_factory.AddInterceptor(new DummyInterceptor); | 153 job_factory.AddInterceptor(new DummyInterceptor); |
141 TestURLRequest request(GURL("foo://bar"), &delegate); | 154 TestURLRequest request(GURL("foo://bar"), &delegate); |
142 request.set_context(request_context); | 155 request.set_context(request_context); |
143 request.Start(); | 156 request.Start(); |
144 | 157 |
145 MessageLoop::current()->Run(); | 158 MessageLoop::current()->Run(); |
146 EXPECT_EQ(URLRequestStatus::FAILED, request.status().status()); | 159 EXPECT_EQ(URLRequestStatus::FAILED, request.status().status()); |
147 EXPECT_EQ(ERR_FAILED, request.status().os_error()); | 160 EXPECT_EQ(ERR_FAILED, request.status().os_error()); |
148 } | 161 } |
149 | 162 |
163 TEST(URLRequestJobFactoryTest, InterceptorDoesntInterceptUnknownProtocols) { | |
164 TestDelegate delegate; | |
165 scoped_refptr<URLRequestContext> request_context(new TestURLRequestContext); | |
166 URLRequestJobFactory job_factory; | |
167 request_context->set_job_factory(&job_factory); | |
168 DummyInterceptor* interceptor = new DummyInterceptor; | |
oshima
2011/05/25 22:45:23
This seems to be leaking. I also wonder if "new Du
willchan no longer on Chromium
2011/05/25 22:53:56
Yes, that's correct. We should fix this.
koz (OOO until 15th September)
2011/05/25 22:54:58
Yep, fixing now.
| |
169 job_factory.AddInterceptor(new DummyInterceptor); | |
170 TestURLRequest request(GURL("foo://bar"), &delegate); | |
171 request.set_context(request_context); | |
172 request.Start(); | |
173 | |
174 MessageLoop::current()->Run(); | |
175 EXPECT_FALSE(interceptor->did_intercept_); | |
176 } | |
177 | |
178 TEST(URLRequestJobFactoryTest, InterceptorInterceptsHandledUnknownProtocols) { | |
179 TestDelegate delegate; | |
180 scoped_refptr<URLRequestContext> request_context(new TestURLRequestContext); | |
181 URLRequestJobFactory job_factory; | |
182 request_context->set_job_factory(&job_factory); | |
183 DummyInterceptor* interceptor = new DummyInterceptor; | |
184 interceptor->handle_all_protocols_ = true; | |
185 job_factory.AddInterceptor(interceptor); | |
186 TestURLRequest request(GURL("foo://bar"), &delegate); | |
187 request.set_context(request_context); | |
188 request.Start(); | |
189 | |
190 MessageLoop::current()->Run(); | |
191 EXPECT_TRUE(interceptor->did_intercept_); | |
192 EXPECT_EQ(URLRequestStatus::FAILED, request.status().status()); | |
193 EXPECT_EQ(ERR_FAILED, request.status().os_error()); | |
194 } | |
195 | |
196 TEST(URLRequestJobFactoryTest, InterceptorAffectsIsHandledProtocol) { | |
197 DummyInterceptor* interceptor = new DummyInterceptor; | |
198 URLRequestJobFactory job_factory; | |
199 job_factory.AddInterceptor(interceptor); | |
200 EXPECT_FALSE(interceptor->WillHandleProtocol("anything")); | |
201 EXPECT_FALSE(job_factory.IsHandledProtocol("anything")); | |
202 interceptor->handle_all_protocols_ = true; | |
203 EXPECT_TRUE(interceptor->WillHandleProtocol("anything")); | |
204 EXPECT_TRUE(job_factory.IsHandledProtocol("anything")); | |
205 } | |
206 | |
150 } // namespace | 207 } // namespace |
151 | 208 |
152 } // namespace net | 209 } // namespace net |
OLD | NEW |