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