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/weak_ptr.h"
9 #include "net/base/request_priority.h"
10 #include "net/url_request/url_request.h"
11 #include "net/url_request/url_request_job.h"
12 #include "net/url_request/url_request_test_util.h"
13 #include "testing/gtest/include/gtest/gtest.h"
19 class MockURLRequestJob
: public URLRequestJob
{
21 MockURLRequestJob(URLRequest
* request
,
22 NetworkDelegate
* network_delegate
,
23 const URLRequestStatus
& status
)
24 : URLRequestJob(request
, network_delegate
),
26 weak_factory_(this) {}
28 virtual void Start() OVERRIDE
{
29 // Start reading asynchronously so that all error reporting and data
30 // callbacks happen as they would for network requests.
31 base::MessageLoop::current()->PostTask(
33 base::Bind(&MockURLRequestJob::StartAsync
, weak_factory_
.GetWeakPtr()));
37 virtual ~MockURLRequestJob() {}
42 NotifyHeadersComplete();
45 URLRequestStatus status_
;
46 base::WeakPtrFactory
<MockURLRequestJob
> weak_factory_
;
49 class DummyProtocolHandler
: public URLRequestJobFactory::ProtocolHandler
{
51 virtual URLRequestJob
* MaybeCreateJob(
52 URLRequest
* request
, NetworkDelegate
* network_delegate
) const OVERRIDE
{
53 return new MockURLRequestJob(
56 URLRequestStatus(URLRequestStatus::SUCCESS
, OK
));
60 TEST(URLRequestJobFactoryTest
, NoProtocolHandler
) {
61 TestDelegate delegate
;
62 TestURLRequestContext request_context
;
63 TestURLRequest
request(
64 GURL("foo://bar"), DEFAULT_PRIORITY
, &delegate
, &request_context
);
67 base::MessageLoop::current()->Run();
68 EXPECT_EQ(URLRequestStatus::FAILED
, request
.status().status());
69 EXPECT_EQ(ERR_UNKNOWN_URL_SCHEME
, request
.status().error());
72 TEST(URLRequestJobFactoryTest
, BasicProtocolHandler
) {
73 TestDelegate delegate
;
74 URLRequestJobFactoryImpl job_factory
;
75 TestURLRequestContext request_context
;
76 request_context
.set_job_factory(&job_factory
);
77 job_factory
.SetProtocolHandler("foo", new DummyProtocolHandler
);
78 TestURLRequest
request(
79 GURL("foo://bar"), DEFAULT_PRIORITY
, &delegate
, &request_context
);
82 base::MessageLoop::current()->Run();
83 EXPECT_EQ(URLRequestStatus::SUCCESS
, request
.status().status());
84 EXPECT_EQ(OK
, request
.status().error());
87 TEST(URLRequestJobFactoryTest
, DeleteProtocolHandler
) {
88 URLRequestJobFactoryImpl job_factory
;
89 TestURLRequestContext request_context
;
90 request_context
.set_job_factory(&job_factory
);
91 job_factory
.SetProtocolHandler("foo", new DummyProtocolHandler
);
92 job_factory
.SetProtocolHandler("foo", NULL
);