@@ -102,6 +102,117 @@ TEST_SUITE("[MESSAGE]")
102102 }
103103 }
104104
105+ TEST_CASE (" parse bad requests" )
106+ {
107+ {
108+ std::string bad_req = " SHUV / HTTP/1.1\r\n " ;
109+ bad_req += " Host: developer.mozilla.org\r\n " ;
110+ bad_req += " User-Agent: curl/8.6.0\r\n " ;
111+ bad_req += " Content-Type: application/json\r\n " ;
112+ bad_req += " Content-Length: 32\r\n " ;
113+ bad_req += " \r\n " ;
114+ bad_req += " {\" name\" : \" Obi\" , \" creed\" : \" Jedi\" }" ;
115+
116+ http::request req;
117+ std::error_code ec{};
118+ bool finished = http::parser<http::request>{}.parse (req, bad_req, ec);
119+ REQUIRE (ec == http::http_read_bad_method);
120+ }
121+
122+ {
123+ std::string bad_req = " POST / HTTP/2.1\r\n " ;
124+ bad_req += " Host: developer.mozilla.org\r\n " ;
125+ bad_req += " User-Agent: curl/8.6.0\r\n " ;
126+ bad_req += " Content-Type: application/json\r\n " ;
127+ bad_req += " Content-Length: 32\r\n " ;
128+ bad_req += " \r\n " ;
129+ bad_req += " {\" name\" : \" Obi\" , \" creed\" : \" Jedi\" }" ;
130+
131+ http::request req;
132+ std::error_code ec{};
133+ bool finished = http::parser<http::request>{}.parse (req, bad_req, ec);
134+ REQUIRE (ec == http::http_read_unsupported_http_version);
135+ }
136+
137+ {
138+ std::string bad_req = " POST / HTTP/11\r\n " ;
139+ bad_req += " Host: developer.mozilla.org\r\n " ;
140+ bad_req += " User-Agent: curl/8.6.0\r\n " ;
141+ bad_req += " Content-Type: application/json\r\n " ;
142+ bad_req += " Content-Length: 32\r\n " ;
143+ bad_req += " \r\n " ;
144+ bad_req += " {\" name\" : \" Obi\" , \" creed\" : \" Jedi\" }" ;
145+
146+ http::request req;
147+ std::error_code ec{};
148+ bool finished = http::parser<http::request>{}.parse (req, bad_req, ec);
149+ REQUIRE (ec == http::http_read_unsupported_http_version);
150+ }
151+
152+ {
153+ std::string bad_req = " POST / PROTOCOL/1.1\r\n " ;
154+ bad_req += " Host: developer.mozilla.org\r\n " ;
155+ bad_req += " User-Agent: curl/8.6.0\r\n " ;
156+ bad_req += " Content-Type: application/json\r\n " ;
157+ bad_req += " Content-Length: 32\r\n " ;
158+ bad_req += " \r\n " ;
159+ bad_req += " {\" name\" : \" Obi\" , \" creed\" : \" Jedi\" }" ;
160+
161+ http::request req;
162+ std::error_code ec{};
163+ bool finished = http::parser<http::request>{}.parse (req, bad_req, ec);
164+ REQUIRE (ec == http::http_read_unsupported_http_version);
165+ }
166+
167+ {
168+ std::string bad_req = " POST / HTTP/1.1\r\n " ;
169+ bad_req += " Host - developer.mozilla.org\r\n " ;
170+ bad_req += " User-Agent: curl/8.6.0\r\n " ;
171+ bad_req += " Content-Type: application/json\r\n " ;
172+ bad_req += " Content-Length: 32\r\n " ;
173+ bad_req += " \r\n " ;
174+ bad_req += " {\" name\" : \" Obi\" , \" creed\" : \" Jedi\" }" ;
175+
176+ http::request req;
177+ std::error_code ec{};
178+ bool finished = http::parser<http::request>{}.parse (req, bad_req, ec);
179+ REQUIRE (ec == http::http_read_header_kv_delimiter_not_found);
180+ }
181+
182+ {
183+ std::string bad_req = " POST / HTTP/1.1\r\n " ;
184+ bad_req += " Host : developer.mozilla.org\r\n " ;
185+ bad_req += " User-Agent: curl/8.6.0\r\n " ;
186+ bad_req += " Content-Type: application/json\r\n " ;
187+ bad_req += " Content-Length: 32\r\n " ;
188+ bad_req += " Sith-Code: 66\r\n " ;
189+ bad_req += " \r\n " ;
190+ bad_req += " {\" name\" : \" Obi\" , \" creed\" : \" Jedi\" }" ;
191+
192+ http::request req;
193+ std::error_code ec{};
194+ bool finished = http::parser<http::request>{}.parse (req, bad_req, ec);
195+ REQUIRE (ec == http::http_read_header_unsupported_field);
196+ }
197+
198+ {
199+ std::string bad_req = " POST / HTTP/1.1\r\n " ;
200+ bad_req += " Host: developer.mozilla.org\r\n " ;
201+ bad_req += " User-Agent: curl/8.6.0\r\n " ;
202+ bad_req += " Content-Type: application/json\r\n " ;
203+ bad_req += " Content-Length: 64\r\n " ;
204+ bad_req += " \r\n " ;
205+ bad_req += " {\" name\" : \" Obi\" , \" creed\" : \" Jedi\" }" ;
206+
207+ http::request req;
208+ std::error_code ec{};
209+ bool finished = http::parser<http::request>{}.parse (req, bad_req, ec);
210+ REQUIRE (!bool (ec));
211+ REQUIRE (!finished);
212+ REQUIRE (bad_req.empty ());
213+ }
214+ }
215+
105216 TEST_CASE (" serialise bad requests" )
106217 {
107218 http::request req;
0 commit comments