blob: 59e8345755ef7530bd26f5e6f5362e8b0cc8c2da [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{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800268 std::string_view body = "-----------------------------d74496d66958873e\r\n"
269 "Content-Disposition: form-data; name=\"Test1\"\r\n"
270 "Other-Header: value=\"v1\"\r\n"
271 "\r\n"
272 "Data1\r\n"
273 "-----------------------------d74496d66958873e--";
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200274
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800275 crow::Request reqIn(body, ec);
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200276
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800277 reqIn.addHeader("Content-Type",
278 "multipart/form-data; "
279 "boundary=---------------------------d74496d66958873e");
280
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200281 ParserError rc = parser.parse(reqIn);
282 ASSERT_EQ(rc, ParserError::PARSER_SUCCESS);
283
284 EXPECT_EQ(parser.boundary,
285 "\r\n-----------------------------d74496d66958873e");
286 ASSERT_EQ(parser.mime_fields.size(), 1);
287
288 EXPECT_EQ(parser.mime_fields[0].fields.at("Content-Disposition"),
289 "form-data; name=\"Test1\"");
290 EXPECT_EQ(parser.mime_fields[0].fields.at("Other-Header"), "value=\"v1\"");
291 EXPECT_EQ(parser.mime_fields[0].content, "Data1");
292}
293
294TEST_F(MultipartTest, TestErrorHeaderWithoutColon)
295{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800296 std::string_view body = "----end\r\n"
297 "abc\r\n"
298 "\r\n"
299 "Data1\r\n"
300 "----end--\r\n";
301 crow::Request reqIn(body, ec);
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200302
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800303 reqIn.addHeader("Content-Type", "multipart/form-data; "
304 "boundary=--end");
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200305
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200306 EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_UNEXPECTED_END_OF_HEADER);
307}
308
309TEST_F(MultipartTest, TestUnknownHeaderIsCorrectlyParsed)
310{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800311 std::string_view body =
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200312 "----end\r\n"
313 "t-DiPpcccc:cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccgcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccaaaaaa\r\n"
314 "\r\n"
315 "Data1\r\n"
316 "----end--\r\n";
317
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800318 crow::Request reqIn(body, ec);
319
320 reqIn.addHeader("Content-Type", "multipart/form-data; "
321 "boundary=--end");
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200322 ParserError rc = parser.parse(reqIn);
323
324 ASSERT_EQ(rc, ParserError::PARSER_SUCCESS);
325
326 EXPECT_EQ(parser.boundary, "\r\n----end");
327 ASSERT_EQ(parser.mime_fields.size(), 1);
328
329 EXPECT_EQ(
330 parser.mime_fields[0].fields.at("t-DiPpcccc"),
331 "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccgcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccaaaaaa");
332 EXPECT_EQ(parser.mime_fields[0].content, "Data1");
333}
334
335TEST_F(MultipartTest, TestErrorMissingSeparatorBetweenMimeFieldsAndData)
336{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800337 std::string_view body =
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200338 "-----------------------------d74496d66958873e\r\n"
339 "t-DiPpcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccgcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccaaaaaa\r\n"
340 "Data1"
341 "-----------------------------d74496d66958873e--";
342
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800343 crow::Request reqIn(body, ec);
344
345 reqIn.addHeader(
346 "Content-Type",
347 "multipart/form-data; boundary=---------------------------d74496d66958873e");
348
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200349 ParserError rc = parser.parse(reqIn);
350
351 EXPECT_EQ(rc, ParserError::ERROR_UNEXPECTED_END_OF_HEADER);
352}
353
354TEST_F(MultipartTest, TestDataWithoutMimeFields)
355{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800356 std::string_view body = "-----------------------------d74496d66958873e\r\n"
357 "\r\n"
358 "Data1\r\n"
359 "-----------------------------d74496d66958873e--";
360
361 crow::Request reqIn(body, ec);
362
363 reqIn.addHeader(
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200364 "Content-Type",
365 "multipart/form-data; boundary=---------------------------d74496d66958873e");
366
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200367 ParserError rc = parser.parse(reqIn);
368
369 ASSERT_EQ(rc, ParserError::PARSER_SUCCESS);
370
371 EXPECT_EQ(parser.boundary,
372 "\r\n-----------------------------d74496d66958873e");
373 ASSERT_EQ(parser.mime_fields.size(), 1);
374
375 EXPECT_EQ(std::distance(parser.mime_fields[0].fields.begin(),
376 parser.mime_fields[0].fields.end()),
377 0);
378 EXPECT_EQ(parser.mime_fields[0].content, "Data1");
379}
380
381TEST_F(MultipartTest, TestErrorMissingFinalBoundry)
382{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800383 std::string_view body =
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200384 "----XX\r\n"
385 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
386 "t-DiPpccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccAAAAAAAAAAAAAAABCDz\r\n"
387 "\335\r\n\r\n";
388
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800389 crow::Request reqIn(body, ec);
390
391 reqIn.addHeader("Content-Type", "multipart/form-data; boundary=--XX");
392
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200393 ParserError rc = parser.parse(reqIn);
394
395 EXPECT_EQ(rc, ParserError::ERROR_UNEXPECTED_END_OF_INPUT);
396}
397
398TEST_F(MultipartTest, TestIgnoreDataAfterFinalBoundary)
399{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800400 std::string_view body =
401 "----XX\r\n"
402 "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n"
403 "Data1\r\n"
404 "----XX--\r\n"
405 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
406 "Data2\r\n"
407 "----XX--\r\n";
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200408
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800409 crow::Request reqIn(body, ec);
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200410
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800411 reqIn.addHeader("Content-Type", "multipart/form-data; boundary=--XX");
412
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200413 ParserError rc = parser.parse(reqIn);
414
415 ASSERT_EQ(rc, ParserError::PARSER_SUCCESS);
416
417 EXPECT_EQ(parser.boundary, "\r\n----XX");
418 EXPECT_EQ(parser.mime_fields.size(), 1);
419
420 EXPECT_EQ(parser.mime_fields[0].fields.at("Content-Disposition"),
421 "form-data; name=\"Test1\"");
422 EXPECT_EQ(parser.mime_fields[0].content, "Data1");
423}
424
425TEST_F(MultipartTest, TestFinalBoundaryIsCorrectlyRecognized)
426{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800427 std::string_view body =
428 "----XX\r\n"
429 "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n"
430 "Data1\r\n"
431 "----XX-abc-\r\n"
432 "StillData1\r\n"
433 "----XX--\r\n";
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200434
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800435 crow::Request reqIn(body, ec);
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200436
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800437 reqIn.addHeader("Content-Type", "multipart/form-data; boundary=--XX");
438
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200439 ParserError rc = parser.parse(reqIn);
440
441 ASSERT_EQ(rc, ParserError::PARSER_SUCCESS);
442
443 EXPECT_EQ(parser.boundary, "\r\n----XX");
444 EXPECT_EQ(parser.mime_fields.size(), 1);
445
446 EXPECT_EQ(parser.mime_fields[0].fields.at("Content-Disposition"),
447 "form-data; name=\"Test1\"");
448 EXPECT_EQ(parser.mime_fields[0].content, "Data1\r\n"
449 "----XX-abc-\r\n"
450 "StillData1");
451}
452
453} // namespace