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

Side by Side Diff: chrome/browser/component_updater/test/url_request_post_interceptor.cc

Issue 74893002: Changed the update protocol for component updater from v2 to v3. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed feedback from cdn@ Created 7 years, 1 month 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
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "chrome/browser/component_updater/test/url_request_post_interceptor.h" 5 #include "chrome/browser/component_updater/test/url_request_post_interceptor.h"
6 #include "base/file_util.h" 6 #include "base/file_util.h"
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/stringprintf.h" 8 #include "base/strings/stringprintf.h"
9 #include "content/public/test/test_browser_thread.h" 9 #include "content/public/test/test_browser_thread.h"
10 #include "net/base/upload_bytes_element_reader.h" 10 #include "net/base/upload_bytes_element_reader.h"
11 #include "net/url_request/url_request.h" 11 #include "net/url_request/url_request.h"
12 #include "net/url_request/url_request_filter.h" 12 #include "net/url_request/url_request_filter.h"
13 #include "net/url_request/url_request_job_factory.h" 13 #include "net/url_request/url_request_job_factory.h"
14 #include "net/url_request/url_request_simple_job.h" 14 #include "net/url_request/url_request_simple_job.h"
15 #include "net/url_request/url_request_test_util.h" 15 #include "net/url_request/url_request_test_util.h"
16 16
17 using content::BrowserThread; 17 using content::BrowserThread;
18 18
19 namespace component_updater { 19 namespace component_updater {
20 20
21 // Returns a canned response.
21 class URLRequestMockJob : public net::URLRequestSimpleJob { 22 class URLRequestMockJob : public net::URLRequestSimpleJob {
22 public: 23 public:
23 URLRequestMockJob(net::URLRequest* request, 24 URLRequestMockJob(net::URLRequest* request,
24 net::NetworkDelegate* network_delegate, 25 net::NetworkDelegate* network_delegate,
25 const std::string& response) 26 const std::string& response)
26 : net::URLRequestSimpleJob(request, network_delegate), 27 : net::URLRequestSimpleJob(request, network_delegate),
27 response_(response) {} 28 response_(response) {}
28 29
29 protected: 30 protected:
31 virtual int GetResponseCode() const OVERRIDE {
32 return 200;
33 }
34
30 virtual int GetData(std::string* mime_type, 35 virtual int GetData(std::string* mime_type,
31 std::string* charset, 36 std::string* charset,
32 std::string* data, 37 std::string* data,
33 const net::CompletionCallback& callback) const OVERRIDE { 38 const net::CompletionCallback& callback) const OVERRIDE {
34 mime_type->assign("text/plain"); 39 mime_type->assign("text/plain");
35 charset->assign("US-ASCII"); 40 charset->assign("US-ASCII");
36 data->assign(response_); 41 data->assign(response_);
37 return net::OK; 42 return net::OK;
38 } 43 }
39 44
40 private: 45 private:
41 virtual ~URLRequestMockJob() {} 46 virtual ~URLRequestMockJob() {}
42 47
43 std::string response_; 48 std::string response_;
44 DISALLOW_COPY_AND_ASSIGN(URLRequestMockJob); 49 DISALLOW_COPY_AND_ASSIGN(URLRequestMockJob);
45 }; 50 };
46 51
47 URLRequestPostInterceptor::URLRequestPostInterceptor(const GURL& url) 52 URLRequestPostInterceptor::URLRequestPostInterceptor(const GURL& url)
48 : url_(url), hit_count_(0), miss_count_(0) {} 53 : url_(url), hit_count_(0) {}
49 54
50 URLRequestPostInterceptor::~URLRequestPostInterceptor() { 55 URLRequestPostInterceptor::~URLRequestPostInterceptor() {
56 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
57 ClearExpectations();
58 }
59
60 void URLRequestPostInterceptor::ClearExpectations() {
51 while (!expectations_.empty()) { 61 while (!expectations_.empty()) {
52 Expectation expectation(expectations_.front()); 62 Expectation expectation(expectations_.front());
53 delete expectation.first; 63 delete expectation.first;
54 expectations_.pop(); 64 expectations_.pop();
55 } 65 }
56 } 66 }
57 67
58 GURL URLRequestPostInterceptor::GetUrl() const { 68 GURL URLRequestPostInterceptor::GetUrl() const {
59 return url_; 69 return url_;
60 } 70 }
61 71
62 bool URLRequestPostInterceptor::ExpectRequest( 72 bool URLRequestPostInterceptor::ExpectRequest(
63 class RequestMatcher* request_matcher) { 73 class RequestMatcher* request_matcher) {
64 expectations_.push(std::make_pair(request_matcher, "")); 74 expectations_.push(std::make_pair(request_matcher, ""));
65 return true; 75 return true;
66 } 76 }
67 77
68 bool URLRequestPostInterceptor::ExpectRequest( 78 bool URLRequestPostInterceptor::ExpectRequest(
69 class RequestMatcher* request_matcher, 79 class RequestMatcher* request_matcher,
70 const std::string& filepath) { 80 const base::FilePath& filepath) {
71 std::string response; 81 std::string response;
72 const base::FilePath path(base::FilePath().AppendASCII(filepath)); 82 if (filepath.empty() || !base::ReadFileToString(filepath, &response))
73 if (filepath.empty() || !base::ReadFileToString(path, &response))
74 return false; 83 return false;
75 expectations_.push(std::make_pair(request_matcher, response)); 84 expectations_.push(std::make_pair(request_matcher, response));
76 return true; 85 return true;
77 } 86 }
78 87
79 int URLRequestPostInterceptor::GetHitCount() const { 88 int URLRequestPostInterceptor::GetHitCount() const {
80 base::AutoLock auto_lock(interceptor_lock_); 89 base::AutoLock auto_lock(interceptor_lock_);
81 return hit_count_; 90 return hit_count_;
82 } 91 }
83 92
84 int URLRequestPostInterceptor::GetMissCount() const { 93 int URLRequestPostInterceptor::GetCount() const {
85 base::AutoLock auto_lock(interceptor_lock_); 94 base::AutoLock auto_lock(interceptor_lock_);
86 return requests_.size() - hit_count_; 95 return static_cast<int>(requests_.size());
87 } 96 }
88 97
89 std::vector<std::string> 98 std::vector<std::string>
90 URLRequestPostInterceptor::GetRequests() const { 99 URLRequestPostInterceptor::GetRequests() const {
91 base::AutoLock auto_lock(interceptor_lock_); 100 base::AutoLock auto_lock(interceptor_lock_);
92 return requests_; 101 return requests_;
93 } 102 }
94 103
95 std::string URLRequestPostInterceptor::GetRequestsAsString() const { 104 std::string URLRequestPostInterceptor::GetRequestsAsString() const {
96 std::vector<std::string> requests(GetRequests()); 105 std::vector<std::string> requests(GetRequests());
97 106
98 std::string s = "Requests are:"; 107 std::string s = "Requests are:";
99 108
100 int i = 0; 109 int i = 0;
101 for (std::vector<std::string>::const_iterator it = requests.begin(); 110 for (std::vector<std::string>::const_iterator it = requests.begin();
102 it != requests.end(); ++it) { 111 it != requests.end(); ++it) {
103 s.append(base::StringPrintf("\n (%d): %s", ++i, it->c_str())); 112 s.append(base::StringPrintf("\n (%d): %s", ++i, it->c_str()));
104 } 113 }
105 114
106 return s; 115 return s;
107 } 116 }
108 117
118 void URLRequestPostInterceptor::Reset() {
119 base::AutoLock auto_lock(interceptor_lock_);
120 hit_count_ = 0;
121 requests_.clear();
122 ClearExpectations();
123 }
124
109 125
110 class URLRequestPostInterceptor::Delegate 126 class URLRequestPostInterceptor::Delegate
111 : public net::URLRequestJobFactory::ProtocolHandler { 127 : public net::URLRequestJobFactory::ProtocolHandler {
112 public: 128 public:
113 Delegate(const std::string& scheme, const std::string& hostname) 129 Delegate(const std::string& scheme, const std::string& hostname)
114 : scheme_(scheme), hostname_(hostname) {} 130 : scheme_(scheme), hostname_(hostname) {}
115 131
116 void Register() { 132 void Register() {
117 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 133 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
118 net::URLRequestFilter::GetInstance()->AddHostnameProtocolHandler( 134 net::URLRequestFilter::GetInstance()->AddHostnameProtocolHandler(
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 const std::string request_body(reader->bytes()); 189 const std::string request_body(reader->bytes());
174 190
175 { 191 {
176 base::AutoLock auto_lock(interceptor->interceptor_lock_); 192 base::AutoLock auto_lock(interceptor->interceptor_lock_);
177 interceptor->requests_.push_back(request_body); 193 interceptor->requests_.push_back(request_body);
178 if (interceptor->expectations_.empty()) 194 if (interceptor->expectations_.empty())
179 return NULL; 195 return NULL;
180 const URLRequestPostInterceptor::Expectation& expectation( 196 const URLRequestPostInterceptor::Expectation& expectation(
181 interceptor->expectations_.front()); 197 interceptor->expectations_.front());
182 if (expectation.first->Match(request_body)) { 198 if (expectation.first->Match(request_body)) {
199 const std::string response(expectation.second);
183 delete expectation.first; 200 delete expectation.first;
184 interceptor->expectations_.pop(); 201 interceptor->expectations_.pop();
185 ++interceptor->hit_count_; 202 ++interceptor->hit_count_;
186 203
187 return new URLRequestMockJob(request, 204 return new URLRequestMockJob(request, network_delegate, response);
188 network_delegate,
189 expectation.second);
190 } 205 }
191 } 206 }
192 207
193 return NULL; 208 return NULL;
194 } 209 }
195 210
196 typedef std::map<GURL, URLRequestPostInterceptor*> InterceptorMap; 211 typedef std::map<GURL, URLRequestPostInterceptor*> InterceptorMap;
197 InterceptorMap interceptors_; 212 InterceptorMap interceptors_;
198 213
199 const std::string scheme_; 214 const std::string scheme_;
(...skipping 15 matching lines...) Expand all
215 } 230 }
216 231
217 URLRequestPostInterceptorFactory::~URLRequestPostInterceptorFactory() { 232 URLRequestPostInterceptorFactory::~URLRequestPostInterceptorFactory() {
218 BrowserThread::PostTask( 233 BrowserThread::PostTask(
219 BrowserThread::IO, FROM_HERE, 234 BrowserThread::IO, FROM_HERE,
220 base::Bind(&URLRequestPostInterceptor::Delegate::Unregister, 235 base::Bind(&URLRequestPostInterceptor::Delegate::Unregister,
221 base::Unretained(delegate_))); 236 base::Unretained(delegate_)));
222 } 237 }
223 238
224 URLRequestPostInterceptor* URLRequestPostInterceptorFactory::CreateInterceptor( 239 URLRequestPostInterceptor* URLRequestPostInterceptorFactory::CreateInterceptor(
225 const std::string& file_path) { 240 const base::FilePath& filepath) {
226 const GURL base_url(base::StringPrintf("%s://%s", 241 const GURL base_url(base::StringPrintf("%s://%s",
227 scheme_.c_str(), 242 scheme_.c_str(),
228 hostname_.c_str())); 243 hostname_.c_str()));
229 GURL absolute_url(base_url.Resolve(file_path)); 244 GURL absolute_url(base_url.Resolve(filepath.MaybeAsASCII()));
230 URLRequestPostInterceptor* interceptor( 245 URLRequestPostInterceptor* interceptor(
231 new URLRequestPostInterceptor(absolute_url)); 246 new URLRequestPostInterceptor(absolute_url));
232 bool res = BrowserThread::PostTask( 247 bool res = BrowserThread::PostTask(
233 BrowserThread::IO, FROM_HERE, 248 BrowserThread::IO, FROM_HERE,
234 base::Bind(&URLRequestPostInterceptor::Delegate::OnCreateInterceptor, 249 base::Bind(&URLRequestPostInterceptor::Delegate::OnCreateInterceptor,
235 base::Unretained(delegate_), 250 base::Unretained(delegate_),
236 base::Unretained(interceptor))); 251 base::Unretained(interceptor)));
237 if (!res) { 252 if (!res) {
238 delete interceptor; 253 delete interceptor;
239 return NULL; 254 return NULL;
240 } 255 }
241 256
242 return interceptor; 257 return interceptor;
243 } 258 }
244 259
245 } // namespace component_updater 260 } // namespace component_updater
246 261
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698