| 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/url_request/sdch_dictionary_fetcher.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" |
| 9 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 10 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 11 #include "base/thread_task_runner_handle.h" | 12 #include "base/thread_task_runner_handle.h" |
| 12 #include "net/base/sdch_manager.h" | 13 #include "net/base/sdch_manager.h" |
| 13 #include "net/url_request/url_request_data_job.h" | 14 #include "net/url_request/url_request_data_job.h" |
| 14 #include "net/url_request/url_request_filter.h" | 15 #include "net/url_request/url_request_filter.h" |
| 15 #include "net/url_request/url_request_interceptor.h" | 16 #include "net/url_request/url_request_interceptor.h" |
| 16 #include "net/url_request/url_request_test_util.h" | 17 #include "net/url_request/url_request_test_util.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "url/gurl.h" | 19 #include "url/gurl.h" |
| 19 | 20 |
| 20 namespace net { | 21 namespace net { |
| 21 | 22 |
| 22 static const char* kSampleBufferContext = "This is a sample buffer."; | 23 static const char* kSampleBufferContext = "This is a sample buffer."; |
| 23 static const char* kTestDomain = "top.domain.test"; | 24 static const char* kTestDomain = "top.domain.test"; |
| 24 | 25 |
| 25 class URLRequestSpecifiedResponseJob : public URLRequestSimpleJob { | 26 class URLRequestSpecifiedResponseJob : public URLRequestSimpleJob { |
| 26 public: | 27 public: |
| 27 URLRequestSpecifiedResponseJob(URLRequest* request, | 28 URLRequestSpecifiedResponseJob(URLRequest* request, |
| 28 NetworkDelegate* network_delegate) | 29 NetworkDelegate* network_delegate) |
| 29 : URLRequestSimpleJob(request, network_delegate) {} | 30 : URLRequestSimpleJob(request, network_delegate) {} |
| 30 | 31 |
| 31 static void AddUrlHandler() { | 32 static void AddUrlHandler() { |
| 32 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | 33 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
| 33 jobs_requested_ = 0; | 34 jobs_requested_ = 0; |
| 34 filter->AddHostnameHandler("http", kTestDomain, | 35 filter->AddHostnameHandler( |
| 35 &URLRequestSpecifiedResponseJob::Factory); | 36 "http", kTestDomain, &URLRequestSpecifiedResponseJob::Factory); |
| 36 } | 37 } |
| 37 | 38 |
| 38 static void RemoveUrlHandler() { | 39 static void RemoveUrlHandler() { |
| 39 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | 40 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
| 40 filter->RemoveHostnameHandler("http", kTestDomain); | 41 filter->RemoveHostnameHandler("http", kTestDomain); |
| 41 jobs_requested_ = 0; | 42 jobs_requested_ = 0; |
| 42 } | 43 } |
| 43 | 44 |
| 44 static URLRequestJob* Factory( | 45 static URLRequestJob* Factory(URLRequest* request, |
| 45 URLRequest* request, | 46 net::NetworkDelegate* network_delegate, |
| 46 net::NetworkDelegate* network_delegate, | 47 const std::string& scheme) { |
| 47 const std::string& scheme) { | |
| 48 ++jobs_requested_; | 48 ++jobs_requested_; |
| 49 return new URLRequestSpecifiedResponseJob(request, network_delegate); | 49 return new URLRequestSpecifiedResponseJob(request, network_delegate); |
| 50 } | 50 } |
| 51 | 51 |
| 52 static std::string ExpectedResponseForURL(const GURL& url) { | 52 static std::string ExpectedResponseForURL(const GURL& url) { |
| 53 return base::StringPrintf("Response for %s\n%s\nEnd Response for %s\n", | 53 return base::StringPrintf("Response for %s\n%s\nEnd Response for %s\n", |
| 54 url.spec().c_str(), kSampleBufferContext, | 54 url.spec().c_str(), |
| 55 kSampleBufferContext, |
| 55 url.spec().c_str()); | 56 url.spec().c_str()); |
| 56 } | 57 } |
| 57 | 58 |
| 58 static int jobs_requested() { return jobs_requested_; } | 59 static int jobs_requested() { return jobs_requested_; } |
| 59 | 60 |
| 60 private: | 61 private: |
| 61 ~URLRequestSpecifiedResponseJob() override{}; | 62 ~URLRequestSpecifiedResponseJob() override{}; |
| 62 int GetData(std::string* mime_type, | 63 int GetData(std::string* mime_type, |
| 63 std::string* charset, | 64 std::string* charset, |
| 64 std::string* data, | 65 std::string* data, |
| 65 const CompletionCallback& callback) const override { | 66 const CompletionCallback& callback) const override { |
| 66 GURL url(request_->url()); | 67 GURL url(request_->url()); |
| 67 *data = ExpectedResponseForURL(url); | 68 *data = ExpectedResponseForURL(url); |
| 68 return OK; | 69 return OK; |
| 69 } | 70 } |
| 70 | 71 |
| 71 static int jobs_requested_; | 72 static int jobs_requested_; |
| 72 }; | 73 }; |
| 73 | 74 |
| 74 int URLRequestSpecifiedResponseJob::jobs_requested_(0); | 75 int URLRequestSpecifiedResponseJob::jobs_requested_(0); |
| 75 | 76 |
| 76 class SdchTestDelegate : public SdchFetcher::Delegate { | 77 class SdchDictionaryFetcherTest : public ::testing::Test { |
| 77 public: | 78 public: |
| 78 struct DictionaryAdditions { | 79 struct DictionaryAdditions { |
| 79 DictionaryAdditions(const std::string& dictionary_text, | 80 DictionaryAdditions(const std::string& dictionary_text, |
| 80 const GURL& dictionary_url) | 81 const GURL& dictionary_url) |
| 81 : dictionary_text(dictionary_text), | 82 : dictionary_text(dictionary_text), dictionary_url(dictionary_url) {} |
| 82 dictionary_url(dictionary_url) {} | |
| 83 | |
| 84 | 83 |
| 85 std::string dictionary_text; | 84 std::string dictionary_text; |
| 86 GURL dictionary_url; | 85 GURL dictionary_url; |
| 87 }; | 86 }; |
| 88 | 87 |
| 89 void AddSdchDictionary(const std::string& dictionary_text, | 88 SdchDictionaryFetcherTest() { |
| 90 const GURL& dictionary_url) override { | 89 URLRequestSpecifiedResponseJob::AddUrlHandler(); |
| 90 context_.reset(new TestURLRequestContext); |
| 91 fetcher_.reset(new SdchDictionaryFetcher( |
| 92 context_.get(), |
| 93 base::Bind(&SdchDictionaryFetcherTest::OnDictionaryFetched, |
| 94 base::Unretained(this)))); |
| 95 } |
| 96 |
| 97 ~SdchDictionaryFetcherTest() { |
| 98 URLRequestSpecifiedResponseJob::RemoveUrlHandler(); |
| 99 } |
| 100 |
| 101 void OnDictionaryFetched(const std::string& dictionary_text, |
| 102 const GURL& dictionary_url) { |
| 91 dictionary_additions.push_back( | 103 dictionary_additions.push_back( |
| 92 DictionaryAdditions(dictionary_text, dictionary_url)); | 104 DictionaryAdditions(dictionary_text, dictionary_url)); |
| 93 } | 105 } |
| 94 | 106 |
| 95 void GetDictionaryAdditions(std::vector<DictionaryAdditions>* out) { | 107 void GetDictionaryAdditions(std::vector<DictionaryAdditions>* out) { |
| 96 out->swap(dictionary_additions); | 108 out->swap(dictionary_additions); |
| 97 dictionary_additions.clear(); | 109 dictionary_additions.clear(); |
| 98 } | 110 } |
| 99 | 111 |
| 100 private: | |
| 101 std::vector<DictionaryAdditions> dictionary_additions; | |
| 102 }; | |
| 103 | |
| 104 class SdchDictionaryFetcherTest : public ::testing::Test { | |
| 105 public: | |
| 106 SdchDictionaryFetcherTest() {} | |
| 107 | |
| 108 void SetUp() override { | |
| 109 DCHECK(!fetcher_.get()); | |
| 110 | |
| 111 URLRequestSpecifiedResponseJob::AddUrlHandler(); | |
| 112 fetcher_delegate_.reset(new SdchTestDelegate); | |
| 113 context_.reset(new TestURLRequestContext); | |
| 114 fetcher_.reset(new SdchDictionaryFetcher( | |
| 115 fetcher_delegate_.get(), context_.get())); | |
| 116 } | |
| 117 | |
| 118 void TearDown() override { | |
| 119 URLRequestSpecifiedResponseJob::RemoveUrlHandler(); | |
| 120 fetcher_.reset(); | |
| 121 context_.reset(); | |
| 122 fetcher_delegate_.reset(); | |
| 123 } | |
| 124 | |
| 125 SdchDictionaryFetcher* fetcher() { return fetcher_.get(); } | 112 SdchDictionaryFetcher* fetcher() { return fetcher_.get(); } |
| 126 SdchTestDelegate* manager() { return fetcher_delegate_.get(); } | |
| 127 | 113 |
| 128 // May not be called outside the SetUp()/TearDown() interval. | 114 // May not be called outside the SetUp()/TearDown() interval. |
| 129 int JobsRequested() { | 115 int JobsRequested() { |
| 130 return URLRequestSpecifiedResponseJob::jobs_requested(); | 116 return URLRequestSpecifiedResponseJob::jobs_requested(); |
| 131 } | 117 } |
| 132 | 118 |
| 133 GURL PathToGurl(const char* path) { | 119 GURL PathToGurl(const char* path) { |
| 134 std::string gurl_string("http://"); | 120 std::string gurl_string("http://"); |
| 135 gurl_string += kTestDomain; | 121 gurl_string += kTestDomain; |
| 136 gurl_string += "/"; | 122 gurl_string += "/"; |
| 137 gurl_string += path; | 123 gurl_string += path; |
| 138 return GURL(gurl_string); | 124 return GURL(gurl_string); |
| 139 } | 125 } |
| 140 | 126 |
| 141 private: | 127 private: |
| 142 scoped_ptr<SdchTestDelegate> fetcher_delegate_; | |
| 143 scoped_ptr<TestURLRequestContext> context_; | 128 scoped_ptr<TestURLRequestContext> context_; |
| 144 scoped_ptr<SdchDictionaryFetcher> fetcher_; | 129 scoped_ptr<SdchDictionaryFetcher> fetcher_; |
| 130 std::vector<DictionaryAdditions> dictionary_additions; |
| 145 }; | 131 }; |
| 146 | 132 |
| 147 // Schedule a fetch and make sure it happens. | 133 // Schedule a fetch and make sure it happens. |
| 148 TEST_F(SdchDictionaryFetcherTest, Basic) { | 134 TEST_F(SdchDictionaryFetcherTest, Basic) { |
| 149 GURL dictionary_url(PathToGurl("dictionary")); | 135 GURL dictionary_url(PathToGurl("dictionary")); |
| 150 fetcher()->Schedule(dictionary_url); | 136 fetcher()->Schedule(dictionary_url); |
| 151 | 137 |
| 152 base::RunLoop().RunUntilIdle(); | 138 base::RunLoop().RunUntilIdle(); |
| 153 EXPECT_EQ(1, JobsRequested()); | 139 EXPECT_EQ(1, JobsRequested()); |
| 154 std::vector<SdchTestDelegate::DictionaryAdditions> additions; | 140 std::vector<DictionaryAdditions> additions; |
| 155 manager()->GetDictionaryAdditions(&additions); | 141 GetDictionaryAdditions(&additions); |
| 156 ASSERT_EQ(1u, additions.size()); | 142 ASSERT_EQ(1u, additions.size()); |
| 157 EXPECT_EQ(URLRequestSpecifiedResponseJob::ExpectedResponseForURL( | 143 EXPECT_EQ( |
| 158 dictionary_url), additions[0].dictionary_text); | 144 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url), |
| 145 additions[0].dictionary_text); |
| 159 } | 146 } |
| 160 | 147 |
| 161 // Multiple fetches of the same URL should result in only one request. | 148 // Multiple fetches of the same URL should result in only one request. |
| 162 TEST_F(SdchDictionaryFetcherTest, Multiple) { | 149 TEST_F(SdchDictionaryFetcherTest, Multiple) { |
| 163 GURL dictionary_url(PathToGurl("dictionary")); | 150 GURL dictionary_url(PathToGurl("dictionary")); |
| 164 fetcher()->Schedule(dictionary_url); | 151 fetcher()->Schedule(dictionary_url); |
| 165 fetcher()->Schedule(dictionary_url); | 152 fetcher()->Schedule(dictionary_url); |
| 166 fetcher()->Schedule(dictionary_url); | 153 fetcher()->Schedule(dictionary_url); |
| 167 base::RunLoop().RunUntilIdle(); | 154 base::RunLoop().RunUntilIdle(); |
| 168 | 155 |
| 169 EXPECT_EQ(1, JobsRequested()); | 156 EXPECT_EQ(1, JobsRequested()); |
| 170 std::vector<SdchTestDelegate::DictionaryAdditions> additions; | 157 std::vector<DictionaryAdditions> additions; |
| 171 manager()->GetDictionaryAdditions(&additions); | 158 GetDictionaryAdditions(&additions); |
| 172 ASSERT_EQ(1u, additions.size()); | 159 ASSERT_EQ(1u, additions.size()); |
| 173 EXPECT_EQ(URLRequestSpecifiedResponseJob::ExpectedResponseForURL( | 160 EXPECT_EQ( |
| 174 dictionary_url), additions[0].dictionary_text); | 161 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url), |
| 162 additions[0].dictionary_text); |
| 175 } | 163 } |
| 176 | 164 |
| 177 // A cancel should result in no actual requests being generated. | 165 // A cancel should result in no actual requests being generated. |
| 178 TEST_F(SdchDictionaryFetcherTest, Cancel) { | 166 TEST_F(SdchDictionaryFetcherTest, Cancel) { |
| 179 GURL dictionary_url_1(PathToGurl("dictionary_1")); | 167 GURL dictionary_url_1(PathToGurl("dictionary_1")); |
| 180 GURL dictionary_url_2(PathToGurl("dictionary_2")); | 168 GURL dictionary_url_2(PathToGurl("dictionary_2")); |
| 181 GURL dictionary_url_3(PathToGurl("dictionary_3")); | 169 GURL dictionary_url_3(PathToGurl("dictionary_3")); |
| 182 | 170 |
| 183 fetcher()->Schedule(dictionary_url_1); | 171 fetcher()->Schedule(dictionary_url_1); |
| 184 fetcher()->Schedule(dictionary_url_2); | 172 fetcher()->Schedule(dictionary_url_2); |
| 185 fetcher()->Schedule(dictionary_url_3); | 173 fetcher()->Schedule(dictionary_url_3); |
| 186 fetcher()->Cancel(); | 174 fetcher()->Cancel(); |
| 187 base::RunLoop().RunUntilIdle(); | 175 base::RunLoop().RunUntilIdle(); |
| 188 | 176 |
| 189 // Synchronous execution may have resulted in a single job being scheduled. | 177 // Synchronous execution may have resulted in a single job being scheduled. |
| 190 EXPECT_GE(1, JobsRequested()); | 178 EXPECT_GE(1, JobsRequested()); |
| 191 } | 179 } |
| 192 | |
| 193 } | 180 } |
| OLD | NEW |