url_request_job_factory_impl_unittest.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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/weak_ptr.h"
9#include "net/url_request/url_request.h"
10#include "net/url_request/url_request_job.h"
11#include "net/url_request/url_request_test_util.h"
12#include "testing/gtest/include/gtest/gtest.h"
13
14namespace net {
15
16namespace {
17
18class MockURLRequestJob : public URLRequestJob {
19 public:
20  MockURLRequestJob(URLRequest* request,
21                    NetworkDelegate* network_delegate,
22                    const URLRequestStatus& status)
23      : URLRequestJob(request, network_delegate),
24        status_(status),
25        ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {}
26
27  virtual void Start() OVERRIDE {
28    // Start reading asynchronously so that all error reporting and data
29    // callbacks happen as they would for network requests.
30    MessageLoop::current()->PostTask(
31        FROM_HERE,
32        base::Bind(&MockURLRequestJob::StartAsync,
33                   weak_factory_.GetWeakPtr()));
34  }
35
36 protected:
37  virtual ~MockURLRequestJob() {}
38
39 private:
40  void StartAsync() {
41    SetStatus(status_);
42    NotifyHeadersComplete();
43  }
44
45  URLRequestStatus status_;
46  base::WeakPtrFactory<MockURLRequestJob> weak_factory_;
47};
48
49class DummyProtocolHandler : public URLRequestJobFactory::ProtocolHandler {
50 public:
51  virtual URLRequestJob* MaybeCreateJob(
52      URLRequest* request, NetworkDelegate* network_delegate) const OVERRIDE {
53    return new MockURLRequestJob(
54        request,
55        network_delegate,
56        URLRequestStatus(URLRequestStatus::SUCCESS, OK));
57  }
58};
59
60TEST(URLRequestJobFactoryTest, NoProtocolHandler) {
61  TestDelegate delegate;
62  TestURLRequestContext request_context;
63  TestURLRequest request(GURL("foo://bar"), &delegate, &request_context, NULL);
64  request.Start();
65
66  MessageLoop::current()->Run();
67  EXPECT_EQ(URLRequestStatus::FAILED, request.status().status());
68  EXPECT_EQ(ERR_UNKNOWN_URL_SCHEME, request.status().error());
69}
70
71TEST(URLRequestJobFactoryTest, BasicProtocolHandler) {
72  TestDelegate delegate;
73  URLRequestJobFactoryImpl job_factory;
74  TestURLRequestContext request_context;
75  request_context.set_job_factory(&job_factory);
76  job_factory.SetProtocolHandler("foo", new DummyProtocolHandler);
77  TestURLRequest request(GURL("foo://bar"), &delegate, &request_context, NULL);
78  request.Start();
79
80  MessageLoop::current()->Run();
81  EXPECT_EQ(URLRequestStatus::SUCCESS, request.status().status());
82  EXPECT_EQ(OK, request.status().error());
83}
84
85TEST(URLRequestJobFactoryTest, DeleteProtocolHandler) {
86  URLRequestJobFactoryImpl job_factory;
87  TestURLRequestContext request_context;
88  request_context.set_job_factory(&job_factory);
89  job_factory.SetProtocolHandler("foo", new DummyProtocolHandler);
90  job_factory.SetProtocolHandler("foo", NULL);
91}
92
93}  // namespace
94
95}  // namespace net
96