blob: 9e79dbd4513772c7a7768b99b1853ce388c5a6b0 [file] [log] [blame]
Nan Zhou77665bd2022-10-12 20:28:58 +00001#include "http_request.hpp"
Nan Zhouca0cb4c2022-06-21 17:47:09 +00002#include "multipart_parser.hpp"
3
Nan Zhoud5c80ad2022-07-11 01:16:31 +00004#include <boost/beast/http/fields.hpp>
Ed Tanousaf4edf62020-07-21 08:46:25 -07005
Ed Tanousf0b59af2024-03-20 13:38:04 -07006#include <iterator>
Nan Zhoud5c80ad2022-07-11 01:16:31 +00007#include <string_view>
Nan Zhouca0cb4c2022-06-21 17:47:09 +00008#include <system_error>
Nan Zhoud5c80ad2022-07-11 01:16:31 +00009#include <vector>
Ed Tanousaf4edf62020-07-21 08:46:25 -070010
Nan Zhoud5c80ad2022-07-11 01:16:31 +000011#include <gtest/gtest.h> // IWYU pragma: keep
12
13// IWYU pragma: no_include <gtest/gtest-message.h>
14// IWYU pragma: no_include <gtest/gtest-test-part.h>
15// IWYU pragma: no_include "gtest/gtest_pred_impl.h"
16// IWYU pragma: no_include <boost/beast/http/impl/fields.hpp>
17// IWYU pragma: no_include <boost/intrusive/detail/list_iterator.hpp>
18// IWYU pragma: no_include <boost/intrusive/detail/tree_iterator.hpp>
Ed Tanousaf4edf62020-07-21 08:46:25 -070019
Nan Zhoue52d1562022-06-21 17:47:15 +000020namespace
21{
22using ::testing::Test;
23
24class MultipartTest : public Test
Ed Tanousaf4edf62020-07-21 08:46:25 -070025{
26 public:
Ed Tanousaf4edf62020-07-21 08:46:25 -070027 MultipartParser parser;
28 std::error_code ec;
29};
30
31TEST_F(MultipartTest, TestGoodMultipartParser)
32{
Ed Tanous6fb96ce2024-01-28 21:30:06 -080033 std::string_view body =
34 "-----------------------------d74496d66958873e\r\n"
35 "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n"
36 "111111111111111111111111112222222222222222222222222222222\r\n"
37 "-----------------------------d74496d66958873e\r\n"
38 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
39 "{\r\n-----------------------------d74496d66958873e123456\r\n"
40 "-----------------------------d74496d66958873e\r\n"
41 "Content-Disposition: form-data; name=\"Test3\"\r\n\r\n"
42 "{\r\n--------d74496d6695887}\r\n"
43 "-----------------------------d74496d66958873e--\r\n";
Ed Tanousaf4edf62020-07-21 08:46:25 -070044
Ed Tanous6fb96ce2024-01-28 21:30:06 -080045 crow::Request reqIn(body, ec);
Ed Tanousaf4edf62020-07-21 08:46:25 -070046
Ed Tanous6fb96ce2024-01-28 21:30:06 -080047 reqIn.addHeader("Content-Type",
48 "multipart/form-data; "
49 "boundary=---------------------------d74496d66958873e");
50
Ed Tanousaf4edf62020-07-21 08:46:25 -070051 ParserError rc = parser.parse(reqIn);
52 ASSERT_EQ(rc, ParserError::PARSER_SUCCESS);
53
Nan Zhoucc606192022-06-21 17:47:19 +000054 EXPECT_EQ(parser.boundary,
Ed Tanousaf4edf62020-07-21 08:46:25 -070055 "\r\n-----------------------------d74496d66958873e");
Nan Zhoucc606192022-06-21 17:47:19 +000056 EXPECT_EQ(parser.mime_fields.size(), 3);
Ed Tanousaf4edf62020-07-21 08:46:25 -070057
Nan Zhoucc606192022-06-21 17:47:19 +000058 EXPECT_EQ(parser.mime_fields[0].fields.at("Content-Disposition"),
Ed Tanousaf4edf62020-07-21 08:46:25 -070059 "form-data; name=\"Test1\"");
Nan Zhoucc606192022-06-21 17:47:19 +000060 EXPECT_EQ(parser.mime_fields[0].content,
Ed Tanousaf4edf62020-07-21 08:46:25 -070061 "111111111111111111111111112222222222222222222222222222222");
62
Nan Zhoucc606192022-06-21 17:47:19 +000063 EXPECT_EQ(parser.mime_fields[1].fields.at("Content-Disposition"),
Ed Tanousaf4edf62020-07-21 08:46:25 -070064 "form-data; name=\"Test2\"");
Nan Zhoucc606192022-06-21 17:47:19 +000065 EXPECT_EQ(parser.mime_fields[1].content,
Ed Tanousaf4edf62020-07-21 08:46:25 -070066 "{\r\n-----------------------------d74496d66958873e123456");
Nan Zhoucc606192022-06-21 17:47:19 +000067 EXPECT_EQ(parser.mime_fields[2].fields.at("Content-Disposition"),
Ed Tanousaf4edf62020-07-21 08:46:25 -070068 "form-data; name=\"Test3\"");
Nan Zhoucc606192022-06-21 17:47:19 +000069 EXPECT_EQ(parser.mime_fields[2].content, "{\r\n--------d74496d6695887}");
Ed Tanousaf4edf62020-07-21 08:46:25 -070070}
71
72TEST_F(MultipartTest, TestBadMultipartParser1)
73{
Ed Tanous6fb96ce2024-01-28 21:30:06 -080074 std::string_view body =
75 "-----------------------------d74496d66958873e\r\n"
76 "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n"
77 "1234567890\r\n"
78 "-----------------------------d74496d66958873e\r-\r\n";
Ed Tanousaf4edf62020-07-21 08:46:25 -070079
Ed Tanous6fb96ce2024-01-28 21:30:06 -080080 crow::Request reqIn(body, ec);
Ed Tanousaf4edf62020-07-21 08:46:25 -070081
Ed Tanous6fb96ce2024-01-28 21:30:06 -080082 reqIn.addHeader("Content-Type",
83 "multipart/form-data; "
84 "boundary=---------------------------d74496d66958873e");
85
Ed Tanousaf4edf62020-07-21 08:46:25 -070086 ParserError rc = parser.parse(reqIn);
Ed Tanousaf4edf62020-07-21 08:46:25 -070087
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +020088 EXPECT_EQ(rc, ParserError::ERROR_UNEXPECTED_END_OF_INPUT);
Ed Tanousaf4edf62020-07-21 08:46:25 -070089}
90
91TEST_F(MultipartTest, TestBadMultipartParser2)
92{
Ed Tanous6fb96ce2024-01-28 21:30:06 -080093 std::string_view body =
94 "-----------------------------d74496d66958873e\r\n"
95 "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n"
96 "abcd\r\n"
97 "-----------------------------d74496d66958873e-\r\n";
98 crow::Request reqIn(body, ec);
Ed Tanousaf4edf62020-07-21 08:46:25 -070099
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800100 reqIn.addHeader("Content-Type",
101 "multipart/form-data; "
102 "boundary=---------------------------d74496d66958873e");
Ed Tanousaf4edf62020-07-21 08:46:25 -0700103
Ed Tanousaf4edf62020-07-21 08:46:25 -0700104 ParserError rc = parser.parse(reqIn);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700105
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200106 EXPECT_EQ(rc, ParserError::ERROR_UNEXPECTED_END_OF_INPUT);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700107}
108
109TEST_F(MultipartTest, TestErrorBoundaryFormat)
110{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800111 std::string_view body =
112 "-----------------------------d74496d66958873e\r\n"
113 "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n"
114 "{\"Key1\": 11223333333333333333333333333333333333333333}\r\n"
115 "-----------------------------d74496d66958873e\r\n"
116 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
117 "123456\r\n"
118 "-----------------------------d74496d66958873e--\r\n";
Ed Tanousaf4edf62020-07-21 08:46:25 -0700119
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800120 crow::Request reqIn(body, ec);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700121
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800122 reqIn.addHeader("Content-Type",
123 "multipart/form-data; "
124 "boundary+=-----------------------------d74496d66958873e");
125
Nan Zhoucc606192022-06-21 17:47:19 +0000126 EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_BOUNDARY_FORMAT);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700127}
128
129TEST_F(MultipartTest, TestErrorBoundaryCR)
130{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800131 std::string_view body =
132 "-----------------------------d74496d66958873e"
133 "Content-Disposition: form-data; name=\"Test1\"\r\n\r"
134 "{\"Key1\": 112233}\r\n"
135 "-----------------------------d74496d66958873e\r\n"
136 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
137 "123456\r\n"
138 "-----------------------------d74496d66958873e--\r\n";
139 crow::Request reqIn(body, ec);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700140
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800141 reqIn.addHeader("Content-Type",
142 "multipart/form-data; "
143 "boundary=---------------------------d74496d66958873e");
Ed Tanousaf4edf62020-07-21 08:46:25 -0700144
Nan Zhoucc606192022-06-21 17:47:19 +0000145 EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_BOUNDARY_CR);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700146}
147
148TEST_F(MultipartTest, TestErrorBoundaryLF)
149{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800150 std::string_view body =
151 "-----------------------------d74496d66958873e\r"
152 "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n"
153 "{\"Key1\": 112233}\r\n"
154 "-----------------------------d74496d66958873e\r\n"
155 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
156 "123456\r\n"
157 "-----------------------------d74496d66958873e--\r\n";
Ed Tanousaf4edf62020-07-21 08:46:25 -0700158
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800159 crow::Request reqIn(body, ec);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700160
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800161 reqIn.addHeader("Content-Type",
162 "multipart/form-data; "
163 "boundary=---------------------------d74496d66958873e");
164
Nan Zhoucc606192022-06-21 17:47:19 +0000165 EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_BOUNDARY_LF);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700166}
167
168TEST_F(MultipartTest, TestErrorBoundaryData)
169{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800170 std::string_view body =
171 "-----------------------------d74496d66958873e\r\n"
172 "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n"
173 "{\"Key1\": 112233}\r\n"
174 "-----------------------------d74496d66958873e\r\n"
175 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
176 "123456\r\n"
177 "-----------------------------d74496d66958873e--\r\n";
Ed Tanousaf4edf62020-07-21 08:46:25 -0700178
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800179 crow::Request reqIn(body, ec);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700180
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800181 reqIn.addHeader("Content-Type",
182 "multipart/form-data; "
183 "boundary=---------------------------d7449sd6d66958873e");
184
Nan Zhoucc606192022-06-21 17:47:19 +0000185 EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_BOUNDARY_DATA);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700186}
187
188TEST_F(MultipartTest, TestErrorEmptyHeader)
189{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800190 std::string_view body =
191 "-----------------------------d74496d66958873e\r\n"
192 ": form-data; name=\"Test1\"\r\n"
193 "{\"Key1\": 112233}\r\n"
194 "-----------------------------d74496d66958873e\r\n"
195 "Content-Disposition: form-data; name=\"Test2\"\r\n"
196 "123456\r\n"
197 "-----------------------------d74496d66958873e--\r\n";
198 crow::Request reqIn(body, ec);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700199
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800200 reqIn.addHeader("Content-Type",
201 "multipart/form-data; "
202 "boundary=---------------------------d74496d66958873e");
Ed Tanousaf4edf62020-07-21 08:46:25 -0700203
Nan Zhoucc606192022-06-21 17:47:19 +0000204 EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_EMPTY_HEADER);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700205}
206
207TEST_F(MultipartTest, TestErrorHeaderName)
208{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800209 std::string_view body =
210 "-----------------------------d74496d66958873e\r\n"
211 "Content-!!Disposition: form-data; name=\"Test1\"\r\n"
212 "{\"Key1\": 112233}\r\n"
213 "-----------------------------d74496d66958873e\r\n"
214 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
215 "123456\r\n"
216 "-----------------------------d74496d66958873e--\r\n";
217 crow::Request reqIn(body, ec);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700218
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800219 reqIn.addHeader("Content-Type",
220 "multipart/form-data; "
221 "boundary=---------------------------d74496d66958873e");
Ed Tanousaf4edf62020-07-21 08:46:25 -0700222
Nan Zhoucc606192022-06-21 17:47:19 +0000223 EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_HEADER_NAME);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700224}
225
226TEST_F(MultipartTest, TestErrorHeaderValue)
227{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800228 std::string_view body =
229 "-----------------------------d74496d66958873e\r\n"
230 "Content-Disposition: form-data; name=\"Test1\"\r"
231 "{\"Key1\": 112233}\r\n"
232 "-----------------------------d74496d66958873e\r\n"
233 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
234 "123456\r\n"
235 "-----------------------------d74496d66958873e--\r\n";
Ed Tanousaf4edf62020-07-21 08:46:25 -0700236
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800237 crow::Request reqIn(body, ec);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700238
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800239 reqIn.addHeader("Content-Type",
240 "multipart/form-data; "
241 "boundary=---------------------------d74496d66958873e");
242
Nan Zhoucc606192022-06-21 17:47:19 +0000243 EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_HEADER_VALUE);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700244}
245
246TEST_F(MultipartTest, TestErrorHeaderEnding)
247{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800248 std::string_view body =
249 "-----------------------------d74496d66958873e\r\n"
250 "Content-Disposition: form-data; name=\"Test1\"\r\n\r"
251 "{\"Key1\": 112233}\r\n"
252 "-----------------------------d74496d66958873e\r\n"
253 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
254 "123456\r\n"
255 "-----------------------------d74496d66958873e--\r\n";
Ed Tanousaf4edf62020-07-21 08:46:25 -0700256
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800257 crow::Request reqIn(body, ec);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700258
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800259 reqIn.addHeader("Content-Type",
260 "multipart/form-data; "
261 "boundary=---------------------------d74496d66958873e");
262
Nan Zhoucc606192022-06-21 17:47:19 +0000263 EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_HEADER_ENDING);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700264}
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200265
266TEST_F(MultipartTest, TestGoodMultipartParserMultipleHeaders)
267{
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400268 std::string_view body =
269 "-----------------------------d74496d66958873e\r\n"
270 "Content-Disposition: form-data; name=\"Test1\"\r\n"
271 "Other-Header: value=\"v1\"\r\n"
272 "\r\n"
273 "Data1\r\n"
274 "-----------------------------d74496d66958873e--";
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200275
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800276 crow::Request reqIn(body, ec);
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200277
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800278 reqIn.addHeader("Content-Type",
279 "multipart/form-data; "
280 "boundary=---------------------------d74496d66958873e");
281
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200282 ParserError rc = parser.parse(reqIn);
283 ASSERT_EQ(rc, ParserError::PARSER_SUCCESS);
284
285 EXPECT_EQ(parser.boundary,
286 "\r\n-----------------------------d74496d66958873e");
287 ASSERT_EQ(parser.mime_fields.size(), 1);
288
289 EXPECT_EQ(parser.mime_fields[0].fields.at("Content-Disposition"),
290 "form-data; name=\"Test1\"");
291 EXPECT_EQ(parser.mime_fields[0].fields.at("Other-Header"), "value=\"v1\"");
292 EXPECT_EQ(parser.mime_fields[0].content, "Data1");
293}
294
295TEST_F(MultipartTest, TestErrorHeaderWithoutColon)
296{
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400297 std::string_view body =
298 "----end\r\n"
299 "abc\r\n"
300 "\r\n"
301 "Data1\r\n"
302 "----end--\r\n";
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800303 crow::Request reqIn(body, ec);
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200304
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800305 reqIn.addHeader("Content-Type", "multipart/form-data; "
306 "boundary=--end");
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200307
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200308 EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_UNEXPECTED_END_OF_HEADER);
309}
310
311TEST_F(MultipartTest, TestUnknownHeaderIsCorrectlyParsed)
312{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800313 std::string_view body =
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200314 "----end\r\n"
315 "t-DiPpcccc:cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccgcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccaaaaaa\r\n"
316 "\r\n"
317 "Data1\r\n"
318 "----end--\r\n";
319
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800320 crow::Request reqIn(body, ec);
321
322 reqIn.addHeader("Content-Type", "multipart/form-data; "
323 "boundary=--end");
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200324 ParserError rc = parser.parse(reqIn);
325
326 ASSERT_EQ(rc, ParserError::PARSER_SUCCESS);
327
328 EXPECT_EQ(parser.boundary, "\r\n----end");
329 ASSERT_EQ(parser.mime_fields.size(), 1);
330
331 EXPECT_EQ(
332 parser.mime_fields[0].fields.at("t-DiPpcccc"),
333 "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccgcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccaaaaaa");
334 EXPECT_EQ(parser.mime_fields[0].content, "Data1");
335}
336
337TEST_F(MultipartTest, TestErrorMissingSeparatorBetweenMimeFieldsAndData)
338{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800339 std::string_view body =
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200340 "-----------------------------d74496d66958873e\r\n"
341 "t-DiPpcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccgcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccaaaaaa\r\n"
342 "Data1"
343 "-----------------------------d74496d66958873e--";
344
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800345 crow::Request reqIn(body, ec);
346
347 reqIn.addHeader(
348 "Content-Type",
349 "multipart/form-data; boundary=---------------------------d74496d66958873e");
350
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200351 ParserError rc = parser.parse(reqIn);
352
353 EXPECT_EQ(rc, ParserError::ERROR_UNEXPECTED_END_OF_HEADER);
354}
355
356TEST_F(MultipartTest, TestDataWithoutMimeFields)
357{
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400358 std::string_view body =
359 "-----------------------------d74496d66958873e\r\n"
360 "\r\n"
361 "Data1\r\n"
362 "-----------------------------d74496d66958873e--";
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800363
364 crow::Request reqIn(body, ec);
365
366 reqIn.addHeader(
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200367 "Content-Type",
368 "multipart/form-data; boundary=---------------------------d74496d66958873e");
369
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200370 ParserError rc = parser.parse(reqIn);
371
372 ASSERT_EQ(rc, ParserError::PARSER_SUCCESS);
373
374 EXPECT_EQ(parser.boundary,
375 "\r\n-----------------------------d74496d66958873e");
376 ASSERT_EQ(parser.mime_fields.size(), 1);
377
378 EXPECT_EQ(std::distance(parser.mime_fields[0].fields.begin(),
379 parser.mime_fields[0].fields.end()),
380 0);
381 EXPECT_EQ(parser.mime_fields[0].content, "Data1");
382}
383
384TEST_F(MultipartTest, TestErrorMissingFinalBoundry)
385{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800386 std::string_view body =
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200387 "----XX\r\n"
388 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
389 "t-DiPpccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccAAAAAAAAAAAAAAABCDz\r\n"
390 "\335\r\n\r\n";
391
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800392 crow::Request reqIn(body, ec);
393
394 reqIn.addHeader("Content-Type", "multipart/form-data; boundary=--XX");
395
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200396 ParserError rc = parser.parse(reqIn);
397
398 EXPECT_EQ(rc, ParserError::ERROR_UNEXPECTED_END_OF_INPUT);
399}
400
401TEST_F(MultipartTest, TestIgnoreDataAfterFinalBoundary)
402{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800403 std::string_view body =
404 "----XX\r\n"
405 "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n"
406 "Data1\r\n"
407 "----XX--\r\n"
408 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
409 "Data2\r\n"
410 "----XX--\r\n";
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200411
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800412 crow::Request reqIn(body, ec);
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200413
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800414 reqIn.addHeader("Content-Type", "multipart/form-data; boundary=--XX");
415
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200416 ParserError rc = parser.parse(reqIn);
417
418 ASSERT_EQ(rc, ParserError::PARSER_SUCCESS);
419
420 EXPECT_EQ(parser.boundary, "\r\n----XX");
421 EXPECT_EQ(parser.mime_fields.size(), 1);
422
423 EXPECT_EQ(parser.mime_fields[0].fields.at("Content-Disposition"),
424 "form-data; name=\"Test1\"");
425 EXPECT_EQ(parser.mime_fields[0].content, "Data1");
426}
427
428TEST_F(MultipartTest, TestFinalBoundaryIsCorrectlyRecognized)
429{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800430 std::string_view body =
431 "----XX\r\n"
432 "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n"
433 "Data1\r\n"
434 "----XX-abc-\r\n"
435 "StillData1\r\n"
436 "----XX--\r\n";
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200437
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800438 crow::Request reqIn(body, ec);
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200439
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800440 reqIn.addHeader("Content-Type", "multipart/form-data; boundary=--XX");
441
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200442 ParserError rc = parser.parse(reqIn);
443
444 ASSERT_EQ(rc, ParserError::PARSER_SUCCESS);
445
446 EXPECT_EQ(parser.boundary, "\r\n----XX");
447 EXPECT_EQ(parser.mime_fields.size(), 1);
448
449 EXPECT_EQ(parser.mime_fields[0].fields.at("Content-Disposition"),
450 "form-data; name=\"Test1\"");
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400451 EXPECT_EQ(parser.mime_fields[0].content,
452 "Data1\r\n"
453 "----XX-abc-\r\n"
454 "StillData1");
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200455}
456
457} // namespace