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