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

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: Use base::StringPiece in some places not to copy data. Created 6 years, 6 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<ServerSocketFactory> factory(new TCPServerSocketFactory());
159 server_ = new HttpServer(socket_factory, this); 162 factory->SetAddressAndPort("127.0.0.1", 0);
163 server_.reset(new HttpServer(factory->CreateAndListen(), this));
160 ASSERT_EQ(OK, server_->GetLocalAddress(&server_address_)); 164 ASSERT_EQ(OK, server_->GetLocalAddress(&server_address_));
161 } 165 }
162 166
163 virtual void OnHttpRequest(int connection_id, 167 virtual void OnHttpRequest(int connection_id,
164 const HttpServerRequestInfo& info) OVERRIDE { 168 const HttpServerRequestInfo& info) OVERRIDE {
165 requests_.push_back(std::make_pair(info, connection_id)); 169 requests_.push_back(std::make_pair(info, connection_id));
166 if (requests_.size() == quit_after_request_count_) 170 if (requests_.size() == quit_after_request_count_)
167 run_loop_quit_func_.Run(); 171 run_loop_quit_func_.Run();
168 } 172 }
169 173
(...skipping 22 matching lines...) Expand all
192 } 196 }
193 197
194 HttpServerRequestInfo GetRequest(size_t request_index) { 198 HttpServerRequestInfo GetRequest(size_t request_index) {
195 return requests_[request_index].first; 199 return requests_[request_index].first;
196 } 200 }
197 201
198 int GetConnectionId(size_t request_index) { 202 int GetConnectionId(size_t request_index) {
199 return requests_[request_index].second; 203 return requests_[request_index].second;
200 } 204 }
201 205
206 void DidAccept(scoped_ptr<StreamSocket> socket) {
207 server_->accepted_socket_.reset(socket.release());
208 server_->DidAccept(OK);
209 }
210
202 protected: 211 protected:
203 scoped_refptr<HttpServer> server_; 212 scoped_ptr<HttpServer> server_;
204 IPEndPoint server_address_; 213 IPEndPoint server_address_;
205 base::Closure run_loop_quit_func_; 214 base::Closure run_loop_quit_func_;
206 std::vector<std::pair<HttpServerRequestInfo, int> > requests_; 215 std::vector<std::pair<HttpServerRequestInfo, int> > requests_;
207 216
208 private: 217 private:
209 size_t quit_after_request_count_; 218 size_t quit_after_request_count_;
210 }; 219 };
211 220
212 class WebSocketTest : public HttpServerTest { 221 class WebSocketTest : public HttpServerTest {
213 virtual void OnHttpRequest(int connection_id, 222 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"); 431 server_->SendRaw(GetConnectionId(0), "Third Piece of Data");
423 432
424 const std::string expected_response("Raw Data More DataThird Piece of Data"); 433 const std::string expected_response("Raw Data More DataThird Piece of Data");
425 std::string response; 434 std::string response;
426 ASSERT_TRUE(client.Read(&response, expected_response.length())); 435 ASSERT_TRUE(client.Read(&response, expected_response.length()));
427 ASSERT_EQ(expected_response, response); 436 ASSERT_EQ(expected_response, response);
428 } 437 }
429 438
430 namespace { 439 namespace {
431 440
432 class MockStreamListenSocket : public StreamListenSocket { 441 class MockStreamSocket : public StreamSocket {
433 public: 442 public:
434 MockStreamListenSocket(StreamListenSocket::Delegate* delegate) 443 MockStreamSocket()
435 : StreamListenSocket(kInvalidSocket, delegate) {} 444 : connected_(true),
445 read_buf_(NULL),
446 read_buf_len_(0) {}
436 447
437 virtual void Accept() OVERRIDE { NOTREACHED(); } 448 // StreamSocket
449 virtual int Connect(const CompletionCallback& callback) OVERRIDE {
450 return ERR_NOT_IMPLEMENTED;
451 }
452 virtual void Disconnect() OVERRIDE {
453 connected_ = false;
454 if (!read_callback_.is_null()) {
455 read_buf_ = NULL;
456 read_buf_len_ = 0;
457 base::ResetAndReturn(&read_callback_).Run(ERR_CONNECTION_CLOSED);
458 }
459 }
460 virtual bool IsConnected() const OVERRIDE { return connected_; }
461 virtual bool IsConnectedAndIdle() const OVERRIDE { return IsConnected(); }
462 virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE {
463 return ERR_NOT_IMPLEMENTED;
464 }
465 virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE {
466 return ERR_NOT_IMPLEMENTED;
467 }
468 virtual const BoundNetLog& NetLog() const OVERRIDE { return net_log_; }
469 virtual void SetSubresourceSpeculation() OVERRIDE {}
470 virtual void SetOmniboxSpeculation() OVERRIDE {}
471 virtual bool WasEverUsed() const OVERRIDE { return true; }
472 virtual bool UsingTCPFastOpen() const OVERRIDE { return false; }
473 virtual bool WasNpnNegotiated() const OVERRIDE { return false; }
474 virtual NextProto GetNegotiatedProtocol() const OVERRIDE {
475 return kProtoUnknown;
476 }
477 virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE { return false; }
478
479 // Socket
480 virtual int Read(IOBuffer* buf, int buf_len,
481 const CompletionCallback& callback) OVERRIDE {
482 if (!connected_) {
483 return ERR_SOCKET_NOT_CONNECTED;
484 }
485 if (pending_read_data_.empty()) {
486 read_buf_ = buf;
487 read_buf_len_ = buf_len;
488 read_callback_ = callback;
489 return ERR_IO_PENDING;
490 }
491 DCHECK_GT(buf_len, 0);
492 int read_len = std::min(static_cast<int>(pending_read_data_.size()),
493 buf_len);
494 memcpy(buf->data(), pending_read_data_.data(), read_len);
495 pending_read_data_.erase(0, read_len);
496 return read_len;
497 }
498 virtual int Write(IOBuffer* buf, int buf_len,
499 const CompletionCallback& callback) OVERRIDE {
500 return ERR_NOT_IMPLEMENTED;
501 }
502 virtual int SetReceiveBufferSize(int32 size) OVERRIDE {
503 return ERR_NOT_IMPLEMENTED;
504 }
505 virtual int SetSendBufferSize(int32 size) OVERRIDE {
506 return ERR_NOT_IMPLEMENTED;
507 }
508
509 void DidRead(const char* data, int data_len) {
510 if (!read_buf_) {
511 pending_read_data_.append(data, data_len);
512 return;
513 }
514 int read_len = std::min(data_len, read_buf_len_);
515 memcpy(read_buf_->data(), data, read_len);
516 pending_read_data_.assign(data + read_len, data_len - read_len);
517 read_buf_ = NULL;
518 read_buf_len_ = 0;
519 base::ResetAndReturn(&read_callback_).Run(read_len);
520 }
438 521
439 private: 522 private:
440 virtual ~MockStreamListenSocket() {} 523 virtual ~MockStreamSocket() {}
524
525 bool connected_;
526 IOBuffer* read_buf_;
527 int read_buf_len_;
528 CompletionCallback read_callback_;
529 std::string pending_read_data_;
530 BoundNetLog net_log_;
531
532 DISALLOW_COPY_AND_ASSIGN(MockStreamSocket);
441 }; 533 };
442 534
443 } // namespace 535 } // namespace
444 536
445 TEST_F(HttpServerTest, RequestWithBodySplitAcrossPackets) { 537 TEST_F(HttpServerTest, RequestWithBodySplitAcrossPackets) {
446 StreamListenSocket* socket = 538 MockStreamSocket* socket = new MockStreamSocket();
447 new MockStreamListenSocket(server_.get()); 539 DidAccept(make_scoped_ptr<StreamSocket>(socket));
448 server_->DidAccept(NULL, make_scoped_ptr(socket));
449 std::string body("body"); 540 std::string body("body");
450 std::string request_text = base::StringPrintf( 541 std::string request_text = base::StringPrintf(
451 "GET /test HTTP/1.1\r\n" 542 "GET /test HTTP/1.1\r\n"
452 "SomeHeader: 1\r\n" 543 "SomeHeader: 1\r\n"
453 "Content-Length: %" PRIuS "\r\n\r\n%s", 544 "Content-Length: %" PRIuS "\r\n\r\n%s",
454 body.length(), 545 body.length(),
455 body.c_str()); 546 body.c_str());
456 server_->DidRead(socket, request_text.c_str(), request_text.length() - 2); 547 socket->DidRead(request_text.c_str(), request_text.length() - 2);
457 ASSERT_EQ(0u, requests_.size()); 548 ASSERT_EQ(0u, requests_.size());
458 server_->DidRead(socket, request_text.c_str() + request_text.length() - 2, 2); 549 socket->DidRead(request_text.c_str() + request_text.length() - 2, 2);
459 ASSERT_EQ(1u, requests_.size()); 550 ASSERT_EQ(1u, requests_.size());
460 ASSERT_EQ(body, GetRequest(0).data); 551 ASSERT_EQ(body, GetRequest(0).data);
461 } 552 }
462 553
463 TEST_F(HttpServerTest, MultipleRequestsOnSameConnection) { 554 TEST_F(HttpServerTest, MultipleRequestsOnSameConnection) {
464 // The idea behind this test is that requests with or without bodies should 555 // The idea behind this test is that requests with or without bodies should
465 // not break parsing of the next request. 556 // not break parsing of the next request.
466 TestHttpClient client; 557 TestHttpClient client;
467 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); 558 ASSERT_EQ(OK, client.ConnectAndWait(server_address_));
468 std::string body = "body"; 559 std::string body = "body";
(...skipping 28 matching lines...) Expand all
497 588
498 ASSERT_EQ(client_connection_id, GetConnectionId(2)); 589 ASSERT_EQ(client_connection_id, GetConnectionId(2));
499 server_->Send200(client_connection_id, "Content for /test3", "text/plain"); 590 server_->Send200(client_connection_id, "Content for /test3", "text/plain");
500 std::string response3; 591 std::string response3;
501 ASSERT_TRUE(client.Read(&response3)); 592 ASSERT_TRUE(client.Read(&response3));
502 ASSERT_TRUE(StartsWithASCII(response3, "HTTP/1.1 200 OK", true)); 593 ASSERT_TRUE(StartsWithASCII(response3, "HTTP/1.1 200 OK", true));
503 ASSERT_TRUE(EndsWith(response3, "Content for /test3", true)); 594 ASSERT_TRUE(EndsWith(response3, "Content for /test3", true));
504 } 595 }
505 596
506 } // namespace net 597 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698