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