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 |
90 void SetUp() override { | |
91 DCHECK(!fetcher_.get()); | |
Ryan Sleevi
2014/11/05 22:21:39
DCHECK in tests is BAD! :)
ASSERT_FALSE(fetcher_.
Randy Smith (Not in Mondays)
2014/11/05 23:50:00
Switching to the ctor makes this moot.
| |
92 | |
93 URLRequestSpecifiedResponseJob::AddUrlHandler(); | |
94 context_.reset(new TestURLRequestContext); | |
95 fetcher_.reset(new SdchDictionaryFetcher( | |
96 context_.get(), | |
97 base::Bind(&SdchDictionaryFetcherTest::OnDictionaryFetched, | |
98 base::Unretained(this)))); | |
Ryan Sleevi
2014/11/05 22:21:39
Is there a reason you need to do this in SetUp/Tea
Randy Smith (Not in Mondays)
2014/11/05 23:50:00
I had thought that a single instance of the test o
Ryan Sleevi
2014/11/06 00:03:45
Common mistake See https://code.google.com/p/googl
| |
99 } | |
100 | |
101 void TearDown() override { | |
102 URLRequestSpecifiedResponseJob::RemoveUrlHandler(); | |
103 fetcher_.reset(); | |
104 context_.reset(); | |
105 } | |
106 | |
107 void OnDictionaryFetched(const std::string& dictionary_text, | |
108 const GURL& dictionary_url) { | |
91 dictionary_additions.push_back( | 109 dictionary_additions.push_back( |
92 DictionaryAdditions(dictionary_text, dictionary_url)); | 110 DictionaryAdditions(dictionary_text, dictionary_url)); |
93 } | 111 } |
94 | 112 |
95 void GetDictionaryAdditions(std::vector<DictionaryAdditions>* out) { | 113 void GetDictionaryAdditions(std::vector<DictionaryAdditions>* out) { |
96 out->swap(dictionary_additions); | 114 out->swap(dictionary_additions); |
97 dictionary_additions.clear(); | 115 dictionary_additions.clear(); |
98 } | 116 } |
99 | 117 |
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(); } | 118 SdchDictionaryFetcher* fetcher() { return fetcher_.get(); } |
126 SdchTestDelegate* manager() { return fetcher_delegate_.get(); } | |
127 | 119 |
128 // May not be called outside the SetUp()/TearDown() interval. | 120 // May not be called outside the SetUp()/TearDown() interval. |
129 int JobsRequested() { | 121 int JobsRequested() { |
130 return URLRequestSpecifiedResponseJob::jobs_requested(); | 122 return URLRequestSpecifiedResponseJob::jobs_requested(); |
131 } | 123 } |
132 | 124 |
133 GURL PathToGurl(const char* path) { | 125 GURL PathToGurl(const char* path) { |
134 std::string gurl_string("http://"); | 126 std::string gurl_string("http://"); |
135 gurl_string += kTestDomain; | 127 gurl_string += kTestDomain; |
136 gurl_string += "/"; | 128 gurl_string += "/"; |
137 gurl_string += path; | 129 gurl_string += path; |
138 return GURL(gurl_string); | 130 return GURL(gurl_string); |
139 } | 131 } |
140 | 132 |
141 private: | 133 private: |
142 scoped_ptr<SdchTestDelegate> fetcher_delegate_; | |
143 scoped_ptr<TestURLRequestContext> context_; | 134 scoped_ptr<TestURLRequestContext> context_; |
144 scoped_ptr<SdchDictionaryFetcher> fetcher_; | 135 scoped_ptr<SdchDictionaryFetcher> fetcher_; |
136 std::vector<DictionaryAdditions> dictionary_additions; | |
145 }; | 137 }; |
146 | 138 |
147 // Schedule a fetch and make sure it happens. | 139 // Schedule a fetch and make sure it happens. |
148 TEST_F(SdchDictionaryFetcherTest, Basic) { | 140 TEST_F(SdchDictionaryFetcherTest, Basic) { |
149 GURL dictionary_url(PathToGurl("dictionary")); | 141 GURL dictionary_url(PathToGurl("dictionary")); |
150 fetcher()->Schedule(dictionary_url); | 142 fetcher()->Schedule(dictionary_url); |
151 | 143 |
152 base::RunLoop().RunUntilIdle(); | 144 base::RunLoop().RunUntilIdle(); |
153 EXPECT_EQ(1, JobsRequested()); | 145 EXPECT_EQ(1, JobsRequested()); |
154 std::vector<SdchTestDelegate::DictionaryAdditions> additions; | 146 std::vector<DictionaryAdditions> additions; |
155 manager()->GetDictionaryAdditions(&additions); | 147 GetDictionaryAdditions(&additions); |
156 ASSERT_EQ(1u, additions.size()); | 148 ASSERT_EQ(1u, additions.size()); |
157 EXPECT_EQ(URLRequestSpecifiedResponseJob::ExpectedResponseForURL( | 149 EXPECT_EQ( |
158 dictionary_url), additions[0].dictionary_text); | 150 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url), |
151 additions[0].dictionary_text); | |
159 } | 152 } |
160 | 153 |
161 // Multiple fetches of the same URL should result in only one request. | 154 // Multiple fetches of the same URL should result in only one request. |
162 TEST_F(SdchDictionaryFetcherTest, Multiple) { | 155 TEST_F(SdchDictionaryFetcherTest, Multiple) { |
163 GURL dictionary_url(PathToGurl("dictionary")); | 156 GURL dictionary_url(PathToGurl("dictionary")); |
164 fetcher()->Schedule(dictionary_url); | 157 fetcher()->Schedule(dictionary_url); |
165 fetcher()->Schedule(dictionary_url); | 158 fetcher()->Schedule(dictionary_url); |
166 fetcher()->Schedule(dictionary_url); | 159 fetcher()->Schedule(dictionary_url); |
167 base::RunLoop().RunUntilIdle(); | 160 base::RunLoop().RunUntilIdle(); |
168 | 161 |
169 EXPECT_EQ(1, JobsRequested()); | 162 EXPECT_EQ(1, JobsRequested()); |
170 std::vector<SdchTestDelegate::DictionaryAdditions> additions; | 163 std::vector<DictionaryAdditions> additions; |
171 manager()->GetDictionaryAdditions(&additions); | 164 GetDictionaryAdditions(&additions); |
172 ASSERT_EQ(1u, additions.size()); | 165 ASSERT_EQ(1u, additions.size()); |
173 EXPECT_EQ(URLRequestSpecifiedResponseJob::ExpectedResponseForURL( | 166 EXPECT_EQ( |
174 dictionary_url), additions[0].dictionary_text); | 167 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url), |
168 additions[0].dictionary_text); | |
175 } | 169 } |
176 | 170 |
177 // A cancel should result in no actual requests being generated. | 171 // A cancel should result in no actual requests being generated. |
178 TEST_F(SdchDictionaryFetcherTest, Cancel) { | 172 TEST_F(SdchDictionaryFetcherTest, Cancel) { |
179 GURL dictionary_url_1(PathToGurl("dictionary_1")); | 173 GURL dictionary_url_1(PathToGurl("dictionary_1")); |
180 GURL dictionary_url_2(PathToGurl("dictionary_2")); | 174 GURL dictionary_url_2(PathToGurl("dictionary_2")); |
181 GURL dictionary_url_3(PathToGurl("dictionary_3")); | 175 GURL dictionary_url_3(PathToGurl("dictionary_3")); |
182 | 176 |
183 fetcher()->Schedule(dictionary_url_1); | 177 fetcher()->Schedule(dictionary_url_1); |
184 fetcher()->Schedule(dictionary_url_2); | 178 fetcher()->Schedule(dictionary_url_2); |
185 fetcher()->Schedule(dictionary_url_3); | 179 fetcher()->Schedule(dictionary_url_3); |
186 fetcher()->Cancel(); | 180 fetcher()->Cancel(); |
187 base::RunLoop().RunUntilIdle(); | 181 base::RunLoop().RunUntilIdle(); |
188 | 182 |
189 // Synchronous execution may have resulted in a single job being scheduled. | 183 // Synchronous execution may have resulted in a single job being scheduled. |
190 EXPECT_GE(1, JobsRequested()); | 184 EXPECT_GE(1, JobsRequested()); |
191 } | 185 } |
192 | |
193 } | 186 } |
OLD | NEW |