| 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 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 | 81 |
| 82 TEST(URLRequestJobFactoryTest, NoProtocolHandler) { | 82 TEST(URLRequestJobFactoryTest, NoProtocolHandler) { |
| 83 TestDelegate delegate; | 83 TestDelegate delegate; |
| 84 scoped_refptr<URLRequestContext> request_context(new TestURLRequestContext); | 84 scoped_refptr<URLRequestContext> request_context(new TestURLRequestContext); |
| 85 TestURLRequest request(GURL("foo://bar"), &delegate); | 85 TestURLRequest request(GURL("foo://bar"), &delegate); |
| 86 request.set_context(request_context); | 86 request.set_context(request_context); |
| 87 request.Start(); | 87 request.Start(); |
| 88 | 88 |
| 89 MessageLoop::current()->Run(); | 89 MessageLoop::current()->Run(); |
| 90 EXPECT_EQ(URLRequestStatus::FAILED, request.status().status()); | 90 EXPECT_EQ(URLRequestStatus::FAILED, request.status().status()); |
| 91 EXPECT_EQ(ERR_UNKNOWN_URL_SCHEME, request.status().os_error()); | 91 EXPECT_EQ(ERR_UNKNOWN_URL_SCHEME, request.status().error()); |
| 92 } | 92 } |
| 93 | 93 |
| 94 TEST(URLRequestJobFactoryTest, BasicProtocolHandler) { | 94 TEST(URLRequestJobFactoryTest, BasicProtocolHandler) { |
| 95 TestDelegate delegate; | 95 TestDelegate delegate; |
| 96 scoped_refptr<URLRequestContext> request_context(new TestURLRequestContext); | 96 scoped_refptr<URLRequestContext> request_context(new TestURLRequestContext); |
| 97 URLRequestJobFactory job_factory; | 97 URLRequestJobFactory job_factory; |
| 98 request_context->set_job_factory(&job_factory); | 98 request_context->set_job_factory(&job_factory); |
| 99 job_factory.SetProtocolHandler("foo", new DummyProtocolHandler); | 99 job_factory.SetProtocolHandler("foo", new DummyProtocolHandler); |
| 100 TestURLRequest request(GURL("foo://bar"), &delegate); | 100 TestURLRequest request(GURL("foo://bar"), &delegate); |
| 101 request.set_context(request_context); | 101 request.set_context(request_context); |
| 102 request.Start(); | 102 request.Start(); |
| 103 | 103 |
| 104 MessageLoop::current()->Run(); | 104 MessageLoop::current()->Run(); |
| 105 EXPECT_EQ(URLRequestStatus::SUCCESS, request.status().status()); | 105 EXPECT_EQ(URLRequestStatus::SUCCESS, request.status().status()); |
| 106 EXPECT_EQ(OK, request.status().os_error()); | 106 EXPECT_EQ(OK, request.status().error()); |
| 107 } | 107 } |
| 108 | 108 |
| 109 TEST(URLRequestJobFactoryTest, DeleteProtocolHandler) { | 109 TEST(URLRequestJobFactoryTest, DeleteProtocolHandler) { |
| 110 scoped_refptr<URLRequestContext> request_context(new TestURLRequestContext); | 110 scoped_refptr<URLRequestContext> request_context(new TestURLRequestContext); |
| 111 URLRequestJobFactory job_factory; | 111 URLRequestJobFactory job_factory; |
| 112 request_context->set_job_factory(&job_factory); | 112 request_context->set_job_factory(&job_factory); |
| 113 job_factory.SetProtocolHandler("foo", new DummyProtocolHandler); | 113 job_factory.SetProtocolHandler("foo", new DummyProtocolHandler); |
| 114 job_factory.SetProtocolHandler("foo", NULL); | 114 job_factory.SetProtocolHandler("foo", NULL); |
| 115 } | 115 } |
| 116 | 116 |
| 117 TEST(URLRequestJobFactoryTest, BasicInterceptor) { | 117 TEST(URLRequestJobFactoryTest, BasicInterceptor) { |
| 118 TestDelegate delegate; | 118 TestDelegate delegate; |
| 119 scoped_refptr<URLRequestContext> request_context(new TestURLRequestContext); | 119 scoped_refptr<URLRequestContext> request_context(new TestURLRequestContext); |
| 120 URLRequestJobFactory job_factory; | 120 URLRequestJobFactory job_factory; |
| 121 request_context->set_job_factory(&job_factory); | 121 request_context->set_job_factory(&job_factory); |
| 122 job_factory.AddInterceptor(new DummyInterceptor); | 122 job_factory.AddInterceptor(new DummyInterceptor); |
| 123 TestURLRequest request(GURL("http://bar"), &delegate); | 123 TestURLRequest request(GURL("http://bar"), &delegate); |
| 124 request.set_context(request_context); | 124 request.set_context(request_context); |
| 125 request.Start(); | 125 request.Start(); |
| 126 | 126 |
| 127 MessageLoop::current()->Run(); | 127 MessageLoop::current()->Run(); |
| 128 EXPECT_EQ(URLRequestStatus::FAILED, request.status().status()); | 128 EXPECT_EQ(URLRequestStatus::FAILED, request.status().status()); |
| 129 EXPECT_EQ(ERR_FAILED, request.status().os_error()); | 129 EXPECT_EQ(ERR_FAILED, request.status().error()); |
| 130 } | 130 } |
| 131 | 131 |
| 132 TEST(URLRequestJobFactoryTest, InterceptorNeedsValidSchemeStill) { | 132 TEST(URLRequestJobFactoryTest, InterceptorNeedsValidSchemeStill) { |
| 133 TestDelegate delegate; | 133 TestDelegate delegate; |
| 134 scoped_refptr<URLRequestContext> request_context(new TestURLRequestContext); | 134 scoped_refptr<URLRequestContext> request_context(new TestURLRequestContext); |
| 135 URLRequestJobFactory job_factory; | 135 URLRequestJobFactory job_factory; |
| 136 request_context->set_job_factory(&job_factory); | 136 request_context->set_job_factory(&job_factory); |
| 137 job_factory.AddInterceptor(new DummyInterceptor); | 137 job_factory.AddInterceptor(new DummyInterceptor); |
| 138 TestURLRequest request(GURL("foo://bar"), &delegate); | 138 TestURLRequest request(GURL("foo://bar"), &delegate); |
| 139 request.set_context(request_context); | 139 request.set_context(request_context); |
| 140 request.Start(); | 140 request.Start(); |
| 141 | 141 |
| 142 MessageLoop::current()->Run(); | 142 MessageLoop::current()->Run(); |
| 143 EXPECT_EQ(URLRequestStatus::FAILED, request.status().status()); | 143 EXPECT_EQ(URLRequestStatus::FAILED, request.status().status()); |
| 144 EXPECT_EQ(ERR_UNKNOWN_URL_SCHEME, request.status().os_error()); | 144 EXPECT_EQ(ERR_UNKNOWN_URL_SCHEME, request.status().error()); |
| 145 } | 145 } |
| 146 | 146 |
| 147 TEST(URLRequestJobFactoryTest, InterceptorOverridesProtocolHandler) { | 147 TEST(URLRequestJobFactoryTest, InterceptorOverridesProtocolHandler) { |
| 148 TestDelegate delegate; | 148 TestDelegate delegate; |
| 149 scoped_refptr<URLRequestContext> request_context(new TestURLRequestContext); | 149 scoped_refptr<URLRequestContext> request_context(new TestURLRequestContext); |
| 150 URLRequestJobFactory job_factory; | 150 URLRequestJobFactory job_factory; |
| 151 request_context->set_job_factory(&job_factory); | 151 request_context->set_job_factory(&job_factory); |
| 152 job_factory.SetProtocolHandler("foo", new DummyProtocolHandler); | 152 job_factory.SetProtocolHandler("foo", new DummyProtocolHandler); |
| 153 job_factory.AddInterceptor(new DummyInterceptor); | 153 job_factory.AddInterceptor(new DummyInterceptor); |
| 154 TestURLRequest request(GURL("foo://bar"), &delegate); | 154 TestURLRequest request(GURL("foo://bar"), &delegate); |
| 155 request.set_context(request_context); | 155 request.set_context(request_context); |
| 156 request.Start(); | 156 request.Start(); |
| 157 | 157 |
| 158 MessageLoop::current()->Run(); | 158 MessageLoop::current()->Run(); |
| 159 EXPECT_EQ(URLRequestStatus::FAILED, request.status().status()); | 159 EXPECT_EQ(URLRequestStatus::FAILED, request.status().status()); |
| 160 EXPECT_EQ(ERR_FAILED, request.status().os_error()); | 160 EXPECT_EQ(ERR_FAILED, request.status().error()); |
| 161 } | 161 } |
| 162 | 162 |
| 163 TEST(URLRequestJobFactoryTest, InterceptorDoesntInterceptUnknownProtocols) { | 163 TEST(URLRequestJobFactoryTest, InterceptorDoesntInterceptUnknownProtocols) { |
| 164 TestDelegate delegate; | 164 TestDelegate delegate; |
| 165 scoped_refptr<URLRequestContext> request_context(new TestURLRequestContext); | 165 scoped_refptr<URLRequestContext> request_context(new TestURLRequestContext); |
| 166 URLRequestJobFactory job_factory; | 166 URLRequestJobFactory job_factory; |
| 167 request_context->set_job_factory(&job_factory); | 167 request_context->set_job_factory(&job_factory); |
| 168 DummyInterceptor* interceptor = new DummyInterceptor; | 168 DummyInterceptor* interceptor = new DummyInterceptor; |
| 169 job_factory.AddInterceptor(interceptor); | 169 job_factory.AddInterceptor(interceptor); |
| 170 TestURLRequest request(GURL("foo://bar"), &delegate); | 170 TestURLRequest request(GURL("foo://bar"), &delegate); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 183 DummyInterceptor* interceptor = new DummyInterceptor; | 183 DummyInterceptor* interceptor = new DummyInterceptor; |
| 184 interceptor->handle_all_protocols_ = true; | 184 interceptor->handle_all_protocols_ = true; |
| 185 job_factory.AddInterceptor(interceptor); | 185 job_factory.AddInterceptor(interceptor); |
| 186 TestURLRequest request(GURL("foo://bar"), &delegate); | 186 TestURLRequest request(GURL("foo://bar"), &delegate); |
| 187 request.set_context(request_context); | 187 request.set_context(request_context); |
| 188 request.Start(); | 188 request.Start(); |
| 189 | 189 |
| 190 MessageLoop::current()->Run(); | 190 MessageLoop::current()->Run(); |
| 191 EXPECT_TRUE(interceptor->did_intercept_); | 191 EXPECT_TRUE(interceptor->did_intercept_); |
| 192 EXPECT_EQ(URLRequestStatus::FAILED, request.status().status()); | 192 EXPECT_EQ(URLRequestStatus::FAILED, request.status().status()); |
| 193 EXPECT_EQ(ERR_FAILED, request.status().os_error()); | 193 EXPECT_EQ(ERR_FAILED, request.status().error()); |
| 194 } | 194 } |
| 195 | 195 |
| 196 TEST(URLRequestJobFactoryTest, InterceptorAffectsIsHandledProtocol) { | 196 TEST(URLRequestJobFactoryTest, InterceptorAffectsIsHandledProtocol) { |
| 197 DummyInterceptor* interceptor = new DummyInterceptor; | 197 DummyInterceptor* interceptor = new DummyInterceptor; |
| 198 URLRequestJobFactory job_factory; | 198 URLRequestJobFactory job_factory; |
| 199 job_factory.AddInterceptor(interceptor); | 199 job_factory.AddInterceptor(interceptor); |
| 200 EXPECT_FALSE(interceptor->WillHandleProtocol("anything")); | 200 EXPECT_FALSE(interceptor->WillHandleProtocol("anything")); |
| 201 EXPECT_FALSE(job_factory.IsHandledProtocol("anything")); | 201 EXPECT_FALSE(job_factory.IsHandledProtocol("anything")); |
| 202 interceptor->handle_all_protocols_ = true; | 202 interceptor->handle_all_protocols_ = true; |
| 203 EXPECT_TRUE(interceptor->WillHandleProtocol("anything")); | 203 EXPECT_TRUE(interceptor->WillHandleProtocol("anything")); |
| 204 EXPECT_TRUE(job_factory.IsHandledProtocol("anything")); | 204 EXPECT_TRUE(job_factory.IsHandledProtocol("anything")); |
| 205 } | 205 } |
| 206 | 206 |
| 207 } // namespace | 207 } // namespace |
| 208 | 208 |
| 209 } // namespace net | 209 } // namespace net |
| OLD | NEW |