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.
5 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_interceptor.h"
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"
21 namespace data_reduction_proxy {
23 class CountingURLRequestInterceptor : public net::URLRequestInterceptor {
25 CountingURLRequestInterceptor()
26 : request_count_(0), redirect_count_(0), response_count_(0) {
29 // URLRequestInterceptor implementation:
30 net::URLRequestJob* MaybeInterceptRequest(
31 net::URLRequest* request,
32 net::NetworkDelegate* network_delegate) const override {
37 net::URLRequestJob* MaybeInterceptRedirect(
38 net::URLRequest* request,
39 net::NetworkDelegate* network_delegate,
40 const GURL& location) const override {
45 net::URLRequestJob* MaybeInterceptResponse(
46 net::URLRequest* request,
47 net::NetworkDelegate* network_delegate) const override {
52 int request_count() const {
53 return request_count_;
56 int redirect_count() const {
57 return redirect_count_;
60 int response_count() const {
61 return response_count_;
65 mutable int request_count_;
66 mutable int redirect_count_;
67 mutable int response_count_;
70 class TestURLRequestContextWithDataReductionProxy
71 : public net::TestURLRequestContext {
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);
81 ~TestURLRequestContextWithDataReductionProxy() override {}
84 class DataReductionProxyInterceptorTest : public testing::Test {
86 DataReductionProxyInterceptorTest()
87 : params_(DataReductionProxyParams::kAllowed) {
88 default_context_.reset(
89 new TestURLRequestContextWithDataReductionProxy(
90 ¶ms_, &default_network_delegate_));
91 default_context_->set_network_delegate(&default_network_delegate_);
92 default_context_->set_net_log(&net_log_);
95 ~DataReductionProxyInterceptorTest() override {
96 // URLRequestJobs may post clean-up tasks on destruction.
97 base::RunLoop().RunUntilIdle();
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();
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_;
114 TEST_F(DataReductionProxyInterceptorTest, TestJobFactoryChaining) {
115 // Verifies that job factories can be chained.
116 scoped_ptr<net::URLRequestJobFactory> impl(
117 new net::URLRequestJobFactoryImpl());
119 CountingURLRequestInterceptor* interceptor2 =
120 new CountingURLRequestInterceptor();
121 scoped_ptr<net::URLRequestJobFactory> factory2(
122 new net::URLRequestInterceptingJobFactory(
123 impl.Pass(), make_scoped_ptr(interceptor2)));
125 CountingURLRequestInterceptor* interceptor1 =
126 new CountingURLRequestInterceptor();
127 scoped_ptr<net::URLRequestJobFactory> factory1(
128 new net::URLRequestInterceptingJobFactory(
129 factory2.Pass(), make_scoped_ptr(interceptor1)));
131 Init(factory1.Pass());
134 scoped_ptr<net::URLRequest> req(default_context_->CreateRequest(
135 GURL("http://foo"), net::DEFAULT_PRIORITY, &d, nullptr));
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());
146 } // namespace data_reduction_proxy