Move GetProcessIntegrityLevel to file_info.h and remove the handle argument.
[chromium-blink-merge.git] / net / url_request / sdch_dictionary_fetcher_unittest.cc
blob1826ddb1681509bc7ffb8e47a84fbd59baa254d1
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"
7 #include <string>
9 #include "base/bind.h"
10 #include "base/callback.h"
11 #include "base/run_loop.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "net/base/sdch_manager.h"
15 #include "net/url_request/url_request_data_job.h"
16 #include "net/url_request/url_request_filter.h"
17 #include "net/url_request/url_request_interceptor.h"
18 #include "net/url_request/url_request_test_util.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 namespace net {
23 namespace {
25 const char kSampleBufferContext[] = "This is a sample buffer.";
26 const char kTestDomain[] = "top.domain.test";
28 class URLRequestSpecifiedResponseJob : public URLRequestSimpleJob {
29 public:
30 URLRequestSpecifiedResponseJob(URLRequest* request,
31 NetworkDelegate* network_delegate,
32 const base::Closure& destruction_callback)
33 : URLRequestSimpleJob(request, network_delegate),
34 destruction_callback_(destruction_callback) {
35 DCHECK(!destruction_callback.is_null());
38 static std::string ExpectedResponseForURL(const GURL& url) {
39 return base::StringPrintf("Response for %s\n%s\nEnd Response for %s\n",
40 url.spec().c_str(),
41 kSampleBufferContext,
42 url.spec().c_str());
45 private:
46 ~URLRequestSpecifiedResponseJob() override { destruction_callback_.Run(); }
48 // URLRequestSimpleJob implementation:
49 int GetData(std::string* mime_type,
50 std::string* charset,
51 std::string* data,
52 const CompletionCallback& callback) const override {
53 GURL url(request_->url());
54 *data = ExpectedResponseForURL(url);
55 return OK;
58 const base::Closure destruction_callback_;
61 class SpecifiedResponseJobInterceptor : public URLRequestInterceptor {
62 public:
63 // A callback to be called whenever a URLRequestSpecifiedResponseJob is
64 // created or destroyed. The argument will be the change in number of
65 // jobs (i.e. +1 for created, -1 for destroyed).
66 typedef base::Callback<void(int outstanding_job_delta)> LifecycleCallback;
68 explicit SpecifiedResponseJobInterceptor(
69 const LifecycleCallback& lifecycle_callback)
70 : lifecycle_callback_(lifecycle_callback), factory_(this) {
71 DCHECK(!lifecycle_callback_.is_null());
73 ~SpecifiedResponseJobInterceptor() override {}
75 URLRequestJob* MaybeInterceptRequest(
76 URLRequest* request,
77 NetworkDelegate* network_delegate) const override {
78 if (!lifecycle_callback_.is_null())
79 lifecycle_callback_.Run(1);
81 return new URLRequestSpecifiedResponseJob(
82 request, network_delegate, base::Bind(lifecycle_callback_, -1));
85 // The caller must ensure that the callback is valid to call for the
86 // lifetime of the SpecifiedResponseJobInterceptor (i.e. until
87 // Unregister() is called).
88 static void RegisterWithFilter(const LifecycleCallback& lifecycle_callback) {
89 scoped_ptr<SpecifiedResponseJobInterceptor> interceptor(
90 new SpecifiedResponseJobInterceptor(lifecycle_callback));
92 net::URLRequestFilter::GetInstance()->AddHostnameInterceptor(
93 "http", kTestDomain, interceptor.Pass());
96 static void Unregister() {
97 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler("http",
98 kTestDomain);
101 private:
102 void OnSpecfiedResponseJobDestruction() const {
103 if (!lifecycle_callback_.is_null())
104 lifecycle_callback_.Run(-1);
107 LifecycleCallback lifecycle_callback_;
108 mutable base::WeakPtrFactory<SpecifiedResponseJobInterceptor> factory_;
111 // Local test infrastructure
112 // * URLRequestSpecifiedResponseJob: A URLRequestJob that returns
113 // a different but derivable response for each URL (used for all
114 // url requests in this file). The class provides interfaces to
115 // signal whenever the total number of jobs transitions to zero.
116 // * SdchDictionaryFetcherTest: Registers a callback with the above
117 // class, and provides blocking interfaces for a transition to zero jobs.
118 // Contains an SdchDictionaryFetcher, and tracks fetcher dictionary
119 // addition callbacks.
120 // Most tests schedule a dictionary fetch, wait for no jobs outstanding,
121 // then test that the fetch results are as expected.
123 class SdchDictionaryFetcherTest : public ::testing::Test {
124 public:
125 struct DictionaryAdditions {
126 DictionaryAdditions(const std::string& dictionary_text,
127 const GURL& dictionary_url)
128 : dictionary_text(dictionary_text), dictionary_url(dictionary_url) {}
130 std::string dictionary_text;
131 GURL dictionary_url;
134 SdchDictionaryFetcherTest()
135 : jobs_requested_(0),
136 jobs_outstanding_(0),
137 context_(new TestURLRequestContext),
138 fetcher_(new SdchDictionaryFetcher(
139 context_.get(),
140 base::Bind(&SdchDictionaryFetcherTest::OnDictionaryFetched,
141 base::Unretained(this)))),
142 factory_(this) {
143 SpecifiedResponseJobInterceptor::RegisterWithFilter(
144 base::Bind(&SdchDictionaryFetcherTest::OnNumberJobsChanged,
145 factory_.GetWeakPtr()));
148 ~SdchDictionaryFetcherTest() override {
149 SpecifiedResponseJobInterceptor::Unregister();
152 void OnDictionaryFetched(const std::string& dictionary_text,
153 const GURL& dictionary_url,
154 const BoundNetLog& net_log) {
155 dictionary_additions.push_back(
156 DictionaryAdditions(dictionary_text, dictionary_url));
159 // Return (in |*out|) all dictionary additions since the last time
160 // this function was called.
161 void GetDictionaryAdditions(std::vector<DictionaryAdditions>* out) {
162 out->swap(dictionary_additions);
163 dictionary_additions.clear();
166 SdchDictionaryFetcher* fetcher() { return fetcher_.get(); }
168 // May not be called outside the SetUp()/TearDown() interval.
169 int jobs_requested() const { return jobs_requested_; }
171 GURL PathToGurl(const char* path) {
172 std::string gurl_string("http://");
173 gurl_string += kTestDomain;
174 gurl_string += "/";
175 gurl_string += path;
176 return GURL(gurl_string);
179 // Block until there are no outstanding URLRequestSpecifiedResponseJobs.
180 void WaitForNoJobs() {
181 if (jobs_outstanding_ == 0)
182 return;
184 run_loop_.reset(new base::RunLoop);
185 run_loop_->Run();
186 run_loop_.reset();
189 private:
190 void OnNumberJobsChanged(int outstanding_jobs_delta) {
191 if (outstanding_jobs_delta > 0)
192 jobs_requested_ += outstanding_jobs_delta;
193 jobs_outstanding_ += outstanding_jobs_delta;
194 if (jobs_outstanding_ == 0 && run_loop_)
195 run_loop_->Quit();
198 int jobs_requested_;
199 int jobs_outstanding_;
200 scoped_ptr<base::RunLoop> run_loop_;
201 scoped_ptr<TestURLRequestContext> context_;
202 scoped_ptr<SdchDictionaryFetcher> fetcher_;
203 std::vector<DictionaryAdditions> dictionary_additions;
204 base::WeakPtrFactory<SdchDictionaryFetcherTest> factory_;
207 // Schedule a fetch and make sure it happens.
208 TEST_F(SdchDictionaryFetcherTest, Basic) {
209 GURL dictionary_url(PathToGurl("dictionary"));
210 fetcher()->Schedule(dictionary_url);
211 WaitForNoJobs();
213 EXPECT_EQ(1, jobs_requested());
214 std::vector<DictionaryAdditions> additions;
215 GetDictionaryAdditions(&additions);
216 ASSERT_EQ(1u, additions.size());
217 EXPECT_EQ(
218 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url),
219 additions[0].dictionary_text);
222 // Multiple fetches of the same URL should result in only one request.
223 TEST_F(SdchDictionaryFetcherTest, Multiple) {
224 GURL dictionary_url(PathToGurl("dictionary"));
225 fetcher()->Schedule(dictionary_url);
226 fetcher()->Schedule(dictionary_url);
227 fetcher()->Schedule(dictionary_url);
228 WaitForNoJobs();
230 EXPECT_EQ(1, jobs_requested());
231 std::vector<DictionaryAdditions> additions;
232 GetDictionaryAdditions(&additions);
233 ASSERT_EQ(1u, additions.size());
234 EXPECT_EQ(
235 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url),
236 additions[0].dictionary_text);
239 // A cancel should result in no actual requests being generated.
240 TEST_F(SdchDictionaryFetcherTest, Cancel) {
241 GURL dictionary_url_1(PathToGurl("dictionary_1"));
242 GURL dictionary_url_2(PathToGurl("dictionary_2"));
243 GURL dictionary_url_3(PathToGurl("dictionary_3"));
245 fetcher()->Schedule(dictionary_url_1);
246 fetcher()->Schedule(dictionary_url_2);
247 fetcher()->Schedule(dictionary_url_3);
248 fetcher()->Cancel();
249 WaitForNoJobs();
251 // Synchronous execution may have resulted in a single job being scheduled.
252 EXPECT_GE(1, jobs_requested());
255 // Attempt to confuse the fetcher loop processing by scheduling a
256 // dictionary addition while another fetch is in process.
257 TEST_F(SdchDictionaryFetcherTest, LoopRace) {
258 GURL dictionary0_url(PathToGurl("dictionary0"));
259 GURL dictionary1_url(PathToGurl("dictionary1"));
260 fetcher()->Schedule(dictionary0_url);
261 fetcher()->Schedule(dictionary1_url);
262 WaitForNoJobs();
264 ASSERT_EQ(2, jobs_requested());
265 std::vector<DictionaryAdditions> additions;
266 GetDictionaryAdditions(&additions);
267 ASSERT_EQ(2u, additions.size());
268 EXPECT_EQ(
269 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary0_url),
270 additions[0].dictionary_text);
271 EXPECT_EQ(
272 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary1_url),
273 additions[1].dictionary_text);
276 } // namespace
278 } // namespace net