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

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

Issue 10905259: Refactor blocking network delegates (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Conversion of tests using BlockingNetworkDelegate* corrected + getting rid of WaitUntilBlocked Created 8 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
« no previous file with comments | « net/url_request/url_request_test_util.cc ('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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 <windows.h> 8 #include <windows.h>
9 #include <shlobj.h> 9 #include <shlobj.h>
10 #endif 10 #endif
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 return false; 139 return false;
140 140
141 for (size_t i = 0; i < size; ++i) { 141 for (size_t i = 0; i < size; ++i) {
142 if (!a[i].Equals(b[i])) 142 if (!a[i].Equals(b[i]))
143 return false; 143 return false;
144 } 144 }
145 145
146 return true; 146 return true;
147 } 147 }
148 148
149 // A network delegate that blocks requests, optionally cancelling or redirecting 149 // A network delegate that allows the user to choose a subset of request stages
150 // them. 150 // to block in. When blocking, the delegate can do one of the following:
151 // * synchronously return a pre-specified error code, or
152 // * asynchronously return that value via an automatically called callback,
153 // or
154 // * block and wait for the user to do a callback.
155 // Additionally, the user may also specify a redirect URL -- then each request
156 // with the current URL different from the redirect target will be redirected
157 // to that target, in the on-before-URL-request stage, independent of whether
158 // the delegate blocks in ON_BEFORE_URL_REQUEST or not.
151 class BlockingNetworkDelegate : public TestNetworkDelegate { 159 class BlockingNetworkDelegate : public TestNetworkDelegate {
152 public: 160 public:
153 BlockingNetworkDelegate() 161 // Stages in which the delegate can block.
154 : retval_(ERR_IO_PENDING), 162 enum Stage {
155 callback_retval_(OK),
156 auth_retval_(NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING),
157 auth_callback_retval_(
158 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION),
159 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {}
160
161 void set_retval(int retval) { retval_ = retval; }
162 void set_callback_retval(int retval) { callback_retval_ = retval; }
163 void set_redirect_url(const GURL& url) { redirect_url_ = url; }
164 void set_auth_retval(NetworkDelegate::AuthRequiredResponse retval) {
165 auth_retval_ = retval; }
166 void set_auth_callback_retval(NetworkDelegate::AuthRequiredResponse retval) {
167 auth_callback_retval_ = retval; }
168 void set_auth_credentials(const AuthCredentials& auth_credentials) {
169 auth_credentials_ = auth_credentials;
170 }
171
172 private:
173 // TestNetworkDelegate implementation.
174 virtual int OnBeforeURLRequest(URLRequest* request,
175 const CompletionCallback& callback,
176 GURL* new_url) OVERRIDE {
177 if (redirect_url_ == request->url()) {
178 // We've already seen this request and redirected elsewhere.
179 return OK;
180 }
181
182 TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url);
183
184 if (!redirect_url_.is_empty())
185 *new_url = redirect_url_;
186
187 if (retval_ != ERR_IO_PENDING)
188 return retval_;
189
190 MessageLoop::current()->PostTask(
191 FROM_HERE,
192 base::Bind(&BlockingNetworkDelegate::DoCallback,
193 weak_factory_.GetWeakPtr(), callback));
194 return ERR_IO_PENDING;
195 }
196
197 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired(
198 URLRequest* request,
199 const AuthChallengeInfo& auth_info,
200 const AuthCallback& callback,
201 AuthCredentials* credentials) OVERRIDE {
202 TestNetworkDelegate::OnAuthRequired(request, auth_info, callback,
203 credentials);
204 switch (auth_retval_) {
205 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION:
206 break;
207 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH:
208 *credentials = auth_credentials_;
209 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH:
210 break;
211 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING:
212 MessageLoop::current()->PostTask(
213 FROM_HERE,
214 base::Bind(&BlockingNetworkDelegate::DoAuthCallback,
215 weak_factory_.GetWeakPtr(), callback, credentials));
216 break;
217 }
218 return auth_retval_;
219 }
220
221 void DoCallback(const CompletionCallback& callback) {
222 callback.Run(callback_retval_);
223 }
224
225 void DoAuthCallback(const AuthCallback& callback,
226 AuthCredentials* credentials) {
227 if (auth_callback_retval_ ==
228 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH) {
229 *credentials = auth_credentials_;
230 }
231 callback.Run(auth_callback_retval_);
232 }
233
234
235 int retval_;
236 int callback_retval_;
237 GURL redirect_url_;
238 NetworkDelegate::AuthRequiredResponse auth_retval_;
239 NetworkDelegate::AuthRequiredResponse auth_callback_retval_;
240 AuthCredentials auth_credentials_;
241 base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_;
242 };
243
244 // A network delegate that allows blocking requests until a callback function is
245 // called.
246 class BlockingNetworkDelegateWithManualCallback : public TestNetworkDelegate {
247 public:
248 enum State {
249 NOT_BLOCKED = 0, 163 NOT_BLOCKED = 0,
250 ON_BEFORE_URL_REQUEST = 1 << 0, 164 ON_BEFORE_URL_REQUEST = 1 << 0,
251 ON_BEFORE_SEND_HEADERS = 1 << 1, 165 ON_BEFORE_SEND_HEADERS = 1 << 1,
252 ON_HEADERS_RECEIVED = 1 << 2, 166 ON_HEADERS_RECEIVED = 1 << 2,
253 ON_AUTH_REQUIRED = 1 << 3 167 ON_AUTH_REQUIRED = 1 << 3
254 }; 168 };
255 169
256 BlockingNetworkDelegateWithManualCallback() 170 // Behavior during blocked stages. During other stages, just
257 : block_on_(0), 171 // returns net::OK or NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION.
258 state_(NOT_BLOCKED) { 172 enum BlockMode {
259 } 173 SYNCHRONOUS, // No callback, returns specified return values.
260 174 AUTO_CALLBACK, // |this| posts a task to run the callback using the
261 // Activates blocking on |state|. 175 // specified return codes.
262 void BlockOn(State state) { 176 USER_CALLBACK, // User takes care of doing a callback. |retval_| and
263 block_on_ |= state; 177 // |auth_retval_| are ignored.
mmenke 2012/09/21 15:15:59 Should add something along the lines of "Also, The
vabr (Chromium) 2012/09/21 20:03:42 Done. Played with the wording, feel free to protes
264 } 178 };
265 179
266 void DoCallback(int rv) { 180 // Creates a delegate which does not block at all.
267 ASSERT_NE(NOT_BLOCKED, state_); 181 explicit BlockingNetworkDelegate(BlockMode block_mode);
268 CompletionCallback callback = callback_; 182
269 Reset(); 183 // Setters.
270 callback.Run(rv); 184 void set_retval(int retval) {
271 } 185 ASSERT_NE(USER_CALLBACK, block_mode_);
vabr (Chromium) 2012/09/21 11:11:36 In USER_CALLBACK mode |retval_| is used at all. Si
mmenke 2012/09/21 15:15:59 There's a comment about it in BlockMode, which I t
vabr (Chromium) 2012/09/21 20:03:42 Good point with the BlockMode. I think it is OK to
272 186 ASSERT_NE(ERR_IO_PENDING, retval);
273 void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response) { 187 ASSERT_NE(OK, retval);
274 ASSERT_EQ(ON_AUTH_REQUIRED, state_); 188 retval_ = retval;
275 AuthCallback auth_callback = auth_callback_; 189 }
276 Reset(); 190
277 auth_callback.Run(response); 191 // If |auth_retval| == AUTH_REQUIRED_RESPONSE_SET_AUTH, then
278 } 192 // |auth_credentials_| will be passed with the response.
279 193 void set_auth_retval(AuthRequiredResponse auth_retval) {
280 // Runs the message loop until |state| is reached. 194 ASSERT_NE(USER_CALLBACK, block_mode_);
281 void WaitForState(State state) { 195 ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, auth_retval);
282 while (state_ != state) 196 auth_retval_ = auth_retval;
283 MessageLoop::current()->RunAllPending(); 197 }
198 void set_auth_credentials(const AuthCredentials& auth_credentials) {
199 auth_credentials_ = auth_credentials;
200 }
201
202 void set_redirect_url(const GURL& url) {
203 redirect_url_ = url;
204 }
205
206 // Add a stage to block on. Unless |stage| is NOT_BLOCKED or ON_AUTH_REQUIRED,
207 // it is mandatory to call set_retval().
208 void BlockOn(Stage stage) {
mmenke 2012/09/21 15:15:59 nit: Since the |= behavior doesn't seem to be nee
vabr (Chromium) 2012/09/21 20:03:42 I don't think I agree that this is not needed -- w
mmenke 2012/09/21 20:20:18 Both blocks could be set with a single call. I di
vabr (Chromium) 2012/09/23 08:46:16 Ah, I see. I misunderstood you previously. Impleme
209 block_on_ |= stage;
210 }
211
212 // For users to trigger a callback returning |response|.
213 // Side-effects: resets |stage_blocked_for_callback_| and stored callbacks.
214 // Only call if |block_mode_| == USER_CALLBACK.
215 void DoCallback(int response);
216 void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response);
mmenke 2012/09/21 15:15:59 optional nit: Suggest these two functions be put
vabr (Chromium) 2012/09/21 20:03:42 Done.
217
218 Stage stage_blocked_for_callback() const {
mmenke 2012/09/21 15:15:59 May want a comment on this, though it's not exactl
vabr (Chromium) 2012/09/21 20:03:42 Done.
219 EXPECT_EQ(USER_CALLBACK, block_mode_);
220 return stage_blocked_for_callback_;
mmenke 2012/09/21 15:15:59 Good idea.
284 } 221 }
285 222
286 private: 223 private:
287 // TestNetworkDelegate implementation. 224 // TestNetworkDelegate implementation.
288 virtual int OnBeforeURLRequest(URLRequest* request, 225 virtual int OnBeforeURLRequest(URLRequest* request,
289 const CompletionCallback& callback, 226 const CompletionCallback& callback,
290 GURL* new_url) OVERRIDE { 227 GURL* new_url) OVERRIDE;
291 TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url);
292 if ((block_on_ & ON_BEFORE_URL_REQUEST) == 0) {
293 return OK;
294 } else {
295 state_ = ON_BEFORE_URL_REQUEST;
296 callback_ = callback;
297 return ERR_IO_PENDING;
298 }
299 }
300 228
301 virtual int OnBeforeSendHeaders(URLRequest* request, 229 virtual int OnBeforeSendHeaders(URLRequest* request,
302 const CompletionCallback& callback, 230 const CompletionCallback& callback,
303 HttpRequestHeaders* headers) OVERRIDE { 231 HttpRequestHeaders* headers) OVERRIDE;
304 TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers);
305 if ((block_on_ & ON_BEFORE_SEND_HEADERS) == 0) {
306 return OK;
307 } else {
308 state_ = ON_BEFORE_SEND_HEADERS;
309 callback_ = callback;
310 return ERR_IO_PENDING;
311 }
312 }
313 232
314 virtual int OnHeadersReceived( 233 virtual int OnHeadersReceived(
315 URLRequest* request, 234 URLRequest* request,
316 const CompletionCallback& callback, 235 const CompletionCallback& callback,
317 HttpResponseHeaders* original_response_headers, 236 HttpResponseHeaders* original_response_headers,
318 scoped_refptr<HttpResponseHeaders>* override_response_headers) 237 scoped_refptr<HttpResponseHeaders>* override_response_headers) OVERRIDE;
319 OVERRIDE {
320 TestNetworkDelegate::OnHeadersReceived(
321 request, callback, original_response_headers,
322 override_response_headers);
323 if ((block_on_ & ON_HEADERS_RECEIVED) == 0) {
324 return OK;
325 } else {
326 state_ = ON_HEADERS_RECEIVED;
327 callback_ = callback;
328 return ERR_IO_PENDING;
329 }
330 }
331 238
332 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired( 239 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired(
333 URLRequest* request, 240 URLRequest* request,
334 const AuthChallengeInfo& auth_info, 241 const AuthChallengeInfo& auth_info,
335 const AuthCallback& callback, 242 const AuthCallback& callback,
336 AuthCredentials* credentials) OVERRIDE { 243 AuthCredentials* credentials) OVERRIDE;
337 if ((block_on_ & ON_AUTH_REQUIRED) == 0) {
338 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
339 } else {
340 state_ = ON_AUTH_REQUIRED;
341 auth_callback_ = callback;
342 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING;
343 }
344 }
345 244
346 void Reset() { 245 void Reset() {
mmenke 2012/09/21 15:15:59 May want to add an: EXPECT_NE(NOT_BLOCKED, stage_
mmenke 2012/09/21 15:18:26 Suggesting this more for documentation purposes th
vabr (Chromium) 2012/09/21 20:03:42 Done.
347 state_ = NOT_BLOCKED; 246 stage_blocked_for_callback_ = NOT_BLOCKED;
348 callback_.Reset(); 247 callback_.Reset();
349 auth_callback_.Reset(); 248 auth_callback_.Reset();
350 } 249 }
mmenke 2012/09/21 15:15:59 Think this should be de-inlined, since everything
vabr (Chromium) 2012/09/21 20:03:42 Done. Plus added comment to the declaration.
351 250
352 int block_on_; // Bit mask on which states to block. 251 void RunCallback(int response, const CompletionCallback& callback);
353 State state_; 252 void RunAuthCallback(AuthRequiredResponse response,
253 const AuthCallback& callback);
254
255 // Checks whether we should block in |stage|. If yes, returns an error code
256 // and optionally sets up callback based on |block_mode_|. If no, returns OK.
257 int MaybeBlockStage(Stage stage, const CompletionCallback& callback);
258
259 // Configuration parameters:
260 const BlockMode block_mode_;
261
262 // Values returned on blocking stages when mode is SYNCHRONOUS or
263 // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING.
264 int retval_; // To be returned in non-auth stages.
265 AuthRequiredResponse auth_retval_;
266
267 GURL redirect_url_; // Used if non-empty.
268 int block_on_; // Bit mask: in which stages to block.
269
270 // |auth_credentials_| will be copied to |*target_auth_credential_| on
271 // callback.
272 AuthCredentials auth_credentials_;
273 AuthCredentials* target_auth_credentials_;
274
275 // Internal parameters:
mmenke 2012/09/21 15:15:59 Think "internal parameters" is a little vague, and
vabr (Chromium) 2012/09/21 20:03:42 Attempted to be more specific, also with the "conf
276 // Last blocked stage waiting for user callback (unused if |block_mode_| !=
277 // USER_CALLBACK).
278 Stage stage_blocked_for_callback_;
279
280 // Callback objects stored during blocking stages.
354 CompletionCallback callback_; 281 CompletionCallback callback_;
355 AuthCallback auth_callback_; 282 AuthCallback auth_callback_;
283
284 base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_;
285
286 DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate);
356 }; 287 };
357 288
289 BlockingNetworkDelegate::BlockingNetworkDelegate(BlockMode block_mode)
290 : block_mode_(block_mode),
291 retval_(block_mode == USER_CALLBACK ? ERR_IO_PENDING : OK),
292 auth_retval_(block_mode == USER_CALLBACK ?
293 AUTH_REQUIRED_RESPONSE_IO_PENDING :
294 AUTH_REQUIRED_RESPONSE_NO_ACTION),
mmenke 2012/09/21 15:15:59 These USER_CALLBACK checks aren't needed, since th
vabr (Chromium) 2012/09/21 20:03:42 True, I forgot I hard-coded those IO_PENDING value
295 block_on_(0),
296 target_auth_credentials_(NULL),
297 stage_blocked_for_callback_(NOT_BLOCKED),
298 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
299 }
300
301 int BlockingNetworkDelegate::OnBeforeURLRequest(
302 URLRequest* request,
303 const CompletionCallback& callback,
304 GURL* new_url) {
305 if (redirect_url_ == request->url())
306 return OK; // We've already seen this request and redirected elsewhere.
307
308 TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url);
309
310 if (!redirect_url_.is_empty())
311 *new_url = redirect_url_;
312
313 return MaybeBlockStage(ON_BEFORE_URL_REQUEST, callback);
314 }
315
316 int BlockingNetworkDelegate::OnBeforeSendHeaders(
317 URLRequest* request,
318 const CompletionCallback& callback,
319 HttpRequestHeaders* headers) {
320 TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers);
321
322 return MaybeBlockStage(ON_BEFORE_SEND_HEADERS, callback);
323 }
324
325 int BlockingNetworkDelegate::OnHeadersReceived(
326 URLRequest* request,
327 const CompletionCallback& callback,
328 HttpResponseHeaders* original_response_headers,
329 scoped_refptr<HttpResponseHeaders>* override_response_headers) {
330 TestNetworkDelegate::OnHeadersReceived(
331 request, callback, original_response_headers,
332 override_response_headers);
333
334 return MaybeBlockStage(ON_HEADERS_RECEIVED, callback);
335 }
336
337 NetworkDelegate::AuthRequiredResponse BlockingNetworkDelegate::OnAuthRequired(
338 URLRequest* request,
339 const AuthChallengeInfo& auth_info,
340 const AuthCallback& callback,
341 AuthCredentials* credentials) {
342 TestNetworkDelegate::OnAuthRequired(request, auth_info, callback,
343 credentials);
344 // Check that the user has provided callback for the previous blocked stage.
345 EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_);
346
347 if ((block_on_ & ON_AUTH_REQUIRED) == 0) {
348 return AUTH_REQUIRED_RESPONSE_NO_ACTION;
349 }
350
351 if (block_mode_ == USER_CALLBACK)
352 stage_blocked_for_callback_ = ON_AUTH_REQUIRED;
mmenke 2012/09/21 15:15:59 Think that this line should just be move into the
vabr (Chromium) 2012/09/21 20:03:42 Nice, thanks! Done.
353
354 if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH)
355 target_auth_credentials_ = credentials;
mmenke 2012/09/21 15:15:59 This seems weird in the USER_CALLBACK case. My su
vabr (Chromium) 2012/09/21 20:03:42 I'm not sure I understood the alternative solution
mmenke 2012/09/21 20:20:18 The alternative was "if (auth_retval_ == AUTH_REQU
356
357 switch (block_mode_) {
358 case SYNCHRONOUS:
359 if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH)
360 *target_auth_credentials_ = auth_credentials_;
361 return auth_retval_;
362
363 case AUTO_CALLBACK:
364 MessageLoop::current()->PostTask(
365 FROM_HERE,
366 base::Bind(&BlockingNetworkDelegate::RunAuthCallback,
367 weak_factory_.GetWeakPtr(), auth_retval_, callback));
368 return AUTH_REQUIRED_RESPONSE_IO_PENDING;
369
370 case USER_CALLBACK:
371 auth_callback_ = callback;
372 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
373 return AUTH_REQUIRED_RESPONSE_IO_PENDING;
374 }
375 NOTREACHED();
376 return AUTH_REQUIRED_RESPONSE_NO_ACTION; // Dummy value.
377 }
378
379 void BlockingNetworkDelegate::DoCallback(int response) {
mmenke 2012/09/21 15:15:59 Per the style guide, The four callback functions s
vabr (Chromium) 2012/09/21 20:03:42 Done.
380 ASSERT_EQ(USER_CALLBACK, block_mode_);
381 ASSERT_NE(NOT_BLOCKED, stage_blocked_for_callback_);
382 ASSERT_NE(ON_AUTH_REQUIRED, stage_blocked_for_callback_);
383 CompletionCallback callback = callback_;
384 Reset();
385 RunCallback(response, callback);
386 }
387
388 void BlockingNetworkDelegate::DoAuthCallback(
389 NetworkDelegate::AuthRequiredResponse response) {
390 ASSERT_EQ(USER_CALLBACK, block_mode_);
391 ASSERT_EQ(ON_AUTH_REQUIRED, stage_blocked_for_callback_);
392 AuthCallback auth_callback = auth_callback_;
393 Reset();
394 RunAuthCallback(response, auth_callback);
395 }
396
397 void BlockingNetworkDelegate::RunCallback(int response,
398 const CompletionCallback& callback) {
399 callback.Run(response);
400 }
401
402 void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response,
403 const AuthCallback& callback) {
mmenke 2012/09/21 15:15:59 nit: Fix indent.
vabr (Chromium) 2012/09/21 20:03:42 Done.
404 if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) {
405 ASSERT_TRUE(target_auth_credentials_ != NULL);
406 *target_auth_credentials_ = auth_credentials_;
407 }
408 callback.Run(response);
409 }
410
411 int BlockingNetworkDelegate::MaybeBlockStage(
412 BlockingNetworkDelegate::Stage stage,
413 const CompletionCallback& callback) {
414 // Check that the user has provided callback for the previous blocked stage.
415 EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_);
416
417 if ((block_on_ & stage) == 0) {
418 return OK;
419 }
420
421 if (block_mode_ == USER_CALLBACK)
422 stage_blocked_for_callback_ = stage;
mmenke 2012/09/21 15:15:59 As above, think this should be moved down right ne
vabr (Chromium) 2012/09/21 20:03:42 Done.
423
424 switch (block_mode_) {
425 case SYNCHRONOUS:
426 EXPECT_NE(OK, retval_);
427 return retval_;
428
429 case AUTO_CALLBACK:
430 MessageLoop::current()->PostTask(
431 FROM_HERE,
432 base::Bind(&BlockingNetworkDelegate::RunCallback,
433 weak_factory_.GetWeakPtr(), retval_, callback));
434 return ERR_IO_PENDING;
435
436 case USER_CALLBACK:
437 callback_ = callback;
438 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
439 return ERR_IO_PENDING;
440 }
441 NOTREACHED();
442 return 0;
443 }
444
358 class TestURLRequestContextWithProxy : public TestURLRequestContext { 445 class TestURLRequestContextWithProxy : public TestURLRequestContext {
359 public: 446 public:
360 // Does not own |delegate|. 447 // Does not own |delegate|.
361 TestURLRequestContextWithProxy(const std::string& proxy, 448 TestURLRequestContextWithProxy(const std::string& proxy,
362 NetworkDelegate* delegate) 449 NetworkDelegate* delegate)
363 : TestURLRequestContext(true) { 450 : TestURLRequestContext(true) {
364 context_storage_.set_proxy_service(ProxyService::CreateFixed(proxy)); 451 context_storage_.set_proxy_service(ProxyService::CreateFixed(proxy));
365 set_network_delegate(delegate); 452 set_network_delegate(delegate);
366 Init(); 453 Init();
367 } 454 }
(...skipping 1539 matching lines...) Expand 10 before | Expand all | Expand 10 after
1907 EXPECT_EQ(1, network_delegate.error_count()); 1994 EXPECT_EQ(1, network_delegate.error_count());
1908 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, network_delegate.last_error()); 1995 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, network_delegate.last_error());
1909 } 1996 }
1910 } 1997 }
1911 1998
1912 // Tests that the network delegate can block and cancel a request. 1999 // Tests that the network delegate can block and cancel a request.
1913 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) { 2000 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) {
1914 ASSERT_TRUE(test_server_.Start()); 2001 ASSERT_TRUE(test_server_.Start());
1915 2002
1916 TestDelegate d; 2003 TestDelegate d;
1917 BlockingNetworkDelegate network_delegate; 2004 BlockingNetworkDelegate network_delegate(
1918 network_delegate.set_callback_retval(ERR_EMPTY_RESPONSE); 2005 BlockingNetworkDelegate::AUTO_CALLBACK);
2006 network_delegate.BlockOn(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
2007 network_delegate.set_retval(ERR_EMPTY_RESPONSE);
1919 2008
1920 TestURLRequestContextWithProxy context( 2009 TestURLRequestContextWithProxy context(
1921 test_server_.host_port_pair().ToString(), 2010 test_server_.host_port_pair().ToString(),
1922 &network_delegate); 2011 &network_delegate);
1923 2012
1924 { 2013 {
1925 URLRequest r(test_server_.GetURL(""), &d, &context); 2014 URLRequest r(test_server_.GetURL(""), &d, &context);
1926 2015
1927 r.Start(); 2016 r.Start();
1928 MessageLoop::current()->Run(); 2017 MessageLoop::current()->Run();
1929 2018
1930 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); 2019 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
1931 EXPECT_EQ(ERR_EMPTY_RESPONSE, r.status().error()); 2020 EXPECT_EQ(ERR_EMPTY_RESPONSE, r.status().error());
1932 EXPECT_EQ(1, network_delegate.created_requests()); 2021 EXPECT_EQ(1, network_delegate.created_requests());
1933 EXPECT_EQ(0, network_delegate.destroyed_requests()); 2022 EXPECT_EQ(0, network_delegate.destroyed_requests());
1934 } 2023 }
1935 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2024 EXPECT_EQ(1, network_delegate.destroyed_requests());
1936 } 2025 }
1937 2026
1938 // Tests that the network delegate can cancel a request synchronously. 2027 // Tests that the network delegate can cancel a request synchronously.
1939 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously) { 2028 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously) {
1940 ASSERT_TRUE(test_server_.Start()); 2029 ASSERT_TRUE(test_server_.Start());
1941 2030
1942 TestDelegate d; 2031 TestDelegate d;
1943 BlockingNetworkDelegate network_delegate; 2032 BlockingNetworkDelegate network_delegate(
2033 BlockingNetworkDelegate::SYNCHRONOUS);
2034 network_delegate.BlockOn(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
1944 network_delegate.set_retval(ERR_EMPTY_RESPONSE); 2035 network_delegate.set_retval(ERR_EMPTY_RESPONSE);
1945 2036
1946 TestURLRequestContextWithProxy context( 2037 TestURLRequestContextWithProxy context(
1947 test_server_.host_port_pair().ToString(), 2038 test_server_.host_port_pair().ToString(),
1948 &network_delegate); 2039 &network_delegate);
1949 2040
1950 { 2041 {
1951 URLRequest r(test_server_.GetURL(""), &d, &context); 2042 URLRequest r(test_server_.GetURL(""), &d, &context);
1952 2043
1953 r.Start(); 2044 r.Start();
1954 MessageLoop::current()->Run(); 2045 MessageLoop::current()->Run();
1955 2046
1956 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); 2047 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
1957 EXPECT_EQ(ERR_EMPTY_RESPONSE, r.status().error()); 2048 EXPECT_EQ(ERR_EMPTY_RESPONSE, r.status().error());
1958 EXPECT_EQ(1, network_delegate.created_requests()); 2049 EXPECT_EQ(1, network_delegate.created_requests());
1959 EXPECT_EQ(0, network_delegate.destroyed_requests()); 2050 EXPECT_EQ(0, network_delegate.destroyed_requests());
1960 } 2051 }
1961 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2052 EXPECT_EQ(1, network_delegate.destroyed_requests());
1962 } 2053 }
1963 2054
1964 // Tests that the network delegate can block and redirect a request to a new 2055 // Tests that the network delegate can block and redirect a request to a new
1965 // URL. 2056 // URL.
1966 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) { 2057 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) {
1967 ASSERT_TRUE(test_server_.Start()); 2058 ASSERT_TRUE(test_server_.Start());
1968 2059
1969 TestDelegate d; 2060 TestDelegate d;
1970 BlockingNetworkDelegate network_delegate; 2061 BlockingNetworkDelegate network_delegate(
2062 BlockingNetworkDelegate::AUTO_CALLBACK);
2063 network_delegate.BlockOn(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
1971 GURL redirect_url(test_server_.GetURL("simple.html")); 2064 GURL redirect_url(test_server_.GetURL("simple.html"));
1972 network_delegate.set_redirect_url(redirect_url); 2065 network_delegate.set_redirect_url(redirect_url);
1973 2066
1974 TestURLRequestContextWithProxy context( 2067 TestURLRequestContextWithProxy context(
1975 test_server_.host_port_pair().ToString(), 2068 test_server_.host_port_pair().ToString(),
1976 &network_delegate); 2069 &network_delegate);
1977 2070
1978 { 2071 {
1979 GURL original_url(test_server_.GetURL("empty.html")); 2072 GURL original_url(test_server_.GetURL("empty.html"));
1980 URLRequest r(original_url, &d, &context); 2073 URLRequest r(original_url, &d, &context);
(...skipping 11 matching lines...) Expand all
1992 } 2085 }
1993 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2086 EXPECT_EQ(1, network_delegate.destroyed_requests());
1994 } 2087 }
1995 2088
1996 // Tests that the network delegate can block and redirect a request to a new 2089 // Tests that the network delegate can block and redirect a request to a new
1997 // URL by setting a redirect_url and returning in OnBeforeURLRequest directly. 2090 // URL by setting a redirect_url and returning in OnBeforeURLRequest directly.
1998 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) { 2091 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) {
1999 ASSERT_TRUE(test_server_.Start()); 2092 ASSERT_TRUE(test_server_.Start());
2000 2093
2001 TestDelegate d; 2094 TestDelegate d;
2002 BlockingNetworkDelegate network_delegate; 2095 BlockingNetworkDelegate network_delegate(
2096 BlockingNetworkDelegate::SYNCHRONOUS);
2003 GURL redirect_url(test_server_.GetURL("simple.html")); 2097 GURL redirect_url(test_server_.GetURL("simple.html"));
2004 network_delegate.set_redirect_url(redirect_url); 2098 network_delegate.set_redirect_url(redirect_url);
2005 network_delegate.set_retval(OK);
2006 2099
2007 TestURLRequestContextWithProxy context( 2100 TestURLRequestContextWithProxy context(
2008 test_server_.host_port_pair().ToString(), 2101 test_server_.host_port_pair().ToString(),
2009 &network_delegate); 2102 &network_delegate);
2010 2103
2011 { 2104 {
2012 GURL original_url(test_server_.GetURL("empty.html")); 2105 GURL original_url(test_server_.GetURL("empty.html"));
2013 URLRequest r(original_url, &d, &context); 2106 URLRequest r(original_url, &d, &context);
2014 2107
2015 r.Start(); 2108 r.Start();
(...skipping 10 matching lines...) Expand all
2026 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2119 EXPECT_EQ(1, network_delegate.destroyed_requests());
2027 } 2120 }
2028 2121
2029 // Tests that redirects caused by the network delegate preserve POST data. 2122 // Tests that redirects caused by the network delegate preserve POST data.
2030 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) { 2123 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) {
2031 ASSERT_TRUE(test_server_.Start()); 2124 ASSERT_TRUE(test_server_.Start());
2032 2125
2033 const char kData[] = "hello world"; 2126 const char kData[] = "hello world";
2034 2127
2035 TestDelegate d; 2128 TestDelegate d;
2036 BlockingNetworkDelegate network_delegate; 2129 BlockingNetworkDelegate network_delegate(
2130 BlockingNetworkDelegate::AUTO_CALLBACK);
2131 network_delegate.BlockOn(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
2037 GURL redirect_url(test_server_.GetURL("echo")); 2132 GURL redirect_url(test_server_.GetURL("echo"));
2038 network_delegate.set_redirect_url(redirect_url); 2133 network_delegate.set_redirect_url(redirect_url);
2039 2134
2040 TestURLRequestContext context(true); 2135 TestURLRequestContext context(true);
2041 context.set_network_delegate(&network_delegate); 2136 context.set_network_delegate(&network_delegate);
2042 context.Init(); 2137 context.Init();
2043 2138
2044 { 2139 {
2045 GURL original_url(test_server_.GetURL("empty.html")); 2140 GURL original_url(test_server_.GetURL("empty.html"));
2046 URLRequest r(original_url, &d, &context); 2141 URLRequest r(original_url, &d, &context);
(...skipping 20 matching lines...) Expand all
2067 } 2162 }
2068 2163
2069 // Tests that the network delegate can synchronously complete OnAuthRequired 2164 // Tests that the network delegate can synchronously complete OnAuthRequired
2070 // by taking no action. This indicates that the NetworkDelegate does not want to 2165 // by taking no action. This indicates that the NetworkDelegate does not want to
2071 // handle the challenge, and is passing the buck along to the 2166 // handle the challenge, and is passing the buck along to the
2072 // URLRequest::Delegate. 2167 // URLRequest::Delegate.
2073 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) { 2168 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) {
2074 ASSERT_TRUE(test_server_.Start()); 2169 ASSERT_TRUE(test_server_.Start());
2075 2170
2076 TestDelegate d; 2171 TestDelegate d;
2077 BlockingNetworkDelegate network_delegate; 2172 BlockingNetworkDelegate network_delegate(
2078 network_delegate.set_auth_retval( 2173 BlockingNetworkDelegate::SYNCHRONOUS);
2079 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION); 2174 network_delegate.BlockOn(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
mmenke 2012/09/21 15:15:59 This is fine, though not actually needed.
vabr (Chromium) 2012/09/21 20:03:42 Removed to be consistent with NetworkDelegateRedir
2080 2175
2081 TestURLRequestContext context(true); 2176 TestURLRequestContext context(true);
2082 context.set_network_delegate(&network_delegate); 2177 context.set_network_delegate(&network_delegate);
2083 context.Init(); 2178 context.Init();
2084 2179
2085 d.set_credentials(AuthCredentials(kUser, kSecret)); 2180 d.set_credentials(AuthCredentials(kUser, kSecret));
2086 2181
2087 { 2182 {
2088 GURL url(test_server_.GetURL("auth-basic")); 2183 GURL url(test_server_.GetURL("auth-basic"));
2089 URLRequest r(url, &d, &context); 2184 URLRequest r(url, &d, &context);
2090 r.Start(); 2185 r.Start();
2091 MessageLoop::current()->Run(); 2186 MessageLoop::current()->Run();
2092 2187
2093 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); 2188 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2094 EXPECT_EQ(0, r.status().error()); 2189 EXPECT_EQ(0, r.status().error());
2095 EXPECT_EQ(200, r.GetResponseCode()); 2190 EXPECT_EQ(200, r.GetResponseCode());
2096 EXPECT_TRUE(d.auth_required_called()); 2191 EXPECT_TRUE(d.auth_required_called());
2097 EXPECT_EQ(1, network_delegate.created_requests()); 2192 EXPECT_EQ(1, network_delegate.created_requests());
2098 EXPECT_EQ(0, network_delegate.destroyed_requests()); 2193 EXPECT_EQ(0, network_delegate.destroyed_requests());
2099 } 2194 }
2100 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2195 EXPECT_EQ(1, network_delegate.destroyed_requests());
2101 } 2196 }
2102 2197
2103 // Tests that the network delegate can synchronously complete OnAuthRequired 2198 // Tests that the network delegate can synchronously complete OnAuthRequired
2104 // by setting credentials. 2199 // by setting credentials.
2105 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) { 2200 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) {
2106 ASSERT_TRUE(test_server_.Start()); 2201 ASSERT_TRUE(test_server_.Start());
2107 2202
2108 TestDelegate d; 2203 TestDelegate d;
2109 BlockingNetworkDelegate network_delegate; 2204 BlockingNetworkDelegate network_delegate(
2205 BlockingNetworkDelegate::SYNCHRONOUS);
2206 network_delegate.BlockOn(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
2110 network_delegate.set_auth_retval( 2207 network_delegate.set_auth_retval(
2111 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); 2208 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
2112 2209
2113 network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); 2210 network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret));
2114 2211
2115 TestURLRequestContext context(true); 2212 TestURLRequestContext context(true);
2116 context.set_network_delegate(&network_delegate); 2213 context.set_network_delegate(&network_delegate);
2117 context.Init(); 2214 context.Init();
2118 2215
2119 { 2216 {
(...skipping 11 matching lines...) Expand all
2131 } 2228 }
2132 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2229 EXPECT_EQ(1, network_delegate.destroyed_requests());
2133 } 2230 }
2134 2231
2135 // Tests that the network delegate can synchronously complete OnAuthRequired 2232 // Tests that the network delegate can synchronously complete OnAuthRequired
2136 // by cancelling authentication. 2233 // by cancelling authentication.
2137 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) { 2234 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) {
2138 ASSERT_TRUE(test_server_.Start()); 2235 ASSERT_TRUE(test_server_.Start());
2139 2236
2140 TestDelegate d; 2237 TestDelegate d;
2141 BlockingNetworkDelegate network_delegate; 2238 BlockingNetworkDelegate network_delegate(
2239 BlockingNetworkDelegate::SYNCHRONOUS);
2240 network_delegate.BlockOn(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
2142 network_delegate.set_auth_retval( 2241 network_delegate.set_auth_retval(
2143 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); 2242 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH);
2144 2243
2145 TestURLRequestContext context(true); 2244 TestURLRequestContext context(true);
2146 context.set_network_delegate(&network_delegate); 2245 context.set_network_delegate(&network_delegate);
2147 context.Init(); 2246 context.Init();
2148 2247
2149 { 2248 {
2150 GURL url(test_server_.GetURL("auth-basic")); 2249 GURL url(test_server_.GetURL("auth-basic"));
2151 URLRequest r(url, &d, &context); 2250 URLRequest r(url, &d, &context);
(...skipping 11 matching lines...) Expand all
2163 } 2262 }
2164 2263
2165 // Tests that the network delegate can asynchronously complete OnAuthRequired 2264 // Tests that the network delegate can asynchronously complete OnAuthRequired
2166 // by taking no action. This indicates that the NetworkDelegate does not want 2265 // by taking no action. This indicates that the NetworkDelegate does not want
2167 // to handle the challenge, and is passing the buck along to the 2266 // to handle the challenge, and is passing the buck along to the
2168 // URLRequest::Delegate. 2267 // URLRequest::Delegate.
2169 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) { 2268 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) {
2170 ASSERT_TRUE(test_server_.Start()); 2269 ASSERT_TRUE(test_server_.Start());
2171 2270
2172 TestDelegate d; 2271 TestDelegate d;
2173 BlockingNetworkDelegate network_delegate; 2272 BlockingNetworkDelegate network_delegate(
2174 network_delegate.set_auth_retval( 2273 BlockingNetworkDelegate::AUTO_CALLBACK);
2175 NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING); 2274 network_delegate.BlockOn(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
2176 network_delegate.set_auth_callback_retval(
2177 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION);
2178 2275
2179 TestURLRequestContext context(true); 2276 TestURLRequestContext context(true);
2180 context.set_network_delegate(&network_delegate); 2277 context.set_network_delegate(&network_delegate);
2181 context.Init(); 2278 context.Init();
2182 2279
2183 d.set_credentials(AuthCredentials(kUser, kSecret)); 2280 d.set_credentials(AuthCredentials(kUser, kSecret));
2184 2281
2185 { 2282 {
2186 GURL url(test_server_.GetURL("auth-basic")); 2283 GURL url(test_server_.GetURL("auth-basic"));
2187 URLRequest r(url, &d, &context); 2284 URLRequest r(url, &d, &context);
2188 r.Start(); 2285 r.Start();
2189 MessageLoop::current()->Run(); 2286 MessageLoop::current()->Run();
2190 2287
2191 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); 2288 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2192 EXPECT_EQ(0, r.status().error()); 2289 EXPECT_EQ(0, r.status().error());
2193 EXPECT_EQ(200, r.GetResponseCode()); 2290 EXPECT_EQ(200, r.GetResponseCode());
2194 EXPECT_TRUE(d.auth_required_called()); 2291 EXPECT_TRUE(d.auth_required_called());
2195 EXPECT_EQ(1, network_delegate.created_requests()); 2292 EXPECT_EQ(1, network_delegate.created_requests());
2196 EXPECT_EQ(0, network_delegate.destroyed_requests()); 2293 EXPECT_EQ(0, network_delegate.destroyed_requests());
2197 } 2294 }
2198 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2295 EXPECT_EQ(1, network_delegate.destroyed_requests());
2199 } 2296 }
2200 2297
2201 // Tests that the network delegate can asynchronously complete OnAuthRequired 2298 // Tests that the network delegate can asynchronously complete OnAuthRequired
2202 // by setting credentials. 2299 // by setting credentials.
2203 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) { 2300 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) {
2204 ASSERT_TRUE(test_server_.Start()); 2301 ASSERT_TRUE(test_server_.Start());
2205 2302
2206 TestDelegate d; 2303 TestDelegate d;
2207 BlockingNetworkDelegate network_delegate; 2304 BlockingNetworkDelegate network_delegate(
2305 BlockingNetworkDelegate::AUTO_CALLBACK);
2306 network_delegate.BlockOn(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
2208 network_delegate.set_auth_retval( 2307 network_delegate.set_auth_retval(
2209 NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING);
2210 network_delegate.set_auth_callback_retval(
2211 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); 2308 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
2212 2309
2213 AuthCredentials auth_credentials(kUser, kSecret); 2310 AuthCredentials auth_credentials(kUser, kSecret);
2214 network_delegate.set_auth_credentials(auth_credentials); 2311 network_delegate.set_auth_credentials(auth_credentials);
2215 2312
2216 TestURLRequestContext context(true); 2313 TestURLRequestContext context(true);
2217 context.set_network_delegate(&network_delegate); 2314 context.set_network_delegate(&network_delegate);
2218 context.Init(); 2315 context.Init();
2219 2316
2220 { 2317 {
(...skipping 12 matching lines...) Expand all
2233 } 2330 }
2234 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2331 EXPECT_EQ(1, network_delegate.destroyed_requests());
2235 } 2332 }
2236 2333
2237 // Tests that the network delegate can asynchronously complete OnAuthRequired 2334 // Tests that the network delegate can asynchronously complete OnAuthRequired
2238 // by cancelling authentication. 2335 // by cancelling authentication.
2239 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) { 2336 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) {
2240 ASSERT_TRUE(test_server_.Start()); 2337 ASSERT_TRUE(test_server_.Start());
2241 2338
2242 TestDelegate d; 2339 TestDelegate d;
2243 BlockingNetworkDelegate network_delegate; 2340 BlockingNetworkDelegate network_delegate(
2341 BlockingNetworkDelegate::AUTO_CALLBACK);
2342 network_delegate.BlockOn(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
2244 network_delegate.set_auth_retval( 2343 network_delegate.set_auth_retval(
2245 NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING);
2246 network_delegate.set_auth_callback_retval(
2247 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); 2344 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH);
2248 2345
2249 TestURLRequestContext context(true); 2346 TestURLRequestContext context(true);
2250 context.set_network_delegate(&network_delegate); 2347 context.set_network_delegate(&network_delegate);
2251 context.Init(); 2348 context.Init();
2252 2349
2253 { 2350 {
2254 GURL url(test_server_.GetURL("auth-basic")); 2351 GURL url(test_server_.GetURL("auth-basic"));
2255 URLRequest r(url, &d, &context); 2352 URLRequest r(url, &d, &context);
2256 r.Start(); 2353 r.Start();
2257 MessageLoop::current()->Run(); 2354 MessageLoop::current()->Run();
2258 2355
2259 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); 2356 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2260 EXPECT_EQ(OK, r.status().error()); 2357 EXPECT_EQ(OK, r.status().error());
2261 EXPECT_EQ(401, r.GetResponseCode()); 2358 EXPECT_EQ(401, r.GetResponseCode());
2262 EXPECT_FALSE(d.auth_required_called()); 2359 EXPECT_FALSE(d.auth_required_called());
2263 EXPECT_EQ(1, network_delegate.created_requests()); 2360 EXPECT_EQ(1, network_delegate.created_requests());
2264 EXPECT_EQ(0, network_delegate.destroyed_requests()); 2361 EXPECT_EQ(0, network_delegate.destroyed_requests());
2265 } 2362 }
2266 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2363 EXPECT_EQ(1, network_delegate.destroyed_requests());
2267 } 2364 }
2268 2365
2269 // Tests that we can handle when a network request was canceled while we were 2366 // Tests that we can handle when a network request was canceled while we were
2270 // waiting for the network delegate. 2367 // waiting for the network delegate.
2271 // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback. 2368 // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback.
2272 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) { 2369 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) {
2273 ASSERT_TRUE(test_server_.Start()); 2370 ASSERT_TRUE(test_server_.Start());
2274 2371
2275 TestDelegate d; 2372 TestDelegate d;
2276 BlockingNetworkDelegateWithManualCallback network_delegate; 2373 BlockingNetworkDelegate network_delegate(
2277 network_delegate.BlockOn( 2374 BlockingNetworkDelegate::USER_CALLBACK);
2278 BlockingNetworkDelegateWithManualCallback::ON_BEFORE_URL_REQUEST); 2375 network_delegate.BlockOn(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
2279 2376
2280 TestURLRequestContext context(true); 2377 TestURLRequestContext context(true);
2281 context.set_network_delegate(&network_delegate); 2378 context.set_network_delegate(&network_delegate);
2282 context.Init(); 2379 context.Init();
2283 2380
2284 { 2381 {
2285 URLRequest r(test_server_.GetURL(""), &d, &context); 2382 URLRequest r(test_server_.GetURL(""), &d, &context);
2286 2383
2287 r.Start(); 2384 r.Start();
2288 network_delegate.WaitForState( 2385 MessageLoop::current()->Run();
2289 BlockingNetworkDelegateWithManualCallback::ON_BEFORE_URL_REQUEST); 2386 EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
2387 network_delegate.stage_blocked_for_callback());
2290 EXPECT_EQ(0, network_delegate.completed_requests()); 2388 EXPECT_EQ(0, network_delegate.completed_requests());
2291 // Cancel before callback. 2389 // Cancel before callback.
2292 r.Cancel(); 2390 r.Cancel();
2293 // Ensure that network delegate is notified. 2391 // Ensure that network delegate is notified.
2294 EXPECT_EQ(1, network_delegate.completed_requests()); 2392 EXPECT_EQ(1, network_delegate.completed_requests());
2295 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); 2393 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
2296 EXPECT_EQ(ERR_ABORTED, r.status().error()); 2394 EXPECT_EQ(ERR_ABORTED, r.status().error());
2297 EXPECT_EQ(1, network_delegate.created_requests()); 2395 EXPECT_EQ(1, network_delegate.created_requests());
2298 EXPECT_EQ(0, network_delegate.destroyed_requests()); 2396 EXPECT_EQ(0, network_delegate.destroyed_requests());
2299 } 2397 }
2300 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2398 EXPECT_EQ(1, network_delegate.destroyed_requests());
2301 } 2399 }
2302 2400
2303 // Tests that we can handle when a network request was canceled while we were 2401 // Tests that we can handle when a network request was canceled while we were
2304 // waiting for the network delegate. 2402 // waiting for the network delegate.
2305 // Part 2: Request is cancelled while waiting for OnBeforeSendHeaders callback. 2403 // Part 2: Request is cancelled while waiting for OnBeforeSendHeaders callback.
2306 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) { 2404 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) {
2307 ASSERT_TRUE(test_server_.Start()); 2405 ASSERT_TRUE(test_server_.Start());
2308 2406
2309 TestDelegate d; 2407 TestDelegate d;
2310 BlockingNetworkDelegateWithManualCallback network_delegate; 2408 BlockingNetworkDelegate network_delegate(
2311 network_delegate.BlockOn( 2409 BlockingNetworkDelegate::USER_CALLBACK);
2312 BlockingNetworkDelegateWithManualCallback::ON_BEFORE_SEND_HEADERS); 2410 network_delegate.BlockOn(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS);
2313 2411
2314 TestURLRequestContext context(true); 2412 TestURLRequestContext context(true);
2315 context.set_network_delegate(&network_delegate); 2413 context.set_network_delegate(&network_delegate);
2316 context.Init(); 2414 context.Init();
2317 2415
2318 { 2416 {
2319 URLRequest r(test_server_.GetURL(""), &d, &context); 2417 URLRequest r(test_server_.GetURL(""), &d, &context);
2320 2418
2321 r.Start(); 2419 r.Start();
2322 network_delegate.WaitForState( 2420 MessageLoop::current()->Run();
2323 BlockingNetworkDelegateWithManualCallback::ON_BEFORE_SEND_HEADERS); 2421 EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
2422 network_delegate.stage_blocked_for_callback());
2324 EXPECT_EQ(0, network_delegate.completed_requests()); 2423 EXPECT_EQ(0, network_delegate.completed_requests());
2325 // Cancel before callback. 2424 // Cancel before callback.
2326 r.Cancel(); 2425 r.Cancel();
2327 // Ensure that network delegate is notified. 2426 // Ensure that network delegate is notified.
2328 EXPECT_EQ(1, network_delegate.completed_requests()); 2427 EXPECT_EQ(1, network_delegate.completed_requests());
2329 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); 2428 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
2330 EXPECT_EQ(ERR_ABORTED, r.status().error()); 2429 EXPECT_EQ(ERR_ABORTED, r.status().error());
2331 EXPECT_EQ(1, network_delegate.created_requests()); 2430 EXPECT_EQ(1, network_delegate.created_requests());
2332 EXPECT_EQ(0, network_delegate.destroyed_requests()); 2431 EXPECT_EQ(0, network_delegate.destroyed_requests());
2333 } 2432 }
2334 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2433 EXPECT_EQ(1, network_delegate.destroyed_requests());
2335 } 2434 }
2336 2435
2337 // Tests that we can handle when a network request was canceled while we were 2436 // Tests that we can handle when a network request was canceled while we were
2338 // waiting for the network delegate. 2437 // waiting for the network delegate.
2339 // Part 3: Request is cancelled while waiting for OnHeadersReceived callback. 2438 // Part 3: Request is cancelled while waiting for OnHeadersReceived callback.
2340 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) { 2439 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) {
2341 ASSERT_TRUE(test_server_.Start()); 2440 ASSERT_TRUE(test_server_.Start());
2342 2441
2343 TestDelegate d; 2442 TestDelegate d;
2344 BlockingNetworkDelegateWithManualCallback network_delegate; 2443 BlockingNetworkDelegate network_delegate(
2345 network_delegate.BlockOn( 2444 BlockingNetworkDelegate::USER_CALLBACK);
2346 BlockingNetworkDelegateWithManualCallback::ON_HEADERS_RECEIVED); 2445 network_delegate.BlockOn(BlockingNetworkDelegate::ON_HEADERS_RECEIVED);
2347 2446
2348 TestURLRequestContext context(true); 2447 TestURLRequestContext context(true);
2349 context.set_network_delegate(&network_delegate); 2448 context.set_network_delegate(&network_delegate);
2350 context.Init(); 2449 context.Init();
2351 2450
2352 { 2451 {
2353 URLRequest r(test_server_.GetURL(""), &d, &context); 2452 URLRequest r(test_server_.GetURL(""), &d, &context);
2354 2453
2355 r.Start(); 2454 r.Start();
2356 network_delegate.WaitForState( 2455 MessageLoop::current()->Run();
2357 BlockingNetworkDelegateWithManualCallback::ON_HEADERS_RECEIVED); 2456 EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED,
2457 network_delegate.stage_blocked_for_callback());
2358 EXPECT_EQ(0, network_delegate.completed_requests()); 2458 EXPECT_EQ(0, network_delegate.completed_requests());
2359 // Cancel before callback. 2459 // Cancel before callback.
2360 r.Cancel(); 2460 r.Cancel();
2361 // Ensure that network delegate is notified. 2461 // Ensure that network delegate is notified.
2362 EXPECT_EQ(1, network_delegate.completed_requests()); 2462 EXPECT_EQ(1, network_delegate.completed_requests());
2363 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); 2463 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
2364 EXPECT_EQ(ERR_ABORTED, r.status().error()); 2464 EXPECT_EQ(ERR_ABORTED, r.status().error());
2365 EXPECT_EQ(1, network_delegate.created_requests()); 2465 EXPECT_EQ(1, network_delegate.created_requests());
2366 EXPECT_EQ(0, network_delegate.destroyed_requests()); 2466 EXPECT_EQ(0, network_delegate.destroyed_requests());
2367 } 2467 }
2368 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2468 EXPECT_EQ(1, network_delegate.destroyed_requests());
2369 } 2469 }
2370 2470
2371 // Tests that we can handle when a network request was canceled while we were 2471 // Tests that we can handle when a network request was canceled while we were
2372 // waiting for the network delegate. 2472 // waiting for the network delegate.
2373 // Part 4: Request is cancelled while waiting for OnAuthRequired callback. 2473 // Part 4: Request is cancelled while waiting for OnAuthRequired callback.
2374 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) { 2474 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) {
2375 ASSERT_TRUE(test_server_.Start()); 2475 ASSERT_TRUE(test_server_.Start());
2376 2476
2377 TestDelegate d; 2477 TestDelegate d;
2378 BlockingNetworkDelegateWithManualCallback network_delegate; 2478 BlockingNetworkDelegate network_delegate(
2379 network_delegate.BlockOn( 2479 BlockingNetworkDelegate::USER_CALLBACK);
2380 BlockingNetworkDelegateWithManualCallback::ON_AUTH_REQUIRED); 2480 network_delegate.BlockOn(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
2381 2481
2382 TestURLRequestContext context(true); 2482 TestURLRequestContext context(true);
2383 context.set_network_delegate(&network_delegate); 2483 context.set_network_delegate(&network_delegate);
2384 context.Init(); 2484 context.Init();
2385 2485
2386 { 2486 {
2387 URLRequest r(test_server_.GetURL("auth-basic"), &d, &context); 2487 URLRequest r(test_server_.GetURL("auth-basic"), &d, &context);
2388 2488
2389 r.Start(); 2489 r.Start();
2390 network_delegate.WaitForState( 2490 MessageLoop::current()->Run();
2391 BlockingNetworkDelegateWithManualCallback::ON_AUTH_REQUIRED); 2491 EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED,
2492 network_delegate.stage_blocked_for_callback());
2392 EXPECT_EQ(0, network_delegate.completed_requests()); 2493 EXPECT_EQ(0, network_delegate.completed_requests());
2393 // Cancel before callback. 2494 // Cancel before callback.
2394 r.Cancel(); 2495 r.Cancel();
2395 // Ensure that network delegate is notified. 2496 // Ensure that network delegate is notified.
2396 EXPECT_EQ(1, network_delegate.completed_requests()); 2497 EXPECT_EQ(1, network_delegate.completed_requests());
2397 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); 2498 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
2398 EXPECT_EQ(ERR_ABORTED, r.status().error()); 2499 EXPECT_EQ(ERR_ABORTED, r.status().error());
2399 EXPECT_EQ(1, network_delegate.created_requests()); 2500 EXPECT_EQ(1, network_delegate.created_requests());
2400 EXPECT_EQ(0, network_delegate.destroyed_requests()); 2501 EXPECT_EQ(0, network_delegate.destroyed_requests());
2401 } 2502 }
(...skipping 2249 matching lines...) Expand 10 before | Expand all | Expand 10 after
4651 4752
4652 EXPECT_FALSE(r.is_pending()); 4753 EXPECT_FALSE(r.is_pending());
4653 EXPECT_EQ(1, d->response_started_count()); 4754 EXPECT_EQ(1, d->response_started_count());
4654 EXPECT_FALSE(d->received_data_before_response()); 4755 EXPECT_FALSE(d->received_data_before_response());
4655 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); 4756 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
4656 } 4757 }
4657 } 4758 }
4658 #endif // !defined(DISABLE_FTP_SUPPORT) 4759 #endif // !defined(DISABLE_FTP_SUPPORT)
4659 4760
4660 } // namespace net 4761 } // namespace net
OLDNEW
« no previous file with comments | « 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