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

Unified 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 side-by-side diff with in-line comments
Download patch
« net/url_request/url_request.h ('K') | « net/url_request/url_request_test_util.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..c4cc1e11aa36b982e0d2f57e8ece54d8759ff410 100644
--- a/net/url_request/url_request_unittest.cc
+++ b/net/url_request/url_request_unittest.cc
@@ -3827,6 +3827,20 @@ class AsyncDelegateLogger : public base::RefCounted<AsyncDelegateLogger> {
return log_position + 1;
}
+ 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.
+ 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 +4155,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::ExpectBeforeNetworkStartBeginAndEnd(
+ entries, log_position + 1);
+ }
}
EXPECT_FALSE(LogContainsEntryWithTypeAfter(
@@ -4196,6 +4215,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::ExpectBeforeNetworkStartBeginAndEnd(
+ entries, log_position + 1);
+ }
}
// The URLRequest::Delegate then gets informed about the redirect.
@@ -4275,6 +4299,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::ExpectBeforeNetworkStartBeginAndEnd(
+ entries, log_position + 1);
+ }
}
EXPECT_FALSE(LogContainsEntryWithTypeAfter(
@@ -4296,7 +4325,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 +4342,18 @@ TEST_F(URLRequestTestHTTP, URLRequestDelegateInfo) {
CapturingNetLog::CapturedEntryList entries;
net_log_.GetEntries(&entries);
+ size_t log_position = 0;
+
+ log_position = AsyncDelegateLogger::ExpectBeforeNetworkStartBeginAndEnd(
+ 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 +4400,11 @@ TEST_F(URLRequestTestHTTP, URLRequestDelegateInfoOnRedirect) {
// OnResponseStarted.
size_t log_position = 0;
for (int i = 0; i < 2; ++i) {
+ if (i == 0) {
+ log_position = AsyncDelegateLogger::ExpectBeforeNetworkStartBeginAndEnd(
+ entries, log_position) + 1;
+ }
+
log_position = ExpectLogContainsSomewhereAfter(
entries,
log_position,
@@ -4427,6 +4465,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::ExpectBeforeNetworkStartBeginAndEnd(
+ entries, log_position) + 1;
+ }
+
log_position = ExpectLogContainsSomewhereAfter(
entries,
log_position,
@@ -5184,6 +5227,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?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.
+ 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());
« 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