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" |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 EXPECT_EQ(0, os_error); | 167 EXPECT_EQ(0, os_error); |
168 | 168 |
169 EXPECT_EQ(8, http_bridge->GetResponseContentLength()); | 169 EXPECT_EQ(8, http_bridge->GetResponseContentLength()); |
170 EXPECT_EQ(std::string("success!"), | 170 EXPECT_EQ(std::string("success!"), |
171 std::string(http_bridge->GetResponseContent())); | 171 std::string(http_bridge->GetResponseContent())); |
172 } | 172 } |
173 | 173 |
174 // Full round-trip test of the HttpBridge, using default UA string and | 174 // Full round-trip test of the HttpBridge, using default UA string and |
175 // no request cookies. | 175 // no request cookies. |
176 TEST_F(HttpBridgeTest, TestMakeSynchronousPostLiveWithPayload) { | 176 TEST_F(HttpBridgeTest, TestMakeSynchronousPostLiveWithPayload) { |
177 scoped_refptr<HTTPTestServer> server = | 177 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(kDocRoot)); |
178 HTTPTestServer::CreateServer(kDocRoot, NULL); | |
179 ASSERT_TRUE(NULL != server.get()); | 178 ASSERT_TRUE(NULL != server.get()); |
180 | 179 |
181 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); | 180 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); |
182 | 181 |
183 std::string payload = "this should be echoed back"; | 182 std::string payload = "this should be echoed back"; |
184 GURL echo = server->TestServerPage("echo"); | 183 GURL echo = server->TestServerPage("echo"); |
185 http_bridge->SetURL(echo.spec().c_str(), echo.IntPort()); | 184 http_bridge->SetURL(echo.spec().c_str(), echo.IntPort()); |
186 http_bridge->SetPostPayload("application/x-www-form-urlencoded", | 185 http_bridge->SetPostPayload("application/x-www-form-urlencoded", |
187 payload.length() + 1, payload.c_str()); | 186 payload.length() + 1, payload.c_str()); |
188 int os_error = 0; | 187 int os_error = 0; |
189 int response_code = 0; | 188 int response_code = 0; |
190 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); | 189 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); |
191 EXPECT_TRUE(success); | 190 EXPECT_TRUE(success); |
192 EXPECT_EQ(200, response_code); | 191 EXPECT_EQ(200, response_code); |
193 EXPECT_EQ(0, os_error); | 192 EXPECT_EQ(0, os_error); |
194 | 193 |
195 EXPECT_EQ(payload.length() + 1, | 194 EXPECT_EQ(payload.length() + 1, |
196 static_cast<size_t>(http_bridge->GetResponseContentLength())); | 195 static_cast<size_t>(http_bridge->GetResponseContentLength())); |
197 EXPECT_EQ(payload, std::string(http_bridge->GetResponseContent())); | 196 EXPECT_EQ(payload, std::string(http_bridge->GetResponseContent())); |
198 } | 197 } |
199 | 198 |
200 // Full round-trip test of the HttpBridge, using custom UA string | 199 // Full round-trip test of the HttpBridge, using custom UA string |
201 TEST_F(HttpBridgeTest, TestMakeSynchronousPostLiveComprehensive) { | 200 TEST_F(HttpBridgeTest, TestMakeSynchronousPostLiveComprehensive) { |
202 scoped_refptr<HTTPTestServer> server = | 201 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(kDocRoot)); |
203 HTTPTestServer::CreateServer(kDocRoot, NULL); | |
204 ASSERT_TRUE(NULL != server.get()); | 202 ASSERT_TRUE(NULL != server.get()); |
205 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); | 203 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); |
206 | 204 |
207 GURL echo_header = server->TestServerPage("echoall"); | 205 GURL echo_header = server->TestServerPage("echoall"); |
208 http_bridge->SetUserAgent("bob"); | 206 http_bridge->SetUserAgent("bob"); |
209 http_bridge->SetURL(echo_header.spec().c_str(), echo_header.IntPort()); | 207 http_bridge->SetURL(echo_header.spec().c_str(), echo_header.IntPort()); |
210 | 208 |
211 std::string test_payload = "###TEST PAYLOAD###"; | 209 std::string test_payload = "###TEST PAYLOAD###"; |
212 http_bridge->SetPostPayload("text/html", test_payload.length() + 1, | 210 http_bridge->SetPostPayload("text/html", test_payload.length() + 1, |
213 test_payload.c_str()); | 211 test_payload.c_str()); |
214 | 212 |
215 int os_error = 0; | 213 int os_error = 0; |
216 int response_code = 0; | 214 int response_code = 0; |
217 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); | 215 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); |
218 EXPECT_TRUE(success); | 216 EXPECT_TRUE(success); |
219 EXPECT_EQ(200, response_code); | 217 EXPECT_EQ(200, response_code); |
220 EXPECT_EQ(0, os_error); | 218 EXPECT_EQ(0, os_error); |
221 | 219 |
222 std::string response(http_bridge->GetResponseContent(), | 220 std::string response(http_bridge->GetResponseContent(), |
223 http_bridge->GetResponseContentLength()); | 221 http_bridge->GetResponseContentLength()); |
224 EXPECT_EQ(std::string::npos, response.find("Cookie:")); | 222 EXPECT_EQ(std::string::npos, response.find("Cookie:")); |
225 EXPECT_NE(std::string::npos, response.find("User-Agent: bob")); | 223 EXPECT_NE(std::string::npos, response.find("User-Agent: bob")); |
226 EXPECT_NE(std::string::npos, response.find(test_payload.c_str())); | 224 EXPECT_NE(std::string::npos, response.find(test_payload.c_str())); |
227 } | 225 } |
228 | 226 |
229 TEST_F(HttpBridgeTest, TestExtraRequestHeaders) { | 227 TEST_F(HttpBridgeTest, TestExtraRequestHeaders) { |
230 scoped_refptr<HTTPTestServer> server = | 228 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(kDocRoot)); |
231 HTTPTestServer::CreateServer(kDocRoot, NULL); | |
232 ASSERT_TRUE(NULL != server.get()); | 229 ASSERT_TRUE(NULL != server.get()); |
233 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); | 230 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); |
234 | 231 |
235 GURL echo_header = server->TestServerPage("echoall"); | 232 GURL echo_header = server->TestServerPage("echoall"); |
236 | 233 |
237 http_bridge->SetURL(echo_header.spec().c_str(), echo_header.IntPort()); | 234 http_bridge->SetURL(echo_header.spec().c_str(), echo_header.IntPort()); |
238 http_bridge->SetExtraRequestHeaders("test:fnord"); | 235 http_bridge->SetExtraRequestHeaders("test:fnord"); |
239 | 236 |
240 std::string test_payload = "###TEST PAYLOAD###"; | 237 std::string test_payload = "###TEST PAYLOAD###"; |
241 http_bridge->SetPostPayload("text/html", test_payload.length() + 1, | 238 http_bridge->SetPostPayload("text/html", test_payload.length() + 1, |
242 test_payload.c_str()); | 239 test_payload.c_str()); |
243 | 240 |
244 int os_error = 0; | 241 int os_error = 0; |
245 int response_code = 0; | 242 int response_code = 0; |
246 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); | 243 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); |
247 EXPECT_TRUE(success); | 244 EXPECT_TRUE(success); |
248 EXPECT_EQ(200, response_code); | 245 EXPECT_EQ(200, response_code); |
249 EXPECT_EQ(0, os_error); | 246 EXPECT_EQ(0, os_error); |
250 | 247 |
251 std::string response(http_bridge->GetResponseContent(), | 248 std::string response(http_bridge->GetResponseContent(), |
252 http_bridge->GetResponseContentLength()); | 249 http_bridge->GetResponseContentLength()); |
253 | 250 |
254 EXPECT_NE(std::string::npos, response.find("fnord")); | 251 EXPECT_NE(std::string::npos, response.find("fnord")); |
255 EXPECT_NE(std::string::npos, response.find(test_payload.c_str())); | 252 EXPECT_NE(std::string::npos, response.find(test_payload.c_str())); |
256 } | 253 } |
257 | 254 |
258 TEST_F(HttpBridgeTest, TestResponseHeader) { | 255 TEST_F(HttpBridgeTest, TestResponseHeader) { |
259 scoped_refptr<HTTPTestServer> server = | 256 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(kDocRoot)); |
260 HTTPTestServer::CreateServer(kDocRoot, NULL); | |
261 ASSERT_TRUE(NULL != server.get()); | 257 ASSERT_TRUE(NULL != server.get()); |
262 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); | 258 scoped_refptr<HttpBridge> http_bridge(BuildBridge()); |
263 | 259 |
264 GURL echo_header = server->TestServerPage("echoall"); | 260 GURL echo_header = server->TestServerPage("echoall"); |
265 http_bridge->SetURL(echo_header.spec().c_str(), echo_header.IntPort()); | 261 http_bridge->SetURL(echo_header.spec().c_str(), echo_header.IntPort()); |
266 | 262 |
267 std::string test_payload = "###TEST PAYLOAD###"; | 263 std::string test_payload = "###TEST PAYLOAD###"; |
268 http_bridge->SetPostPayload("text/html", test_payload.length() + 1, | 264 http_bridge->SetPostPayload("text/html", test_payload.length() + 1, |
269 test_payload.c_str()); | 265 test_payload.c_str()); |
270 | 266 |
271 int os_error = 0; | 267 int os_error = 0; |
272 int response_code = 0; | 268 int response_code = 0; |
273 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); | 269 bool success = http_bridge->MakeSynchronousPost(&os_error, &response_code); |
274 EXPECT_TRUE(success); | 270 EXPECT_TRUE(success); |
275 EXPECT_EQ(200, response_code); | 271 EXPECT_EQ(200, response_code); |
276 EXPECT_EQ(0, os_error); | 272 EXPECT_EQ(0, os_error); |
277 | 273 |
278 EXPECT_EQ(http_bridge->GetResponseHeaderValue("Content-type"), "text/html"); | 274 EXPECT_EQ(http_bridge->GetResponseHeaderValue("Content-type"), "text/html"); |
279 EXPECT_TRUE(http_bridge->GetResponseHeaderValue("invalid-header").empty()); | 275 EXPECT_TRUE(http_bridge->GetResponseHeaderValue("invalid-header").empty()); |
280 } | 276 } |
OLD | NEW |