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

Unified Diff: net/server/http_server_unittest.cc

Issue 212683006: HttpServer: allows sending raw response data for nontypical responses. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Replaces request+connection with std::pair. Created 6 years, 9 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/server/http_server.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/server/http_server_unittest.cc
diff --git a/net/server/http_server_unittest.cc b/net/server/http_server_unittest.cc
index 1de2100ab68fc20a5ba409a635cf11b060afc6cc..4f663f88e88df8868ea565a95e8af691fb031ecb 100644
--- a/net/server/http_server_unittest.cc
+++ b/net/server/http_server_unittest.cc
@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include <utility>
#include <vector>
#include "base/bind.h"
@@ -38,6 +39,8 @@ namespace net {
namespace {
+const int kMaxExpectedResponseLength = 2048;
+
void SetTimedOutAndQuitLoop(const base::WeakPtr<bool> timed_out,
const base::Closure& quit_loop_func) {
if (timed_out) {
@@ -86,6 +89,15 @@ class TestHttpClient {
Write();
}
+ bool Read(std::string* data) {
+ base::RunLoop run_loop;
+ Read(run_loop.QuitClosure());
+ bool success = RunLoopWithTimeout(&run_loop);
+ std::string io_buffer_contents(read_buffer_->data());
+ *data = io_buffer_contents.substr(0, bytes_received_);
+ return success;
+ }
+
private:
void OnConnect(const base::Closure& quit_loop, int result) {
connect_result_ = result;
@@ -108,8 +120,27 @@ class TestHttpClient {
Write();
}
+ void Read(const base::Closure& quit_loop) {
Ryan Sleevi 2014/03/27 20:59:46 This overloads with the Read method on 92. Rename
gunsch 2014/03/27 23:41:17 Done.
+ read_buffer_ = new IOBufferWithSize(kMaxExpectedResponseLength);
+ int result = socket_->Read(read_buffer_,
+ kMaxExpectedResponseLength,
+ base::Bind(&TestHttpClient::OnRead,
+ base::Unretained(this),
+ quit_loop));
+ if (result != ERR_IO_PENDING)
+ OnRead(quit_loop, result);
+ }
+
+ void OnRead(const base::Closure& quit_loop, int result) {
+ bytes_received_ = result;
+ quit_loop.Run();
+ }
+
+ base::Closure run_loop_quit_func_;
+ scoped_refptr<IOBufferWithSize> read_buffer_;
scoped_refptr<DrainableIOBuffer> write_buffer_;
scoped_ptr<TCPClientSocket> socket_;
+ int bytes_received_;
int connect_result_;
};
@@ -128,7 +159,7 @@ class HttpServerTest : public testing::Test,
virtual void OnHttpRequest(int connection_id,
const HttpServerRequestInfo& info) OVERRIDE {
- requests_.push_back(info);
+ requests_.push_back(std::make_pair(info, connection_id));
if (requests_.size() == quit_after_request_count_)
run_loop_quit_func_.Run();
}
@@ -157,11 +188,19 @@ class HttpServerTest : public testing::Test,
return success;
}
+ HttpServerRequestInfo request(int request_index) {
Ryan Sleevi 2014/03/27 20:59:46 naming: GetRequest() defensive: Should you DCHECK_
gunsch 2014/03/27 23:41:17 Done.
+ return requests_[request_index].first;
+ }
+
+ int connection_id(int request_index) {
Ryan Sleevi 2014/03/27 20:59:46 naming: GetConnectionId() defensive: Should you DC
gunsch 2014/03/27 23:41:17 Done.
+ return requests_[request_index].second;
+ }
+
protected:
scoped_refptr<HttpServer> server_;
IPEndPoint server_address_;
base::Closure run_loop_quit_func_;
- std::vector<HttpServerRequestInfo> requests_;
+ std::vector<std::pair<HttpServerRequestInfo, int> > requests_;
private:
size_t quit_after_request_count_;
@@ -172,11 +211,11 @@ TEST_F(HttpServerTest, Request) {
ASSERT_EQ(OK, client.ConnectAndWait(server_address_));
client.Send("GET /test HTTP/1.1\r\n\r\n");
ASSERT_TRUE(RunUntilRequestsReceived(1));
- ASSERT_EQ("GET", requests_[0].method);
- ASSERT_EQ("/test", requests_[0].path);
- ASSERT_EQ("", requests_[0].data);
- ASSERT_EQ(0u, requests_[0].headers.size());
- ASSERT_TRUE(StartsWithASCII(requests_[0].peer.ToString(), "127.0.0.1", true));
+ ASSERT_EQ("GET", request(0).method);
+ ASSERT_EQ("/test", request(0).path);
+ ASSERT_EQ("", request(0).data);
+ ASSERT_EQ(0u, request(0).headers.size());
+ ASSERT_TRUE(StartsWithASCII(request(0).peer.ToString(), "127.0.0.1", true));
}
TEST_F(HttpServerTest, RequestWithHeaders) {
@@ -199,13 +238,13 @@ TEST_F(HttpServerTest, RequestWithHeaders) {
client.Send("GET /test HTTP/1.1\r\n" + headers + "\r\n");
ASSERT_TRUE(RunUntilRequestsReceived(1));
- ASSERT_EQ("", requests_[0].data);
+ ASSERT_EQ("", request(0).data);
for (size_t i = 0; i < arraysize(kHeaders); ++i) {
std::string field = StringToLowerASCII(std::string(kHeaders[i][0]));
std::string value = kHeaders[i][2];
- ASSERT_EQ(1u, requests_[0].headers.count(field)) << field;
- ASSERT_EQ(value, requests_[0].headers[field]) << kHeaders[i][0];
+ ASSERT_EQ(1u, request(0).headers.count(field)) << field;
+ ASSERT_EQ(value, request(0).headers[field]) << kHeaders[i][0];
}
}
@@ -220,8 +259,8 @@ TEST_F(HttpServerTest, RequestWithBody) {
body.length(),
body.c_str()));
ASSERT_TRUE(RunUntilRequestsReceived(1));
- ASSERT_EQ(2u, requests_[0].headers.size());
- ASSERT_EQ(body.length(), requests_[0].data.length());
+ ASSERT_EQ(2u, request(0).headers.size());
+ ASSERT_EQ(body.length(), request(0).data.length());
ASSERT_EQ('a', body[0]);
ASSERT_EQ('c', *body.rbegin());
}
@@ -261,6 +300,33 @@ TEST_F(HttpServerTest, RequestWithTooLargeBody) {
ASSERT_EQ(0u, requests_.size());
}
+TEST_F(HttpServerTest, Send200) {
+ TestHttpClient client;
+ ASSERT_EQ(OK, client.ConnectAndWait(server_address_));
+ client.Send("GET /test HTTP/1.1\r\n\r\n");
+ ASSERT_TRUE(RunUntilRequestsReceived(1));
+ server_->Send200(connection_id(0), "Response!", "text/plain");
+
+ std::string response;
+ ASSERT_TRUE(client.Read(&response));
+ ASSERT_TRUE(StartsWithASCII(response, "HTTP/1.1 200 OK", true));
+ ASSERT_TRUE(response.find("Response!") != std::string::npos);
Ryan Sleevi 2014/03/27 20:59:46 ASSERT_NE(std::string::npos, response.find("Respon
gunsch 2014/03/27 23:41:17 Done.
+}
+
+TEST_F(HttpServerTest, SendRaw) {
+ TestHttpClient client;
+ ASSERT_EQ(OK, client.ConnectAndWait(server_address_));
+ client.Send("GET /test HTTP/1.1\r\n\r\n");
+ ASSERT_TRUE(RunUntilRequestsReceived(1));
+ server_->SendRaw(connection_id(0), "Raw Data ");
+ server_->SendRaw(connection_id(0), "More Data");
+ server_->SendRaw(connection_id(0), "Third Piece of Data");
+
+ std::string response;
+ ASSERT_TRUE(client.Read(&response));
+ ASSERT_EQ("Raw Data More DataThird Piece of Data", response);
+}
+
namespace {
class MockStreamListenSocket : public StreamListenSocket {
@@ -281,17 +347,17 @@ TEST_F(HttpServerTest, RequestWithBodySplitAcrossPackets) {
new MockStreamListenSocket(server_.get());
server_->DidAccept(NULL, make_scoped_ptr(socket));
std::string body("body");
- std::string request = base::StringPrintf(
+ std::string request_text = base::StringPrintf(
"GET /test HTTP/1.1\r\n"
"SomeHeader: 1\r\n"
"Content-Length: %" PRIuS "\r\n\r\n%s",
body.length(),
body.c_str());
- server_->DidRead(socket, request.c_str(), request.length() - 2);
+ server_->DidRead(socket, request_text.c_str(), request_text.length() - 2);
ASSERT_EQ(0u, requests_.size());
- server_->DidRead(socket, request.c_str() + request.length() - 2, 2);
+ server_->DidRead(socket, request_text.c_str() + request_text.length() - 2, 2);
ASSERT_EQ(1u, requests_.size());
- ASSERT_EQ(body, requests_[0].data);
+ ASSERT_EQ(body, request(0).data);
}
TEST_F(HttpServerTest, MultipleRequestsOnSameConnection) {
@@ -306,15 +372,35 @@ TEST_F(HttpServerTest, MultipleRequestsOnSameConnection) {
body.length(),
body.c_str()));
ASSERT_TRUE(RunUntilRequestsReceived(1));
- ASSERT_EQ(body, requests_[0].data);
+ ASSERT_EQ(body, request(0).data);
+
+ int client_connection_id = connection_id(0);
+ server_->Send200(client_connection_id, "Content for /test", "text/plain");
+ std::string response1;
+ ASSERT_TRUE(client.Read(&response1));
+ ASSERT_TRUE(StartsWithASCII(response1, "HTTP/1.1 200 OK", true));
+ ASSERT_TRUE(EndsWith(response1, "Content for /test", true));
client.Send("GET /test2 HTTP/1.1\r\n\r\n");
ASSERT_TRUE(RunUntilRequestsReceived(2));
- ASSERT_EQ("/test2", requests_[1].path);
+ ASSERT_EQ("/test2", request(1).path);
+
+ ASSERT_EQ(client_connection_id, connection_id(1));
+ server_->Send404(client_connection_id);
+ std::string response2;
+ ASSERT_TRUE(client.Read(&response2));
+ ASSERT_TRUE(StartsWithASCII(response2, "HTTP/1.1 404 Not Found", true));
client.Send("GET /test3 HTTP/1.1\r\n\r\n");
ASSERT_TRUE(RunUntilRequestsReceived(3));
- ASSERT_EQ("/test3", requests_[2].path);
+ ASSERT_EQ("/test3", request(2).path);
+
+ ASSERT_EQ(client_connection_id, connection_id(2));
+ server_->Send200(client_connection_id, "Content for /test3", "text/plain");
+ std::string response3;
+ ASSERT_TRUE(client.Read(&response3));
+ ASSERT_TRUE(StartsWithASCII(response3, "HTTP/1.1 200 OK", true));
+ ASSERT_TRUE(EndsWith(response3, "Content for /test3", true));
}
} // namespace net
« no previous file with comments | « net/server/http_server.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698