Index: sync/internal_api/http_bridge_unittest.cc |
diff --git a/sync/internal_api/http_bridge_unittest.cc b/sync/internal_api/http_bridge_unittest.cc |
index 3c6db88be2ded0327d32ff9de36dbdc05dd17f0d..4d83462f78d5a453294cf3f1d48969c98d1b4ee0 100644 |
--- a/sync/internal_api/http_bridge_unittest.cc |
+++ b/sync/internal_api/http_bridge_unittest.cc |
@@ -2,8 +2,10 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
+#include "base/metrics/field_trial.h" |
#include "base/strings/stringprintf.h" |
#include "base/synchronization/waitable_event.h" |
+#include "base/test/mock_entropy_provider.h" |
#include "base/threading/thread.h" |
#include "net/http/http_response_headers.h" |
#include "net/test/spawned_test_server/spawned_test_server.h" |
@@ -24,6 +26,38 @@ const base::FilePath::CharType kDocRoot[] = |
const char kUserAgent[] = "user-agent"; |
+bool GzipUncompress(const char* input, int length, std::string* output) { |
Nicolas Zea
2015/07/28 00:27:19
Comment where this code comes from?
Gang Wu
2015/07/29 21:09:16
Done.
|
+ z_stream stream = {0}; |
+ |
+ stream.next_in = reinterpret_cast<uint8*>(const_cast<char*>(input)); |
+ stream.avail_in = length; |
+ stream.next_out = reinterpret_cast<uint8*>(&(*output)[0]); |
+ stream.avail_out = output->size(); |
+ |
+ bool success = false; |
+ while (stream.avail_in > 0 && stream.avail_out > 0) { |
+ // 16 is added to read in gzip format. |
+ int result = inflateInit2(&stream, MAX_WBITS + 16); |
+ DCHECK_EQ(Z_OK, result); |
+ |
+ result = inflate(&stream, Z_FINISH); |
+ success = (result == Z_STREAM_END); |
+ if (!success) { |
+ DVLOG(2) << "inflate() failed. Result: " << result; |
+ break; |
+ } |
+ |
+ result = inflateEnd(&stream); |
+ DCHECK(result == Z_OK); |
+ } |
+ |
+ if (stream.avail_in == 0) { |
+ success = true; |
+ output->resize(output->size() - stream.avail_out); |
+ } |
+ return success; |
+} |
+ |
class SyncHttpBridgeTest : public testing::Test { |
public: |
SyncHttpBridgeTest() |
@@ -146,9 +180,12 @@ class ShuntedHttpBridge : public HttpBridge { |
std::string response_content = "success!"; |
net::TestURLFetcher fetcher(0, GURL("http://www.google.com"), NULL); |
+ scoped_refptr<net::HttpResponseHeaders> response_headers( |
+ new net::HttpResponseHeaders("")); |
fetcher.set_response_code(200); |
fetcher.set_cookies(cookies); |
fetcher.SetResponseString(response_content); |
+ fetcher.set_response_headers(response_headers); |
OnURLFetchComplete(&fetcher); |
} |
SyncHttpBridgeTest* test_; |
@@ -232,6 +269,71 @@ TEST_F(SyncHttpBridgeTest, TestMakeSynchronousPostLiveWithPayload) { |
EXPECT_EQ(payload, std::string(http_bridge->GetResponseContent())); |
} |
+// Full round-trip test of the HttpBridge with compress data, using default |
Nicolas Zea
2015/07/28 00:27:19
nit: compress -> compressed
Gang Wu
2015/07/29 21:09:16
Done.
|
+// UA string and no request cookies. |
+TEST_F(SyncHttpBridgeTest, TestMakeSynchronousPostLiveWithCompressedPayload) { |
Nicolas Zea
2015/07/28 00:27:19
nit: I think it would make a bit more sense to ord
Gang Wu
2015/07/29 21:09:16
rename two tests to make it easier to understand,
|
+ ASSERT_TRUE(test_server_.Start()); |
+ |
+ scoped_refptr<HttpBridge> http_bridge(BuildBridge()); |
+ |
+ std::string payload = "this should be echoed back"; |
+ GURL echo = test_server_.GetURL("echo"); |
+ http_bridge->SetURL(echo.spec().c_str(), echo.IntPort()); |
+ http_bridge->SetPostPayload("application/x-www-form-urlencoded", |
+ payload.length() + 1, payload.c_str()); |
+ int os_error = 0; |
+ int response_code = 0; |
+ base::FieldTrialList field_trial_list(new base::MockEntropyProvider()); |
+ base::FieldTrialList::CreateFieldTrial("SyncHttpContentCompression", |
+ "Enabled"); |
+ bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); |
+ EXPECT_TRUE(success); |
+ EXPECT_EQ(200, response_code); |
+ EXPECT_EQ(0, os_error); |
+ |
+ EXPECT_NE(payload.length() + 1, |
+ static_cast<size_t>(http_bridge->GetResponseContentLength())); |
+ std::string uncompressed_payload(payload.size() * 10, ' '); |
Nicolas Zea
2015/07/28 00:27:19
Why initialize it to so many spaces?
Gang Wu
2015/07/29 21:09:16
fixed.
|
+ GzipUncompress(http_bridge->GetResponseContent(), |
+ http_bridge->GetResponseContentLength(), |
+ &uncompressed_payload); |
+ EXPECT_EQ(payload.length() + 1, uncompressed_payload.size()); |
Nicolas Zea
2015/07/28 00:27:19
This check is redundant given the one below.
Gang Wu
2015/07/29 21:09:16
Done.
|
+ EXPECT_EQ(payload + '\0', uncompressed_payload); |
Nicolas Zea
2015/07/28 00:27:19
Do all http responses include the null character a
Gang Wu
2015/07/29 21:09:16
it is because in lines above, sent payload by
http
|
+} |
+ |
+// Full round-trip test of the HttpBridge with compression. |
+TEST_F(SyncHttpBridgeTest, |
+ TestMakeSynchronousPostLiveComprehensiveWithCompression) { |
Nicolas Zea
2015/07/28 00:27:19
How is this test different from the one above? The
Gang Wu
2015/07/29 21:09:16
rename both test name.
|
+ ASSERT_TRUE(test_server_.Start()); |
+ |
+ scoped_refptr<HttpBridge> http_bridge(BuildBridge()); |
+ |
+ GURL echo_header = test_server_.GetURL("echoall"); |
+ http_bridge->SetURL(echo_header.spec().c_str(), echo_header.IntPort()); |
+ |
+ std::string test_payload = "###TEST PAYLOAD###"; |
+ http_bridge->SetPostPayload("text/html", test_payload.length() + 1, |
+ test_payload.c_str()); |
+ |
+ int os_error = 0; |
+ int response_code = 0; |
+ base::FieldTrialList field_trial_list(new base::MockEntropyProvider()); |
+ base::FieldTrialList::CreateFieldTrial("SyncHttpContentCompression", |
+ "Enabled"); |
+ bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); |
+ EXPECT_TRUE(success); |
+ EXPECT_EQ(200, response_code); |
+ EXPECT_EQ(0, os_error); |
+ |
+ std::string response(http_bridge->GetResponseContent(), |
+ http_bridge->GetResponseContentLength()); |
+ EXPECT_NE(std::string::npos, response.find("Content-Encoding: gzip")); |
+ EXPECT_NE(std::string::npos, response.find("Accept-Encoding: gzip, deflate")); |
+ EXPECT_NE(std::string::npos, |
+ response.find(base::StringPrintf( |
+ "%s: %s", net::HttpRequestHeaders::kUserAgent, kUserAgent))); |
+} |
+ |
// Full round-trip test of the HttpBridge. |
TEST_F(SyncHttpBridgeTest, TestMakeSynchronousPostLiveComprehensive) { |
ASSERT_TRUE(test_server_.Start()); |
@@ -255,6 +357,7 @@ TEST_F(SyncHttpBridgeTest, TestMakeSynchronousPostLiveComprehensive) { |
std::string response(http_bridge->GetResponseContent(), |
http_bridge->GetResponseContentLength()); |
EXPECT_EQ(std::string::npos, response.find("Cookie:")); |
+ EXPECT_NE(std::string::npos, response.find("Accept-Encoding: deflate")); |
EXPECT_NE(std::string::npos, |
response.find(base::StringPrintf("%s: %s", |
net::HttpRequestHeaders::kUserAgent, kUserAgent))); |