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

Unified Diff: net/http/http_network_transaction_unittest.cc

Issue 1327763003: Added and implemented URLRequest::GetTotalSentBytes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@spdy_sent_bytes_impl
Patch Set: Addressed nits Created 5 years, 3 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
« no previous file with comments | « net/http/http_network_transaction.cc ('k') | net/http/http_transaction.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/http/http_network_transaction_unittest.cc
diff --git a/net/http/http_network_transaction_unittest.cc b/net/http/http_network_transaction_unittest.cc
index 14bda39448d3534d25d6c6b5e742fb08a47b8f07..25af22fabeebddd8ece2587a2d6518192ed6179f 100644
--- a/net/http/http_network_transaction_unittest.cc
+++ b/net/http/http_network_transaction_unittest.cc
@@ -265,7 +265,8 @@ class HttpNetworkTransactionTest
int rv;
std::string status_line;
std::string response_data;
- int64 totalReceivedBytes;
+ int64_t total_received_bytes;
+ int64_t total_sent_bytes;
LoadTimingInfo load_timing_info;
ConnectionAttempts connection_attempts;
};
@@ -341,6 +342,8 @@ class HttpNetworkTransactionTest
EXPECT_EQ(ERR_IO_PENDING, rv);
out.rv = callback.WaitForResult();
+ out.total_received_bytes = trans->GetTotalReceivedBytes();
+ out.total_sent_bytes = trans->GetTotalSentBytes();
// Even in the failure cases that use this function, connections are always
// successfully established before the error.
@@ -392,23 +395,30 @@ class HttpNetworkTransactionTest
EXPECT_EQ("['Host: www.example.org','Connection: keep-alive']",
response_headers);
- out.totalReceivedBytes = trans->GetTotalReceivedBytes();
+ out.total_received_bytes = trans->GetTotalReceivedBytes();
+ // The total number of sent bytes should not have changed.
+ EXPECT_EQ(out.total_sent_bytes, trans->GetTotalSentBytes());
+
trans->GetConnectionAttempts(&out.connection_attempts);
return out;
}
SimpleGetHelperResult SimpleGetHelper(MockRead data_reads[],
size_t reads_count) {
- StaticSocketDataProvider reads(data_reads, reads_count, NULL, 0);
- StaticSocketDataProvider* data[] = { &reads };
- return SimpleGetHelperForData(data, 1);
- }
+ MockWrite data_writes[] = {
+ MockWrite("GET / HTTP/1.1\r\n"
+ "Host: www.example.org\r\n"
+ "Connection: keep-alive\r\n\r\n"),
+ };
+
+ StaticSocketDataProvider reads(data_reads, reads_count, data_writes,
+ arraysize(data_writes));
+ StaticSocketDataProvider* data[] = {&reads};
+ SimpleGetHelperResult out = SimpleGetHelperForData(data, 1);
- int64 ReadsSize(MockRead data_reads[], size_t reads_count) {
- int64 size = 0;
- for (size_t i = 0; i < reads_count; ++i)
- size += data_reads[i].data_len;
- return size;
+ EXPECT_EQ(CountWriteBytes(data_writes, arraysize(data_writes)),
+ out.total_sent_bytes);
+ return out;
}
void ConnectStatusHelperWithExpectedStatus(const MockRead& status,
@@ -679,8 +689,8 @@ TEST_P(HttpNetworkTransactionTest, SimpleGET) {
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.0 200 OK", out.status_line);
EXPECT_EQ("hello world", out.response_data);
- int64 reads_size = ReadsSize(data_reads, arraysize(data_reads));
- EXPECT_EQ(reads_size, out.totalReceivedBytes);
+ int64_t reads_size = CountReadBytes(data_reads, arraysize(data_reads));
+ EXPECT_EQ(reads_size, out.total_received_bytes);
EXPECT_EQ(0u, out.connection_attempts.size());
}
@@ -695,8 +705,8 @@ TEST_P(HttpNetworkTransactionTest, SimpleGETNoHeaders) {
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/0.9 200 OK", out.status_line);
EXPECT_EQ("hello world", out.response_data);
- int64 reads_size = ReadsSize(data_reads, arraysize(data_reads));
- EXPECT_EQ(reads_size, out.totalReceivedBytes);
+ int64_t reads_size = CountReadBytes(data_reads, arraysize(data_reads));
+ EXPECT_EQ(reads_size, out.total_received_bytes);
}
// Allow up to 4 bytes of junk to precede status line.
@@ -710,8 +720,8 @@ TEST_P(HttpNetworkTransactionTest, StatusLineJunk3Bytes) {
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.0 404 Not Found", out.status_line);
EXPECT_EQ("DATA", out.response_data);
- int64 reads_size = ReadsSize(data_reads, arraysize(data_reads));
- EXPECT_EQ(reads_size, out.totalReceivedBytes);
+ int64_t reads_size = CountReadBytes(data_reads, arraysize(data_reads));
+ EXPECT_EQ(reads_size, out.total_received_bytes);
}
// Allow up to 4 bytes of junk to precede status line.
@@ -725,8 +735,8 @@ TEST_P(HttpNetworkTransactionTest, StatusLineJunk4Bytes) {
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.0 404 Not Found", out.status_line);
EXPECT_EQ("DATA", out.response_data);
- int64 reads_size = ReadsSize(data_reads, arraysize(data_reads));
- EXPECT_EQ(reads_size, out.totalReceivedBytes);
+ int64_t reads_size = CountReadBytes(data_reads, arraysize(data_reads));
+ EXPECT_EQ(reads_size, out.total_received_bytes);
}
// Beyond 4 bytes of slop and it should fail to find a status line.
@@ -740,8 +750,8 @@ TEST_P(HttpNetworkTransactionTest, StatusLineJunk5Bytes) {
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/0.9 200 OK", out.status_line);
EXPECT_EQ("xxxxxHTTP/1.1 404 Not Found\nServer: blah", out.response_data);
- int64 reads_size = ReadsSize(data_reads, arraysize(data_reads));
- EXPECT_EQ(reads_size, out.totalReceivedBytes);
+ int64_t reads_size = CountReadBytes(data_reads, arraysize(data_reads));
+ EXPECT_EQ(reads_size, out.total_received_bytes);
}
// Same as StatusLineJunk4Bytes, except the read chunks are smaller.
@@ -759,8 +769,8 @@ TEST_P(HttpNetworkTransactionTest, StatusLineJunk4Bytes_Slow) {
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.0 404 Not Found", out.status_line);
EXPECT_EQ("DATA", out.response_data);
- int64 reads_size = ReadsSize(data_reads, arraysize(data_reads));
- EXPECT_EQ(reads_size, out.totalReceivedBytes);
+ int64_t reads_size = CountReadBytes(data_reads, arraysize(data_reads));
+ EXPECT_EQ(reads_size, out.total_received_bytes);
}
// Close the connection before enough bytes to have a status line.
@@ -774,8 +784,8 @@ TEST_P(HttpNetworkTransactionTest, StatusLinePartial) {
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/0.9 200 OK", out.status_line);
EXPECT_EQ("HTT", out.response_data);
- int64 reads_size = ReadsSize(data_reads, arraysize(data_reads));
- EXPECT_EQ(reads_size, out.totalReceivedBytes);
+ int64_t reads_size = CountReadBytes(data_reads, arraysize(data_reads));
+ EXPECT_EQ(reads_size, out.total_received_bytes);
}
// Simulate a 204 response, lacking a Content-Length header, sent over a
@@ -793,9 +803,9 @@ TEST_P(HttpNetworkTransactionTest, StopsReading204) {
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 204 No Content", out.status_line);
EXPECT_EQ("", out.response_data);
- int64 reads_size = ReadsSize(data_reads, arraysize(data_reads));
- int64 response_size = reads_size - strlen(junk);
- EXPECT_EQ(response_size, out.totalReceivedBytes);
+ int64_t reads_size = CountReadBytes(data_reads, arraysize(data_reads));
+ int64_t response_size = reads_size - strlen(junk);
+ EXPECT_EQ(response_size, out.total_received_bytes);
}
// A simple request using chunked encoding with some extra data after.
@@ -817,9 +827,9 @@ TEST_P(HttpNetworkTransactionTest, ChunkedEncoding) {
EXPECT_EQ(OK, out.rv);
EXPECT_EQ("HTTP/1.1 200 OK", out.status_line);
EXPECT_EQ("Hello world", out.response_data);
- int64 reads_size = ReadsSize(data_reads, arraysize(data_reads));
- int64 response_size = reads_size - extra_data.size();
- EXPECT_EQ(response_size, out.totalReceivedBytes);
+ int64_t reads_size = CountReadBytes(data_reads, arraysize(data_reads));
+ int64_t response_size = reads_size - extra_data.size();
+ EXPECT_EQ(response_size, out.total_received_bytes);
}
// Next tests deal with http://crbug.com/56344.
@@ -1955,7 +1965,9 @@ TEST_P(HttpNetworkTransactionTest, BasicAuth) {
EXPECT_TRUE(trans->GetLoadTimingInfo(&load_timing_info1));
TestLoadTimingNotReused(load_timing_info1, CONNECT_TIMING_HAS_DNS_TIMES);
- int64 reads_size1 = ReadsSize(data_reads1, arraysize(data_reads1));
+ int64_t writes_size1 = CountWriteBytes(data_writes1, arraysize(data_writes1));
+ EXPECT_EQ(writes_size1, trans->GetTotalSentBytes());
+ int64_t reads_size1 = CountReadBytes(data_reads1, arraysize(data_reads1));
EXPECT_EQ(reads_size1, trans->GetTotalReceivedBytes());
const HttpResponseInfo* response = trans->GetResponseInfo();
@@ -1980,7 +1992,9 @@ TEST_P(HttpNetworkTransactionTest, BasicAuth) {
load_timing_info2.connect_timing.connect_start);
EXPECT_NE(load_timing_info1.socket_log_id, load_timing_info2.socket_log_id);
- int64 reads_size2 = ReadsSize(data_reads2, arraysize(data_reads2));
+ int64_t writes_size2 = CountWriteBytes(data_writes2, arraysize(data_writes2));
+ EXPECT_EQ(writes_size1 + writes_size2, trans->GetTotalSentBytes());
+ int64_t reads_size2 = CountReadBytes(data_reads2, arraysize(data_reads2));
EXPECT_EQ(reads_size1 + reads_size2, trans->GetTotalReceivedBytes());
response = trans->GetResponseInfo();
@@ -2026,7 +2040,9 @@ TEST_P(HttpNetworkTransactionTest, DoNotSendAuth) {
rv = callback.WaitForResult();
EXPECT_EQ(0, rv);
- int64 reads_size = ReadsSize(data_reads, arraysize(data_reads));
+ int64_t writes_size = CountWriteBytes(data_writes, arraysize(data_writes));
+ EXPECT_EQ(writes_size, trans->GetTotalSentBytes());
+ int64_t reads_size = CountReadBytes(data_reads, arraysize(data_reads));
EXPECT_EQ(reads_size, trans->GetTotalReceivedBytes());
const HttpResponseInfo* response = trans->GetResponseInfo();
@@ -2132,7 +2148,10 @@ TEST_P(HttpNetworkTransactionTest, BasicAuthKeepAlive) {
std::string response_data;
rv = ReadTransaction(trans.get(), &response_data);
EXPECT_EQ(OK, rv);
- int64 reads_size1 = ReadsSize(data_reads1, arraysize(data_reads1));
+
+ int64_t writes_size1 = CountWriteBytes(data_writes1, arraysize(data_writes1));
+ EXPECT_EQ(writes_size1, trans->GetTotalSentBytes());
+ int64_t reads_size1 = CountReadBytes(data_reads1, arraysize(data_reads1));
EXPECT_EQ(reads_size1, trans->GetTotalReceivedBytes());
}
@@ -14584,4 +14603,145 @@ TEST_P(HttpNetworkTransactionTest, ProxyHeadersNotSentOverWsTunnel) {
session->CloseAllConnections();
}
+TEST_P(HttpNetworkTransactionTest, TotalNetworkBytesPost) {
+ ScopedVector<UploadElementReader> element_readers;
+ element_readers.push_back(new UploadBytesElementReader("foo", 3));
+ ElementsUploadDataStream upload_data_stream(element_readers.Pass(), 0);
+
+ HttpRequestInfo request;
+ request.method = "POST";
+ request.url = GURL("http://www.foo.com/");
+ request.upload_data_stream = &upload_data_stream;
+
+ scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_));
+ scoped_ptr<HttpTransaction> trans(
+ new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get()));
+ MockWrite data_writes[] = {
+ MockWrite("POST / HTTP/1.1\r\n"
+ "Host: www.foo.com\r\n"
+ "Connection: keep-alive\r\n"
+ "Content-Length: 3\r\n\r\n"),
+ MockWrite("foo"),
+ };
+
+ MockRead data_reads[] = {
+ MockRead("HTTP/1.1 200 OK\r\n\r\n"), MockRead("hello world"),
+ MockRead(SYNCHRONOUS, OK),
+ };
+ StaticSocketDataProvider data(data_reads, arraysize(data_reads), data_writes,
+ arraysize(data_writes));
+ session_deps_.socket_factory->AddSocketDataProvider(&data);
+
+ TestCompletionCallback callback;
+
+ EXPECT_EQ(ERR_IO_PENDING,
+ trans->Start(&request, callback.callback(), BoundNetLog()));
+ EXPECT_EQ(OK, callback.WaitForResult());
+
+ std::string response_data;
+ EXPECT_EQ(OK, ReadTransaction(trans.get(), &response_data));
+
+ EXPECT_EQ(CountWriteBytes(data_writes, arraysize(data_writes)),
+ trans->GetTotalSentBytes());
+ EXPECT_EQ(CountReadBytes(data_reads, arraysize(data_reads)),
+ trans->GetTotalReceivedBytes());
+}
+
+TEST_P(HttpNetworkTransactionTest, TotalNetworkBytesPost100Continue) {
+ ScopedVector<UploadElementReader> element_readers;
+ element_readers.push_back(new UploadBytesElementReader("foo", 3));
+ ElementsUploadDataStream upload_data_stream(element_readers.Pass(), 0);
+
+ HttpRequestInfo request;
+ request.method = "POST";
+ request.url = GURL("http://www.foo.com/");
+ request.upload_data_stream = &upload_data_stream;
+
+ scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_));
+ scoped_ptr<HttpTransaction> trans(
+ new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get()));
+ MockWrite data_writes[] = {
+ MockWrite("POST / HTTP/1.1\r\n"
+ "Host: www.foo.com\r\n"
+ "Connection: keep-alive\r\n"
+ "Content-Length: 3\r\n\r\n"),
+ MockWrite("foo"),
+ };
+
+ MockRead data_reads[] = {
+ MockRead("HTTP/1.1 100 Continue\r\n\r\n"),
+ MockRead("HTTP/1.1 200 OK\r\n\r\n"), MockRead("hello world"),
+ MockRead(SYNCHRONOUS, OK),
+ };
+ StaticSocketDataProvider data(data_reads, arraysize(data_reads), data_writes,
+ arraysize(data_writes));
+ session_deps_.socket_factory->AddSocketDataProvider(&data);
+
+ TestCompletionCallback callback;
+
+ EXPECT_EQ(ERR_IO_PENDING,
+ trans->Start(&request, callback.callback(), BoundNetLog()));
+ EXPECT_EQ(OK, callback.WaitForResult());
+
+ std::string response_data;
+ EXPECT_EQ(OK, ReadTransaction(trans.get(), &response_data));
+
+ EXPECT_EQ(CountWriteBytes(data_writes, arraysize(data_writes)),
+ trans->GetTotalSentBytes());
+ EXPECT_EQ(CountReadBytes(data_reads, arraysize(data_reads)),
+ trans->GetTotalReceivedBytes());
+}
+
+TEST_P(HttpNetworkTransactionTest, TotalNetworkBytesChunkedPost) {
+ ScopedVector<UploadElementReader> element_readers;
+ element_readers.push_back(new UploadBytesElementReader("foo", 3));
+ ChunkedUploadDataStream upload_data_stream(0);
+
+ HttpRequestInfo request;
+ request.method = "POST";
+ request.url = GURL("http://www.foo.com/");
+ request.upload_data_stream = &upload_data_stream;
+
+ scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_));
+ scoped_ptr<HttpTransaction> trans(
+ new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get()));
+ // Send headers successfully, but get an error while sending the body.
+ MockWrite data_writes[] = {
+ MockWrite("POST / HTTP/1.1\r\n"
+ "Host: www.foo.com\r\n"
+ "Connection: keep-alive\r\n"
+ "Transfer-Encoding: chunked\r\n\r\n"),
+ MockWrite("1\r\nf\r\n"), MockWrite("2\r\noo\r\n"), MockWrite("0\r\n\r\n"),
+ };
+
+ MockRead data_reads[] = {
+ MockRead("HTTP/1.1 200 OK\r\n\r\n"), MockRead("hello world"),
+ MockRead(SYNCHRONOUS, OK),
+ };
+ StaticSocketDataProvider data(data_reads, arraysize(data_reads), data_writes,
+ arraysize(data_writes));
+ session_deps_.socket_factory->AddSocketDataProvider(&data);
+
+ TestCompletionCallback callback;
+
+ EXPECT_EQ(ERR_IO_PENDING,
+ trans->Start(&request, callback.callback(), BoundNetLog()));
+
+ base::RunLoop().RunUntilIdle();
+ upload_data_stream.AppendData("f", 1, false);
+
+ base::RunLoop().RunUntilIdle();
+ upload_data_stream.AppendData("oo", 2, true);
+
+ EXPECT_EQ(OK, callback.WaitForResult());
+
+ std::string response_data;
+ EXPECT_EQ(OK, ReadTransaction(trans.get(), &response_data));
+
+ EXPECT_EQ(CountWriteBytes(data_writes, arraysize(data_writes)),
+ trans->GetTotalSentBytes());
+ EXPECT_EQ(CountReadBytes(data_reads, arraysize(data_reads)),
+ trans->GetTotalReceivedBytes());
+}
+
} // namespace net
« no previous file with comments | « net/http/http_network_transaction.cc ('k') | net/http/http_transaction.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698