[NaCl SDK] Fix mysterious gtest failure in Html5FsTest::OpenForCreate
[chromium-blink-merge.git] / net / url_request / url_request_job_factory_impl_unittest.cc
blob2c7b3e2f2a528ba650bb8b05a4c672f72f82d1d4
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/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"
16 namespace net {
18 namespace {
20 class 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) {}
29 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()));
37 protected:
38 ~MockURLRequestJob() override {}
40 private:
41 void StartAsync() {
42 SetStatus(status_);
43 NotifyHeadersComplete();
46 URLRequestStatus status_;
47 base::WeakPtrFactory<MockURLRequestJob> weak_factory_;
50 class DummyProtocolHandler : public URLRequestJobFactory::ProtocolHandler {
51 public:
52 URLRequestJob* MaybeCreateJob(
53 URLRequest* request,
54 NetworkDelegate* network_delegate) const override {
55 return new MockURLRequestJob(
56 request,
57 network_delegate,
58 URLRequestStatus(URLRequestStatus::SUCCESS, OK));
62 TEST(URLRequestJobFactoryTest, NoProtocolHandler) {
63 TestDelegate delegate;
64 TestURLRequestContext request_context;
65 scoped_ptr<URLRequest> request(request_context.CreateRequest(
66 GURL("foo://bar"), DEFAULT_PRIORITY, &delegate, NULL));
67 request->Start();
69 base::MessageLoop::current()->Run();
70 EXPECT_EQ(URLRequestStatus::FAILED, request->status().status());
71 EXPECT_EQ(ERR_UNKNOWN_URL_SCHEME, request->status().error());
74 TEST(URLRequestJobFactoryTest, BasicProtocolHandler) {
75 TestDelegate delegate;
76 URLRequestJobFactoryImpl job_factory;
77 TestURLRequestContext request_context;
78 request_context.set_job_factory(&job_factory);
79 job_factory.SetProtocolHandler("foo", new DummyProtocolHandler);
80 scoped_ptr<URLRequest> request(request_context.CreateRequest(
81 GURL("foo://bar"), DEFAULT_PRIORITY, &delegate, NULL));
82 request->Start();
84 base::MessageLoop::current()->Run();
85 EXPECT_EQ(URLRequestStatus::SUCCESS, request->status().status());
86 EXPECT_EQ(OK, request->status().error());
89 TEST(URLRequestJobFactoryTest, DeleteProtocolHandler) {
90 URLRequestJobFactoryImpl job_factory;
91 TestURLRequestContext request_context;
92 request_context.set_job_factory(&job_factory);
93 job_factory.SetProtocolHandler("foo", new DummyProtocolHandler);
94 job_factory.SetProtocolHandler("foo", NULL);
97 } // namespace
99 } // namespace net