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