Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(97)

Side by Side Diff: net/base/sdch_dictionary_fetcher_unittest.cc

Issue 495523003: Change SDCHDictionaryFetcher to use URLRequest instead of URLFetcher. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Cleaned up state machine & handled recursion properly. Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "net/base/sdch_dictionary_fetcher.h"
6
7 #include <string>
8
9 #include "base/run_loop.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "net/base/sdch_manager.h"
13 #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_interceptor.h"
16 #include "net/url_request/url_request_test_util.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "url/gurl.h"
19
20 namespace net {
21
22 static const char* kSampleBufferContext = "This is a sample buffer.";
23
24 class URLRequestSpecifiedResponseJob : public URLRequestSimpleJob {
25 public:
26 URLRequestSpecifiedResponseJob(URLRequest* request,
27 NetworkDelegate* network_delegate)
28 : URLRequestSimpleJob(request, network_delegate) {}
29
30 static void AddUrlHandler() {
31 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
32 jobs_requested_ = 0;
33 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
34 &URLRequestSpecifiedResponseJob::Factory);
35 }
36
37 static void RemoveUrlHandler() {
38 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
39 filter->RemoveHostnameHandler("http", "top.level.domain");
40 jobs_requested_ = 0;
41 }
42
43 static URLRequestJob* Factory(
44 URLRequest* request,
45 net::NetworkDelegate* network_delegate,
46 const std::string& scheme) {
47 ++jobs_requested_;
48 return new URLRequestSpecifiedResponseJob(request, network_delegate);
49 }
50
51 static std::string ExpectedResponseForURL(const GURL& url) {
52 return base::StringPrintf("Response for %s\n%s\nEnd Response for %s\n",
53 url.spec().c_str(), kSampleBufferContext,
54 url.spec().c_str());
55 }
56
57 static int jobs_requested() { return jobs_requested_; }
58
59 private:
60 virtual ~URLRequestSpecifiedResponseJob() {};
61 virtual int GetData(std::string* mime_type,
62 std::string* charset,
63 std::string* data,
64 const CompletionCallback& callback) const OVERRIDE {
65 GURL url(request_->url());
66 *data = ExpectedResponseForURL(url);
67 return OK;
68 }
69
70 static int jobs_requested_;
71 };
72
73 int URLRequestSpecifiedResponseJob::jobs_requested_(0);
74
75 class SdchTestManager : public SdchManager {
76 public:
77 struct DictionaryAdditions {
78 DictionaryAdditions(const std::string& dictionary_text,
79 const GURL& dictionary_url)
80 : dictionary_text(dictionary_text),
81 dictionary_url(dictionary_url) {}
82
83
84 std::string dictionary_text;
85 GURL dictionary_url;
86 };
87
88 virtual bool AddSdchDictionary(const std::string& dictionary_text,
89 const GURL& dictionary_url) OVERRIDE {
90 dictionary_additions.push_back(
91 DictionaryAdditions(dictionary_text, dictionary_url));
92 return true;
93 }
94
95 void GetDictionaryAdditions(std::vector<DictionaryAdditions>* out) {
96 out->swap(dictionary_additions);
97 dictionary_additions.clear();
98 }
99
100 private:
101 std::vector<DictionaryAdditions> dictionary_additions;
102 };
103
104 static const char* kInterceptionGURL = "http://top.level.domain/dictionary";
105
106 class SdchDictionaryFetcherTest : public ::testing::Test {
107 public:
108 SdchDictionaryFetcherTest() {}
109
110 virtual void SetUp() OVERRIDE {
111 DCHECK(!fetcher_.get());
112
113 URLRequestSpecifiedResponseJob::AddUrlHandler();
114 manager_.reset(new SdchTestManager);
115 context_.reset(new TestURLRequestContext);
116 fetcher_.reset(new SdchDictionaryFetcher(manager_.get(), context_.get()));
117 }
118
119 virtual void TearDown() OVERRIDE {
120 URLRequestSpecifiedResponseJob::RemoveUrlHandler();
121 fetcher_.reset();
122 context_.reset();
123 manager_.reset();
124 }
125
126 SdchDictionaryFetcher* fetcher() { return fetcher_.get(); }
127 SdchTestManager* manager() { return manager_.get(); }
128
129 // May not be called outside the SetUp()/TearDown() interval.
130 int JobsRequested() {
131 return URLRequestSpecifiedResponseJob::jobs_requested();
132 }
133
134 private:
135 scoped_ptr<SdchTestManager> manager_;
136 scoped_ptr<TestURLRequestContext> context_;
137 scoped_ptr<SdchDictionaryFetcher> fetcher_;
138 };
139
140 // Schedule a fetch and make sure it happens.
141 TEST_F(SdchDictionaryFetcherTest, Basic) {
142 GURL interception_url = GURL(kInterceptionGURL);
143 fetcher()->Schedule(interception_url);
144
145 base::RunLoop().RunUntilIdle();
146 EXPECT_EQ(1, JobsRequested());
147 std::vector<SdchTestManager::DictionaryAdditions> additions;
148 manager()->GetDictionaryAdditions(&additions);
149 ASSERT_EQ(1u, additions.size());
150 EXPECT_EQ(URLRequestSpecifiedResponseJob::ExpectedResponseForURL(
151 interception_url), additions[0].dictionary_text);
152 }
153
154 // Multiple fetches of the same URL should result in only one request.
155 TEST_F(SdchDictionaryFetcherTest, Multiple) {
156 GURL interception_url = GURL(kInterceptionGURL);
157 fetcher()->Schedule(interception_url);
158 fetcher()->Schedule(interception_url);
159 fetcher()->Schedule(interception_url);
160 base::RunLoop().RunUntilIdle();
161
162 EXPECT_EQ(1, JobsRequested());
163 std::vector<SdchTestManager::DictionaryAdditions> additions;
164 manager()->GetDictionaryAdditions(&additions);
165 ASSERT_EQ(1u, additions.size());
166 EXPECT_EQ(URLRequestSpecifiedResponseJob::ExpectedResponseForURL(
167 interception_url), additions[0].dictionary_text);
168 }
169
170 // A cancel should result in no actual requests being generated.
171 TEST_F(SdchDictionaryFetcherTest, Cancel) {
172 GURL interception_url_1 = GURL(std::string(kInterceptionGURL) + "_1");
173 GURL interception_url_2 = GURL(std::string(kInterceptionGURL) + "_2");
174 GURL interception_url_3 = GURL(std::string(kInterceptionGURL) + "_3");
175 fetcher()->Schedule(interception_url_1);
176 fetcher()->Schedule(interception_url_2);
177 fetcher()->Schedule(interception_url_3);
178 fetcher()->Cancel();
179 base::RunLoop().RunUntilIdle();
180
181 // Synchronous execution may have resulted in a single job being scheduled.
182 EXPECT_GE(1, JobsRequested());
183 }
184
185 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698