forked from rbeeli/websocketclient-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpParser.cpp
140 lines (119 loc) · 5.2 KB
/
HttpParser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <gtest/gtest.h>
#include <string>
#include "ws_client/HttpParser.hpp"
using namespace ws_client;
using std::string;
// ----------------------------------------------------------------------------
// HttpParser::parse_request_status_line
// ----------------------------------------------------------------------------
TEST(HttpParser_parse_header_status_line, valid_200_ok)
{
auto result = HttpParser::parse_request_status_line("HTTP/1.1 200 OK");
EXPECT_TRUE(result.has_value());
EXPECT_TRUE(result.value().protocol_version == "HTTP/1.1");
EXPECT_TRUE(result.value().status_code == 200);
EXPECT_TRUE(result.value().reason == "OK");
}
TEST(HttpParser_parse_header_status_line, invalid_protocol_and_status_code_format)
{
auto result = HttpParser::parse_request_status_line("HTP/1.1 TWOHUNDRED OK");
EXPECT_TRUE(!result.has_value());
}
TEST(HttpParser_parse_header_status_line, missing_reason_phrase)
{
auto result = HttpParser::parse_request_status_line("HTTP/1.1 404 ");
EXPECT_TRUE(result.has_value());
EXPECT_TRUE(result.value().reason.empty());
}
TEST(HttpParser_parse_header_status_line, status_code_not_a_number)
{
auto result = HttpParser::parse_request_status_line("HTTP/1.1 ABC Not Found");
EXPECT_TRUE(!result.has_value());
}
TEST(HttpParser_parse_header_status_line, empty_input_string)
{
auto result = HttpParser::parse_request_status_line("");
EXPECT_TRUE(!result.has_value());
}
TEST(HttpParser_parse_header_status_line, only_protocol_and_version)
{
auto result = HttpParser::parse_request_status_line("HTTP/1.1");
EXPECT_TRUE(!result.has_value());
}
TEST(HttpParser_parse_header_status_line, valid_status_line_with_extra_spaces)
{
auto result = HttpParser::parse_request_status_line(" HTTP/1.1 200 OK ");
EXPECT_TRUE(result.has_value());
EXPECT_TRUE(result.value().protocol_version == "HTTP/1.1");
EXPECT_TRUE(result.value().status_code == 200);
EXPECT_TRUE(result.value().reason == "OK");
}
// ----------------------------------------------------------------------------
// HttpParser::parse_header_fields
// ----------------------------------------------------------------------------
TEST(HttpParser_parse_header_fields, parse_simple_case)
{
string str = "Content-Encoding: gzip\r\n"
"Content-Length: 14\r\n";
auto result = HttpParser::parse_header_fields(str);
ASSERT_TRUE(result.has_value()) << "Parsing should succeed with comprehensive headers.";
auto& fields = *result;
EXPECT_EQ(fields.count_key("Content-Encoding"), 1u)
<< "Content-Encoding should be present once.";
EXPECT_EQ(fields.get_first("Content-Encoding"), "gzip");
EXPECT_EQ(fields.get_first("Content-Length"), "14");
}
TEST(HttpParser_parse_header_fields, parse_valid_headers_case_insensitive)
{
string str = "content-type: text/html\r\n"
"CONTENT-LENGTH: 123\r\n";
auto result = HttpParser::parse_header_fields(str);
ASSERT_TRUE(result.has_value()) << "Parsing should succeed.";
auto& fields = *result;
EXPECT_EQ(fields.get_first("CONTENT-Type"), "text/html");
EXPECT_EQ(fields.get_first("CONTENT-Length"), "123");
}
TEST(HttpParser_parse_header_fields, parse_header_fields_with_multiple_same_keys_case_insensitive)
{
string str = "set-cookie: id=123\r\n"
"Set-Cookie: token=abc\r\n";
auto result = HttpParser::parse_header_fields(str);
ASSERT_TRUE(result.has_value()) << "Parsing should succeed.";
auto& fields = *result;
auto values = fields.get("Set-Cookie");
ASSERT_EQ(values.size(), 2u);
EXPECT_TRUE(std::find(values.begin(), values.end(), "id=123") != values.end());
EXPECT_TRUE(std::find(values.begin(), values.end(), "token=abc") != values.end());
}
TEST(HttpParser_parse_header_fields, handle_empty_field_name)
{
string str = ": No-Name-Header-Value\r\n";
auto result = HttpParser::parse_header_fields(str);
ASSERT_FALSE(result.has_value()) << "Parsing should fail due to empty field name.";
}
TEST(HttpParser_parse_header_fields, parse_empty_field_value)
{
string str = "X-Custom-Header:\r\n";
auto result = HttpParser::parse_header_fields(str);
ASSERT_TRUE(result.has_value()) << "Parsing should succeed.";
auto& fields = *result;
EXPECT_EQ(fields.count_key("X-Custom-Header"), 1) << "X-Custom-Header should exist.";
EXPECT_EQ(fields.get_first("X-Custom-Header"), "") << "X-Custom-Header value should be empty.";
}
TEST(HttpParser_parse_header_fields, parse_header_missing_new_line)
{
string str = "X-Custom-Header: X";
auto result = HttpParser::parse_header_fields(str);
ASSERT_TRUE(result.has_value()) << "Parsing should succeed.";
auto& fields = *result;
EXPECT_EQ(fields.count_key("X-Custom-Header"), 1) << "X-Custom-Header should exist.";
EXPECT_EQ(fields.get_first("X-Custom-Header"), "X") << "X-Custom-Header value should be empty.";
}
TEST(HttpParser_parse_header_fields, no_http_status_line)
{
string str = "Content-Type: application/json\r\n";
auto result = HttpParser::parse_header_fields(str);
ASSERT_TRUE(result.has_value()) << "Parsing should not skip the non-HTTP status line.";
auto& fields = *result;
EXPECT_EQ(fields.get_first("Content-Type"), "application/json");
}