| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/message_loop_proxy.h" | 5 #include "base/message_loop_proxy.h" |
| 6 #include "base/thread.h" | 6 #include "base/thread.h" |
| 7 #include "chrome/browser/chrome_thread.h" | 7 #include "chrome/browser/chrome_thread.h" |
| 8 #include "chrome/browser/sync/glue/http_bridge.h" | 8 #include "chrome/browser/sync/glue/http_bridge.h" |
| 9 #include "chrome/common/net/test_url_fetcher_factory.h" | 9 #include "chrome/common/net/test_url_fetcher_factory.h" |
| 10 #include "net/url_request/url_request_unittest.h" | 10 #include "net/url_request/url_request_unittest.h" |
| 11 #include "net/test/test_server.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 13 |
| 13 using browser_sync::HttpBridge; | 14 using browser_sync::HttpBridge; |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 // TODO(timsteele): Should use PathService here. See Chromium Issue 3113. | 17 // TODO(timsteele): Should use PathService here. See Chromium Issue 3113. |
| 17 const wchar_t kDocRoot[] = L"chrome/test/data"; | 18 const wchar_t kDocRoot[] = L"chrome/test/data"; |
| 18 } | 19 } |
| 19 | 20 |
| 20 // Lazy getter for TestURLRequestContext instances. | 21 // Lazy getter for TestURLRequestContext instances. |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 EXPECT_EQ(0, os_error); | 168 EXPECT_EQ(0, os_error); |
| 168 | 169 |
| 169 EXPECT_EQ(8, http_bridge->GetResponseContentLength()); | 170 EXPECT_EQ(8, http_bridge->GetResponseContentLength()); |
| 170 EXPECT_EQ(std::string("success!"), | 171 EXPECT_EQ(std::string("success!"), |
| 171 std::string(http_bridge->GetResponseContent())); | 172 std::string(http_bridge->GetResponseContent())); |
| 172 } | 173 } |
| 173 | 174 |
| 174 // Full round-trip test of the HttpBridge, using default UA string and | 175 // Full round-trip test of the HttpBridge, using default UA string and |
| 175 // no request cookies. | 176 // no request cookies. |
| 176 TEST_F(HttpBridgeTest, TestMakeSynchronousPostLiveWithPayload) { | 177 TEST_F(HttpBridgeTest, TestMakeSynchronousPostLiveWithPayload) { |
| 177 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(kDocRoot)); | 178 scoped_refptr<net::HTTPTestServer> server( |
| 179 net::HTTPTestServer::CreateServer(kDocRoot)); |
| 178 ASSERT_TRUE(NULL != server.get()); | 180 ASSERT_TRUE(NULL != server.get()); |
| 179 | 181 |
| 180 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); | 182 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); |
| 181 | 183 |
| 182 std::string payload = "this should be echoed back"; | 184 std::string payload = "this should be echoed back"; |
| 183 GURL echo = server->TestServerPage("echo"); | 185 GURL echo = server->TestServerPage("echo"); |
| 184 http_bridge->SetURL(echo.spec().c_str(), echo.IntPort()); | 186 http_bridge->SetURL(echo.spec().c_str(), echo.IntPort()); |
| 185 http_bridge->SetPostPayload("application/x-www-form-urlencoded", | 187 http_bridge->SetPostPayload("application/x-www-form-urlencoded", |
| 186 payload.length() + 1, payload.c_str()); | 188 payload.length() + 1, payload.c_str()); |
| 187 int os_error = 0; | 189 int os_error = 0; |
| 188 int response_code = 0; | 190 int response_code = 0; |
| 189 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); | 191 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); |
| 190 EXPECT_TRUE(success); | 192 EXPECT_TRUE(success); |
| 191 EXPECT_EQ(200, response_code); | 193 EXPECT_EQ(200, response_code); |
| 192 EXPECT_EQ(0, os_error); | 194 EXPECT_EQ(0, os_error); |
| 193 | 195 |
| 194 EXPECT_EQ(payload.length() + 1, | 196 EXPECT_EQ(payload.length() + 1, |
| 195 static_cast<size_t>(http_bridge->GetResponseContentLength())); | 197 static_cast<size_t>(http_bridge->GetResponseContentLength())); |
| 196 EXPECT_EQ(payload, std::string(http_bridge->GetResponseContent())); | 198 EXPECT_EQ(payload, std::string(http_bridge->GetResponseContent())); |
| 197 } | 199 } |
| 198 | 200 |
| 199 // Full round-trip test of the HttpBridge, using custom UA string | 201 // Full round-trip test of the HttpBridge, using custom UA string |
| 200 TEST_F(HttpBridgeTest, TestMakeSynchronousPostLiveComprehensive) { | 202 TEST_F(HttpBridgeTest, TestMakeSynchronousPostLiveComprehensive) { |
| 201 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(kDocRoot)); | 203 scoped_refptr<net::HTTPTestServer> server( |
| 204 net::HTTPTestServer::CreateServer(kDocRoot)); |
| 202 ASSERT_TRUE(NULL != server.get()); | 205 ASSERT_TRUE(NULL != server.get()); |
| 203 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); | 206 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); |
| 204 | 207 |
| 205 GURL echo_header = server->TestServerPage("echoall"); | 208 GURL echo_header = server->TestServerPage("echoall"); |
| 206 http_bridge->SetUserAgent("bob"); | 209 http_bridge->SetUserAgent("bob"); |
| 207 http_bridge->SetURL(echo_header.spec().c_str(), echo_header.IntPort()); | 210 http_bridge->SetURL(echo_header.spec().c_str(), echo_header.IntPort()); |
| 208 | 211 |
| 209 std::string test_payload = "###TEST PAYLOAD###"; | 212 std::string test_payload = "###TEST PAYLOAD###"; |
| 210 http_bridge->SetPostPayload("text/html", test_payload.length() + 1, | 213 http_bridge->SetPostPayload("text/html", test_payload.length() + 1, |
| 211 test_payload.c_str()); | 214 test_payload.c_str()); |
| 212 | 215 |
| 213 int os_error = 0; | 216 int os_error = 0; |
| 214 int response_code = 0; | 217 int response_code = 0; |
| 215 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); | 218 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); |
| 216 EXPECT_TRUE(success); | 219 EXPECT_TRUE(success); |
| 217 EXPECT_EQ(200, response_code); | 220 EXPECT_EQ(200, response_code); |
| 218 EXPECT_EQ(0, os_error); | 221 EXPECT_EQ(0, os_error); |
| 219 | 222 |
| 220 std::string response(http_bridge->GetResponseContent(), | 223 std::string response(http_bridge->GetResponseContent(), |
| 221 http_bridge->GetResponseContentLength()); | 224 http_bridge->GetResponseContentLength()); |
| 222 EXPECT_EQ(std::string::npos, response.find("Cookie:")); | 225 EXPECT_EQ(std::string::npos, response.find("Cookie:")); |
| 223 EXPECT_NE(std::string::npos, response.find("User-Agent: bob")); | 226 EXPECT_NE(std::string::npos, response.find("User-Agent: bob")); |
| 224 EXPECT_NE(std::string::npos, response.find(test_payload.c_str())); | 227 EXPECT_NE(std::string::npos, response.find(test_payload.c_str())); |
| 225 } | 228 } |
| 226 | 229 |
| 227 TEST_F(HttpBridgeTest, TestExtraRequestHeaders) { | 230 TEST_F(HttpBridgeTest, TestExtraRequestHeaders) { |
| 228 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(kDocRoot)); | 231 scoped_refptr<net::HTTPTestServer> server( |
| 232 net::HTTPTestServer::CreateServer(kDocRoot)); |
| 229 ASSERT_TRUE(NULL != server.get()); | 233 ASSERT_TRUE(NULL != server.get()); |
| 230 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); | 234 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); |
| 231 | 235 |
| 232 GURL echo_header = server->TestServerPage("echoall"); | 236 GURL echo_header = server->TestServerPage("echoall"); |
| 233 | 237 |
| 234 http_bridge->SetURL(echo_header.spec().c_str(), echo_header.IntPort()); | 238 http_bridge->SetURL(echo_header.spec().c_str(), echo_header.IntPort()); |
| 235 http_bridge->SetExtraRequestHeaders("test:fnord"); | 239 http_bridge->SetExtraRequestHeaders("test:fnord"); |
| 236 | 240 |
| 237 std::string test_payload = "###TEST PAYLOAD###"; | 241 std::string test_payload = "###TEST PAYLOAD###"; |
| 238 http_bridge->SetPostPayload("text/html", test_payload.length() + 1, | 242 http_bridge->SetPostPayload("text/html", test_payload.length() + 1, |
| 239 test_payload.c_str()); | 243 test_payload.c_str()); |
| 240 | 244 |
| 241 int os_error = 0; | 245 int os_error = 0; |
| 242 int response_code = 0; | 246 int response_code = 0; |
| 243 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); | 247 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); |
| 244 EXPECT_TRUE(success); | 248 EXPECT_TRUE(success); |
| 245 EXPECT_EQ(200, response_code); | 249 EXPECT_EQ(200, response_code); |
| 246 EXPECT_EQ(0, os_error); | 250 EXPECT_EQ(0, os_error); |
| 247 | 251 |
| 248 std::string response(http_bridge->GetResponseContent(), | 252 std::string response(http_bridge->GetResponseContent(), |
| 249 http_bridge->GetResponseContentLength()); | 253 http_bridge->GetResponseContentLength()); |
| 250 | 254 |
| 251 EXPECT_NE(std::string::npos, response.find("fnord")); | 255 EXPECT_NE(std::string::npos, response.find("fnord")); |
| 252 EXPECT_NE(std::string::npos, response.find(test_payload.c_str())); | 256 EXPECT_NE(std::string::npos, response.find(test_payload.c_str())); |
| 253 } | 257 } |
| 254 | 258 |
| 255 TEST_F(HttpBridgeTest, TestResponseHeader) { | 259 TEST_F(HttpBridgeTest, TestResponseHeader) { |
| 256 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(kDocRoot)); | 260 scoped_refptr<net::HTTPTestServer> server( |
| 261 net::HTTPTestServer::CreateServer(kDocRoot)); |
| 257 ASSERT_TRUE(NULL != server.get()); | 262 ASSERT_TRUE(NULL != server.get()); |
| 258 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); | 263 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); |
| 259 | 264 |
| 260 GURL echo_header = server->TestServerPage("echoall"); | 265 GURL echo_header = server->TestServerPage("echoall"); |
| 261 http_bridge->SetURL(echo_header.spec().c_str(), echo_header.IntPort()); | 266 http_bridge->SetURL(echo_header.spec().c_str(), echo_header.IntPort()); |
| 262 | 267 |
| 263 std::string test_payload = "###TEST PAYLOAD###"; | 268 std::string test_payload = "###TEST PAYLOAD###"; |
| 264 http_bridge->SetPostPayload("text/html", test_payload.length() + 1, | 269 http_bridge->SetPostPayload("text/html", test_payload.length() + 1, |
| 265 test_payload.c_str()); | 270 test_payload.c_str()); |
| 266 | 271 |
| 267 int os_error = 0; | 272 int os_error = 0; |
| 268 int response_code = 0; | 273 int response_code = 0; |
| 269 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); | 274 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); |
| 270 EXPECT_TRUE(success); | 275 EXPECT_TRUE(success); |
| 271 EXPECT_EQ(200, response_code); | 276 EXPECT_EQ(200, response_code); |
| 272 EXPECT_EQ(0, os_error); | 277 EXPECT_EQ(0, os_error); |
| 273 | 278 |
| 274 EXPECT_EQ(http_bridge->GetResponseHeaderValue("Content-type"), "text/html"); | 279 EXPECT_EQ(http_bridge->GetResponseHeaderValue("Content-type"), "text/html"); |
| 275 EXPECT_TRUE(http_bridge->GetResponseHeaderValue("invalid-header").empty()); | 280 EXPECT_TRUE(http_bridge->GetResponseHeaderValue("invalid-header").empty()); |
| 276 } | 281 } |
| OLD | NEW |