| 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), |
| 138 context_(new TestURLRequestContext), | 176 context_(new TestURLRequestContext), |
| 139 fetcher_(new SdchDictionaryFetcher( | 177 fetcher_(new SdchDictionaryFetcher( |
| 140 context_.get(), | 178 context_.get(), |
| 141 base::Bind(&SdchDictionaryFetcherTest::OnDictionaryFetched, | 179 base::Bind(&SdchDictionaryFetcherTest::OnDictionaryFetched, |
| 142 base::Unretained(this)))), | 180 base::Unretained(this)))), |
| 143 factory_(this) { | 181 factory_(this) { |
| 182 response_info_to_return_.request_time = base::Time::Now(); |
| 183 response_info_to_return_.response_time = base::Time::Now(); |
| 144 SpecifiedResponseJobInterceptor::RegisterWithFilter( | 184 SpecifiedResponseJobInterceptor::RegisterWithFilter( |
| 185 &response_info_to_return_, |
| 145 base::Bind(&SdchDictionaryFetcherTest::OnNumberJobsChanged, | 186 base::Bind(&SdchDictionaryFetcherTest::OnNumberJobsChanged, |
| 146 factory_.GetWeakPtr())); | 187 factory_.GetWeakPtr())); |
| 147 } | 188 } |
| 148 | 189 |
| 149 ~SdchDictionaryFetcherTest() override { | 190 ~SdchDictionaryFetcherTest() override { |
| 150 SpecifiedResponseJobInterceptor::Unregister(); | 191 SpecifiedResponseJobInterceptor::Unregister(); |
| 151 } | 192 } |
| 152 | 193 |
| 153 void OnDictionaryFetched(const std::string& dictionary_text, | 194 void OnDictionaryFetched( |
| 154 const GURL& dictionary_url, | 195 const std::string& dictionary_text, |
| 155 const BoundNetLog& net_log) { | 196 const GURL& dictionary_url, |
| 197 scoped_ptr<net::SdchDictionaryFetcher::Data> extra_data, |
| 198 const BoundNetLog& net_log) { |
| 156 dictionary_additions.push_back( | 199 dictionary_additions.push_back( |
| 157 DictionaryAdditions(dictionary_text, dictionary_url)); | 200 DictionaryAdditions(dictionary_text, dictionary_url)); |
| 201 last_extra_data_ = extra_data.Pass(); |
| 158 } | 202 } |
| 159 | 203 |
| 160 // Return (in |*out|) all dictionary additions since the last time | 204 // Return (in |*out|) all dictionary additions since the last time |
| 161 // this function was called. | 205 // this function was called. |
| 162 void GetDictionaryAdditions(std::vector<DictionaryAdditions>* out) { | 206 void GetDictionaryAdditions(std::vector<DictionaryAdditions>* out) { |
| 163 out->swap(dictionary_additions); | 207 out->swap(dictionary_additions); |
| 164 dictionary_additions.clear(); | 208 dictionary_additions.clear(); |
| 165 } | 209 } |
| 166 | 210 |
| 167 SdchDictionaryFetcher* fetcher() { return fetcher_.get(); } | 211 SdchDictionaryFetcher* fetcher() { return fetcher_.get(); } |
| 168 | 212 |
| 169 // May not be called outside the SetUp()/TearDown() interval. | 213 // May not be called outside the SetUp()/TearDown() interval. |
| 170 int jobs_requested() const { return jobs_requested_; } | 214 int jobs_requested() const { return jobs_requested_; } |
| 171 | 215 |
| 172 GURL PathToGurl(const char* path) { | 216 GURL PathToGurl(const char* path) const { |
| 173 std::string gurl_string("http://"); | 217 std::string gurl_string("http://"); |
| 174 gurl_string += kTestDomain; | 218 gurl_string += kTestDomain; |
| 175 gurl_string += "/"; | 219 gurl_string += "/"; |
| 176 gurl_string += path; | 220 gurl_string += path; |
| 177 return GURL(gurl_string); | 221 return GURL(gurl_string); |
| 178 } | 222 } |
| 179 | 223 |
| 180 // Block until there are no outstanding URLRequestSpecifiedResponseJobs. | 224 // Block until there are no outstanding URLRequestSpecifiedResponseJobs. |
| 181 void WaitForNoJobs() { | 225 void WaitForNoJobs() { |
| 182 if (jobs_outstanding_ == 0) | 226 if (jobs_outstanding_ == 0) |
| 183 return; | 227 return; |
| 184 | 228 |
| 185 run_loop_.reset(new base::RunLoop); | 229 run_loop_.reset(new base::RunLoop); |
| 186 run_loop_->Run(); | 230 run_loop_->Run(); |
| 187 run_loop_.reset(); | 231 run_loop_.reset(); |
| 188 } | 232 } |
| 189 | 233 |
| 234 HttpResponseInfo* response_info_to_return() { |
| 235 return &response_info_to_return_; |
| 236 } |
| 237 |
| 238 // Simplified wrapper around fetcher()->Schedule(). |
| 239 void Schedule(const GURL& dictionary_url) { |
| 240 fetcher()->Schedule(dictionary_url, |
| 241 scoped_ptr<net::SdchDictionaryFetcher::Data>()); |
| 242 } |
| 243 |
| 244 const SdchDictionaryFetcher::Data* last_extra_data() const { |
| 245 return last_extra_data_.get(); |
| 246 } |
| 247 |
| 248 int last_load_flags_seen() const { return last_load_flags_seen_; } |
| 249 |
| 190 private: | 250 private: |
| 191 void OnNumberJobsChanged(int outstanding_jobs_delta) { | 251 void OnNumberJobsChanged(int outstanding_jobs_delta, int load_flags) { |
| 252 DCHECK_NE(0, outstanding_jobs_delta); |
| 192 if (outstanding_jobs_delta > 0) | 253 if (outstanding_jobs_delta > 0) |
| 193 jobs_requested_ += outstanding_jobs_delta; | 254 jobs_requested_ += outstanding_jobs_delta; |
| 255 else |
| 256 last_load_flags_seen_ = load_flags; |
| 194 jobs_outstanding_ += outstanding_jobs_delta; | 257 jobs_outstanding_ += outstanding_jobs_delta; |
| 195 if (jobs_outstanding_ == 0 && run_loop_) | 258 if (jobs_outstanding_ == 0 && run_loop_) |
| 196 run_loop_->Quit(); | 259 run_loop_->Quit(); |
| 197 } | 260 } |
| 198 | 261 |
| 199 int jobs_requested_; | 262 int jobs_requested_; |
| 200 int jobs_outstanding_; | 263 int jobs_outstanding_; |
| 264 int last_load_flags_seen_; |
| 201 scoped_ptr<base::RunLoop> run_loop_; | 265 scoped_ptr<base::RunLoop> run_loop_; |
| 202 scoped_ptr<TestURLRequestContext> context_; | 266 scoped_ptr<TestURLRequestContext> context_; |
| 203 scoped_ptr<SdchDictionaryFetcher> fetcher_; | 267 scoped_ptr<SdchDictionaryFetcher> fetcher_; |
| 204 std::vector<DictionaryAdditions> dictionary_additions; | 268 std::vector<DictionaryAdditions> dictionary_additions; |
| 269 HttpResponseInfo response_info_to_return_; |
| 270 scoped_ptr<SdchDictionaryFetcher::Data> last_extra_data_; |
| 205 base::WeakPtrFactory<SdchDictionaryFetcherTest> factory_; | 271 base::WeakPtrFactory<SdchDictionaryFetcherTest> factory_; |
| 206 }; | 272 }; |
| 207 | 273 |
| 208 // Schedule a fetch and make sure it happens. | 274 // Schedule a fetch and make sure it happens. |
| 209 TEST_F(SdchDictionaryFetcherTest, Basic) { | 275 TEST_F(SdchDictionaryFetcherTest, Basic) { |
| 210 GURL dictionary_url(PathToGurl("dictionary")); | 276 GURL dictionary_url(PathToGurl("dictionary")); |
| 211 fetcher()->Schedule(dictionary_url); | 277 Schedule(dictionary_url); |
| 212 WaitForNoJobs(); | 278 WaitForNoJobs(); |
| 213 | 279 |
| 214 EXPECT_EQ(1, jobs_requested()); | 280 EXPECT_EQ(1, jobs_requested()); |
| 281 std::vector<DictionaryAdditions> additions; |
| 282 GetDictionaryAdditions(&additions); |
| 283 ASSERT_EQ(1u, additions.size()); |
| 284 EXPECT_EQ( |
| 285 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url), |
| 286 additions[0].dictionary_text); |
| 287 EXPECT_FALSE(last_load_flags_seen() & LOAD_ONLY_FROM_CACHE); |
| 288 } |
| 289 |
| 290 // Confirm that if we pass extra data, it makes it all the way through. |
| 291 TEST_F(SdchDictionaryFetcherTest, ExtraData) { |
| 292 struct ExtraData : public SdchDictionaryFetcher::Data { |
| 293 ExtraData() : tag(0) {} |
| 294 ~ExtraData() override {} |
| 295 |
| 296 uint32 tag; |
| 297 |
| 298 private: |
| 299 DISALLOW_COPY_AND_ASSIGN(ExtraData); |
| 300 }; |
| 301 |
| 302 const uint32 kExtraDataTag = 0xb0e0e0f0; |
| 303 scoped_ptr<ExtraData> extra_data(new ExtraData); |
| 304 extra_data->tag = kExtraDataTag; |
| 305 |
| 306 EXPECT_FALSE(last_extra_data()); |
| 307 |
| 308 GURL dictionary_url(PathToGurl("dictionary")); |
| 309 fetcher()->Schedule(dictionary_url, extra_data.Pass()); |
| 310 WaitForNoJobs(); |
| 311 |
| 312 EXPECT_EQ(1, jobs_requested()); |
| 215 std::vector<DictionaryAdditions> additions; | 313 std::vector<DictionaryAdditions> additions; |
| 216 GetDictionaryAdditions(&additions); | 314 GetDictionaryAdditions(&additions); |
| 217 ASSERT_EQ(1u, additions.size()); | 315 ASSERT_EQ(1u, additions.size()); |
| 218 EXPECT_EQ( | 316 EXPECT_EQ( |
| 219 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url), | 317 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url), |
| 220 additions[0].dictionary_text); | 318 additions[0].dictionary_text); |
| 319 EXPECT_FALSE(last_load_flags_seen() & LOAD_ONLY_FROM_CACHE); |
| 320 ASSERT_TRUE(last_extra_data()); |
| 321 EXPECT_EQ(kExtraDataTag, |
| 322 (static_cast<const ExtraData*>(last_extra_data()))->tag); |
| 221 } | 323 } |
| 222 | 324 |
| 223 // Multiple fetches of the same URL should result in only one request. | 325 // Multiple fetches of the same URL should result in only one request. |
| 224 TEST_F(SdchDictionaryFetcherTest, Multiple) { | 326 TEST_F(SdchDictionaryFetcherTest, Multiple) { |
| 225 GURL dictionary_url(PathToGurl("dictionary")); | 327 GURL dictionary_url(PathToGurl("dictionary")); |
| 226 fetcher()->Schedule(dictionary_url); | 328 Schedule(dictionary_url); |
| 227 fetcher()->Schedule(dictionary_url); | 329 Schedule(dictionary_url); |
| 228 fetcher()->Schedule(dictionary_url); | 330 Schedule(dictionary_url); |
| 229 WaitForNoJobs(); | 331 WaitForNoJobs(); |
| 230 | 332 |
| 231 EXPECT_EQ(1, jobs_requested()); | 333 EXPECT_EQ(1, jobs_requested()); |
| 232 std::vector<DictionaryAdditions> additions; | 334 std::vector<DictionaryAdditions> additions; |
| 233 GetDictionaryAdditions(&additions); | 335 GetDictionaryAdditions(&additions); |
| 234 ASSERT_EQ(1u, additions.size()); | 336 ASSERT_EQ(1u, additions.size()); |
| 235 EXPECT_EQ( | 337 EXPECT_EQ( |
| 236 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url), | 338 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url), |
| 237 additions[0].dictionary_text); | 339 additions[0].dictionary_text); |
| 238 } | 340 } |
| 239 | 341 |
| 240 // A cancel should result in no actual requests being generated. | 342 // A cancel should result in no actual requests being generated. |
| 241 TEST_F(SdchDictionaryFetcherTest, Cancel) { | 343 TEST_F(SdchDictionaryFetcherTest, Cancel) { |
| 242 GURL dictionary_url_1(PathToGurl("dictionary_1")); | 344 GURL dictionary_url_1(PathToGurl("dictionary_1")); |
| 243 GURL dictionary_url_2(PathToGurl("dictionary_2")); | 345 GURL dictionary_url_2(PathToGurl("dictionary_2")); |
| 244 GURL dictionary_url_3(PathToGurl("dictionary_3")); | 346 GURL dictionary_url_3(PathToGurl("dictionary_3")); |
| 245 | 347 |
| 246 fetcher()->Schedule(dictionary_url_1); | 348 Schedule(dictionary_url_1); |
| 247 fetcher()->Schedule(dictionary_url_2); | 349 Schedule(dictionary_url_2); |
| 248 fetcher()->Schedule(dictionary_url_3); | 350 Schedule(dictionary_url_3); |
| 249 fetcher()->Cancel(); | 351 fetcher()->Cancel(); |
| 250 WaitForNoJobs(); | 352 WaitForNoJobs(); |
| 251 | 353 |
| 252 // Synchronous execution may have resulted in a single job being scheduled. | 354 // Synchronous execution may have resulted in a single job being scheduled. |
| 253 EXPECT_GE(1, jobs_requested()); | 355 EXPECT_GE(1, jobs_requested()); |
| 254 } | 356 } |
| 255 | 357 |
| 256 // Attempt to confuse the fetcher loop processing by scheduling a | 358 // Attempt to confuse the fetcher loop processing by scheduling a |
| 257 // dictionary addition while another fetch is in process. | 359 // dictionary addition while another fetch is in process. |
| 258 TEST_F(SdchDictionaryFetcherTest, LoopRace) { | 360 TEST_F(SdchDictionaryFetcherTest, LoopRace) { |
| 259 GURL dictionary0_url(PathToGurl("dictionary0")); | 361 GURL dictionary0_url(PathToGurl("dictionary0")); |
| 260 GURL dictionary1_url(PathToGurl("dictionary1")); | 362 GURL dictionary1_url(PathToGurl("dictionary1")); |
| 261 fetcher()->Schedule(dictionary0_url); | 363 Schedule(dictionary0_url); |
| 262 fetcher()->Schedule(dictionary1_url); | 364 Schedule(dictionary1_url); |
| 263 WaitForNoJobs(); | 365 WaitForNoJobs(); |
| 264 | 366 |
| 265 ASSERT_EQ(2, jobs_requested()); | 367 ASSERT_EQ(2, jobs_requested()); |
| 266 std::vector<DictionaryAdditions> additions; | 368 std::vector<DictionaryAdditions> additions; |
| 267 GetDictionaryAdditions(&additions); | 369 GetDictionaryAdditions(&additions); |
| 268 ASSERT_EQ(2u, additions.size()); | 370 ASSERT_EQ(2u, additions.size()); |
| 269 EXPECT_EQ( | 371 EXPECT_EQ( |
| 270 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary0_url), | 372 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary0_url), |
| 271 additions[0].dictionary_text); | 373 additions[0].dictionary_text); |
| 272 EXPECT_EQ( | 374 EXPECT_EQ( |
| 273 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary1_url), | 375 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary1_url), |
| 274 additions[1].dictionary_text); | 376 additions[1].dictionary_text); |
| 275 } | 377 } |
| 276 | 378 |
| 379 TEST_F(SdchDictionaryFetcherTest, ScheduleReloadLoadFlags) { |
| 380 GURL dictionary_url(PathToGurl("dictionary")); |
| 381 fetcher()->ScheduleReload(dictionary_url, |
| 382 scoped_ptr<net::SdchDictionaryFetcher::Data>()); |
| 383 |
| 384 WaitForNoJobs(); |
| 385 EXPECT_EQ(1, jobs_requested()); |
| 386 std::vector<DictionaryAdditions> additions; |
| 387 GetDictionaryAdditions(&additions); |
| 388 ASSERT_EQ(1u, additions.size()); |
| 389 EXPECT_EQ( |
| 390 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url), |
| 391 additions[0].dictionary_text); |
| 392 EXPECT_TRUE(last_load_flags_seen() & LOAD_ONLY_FROM_CACHE); |
| 393 } |
| 394 |
| 395 TEST_F(SdchDictionaryFetcherTest, ScheduleReloadFresh) { |
| 396 response_info_to_return()->headers = new HttpResponseHeaders(""); |
| 397 response_info_to_return()->headers->AddHeader("Cache-Control: max-age=1000"); |
| 398 |
| 399 GURL dictionary_url(PathToGurl("dictionary")); |
| 400 fetcher()->ScheduleReload(dictionary_url, |
| 401 scoped_ptr<net::SdchDictionaryFetcher::Data>()); |
| 402 |
| 403 WaitForNoJobs(); |
| 404 EXPECT_EQ(1, jobs_requested()); |
| 405 std::vector<DictionaryAdditions> additions; |
| 406 GetDictionaryAdditions(&additions); |
| 407 ASSERT_EQ(1u, additions.size()); |
| 408 EXPECT_EQ( |
| 409 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url), |
| 410 additions[0].dictionary_text); |
| 411 EXPECT_TRUE(last_load_flags_seen() & LOAD_ONLY_FROM_CACHE); |
| 412 } |
| 413 |
| 414 TEST_F(SdchDictionaryFetcherTest, ScheduleReloadStale) { |
| 415 response_info_to_return()->headers = new HttpResponseHeaders(""); |
| 416 response_info_to_return()->headers->AddHeader("Cache-Control: no-cache"); |
| 417 |
| 418 GURL dictionary_url(PathToGurl("dictionary")); |
| 419 fetcher()->ScheduleReload(dictionary_url, |
| 420 scoped_ptr<net::SdchDictionaryFetcher::Data>()); |
| 421 |
| 422 WaitForNoJobs(); |
| 423 EXPECT_EQ(1, jobs_requested()); |
| 424 std::vector<DictionaryAdditions> additions; |
| 425 GetDictionaryAdditions(&additions); |
| 426 ASSERT_EQ(0u, additions.size()); |
| 427 EXPECT_TRUE(last_load_flags_seen() & LOAD_ONLY_FROM_CACHE); |
| 428 } |
| 429 |
| 277 } // namespace | 430 } // namespace |
| 278 | 431 |
| 279 } // namespace net | 432 } // namespace net |
| OLD | NEW |