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

Side by Side Diff: components/sync/engine/net/http_bridge_unittest.cc

Issue 2466663002: [Sync] Add tests for compression from client to server (Closed)
Patch Set: add dependency Created 4 years, 1 month 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 unified diff | Download patch
« no previous file with comments | « components/sync/BUILD.gn ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/sync/engine/net/http_bridge.h" 5 #include "components/sync/engine/net/http_bridge.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include "base/bit_cast.h" 9 #include "base/bit_cast.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
11 #include "base/single_thread_task_runner.h" 11 #include "base/single_thread_task_runner.h"
12 #include "base/strings/stringprintf.h" 12 #include "base/strings/stringprintf.h"
13 #include "base/test/scoped_feature_list.h"
13 #include "base/threading/thread.h" 14 #include "base/threading/thread.h"
14 #include "build/build_config.h" 15 #include "build/build_config.h"
15 #include "components/sync/base/cancelation_signal.h" 16 #include "components/sync/base/cancelation_signal.h"
16 #include "net/http/http_response_headers.h" 17 #include "net/http/http_response_headers.h"
17 #include "net/test/embedded_test_server/embedded_test_server.h" 18 #include "net/test/embedded_test_server/embedded_test_server.h"
18 #include "net/url_request/test_url_fetcher_factory.h" 19 #include "net/url_request/test_url_fetcher_factory.h"
19 #include "net/url_request/url_request_test_util.h" 20 #include "net/url_request/url_request_test_util.h"
20 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "third_party/zlib/google/compression_utils.h"
21 23
22 namespace syncer { 24 namespace syncer {
23 25
24 namespace { 26 namespace {
25 27
26 // TODO(timsteele): Should use PathService here. See Chromium Issue 3113. 28 // TODO(timsteele): Should use PathService here. See Chromium Issue 3113.
27 const base::FilePath::CharType kDocRoot[] = 29 const base::FilePath::CharType kDocRoot[] =
28 FILE_PATH_LITERAL("chrome/test/data"); 30 FILE_PATH_LITERAL("chrome/test/data");
29 31
30 } // namespace 32 } // namespace
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); 243 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code);
242 EXPECT_TRUE(success); 244 EXPECT_TRUE(success);
243 EXPECT_EQ(200, response_code); 245 EXPECT_EQ(200, response_code);
244 EXPECT_EQ(0, os_error); 246 EXPECT_EQ(0, os_error);
245 247
246 EXPECT_EQ(payload.length() + 1, 248 EXPECT_EQ(payload.length() + 1,
247 static_cast<size_t>(http_bridge->GetResponseContentLength())); 249 static_cast<size_t>(http_bridge->GetResponseContentLength()));
248 EXPECT_EQ(payload, std::string(http_bridge->GetResponseContent())); 250 EXPECT_EQ(payload, std::string(http_bridge->GetResponseContent()));
249 } 251 }
250 252
253 // Full round-trip test of the HttpBridge with compressed data, check if the
254 // data is correctly compressed.
255 TEST_F(MAYBE_SyncHttpBridgeTest, CompressedRequestPayloadCheck) {
256 base::test::ScopedFeatureList scoped_feature_list;
257 scoped_feature_list.InitAndEnableFeature(kSyncClientToServerCompression);
258
259 ASSERT_TRUE(test_server_.Start());
260
261 scoped_refptr<HttpBridge> http_bridge(BuildBridge());
262
263 std::string payload = "this should be echoed back";
264 GURL echo = test_server_.GetURL("/echo");
265 http_bridge->SetURL(echo.spec().c_str(), echo.IntPort());
266 http_bridge->SetPostPayload("application/x-www-form-urlencoded",
267 payload.length(), payload.c_str());
268 int os_error = 0;
269 int response_code = 0;
270 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code);
271 EXPECT_TRUE(success);
272 EXPECT_EQ(200, response_code);
273 EXPECT_EQ(0, os_error);
274
275 EXPECT_NE(payload.length() + 1,
276 static_cast<size_t>(http_bridge->GetResponseContentLength()));
277 std::string compressed_payload(http_bridge->GetResponseContent(),
278 http_bridge->GetResponseContentLength());
279 std::string uncompressed_payload;
280 compression::GzipUncompress(compressed_payload, &uncompressed_payload);
281 EXPECT_EQ(payload, uncompressed_payload);
282 }
283
251 // Full round-trip test of the HttpBridge with compression, check if header 284 // Full round-trip test of the HttpBridge with compression, check if header
252 // fields("Content-Encoding" ,"Accept-Encoding" and user agent) are set 285 // fields("Content-Encoding" ,"Accept-Encoding" and user agent) are set
253 // correctly. 286 // correctly.
254 TEST_F(MAYBE_SyncHttpBridgeTest, CompressedRequestHeaderCheck) { 287 TEST_F(MAYBE_SyncHttpBridgeTest, CompressedRequestHeaderCheck) {
288 base::test::ScopedFeatureList scoped_feature_list;
289 scoped_feature_list.InitAndEnableFeature(kSyncClientToServerCompression);
290
255 ASSERT_TRUE(test_server_.Start()); 291 ASSERT_TRUE(test_server_.Start());
256 292
257 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); 293 scoped_refptr<HttpBridge> http_bridge(BuildBridge());
258 294
259 GURL echo_header = test_server_.GetURL("/echoall"); 295 GURL echo_header = test_server_.GetURL("/echoall");
260 http_bridge->SetURL(echo_header.spec().c_str(), echo_header.IntPort()); 296 http_bridge->SetURL(echo_header.spec().c_str(), echo_header.IntPort());
261 297
262 std::string test_payload = "###TEST PAYLOAD###"; 298 std::string test_payload = "###TEST PAYLOAD###";
263 http_bridge->SetPostPayload("text/html", test_payload.length() + 1, 299 http_bridge->SetPostPayload("text/html", test_payload.length() + 1,
264 test_payload.c_str()); 300 test_payload.c_str());
265 301
266 int os_error = 0; 302 int os_error = 0;
267 int response_code = 0; 303 int response_code = 0;
268 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); 304 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code);
269 EXPECT_TRUE(success); 305 EXPECT_TRUE(success);
270 EXPECT_EQ(200, response_code); 306 EXPECT_EQ(200, response_code);
271 EXPECT_EQ(0, os_error); 307 EXPECT_EQ(0, os_error);
272 308
273 std::string response(http_bridge->GetResponseContent(), 309 std::string response(http_bridge->GetResponseContent(),
274 http_bridge->GetResponseContentLength()); 310 http_bridge->GetResponseContentLength());
311 EXPECT_NE(std::string::npos, response.find("Content-Encoding: gzip"));
275 EXPECT_NE(std::string::npos, 312 EXPECT_NE(std::string::npos,
276 response.find(base::StringPrintf( 313 response.find(base::StringPrintf(
277 "%s: %s", net::HttpRequestHeaders::kAcceptEncoding, 314 "%s: %s", net::HttpRequestHeaders::kAcceptEncoding,
278 "gzip, deflate"))); 315 "gzip, deflate")));
279 EXPECT_NE(std::string::npos, 316 EXPECT_NE(std::string::npos,
280 response.find(base::StringPrintf( 317 response.find(base::StringPrintf(
281 "%s: %s", net::HttpRequestHeaders::kUserAgent, kUserAgent))); 318 "%s: %s", net::HttpRequestHeaders::kUserAgent, kUserAgent)));
282 } 319 }
283 320
284 // Full round-trip test of the HttpBridge. 321 // Full round-trip test of the HttpBridge.
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after
569 606
570 // Sync thread: Finally run the posted task, only to find that our 607 // Sync thread: Finally run the posted task, only to find that our
571 // HttpBridgeFactory has been neutered. Should not crash. 608 // HttpBridgeFactory has been neutered. Should not crash.
572 factory->Init("TestUserAgent", BindToTrackerCallback()); 609 factory->Init("TestUserAgent", BindToTrackerCallback());
573 610
574 // At this point, attempting to use the factory would trigger a crash. Both 611 // At this point, attempting to use the factory would trigger a crash. Both
575 // this test and the real world code should make sure this never happens. 612 // this test and the real world code should make sure this never happens.
576 } 613 }
577 614
578 } // namespace syncer 615 } // namespace syncer
OLDNEW
« no previous file with comments | « components/sync/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698