| 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_impl.h" | 5 #include "net/url_request/url_request_job_factory_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| 11 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
| 12 #include "base/thread_task_runner_handle.h" | 12 #include "base/thread_task_runner_handle.h" |
| 13 #include "net/base/request_priority.h" | 13 #include "net/base/request_priority.h" |
| 14 #include "net/url_request/url_request.h" | 14 #include "net/url_request/url_request.h" |
| 15 #include "net/url_request/url_request_job.h" | 15 #include "net/url_request/url_request_job.h" |
| 16 #include "net/url_request/url_request_test_util.h" | 16 #include "net/url_request/url_request_test_util.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 18 |
| 19 namespace net { | 19 namespace net { |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 class MockURLRequestJob : public URLRequestJob { | 23 class MockURLRequestJob : public URLRequestJob { |
| 24 public: | 24 public: |
| 25 MockURLRequestJob(URLRequest* request, | 25 MockURLRequestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 26 NetworkDelegate* network_delegate, | 26 : URLRequestJob(request, network_delegate), weak_factory_(this) {} |
| 27 const URLRequestStatus& status) | |
| 28 : URLRequestJob(request, network_delegate), | |
| 29 status_(status), | |
| 30 weak_factory_(this) {} | |
| 31 | 27 |
| 32 void Start() override { | 28 void Start() override { |
| 33 // Start reading asynchronously so that all error reporting and data | 29 // Start reading asynchronously so that all error reporting and data |
| 34 // callbacks happen as they would for network requests. | 30 // callbacks happen as they would for network requests. |
| 35 base::ThreadTaskRunnerHandle::Get()->PostTask( | 31 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 36 FROM_HERE, | 32 FROM_HERE, |
| 37 base::Bind(&MockURLRequestJob::StartAsync, weak_factory_.GetWeakPtr())); | 33 base::Bind(&MockURLRequestJob::StartAsync, weak_factory_.GetWeakPtr())); |
| 38 } | 34 } |
| 39 | 35 |
| 40 protected: | 36 protected: |
| 41 ~MockURLRequestJob() override {} | 37 ~MockURLRequestJob() override {} |
| 42 | 38 |
| 43 private: | 39 private: |
| 44 void StartAsync() { | 40 void StartAsync() { |
| 45 SetStatus(status_); | |
| 46 NotifyHeadersComplete(); | 41 NotifyHeadersComplete(); |
| 47 } | 42 } |
| 48 | 43 |
| 49 URLRequestStatus status_; | |
| 50 base::WeakPtrFactory<MockURLRequestJob> weak_factory_; | 44 base::WeakPtrFactory<MockURLRequestJob> weak_factory_; |
| 51 }; | 45 }; |
| 52 | 46 |
| 53 class DummyProtocolHandler : public URLRequestJobFactory::ProtocolHandler { | 47 class DummyProtocolHandler : public URLRequestJobFactory::ProtocolHandler { |
| 54 public: | 48 public: |
| 55 URLRequestJob* MaybeCreateJob( | 49 URLRequestJob* MaybeCreateJob( |
| 56 URLRequest* request, | 50 URLRequest* request, |
| 57 NetworkDelegate* network_delegate) const override { | 51 NetworkDelegate* network_delegate) const override { |
| 58 return new MockURLRequestJob( | 52 return new MockURLRequestJob(request, network_delegate); |
| 59 request, | |
| 60 network_delegate, | |
| 61 URLRequestStatus(URLRequestStatus::SUCCESS, OK)); | |
| 62 } | 53 } |
| 63 }; | 54 }; |
| 64 | 55 |
| 65 TEST(URLRequestJobFactoryTest, NoProtocolHandler) { | 56 TEST(URLRequestJobFactoryTest, NoProtocolHandler) { |
| 66 TestDelegate delegate; | 57 TestDelegate delegate; |
| 67 TestURLRequestContext request_context; | 58 TestURLRequestContext request_context; |
| 68 scoped_ptr<URLRequest> request(request_context.CreateRequest( | 59 scoped_ptr<URLRequest> request(request_context.CreateRequest( |
| 69 GURL("foo://bar"), DEFAULT_PRIORITY, &delegate)); | 60 GURL("foo://bar"), DEFAULT_PRIORITY, &delegate)); |
| 70 request->Start(); | 61 request->Start(); |
| 71 | 62 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 95 TestURLRequestContext request_context; | 86 TestURLRequestContext request_context; |
| 96 request_context.set_job_factory(&job_factory); | 87 request_context.set_job_factory(&job_factory); |
| 97 job_factory.SetProtocolHandler("foo", | 88 job_factory.SetProtocolHandler("foo", |
| 98 make_scoped_ptr(new DummyProtocolHandler)); | 89 make_scoped_ptr(new DummyProtocolHandler)); |
| 99 job_factory.SetProtocolHandler("foo", nullptr); | 90 job_factory.SetProtocolHandler("foo", nullptr); |
| 100 } | 91 } |
| 101 | 92 |
| 102 } // namespace | 93 } // namespace |
| 103 | 94 |
| 104 } // namespace net | 95 } // namespace net |
| OLD | NEW |