| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <utility> | 5 #include <utility> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 protected: | 202 protected: |
| 203 scoped_refptr<HttpServer> server_; | 203 scoped_refptr<HttpServer> server_; |
| 204 IPEndPoint server_address_; | 204 IPEndPoint server_address_; |
| 205 base::Closure run_loop_quit_func_; | 205 base::Closure run_loop_quit_func_; |
| 206 std::vector<std::pair<HttpServerRequestInfo, int> > requests_; | 206 std::vector<std::pair<HttpServerRequestInfo, int> > requests_; |
| 207 | 207 |
| 208 private: | 208 private: |
| 209 size_t quit_after_request_count_; | 209 size_t quit_after_request_count_; |
| 210 }; | 210 }; |
| 211 | 211 |
| 212 class WebSocketTest : public HttpServerTest { |
| 213 virtual void OnHttpRequest(int connection_id, |
| 214 const HttpServerRequestInfo& info) OVERRIDE { |
| 215 NOTREACHED(); |
| 216 } |
| 217 |
| 218 virtual void OnWebSocketRequest(int connection_id, |
| 219 const HttpServerRequestInfo& info) OVERRIDE { |
| 220 HttpServerTest::OnHttpRequest(connection_id, info); |
| 221 } |
| 222 |
| 223 virtual void OnWebSocketMessage(int connection_id, |
| 224 const std::string& data) OVERRIDE { |
| 225 } |
| 226 }; |
| 227 |
| 212 TEST_F(HttpServerTest, Request) { | 228 TEST_F(HttpServerTest, Request) { |
| 213 TestHttpClient client; | 229 TestHttpClient client; |
| 214 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); | 230 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); |
| 215 client.Send("GET /test HTTP/1.1\r\n\r\n"); | 231 client.Send("GET /test HTTP/1.1\r\n\r\n"); |
| 216 ASSERT_TRUE(RunUntilRequestsReceived(1)); | 232 ASSERT_TRUE(RunUntilRequestsReceived(1)); |
| 217 ASSERT_EQ("GET", GetRequest(0).method); | 233 ASSERT_EQ("GET", GetRequest(0).method); |
| 218 ASSERT_EQ("/test", GetRequest(0).path); | 234 ASSERT_EQ("/test", GetRequest(0).path); |
| 219 ASSERT_EQ("", GetRequest(0).data); | 235 ASSERT_EQ("", GetRequest(0).data); |
| 220 ASSERT_EQ(0u, GetRequest(0).headers.size()); | 236 ASSERT_EQ(0u, GetRequest(0).headers.size()); |
| 221 ASSERT_TRUE(StartsWithASCII(GetRequest(0).peer.ToString(), | 237 ASSERT_TRUE(StartsWithASCII(GetRequest(0).peer.ToString(), |
| (...skipping 24 matching lines...) Expand all Loading... |
| 246 ASSERT_EQ("", GetRequest(0).data); | 262 ASSERT_EQ("", GetRequest(0).data); |
| 247 | 263 |
| 248 for (size_t i = 0; i < arraysize(kHeaders); ++i) { | 264 for (size_t i = 0; i < arraysize(kHeaders); ++i) { |
| 249 std::string field = StringToLowerASCII(std::string(kHeaders[i][0])); | 265 std::string field = StringToLowerASCII(std::string(kHeaders[i][0])); |
| 250 std::string value = kHeaders[i][2]; | 266 std::string value = kHeaders[i][2]; |
| 251 ASSERT_EQ(1u, GetRequest(0).headers.count(field)) << field; | 267 ASSERT_EQ(1u, GetRequest(0).headers.count(field)) << field; |
| 252 ASSERT_EQ(value, GetRequest(0).headers[field]) << kHeaders[i][0]; | 268 ASSERT_EQ(value, GetRequest(0).headers[field]) << kHeaders[i][0]; |
| 253 } | 269 } |
| 254 } | 270 } |
| 255 | 271 |
| 272 TEST_F(HttpServerTest, RequestWithDuplicateHeaders) { |
| 273 TestHttpClient client; |
| 274 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); |
| 275 const char* kHeaders[][3] = { |
| 276 {"FirstHeader", ": ", "1"}, |
| 277 {"DuplicateHeader", ": ", "2"}, |
| 278 {"MiddleHeader", ": ", "3"}, |
| 279 {"DuplicateHeader", ": ", "4"}, |
| 280 {"LastHeader", ": ", "5"}, |
| 281 }; |
| 282 std::string headers; |
| 283 for (size_t i = 0; i < arraysize(kHeaders); ++i) { |
| 284 headers += |
| 285 std::string(kHeaders[i][0]) + kHeaders[i][1] + kHeaders[i][2] + "\r\n"; |
| 286 } |
| 287 |
| 288 client.Send("GET /test HTTP/1.1\r\n" + headers + "\r\n"); |
| 289 ASSERT_TRUE(RunUntilRequestsReceived(1)); |
| 290 ASSERT_EQ("", GetRequest(0).data); |
| 291 |
| 292 for (size_t i = 0; i < arraysize(kHeaders); ++i) { |
| 293 std::string field = StringToLowerASCII(std::string(kHeaders[i][0])); |
| 294 std::string value = (field == "duplicateheader") ? "2,4" : kHeaders[i][2]; |
| 295 ASSERT_EQ(1u, GetRequest(0).headers.count(field)) << field; |
| 296 ASSERT_EQ(value, GetRequest(0).headers[field]) << kHeaders[i][0]; |
| 297 } |
| 298 } |
| 299 |
| 256 TEST_F(HttpServerTest, RequestWithBody) { | 300 TEST_F(HttpServerTest, RequestWithBody) { |
| 257 TestHttpClient client; | 301 TestHttpClient client; |
| 258 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); | 302 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); |
| 259 std::string body = "a" + std::string(1 << 10, 'b') + "c"; | 303 std::string body = "a" + std::string(1 << 10, 'b') + "c"; |
| 260 client.Send(base::StringPrintf( | 304 client.Send(base::StringPrintf( |
| 261 "GET /test HTTP/1.1\r\n" | 305 "GET /test HTTP/1.1\r\n" |
| 262 "SomeHeader: 1\r\n" | 306 "SomeHeader: 1\r\n" |
| 263 "Content-Length: %" PRIuS "\r\n\r\n%s", | 307 "Content-Length: %" PRIuS "\r\n\r\n%s", |
| 264 body.length(), | 308 body.length(), |
| 265 body.c_str())); | 309 body.c_str())); |
| 266 ASSERT_TRUE(RunUntilRequestsReceived(1)); | 310 ASSERT_TRUE(RunUntilRequestsReceived(1)); |
| 267 ASSERT_EQ(2u, GetRequest(0).headers.size()); | 311 ASSERT_EQ(2u, GetRequest(0).headers.size()); |
| 268 ASSERT_EQ(body.length(), GetRequest(0).data.length()); | 312 ASSERT_EQ(body.length(), GetRequest(0).data.length()); |
| 269 ASSERT_EQ('a', body[0]); | 313 ASSERT_EQ('a', body[0]); |
| 270 ASSERT_EQ('c', *body.rbegin()); | 314 ASSERT_EQ('c', *body.rbegin()); |
| 271 } | 315 } |
| 272 | 316 |
| 317 TEST_F(WebSocketTest, RequestWebSocket) { |
| 318 TestHttpClient client; |
| 319 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); |
| 320 client.Send( |
| 321 "GET /test HTTP/1.1\r\n" |
| 322 "Upgrade: WebSocket\r\n" |
| 323 "Connection: SomethingElse, Upgrade\r\n" |
| 324 "Sec-WebSocket-Version: 8\r\n" |
| 325 "Sec-WebSocket-Key: key\r\n" |
| 326 "\r\n"); |
| 327 ASSERT_TRUE(RunUntilRequestsReceived(1)); |
| 328 } |
| 329 |
| 273 TEST_F(HttpServerTest, RequestWithTooLargeBody) { | 330 TEST_F(HttpServerTest, RequestWithTooLargeBody) { |
| 274 class TestURLFetcherDelegate : public URLFetcherDelegate { | 331 class TestURLFetcherDelegate : public URLFetcherDelegate { |
| 275 public: | 332 public: |
| 276 TestURLFetcherDelegate(const base::Closure& quit_loop_func) | 333 TestURLFetcherDelegate(const base::Closure& quit_loop_func) |
| 277 : quit_loop_func_(quit_loop_func) {} | 334 : quit_loop_func_(quit_loop_func) {} |
| 278 virtual ~TestURLFetcherDelegate() {} | 335 virtual ~TestURLFetcherDelegate() {} |
| 279 | 336 |
| 280 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE { | 337 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE { |
| 281 EXPECT_EQ(HTTP_INTERNAL_SERVER_ERROR, source->GetResponseCode()); | 338 EXPECT_EQ(HTTP_INTERNAL_SERVER_ERROR, source->GetResponseCode()); |
| 282 quit_loop_func_.Run(); | 339 quit_loop_func_.Run(); |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 403 | 460 |
| 404 ASSERT_EQ(client_connection_id, GetConnectionId(2)); | 461 ASSERT_EQ(client_connection_id, GetConnectionId(2)); |
| 405 server_->Send200(client_connection_id, "Content for /test3", "text/plain"); | 462 server_->Send200(client_connection_id, "Content for /test3", "text/plain"); |
| 406 std::string response3; | 463 std::string response3; |
| 407 ASSERT_TRUE(client.Read(&response3)); | 464 ASSERT_TRUE(client.Read(&response3)); |
| 408 ASSERT_TRUE(StartsWithASCII(response3, "HTTP/1.1 200 OK", true)); | 465 ASSERT_TRUE(StartsWithASCII(response3, "HTTP/1.1 200 OK", true)); |
| 409 ASSERT_TRUE(EndsWith(response3, "Content for /test3", true)); | 466 ASSERT_TRUE(EndsWith(response3, "Content for /test3", true)); |
| 410 } | 467 } |
| 411 | 468 |
| 412 } // namespace net | 469 } // namespace net |
| OLD | NEW |