Chromium Code Reviews| Index: net/base/sdch_dictionary_fetcher_unittest.cc |
| diff --git a/net/base/sdch_dictionary_fetcher_unittest.cc b/net/base/sdch_dictionary_fetcher_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1a2ad95fd8f1daf650cba08c430563ee16e78bbf |
| --- /dev/null |
| +++ b/net/base/sdch_dictionary_fetcher_unittest.cc |
| @@ -0,0 +1,185 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "net/base/sdch_dictionary_fetcher.h" |
| + |
| +#include <string> |
| + |
| +#include "base/run_loop.h" |
| +#include "base/strings/stringprintf.h" |
| +#include "base/thread_task_runner_handle.h" |
| +#include "net/base/sdch_manager.h" |
| +#include "net/url_request/url_request_data_job.h" |
| +#include "net/url_request/url_request_filter.h" |
| +#include "net/url_request/url_request_interceptor.h" |
| +#include "net/url_request/url_request_test_util.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "url/gurl.h" |
| + |
| +namespace net { |
| + |
| +static const char* kSampleBufferContext = "This is a sample buffer."; |
| + |
| +class URLRequestSpecifiedResponseJob : public URLRequestSimpleJob { |
| + public: |
| + URLRequestSpecifiedResponseJob(URLRequest* request, |
| + NetworkDelegate* network_delegate) |
| + : URLRequestSimpleJob(request, network_delegate) {} |
| + |
| + static void AddUrlHandler() { |
| + net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
| + jobs_requested_ = 0; |
| + filter->AddHostnameHandler("http", "top.level.domain", |
|
Ryan Sleevi
2014/09/04 01:14:21
Use an RFC 2606 reserved TLD - .test is likely bes
Randy Smith (Not in Mondays)
2014/09/04 18:52:57
Done, along with some refactoring out of common st
|
| + &URLRequestSpecifiedResponseJob::Factory); |
| + } |
| + |
| + static void RemoveUrlHandler() { |
| + net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
| + filter->RemoveHostnameHandler("http", "top.level.domain"); |
| + jobs_requested_ = 0; |
| + } |
| + |
| + static URLRequestJob* Factory( |
| + URLRequest* request, |
| + net::NetworkDelegate* network_delegate, |
| + const std::string& scheme) { |
| + ++jobs_requested_; |
| + return new URLRequestSpecifiedResponseJob(request, network_delegate); |
| + } |
| + |
| + static std::string ExpectedResponseForURL(const GURL& url) { |
| + return base::StringPrintf("Response for %s\n%s\nEnd Response for %s\n", |
| + url.spec().c_str(), kSampleBufferContext, |
| + url.spec().c_str()); |
| + } |
| + |
| + static int jobs_requested() { return jobs_requested_; } |
| + |
| + private: |
| + virtual ~URLRequestSpecifiedResponseJob() {}; |
| + virtual int GetData(std::string* mime_type, |
| + std::string* charset, |
| + std::string* data, |
| + const CompletionCallback& callback) const OVERRIDE { |
| + GURL url(request_->url()); |
| + *data = ExpectedResponseForURL(url); |
| + return OK; |
| + } |
| + |
| + static int jobs_requested_; |
| +}; |
| + |
| +int URLRequestSpecifiedResponseJob::jobs_requested_(0); |
| + |
| +class SdchTestManager : public SdchManager { |
| + public: |
| + struct DictionaryAdditions { |
| + DictionaryAdditions(const std::string& dictionary_text, |
| + const GURL& dictionary_url) |
| + : dictionary_text(dictionary_text), |
| + dictionary_url(dictionary_url) {} |
| + |
| + |
| + std::string dictionary_text; |
| + GURL dictionary_url; |
| + }; |
| + |
| + virtual bool AddSdchDictionary(const std::string& dictionary_text, |
| + const GURL& dictionary_url) OVERRIDE { |
| + dictionary_additions.push_back( |
| + DictionaryAdditions(dictionary_text, dictionary_url)); |
| + return true; |
| + } |
| + |
| + void GetDictionaryAdditions(std::vector<DictionaryAdditions>* out) { |
| + out->swap(dictionary_additions); |
| + dictionary_additions.clear(); |
| + } |
| + |
| + private: |
| + std::vector<DictionaryAdditions> dictionary_additions; |
| +}; |
| + |
| +static const char* kInterceptionGURL = "http://top.level.domain/dictionary"; |
| + |
| +class SdchDictionaryFetcherTest : public ::testing::Test { |
| + public: |
| + SdchDictionaryFetcherTest() {} |
| + |
| + virtual void SetUp() OVERRIDE { |
| + DCHECK(!fetcher_.get()); |
| + |
| + URLRequestSpecifiedResponseJob::AddUrlHandler(); |
| + manager_.reset(new SdchTestManager); |
| + context_.reset(new TestURLRequestContext); |
| + fetcher_.reset(new SdchDictionaryFetcher(manager_.get(), context_.get())); |
| + } |
| + |
| + virtual void TearDown() OVERRIDE { |
| + URLRequestSpecifiedResponseJob::RemoveUrlHandler(); |
| + fetcher_.reset(); |
| + context_.reset(); |
| + manager_.reset(); |
| + } |
| + |
| + SdchDictionaryFetcher* fetcher() { return fetcher_.get(); } |
| + SdchTestManager* manager() { return manager_.get(); } |
| + |
| + // May not be called outside the SetUp()/TearDown() interval. |
| + int JobsRequested() { |
| + return URLRequestSpecifiedResponseJob::jobs_requested(); |
| + } |
| + |
| + private: |
| + scoped_ptr<SdchTestManager> manager_; |
| + scoped_ptr<TestURLRequestContext> context_; |
| + scoped_ptr<SdchDictionaryFetcher> fetcher_; |
| +}; |
| + |
| +// Schedule a fetch and make sure it happens. |
| +TEST_F(SdchDictionaryFetcherTest, Basic) { |
| + GURL interception_url = GURL(kInterceptionGURL); |
| + fetcher()->Schedule(interception_url); |
| + |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_EQ(1, JobsRequested()); |
| + std::vector<SdchTestManager::DictionaryAdditions> additions; |
| + manager()->GetDictionaryAdditions(&additions); |
| + ASSERT_EQ(1u, additions.size()); |
| + EXPECT_EQ(URLRequestSpecifiedResponseJob::ExpectedResponseForURL( |
| + interception_url), additions[0].dictionary_text); |
| +} |
| + |
| +// Multiple fetches of the same URL should result in only one request. |
| +TEST_F(SdchDictionaryFetcherTest, Multiple) { |
| + GURL interception_url = GURL(kInterceptionGURL); |
| + fetcher()->Schedule(interception_url); |
| + fetcher()->Schedule(interception_url); |
| + fetcher()->Schedule(interception_url); |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + EXPECT_EQ(1, JobsRequested()); |
| + std::vector<SdchTestManager::DictionaryAdditions> additions; |
| + manager()->GetDictionaryAdditions(&additions); |
| + ASSERT_EQ(1u, additions.size()); |
| + EXPECT_EQ(URLRequestSpecifiedResponseJob::ExpectedResponseForURL( |
| + interception_url), additions[0].dictionary_text); |
| +} |
| + |
| +// A cancel should result in no actual requests being generated. |
| +TEST_F(SdchDictionaryFetcherTest, Cancel) { |
| + GURL interception_url_1 = GURL(std::string(kInterceptionGURL) + "_1"); |
| + GURL interception_url_2 = GURL(std::string(kInterceptionGURL) + "_2"); |
| + GURL interception_url_3 = GURL(std::string(kInterceptionGURL) + "_3"); |
| + fetcher()->Schedule(interception_url_1); |
| + fetcher()->Schedule(interception_url_2); |
| + fetcher()->Schedule(interception_url_3); |
| + fetcher()->Cancel(); |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + // Synchronous execution may have resulted in a single job being scheduled. |
| + EXPECT_GE(1, JobsRequested()); |
| +} |
| + |
| +} |