Upstream changes for NetworkQualityProvider
[chromium-blink-merge.git] / net / url_request / sdch_dictionary_fetcher_unittest.cc
blob98eecaa4c08f901080dd60ee205867a87f6ad556
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>
8 #include <vector>
10 #include "base/bind.h"
11 #include "base/callback.h"
12 #include "base/logging.h"
13 #include "base/macros.h"
14 #include "base/run_loop.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/thread_task_runner_handle.h"
17 #include "net/base/load_flags.h"
18 #include "net/base/sdch_manager.h"
19 #include "net/http/http_response_headers.h"
20 #include "net/url_request/url_request_data_job.h"
21 #include "net/url_request/url_request_filter.h"
22 #include "net/url_request/url_request_interceptor.h"
23 #include "net/url_request/url_request_test_util.h"
24 #include "testing/gtest/include/gtest/gtest.h"
26 namespace net {
28 namespace {
30 const char kSampleBufferContext[] = "This is a sample buffer.";
31 const char kTestDomain[] = "top.domain.test";
33 class URLRequestSpecifiedResponseJob : public URLRequestSimpleJob {
34 public:
35 // Called on destruction with load flags used for this request.
36 typedef base::Callback<void(int)> DestructionCallback;
38 URLRequestSpecifiedResponseJob(
39 URLRequest* request,
40 NetworkDelegate* network_delegate,
41 const HttpResponseInfo& response_info_to_return,
42 const DestructionCallback& destruction_callback)
43 : URLRequestSimpleJob(request, network_delegate),
44 response_info_to_return_(response_info_to_return),
45 last_load_flags_seen_(request->load_flags()),
46 destruction_callback_(destruction_callback) {
47 DCHECK(!destruction_callback.is_null());
50 static std::string ExpectedResponseForURL(const GURL& url) {
51 return base::StringPrintf("Response for %s\n%s\nEnd Response for %s\n",
52 url.spec().c_str(),
53 kSampleBufferContext,
54 url.spec().c_str());
57 // URLRequestJob
58 void GetResponseInfo(HttpResponseInfo* info) override {
59 *info = response_info_to_return_;
62 private:
63 ~URLRequestSpecifiedResponseJob() override {
64 destruction_callback_.Run(last_load_flags_seen_);
67 // URLRequestSimpleJob implementation:
68 int GetData(std::string* mime_type,
69 std::string* charset,
70 std::string* data,
71 const CompletionCallback& callback) const override {
72 GURL url(request_->url());
73 *data = ExpectedResponseForURL(url);
74 return OK;
77 const HttpResponseInfo response_info_to_return_;
78 int last_load_flags_seen_;
79 const DestructionCallback destruction_callback_;
81 DISALLOW_COPY_AND_ASSIGN(URLRequestSpecifiedResponseJob);
84 class SpecifiedResponseJobInterceptor : public URLRequestInterceptor {
85 public:
86 // A callback to be called whenever a URLRequestSpecifiedResponseJob is
87 // created or destroyed. The first argument will be the change in number of
88 // jobs (i.e. +1 for created, -1 for destroyed).
89 // The second argument will be undefined if the job is being created,
90 // and will contain the load flags passed to the request the
91 // job was created for if the job is being destroyed.
92 typedef base::Callback<void(int outstanding_job_delta,
93 int destruction_load_flags)> LifecycleCallback;
95 // |*info| will be returned from all child URLRequestSpecifiedResponseJobs.
96 // Note that: a) this pointer is shared with the caller, and the caller must
97 // guarantee that |*info| outlives the SpecifiedResponseJobInterceptor, and
98 // b) |*info| is mutable, and changes to should propagate to
99 // URLRequestSpecifiedResponseJobs created after any change.
100 SpecifiedResponseJobInterceptor(HttpResponseInfo* http_response_info,
101 const LifecycleCallback& lifecycle_callback)
102 : http_response_info_(http_response_info),
103 lifecycle_callback_(lifecycle_callback) {
104 DCHECK(!lifecycle_callback_.is_null());
106 ~SpecifiedResponseJobInterceptor() override {}
108 URLRequestJob* MaybeInterceptRequest(
109 URLRequest* request,
110 NetworkDelegate* network_delegate) const override {
111 lifecycle_callback_.Run(1, 0);
113 return new URLRequestSpecifiedResponseJob(
114 request, network_delegate, *http_response_info_,
115 base::Bind(lifecycle_callback_, -1));
118 // The caller must ensure that both |*http_response_info| and the
119 // callback remain valid for the lifetime of the
120 // SpecifiedResponseJobInterceptor (i.e. until Unregister() is called).
121 static void RegisterWithFilter(HttpResponseInfo* http_response_info,
122 const LifecycleCallback& lifecycle_callback) {
123 scoped_ptr<SpecifiedResponseJobInterceptor> interceptor(
124 new SpecifiedResponseJobInterceptor(http_response_info,
125 lifecycle_callback));
127 URLRequestFilter::GetInstance()->AddHostnameInterceptor("http", kTestDomain,
128 interceptor.Pass());
131 static void Unregister() {
132 URLRequestFilter::GetInstance()->RemoveHostnameHandler("http", kTestDomain);
135 private:
136 HttpResponseInfo* http_response_info_;
137 LifecycleCallback lifecycle_callback_;
138 DISALLOW_COPY_AND_ASSIGN(SpecifiedResponseJobInterceptor);
141 // Local test infrastructure
142 // * URLRequestSpecifiedResponseJob: A URLRequestJob that returns
143 // a different but derivable response for each URL (used for all
144 // url requests in this file). This class is initialized with
145 // the HttpResponseInfo to return (if any), as well as a callback
146 // that is called when the class is destroyed. That callback
147 // takes as arguemnt the load flags used for the request the
148 // job was created for.
149 // * SpecifiedResponseJobInterceptor: This class is a
150 // URLRequestInterceptor that generates the class above. It is constructed
151 // with a pointer to the (mutable) resposne info that should be
152 // returned from the URLRequestSpecifiedResponseJob children, as well as
153 // a callback that is run when URLRequestSpecifiedResponseJobs are
154 // created or destroyed.
155 // * SdchDictionaryFetcherTest: This class registers the above interceptor,
156 // tracks the number of jobs requested and the subset of those
157 // that are still outstanding. It exports an interface to wait until there
158 // are no jobs outstanding. It shares an HttpResponseInfo structure
159 // with the SpecifiedResponseJobInterceptor to control the response
160 // information returned by the jbos.
161 // The standard pattern for tests is to schedule a dictionary fetch, wait
162 // for no jobs outstanding, then test that the fetch results are as expected.
164 class SdchDictionaryFetcherTest : public ::testing::Test {
165 public:
166 struct DictionaryAdditions {
167 DictionaryAdditions(const std::string& dictionary_text,
168 const GURL& dictionary_url)
169 : dictionary_text(dictionary_text), dictionary_url(dictionary_url) {}
171 std::string dictionary_text;
172 GURL dictionary_url;
175 SdchDictionaryFetcherTest()
176 : jobs_requested_(0),
177 jobs_outstanding_(0),
178 last_load_flags_seen_(LOAD_NORMAL),
179 context_(new TestURLRequestContext),
180 fetcher_(new SdchDictionaryFetcher(context_.get())),
181 factory_(this) {
182 response_info_to_return_.request_time = base::Time::Now();
183 response_info_to_return_.response_time = base::Time::Now();
184 SpecifiedResponseJobInterceptor::RegisterWithFilter(
185 &response_info_to_return_,
186 base::Bind(&SdchDictionaryFetcherTest::OnNumberJobsChanged,
187 factory_.GetWeakPtr()));
190 ~SdchDictionaryFetcherTest() override {
191 SpecifiedResponseJobInterceptor::Unregister();
194 void OnDictionaryFetched(const std::string& dictionary_text,
195 const GURL& dictionary_url,
196 const BoundNetLog& net_log,
197 bool was_from_cache) {
198 dictionary_additions_.push_back(
199 DictionaryAdditions(dictionary_text, dictionary_url));
202 // Return (in |*out|) all dictionary additions since the last time
203 // this function was called.
204 void GetDictionaryAdditions(std::vector<DictionaryAdditions>* out) {
205 out->swap(dictionary_additions_);
206 dictionary_additions_.clear();
209 SdchDictionaryFetcher* fetcher() { return fetcher_.get(); }
211 // May not be called outside the SetUp()/TearDown() interval.
212 int jobs_requested() const { return jobs_requested_; }
214 GURL PathToGurl(const char* path) const {
215 std::string gurl_string("http://");
216 gurl_string += kTestDomain;
217 gurl_string += "/";
218 gurl_string += path;
219 return GURL(gurl_string);
222 // Block until there are no outstanding URLRequestSpecifiedResponseJobs.
223 void WaitForNoJobs() {
224 if (jobs_outstanding_ == 0)
225 return;
227 run_loop_.reset(new base::RunLoop);
228 run_loop_->Run();
229 run_loop_.reset();
232 HttpResponseInfo* response_info_to_return() {
233 return &response_info_to_return_;
236 int last_load_flags_seen() const { return last_load_flags_seen_; }
238 const SdchDictionaryFetcher::OnDictionaryFetchedCallback
239 GetDefaultCallback() {
240 return base::Bind(&SdchDictionaryFetcherTest::OnDictionaryFetched,
241 base::Unretained(this));
244 private:
245 void OnNumberJobsChanged(int outstanding_jobs_delta, int load_flags) {
246 DCHECK_NE(0, outstanding_jobs_delta);
247 if (outstanding_jobs_delta > 0)
248 jobs_requested_ += outstanding_jobs_delta;
249 else
250 last_load_flags_seen_ = load_flags;
251 jobs_outstanding_ += outstanding_jobs_delta;
252 if (jobs_outstanding_ == 0 && run_loop_)
253 run_loop_->Quit();
256 int jobs_requested_;
257 int jobs_outstanding_;
259 // Last load flags seen by the interceptor installed in
260 // SdchDictionaryFetcherTest(). These are available to test bodies and
261 // currently used for ensuring that certain loads are marked only-from-cache.
262 int last_load_flags_seen_;
264 scoped_ptr<base::RunLoop> run_loop_;
265 scoped_ptr<TestURLRequestContext> context_;
266 scoped_ptr<SdchDictionaryFetcher> fetcher_;
267 std::vector<DictionaryAdditions> dictionary_additions_;
269 // The request_time and response_time fields are filled in by the constructor
270 // for SdchDictionaryFetcherTest. Tests can fill the other fields of this
271 // member in to alter the HttpResponseInfo returned by the fetcher's
272 // URLRequestJob.
273 HttpResponseInfo response_info_to_return_;
275 base::WeakPtrFactory<SdchDictionaryFetcherTest> factory_;
277 DISALLOW_COPY_AND_ASSIGN(SdchDictionaryFetcherTest);
280 // Schedule a fetch and make sure it happens.
281 TEST_F(SdchDictionaryFetcherTest, Basic) {
282 GURL dictionary_url(PathToGurl("dictionary"));
283 fetcher()->Schedule(dictionary_url, GetDefaultCallback());
284 WaitForNoJobs();
286 EXPECT_EQ(1, jobs_requested());
287 std::vector<DictionaryAdditions> additions;
288 GetDictionaryAdditions(&additions);
289 ASSERT_EQ(1u, additions.size());
290 EXPECT_EQ(
291 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url),
292 additions[0].dictionary_text);
293 EXPECT_FALSE(last_load_flags_seen() & LOAD_ONLY_FROM_CACHE);
296 // Multiple fetches of the same URL should result in only one request.
297 TEST_F(SdchDictionaryFetcherTest, Multiple) {
298 GURL dictionary_url(PathToGurl("dictionary"));
299 EXPECT_TRUE(fetcher()->Schedule(dictionary_url, GetDefaultCallback()));
300 EXPECT_FALSE(fetcher()->Schedule(dictionary_url, GetDefaultCallback()));
301 EXPECT_FALSE(fetcher()->Schedule(dictionary_url, GetDefaultCallback()));
302 WaitForNoJobs();
304 EXPECT_EQ(1, jobs_requested());
305 std::vector<DictionaryAdditions> additions;
306 GetDictionaryAdditions(&additions);
307 ASSERT_EQ(1u, additions.size());
308 EXPECT_EQ(
309 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url),
310 additions[0].dictionary_text);
313 // A cancel should result in no actual requests being generated.
314 TEST_F(SdchDictionaryFetcherTest, Cancel) {
315 GURL dictionary_url_1(PathToGurl("dictionary_1"));
316 GURL dictionary_url_2(PathToGurl("dictionary_2"));
317 GURL dictionary_url_3(PathToGurl("dictionary_3"));
319 fetcher()->Schedule(dictionary_url_1, GetDefaultCallback());
320 fetcher()->Schedule(dictionary_url_2, GetDefaultCallback());
321 fetcher()->Schedule(dictionary_url_3, GetDefaultCallback());
322 fetcher()->Cancel();
323 WaitForNoJobs();
325 // Synchronous execution may have resulted in a single job being scheduled.
326 EXPECT_GE(1, jobs_requested());
329 // Attempt to confuse the fetcher loop processing by scheduling a
330 // dictionary addition while another fetch is in process.
331 TEST_F(SdchDictionaryFetcherTest, LoopRace) {
332 GURL dictionary0_url(PathToGurl("dictionary0"));
333 GURL dictionary1_url(PathToGurl("dictionary1"));
334 fetcher()->Schedule(dictionary0_url, GetDefaultCallback());
335 fetcher()->Schedule(dictionary1_url, GetDefaultCallback());
336 WaitForNoJobs();
338 ASSERT_EQ(2, jobs_requested());
339 std::vector<DictionaryAdditions> additions;
340 GetDictionaryAdditions(&additions);
341 ASSERT_EQ(2u, additions.size());
342 EXPECT_EQ(
343 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary0_url),
344 additions[0].dictionary_text);
345 EXPECT_EQ(
346 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary1_url),
347 additions[1].dictionary_text);
350 TEST_F(SdchDictionaryFetcherTest, ScheduleReloadLoadFlags) {
351 GURL dictionary_url(PathToGurl("dictionary"));
352 fetcher()->ScheduleReload(dictionary_url, GetDefaultCallback());
354 WaitForNoJobs();
355 EXPECT_EQ(1, jobs_requested());
356 std::vector<DictionaryAdditions> additions;
357 GetDictionaryAdditions(&additions);
358 ASSERT_EQ(1u, additions.size());
359 EXPECT_EQ(
360 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url),
361 additions[0].dictionary_text);
362 EXPECT_TRUE(last_load_flags_seen() & LOAD_ONLY_FROM_CACHE);
365 TEST_F(SdchDictionaryFetcherTest, ScheduleReloadFresh) {
366 std::string raw_headers = "\0";
367 response_info_to_return()->headers = new HttpResponseHeaders(
368 HttpUtil::AssembleRawHeaders(raw_headers.data(), raw_headers.size()));
369 response_info_to_return()->headers->AddHeader("Cache-Control: max-age=1000");
371 GURL dictionary_url(PathToGurl("dictionary"));
372 fetcher()->ScheduleReload(dictionary_url, GetDefaultCallback());
374 WaitForNoJobs();
375 EXPECT_EQ(1, jobs_requested());
376 std::vector<DictionaryAdditions> additions;
377 GetDictionaryAdditions(&additions);
378 ASSERT_EQ(1u, additions.size());
379 EXPECT_EQ(
380 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url),
381 additions[0].dictionary_text);
382 EXPECT_TRUE(last_load_flags_seen() & LOAD_ONLY_FROM_CACHE);
385 TEST_F(SdchDictionaryFetcherTest, ScheduleReloadStale) {
386 response_info_to_return()->headers = new HttpResponseHeaders("");
387 response_info_to_return()->headers->AddHeader("Cache-Control: no-cache");
389 GURL dictionary_url(PathToGurl("dictionary"));
390 fetcher()->ScheduleReload(dictionary_url, GetDefaultCallback());
392 WaitForNoJobs();
393 EXPECT_EQ(1, jobs_requested());
394 std::vector<DictionaryAdditions> additions;
395 GetDictionaryAdditions(&additions);
396 EXPECT_EQ(0u, additions.size());
397 EXPECT_TRUE(last_load_flags_seen() & LOAD_ONLY_FROM_CACHE);
400 TEST_F(SdchDictionaryFetcherTest, ScheduleReloadThenLoad) {
401 GURL dictionary_url(PathToGurl("dictionary"));
402 EXPECT_TRUE(fetcher()->ScheduleReload(dictionary_url, GetDefaultCallback()));
403 EXPECT_TRUE(fetcher()->Schedule(dictionary_url, GetDefaultCallback()));
405 WaitForNoJobs();
406 EXPECT_EQ(2, jobs_requested());
409 TEST_F(SdchDictionaryFetcherTest, ScheduleLoadThenReload) {
410 GURL dictionary_url(PathToGurl("dictionary"));
411 EXPECT_TRUE(fetcher()->Schedule(dictionary_url, GetDefaultCallback()));
412 EXPECT_FALSE(fetcher()->ScheduleReload(dictionary_url, GetDefaultCallback()));
414 WaitForNoJobs();
415 EXPECT_EQ(1, jobs_requested());
418 TEST_F(SdchDictionaryFetcherTest, CancelAllowsFutureFetches) {
419 GURL dictionary_url(PathToGurl("dictionary"));
420 EXPECT_TRUE(fetcher()->Schedule(dictionary_url, GetDefaultCallback()));
421 EXPECT_FALSE(fetcher()->Schedule(dictionary_url, GetDefaultCallback()));
423 WaitForNoJobs();
424 EXPECT_EQ(1, jobs_requested());
426 fetcher()->Cancel();
427 WaitForNoJobs();
428 EXPECT_TRUE(fetcher()->Schedule(dictionary_url, GetDefaultCallback()));
430 WaitForNoJobs();
431 EXPECT_EQ(2, jobs_requested());
434 } // namespace
436 } // namespace net