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

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: Disabling NetworkDelegateBlockAsynchronously in CF Created 8 years, 2 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. In every blocking stage the
264 } 178 // message loop is quit.
265 179 };
266 void DoCallback(int rv) { 180
267 ASSERT_NE(NOT_BLOCKED, state_); 181 // Creates a delegate which does not block at all.
268 CompletionCallback callback = callback_; 182 explicit BlockingNetworkDelegate(BlockMode block_mode);
269 Reset(); 183
270 callback.Run(rv); 184 // For users to trigger a callback returning |response|.
271 } 185 // Side-effects: resets |stage_blocked_for_callback_| and stored callbacks.
272 186 // Only call if |block_mode_| == USER_CALLBACK.
273 void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response) { 187 void DoCallback(int response);
274 ASSERT_EQ(ON_AUTH_REQUIRED, state_); 188 void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response);
275 AuthCallback auth_callback = auth_callback_; 189
276 Reset(); 190 // Setters.
277 auth_callback.Run(response); 191 void set_retval(int retval) {
278 } 192 ASSERT_NE(USER_CALLBACK, block_mode_);
279 193 ASSERT_NE(ERR_IO_PENDING, retval);
280 // Runs the message loop until |state| is reached. 194 ASSERT_NE(OK, retval);
281 void WaitForState(State state) { 195 retval_ = retval;
282 while (state_ != state) 196 }
283 MessageLoop::current()->RunAllPending(); 197
198 // If |auth_retval| == AUTH_REQUIRED_RESPONSE_SET_AUTH, then
199 // |auth_credentials_| will be passed with the response.
200 void set_auth_retval(AuthRequiredResponse auth_retval) {
201 ASSERT_NE(USER_CALLBACK, block_mode_);
202 ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, auth_retval);
203 auth_retval_ = auth_retval;
204 }
205 void set_auth_credentials(const AuthCredentials& auth_credentials) {
206 auth_credentials_ = auth_credentials;
207 }
208
209 void set_redirect_url(const GURL& url) {
210 redirect_url_ = url;
211 }
212
213 void set_block_on(int block_on) {
214 block_on_ = block_on;
215 }
216
217 // Allows the user to check in which state did we block.
218 Stage stage_blocked_for_callback() const {
219 EXPECT_EQ(USER_CALLBACK, block_mode_);
220 return stage_blocked_for_callback_;
284 } 221 }
285 222
286 private: 223 private:
224 void RunCallback(int response, const CompletionCallback& callback);
225 void RunAuthCallback(AuthRequiredResponse response,
226 const AuthCallback& callback);
227
287 // TestNetworkDelegate implementation. 228 // TestNetworkDelegate implementation.
288 virtual int OnBeforeURLRequest(URLRequest* request, 229 virtual int OnBeforeURLRequest(URLRequest* request,
289 const CompletionCallback& callback, 230 const CompletionCallback& callback,
290 GURL* new_url) OVERRIDE { 231 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 232
301 virtual int OnBeforeSendHeaders(URLRequest* request, 233 virtual int OnBeforeSendHeaders(URLRequest* request,
302 const CompletionCallback& callback, 234 const CompletionCallback& callback,
303 HttpRequestHeaders* headers) OVERRIDE { 235 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 236
314 virtual int OnHeadersReceived( 237 virtual int OnHeadersReceived(
315 URLRequest* request, 238 URLRequest* request,
316 const CompletionCallback& callback, 239 const CompletionCallback& callback,
317 HttpResponseHeaders* original_response_headers, 240 HttpResponseHeaders* original_response_headers,
318 scoped_refptr<HttpResponseHeaders>* override_response_headers) 241 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 242
332 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired( 243 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired(
333 URLRequest* request, 244 URLRequest* request,
334 const AuthChallengeInfo& auth_info, 245 const AuthChallengeInfo& auth_info,
335 const AuthCallback& callback, 246 const AuthCallback& callback,
336 AuthCredentials* credentials) OVERRIDE { 247 AuthCredentials* credentials) OVERRIDE;
337 if ((block_on_ & ON_AUTH_REQUIRED) == 0) { 248
338 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION; 249 // Resets the callbacks and |stage_blocked_for_callback_|.
339 } else { 250 void Reset();
340 state_ = ON_AUTH_REQUIRED; 251
341 auth_callback_ = callback; 252 // Checks whether we should block in |stage|. If yes, returns an error code
342 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING; 253 // and optionally sets up callback based on |block_mode_|. If no, returns OK.
343 } 254 int MaybeBlockStage(Stage stage, const CompletionCallback& callback);
344 } 255
345 256 // Configuration parameters, can be adjusted by public methods:
346 void Reset() { 257 const BlockMode block_mode_;
347 state_ = NOT_BLOCKED; 258
348 callback_.Reset(); 259 // Values returned on blocking stages when mode is SYNCHRONOUS or
349 auth_callback_.Reset(); 260 // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING.
350 } 261 int retval_; // To be returned in non-auth stages.
351 262 AuthRequiredResponse auth_retval_;
352 int block_on_; // Bit mask on which states to block. 263
353 State state_; 264 GURL redirect_url_; // Used if non-empty.
265 int block_on_; // Bit mask: in which stages to block.
266
267 // |auth_credentials_| will be copied to |*target_auth_credential_| on
268 // callback.
269 AuthCredentials auth_credentials_;
270 AuthCredentials* target_auth_credentials_;
271
272 // Internal variables, not set by not the user:
273 // Last blocked stage waiting for user callback (unused if |block_mode_| !=
274 // USER_CALLBACK).
275 Stage stage_blocked_for_callback_;
276
277 // Callback objects stored during blocking stages.
354 CompletionCallback callback_; 278 CompletionCallback callback_;
355 AuthCallback auth_callback_; 279 AuthCallback auth_callback_;
280
281 base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_;
282
283 DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate);
356 }; 284 };
357 285
286 BlockingNetworkDelegate::BlockingNetworkDelegate(BlockMode block_mode)
287 : block_mode_(block_mode),
288 retval_(OK),
289 auth_retval_(AUTH_REQUIRED_RESPONSE_NO_ACTION),
290 block_on_(0),
291 target_auth_credentials_(NULL),
292 stage_blocked_for_callback_(NOT_BLOCKED),
293 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
294 }
295
296 void BlockingNetworkDelegate::DoCallback(int response) {
297 ASSERT_EQ(USER_CALLBACK, block_mode_);
298 ASSERT_NE(NOT_BLOCKED, stage_blocked_for_callback_);
299 ASSERT_NE(ON_AUTH_REQUIRED, stage_blocked_for_callback_);
300 CompletionCallback callback = callback_;
301 Reset();
302 RunCallback(response, callback);
303 }
304
305 void BlockingNetworkDelegate::DoAuthCallback(
306 NetworkDelegate::AuthRequiredResponse response) {
307 ASSERT_EQ(USER_CALLBACK, block_mode_);
308 ASSERT_EQ(ON_AUTH_REQUIRED, stage_blocked_for_callback_);
309 AuthCallback auth_callback = auth_callback_;
310 Reset();
311 RunAuthCallback(response, auth_callback);
312 }
313
314 void BlockingNetworkDelegate::RunCallback(int response,
315 const CompletionCallback& callback) {
316 callback.Run(response);
317 }
318
319 void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response,
320 const AuthCallback& callback) {
321 if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) {
322 ASSERT_TRUE(target_auth_credentials_ != NULL);
323 *target_auth_credentials_ = auth_credentials_;
324 }
325 callback.Run(response);
326 }
327
328 int BlockingNetworkDelegate::OnBeforeURLRequest(
329 URLRequest* request,
330 const CompletionCallback& callback,
331 GURL* new_url) {
332 if (redirect_url_ == request->url())
333 return OK; // We've already seen this request and redirected elsewhere.
334
335 TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url);
336
337 if (!redirect_url_.is_empty())
338 *new_url = redirect_url_;
339
340 return MaybeBlockStage(ON_BEFORE_URL_REQUEST, callback);
341 }
342
343 int BlockingNetworkDelegate::OnBeforeSendHeaders(
344 URLRequest* request,
345 const CompletionCallback& callback,
346 HttpRequestHeaders* headers) {
347 TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers);
348
349 return MaybeBlockStage(ON_BEFORE_SEND_HEADERS, callback);
350 }
351
352 int BlockingNetworkDelegate::OnHeadersReceived(
353 URLRequest* request,
354 const CompletionCallback& callback,
355 HttpResponseHeaders* original_response_headers,
356 scoped_refptr<HttpResponseHeaders>* override_response_headers) {
357 TestNetworkDelegate::OnHeadersReceived(
358 request, callback, original_response_headers,
359 override_response_headers);
360
361 return MaybeBlockStage(ON_HEADERS_RECEIVED, callback);
362 }
363
364 NetworkDelegate::AuthRequiredResponse BlockingNetworkDelegate::OnAuthRequired(
365 URLRequest* request,
366 const AuthChallengeInfo& auth_info,
367 const AuthCallback& callback,
368 AuthCredentials* credentials) {
369 TestNetworkDelegate::OnAuthRequired(request, auth_info, callback,
370 credentials);
371 // Check that the user has provided callback for the previous blocked stage.
372 EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_);
373
374 if ((block_on_ & ON_AUTH_REQUIRED) == 0) {
375 return AUTH_REQUIRED_RESPONSE_NO_ACTION;
376 }
377
378 target_auth_credentials_ = credentials;
379
380 switch (block_mode_) {
381 case SYNCHRONOUS:
382 if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH)
383 *target_auth_credentials_ = auth_credentials_;
384 return auth_retval_;
385
386 case AUTO_CALLBACK:
387 MessageLoop::current()->PostTask(
388 FROM_HERE,
389 base::Bind(&BlockingNetworkDelegate::RunAuthCallback,
390 weak_factory_.GetWeakPtr(), auth_retval_, callback));
391 return AUTH_REQUIRED_RESPONSE_IO_PENDING;
392
393 case USER_CALLBACK:
394 auth_callback_ = callback;
395 stage_blocked_for_callback_ = ON_AUTH_REQUIRED;
396 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
397 return AUTH_REQUIRED_RESPONSE_IO_PENDING;
398 }
399 NOTREACHED();
400 return AUTH_REQUIRED_RESPONSE_NO_ACTION; // Dummy value.
401 }
402
403 void BlockingNetworkDelegate::Reset() {
404 EXPECT_NE(NOT_BLOCKED, stage_blocked_for_callback_);
405 stage_blocked_for_callback_ = NOT_BLOCKED;
406 callback_.Reset();
407 auth_callback_.Reset();
408 }
409
410 int BlockingNetworkDelegate::MaybeBlockStage(
411 BlockingNetworkDelegate::Stage stage,
412 const CompletionCallback& callback) {
413 // Check that the user has provided callback for the previous blocked stage.
414 EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_);
415
416 if ((block_on_ & stage) == 0) {
417 return OK;
418 }
419
420 switch (block_mode_) {
421 case SYNCHRONOUS:
422 EXPECT_NE(OK, retval_);
423 return retval_;
424
425 case AUTO_CALLBACK:
426 MessageLoop::current()->PostTask(
427 FROM_HERE,
428 base::Bind(&BlockingNetworkDelegate::RunCallback,
429 weak_factory_.GetWeakPtr(), retval_, callback));
430 return ERR_IO_PENDING;
431
432 case USER_CALLBACK:
433 callback_ = callback;
434 stage_blocked_for_callback_ = stage;
435 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
436 return ERR_IO_PENDING;
437 }
438 NOTREACHED();
439 return 0;
440 }
441
358 class TestURLRequestContextWithProxy : public TestURLRequestContext { 442 class TestURLRequestContextWithProxy : public TestURLRequestContext {
359 public: 443 public:
360 // Does not own |delegate|. 444 // Does not own |delegate|.
361 TestURLRequestContextWithProxy(const std::string& proxy, 445 TestURLRequestContextWithProxy(const std::string& proxy,
362 NetworkDelegate* delegate) 446 NetworkDelegate* delegate)
363 : TestURLRequestContext(true) { 447 : TestURLRequestContext(true) {
364 context_storage_.set_proxy_service(ProxyService::CreateFixed(proxy)); 448 context_storage_.set_proxy_service(ProxyService::CreateFixed(proxy));
365 set_network_delegate(delegate); 449 set_network_delegate(delegate);
366 Init(); 450 Init();
367 } 451 }
(...skipping 1534 matching lines...) Expand 10 before | Expand all | Expand 10 after
1902 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().error()); 1986 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().error());
1903 EXPECT_EQ(1, d.response_started_count()); 1987 EXPECT_EQ(1, d.response_started_count());
1904 // We should not have followed the redirect. 1988 // We should not have followed the redirect.
1905 EXPECT_EQ(0, d.received_redirect_count()); 1989 EXPECT_EQ(0, d.received_redirect_count());
1906 1990
1907 EXPECT_EQ(1, network_delegate.error_count()); 1991 EXPECT_EQ(1, network_delegate.error_count());
1908 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, network_delegate.last_error()); 1992 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, network_delegate.last_error());
1909 } 1993 }
1910 } 1994 }
1911 1995
1996 // Tests that we can block and asynchronously return OK in various stages.
1997 TEST_F(URLRequestTestHTTP, NetworkDelegateBlockAsynchronously) {
1998 static const BlockingNetworkDelegate::Stage blocking_stages[] = {
1999 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
2000 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
2001 BlockingNetworkDelegate::ON_HEADERS_RECEIVED
2002 };
2003 static const size_t blocking_stages_length = arraysize(blocking_stages);
2004
2005 ASSERT_TRUE(test_server_.Start());
2006
2007 TestDelegate d;
2008 BlockingNetworkDelegate network_delegate(
2009 BlockingNetworkDelegate::USER_CALLBACK);
2010 network_delegate.set_block_on(
2011 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST |
2012 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS |
2013 BlockingNetworkDelegate::ON_HEADERS_RECEIVED);
2014
2015 TestURLRequestContext context(true);
2016 context.set_network_delegate(&network_delegate);
2017 context.Init();
2018
2019 {
2020 URLRequest r(test_server_.GetURL("empty.html"), &d, &context);
2021
2022 r.Start();
2023 for (size_t i = 0; i < blocking_stages_length; ++i) {
2024 MessageLoop::current()->Run();
2025 EXPECT_EQ(blocking_stages[i],
2026 network_delegate.stage_blocked_for_callback());
2027 network_delegate.DoCallback(OK);
2028 }
2029 MessageLoop::current()->Run();
2030 EXPECT_EQ(200, r.GetResponseCode());
2031 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2032 EXPECT_EQ(1, network_delegate.created_requests());
2033 EXPECT_EQ(0, network_delegate.destroyed_requests());
2034 }
2035 EXPECT_EQ(1, network_delegate.destroyed_requests());
2036 }
2037
1912 // Tests that the network delegate can block and cancel a request. 2038 // Tests that the network delegate can block and cancel a request.
1913 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) { 2039 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) {
1914 ASSERT_TRUE(test_server_.Start()); 2040 ASSERT_TRUE(test_server_.Start());
1915 2041
1916 TestDelegate d; 2042 TestDelegate d;
1917 BlockingNetworkDelegate network_delegate; 2043 BlockingNetworkDelegate network_delegate(
1918 network_delegate.set_callback_retval(ERR_EMPTY_RESPONSE); 2044 BlockingNetworkDelegate::AUTO_CALLBACK);
2045 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
2046 network_delegate.set_retval(ERR_EMPTY_RESPONSE);
1919 2047
1920 TestURLRequestContextWithProxy context( 2048 TestURLRequestContextWithProxy context(
1921 test_server_.host_port_pair().ToString(), 2049 test_server_.host_port_pair().ToString(),
1922 &network_delegate); 2050 &network_delegate);
1923 2051
1924 { 2052 {
1925 URLRequest r(test_server_.GetURL(""), &d, &context); 2053 URLRequest r(test_server_.GetURL(""), &d, &context);
1926 2054
1927 r.Start(); 2055 r.Start();
1928 MessageLoop::current()->Run(); 2056 MessageLoop::current()->Run();
1929 2057
1930 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); 2058 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
1931 EXPECT_EQ(ERR_EMPTY_RESPONSE, r.status().error()); 2059 EXPECT_EQ(ERR_EMPTY_RESPONSE, r.status().error());
1932 EXPECT_EQ(1, network_delegate.created_requests()); 2060 EXPECT_EQ(1, network_delegate.created_requests());
1933 EXPECT_EQ(0, network_delegate.destroyed_requests()); 2061 EXPECT_EQ(0, network_delegate.destroyed_requests());
1934 } 2062 }
1935 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2063 EXPECT_EQ(1, network_delegate.destroyed_requests());
1936 } 2064 }
1937 2065
1938 // Tests that the network delegate can cancel a request synchronously. 2066 // Tests that the network delegate can cancel a request synchronously.
1939 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously) { 2067 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously) {
1940 ASSERT_TRUE(test_server_.Start()); 2068 ASSERT_TRUE(test_server_.Start());
1941 2069
1942 TestDelegate d; 2070 TestDelegate d;
1943 BlockingNetworkDelegate network_delegate; 2071 BlockingNetworkDelegate network_delegate(
2072 BlockingNetworkDelegate::SYNCHRONOUS);
2073 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
1944 network_delegate.set_retval(ERR_EMPTY_RESPONSE); 2074 network_delegate.set_retval(ERR_EMPTY_RESPONSE);
1945 2075
1946 TestURLRequestContextWithProxy context( 2076 TestURLRequestContextWithProxy context(
1947 test_server_.host_port_pair().ToString(), 2077 test_server_.host_port_pair().ToString(),
1948 &network_delegate); 2078 &network_delegate);
1949 2079
1950 { 2080 {
1951 URLRequest r(test_server_.GetURL(""), &d, &context); 2081 URLRequest r(test_server_.GetURL(""), &d, &context);
1952 2082
1953 r.Start(); 2083 r.Start();
1954 MessageLoop::current()->Run(); 2084 MessageLoop::current()->Run();
1955 2085
1956 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); 2086 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
1957 EXPECT_EQ(ERR_EMPTY_RESPONSE, r.status().error()); 2087 EXPECT_EQ(ERR_EMPTY_RESPONSE, r.status().error());
1958 EXPECT_EQ(1, network_delegate.created_requests()); 2088 EXPECT_EQ(1, network_delegate.created_requests());
1959 EXPECT_EQ(0, network_delegate.destroyed_requests()); 2089 EXPECT_EQ(0, network_delegate.destroyed_requests());
1960 } 2090 }
1961 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2091 EXPECT_EQ(1, network_delegate.destroyed_requests());
1962 } 2092 }
1963 2093
1964 // Tests that the network delegate can block and redirect a request to a new 2094 // Tests that the network delegate can block and redirect a request to a new
1965 // URL. 2095 // URL.
1966 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) { 2096 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) {
1967 ASSERT_TRUE(test_server_.Start()); 2097 ASSERT_TRUE(test_server_.Start());
1968 2098
1969 TestDelegate d; 2099 TestDelegate d;
1970 BlockingNetworkDelegate network_delegate; 2100 BlockingNetworkDelegate network_delegate(
2101 BlockingNetworkDelegate::AUTO_CALLBACK);
2102 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
1971 GURL redirect_url(test_server_.GetURL("simple.html")); 2103 GURL redirect_url(test_server_.GetURL("simple.html"));
1972 network_delegate.set_redirect_url(redirect_url); 2104 network_delegate.set_redirect_url(redirect_url);
1973 2105
1974 TestURLRequestContextWithProxy context( 2106 TestURLRequestContextWithProxy context(
1975 test_server_.host_port_pair().ToString(), 2107 test_server_.host_port_pair().ToString(),
1976 &network_delegate); 2108 &network_delegate);
1977 2109
1978 { 2110 {
1979 GURL original_url(test_server_.GetURL("empty.html")); 2111 GURL original_url(test_server_.GetURL("empty.html"));
1980 URLRequest r(original_url, &d, &context); 2112 URLRequest r(original_url, &d, &context);
(...skipping 11 matching lines...) Expand all
1992 } 2124 }
1993 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2125 EXPECT_EQ(1, network_delegate.destroyed_requests());
1994 } 2126 }
1995 2127
1996 // Tests that the network delegate can block and redirect a request to a new 2128 // 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. 2129 // URL by setting a redirect_url and returning in OnBeforeURLRequest directly.
1998 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) { 2130 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) {
1999 ASSERT_TRUE(test_server_.Start()); 2131 ASSERT_TRUE(test_server_.Start());
2000 2132
2001 TestDelegate d; 2133 TestDelegate d;
2002 BlockingNetworkDelegate network_delegate; 2134 BlockingNetworkDelegate network_delegate(
2135 BlockingNetworkDelegate::SYNCHRONOUS);
2003 GURL redirect_url(test_server_.GetURL("simple.html")); 2136 GURL redirect_url(test_server_.GetURL("simple.html"));
2004 network_delegate.set_redirect_url(redirect_url); 2137 network_delegate.set_redirect_url(redirect_url);
2005 network_delegate.set_retval(OK);
2006 2138
2007 TestURLRequestContextWithProxy context( 2139 TestURLRequestContextWithProxy context(
2008 test_server_.host_port_pair().ToString(), 2140 test_server_.host_port_pair().ToString(),
2009 &network_delegate); 2141 &network_delegate);
2010 2142
2011 { 2143 {
2012 GURL original_url(test_server_.GetURL("empty.html")); 2144 GURL original_url(test_server_.GetURL("empty.html"));
2013 URLRequest r(original_url, &d, &context); 2145 URLRequest r(original_url, &d, &context);
2014 2146
2015 r.Start(); 2147 r.Start();
(...skipping 10 matching lines...) Expand all
2026 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2158 EXPECT_EQ(1, network_delegate.destroyed_requests());
2027 } 2159 }
2028 2160
2029 // Tests that redirects caused by the network delegate preserve POST data. 2161 // Tests that redirects caused by the network delegate preserve POST data.
2030 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) { 2162 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) {
2031 ASSERT_TRUE(test_server_.Start()); 2163 ASSERT_TRUE(test_server_.Start());
2032 2164
2033 const char kData[] = "hello world"; 2165 const char kData[] = "hello world";
2034 2166
2035 TestDelegate d; 2167 TestDelegate d;
2036 BlockingNetworkDelegate network_delegate; 2168 BlockingNetworkDelegate network_delegate(
2169 BlockingNetworkDelegate::AUTO_CALLBACK);
2170 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
2037 GURL redirect_url(test_server_.GetURL("echo")); 2171 GURL redirect_url(test_server_.GetURL("echo"));
2038 network_delegate.set_redirect_url(redirect_url); 2172 network_delegate.set_redirect_url(redirect_url);
2039 2173
2040 TestURLRequestContext context(true); 2174 TestURLRequestContext context(true);
2041 context.set_network_delegate(&network_delegate); 2175 context.set_network_delegate(&network_delegate);
2042 context.Init(); 2176 context.Init();
2043 2177
2044 { 2178 {
2045 GURL original_url(test_server_.GetURL("empty.html")); 2179 GURL original_url(test_server_.GetURL("empty.html"));
2046 URLRequest r(original_url, &d, &context); 2180 URLRequest r(original_url, &d, &context);
(...skipping 20 matching lines...) Expand all
2067 } 2201 }
2068 2202
2069 // Tests that the network delegate can synchronously complete OnAuthRequired 2203 // Tests that the network delegate can synchronously complete OnAuthRequired
2070 // by taking no action. This indicates that the NetworkDelegate does not want to 2204 // 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 2205 // handle the challenge, and is passing the buck along to the
2072 // URLRequest::Delegate. 2206 // URLRequest::Delegate.
2073 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) { 2207 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) {
2074 ASSERT_TRUE(test_server_.Start()); 2208 ASSERT_TRUE(test_server_.Start());
2075 2209
2076 TestDelegate d; 2210 TestDelegate d;
2077 BlockingNetworkDelegate network_delegate; 2211 BlockingNetworkDelegate network_delegate(
2078 network_delegate.set_auth_retval( 2212 BlockingNetworkDelegate::SYNCHRONOUS);
2079 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION);
2080 2213
2081 TestURLRequestContext context(true); 2214 TestURLRequestContext context(true);
2082 context.set_network_delegate(&network_delegate); 2215 context.set_network_delegate(&network_delegate);
2083 context.Init(); 2216 context.Init();
2084 2217
2085 d.set_credentials(AuthCredentials(kUser, kSecret)); 2218 d.set_credentials(AuthCredentials(kUser, kSecret));
2086 2219
2087 { 2220 {
2088 GURL url(test_server_.GetURL("auth-basic")); 2221 GURL url(test_server_.GetURL("auth-basic"));
2089 URLRequest r(url, &d, &context); 2222 URLRequest r(url, &d, &context);
2090 r.Start(); 2223 r.Start();
2091 MessageLoop::current()->Run(); 2224 MessageLoop::current()->Run();
2092 2225
2093 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); 2226 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2094 EXPECT_EQ(0, r.status().error()); 2227 EXPECT_EQ(0, r.status().error());
2095 EXPECT_EQ(200, r.GetResponseCode()); 2228 EXPECT_EQ(200, r.GetResponseCode());
2096 EXPECT_TRUE(d.auth_required_called()); 2229 EXPECT_TRUE(d.auth_required_called());
2097 EXPECT_EQ(1, network_delegate.created_requests()); 2230 EXPECT_EQ(1, network_delegate.created_requests());
2098 EXPECT_EQ(0, network_delegate.destroyed_requests()); 2231 EXPECT_EQ(0, network_delegate.destroyed_requests());
2099 } 2232 }
2100 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2233 EXPECT_EQ(1, network_delegate.destroyed_requests());
2101 } 2234 }
2102 2235
2103 // Tests that the network delegate can synchronously complete OnAuthRequired 2236 // Tests that the network delegate can synchronously complete OnAuthRequired
2104 // by setting credentials. 2237 // by setting credentials.
2105 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) { 2238 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) {
2106 ASSERT_TRUE(test_server_.Start()); 2239 ASSERT_TRUE(test_server_.Start());
2107 2240
2108 TestDelegate d; 2241 TestDelegate d;
2109 BlockingNetworkDelegate network_delegate; 2242 BlockingNetworkDelegate network_delegate(
2243 BlockingNetworkDelegate::SYNCHRONOUS);
2244 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
2110 network_delegate.set_auth_retval( 2245 network_delegate.set_auth_retval(
2111 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); 2246 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
2112 2247
2113 network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); 2248 network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret));
2114 2249
2115 TestURLRequestContext context(true); 2250 TestURLRequestContext context(true);
2116 context.set_network_delegate(&network_delegate); 2251 context.set_network_delegate(&network_delegate);
2117 context.Init(); 2252 context.Init();
2118 2253
2119 { 2254 {
(...skipping 11 matching lines...) Expand all
2131 } 2266 }
2132 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2267 EXPECT_EQ(1, network_delegate.destroyed_requests());
2133 } 2268 }
2134 2269
2135 // Tests that the network delegate can synchronously complete OnAuthRequired 2270 // Tests that the network delegate can synchronously complete OnAuthRequired
2136 // by cancelling authentication. 2271 // by cancelling authentication.
2137 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) { 2272 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) {
2138 ASSERT_TRUE(test_server_.Start()); 2273 ASSERT_TRUE(test_server_.Start());
2139 2274
2140 TestDelegate d; 2275 TestDelegate d;
2141 BlockingNetworkDelegate network_delegate; 2276 BlockingNetworkDelegate network_delegate(
2277 BlockingNetworkDelegate::SYNCHRONOUS);
2278 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
2142 network_delegate.set_auth_retval( 2279 network_delegate.set_auth_retval(
2143 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); 2280 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH);
2144 2281
2145 TestURLRequestContext context(true); 2282 TestURLRequestContext context(true);
2146 context.set_network_delegate(&network_delegate); 2283 context.set_network_delegate(&network_delegate);
2147 context.Init(); 2284 context.Init();
2148 2285
2149 { 2286 {
2150 GURL url(test_server_.GetURL("auth-basic")); 2287 GURL url(test_server_.GetURL("auth-basic"));
2151 URLRequest r(url, &d, &context); 2288 URLRequest r(url, &d, &context);
(...skipping 11 matching lines...) Expand all
2163 } 2300 }
2164 2301
2165 // Tests that the network delegate can asynchronously complete OnAuthRequired 2302 // Tests that the network delegate can asynchronously complete OnAuthRequired
2166 // by taking no action. This indicates that the NetworkDelegate does not want 2303 // 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 2304 // to handle the challenge, and is passing the buck along to the
2168 // URLRequest::Delegate. 2305 // URLRequest::Delegate.
2169 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) { 2306 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) {
2170 ASSERT_TRUE(test_server_.Start()); 2307 ASSERT_TRUE(test_server_.Start());
2171 2308
2172 TestDelegate d; 2309 TestDelegate d;
2173 BlockingNetworkDelegate network_delegate; 2310 BlockingNetworkDelegate network_delegate(
2174 network_delegate.set_auth_retval( 2311 BlockingNetworkDelegate::AUTO_CALLBACK);
2175 NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING); 2312 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
2176 network_delegate.set_auth_callback_retval(
2177 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION);
2178 2313
2179 TestURLRequestContext context(true); 2314 TestURLRequestContext context(true);
2180 context.set_network_delegate(&network_delegate); 2315 context.set_network_delegate(&network_delegate);
2181 context.Init(); 2316 context.Init();
2182 2317
2183 d.set_credentials(AuthCredentials(kUser, kSecret)); 2318 d.set_credentials(AuthCredentials(kUser, kSecret));
2184 2319
2185 { 2320 {
2186 GURL url(test_server_.GetURL("auth-basic")); 2321 GURL url(test_server_.GetURL("auth-basic"));
2187 URLRequest r(url, &d, &context); 2322 URLRequest r(url, &d, &context);
2188 r.Start(); 2323 r.Start();
2189 MessageLoop::current()->Run(); 2324 MessageLoop::current()->Run();
2190 2325
2191 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); 2326 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2192 EXPECT_EQ(0, r.status().error()); 2327 EXPECT_EQ(0, r.status().error());
2193 EXPECT_EQ(200, r.GetResponseCode()); 2328 EXPECT_EQ(200, r.GetResponseCode());
2194 EXPECT_TRUE(d.auth_required_called()); 2329 EXPECT_TRUE(d.auth_required_called());
2195 EXPECT_EQ(1, network_delegate.created_requests()); 2330 EXPECT_EQ(1, network_delegate.created_requests());
2196 EXPECT_EQ(0, network_delegate.destroyed_requests()); 2331 EXPECT_EQ(0, network_delegate.destroyed_requests());
2197 } 2332 }
2198 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2333 EXPECT_EQ(1, network_delegate.destroyed_requests());
2199 } 2334 }
2200 2335
2201 // Tests that the network delegate can asynchronously complete OnAuthRequired 2336 // Tests that the network delegate can asynchronously complete OnAuthRequired
2202 // by setting credentials. 2337 // by setting credentials.
2203 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) { 2338 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) {
2204 ASSERT_TRUE(test_server_.Start()); 2339 ASSERT_TRUE(test_server_.Start());
2205 2340
2206 TestDelegate d; 2341 TestDelegate d;
2207 BlockingNetworkDelegate network_delegate; 2342 BlockingNetworkDelegate network_delegate(
2343 BlockingNetworkDelegate::AUTO_CALLBACK);
2344 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
2208 network_delegate.set_auth_retval( 2345 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); 2346 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
2212 2347
2213 AuthCredentials auth_credentials(kUser, kSecret); 2348 AuthCredentials auth_credentials(kUser, kSecret);
2214 network_delegate.set_auth_credentials(auth_credentials); 2349 network_delegate.set_auth_credentials(auth_credentials);
2215 2350
2216 TestURLRequestContext context(true); 2351 TestURLRequestContext context(true);
2217 context.set_network_delegate(&network_delegate); 2352 context.set_network_delegate(&network_delegate);
2218 context.Init(); 2353 context.Init();
2219 2354
2220 { 2355 {
(...skipping 12 matching lines...) Expand all
2233 } 2368 }
2234 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2369 EXPECT_EQ(1, network_delegate.destroyed_requests());
2235 } 2370 }
2236 2371
2237 // Tests that the network delegate can asynchronously complete OnAuthRequired 2372 // Tests that the network delegate can asynchronously complete OnAuthRequired
2238 // by cancelling authentication. 2373 // by cancelling authentication.
2239 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) { 2374 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) {
2240 ASSERT_TRUE(test_server_.Start()); 2375 ASSERT_TRUE(test_server_.Start());
2241 2376
2242 TestDelegate d; 2377 TestDelegate d;
2243 BlockingNetworkDelegate network_delegate; 2378 BlockingNetworkDelegate network_delegate(
2379 BlockingNetworkDelegate::AUTO_CALLBACK);
2380 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
2244 network_delegate.set_auth_retval( 2381 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); 2382 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH);
2248 2383
2249 TestURLRequestContext context(true); 2384 TestURLRequestContext context(true);
2250 context.set_network_delegate(&network_delegate); 2385 context.set_network_delegate(&network_delegate);
2251 context.Init(); 2386 context.Init();
2252 2387
2253 { 2388 {
2254 GURL url(test_server_.GetURL("auth-basic")); 2389 GURL url(test_server_.GetURL("auth-basic"));
2255 URLRequest r(url, &d, &context); 2390 URLRequest r(url, &d, &context);
2256 r.Start(); 2391 r.Start();
2257 MessageLoop::current()->Run(); 2392 MessageLoop::current()->Run();
2258 2393
2259 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); 2394 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2260 EXPECT_EQ(OK, r.status().error()); 2395 EXPECT_EQ(OK, r.status().error());
2261 EXPECT_EQ(401, r.GetResponseCode()); 2396 EXPECT_EQ(401, r.GetResponseCode());
2262 EXPECT_FALSE(d.auth_required_called()); 2397 EXPECT_FALSE(d.auth_required_called());
2263 EXPECT_EQ(1, network_delegate.created_requests()); 2398 EXPECT_EQ(1, network_delegate.created_requests());
2264 EXPECT_EQ(0, network_delegate.destroyed_requests()); 2399 EXPECT_EQ(0, network_delegate.destroyed_requests());
2265 } 2400 }
2266 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2401 EXPECT_EQ(1, network_delegate.destroyed_requests());
2267 } 2402 }
2268 2403
2269 // Tests that we can handle when a network request was canceled while we were 2404 // Tests that we can handle when a network request was canceled while we were
2270 // waiting for the network delegate. 2405 // waiting for the network delegate.
2271 // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback. 2406 // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback.
2272 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) { 2407 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) {
2273 ASSERT_TRUE(test_server_.Start()); 2408 ASSERT_TRUE(test_server_.Start());
2274 2409
2275 TestDelegate d; 2410 TestDelegate d;
2276 BlockingNetworkDelegateWithManualCallback network_delegate; 2411 BlockingNetworkDelegate network_delegate(
2277 network_delegate.BlockOn( 2412 BlockingNetworkDelegate::USER_CALLBACK);
2278 BlockingNetworkDelegateWithManualCallback::ON_BEFORE_URL_REQUEST); 2413 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
2279 2414
2280 TestURLRequestContext context(true); 2415 TestURLRequestContext context(true);
2281 context.set_network_delegate(&network_delegate); 2416 context.set_network_delegate(&network_delegate);
2282 context.Init(); 2417 context.Init();
2283 2418
2284 { 2419 {
2285 URLRequest r(test_server_.GetURL(""), &d, &context); 2420 URLRequest r(test_server_.GetURL(""), &d, &context);
2286 2421
2287 r.Start(); 2422 r.Start();
2288 network_delegate.WaitForState( 2423 MessageLoop::current()->Run();
2289 BlockingNetworkDelegateWithManualCallback::ON_BEFORE_URL_REQUEST); 2424 EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
2425 network_delegate.stage_blocked_for_callback());
2290 EXPECT_EQ(0, network_delegate.completed_requests()); 2426 EXPECT_EQ(0, network_delegate.completed_requests());
2291 // Cancel before callback. 2427 // Cancel before callback.
2292 r.Cancel(); 2428 r.Cancel();
2293 // Ensure that network delegate is notified. 2429 // Ensure that network delegate is notified.
2294 EXPECT_EQ(1, network_delegate.completed_requests()); 2430 EXPECT_EQ(1, network_delegate.completed_requests());
2295 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); 2431 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
2296 EXPECT_EQ(ERR_ABORTED, r.status().error()); 2432 EXPECT_EQ(ERR_ABORTED, r.status().error());
2297 EXPECT_EQ(1, network_delegate.created_requests()); 2433 EXPECT_EQ(1, network_delegate.created_requests());
2298 EXPECT_EQ(0, network_delegate.destroyed_requests()); 2434 EXPECT_EQ(0, network_delegate.destroyed_requests());
2299 } 2435 }
2300 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2436 EXPECT_EQ(1, network_delegate.destroyed_requests());
2301 } 2437 }
2302 2438
2303 // Tests that we can handle when a network request was canceled while we were 2439 // Tests that we can handle when a network request was canceled while we were
2304 // waiting for the network delegate. 2440 // waiting for the network delegate.
2305 // Part 2: Request is cancelled while waiting for OnBeforeSendHeaders callback. 2441 // Part 2: Request is cancelled while waiting for OnBeforeSendHeaders callback.
2306 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) { 2442 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) {
2307 ASSERT_TRUE(test_server_.Start()); 2443 ASSERT_TRUE(test_server_.Start());
2308 2444
2309 TestDelegate d; 2445 TestDelegate d;
2310 BlockingNetworkDelegateWithManualCallback network_delegate; 2446 BlockingNetworkDelegate network_delegate(
2311 network_delegate.BlockOn( 2447 BlockingNetworkDelegate::USER_CALLBACK);
2312 BlockingNetworkDelegateWithManualCallback::ON_BEFORE_SEND_HEADERS); 2448 network_delegate.set_block_on(
2449 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS);
2313 2450
2314 TestURLRequestContext context(true); 2451 TestURLRequestContext context(true);
2315 context.set_network_delegate(&network_delegate); 2452 context.set_network_delegate(&network_delegate);
2316 context.Init(); 2453 context.Init();
2317 2454
2318 { 2455 {
2319 URLRequest r(test_server_.GetURL(""), &d, &context); 2456 URLRequest r(test_server_.GetURL(""), &d, &context);
2320 2457
2321 r.Start(); 2458 r.Start();
2322 network_delegate.WaitForState( 2459 MessageLoop::current()->Run();
2323 BlockingNetworkDelegateWithManualCallback::ON_BEFORE_SEND_HEADERS); 2460 EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
2461 network_delegate.stage_blocked_for_callback());
2324 EXPECT_EQ(0, network_delegate.completed_requests()); 2462 EXPECT_EQ(0, network_delegate.completed_requests());
2325 // Cancel before callback. 2463 // Cancel before callback.
2326 r.Cancel(); 2464 r.Cancel();
2327 // Ensure that network delegate is notified. 2465 // Ensure that network delegate is notified.
2328 EXPECT_EQ(1, network_delegate.completed_requests()); 2466 EXPECT_EQ(1, network_delegate.completed_requests());
2329 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); 2467 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
2330 EXPECT_EQ(ERR_ABORTED, r.status().error()); 2468 EXPECT_EQ(ERR_ABORTED, r.status().error());
2331 EXPECT_EQ(1, network_delegate.created_requests()); 2469 EXPECT_EQ(1, network_delegate.created_requests());
2332 EXPECT_EQ(0, network_delegate.destroyed_requests()); 2470 EXPECT_EQ(0, network_delegate.destroyed_requests());
2333 } 2471 }
2334 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2472 EXPECT_EQ(1, network_delegate.destroyed_requests());
2335 } 2473 }
2336 2474
2337 // Tests that we can handle when a network request was canceled while we were 2475 // Tests that we can handle when a network request was canceled while we were
2338 // waiting for the network delegate. 2476 // waiting for the network delegate.
2339 // Part 3: Request is cancelled while waiting for OnHeadersReceived callback. 2477 // Part 3: Request is cancelled while waiting for OnHeadersReceived callback.
2340 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) { 2478 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) {
2341 ASSERT_TRUE(test_server_.Start()); 2479 ASSERT_TRUE(test_server_.Start());
2342 2480
2343 TestDelegate d; 2481 TestDelegate d;
2344 BlockingNetworkDelegateWithManualCallback network_delegate; 2482 BlockingNetworkDelegate network_delegate(
2345 network_delegate.BlockOn( 2483 BlockingNetworkDelegate::USER_CALLBACK);
2346 BlockingNetworkDelegateWithManualCallback::ON_HEADERS_RECEIVED); 2484 network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED);
2347 2485
2348 TestURLRequestContext context(true); 2486 TestURLRequestContext context(true);
2349 context.set_network_delegate(&network_delegate); 2487 context.set_network_delegate(&network_delegate);
2350 context.Init(); 2488 context.Init();
2351 2489
2352 { 2490 {
2353 URLRequest r(test_server_.GetURL(""), &d, &context); 2491 URLRequest r(test_server_.GetURL(""), &d, &context);
2354 2492
2355 r.Start(); 2493 r.Start();
2356 network_delegate.WaitForState( 2494 MessageLoop::current()->Run();
2357 BlockingNetworkDelegateWithManualCallback::ON_HEADERS_RECEIVED); 2495 EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED,
2496 network_delegate.stage_blocked_for_callback());
2358 EXPECT_EQ(0, network_delegate.completed_requests()); 2497 EXPECT_EQ(0, network_delegate.completed_requests());
2359 // Cancel before callback. 2498 // Cancel before callback.
2360 r.Cancel(); 2499 r.Cancel();
2361 // Ensure that network delegate is notified. 2500 // Ensure that network delegate is notified.
2362 EXPECT_EQ(1, network_delegate.completed_requests()); 2501 EXPECT_EQ(1, network_delegate.completed_requests());
2363 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); 2502 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
2364 EXPECT_EQ(ERR_ABORTED, r.status().error()); 2503 EXPECT_EQ(ERR_ABORTED, r.status().error());
2365 EXPECT_EQ(1, network_delegate.created_requests()); 2504 EXPECT_EQ(1, network_delegate.created_requests());
2366 EXPECT_EQ(0, network_delegate.destroyed_requests()); 2505 EXPECT_EQ(0, network_delegate.destroyed_requests());
2367 } 2506 }
2368 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2507 EXPECT_EQ(1, network_delegate.destroyed_requests());
2369 } 2508 }
2370 2509
2371 // Tests that we can handle when a network request was canceled while we were 2510 // Tests that we can handle when a network request was canceled while we were
2372 // waiting for the network delegate. 2511 // waiting for the network delegate.
2373 // Part 4: Request is cancelled while waiting for OnAuthRequired callback. 2512 // Part 4: Request is cancelled while waiting for OnAuthRequired callback.
2374 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) { 2513 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) {
2375 ASSERT_TRUE(test_server_.Start()); 2514 ASSERT_TRUE(test_server_.Start());
2376 2515
2377 TestDelegate d; 2516 TestDelegate d;
2378 BlockingNetworkDelegateWithManualCallback network_delegate; 2517 BlockingNetworkDelegate network_delegate(
2379 network_delegate.BlockOn( 2518 BlockingNetworkDelegate::USER_CALLBACK);
2380 BlockingNetworkDelegateWithManualCallback::ON_AUTH_REQUIRED); 2519 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
2381 2520
2382 TestURLRequestContext context(true); 2521 TestURLRequestContext context(true);
2383 context.set_network_delegate(&network_delegate); 2522 context.set_network_delegate(&network_delegate);
2384 context.Init(); 2523 context.Init();
2385 2524
2386 { 2525 {
2387 URLRequest r(test_server_.GetURL("auth-basic"), &d, &context); 2526 URLRequest r(test_server_.GetURL("auth-basic"), &d, &context);
2388 2527
2389 r.Start(); 2528 r.Start();
2390 network_delegate.WaitForState( 2529 MessageLoop::current()->Run();
2391 BlockingNetworkDelegateWithManualCallback::ON_AUTH_REQUIRED); 2530 EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED,
2531 network_delegate.stage_blocked_for_callback());
2392 EXPECT_EQ(0, network_delegate.completed_requests()); 2532 EXPECT_EQ(0, network_delegate.completed_requests());
2393 // Cancel before callback. 2533 // Cancel before callback.
2394 r.Cancel(); 2534 r.Cancel();
2395 // Ensure that network delegate is notified. 2535 // Ensure that network delegate is notified.
2396 EXPECT_EQ(1, network_delegate.completed_requests()); 2536 EXPECT_EQ(1, network_delegate.completed_requests());
2397 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); 2537 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
2398 EXPECT_EQ(ERR_ABORTED, r.status().error()); 2538 EXPECT_EQ(ERR_ABORTED, r.status().error());
2399 EXPECT_EQ(1, network_delegate.created_requests()); 2539 EXPECT_EQ(1, network_delegate.created_requests());
2400 EXPECT_EQ(0, network_delegate.destroyed_requests()); 2540 EXPECT_EQ(0, network_delegate.destroyed_requests());
2401 } 2541 }
(...skipping 2249 matching lines...) Expand 10 before | Expand all | Expand 10 after
4651 4791
4652 EXPECT_FALSE(r.is_pending()); 4792 EXPECT_FALSE(r.is_pending());
4653 EXPECT_EQ(1, d->response_started_count()); 4793 EXPECT_EQ(1, d->response_started_count());
4654 EXPECT_FALSE(d->received_data_before_response()); 4794 EXPECT_FALSE(d->received_data_before_response());
4655 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); 4795 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
4656 } 4796 }
4657 } 4797 }
4658 #endif // !defined(DISABLE_FTP_SUPPORT) 4798 #endif // !defined(DISABLE_FTP_SUPPORT)
4659 4799
4660 } // namespace net 4800 } // 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