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/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 
     14 namespace net {
     15 
     16 namespace {
     17 
     18 class 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         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     base::MessageLoop::current()->PostTask(
     31         FROM_HERE,
     32         base::Bind(&MockURLRequestJob::StartAsync, weak_factory_.GetWeakPtr()));
     33   }
     34 
     35  protected:
     36   virtual ~MockURLRequestJob() {}
     37 
     38  private:
     39   void StartAsync() {
     40     SetStatus(status_);
     41     NotifyHeadersComplete();
     42   }
     43 
     44   URLRequestStatus status_;
     45   base::WeakPtrFactory<MockURLRequestJob> weak_factory_;
     46 };
     47 
     48 class DummyProtocolHandler : public URLRequestJobFactory::ProtocolHandler {
     49  public:
     50   virtual URLRequestJob* MaybeCreateJob(
     51       URLRequest* request, NetworkDelegate* network_delegate) const OVERRIDE {
     52     return new MockURLRequestJob(
     53         request,
     54         network_delegate,
     55         URLRequestStatus(URLRequestStatus::SUCCESS, OK));
     56   }
     57 };
     58 
     59 TEST(URLRequestJobFactoryTest, NoProtocolHandler) {
     60   TestDelegate delegate;
     61   TestURLRequestContext request_context;
     62   TestURLRequest request(GURL("foo://bar"), &delegate, &request_context, NULL);
     63   request.Start();
     64 
     65   base::MessageLoop::current()->Run();
     66   EXPECT_EQ(URLRequestStatus::FAILED, request.status().status());
     67   EXPECT_EQ(ERR_UNKNOWN_URL_SCHEME, request.status().error());
     68 }
     69 
     70 TEST(URLRequestJobFactoryTest, BasicProtocolHandler) {
     71   TestDelegate delegate;
     72   URLRequestJobFactoryImpl job_factory;
     73   TestURLRequestContext request_context;
     74   request_context.set_job_factory(&job_factory);
     75   job_factory.SetProtocolHandler("foo", new DummyProtocolHandler);
     76   TestURLRequest request(GURL("foo://bar"), &delegate, &request_context, NULL);
     77   request.Start();
     78 
     79   base::MessageLoop::current()->Run();
     80   EXPECT_EQ(URLRequestStatus::SUCCESS, request.status().status());
     81   EXPECT_EQ(OK, request.status().error());
     82 }
     83 
     84 TEST(URLRequestJobFactoryTest, DeleteProtocolHandler) {
     85   URLRequestJobFactoryImpl job_factory;
     86   TestURLRequestContext request_context;
     87   request_context.set_job_factory(&job_factory);
     88   job_factory.SetProtocolHandler("foo", new DummyProtocolHandler);
     89   job_factory.SetProtocolHandler("foo", NULL);
     90 }
     91 
     92 }  // namespace
     93 
     94 }  // namespace net
     95