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

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: 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 | « no previous file | 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 common base for BlockingNetworkDelegate* classes. Takes care of
150 // when-to-block and what-to return book-keeping.
151 class BlockingNetworkDelegateBase : public TestNetworkDelegate {
152 public:
153 enum State { // When to block.
154 NOT_BLOCKED = 0,
155 ON_BEFORE_URL_REQUEST = 1 << 0,
156 ON_BEFORE_SEND_HEADERS = 1 << 1,
157 ON_HEADERS_RECEIVED = 1 << 2,
158 ON_AUTH_REQUIRED = 1 << 3
159 };
160
161 void set_retval(int retval) {
162 retval_ = retval;
163 }
164
165 State state() {
166 return state_;
167 }
168
169 // Activates blocking on |state|.
170 void BlockOn(State state) {
171 block_on_ |= state;
172 }
173
174 // Runs the message loop until |state| is reached.
175 void WaitForState(State state) {
176 while (state_ != state)
177 MessageLoop::current()->RunAllPending();
178 }
179
180 protected:
181 BlockingNetworkDelegateBase(int block_on,
182 int retval,
183 bool call_base_on_auth_required);
184
185 void ResetState() {
186 state_ = NOT_BLOCKED;
187 }
188
189 // TestNetworkDelegate implementation.
190 virtual int OnBeforeURLRequest(URLRequest* request,
191 const CompletionCallback& callback,
192 GURL* new_url) OVERRIDE;
193
194 virtual int OnBeforeSendHeaders(URLRequest* request,
195 const CompletionCallback& callback,
196 HttpRequestHeaders* headers) OVERRIDE;
197
198 virtual int OnHeadersReceived(
199 URLRequest* request,
200 const CompletionCallback& callback,
201 HttpResponseHeaders* original_response_headers,
202 scoped_refptr<HttpResponseHeaders>* override_response_headers) OVERRIDE;
203
204 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired(
205 URLRequest* request,
206 const AuthChallengeInfo& auth_info,
207 const AuthCallback& callback,
208 AuthCredentials* credentials) OVERRIDE;
209
210 private:
211 int block_on_; // Bit mask on which states to block.
212 State state_;
213 int retval_;
214 // Should we call TestNetworkDelegate::OnAuthRequired in OnAuthRequired?
215 bool call_base_on_auth_required_;
216
217 DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegateBase);
218 };
219
220 BlockingNetworkDelegateBase::BlockingNetworkDelegateBase(
221 int block_on,
222 int retval,
223 bool call_base_on_auth_required)
224 : block_on_(block_on),
225 state_(NOT_BLOCKED),
226 retval_(retval),
227 call_base_on_auth_required_(call_base_on_auth_required) {
228 }
229
230 int BlockingNetworkDelegateBase::OnBeforeURLRequest(
231 URLRequest* request,
232 const CompletionCallback& callback,
233 GURL* new_url) {
mmenke 2012/09/13 15:28:02 Suggest you inline these, for consistency with the
vabr (Chromium) 2012/09/13 17:23:35 I see the lack of consistency, but these are the r
mmenke 2012/09/13 18:48:07 Rightly or wrongly, in practice this rule tends to
234 TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url);
235 if ((block_on_ & ON_BEFORE_URL_REQUEST) != 0) {
mmenke 2012/09/13 15:28:02 nit: Suggest you put the "== 0" case first, for c
vabr (Chromium) 2012/09/13 17:23:35 Done, also on the places below. Thanks!
236 state_ = ON_BEFORE_URL_REQUEST;
237 return retval_;
238 }
239 return OK;
240 }
241
242 int BlockingNetworkDelegateBase::OnBeforeSendHeaders(
243 URLRequest* request,
244 const CompletionCallback& callback,
245 HttpRequestHeaders* headers) {
246 TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers);
247 if ((block_on_ & ON_BEFORE_SEND_HEADERS) != 0) {
248 state_ = ON_BEFORE_SEND_HEADERS;
249 return retval_;
250 }
251 return OK;
252 }
253
254 int BlockingNetworkDelegateBase::OnHeadersReceived(
255 URLRequest* request,
256 const CompletionCallback& callback,
257 HttpResponseHeaders* original_response_headers,
258 scoped_refptr<HttpResponseHeaders>* override_response_headers) {
259 TestNetworkDelegate::OnHeadersReceived(
260 request, callback,
261 original_response_headers, override_response_headers);
262 if ((block_on_ & ON_HEADERS_RECEIVED) != 0) {
263 state_ = ON_HEADERS_RECEIVED;
264 return retval_;
265 }
266 return OK;
267 }
268
269 NetworkDelegate::AuthRequiredResponse
270 BlockingNetworkDelegateBase::OnAuthRequired(URLRequest* request,
271 const AuthChallengeInfo& auth_info,
272 const AuthCallback& callback,
273 AuthCredentials* credentials) {
274 if (call_base_on_auth_required_) {
275 TestNetworkDelegate::OnAuthRequired(request, auth_info, callback,
276 credentials);
277 }
278 if ((block_on_ & ON_AUTH_REQUIRED) != 0) {
279 state_ = ON_AUTH_REQUIRED;
280 // Here we just need to report something different than NO_ACTION.
281 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH;
282 }
283 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
284 }
285
149 // A network delegate that blocks requests, optionally cancelling or redirecting 286 // A network delegate that blocks requests, optionally cancelling or redirecting
150 // them. 287 // them.
151 class BlockingNetworkDelegate : public TestNetworkDelegate { 288 class BlockingNetworkDelegate : public BlockingNetworkDelegateBase {
152 public: 289 public:
153 BlockingNetworkDelegate() 290 BlockingNetworkDelegate()
154 : retval_(ERR_IO_PENDING), 291 : BlockingNetworkDelegateBase(0, ERR_IO_PENDING, true),
155 callback_retval_(OK), 292 callback_retval_(OK),
156 auth_retval_(NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING), 293 auth_retval_(NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING),
157 auth_callback_retval_( 294 auth_callback_retval_(
158 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION), 295 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION),
159 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {} 296 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {}
160 297
161 void set_retval(int retval) { retval_ = retval; } 298 void set_callback_retval(int retval) {
162 void set_callback_retval(int retval) { callback_retval_ = retval; } 299 callback_retval_ = retval;
163 void set_redirect_url(const GURL& url) { redirect_url_ = url; } 300 }
301 void set_redirect_url(const GURL& url) {
302 redirect_url_ = url;
303 }
164 void set_auth_retval(NetworkDelegate::AuthRequiredResponse retval) { 304 void set_auth_retval(NetworkDelegate::AuthRequiredResponse retval) {
165 auth_retval_ = retval; } 305 auth_retval_ = retval;
306 }
166 void set_auth_callback_retval(NetworkDelegate::AuthRequiredResponse retval) { 307 void set_auth_callback_retval(NetworkDelegate::AuthRequiredResponse retval) {
167 auth_callback_retval_ = retval; } 308 auth_callback_retval_ = retval;
309 }
168 void set_auth_credentials(const AuthCredentials& auth_credentials) { 310 void set_auth_credentials(const AuthCredentials& auth_credentials) {
169 auth_credentials_ = auth_credentials; 311 auth_credentials_ = auth_credentials;
170 } 312 }
171 313
314 // TODO(vabr): remove when corresponding On* overrides are implemented.
mmenke 2012/09/13 15:28:02 Aren't these already implemented by BlockingNetwor
vabr (Chromium) 2012/09/13 17:23:35 Yes, they are, but they will not post the callback
mmenke 2012/09/13 18:48:07 This sort of gets at one of my main complaints - t
mmenke 2012/09/13 20:44:20 Sorry, that was worded really poorly. And I ended
315 void BlockOn(State state) {
316 DCHECK(state != ON_BEFORE_SEND_HEADERS && state != ON_HEADERS_RECEIVED);
317 BlockingNetworkDelegateBase::BlockOn(state);
318 }
319
172 private: 320 private:
173 // TestNetworkDelegate implementation. 321 // TestNetworkDelegate implementation.
174 virtual int OnBeforeURLRequest(URLRequest* request, 322 virtual int OnBeforeURLRequest(URLRequest* request,
175 const CompletionCallback& callback, 323 const CompletionCallback& callback,
176 GURL* new_url) OVERRIDE { 324 GURL* new_url) OVERRIDE {
177 if (redirect_url_ == request->url()) { 325 if (redirect_url_ == request->url()) {
178 // We've already seen this request and redirected elsewhere. 326 // We've already seen this request and redirected elsewhere.
179 return OK; 327 return OK;
180 } 328 }
181 329
182 TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); 330 int retval = BlockingNetworkDelegateBase::OnBeforeURLRequest(
331 request, callback, new_url);
183 332
184 if (!redirect_url_.is_empty()) 333 if (!redirect_url_.is_empty())
185 *new_url = redirect_url_; 334 *new_url = redirect_url_;
186 335
187 if (retval_ != ERR_IO_PENDING) 336 if (retval != ERR_IO_PENDING)
188 return retval_; 337 return retval;
189 338
190 MessageLoop::current()->PostTask( 339 MessageLoop::current()->PostTask(
191 FROM_HERE, 340 FROM_HERE,
192 base::Bind(&BlockingNetworkDelegate::DoCallback, 341 base::Bind(&BlockingNetworkDelegate::DoCallback,
193 weak_factory_.GetWeakPtr(), callback)); 342 weak_factory_.GetWeakPtr(), callback));
194 return ERR_IO_PENDING; 343 return ERR_IO_PENDING;
195 } 344 }
196 345
197 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired( 346 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired(
198 URLRequest* request, 347 URLRequest* request,
199 const AuthChallengeInfo& auth_info, 348 const AuthChallengeInfo& auth_info,
200 const AuthCallback& callback, 349 const AuthCallback& callback,
201 AuthCredentials* credentials) OVERRIDE { 350 AuthCredentials* credentials) OVERRIDE {
202 TestNetworkDelegate::OnAuthRequired(request, auth_info, callback, 351 BlockingNetworkDelegateBase::OnAuthRequired(request, auth_info, callback,
203 credentials); 352 credentials);
204 switch (auth_retval_) { 353 switch (auth_retval_) {
205 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION: 354 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION:
206 break; 355 break;
207 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH: 356 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH:
208 *credentials = auth_credentials_; 357 *credentials = auth_credentials_;
209 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH: 358 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH:
210 break; 359 break;
211 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING: 360 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING:
212 MessageLoop::current()->PostTask( 361 MessageLoop::current()->PostTask(
213 FROM_HERE, 362 FROM_HERE,
(...skipping 11 matching lines...) Expand all
225 void DoAuthCallback(const AuthCallback& callback, 374 void DoAuthCallback(const AuthCallback& callback,
226 AuthCredentials* credentials) { 375 AuthCredentials* credentials) {
227 if (auth_callback_retval_ == 376 if (auth_callback_retval_ ==
228 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH) { 377 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH) {
229 *credentials = auth_credentials_; 378 *credentials = auth_credentials_;
230 } 379 }
231 callback.Run(auth_callback_retval_); 380 callback.Run(auth_callback_retval_);
232 } 381 }
233 382
234 383
235 int retval_;
236 int callback_retval_; 384 int callback_retval_;
237 GURL redirect_url_; 385 GURL redirect_url_;
238 NetworkDelegate::AuthRequiredResponse auth_retval_; 386 NetworkDelegate::AuthRequiredResponse auth_retval_;
239 NetworkDelegate::AuthRequiredResponse auth_callback_retval_; 387 NetworkDelegate::AuthRequiredResponse auth_callback_retval_;
240 AuthCredentials auth_credentials_; 388 AuthCredentials auth_credentials_;
241 base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_; 389 base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_;
390
391 DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate);
242 }; 392 };
243 393
244 // A network delegate that allows blocking requests until a callback function is 394 // A network delegate that allows blocking requests until a callback function is
245 // called. 395 // called.
246 class BlockingNetworkDelegateWithManualCallback : public TestNetworkDelegate { 396 class BlockingNetworkDelegateWithManualCallback
397 : public BlockingNetworkDelegateBase {
247 public: 398 public:
248 enum State {
249 NOT_BLOCKED = 0,
250 ON_BEFORE_URL_REQUEST = 1 << 0,
251 ON_BEFORE_SEND_HEADERS = 1 << 1,
252 ON_HEADERS_RECEIVED = 1 << 2,
253 ON_AUTH_REQUIRED = 1 << 3
254 };
255
256 BlockingNetworkDelegateWithManualCallback() 399 BlockingNetworkDelegateWithManualCallback()
257 : block_on_(0), 400 : BlockingNetworkDelegateBase(0, ERR_IO_PENDING, false) {
258 state_(NOT_BLOCKED) {
259 }
260
261 // Activates blocking on |state|.
262 void BlockOn(State state) {
263 block_on_ |= state;
264 } 401 }
265 402
266 void DoCallback(int rv) { 403 void DoCallback(int rv) {
267 ASSERT_NE(NOT_BLOCKED, state_); 404 ASSERT_NE(BlockingNetworkDelegateBase::NOT_BLOCKED, state());
268 CompletionCallback callback = callback_; 405 CompletionCallback callback = callback_;
269 Reset(); 406 Reset();
270 callback.Run(rv); 407 callback.Run(rv);
271 } 408 }
272 409
273 void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response) { 410 void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response) {
274 ASSERT_EQ(ON_AUTH_REQUIRED, state_); 411 ASSERT_EQ(BlockingNetworkDelegateBase::ON_AUTH_REQUIRED, state());
275 AuthCallback auth_callback = auth_callback_; 412 AuthCallback auth_callback = auth_callback_;
276 Reset(); 413 Reset();
277 auth_callback.Run(response); 414 auth_callback.Run(response);
278 } 415 }
279 416
280 // Runs the message loop until |state| is reached.
281 void WaitForState(State state) {
282 while (state_ != state)
283 MessageLoop::current()->RunAllPending();
284 }
285
286 private: 417 private:
287 // TestNetworkDelegate implementation. 418 // TestNetworkDelegate implementation.
288 virtual int OnBeforeURLRequest(URLRequest* request, 419 virtual int OnBeforeURLRequest(URLRequest* request,
289 const CompletionCallback& callback, 420 const CompletionCallback& callback,
290 GURL* new_url) OVERRIDE { 421 GURL* new_url) OVERRIDE {
291 TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); 422 int retval = BlockingNetworkDelegateBase::OnBeforeURLRequest(
292 if ((block_on_ & ON_BEFORE_URL_REQUEST) == 0) { 423 request, callback, new_url);
293 return OK; 424 if (retval == ERR_IO_PENDING)
294 } else {
295 state_ = ON_BEFORE_URL_REQUEST;
296 callback_ = callback; 425 callback_ = callback;
297 return ERR_IO_PENDING; 426 return retval;
298 }
299 } 427 }
300 428
301 virtual int OnBeforeSendHeaders(URLRequest* request, 429 virtual int OnBeforeSendHeaders(URLRequest* request,
302 const CompletionCallback& callback, 430 const CompletionCallback& callback,
303 HttpRequestHeaders* headers) OVERRIDE { 431 HttpRequestHeaders* headers) OVERRIDE {
304 TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers); 432 int retval = BlockingNetworkDelegateBase::OnBeforeSendHeaders(
305 if ((block_on_ & ON_BEFORE_SEND_HEADERS) == 0) { 433 request, callback, headers);
306 return OK; 434 if (retval == ERR_IO_PENDING)
307 } else {
308 state_ = ON_BEFORE_SEND_HEADERS;
309 callback_ = callback; 435 callback_ = callback;
310 return ERR_IO_PENDING; 436 return retval;
311 }
312 } 437 }
313 438
314 virtual int OnHeadersReceived( 439 virtual int OnHeadersReceived(
315 URLRequest* request, 440 URLRequest* request,
316 const CompletionCallback& callback, 441 const CompletionCallback& callback,
317 HttpResponseHeaders* original_response_headers, 442 HttpResponseHeaders* original_response_headers,
318 scoped_refptr<HttpResponseHeaders>* override_response_headers) 443 scoped_refptr<HttpResponseHeaders>* override_response_headers)
319 OVERRIDE { 444 OVERRIDE {
320 TestNetworkDelegate::OnHeadersReceived( 445 int retval = BlockingNetworkDelegateBase::OnHeadersReceived(
321 request, callback, original_response_headers, 446 request, callback,
322 override_response_headers); 447 original_response_headers, override_response_headers);
323 if ((block_on_ & ON_HEADERS_RECEIVED) == 0) { 448 if (retval == ERR_IO_PENDING)
324 return OK;
325 } else {
326 state_ = ON_HEADERS_RECEIVED;
327 callback_ = callback; 449 callback_ = callback;
328 return ERR_IO_PENDING; 450 return retval;
329 }
330 } 451 }
331 452
332 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired( 453 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired(
333 URLRequest* request, 454 URLRequest* request,
334 const AuthChallengeInfo& auth_info, 455 const AuthChallengeInfo& auth_info,
335 const AuthCallback& callback, 456 const AuthCallback& callback,
336 AuthCredentials* credentials) OVERRIDE { 457 AuthCredentials* credentials) OVERRIDE {
337 if ((block_on_ & ON_AUTH_REQUIRED) == 0) { 458 if (BlockingNetworkDelegateBase::OnAuthRequired(
338 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION; 459 request, auth_info, callback, credentials) !=
339 } else { 460 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION) {
340 state_ = ON_AUTH_REQUIRED; 461 // We should block on authorisation requirement.
341 auth_callback_ = callback; 462 auth_callback_ = callback;
342 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING; 463 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING;
343 } 464 }
465 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
344 } 466 }
345 467
346 void Reset() { 468 void Reset() {
347 state_ = NOT_BLOCKED; 469 ResetState();
348 callback_.Reset(); 470 callback_.Reset();
349 auth_callback_.Reset(); 471 auth_callback_.Reset();
350 } 472 }
351 473
352 int block_on_; // Bit mask on which states to block.
353 State state_;
354 CompletionCallback callback_; 474 CompletionCallback callback_;
355 AuthCallback auth_callback_; 475 AuthCallback auth_callback_;
476
477 DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegateWithManualCallback);
356 }; 478 };
357 479
358 480
359 // A simple Interceptor that returns a pre-built URLRequestJob one time. 481 // A simple Interceptor that returns a pre-built URLRequestJob one time.
360 class TestJobInterceptor : public URLRequestJobFactory::Interceptor { 482 class TestJobInterceptor : public URLRequestJobFactory::Interceptor {
361 public: 483 public:
362 TestJobInterceptor() 484 TestJobInterceptor()
363 : main_intercept_job_(NULL) { 485 : main_intercept_job_(NULL) {
364 } 486 }
365 487
(...skipping 1577 matching lines...) Expand 10 before | Expand all | Expand 10 after
1943 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, network_delegate.last_error()); 2065 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, network_delegate.last_error());
1944 } 2066 }
1945 } 2067 }
1946 2068
1947 // Tests that the network delegate can block and cancel a request. 2069 // Tests that the network delegate can block and cancel a request.
1948 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) { 2070 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) {
1949 ASSERT_TRUE(test_server_.Start()); 2071 ASSERT_TRUE(test_server_.Start());
1950 2072
1951 TestDelegate d; 2073 TestDelegate d;
1952 BlockingNetworkDelegate network_delegate; 2074 BlockingNetworkDelegate network_delegate;
2075 network_delegate.BlockOn(BlockingNetworkDelegateBase::ON_BEFORE_URL_REQUEST);
1953 network_delegate.set_callback_retval(ERR_EMPTY_RESPONSE); 2076 network_delegate.set_callback_retval(ERR_EMPTY_RESPONSE);
1954 2077
1955 TestURLRequestContextWithProxy context( 2078 TestURLRequestContextWithProxy context(
1956 test_server_.host_port_pair().ToString(), 2079 test_server_.host_port_pair().ToString(),
1957 &network_delegate); 2080 &network_delegate);
1958 2081
1959 { 2082 {
1960 URLRequest r(test_server_.GetURL(""), &d, &context); 2083 URLRequest r(test_server_.GetURL(""), &d, &context);
1961 2084
1962 r.Start(); 2085 r.Start();
1963 MessageLoop::current()->Run(); 2086 MessageLoop::current()->Run();
1964 2087
1965 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); 2088 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
1966 EXPECT_EQ(ERR_EMPTY_RESPONSE, r.status().error()); 2089 EXPECT_EQ(ERR_EMPTY_RESPONSE, r.status().error());
1967 EXPECT_EQ(1, network_delegate.created_requests()); 2090 EXPECT_EQ(1, network_delegate.created_requests());
1968 EXPECT_EQ(0, network_delegate.destroyed_requests()); 2091 EXPECT_EQ(0, network_delegate.destroyed_requests());
1969 } 2092 }
1970 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2093 EXPECT_EQ(1, network_delegate.destroyed_requests());
1971 } 2094 }
1972 2095
1973 // Tests that the network delegate can cancel a request synchronously. 2096 // Tests that the network delegate can cancel a request synchronously.
1974 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously) { 2097 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously) {
1975 ASSERT_TRUE(test_server_.Start()); 2098 ASSERT_TRUE(test_server_.Start());
1976 2099
1977 TestDelegate d; 2100 TestDelegate d;
1978 BlockingNetworkDelegate network_delegate; 2101 BlockingNetworkDelegate network_delegate;
2102 network_delegate.BlockOn(BlockingNetworkDelegateBase::ON_BEFORE_URL_REQUEST);
1979 network_delegate.set_retval(ERR_EMPTY_RESPONSE); 2103 network_delegate.set_retval(ERR_EMPTY_RESPONSE);
1980 2104
1981 TestURLRequestContextWithProxy context( 2105 TestURLRequestContextWithProxy context(
1982 test_server_.host_port_pair().ToString(), 2106 test_server_.host_port_pair().ToString(),
1983 &network_delegate); 2107 &network_delegate);
1984 2108
1985 { 2109 {
1986 URLRequest r(test_server_.GetURL(""), &d, &context); 2110 URLRequest r(test_server_.GetURL(""), &d, &context);
1987 2111
1988 r.Start(); 2112 r.Start();
1989 MessageLoop::current()->Run(); 2113 MessageLoop::current()->Run();
1990 2114
1991 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); 2115 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
1992 EXPECT_EQ(ERR_EMPTY_RESPONSE, r.status().error()); 2116 EXPECT_EQ(ERR_EMPTY_RESPONSE, r.status().error());
1993 EXPECT_EQ(1, network_delegate.created_requests()); 2117 EXPECT_EQ(1, network_delegate.created_requests());
1994 EXPECT_EQ(0, network_delegate.destroyed_requests()); 2118 EXPECT_EQ(0, network_delegate.destroyed_requests());
1995 } 2119 }
1996 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2120 EXPECT_EQ(1, network_delegate.destroyed_requests());
1997 } 2121 }
1998 2122
1999 // Tests that the network delegate can block and redirect a request to a new 2123 // Tests that the network delegate can block and redirect a request to a new
2000 // URL. 2124 // URL.
2001 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) { 2125 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) {
2002 ASSERT_TRUE(test_server_.Start()); 2126 ASSERT_TRUE(test_server_.Start());
2003 2127
2004 TestDelegate d; 2128 TestDelegate d;
2005 BlockingNetworkDelegate network_delegate; 2129 BlockingNetworkDelegate network_delegate;
2130 network_delegate.BlockOn(BlockingNetworkDelegateBase::ON_BEFORE_URL_REQUEST);
2006 GURL redirect_url(test_server_.GetURL("simple.html")); 2131 GURL redirect_url(test_server_.GetURL("simple.html"));
2007 network_delegate.set_redirect_url(redirect_url); 2132 network_delegate.set_redirect_url(redirect_url);
2008 2133
2009 TestURLRequestContextWithProxy context( 2134 TestURLRequestContextWithProxy context(
2010 test_server_.host_port_pair().ToString(), 2135 test_server_.host_port_pair().ToString(),
2011 &network_delegate); 2136 &network_delegate);
2012 2137
2013 { 2138 {
2014 GURL original_url(test_server_.GetURL("empty.html")); 2139 GURL original_url(test_server_.GetURL("empty.html"));
2015 URLRequest r(original_url, &d, &context); 2140 URLRequest r(original_url, &d, &context);
(...skipping 12 matching lines...) Expand all
2028 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2153 EXPECT_EQ(1, network_delegate.destroyed_requests());
2029 } 2154 }
2030 2155
2031 // Tests that the network delegate can block and redirect a request to a new 2156 // Tests that the network delegate can block and redirect a request to a new
2032 // URL by setting a redirect_url and returning in OnBeforeURLRequest directly. 2157 // URL by setting a redirect_url and returning in OnBeforeURLRequest directly.
2033 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) { 2158 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) {
2034 ASSERT_TRUE(test_server_.Start()); 2159 ASSERT_TRUE(test_server_.Start());
2035 2160
2036 TestDelegate d; 2161 TestDelegate d;
2037 BlockingNetworkDelegate network_delegate; 2162 BlockingNetworkDelegate network_delegate;
2163 network_delegate.BlockOn(BlockingNetworkDelegateBase::ON_BEFORE_URL_REQUEST);
2038 GURL redirect_url(test_server_.GetURL("simple.html")); 2164 GURL redirect_url(test_server_.GetURL("simple.html"));
2039 network_delegate.set_redirect_url(redirect_url); 2165 network_delegate.set_redirect_url(redirect_url);
2040 network_delegate.set_retval(OK); 2166 network_delegate.set_retval(OK);
2041 2167
2042 TestURLRequestContextWithProxy context( 2168 TestURLRequestContextWithProxy context(
2043 test_server_.host_port_pair().ToString(), 2169 test_server_.host_port_pair().ToString(),
2044 &network_delegate); 2170 &network_delegate);
2045 2171
2046 { 2172 {
2047 GURL original_url(test_server_.GetURL("empty.html")); 2173 GURL original_url(test_server_.GetURL("empty.html"));
(...skipping 14 matching lines...) Expand all
2062 } 2188 }
2063 2189
2064 // Tests that redirects caused by the network delegate preserve POST data. 2190 // Tests that redirects caused by the network delegate preserve POST data.
2065 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) { 2191 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) {
2066 ASSERT_TRUE(test_server_.Start()); 2192 ASSERT_TRUE(test_server_.Start());
2067 2193
2068 const char kData[] = "hello world"; 2194 const char kData[] = "hello world";
2069 2195
2070 TestDelegate d; 2196 TestDelegate d;
2071 BlockingNetworkDelegate network_delegate; 2197 BlockingNetworkDelegate network_delegate;
2198 network_delegate.BlockOn(BlockingNetworkDelegateBase::ON_BEFORE_URL_REQUEST);
2072 GURL redirect_url(test_server_.GetURL("echo")); 2199 GURL redirect_url(test_server_.GetURL("echo"));
2073 network_delegate.set_redirect_url(redirect_url); 2200 network_delegate.set_redirect_url(redirect_url);
2074 2201
2075 TestURLRequestContext context(true); 2202 TestURLRequestContext context(true);
2076 context.set_network_delegate(&network_delegate); 2203 context.set_network_delegate(&network_delegate);
2077 context.Init(); 2204 context.Init();
2078 2205
2079 { 2206 {
2080 GURL original_url(test_server_.GetURL("empty.html")); 2207 GURL original_url(test_server_.GetURL("empty.html"));
2081 URLRequest r(original_url, &d, &context); 2208 URLRequest r(original_url, &d, &context);
(...skipping 21 matching lines...) Expand all
2103 2230
2104 // Tests that the network delegate can synchronously complete OnAuthRequired 2231 // Tests that the network delegate can synchronously complete OnAuthRequired
2105 // by taking no action. This indicates that the NetworkDelegate does not want to 2232 // by taking no action. This indicates that the NetworkDelegate does not want to
2106 // handle the challenge, and is passing the buck along to the 2233 // handle the challenge, and is passing the buck along to the
2107 // URLRequest::Delegate. 2234 // URLRequest::Delegate.
2108 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) { 2235 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) {
2109 ASSERT_TRUE(test_server_.Start()); 2236 ASSERT_TRUE(test_server_.Start());
2110 2237
2111 TestDelegate d; 2238 TestDelegate d;
2112 BlockingNetworkDelegate network_delegate; 2239 BlockingNetworkDelegate network_delegate;
2240 network_delegate.BlockOn(BlockingNetworkDelegateBase::ON_BEFORE_URL_REQUEST);
2113 network_delegate.set_auth_retval( 2241 network_delegate.set_auth_retval(
2114 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION); 2242 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION);
2115 2243
2116 TestURLRequestContext context(true); 2244 TestURLRequestContext context(true);
2117 context.set_network_delegate(&network_delegate); 2245 context.set_network_delegate(&network_delegate);
2118 context.Init(); 2246 context.Init();
2119 2247
2120 d.set_credentials(AuthCredentials(kUser, kSecret)); 2248 d.set_credentials(AuthCredentials(kUser, kSecret));
2121 2249
2122 { 2250 {
(...skipping 12 matching lines...) Expand all
2135 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2263 EXPECT_EQ(1, network_delegate.destroyed_requests());
2136 } 2264 }
2137 2265
2138 // Tests that the network delegate can synchronously complete OnAuthRequired 2266 // Tests that the network delegate can synchronously complete OnAuthRequired
2139 // by setting credentials. 2267 // by setting credentials.
2140 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) { 2268 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) {
2141 ASSERT_TRUE(test_server_.Start()); 2269 ASSERT_TRUE(test_server_.Start());
2142 2270
2143 TestDelegate d; 2271 TestDelegate d;
2144 BlockingNetworkDelegate network_delegate; 2272 BlockingNetworkDelegate network_delegate;
2273 network_delegate.BlockOn(BlockingNetworkDelegateBase::ON_BEFORE_URL_REQUEST);
2145 network_delegate.set_auth_retval( 2274 network_delegate.set_auth_retval(
2146 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); 2275 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
2147 2276
2148 network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); 2277 network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret));
2149 2278
2150 TestURLRequestContext context(true); 2279 TestURLRequestContext context(true);
2151 context.set_network_delegate(&network_delegate); 2280 context.set_network_delegate(&network_delegate);
2152 context.Init(); 2281 context.Init();
2153 2282
2154 { 2283 {
(...skipping 12 matching lines...) Expand all
2167 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2296 EXPECT_EQ(1, network_delegate.destroyed_requests());
2168 } 2297 }
2169 2298
2170 // Tests that the network delegate can synchronously complete OnAuthRequired 2299 // Tests that the network delegate can synchronously complete OnAuthRequired
2171 // by cancelling authentication. 2300 // by cancelling authentication.
2172 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) { 2301 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) {
2173 ASSERT_TRUE(test_server_.Start()); 2302 ASSERT_TRUE(test_server_.Start());
2174 2303
2175 TestDelegate d; 2304 TestDelegate d;
2176 BlockingNetworkDelegate network_delegate; 2305 BlockingNetworkDelegate network_delegate;
2306 network_delegate.BlockOn(BlockingNetworkDelegateBase::ON_BEFORE_URL_REQUEST);
2177 network_delegate.set_auth_retval( 2307 network_delegate.set_auth_retval(
2178 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); 2308 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH);
2179 2309
2180 TestURLRequestContext context(true); 2310 TestURLRequestContext context(true);
2181 context.set_network_delegate(&network_delegate); 2311 context.set_network_delegate(&network_delegate);
2182 context.Init(); 2312 context.Init();
2183 2313
2184 { 2314 {
2185 GURL url(test_server_.GetURL("auth-basic")); 2315 GURL url(test_server_.GetURL("auth-basic"));
2186 URLRequest r(url, &d, &context); 2316 URLRequest r(url, &d, &context);
(...skipping 12 matching lines...) Expand all
2199 2329
2200 // Tests that the network delegate can asynchronously complete OnAuthRequired 2330 // Tests that the network delegate can asynchronously complete OnAuthRequired
2201 // by taking no action. This indicates that the NetworkDelegate does not want 2331 // by taking no action. This indicates that the NetworkDelegate does not want
2202 // to handle the challenge, and is passing the buck along to the 2332 // to handle the challenge, and is passing the buck along to the
2203 // URLRequest::Delegate. 2333 // URLRequest::Delegate.
2204 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) { 2334 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) {
2205 ASSERT_TRUE(test_server_.Start()); 2335 ASSERT_TRUE(test_server_.Start());
2206 2336
2207 TestDelegate d; 2337 TestDelegate d;
2208 BlockingNetworkDelegate network_delegate; 2338 BlockingNetworkDelegate network_delegate;
2339 network_delegate.BlockOn(BlockingNetworkDelegateBase::ON_BEFORE_URL_REQUEST);
2209 network_delegate.set_auth_retval( 2340 network_delegate.set_auth_retval(
2210 NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING); 2341 NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING);
2211 network_delegate.set_auth_callback_retval( 2342 network_delegate.set_auth_callback_retval(
2212 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION); 2343 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION);
2213 2344
2214 TestURLRequestContext context(true); 2345 TestURLRequestContext context(true);
2215 context.set_network_delegate(&network_delegate); 2346 context.set_network_delegate(&network_delegate);
2216 context.Init(); 2347 context.Init();
2217 2348
2218 d.set_credentials(AuthCredentials(kUser, kSecret)); 2349 d.set_credentials(AuthCredentials(kUser, kSecret));
(...skipping 14 matching lines...) Expand all
2233 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2364 EXPECT_EQ(1, network_delegate.destroyed_requests());
2234 } 2365 }
2235 2366
2236 // Tests that the network delegate can asynchronously complete OnAuthRequired 2367 // Tests that the network delegate can asynchronously complete OnAuthRequired
2237 // by setting credentials. 2368 // by setting credentials.
2238 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) { 2369 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) {
2239 ASSERT_TRUE(test_server_.Start()); 2370 ASSERT_TRUE(test_server_.Start());
2240 2371
2241 TestDelegate d; 2372 TestDelegate d;
2242 BlockingNetworkDelegate network_delegate; 2373 BlockingNetworkDelegate network_delegate;
2374 network_delegate.BlockOn(BlockingNetworkDelegateBase::ON_BEFORE_URL_REQUEST);
2243 network_delegate.set_auth_retval( 2375 network_delegate.set_auth_retval(
2244 NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING); 2376 NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING);
2245 network_delegate.set_auth_callback_retval( 2377 network_delegate.set_auth_callback_retval(
2246 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); 2378 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
2247 2379
2248 AuthCredentials auth_credentials(kUser, kSecret); 2380 AuthCredentials auth_credentials(kUser, kSecret);
2249 network_delegate.set_auth_credentials(auth_credentials); 2381 network_delegate.set_auth_credentials(auth_credentials);
2250 2382
2251 TestURLRequestContext context(true); 2383 TestURLRequestContext context(true);
2252 context.set_network_delegate(&network_delegate); 2384 context.set_network_delegate(&network_delegate);
(...skipping 16 matching lines...) Expand all
2269 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2401 EXPECT_EQ(1, network_delegate.destroyed_requests());
2270 } 2402 }
2271 2403
2272 // Tests that the network delegate can asynchronously complete OnAuthRequired 2404 // Tests that the network delegate can asynchronously complete OnAuthRequired
2273 // by cancelling authentication. 2405 // by cancelling authentication.
2274 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) { 2406 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) {
2275 ASSERT_TRUE(test_server_.Start()); 2407 ASSERT_TRUE(test_server_.Start());
2276 2408
2277 TestDelegate d; 2409 TestDelegate d;
2278 BlockingNetworkDelegate network_delegate; 2410 BlockingNetworkDelegate network_delegate;
2411 network_delegate.BlockOn(BlockingNetworkDelegateBase::ON_BEFORE_URL_REQUEST);
2279 network_delegate.set_auth_retval( 2412 network_delegate.set_auth_retval(
2280 NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING); 2413 NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING);
2281 network_delegate.set_auth_callback_retval( 2414 network_delegate.set_auth_callback_retval(
2282 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); 2415 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH);
2283 2416
2284 TestURLRequestContext context(true); 2417 TestURLRequestContext context(true);
2285 context.set_network_delegate(&network_delegate); 2418 context.set_network_delegate(&network_delegate);
2286 context.Init(); 2419 context.Init();
2287 2420
2288 { 2421 {
(...skipping 13 matching lines...) Expand all
2302 } 2435 }
2303 2436
2304 // Tests that we can handle when a network request was canceled while we were 2437 // Tests that we can handle when a network request was canceled while we were
2305 // waiting for the network delegate. 2438 // waiting for the network delegate.
2306 // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback. 2439 // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback.
2307 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) { 2440 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) {
2308 ASSERT_TRUE(test_server_.Start()); 2441 ASSERT_TRUE(test_server_.Start());
2309 2442
2310 TestDelegate d; 2443 TestDelegate d;
2311 BlockingNetworkDelegateWithManualCallback network_delegate; 2444 BlockingNetworkDelegateWithManualCallback network_delegate;
2312 network_delegate.BlockOn( 2445 network_delegate.BlockOn(BlockingNetworkDelegateBase::ON_BEFORE_URL_REQUEST);
2313 BlockingNetworkDelegateWithManualCallback::ON_BEFORE_URL_REQUEST);
2314 2446
2315 TestURLRequestContext context(true); 2447 TestURLRequestContext context(true);
2316 context.set_network_delegate(&network_delegate); 2448 context.set_network_delegate(&network_delegate);
2317 context.Init(); 2449 context.Init();
2318 2450
2319 { 2451 {
2320 URLRequest r(test_server_.GetURL(""), &d, &context); 2452 URLRequest r(test_server_.GetURL(""), &d, &context);
2321 2453
2322 r.Start(); 2454 r.Start();
2323 network_delegate.WaitForState( 2455 network_delegate.WaitForState(
2324 BlockingNetworkDelegateWithManualCallback::ON_BEFORE_URL_REQUEST); 2456 BlockingNetworkDelegateBase::ON_BEFORE_URL_REQUEST);
2325 EXPECT_EQ(0, network_delegate.completed_requests()); 2457 EXPECT_EQ(0, network_delegate.completed_requests());
2326 // Cancel before callback. 2458 // Cancel before callback.
2327 r.Cancel(); 2459 r.Cancel();
2328 // Ensure that network delegate is notified. 2460 // Ensure that network delegate is notified.
2329 EXPECT_EQ(1, network_delegate.completed_requests()); 2461 EXPECT_EQ(1, network_delegate.completed_requests());
2330 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); 2462 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
2331 EXPECT_EQ(ERR_ABORTED, r.status().error()); 2463 EXPECT_EQ(ERR_ABORTED, r.status().error());
2332 EXPECT_EQ(1, network_delegate.created_requests()); 2464 EXPECT_EQ(1, network_delegate.created_requests());
2333 EXPECT_EQ(0, network_delegate.destroyed_requests()); 2465 EXPECT_EQ(0, network_delegate.destroyed_requests());
2334 } 2466 }
2335 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2467 EXPECT_EQ(1, network_delegate.destroyed_requests());
2336 } 2468 }
2337 2469
2338 // Tests that we can handle when a network request was canceled while we were 2470 // Tests that we can handle when a network request was canceled while we were
2339 // waiting for the network delegate. 2471 // waiting for the network delegate.
2340 // Part 2: Request is cancelled while waiting for OnBeforeSendHeaders callback. 2472 // Part 2: Request is cancelled while waiting for OnBeforeSendHeaders callback.
2341 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) { 2473 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) {
2342 ASSERT_TRUE(test_server_.Start()); 2474 ASSERT_TRUE(test_server_.Start());
2343 2475
2344 TestDelegate d; 2476 TestDelegate d;
2345 BlockingNetworkDelegateWithManualCallback network_delegate; 2477 BlockingNetworkDelegateWithManualCallback network_delegate;
2346 network_delegate.BlockOn( 2478 network_delegate.BlockOn(BlockingNetworkDelegateBase::ON_BEFORE_SEND_HEADERS);
2347 BlockingNetworkDelegateWithManualCallback::ON_BEFORE_SEND_HEADERS);
2348 2479
2349 TestURLRequestContext context(true); 2480 TestURLRequestContext context(true);
2350 context.set_network_delegate(&network_delegate); 2481 context.set_network_delegate(&network_delegate);
2351 context.Init(); 2482 context.Init();
2352 2483
2353 { 2484 {
2354 URLRequest r(test_server_.GetURL(""), &d, &context); 2485 URLRequest r(test_server_.GetURL(""), &d, &context);
2355 2486
2356 r.Start(); 2487 r.Start();
2357 network_delegate.WaitForState( 2488 network_delegate.WaitForState(
2358 BlockingNetworkDelegateWithManualCallback::ON_BEFORE_SEND_HEADERS); 2489 BlockingNetworkDelegateBase::ON_BEFORE_SEND_HEADERS);
2359 EXPECT_EQ(0, network_delegate.completed_requests()); 2490 EXPECT_EQ(0, network_delegate.completed_requests());
2360 // Cancel before callback. 2491 // Cancel before callback.
2361 r.Cancel(); 2492 r.Cancel();
2362 // Ensure that network delegate is notified. 2493 // Ensure that network delegate is notified.
2363 EXPECT_EQ(1, network_delegate.completed_requests()); 2494 EXPECT_EQ(1, network_delegate.completed_requests());
2364 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); 2495 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
2365 EXPECT_EQ(ERR_ABORTED, r.status().error()); 2496 EXPECT_EQ(ERR_ABORTED, r.status().error());
2366 EXPECT_EQ(1, network_delegate.created_requests()); 2497 EXPECT_EQ(1, network_delegate.created_requests());
2367 EXPECT_EQ(0, network_delegate.destroyed_requests()); 2498 EXPECT_EQ(0, network_delegate.destroyed_requests());
2368 } 2499 }
2369 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2500 EXPECT_EQ(1, network_delegate.destroyed_requests());
2370 } 2501 }
2371 2502
2372 // Tests that we can handle when a network request was canceled while we were 2503 // Tests that we can handle when a network request was canceled while we were
2373 // waiting for the network delegate. 2504 // waiting for the network delegate.
2374 // Part 3: Request is cancelled while waiting for OnHeadersReceived callback. 2505 // Part 3: Request is cancelled while waiting for OnHeadersReceived callback.
2375 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) { 2506 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) {
2376 ASSERT_TRUE(test_server_.Start()); 2507 ASSERT_TRUE(test_server_.Start());
2377 2508
2378 TestDelegate d; 2509 TestDelegate d;
2379 BlockingNetworkDelegateWithManualCallback network_delegate; 2510 BlockingNetworkDelegateWithManualCallback network_delegate;
2380 network_delegate.BlockOn( 2511 network_delegate.BlockOn(BlockingNetworkDelegateBase::ON_HEADERS_RECEIVED);
2381 BlockingNetworkDelegateWithManualCallback::ON_HEADERS_RECEIVED);
2382 2512
2383 TestURLRequestContext context(true); 2513 TestURLRequestContext context(true);
2384 context.set_network_delegate(&network_delegate); 2514 context.set_network_delegate(&network_delegate);
2385 context.Init(); 2515 context.Init();
2386 2516
2387 { 2517 {
2388 URLRequest r(test_server_.GetURL(""), &d, &context); 2518 URLRequest r(test_server_.GetURL(""), &d, &context);
2389 2519
2390 r.Start(); 2520 r.Start();
2391 network_delegate.WaitForState( 2521 network_delegate.WaitForState(
2392 BlockingNetworkDelegateWithManualCallback::ON_HEADERS_RECEIVED); 2522 BlockingNetworkDelegateBase::ON_HEADERS_RECEIVED);
2393 EXPECT_EQ(0, network_delegate.completed_requests()); 2523 EXPECT_EQ(0, network_delegate.completed_requests());
2394 // Cancel before callback. 2524 // Cancel before callback.
2395 r.Cancel(); 2525 r.Cancel();
2396 // Ensure that network delegate is notified. 2526 // Ensure that network delegate is notified.
2397 EXPECT_EQ(1, network_delegate.completed_requests()); 2527 EXPECT_EQ(1, network_delegate.completed_requests());
2398 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); 2528 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
2399 EXPECT_EQ(ERR_ABORTED, r.status().error()); 2529 EXPECT_EQ(ERR_ABORTED, r.status().error());
2400 EXPECT_EQ(1, network_delegate.created_requests()); 2530 EXPECT_EQ(1, network_delegate.created_requests());
2401 EXPECT_EQ(0, network_delegate.destroyed_requests()); 2531 EXPECT_EQ(0, network_delegate.destroyed_requests());
2402 } 2532 }
2403 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2533 EXPECT_EQ(1, network_delegate.destroyed_requests());
2404 } 2534 }
2405 2535
2406 // Tests that we can handle when a network request was canceled while we were 2536 // Tests that we can handle when a network request was canceled while we were
2407 // waiting for the network delegate. 2537 // waiting for the network delegate.
2408 // Part 4: Request is cancelled while waiting for OnAuthRequired callback. 2538 // Part 4: Request is cancelled while waiting for OnAuthRequired callback.
2409 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) { 2539 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) {
2410 ASSERT_TRUE(test_server_.Start()); 2540 ASSERT_TRUE(test_server_.Start());
2411 2541
2412 TestDelegate d; 2542 TestDelegate d;
2413 BlockingNetworkDelegateWithManualCallback network_delegate; 2543 BlockingNetworkDelegateWithManualCallback network_delegate;
2414 network_delegate.BlockOn( 2544 network_delegate.BlockOn(
2415 BlockingNetworkDelegateWithManualCallback::ON_AUTH_REQUIRED); 2545 BlockingNetworkDelegateBase::ON_AUTH_REQUIRED);
2416 2546
2417 TestURLRequestContext context(true); 2547 TestURLRequestContext context(true);
2418 context.set_network_delegate(&network_delegate); 2548 context.set_network_delegate(&network_delegate);
2419 context.Init(); 2549 context.Init();
2420 2550
2421 { 2551 {
2422 URLRequest r(test_server_.GetURL("auth-basic"), &d, &context); 2552 URLRequest r(test_server_.GetURL("auth-basic"), &d, &context);
2423 2553
2424 r.Start(); 2554 r.Start();
2425 network_delegate.WaitForState( 2555 network_delegate.WaitForState(
2426 BlockingNetworkDelegateWithManualCallback::ON_AUTH_REQUIRED); 2556 BlockingNetworkDelegateBase::ON_AUTH_REQUIRED);
2427 EXPECT_EQ(0, network_delegate.completed_requests()); 2557 EXPECT_EQ(0, network_delegate.completed_requests());
2428 // Cancel before callback. 2558 // Cancel before callback.
2429 r.Cancel(); 2559 r.Cancel();
2430 // Ensure that network delegate is notified. 2560 // Ensure that network delegate is notified.
2431 EXPECT_EQ(1, network_delegate.completed_requests()); 2561 EXPECT_EQ(1, network_delegate.completed_requests());
2432 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); 2562 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
2433 EXPECT_EQ(ERR_ABORTED, r.status().error()); 2563 EXPECT_EQ(ERR_ABORTED, r.status().error());
2434 EXPECT_EQ(1, network_delegate.created_requests()); 2564 EXPECT_EQ(1, network_delegate.created_requests());
2435 EXPECT_EQ(0, network_delegate.destroyed_requests()); 2565 EXPECT_EQ(0, network_delegate.destroyed_requests());
2436 } 2566 }
(...skipping 2249 matching lines...) Expand 10 before | Expand all | Expand 10 after
4686 4816
4687 EXPECT_FALSE(r.is_pending()); 4817 EXPECT_FALSE(r.is_pending());
4688 EXPECT_EQ(1, d->response_started_count()); 4818 EXPECT_EQ(1, d->response_started_count());
4689 EXPECT_FALSE(d->received_data_before_response()); 4819 EXPECT_FALSE(d->received_data_before_response());
4690 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); 4820 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
4691 } 4821 }
4692 } 4822 }
4693 #endif // !defined(DISABLE_FTP_SUPPORT) 4823 #endif // !defined(DISABLE_FTP_SUPPORT)
4694 4824
4695 } // namespace net 4825 } // namespace net
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698