Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 { |
| 173 SYNCHRONOUS, // No callback, returns specified return values. | |
| 174 AUTO_CALLBACK, // |this| posts a task to run the callback using the | |
| 175 // specified 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 block_mode); | |
| 182 | |
| 183 // Setters. | |
| 184 void set_retval(int retval) { | |
| 185 ASSERT_NE(ERR_IO_PENDING, retval); | |
| 186 ASSERT_NE(OK, retval); | |
|
mmenke
2012/09/20 15:37:52
OK actually makes sense, if BlockMode is AUTO_CALL
vabr (Chromium)
2012/09/21 11:11:36
But in that case OK is the default value.
Until th
| |
| 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 block_mode) | |
| 288 : block_mode_(block_mode), | |
| 289 retval_(block_mode == USER_CALLBACK ? ERR_IO_PENDING : OK), | |
| 290 auth_retval_(block_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 EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); | |
| 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); | |
|
mmenke
2012/09/20 15:37:52
nit: Reverse order
vabr (Chromium)
2012/09/21 11:11:36
Done.
| |
| 377 ASSERT_NE(NOT_BLOCKED, stage_blocked_for_callback_); | |
| 378 ASSERT_NE(ON_AUTH_REQUIRED, stage_blocked_for_callback_); | |
| 379 CompletionCallback callback = callback_; | |
| 380 Reset(); | |
| 381 RunCallback(response, callback); | |
| 382 } | |
| 383 | |
| 384 void BlockingNetworkDelegate::DoAuthCallback( | |
| 385 NetworkDelegate::AuthRequiredResponse response) { | |
| 386 ASSERT_EQ(block_mode_, USER_CALLBACK); | |
|
mmenke
2012/09/20 15:37:52
nit: Reverse order
vabr (Chromium)
2012/09/21 11:11:36
Done.
| |
| 387 ASSERT_EQ(ON_AUTH_REQUIRED, stage_blocked_for_callback_); | |
| 388 AuthCallback auth_callback = auth_callback_; | |
| 389 Reset(); | |
| 390 RunAuthCallback(response, auth_callback); | |
| 391 } | |
| 392 | |
| 393 void BlockingNetworkDelegate::RunCallback(int response, | |
| 394 const CompletionCallback& callback) { | |
| 395 callback.Run(response); | |
| 396 } | |
| 397 | |
| 398 void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response, | |
| 399 const AuthCallback& callback) { | |
| 400 if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) { | |
| 401 ASSERT_TRUE(target_auth_credentials_ != NULL); | |
| 402 *target_auth_credentials_ = auth_credentials_; | |
| 403 } | |
| 404 callback.Run(response); | |
| 405 } | |
| 406 | |
| 407 int BlockingNetworkDelegate::MaybeBlockStage( | |
| 408 BlockingNetworkDelegate::Stage stage, | |
| 409 const CompletionCallback& callback) { | |
| 410 EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); | |
| 411 | |
| 412 if ((block_on_ & stage) == 0) { | |
| 413 return OK; | |
| 414 } | |
| 415 | |
| 416 EXPECT_NE(OK, retval_); | |
|
mmenke
2012/09/20 15:37:52
OK is actually OK for the AUTO_CALLBACK case. Doe
vabr (Chromium)
2012/09/21 11:11:36
Done.
| |
| 417 if (block_mode_ != SYNCHRONOUS) | |
| 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 } | |
| 438 | |
| 358 class TestURLRequestContextWithProxy : public TestURLRequestContext { | 439 class TestURLRequestContextWithProxy : public TestURLRequestContext { |
| 359 public: | 440 public: |
| 360 // Does not own |delegate|. | 441 // Does not own |delegate|. |
| 361 TestURLRequestContextWithProxy(const std::string& proxy, | 442 TestURLRequestContextWithProxy(const std::string& proxy, |
| 362 NetworkDelegate* delegate) | 443 NetworkDelegate* delegate) |
| 363 : TestURLRequestContext(true) { | 444 : TestURLRequestContext(true) { |
| 364 context_storage_.set_proxy_service(ProxyService::CreateFixed(proxy)); | 445 context_storage_.set_proxy_service(ProxyService::CreateFixed(proxy)); |
| 365 set_network_delegate(delegate); | 446 set_network_delegate(delegate); |
| 366 Init(); | 447 Init(); |
| 367 } | 448 } |
| (...skipping 1539 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1907 EXPECT_EQ(1, network_delegate.error_count()); | 1988 EXPECT_EQ(1, network_delegate.error_count()); |
| 1908 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, network_delegate.last_error()); | 1989 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, network_delegate.last_error()); |
| 1909 } | 1990 } |
| 1910 } | 1991 } |
| 1911 | 1992 |
| 1912 // Tests that the network delegate can block and cancel a request. | 1993 // Tests that the network delegate can block and cancel a request. |
| 1913 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) { | 1994 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) { |
| 1914 ASSERT_TRUE(test_server_.Start()); | 1995 ASSERT_TRUE(test_server_.Start()); |
| 1915 | 1996 |
| 1916 TestDelegate d; | 1997 TestDelegate d; |
| 1917 BlockingNetworkDelegate network_delegate; | 1998 BlockingNetworkDelegate network_delegate( |
| 1918 network_delegate.set_callback_retval(ERR_EMPTY_RESPONSE); | 1999 BlockingNetworkDelegate::AUTO_CALLBACK); |
| 2000 network_delegate.BlockOn(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); | |
| 2001 network_delegate.set_retval(ERR_EMPTY_RESPONSE); | |
| 1919 | 2002 |
| 1920 TestURLRequestContextWithProxy context( | 2003 TestURLRequestContextWithProxy context( |
| 1921 test_server_.host_port_pair().ToString(), | 2004 test_server_.host_port_pair().ToString(), |
| 1922 &network_delegate); | 2005 &network_delegate); |
| 1923 | 2006 |
| 1924 { | 2007 { |
| 1925 URLRequest r(test_server_.GetURL(""), &d, &context); | 2008 URLRequest r(test_server_.GetURL(""), &d, &context); |
| 1926 | 2009 |
| 1927 r.Start(); | 2010 r.Start(); |
| 1928 MessageLoop::current()->Run(); | 2011 MessageLoop::current()->Run(); |
| 1929 | 2012 |
| 1930 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); | 2013 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); |
| 1931 EXPECT_EQ(ERR_EMPTY_RESPONSE, r.status().error()); | 2014 EXPECT_EQ(ERR_EMPTY_RESPONSE, r.status().error()); |
| 1932 EXPECT_EQ(1, network_delegate.created_requests()); | 2015 EXPECT_EQ(1, network_delegate.created_requests()); |
| 1933 EXPECT_EQ(0, network_delegate.destroyed_requests()); | 2016 EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 1934 } | 2017 } |
| 1935 EXPECT_EQ(1, network_delegate.destroyed_requests()); | 2018 EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 1936 } | 2019 } |
| 1937 | 2020 |
| 1938 // Tests that the network delegate can cancel a request synchronously. | 2021 // Tests that the network delegate can cancel a request synchronously. |
| 1939 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously) { | 2022 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously) { |
| 1940 ASSERT_TRUE(test_server_.Start()); | 2023 ASSERT_TRUE(test_server_.Start()); |
| 1941 | 2024 |
| 1942 TestDelegate d; | 2025 TestDelegate d; |
| 1943 BlockingNetworkDelegate network_delegate; | 2026 BlockingNetworkDelegate network_delegate( |
| 2027 BlockingNetworkDelegate::AUTO_CALLBACK); | |
|
mmenke
2012/09/20 15:37:52
This changes behavior. It should be SYNCHRONOUS.
vabr (Chromium)
2012/09/21 11:11:36
Done.
| |
| 2028 network_delegate.BlockOn(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); | |
| 1944 network_delegate.set_retval(ERR_EMPTY_RESPONSE); | 2029 network_delegate.set_retval(ERR_EMPTY_RESPONSE); |
| 1945 | 2030 |
| 1946 TestURLRequestContextWithProxy context( | 2031 TestURLRequestContextWithProxy context( |
| 1947 test_server_.host_port_pair().ToString(), | 2032 test_server_.host_port_pair().ToString(), |
| 1948 &network_delegate); | 2033 &network_delegate); |
| 1949 | 2034 |
| 1950 { | 2035 { |
| 1951 URLRequest r(test_server_.GetURL(""), &d, &context); | 2036 URLRequest r(test_server_.GetURL(""), &d, &context); |
| 1952 | 2037 |
| 1953 r.Start(); | 2038 r.Start(); |
| 1954 MessageLoop::current()->Run(); | 2039 MessageLoop::current()->Run(); |
| 1955 | 2040 |
| 1956 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); | 2041 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); |
| 1957 EXPECT_EQ(ERR_EMPTY_RESPONSE, r.status().error()); | 2042 EXPECT_EQ(ERR_EMPTY_RESPONSE, r.status().error()); |
| 1958 EXPECT_EQ(1, network_delegate.created_requests()); | 2043 EXPECT_EQ(1, network_delegate.created_requests()); |
| 1959 EXPECT_EQ(0, network_delegate.destroyed_requests()); | 2044 EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 1960 } | 2045 } |
| 1961 EXPECT_EQ(1, network_delegate.destroyed_requests()); | 2046 EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 1962 } | 2047 } |
| 1963 | 2048 |
| 1964 // Tests that the network delegate can block and redirect a request to a new | 2049 // Tests that the network delegate can block and redirect a request to a new |
| 1965 // URL. | 2050 // URL. |
| 1966 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) { | 2051 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) { |
|
vabr (Chromium)
2012/09/20 11:45:44
Given that redirect now is done independently of b
mmenke
2012/09/20 15:37:52
Ahh...There's a bug here. This should use network
mmenke
2012/09/20 15:40:49
Sorry - comment below is more accurate than the on
vabr (Chromium)
2012/09/21 11:11:36
Done.
| |
| 1967 ASSERT_TRUE(test_server_.Start()); | 2052 ASSERT_TRUE(test_server_.Start()); |
| 1968 | 2053 |
| 1969 TestDelegate d; | 2054 TestDelegate d; |
| 1970 BlockingNetworkDelegate network_delegate; | 2055 BlockingNetworkDelegate network_delegate( |
| 2056 BlockingNetworkDelegate::AUTO_CALLBACK); | |
| 1971 GURL redirect_url(test_server_.GetURL("simple.html")); | 2057 GURL redirect_url(test_server_.GetURL("simple.html")); |
| 1972 network_delegate.set_redirect_url(redirect_url); | 2058 network_delegate.set_redirect_url(redirect_url); |
| 1973 | 2059 |
| 1974 TestURLRequestContextWithProxy context( | 2060 TestURLRequestContextWithProxy context( |
| 1975 test_server_.host_port_pair().ToString(), | 2061 test_server_.host_port_pair().ToString(), |
| 1976 &network_delegate); | 2062 &network_delegate); |
| 1977 | 2063 |
| 1978 { | 2064 { |
| 1979 GURL original_url(test_server_.GetURL("empty.html")); | 2065 GURL original_url(test_server_.GetURL("empty.html")); |
| 1980 URLRequest r(original_url, &d, &context); | 2066 URLRequest r(original_url, &d, &context); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 1992 } | 2078 } |
| 1993 EXPECT_EQ(1, network_delegate.destroyed_requests()); | 2079 EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 1994 } | 2080 } |
| 1995 | 2081 |
| 1996 // Tests that the network delegate can block and redirect a request to a new | 2082 // 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. | 2083 // URL by setting a redirect_url and returning in OnBeforeURLRequest directly. |
| 1998 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) { | 2084 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) { |
| 1999 ASSERT_TRUE(test_server_.Start()); | 2085 ASSERT_TRUE(test_server_.Start()); |
| 2000 | 2086 |
| 2001 TestDelegate d; | 2087 TestDelegate d; |
| 2002 BlockingNetworkDelegate network_delegate; | 2088 BlockingNetworkDelegate network_delegate( |
| 2089 BlockingNetworkDelegate::SYNCHRONOUS); | |
| 2003 GURL redirect_url(test_server_.GetURL("simple.html")); | 2090 GURL redirect_url(test_server_.GetURL("simple.html")); |
| 2004 network_delegate.set_redirect_url(redirect_url); | 2091 network_delegate.set_redirect_url(redirect_url); |
| 2005 network_delegate.set_retval(OK); | |
| 2006 | 2092 |
| 2007 TestURLRequestContextWithProxy context( | 2093 TestURLRequestContextWithProxy context( |
| 2008 test_server_.host_port_pair().ToString(), | 2094 test_server_.host_port_pair().ToString(), |
| 2009 &network_delegate); | 2095 &network_delegate); |
| 2010 | 2096 |
| 2011 { | 2097 { |
| 2012 GURL original_url(test_server_.GetURL("empty.html")); | 2098 GURL original_url(test_server_.GetURL("empty.html")); |
| 2013 URLRequest r(original_url, &d, &context); | 2099 URLRequest r(original_url, &d, &context); |
| 2014 | 2100 |
| 2015 r.Start(); | 2101 r.Start(); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 2026 EXPECT_EQ(1, network_delegate.destroyed_requests()); | 2112 EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2027 } | 2113 } |
| 2028 | 2114 |
| 2029 // Tests that redirects caused by the network delegate preserve POST data. | 2115 // Tests that redirects caused by the network delegate preserve POST data. |
| 2030 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) { | 2116 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) { |
| 2031 ASSERT_TRUE(test_server_.Start()); | 2117 ASSERT_TRUE(test_server_.Start()); |
| 2032 | 2118 |
| 2033 const char kData[] = "hello world"; | 2119 const char kData[] = "hello world"; |
| 2034 | 2120 |
| 2035 TestDelegate d; | 2121 TestDelegate d; |
| 2036 BlockingNetworkDelegate network_delegate; | 2122 BlockingNetworkDelegate network_delegate( |
| 2123 BlockingNetworkDelegate::SYNCHRONOUS); | |
|
mmenke
2012/09/20 15:37:52
This changes behavior. It should be AUTO_CALLBACK
mmenke
2012/09/20 15:37:52
If we want to preserve behavior here, we should ma
vabr (Chromium)
2012/09/21 11:11:36
Why headers? I don't think the original BlockingNe
vabr (Chromium)
2012/09/21 11:11:36
Done.
mmenke
2012/09/21 15:15:59
Err...I meant on before send, actually (Hence the
| |
| 2037 GURL redirect_url(test_server_.GetURL("echo")); | 2124 GURL redirect_url(test_server_.GetURL("echo")); |
| 2038 network_delegate.set_redirect_url(redirect_url); | 2125 network_delegate.set_redirect_url(redirect_url); |
| 2039 | 2126 |
| 2040 TestURLRequestContext context(true); | 2127 TestURLRequestContext context(true); |
| 2041 context.set_network_delegate(&network_delegate); | 2128 context.set_network_delegate(&network_delegate); |
| 2042 context.Init(); | 2129 context.Init(); |
| 2043 | 2130 |
| 2044 { | 2131 { |
| 2045 GURL original_url(test_server_.GetURL("empty.html")); | 2132 GURL original_url(test_server_.GetURL("empty.html")); |
| 2046 URLRequest r(original_url, &d, &context); | 2133 URLRequest r(original_url, &d, &context); |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 2067 } | 2154 } |
| 2068 | 2155 |
| 2069 // Tests that the network delegate can synchronously complete OnAuthRequired | 2156 // Tests that the network delegate can synchronously complete OnAuthRequired |
| 2070 // by taking no action. This indicates that the NetworkDelegate does not want to | 2157 // 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 | 2158 // handle the challenge, and is passing the buck along to the |
| 2072 // URLRequest::Delegate. | 2159 // URLRequest::Delegate. |
| 2073 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) { | 2160 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) { |
| 2074 ASSERT_TRUE(test_server_.Start()); | 2161 ASSERT_TRUE(test_server_.Start()); |
| 2075 | 2162 |
| 2076 TestDelegate d; | 2163 TestDelegate d; |
| 2077 BlockingNetworkDelegate network_delegate; | 2164 BlockingNetworkDelegate network_delegate( |
| 2078 network_delegate.set_auth_retval( | 2165 BlockingNetworkDelegate::AUTO_CALLBACK); |
|
mmenke
2012/09/20 15:37:52
This should be SYNCHRONOUS.
vabr (Chromium)
2012/09/21 11:11:36
Done.
| |
| 2079 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION); | 2166 network_delegate.BlockOn(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
| 2080 | 2167 |
| 2081 TestURLRequestContext context(true); | 2168 TestURLRequestContext context(true); |
| 2082 context.set_network_delegate(&network_delegate); | 2169 context.set_network_delegate(&network_delegate); |
| 2083 context.Init(); | 2170 context.Init(); |
| 2084 | 2171 |
| 2085 d.set_credentials(AuthCredentials(kUser, kSecret)); | 2172 d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 2086 | 2173 |
| 2087 { | 2174 { |
| 2088 GURL url(test_server_.GetURL("auth-basic")); | 2175 GURL url(test_server_.GetURL("auth-basic")); |
| 2089 URLRequest r(url, &d, &context); | 2176 URLRequest r(url, &d, &context); |
| 2090 r.Start(); | 2177 r.Start(); |
| 2091 MessageLoop::current()->Run(); | 2178 MessageLoop::current()->Run(); |
| 2092 | 2179 |
| 2093 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); | 2180 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 2094 EXPECT_EQ(0, r.status().error()); | 2181 EXPECT_EQ(0, r.status().error()); |
| 2095 EXPECT_EQ(200, r.GetResponseCode()); | 2182 EXPECT_EQ(200, r.GetResponseCode()); |
| 2096 EXPECT_TRUE(d.auth_required_called()); | 2183 EXPECT_TRUE(d.auth_required_called()); |
| 2097 EXPECT_EQ(1, network_delegate.created_requests()); | 2184 EXPECT_EQ(1, network_delegate.created_requests()); |
| 2098 EXPECT_EQ(0, network_delegate.destroyed_requests()); | 2185 EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2099 } | 2186 } |
| 2100 EXPECT_EQ(1, network_delegate.destroyed_requests()); | 2187 EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2101 } | 2188 } |
| 2102 | 2189 |
| 2103 // Tests that the network delegate can synchronously complete OnAuthRequired | 2190 // Tests that the network delegate can synchronously complete OnAuthRequired |
| 2104 // by setting credentials. | 2191 // by setting credentials. |
| 2105 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) { | 2192 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) { |
| 2106 ASSERT_TRUE(test_server_.Start()); | 2193 ASSERT_TRUE(test_server_.Start()); |
| 2107 | 2194 |
| 2108 TestDelegate d; | 2195 TestDelegate d; |
| 2109 BlockingNetworkDelegate network_delegate; | 2196 BlockingNetworkDelegate network_delegate( |
| 2197 BlockingNetworkDelegate::AUTO_CALLBACK); | |
|
mmenke
2012/09/20 15:37:52
SYNCHRONOUS
vabr (Chromium)
2012/09/21 11:11:36
Done.
| |
| 2198 network_delegate.BlockOn(BlockingNetworkDelegate::ON_AUTH_REQUIRED); | |
| 2110 network_delegate.set_auth_retval( | 2199 network_delegate.set_auth_retval( |
| 2111 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); | 2200 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 2112 | 2201 |
| 2113 network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); | 2202 network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
| 2114 | 2203 |
| 2115 TestURLRequestContext context(true); | 2204 TestURLRequestContext context(true); |
| 2116 context.set_network_delegate(&network_delegate); | 2205 context.set_network_delegate(&network_delegate); |
| 2117 context.Init(); | 2206 context.Init(); |
| 2118 | 2207 |
| 2119 { | 2208 { |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 2131 } | 2220 } |
| 2132 EXPECT_EQ(1, network_delegate.destroyed_requests()); | 2221 EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2133 } | 2222 } |
| 2134 | 2223 |
| 2135 // Tests that the network delegate can synchronously complete OnAuthRequired | 2224 // Tests that the network delegate can synchronously complete OnAuthRequired |
| 2136 // by cancelling authentication. | 2225 // by cancelling authentication. |
| 2137 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) { | 2226 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) { |
| 2138 ASSERT_TRUE(test_server_.Start()); | 2227 ASSERT_TRUE(test_server_.Start()); |
| 2139 | 2228 |
| 2140 TestDelegate d; | 2229 TestDelegate d; |
| 2141 BlockingNetworkDelegate network_delegate; | 2230 BlockingNetworkDelegate network_delegate( |
| 2231 BlockingNetworkDelegate::AUTO_CALLBACK); | |
|
mmenke
2012/09/20 15:37:52
SYNCHRONOUS
vabr (Chromium)
2012/09/21 11:11:36
Done.
| |
| 2232 network_delegate.BlockOn(BlockingNetworkDelegate::ON_AUTH_REQUIRED); | |
| 2142 network_delegate.set_auth_retval( | 2233 network_delegate.set_auth_retval( |
| 2143 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); | 2234 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 2144 | 2235 |
| 2145 TestURLRequestContext context(true); | 2236 TestURLRequestContext context(true); |
| 2146 context.set_network_delegate(&network_delegate); | 2237 context.set_network_delegate(&network_delegate); |
| 2147 context.Init(); | 2238 context.Init(); |
| 2148 | 2239 |
| 2149 { | 2240 { |
| 2150 GURL url(test_server_.GetURL("auth-basic")); | 2241 GURL url(test_server_.GetURL("auth-basic")); |
| 2151 URLRequest r(url, &d, &context); | 2242 URLRequest r(url, &d, &context); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 2163 } | 2254 } |
| 2164 | 2255 |
| 2165 // Tests that the network delegate can asynchronously complete OnAuthRequired | 2256 // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 2166 // by taking no action. This indicates that the NetworkDelegate does not want | 2257 // 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 | 2258 // to handle the challenge, and is passing the buck along to the |
| 2168 // URLRequest::Delegate. | 2259 // URLRequest::Delegate. |
| 2169 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) { | 2260 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) { |
| 2170 ASSERT_TRUE(test_server_.Start()); | 2261 ASSERT_TRUE(test_server_.Start()); |
| 2171 | 2262 |
| 2172 TestDelegate d; | 2263 TestDelegate d; |
| 2173 BlockingNetworkDelegate network_delegate; | 2264 BlockingNetworkDelegate network_delegate( |
| 2265 BlockingNetworkDelegate::AUTO_CALLBACK); | |
| 2266 network_delegate.BlockOn(BlockingNetworkDelegate::ON_AUTH_REQUIRED); | |
| 2174 network_delegate.set_auth_retval( | 2267 network_delegate.set_auth_retval( |
| 2175 NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING); | |
| 2176 network_delegate.set_auth_callback_retval( | |
| 2177 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION); | 2268 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION); |
| 2178 | 2269 |
| 2179 TestURLRequestContext context(true); | 2270 TestURLRequestContext context(true); |
| 2180 context.set_network_delegate(&network_delegate); | 2271 context.set_network_delegate(&network_delegate); |
| 2181 context.Init(); | 2272 context.Init(); |
| 2182 | 2273 |
| 2183 d.set_credentials(AuthCredentials(kUser, kSecret)); | 2274 d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 2184 | 2275 |
| 2185 { | 2276 { |
| 2186 GURL url(test_server_.GetURL("auth-basic")); | 2277 GURL url(test_server_.GetURL("auth-basic")); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 2197 } | 2288 } |
| 2198 EXPECT_EQ(1, network_delegate.destroyed_requests()); | 2289 EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2199 } | 2290 } |
| 2200 | 2291 |
| 2201 // Tests that the network delegate can asynchronously complete OnAuthRequired | 2292 // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 2202 // by setting credentials. | 2293 // by setting credentials. |
| 2203 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) { | 2294 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) { |
| 2204 ASSERT_TRUE(test_server_.Start()); | 2295 ASSERT_TRUE(test_server_.Start()); |
| 2205 | 2296 |
| 2206 TestDelegate d; | 2297 TestDelegate d; |
| 2207 BlockingNetworkDelegate network_delegate; | 2298 BlockingNetworkDelegate network_delegate( |
| 2299 BlockingNetworkDelegate::AUTO_CALLBACK); | |
| 2300 network_delegate.BlockOn(BlockingNetworkDelegate::ON_AUTH_REQUIRED); | |
| 2208 network_delegate.set_auth_retval( | 2301 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); | 2302 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 2212 | 2303 |
| 2213 AuthCredentials auth_credentials(kUser, kSecret); | 2304 AuthCredentials auth_credentials(kUser, kSecret); |
| 2214 network_delegate.set_auth_credentials(auth_credentials); | 2305 network_delegate.set_auth_credentials(auth_credentials); |
| 2215 | 2306 |
| 2216 TestURLRequestContext context(true); | 2307 TestURLRequestContext context(true); |
| 2217 context.set_network_delegate(&network_delegate); | 2308 context.set_network_delegate(&network_delegate); |
| 2218 context.Init(); | 2309 context.Init(); |
| 2219 | 2310 |
| 2220 { | 2311 { |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 2233 } | 2324 } |
| 2234 EXPECT_EQ(1, network_delegate.destroyed_requests()); | 2325 EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2235 } | 2326 } |
| 2236 | 2327 |
| 2237 // Tests that the network delegate can asynchronously complete OnAuthRequired | 2328 // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 2238 // by cancelling authentication. | 2329 // by cancelling authentication. |
| 2239 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) { | 2330 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) { |
| 2240 ASSERT_TRUE(test_server_.Start()); | 2331 ASSERT_TRUE(test_server_.Start()); |
| 2241 | 2332 |
| 2242 TestDelegate d; | 2333 TestDelegate d; |
| 2243 BlockingNetworkDelegate network_delegate; | 2334 BlockingNetworkDelegate network_delegate( |
| 2335 BlockingNetworkDelegate::AUTO_CALLBACK); | |
| 2336 network_delegate.BlockOn(BlockingNetworkDelegate::ON_AUTH_REQUIRED); | |
| 2244 network_delegate.set_auth_retval( | 2337 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); | 2338 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 2248 | 2339 |
| 2249 TestURLRequestContext context(true); | 2340 TestURLRequestContext context(true); |
| 2250 context.set_network_delegate(&network_delegate); | 2341 context.set_network_delegate(&network_delegate); |
| 2251 context.Init(); | 2342 context.Init(); |
| 2252 | 2343 |
| 2253 { | 2344 { |
| 2254 GURL url(test_server_.GetURL("auth-basic")); | 2345 GURL url(test_server_.GetURL("auth-basic")); |
| 2255 URLRequest r(url, &d, &context); | 2346 URLRequest r(url, &d, &context); |
| 2256 r.Start(); | 2347 r.Start(); |
| 2257 MessageLoop::current()->Run(); | 2348 MessageLoop::current()->Run(); |
| 2258 | 2349 |
| 2259 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); | 2350 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 2260 EXPECT_EQ(OK, r.status().error()); | 2351 EXPECT_EQ(OK, r.status().error()); |
| 2261 EXPECT_EQ(401, r.GetResponseCode()); | 2352 EXPECT_EQ(401, r.GetResponseCode()); |
| 2262 EXPECT_FALSE(d.auth_required_called()); | 2353 EXPECT_FALSE(d.auth_required_called()); |
| 2263 EXPECT_EQ(1, network_delegate.created_requests()); | 2354 EXPECT_EQ(1, network_delegate.created_requests()); |
| 2264 EXPECT_EQ(0, network_delegate.destroyed_requests()); | 2355 EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2265 } | 2356 } |
| 2266 EXPECT_EQ(1, network_delegate.destroyed_requests()); | 2357 EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2267 } | 2358 } |
| 2268 | 2359 |
| 2269 // Tests that we can handle when a network request was canceled while we were | 2360 // Tests that we can handle when a network request was canceled while we were |
| 2270 // waiting for the network delegate. | 2361 // waiting for the network delegate. |
| 2271 // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback. | 2362 // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback. |
| 2272 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) { | 2363 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) { |
| 2273 ASSERT_TRUE(test_server_.Start()); | 2364 ASSERT_TRUE(test_server_.Start()); |
| 2274 | 2365 |
| 2275 TestDelegate d; | 2366 TestDelegate d; |
| 2276 BlockingNetworkDelegateWithManualCallback network_delegate; | 2367 BlockingNetworkDelegate network_delegate( |
| 2277 network_delegate.BlockOn( | 2368 BlockingNetworkDelegate::USER_CALLBACK); |
| 2278 BlockingNetworkDelegateWithManualCallback::ON_BEFORE_URL_REQUEST); | 2369 network_delegate.BlockOn(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
| 2279 | 2370 |
| 2280 TestURLRequestContext context(true); | 2371 TestURLRequestContext context(true); |
| 2281 context.set_network_delegate(&network_delegate); | 2372 context.set_network_delegate(&network_delegate); |
| 2282 context.Init(); | 2373 context.Init(); |
| 2283 | 2374 |
| 2284 { | 2375 { |
| 2285 URLRequest r(test_server_.GetURL(""), &d, &context); | 2376 URLRequest r(test_server_.GetURL(""), &d, &context); |
| 2286 | 2377 |
| 2287 r.Start(); | 2378 r.Start(); |
| 2288 network_delegate.WaitForState( | 2379 network_delegate.WaitForState( |
| 2289 BlockingNetworkDelegateWithManualCallback::ON_BEFORE_URL_REQUEST); | 2380 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
| 2290 EXPECT_EQ(0, network_delegate.completed_requests()); | 2381 EXPECT_EQ(0, network_delegate.completed_requests()); |
| 2291 // Cancel before callback. | 2382 // Cancel before callback. |
| 2292 r.Cancel(); | 2383 r.Cancel(); |
| 2293 // Ensure that network delegate is notified. | 2384 // Ensure that network delegate is notified. |
| 2294 EXPECT_EQ(1, network_delegate.completed_requests()); | 2385 EXPECT_EQ(1, network_delegate.completed_requests()); |
| 2295 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); | 2386 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
| 2296 EXPECT_EQ(ERR_ABORTED, r.status().error()); | 2387 EXPECT_EQ(ERR_ABORTED, r.status().error()); |
| 2297 EXPECT_EQ(1, network_delegate.created_requests()); | 2388 EXPECT_EQ(1, network_delegate.created_requests()); |
| 2298 EXPECT_EQ(0, network_delegate.destroyed_requests()); | 2389 EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2299 } | 2390 } |
| 2300 EXPECT_EQ(1, network_delegate.destroyed_requests()); | 2391 EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2301 } | 2392 } |
| 2302 | 2393 |
| 2303 // Tests that we can handle when a network request was canceled while we were | 2394 // Tests that we can handle when a network request was canceled while we were |
| 2304 // waiting for the network delegate. | 2395 // waiting for the network delegate. |
| 2305 // Part 2: Request is cancelled while waiting for OnBeforeSendHeaders callback. | 2396 // Part 2: Request is cancelled while waiting for OnBeforeSendHeaders callback. |
| 2306 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) { | 2397 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) { |
| 2307 ASSERT_TRUE(test_server_.Start()); | 2398 ASSERT_TRUE(test_server_.Start()); |
| 2308 | 2399 |
| 2309 TestDelegate d; | 2400 TestDelegate d; |
| 2310 BlockingNetworkDelegateWithManualCallback network_delegate; | 2401 BlockingNetworkDelegate network_delegate( |
| 2311 network_delegate.BlockOn( | 2402 BlockingNetworkDelegate::USER_CALLBACK); |
| 2312 BlockingNetworkDelegateWithManualCallback::ON_BEFORE_SEND_HEADERS); | 2403 network_delegate.BlockOn(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS); |
| 2313 | 2404 |
| 2314 TestURLRequestContext context(true); | 2405 TestURLRequestContext context(true); |
| 2315 context.set_network_delegate(&network_delegate); | 2406 context.set_network_delegate(&network_delegate); |
| 2316 context.Init(); | 2407 context.Init(); |
| 2317 | 2408 |
| 2318 { | 2409 { |
| 2319 URLRequest r(test_server_.GetURL(""), &d, &context); | 2410 URLRequest r(test_server_.GetURL(""), &d, &context); |
| 2320 | 2411 |
| 2321 r.Start(); | 2412 r.Start(); |
| 2322 network_delegate.WaitForState( | 2413 network_delegate.WaitForState( |
| 2323 BlockingNetworkDelegateWithManualCallback::ON_BEFORE_SEND_HEADERS); | 2414 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS); |
| 2324 EXPECT_EQ(0, network_delegate.completed_requests()); | 2415 EXPECT_EQ(0, network_delegate.completed_requests()); |
| 2325 // Cancel before callback. | 2416 // Cancel before callback. |
| 2326 r.Cancel(); | 2417 r.Cancel(); |
| 2327 // Ensure that network delegate is notified. | 2418 // Ensure that network delegate is notified. |
| 2328 EXPECT_EQ(1, network_delegate.completed_requests()); | 2419 EXPECT_EQ(1, network_delegate.completed_requests()); |
| 2329 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); | 2420 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
| 2330 EXPECT_EQ(ERR_ABORTED, r.status().error()); | 2421 EXPECT_EQ(ERR_ABORTED, r.status().error()); |
| 2331 EXPECT_EQ(1, network_delegate.created_requests()); | 2422 EXPECT_EQ(1, network_delegate.created_requests()); |
| 2332 EXPECT_EQ(0, network_delegate.destroyed_requests()); | 2423 EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2333 } | 2424 } |
| 2334 EXPECT_EQ(1, network_delegate.destroyed_requests()); | 2425 EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2335 } | 2426 } |
| 2336 | 2427 |
| 2337 // Tests that we can handle when a network request was canceled while we were | 2428 // Tests that we can handle when a network request was canceled while we were |
| 2338 // waiting for the network delegate. | 2429 // waiting for the network delegate. |
| 2339 // Part 3: Request is cancelled while waiting for OnHeadersReceived callback. | 2430 // Part 3: Request is cancelled while waiting for OnHeadersReceived callback. |
| 2340 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) { | 2431 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) { |
| 2341 ASSERT_TRUE(test_server_.Start()); | 2432 ASSERT_TRUE(test_server_.Start()); |
| 2342 | 2433 |
| 2343 TestDelegate d; | 2434 TestDelegate d; |
| 2344 BlockingNetworkDelegateWithManualCallback network_delegate; | 2435 BlockingNetworkDelegate network_delegate( |
| 2345 network_delegate.BlockOn( | 2436 BlockingNetworkDelegate::USER_CALLBACK); |
| 2346 BlockingNetworkDelegateWithManualCallback::ON_HEADERS_RECEIVED); | 2437 network_delegate.BlockOn(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 2347 | 2438 |
| 2348 TestURLRequestContext context(true); | 2439 TestURLRequestContext context(true); |
| 2349 context.set_network_delegate(&network_delegate); | 2440 context.set_network_delegate(&network_delegate); |
| 2350 context.Init(); | 2441 context.Init(); |
| 2351 | 2442 |
| 2352 { | 2443 { |
| 2353 URLRequest r(test_server_.GetURL(""), &d, &context); | 2444 URLRequest r(test_server_.GetURL(""), &d, &context); |
| 2354 | 2445 |
| 2355 r.Start(); | 2446 r.Start(); |
| 2356 network_delegate.WaitForState( | 2447 network_delegate.WaitForState( |
| 2357 BlockingNetworkDelegateWithManualCallback::ON_HEADERS_RECEIVED); | 2448 BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 2358 EXPECT_EQ(0, network_delegate.completed_requests()); | 2449 EXPECT_EQ(0, network_delegate.completed_requests()); |
| 2359 // Cancel before callback. | 2450 // Cancel before callback. |
| 2360 r.Cancel(); | 2451 r.Cancel(); |
| 2361 // Ensure that network delegate is notified. | 2452 // Ensure that network delegate is notified. |
| 2362 EXPECT_EQ(1, network_delegate.completed_requests()); | 2453 EXPECT_EQ(1, network_delegate.completed_requests()); |
| 2363 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); | 2454 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
| 2364 EXPECT_EQ(ERR_ABORTED, r.status().error()); | 2455 EXPECT_EQ(ERR_ABORTED, r.status().error()); |
| 2365 EXPECT_EQ(1, network_delegate.created_requests()); | 2456 EXPECT_EQ(1, network_delegate.created_requests()); |
| 2366 EXPECT_EQ(0, network_delegate.destroyed_requests()); | 2457 EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2367 } | 2458 } |
| 2368 EXPECT_EQ(1, network_delegate.destroyed_requests()); | 2459 EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2369 } | 2460 } |
| 2370 | 2461 |
| 2371 // Tests that we can handle when a network request was canceled while we were | 2462 // Tests that we can handle when a network request was canceled while we were |
| 2372 // waiting for the network delegate. | 2463 // waiting for the network delegate. |
| 2373 // Part 4: Request is cancelled while waiting for OnAuthRequired callback. | 2464 // Part 4: Request is cancelled while waiting for OnAuthRequired callback. |
| 2374 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) { | 2465 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) { |
| 2375 ASSERT_TRUE(test_server_.Start()); | 2466 ASSERT_TRUE(test_server_.Start()); |
| 2376 | 2467 |
| 2377 TestDelegate d; | 2468 TestDelegate d; |
| 2378 BlockingNetworkDelegateWithManualCallback network_delegate; | 2469 BlockingNetworkDelegate network_delegate( |
| 2379 network_delegate.BlockOn( | 2470 BlockingNetworkDelegate::USER_CALLBACK); |
| 2380 BlockingNetworkDelegateWithManualCallback::ON_AUTH_REQUIRED); | 2471 network_delegate.BlockOn(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
| 2381 | 2472 |
| 2382 TestURLRequestContext context(true); | 2473 TestURLRequestContext context(true); |
| 2383 context.set_network_delegate(&network_delegate); | 2474 context.set_network_delegate(&network_delegate); |
| 2384 context.Init(); | 2475 context.Init(); |
| 2385 | 2476 |
| 2386 { | 2477 { |
| 2387 URLRequest r(test_server_.GetURL("auth-basic"), &d, &context); | 2478 URLRequest r(test_server_.GetURL("auth-basic"), &d, &context); |
| 2388 | 2479 |
| 2389 r.Start(); | 2480 r.Start(); |
| 2390 network_delegate.WaitForState( | 2481 network_delegate.WaitForState( |
| 2391 BlockingNetworkDelegateWithManualCallback::ON_AUTH_REQUIRED); | 2482 BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
| 2392 EXPECT_EQ(0, network_delegate.completed_requests()); | 2483 EXPECT_EQ(0, network_delegate.completed_requests()); |
| 2393 // Cancel before callback. | 2484 // Cancel before callback. |
| 2394 r.Cancel(); | 2485 r.Cancel(); |
| 2395 // Ensure that network delegate is notified. | 2486 // Ensure that network delegate is notified. |
| 2396 EXPECT_EQ(1, network_delegate.completed_requests()); | 2487 EXPECT_EQ(1, network_delegate.completed_requests()); |
| 2397 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); | 2488 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
| 2398 EXPECT_EQ(ERR_ABORTED, r.status().error()); | 2489 EXPECT_EQ(ERR_ABORTED, r.status().error()); |
| 2399 EXPECT_EQ(1, network_delegate.created_requests()); | 2490 EXPECT_EQ(1, network_delegate.created_requests()); |
| 2400 EXPECT_EQ(0, network_delegate.destroyed_requests()); | 2491 EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2401 } | 2492 } |
| (...skipping 2249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 4651 | 4742 |
| 4652 EXPECT_FALSE(r.is_pending()); | 4743 EXPECT_FALSE(r.is_pending()); |
| 4653 EXPECT_EQ(1, d->response_started_count()); | 4744 EXPECT_EQ(1, d->response_started_count()); |
| 4654 EXPECT_FALSE(d->received_data_before_response()); | 4745 EXPECT_FALSE(d->received_data_before_response()); |
| 4655 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); | 4746 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
| 4656 } | 4747 } |
| 4657 } | 4748 } |
| 4658 #endif // !defined(DISABLE_FTP_SUPPORT) | 4749 #endif // !defined(DISABLE_FTP_SUPPORT) |
| 4659 | 4750 |
| 4660 } // namespace net | 4751 } // namespace net |
| OLD | NEW |