Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(652)

Side by Side Diff: net/server/http_server_unittest.cc

Issue 296053012: Replace StreamListenSocket with StreamSocket in HttpServer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments. Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <utility> 6 #include <utility>
6 #include <vector> 7 #include <vector>
7 8
8 #include "base/bind.h" 9 #include "base/bind.h"
9 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
11 #include "base/callback_helpers.h"
10 #include "base/compiler_specific.h" 12 #include "base/compiler_specific.h"
11 #include "base/format_macros.h" 13 #include "base/format_macros.h"
12 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h" 16 #include "base/memory/weak_ptr.h"
15 #include "base/message_loop/message_loop.h" 17 #include "base/message_loop/message_loop.h"
16 #include "base/message_loop/message_loop_proxy.h" 18 #include "base/message_loop/message_loop_proxy.h"
17 #include "base/run_loop.h" 19 #include "base/run_loop.h"
18 #include "base/strings/string_split.h" 20 #include "base/strings/string_split.h"
19 #include "base/strings/string_util.h" 21 #include "base/strings/string_util.h"
20 #include "base/strings/stringprintf.h" 22 #include "base/strings/stringprintf.h"
21 #include "base/time/time.h" 23 #include "base/time/time.h"
22 #include "net/base/address_list.h" 24 #include "net/base/address_list.h"
23 #include "net/base/io_buffer.h" 25 #include "net/base/io_buffer.h"
24 #include "net/base/ip_endpoint.h" 26 #include "net/base/ip_endpoint.h"
25 #include "net/base/net_errors.h" 27 #include "net/base/net_errors.h"
26 #include "net/base/net_log.h" 28 #include "net/base/net_log.h"
29 #include "net/base/net_util.h"
27 #include "net/base/test_completion_callback.h" 30 #include "net/base/test_completion_callback.h"
28 #include "net/server/http_server.h" 31 #include "net/server/http_server.h"
29 #include "net/server/http_server_request_info.h" 32 #include "net/server/http_server_request_info.h"
30 #include "net/socket/tcp_client_socket.h" 33 #include "net/socket/tcp_client_socket.h"
31 #include "net/socket/tcp_listen_socket.h" 34 #include "net/socket/tcp_server_socket.h"
32 #include "net/url_request/url_fetcher.h" 35 #include "net/url_request/url_fetcher.h"
33 #include "net/url_request/url_fetcher_delegate.h" 36 #include "net/url_request/url_fetcher_delegate.h"
34 #include "net/url_request/url_request_context.h" 37 #include "net/url_request/url_request_context.h"
35 #include "net/url_request/url_request_context_getter.h" 38 #include "net/url_request/url_request_context_getter.h"
36 #include "net/url_request/url_request_test_util.h" 39 #include "net/url_request/url_request_test_util.h"
37 #include "testing/gtest/include/gtest/gtest.h" 40 #include "testing/gtest/include/gtest/gtest.h"
38 41
39 namespace net { 42 namespace net {
40 43
41 namespace { 44 namespace {
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 }; 151 };
149 152
150 } // namespace 153 } // namespace
151 154
152 class HttpServerTest : public testing::Test, 155 class HttpServerTest : public testing::Test,
153 public HttpServer::Delegate { 156 public HttpServer::Delegate {
154 public: 157 public:
155 HttpServerTest() : quit_after_request_count_(0) {} 158 HttpServerTest() : quit_after_request_count_(0) {}
156 159
157 virtual void SetUp() OVERRIDE { 160 virtual void SetUp() OVERRIDE {
158 TCPListenSocketFactory socket_factory("127.0.0.1", 0); 161 scoped_ptr<ServerSocket> server_socket(
159 server_ = new HttpServer(socket_factory, this); 162 new TCPServerSocket(NULL, net::NetLog::Source()));
163 server_socket->ListenWithAddressAndPort("127.0.0.1", 0, 1);
164 server_.reset(new HttpServer(server_socket.Pass(), this));
160 ASSERT_EQ(OK, server_->GetLocalAddress(&server_address_)); 165 ASSERT_EQ(OK, server_->GetLocalAddress(&server_address_));
161 } 166 }
162 167
163 virtual void OnHttpRequest(int connection_id, 168 virtual void OnHttpRequest(int connection_id,
164 const HttpServerRequestInfo& info) OVERRIDE { 169 const HttpServerRequestInfo& info) OVERRIDE {
165 requests_.push_back(std::make_pair(info, connection_id)); 170 requests_.push_back(std::make_pair(info, connection_id));
166 if (requests_.size() == quit_after_request_count_) 171 if (requests_.size() == quit_after_request_count_)
167 run_loop_quit_func_.Run(); 172 run_loop_quit_func_.Run();
168 } 173 }
169 174
(...skipping 22 matching lines...) Expand all
192 } 197 }
193 198
194 HttpServerRequestInfo GetRequest(size_t request_index) { 199 HttpServerRequestInfo GetRequest(size_t request_index) {
195 return requests_[request_index].first; 200 return requests_[request_index].first;
196 } 201 }
197 202
198 int GetConnectionId(size_t request_index) { 203 int GetConnectionId(size_t request_index) {
199 return requests_[request_index].second; 204 return requests_[request_index].second;
200 } 205 }
201 206
207 void HandleAcceptResult(scoped_ptr<StreamSocket> socket) {
208 server_->accepted_socket_.reset(socket.release());
209 server_->HandleAcceptResult(OK);
210 }
211
202 protected: 212 protected:
203 scoped_refptr<HttpServer> server_; 213 scoped_ptr<HttpServer> server_;
204 IPEndPoint server_address_; 214 IPEndPoint server_address_;
205 base::Closure run_loop_quit_func_; 215 base::Closure run_loop_quit_func_;
206 std::vector<std::pair<HttpServerRequestInfo, int> > requests_; 216 std::vector<std::pair<HttpServerRequestInfo, int> > requests_;
207 217
208 private: 218 private:
209 size_t quit_after_request_count_; 219 size_t quit_after_request_count_;
210 }; 220 };
211 221
212 class WebSocketTest : public HttpServerTest { 222 class WebSocketTest : public HttpServerTest {
213 virtual void OnHttpRequest(int connection_id, 223 virtual void OnHttpRequest(int connection_id,
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 server_->SendRaw(GetConnectionId(0), "Third Piece of Data"); 432 server_->SendRaw(GetConnectionId(0), "Third Piece of Data");
423 433
424 const std::string expected_response("Raw Data More DataThird Piece of Data"); 434 const std::string expected_response("Raw Data More DataThird Piece of Data");
425 std::string response; 435 std::string response;
426 ASSERT_TRUE(client.Read(&response, expected_response.length())); 436 ASSERT_TRUE(client.Read(&response, expected_response.length()));
427 ASSERT_EQ(expected_response, response); 437 ASSERT_EQ(expected_response, response);
428 } 438 }
429 439
430 namespace { 440 namespace {
431 441
432 class MockStreamListenSocket : public StreamListenSocket { 442 class MockStreamSocket : public StreamSocket {
433 public: 443 public:
434 MockStreamListenSocket(StreamListenSocket::Delegate* delegate) 444 MockStreamSocket()
435 : StreamListenSocket(kInvalidSocket, delegate) {} 445 : connected_(true),
446 read_buf_(NULL),
447 read_buf_len_(0) {}
436 448
437 virtual void Accept() OVERRIDE { NOTREACHED(); } 449 // StreamSocket
450 virtual int Connect(const CompletionCallback& callback) OVERRIDE {
451 return ERR_NOT_IMPLEMENTED;
452 }
453 virtual void Disconnect() OVERRIDE {
454 connected_ = false;
455 if (!read_callback_.is_null()) {
456 read_buf_ = NULL;
457 read_buf_len_ = 0;
458 base::ResetAndReturn(&read_callback_).Run(ERR_CONNECTION_CLOSED);
459 }
460 }
461 virtual bool IsConnected() const OVERRIDE { return connected_; }
462 virtual bool IsConnectedAndIdle() const OVERRIDE { return IsConnected(); }
463 virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE {
464 return ERR_NOT_IMPLEMENTED;
465 }
466 virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE {
467 return ERR_NOT_IMPLEMENTED;
468 }
469 virtual const BoundNetLog& NetLog() const OVERRIDE { return net_log_; }
470 virtual void SetSubresourceSpeculation() OVERRIDE {}
471 virtual void SetOmniboxSpeculation() OVERRIDE {}
472 virtual bool WasEverUsed() const OVERRIDE { return true; }
473 virtual bool UsingTCPFastOpen() const OVERRIDE { return false; }
474 virtual bool WasNpnNegotiated() const OVERRIDE { return false; }
475 virtual NextProto GetNegotiatedProtocol() const OVERRIDE {
476 return kProtoUnknown;
477 }
478 virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE { return false; }
479
480 // Socket
481 virtual int Read(IOBuffer* buf, int buf_len,
482 const CompletionCallback& callback) OVERRIDE {
483 if (!connected_) {
484 return ERR_SOCKET_NOT_CONNECTED;
485 }
486 if (pending_read_data_.empty()) {
487 read_buf_ = buf;
488 read_buf_len_ = buf_len;
489 read_callback_ = callback;
490 return ERR_IO_PENDING;
491 }
492 DCHECK_GT(buf_len, 0);
493 int read_len = std::min(static_cast<int>(pending_read_data_.size()),
494 buf_len);
495 memcpy(buf->data(), pending_read_data_.data(), read_len);
496 pending_read_data_.erase(0, read_len);
497 return read_len;
498 }
499 virtual int Write(IOBuffer* buf, int buf_len,
500 const CompletionCallback& callback) OVERRIDE {
501 return ERR_NOT_IMPLEMENTED;
502 }
503 virtual int SetReceiveBufferSize(int32 size) OVERRIDE {
504 return ERR_NOT_IMPLEMENTED;
505 }
506 virtual int SetSendBufferSize(int32 size) OVERRIDE {
507 return ERR_NOT_IMPLEMENTED;
508 }
509
510 void DidRead(const char* data, int data_len) {
511 if (!read_buf_) {
512 pending_read_data_.append(data, data_len);
513 return;
514 }
515 int read_len = std::min(data_len, read_buf_len_);
516 memcpy(read_buf_->data(), data, read_len);
517 pending_read_data_.assign(data + read_len, data_len - read_len);
518 read_buf_ = NULL;
519 read_buf_len_ = 0;
520 base::ResetAndReturn(&read_callback_).Run(read_len);
521 }
438 522
439 private: 523 private:
440 virtual ~MockStreamListenSocket() {} 524 virtual ~MockStreamSocket() {}
525
526 bool connected_;
527 IOBuffer* read_buf_;
528 int read_buf_len_;
529 CompletionCallback read_callback_;
530 std::string pending_read_data_;
531 BoundNetLog net_log_;
532
533 DISALLOW_COPY_AND_ASSIGN(MockStreamSocket);
441 }; 534 };
442 535
443 } // namespace 536 } // namespace
444 537
445 TEST_F(HttpServerTest, RequestWithBodySplitAcrossPackets) { 538 TEST_F(HttpServerTest, RequestWithBodySplitAcrossPackets) {
446 StreamListenSocket* socket = 539 MockStreamSocket* socket = new MockStreamSocket();
447 new MockStreamListenSocket(server_.get()); 540 HandleAcceptResult(make_scoped_ptr<StreamSocket>(socket));
448 server_->DidAccept(NULL, make_scoped_ptr(socket));
449 std::string body("body"); 541 std::string body("body");
450 std::string request_text = base::StringPrintf( 542 std::string request_text = base::StringPrintf(
451 "GET /test HTTP/1.1\r\n" 543 "GET /test HTTP/1.1\r\n"
452 "SomeHeader: 1\r\n" 544 "SomeHeader: 1\r\n"
453 "Content-Length: %" PRIuS "\r\n\r\n%s", 545 "Content-Length: %" PRIuS "\r\n\r\n%s",
454 body.length(), 546 body.length(),
455 body.c_str()); 547 body.c_str());
456 server_->DidRead(socket, request_text.c_str(), request_text.length() - 2); 548 socket->DidRead(request_text.c_str(), request_text.length() - 2);
457 ASSERT_EQ(0u, requests_.size()); 549 ASSERT_EQ(0u, requests_.size());
458 server_->DidRead(socket, request_text.c_str() + request_text.length() - 2, 2); 550 socket->DidRead(request_text.c_str() + request_text.length() - 2, 2);
459 ASSERT_EQ(1u, requests_.size()); 551 ASSERT_EQ(1u, requests_.size());
460 ASSERT_EQ(body, GetRequest(0).data); 552 ASSERT_EQ(body, GetRequest(0).data);
461 } 553 }
462 554
463 TEST_F(HttpServerTest, MultipleRequestsOnSameConnection) { 555 TEST_F(HttpServerTest, MultipleRequestsOnSameConnection) {
464 // The idea behind this test is that requests with or without bodies should 556 // The idea behind this test is that requests with or without bodies should
465 // not break parsing of the next request. 557 // not break parsing of the next request.
466 TestHttpClient client; 558 TestHttpClient client;
467 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); 559 ASSERT_EQ(OK, client.ConnectAndWait(server_address_));
468 std::string body = "body"; 560 std::string body = "body";
(...skipping 28 matching lines...) Expand all
497 589
498 ASSERT_EQ(client_connection_id, GetConnectionId(2)); 590 ASSERT_EQ(client_connection_id, GetConnectionId(2));
499 server_->Send200(client_connection_id, "Content for /test3", "text/plain"); 591 server_->Send200(client_connection_id, "Content for /test3", "text/plain");
500 std::string response3; 592 std::string response3;
501 ASSERT_TRUE(client.Read(&response3)); 593 ASSERT_TRUE(client.Read(&response3));
502 ASSERT_TRUE(StartsWithASCII(response3, "HTTP/1.1 200 OK", true)); 594 ASSERT_TRUE(StartsWithASCII(response3, "HTTP/1.1 200 OK", true));
503 ASSERT_TRUE(EndsWith(response3, "Content for /test3", true)); 595 ASSERT_TRUE(EndsWith(response3, "Content for /test3", true));
504 } 596 }
505 597
506 } // namespace net 598 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698