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("POST", request
->method_string
);
39 EXPECT_EQ(METHOD_POST
, request
->method
);
40 EXPECT_EQ("1234567890", request
->content
);
41 ASSERT_EQ(3u, request
->headers
.size());
43 EXPECT_EQ(1u, request
->headers
.count("Host"));
44 EXPECT_EQ(1u, request
->headers
.count("Multi-line-header"));
45 EXPECT_EQ(1u, request
->headers
.count("Content-Length"));
47 EXPECT_EQ("localhost:1234", request
->headers
["Host"]);
48 EXPECT_EQ("abcd efgh ijkl", request
->headers
["Multi-line-header"]);
49 EXPECT_EQ("10", request
->headers
["Content-Length"]);
52 // No other request available yet since we do not support multiple requests
54 EXPECT_EQ(HttpRequestParser::WAITING
, parser
.ParseRequest());
57 TEST(HttpRequestTest
, ParseRequestWithEmptyBody
) {
58 HttpRequestParser parser
;
60 parser
.ProcessChunk("POST /foobar.html HTTP/1.1\r\n");
61 parser
.ProcessChunk("Content-Length: 0\r\n\r\n");
62 ASSERT_EQ(HttpRequestParser::ACCEPTED
, parser
.ParseRequest());
64 scoped_ptr
<HttpRequest
> request
= parser
.GetRequest();
65 EXPECT_EQ("", request
->content
);
66 EXPECT_TRUE(request
->has_content
);
67 EXPECT_EQ(1u, request
->headers
.count("Content-Length"));
68 EXPECT_EQ("0", request
->headers
["Content-Length"]);
71 TEST(HttpRequestTest
, ParseRequestWithoutBody
) {
72 HttpRequestParser parser
;
74 parser
.ProcessChunk("POST /foobar.html HTTP/1.1\r\n\r\n");
75 ASSERT_EQ(HttpRequestParser::ACCEPTED
, parser
.ParseRequest());
77 scoped_ptr
<HttpRequest
> request
= parser
.GetRequest();
78 EXPECT_EQ("", request
->content
);
79 EXPECT_FALSE(request
->has_content
);
82 TEST(HttpRequestTest
, ParseGet
) {
83 HttpRequestParser parser
;
85 parser
.ProcessChunk("GET /foobar.html HTTP/1.1\r\n\r\n");
86 ASSERT_EQ(HttpRequestParser::ACCEPTED
, parser
.ParseRequest());
88 scoped_ptr
<HttpRequest
> request
= parser
.GetRequest();
89 EXPECT_EQ("/foobar.html", request
->relative_url
);
90 EXPECT_EQ("GET", request
->method_string
);
91 EXPECT_EQ(METHOD_GET
, request
->method
);
92 EXPECT_EQ("", request
->content
);
93 EXPECT_FALSE(request
->has_content
);
96 } // namespace test_server