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

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