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/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_job.h" | 9 #include "net/url_request/url_request_job.h" |
10 #include "net/url_request/url_request_test_util.h" | 10 #include "net/url_request/url_request_test_util.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 | 12 |
13 namespace net { | 13 namespace net { |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 class MockURLRequestJob : public URLRequestJob { | 17 class MockURLRequestJob : public URLRequestJob { |
18 public: | 18 public: |
19 MockURLRequestJob(URLRequest* request, const URLRequestStatus& status) | 19 MockURLRequestJob(URLRequest* request, const URLRequestStatus& status) |
20 : URLRequestJob(request), | 20 : URLRequestJob(request), |
21 status_(status), | 21 status_(status), |
22 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {} | 22 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {} |
23 | 23 |
24 virtual void Start() { | 24 virtual void Start() OVERRIDE { |
25 // Start reading asynchronously so that all error reporting and data | 25 // Start reading asynchronously so that all error reporting and data |
26 // callbacks happen as they would for network requests. | 26 // callbacks happen as they would for network requests. |
27 MessageLoop::current()->PostTask( | 27 MessageLoop::current()->PostTask( |
28 FROM_HERE, | 28 FROM_HERE, |
29 base::Bind(&MockURLRequestJob::StartAsync, | 29 base::Bind(&MockURLRequestJob::StartAsync, |
30 weak_factory_.GetWeakPtr())); | 30 weak_factory_.GetWeakPtr())); |
31 } | 31 } |
32 | 32 |
| 33 protected: |
| 34 virtual ~MockURLRequestJob() {} |
| 35 |
33 private: | 36 private: |
34 void StartAsync() { | 37 void StartAsync() { |
35 SetStatus(status_); | 38 SetStatus(status_); |
36 NotifyHeadersComplete(); | 39 NotifyHeadersComplete(); |
37 } | 40 } |
38 | 41 |
39 URLRequestStatus status_; | 42 URLRequestStatus status_; |
40 base::WeakPtrFactory<MockURLRequestJob> weak_factory_; | 43 base::WeakPtrFactory<MockURLRequestJob> weak_factory_; |
41 }; | 44 }; |
42 | 45 |
43 class DummyProtocolHandler : public URLRequestJobFactory::ProtocolHandler { | 46 class DummyProtocolHandler : public URLRequestJobFactory::ProtocolHandler { |
44 public: | 47 public: |
45 virtual URLRequestJob* MaybeCreateJob(URLRequest* request) const { | 48 virtual URLRequestJob* MaybeCreateJob(URLRequest* request) const OVERRIDE { |
46 return new MockURLRequestJob( | 49 return new MockURLRequestJob( |
47 request, URLRequestStatus(URLRequestStatus::SUCCESS, OK)); | 50 request, URLRequestStatus(URLRequestStatus::SUCCESS, OK)); |
48 } | 51 } |
49 }; | 52 }; |
50 | 53 |
51 class DummyInterceptor : public URLRequestJobFactory::Interceptor { | 54 class DummyInterceptor : public URLRequestJobFactory::Interceptor { |
52 public: | 55 public: |
53 DummyInterceptor() | 56 DummyInterceptor() |
54 : did_intercept_(false), | 57 : did_intercept_(false), |
55 handle_all_protocols_(false) { } | 58 handle_all_protocols_(false) { |
| 59 } |
56 | 60 |
57 virtual URLRequestJob* MaybeIntercept(URLRequest* request) const { | 61 virtual URLRequestJob* MaybeIntercept(URLRequest* request) const OVERRIDE { |
58 did_intercept_ = true; | 62 did_intercept_ = true; |
59 return new MockURLRequestJob( | 63 return new MockURLRequestJob( |
60 request, | 64 request, |
61 URLRequestStatus(URLRequestStatus::FAILED, ERR_FAILED)); | 65 URLRequestStatus(URLRequestStatus::FAILED, ERR_FAILED)); |
62 } | 66 } |
63 | 67 |
64 virtual URLRequestJob* MaybeInterceptRedirect( | 68 virtual URLRequestJob* MaybeInterceptRedirect( |
65 const GURL& /* location */, | 69 const GURL& /* location */, |
66 URLRequest* /* request */) const { | 70 URLRequest* /* request */) const OVERRIDE { |
67 return NULL; | 71 return NULL; |
68 } | 72 } |
69 | 73 |
70 virtual URLRequestJob* MaybeInterceptResponse( | 74 virtual URLRequestJob* MaybeInterceptResponse( |
71 URLRequest* /* request */) const { | 75 URLRequest* /* request */) const OVERRIDE { |
72 return NULL; | 76 return NULL; |
73 } | 77 } |
74 | 78 |
75 virtual bool WillHandleProtocol( | 79 virtual bool WillHandleProtocol( |
76 const std::string& /* protocol */) const { | 80 const std::string& /* protocol */) const OVERRIDE { |
77 return handle_all_protocols_; | 81 return handle_all_protocols_; |
78 } | 82 } |
79 | 83 |
80 mutable bool did_intercept_; | 84 mutable bool did_intercept_; |
81 mutable bool handle_all_protocols_; | 85 mutable bool handle_all_protocols_; |
82 }; | 86 }; |
83 | 87 |
84 TEST(URLRequestJobFactoryTest, NoProtocolHandler) { | 88 TEST(URLRequestJobFactoryTest, NoProtocolHandler) { |
85 TestDelegate delegate; | 89 TestDelegate delegate; |
86 scoped_refptr<URLRequestContext> request_context(new TestURLRequestContext); | 90 scoped_refptr<URLRequestContext> request_context(new TestURLRequestContext); |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 EXPECT_FALSE(interceptor->WillHandleProtocol("anything")); | 206 EXPECT_FALSE(interceptor->WillHandleProtocol("anything")); |
203 EXPECT_FALSE(job_factory.IsHandledProtocol("anything")); | 207 EXPECT_FALSE(job_factory.IsHandledProtocol("anything")); |
204 interceptor->handle_all_protocols_ = true; | 208 interceptor->handle_all_protocols_ = true; |
205 EXPECT_TRUE(interceptor->WillHandleProtocol("anything")); | 209 EXPECT_TRUE(interceptor->WillHandleProtocol("anything")); |
206 EXPECT_TRUE(job_factory.IsHandledProtocol("anything")); | 210 EXPECT_TRUE(job_factory.IsHandledProtocol("anything")); |
207 } | 211 } |
208 | 212 |
209 } // namespace | 213 } // namespace |
210 | 214 |
211 } // namespace net | 215 } // namespace net |
OLD | NEW |