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/strings/stringprintf.h" | 6 #include "base/strings/stringprintf.h" |
6 #include "base/synchronization/waitable_event.h" | 7 #include "base/synchronization/waitable_event.h" |
8 #include "base/test/mock_entropy_provider.h" | |
7 #include "base/threading/thread.h" | 9 #include "base/threading/thread.h" |
8 #include "net/http/http_response_headers.h" | 10 #include "net/http/http_response_headers.h" |
9 #include "net/test/spawned_test_server/spawned_test_server.h" | 11 #include "net/test/spawned_test_server/spawned_test_server.h" |
10 #include "net/url_request/test_url_fetcher_factory.h" | 12 #include "net/url_request/test_url_fetcher_factory.h" |
11 #include "net/url_request/url_fetcher_delegate.h" | 13 #include "net/url_request/url_fetcher_delegate.h" |
12 #include "net/url_request/url_request_test_util.h" | 14 #include "net/url_request/url_request_test_util.h" |
13 #include "sync/internal_api/public/base/cancelation_signal.h" | 15 #include "sync/internal_api/public/base/cancelation_signal.h" |
14 #include "sync/internal_api/public/http_bridge.h" | 16 #include "sync/internal_api/public/http_bridge.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
16 | 18 |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
139 private: | 141 private: |
140 ~ShuntedHttpBridge() override {} | 142 ~ShuntedHttpBridge() override {} |
141 | 143 |
142 void CallOnURLFetchComplete() { | 144 void CallOnURLFetchComplete() { |
143 ASSERT_TRUE(base::MessageLoop::current() == test_->GetIOThreadLoop()); | 145 ASSERT_TRUE(base::MessageLoop::current() == test_->GetIOThreadLoop()); |
144 // We return no cookies and a dummy content response. | 146 // We return no cookies and a dummy content response. |
145 net::ResponseCookies cookies; | 147 net::ResponseCookies cookies; |
146 | 148 |
147 std::string response_content = "success!"; | 149 std::string response_content = "success!"; |
148 net::TestURLFetcher fetcher(0, GURL("http://www.google.com"), NULL); | 150 net::TestURLFetcher fetcher(0, GURL("http://www.google.com"), NULL); |
151 scoped_refptr<net::HttpResponseHeaders> response_headers( | |
152 new net::HttpResponseHeaders("")); | |
149 fetcher.set_response_code(200); | 153 fetcher.set_response_code(200); |
150 fetcher.set_cookies(cookies); | 154 fetcher.set_cookies(cookies); |
151 fetcher.SetResponseString(response_content); | 155 fetcher.SetResponseString(response_content); |
156 fetcher.set_response_headers(response_headers); | |
152 OnURLFetchComplete(&fetcher); | 157 OnURLFetchComplete(&fetcher); |
153 } | 158 } |
154 SyncHttpBridgeTest* test_; | 159 SyncHttpBridgeTest* test_; |
155 bool never_finishes_; | 160 bool never_finishes_; |
156 }; | 161 }; |
157 | 162 |
158 void SyncHttpBridgeTest::RunSyncThreadBridgeUseTest( | 163 void SyncHttpBridgeTest::RunSyncThreadBridgeUseTest( |
159 base::WaitableEvent* signal_when_created, | 164 base::WaitableEvent* signal_when_created, |
160 base::WaitableEvent* signal_when_released) { | 165 base::WaitableEvent* signal_when_released) { |
161 scoped_refptr<net::URLRequestContextGetter> ctx_getter( | 166 scoped_refptr<net::URLRequestContextGetter> ctx_getter( |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
225 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); | 230 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); |
226 EXPECT_TRUE(success); | 231 EXPECT_TRUE(success); |
227 EXPECT_EQ(200, response_code); | 232 EXPECT_EQ(200, response_code); |
228 EXPECT_EQ(0, os_error); | 233 EXPECT_EQ(0, os_error); |
229 | 234 |
230 EXPECT_EQ(payload.length() + 1, | 235 EXPECT_EQ(payload.length() + 1, |
231 static_cast<size_t>(http_bridge->GetResponseContentLength())); | 236 static_cast<size_t>(http_bridge->GetResponseContentLength())); |
232 EXPECT_EQ(payload, std::string(http_bridge->GetResponseContent())); | 237 EXPECT_EQ(payload, std::string(http_bridge->GetResponseContent())); |
233 } | 238 } |
234 | 239 |
240 // Full round-trip test of the HttpBridge with compressed data, using default | |
241 // UA string and no request cookies. | |
Nicolas Zea
2015/07/30 19:59:44
Do the UA string or request cookies actually matte
Gang Wu
2015/07/31 00:39:45
comment updated.
| |
242 TEST_F(SyncHttpBridgeTest, CompressedRequestPayloadCheck) { | |
243 ASSERT_TRUE(test_server_.Start()); | |
244 | |
245 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); | |
246 | |
247 std::string payload = "this should be echoed back"; | |
248 GURL echo = test_server_.GetURL("echo"); | |
249 http_bridge->SetURL(echo.spec().c_str(), echo.IntPort()); | |
250 http_bridge->SetPostPayload("application/x-www-form-urlencoded", | |
251 payload.length(), payload.c_str()); | |
252 int os_error = 0; | |
253 int response_code = 0; | |
254 base::FieldTrialList field_trial_list(new base::MockEntropyProvider()); | |
255 base::FieldTrialList::CreateFieldTrial("SyncHttpContentCompression", | |
256 "Enabled"); | |
257 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); | |
258 EXPECT_TRUE(success); | |
259 EXPECT_EQ(200, response_code); | |
260 EXPECT_EQ(0, os_error); | |
261 | |
262 EXPECT_NE(payload.length() + 1, | |
263 static_cast<size_t>(http_bridge->GetResponseContentLength())); | |
264 std::string compressed_payload(http_bridge->GetResponseContent(), | |
265 http_bridge->GetResponseContentLength()); | |
266 std::string uncompressed_payload; | |
267 GzipUncompress(compressed_payload, &uncompressed_payload); | |
268 EXPECT_EQ(payload, uncompressed_payload); | |
269 } | |
270 | |
271 // Full round-trip test of the HttpBridge with compression. | |
Nicolas Zea
2015/07/30 19:59:44
Update comment to explain what is being tested w.r
Gang Wu
2015/07/31 00:39:45
Done.
| |
272 TEST_F(SyncHttpBridgeTest, CompressedRequestHeaderCheck) { | |
273 ASSERT_TRUE(test_server_.Start()); | |
274 | |
275 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); | |
276 | |
277 GURL echo_header = test_server_.GetURL("echoall"); | |
278 http_bridge->SetURL(echo_header.spec().c_str(), echo_header.IntPort()); | |
279 | |
280 std::string test_payload = "###TEST PAYLOAD###"; | |
281 http_bridge->SetPostPayload("text/html", test_payload.length() + 1, | |
282 test_payload.c_str()); | |
283 | |
284 int os_error = 0; | |
285 int response_code = 0; | |
286 base::FieldTrialList field_trial_list(new base::MockEntropyProvider()); | |
287 base::FieldTrialList::CreateFieldTrial("SyncHttpContentCompression", | |
288 "Enabled"); | |
289 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); | |
290 EXPECT_TRUE(success); | |
291 EXPECT_EQ(200, response_code); | |
292 EXPECT_EQ(0, os_error); | |
293 | |
294 std::string response(http_bridge->GetResponseContent(), | |
295 http_bridge->GetResponseContentLength()); | |
296 EXPECT_NE(std::string::npos, response.find("Content-Encoding: gzip")); | |
297 EXPECT_NE(std::string::npos, response.find("Accept-Encoding: gzip, deflate")); | |
298 EXPECT_NE(std::string::npos, | |
299 response.find(base::StringPrintf( | |
300 "%s: %s", net::HttpRequestHeaders::kUserAgent, kUserAgent))); | |
301 } | |
302 | |
235 // Full round-trip test of the HttpBridge. | 303 // Full round-trip test of the HttpBridge. |
236 TEST_F(SyncHttpBridgeTest, TestMakeSynchronousPostLiveComprehensive) { | 304 TEST_F(SyncHttpBridgeTest, TestMakeSynchronousPostLiveComprehensive) { |
237 ASSERT_TRUE(test_server_.Start()); | 305 ASSERT_TRUE(test_server_.Start()); |
238 | 306 |
239 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); | 307 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); |
240 | 308 |
241 GURL echo_header = test_server_.GetURL("echoall"); | 309 GURL echo_header = test_server_.GetURL("echoall"); |
242 http_bridge->SetURL(echo_header.spec().c_str(), echo_header.IntPort()); | 310 http_bridge->SetURL(echo_header.spec().c_str(), echo_header.IntPort()); |
243 | 311 |
244 std::string test_payload = "###TEST PAYLOAD###"; | 312 std::string test_payload = "###TEST PAYLOAD###"; |
245 http_bridge->SetPostPayload("text/html", test_payload.length() + 1, | 313 http_bridge->SetPostPayload("text/html", test_payload.length() + 1, |
246 test_payload.c_str()); | 314 test_payload.c_str()); |
247 | 315 |
248 int os_error = 0; | 316 int os_error = 0; |
249 int response_code = 0; | 317 int response_code = 0; |
250 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); | 318 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); |
251 EXPECT_TRUE(success); | 319 EXPECT_TRUE(success); |
252 EXPECT_EQ(200, response_code); | 320 EXPECT_EQ(200, response_code); |
253 EXPECT_EQ(0, os_error); | 321 EXPECT_EQ(0, os_error); |
254 | 322 |
255 std::string response(http_bridge->GetResponseContent(), | 323 std::string response(http_bridge->GetResponseContent(), |
256 http_bridge->GetResponseContentLength()); | 324 http_bridge->GetResponseContentLength()); |
257 EXPECT_EQ(std::string::npos, response.find("Cookie:")); | 325 EXPECT_EQ(std::string::npos, response.find("Cookie:")); |
326 EXPECT_NE(std::string::npos, response.find("Accept-Encoding: deflate")); | |
258 EXPECT_NE(std::string::npos, | 327 EXPECT_NE(std::string::npos, |
259 response.find(base::StringPrintf("%s: %s", | 328 response.find(base::StringPrintf("%s: %s", |
260 net::HttpRequestHeaders::kUserAgent, kUserAgent))); | 329 net::HttpRequestHeaders::kUserAgent, kUserAgent))); |
261 EXPECT_NE(std::string::npos, response.find(test_payload.c_str())); | 330 EXPECT_NE(std::string::npos, response.find(test_payload.c_str())); |
262 } | 331 } |
263 | 332 |
264 TEST_F(SyncHttpBridgeTest, TestExtraRequestHeaders) { | 333 TEST_F(SyncHttpBridgeTest, TestExtraRequestHeaders) { |
265 ASSERT_TRUE(test_server_.Start()); | 334 ASSERT_TRUE(test_server_.Start()); |
266 | 335 |
267 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); | 336 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
512 | 581 |
513 // Sync thread: Finally run the posted task, only to find that our | 582 // Sync thread: Finally run the posted task, only to find that our |
514 // HttpBridgeFactory has been neutered. Should not crash. | 583 // HttpBridgeFactory has been neutered. Should not crash. |
515 factory->Init("TestUserAgent"); | 584 factory->Init("TestUserAgent"); |
516 | 585 |
517 // At this point, attempting to use the factory would trigger a crash. Both | 586 // At this point, attempting to use the factory would trigger a crash. Both |
518 // this test and the real world code should make sure this never happens. | 587 // this test and the real world code should make sure this never happens. |
519 }; | 588 }; |
520 | 589 |
521 } // namespace syncer | 590 } // namespace syncer |
OLD | NEW |