| OLD | NEW |
| 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 "base/metrics/field_trial.h" | 5 #include "base/metrics/field_trial.h" |
| 6 #include "base/strings/stringprintf.h" | 6 #include "base/strings/stringprintf.h" |
| 7 #include "base/synchronization/waitable_event.h" | 7 #include "base/synchronization/waitable_event.h" |
| 8 #include "base/test/mock_entropy_provider.h" | 8 #include "base/test/mock_entropy_provider.h" |
| 9 #include "base/threading/thread.h" | 9 #include "base/threading/thread.h" |
| 10 #include "net/http/http_response_headers.h" | 10 #include "net/http/http_response_headers.h" |
| 11 #include "net/test/spawned_test_server/spawned_test_server.h" | 11 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 12 #include "net/url_request/test_url_fetcher_factory.h" | 12 #include "net/url_request/test_url_fetcher_factory.h" |
| 13 #include "net/url_request/url_fetcher_delegate.h" | 13 #include "net/url_request/url_fetcher_delegate.h" |
| 14 #include "net/url_request/url_request_test_util.h" | 14 #include "net/url_request/url_request_test_util.h" |
| 15 #include "sync/internal_api/public/base/cancelation_signal.h" | 15 #include "sync/internal_api/public/base/cancelation_signal.h" |
| 16 #include "sync/internal_api/public/http_bridge.h" | 16 #include "sync/internal_api/public/http_bridge.h" |
| 17 #include "sync/internal_api/public/http_post_provider_factory.h" | 17 #include "sync/internal_api/public/http_post_provider_factory.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "third_party/zlib/zlib.h" | 19 #include "third_party/zlib/zlib.h" |
| 20 | 20 |
| 21 namespace syncer { | 21 namespace syncer { |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 const char kUserAgent[] = "user-agent"; | 108 const char kUserAgent[] = "user-agent"; |
| 109 | 109 |
| 110 #if defined(OS_ANDROID) | 110 #if defined(OS_ANDROID) |
| 111 #define MAYBE_SyncHttpBridgeTest DISABLED_SyncHttpBridgeTest | 111 #define MAYBE_SyncHttpBridgeTest DISABLED_SyncHttpBridgeTest |
| 112 #else | 112 #else |
| 113 #define MAYBE_SyncHttpBridgeTest SyncHttpBridgeTest | 113 #define MAYBE_SyncHttpBridgeTest SyncHttpBridgeTest |
| 114 #endif // defined(OS_ANDROID) | 114 #endif // defined(OS_ANDROID) |
| 115 class MAYBE_SyncHttpBridgeTest : public testing::Test { | 115 class MAYBE_SyncHttpBridgeTest : public testing::Test { |
| 116 public: | 116 public: |
| 117 MAYBE_SyncHttpBridgeTest() | 117 MAYBE_SyncHttpBridgeTest() |
| 118 : test_server_(net::SpawnedTestServer::TYPE_HTTP, | 118 : fake_default_request_context_getter_(NULL), |
| 119 net::SpawnedTestServer::kLocalhost, | |
| 120 base::FilePath(kDocRoot)), | |
| 121 fake_default_request_context_getter_(NULL), | |
| 122 bridge_for_race_test_(NULL), | 119 bridge_for_race_test_(NULL), |
| 123 io_thread_("IO thread") { | 120 io_thread_("IO thread") { |
| 121 test_server_.AddDefaultHandlers(base::FilePath(kDocRoot)); |
| 124 } | 122 } |
| 125 | 123 |
| 126 void SetUp() override { | 124 void SetUp() override { |
| 127 base::Thread::Options options; | 125 base::Thread::Options options; |
| 128 options.message_loop_type = base::MessageLoop::TYPE_IO; | 126 options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 129 io_thread_.StartWithOptions(options); | 127 io_thread_.StartWithOptions(options); |
| 130 } | 128 } |
| 131 | 129 |
| 132 void TearDown() override { | 130 void TearDown() override { |
| 133 if (fake_default_request_context_getter_) { | 131 if (fake_default_request_context_getter_) { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 base::MessageLoop::QuitWhenIdleClosure()); | 172 base::MessageLoop::QuitWhenIdleClosure()); |
| 175 } | 173 } |
| 176 | 174 |
| 177 base::MessageLoop* GetIOThreadLoop() { return io_thread_.message_loop(); } | 175 base::MessageLoop* GetIOThreadLoop() { return io_thread_.message_loop(); } |
| 178 | 176 |
| 179 // Note this is lazy created, so don't call this before your bridge. | 177 // Note this is lazy created, so don't call this before your bridge. |
| 180 net::TestURLRequestContextGetter* GetTestRequestContextGetter() { | 178 net::TestURLRequestContextGetter* GetTestRequestContextGetter() { |
| 181 return fake_default_request_context_getter_; | 179 return fake_default_request_context_getter_; |
| 182 } | 180 } |
| 183 | 181 |
| 184 net::SpawnedTestServer test_server_; | 182 net::EmbeddedTestServer test_server_; |
| 185 | 183 |
| 186 base::Thread* io_thread() { return &io_thread_; } | 184 base::Thread* io_thread() { return &io_thread_; } |
| 187 | 185 |
| 188 HttpBridge* bridge_for_race_test() { return bridge_for_race_test_; } | 186 HttpBridge* bridge_for_race_test() { return bridge_for_race_test_; } |
| 189 | 187 |
| 190 private: | 188 private: |
| 191 // A make-believe "default" request context, as would be returned by | 189 // A make-believe "default" request context, as would be returned by |
| 192 // Profile::GetDefaultRequestContext(). Created lazily by BuildBridge. | 190 // Profile::GetDefaultRequestContext(). Created lazily by BuildBridge. |
| 193 net::TestURLRequestContextGetter* fake_default_request_context_getter_; | 191 net::TestURLRequestContextGetter* fake_default_request_context_getter_; |
| 194 | 192 |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 } | 301 } |
| 304 | 302 |
| 305 // Full round-trip test of the HttpBridge, using default UA string and | 303 // Full round-trip test of the HttpBridge, using default UA string and |
| 306 // no request cookies. | 304 // no request cookies. |
| 307 TEST_F(MAYBE_SyncHttpBridgeTest, TestMakeSynchronousPostLiveWithPayload) { | 305 TEST_F(MAYBE_SyncHttpBridgeTest, TestMakeSynchronousPostLiveWithPayload) { |
| 308 ASSERT_TRUE(test_server_.Start()); | 306 ASSERT_TRUE(test_server_.Start()); |
| 309 | 307 |
| 310 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); | 308 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); |
| 311 | 309 |
| 312 std::string payload = "this should be echoed back"; | 310 std::string payload = "this should be echoed back"; |
| 313 GURL echo = test_server_.GetURL("echo"); | 311 GURL echo = test_server_.GetURL("/echo"); |
| 314 http_bridge->SetURL(echo.spec().c_str(), echo.IntPort()); | 312 http_bridge->SetURL(echo.spec().c_str(), echo.IntPort()); |
| 315 http_bridge->SetPostPayload("application/x-www-form-urlencoded", | 313 http_bridge->SetPostPayload("application/x-www-form-urlencoded", |
| 316 payload.length() + 1, payload.c_str()); | 314 payload.length() + 1, payload.c_str()); |
| 317 int os_error = 0; | 315 int os_error = 0; |
| 318 int response_code = 0; | 316 int response_code = 0; |
| 319 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); | 317 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); |
| 320 EXPECT_TRUE(success); | 318 EXPECT_TRUE(success); |
| 321 EXPECT_EQ(200, response_code); | 319 EXPECT_EQ(200, response_code); |
| 322 EXPECT_EQ(0, os_error); | 320 EXPECT_EQ(0, os_error); |
| 323 | 321 |
| 324 EXPECT_EQ(payload.length() + 1, | 322 EXPECT_EQ(payload.length() + 1, |
| 325 static_cast<size_t>(http_bridge->GetResponseContentLength())); | 323 static_cast<size_t>(http_bridge->GetResponseContentLength())); |
| 326 EXPECT_EQ(payload, std::string(http_bridge->GetResponseContent())); | 324 EXPECT_EQ(payload, std::string(http_bridge->GetResponseContent())); |
| 327 } | 325 } |
| 328 | 326 |
| 329 // Full round-trip test of the HttpBridge with compressed data, check if the | 327 // Full round-trip test of the HttpBridge with compressed data, check if the |
| 330 // data is correctly compressed. | 328 // data is correctly compressed. |
| 331 TEST_F(MAYBE_SyncHttpBridgeTest, CompressedRequestPayloadCheck) { | 329 TEST_F(MAYBE_SyncHttpBridgeTest, CompressedRequestPayloadCheck) { |
| 332 ASSERT_TRUE(test_server_.Start()); | 330 ASSERT_TRUE(test_server_.Start()); |
| 333 | 331 |
| 334 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); | 332 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); |
| 335 | 333 |
| 336 std::string payload = "this should be echoed back"; | 334 std::string payload = "this should be echoed back"; |
| 337 GURL echo = test_server_.GetURL("echo"); | 335 GURL echo = test_server_.GetURL("/echo"); |
| 338 http_bridge->SetURL(echo.spec().c_str(), echo.IntPort()); | 336 http_bridge->SetURL(echo.spec().c_str(), echo.IntPort()); |
| 339 http_bridge->SetPostPayload("application/x-www-form-urlencoded", | 337 http_bridge->SetPostPayload("application/x-www-form-urlencoded", |
| 340 payload.length(), payload.c_str()); | 338 payload.length(), payload.c_str()); |
| 341 int os_error = 0; | 339 int os_error = 0; |
| 342 int response_code = 0; | 340 int response_code = 0; |
| 343 base::FieldTrialList field_trial_list(new base::MockEntropyProvider()); | 341 base::FieldTrialList field_trial_list(new base::MockEntropyProvider()); |
| 344 base::FieldTrialList::CreateFieldTrial("SyncHttpContentCompression", | 342 base::FieldTrialList::CreateFieldTrial("SyncHttpContentCompression", |
| 345 "Enabled"); | 343 "Enabled"); |
| 346 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); | 344 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); |
| 347 EXPECT_TRUE(success); | 345 EXPECT_TRUE(success); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 358 } | 356 } |
| 359 | 357 |
| 360 // Full round-trip test of the HttpBridge with compression, check if header | 358 // Full round-trip test of the HttpBridge with compression, check if header |
| 361 // fields("Content-Encoding" ,"Accept-Encoding" and user agent) are set | 359 // fields("Content-Encoding" ,"Accept-Encoding" and user agent) are set |
| 362 // correctly. | 360 // correctly. |
| 363 TEST_F(MAYBE_SyncHttpBridgeTest, CompressedRequestHeaderCheck) { | 361 TEST_F(MAYBE_SyncHttpBridgeTest, CompressedRequestHeaderCheck) { |
| 364 ASSERT_TRUE(test_server_.Start()); | 362 ASSERT_TRUE(test_server_.Start()); |
| 365 | 363 |
| 366 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); | 364 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); |
| 367 | 365 |
| 368 GURL echo_header = test_server_.GetURL("echoall"); | 366 GURL echo_header = test_server_.GetURL("/echoall"); |
| 369 http_bridge->SetURL(echo_header.spec().c_str(), echo_header.IntPort()); | 367 http_bridge->SetURL(echo_header.spec().c_str(), echo_header.IntPort()); |
| 370 | 368 |
| 371 std::string test_payload = "###TEST PAYLOAD###"; | 369 std::string test_payload = "###TEST PAYLOAD###"; |
| 372 http_bridge->SetPostPayload("text/html", test_payload.length() + 1, | 370 http_bridge->SetPostPayload("text/html", test_payload.length() + 1, |
| 373 test_payload.c_str()); | 371 test_payload.c_str()); |
| 374 | 372 |
| 375 int os_error = 0; | 373 int os_error = 0; |
| 376 int response_code = 0; | 374 int response_code = 0; |
| 377 base::FieldTrialList field_trial_list(new base::MockEntropyProvider()); | 375 base::FieldTrialList field_trial_list(new base::MockEntropyProvider()); |
| 378 base::FieldTrialList::CreateFieldTrial("SyncHttpContentCompression", | 376 base::FieldTrialList::CreateFieldTrial("SyncHttpContentCompression", |
| (...skipping 14 matching lines...) Expand all Loading... |
| 393 response.find(base::StringPrintf( | 391 response.find(base::StringPrintf( |
| 394 "%s: %s", net::HttpRequestHeaders::kUserAgent, kUserAgent))); | 392 "%s: %s", net::HttpRequestHeaders::kUserAgent, kUserAgent))); |
| 395 } | 393 } |
| 396 | 394 |
| 397 // Full round-trip test of the HttpBridge. | 395 // Full round-trip test of the HttpBridge. |
| 398 TEST_F(MAYBE_SyncHttpBridgeTest, TestMakeSynchronousPostLiveComprehensive) { | 396 TEST_F(MAYBE_SyncHttpBridgeTest, TestMakeSynchronousPostLiveComprehensive) { |
| 399 ASSERT_TRUE(test_server_.Start()); | 397 ASSERT_TRUE(test_server_.Start()); |
| 400 | 398 |
| 401 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); | 399 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); |
| 402 | 400 |
| 403 GURL echo_header = test_server_.GetURL("echoall"); | 401 GURL echo_header = test_server_.GetURL("/echoall"); |
| 404 http_bridge->SetURL(echo_header.spec().c_str(), echo_header.IntPort()); | 402 http_bridge->SetURL(echo_header.spec().c_str(), echo_header.IntPort()); |
| 405 | 403 |
| 406 std::string test_payload = "###TEST PAYLOAD###"; | 404 std::string test_payload = "###TEST PAYLOAD###"; |
| 407 http_bridge->SetPostPayload("text/html", test_payload.length() + 1, | 405 http_bridge->SetPostPayload("text/html", test_payload.length() + 1, |
| 408 test_payload.c_str()); | 406 test_payload.c_str()); |
| 409 | 407 |
| 410 int os_error = 0; | 408 int os_error = 0; |
| 411 int response_code = 0; | 409 int response_code = 0; |
| 412 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); | 410 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); |
| 413 EXPECT_TRUE(success); | 411 EXPECT_TRUE(success); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 425 response.find(base::StringPrintf("%s: %s", | 423 response.find(base::StringPrintf("%s: %s", |
| 426 net::HttpRequestHeaders::kUserAgent, kUserAgent))); | 424 net::HttpRequestHeaders::kUserAgent, kUserAgent))); |
| 427 EXPECT_NE(std::string::npos, response.find(test_payload.c_str())); | 425 EXPECT_NE(std::string::npos, response.find(test_payload.c_str())); |
| 428 } | 426 } |
| 429 | 427 |
| 430 TEST_F(MAYBE_SyncHttpBridgeTest, TestExtraRequestHeaders) { | 428 TEST_F(MAYBE_SyncHttpBridgeTest, TestExtraRequestHeaders) { |
| 431 ASSERT_TRUE(test_server_.Start()); | 429 ASSERT_TRUE(test_server_.Start()); |
| 432 | 430 |
| 433 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); | 431 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); |
| 434 | 432 |
| 435 GURL echo_header = test_server_.GetURL("echoall"); | 433 GURL echo_header = test_server_.GetURL("/echoall"); |
| 436 | 434 |
| 437 http_bridge->SetURL(echo_header.spec().c_str(), echo_header.IntPort()); | 435 http_bridge->SetURL(echo_header.spec().c_str(), echo_header.IntPort()); |
| 438 http_bridge->SetExtraRequestHeaders("test:fnord"); | 436 http_bridge->SetExtraRequestHeaders("test:fnord"); |
| 439 | 437 |
| 440 std::string test_payload = "###TEST PAYLOAD###"; | 438 std::string test_payload = "###TEST PAYLOAD###"; |
| 441 http_bridge->SetPostPayload("text/html", test_payload.length() + 1, | 439 http_bridge->SetPostPayload("text/html", test_payload.length() + 1, |
| 442 test_payload.c_str()); | 440 test_payload.c_str()); |
| 443 | 441 |
| 444 int os_error = 0; | 442 int os_error = 0; |
| 445 int response_code = 0; | 443 int response_code = 0; |
| 446 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); | 444 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); |
| 447 EXPECT_TRUE(success); | 445 EXPECT_TRUE(success); |
| 448 EXPECT_EQ(200, response_code); | 446 EXPECT_EQ(200, response_code); |
| 449 EXPECT_EQ(0, os_error); | 447 EXPECT_EQ(0, os_error); |
| 450 | 448 |
| 451 std::string response(http_bridge->GetResponseContent(), | 449 std::string response(http_bridge->GetResponseContent(), |
| 452 http_bridge->GetResponseContentLength()); | 450 http_bridge->GetResponseContentLength()); |
| 453 | 451 |
| 454 EXPECT_NE(std::string::npos, response.find("fnord")); | 452 EXPECT_NE(std::string::npos, response.find("fnord")); |
| 455 EXPECT_NE(std::string::npos, response.find(test_payload.c_str())); | 453 EXPECT_NE(std::string::npos, response.find(test_payload.c_str())); |
| 456 } | 454 } |
| 457 | 455 |
| 458 TEST_F(MAYBE_SyncHttpBridgeTest, TestResponseHeader) { | 456 TEST_F(MAYBE_SyncHttpBridgeTest, TestResponseHeader) { |
| 459 ASSERT_TRUE(test_server_.Start()); | 457 ASSERT_TRUE(test_server_.Start()); |
| 460 | 458 |
| 461 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); | 459 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); |
| 462 | 460 |
| 463 GURL echo_header = test_server_.GetURL("echoall"); | 461 GURL echo_header = test_server_.GetURL("/echoall"); |
| 464 http_bridge->SetURL(echo_header.spec().c_str(), echo_header.IntPort()); | 462 http_bridge->SetURL(echo_header.spec().c_str(), echo_header.IntPort()); |
| 465 | 463 |
| 466 std::string test_payload = "###TEST PAYLOAD###"; | 464 std::string test_payload = "###TEST PAYLOAD###"; |
| 467 http_bridge->SetPostPayload("text/html", test_payload.length() + 1, | 465 http_bridge->SetPostPayload("text/html", test_payload.length() + 1, |
| 468 test_payload.c_str()); | 466 test_payload.c_str()); |
| 469 | 467 |
| 470 int os_error = 0; | 468 int os_error = 0; |
| 471 int response_code = 0; | 469 int response_code = 0; |
| 472 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); | 470 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); |
| 473 EXPECT_TRUE(success); | 471 EXPECT_TRUE(success); |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 678 | 676 |
| 679 // Sync thread: Finally run the posted task, only to find that our | 677 // Sync thread: Finally run the posted task, only to find that our |
| 680 // HttpBridgeFactory has been neutered. Should not crash. | 678 // HttpBridgeFactory has been neutered. Should not crash. |
| 681 factory->Init("TestUserAgent", BindToTrackerCallback()); | 679 factory->Init("TestUserAgent", BindToTrackerCallback()); |
| 682 | 680 |
| 683 // At this point, attempting to use the factory would trigger a crash. Both | 681 // At this point, attempting to use the factory would trigger a crash. Both |
| 684 // this test and the real world code should make sure this never happens. | 682 // this test and the real world code should make sure this never happens. |
| 685 }; | 683 }; |
| 686 | 684 |
| 687 } // namespace syncer | 685 } // namespace syncer |
| OLD | NEW |