Lots of random cleanups, mostly for native_theme_win.cc:
[chromium-blink-merge.git] / net / url_request / url_request_job_factory_impl_unittest.cc
blob93ca0f83f3727b94f20a49353824b1bc344aba50
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/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"
15 namespace net {
17 namespace {
19 class MockURLRequestJob : public URLRequestJob {
20 public:
21 MockURLRequestJob(URLRequest* request,
22 NetworkDelegate* network_delegate,
23 const URLRequestStatus& status)
24 : URLRequestJob(request, network_delegate),
25 status_(status),
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(
32 FROM_HERE,
33 base::Bind(&MockURLRequestJob::StartAsync, weak_factory_.GetWeakPtr()));
36 protected:
37 virtual ~MockURLRequestJob() {}
39 private:
40 void StartAsync() {
41 SetStatus(status_);
42 NotifyHeadersComplete();
45 URLRequestStatus status_;
46 base::WeakPtrFactory<MockURLRequestJob> weak_factory_;
49 class 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));
60 TEST(URLRequestJobFactoryTest, NoProtocolHandler) {
61 TestDelegate delegate;
62 TestURLRequestContext request_context;
63 TestURLRequest request(
64 GURL("foo://bar"), DEFAULT_PRIORITY, &delegate, &request_context);
65 request.Start();
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);
80 request.Start();
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);
95 } // namespace
97 } // namespace net