1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/test/embedded_test_server/http_request.h"
7 #include "testing/gtest/include/gtest/gtest.h"
10 namespace test_server
{
12 TEST(HttpRequestTest
, ParseRequest
) {
13 HttpRequestParser parser
;
15 // Process request in chunks to check if the parser deals with border cases.
16 // Also, check multi-line headers as well as multiple requests in the same
17 // chunk. This basically should cover all the simplest border cases.
18 parser
.ProcessChunk("POST /foobar.html HTTP/1.1\r\n");
19 EXPECT_EQ(HttpRequestParser::WAITING
, parser
.ParseRequest());
20 parser
.ProcessChunk("Host: localhost:1234\r\n");
21 EXPECT_EQ(HttpRequestParser::WAITING
, parser
.ParseRequest());
22 parser
.ProcessChunk("Multi-line-header: abcd\r\n");
23 EXPECT_EQ(HttpRequestParser::WAITING
, parser
.ParseRequest());
24 parser
.ProcessChunk(" efgh\r\n");
25 EXPECT_EQ(HttpRequestParser::WAITING
, parser
.ParseRequest());
26 parser
.ProcessChunk(" ijkl\r\n");
27 EXPECT_EQ(HttpRequestParser::WAITING
, parser
.ParseRequest());
28 parser
.ProcessChunk("Content-Length: 10\r\n\r\n");
29 EXPECT_EQ(HttpRequestParser::WAITING
, parser
.ParseRequest());
30 // Content data and another request in the same chunk (possible in http/1.1).
31 parser
.ProcessChunk("1234567890GET /another.html HTTP/1.1\r\n\r\n");
32 ASSERT_EQ(HttpRequestParser::ACCEPTED
, parser
.ParseRequest());
34 // Fetch the first request and validate it.
36 scoped_ptr
<HttpRequest
> request
= parser
.GetRequest();
37 EXPECT_EQ("/foobar.html", request
->relative_url
);
38 EXPECT_EQ(METHOD_POST
, request
->method
);
39 EXPECT_EQ("1234567890", request
->content
);
40 ASSERT_EQ(3u, request
->headers
.size());
42 EXPECT_EQ(1u, request
->headers
.count("Host"));
43 EXPECT_EQ(1u, request
->headers
.count("Multi-line-header"));
44 EXPECT_EQ(1u, request
->headers
.count("Content-Length"));
46 EXPECT_EQ("localhost:1234", request
->headers
["Host"]);
47 EXPECT_EQ("abcd efgh ijkl", request
->headers
["Multi-line-header"]);
48 EXPECT_EQ("10", request
->headers
["Content-Length"]);
51 // No other request available yet since we do not support multiple requests
53 EXPECT_EQ(HttpRequestParser::WAITING
, parser
.ParseRequest());
56 TEST(HttpRequestTest
, ParseRequestWithEmptyBody
) {
57 HttpRequestParser parser
;
59 parser
.ProcessChunk("POST /foobar.html HTTP/1.1\r\n");
60 parser
.ProcessChunk("Content-Length: 0\r\n\r\n");
61 ASSERT_EQ(HttpRequestParser::ACCEPTED
, parser
.ParseRequest());
63 scoped_ptr
<HttpRequest
> request
= parser
.GetRequest();
64 EXPECT_EQ("", request
->content
);
65 EXPECT_TRUE(request
->has_content
);
66 EXPECT_EQ(1u, request
->headers
.count("Content-Length"));
67 EXPECT_EQ("0", request
->headers
["Content-Length"]);
70 TEST(HttpRequestTest
, ParseRequestWithoutBody
) {
71 HttpRequestParser parser
;
73 parser
.ProcessChunk("POST /foobar.html HTTP/1.1\r\n\r\n");
74 ASSERT_EQ(HttpRequestParser::ACCEPTED
, parser
.ParseRequest());
76 scoped_ptr
<HttpRequest
> request
= parser
.GetRequest();
77 EXPECT_EQ("", request
->content
);
78 EXPECT_FALSE(request
->has_content
);
81 } // namespace test_server