Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / components / data_reduction_proxy / core / browser / data_reduction_proxy_interceptor_unittest.cc
1 // Copyright 2014 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 "components/data_reduction_proxy/core/browser/data_reduction_proxy_interceptor.h"
6
7 #include "base/message_loop/message_loop.h"
8 #include "base/run_loop.h"
9 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h"
10 #include "net/base/capturing_net_log.h"
11 #include "net/http/http_response_headers.h"
12 #include "net/url_request/url_request.h"
13 #include "net/url_request/url_request_intercepting_job_factory.h"
14 #include "net/url_request/url_request_interceptor.h"
15 #include "net/url_request/url_request_job.h"
16 #include "net/url_request/url_request_job_factory.h"
17 #include "net/url_request/url_request_job_factory_impl.h"
18 #include "net/url_request/url_request_test_util.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20
21 namespace data_reduction_proxy {
22
23 class CountingURLRequestInterceptor : public net::URLRequestInterceptor {
24  public:
25   CountingURLRequestInterceptor()
26       : request_count_(0), redirect_count_(0), response_count_(0) {
27   }
28
29   // URLRequestInterceptor implementation:
30   net::URLRequestJob* MaybeInterceptRequest(
31       net::URLRequest* request,
32       net::NetworkDelegate* network_delegate) const override {
33     request_count_++;
34     return nullptr;
35   }
36
37   net::URLRequestJob* MaybeInterceptRedirect(
38       net::URLRequest* request,
39       net::NetworkDelegate* network_delegate,
40       const GURL& location) const override {
41     redirect_count_++;
42     return nullptr;
43   }
44
45   net::URLRequestJob* MaybeInterceptResponse(
46       net::URLRequest* request,
47       net::NetworkDelegate* network_delegate) const override {
48     response_count_++;
49     return nullptr;
50   }
51
52   int request_count() const {
53     return request_count_;
54   }
55
56   int redirect_count() const {
57     return redirect_count_;
58   }
59
60   int response_count() const {
61     return response_count_;
62   }
63
64  private:
65   mutable int request_count_;
66   mutable int redirect_count_;
67   mutable int response_count_;
68 };
69
70 class TestURLRequestContextWithDataReductionProxy
71     : public net::TestURLRequestContext {
72  public:
73   TestURLRequestContextWithDataReductionProxy(DataReductionProxyParams* params,
74                                               net::NetworkDelegate* delegate)
75       : net::TestURLRequestContext(true) {
76     std::string proxy = params->origin().spec();
77     context_storage_.set_proxy_service(net::ProxyService::CreateFixed(proxy));
78     set_network_delegate(delegate);
79   }
80
81   ~TestURLRequestContextWithDataReductionProxy() override {}
82 };
83
84 class DataReductionProxyInterceptorTest : public testing::Test {
85  public:
86   DataReductionProxyInterceptorTest()
87       : params_(DataReductionProxyParams::kAllowed) {
88     default_context_.reset(
89         new TestURLRequestContextWithDataReductionProxy(
90             &params_, &default_network_delegate_));
91     default_context_->set_network_delegate(&default_network_delegate_);
92     default_context_->set_net_log(&net_log_);
93   }
94
95   ~DataReductionProxyInterceptorTest() override {
96     // URLRequestJobs may post clean-up tasks on destruction.
97     base::RunLoop().RunUntilIdle();
98   }
99
100   void Init(scoped_ptr<net::URLRequestJobFactory> factory) {
101     job_factory_ = factory.Pass();
102     default_context_->set_job_factory(job_factory_.get());
103     default_context_->Init();
104   }
105
106   DataReductionProxyParams params_;
107   net::CapturingNetLog net_log_;
108   net::TestNetworkDelegate default_network_delegate_;
109   scoped_ptr<net::URLRequestJobFactory> job_factory_;
110   scoped_ptr<net::TestURLRequestContext> default_context_;
111   base::MessageLoopForIO loop_;
112 };
113
114 TEST_F(DataReductionProxyInterceptorTest, TestJobFactoryChaining) {
115   // Verifies that job factories can be chained.
116   scoped_ptr<net::URLRequestJobFactory> impl(
117       new net::URLRequestJobFactoryImpl());
118
119   CountingURLRequestInterceptor* interceptor2 =
120       new CountingURLRequestInterceptor();
121   scoped_ptr<net::URLRequestJobFactory> factory2(
122       new net::URLRequestInterceptingJobFactory(
123           impl.Pass(), make_scoped_ptr(interceptor2)));
124
125   CountingURLRequestInterceptor* interceptor1 =
126       new CountingURLRequestInterceptor();
127   scoped_ptr<net::URLRequestJobFactory> factory1(
128       new net::URLRequestInterceptingJobFactory(
129           factory2.Pass(), make_scoped_ptr(interceptor1)));
130
131   Init(factory1.Pass());
132
133   net::TestDelegate d;
134   scoped_ptr<net::URLRequest> req(default_context_->CreateRequest(
135       GURL("http://foo"), net::DEFAULT_PRIORITY, &d, nullptr));
136
137   req->Start();
138   base::RunLoop().Run();
139   EXPECT_EQ(1, interceptor1->request_count());
140   EXPECT_EQ(0, interceptor1->redirect_count());
141   EXPECT_EQ(1, interceptor1->response_count());
142   EXPECT_EQ(1, interceptor2->request_count());
143   EXPECT_EQ(0, interceptor2->redirect_count());
144   EXPECT_EQ(1, interceptor2->response_count());
145 }
146 }  // namespace data_reduction_proxy