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/memory/scoped_ptr.h"
9 #include "base/memory/weak_ptr.h"
10 #include "net/base/request_priority.h"
11 #include "net/url_request/url_request.h"
12 #include "net/url_request/url_request_job.h"
13 #include "net/url_request/url_request_test_util.h"
14 #include "testing/gtest/include/gtest/gtest.h"
20 class MockURLRequestJob
: public URLRequestJob
{
22 MockURLRequestJob(URLRequest
* request
,
23 NetworkDelegate
* network_delegate
,
24 const URLRequestStatus
& status
)
25 : URLRequestJob(request
, network_delegate
),
27 weak_factory_(this) {}
29 void Start() override
{
30 // Start reading asynchronously so that all error reporting and data
31 // callbacks happen as they would for network requests.
32 base::MessageLoop::current()->PostTask(
34 base::Bind(&MockURLRequestJob::StartAsync
, weak_factory_
.GetWeakPtr()));
38 ~MockURLRequestJob() override
{}
43 NotifyHeadersComplete();
46 URLRequestStatus status_
;
47 base::WeakPtrFactory
<MockURLRequestJob
> weak_factory_
;
50 class DummyProtocolHandler
: public URLRequestJobFactory::ProtocolHandler
{
52 URLRequestJob
* MaybeCreateJob(
54 NetworkDelegate
* network_delegate
) const override
{
55 return new MockURLRequestJob(
58 URLRequestStatus(URLRequestStatus::SUCCESS
, OK
));
62 TEST(URLRequestJobFactoryTest
, NoProtocolHandler
) {
63 TestDelegate delegate
;
64 TestURLRequestContext request_context
;
65 scoped_ptr
<URLRequest
> request(request_context
.CreateRequest(
66 GURL("foo://bar"), DEFAULT_PRIORITY
, &delegate
));
69 base::MessageLoop::current()->Run();
70 EXPECT_EQ(URLRequestStatus::FAILED
, request
->status().status());
71 EXPECT_EQ(ERR_UNKNOWN_URL_SCHEME
, request
->status().error());
74 TEST(URLRequestJobFactoryTest
, BasicProtocolHandler
) {
75 TestDelegate delegate
;
76 URLRequestJobFactoryImpl job_factory
;
77 TestURLRequestContext request_context
;
78 request_context
.set_job_factory(&job_factory
);
79 job_factory
.SetProtocolHandler("foo", new DummyProtocolHandler
);
80 scoped_ptr
<URLRequest
> request(request_context
.CreateRequest(
81 GURL("foo://bar"), DEFAULT_PRIORITY
, &delegate
));
84 base::MessageLoop::current()->Run();
85 EXPECT_EQ(URLRequestStatus::SUCCESS
, request
->status().status());
86 EXPECT_EQ(OK
, request
->status().error());
89 TEST(URLRequestJobFactoryTest
, DeleteProtocolHandler
) {
90 URLRequestJobFactoryImpl job_factory
;
91 TestURLRequestContext request_context
;
92 request_context
.set_job_factory(&job_factory
);
93 job_factory
.SetProtocolHandler("foo", new DummyProtocolHandler
);
94 job_factory
.SetProtocolHandler("foo", NULL
);