OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/url_request/sdch_dictionary_fetcher.h" | 5 #include "net/url_request/sdch_dictionary_fetcher.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
13 #include "base/thread_task_runner_handle.h" | 13 #include "base/thread_task_runner_handle.h" |
14 #include "net/base/load_flags.h" | |
14 #include "net/base/sdch_manager.h" | 15 #include "net/base/sdch_manager.h" |
16 #include "net/http/http_response_headers.h" | |
15 #include "net/url_request/url_request_data_job.h" | 17 #include "net/url_request/url_request_data_job.h" |
16 #include "net/url_request/url_request_filter.h" | 18 #include "net/url_request/url_request_filter.h" |
17 #include "net/url_request/url_request_interceptor.h" | 19 #include "net/url_request/url_request_interceptor.h" |
18 #include "net/url_request/url_request_test_util.h" | 20 #include "net/url_request/url_request_test_util.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
20 | 22 |
21 namespace net { | 23 namespace net { |
22 | 24 |
23 namespace { | 25 namespace { |
24 | 26 |
25 const char kSampleBufferContext[] = "This is a sample buffer."; | 27 const char kSampleBufferContext[] = "This is a sample buffer."; |
26 const char kTestDomain[] = "top.domain.test"; | 28 const char kTestDomain[] = "top.domain.test"; |
27 | 29 |
28 class URLRequestSpecifiedResponseJob : public URLRequestSimpleJob { | 30 class URLRequestSpecifiedResponseJob : public URLRequestSimpleJob { |
29 public: | 31 public: |
30 URLRequestSpecifiedResponseJob(URLRequest* request, | 32 // Called on destruction with load flags used for this request. |
31 NetworkDelegate* network_delegate, | 33 typedef base::Callback<void(int)> DestructionCallback; |
32 const base::Closure& destruction_callback) | 34 |
35 URLRequestSpecifiedResponseJob( | |
36 URLRequest* request, | |
37 NetworkDelegate* network_delegate, | |
38 const HttpResponseInfo& response_info_to_return, | |
39 const DestructionCallback& destruction_callback) | |
33 : URLRequestSimpleJob(request, network_delegate), | 40 : URLRequestSimpleJob(request, network_delegate), |
41 response_info_to_return_(response_info_to_return), | |
42 last_load_flags_seen_(request->load_flags()), | |
34 destruction_callback_(destruction_callback) { | 43 destruction_callback_(destruction_callback) { |
35 DCHECK(!destruction_callback.is_null()); | 44 DCHECK(!destruction_callback.is_null()); |
36 } | 45 } |
37 | 46 |
38 static std::string ExpectedResponseForURL(const GURL& url) { | 47 static std::string ExpectedResponseForURL(const GURL& url) { |
39 return base::StringPrintf("Response for %s\n%s\nEnd Response for %s\n", | 48 return base::StringPrintf("Response for %s\n%s\nEnd Response for %s\n", |
40 url.spec().c_str(), | 49 url.spec().c_str(), |
41 kSampleBufferContext, | 50 kSampleBufferContext, |
42 url.spec().c_str()); | 51 url.spec().c_str()); |
43 } | 52 } |
44 | 53 |
54 // URLRequestJob | |
55 void GetResponseInfo(HttpResponseInfo* info) override { | |
56 *info = response_info_to_return_; | |
57 } | |
58 | |
45 private: | 59 private: |
46 ~URLRequestSpecifiedResponseJob() override { destruction_callback_.Run(); } | 60 ~URLRequestSpecifiedResponseJob() override { |
61 destruction_callback_.Run(last_load_flags_seen_); | |
62 } | |
47 | 63 |
48 // URLRequestSimpleJob implementation: | 64 // URLRequestSimpleJob implementation: |
49 int GetData(std::string* mime_type, | 65 int GetData(std::string* mime_type, |
50 std::string* charset, | 66 std::string* charset, |
51 std::string* data, | 67 std::string* data, |
52 const CompletionCallback& callback) const override { | 68 const CompletionCallback& callback) const override { |
53 GURL url(request_->url()); | 69 GURL url(request_->url()); |
54 *data = ExpectedResponseForURL(url); | 70 *data = ExpectedResponseForURL(url); |
55 return OK; | 71 return OK; |
56 } | 72 } |
57 | 73 |
58 const base::Closure destruction_callback_; | 74 const HttpResponseInfo response_info_to_return_; |
75 int last_load_flags_seen_; | |
76 const DestructionCallback destruction_callback_; | |
59 }; | 77 }; |
Alexei Svitkine (slow)
2015/02/19 16:25:32
Nit: DISALLOW()?
mmenke
2015/02/19 20:14:35
And include the header for it.
Elly Fong-Jones
2015/02/20 20:35:26
Done.
Elly Fong-Jones
2015/02/20 20:35:26
Done.
| |
60 | 78 |
61 class SpecifiedResponseJobInterceptor : public URLRequestInterceptor { | 79 class SpecifiedResponseJobInterceptor : public URLRequestInterceptor { |
62 public: | 80 public: |
63 // A callback to be called whenever a URLRequestSpecifiedResponseJob is | 81 // A callback to be called whenever a URLRequestSpecifiedResponseJob is |
64 // created or destroyed. The argument will be the change in number of | 82 // created or destroyed. The first argument will be the change in number of |
65 // jobs (i.e. +1 for created, -1 for destroyed). | 83 // jobs (i.e. +1 for created, -1 for destroyed). |
66 typedef base::Callback<void(int outstanding_job_delta)> LifecycleCallback; | 84 // The second argument will be undefined if the job is being created, |
85 // and will contain the load flags passed to the request the | |
86 // job was created for if the job is being destroyed. | |
87 typedef base::Callback<void(int outstanding_job_delta, | |
88 int destruction_load_flags)> LifecycleCallback; | |
67 | 89 |
68 explicit SpecifiedResponseJobInterceptor( | 90 // |*info| will be returned from all child URLRequestSpecifiedResponseJobs. |
69 const LifecycleCallback& lifecycle_callback) | 91 // Note that: a) this pointer is shared with the caller, and the caller must |
70 : lifecycle_callback_(lifecycle_callback), factory_(this) { | 92 // guarantee that |*info| outlives the SpecifiedResponseJobInterceptor, and |
93 // b) |*info| is mutable, and changes to should propagate to | |
94 // URLRequestSpecifiedResponseJobs created after any change. | |
95 SpecifiedResponseJobInterceptor(HttpResponseInfo* http_response_info, | |
96 const LifecycleCallback& lifecycle_callback) | |
97 : http_response_info_(http_response_info), | |
98 lifecycle_callback_(lifecycle_callback) { | |
71 DCHECK(!lifecycle_callback_.is_null()); | 99 DCHECK(!lifecycle_callback_.is_null()); |
72 } | 100 } |
73 ~SpecifiedResponseJobInterceptor() override {} | 101 ~SpecifiedResponseJobInterceptor() override {} |
74 | 102 |
75 URLRequestJob* MaybeInterceptRequest( | 103 URLRequestJob* MaybeInterceptRequest( |
76 URLRequest* request, | 104 URLRequest* request, |
77 NetworkDelegate* network_delegate) const override { | 105 NetworkDelegate* network_delegate) const override { |
78 if (!lifecycle_callback_.is_null()) | 106 lifecycle_callback_.Run(1, 0); |
79 lifecycle_callback_.Run(1); | |
80 | 107 |
81 return new URLRequestSpecifiedResponseJob( | 108 return new URLRequestSpecifiedResponseJob( |
82 request, network_delegate, base::Bind(lifecycle_callback_, -1)); | 109 request, network_delegate, *http_response_info_, |
110 base::Bind(lifecycle_callback_, -1)); | |
83 } | 111 } |
84 | 112 |
85 // The caller must ensure that the callback is valid to call for the | 113 // The caller must ensure that both |*http_response_info| and the |
86 // lifetime of the SpecifiedResponseJobInterceptor (i.e. until | 114 // callback remain valid for the lifetime of the |
87 // Unregister() is called). | 115 // SpecifiedResponseJobInterceptor (i.e. until Unregister() is called). |
88 static void RegisterWithFilter(const LifecycleCallback& lifecycle_callback) { | 116 static void RegisterWithFilter(HttpResponseInfo* http_response_info, |
117 const LifecycleCallback& lifecycle_callback) { | |
89 scoped_ptr<SpecifiedResponseJobInterceptor> interceptor( | 118 scoped_ptr<SpecifiedResponseJobInterceptor> interceptor( |
90 new SpecifiedResponseJobInterceptor(lifecycle_callback)); | 119 new SpecifiedResponseJobInterceptor(http_response_info, |
120 lifecycle_callback)); | |
91 | 121 |
92 net::URLRequestFilter::GetInstance()->AddHostnameInterceptor( | 122 net::URLRequestFilter::GetInstance()->AddHostnameInterceptor( |
93 "http", kTestDomain, interceptor.Pass()); | 123 "http", kTestDomain, interceptor.Pass()); |
94 } | 124 } |
95 | 125 |
96 static void Unregister() { | 126 static void Unregister() { |
97 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler("http", | 127 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler("http", |
98 kTestDomain); | 128 kTestDomain); |
99 } | 129 } |
100 | 130 |
101 private: | 131 private: |
102 void OnSpecfiedResponseJobDestruction() const { | 132 HttpResponseInfo* http_response_info_; |
103 if (!lifecycle_callback_.is_null()) | |
104 lifecycle_callback_.Run(-1); | |
105 } | |
106 | |
107 LifecycleCallback lifecycle_callback_; | 133 LifecycleCallback lifecycle_callback_; |
108 mutable base::WeakPtrFactory<SpecifiedResponseJobInterceptor> factory_; | |
109 }; | 134 }; |
110 | 135 |
111 // Local test infrastructure | 136 // Local test infrastructure |
112 // * URLRequestSpecifiedResponseJob: A URLRequestJob that returns | 137 // * URLRequestSpecifiedResponseJob: A URLRequestJob that returns |
113 // a different but derivable response for each URL (used for all | 138 // a different but derivable response for each URL (used for all |
114 // url requests in this file). The class provides interfaces to | 139 // url requests in this file). This class is initialized with |
115 // signal whenever the total number of jobs transitions to zero. | 140 // the HttpResponseInfo to return (if any), as well as a callback |
116 // * SdchDictionaryFetcherTest: Registers a callback with the above | 141 // that is called when the class is destroyed. That callback |
117 // class, and provides blocking interfaces for a transition to zero jobs. | 142 // takes as arguemnt the load flags used for the request the |
118 // Contains an SdchDictionaryFetcher, and tracks fetcher dictionary | 143 // job was created for. |
119 // addition callbacks. | 144 // * SpecifiedResponseJobInterceptor: This class is a |
120 // Most tests schedule a dictionary fetch, wait for no jobs outstanding, | 145 // URLRequestInterceptor that generates the class above. It is constructed |
121 // then test that the fetch results are as expected. | 146 // with a pointer to the (mutable) resposne info that should be |
147 // returned from the URLRequestSpecifiedResponseJob children, as well as | |
148 // a callback that is run when URLRequestSpecifiedResponseJobs are | |
149 // created or destroyed. | |
150 // * SdchDictionaryFetcherTest: This class registers the above interceptor, | |
151 // tracks the number of jobs requested and the subset of those | |
152 // that are still outstanding. It exports an interface to wait until there | |
153 // are no jobs outstanding. It shares an HttpResponseInfo structure | |
154 // with the SpecifiedResponseJobInterceptor to control the response | |
155 // information returned by the jbos. | |
156 // The standard pattern for tests is to schedule a dictionary fetch, wait | |
157 // for no jobs outstanding, then test that the fetch results are as expected. | |
122 | 158 |
123 class SdchDictionaryFetcherTest : public ::testing::Test { | 159 class SdchDictionaryFetcherTest : public ::testing::Test { |
124 public: | 160 public: |
125 struct DictionaryAdditions { | 161 struct DictionaryAdditions { |
126 DictionaryAdditions(const std::string& dictionary_text, | 162 DictionaryAdditions(const std::string& dictionary_text, |
127 const GURL& dictionary_url) | 163 const GURL& dictionary_url) |
128 : dictionary_text(dictionary_text), dictionary_url(dictionary_url) {} | 164 : dictionary_text(dictionary_text), dictionary_url(dictionary_url) {} |
129 | 165 |
130 std::string dictionary_text; | 166 std::string dictionary_text; |
131 GURL dictionary_url; | 167 GURL dictionary_url; |
132 }; | 168 }; |
133 | 169 |
134 SdchDictionaryFetcherTest() | 170 SdchDictionaryFetcherTest() |
135 : jobs_requested_(0), | 171 : jobs_requested_(0), |
136 jobs_outstanding_(0), | 172 jobs_outstanding_(0), |
173 last_load_flags_seen_(LOAD_NORMAL), | |
137 context_(new TestURLRequestContext), | 174 context_(new TestURLRequestContext), |
138 fetcher_(new SdchDictionaryFetcher( | 175 fetcher_(new SdchDictionaryFetcher(context_.get())), |
139 context_.get(), | |
140 base::Bind(&SdchDictionaryFetcherTest::OnDictionaryFetched, | |
141 base::Unretained(this)))), | |
142 factory_(this) { | 176 factory_(this) { |
177 response_info_to_return_.request_time = base::Time::Now(); | |
178 response_info_to_return_.response_time = base::Time::Now(); | |
143 SpecifiedResponseJobInterceptor::RegisterWithFilter( | 179 SpecifiedResponseJobInterceptor::RegisterWithFilter( |
180 &response_info_to_return_, | |
144 base::Bind(&SdchDictionaryFetcherTest::OnNumberJobsChanged, | 181 base::Bind(&SdchDictionaryFetcherTest::OnNumberJobsChanged, |
145 factory_.GetWeakPtr())); | 182 factory_.GetWeakPtr())); |
146 } | 183 } |
147 | 184 |
148 ~SdchDictionaryFetcherTest() override { | 185 ~SdchDictionaryFetcherTest() override { |
149 SpecifiedResponseJobInterceptor::Unregister(); | 186 SpecifiedResponseJobInterceptor::Unregister(); |
150 } | 187 } |
151 | 188 |
152 void OnDictionaryFetched(const std::string& dictionary_text, | 189 void OnDictionaryFetched(const std::string& dictionary_text, |
153 const GURL& dictionary_url, | 190 const GURL& dictionary_url, |
154 const BoundNetLog& net_log) { | 191 const BoundNetLog& net_log) { |
155 dictionary_additions.push_back( | 192 dictionary_additions_.push_back( |
156 DictionaryAdditions(dictionary_text, dictionary_url)); | 193 DictionaryAdditions(dictionary_text, dictionary_url)); |
157 } | 194 } |
158 | 195 |
159 // Return (in |*out|) all dictionary additions since the last time | 196 // Return (in |*out|) all dictionary additions since the last time |
160 // this function was called. | 197 // this function was called. |
161 void GetDictionaryAdditions(std::vector<DictionaryAdditions>* out) { | 198 void GetDictionaryAdditions(std::vector<DictionaryAdditions>* out) { |
162 out->swap(dictionary_additions); | 199 out->swap(dictionary_additions_); |
163 dictionary_additions.clear(); | 200 dictionary_additions_.clear(); |
164 } | 201 } |
165 | 202 |
166 SdchDictionaryFetcher* fetcher() { return fetcher_.get(); } | 203 SdchDictionaryFetcher* fetcher() { return fetcher_.get(); } |
167 | 204 |
168 // May not be called outside the SetUp()/TearDown() interval. | 205 // May not be called outside the SetUp()/TearDown() interval. |
169 int jobs_requested() const { return jobs_requested_; } | 206 int jobs_requested() const { return jobs_requested_; } |
170 | 207 |
171 GURL PathToGurl(const char* path) { | 208 GURL PathToGurl(const char* path) const { |
172 std::string gurl_string("http://"); | 209 std::string gurl_string("http://"); |
173 gurl_string += kTestDomain; | 210 gurl_string += kTestDomain; |
174 gurl_string += "/"; | 211 gurl_string += "/"; |
175 gurl_string += path; | 212 gurl_string += path; |
176 return GURL(gurl_string); | 213 return GURL(gurl_string); |
177 } | 214 } |
178 | 215 |
179 // Block until there are no outstanding URLRequestSpecifiedResponseJobs. | 216 // Block until there are no outstanding URLRequestSpecifiedResponseJobs. |
180 void WaitForNoJobs() { | 217 void WaitForNoJobs() { |
181 if (jobs_outstanding_ == 0) | 218 if (jobs_outstanding_ == 0) |
182 return; | 219 return; |
183 | 220 |
184 run_loop_.reset(new base::RunLoop); | 221 run_loop_.reset(new base::RunLoop); |
185 run_loop_->Run(); | 222 run_loop_->Run(); |
186 run_loop_.reset(); | 223 run_loop_.reset(); |
187 } | 224 } |
188 | 225 |
226 HttpResponseInfo* response_info_to_return() { | |
227 return &response_info_to_return_; | |
228 } | |
229 | |
230 int last_load_flags_seen() const { return last_load_flags_seen_; } | |
231 | |
232 const SdchDictionaryFetcher::OnDictionaryFetchedCallback | |
233 GetDefaultCallback() { | |
234 return base::Bind(&SdchDictionaryFetcherTest::OnDictionaryFetched, | |
235 base::Unretained(this)); | |
236 } | |
237 | |
189 private: | 238 private: |
190 void OnNumberJobsChanged(int outstanding_jobs_delta) { | 239 void OnNumberJobsChanged(int outstanding_jobs_delta, int load_flags) { |
240 DCHECK_NE(0, outstanding_jobs_delta); | |
mmenke
2015/02/19 21:44:58
Include base/logging.h for DCHECK
Elly Fong-Jones
2015/02/20 20:35:26
Done.
| |
191 if (outstanding_jobs_delta > 0) | 241 if (outstanding_jobs_delta > 0) |
192 jobs_requested_ += outstanding_jobs_delta; | 242 jobs_requested_ += outstanding_jobs_delta; |
243 else | |
244 last_load_flags_seen_ = load_flags; | |
193 jobs_outstanding_ += outstanding_jobs_delta; | 245 jobs_outstanding_ += outstanding_jobs_delta; |
194 if (jobs_outstanding_ == 0 && run_loop_) | 246 if (jobs_outstanding_ == 0 && run_loop_) |
195 run_loop_->Quit(); | 247 run_loop_->Quit(); |
196 } | 248 } |
197 | 249 |
198 int jobs_requested_; | 250 int jobs_requested_; |
199 int jobs_outstanding_; | 251 int jobs_outstanding_; |
252 // Last load flags seen by the interceptor installed in | |
253 // SdchDictionaryFetcherTest(). These are available to test bodies and | |
254 // currently used for ensuring that certain loads are marked only-from-cache. | |
255 int last_load_flags_seen_; | |
200 scoped_ptr<base::RunLoop> run_loop_; | 256 scoped_ptr<base::RunLoop> run_loop_; |
201 scoped_ptr<TestURLRequestContext> context_; | 257 scoped_ptr<TestURLRequestContext> context_; |
202 scoped_ptr<SdchDictionaryFetcher> fetcher_; | 258 scoped_ptr<SdchDictionaryFetcher> fetcher_; |
203 std::vector<DictionaryAdditions> dictionary_additions; | 259 std::vector<DictionaryAdditions> dictionary_additions_; |
260 // The request_time and response_time fields are filled in by the constructor | |
261 // for SdchDictionaryFetcherTest. Tests can fill the other fields of this | |
262 // member in to alter the HttpResponseInfo returned by the fetcher's | |
263 // URLRequestJob. | |
264 HttpResponseInfo response_info_to_return_; | |
204 base::WeakPtrFactory<SdchDictionaryFetcherTest> factory_; | 265 base::WeakPtrFactory<SdchDictionaryFetcherTest> factory_; |
205 }; | 266 }; |
206 | 267 |
207 // Schedule a fetch and make sure it happens. | 268 // Schedule a fetch and make sure it happens. |
208 TEST_F(SdchDictionaryFetcherTest, Basic) { | 269 TEST_F(SdchDictionaryFetcherTest, Basic) { |
209 GURL dictionary_url(PathToGurl("dictionary")); | 270 GURL dictionary_url(PathToGurl("dictionary")); |
210 fetcher()->Schedule(dictionary_url); | 271 fetcher()->Schedule(dictionary_url, GetDefaultCallback()); |
211 WaitForNoJobs(); | 272 WaitForNoJobs(); |
212 | 273 |
213 EXPECT_EQ(1, jobs_requested()); | 274 EXPECT_EQ(1, jobs_requested()); |
214 std::vector<DictionaryAdditions> additions; | 275 std::vector<DictionaryAdditions> additions; |
215 GetDictionaryAdditions(&additions); | 276 GetDictionaryAdditions(&additions); |
216 ASSERT_EQ(1u, additions.size()); | 277 ASSERT_EQ(1u, additions.size()); |
217 EXPECT_EQ( | 278 EXPECT_EQ( |
218 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url), | 279 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url), |
219 additions[0].dictionary_text); | 280 additions[0].dictionary_text); |
281 EXPECT_FALSE(last_load_flags_seen() & LOAD_ONLY_FROM_CACHE); | |
220 } | 282 } |
221 | 283 |
222 // Multiple fetches of the same URL should result in only one request. | 284 // Multiple fetches of the same URL should result in only one request. |
223 TEST_F(SdchDictionaryFetcherTest, Multiple) { | 285 TEST_F(SdchDictionaryFetcherTest, Multiple) { |
224 GURL dictionary_url(PathToGurl("dictionary")); | 286 GURL dictionary_url(PathToGurl("dictionary")); |
225 fetcher()->Schedule(dictionary_url); | 287 fetcher()->Schedule(dictionary_url, GetDefaultCallback()); |
226 fetcher()->Schedule(dictionary_url); | 288 fetcher()->Schedule(dictionary_url, GetDefaultCallback()); |
227 fetcher()->Schedule(dictionary_url); | 289 fetcher()->Schedule(dictionary_url, GetDefaultCallback()); |
228 WaitForNoJobs(); | 290 WaitForNoJobs(); |
229 | 291 |
230 EXPECT_EQ(1, jobs_requested()); | 292 EXPECT_EQ(1, jobs_requested()); |
231 std::vector<DictionaryAdditions> additions; | 293 std::vector<DictionaryAdditions> additions; |
232 GetDictionaryAdditions(&additions); | 294 GetDictionaryAdditions(&additions); |
233 ASSERT_EQ(1u, additions.size()); | 295 ASSERT_EQ(1u, additions.size()); |
234 EXPECT_EQ( | 296 EXPECT_EQ( |
235 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url), | 297 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url), |
236 additions[0].dictionary_text); | 298 additions[0].dictionary_text); |
237 } | 299 } |
238 | 300 |
239 // A cancel should result in no actual requests being generated. | 301 // A cancel should result in no actual requests being generated. |
240 TEST_F(SdchDictionaryFetcherTest, Cancel) { | 302 TEST_F(SdchDictionaryFetcherTest, Cancel) { |
241 GURL dictionary_url_1(PathToGurl("dictionary_1")); | 303 GURL dictionary_url_1(PathToGurl("dictionary_1")); |
242 GURL dictionary_url_2(PathToGurl("dictionary_2")); | 304 GURL dictionary_url_2(PathToGurl("dictionary_2")); |
243 GURL dictionary_url_3(PathToGurl("dictionary_3")); | 305 GURL dictionary_url_3(PathToGurl("dictionary_3")); |
244 | 306 |
245 fetcher()->Schedule(dictionary_url_1); | 307 fetcher()->Schedule(dictionary_url_1, GetDefaultCallback()); |
246 fetcher()->Schedule(dictionary_url_2); | 308 fetcher()->Schedule(dictionary_url_2, GetDefaultCallback()); |
247 fetcher()->Schedule(dictionary_url_3); | 309 fetcher()->Schedule(dictionary_url_3, GetDefaultCallback()); |
248 fetcher()->Cancel(); | 310 fetcher()->Cancel(); |
249 WaitForNoJobs(); | 311 WaitForNoJobs(); |
250 | 312 |
251 // Synchronous execution may have resulted in a single job being scheduled. | 313 // Synchronous execution may have resulted in a single job being scheduled. |
252 EXPECT_GE(1, jobs_requested()); | 314 EXPECT_GE(1, jobs_requested()); |
253 } | 315 } |
254 | 316 |
255 // Attempt to confuse the fetcher loop processing by scheduling a | 317 // Attempt to confuse the fetcher loop processing by scheduling a |
256 // dictionary addition while another fetch is in process. | 318 // dictionary addition while another fetch is in process. |
257 TEST_F(SdchDictionaryFetcherTest, LoopRace) { | 319 TEST_F(SdchDictionaryFetcherTest, LoopRace) { |
258 GURL dictionary0_url(PathToGurl("dictionary0")); | 320 GURL dictionary0_url(PathToGurl("dictionary0")); |
259 GURL dictionary1_url(PathToGurl("dictionary1")); | 321 GURL dictionary1_url(PathToGurl("dictionary1")); |
260 fetcher()->Schedule(dictionary0_url); | 322 fetcher()->Schedule(dictionary0_url, GetDefaultCallback()); |
261 fetcher()->Schedule(dictionary1_url); | 323 fetcher()->Schedule(dictionary1_url, GetDefaultCallback()); |
262 WaitForNoJobs(); | 324 WaitForNoJobs(); |
263 | 325 |
264 ASSERT_EQ(2, jobs_requested()); | 326 ASSERT_EQ(2, jobs_requested()); |
265 std::vector<DictionaryAdditions> additions; | 327 std::vector<DictionaryAdditions> additions; |
266 GetDictionaryAdditions(&additions); | 328 GetDictionaryAdditions(&additions); |
267 ASSERT_EQ(2u, additions.size()); | 329 ASSERT_EQ(2u, additions.size()); |
268 EXPECT_EQ( | 330 EXPECT_EQ( |
269 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary0_url), | 331 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary0_url), |
270 additions[0].dictionary_text); | 332 additions[0].dictionary_text); |
271 EXPECT_EQ( | 333 EXPECT_EQ( |
272 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary1_url), | 334 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary1_url), |
273 additions[1].dictionary_text); | 335 additions[1].dictionary_text); |
274 } | 336 } |
275 | 337 |
338 TEST_F(SdchDictionaryFetcherTest, ScheduleReloadLoadFlags) { | |
mmenke
2015/02/19 21:44:58
Should probably have a test where we have a couple
| |
339 GURL dictionary_url(PathToGurl("dictionary")); | |
340 fetcher()->ScheduleReload(dictionary_url, GetDefaultCallback()); | |
341 | |
342 WaitForNoJobs(); | |
343 EXPECT_EQ(1, jobs_requested()); | |
344 std::vector<DictionaryAdditions> additions; | |
mmenke
2015/02/19 21:44:58
Should include <vector>
Elly Fong-Jones
2015/02/20 20:35:26
Done.
| |
345 GetDictionaryAdditions(&additions); | |
346 ASSERT_EQ(1u, additions.size()); | |
347 EXPECT_EQ( | |
348 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url), | |
349 additions[0].dictionary_text); | |
350 EXPECT_TRUE(last_load_flags_seen() & LOAD_ONLY_FROM_CACHE); | |
351 } | |
352 | |
353 TEST_F(SdchDictionaryFetcherTest, ScheduleReloadFresh) { | |
354 std::string raw_headers = "\0"; | |
355 response_info_to_return()->headers = new HttpResponseHeaders( | |
356 HttpUtil::AssembleRawHeaders(raw_headers.data(), raw_headers.size())); | |
357 response_info_to_return()->headers->AddHeader("Cache-Control: max-age=1000"); | |
358 | |
359 GURL dictionary_url(PathToGurl("dictionary")); | |
360 fetcher()->ScheduleReload(dictionary_url, GetDefaultCallback()); | |
361 | |
362 WaitForNoJobs(); | |
363 EXPECT_EQ(1, jobs_requested()); | |
364 std::vector<DictionaryAdditions> additions; | |
365 GetDictionaryAdditions(&additions); | |
366 ASSERT_EQ(1u, additions.size()); | |
367 EXPECT_EQ( | |
368 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url), | |
369 additions[0].dictionary_text); | |
370 EXPECT_TRUE(last_load_flags_seen() & LOAD_ONLY_FROM_CACHE); | |
371 } | |
372 | |
373 TEST_F(SdchDictionaryFetcherTest, ScheduleReloadStale) { | |
374 response_info_to_return()->headers = new HttpResponseHeaders(""); | |
375 response_info_to_return()->headers->AddHeader("Cache-Control: no-cache"); | |
376 | |
377 GURL dictionary_url(PathToGurl("dictionary")); | |
378 fetcher()->ScheduleReload(dictionary_url, GetDefaultCallback()); | |
379 | |
380 WaitForNoJobs(); | |
381 EXPECT_EQ(1, jobs_requested()); | |
382 std::vector<DictionaryAdditions> additions; | |
383 GetDictionaryAdditions(&additions); | |
384 EXPECT_EQ(0u, additions.size()); | |
385 EXPECT_TRUE(last_load_flags_seen() & LOAD_ONLY_FROM_CACHE); | |
386 } | |
387 | |
276 } // namespace | 388 } // namespace |
277 | 389 |
278 } // namespace net | 390 } // namespace net |
OLD | NEW |