Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
|
mmenke
2012/09/19 19:39:47
nit: The new rule is to leave this alone, and las
vabr (Chromium)
2012/09/20 11:45:44
:D
I swear I did not touch that! Not sure how that
| |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "build/build_config.h" | 5 #include "build/build_config.h" |
| 6 | 6 |
| 7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
| 8 #include <windows.h> | 8 #include <windows.h> |
| 9 #include <shlobj.h> | 9 #include <shlobj.h> |
| 10 #endif | 10 #endif |
| 11 | 11 |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after 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. On blocking, the delegate allows either: |
|
mmenke
2012/09/19 19:39:47
nit: For readability, suggest you replace the sec
vabr (Chromium)
2012/09/20 11:45:44
Done.
| |
| 151 // * to synchronously return a pre-specified error code, or | |
| 152 // * to asynchronously return that value via an automatically called callback, | |
| 153 // or | |
| 154 // * to block and wait for the user to do a callback. | |
| 155 // Additionally, the user may also specify a redirect URL -- then each request | |
| 156 // with the current URL different from the redirect target will be redirected | |
| 157 // to that target, in the on-before-URL-request stage, independent of whether | |
| 158 // the delegate blocks in ON_BEFORE_URL_REQUEST or not. | |
| 151 class BlockingNetworkDelegate : public TestNetworkDelegate { | 159 class BlockingNetworkDelegate : public TestNetworkDelegate { |
|
mmenke
2012/09/19 19:39:47
TestNetworkDelegate posts a task to quit the curre
vabr (Chromium)
2012/09/20 11:45:44
I'm a bit confused: are we speaking about TestDele
mmenke
2012/09/20 15:37:52
Sorry, I got them confused.
| |
| 152 public: | 160 public: |
| 153 BlockingNetworkDelegate() | 161 // Stages in which the delegate can block. |
| 154 : retval_(ERR_IO_PENDING), | 162 enum Stage { |
| 155 callback_retval_(OK), | |
| 156 auth_retval_(NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING), | |
| 157 auth_callback_retval_( | |
| 158 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION), | |
| 159 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {} | |
| 160 | |
| 161 void set_retval(int retval) { retval_ = retval; } | |
| 162 void set_callback_retval(int retval) { callback_retval_ = retval; } | |
| 163 void set_redirect_url(const GURL& url) { redirect_url_ = url; } | |
| 164 void set_auth_retval(NetworkDelegate::AuthRequiredResponse retval) { | |
| 165 auth_retval_ = retval; } | |
| 166 void set_auth_callback_retval(NetworkDelegate::AuthRequiredResponse retval) { | |
| 167 auth_callback_retval_ = retval; } | |
| 168 void set_auth_credentials(const AuthCredentials& auth_credentials) { | |
| 169 auth_credentials_ = auth_credentials; | |
| 170 } | |
| 171 | |
| 172 private: | |
| 173 // TestNetworkDelegate implementation. | |
| 174 virtual int OnBeforeURLRequest(URLRequest* request, | |
| 175 const CompletionCallback& callback, | |
| 176 GURL* new_url) OVERRIDE { | |
| 177 if (redirect_url_ == request->url()) { | |
| 178 // We've already seen this request and redirected elsewhere. | |
| 179 return OK; | |
| 180 } | |
| 181 | |
| 182 TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); | |
| 183 | |
| 184 if (!redirect_url_.is_empty()) | |
| 185 *new_url = redirect_url_; | |
| 186 | |
| 187 if (retval_ != ERR_IO_PENDING) | |
| 188 return retval_; | |
| 189 | |
| 190 MessageLoop::current()->PostTask( | |
| 191 FROM_HERE, | |
| 192 base::Bind(&BlockingNetworkDelegate::DoCallback, | |
| 193 weak_factory_.GetWeakPtr(), callback)); | |
| 194 return ERR_IO_PENDING; | |
| 195 } | |
| 196 | |
| 197 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired( | |
| 198 URLRequest* request, | |
| 199 const AuthChallengeInfo& auth_info, | |
| 200 const AuthCallback& callback, | |
| 201 AuthCredentials* credentials) OVERRIDE { | |
| 202 TestNetworkDelegate::OnAuthRequired(request, auth_info, callback, | |
| 203 credentials); | |
| 204 switch (auth_retval_) { | |
| 205 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION: | |
| 206 break; | |
| 207 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH: | |
| 208 *credentials = auth_credentials_; | |
| 209 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH: | |
| 210 break; | |
| 211 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING: | |
| 212 MessageLoop::current()->PostTask( | |
| 213 FROM_HERE, | |
| 214 base::Bind(&BlockingNetworkDelegate::DoAuthCallback, | |
| 215 weak_factory_.GetWeakPtr(), callback, credentials)); | |
| 216 break; | |
| 217 } | |
| 218 return auth_retval_; | |
| 219 } | |
| 220 | |
| 221 void DoCallback(const CompletionCallback& callback) { | |
| 222 callback.Run(callback_retval_); | |
| 223 } | |
| 224 | |
| 225 void DoAuthCallback(const AuthCallback& callback, | |
| 226 AuthCredentials* credentials) { | |
| 227 if (auth_callback_retval_ == | |
| 228 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH) { | |
| 229 *credentials = auth_credentials_; | |
| 230 } | |
| 231 callback.Run(auth_callback_retval_); | |
| 232 } | |
| 233 | |
| 234 | |
| 235 int retval_; | |
| 236 int callback_retval_; | |
| 237 GURL redirect_url_; | |
| 238 NetworkDelegate::AuthRequiredResponse auth_retval_; | |
| 239 NetworkDelegate::AuthRequiredResponse auth_callback_retval_; | |
| 240 AuthCredentials auth_credentials_; | |
| 241 base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_; | |
| 242 }; | |
| 243 | |
| 244 // A network delegate that allows blocking requests until a callback function is | |
| 245 // called. | |
| 246 class BlockingNetworkDelegateWithManualCallback : public TestNetworkDelegate { | |
| 247 public: | |
| 248 enum State { | |
| 249 NOT_BLOCKED = 0, | 163 NOT_BLOCKED = 0, |
| 250 ON_BEFORE_URL_REQUEST = 1 << 0, | 164 ON_BEFORE_URL_REQUEST = 1 << 0, |
| 251 ON_BEFORE_SEND_HEADERS = 1 << 1, | 165 ON_BEFORE_SEND_HEADERS = 1 << 1, |
| 252 ON_HEADERS_RECEIVED = 1 << 2, | 166 ON_HEADERS_RECEIVED = 1 << 2, |
| 253 ON_AUTH_REQUIRED = 1 << 3 | 167 ON_AUTH_REQUIRED = 1 << 3 |
| 254 }; | 168 }; |
| 255 | 169 |
| 256 BlockingNetworkDelegateWithManualCallback() | 170 // Behavior during blocked stages. During other stages, just |
| 257 : block_on_(0), | 171 // returns net::OK or NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION. |
| 258 state_(NOT_BLOCKED) { | 172 enum BlockMode { |
| 173 SYNCHRONOUS, // No callback, returns specified return values. | |
| 174 AUTO_CALLBACK, // |this| takes care of doing a callback using the specified | |
| 175 // return codes. | |
|
mmenke
2012/09/19 19:39:47
nit: "|this| posts a task to run the callback usi
vabr (Chromium)
2012/09/20 11:45:44
Done.
| |
| 176 USER_CALLBACK, // User takes care of doing a callback. |retval_| and | |
| 177 // |auth_retval_| are ignored. | |
| 178 }; | |
| 179 | |
| 180 // Creates a delegate which does not block at all. | |
| 181 explicit BlockingNetworkDelegate(BlockMode mode); | |
|
mmenke
2012/09/19 19:39:47
nit: Just for consistency with the class field na
vabr (Chromium)
2012/09/20 11:45:44
Done.
| |
| 182 | |
| 183 // Setters. | |
| 184 void set_retval(int retval) { | |
| 185 ASSERT_NE(ERR_IO_PENDING, retval); | |
| 186 ASSERT_NE(OK, retval); | |
| 187 retval_ = retval; | |
| 259 } | 188 } |
| 260 | 189 |
| 261 // Activates blocking on |state|. | 190 // If |auth_retval| == AUTH_REQUIRED_RESPONSE_SET_AUTH, then |
| 262 void BlockOn(State state) { | 191 // |auth_credentials_| will be passed with the response. |
| 263 block_on_ |= state; | 192 void set_auth_retval(AuthRequiredResponse auth_retval) { |
| 193 ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, auth_retval); | |
| 194 auth_retval_ = auth_retval; | |
| 195 } | |
| 196 void set_auth_credentials(const AuthCredentials& auth_credentials) { | |
| 197 auth_credentials_ = auth_credentials; | |
| 264 } | 198 } |
| 265 | 199 |
| 266 void DoCallback(int rv) { | 200 void set_redirect_url(const GURL& url) { |
| 267 ASSERT_NE(NOT_BLOCKED, state_); | 201 redirect_url_ = url; |
| 268 CompletionCallback callback = callback_; | |
| 269 Reset(); | |
| 270 callback.Run(rv); | |
| 271 } | 202 } |
| 272 | 203 |
| 273 void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response) { | 204 // Add a stage to block on. Unless |stage| is NOT_BLOCKED or ON_AUTH_REQUIRED, |
| 274 ASSERT_EQ(ON_AUTH_REQUIRED, state_); | 205 // it is mandatory to call set_retval(). |
| 275 AuthCallback auth_callback = auth_callback_; | 206 void BlockOn(Stage stage) { |
| 276 Reset(); | 207 block_on_ |= stage; |
| 277 auth_callback.Run(response); | |
| 278 } | 208 } |
| 279 | 209 |
| 280 // Runs the message loop until |state| is reached. | 210 // For users to trigger a callback returning |response|. |
| 281 void WaitForState(State state) { | 211 // Side-effects: resets |stage_blocked_for_callback_| and stored callbacks. |
| 282 while (state_ != state) | 212 // Only call if |block_mode_| == USER_CALLBACK. |
| 213 void DoCallback(int response); | |
| 214 void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response); | |
| 215 | |
| 216 // Runs the message loop until |stage| is reached. | |
| 217 void WaitForState(Stage stage) { | |
| 218 ASSERT_EQ(USER_CALLBACK, block_mode_); | |
| 219 while (stage_blocked_for_callback_ != stage) | |
| 283 MessageLoop::current()->RunAllPending(); | 220 MessageLoop::current()->RunAllPending(); |
| 284 } | 221 } |
| 285 | 222 |
| 286 private: | 223 private: |
| 287 // TestNetworkDelegate implementation. | 224 // TestNetworkDelegate implementation. |
| 288 virtual int OnBeforeURLRequest(URLRequest* request, | 225 virtual int OnBeforeURLRequest(URLRequest* request, |
| 289 const CompletionCallback& callback, | 226 const CompletionCallback& callback, |
| 290 GURL* new_url) OVERRIDE { | 227 GURL* new_url) OVERRIDE; |
| 291 TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); | |
| 292 if ((block_on_ & ON_BEFORE_URL_REQUEST) == 0) { | |
| 293 return OK; | |
| 294 } else { | |
| 295 state_ = ON_BEFORE_URL_REQUEST; | |
| 296 callback_ = callback; | |
| 297 return ERR_IO_PENDING; | |
| 298 } | |
| 299 } | |
| 300 | 228 |
| 301 virtual int OnBeforeSendHeaders(URLRequest* request, | 229 virtual int OnBeforeSendHeaders(URLRequest* request, |
| 302 const CompletionCallback& callback, | 230 const CompletionCallback& callback, |
| 303 HttpRequestHeaders* headers) OVERRIDE { | 231 HttpRequestHeaders* headers) OVERRIDE; |
| 304 TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers); | |
| 305 if ((block_on_ & ON_BEFORE_SEND_HEADERS) == 0) { | |
| 306 return OK; | |
| 307 } else { | |
| 308 state_ = ON_BEFORE_SEND_HEADERS; | |
| 309 callback_ = callback; | |
| 310 return ERR_IO_PENDING; | |
| 311 } | |
| 312 } | |
| 313 | 232 |
| 314 virtual int OnHeadersReceived( | 233 virtual int OnHeadersReceived( |
| 315 URLRequest* request, | 234 URLRequest* request, |
| 316 const CompletionCallback& callback, | 235 const CompletionCallback& callback, |
| 317 HttpResponseHeaders* original_response_headers, | 236 HttpResponseHeaders* original_response_headers, |
| 318 scoped_refptr<HttpResponseHeaders>* override_response_headers) | 237 scoped_refptr<HttpResponseHeaders>* override_response_headers) OVERRIDE; |
| 319 OVERRIDE { | |
| 320 TestNetworkDelegate::OnHeadersReceived( | |
| 321 request, callback, original_response_headers, | |
| 322 override_response_headers); | |
| 323 if ((block_on_ & ON_HEADERS_RECEIVED) == 0) { | |
| 324 return OK; | |
| 325 } else { | |
| 326 state_ = ON_HEADERS_RECEIVED; | |
| 327 callback_ = callback; | |
| 328 return ERR_IO_PENDING; | |
| 329 } | |
| 330 } | |
| 331 | 238 |
| 332 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired( | 239 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
| 333 URLRequest* request, | 240 URLRequest* request, |
| 334 const AuthChallengeInfo& auth_info, | 241 const AuthChallengeInfo& auth_info, |
| 335 const AuthCallback& callback, | 242 const AuthCallback& callback, |
| 336 AuthCredentials* credentials) OVERRIDE { | 243 AuthCredentials* credentials) OVERRIDE; |
| 337 if ((block_on_ & ON_AUTH_REQUIRED) == 0) { | |
| 338 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION; | |
| 339 } else { | |
| 340 state_ = ON_AUTH_REQUIRED; | |
| 341 auth_callback_ = callback; | |
| 342 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING; | |
| 343 } | |
| 344 } | |
| 345 | 244 |
| 346 void Reset() { | 245 void Reset() { |
| 347 state_ = NOT_BLOCKED; | 246 stage_blocked_for_callback_ = NOT_BLOCKED; |
| 348 callback_.Reset(); | 247 callback_.Reset(); |
| 349 auth_callback_.Reset(); | 248 auth_callback_.Reset(); |
| 350 } | 249 } |
| 351 | 250 |
| 352 int block_on_; // Bit mask on which states to block. | 251 void RunCallback(int response, const CompletionCallback& callback); |
| 353 State state_; | 252 void RunAuthCallback(AuthRequiredResponse response, |
| 253 const AuthCallback& callback); | |
| 254 | |
| 255 // Checks whether we should block in |stage|. If yes, returns an error code | |
| 256 // and optionally sets up callback based on |block_mode_|. If no, returns OK. | |
| 257 int MaybeBlockStage(Stage stage, const CompletionCallback& callback); | |
| 258 | |
| 259 // Configuration parameters: | |
| 260 const BlockMode block_mode_; | |
| 261 | |
| 262 // Values returned on blocking stages when mode is SYNCHRONOUS or | |
| 263 // AUTO_CALLBACK. | |
| 264 int retval_; // To be returned in non-auth stages. | |
| 265 AuthRequiredResponse auth_retval_; | |
| 266 | |
| 267 GURL redirect_url_; // Used if non-empty. | |
| 268 int block_on_; // Bit mask: in which stages to block. | |
| 269 | |
| 270 // |auth_credentials_| will be copied to |*target_auth_credential_| on | |
| 271 // callback. | |
| 272 AuthCredentials auth_credentials_; | |
| 273 AuthCredentials* target_auth_credentials_; | |
| 274 | |
| 275 // Internal parameters: | |
| 276 Stage stage_blocked_for_callback_; // In which stage did we block? | |
| 277 | |
| 278 // Callback objects stored during blocking stages. | |
| 354 CompletionCallback callback_; | 279 CompletionCallback callback_; |
| 355 AuthCallback auth_callback_; | 280 AuthCallback auth_callback_; |
| 281 | |
| 282 base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_; | |
| 283 | |
| 284 DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate); | |
| 356 }; | 285 }; |
| 357 | 286 |
| 287 BlockingNetworkDelegate::BlockingNetworkDelegate(BlockMode mode) | |
| 288 : block_mode_(mode), | |
| 289 retval_(mode == USER_CALLBACK ? ERR_IO_PENDING : OK), | |
| 290 auth_retval_(mode == USER_CALLBACK ? | |
| 291 AUTH_REQUIRED_RESPONSE_IO_PENDING : | |
| 292 AUTH_REQUIRED_RESPONSE_NO_ACTION), | |
| 293 block_on_(0), | |
| 294 target_auth_credentials_(NULL), | |
| 295 stage_blocked_for_callback_(NOT_BLOCKED), | |
| 296 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | |
| 297 } | |
| 298 | |
| 299 int BlockingNetworkDelegate::OnBeforeURLRequest( | |
| 300 URLRequest* request, | |
| 301 const CompletionCallback& callback, | |
| 302 GURL* new_url) { | |
| 303 if (redirect_url_ == request->url()) | |
| 304 return OK; // We've already seen this request and redirected elsewhere. | |
| 305 | |
| 306 TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); | |
| 307 | |
| 308 if (!redirect_url_.is_empty()) | |
| 309 *new_url = redirect_url_; | |
| 310 | |
| 311 return MaybeBlockStage(ON_BEFORE_URL_REQUEST, callback); | |
| 312 } | |
| 313 | |
| 314 int BlockingNetworkDelegate::OnBeforeSendHeaders( | |
| 315 URLRequest* request, | |
| 316 const CompletionCallback& callback, | |
| 317 HttpRequestHeaders* headers) { | |
| 318 TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers); | |
| 319 | |
| 320 return MaybeBlockStage(ON_BEFORE_SEND_HEADERS, callback); | |
| 321 } | |
| 322 | |
| 323 int BlockingNetworkDelegate::OnHeadersReceived( | |
| 324 URLRequest* request, | |
| 325 const CompletionCallback& callback, | |
| 326 HttpResponseHeaders* original_response_headers, | |
| 327 scoped_refptr<HttpResponseHeaders>* override_response_headers) { | |
| 328 TestNetworkDelegate::OnHeadersReceived( | |
| 329 request, callback, original_response_headers, | |
| 330 override_response_headers); | |
| 331 | |
| 332 return MaybeBlockStage(ON_HEADERS_RECEIVED, callback); | |
| 333 } | |
| 334 | |
| 335 NetworkDelegate::AuthRequiredResponse BlockingNetworkDelegate::OnAuthRequired( | |
| 336 URLRequest* request, | |
| 337 const AuthChallengeInfo& auth_info, | |
| 338 const AuthCallback& callback, | |
| 339 AuthCredentials* credentials) { | |
| 340 TestNetworkDelegate::OnAuthRequired(request, auth_info, callback, | |
| 341 credentials); | |
| 342 stage_blocked_for_callback_ = NOT_BLOCKED; | |
|
mmenke
2012/09/19 19:39:47
Don't think there's any way for this not to be tru
vabr (Chromium)
2012/09/20 11:45:44
Done.
| |
| 343 | |
| 344 if ((block_on_ & ON_AUTH_REQUIRED) == 0) { | |
| 345 return AUTH_REQUIRED_RESPONSE_NO_ACTION; | |
| 346 } | |
| 347 | |
| 348 if (block_mode_ == USER_CALLBACK) | |
| 349 stage_blocked_for_callback_ = ON_AUTH_REQUIRED; | |
| 350 | |
| 351 if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) | |
| 352 target_auth_credentials_ = credentials; | |
| 353 | |
| 354 switch (block_mode_) { | |
| 355 case SYNCHRONOUS: | |
| 356 if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) | |
| 357 *target_auth_credentials_ = auth_credentials_; | |
| 358 return auth_retval_; | |
| 359 | |
| 360 case AUTO_CALLBACK: | |
| 361 MessageLoop::current()->PostTask( | |
| 362 FROM_HERE, | |
| 363 base::Bind(&BlockingNetworkDelegate::RunAuthCallback, | |
| 364 weak_factory_.GetWeakPtr(), auth_retval_, callback)); | |
| 365 return AUTH_REQUIRED_RESPONSE_IO_PENDING; | |
| 366 | |
| 367 case USER_CALLBACK: | |
| 368 auth_callback_ = callback; | |
| 369 return AUTH_REQUIRED_RESPONSE_IO_PENDING; | |
| 370 } | |
| 371 NOTREACHED(); | |
| 372 return AUTH_REQUIRED_RESPONSE_NO_ACTION; // Dummy value. | |
| 373 } | |
| 374 | |
| 375 void BlockingNetworkDelegate::DoCallback(int response) { | |
| 376 ASSERT_EQ(block_mode_, USER_CALLBACK); | |
| 377 ASSERT_NE(NOT_BLOCKED, stage_blocked_for_callback_); | |
|
mmenke
2012/09/19 19:39:47
nit: ASSERT_NE(ON_AUTH_REQUIRED, stage_blocked_fo
vabr (Chromium)
2012/09/20 11:45:44
Done.
| |
| 378 CompletionCallback callback = callback_; | |
| 379 Reset(); | |
| 380 RunCallback(response, callback); | |
| 381 } | |
| 382 | |
| 383 void BlockingNetworkDelegate::DoAuthCallback( | |
| 384 NetworkDelegate::AuthRequiredResponse response) { | |
| 385 ASSERT_EQ(block_mode_, USER_CALLBACK); | |
| 386 ASSERT_EQ(ON_AUTH_REQUIRED, stage_blocked_for_callback_); | |
| 387 AuthCallback auth_callback = auth_callback_; | |
| 388 Reset(); | |
| 389 RunAuthCallback(response, auth_callback); | |
| 390 } | |
| 391 | |
| 392 void BlockingNetworkDelegate::RunCallback(int response, | |
| 393 const CompletionCallback& callback) { | |
| 394 callback.Run(response); | |
| 395 } | |
| 396 | |
| 397 void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response, | |
| 398 const AuthCallback& callback) { | |
| 399 if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) { | |
| 400 ASSERT_TRUE(target_auth_credentials_ != NULL); | |
| 401 *target_auth_credentials_ = auth_credentials_; | |
| 402 } | |
| 403 callback.Run(response); | |
| 404 } | |
| 405 | |
| 406 int BlockingNetworkDelegate::MaybeBlockStage( | |
| 407 BlockingNetworkDelegate::Stage stage, | |
| 408 const CompletionCallback& callback) { | |
| 409 stage_blocked_for_callback_ = NOT_BLOCKED; | |
|
mmenke
2012/09/19 19:39:47
Don't think there's any way for this not to be tru
vabr (Chromium)
2012/09/20 11:45:44
Done.
| |
| 410 | |
| 411 if ((block_on_ & stage) == 0) { | |
| 412 return OK; | |
| 413 } | |
| 414 | |
| 415 if (redirect_url_.is_empty() || stage != ON_BEFORE_URL_REQUEST) | |
|
mmenke
2012/09/19 19:39:47
This if statement isn't needed anymore, is it? If
vabr (Chromium)
2012/09/20 11:45:44
Done.
| |
| 416 EXPECT_NE(OK, retval_); | |
| 417 if (block_mode_ == USER_CALLBACK) | |
| 418 stage_blocked_for_callback_ = stage; | |
|
mmenke
2012/09/19 19:39:47
optional: Do we want to set this on AUTO_CALLBACK
vabr (Chromium)
2012/09/20 11:45:44
Done.
| |
| 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); | |
| 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) { |
| 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); | |
| 2057 network_delegate.BlockOn(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); | |
| 1971 GURL redirect_url(test_server_.GetURL("simple.html")); | 2058 GURL redirect_url(test_server_.GetURL("simple.html")); |
| 1972 network_delegate.set_redirect_url(redirect_url); | 2059 network_delegate.set_redirect_url(redirect_url); |
| 1973 | 2060 |
| 1974 TestURLRequestContextWithProxy context( | 2061 TestURLRequestContextWithProxy context( |
| 1975 test_server_.host_port_pair().ToString(), | 2062 test_server_.host_port_pair().ToString(), |
| 1976 &network_delegate); | 2063 &network_delegate); |
| 1977 | 2064 |
| 1978 { | 2065 { |
| 1979 GURL original_url(test_server_.GetURL("empty.html")); | 2066 GURL original_url(test_server_.GetURL("empty.html")); |
| 1980 URLRequest r(original_url, &d, &context); | 2067 URLRequest r(original_url, &d, &context); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 1992 } | 2079 } |
| 1993 EXPECT_EQ(1, network_delegate.destroyed_requests()); | 2080 EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 1994 } | 2081 } |
| 1995 | 2082 |
| 1996 // Tests that the network delegate can block and redirect a request to a new | 2083 // Tests that the network delegate can block and redirect a request to a new |
| 1997 // URL by setting a redirect_url and returning in OnBeforeURLRequest directly. | 2084 // URL by setting a redirect_url and returning in OnBeforeURLRequest directly. |
| 1998 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) { | 2085 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) { |
| 1999 ASSERT_TRUE(test_server_.Start()); | 2086 ASSERT_TRUE(test_server_.Start()); |
| 2000 | 2087 |
| 2001 TestDelegate d; | 2088 TestDelegate d; |
| 2002 BlockingNetworkDelegate network_delegate; | 2089 BlockingNetworkDelegate network_delegate( |
| 2090 BlockingNetworkDelegate::AUTO_CALLBACK); | |
| 2091 network_delegate.BlockOn(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); | |
| 2003 GURL redirect_url(test_server_.GetURL("simple.html")); | 2092 GURL redirect_url(test_server_.GetURL("simple.html")); |
| 2004 network_delegate.set_redirect_url(redirect_url); | 2093 network_delegate.set_redirect_url(redirect_url); |
| 2005 network_delegate.set_retval(OK); | |
| 2006 | 2094 |
| 2007 TestURLRequestContextWithProxy context( | 2095 TestURLRequestContextWithProxy context( |
| 2008 test_server_.host_port_pair().ToString(), | 2096 test_server_.host_port_pair().ToString(), |
| 2009 &network_delegate); | 2097 &network_delegate); |
| 2010 | 2098 |
| 2011 { | 2099 { |
| 2012 GURL original_url(test_server_.GetURL("empty.html")); | 2100 GURL original_url(test_server_.GetURL("empty.html")); |
| 2013 URLRequest r(original_url, &d, &context); | 2101 URLRequest r(original_url, &d, &context); |
| 2014 | 2102 |
| 2015 r.Start(); | 2103 r.Start(); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 2026 EXPECT_EQ(1, network_delegate.destroyed_requests()); | 2114 EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2027 } | 2115 } |
| 2028 | 2116 |
| 2029 // Tests that redirects caused by the network delegate preserve POST data. | 2117 // Tests that redirects caused by the network delegate preserve POST data. |
| 2030 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) { | 2118 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) { |
| 2031 ASSERT_TRUE(test_server_.Start()); | 2119 ASSERT_TRUE(test_server_.Start()); |
| 2032 | 2120 |
| 2033 const char kData[] = "hello world"; | 2121 const char kData[] = "hello world"; |
| 2034 | 2122 |
| 2035 TestDelegate d; | 2123 TestDelegate d; |
| 2036 BlockingNetworkDelegate network_delegate; | 2124 BlockingNetworkDelegate network_delegate( |
| 2125 BlockingNetworkDelegate::AUTO_CALLBACK); | |
| 2126 network_delegate.BlockOn(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); | |
| 2037 GURL redirect_url(test_server_.GetURL("echo")); | 2127 GURL redirect_url(test_server_.GetURL("echo")); |
| 2038 network_delegate.set_redirect_url(redirect_url); | 2128 network_delegate.set_redirect_url(redirect_url); |
| 2039 | 2129 |
| 2040 TestURLRequestContext context(true); | 2130 TestURLRequestContext context(true); |
| 2041 context.set_network_delegate(&network_delegate); | 2131 context.set_network_delegate(&network_delegate); |
| 2042 context.Init(); | 2132 context.Init(); |
| 2043 | 2133 |
| 2044 { | 2134 { |
| 2045 GURL original_url(test_server_.GetURL("empty.html")); | 2135 GURL original_url(test_server_.GetURL("empty.html")); |
| 2046 URLRequest r(original_url, &d, &context); | 2136 URLRequest r(original_url, &d, &context); |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 2067 } | 2157 } |
| 2068 | 2158 |
| 2069 // Tests that the network delegate can synchronously complete OnAuthRequired | 2159 // Tests that the network delegate can synchronously complete OnAuthRequired |
| 2070 // by taking no action. This indicates that the NetworkDelegate does not want to | 2160 // 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 | 2161 // handle the challenge, and is passing the buck along to the |
| 2072 // URLRequest::Delegate. | 2162 // URLRequest::Delegate. |
| 2073 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) { | 2163 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) { |
| 2074 ASSERT_TRUE(test_server_.Start()); | 2164 ASSERT_TRUE(test_server_.Start()); |
| 2075 | 2165 |
| 2076 TestDelegate d; | 2166 TestDelegate d; |
| 2077 BlockingNetworkDelegate network_delegate; | 2167 BlockingNetworkDelegate network_delegate( |
| 2078 network_delegate.set_auth_retval( | 2168 BlockingNetworkDelegate::AUTO_CALLBACK); |
| 2079 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION); | 2169 network_delegate.BlockOn(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
| 2080 | 2170 |
| 2081 TestURLRequestContext context(true); | 2171 TestURLRequestContext context(true); |
| 2082 context.set_network_delegate(&network_delegate); | 2172 context.set_network_delegate(&network_delegate); |
| 2083 context.Init(); | 2173 context.Init(); |
| 2084 | 2174 |
| 2085 d.set_credentials(AuthCredentials(kUser, kSecret)); | 2175 d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 2086 | 2176 |
| 2087 { | 2177 { |
| 2088 GURL url(test_server_.GetURL("auth-basic")); | 2178 GURL url(test_server_.GetURL("auth-basic")); |
| 2089 URLRequest r(url, &d, &context); | 2179 URLRequest r(url, &d, &context); |
| 2090 r.Start(); | 2180 r.Start(); |
| 2091 MessageLoop::current()->Run(); | 2181 MessageLoop::current()->Run(); |
| 2092 | 2182 |
| 2093 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); | 2183 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 2094 EXPECT_EQ(0, r.status().error()); | 2184 EXPECT_EQ(0, r.status().error()); |
| 2095 EXPECT_EQ(200, r.GetResponseCode()); | 2185 EXPECT_EQ(200, r.GetResponseCode()); |
| 2096 EXPECT_TRUE(d.auth_required_called()); | 2186 EXPECT_TRUE(d.auth_required_called()); |
| 2097 EXPECT_EQ(1, network_delegate.created_requests()); | 2187 EXPECT_EQ(1, network_delegate.created_requests()); |
| 2098 EXPECT_EQ(0, network_delegate.destroyed_requests()); | 2188 EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2099 } | 2189 } |
| 2100 EXPECT_EQ(1, network_delegate.destroyed_requests()); | 2190 EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2101 } | 2191 } |
| 2102 | 2192 |
| 2103 // Tests that the network delegate can synchronously complete OnAuthRequired | 2193 // Tests that the network delegate can synchronously complete OnAuthRequired |
| 2104 // by setting credentials. | 2194 // by setting credentials. |
| 2105 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) { | 2195 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) { |
| 2106 ASSERT_TRUE(test_server_.Start()); | 2196 ASSERT_TRUE(test_server_.Start()); |
| 2107 | 2197 |
| 2108 TestDelegate d; | 2198 TestDelegate d; |
| 2109 BlockingNetworkDelegate network_delegate; | 2199 BlockingNetworkDelegate network_delegate( |
| 2200 BlockingNetworkDelegate::AUTO_CALLBACK); | |
| 2201 network_delegate.BlockOn(BlockingNetworkDelegate::ON_AUTH_REQUIRED); | |
| 2110 network_delegate.set_auth_retval( | 2202 network_delegate.set_auth_retval( |
| 2111 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); | 2203 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 2112 | 2204 |
| 2113 network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); | 2205 network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
| 2114 | 2206 |
| 2115 TestURLRequestContext context(true); | 2207 TestURLRequestContext context(true); |
| 2116 context.set_network_delegate(&network_delegate); | 2208 context.set_network_delegate(&network_delegate); |
| 2117 context.Init(); | 2209 context.Init(); |
| 2118 | 2210 |
| 2119 { | 2211 { |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 2131 } | 2223 } |
| 2132 EXPECT_EQ(1, network_delegate.destroyed_requests()); | 2224 EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2133 } | 2225 } |
| 2134 | 2226 |
| 2135 // Tests that the network delegate can synchronously complete OnAuthRequired | 2227 // Tests that the network delegate can synchronously complete OnAuthRequired |
| 2136 // by cancelling authentication. | 2228 // by cancelling authentication. |
| 2137 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) { | 2229 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) { |
| 2138 ASSERT_TRUE(test_server_.Start()); | 2230 ASSERT_TRUE(test_server_.Start()); |
| 2139 | 2231 |
| 2140 TestDelegate d; | 2232 TestDelegate d; |
| 2141 BlockingNetworkDelegate network_delegate; | 2233 BlockingNetworkDelegate network_delegate( |
| 2234 BlockingNetworkDelegate::AUTO_CALLBACK); | |
| 2235 network_delegate.BlockOn(BlockingNetworkDelegate::ON_AUTH_REQUIRED); | |
| 2142 network_delegate.set_auth_retval( | 2236 network_delegate.set_auth_retval( |
| 2143 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); | 2237 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 2144 | 2238 |
| 2145 TestURLRequestContext context(true); | 2239 TestURLRequestContext context(true); |
| 2146 context.set_network_delegate(&network_delegate); | 2240 context.set_network_delegate(&network_delegate); |
| 2147 context.Init(); | 2241 context.Init(); |
| 2148 | 2242 |
| 2149 { | 2243 { |
| 2150 GURL url(test_server_.GetURL("auth-basic")); | 2244 GURL url(test_server_.GetURL("auth-basic")); |
| 2151 URLRequest r(url, &d, &context); | 2245 URLRequest r(url, &d, &context); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 2163 } | 2257 } |
| 2164 | 2258 |
| 2165 // Tests that the network delegate can asynchronously complete OnAuthRequired | 2259 // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 2166 // by taking no action. This indicates that the NetworkDelegate does not want | 2260 // 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 | 2261 // to handle the challenge, and is passing the buck along to the |
| 2168 // URLRequest::Delegate. | 2262 // URLRequest::Delegate. |
| 2169 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) { | 2263 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) { |
| 2170 ASSERT_TRUE(test_server_.Start()); | 2264 ASSERT_TRUE(test_server_.Start()); |
| 2171 | 2265 |
| 2172 TestDelegate d; | 2266 TestDelegate d; |
| 2173 BlockingNetworkDelegate network_delegate; | 2267 BlockingNetworkDelegate network_delegate( |
| 2268 BlockingNetworkDelegate::AUTO_CALLBACK); | |
| 2269 network_delegate.BlockOn(BlockingNetworkDelegate::ON_AUTH_REQUIRED); | |
| 2174 network_delegate.set_auth_retval( | 2270 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); | 2271 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION); |
| 2178 | 2272 |
| 2179 TestURLRequestContext context(true); | 2273 TestURLRequestContext context(true); |
| 2180 context.set_network_delegate(&network_delegate); | 2274 context.set_network_delegate(&network_delegate); |
| 2181 context.Init(); | 2275 context.Init(); |
| 2182 | 2276 |
| 2183 d.set_credentials(AuthCredentials(kUser, kSecret)); | 2277 d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 2184 | 2278 |
| 2185 { | 2279 { |
| 2186 GURL url(test_server_.GetURL("auth-basic")); | 2280 GURL url(test_server_.GetURL("auth-basic")); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 2197 } | 2291 } |
| 2198 EXPECT_EQ(1, network_delegate.destroyed_requests()); | 2292 EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2199 } | 2293 } |
| 2200 | 2294 |
| 2201 // Tests that the network delegate can asynchronously complete OnAuthRequired | 2295 // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 2202 // by setting credentials. | 2296 // by setting credentials. |
| 2203 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) { | 2297 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) { |
| 2204 ASSERT_TRUE(test_server_.Start()); | 2298 ASSERT_TRUE(test_server_.Start()); |
| 2205 | 2299 |
| 2206 TestDelegate d; | 2300 TestDelegate d; |
| 2207 BlockingNetworkDelegate network_delegate; | 2301 BlockingNetworkDelegate network_delegate( |
| 2302 BlockingNetworkDelegate::AUTO_CALLBACK); | |
| 2303 network_delegate.BlockOn(BlockingNetworkDelegate::ON_AUTH_REQUIRED); | |
| 2208 network_delegate.set_auth_retval( | 2304 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); | 2305 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 2212 | 2306 |
| 2213 AuthCredentials auth_credentials(kUser, kSecret); | 2307 AuthCredentials auth_credentials(kUser, kSecret); |
| 2214 network_delegate.set_auth_credentials(auth_credentials); | 2308 network_delegate.set_auth_credentials(auth_credentials); |
| 2215 | 2309 |
| 2216 TestURLRequestContext context(true); | 2310 TestURLRequestContext context(true); |
| 2217 context.set_network_delegate(&network_delegate); | 2311 context.set_network_delegate(&network_delegate); |
| 2218 context.Init(); | 2312 context.Init(); |
| 2219 | 2313 |
| 2220 { | 2314 { |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 2233 } | 2327 } |
| 2234 EXPECT_EQ(1, network_delegate.destroyed_requests()); | 2328 EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2235 } | 2329 } |
| 2236 | 2330 |
| 2237 // Tests that the network delegate can asynchronously complete OnAuthRequired | 2331 // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 2238 // by cancelling authentication. | 2332 // by cancelling authentication. |
| 2239 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) { | 2333 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) { |
| 2240 ASSERT_TRUE(test_server_.Start()); | 2334 ASSERT_TRUE(test_server_.Start()); |
| 2241 | 2335 |
| 2242 TestDelegate d; | 2336 TestDelegate d; |
| 2243 BlockingNetworkDelegate network_delegate; | 2337 BlockingNetworkDelegate network_delegate( |
| 2338 BlockingNetworkDelegate::AUTO_CALLBACK); | |
| 2339 network_delegate.BlockOn(BlockingNetworkDelegate::ON_AUTH_REQUIRED); | |
| 2244 network_delegate.set_auth_retval( | 2340 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); | 2341 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 2248 | 2342 |
| 2249 TestURLRequestContext context(true); | 2343 TestURLRequestContext context(true); |
| 2250 context.set_network_delegate(&network_delegate); | 2344 context.set_network_delegate(&network_delegate); |
| 2251 context.Init(); | 2345 context.Init(); |
| 2252 | 2346 |
| 2253 { | 2347 { |
| 2254 GURL url(test_server_.GetURL("auth-basic")); | 2348 GURL url(test_server_.GetURL("auth-basic")); |
| 2255 URLRequest r(url, &d, &context); | 2349 URLRequest r(url, &d, &context); |
| 2256 r.Start(); | 2350 r.Start(); |
| 2257 MessageLoop::current()->Run(); | 2351 MessageLoop::current()->Run(); |
| 2258 | 2352 |
| 2259 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); | 2353 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 2260 EXPECT_EQ(OK, r.status().error()); | 2354 EXPECT_EQ(OK, r.status().error()); |
| 2261 EXPECT_EQ(401, r.GetResponseCode()); | 2355 EXPECT_EQ(401, r.GetResponseCode()); |
| 2262 EXPECT_FALSE(d.auth_required_called()); | 2356 EXPECT_FALSE(d.auth_required_called()); |
| 2263 EXPECT_EQ(1, network_delegate.created_requests()); | 2357 EXPECT_EQ(1, network_delegate.created_requests()); |
| 2264 EXPECT_EQ(0, network_delegate.destroyed_requests()); | 2358 EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2265 } | 2359 } |
| 2266 EXPECT_EQ(1, network_delegate.destroyed_requests()); | 2360 EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2267 } | 2361 } |
| 2268 | 2362 |
| 2269 // Tests that we can handle when a network request was canceled while we were | 2363 // Tests that we can handle when a network request was canceled while we were |
| 2270 // waiting for the network delegate. | 2364 // waiting for the network delegate. |
| 2271 // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback. | 2365 // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback. |
| 2272 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) { | 2366 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) { |
| 2273 ASSERT_TRUE(test_server_.Start()); | 2367 ASSERT_TRUE(test_server_.Start()); |
| 2274 | 2368 |
| 2275 TestDelegate d; | 2369 TestDelegate d; |
| 2276 BlockingNetworkDelegateWithManualCallback network_delegate; | 2370 BlockingNetworkDelegate network_delegate( |
| 2277 network_delegate.BlockOn( | 2371 BlockingNetworkDelegate::USER_CALLBACK); |
| 2278 BlockingNetworkDelegateWithManualCallback::ON_BEFORE_URL_REQUEST); | 2372 network_delegate.BlockOn(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
| 2279 | 2373 |
| 2280 TestURLRequestContext context(true); | 2374 TestURLRequestContext context(true); |
| 2281 context.set_network_delegate(&network_delegate); | 2375 context.set_network_delegate(&network_delegate); |
| 2282 context.Init(); | 2376 context.Init(); |
| 2283 | 2377 |
| 2284 { | 2378 { |
| 2285 URLRequest r(test_server_.GetURL(""), &d, &context); | 2379 URLRequest r(test_server_.GetURL(""), &d, &context); |
| 2286 | 2380 |
| 2287 r.Start(); | 2381 r.Start(); |
| 2288 network_delegate.WaitForState( | 2382 network_delegate.WaitForState( |
| 2289 BlockingNetworkDelegateWithManualCallback::ON_BEFORE_URL_REQUEST); | 2383 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
| 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.BlockOn(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS); |
| 2313 | 2407 |
| 2314 TestURLRequestContext context(true); | 2408 TestURLRequestContext context(true); |
| 2315 context.set_network_delegate(&network_delegate); | 2409 context.set_network_delegate(&network_delegate); |
| 2316 context.Init(); | 2410 context.Init(); |
| 2317 | 2411 |
| 2318 { | 2412 { |
| 2319 URLRequest r(test_server_.GetURL(""), &d, &context); | 2413 URLRequest r(test_server_.GetURL(""), &d, &context); |
| 2320 | 2414 |
| 2321 r.Start(); | 2415 r.Start(); |
| 2322 network_delegate.WaitForState( | 2416 network_delegate.WaitForState( |
| 2323 BlockingNetworkDelegateWithManualCallback::ON_BEFORE_SEND_HEADERS); | 2417 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS); |
| 2324 EXPECT_EQ(0, network_delegate.completed_requests()); | 2418 EXPECT_EQ(0, network_delegate.completed_requests()); |
| 2325 // Cancel before callback. | 2419 // Cancel before callback. |
| 2326 r.Cancel(); | 2420 r.Cancel(); |
| 2327 // Ensure that network delegate is notified. | 2421 // Ensure that network delegate is notified. |
| 2328 EXPECT_EQ(1, network_delegate.completed_requests()); | 2422 EXPECT_EQ(1, network_delegate.completed_requests()); |
| 2329 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); | 2423 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
| 2330 EXPECT_EQ(ERR_ABORTED, r.status().error()); | 2424 EXPECT_EQ(ERR_ABORTED, r.status().error()); |
| 2331 EXPECT_EQ(1, network_delegate.created_requests()); | 2425 EXPECT_EQ(1, network_delegate.created_requests()); |
| 2332 EXPECT_EQ(0, network_delegate.destroyed_requests()); | 2426 EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2333 } | 2427 } |
| 2334 EXPECT_EQ(1, network_delegate.destroyed_requests()); | 2428 EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2335 } | 2429 } |
| 2336 | 2430 |
| 2337 // Tests that we can handle when a network request was canceled while we were | 2431 // Tests that we can handle when a network request was canceled while we were |
| 2338 // waiting for the network delegate. | 2432 // waiting for the network delegate. |
| 2339 // Part 3: Request is cancelled while waiting for OnHeadersReceived callback. | 2433 // Part 3: Request is cancelled while waiting for OnHeadersReceived callback. |
| 2340 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) { | 2434 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) { |
| 2341 ASSERT_TRUE(test_server_.Start()); | 2435 ASSERT_TRUE(test_server_.Start()); |
| 2342 | 2436 |
| 2343 TestDelegate d; | 2437 TestDelegate d; |
| 2344 BlockingNetworkDelegateWithManualCallback network_delegate; | 2438 BlockingNetworkDelegate network_delegate( |
| 2345 network_delegate.BlockOn( | 2439 BlockingNetworkDelegate::USER_CALLBACK); |
| 2346 BlockingNetworkDelegateWithManualCallback::ON_HEADERS_RECEIVED); | 2440 network_delegate.BlockOn(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 2347 | 2441 |
| 2348 TestURLRequestContext context(true); | 2442 TestURLRequestContext context(true); |
| 2349 context.set_network_delegate(&network_delegate); | 2443 context.set_network_delegate(&network_delegate); |
| 2350 context.Init(); | 2444 context.Init(); |
| 2351 | 2445 |
| 2352 { | 2446 { |
| 2353 URLRequest r(test_server_.GetURL(""), &d, &context); | 2447 URLRequest r(test_server_.GetURL(""), &d, &context); |
| 2354 | 2448 |
| 2355 r.Start(); | 2449 r.Start(); |
| 2356 network_delegate.WaitForState( | 2450 network_delegate.WaitForState( |
| 2357 BlockingNetworkDelegateWithManualCallback::ON_HEADERS_RECEIVED); | 2451 BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 2358 EXPECT_EQ(0, network_delegate.completed_requests()); | 2452 EXPECT_EQ(0, network_delegate.completed_requests()); |
| 2359 // Cancel before callback. | 2453 // Cancel before callback. |
| 2360 r.Cancel(); | 2454 r.Cancel(); |
| 2361 // Ensure that network delegate is notified. | 2455 // Ensure that network delegate is notified. |
| 2362 EXPECT_EQ(1, network_delegate.completed_requests()); | 2456 EXPECT_EQ(1, network_delegate.completed_requests()); |
| 2363 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); | 2457 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
| 2364 EXPECT_EQ(ERR_ABORTED, r.status().error()); | 2458 EXPECT_EQ(ERR_ABORTED, r.status().error()); |
| 2365 EXPECT_EQ(1, network_delegate.created_requests()); | 2459 EXPECT_EQ(1, network_delegate.created_requests()); |
| 2366 EXPECT_EQ(0, network_delegate.destroyed_requests()); | 2460 EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2367 } | 2461 } |
| 2368 EXPECT_EQ(1, network_delegate.destroyed_requests()); | 2462 EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2369 } | 2463 } |
| 2370 | 2464 |
| 2371 // Tests that we can handle when a network request was canceled while we were | 2465 // Tests that we can handle when a network request was canceled while we were |
| 2372 // waiting for the network delegate. | 2466 // waiting for the network delegate. |
| 2373 // Part 4: Request is cancelled while waiting for OnAuthRequired callback. | 2467 // Part 4: Request is cancelled while waiting for OnAuthRequired callback. |
| 2374 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) { | 2468 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) { |
| 2375 ASSERT_TRUE(test_server_.Start()); | 2469 ASSERT_TRUE(test_server_.Start()); |
| 2376 | 2470 |
| 2377 TestDelegate d; | 2471 TestDelegate d; |
| 2378 BlockingNetworkDelegateWithManualCallback network_delegate; | 2472 BlockingNetworkDelegate network_delegate( |
| 2379 network_delegate.BlockOn( | 2473 BlockingNetworkDelegate::USER_CALLBACK); |
| 2380 BlockingNetworkDelegateWithManualCallback::ON_AUTH_REQUIRED); | 2474 network_delegate.BlockOn(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
| 2381 | 2475 |
| 2382 TestURLRequestContext context(true); | 2476 TestURLRequestContext context(true); |
| 2383 context.set_network_delegate(&network_delegate); | 2477 context.set_network_delegate(&network_delegate); |
| 2384 context.Init(); | 2478 context.Init(); |
| 2385 | 2479 |
| 2386 { | 2480 { |
| 2387 URLRequest r(test_server_.GetURL("auth-basic"), &d, &context); | 2481 URLRequest r(test_server_.GetURL("auth-basic"), &d, &context); |
| 2388 | 2482 |
| 2389 r.Start(); | 2483 r.Start(); |
| 2390 network_delegate.WaitForState( | 2484 network_delegate.WaitForState( |
| 2391 BlockingNetworkDelegateWithManualCallback::ON_AUTH_REQUIRED); | 2485 BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
| 2392 EXPECT_EQ(0, network_delegate.completed_requests()); | 2486 EXPECT_EQ(0, network_delegate.completed_requests()); |
| 2393 // Cancel before callback. | 2487 // Cancel before callback. |
| 2394 r.Cancel(); | 2488 r.Cancel(); |
| 2395 // Ensure that network delegate is notified. | 2489 // Ensure that network delegate is notified. |
| 2396 EXPECT_EQ(1, network_delegate.completed_requests()); | 2490 EXPECT_EQ(1, network_delegate.completed_requests()); |
| 2397 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); | 2491 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
| 2398 EXPECT_EQ(ERR_ABORTED, r.status().error()); | 2492 EXPECT_EQ(ERR_ABORTED, r.status().error()); |
| 2399 EXPECT_EQ(1, network_delegate.created_requests()); | 2493 EXPECT_EQ(1, network_delegate.created_requests()); |
| 2400 EXPECT_EQ(0, network_delegate.destroyed_requests()); | 2494 EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2401 } | 2495 } |
| (...skipping 2249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 4651 | 4745 |
| 4652 EXPECT_FALSE(r.is_pending()); | 4746 EXPECT_FALSE(r.is_pending()); |
| 4653 EXPECT_EQ(1, d->response_started_count()); | 4747 EXPECT_EQ(1, d->response_started_count()); |
| 4654 EXPECT_FALSE(d->received_data_before_response()); | 4748 EXPECT_FALSE(d->received_data_before_response()); |
| 4655 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); | 4749 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
| 4656 } | 4750 } |
| 4657 } | 4751 } |
| 4658 #endif // !defined(DISABLE_FTP_SUPPORT) | 4752 #endif // !defined(DISABLE_FTP_SUPPORT) |
| 4659 | 4753 |
| 4660 } // namespace net | 4754 } // namespace net |
| OLD | NEW |