| 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/base/sdch_dictionary_fetcher.h" | 5 #include "net/base/sdch_dictionary_fetcher.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 | 24 |
| 25 class URLRequestSpecifiedResponseJob : public URLRequestSimpleJob { | 25 class URLRequestSpecifiedResponseJob : public URLRequestSimpleJob { |
| 26 public: | 26 public: |
| 27 URLRequestSpecifiedResponseJob(URLRequest* request, | 27 URLRequestSpecifiedResponseJob(URLRequest* request, |
| 28 NetworkDelegate* network_delegate) | 28 NetworkDelegate* network_delegate) |
| 29 : URLRequestSimpleJob(request, network_delegate) {} | 29 : URLRequestSimpleJob(request, network_delegate) {} |
| 30 | 30 |
| 31 static void AddUrlHandler() { | 31 static void AddUrlHandler() { |
| 32 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | 32 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
| 33 jobs_requested_ = 0; | 33 jobs_requested_ = 0; |
| 34 filter->AddHostnameHandler("http", kTestDomain, | 34 filter->AddHostnameHandler( |
| 35 &URLRequestSpecifiedResponseJob::Factory); | 35 "http", kTestDomain, &URLRequestSpecifiedResponseJob::Factory); |
| 36 } | 36 } |
| 37 | 37 |
| 38 static void RemoveUrlHandler() { | 38 static void RemoveUrlHandler() { |
| 39 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | 39 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
| 40 filter->RemoveHostnameHandler("http", kTestDomain); | 40 filter->RemoveHostnameHandler("http", kTestDomain); |
| 41 jobs_requested_ = 0; | 41 jobs_requested_ = 0; |
| 42 } | 42 } |
| 43 | 43 |
| 44 static URLRequestJob* Factory( | 44 static URLRequestJob* Factory(URLRequest* request, |
| 45 URLRequest* request, | 45 net::NetworkDelegate* network_delegate, |
| 46 net::NetworkDelegate* network_delegate, | 46 const std::string& scheme) { |
| 47 const std::string& scheme) { | |
| 48 ++jobs_requested_; | 47 ++jobs_requested_; |
| 49 return new URLRequestSpecifiedResponseJob(request, network_delegate); | 48 return new URLRequestSpecifiedResponseJob(request, network_delegate); |
| 50 } | 49 } |
| 51 | 50 |
| 52 static std::string ExpectedResponseForURL(const GURL& url) { | 51 static std::string ExpectedResponseForURL(const GURL& url) { |
| 53 return base::StringPrintf("Response for %s\n%s\nEnd Response for %s\n", | 52 return base::StringPrintf("Response for %s\n%s\nEnd Response for %s\n", |
| 54 url.spec().c_str(), kSampleBufferContext, | 53 url.spec().c_str(), |
| 54 kSampleBufferContext, |
| 55 url.spec().c_str()); | 55 url.spec().c_str()); |
| 56 } | 56 } |
| 57 | 57 |
| 58 static int jobs_requested() { return jobs_requested_; } | 58 static int jobs_requested() { return jobs_requested_; } |
| 59 | 59 |
| 60 private: | 60 private: |
| 61 virtual ~URLRequestSpecifiedResponseJob() {}; | 61 virtual ~URLRequestSpecifiedResponseJob(){}; |
| 62 virtual int GetData(std::string* mime_type, | 62 virtual int GetData(std::string* mime_type, |
| 63 std::string* charset, | 63 std::string* charset, |
| 64 std::string* data, | 64 std::string* data, |
| 65 const CompletionCallback& callback) const override { | 65 const CompletionCallback& callback) const override { |
| 66 GURL url(request_->url()); | 66 GURL url(request_->url()); |
| 67 *data = ExpectedResponseForURL(url); | 67 *data = ExpectedResponseForURL(url); |
| 68 return OK; | 68 return OK; |
| 69 } | 69 } |
| 70 | 70 |
| 71 static int jobs_requested_; | 71 static int jobs_requested_; |
| 72 }; | 72 }; |
| 73 | 73 |
| 74 int URLRequestSpecifiedResponseJob::jobs_requested_(0); | 74 int URLRequestSpecifiedResponseJob::jobs_requested_(0); |
| 75 | 75 |
| 76 class SdchTestDelegate : public SdchFetcher::Delegate { | 76 class SdchTestDelegate : public SdchFetcher::Delegate { |
| 77 public: | 77 public: |
| 78 struct DictionaryAdditions { | 78 struct DictionaryAdditions { |
| 79 DictionaryAdditions(const std::string& dictionary_text, | 79 DictionaryAdditions(const std::string& dictionary_text, |
| 80 const GURL& dictionary_url) | 80 const GURL& dictionary_url) |
| 81 : dictionary_text(dictionary_text), | 81 : dictionary_text(dictionary_text), dictionary_url(dictionary_url) {} |
| 82 dictionary_url(dictionary_url) {} | |
| 83 | |
| 84 | 82 |
| 85 std::string dictionary_text; | 83 std::string dictionary_text; |
| 86 GURL dictionary_url; | 84 GURL dictionary_url; |
| 87 }; | 85 }; |
| 88 | 86 |
| 89 virtual void AddSdchDictionary(const std::string& dictionary_text, | 87 virtual void AddSdchDictionary(const std::string& dictionary_text, |
| 90 const GURL& dictionary_url) override { | 88 const GURL& dictionary_url) override { |
| 91 dictionary_additions.push_back( | 89 dictionary_additions.push_back( |
| 92 DictionaryAdditions(dictionary_text, dictionary_url)); | 90 DictionaryAdditions(dictionary_text, dictionary_url)); |
| 93 } | 91 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 104 class SdchDictionaryFetcherTest : public ::testing::Test { | 102 class SdchDictionaryFetcherTest : public ::testing::Test { |
| 105 public: | 103 public: |
| 106 SdchDictionaryFetcherTest() {} | 104 SdchDictionaryFetcherTest() {} |
| 107 | 105 |
| 108 virtual void SetUp() override { | 106 virtual void SetUp() override { |
| 109 DCHECK(!fetcher_.get()); | 107 DCHECK(!fetcher_.get()); |
| 110 | 108 |
| 111 URLRequestSpecifiedResponseJob::AddUrlHandler(); | 109 URLRequestSpecifiedResponseJob::AddUrlHandler(); |
| 112 fetcher_delegate_.reset(new SdchTestDelegate); | 110 fetcher_delegate_.reset(new SdchTestDelegate); |
| 113 context_.reset(new TestURLRequestContext); | 111 context_.reset(new TestURLRequestContext); |
| 114 fetcher_.reset(new SdchDictionaryFetcher( | 112 fetcher_.reset( |
| 115 fetcher_delegate_.get(), context_.get())); | 113 new SdchDictionaryFetcher(fetcher_delegate_.get(), context_.get())); |
| 116 } | 114 } |
| 117 | 115 |
| 118 virtual void TearDown() override { | 116 virtual void TearDown() override { |
| 119 URLRequestSpecifiedResponseJob::RemoveUrlHandler(); | 117 URLRequestSpecifiedResponseJob::RemoveUrlHandler(); |
| 120 fetcher_.reset(); | 118 fetcher_.reset(); |
| 121 context_.reset(); | 119 context_.reset(); |
| 122 fetcher_delegate_.reset(); | 120 fetcher_delegate_.reset(); |
| 123 } | 121 } |
| 124 | 122 |
| 125 SdchDictionaryFetcher* fetcher() { return fetcher_.get(); } | 123 SdchDictionaryFetcher* fetcher() { return fetcher_.get(); } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 147 // Schedule a fetch and make sure it happens. | 145 // Schedule a fetch and make sure it happens. |
| 148 TEST_F(SdchDictionaryFetcherTest, Basic) { | 146 TEST_F(SdchDictionaryFetcherTest, Basic) { |
| 149 GURL dictionary_url(PathToGurl("dictionary")); | 147 GURL dictionary_url(PathToGurl("dictionary")); |
| 150 fetcher()->Schedule(dictionary_url); | 148 fetcher()->Schedule(dictionary_url); |
| 151 | 149 |
| 152 base::RunLoop().RunUntilIdle(); | 150 base::RunLoop().RunUntilIdle(); |
| 153 EXPECT_EQ(1, JobsRequested()); | 151 EXPECT_EQ(1, JobsRequested()); |
| 154 std::vector<SdchTestDelegate::DictionaryAdditions> additions; | 152 std::vector<SdchTestDelegate::DictionaryAdditions> additions; |
| 155 manager()->GetDictionaryAdditions(&additions); | 153 manager()->GetDictionaryAdditions(&additions); |
| 156 ASSERT_EQ(1u, additions.size()); | 154 ASSERT_EQ(1u, additions.size()); |
| 157 EXPECT_EQ(URLRequestSpecifiedResponseJob::ExpectedResponseForURL( | 155 EXPECT_EQ( |
| 158 dictionary_url), additions[0].dictionary_text); | 156 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url), |
| 157 additions[0].dictionary_text); |
| 159 } | 158 } |
| 160 | 159 |
| 161 // Multiple fetches of the same URL should result in only one request. | 160 // Multiple fetches of the same URL should result in only one request. |
| 162 TEST_F(SdchDictionaryFetcherTest, Multiple) { | 161 TEST_F(SdchDictionaryFetcherTest, Multiple) { |
| 163 GURL dictionary_url(PathToGurl("dictionary")); | 162 GURL dictionary_url(PathToGurl("dictionary")); |
| 164 fetcher()->Schedule(dictionary_url); | 163 fetcher()->Schedule(dictionary_url); |
| 165 fetcher()->Schedule(dictionary_url); | 164 fetcher()->Schedule(dictionary_url); |
| 166 fetcher()->Schedule(dictionary_url); | 165 fetcher()->Schedule(dictionary_url); |
| 167 base::RunLoop().RunUntilIdle(); | 166 base::RunLoop().RunUntilIdle(); |
| 168 | 167 |
| 169 EXPECT_EQ(1, JobsRequested()); | 168 EXPECT_EQ(1, JobsRequested()); |
| 170 std::vector<SdchTestDelegate::DictionaryAdditions> additions; | 169 std::vector<SdchTestDelegate::DictionaryAdditions> additions; |
| 171 manager()->GetDictionaryAdditions(&additions); | 170 manager()->GetDictionaryAdditions(&additions); |
| 172 ASSERT_EQ(1u, additions.size()); | 171 ASSERT_EQ(1u, additions.size()); |
| 173 EXPECT_EQ(URLRequestSpecifiedResponseJob::ExpectedResponseForURL( | 172 EXPECT_EQ( |
| 174 dictionary_url), additions[0].dictionary_text); | 173 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url), |
| 174 additions[0].dictionary_text); |
| 175 } | 175 } |
| 176 | 176 |
| 177 // A cancel should result in no actual requests being generated. | 177 // A cancel should result in no actual requests being generated. |
| 178 TEST_F(SdchDictionaryFetcherTest, Cancel) { | 178 TEST_F(SdchDictionaryFetcherTest, Cancel) { |
| 179 GURL dictionary_url_1(PathToGurl("dictionary_1")); | 179 GURL dictionary_url_1(PathToGurl("dictionary_1")); |
| 180 GURL dictionary_url_2(PathToGurl("dictionary_2")); | 180 GURL dictionary_url_2(PathToGurl("dictionary_2")); |
| 181 GURL dictionary_url_3(PathToGurl("dictionary_3")); | 181 GURL dictionary_url_3(PathToGurl("dictionary_3")); |
| 182 | 182 |
| 183 fetcher()->Schedule(dictionary_url_1); | 183 fetcher()->Schedule(dictionary_url_1); |
| 184 fetcher()->Schedule(dictionary_url_2); | 184 fetcher()->Schedule(dictionary_url_2); |
| 185 fetcher()->Schedule(dictionary_url_3); | 185 fetcher()->Schedule(dictionary_url_3); |
| 186 fetcher()->Cancel(); | 186 fetcher()->Cancel(); |
| 187 base::RunLoop().RunUntilIdle(); | 187 base::RunLoop().RunUntilIdle(); |
| 188 | 188 |
| 189 // Synchronous execution may have resulted in a single job being scheduled. | 189 // Synchronous execution may have resulted in a single job being scheduled. |
| 190 EXPECT_GE(1, JobsRequested()); | 190 EXPECT_GE(1, JobsRequested()); |
| 191 } | 191 } |
| 192 | |
| 193 } | 192 } |
| OLD | NEW |