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

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

Issue 56023005: First changelist of a series to migrate the component updater protocol (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Dump pings in all cases for easier debugging of failed tests. 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
« no previous file with comments | « chrome/browser/component_updater/test/url_request_post_interceptor.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 6 #include "base/file_util.h"
7 #include "base/strings/stringprintf.h"
8 #include "base/synchronization/lock.h"
7 #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"
8 #include "net/url_request/url_request.h" 11 #include "net/url_request/url_request.h"
9 #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"
14 #include "net/url_request/url_request_simple_job.h"
10 #include "net/url_request/url_request_test_util.h" 15 #include "net/url_request/url_request_test_util.h"
11 16
12 using content::BrowserThread; 17 using content::BrowserThread;
13 18
19 // Returns an empty response body for any request.
20 class URLRequestMockJob : public net::URLRequestSimpleJob {
21 public:
22 URLRequestMockJob(net::URLRequest* request,
23 net::NetworkDelegate* network_delegate)
24 : net::URLRequestSimpleJob(request, network_delegate) {}
25
26 protected:
27 virtual int GetData(std::string* mime_type,
28 std::string* charset,
29 std::string* data,
30 const net::CompletionCallback& callback) const OVERRIDE {
31 mime_type->assign("text/plain");
32 charset->assign("US-ASCII");
33 data->clear();
34 return net::OK;
35 }
36
37 private:
38 virtual ~URLRequestMockJob() {}
39
40 DISALLOW_COPY_AND_ASSIGN(URLRequestMockJob);
41 };
42
43 class URLRequestPostInterceptor::Delegate
44 : public net::URLRequestJobFactory::ProtocolHandler {
45 public:
46 Delegate(const std::string& scheme, const std::string& hostname)
47 : hit_count_(0), scheme_(scheme), hostname_(hostname) {}
48
49 void Register() {
50 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
51 net::URLRequestFilter::GetInstance()->AddHostnameProtocolHandler(
52 scheme_, hostname_,
53 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>(this));
54 }
55
56 void Unregister() {
57 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
58 net::URLRequestFilter::GetInstance()->
59 RemoveHostnameHandler(scheme_, hostname_);
60 }
61
62 // Returns how many requests have been intercepted and captured.
63 int GetHitCount() const {
64 base::AutoLock auto_lock(hit_count_lock_);
65 DCHECK(hit_count_ == static_cast<int>(requests_.size()));
66 return hit_count_;
67 }
68
69 std::vector<std::string> GetRequests() const {
70 base::AutoLock auto_lock(hit_count_lock_);
71 return requests_;
72 }
73
74 private:
75 virtual ~Delegate() {}
76
77 virtual net::URLRequestJob* MaybeCreateJob(
78 net::URLRequest* request,
79 net::NetworkDelegate* network_delegate) const OVERRIDE {
80 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
81
82 if (!request->has_upload())
83 return NULL;
84
85 const net::UploadDataStream* stream = request->get_upload();
86 const net::UploadBytesElementReader* reader =
87 stream->element_readers()[0]->AsBytesReader();
88 const int size = reader->length();
89 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(size));
90 const std::string request_body(reader->bytes());
91
92 {
93 base::AutoLock auto_lock(hit_count_lock_);
94 ++hit_count_;
95 requests_.push_back(request_body);
96 }
97
98 return new URLRequestMockJob(request, network_delegate);
99 }
100
101 mutable base::Lock hit_count_lock_;
102 mutable int hit_count_;
103 mutable std::vector<std::string> requests_;
cpu_(ooo_6.6-7.5) 2013/11/04 22:25:53 is it worth to have all the interesting members as
Sorin Jianu 2013/11/05 22:41:47 These members have to be mutable since the virtual
104
105 const std::string scheme_;
106 const std::string hostname_;
107
108 DISALLOW_COPY_AND_ASSIGN(Delegate);
109 };
110
14 URLRequestPostInterceptor::URLRequestPostInterceptor( 111 URLRequestPostInterceptor::URLRequestPostInterceptor(
15 RequestCounter* counter) : delegate_(new Delegate(counter)) { 112 const std::string& scheme,
113 const std::string& hostname)
114 : delegate_(new Delegate(scheme, hostname)) {
16 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 115 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
17 base::Bind(&Delegate::Register, 116 base::Bind(&Delegate::Register,
18 base::Unretained(delegate_))); 117 base::Unretained(delegate_)));
19 } 118 }
20 119
21 URLRequestPostInterceptor::~URLRequestPostInterceptor() { 120 URLRequestPostInterceptor::~URLRequestPostInterceptor() {
22 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 121 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
23 base::Bind(&Delegate::Unregister, 122 base::Bind(&Delegate::Unregister,
24 base::Unretained(delegate_))); 123 base::Unretained(delegate_)));
25 } 124 }
26 125
27 URLRequestPostInterceptor::Delegate::Delegate( 126 int URLRequestPostInterceptor::GetHitCount() const {
28 RequestCounter* counter) : counter_(counter) { 127 return delegate_->GetHitCount();
29 } 128 }
30 129
31 void URLRequestPostInterceptor::Delegate::Register() { 130 std::vector<std::string> URLRequestPostInterceptor::GetRequests() const {
32 net::URLRequestFilter::GetInstance()->AddHostnameProtocolHandler( 131 return delegate_->GetRequests();
33 "http", "localhost2",
34 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>(this));
35 } 132 }
36 133
37 void URLRequestPostInterceptor::Delegate::Unregister() { 134 std::string URLRequestPostInterceptor::GetRequestsAsString() const {
38 net::URLRequestFilter::GetInstance()-> 135 std::vector<std::string> requests(GetRequests());
39 RemoveHostnameHandler("http", "localhost2"); 136
137 std::string s = "Requests are:";
138
139 int i = 0;
140 for (std::vector<std::string>::const_iterator it = requests.begin();
141 it != requests.end(); ++it) {
142 s.append(base::StringPrintf("\n (%d): %s", ++i, it->c_str()));
143 }
144
145 return s;
40 } 146 }
41 147
42 net::URLRequestJob* URLRequestPostInterceptor::Delegate::MaybeCreateJob(
43 net::URLRequest* request,
44 net::NetworkDelegate* network_delegate) const {
45 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
46 if (request->has_upload()) {
47 counter_->Trial(request);
48 return new URLRequestPingMockJob(request, network_delegate);
49 }
50 return NULL;
51 }
52
53 URLRequestPingMockJob::URLRequestPingMockJob(
54 net::URLRequest* request,
55 net::NetworkDelegate* network_delegate)
56 : net::URLRequestSimpleJob(request, network_delegate) {
57 }
58
59 int URLRequestPingMockJob::GetData(
60 std::string* mime_type,
61 std::string* charset,
62 std::string* data,
63 const net::CompletionCallback& callback) const {
64 mime_type->assign("text/plain");
65 charset->assign("US-ASCII");
66 data->assign(""); // There is no reason to have a response body.
67 return net::OK;
68 }
OLDNEW
« no previous file with comments | « chrome/browser/component_updater/test/url_request_post_interceptor.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698