Repository navigation
Expand file tree
/
Copy pathhttp1_error_synth_test.cpp
More file actions
127 lines (106 loc) · 4.59 KB
/
Copy pathhttp1_error_synth_test.cpp
File metadata and controls
127 lines (106 loc) · 4.59 KB
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
/*
This file is part of libhttpserver
Copyright (C) 2011-2026 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the file
LICENSE in the distribution; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
// TASK-108 step 6: the bare error response serializer
// (http1_error_synth). The engine answers without routing when the head
// never parsed or the framing was rejected; the synth pins that wire
// form: version-mirrored status line, "Connection: close",
// "Content-Length: 0", empty body. Parsed through the parity response
// frame parser like every other wire observation. Header-only surface;
// the LDADD stays empty.
#include <string>
#include <vector>
#include <httpserver/detail/http1_error_synth.hpp>
#include <parity/response_frame.hpp>
#include "./littletest.hpp"
namespace {
namespace http = httpserver::http;
using httpserver::detail::http1_error_synth;
using parity::observed_response;
using parity::response_frame_parser;
// Serializes one error and parses it back.
observed_response wire_for(http::protocol version, std::uint16_t code) {
const std::string bytes = http1_error_synth::serialize(version, code, {});
response_frame_parser parser;
const std::vector<observed_response> done = parser.feed(bytes);
if (done.size() != 1) {
return observed_response{};
}
return done.front();
}
bool has_connection_close(const observed_response& response) {
for (const parity::observed_header& header : response.headers) {
if (header.name == "Connection"
&& header.value.find("close") != std::string::npos) {
return true;
}
}
return false;
}
} // namespace
LT_BEGIN_SUITE(http1_error_synth_suite)
void set_up() {
}
void tear_down() {
}
LT_END_SUITE(http1_error_synth_suite)
// The malformed-head answer: 400, empty length-framed body, close.
LT_BEGIN_AUTO_TEST(http1_error_synth_suite, bad_request_400_http_1_1)
const observed_response response =
wire_for(http::protocol::http_1_1, 400);
LT_CHECK_EQ(response.status, 400);
LT_CHECK(response.raw_status_line.find("HTTP/1.1") == 0);
LT_CHECK_EQ(response.body, std::string(""));
LT_CHECK_EQ(response.framing, std::string("content-length"));
LT_CHECK(has_connection_close(response));
LT_END_AUTO_TEST(bad_request_400_http_1_1)
// The head-budget answer: 431.
LT_BEGIN_AUTO_TEST(http1_error_synth_suite, headers_too_large_431)
const observed_response response =
wire_for(http::protocol::http_1_1, 431);
LT_CHECK_EQ(response.status, 431);
LT_CHECK_EQ(response.body, std::string(""));
LT_CHECK(has_connection_close(response));
LT_END_AUTO_TEST(headers_too_large_431)
// The unsupported-coding answer: 501.
LT_BEGIN_AUTO_TEST(http1_error_synth_suite, not_implemented_501)
const observed_response response =
wire_for(http::protocol::http_1_1, 501);
LT_CHECK_EQ(response.status, 501);
LT_CHECK(has_connection_close(response));
LT_END_AUTO_TEST(not_implemented_501)
// The overload answer: 503.
LT_BEGIN_AUTO_TEST(http1_error_synth_suite, unavailable_503)
const observed_response response =
wire_for(http::protocol::http_1_1, 503);
LT_CHECK_EQ(response.status, 503);
LT_CHECK(has_connection_close(response));
LT_END_AUTO_TEST(unavailable_503)
// On HTTP/1.0 the status line mirrors the request version; the empty
// body keeps its explicit zero length (valid 1.0 framing) and the
// close is signaled through the Connection field.
LT_BEGIN_AUTO_TEST(http1_error_synth_suite, http_1_0_mirrors_version)
const observed_response response =
wire_for(http::protocol::http_1_0, 400);
LT_CHECK_EQ(response.status, 400);
LT_CHECK(response.raw_status_line.find("HTTP/1.0") == 0);
LT_CHECK_EQ(response.framing, std::string("content-length"));
LT_CHECK(has_connection_close(response));
LT_END_AUTO_TEST(http_1_0_mirrors_version)
LT_BEGIN_AUTO_TEST_ENV()
AUTORUN_TESTS()
LT_END_AUTO_TEST_ENV()