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 <algorithm> | 5 #include <algorithm> |
6 #include <utility> | 6 #include <utility> |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 int connect_result_; | 174 int connect_result_; |
175 }; | 175 }; |
176 | 176 |
177 } // namespace | 177 } // namespace |
178 | 178 |
179 class HttpServerTest : public testing::Test, | 179 class HttpServerTest : public testing::Test, |
180 public HttpServer::Delegate { | 180 public HttpServer::Delegate { |
181 public: | 181 public: |
182 HttpServerTest() : quit_after_request_count_(0) {} | 182 HttpServerTest() : quit_after_request_count_(0) {} |
183 | 183 |
184 virtual void SetUp() OVERRIDE { | 184 virtual void SetUp() override { |
185 scoped_ptr<ServerSocket> server_socket( | 185 scoped_ptr<ServerSocket> server_socket( |
186 new TCPServerSocket(NULL, net::NetLog::Source())); | 186 new TCPServerSocket(NULL, net::NetLog::Source())); |
187 server_socket->ListenWithAddressAndPort("127.0.0.1", 0, 1); | 187 server_socket->ListenWithAddressAndPort("127.0.0.1", 0, 1); |
188 server_.reset(new HttpServer(server_socket.Pass(), this)); | 188 server_.reset(new HttpServer(server_socket.Pass(), this)); |
189 ASSERT_EQ(OK, server_->GetLocalAddress(&server_address_)); | 189 ASSERT_EQ(OK, server_->GetLocalAddress(&server_address_)); |
190 } | 190 } |
191 | 191 |
192 virtual void OnConnect(int connection_id) OVERRIDE {} | 192 virtual void OnConnect(int connection_id) override {} |
193 | 193 |
194 virtual void OnHttpRequest(int connection_id, | 194 virtual void OnHttpRequest(int connection_id, |
195 const HttpServerRequestInfo& info) OVERRIDE { | 195 const HttpServerRequestInfo& info) override { |
196 requests_.push_back(std::make_pair(info, connection_id)); | 196 requests_.push_back(std::make_pair(info, connection_id)); |
197 if (requests_.size() == quit_after_request_count_) | 197 if (requests_.size() == quit_after_request_count_) |
198 run_loop_quit_func_.Run(); | 198 run_loop_quit_func_.Run(); |
199 } | 199 } |
200 | 200 |
201 virtual void OnWebSocketRequest(int connection_id, | 201 virtual void OnWebSocketRequest(int connection_id, |
202 const HttpServerRequestInfo& info) OVERRIDE { | 202 const HttpServerRequestInfo& info) override { |
203 NOTREACHED(); | 203 NOTREACHED(); |
204 } | 204 } |
205 | 205 |
206 virtual void OnWebSocketMessage(int connection_id, | 206 virtual void OnWebSocketMessage(int connection_id, |
207 const std::string& data) OVERRIDE { | 207 const std::string& data) override { |
208 NOTREACHED(); | 208 NOTREACHED(); |
209 } | 209 } |
210 | 210 |
211 virtual void OnClose(int connection_id) OVERRIDE {} | 211 virtual void OnClose(int connection_id) override {} |
212 | 212 |
213 bool RunUntilRequestsReceived(size_t count) { | 213 bool RunUntilRequestsReceived(size_t count) { |
214 quit_after_request_count_ = count; | 214 quit_after_request_count_ = count; |
215 if (requests_.size() == count) | 215 if (requests_.size() == count) |
216 return true; | 216 return true; |
217 | 217 |
218 base::RunLoop run_loop; | 218 base::RunLoop run_loop; |
219 run_loop_quit_func_ = run_loop.QuitClosure(); | 219 run_loop_quit_func_ = run_loop.QuitClosure(); |
220 bool success = RunLoopWithTimeout(&run_loop); | 220 bool success = RunLoopWithTimeout(&run_loop); |
221 run_loop_quit_func_.Reset(); | 221 run_loop_quit_func_.Reset(); |
(...skipping 20 matching lines...) Expand all Loading... |
242 std::vector<std::pair<HttpServerRequestInfo, int> > requests_; | 242 std::vector<std::pair<HttpServerRequestInfo, int> > requests_; |
243 | 243 |
244 private: | 244 private: |
245 size_t quit_after_request_count_; | 245 size_t quit_after_request_count_; |
246 }; | 246 }; |
247 | 247 |
248 namespace { | 248 namespace { |
249 | 249 |
250 class WebSocketTest : public HttpServerTest { | 250 class WebSocketTest : public HttpServerTest { |
251 virtual void OnHttpRequest(int connection_id, | 251 virtual void OnHttpRequest(int connection_id, |
252 const HttpServerRequestInfo& info) OVERRIDE { | 252 const HttpServerRequestInfo& info) override { |
253 NOTREACHED(); | 253 NOTREACHED(); |
254 } | 254 } |
255 | 255 |
256 virtual void OnWebSocketRequest(int connection_id, | 256 virtual void OnWebSocketRequest(int connection_id, |
257 const HttpServerRequestInfo& info) OVERRIDE { | 257 const HttpServerRequestInfo& info) override { |
258 HttpServerTest::OnHttpRequest(connection_id, info); | 258 HttpServerTest::OnHttpRequest(connection_id, info); |
259 } | 259 } |
260 | 260 |
261 virtual void OnWebSocketMessage(int connection_id, | 261 virtual void OnWebSocketMessage(int connection_id, |
262 const std::string& data) OVERRIDE { | 262 const std::string& data) override { |
263 } | 263 } |
264 }; | 264 }; |
265 | 265 |
266 TEST_F(HttpServerTest, Request) { | 266 TEST_F(HttpServerTest, Request) { |
267 TestHttpClient client; | 267 TestHttpClient client; |
268 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); | 268 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); |
269 client.Send("GET /test HTTP/1.1\r\n\r\n"); | 269 client.Send("GET /test HTTP/1.1\r\n\r\n"); |
270 ASSERT_TRUE(RunUntilRequestsReceived(1)); | 270 ASSERT_TRUE(RunUntilRequestsReceived(1)); |
271 ASSERT_EQ("GET", GetRequest(0).method); | 271 ASSERT_EQ("GET", GetRequest(0).method); |
272 ASSERT_EQ("/test", GetRequest(0).path); | 272 ASSERT_EQ("/test", GetRequest(0).path); |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 ASSERT_TRUE(RunUntilRequestsReceived(1)); | 402 ASSERT_TRUE(RunUntilRequestsReceived(1)); |
403 } | 403 } |
404 | 404 |
405 TEST_F(HttpServerTest, RequestWithTooLargeBody) { | 405 TEST_F(HttpServerTest, RequestWithTooLargeBody) { |
406 class TestURLFetcherDelegate : public URLFetcherDelegate { | 406 class TestURLFetcherDelegate : public URLFetcherDelegate { |
407 public: | 407 public: |
408 TestURLFetcherDelegate(const base::Closure& quit_loop_func) | 408 TestURLFetcherDelegate(const base::Closure& quit_loop_func) |
409 : quit_loop_func_(quit_loop_func) {} | 409 : quit_loop_func_(quit_loop_func) {} |
410 virtual ~TestURLFetcherDelegate() {} | 410 virtual ~TestURLFetcherDelegate() {} |
411 | 411 |
412 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE { | 412 virtual void OnURLFetchComplete(const URLFetcher* source) override { |
413 EXPECT_EQ(HTTP_INTERNAL_SERVER_ERROR, source->GetResponseCode()); | 413 EXPECT_EQ(HTTP_INTERNAL_SERVER_ERROR, source->GetResponseCode()); |
414 quit_loop_func_.Run(); | 414 quit_loop_func_.Run(); |
415 } | 415 } |
416 | 416 |
417 private: | 417 private: |
418 base::Closure quit_loop_func_; | 418 base::Closure quit_loop_func_; |
419 }; | 419 }; |
420 | 420 |
421 base::RunLoop run_loop; | 421 base::RunLoop run_loop; |
422 TestURLFetcherDelegate delegate(run_loop.QuitClosure()); | 422 TestURLFetcherDelegate delegate(run_loop.QuitClosure()); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
466 } | 466 } |
467 | 467 |
468 class MockStreamSocket : public StreamSocket { | 468 class MockStreamSocket : public StreamSocket { |
469 public: | 469 public: |
470 MockStreamSocket() | 470 MockStreamSocket() |
471 : connected_(true), | 471 : connected_(true), |
472 read_buf_(NULL), | 472 read_buf_(NULL), |
473 read_buf_len_(0) {} | 473 read_buf_len_(0) {} |
474 | 474 |
475 // StreamSocket | 475 // StreamSocket |
476 virtual int Connect(const CompletionCallback& callback) OVERRIDE { | 476 virtual int Connect(const CompletionCallback& callback) override { |
477 return ERR_NOT_IMPLEMENTED; | 477 return ERR_NOT_IMPLEMENTED; |
478 } | 478 } |
479 virtual void Disconnect() OVERRIDE { | 479 virtual void Disconnect() override { |
480 connected_ = false; | 480 connected_ = false; |
481 if (!read_callback_.is_null()) { | 481 if (!read_callback_.is_null()) { |
482 read_buf_ = NULL; | 482 read_buf_ = NULL; |
483 read_buf_len_ = 0; | 483 read_buf_len_ = 0; |
484 base::ResetAndReturn(&read_callback_).Run(ERR_CONNECTION_CLOSED); | 484 base::ResetAndReturn(&read_callback_).Run(ERR_CONNECTION_CLOSED); |
485 } | 485 } |
486 } | 486 } |
487 virtual bool IsConnected() const OVERRIDE { return connected_; } | 487 virtual bool IsConnected() const override { return connected_; } |
488 virtual bool IsConnectedAndIdle() const OVERRIDE { return IsConnected(); } | 488 virtual bool IsConnectedAndIdle() const override { return IsConnected(); } |
489 virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE { | 489 virtual int GetPeerAddress(IPEndPoint* address) const override { |
490 return ERR_NOT_IMPLEMENTED; | 490 return ERR_NOT_IMPLEMENTED; |
491 } | 491 } |
492 virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE { | 492 virtual int GetLocalAddress(IPEndPoint* address) const override { |
493 return ERR_NOT_IMPLEMENTED; | 493 return ERR_NOT_IMPLEMENTED; |
494 } | 494 } |
495 virtual const BoundNetLog& NetLog() const OVERRIDE { return net_log_; } | 495 virtual const BoundNetLog& NetLog() const override { return net_log_; } |
496 virtual void SetSubresourceSpeculation() OVERRIDE {} | 496 virtual void SetSubresourceSpeculation() override {} |
497 virtual void SetOmniboxSpeculation() OVERRIDE {} | 497 virtual void SetOmniboxSpeculation() override {} |
498 virtual bool WasEverUsed() const OVERRIDE { return true; } | 498 virtual bool WasEverUsed() const override { return true; } |
499 virtual bool UsingTCPFastOpen() const OVERRIDE { return false; } | 499 virtual bool UsingTCPFastOpen() const override { return false; } |
500 virtual bool WasNpnNegotiated() const OVERRIDE { return false; } | 500 virtual bool WasNpnNegotiated() const override { return false; } |
501 virtual NextProto GetNegotiatedProtocol() const OVERRIDE { | 501 virtual NextProto GetNegotiatedProtocol() const override { |
502 return kProtoUnknown; | 502 return kProtoUnknown; |
503 } | 503 } |
504 virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE { return false; } | 504 virtual bool GetSSLInfo(SSLInfo* ssl_info) override { return false; } |
505 | 505 |
506 // Socket | 506 // Socket |
507 virtual int Read(IOBuffer* buf, int buf_len, | 507 virtual int Read(IOBuffer* buf, int buf_len, |
508 const CompletionCallback& callback) OVERRIDE { | 508 const CompletionCallback& callback) override { |
509 if (!connected_) { | 509 if (!connected_) { |
510 return ERR_SOCKET_NOT_CONNECTED; | 510 return ERR_SOCKET_NOT_CONNECTED; |
511 } | 511 } |
512 if (pending_read_data_.empty()) { | 512 if (pending_read_data_.empty()) { |
513 read_buf_ = buf; | 513 read_buf_ = buf; |
514 read_buf_len_ = buf_len; | 514 read_buf_len_ = buf_len; |
515 read_callback_ = callback; | 515 read_callback_ = callback; |
516 return ERR_IO_PENDING; | 516 return ERR_IO_PENDING; |
517 } | 517 } |
518 DCHECK_GT(buf_len, 0); | 518 DCHECK_GT(buf_len, 0); |
519 int read_len = std::min(static_cast<int>(pending_read_data_.size()), | 519 int read_len = std::min(static_cast<int>(pending_read_data_.size()), |
520 buf_len); | 520 buf_len); |
521 memcpy(buf->data(), pending_read_data_.data(), read_len); | 521 memcpy(buf->data(), pending_read_data_.data(), read_len); |
522 pending_read_data_.erase(0, read_len); | 522 pending_read_data_.erase(0, read_len); |
523 return read_len; | 523 return read_len; |
524 } | 524 } |
525 virtual int Write(IOBuffer* buf, int buf_len, | 525 virtual int Write(IOBuffer* buf, int buf_len, |
526 const CompletionCallback& callback) OVERRIDE { | 526 const CompletionCallback& callback) override { |
527 return ERR_NOT_IMPLEMENTED; | 527 return ERR_NOT_IMPLEMENTED; |
528 } | 528 } |
529 virtual int SetReceiveBufferSize(int32 size) OVERRIDE { | 529 virtual int SetReceiveBufferSize(int32 size) override { |
530 return ERR_NOT_IMPLEMENTED; | 530 return ERR_NOT_IMPLEMENTED; |
531 } | 531 } |
532 virtual int SetSendBufferSize(int32 size) OVERRIDE { | 532 virtual int SetSendBufferSize(int32 size) override { |
533 return ERR_NOT_IMPLEMENTED; | 533 return ERR_NOT_IMPLEMENTED; |
534 } | 534 } |
535 | 535 |
536 void DidRead(const char* data, int data_len) { | 536 void DidRead(const char* data, int data_len) { |
537 if (!read_buf_.get()) { | 537 if (!read_buf_.get()) { |
538 pending_read_data_.append(data, data_len); | 538 pending_read_data_.append(data, data_len); |
539 return; | 539 return; |
540 } | 540 } |
541 int read_len = std::min(data_len, read_buf_len_); | 541 int read_len = std::min(data_len, read_buf_len_); |
542 memcpy(read_buf_->data(), data, read_len); | 542 memcpy(read_buf_->data(), data, read_len); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
614 ASSERT_EQ(client_connection_id, GetConnectionId(2)); | 614 ASSERT_EQ(client_connection_id, GetConnectionId(2)); |
615 server_->Send200(client_connection_id, "Content for /test3", "text/plain"); | 615 server_->Send200(client_connection_id, "Content for /test3", "text/plain"); |
616 std::string response3; | 616 std::string response3; |
617 ASSERT_TRUE(client.ReadResponse(&response3)); | 617 ASSERT_TRUE(client.ReadResponse(&response3)); |
618 ASSERT_TRUE(StartsWithASCII(response3, "HTTP/1.1 200 OK", true)); | 618 ASSERT_TRUE(StartsWithASCII(response3, "HTTP/1.1 200 OK", true)); |
619 ASSERT_TRUE(EndsWith(response3, "Content for /test3", true)); | 619 ASSERT_TRUE(EndsWith(response3, "Content for /test3", true)); |
620 } | 620 } |
621 | 621 |
622 class CloseOnConnectHttpServerTest : public HttpServerTest { | 622 class CloseOnConnectHttpServerTest : public HttpServerTest { |
623 public: | 623 public: |
624 virtual void OnConnect(int connection_id) OVERRIDE { | 624 virtual void OnConnect(int connection_id) override { |
625 connection_ids_.push_back(connection_id); | 625 connection_ids_.push_back(connection_id); |
626 server_->Close(connection_id); | 626 server_->Close(connection_id); |
627 } | 627 } |
628 | 628 |
629 protected: | 629 protected: |
630 std::vector<int> connection_ids_; | 630 std::vector<int> connection_ids_; |
631 }; | 631 }; |
632 | 632 |
633 TEST_F(CloseOnConnectHttpServerTest, ServerImmediatelyClosesConnection) { | 633 TEST_F(CloseOnConnectHttpServerTest, ServerImmediatelyClosesConnection) { |
634 TestHttpClient client; | 634 TestHttpClient client; |
635 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); | 635 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); |
636 client.Send("GET / HTTP/1.1\r\n\r\n"); | 636 client.Send("GET / HTTP/1.1\r\n\r\n"); |
637 ASSERT_FALSE(RunUntilRequestsReceived(1)); | 637 ASSERT_FALSE(RunUntilRequestsReceived(1)); |
638 ASSERT_EQ(1ul, connection_ids_.size()); | 638 ASSERT_EQ(1ul, connection_ids_.size()); |
639 ASSERT_EQ(0ul, requests_.size()); | 639 ASSERT_EQ(0ul, requests_.size()); |
640 } | 640 } |
641 | 641 |
642 } // namespace | 642 } // namespace |
643 | 643 |
644 } // namespace net | 644 } // namespace net |
OLD | NEW |