Chromium Code Reviews| 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 "net/server/http_server.h" | 5 #include "net/server/http_server.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 52 #include "testing/gtest/include/gtest/gtest.h" | 52 #include "testing/gtest/include/gtest/gtest.h" |
| 53 | 53 |
| 54 using net::test::IsOk; | 54 using net::test::IsOk; |
| 55 | 55 |
| 56 namespace net { | 56 namespace net { |
| 57 | 57 |
| 58 namespace { | 58 namespace { |
| 59 | 59 |
| 60 const int kMaxExpectedResponseLength = 2048; | 60 const int kMaxExpectedResponseLength = 2048; |
| 61 | 61 |
| 62 void SetTimedOutAndQuitLoop(const base::WeakPtr<bool> timed_out, | |
| 63 const base::Closure& quit_loop_func) { | |
| 64 if (timed_out) { | |
| 65 *timed_out = true; | |
| 66 quit_loop_func.Run(); | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 bool RunLoopWithTimeout(base::RunLoop* run_loop) { | |
| 71 bool timed_out = false; | |
| 72 base::WeakPtrFactory<bool> timed_out_weak_factory(&timed_out); | |
| 73 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
| 74 FROM_HERE, | |
| 75 base::Bind(&SetTimedOutAndQuitLoop, timed_out_weak_factory.GetWeakPtr(), | |
| 76 run_loop->QuitClosure()), | |
| 77 base::TimeDelta::FromSeconds(1)); | |
| 78 run_loop->Run(); | |
| 79 return !timed_out; | |
| 80 } | |
| 81 | |
| 82 class TestHttpClient { | 62 class TestHttpClient { |
| 83 public: | 63 public: |
| 84 TestHttpClient() : connect_result_(OK) {} | 64 TestHttpClient() : connect_result_(OK) {} |
| 85 | 65 |
| 86 int ConnectAndWait(const IPEndPoint& address) { | 66 int ConnectAndWait(const IPEndPoint& address) { |
| 87 AddressList addresses(address); | 67 AddressList addresses(address); |
| 88 NetLogSource source; | 68 NetLogSource source; |
| 89 socket_.reset(new TCPClientSocket(addresses, NULL, NULL, source)); | 69 socket_.reset(new TCPClientSocket(addresses, NULL, NULL, source)); |
| 90 | 70 |
| 91 base::RunLoop run_loop; | 71 base::RunLoop run_loop; |
| 92 connect_result_ = socket_->Connect(base::Bind(&TestHttpClient::OnConnect, | 72 connect_result_ = socket_->Connect(base::Bind(&TestHttpClient::OnConnect, |
| 93 base::Unretained(this), | 73 base::Unretained(this), |
| 94 run_loop.QuitClosure())); | 74 run_loop.QuitClosure())); |
| 95 if (connect_result_ != OK && connect_result_ != ERR_IO_PENDING) | 75 if (connect_result_ != OK && connect_result_ != ERR_IO_PENDING) |
| 96 return connect_result_; | 76 return connect_result_; |
| 97 | 77 |
| 98 if (!RunLoopWithTimeout(&run_loop)) | 78 run_loop.Run(); |
| 99 return ERR_TIMED_OUT; | |
| 100 return connect_result_; | 79 return connect_result_; |
| 101 } | 80 } |
| 102 | 81 |
| 103 void Send(const std::string& data) { | 82 void Send(const std::string& data) { |
| 104 write_buffer_ = | 83 write_buffer_ = |
| 105 new DrainableIOBuffer(new StringIOBuffer(data), data.length()); | 84 new DrainableIOBuffer(new StringIOBuffer(data), data.length()); |
| 106 Write(); | 85 Write(); |
| 107 } | 86 } |
| 108 | 87 |
| 109 bool Read(std::string* message, int expected_bytes) { | 88 bool Read(std::string* message, int expected_bytes) { |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 241 NOTREACHED(); | 220 NOTREACHED(); |
| 242 } | 221 } |
| 243 | 222 |
| 244 void OnClose(int connection_id) override { | 223 void OnClose(int connection_id) override { |
| 245 DCHECK(connection_map_.find(connection_id) != connection_map_.end()); | 224 DCHECK(connection_map_.find(connection_id) != connection_map_.end()); |
| 246 connection_map_[connection_id] = false; | 225 connection_map_[connection_id] = false; |
| 247 if (connection_id == quit_on_close_connection_) | 226 if (connection_id == quit_on_close_connection_) |
| 248 run_loop_quit_func_.Run(); | 227 run_loop_quit_func_.Run(); |
| 249 } | 228 } |
| 250 | 229 |
| 251 bool RunUntilRequestsReceived(size_t count) { | 230 void RunUntilRequestsReceived(size_t count) { |
| 252 quit_after_request_count_ = count; | 231 quit_after_request_count_ = count; |
| 253 if (requests_.size() == count) | 232 if (requests_.size() == count) |
| 254 return true; | 233 return; |
| 255 | 234 |
| 256 base::RunLoop run_loop; | 235 base::RunLoop run_loop; |
| 257 run_loop_quit_func_ = run_loop.QuitClosure(); | 236 run_loop_quit_func_ = run_loop.QuitClosure(); |
| 258 bool success = RunLoopWithTimeout(&run_loop); | 237 run_loop.Run(); |
| 259 run_loop_quit_func_.Reset(); | 238 run_loop_quit_func_.Reset(); |
| 260 return success; | |
| 261 } | 239 } |
| 262 | 240 |
| 263 bool RunUntilConnectionIdClosed(int connection_id) { | 241 void RunUntilConnectionIdClosed(int connection_id) { |
| 264 quit_on_close_connection_ = connection_id; | 242 quit_on_close_connection_ = connection_id; |
| 265 auto iter = connection_map_.find(connection_id); | 243 auto iter = connection_map_.find(connection_id); |
| 266 if (iter != connection_map_.end() && !iter->second) { | 244 if (iter != connection_map_.end() && !iter->second) { |
| 267 // Already disconnected. | 245 // Already disconnected. |
| 268 return true; | 246 return; |
| 269 } | 247 } |
| 270 | 248 |
| 271 base::RunLoop run_loop; | 249 base::RunLoop run_loop; |
| 272 run_loop_quit_func_ = run_loop.QuitClosure(); | 250 run_loop_quit_func_ = run_loop.QuitClosure(); |
| 273 bool success = RunLoopWithTimeout(&run_loop); | 251 run_loop.Run(); |
| 274 run_loop_quit_func_.Reset(); | 252 run_loop_quit_func_.Reset(); |
| 275 return success; | |
| 276 } | 253 } |
| 277 | 254 |
| 278 HttpServerRequestInfo GetRequest(size_t request_index) { | 255 HttpServerRequestInfo GetRequest(size_t request_index) { |
| 279 return requests_[request_index].first; | 256 return requests_[request_index].first; |
| 280 } | 257 } |
| 281 | 258 |
| 282 size_t num_requests() const { return requests_.size(); } | 259 size_t num_requests() const { return requests_.size(); } |
| 283 | 260 |
| 284 int GetConnectionId(size_t request_index) { | 261 int GetConnectionId(size_t request_index) { |
| 285 return requests_[request_index].second; | 262 return requests_[request_index].second; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 319 } | 296 } |
| 320 | 297 |
| 321 void OnWebSocketMessage(int connection_id, const std::string& data) override { | 298 void OnWebSocketMessage(int connection_id, const std::string& data) override { |
| 322 } | 299 } |
| 323 }; | 300 }; |
| 324 | 301 |
| 325 TEST_F(HttpServerTest, Request) { | 302 TEST_F(HttpServerTest, Request) { |
| 326 TestHttpClient client; | 303 TestHttpClient client; |
| 327 ASSERT_THAT(client.ConnectAndWait(server_address_), IsOk()); | 304 ASSERT_THAT(client.ConnectAndWait(server_address_), IsOk()); |
| 328 client.Send("GET /test HTTP/1.1\r\n\r\n"); | 305 client.Send("GET /test HTTP/1.1\r\n\r\n"); |
| 329 ASSERT_TRUE(RunUntilRequestsReceived(1)); | 306 RunUntilRequestsReceived(1); |
| 330 ASSERT_EQ("GET", GetRequest(0).method); | 307 ASSERT_EQ("GET", GetRequest(0).method); |
| 331 ASSERT_EQ("/test", GetRequest(0).path); | 308 ASSERT_EQ("/test", GetRequest(0).path); |
| 332 ASSERT_EQ("", GetRequest(0).data); | 309 ASSERT_EQ("", GetRequest(0).data); |
| 333 ASSERT_EQ(0u, GetRequest(0).headers.size()); | 310 ASSERT_EQ(0u, GetRequest(0).headers.size()); |
| 334 ASSERT_TRUE(base::StartsWith(GetRequest(0).peer.ToString(), "127.0.0.1", | 311 ASSERT_TRUE(base::StartsWith(GetRequest(0).peer.ToString(), "127.0.0.1", |
| 335 base::CompareCase::SENSITIVE)); | 312 base::CompareCase::SENSITIVE)); |
| 336 } | 313 } |
| 337 | 314 |
| 338 TEST_F(HttpServerTest, RequestBrokenTermination) { | 315 TEST_F(HttpServerTest, RequestBrokenTermination) { |
| 339 TestHttpClient client; | 316 TestHttpClient client; |
| 340 ASSERT_THAT(client.ConnectAndWait(server_address_), IsOk()); | 317 ASSERT_THAT(client.ConnectAndWait(server_address_), IsOk()); |
| 341 client.Send("GET /test HTTP/1.1\r\n\r)"); | 318 client.Send("GET /test HTTP/1.1\r\n\r)"); |
| 342 ASSERT_TRUE(RunUntilConnectionIdClosed(1)); | 319 RunUntilConnectionIdClosed(1); |
| 343 EXPECT_EQ(0u, num_requests()); | 320 EXPECT_EQ(0u, num_requests()); |
| 344 client.ExpectUsedThenDisconnectedWithNoData(); | 321 client.ExpectUsedThenDisconnectedWithNoData(); |
| 345 } | 322 } |
| 346 | 323 |
| 347 TEST_F(HttpServerTest, RequestWithHeaders) { | 324 TEST_F(HttpServerTest, RequestWithHeaders) { |
| 348 TestHttpClient client; | 325 TestHttpClient client; |
| 349 ASSERT_THAT(client.ConnectAndWait(server_address_), IsOk()); | 326 ASSERT_THAT(client.ConnectAndWait(server_address_), IsOk()); |
| 350 const char* const kHeaders[][3] = { | 327 const char* const kHeaders[][3] = { |
| 351 {"Header", ": ", "1"}, | 328 {"Header", ": ", "1"}, |
| 352 {"HeaderWithNoWhitespace", ":", "1"}, | 329 {"HeaderWithNoWhitespace", ":", "1"}, |
| 353 {"HeaderWithWhitespace", " : \t ", "1 1 1 \t "}, | 330 {"HeaderWithWhitespace", " : \t ", "1 1 1 \t "}, |
| 354 {"HeaderWithColon", ": ", "1:1"}, | 331 {"HeaderWithColon", ": ", "1:1"}, |
| 355 {"EmptyHeader", ":", ""}, | 332 {"EmptyHeader", ":", ""}, |
| 356 {"EmptyHeaderWithWhitespace", ": \t ", ""}, | 333 {"EmptyHeaderWithWhitespace", ": \t ", ""}, |
| 357 {"HeaderWithNonASCII", ": ", "\xf7"}, | 334 {"HeaderWithNonASCII", ": ", "\xf7"}, |
| 358 }; | 335 }; |
| 359 std::string headers; | 336 std::string headers; |
| 360 for (size_t i = 0; i < arraysize(kHeaders); ++i) { | 337 for (size_t i = 0; i < arraysize(kHeaders); ++i) { |
| 361 headers += | 338 headers += |
| 362 std::string(kHeaders[i][0]) + kHeaders[i][1] + kHeaders[i][2] + "\r\n"; | 339 std::string(kHeaders[i][0]) + kHeaders[i][1] + kHeaders[i][2] + "\r\n"; |
| 363 } | 340 } |
| 364 | 341 |
| 365 client.Send("GET /test HTTP/1.1\r\n" + headers + "\r\n"); | 342 client.Send("GET /test HTTP/1.1\r\n" + headers + "\r\n"); |
| 366 ASSERT_TRUE(RunUntilRequestsReceived(1)); | 343 RunUntilRequestsReceived(1); |
| 367 ASSERT_EQ("", GetRequest(0).data); | 344 ASSERT_EQ("", GetRequest(0).data); |
| 368 | 345 |
| 369 for (size_t i = 0; i < arraysize(kHeaders); ++i) { | 346 for (size_t i = 0; i < arraysize(kHeaders); ++i) { |
| 370 std::string field = base::ToLowerASCII(std::string(kHeaders[i][0])); | 347 std::string field = base::ToLowerASCII(std::string(kHeaders[i][0])); |
| 371 std::string value = kHeaders[i][2]; | 348 std::string value = kHeaders[i][2]; |
| 372 ASSERT_EQ(1u, GetRequest(0).headers.count(field)) << field; | 349 ASSERT_EQ(1u, GetRequest(0).headers.count(field)) << field; |
| 373 ASSERT_EQ(value, GetRequest(0).headers[field]) << kHeaders[i][0]; | 350 ASSERT_EQ(value, GetRequest(0).headers[field]) << kHeaders[i][0]; |
| 374 } | 351 } |
| 375 } | 352 } |
| 376 | 353 |
| 377 TEST_F(HttpServerTest, RequestWithDuplicateHeaders) { | 354 TEST_F(HttpServerTest, RequestWithDuplicateHeaders) { |
| 378 TestHttpClient client; | 355 TestHttpClient client; |
| 379 ASSERT_THAT(client.ConnectAndWait(server_address_), IsOk()); | 356 ASSERT_THAT(client.ConnectAndWait(server_address_), IsOk()); |
| 380 const char* const kHeaders[][3] = { | 357 const char* const kHeaders[][3] = { |
| 381 {"FirstHeader", ": ", "1"}, | 358 {"FirstHeader", ": ", "1"}, |
| 382 {"DuplicateHeader", ": ", "2"}, | 359 {"DuplicateHeader", ": ", "2"}, |
| 383 {"MiddleHeader", ": ", "3"}, | 360 {"MiddleHeader", ": ", "3"}, |
| 384 {"DuplicateHeader", ": ", "4"}, | 361 {"DuplicateHeader", ": ", "4"}, |
| 385 {"LastHeader", ": ", "5"}, | 362 {"LastHeader", ": ", "5"}, |
| 386 }; | 363 }; |
| 387 std::string headers; | 364 std::string headers; |
| 388 for (size_t i = 0; i < arraysize(kHeaders); ++i) { | 365 for (size_t i = 0; i < arraysize(kHeaders); ++i) { |
| 389 headers += | 366 headers += |
| 390 std::string(kHeaders[i][0]) + kHeaders[i][1] + kHeaders[i][2] + "\r\n"; | 367 std::string(kHeaders[i][0]) + kHeaders[i][1] + kHeaders[i][2] + "\r\n"; |
| 391 } | 368 } |
| 392 | 369 |
| 393 client.Send("GET /test HTTP/1.1\r\n" + headers + "\r\n"); | 370 client.Send("GET /test HTTP/1.1\r\n" + headers + "\r\n"); |
| 394 ASSERT_TRUE(RunUntilRequestsReceived(1)); | 371 RunUntilRequestsReceived(1); |
| 395 ASSERT_EQ("", GetRequest(0).data); | 372 ASSERT_EQ("", GetRequest(0).data); |
| 396 | 373 |
| 397 for (size_t i = 0; i < arraysize(kHeaders); ++i) { | 374 for (size_t i = 0; i < arraysize(kHeaders); ++i) { |
| 398 std::string field = base::ToLowerASCII(std::string(kHeaders[i][0])); | 375 std::string field = base::ToLowerASCII(std::string(kHeaders[i][0])); |
| 399 std::string value = (field == "duplicateheader") ? "2,4" : kHeaders[i][2]; | 376 std::string value = (field == "duplicateheader") ? "2,4" : kHeaders[i][2]; |
| 400 ASSERT_EQ(1u, GetRequest(0).headers.count(field)) << field; | 377 ASSERT_EQ(1u, GetRequest(0).headers.count(field)) << field; |
| 401 ASSERT_EQ(value, GetRequest(0).headers[field]) << kHeaders[i][0]; | 378 ASSERT_EQ(value, GetRequest(0).headers[field]) << kHeaders[i][0]; |
| 402 } | 379 } |
| 403 } | 380 } |
| 404 | 381 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 415 "EmptyHeader:", | 392 "EmptyHeader:", |
| 416 "EmptyHeaderWithWhitespace: \t ", | 393 "EmptyHeaderWithWhitespace: \t ", |
| 417 "HeaderWithNonASCII: \xf7", | 394 "HeaderWithNonASCII: \xf7", |
| 418 }; | 395 }; |
| 419 std::string headers; | 396 std::string headers; |
| 420 for (size_t i = 0; i < arraysize(kHeaders); ++i) { | 397 for (size_t i = 0; i < arraysize(kHeaders); ++i) { |
| 421 headers += std::string(kHeaders[i]) + "\r\n"; | 398 headers += std::string(kHeaders[i]) + "\r\n"; |
| 422 } | 399 } |
| 423 | 400 |
| 424 client.Send("GET /test HTTP/1.1\r\n" + headers + "\r\n"); | 401 client.Send("GET /test HTTP/1.1\r\n" + headers + "\r\n"); |
| 425 ASSERT_TRUE(RunUntilRequestsReceived(1)); | 402 RunUntilRequestsReceived(1); |
| 426 ASSERT_EQ("", GetRequest(0).data); | 403 ASSERT_EQ("", GetRequest(0).data); |
| 427 | 404 |
| 428 ASSERT_TRUE(GetRequest(0).HasHeaderValue("header", "abcd")); | 405 ASSERT_TRUE(GetRequest(0).HasHeaderValue("header", "abcd")); |
| 429 ASSERT_FALSE(GetRequest(0).HasHeaderValue("header", "bc")); | 406 ASSERT_FALSE(GetRequest(0).HasHeaderValue("header", "bc")); |
| 430 ASSERT_TRUE(GetRequest(0).HasHeaderValue("headerwithnowhitespace", "e")); | 407 ASSERT_TRUE(GetRequest(0).HasHeaderValue("headerwithnowhitespace", "e")); |
| 431 ASSERT_TRUE(GetRequest(0).HasHeaderValue("headerwithwhitespace", "f")); | 408 ASSERT_TRUE(GetRequest(0).HasHeaderValue("headerwithwhitespace", "f")); |
| 432 ASSERT_TRUE(GetRequest(0).HasHeaderValue("duplicateheader", "g")); | 409 ASSERT_TRUE(GetRequest(0).HasHeaderValue("duplicateheader", "g")); |
| 433 ASSERT_TRUE(GetRequest(0).HasHeaderValue("headerwithcomma", "h")); | 410 ASSERT_TRUE(GetRequest(0).HasHeaderValue("headerwithcomma", "h")); |
| 434 ASSERT_TRUE(GetRequest(0).HasHeaderValue("headerwithcomma", "i")); | 411 ASSERT_TRUE(GetRequest(0).HasHeaderValue("headerwithcomma", "i")); |
| 435 ASSERT_TRUE(GetRequest(0).HasHeaderValue("headerwithcomma", "j")); | 412 ASSERT_TRUE(GetRequest(0).HasHeaderValue("headerwithcomma", "j")); |
| 436 ASSERT_TRUE(GetRequest(0).HasHeaderValue("duplicateheader", "k")); | 413 ASSERT_TRUE(GetRequest(0).HasHeaderValue("duplicateheader", "k")); |
| 437 ASSERT_FALSE(GetRequest(0).HasHeaderValue("emptyheader", "x")); | 414 ASSERT_FALSE(GetRequest(0).HasHeaderValue("emptyheader", "x")); |
| 438 ASSERT_FALSE(GetRequest(0).HasHeaderValue("emptyheaderwithwhitespace", "x")); | 415 ASSERT_FALSE(GetRequest(0).HasHeaderValue("emptyheaderwithwhitespace", "x")); |
| 439 ASSERT_TRUE(GetRequest(0).HasHeaderValue("headerwithnonascii", "\xf7")); | 416 ASSERT_TRUE(GetRequest(0).HasHeaderValue("headerwithnonascii", "\xf7")); |
| 440 } | 417 } |
| 441 | 418 |
| 442 TEST_F(HttpServerTest, RequestWithBody) { | 419 TEST_F(HttpServerTest, RequestWithBody) { |
| 443 TestHttpClient client; | 420 TestHttpClient client; |
| 444 ASSERT_THAT(client.ConnectAndWait(server_address_), IsOk()); | 421 ASSERT_THAT(client.ConnectAndWait(server_address_), IsOk()); |
| 445 std::string body = "a" + std::string(1 << 10, 'b') + "c"; | 422 std::string body = "a" + std::string(1 << 10, 'b') + "c"; |
| 446 client.Send(base::StringPrintf( | 423 client.Send(base::StringPrintf( |
| 447 "GET /test HTTP/1.1\r\n" | 424 "GET /test HTTP/1.1\r\n" |
| 448 "SomeHeader: 1\r\n" | 425 "SomeHeader: 1\r\n" |
| 449 "Content-Length: %" PRIuS "\r\n\r\n%s", | 426 "Content-Length: %" PRIuS "\r\n\r\n%s", |
| 450 body.length(), | 427 body.length(), |
| 451 body.c_str())); | 428 body.c_str())); |
| 452 ASSERT_TRUE(RunUntilRequestsReceived(1)); | 429 RunUntilRequestsReceived(1); |
| 453 ASSERT_EQ(2u, GetRequest(0).headers.size()); | 430 ASSERT_EQ(2u, GetRequest(0).headers.size()); |
| 454 ASSERT_EQ(body.length(), GetRequest(0).data.length()); | 431 ASSERT_EQ(body.length(), GetRequest(0).data.length()); |
| 455 ASSERT_EQ('a', body[0]); | 432 ASSERT_EQ('a', body[0]); |
| 456 ASSERT_EQ('c', *body.rbegin()); | 433 ASSERT_EQ('c', *body.rbegin()); |
| 457 } | 434 } |
| 458 | 435 |
| 459 TEST_F(WebSocketTest, RequestWebSocket) { | 436 TEST_F(WebSocketTest, RequestWebSocket) { |
| 460 TestHttpClient client; | 437 TestHttpClient client; |
| 461 ASSERT_THAT(client.ConnectAndWait(server_address_), IsOk()); | 438 ASSERT_THAT(client.ConnectAndWait(server_address_), IsOk()); |
| 462 client.Send( | 439 client.Send( |
| 463 "GET /test HTTP/1.1\r\n" | 440 "GET /test HTTP/1.1\r\n" |
| 464 "Upgrade: WebSocket\r\n" | 441 "Upgrade: WebSocket\r\n" |
| 465 "Connection: SomethingElse, Upgrade\r\n" | 442 "Connection: SomethingElse, Upgrade\r\n" |
| 466 "Sec-WebSocket-Version: 8\r\n" | 443 "Sec-WebSocket-Version: 8\r\n" |
| 467 "Sec-WebSocket-Key: key\r\n" | 444 "Sec-WebSocket-Key: key\r\n" |
| 468 "\r\n"); | 445 "\r\n"); |
| 469 ASSERT_TRUE(RunUntilRequestsReceived(1)); | 446 RunUntilRequestsReceived(1); |
| 470 } | 447 } |
| 471 | 448 |
| 472 TEST_F(WebSocketTest, RequestWebSocketTrailingJunk) { | 449 TEST_F(WebSocketTest, RequestWebSocketTrailingJunk) { |
| 473 TestHttpClient client; | 450 TestHttpClient client; |
| 474 ASSERT_THAT(client.ConnectAndWait(server_address_), IsOk()); | 451 ASSERT_THAT(client.ConnectAndWait(server_address_), IsOk()); |
| 475 client.Send( | 452 client.Send( |
| 476 "GET /test HTTP/1.1\r\n" | 453 "GET /test HTTP/1.1\r\n" |
| 477 "Upgrade: WebSocket\r\n" | 454 "Upgrade: WebSocket\r\n" |
| 478 "Connection: SomethingElse, Upgrade\r\n" | 455 "Connection: SomethingElse, Upgrade\r\n" |
| 479 "Sec-WebSocket-Version: 8\r\n" | 456 "Sec-WebSocket-Version: 8\r\n" |
| 480 "Sec-WebSocket-Key: key\r\n" | 457 "Sec-WebSocket-Key: key\r\n" |
| 481 "\r\nHello? Anyone"); | 458 "\r\nHello? Anyone"); |
| 482 ASSERT_TRUE(RunUntilConnectionIdClosed(1)); | 459 RunUntilConnectionIdClosed(1); |
| 483 client.ExpectUsedThenDisconnectedWithNoData(); | 460 client.ExpectUsedThenDisconnectedWithNoData(); |
| 484 } | 461 } |
| 485 | 462 |
| 486 TEST_F(HttpServerTest, RequestWithTooLargeBody) { | 463 TEST_F(HttpServerTest, RequestWithTooLargeBody) { |
| 487 class TestURLFetcherDelegate : public URLFetcherDelegate { | 464 class TestURLFetcherDelegate : public URLFetcherDelegate { |
| 488 public: | 465 public: |
| 489 TestURLFetcherDelegate(const base::Closure& quit_loop_func) | 466 TestURLFetcherDelegate(const base::Closure& quit_loop_func) |
| 490 : quit_loop_func_(quit_loop_func) {} | 467 : quit_loop_func_(quit_loop_func) {} |
| 491 ~TestURLFetcherDelegate() override {} | 468 ~TestURLFetcherDelegate() override {} |
| 492 | 469 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 506 new TestURLRequestContextGetter(base::ThreadTaskRunnerHandle::Get())); | 483 new TestURLRequestContextGetter(base::ThreadTaskRunnerHandle::Get())); |
| 507 std::unique_ptr<URLFetcher> fetcher = URLFetcher::Create( | 484 std::unique_ptr<URLFetcher> fetcher = URLFetcher::Create( |
| 508 GURL(base::StringPrintf("http://127.0.0.1:%d/test", | 485 GURL(base::StringPrintf("http://127.0.0.1:%d/test", |
| 509 server_address_.port())), | 486 server_address_.port())), |
| 510 URLFetcher::GET, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS); | 487 URLFetcher::GET, &delegate, TRAFFIC_ANNOTATION_FOR_TESTS); |
| 511 fetcher->SetRequestContext(request_context_getter.get()); | 488 fetcher->SetRequestContext(request_context_getter.get()); |
| 512 fetcher->AddExtraRequestHeader( | 489 fetcher->AddExtraRequestHeader( |
| 513 base::StringPrintf("content-length:%d", 1 << 30)); | 490 base::StringPrintf("content-length:%d", 1 << 30)); |
| 514 fetcher->Start(); | 491 fetcher->Start(); |
| 515 | 492 |
| 516 ASSERT_TRUE(RunLoopWithTimeout(&run_loop)); | 493 run_loop.Run(); |
| 517 ASSERT_EQ(0u, requests_.size()); | 494 ASSERT_EQ(0u, requests_.size()); |
| 518 } | 495 } |
| 519 | 496 |
| 520 TEST_F(HttpServerTest, Send200) { | 497 TEST_F(HttpServerTest, Send200) { |
| 521 TestHttpClient client; | 498 TestHttpClient client; |
| 522 ASSERT_THAT(client.ConnectAndWait(server_address_), IsOk()); | 499 ASSERT_THAT(client.ConnectAndWait(server_address_), IsOk()); |
| 523 client.Send("GET /test HTTP/1.1\r\n\r\n"); | 500 client.Send("GET /test HTTP/1.1\r\n\r\n"); |
| 524 ASSERT_TRUE(RunUntilRequestsReceived(1)); | 501 RunUntilRequestsReceived(1); |
| 525 server_->Send200(GetConnectionId(0), "Response!", "text/plain"); | 502 server_->Send200(GetConnectionId(0), "Response!", "text/plain"); |
| 526 | 503 |
| 527 std::string response; | 504 std::string response; |
| 528 ASSERT_TRUE(client.ReadResponse(&response)); | 505 ASSERT_TRUE(client.ReadResponse(&response)); |
| 529 ASSERT_TRUE(base::StartsWith(response, "HTTP/1.1 200 OK", | 506 ASSERT_TRUE(base::StartsWith(response, "HTTP/1.1 200 OK", |
| 530 base::CompareCase::SENSITIVE)); | 507 base::CompareCase::SENSITIVE)); |
| 531 ASSERT_TRUE( | 508 ASSERT_TRUE( |
| 532 base::EndsWith(response, "Response!", base::CompareCase::SENSITIVE)); | 509 base::EndsWith(response, "Response!", base::CompareCase::SENSITIVE)); |
| 533 } | 510 } |
| 534 | 511 |
| 535 TEST_F(HttpServerTest, SendRaw) { | 512 TEST_F(HttpServerTest, SendRaw) { |
| 536 TestHttpClient client; | 513 TestHttpClient client; |
| 537 ASSERT_THAT(client.ConnectAndWait(server_address_), IsOk()); | 514 ASSERT_THAT(client.ConnectAndWait(server_address_), IsOk()); |
| 538 client.Send("GET /test HTTP/1.1\r\n\r\n"); | 515 client.Send("GET /test HTTP/1.1\r\n\r\n"); |
| 539 ASSERT_TRUE(RunUntilRequestsReceived(1)); | 516 RunUntilRequestsReceived(1); |
| 540 server_->SendRaw(GetConnectionId(0), "Raw Data "); | 517 server_->SendRaw(GetConnectionId(0), "Raw Data "); |
| 541 server_->SendRaw(GetConnectionId(0), "More Data"); | 518 server_->SendRaw(GetConnectionId(0), "More Data"); |
| 542 server_->SendRaw(GetConnectionId(0), "Third Piece of Data"); | 519 server_->SendRaw(GetConnectionId(0), "Third Piece of Data"); |
| 543 | 520 |
| 544 const std::string expected_response("Raw Data More DataThird Piece of Data"); | 521 const std::string expected_response("Raw Data More DataThird Piece of Data"); |
| 545 std::string response; | 522 std::string response; |
| 546 ASSERT_TRUE(client.Read(&response, expected_response.length())); | 523 ASSERT_TRUE(client.Read(&response, expected_response.length())); |
| 547 ASSERT_EQ(expected_response, response); | 524 ASSERT_EQ(expected_response, response); |
| 548 } | 525 } |
| 549 | 526 |
| 550 TEST_F(HttpServerTest, WrongProtocolRequest) { | 527 TEST_F(HttpServerTest, WrongProtocolRequest) { |
| 551 const char* const kBadProtocolRequests[] = { | 528 const char* const kBadProtocolRequests[] = { |
| 552 "GET /test HTTP/1.0\r\n\r\n", | 529 "GET /test HTTP/1.0\r\n\r\n", |
| 553 "GET /test foo\r\n\r\n", | 530 "GET /test foo\r\n\r\n", |
| 554 "GET /test \r\n\r\n", | 531 "GET /test \r\n\r\n", |
| 555 }; | 532 }; |
| 556 | 533 |
| 557 for (size_t i = 0; i < arraysize(kBadProtocolRequests); ++i) { | 534 for (size_t i = 0; i < arraysize(kBadProtocolRequests); ++i) { |
| 558 TestHttpClient client; | 535 TestHttpClient client; |
| 559 ASSERT_THAT(client.ConnectAndWait(server_address_), IsOk()); | 536 ASSERT_THAT(client.ConnectAndWait(server_address_), IsOk()); |
| 560 | 537 |
| 561 client.Send(kBadProtocolRequests[i]); | 538 client.Send(kBadProtocolRequests[i]); |
| 562 ASSERT_FALSE(RunUntilRequestsReceived(1)); | 539 client.ExpectUsedThenDisconnectedWithNoData(); |
| 563 | 540 |
| 564 // Assert that the delegate was updated properly. | 541 // Assert that the delegate was updated properly. |
| 565 ASSERT_EQ(1u, connection_map().size()); | 542 ASSERT_EQ(1u, connection_map().size()); |
| 566 ASSERT_FALSE(connection_map().begin()->second); | 543 ASSERT_FALSE(connection_map().begin()->second); |
| 567 | 544 EXPECT_EQ(0ul, requests_.size()); |
| 568 client.ExpectUsedThenDisconnectedWithNoData(); | |
| 569 | 545 |
| 570 // Reset the state of the connection map. | 546 // Reset the state of the connection map. |
| 571 connection_map().clear(); | 547 connection_map().clear(); |
| 572 } | 548 } |
| 573 } | 549 } |
| 574 | 550 |
| 575 class MockStreamSocket : public StreamSocket { | 551 class MockStreamSocket : public StreamSocket { |
| 576 public: | 552 public: |
| 577 MockStreamSocket() | 553 MockStreamSocket() |
| 578 : connected_(true), | 554 : connected_(true), |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 693 // The idea behind this test is that requests with or without bodies should | 669 // The idea behind this test is that requests with or without bodies should |
| 694 // not break parsing of the next request. | 670 // not break parsing of the next request. |
| 695 TestHttpClient client; | 671 TestHttpClient client; |
| 696 ASSERT_THAT(client.ConnectAndWait(server_address_), IsOk()); | 672 ASSERT_THAT(client.ConnectAndWait(server_address_), IsOk()); |
| 697 std::string body = "body"; | 673 std::string body = "body"; |
| 698 client.Send(base::StringPrintf( | 674 client.Send(base::StringPrintf( |
| 699 "GET /test HTTP/1.1\r\n" | 675 "GET /test HTTP/1.1\r\n" |
| 700 "Content-Length: %" PRIuS "\r\n\r\n%s", | 676 "Content-Length: %" PRIuS "\r\n\r\n%s", |
| 701 body.length(), | 677 body.length(), |
| 702 body.c_str())); | 678 body.c_str())); |
| 703 ASSERT_TRUE(RunUntilRequestsReceived(1)); | 679 RunUntilRequestsReceived(1); |
| 704 ASSERT_EQ(body, GetRequest(0).data); | 680 ASSERT_EQ(body, GetRequest(0).data); |
| 705 | 681 |
| 706 int client_connection_id = GetConnectionId(0); | 682 int client_connection_id = GetConnectionId(0); |
| 707 server_->Send200(client_connection_id, "Content for /test", "text/plain"); | 683 server_->Send200(client_connection_id, "Content for /test", "text/plain"); |
| 708 std::string response1; | 684 std::string response1; |
| 709 ASSERT_TRUE(client.ReadResponse(&response1)); | 685 ASSERT_TRUE(client.ReadResponse(&response1)); |
| 710 ASSERT_TRUE(base::StartsWith(response1, "HTTP/1.1 200 OK", | 686 ASSERT_TRUE(base::StartsWith(response1, "HTTP/1.1 200 OK", |
| 711 base::CompareCase::SENSITIVE)); | 687 base::CompareCase::SENSITIVE)); |
| 712 ASSERT_TRUE(base::EndsWith(response1, "Content for /test", | 688 ASSERT_TRUE(base::EndsWith(response1, "Content for /test", |
| 713 base::CompareCase::SENSITIVE)); | 689 base::CompareCase::SENSITIVE)); |
| 714 | 690 |
| 715 client.Send("GET /test2 HTTP/1.1\r\n\r\n"); | 691 client.Send("GET /test2 HTTP/1.1\r\n\r\n"); |
| 716 ASSERT_TRUE(RunUntilRequestsReceived(2)); | 692 RunUntilRequestsReceived(2); |
| 717 ASSERT_EQ("/test2", GetRequest(1).path); | 693 ASSERT_EQ("/test2", GetRequest(1).path); |
| 718 | 694 |
| 719 ASSERT_EQ(client_connection_id, GetConnectionId(1)); | 695 ASSERT_EQ(client_connection_id, GetConnectionId(1)); |
| 720 server_->Send404(client_connection_id); | 696 server_->Send404(client_connection_id); |
| 721 std::string response2; | 697 std::string response2; |
| 722 ASSERT_TRUE(client.ReadResponse(&response2)); | 698 ASSERT_TRUE(client.ReadResponse(&response2)); |
| 723 ASSERT_TRUE(base::StartsWith(response2, "HTTP/1.1 404 Not Found", | 699 ASSERT_TRUE(base::StartsWith(response2, "HTTP/1.1 404 Not Found", |
| 724 base::CompareCase::SENSITIVE)); | 700 base::CompareCase::SENSITIVE)); |
| 725 | 701 |
| 726 client.Send("GET /test3 HTTP/1.1\r\n\r\n"); | 702 client.Send("GET /test3 HTTP/1.1\r\n\r\n"); |
| 727 ASSERT_TRUE(RunUntilRequestsReceived(3)); | 703 RunUntilRequestsReceived(3); |
| 728 ASSERT_EQ("/test3", GetRequest(2).path); | 704 ASSERT_EQ("/test3", GetRequest(2).path); |
| 729 | 705 |
| 730 ASSERT_EQ(client_connection_id, GetConnectionId(2)); | 706 ASSERT_EQ(client_connection_id, GetConnectionId(2)); |
| 731 server_->Send200(client_connection_id, "Content for /test3", "text/plain"); | 707 server_->Send200(client_connection_id, "Content for /test3", "text/plain"); |
| 732 std::string response3; | 708 std::string response3; |
| 733 ASSERT_TRUE(client.ReadResponse(&response3)); | 709 ASSERT_TRUE(client.ReadResponse(&response3)); |
| 734 ASSERT_TRUE(base::StartsWith(response3, "HTTP/1.1 200 OK", | 710 ASSERT_TRUE(base::StartsWith(response3, "HTTP/1.1 200 OK", |
| 735 base::CompareCase::SENSITIVE)); | 711 base::CompareCase::SENSITIVE)); |
| 736 ASSERT_TRUE(base::EndsWith(response3, "Content for /test3", | 712 ASSERT_TRUE(base::EndsWith(response3, "Content for /test3", |
| 737 base::CompareCase::SENSITIVE)); | 713 base::CompareCase::SENSITIVE)); |
| 738 } | 714 } |
| 739 | 715 |
| 740 class CloseOnConnectHttpServerTest : public HttpServerTest { | 716 class CloseOnConnectHttpServerTest : public HttpServerTest { |
| 741 public: | 717 public: |
| 742 void OnConnect(int connection_id) override { | 718 void OnConnect(int connection_id) override { |
| 743 HttpServerTest::OnConnect(connection_id); | 719 HttpServerTest::OnConnect(connection_id); |
| 744 connection_ids_.push_back(connection_id); | 720 connection_ids_.push_back(connection_id); |
| 745 server_->Close(connection_id); | 721 server_->Close(connection_id); |
| 746 } | 722 } |
| 747 | 723 |
| 748 protected: | 724 protected: |
| 749 std::vector<int> connection_ids_; | 725 std::vector<int> connection_ids_; |
| 750 }; | 726 }; |
| 751 | 727 |
| 752 TEST_F(CloseOnConnectHttpServerTest, ServerImmediatelyClosesConnection) { | 728 TEST_F(CloseOnConnectHttpServerTest, ServerImmediatelyClosesConnection) { |
| 753 TestHttpClient client; | 729 TestHttpClient client; |
| 754 ASSERT_THAT(client.ConnectAndWait(server_address_), IsOk()); | 730 ASSERT_THAT(client.ConnectAndWait(server_address_), IsOk()); |
| 755 client.Send("GET / HTTP/1.1\r\n\r\n"); | 731 client.Send("GET / HTTP/1.1\r\n\r\n"); |
|
mmenke
2017/06/13 17:29:26
The fact that this always succeeds seems a bit wei
| |
| 756 ASSERT_FALSE(RunUntilRequestsReceived(1)); | 732 |
| 757 ASSERT_EQ(1ul, connection_ids_.size()); | 733 // The server should close the socket without responding. |
| 758 ASSERT_EQ(0ul, requests_.size()); | 734 client.ExpectUsedThenDisconnectedWithNoData(); |
| 735 | |
| 736 // Run any tasks the TestServer posted. | |
| 737 base::RunLoop().RunUntilIdle(); | |
| 738 | |
| 739 EXPECT_EQ(1ul, connection_ids_.size()); | |
| 740 // OnHttpRequest() should never have been called, since the connection was | |
| 741 // closed without reading from it. | |
| 742 EXPECT_EQ(0ul, requests_.size()); | |
| 759 } | 743 } |
| 760 | 744 |
| 761 } // namespace | 745 } // namespace |
| 762 | 746 |
| 763 } // namespace net | 747 } // namespace net |
| OLD | NEW |