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

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