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