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