Set network_accessed earlier, when network transaction creates stream.
HttpResponseInfo contains a "network_accessed" flag that is currently
set when HttpNetworkTransaction finishes sending the request. This is
not entirely accurate; it ends up false for requests that don't send a
request but do touch the network in other ways (e.g. DNS resolution
fails, or TCP connection is terminated before we finish sending the
request).
This flag is used only by Domain Reliability for checking whether a
request touched the network, and by ServiceWorker for histograms.
horo says ServiceWorker won't be affected, and I would prefer these
semantics for Domain Reliability's use, so I'm changing it.
Note that this also changes several instances of GetResponseInfo
to stop returning NULL.
See also for context:
https://groups.google.com/a/chromium.org/forum/#!topic/net-dev/Wtn65jGyot0
BUG=480565
Review URL: https://codereview.chromium.org/1072423005
Cr-Commit-Position: refs/heads/master@{#330011}
diff --git a/net/url_request/url_request_unittest.cc b/net/url_request/url_request_unittest.cc
index 20687e91..fd9a6504 100644
--- a/net/url_request/url_request_unittest.cc
+++ b/net/url_request/url_request_unittest.cc
@@ -6823,6 +6823,59 @@
EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, req->status().error());
}
+TEST_F(URLRequestTestHTTP, NetworkAccessedSetOnNetworkRequest) {
+ ASSERT_TRUE(test_server_.Start());
+
+ TestDelegate d;
+ GURL test_url(test_server_.GetURL(std::string()));
+ scoped_ptr<URLRequest> req(
+ default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d));
+
+ req->Start();
+ base::RunLoop().Run();
+
+ EXPECT_TRUE(req->response_info().network_accessed);
+}
+
+TEST_F(URLRequestTestHTTP, NetworkAccessedClearOnCachedResponse) {
+ ASSERT_TRUE(test_server_.Start());
+
+ // Populate the cache.
+ TestDelegate d;
+ scoped_ptr<URLRequest> req(default_context_.CreateRequest(
+ test_server_.GetURL("cachetime"), DEFAULT_PRIORITY, &d));
+ req->Start();
+ base::RunLoop().Run();
+
+ EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
+ EXPECT_TRUE(req->response_info().network_accessed);
+ EXPECT_FALSE(req->response_info().was_cached);
+
+ req = default_context_.CreateRequest(test_server_.GetURL("cachetime"),
+ DEFAULT_PRIORITY, &d);
+ req->Start();
+ base::RunLoop().Run();
+
+ EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
+ EXPECT_FALSE(req->response_info().network_accessed);
+ EXPECT_TRUE(req->response_info().was_cached);
+}
+
+TEST_F(URLRequestTestHTTP, NetworkAccessedClearOnLoadOnlyFromCache) {
+ ASSERT_TRUE(test_server_.Start());
+
+ TestDelegate d;
+ GURL test_url(test_server_.GetURL(std::string()));
+ scoped_ptr<URLRequest> req(
+ default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d));
+ req->SetLoadFlags(LOAD_ONLY_FROM_CACHE);
+
+ req->Start();
+ base::RunLoop().Run();
+
+ EXPECT_FALSE(req->response_info().network_accessed);
+}
+
class URLRequestInterceptorTestHTTP : public URLRequestTestHTTP {
public:
// TODO(bengr): Merge this with the URLRequestInterceptorHTTPTest fixture,
@@ -9089,4 +9142,57 @@
}
#endif // !defined(DISABLE_FTP_SUPPORT)
+TEST_F(URLRequestTest, NetworkAccessedClearBeforeNetworkStart) {
+ TestDelegate d;
+ scoped_ptr<URLRequest> req(default_context_.CreateRequest(
+ GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d));
+ d.set_quit_on_network_start(true);
+
+ EXPECT_FALSE(req->response_info().network_accessed);
+
+ req->Start();
+ base::RunLoop().Run();
+
+ EXPECT_EQ(1, d.received_before_network_start_count());
+ EXPECT_EQ(0, d.response_started_count());
+ EXPECT_FALSE(req->response_info().network_accessed);
+
+ req->ResumeNetworkStart();
+ base::RunLoop().Run();
+}
+
+TEST_F(URLRequestTest, NetworkAccessedClearOnDataRequest) {
+ TestDelegate d;
+ scoped_ptr<URLRequest> req(
+ default_context_.CreateRequest(GURL("data:,"), DEFAULT_PRIORITY, &d));
+
+ EXPECT_FALSE(req->response_info().network_accessed);
+
+ req->Start();
+ base::RunLoop().Run();
+
+ EXPECT_EQ(1, default_network_delegate_.completed_requests());
+ EXPECT_FALSE(req->response_info().network_accessed);
+}
+
+TEST_F(URLRequestTest, NetworkAccessedSetOnHostResolutionFailure) {
+ MockHostResolver host_resolver;
+ TestNetworkDelegate network_delegate; // Must outlive URLRequest.
+ TestURLRequestContext context(true);
+ context.set_network_delegate(&network_delegate);
+ context.set_host_resolver(&host_resolver);
+ host_resolver.rules()->AddSimulatedFailure("*");
+ context.Init();
+
+ TestDelegate d;
+ scoped_ptr<URLRequest> req(context.CreateRequest(
+ GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d));
+
+ EXPECT_FALSE(req->response_info().network_accessed);
+
+ req->Start();
+ base::RunLoop().Run();
+ EXPECT_TRUE(req->response_info().network_accessed);
+}
+
} // namespace net