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/base/sdch_dictionary_fetcher.h"
9 #include "base/run_loop.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "net/base/sdch_manager.h"
13 #include "net/url_request/url_request_data_job.h"
14 #include "net/url_request/url_request_filter.h"
15 #include "net/url_request/url_request_interceptor.h"
16 #include "net/url_request/url_request_test_util.h"
17 #include "testing/gtest/include/gtest/gtest.h"
22 static const char* kSampleBufferContext
= "This is a sample buffer.";
23 static const char* kTestDomain
= "top.domain.test";
25 class URLRequestSpecifiedResponseJob
: public URLRequestSimpleJob
{
27 URLRequestSpecifiedResponseJob(URLRequest
* request
,
28 NetworkDelegate
* network_delegate
)
29 : URLRequestSimpleJob(request
, network_delegate
) {}
31 static void AddUrlHandler() {
32 net::URLRequestFilter
* filter
= net::URLRequestFilter::GetInstance();
34 filter
->AddHostnameHandler("http", kTestDomain
,
35 &URLRequestSpecifiedResponseJob::Factory
);
38 static void RemoveUrlHandler() {
39 net::URLRequestFilter
* filter
= net::URLRequestFilter::GetInstance();
40 filter
->RemoveHostnameHandler("http", kTestDomain
);
44 static URLRequestJob
* Factory(
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",
54 url
.spec().c_str(), kSampleBufferContext
,
58 static int jobs_requested() { return jobs_requested_
; }
61 virtual ~URLRequestSpecifiedResponseJob() {};
62 virtual int GetData(std::string
* mime_type
,
65 const CompletionCallback
& callback
) const override
{
66 GURL
url(request_
->url());
67 *data
= ExpectedResponseForURL(url
);
71 static int jobs_requested_
;
74 int URLRequestSpecifiedResponseJob::jobs_requested_(0);
76 class SdchTestDelegate
: public SdchFetcher::Delegate
{
78 struct DictionaryAdditions
{
79 DictionaryAdditions(const std::string
& dictionary_text
,
80 const GURL
& dictionary_url
)
81 : dictionary_text(dictionary_text
),
82 dictionary_url(dictionary_url
) {}
85 std::string dictionary_text
;
89 virtual void AddSdchDictionary(const std::string
& dictionary_text
,
90 const GURL
& dictionary_url
) override
{
91 dictionary_additions
.push_back(
92 DictionaryAdditions(dictionary_text
, dictionary_url
));
95 void GetDictionaryAdditions(std::vector
<DictionaryAdditions
>* out
) {
96 out
->swap(dictionary_additions
);
97 dictionary_additions
.clear();
101 std::vector
<DictionaryAdditions
> dictionary_additions
;
104 class SdchDictionaryFetcherTest
: public ::testing::Test
{
106 SdchDictionaryFetcherTest() {}
108 virtual void SetUp() override
{
109 DCHECK(!fetcher_
.get());
111 URLRequestSpecifiedResponseJob::AddUrlHandler();
112 fetcher_delegate_
.reset(new SdchTestDelegate
);
113 context_
.reset(new TestURLRequestContext
);
114 fetcher_
.reset(new SdchDictionaryFetcher(
115 fetcher_delegate_
.get(), context_
.get()));
118 virtual void TearDown() override
{
119 URLRequestSpecifiedResponseJob::RemoveUrlHandler();
122 fetcher_delegate_
.reset();
125 SdchDictionaryFetcher
* fetcher() { return fetcher_
.get(); }
126 SdchTestDelegate
* manager() { return fetcher_delegate_
.get(); }
128 // May not be called outside the SetUp()/TearDown() interval.
129 int JobsRequested() {
130 return URLRequestSpecifiedResponseJob::jobs_requested();
133 GURL
PathToGurl(const char* path
) {
134 std::string
gurl_string("http://");
135 gurl_string
+= kTestDomain
;
138 return GURL(gurl_string
);
142 scoped_ptr
<SdchTestDelegate
> fetcher_delegate_
;
143 scoped_ptr
<TestURLRequestContext
> context_
;
144 scoped_ptr
<SdchDictionaryFetcher
> fetcher_
;
147 // Schedule a fetch and make sure it happens.
148 TEST_F(SdchDictionaryFetcherTest
, Basic
) {
149 GURL
dictionary_url(PathToGurl("dictionary"));
150 fetcher()->Schedule(dictionary_url
);
152 base::RunLoop().RunUntilIdle();
153 EXPECT_EQ(1, JobsRequested());
154 std::vector
<SdchTestDelegate::DictionaryAdditions
> additions
;
155 manager()->GetDictionaryAdditions(&additions
);
156 ASSERT_EQ(1u, additions
.size());
157 EXPECT_EQ(URLRequestSpecifiedResponseJob::ExpectedResponseForURL(
158 dictionary_url
), additions
[0].dictionary_text
);
161 // Multiple fetches of the same URL should result in only one request.
162 TEST_F(SdchDictionaryFetcherTest
, Multiple
) {
163 GURL
dictionary_url(PathToGurl("dictionary"));
164 fetcher()->Schedule(dictionary_url
);
165 fetcher()->Schedule(dictionary_url
);
166 fetcher()->Schedule(dictionary_url
);
167 base::RunLoop().RunUntilIdle();
169 EXPECT_EQ(1, JobsRequested());
170 std::vector
<SdchTestDelegate::DictionaryAdditions
> additions
;
171 manager()->GetDictionaryAdditions(&additions
);
172 ASSERT_EQ(1u, additions
.size());
173 EXPECT_EQ(URLRequestSpecifiedResponseJob::ExpectedResponseForURL(
174 dictionary_url
), additions
[0].dictionary_text
);
177 // A cancel should result in no actual requests being generated.
178 TEST_F(SdchDictionaryFetcherTest
, Cancel
) {
179 GURL
dictionary_url_1(PathToGurl("dictionary_1"));
180 GURL
dictionary_url_2(PathToGurl("dictionary_2"));
181 GURL
dictionary_url_3(PathToGurl("dictionary_3"));
183 fetcher()->Schedule(dictionary_url_1
);
184 fetcher()->Schedule(dictionary_url_2
);
185 fetcher()->Schedule(dictionary_url_3
);
187 base::RunLoop().RunUntilIdle();
189 // Synchronous execution may have resulted in a single job being scheduled.
190 EXPECT_GE(1, JobsRequested());