Upstream changes for NetworkQualityProvider
[chromium-blink-merge.git] / net / url_request / url_request_job_factory_impl_unittest.cc
blobe480755f9a1746bb9fae4be32b322f8d191f1056
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"
7 #include "base/bind.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"
19 namespace net {
21 namespace {
23 class MockURLRequestJob : public URLRequestJob {
24 public:
25 MockURLRequestJob(URLRequest* request,
26 NetworkDelegate* network_delegate,
27 const URLRequestStatus& status)
28 : URLRequestJob(request, network_delegate),
29 status_(status),
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(
36 FROM_HERE,
37 base::Bind(&MockURLRequestJob::StartAsync, weak_factory_.GetWeakPtr()));
40 protected:
41 ~MockURLRequestJob() override {}
43 private:
44 void StartAsync() {
45 SetStatus(status_);
46 NotifyHeadersComplete();
49 URLRequestStatus status_;
50 base::WeakPtrFactory<MockURLRequestJob> weak_factory_;
53 class DummyProtocolHandler : public URLRequestJobFactory::ProtocolHandler {
54 public:
55 URLRequestJob* MaybeCreateJob(
56 URLRequest* request,
57 NetworkDelegate* network_delegate) const override {
58 return new MockURLRequestJob(
59 request,
60 network_delegate,
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));
70 request->Start();
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));
85 request->Start();
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);
100 } // namespace
102 } // namespace net