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

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

Issue 497223003: Revert of Replace StreamListenSocket with StreamSocket in HttpServer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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
« no previous file with comments | « net/server/http_server_response_info.cc ('k') | net/server/web_socket.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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>
6 #include <utility> 5 #include <utility>
7 #include <vector> 6 #include <vector>
8 7
9 #include "base/bind.h" 8 #include "base/bind.h"
10 #include "base/bind_helpers.h" 9 #include "base/bind_helpers.h"
11 #include "base/callback_helpers.h"
12 #include "base/compiler_specific.h" 10 #include "base/compiler_specific.h"
13 #include "base/format_macros.h" 11 #include "base/format_macros.h"
14 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h" 14 #include "base/memory/weak_ptr.h"
17 #include "base/message_loop/message_loop.h" 15 #include "base/message_loop/message_loop.h"
18 #include "base/message_loop/message_loop_proxy.h" 16 #include "base/message_loop/message_loop_proxy.h"
19 #include "base/run_loop.h" 17 #include "base/run_loop.h"
20 #include "base/strings/string_split.h" 18 #include "base/strings/string_split.h"
21 #include "base/strings/string_util.h" 19 #include "base/strings/string_util.h"
22 #include "base/strings/stringprintf.h" 20 #include "base/strings/stringprintf.h"
23 #include "base/time/time.h" 21 #include "base/time/time.h"
24 #include "net/base/address_list.h" 22 #include "net/base/address_list.h"
25 #include "net/base/io_buffer.h" 23 #include "net/base/io_buffer.h"
26 #include "net/base/ip_endpoint.h" 24 #include "net/base/ip_endpoint.h"
27 #include "net/base/net_errors.h" 25 #include "net/base/net_errors.h"
28 #include "net/base/net_log.h" 26 #include "net/base/net_log.h"
29 #include "net/base/net_util.h"
30 #include "net/base/test_completion_callback.h" 27 #include "net/base/test_completion_callback.h"
31 #include "net/server/http_server.h" 28 #include "net/server/http_server.h"
32 #include "net/server/http_server_request_info.h" 29 #include "net/server/http_server_request_info.h"
33 #include "net/socket/tcp_client_socket.h" 30 #include "net/socket/tcp_client_socket.h"
34 #include "net/socket/tcp_server_socket.h" 31 #include "net/socket/tcp_listen_socket.h"
35 #include "net/url_request/url_fetcher.h" 32 #include "net/url_request/url_fetcher.h"
36 #include "net/url_request/url_fetcher_delegate.h" 33 #include "net/url_request/url_fetcher_delegate.h"
37 #include "net/url_request/url_request_context.h" 34 #include "net/url_request/url_request_context.h"
38 #include "net/url_request/url_request_context_getter.h" 35 #include "net/url_request/url_request_context_getter.h"
39 #include "net/url_request/url_request_test_util.h" 36 #include "net/url_request/url_request_test_util.h"
40 #include "testing/gtest/include/gtest/gtest.h" 37 #include "testing/gtest/include/gtest/gtest.h"
41 38
42 namespace net { 39 namespace net {
43 40
44 namespace { 41 namespace {
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 }; 148 };
152 149
153 } // namespace 150 } // namespace
154 151
155 class HttpServerTest : public testing::Test, 152 class HttpServerTest : public testing::Test,
156 public HttpServer::Delegate { 153 public HttpServer::Delegate {
157 public: 154 public:
158 HttpServerTest() : quit_after_request_count_(0) {} 155 HttpServerTest() : quit_after_request_count_(0) {}
159 156
160 virtual void SetUp() OVERRIDE { 157 virtual void SetUp() OVERRIDE {
161 scoped_ptr<ServerSocket> server_socket( 158 TCPListenSocketFactory socket_factory("127.0.0.1", 0);
162 new TCPServerSocket(NULL, net::NetLog::Source())); 159 server_ = new HttpServer(socket_factory, this);
163 server_socket->ListenWithAddressAndPort("127.0.0.1", 0, 1);
164 server_.reset(new HttpServer(server_socket.Pass(), this));
165 ASSERT_EQ(OK, server_->GetLocalAddress(&server_address_)); 160 ASSERT_EQ(OK, server_->GetLocalAddress(&server_address_));
166 } 161 }
167 162
168 virtual void OnHttpRequest(int connection_id, 163 virtual void OnHttpRequest(int connection_id,
169 const HttpServerRequestInfo& info) OVERRIDE { 164 const HttpServerRequestInfo& info) OVERRIDE {
170 requests_.push_back(std::make_pair(info, connection_id)); 165 requests_.push_back(std::make_pair(info, connection_id));
171 if (requests_.size() == quit_after_request_count_) 166 if (requests_.size() == quit_after_request_count_)
172 run_loop_quit_func_.Run(); 167 run_loop_quit_func_.Run();
173 } 168 }
174 169
(...skipping 22 matching lines...) Expand all
197 } 192 }
198 193
199 HttpServerRequestInfo GetRequest(size_t request_index) { 194 HttpServerRequestInfo GetRequest(size_t request_index) {
200 return requests_[request_index].first; 195 return requests_[request_index].first;
201 } 196 }
202 197
203 int GetConnectionId(size_t request_index) { 198 int GetConnectionId(size_t request_index) {
204 return requests_[request_index].second; 199 return requests_[request_index].second;
205 } 200 }
206 201
207 void HandleAcceptResult(scoped_ptr<StreamSocket> socket) {
208 server_->accepted_socket_.reset(socket.release());
209 server_->HandleAcceptResult(OK);
210 }
211
212 protected: 202 protected:
213 scoped_ptr<HttpServer> server_; 203 scoped_refptr<HttpServer> server_;
214 IPEndPoint server_address_; 204 IPEndPoint server_address_;
215 base::Closure run_loop_quit_func_; 205 base::Closure run_loop_quit_func_;
216 std::vector<std::pair<HttpServerRequestInfo, int> > requests_; 206 std::vector<std::pair<HttpServerRequestInfo, int> > requests_;
217 207
218 private: 208 private:
219 size_t quit_after_request_count_; 209 size_t quit_after_request_count_;
220 }; 210 };
221 211
222 class WebSocketTest : public HttpServerTest { 212 class WebSocketTest : public HttpServerTest {
223 virtual void OnHttpRequest(int connection_id, 213 virtual void OnHttpRequest(int connection_id,
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
432 server_->SendRaw(GetConnectionId(0), "Third Piece of Data"); 422 server_->SendRaw(GetConnectionId(0), "Third Piece of Data");
433 423
434 const std::string expected_response("Raw Data More DataThird Piece of Data"); 424 const std::string expected_response("Raw Data More DataThird Piece of Data");
435 std::string response; 425 std::string response;
436 ASSERT_TRUE(client.Read(&response, expected_response.length())); 426 ASSERT_TRUE(client.Read(&response, expected_response.length()));
437 ASSERT_EQ(expected_response, response); 427 ASSERT_EQ(expected_response, response);
438 } 428 }
439 429
440 namespace { 430 namespace {
441 431
442 class MockStreamSocket : public StreamSocket { 432 class MockStreamListenSocket : public StreamListenSocket {
443 public: 433 public:
444 MockStreamSocket() 434 MockStreamListenSocket(StreamListenSocket::Delegate* delegate)
445 : connected_(true), 435 : StreamListenSocket(kInvalidSocket, delegate) {}
446 read_buf_(NULL),
447 read_buf_len_(0) {}
448 436
449 // StreamSocket 437 virtual void Accept() OVERRIDE { NOTREACHED(); }
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 }
522 438
523 private: 439 private:
524 virtual ~MockStreamSocket() {} 440 virtual ~MockStreamListenSocket() {}
525
526 bool connected_;
527 scoped_refptr<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);
534 }; 441 };
535 442
536 } // namespace 443 } // namespace
537 444
538 TEST_F(HttpServerTest, RequestWithBodySplitAcrossPackets) { 445 TEST_F(HttpServerTest, RequestWithBodySplitAcrossPackets) {
539 MockStreamSocket* socket = new MockStreamSocket(); 446 StreamListenSocket* socket =
540 HandleAcceptResult(make_scoped_ptr<StreamSocket>(socket)); 447 new MockStreamListenSocket(server_.get());
448 server_->DidAccept(NULL, make_scoped_ptr(socket));
541 std::string body("body"); 449 std::string body("body");
542 std::string request_text = base::StringPrintf( 450 std::string request_text = base::StringPrintf(
543 "GET /test HTTP/1.1\r\n" 451 "GET /test HTTP/1.1\r\n"
544 "SomeHeader: 1\r\n" 452 "SomeHeader: 1\r\n"
545 "Content-Length: %" PRIuS "\r\n\r\n%s", 453 "Content-Length: %" PRIuS "\r\n\r\n%s",
546 body.length(), 454 body.length(),
547 body.c_str()); 455 body.c_str());
548 socket->DidRead(request_text.c_str(), request_text.length() - 2); 456 server_->DidRead(socket, request_text.c_str(), request_text.length() - 2);
549 ASSERT_EQ(0u, requests_.size()); 457 ASSERT_EQ(0u, requests_.size());
550 socket->DidRead(request_text.c_str() + request_text.length() - 2, 2); 458 server_->DidRead(socket, request_text.c_str() + request_text.length() - 2, 2);
551 ASSERT_EQ(1u, requests_.size()); 459 ASSERT_EQ(1u, requests_.size());
552 ASSERT_EQ(body, GetRequest(0).data); 460 ASSERT_EQ(body, GetRequest(0).data);
553 } 461 }
554 462
555 TEST_F(HttpServerTest, MultipleRequestsOnSameConnection) { 463 TEST_F(HttpServerTest, MultipleRequestsOnSameConnection) {
556 // The idea behind this test is that requests with or without bodies should 464 // The idea behind this test is that requests with or without bodies should
557 // not break parsing of the next request. 465 // not break parsing of the next request.
558 TestHttpClient client; 466 TestHttpClient client;
559 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); 467 ASSERT_EQ(OK, client.ConnectAndWait(server_address_));
560 std::string body = "body"; 468 std::string body = "body";
(...skipping 28 matching lines...) Expand all
589 497
590 ASSERT_EQ(client_connection_id, GetConnectionId(2)); 498 ASSERT_EQ(client_connection_id, GetConnectionId(2));
591 server_->Send200(client_connection_id, "Content for /test3", "text/plain"); 499 server_->Send200(client_connection_id, "Content for /test3", "text/plain");
592 std::string response3; 500 std::string response3;
593 ASSERT_TRUE(client.Read(&response3)); 501 ASSERT_TRUE(client.Read(&response3));
594 ASSERT_TRUE(StartsWithASCII(response3, "HTTP/1.1 200 OK", true)); 502 ASSERT_TRUE(StartsWithASCII(response3, "HTTP/1.1 200 OK", true));
595 ASSERT_TRUE(EndsWith(response3, "Content for /test3", true)); 503 ASSERT_TRUE(EndsWith(response3, "Content for /test3", true));
596 } 504 }
597 505
598 } // namespace net 506 } // namespace net
OLDNEW
« no previous file with comments | « net/server/http_server_response_info.cc ('k') | net/server/web_socket.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698