This source file includes following definitions.
- TEST
- TEST
- TEST
#include "net/test/embedded_test_server/http_request.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace test_server {
TEST(HttpRequestTest, ParseRequest) {
HttpRequestParser parser;
parser.ProcessChunk("POST /foobar.html HTTP/1.1\r\n");
EXPECT_EQ(HttpRequestParser::WAITING, parser.ParseRequest());
parser.ProcessChunk("Host: localhost:1234\r\n");
EXPECT_EQ(HttpRequestParser::WAITING, parser.ParseRequest());
parser.ProcessChunk("Multi-line-header: abcd\r\n");
EXPECT_EQ(HttpRequestParser::WAITING, parser.ParseRequest());
parser.ProcessChunk(" efgh\r\n");
EXPECT_EQ(HttpRequestParser::WAITING, parser.ParseRequest());
parser.ProcessChunk(" ijkl\r\n");
EXPECT_EQ(HttpRequestParser::WAITING, parser.ParseRequest());
parser.ProcessChunk("Content-Length: 10\r\n\r\n");
EXPECT_EQ(HttpRequestParser::WAITING, parser.ParseRequest());
parser.ProcessChunk("1234567890GET /another.html HTTP/1.1\r\n\r\n");
ASSERT_EQ(HttpRequestParser::ACCEPTED, parser.ParseRequest());
{
scoped_ptr<HttpRequest> request = parser.GetRequest();
EXPECT_EQ("/foobar.html", request->relative_url);
EXPECT_EQ(METHOD_POST, request->method);
EXPECT_EQ("1234567890", request->content);
ASSERT_EQ(3u, request->headers.size());
EXPECT_EQ(1u, request->headers.count("Host"));
EXPECT_EQ(1u, request->headers.count("Multi-line-header"));
EXPECT_EQ(1u, request->headers.count("Content-Length"));
EXPECT_EQ("localhost:1234", request->headers["Host"]);
EXPECT_EQ("abcd efgh ijkl", request->headers["Multi-line-header"]);
EXPECT_EQ("10", request->headers["Content-Length"]);
}
EXPECT_EQ(HttpRequestParser::WAITING, parser.ParseRequest());
}
TEST(HttpRequestTest, ParseRequestWithEmptyBody) {
HttpRequestParser parser;
parser.ProcessChunk("POST /foobar.html HTTP/1.1\r\n");
parser.ProcessChunk("Content-Length: 0\r\n\r\n");
ASSERT_EQ(HttpRequestParser::ACCEPTED, parser.ParseRequest());
scoped_ptr<HttpRequest> request = parser.GetRequest();
EXPECT_EQ("", request->content);
EXPECT_TRUE(request->has_content);
EXPECT_EQ(1u, request->headers.count("Content-Length"));
EXPECT_EQ("0", request->headers["Content-Length"]);
}
TEST(HttpRequestTest, ParseRequestWithoutBody) {
HttpRequestParser parser;
parser.ProcessChunk("POST /foobar.html HTTP/1.1\r\n\r\n");
ASSERT_EQ(HttpRequestParser::ACCEPTED, parser.ParseRequest());
scoped_ptr<HttpRequest> request = parser.GetRequest();
EXPECT_EQ("", request->content);
EXPECT_FALSE(request->has_content);
}
}
}