Home | History | Annotate | Download | only in url_request
      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/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"
     14 
     15 namespace net {
     16 
     17 namespace {
     18 
     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) {}
     27 
     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()));
     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 
     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));
     57   }
     58 };
     59 
     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();
     66 
     67   base::MessageLoop::current()->Run();
     68   EXPECT_EQ(URLRequestStatus::FAILED, request.status().status());
     69   EXPECT_EQ(ERR_UNKNOWN_URL_SCHEME, request.status().error());
     70 }
     71 
     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();
     81 
     82   base::MessageLoop::current()->Run();
     83   EXPECT_EQ(URLRequestStatus::SUCCESS, request.status().status());
     84   EXPECT_EQ(OK, request.status().error());
     85 }
     86 
     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);
     93 }
     94 
     95 }  // namespace
     96 
     97 }  // namespace net
     98