url_request_job_factory_impl_unittest.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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. 4 5#include "net/url_request/url_request_job_factory_impl.h" 6 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" 13 14namespace net { 15 16namespace { 17 18class 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)) {} 26 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())); 34 } 35 36 protected: 37 virtual ~MockURLRequestJob() {} 38 39 private: 40 void StartAsync() { 41 SetStatus(status_); 42 NotifyHeadersComplete(); 43 } 44 45 URLRequestStatus status_; 46 base::WeakPtrFactory<MockURLRequestJob> weak_factory_; 47}; 48 49class 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)); 57 } 58}; 59 60class 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 98TEST(URLRequestJobFactoryTest, NoProtocolHandler) { 99 TestDelegate delegate; 100 TestURLRequestContext request_context; 101 TestURLRequest request(GURL("foo://bar"), &delegate, &request_context); 102 request.Start(); 103 104 MessageLoop::current()->Run(); 105 EXPECT_EQ(URLRequestStatus::FAILED, request.status().status()); 106 EXPECT_EQ(ERR_UNKNOWN_URL_SCHEME, request.status().error()); 107} 108 109TEST(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(); 117 118 MessageLoop::current()->Run(); 119 EXPECT_EQ(URLRequestStatus::SUCCESS, request.status().status()); 120 EXPECT_EQ(OK, request.status().error()); 121} 122 123TEST(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); 129} 130 131TEST(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 145TEST(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 159TEST(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 174TEST(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 188TEST(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 205TEST(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 217 218} // namespace net 219