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

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: Updated content-length in test to actual length 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
« no previous file with comments | « net/url_request/url_request_test_util.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "build/build_config.h" 5 #include "build/build_config.h"
6 6
7 #if defined(OS_WIN) 7 #if defined(OS_WIN)
8 #include <windows.h> 8 #include <windows.h>
9 #include <shlobj.h> 9 #include <shlobj.h>
10 #endif 10 #endif
(...skipping 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 // Find delegate request begin and end messages for OnBeforeNetworkStart.
3831 // Returns the position of the end message.
3832 static size_t ExpectBeforeNetworkEvents(
3833 const CapturingNetLog::CapturedEntryList& entries,
3834 size_t log_position) {
3835 log_position =
3836 ExpectLogContainsSomewhereAfter(entries,
3837 log_position,
3838 NetLog::TYPE_URL_REQUEST_DELEGATE,
3839 NetLog::PHASE_BEGIN);
3840 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE,
3841 entries[log_position + 1].type);
3842 EXPECT_EQ(NetLog::PHASE_END, entries[log_position + 1].phase);
3843 return log_position + 1;
3844 }
3845
3830 private: 3846 private:
3831 friend class base::RefCounted<AsyncDelegateLogger>; 3847 friend class base::RefCounted<AsyncDelegateLogger>;
3832 3848
3833 AsyncDelegateLogger(URLRequest* url_request, 3849 AsyncDelegateLogger(URLRequest* url_request,
3834 LoadState expected_first_load_state, 3850 LoadState expected_first_load_state,
3835 LoadState expected_second_load_state, 3851 LoadState expected_second_load_state,
3836 LoadState expected_third_load_state, 3852 LoadState expected_third_load_state,
3837 const Callback& callback) 3853 const Callback& callback)
3838 : url_request_(url_request), 3854 : url_request_(url_request),
3839 expected_first_load_state_(expected_first_load_state), 3855 expected_first_load_state_(expected_first_load_state),
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after
4134 log_position + 1, 4150 log_position + 1,
4135 NetLog::TYPE_URL_REQUEST_DELEGATE, 4151 NetLog::TYPE_URL_REQUEST_DELEGATE,
4136 NetLog::PHASE_BEGIN); 4152 NetLog::PHASE_BEGIN);
4137 4153
4138 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, 4154 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4139 log_position + 1); 4155 log_position + 1);
4140 4156
4141 ASSERT_LT(log_position, entries.size()); 4157 ASSERT_LT(log_position, entries.size());
4142 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); 4158 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4143 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); 4159 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4160
4161 if (i == 1) {
4162 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4163 entries, log_position + 1);
4164 }
4144 } 4165 }
4145 4166
4146 EXPECT_FALSE(LogContainsEntryWithTypeAfter( 4167 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4147 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); 4168 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4148 } 4169 }
4149 4170
4150 // Tests handling of delegate info from a network delegate in the case of an 4171 // Tests handling of delegate info from a network delegate in the case of an
4151 // HTTP redirect. 4172 // HTTP redirect.
4152 TEST_F(URLRequestTestHTTP, NetworkDelegateInfoRedirect) { 4173 TEST_F(URLRequestTestHTTP, NetworkDelegateInfoRedirect) {
4153 ASSERT_TRUE(test_server_.Start()); 4174 ASSERT_TRUE(test_server_.Start());
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
4189 log_position + 1, 4210 log_position + 1,
4190 NetLog::TYPE_URL_REQUEST_DELEGATE, 4211 NetLog::TYPE_URL_REQUEST_DELEGATE,
4191 NetLog::PHASE_BEGIN); 4212 NetLog::PHASE_BEGIN);
4192 4213
4193 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, 4214 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4194 log_position + 1); 4215 log_position + 1);
4195 4216
4196 ASSERT_LT(log_position, entries.size()); 4217 ASSERT_LT(log_position, entries.size());
4197 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); 4218 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4198 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); 4219 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4220
4221 if (i == 1) {
4222 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4223 entries, log_position + 1);
4224 }
4199 } 4225 }
4200 4226
4201 // The URLRequest::Delegate then gets informed about the redirect. 4227 // The URLRequest::Delegate then gets informed about the redirect.
4202 log_position = ExpectLogContainsSomewhereAfter( 4228 log_position = ExpectLogContainsSomewhereAfter(
4203 entries, 4229 entries,
4204 log_position + 1, 4230 log_position + 1,
4205 NetLog::TYPE_URL_REQUEST_DELEGATE, 4231 NetLog::TYPE_URL_REQUEST_DELEGATE,
4206 NetLog::PHASE_BEGIN); 4232 NetLog::PHASE_BEGIN);
4207 4233
4208 // The NetworkDelegate logged information in the same three events as before. 4234 // 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, 4294 log_position + 1,
4269 NetLog::TYPE_URL_REQUEST_DELEGATE, 4295 NetLog::TYPE_URL_REQUEST_DELEGATE,
4270 NetLog::PHASE_BEGIN); 4296 NetLog::PHASE_BEGIN);
4271 4297
4272 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, 4298 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4273 log_position + 1); 4299 log_position + 1);
4274 4300
4275 ASSERT_LT(log_position, entries.size()); 4301 ASSERT_LT(log_position, entries.size());
4276 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); 4302 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4277 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); 4303 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4304
4305 if (i == 1) {
4306 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4307 entries, log_position + 1);
4308 }
4278 } 4309 }
4279 4310
4280 EXPECT_FALSE(LogContainsEntryWithTypeAfter( 4311 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4281 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); 4312 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4282 } 4313 }
4283 4314
4284 // Tests handling of delegate info from a URLRequest::Delegate. 4315 // Tests handling of delegate info from a URLRequest::Delegate.
4285 TEST_F(URLRequestTestHTTP, URLRequestDelegateInfo) { 4316 TEST_F(URLRequestTestHTTP, URLRequestDelegateInfo) {
4286 ASSERT_TRUE(test_server_.Start()); 4317 ASSERT_TRUE(test_server_.Start());
4287 4318
4288 AsyncLoggingUrlRequestDelegate request_delegate( 4319 AsyncLoggingUrlRequestDelegate request_delegate(
4289 AsyncLoggingUrlRequestDelegate::NO_CANCEL); 4320 AsyncLoggingUrlRequestDelegate::NO_CANCEL);
4290 TestURLRequestContext context(true); 4321 TestURLRequestContext context(true);
4291 context.set_network_delegate(NULL); 4322 context.set_network_delegate(NULL);
4292 context.set_net_log(&net_log_); 4323 context.set_net_log(&net_log_);
4293 context.Init(); 4324 context.Init();
4294 4325
4295 { 4326 {
4296 // A chunked response with delays between chunks is used to make sure that 4327 // 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 4328 // attempts by the URLRequest delegate to log information while reading the
4298 // body are ignored. Since they are ignored, this test is robust against 4329 // body are ignored. Since they are ignored, this test is robust against
4299 // the possability of multiple reads being combined in the unlikely event 4330 // the possibility of multiple reads being combined in the unlikely event
4300 // that it occurs. 4331 // that it occurs.
4301 URLRequest r(test_server_.GetURL("chunked?waitBetweenChunks=20"), 4332 URLRequest r(test_server_.GetURL("chunked?waitBetweenChunks=20"),
4302 DEFAULT_PRIORITY, 4333 DEFAULT_PRIORITY,
4303 &request_delegate, 4334 &request_delegate,
4304 &context); 4335 &context);
4305 LoadStateWithParam load_state = r.GetLoadState(); 4336 LoadStateWithParam load_state = r.GetLoadState();
4306 r.Start(); 4337 r.Start();
4307 base::RunLoop().Run(); 4338 base::RunLoop().Run();
4308 4339
4309 EXPECT_EQ(200, r.GetResponseCode()); 4340 EXPECT_EQ(200, r.GetResponseCode());
4310 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); 4341 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
4311 } 4342 }
4312 4343
4313 CapturingNetLog::CapturedEntryList entries; 4344 CapturingNetLog::CapturedEntryList entries;
4314 net_log_.GetEntries(&entries); 4345 net_log_.GetEntries(&entries);
4315 4346
4347 size_t log_position = 0;
4348
4349 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4350 entries, log_position);
4351
4316 // The delegate info should only have been logged on header complete. Other 4352 // The delegate info should only have been logged on header complete. Other
4317 // times it should silently be ignored. 4353 // times it should silently be ignored.
4318 4354 log_position =
4319 size_t log_position = ExpectLogContainsSomewhereAfter( 4355 ExpectLogContainsSomewhereAfter(entries,
4320 entries, 4356 log_position + 1,
4321 0, 4357 NetLog::TYPE_URL_REQUEST_DELEGATE,
4322 NetLog::TYPE_URL_REQUEST_DELEGATE, 4358 NetLog::PHASE_BEGIN);
4323 NetLog::PHASE_BEGIN);
4324 4359
4325 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, 4360 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4326 log_position + 1); 4361 log_position + 1);
4327 4362
4328 ASSERT_LT(log_position, entries.size()); 4363 ASSERT_LT(log_position, entries.size());
4329 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); 4364 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4330 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); 4365 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4331 4366
4332 EXPECT_FALSE(LogContainsEntryWithTypeAfter( 4367 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4333 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); 4368 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
(...skipping 26 matching lines...) Expand all
4360 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); 4395 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
4361 } 4396 }
4362 4397
4363 CapturingNetLog::CapturedEntryList entries; 4398 CapturingNetLog::CapturedEntryList entries;
4364 net_log_.GetEntries(&entries); 4399 net_log_.GetEntries(&entries);
4365 4400
4366 // Delegate info should only have been logged in OnReceivedRedirect and 4401 // Delegate info should only have been logged in OnReceivedRedirect and
4367 // OnResponseStarted. 4402 // OnResponseStarted.
4368 size_t log_position = 0; 4403 size_t log_position = 0;
4369 for (int i = 0; i < 2; ++i) { 4404 for (int i = 0; i < 2; ++i) {
4405 if (i == 0) {
4406 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4407 entries, log_position) + 1;
4408 }
4409
4370 log_position = ExpectLogContainsSomewhereAfter( 4410 log_position = ExpectLogContainsSomewhereAfter(
4371 entries, 4411 entries,
4372 log_position, 4412 log_position,
4373 NetLog::TYPE_URL_REQUEST_DELEGATE, 4413 NetLog::TYPE_URL_REQUEST_DELEGATE,
4374 NetLog::PHASE_BEGIN); 4414 NetLog::PHASE_BEGIN);
4375 4415
4376 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, 4416 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4377 log_position + 1); 4417 log_position + 1);
4378 4418
4379 ASSERT_LT(log_position, entries.size()); 4419 ASSERT_LT(log_position, entries.size());
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
4420 4460
4421 CapturingNetLog::CapturedEntryList entries; 4461 CapturingNetLog::CapturedEntryList entries;
4422 net_log.GetEntries(&entries); 4462 net_log.GetEntries(&entries);
4423 4463
4424 // Delegate info is always logged in both OnReceivedRedirect and 4464 // Delegate info is always logged in both OnReceivedRedirect and
4425 // OnResponseStarted. In the CANCEL_ON_RECEIVED_REDIRECT, the 4465 // OnResponseStarted. In the CANCEL_ON_RECEIVED_REDIRECT, the
4426 // OnResponseStarted delegate call is after cancellation, but logging is 4466 // OnResponseStarted delegate call is after cancellation, but logging is
4427 // still currently supported in that call. 4467 // still currently supported in that call.
4428 size_t log_position = 0; 4468 size_t log_position = 0;
4429 for (int i = 0; i < 2; ++i) { 4469 for (int i = 0; i < 2; ++i) {
4470 if (i == 0) {
4471 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4472 entries, log_position) + 1;
4473 }
4474
4430 log_position = ExpectLogContainsSomewhereAfter( 4475 log_position = ExpectLogContainsSomewhereAfter(
4431 entries, 4476 entries,
4432 log_position, 4477 log_position,
4433 NetLog::TYPE_URL_REQUEST_DELEGATE, 4478 NetLog::TYPE_URL_REQUEST_DELEGATE,
4434 NetLog::PHASE_BEGIN); 4479 NetLog::PHASE_BEGIN);
4435 4480
4436 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, 4481 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4437 log_position + 1); 4482 log_position + 1);
4438 4483
4439 ASSERT_LT(log_position, entries.size()); 4484 ASSERT_LT(log_position, entries.size());
(...skipping 737 matching lines...) Expand 10 before | Expand all | Expand 10 after
5177 &d, 5222 &d,
5178 &default_context_); 5223 &default_context_);
5179 req.SetReferrer("http://foo.com/test#fragment"); 5224 req.SetReferrer("http://foo.com/test#fragment");
5180 req.SetReferrer(""); 5225 req.SetReferrer("");
5181 req.Start(); 5226 req.Start();
5182 base::RunLoop().Run(); 5227 base::RunLoop().Run();
5183 5228
5184 EXPECT_EQ(std::string("None"), d.data_received()); 5229 EXPECT_EQ(std::string("None"), d.data_received());
5185 } 5230 }
5186 5231
5232 // Defer network start and then resume, checking that the request was a success
5233 // and bytes were received.
5234 TEST_F(URLRequestTestHTTP, DeferredBeforeNetworkStart) {
5235 ASSERT_TRUE(test_server_.Start());
5236
5237 TestDelegate d;
5238 {
5239 d.set_quit_on_network_start(true);
5240 GURL test_url(test_server_.GetURL("echo"));
5241 URLRequest req(test_url, DEFAULT_PRIORITY, &d, &default_context_);
5242
5243 req.Start();
5244 base::RunLoop().Run();
5245
5246 EXPECT_EQ(1, d.received_before_network_start_count());
5247 EXPECT_EQ(0, d.response_started_count());
5248
5249 req.ResumeNetworkStart();
5250 base::RunLoop().Run();
5251
5252 EXPECT_EQ(1, d.response_started_count());
5253 EXPECT_NE(0, d.bytes_received());
5254 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
5255 }
5256 }
5257
5258 // Check that OnBeforeNetworkStart is only called once even if there is a
5259 // redirect.
5260 TEST_F(URLRequestTestHTTP, BeforeNetworkStartCalledOnce) {
5261 ASSERT_TRUE(test_server_.Start());
5262
5263 TestDelegate d;
5264 {
5265 d.set_quit_on_redirect(true);
5266 d.set_quit_on_network_start(true);
5267 URLRequest req(test_server_.GetURL("server-redirect?echo"),
5268 DEFAULT_PRIORITY,
5269 &d,
5270 &default_context_);
5271
5272 req.Start();
5273 base::RunLoop().Run();
5274
5275 EXPECT_EQ(1, d.received_before_network_start_count());
5276 EXPECT_EQ(0, d.response_started_count());
5277 EXPECT_EQ(0, d.received_redirect_count());
5278
5279 req.ResumeNetworkStart();
5280 base::RunLoop().Run();
5281
5282 EXPECT_EQ(1, d.received_redirect_count());
5283 req.FollowDeferredRedirect();
5284 base::RunLoop().Run();
5285
5286 // Check that the redirect's new network transaction does not get propagated
5287 // to a second OnBeforeNetworkStart() notification.
5288 EXPECT_EQ(1, d.received_before_network_start_count());
5289
5290 EXPECT_EQ(1, d.response_started_count());
5291 EXPECT_NE(0, d.bytes_received());
5292 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
5293 }
5294 }
5295
5296 // Cancel the request after learning that the request would use the network.
5297 TEST_F(URLRequestTestHTTP, CancelOnBeforeNetworkStart) {
5298 ASSERT_TRUE(test_server_.Start());
5299
5300 TestDelegate d;
5301 {
5302 d.set_quit_on_network_start(true);
5303 GURL test_url(test_server_.GetURL("echo"));
5304 URLRequest req(test_url, DEFAULT_PRIORITY, &d, &default_context_);
5305
5306 req.Start();
5307 base::RunLoop().Run();
5308
5309 EXPECT_EQ(1, d.received_before_network_start_count());
5310 EXPECT_EQ(0, d.response_started_count());
5311
5312 req.Cancel();
5313 base::RunLoop().Run();
5314
5315 EXPECT_EQ(1, d.response_started_count());
5316 EXPECT_EQ(0, d.bytes_received());
5317 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
5318 }
5319 }
5320
5187 TEST_F(URLRequestTestHTTP, CancelRedirect) { 5321 TEST_F(URLRequestTestHTTP, CancelRedirect) {
5188 ASSERT_TRUE(test_server_.Start()); 5322 ASSERT_TRUE(test_server_.Start());
5189 5323
5190 TestDelegate d; 5324 TestDelegate d;
5191 { 5325 {
5192 d.set_cancel_in_received_redirect(true); 5326 d.set_cancel_in_received_redirect(true);
5193 URLRequest req(test_server_.GetURL("files/redirect-test.html"), 5327 URLRequest req(test_server_.GetURL("files/redirect-test.html"),
5194 DEFAULT_PRIORITY, 5328 DEFAULT_PRIORITY,
5195 &d, 5329 &d,
5196 &default_context_); 5330 &default_context_);
(...skipping 2284 matching lines...) Expand 10 before | Expand all | Expand 10 after
7481 7615
7482 EXPECT_FALSE(r.is_pending()); 7616 EXPECT_FALSE(r.is_pending());
7483 EXPECT_EQ(1, d->response_started_count()); 7617 EXPECT_EQ(1, d->response_started_count());
7484 EXPECT_FALSE(d->received_data_before_response()); 7618 EXPECT_FALSE(d->received_data_before_response());
7485 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); 7619 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
7486 } 7620 }
7487 } 7621 }
7488 #endif // !defined(DISABLE_FTP_SUPPORT) 7622 #endif // !defined(DISABLE_FTP_SUPPORT)
7489 7623
7490 } // namespace net 7624 } // namespace net
OLDNEW
« no previous file with comments | « net/url_request/url_request_test_util.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698