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 "net/url_request/sdch_dictionary_fetcher.h"
10 #include "base/run_loop.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "net/base/sdch_manager.h"
14 #include "net/url_request/url_request_data_job.h"
15 #include "net/url_request/url_request_filter.h"
16 #include "net/url_request/url_request_interceptor.h"
17 #include "net/url_request/url_request_test_util.h"
18 #include "testing/gtest/include/gtest/gtest.h"
23 static const char* kSampleBufferContext
= "This is a sample buffer.";
24 static const char* kTestDomain
= "top.domain.test";
26 class URLRequestSpecifiedResponseJob
: public URLRequestSimpleJob
{
28 URLRequestSpecifiedResponseJob(URLRequest
* request
,
29 NetworkDelegate
* network_delegate
)
30 : URLRequestSimpleJob(request
, network_delegate
) {}
32 static void AddUrlHandler() {
33 net::URLRequestFilter
* filter
= net::URLRequestFilter::GetInstance();
35 filter
->AddHostnameHandler(
36 "http", kTestDomain
, &URLRequestSpecifiedResponseJob::Factory
);
39 static void RemoveUrlHandler() {
40 net::URLRequestFilter
* filter
= net::URLRequestFilter::GetInstance();
41 filter
->RemoveHostnameHandler("http", kTestDomain
);
45 static URLRequestJob
* Factory(URLRequest
* request
,
46 net::NetworkDelegate
* network_delegate
,
47 const std::string
& scheme
) {
49 return new URLRequestSpecifiedResponseJob(request
, network_delegate
);
52 static std::string
ExpectedResponseForURL(const GURL
& url
) {
53 return base::StringPrintf("Response for %s\n%s\nEnd Response for %s\n",
59 static int jobs_requested() { return jobs_requested_
; }
62 ~URLRequestSpecifiedResponseJob() override
{};
63 int GetData(std::string
* mime_type
,
66 const CompletionCallback
& callback
) const override
{
67 GURL
url(request_
->url());
68 *data
= ExpectedResponseForURL(url
);
72 static int jobs_requested_
;
75 int URLRequestSpecifiedResponseJob::jobs_requested_(0);
77 class SdchDictionaryFetcherTest
: public ::testing::Test
{
79 struct DictionaryAdditions
{
80 DictionaryAdditions(const std::string
& dictionary_text
,
81 const GURL
& dictionary_url
)
82 : dictionary_text(dictionary_text
), dictionary_url(dictionary_url
) {}
84 std::string dictionary_text
;
88 SdchDictionaryFetcherTest() {
89 URLRequestSpecifiedResponseJob::AddUrlHandler();
90 context_
.reset(new TestURLRequestContext
);
91 fetcher_
.reset(new SdchDictionaryFetcher(
93 base::Bind(&SdchDictionaryFetcherTest::OnDictionaryFetched
,
94 base::Unretained(this))));
97 ~SdchDictionaryFetcherTest() {
98 URLRequestSpecifiedResponseJob::RemoveUrlHandler();
101 void OnDictionaryFetched(const std::string
& dictionary_text
,
102 const GURL
& dictionary_url
,
103 const BoundNetLog
& net_log
) {
104 dictionary_additions
.push_back(
105 DictionaryAdditions(dictionary_text
, dictionary_url
));
108 void GetDictionaryAdditions(std::vector
<DictionaryAdditions
>* out
) {
109 out
->swap(dictionary_additions
);
110 dictionary_additions
.clear();
113 SdchDictionaryFetcher
* fetcher() { return fetcher_
.get(); }
115 // May not be called outside the SetUp()/TearDown() interval.
116 int JobsRequested() {
117 return URLRequestSpecifiedResponseJob::jobs_requested();
120 GURL
PathToGurl(const char* path
) {
121 std::string
gurl_string("http://");
122 gurl_string
+= kTestDomain
;
125 return GURL(gurl_string
);
129 scoped_ptr
<TestURLRequestContext
> context_
;
130 scoped_ptr
<SdchDictionaryFetcher
> fetcher_
;
131 std::vector
<DictionaryAdditions
> dictionary_additions
;
134 // Schedule a fetch and make sure it happens.
135 TEST_F(SdchDictionaryFetcherTest
, Basic
) {
136 GURL
dictionary_url(PathToGurl("dictionary"));
137 fetcher()->Schedule(dictionary_url
);
139 base::RunLoop().RunUntilIdle();
140 EXPECT_EQ(1, JobsRequested());
141 std::vector
<DictionaryAdditions
> additions
;
142 GetDictionaryAdditions(&additions
);
143 ASSERT_EQ(1u, additions
.size());
145 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url
),
146 additions
[0].dictionary_text
);
149 // Multiple fetches of the same URL should result in only one request.
150 TEST_F(SdchDictionaryFetcherTest
, Multiple
) {
151 GURL
dictionary_url(PathToGurl("dictionary"));
152 fetcher()->Schedule(dictionary_url
);
153 fetcher()->Schedule(dictionary_url
);
154 fetcher()->Schedule(dictionary_url
);
155 base::RunLoop().RunUntilIdle();
157 EXPECT_EQ(1, JobsRequested());
158 std::vector
<DictionaryAdditions
> additions
;
159 GetDictionaryAdditions(&additions
);
160 ASSERT_EQ(1u, additions
.size());
162 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url
),
163 additions
[0].dictionary_text
);
166 // A cancel should result in no actual requests being generated.
167 TEST_F(SdchDictionaryFetcherTest
, Cancel
) {
168 GURL
dictionary_url_1(PathToGurl("dictionary_1"));
169 GURL
dictionary_url_2(PathToGurl("dictionary_2"));
170 GURL
dictionary_url_3(PathToGurl("dictionary_3"));
172 fetcher()->Schedule(dictionary_url_1
);
173 fetcher()->Schedule(dictionary_url_2
);
174 fetcher()->Schedule(dictionary_url_3
);
176 base::RunLoop().RunUntilIdle();
178 // Synchronous execution may have resulted in a single job being scheduled.
179 EXPECT_GE(1, JobsRequested());