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.
5 #include "net/url_request/url_request_job_factory_impl.h"
8 #include "base/location.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "net/base/request_priority.h"
14 #include "net/url_request/url_request.h"
15 #include "net/url_request/url_request_job.h"
16 #include "net/url_request/url_request_test_util.h"
17 #include "testing/gtest/include/gtest/gtest.h"
23 class MockURLRequestJob
: public URLRequestJob
{
25 MockURLRequestJob(URLRequest
* request
,
26 NetworkDelegate
* network_delegate
,
27 const URLRequestStatus
& status
)
28 : URLRequestJob(request
, network_delegate
),
30 weak_factory_(this) {}
32 void Start() override
{
33 // Start reading asynchronously so that all error reporting and data
34 // callbacks happen as they would for network requests.
35 base::ThreadTaskRunnerHandle::Get()->PostTask(
37 base::Bind(&MockURLRequestJob::StartAsync
, weak_factory_
.GetWeakPtr()));
41 ~MockURLRequestJob() override
{}
46 NotifyHeadersComplete();
49 URLRequestStatus status_
;
50 base::WeakPtrFactory
<MockURLRequestJob
> weak_factory_
;
53 class DummyProtocolHandler
: public URLRequestJobFactory::ProtocolHandler
{
55 URLRequestJob
* MaybeCreateJob(
57 NetworkDelegate
* network_delegate
) const override
{
58 return new MockURLRequestJob(
61 URLRequestStatus(URLRequestStatus::SUCCESS
, OK
));
65 TEST(URLRequestJobFactoryTest
, NoProtocolHandler
) {
66 TestDelegate delegate
;
67 TestURLRequestContext request_context
;
68 scoped_ptr
<URLRequest
> request(request_context
.CreateRequest(
69 GURL("foo://bar"), DEFAULT_PRIORITY
, &delegate
));
72 base::MessageLoop::current()->Run();
73 EXPECT_EQ(URLRequestStatus::FAILED
, request
->status().status());
74 EXPECT_EQ(ERR_UNKNOWN_URL_SCHEME
, request
->status().error());
77 TEST(URLRequestJobFactoryTest
, BasicProtocolHandler
) {
78 TestDelegate delegate
;
79 URLRequestJobFactoryImpl job_factory
;
80 TestURLRequestContext request_context
;
81 request_context
.set_job_factory(&job_factory
);
82 job_factory
.SetProtocolHandler("foo", new DummyProtocolHandler
);
83 scoped_ptr
<URLRequest
> request(request_context
.CreateRequest(
84 GURL("foo://bar"), DEFAULT_PRIORITY
, &delegate
));
87 base::MessageLoop::current()->Run();
88 EXPECT_EQ(URLRequestStatus::SUCCESS
, request
->status().status());
89 EXPECT_EQ(OK
, request
->status().error());
92 TEST(URLRequestJobFactoryTest
, DeleteProtocolHandler
) {
93 URLRequestJobFactoryImpl job_factory
;
94 TestURLRequestContext request_context
;
95 request_context
.set_job_factory(&job_factory
);
96 job_factory
.SetProtocolHandler("foo", new DummyProtocolHandler
);
97 job_factory
.SetProtocolHandler("foo", NULL
);