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

Side by Side Diff: net/url_request/url_request_unittest.cc

Issue 8743019: base::Bind: Convert net::NetworkDelegate. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years 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 (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "build/build_config.h" 5 #include "build/build_config.h"
6 6
7 #if defined(OS_WIN) 7 #if defined(OS_WIN)
8 #include <shlobj.h> 8 #include <shlobj.h>
9 #include <windows.h> 9 #include <windows.h>
10 #endif 10 #endif
11 11
12 #include <algorithm> 12 #include <algorithm>
13 #include <string> 13 #include <string>
14 14
15 #include "base/basictypes.h" 15 #include "base/basictypes.h"
16 #include "base/bind.h" 16 #include "base/bind.h"
17 #include "base/compiler_specific.h" 17 #include "base/compiler_specific.h"
18 #include "base/file_util.h" 18 #include "base/file_util.h"
19 #include "base/format_macros.h" 19 #include "base/format_macros.h"
csilv 2011/11/30 21:56:50 add include for base/memory/weak_ptr.h
James Hawkins 2011/12/01 00:40:26 Done.
20 #include "base/message_loop.h" 20 #include "base/message_loop.h"
21 #include "base/path_service.h" 21 #include "base/path_service.h"
22 #include "base/process_util.h" 22 #include "base/process_util.h"
23 #include "base/string_number_conversions.h" 23 #include "base/string_number_conversions.h"
24 #include "base/string_piece.h" 24 #include "base/string_piece.h"
25 #include "base/string_util.h" 25 #include "base/string_util.h"
26 #include "base/stringprintf.h" 26 #include "base/stringprintf.h"
27 #include "base/utf_string_conversions.h" 27 #include "base/utf_string_conversions.h"
28 #include "net/base/cookie_monster.h" 28 #include "net/base/cookie_monster.h"
29 #include "net/base/cookie_store_test_helpers.h" 29 #include "net/base/cookie_store_test_helpers.h"
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 // A network delegate that blocks requests, optionally cancelling or redirecting 124 // A network delegate that blocks requests, optionally cancelling or redirecting
125 // them. 125 // them.
126 class BlockingNetworkDelegate : public TestNetworkDelegate { 126 class BlockingNetworkDelegate : public TestNetworkDelegate {
127 public: 127 public:
128 BlockingNetworkDelegate() 128 BlockingNetworkDelegate()
129 : retval_(net::ERR_IO_PENDING), 129 : retval_(net::ERR_IO_PENDING),
130 callback_retval_(net::OK), 130 callback_retval_(net::OK),
131 auth_retval_(NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING), 131 auth_retval_(NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING),
132 auth_callback_retval_( 132 auth_callback_retval_(
133 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION), 133 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION),
134 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {} 134 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {}
135 135
136 void set_retval(int retval) { retval_ = retval; } 136 void set_retval(int retval) { retval_ = retval; }
137 void set_callback_retval(int retval) { callback_retval_ = retval; } 137 void set_callback_retval(int retval) { callback_retval_ = retval; }
138 void set_redirect_url(const GURL& url) { redirect_url_ = url; } 138 void set_redirect_url(const GURL& url) { redirect_url_ = url; }
139 void set_auth_retval(NetworkDelegate::AuthRequiredResponse retval) { 139 void set_auth_retval(NetworkDelegate::AuthRequiredResponse retval) {
140 auth_retval_ = retval; } 140 auth_retval_ = retval; }
141 void set_auth_callback_retval(NetworkDelegate::AuthRequiredResponse retval) { 141 void set_auth_callback_retval(NetworkDelegate::AuthRequiredResponse retval) {
142 auth_callback_retval_ = retval; } 142 auth_callback_retval_ = retval; }
143 void set_auth_credentials(const AuthCredentials& auth_credentials) { 143 void set_auth_credentials(const AuthCredentials& auth_credentials) {
144 auth_credentials_ = auth_credentials; 144 auth_credentials_ = auth_credentials;
145 } 145 }
146 146
147 private: 147 private:
148 // TestNetworkDelegate: 148 // TestNetworkDelegate implementation.
149 virtual int OnBeforeURLRequest(net::URLRequest* request, 149 virtual int OnBeforeURLRequest(net::URLRequest* request,
150 net::OldCompletionCallback* callback, 150 const net::CompletionCallback& callback,
151 GURL* new_url) { 151 GURL* new_url) OVERRIDE {
152 if (redirect_url_ == request->url()) { 152 if (redirect_url_ == request->url()) {
153 // We've already seen this request and redirected elsewhere. 153 // We've already seen this request and redirected elsewhere.
154 return net::OK; 154 return net::OK;
155 } 155 }
156 156
157 TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); 157 TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url);
158 158
159 if (!redirect_url_.is_empty()) 159 if (!redirect_url_.is_empty())
160 *new_url = redirect_url_; 160 *new_url = redirect_url_;
161 161
162 if (retval_ != net::ERR_IO_PENDING) 162 if (retval_ != net::ERR_IO_PENDING)
163 return retval_; 163 return retval_;
164 164
165 MessageLoop::current()->PostTask( 165 MessageLoop::current()->PostTask(
166 FROM_HERE, 166 FROM_HERE,
167 method_factory_.NewRunnableMethod(&BlockingNetworkDelegate::DoCallback, 167 base::Bind(&BlockingNetworkDelegate::DoCallback,
168 callback)); 168 weak_factory_.GetWeakPtr(), callback));
169 return net::ERR_IO_PENDING; 169 return net::ERR_IO_PENDING;
170 } 170 }
171 171
172 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired( 172 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired(
173 URLRequest* request, 173 URLRequest* request,
174 const AuthChallengeInfo& auth_info, 174 const AuthChallengeInfo& auth_info,
175 const AuthCallback& callback, 175 const AuthCallback& callback,
176 AuthCredentials* credentials) OVERRIDE { 176 AuthCredentials* credentials) OVERRIDE {
177 TestNetworkDelegate::OnAuthRequired(request, auth_info, callback, 177 TestNetworkDelegate::OnAuthRequired(request, auth_info, callback,
178 credentials); 178 credentials);
179 switch (auth_retval_) { 179 switch (auth_retval_) {
180 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION: 180 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION:
181 break; 181 break;
182 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH: 182 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH:
183 *credentials = auth_credentials_; 183 *credentials = auth_credentials_;
184 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH: 184 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH:
185 break; 185 break;
186 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING: 186 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING:
187 MessageLoop::current()->PostTask( 187 MessageLoop::current()->PostTask(
188 FROM_HERE, 188 FROM_HERE,
189 method_factory_.NewRunnableMethod( 189 base::Bind(&BlockingNetworkDelegate::DoAuthCallback,
190 &BlockingNetworkDelegate::DoAuthCallback, 190 weak_factory_.GetWeakPtr(), callback, credentials));
191 callback, credentials));
192 break; 191 break;
193 } 192 }
194 return auth_retval_; 193 return auth_retval_;
195 } 194 }
196 195
197 void DoCallback(net::OldCompletionCallback* callback) { 196 void DoCallback(const net::CompletionCallback& callback) {
198 callback->Run(callback_retval_); 197 callback.Run(callback_retval_);
199 } 198 }
200 199
201 void DoAuthCallback(const AuthCallback& callback, 200 void DoAuthCallback(const AuthCallback& callback,
202 AuthCredentials* credentials) { 201 AuthCredentials* credentials) {
203 if (auth_callback_retval_ == 202 if (auth_callback_retval_ ==
204 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH) { 203 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH) {
205 *credentials = auth_credentials_; 204 *credentials = auth_credentials_;
206 } 205 }
207 callback.Run(auth_callback_retval_); 206 callback.Run(auth_callback_retval_);
208 } 207 }
209 208
210 209
211 int retval_; 210 int retval_;
212 int callback_retval_; 211 int callback_retval_;
213 GURL redirect_url_; 212 GURL redirect_url_;
214 NetworkDelegate::AuthRequiredResponse auth_retval_; 213 NetworkDelegate::AuthRequiredResponse auth_retval_;
215 NetworkDelegate::AuthRequiredResponse auth_callback_retval_; 214 NetworkDelegate::AuthRequiredResponse auth_callback_retval_;
216 AuthCredentials auth_credentials_; 215 AuthCredentials auth_credentials_;
217 ScopedRunnableMethodFactory<BlockingNetworkDelegate> method_factory_; 216 base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_;
218 }; 217 };
219 218
220 // A simple Interceptor that returns a pre-built URLRequestJob one time. 219 // A simple Interceptor that returns a pre-built URLRequestJob one time.
221 class TestJobInterceptor : public URLRequestJobFactory::Interceptor { 220 class TestJobInterceptor : public URLRequestJobFactory::Interceptor {
222 public: 221 public:
223 TestJobInterceptor() 222 TestJobInterceptor()
224 : main_intercept_job_(NULL) { 223 : main_intercept_job_(NULL) {
225 } 224 }
226 225
227 virtual URLRequestJob* MaybeIntercept(URLRequest* request) const OVERRIDE { 226 virtual URLRequestJob* MaybeIntercept(URLRequest* request) const OVERRIDE {
(...skipping 3442 matching lines...) Expand 10 before | Expand all | Expand 10 after
3670 req.SetExtraRequestHeaders(headers); 3669 req.SetExtraRequestHeaders(headers);
3671 req.Start(); 3670 req.Start();
3672 MessageLoop::current()->Run(); 3671 MessageLoop::current()->Run();
3673 // If the net tests are being run with ChromeFrame then we need to allow for 3672 // If the net tests are being run with ChromeFrame then we need to allow for
3674 // the 'chromeframe' suffix which is added to the user agent before the 3673 // the 'chromeframe' suffix which is added to the user agent before the
3675 // closing parentheses. 3674 // closing parentheses.
3676 EXPECT_TRUE(StartsWithASCII(d.data_received(), "Lynx (textmode", true)); 3675 EXPECT_TRUE(StartsWithASCII(d.data_received(), "Lynx (textmode", true));
3677 } 3676 }
3678 3677
3679 } // namespace net 3678 } // namespace net
OLDNEW
« net/url_request/url_request_http_job.cc ('K') | « net/url_request/url_request_test_util.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698