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

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