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

Side by Side Diff: net/url_request/url_request_unittest.cc

Issue 122453002: Allows deferral of a URLRequest just before talking to the network, at (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Formatting nits Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "build/build_config.h" 5 #include "build/build_config.h"
6 6
7 #if defined(OS_WIN) 7 #if defined(OS_WIN)
8 #include <windows.h> 8 #include <windows.h>
9 #include <shlobj.h> 9 #include <shlobj.h>
10 #endif 10 #endif
(...skipping 3809 matching lines...) Expand 10 before | Expand all | Expand 10 after
3820 &delegate_info)); 3820 &delegate_info));
3821 EXPECT_EQ(kSecondDelegateInfo, delegate_info); 3821 EXPECT_EQ(kSecondDelegateInfo, delegate_info);
3822 3822
3823 ++log_position; 3823 ++log_position;
3824 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); 3824 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type);
3825 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); 3825 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
3826 3826
3827 return log_position + 1; 3827 return log_position + 1;
3828 } 3828 }
3829 3829
3830 static size_t ExpectBeforeNetworkStartBeginAndEnd(
3831 const CapturingNetLog::CapturedEntryList& entries, size_t log_position) {
3832 log_position = ExpectLogContainsSomewhereAfter(
3833 entries, log_position, NetLog::TYPE_URL_REQUEST_DELEGATE,
3834 NetLog::PHASE_BEGIN);
3835 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE,
3836 entries[log_position + 1].type);
3837 EXPECT_EQ(NetLog::PHASE_END, entries[log_position + 1].phase);
3838 return log_position + 1;
3839 }
3840
3830 private: 3841 private:
3831 friend class base::RefCounted<AsyncDelegateLogger>; 3842 friend class base::RefCounted<AsyncDelegateLogger>;
3832 3843
3833 AsyncDelegateLogger(URLRequest* url_request, 3844 AsyncDelegateLogger(URLRequest* url_request,
3834 LoadState expected_first_load_state, 3845 LoadState expected_first_load_state,
3835 LoadState expected_second_load_state, 3846 LoadState expected_second_load_state,
3836 LoadState expected_third_load_state, 3847 LoadState expected_third_load_state,
3837 const Callback& callback) 3848 const Callback& callback)
3838 : url_request_(url_request), 3849 : url_request_(url_request),
3839 expected_first_load_state_(expected_first_load_state), 3850 expected_first_load_state_(expected_first_load_state),
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after
4134 log_position + 1, 4145 log_position + 1,
4135 NetLog::TYPE_URL_REQUEST_DELEGATE, 4146 NetLog::TYPE_URL_REQUEST_DELEGATE,
4136 NetLog::PHASE_BEGIN); 4147 NetLog::PHASE_BEGIN);
4137 4148
4138 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, 4149 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4139 log_position + 1); 4150 log_position + 1);
4140 4151
4141 ASSERT_LT(log_position, entries.size()); 4152 ASSERT_LT(log_position, entries.size());
4142 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); 4153 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4143 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); 4154 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4155
4156 if (i == 1)
4157 log_position = AsyncDelegateLogger::ExpectBeforeNetworkStartBeginAndEnd(
4158 entries, log_position + 1);
mmenke 2014/01/07 15:26:23 nit: Use braces when body of if takes up more tha
jkarlin 2014/01/07 16:19:32 Done.
4144 } 4159 }
4145 4160
4146 EXPECT_FALSE(LogContainsEntryWithTypeAfter( 4161 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4147 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); 4162 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4148 } 4163 }
4149 4164
4150 // Tests handling of delegate info from a network delegate in the case of an 4165 // Tests handling of delegate info from a network delegate in the case of an
4151 // HTTP redirect. 4166 // HTTP redirect.
4152 TEST_F(URLRequestTestHTTP, NetworkDelegateInfoRedirect) { 4167 TEST_F(URLRequestTestHTTP, NetworkDelegateInfoRedirect) {
4153 ASSERT_TRUE(test_server_.Start()); 4168 ASSERT_TRUE(test_server_.Start());
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
4189 log_position + 1, 4204 log_position + 1,
4190 NetLog::TYPE_URL_REQUEST_DELEGATE, 4205 NetLog::TYPE_URL_REQUEST_DELEGATE,
4191 NetLog::PHASE_BEGIN); 4206 NetLog::PHASE_BEGIN);
4192 4207
4193 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, 4208 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4194 log_position + 1); 4209 log_position + 1);
4195 4210
4196 ASSERT_LT(log_position, entries.size()); 4211 ASSERT_LT(log_position, entries.size());
4197 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); 4212 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4198 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); 4213 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4214
4215 if (i == 1)
4216 log_position = AsyncDelegateLogger::ExpectBeforeNetworkStartBeginAndEnd(
4217 entries, log_position + 1);
4199 } 4218 }
4200 4219
4201 // The URLRequest::Delegate then gets informed about the redirect. 4220 // The URLRequest::Delegate then gets informed about the redirect.
4202 log_position = ExpectLogContainsSomewhereAfter( 4221 log_position = ExpectLogContainsSomewhereAfter(
4203 entries, 4222 entries,
4204 log_position + 1, 4223 log_position + 1,
4205 NetLog::TYPE_URL_REQUEST_DELEGATE, 4224 NetLog::TYPE_URL_REQUEST_DELEGATE,
4206 NetLog::PHASE_BEGIN); 4225 NetLog::PHASE_BEGIN);
4207 4226
4208 // The NetworkDelegate logged information in the same three events as before. 4227 // The NetworkDelegate logged information in the same three events as before.
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
4268 log_position + 1, 4287 log_position + 1,
4269 NetLog::TYPE_URL_REQUEST_DELEGATE, 4288 NetLog::TYPE_URL_REQUEST_DELEGATE,
4270 NetLog::PHASE_BEGIN); 4289 NetLog::PHASE_BEGIN);
4271 4290
4272 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, 4291 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4273 log_position + 1); 4292 log_position + 1);
4274 4293
4275 ASSERT_LT(log_position, entries.size()); 4294 ASSERT_LT(log_position, entries.size());
4276 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); 4295 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4277 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); 4296 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4297
4298 if (i == 1)
4299 log_position = AsyncDelegateLogger::ExpectBeforeNetworkStartBeginAndEnd(
4300 entries, log_position + 1);
4278 } 4301 }
4279 4302
4280 EXPECT_FALSE(LogContainsEntryWithTypeAfter( 4303 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4281 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); 4304 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4282 } 4305 }
4283 4306
4284 // Tests handling of delegate info from a URLRequest::Delegate. 4307 // Tests handling of delegate info from a URLRequest::Delegate.
4285 TEST_F(URLRequestTestHTTP, URLRequestDelegateInfo) { 4308 TEST_F(URLRequestTestHTTP, URLRequestDelegateInfo) {
4286 ASSERT_TRUE(test_server_.Start()); 4309 ASSERT_TRUE(test_server_.Start());
4287 4310
4288 AsyncLoggingUrlRequestDelegate request_delegate( 4311 AsyncLoggingUrlRequestDelegate request_delegate(
4289 AsyncLoggingUrlRequestDelegate::NO_CANCEL); 4312 AsyncLoggingUrlRequestDelegate::NO_CANCEL);
4290 TestURLRequestContext context(true); 4313 TestURLRequestContext context(true);
4291 context.set_network_delegate(NULL); 4314 context.set_network_delegate(NULL);
4292 context.set_net_log(&net_log_); 4315 context.set_net_log(&net_log_);
4293 context.Init(); 4316 context.Init();
4294 4317
4295 { 4318 {
4296 // A chunked response with delays between chunks is used to make sure that 4319 // A chunked response with delays between chunks is used to make sure that
4297 // attempts by the URLRequest delegate to log information while reading the 4320 // attempts by the URLRequest delegate to log information while reading the
4298 // body are ignored. Since they are ignored, this test is robust against 4321 // body are ignored. Since they are ignored, this test is robust against
4299 // the possability of multiple reads being combined in the unlikely event 4322 // the possibility of multiple reads being combined in the unlikely event
4300 // that it occurs. 4323 // that it occurs.
4301 URLRequest r(test_server_.GetURL("chunked?waitBetweenChunks=20"), 4324 URLRequest r(test_server_.GetURL("chunked?waitBetweenChunks=20"),
4302 DEFAULT_PRIORITY, 4325 DEFAULT_PRIORITY,
4303 &request_delegate, 4326 &request_delegate,
4304 &context); 4327 &context);
4305 LoadStateWithParam load_state = r.GetLoadState(); 4328 LoadStateWithParam load_state = r.GetLoadState();
4306 r.Start(); 4329 r.Start();
4307 base::RunLoop().Run(); 4330 base::RunLoop().Run();
4308 4331
4309 EXPECT_EQ(200, r.GetResponseCode()); 4332 EXPECT_EQ(200, r.GetResponseCode());
4310 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); 4333 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
4311 } 4334 }
4312 4335
4313 CapturingNetLog::CapturedEntryList entries; 4336 CapturingNetLog::CapturedEntryList entries;
4314 net_log_.GetEntries(&entries); 4337 net_log_.GetEntries(&entries);
4315 4338
4339 size_t log_position = 0;
4340
4341 log_position = AsyncDelegateLogger::ExpectBeforeNetworkStartBeginAndEnd(
4342 entries, log_position);
4343
4316 // The delegate info should only have been logged on header complete. Other 4344 // The delegate info should only have been logged on header complete. Other
4317 // times it should silently be ignored. 4345 // times it should silently be ignored.
4318 4346 log_position = ExpectLogContainsSomewhereAfter(
4319 size_t log_position = ExpectLogContainsSomewhereAfter( 4347 entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE,
4320 entries, 4348 NetLog::PHASE_BEGIN);
4321 0,
4322 NetLog::TYPE_URL_REQUEST_DELEGATE,
4323 NetLog::PHASE_BEGIN);
4324 4349
4325 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, 4350 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4326 log_position + 1); 4351 log_position + 1);
4327 4352
4328 ASSERT_LT(log_position, entries.size()); 4353 ASSERT_LT(log_position, entries.size());
4329 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); 4354 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4330 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); 4355 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4331 4356
4332 EXPECT_FALSE(LogContainsEntryWithTypeAfter( 4357 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4333 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); 4358 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
(...skipping 26 matching lines...) Expand all
4360 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); 4385 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
4361 } 4386 }
4362 4387
4363 CapturingNetLog::CapturedEntryList entries; 4388 CapturingNetLog::CapturedEntryList entries;
4364 net_log_.GetEntries(&entries); 4389 net_log_.GetEntries(&entries);
4365 4390
4366 // Delegate info should only have been logged in OnReceivedRedirect and 4391 // Delegate info should only have been logged in OnReceivedRedirect and
4367 // OnResponseStarted. 4392 // OnResponseStarted.
4368 size_t log_position = 0; 4393 size_t log_position = 0;
4369 for (int i = 0; i < 2; ++i) { 4394 for (int i = 0; i < 2; ++i) {
4395 if (i == 0)
4396 log_position = AsyncDelegateLogger::ExpectBeforeNetworkStartBeginAndEnd(
4397 entries, log_position) + 1;
4398
4370 log_position = ExpectLogContainsSomewhereAfter( 4399 log_position = ExpectLogContainsSomewhereAfter(
4371 entries, 4400 entries,
4372 log_position, 4401 log_position,
4373 NetLog::TYPE_URL_REQUEST_DELEGATE, 4402 NetLog::TYPE_URL_REQUEST_DELEGATE,
4374 NetLog::PHASE_BEGIN); 4403 NetLog::PHASE_BEGIN);
4375 4404
4376 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, 4405 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4377 log_position + 1); 4406 log_position + 1);
4378 4407
4379 ASSERT_LT(log_position, entries.size()); 4408 ASSERT_LT(log_position, entries.size());
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
4420 4449
4421 CapturingNetLog::CapturedEntryList entries; 4450 CapturingNetLog::CapturedEntryList entries;
4422 net_log.GetEntries(&entries); 4451 net_log.GetEntries(&entries);
4423 4452
4424 // Delegate info is always logged in both OnReceivedRedirect and 4453 // Delegate info is always logged in both OnReceivedRedirect and
4425 // OnResponseStarted. In the CANCEL_ON_RECEIVED_REDIRECT, the 4454 // OnResponseStarted. In the CANCEL_ON_RECEIVED_REDIRECT, the
4426 // OnResponseStarted delegate call is after cancellation, but logging is 4455 // OnResponseStarted delegate call is after cancellation, but logging is
4427 // still currently supported in that call. 4456 // still currently supported in that call.
4428 size_t log_position = 0; 4457 size_t log_position = 0;
4429 for (int i = 0; i < 2; ++i) { 4458 for (int i = 0; i < 2; ++i) {
4459 if (i == 0)
4460 log_position =
4461 AsyncDelegateLogger::ExpectBeforeNetworkStartBeginAndEnd(
4462 entries, log_position) + 1;
4463
4430 log_position = ExpectLogContainsSomewhereAfter( 4464 log_position = ExpectLogContainsSomewhereAfter(
4431 entries, 4465 entries,
4432 log_position, 4466 log_position,
4433 NetLog::TYPE_URL_REQUEST_DELEGATE, 4467 NetLog::TYPE_URL_REQUEST_DELEGATE,
4434 NetLog::PHASE_BEGIN); 4468 NetLog::PHASE_BEGIN);
4435 4469
4436 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, 4470 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4437 log_position + 1); 4471 log_position + 1);
4438 4472
4439 ASSERT_LT(log_position, entries.size()); 4473 ASSERT_LT(log_position, entries.size());
(...skipping 737 matching lines...) Expand 10 before | Expand all | Expand 10 after
5177 &d, 5211 &d,
5178 &default_context_); 5212 &default_context_);
5179 req.SetReferrer("http://foo.com/test#fragment"); 5213 req.SetReferrer("http://foo.com/test#fragment");
5180 req.SetReferrer(""); 5214 req.SetReferrer("");
5181 req.Start(); 5215 req.Start();
5182 base::RunLoop().Run(); 5216 base::RunLoop().Run();
5183 5217
5184 EXPECT_EQ(std::string("None"), d.data_received()); 5218 EXPECT_EQ(std::string("None"), d.data_received());
5185 } 5219 }
5186 5220
5221 // Defer network start and then resume, checking that the request was a success
5222 // and bytes were received.
5223 TEST_F(URLRequestTestHTTP, DeferredBeforeNetworkStart) {
5224 ASSERT_TRUE(test_server_.Start());
5225
5226 TestDelegate d;
5227 {
5228 d.set_quit_on_network_start(true);
5229 GURL test_url(test_server_.GetURL("echo"));
5230 URLRequest req(test_url, DEFAULT_PRIORITY, &d, &default_context_);
5231
5232 req.Start();
5233 base::RunLoop().Run();
5234
5235 EXPECT_EQ(1, d.received_before_network_start_count());
5236 EXPECT_EQ(0, d.response_started_count());
5237
5238 req.ResumeNetworkStart();
5239 base::RunLoop().Run();
5240
5241 EXPECT_EQ(1, d.response_started_count());
5242 EXPECT_NE(0, d.bytes_received());
5243 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
5244 }
5245 }
5246
5247 // Check that OnBeforeNetworkStart is only called once even if there is a
5248 // redirect.
5249 TEST_F(URLRequestTestHTTP, BeforeNetworkStartCalledOnce) {
5250 ASSERT_TRUE(test_server_.Start());
5251
5252 TestDelegate d;
5253 {
5254 d.set_quit_on_redirect(true);
5255 d.set_quit_on_network_start(true);
5256 URLRequest req(test_server_.GetURL("server-redirect?simple.html"),
5257 DEFAULT_PRIORITY,
5258 &d,
5259 &default_context_);
5260
5261 req.Start();
5262 base::RunLoop().Run();
5263
5264 EXPECT_EQ(1, d.received_before_network_start_count());
5265 EXPECT_EQ(0, d.response_started_count());
5266 EXPECT_EQ(0, d.received_redirect_count());
5267
5268 req.ResumeNetworkStart();
5269 base::RunLoop().Run();
5270
5271 EXPECT_EQ(1, d.received_redirect_count());
5272 req.FollowDeferredRedirect();
5273 base::RunLoop().Run();
5274
5275 // Check that the redirect's new network transaction does not get propagated
5276 // to a second OnBeforeNetworkStart() notification.
5277 EXPECT_EQ(1, d.received_before_network_start_count());
5278
5279 EXPECT_EQ(1, d.response_started_count());
5280 EXPECT_NE(0, d.bytes_received());
5281 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
5282 }
5283 }
5284
5285 // Cancel the request after learning that the request would use the network.
5286 TEST_F(URLRequestTestHTTP, CancelOnBeforeNetworkStart) {
5287 ASSERT_TRUE(test_server_.Start());
5288
5289 TestDelegate d;
5290 {
5291 d.set_quit_on_network_start(true);
5292 GURL test_url(test_server_.GetURL("echo"));
5293 URLRequest req(test_url, DEFAULT_PRIORITY, &d, &default_context_);
5294
5295 req.Start();
5296 base::RunLoop().Run();
5297
5298 EXPECT_EQ(1, d.received_before_network_start_count());
5299 EXPECT_EQ(0, d.response_started_count());
5300
5301 req.Cancel();
5302 base::RunLoop().Run();
5303
5304 EXPECT_EQ(1, d.response_started_count());
5305 EXPECT_EQ(0, d.bytes_received());
5306 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
5307 }
5308 }
5309
5187 TEST_F(URLRequestTestHTTP, CancelRedirect) { 5310 TEST_F(URLRequestTestHTTP, CancelRedirect) {
5188 ASSERT_TRUE(test_server_.Start()); 5311 ASSERT_TRUE(test_server_.Start());
5189 5312
5190 TestDelegate d; 5313 TestDelegate d;
5191 { 5314 {
5192 d.set_cancel_in_received_redirect(true); 5315 d.set_cancel_in_received_redirect(true);
5193 URLRequest req(test_server_.GetURL("files/redirect-test.html"), 5316 URLRequest req(test_server_.GetURL("files/redirect-test.html"),
5194 DEFAULT_PRIORITY, 5317 DEFAULT_PRIORITY,
5195 &d, 5318 &d,
5196 &default_context_); 5319 &default_context_);
(...skipping 2284 matching lines...) Expand 10 before | Expand all | Expand 10 after
7481 7604
7482 EXPECT_FALSE(r.is_pending()); 7605 EXPECT_FALSE(r.is_pending());
7483 EXPECT_EQ(1, d->response_started_count()); 7606 EXPECT_EQ(1, d->response_started_count());
7484 EXPECT_FALSE(d->received_data_before_response()); 7607 EXPECT_FALSE(d->received_data_before_response());
7485 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); 7608 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
7486 } 7609 }
7487 } 7610 }
7488 #endif // !defined(DISABLE_FTP_SUPPORT) 7611 #endif // !defined(DISABLE_FTP_SUPPORT)
7489 7612
7490 } // namespace net 7613 } // namespace net
OLDNEW
« net/url_request/url_request_http_job.h ('K') | « net/url_request/url_request_test_util.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698