Index: net/url_request/url_request_unittest.cc |
diff --git a/net/url_request/url_request_unittest.cc b/net/url_request/url_request_unittest.cc |
index 57849cd695ee7060f7dc5891537e898f521fded4..d2d369b80c277e8a06cb0aa9156b7886813b5653 100644 |
--- a/net/url_request/url_request_unittest.cc |
+++ b/net/url_request/url_request_unittest.cc |
@@ -3827,6 +3827,22 @@ class AsyncDelegateLogger : public base::RefCounted<AsyncDelegateLogger> { |
return log_position + 1; |
} |
+ // Find delegate request begin and end messages for OnBeforeNetworkStart. |
+ // Returns the position of the end message. |
+ static size_t ExpectBeforeNetworkEvents( |
+ const CapturingNetLog::CapturedEntryList& entries, |
+ size_t log_position) { |
+ log_position = |
+ ExpectLogContainsSomewhereAfter(entries, |
+ log_position, |
+ NetLog::TYPE_URL_REQUEST_DELEGATE, |
+ NetLog::PHASE_BEGIN); |
+ EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, |
+ entries[log_position + 1].type); |
+ EXPECT_EQ(NetLog::PHASE_END, entries[log_position + 1].phase); |
+ return log_position + 1; |
+ } |
+ |
private: |
friend class base::RefCounted<AsyncDelegateLogger>; |
@@ -4141,6 +4157,11 @@ TEST_F(URLRequestTestHTTP, NetworkDelegateInfo) { |
ASSERT_LT(log_position, entries.size()); |
EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
+ |
+ if (i == 1) { |
+ log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
+ entries, log_position + 1); |
+ } |
} |
EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
@@ -4196,6 +4217,11 @@ TEST_F(URLRequestTestHTTP, NetworkDelegateInfoRedirect) { |
ASSERT_LT(log_position, entries.size()); |
EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
+ |
+ if (i == 1) { |
+ log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
+ entries, log_position + 1); |
+ } |
} |
// The URLRequest::Delegate then gets informed about the redirect. |
@@ -4275,6 +4301,11 @@ TEST_F(URLRequestTestHTTP, NetworkDelegateInfoAuth) { |
ASSERT_LT(log_position, entries.size()); |
EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
+ |
+ if (i == 1) { |
+ log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
+ entries, log_position + 1); |
+ } |
} |
EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
@@ -4296,7 +4327,7 @@ TEST_F(URLRequestTestHTTP, URLRequestDelegateInfo) { |
// A chunked response with delays between chunks is used to make sure that |
// attempts by the URLRequest delegate to log information while reading the |
// body are ignored. Since they are ignored, this test is robust against |
- // the possability of multiple reads being combined in the unlikely event |
+ // the possibility of multiple reads being combined in the unlikely event |
// that it occurs. |
URLRequest r(test_server_.GetURL("chunked?waitBetweenChunks=20"), |
DEFAULT_PRIORITY, |
@@ -4313,14 +4344,18 @@ TEST_F(URLRequestTestHTTP, URLRequestDelegateInfo) { |
CapturingNetLog::CapturedEntryList entries; |
net_log_.GetEntries(&entries); |
+ size_t log_position = 0; |
+ |
+ log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
+ entries, log_position); |
+ |
// The delegate info should only have been logged on header complete. Other |
// times it should silently be ignored. |
- |
- size_t log_position = ExpectLogContainsSomewhereAfter( |
- entries, |
- 0, |
- NetLog::TYPE_URL_REQUEST_DELEGATE, |
- NetLog::PHASE_BEGIN); |
+ log_position = |
+ ExpectLogContainsSomewhereAfter(entries, |
+ log_position + 1, |
+ NetLog::TYPE_URL_REQUEST_DELEGATE, |
+ NetLog::PHASE_BEGIN); |
log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
log_position + 1); |
@@ -4367,6 +4402,11 @@ TEST_F(URLRequestTestHTTP, URLRequestDelegateInfoOnRedirect) { |
// OnResponseStarted. |
size_t log_position = 0; |
for (int i = 0; i < 2; ++i) { |
+ if (i == 0) { |
+ log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
+ entries, log_position) + 1; |
+ } |
+ |
log_position = ExpectLogContainsSomewhereAfter( |
entries, |
log_position, |
@@ -4427,6 +4467,11 @@ TEST_F(URLRequestTestHTTP, URLRequestDelegateOnRedirectCancelled) { |
// still currently supported in that call. |
size_t log_position = 0; |
for (int i = 0; i < 2; ++i) { |
+ if (i == 0) { |
+ log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
+ entries, log_position) + 1; |
+ } |
+ |
log_position = ExpectLogContainsSomewhereAfter( |
entries, |
log_position, |
@@ -5184,6 +5229,95 @@ TEST_F(URLRequestTestHTTP, EmptyReferrerAfterValidReferrer) { |
EXPECT_EQ(std::string("None"), d.data_received()); |
} |
+// Defer network start and then resume, checking that the request was a success |
+// and bytes were received. |
+TEST_F(URLRequestTestHTTP, DeferredBeforeNetworkStart) { |
+ ASSERT_TRUE(test_server_.Start()); |
+ |
+ TestDelegate d; |
+ { |
+ d.set_quit_on_network_start(true); |
+ GURL test_url(test_server_.GetURL("echo")); |
+ URLRequest req(test_url, DEFAULT_PRIORITY, &d, &default_context_); |
+ |
+ req.Start(); |
+ base::RunLoop().Run(); |
+ |
+ EXPECT_EQ(1, d.received_before_network_start_count()); |
+ EXPECT_EQ(0, d.response_started_count()); |
+ |
+ req.ResumeNetworkStart(); |
+ base::RunLoop().Run(); |
+ |
+ EXPECT_EQ(1, d.response_started_count()); |
+ EXPECT_NE(0, d.bytes_received()); |
+ EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status()); |
+ } |
+} |
+ |
+// Check that OnBeforeNetworkStart is only called once even if there is a |
+// redirect. |
+TEST_F(URLRequestTestHTTP, BeforeNetworkStartCalledOnce) { |
+ ASSERT_TRUE(test_server_.Start()); |
+ |
+ TestDelegate d; |
+ { |
+ d.set_quit_on_redirect(true); |
+ d.set_quit_on_network_start(true); |
+ URLRequest req(test_server_.GetURL("server-redirect?echo"), |
+ DEFAULT_PRIORITY, |
+ &d, |
+ &default_context_); |
+ |
+ req.Start(); |
+ base::RunLoop().Run(); |
+ |
+ EXPECT_EQ(1, d.received_before_network_start_count()); |
+ EXPECT_EQ(0, d.response_started_count()); |
+ EXPECT_EQ(0, d.received_redirect_count()); |
+ |
+ req.ResumeNetworkStart(); |
+ base::RunLoop().Run(); |
+ |
+ EXPECT_EQ(1, d.received_redirect_count()); |
+ req.FollowDeferredRedirect(); |
+ base::RunLoop().Run(); |
+ |
+ // Check that the redirect's new network transaction does not get propagated |
+ // to a second OnBeforeNetworkStart() notification. |
+ EXPECT_EQ(1, d.received_before_network_start_count()); |
+ |
+ EXPECT_EQ(1, d.response_started_count()); |
+ EXPECT_NE(0, d.bytes_received()); |
+ EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status()); |
+ } |
+} |
+ |
+// Cancel the request after learning that the request would use the network. |
+TEST_F(URLRequestTestHTTP, CancelOnBeforeNetworkStart) { |
+ ASSERT_TRUE(test_server_.Start()); |
+ |
+ TestDelegate d; |
+ { |
+ d.set_quit_on_network_start(true); |
+ GURL test_url(test_server_.GetURL("echo")); |
+ URLRequest req(test_url, DEFAULT_PRIORITY, &d, &default_context_); |
+ |
+ req.Start(); |
+ base::RunLoop().Run(); |
+ |
+ EXPECT_EQ(1, d.received_before_network_start_count()); |
+ EXPECT_EQ(0, d.response_started_count()); |
+ |
+ req.Cancel(); |
+ base::RunLoop().Run(); |
+ |
+ EXPECT_EQ(1, d.response_started_count()); |
+ EXPECT_EQ(0, d.bytes_received()); |
+ EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); |
+ } |
+} |
+ |
TEST_F(URLRequestTestHTTP, CancelRedirect) { |
ASSERT_TRUE(test_server_.Start()); |