url_request_job_factory_impl_unittest.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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.
4
5#include "net/url_request/url_request_job_factory_impl.h"
6
7#include "base/bind.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"
15
16namespace net {
17
18namespace {
19
20class MockURLRequestJob : public URLRequestJob {
21 public:
22  MockURLRequestJob(URLRequest* request,
23                    NetworkDelegate* network_delegate,
24                    const URLRequestStatus& status)
25      : URLRequestJob(request, network_delegate),
26        status_(status),
27        weak_factory_(this) {}
28
29  virtual 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(
33        FROM_HERE,
34        base::Bind(&MockURLRequestJob::StartAsync, weak_factory_.GetWeakPtr()));
35  }
36
37 protected:
38  virtual ~MockURLRequestJob() {}
39
40 private:
41  void StartAsync() {
42    SetStatus(status_);
43    NotifyHeadersComplete();
44  }
45
46  URLRequestStatus status_;
47  base::WeakPtrFactory<MockURLRequestJob> weak_factory_;
48};
49
50class DummyProtocolHandler : public URLRequestJobFactory::ProtocolHandler {
51 public:
52  virtual URLRequestJob* MaybeCreateJob(
53      URLRequest* request, NetworkDelegate* network_delegate) const OVERRIDE {
54    return new MockURLRequestJob(
55        request,
56        network_delegate,
57        URLRequestStatus(URLRequestStatus::SUCCESS, OK));
58  }
59};
60
61TEST(URLRequestJobFactoryTest, NoProtocolHandler) {
62  TestDelegate delegate;
63  TestURLRequestContext request_context;
64  scoped_ptr<URLRequest> request(request_context.CreateRequest(
65      GURL("foo://bar"), DEFAULT_PRIORITY, &delegate, NULL));
66  request->Start();
67
68  base::MessageLoop::current()->Run();
69  EXPECT_EQ(URLRequestStatus::FAILED, request->status().status());
70  EXPECT_EQ(ERR_UNKNOWN_URL_SCHEME, request->status().error());
71}
72
73TEST(URLRequestJobFactoryTest, BasicProtocolHandler) {
74  TestDelegate delegate;
75  URLRequestJobFactoryImpl job_factory;
76  TestURLRequestContext request_context;
77  request_context.set_job_factory(&job_factory);
78  job_factory.SetProtocolHandler("foo", new DummyProtocolHandler);
79  scoped_ptr<URLRequest> request(request_context.CreateRequest(
80      GURL("foo://bar"), DEFAULT_PRIORITY, &delegate, NULL));
81  request->Start();
82
83  base::MessageLoop::current()->Run();
84  EXPECT_EQ(URLRequestStatus::SUCCESS, request->status().status());
85  EXPECT_EQ(OK, request->status().error());
86}
87
88TEST(URLRequestJobFactoryTest, DeleteProtocolHandler) {
89  URLRequestJobFactoryImpl job_factory;
90  TestURLRequestContext request_context;
91  request_context.set_job_factory(&job_factory);
92  job_factory.SetProtocolHandler("foo", new DummyProtocolHandler);
93  job_factory.SetProtocolHandler("foo", NULL);
94}
95
96}  // namespace
97
98}  // namespace net
99