blob: c294b7be8c7fa3c78bc2a0c39e5bb9a8a1b637cd [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
Ed Tanous478b7ad2024-07-15 19:11:54 -070011#include <gtest/gtest.h>
Ed Tanousaf4edf62020-07-21 08:46:25 -070012
Nan Zhoue52d1562022-06-21 17:47:15 +000013namespace
14{
15using ::testing::Test;
16
17class MultipartTest : public Test
Ed Tanousaf4edf62020-07-21 08:46:25 -070018{
19 public:
Ed Tanousaf4edf62020-07-21 08:46:25 -070020 MultipartParser parser;
21 std::error_code ec;
22};
23
24TEST_F(MultipartTest, TestGoodMultipartParser)
25{
Ed Tanous6fb96ce2024-01-28 21:30:06 -080026 std::string_view body =
27 "-----------------------------d74496d66958873e\r\n"
28 "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n"
29 "111111111111111111111111112222222222222222222222222222222\r\n"
30 "-----------------------------d74496d66958873e\r\n"
31 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
32 "{\r\n-----------------------------d74496d66958873e123456\r\n"
33 "-----------------------------d74496d66958873e\r\n"
34 "Content-Disposition: form-data; name=\"Test3\"\r\n\r\n"
35 "{\r\n--------d74496d6695887}\r\n"
36 "-----------------------------d74496d66958873e--\r\n";
Ed Tanousaf4edf62020-07-21 08:46:25 -070037
Ed Tanous6fb96ce2024-01-28 21:30:06 -080038 crow::Request reqIn(body, ec);
Ed Tanousaf4edf62020-07-21 08:46:25 -070039
Ed Tanous6fb96ce2024-01-28 21:30:06 -080040 reqIn.addHeader("Content-Type",
41 "multipart/form-data; "
42 "boundary=---------------------------d74496d66958873e");
43
Ed Tanousaf4edf62020-07-21 08:46:25 -070044 ParserError rc = parser.parse(reqIn);
45 ASSERT_EQ(rc, ParserError::PARSER_SUCCESS);
46
Nan Zhoucc606192022-06-21 17:47:19 +000047 EXPECT_EQ(parser.boundary,
Ed Tanousaf4edf62020-07-21 08:46:25 -070048 "\r\n-----------------------------d74496d66958873e");
Nan Zhoucc606192022-06-21 17:47:19 +000049 EXPECT_EQ(parser.mime_fields.size(), 3);
Ed Tanousaf4edf62020-07-21 08:46:25 -070050
Nan Zhoucc606192022-06-21 17:47:19 +000051 EXPECT_EQ(parser.mime_fields[0].fields.at("Content-Disposition"),
Ed Tanousaf4edf62020-07-21 08:46:25 -070052 "form-data; name=\"Test1\"");
Nan Zhoucc606192022-06-21 17:47:19 +000053 EXPECT_EQ(parser.mime_fields[0].content,
Ed Tanousaf4edf62020-07-21 08:46:25 -070054 "111111111111111111111111112222222222222222222222222222222");
55
Nan Zhoucc606192022-06-21 17:47:19 +000056 EXPECT_EQ(parser.mime_fields[1].fields.at("Content-Disposition"),
Ed Tanousaf4edf62020-07-21 08:46:25 -070057 "form-data; name=\"Test2\"");
Nan Zhoucc606192022-06-21 17:47:19 +000058 EXPECT_EQ(parser.mime_fields[1].content,
Ed Tanousaf4edf62020-07-21 08:46:25 -070059 "{\r\n-----------------------------d74496d66958873e123456");
Nan Zhoucc606192022-06-21 17:47:19 +000060 EXPECT_EQ(parser.mime_fields[2].fields.at("Content-Disposition"),
Ed Tanousaf4edf62020-07-21 08:46:25 -070061 "form-data; name=\"Test3\"");
Nan Zhoucc606192022-06-21 17:47:19 +000062 EXPECT_EQ(parser.mime_fields[2].content, "{\r\n--------d74496d6695887}");
Ed Tanousaf4edf62020-07-21 08:46:25 -070063}
64
65TEST_F(MultipartTest, TestBadMultipartParser1)
66{
Ed Tanous6fb96ce2024-01-28 21:30:06 -080067 std::string_view body =
68 "-----------------------------d74496d66958873e\r\n"
69 "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n"
70 "1234567890\r\n"
71 "-----------------------------d74496d66958873e\r-\r\n";
Ed Tanousaf4edf62020-07-21 08:46:25 -070072
Ed Tanous6fb96ce2024-01-28 21:30:06 -080073 crow::Request reqIn(body, ec);
Ed Tanousaf4edf62020-07-21 08:46:25 -070074
Ed Tanous6fb96ce2024-01-28 21:30:06 -080075 reqIn.addHeader("Content-Type",
76 "multipart/form-data; "
77 "boundary=---------------------------d74496d66958873e");
78
Ed Tanousaf4edf62020-07-21 08:46:25 -070079 ParserError rc = parser.parse(reqIn);
Ed Tanousaf4edf62020-07-21 08:46:25 -070080
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +020081 EXPECT_EQ(rc, ParserError::ERROR_UNEXPECTED_END_OF_INPUT);
Ed Tanousaf4edf62020-07-21 08:46:25 -070082}
83
84TEST_F(MultipartTest, TestBadMultipartParser2)
85{
Ed Tanous6fb96ce2024-01-28 21:30:06 -080086 std::string_view body =
87 "-----------------------------d74496d66958873e\r\n"
88 "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n"
89 "abcd\r\n"
90 "-----------------------------d74496d66958873e-\r\n";
91 crow::Request reqIn(body, ec);
Ed Tanousaf4edf62020-07-21 08:46:25 -070092
Ed Tanous6fb96ce2024-01-28 21:30:06 -080093 reqIn.addHeader("Content-Type",
94 "multipart/form-data; "
95 "boundary=---------------------------d74496d66958873e");
Ed Tanousaf4edf62020-07-21 08:46:25 -070096
Ed Tanousaf4edf62020-07-21 08:46:25 -070097 ParserError rc = parser.parse(reqIn);
Ed Tanousaf4edf62020-07-21 08:46:25 -070098
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +020099 EXPECT_EQ(rc, ParserError::ERROR_UNEXPECTED_END_OF_INPUT);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700100}
101
102TEST_F(MultipartTest, TestErrorBoundaryFormat)
103{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800104 std::string_view body =
105 "-----------------------------d74496d66958873e\r\n"
106 "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n"
107 "{\"Key1\": 11223333333333333333333333333333333333333333}\r\n"
108 "-----------------------------d74496d66958873e\r\n"
109 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
110 "123456\r\n"
111 "-----------------------------d74496d66958873e--\r\n";
Ed Tanousaf4edf62020-07-21 08:46:25 -0700112
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800113 crow::Request reqIn(body, ec);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700114
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800115 reqIn.addHeader("Content-Type",
116 "multipart/form-data; "
117 "boundary+=-----------------------------d74496d66958873e");
118
Nan Zhoucc606192022-06-21 17:47:19 +0000119 EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_BOUNDARY_FORMAT);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700120}
121
122TEST_F(MultipartTest, TestErrorBoundaryCR)
123{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800124 std::string_view body =
125 "-----------------------------d74496d66958873e"
126 "Content-Disposition: form-data; name=\"Test1\"\r\n\r"
127 "{\"Key1\": 112233}\r\n"
128 "-----------------------------d74496d66958873e\r\n"
129 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
130 "123456\r\n"
131 "-----------------------------d74496d66958873e--\r\n";
132 crow::Request reqIn(body, ec);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700133
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800134 reqIn.addHeader("Content-Type",
135 "multipart/form-data; "
136 "boundary=---------------------------d74496d66958873e");
Ed Tanousaf4edf62020-07-21 08:46:25 -0700137
Nan Zhoucc606192022-06-21 17:47:19 +0000138 EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_BOUNDARY_CR);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700139}
140
141TEST_F(MultipartTest, TestErrorBoundaryLF)
142{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800143 std::string_view body =
144 "-----------------------------d74496d66958873e\r"
145 "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n"
146 "{\"Key1\": 112233}\r\n"
147 "-----------------------------d74496d66958873e\r\n"
148 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
149 "123456\r\n"
150 "-----------------------------d74496d66958873e--\r\n";
Ed Tanousaf4edf62020-07-21 08:46:25 -0700151
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800152 crow::Request reqIn(body, ec);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700153
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800154 reqIn.addHeader("Content-Type",
155 "multipart/form-data; "
156 "boundary=---------------------------d74496d66958873e");
157
Nan Zhoucc606192022-06-21 17:47:19 +0000158 EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_BOUNDARY_LF);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700159}
160
161TEST_F(MultipartTest, TestErrorBoundaryData)
162{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800163 std::string_view body =
164 "-----------------------------d74496d66958873e\r\n"
165 "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n"
166 "{\"Key1\": 112233}\r\n"
167 "-----------------------------d74496d66958873e\r\n"
168 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
169 "123456\r\n"
170 "-----------------------------d74496d66958873e--\r\n";
Ed Tanousaf4edf62020-07-21 08:46:25 -0700171
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800172 crow::Request reqIn(body, ec);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700173
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800174 reqIn.addHeader("Content-Type",
175 "multipart/form-data; "
176 "boundary=---------------------------d7449sd6d66958873e");
177
Nan Zhoucc606192022-06-21 17:47:19 +0000178 EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_BOUNDARY_DATA);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700179}
180
181TEST_F(MultipartTest, TestErrorEmptyHeader)
182{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800183 std::string_view body =
184 "-----------------------------d74496d66958873e\r\n"
185 ": form-data; name=\"Test1\"\r\n"
186 "{\"Key1\": 112233}\r\n"
187 "-----------------------------d74496d66958873e\r\n"
188 "Content-Disposition: form-data; name=\"Test2\"\r\n"
189 "123456\r\n"
190 "-----------------------------d74496d66958873e--\r\n";
191 crow::Request reqIn(body, ec);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700192
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800193 reqIn.addHeader("Content-Type",
194 "multipart/form-data; "
195 "boundary=---------------------------d74496d66958873e");
Ed Tanousaf4edf62020-07-21 08:46:25 -0700196
Nan Zhoucc606192022-06-21 17:47:19 +0000197 EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_EMPTY_HEADER);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700198}
199
200TEST_F(MultipartTest, TestErrorHeaderName)
201{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800202 std::string_view body =
203 "-----------------------------d74496d66958873e\r\n"
204 "Content-!!Disposition: form-data; name=\"Test1\"\r\n"
205 "{\"Key1\": 112233}\r\n"
206 "-----------------------------d74496d66958873e\r\n"
207 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
208 "123456\r\n"
209 "-----------------------------d74496d66958873e--\r\n";
210 crow::Request reqIn(body, ec);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700211
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800212 reqIn.addHeader("Content-Type",
213 "multipart/form-data; "
214 "boundary=---------------------------d74496d66958873e");
Ed Tanousaf4edf62020-07-21 08:46:25 -0700215
Nan Zhoucc606192022-06-21 17:47:19 +0000216 EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_HEADER_NAME);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700217}
218
219TEST_F(MultipartTest, TestErrorHeaderValue)
220{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800221 std::string_view body =
222 "-----------------------------d74496d66958873e\r\n"
223 "Content-Disposition: form-data; name=\"Test1\"\r"
224 "{\"Key1\": 112233}\r\n"
225 "-----------------------------d74496d66958873e\r\n"
226 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
227 "123456\r\n"
228 "-----------------------------d74496d66958873e--\r\n";
Ed Tanousaf4edf62020-07-21 08:46:25 -0700229
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800230 crow::Request reqIn(body, ec);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700231
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800232 reqIn.addHeader("Content-Type",
233 "multipart/form-data; "
234 "boundary=---------------------------d74496d66958873e");
235
Nan Zhoucc606192022-06-21 17:47:19 +0000236 EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_HEADER_VALUE);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700237}
238
239TEST_F(MultipartTest, TestErrorHeaderEnding)
240{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800241 std::string_view body =
242 "-----------------------------d74496d66958873e\r\n"
243 "Content-Disposition: form-data; name=\"Test1\"\r\n\r"
244 "{\"Key1\": 112233}\r\n"
245 "-----------------------------d74496d66958873e\r\n"
246 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
247 "123456\r\n"
248 "-----------------------------d74496d66958873e--\r\n";
Ed Tanousaf4edf62020-07-21 08:46:25 -0700249
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800250 crow::Request reqIn(body, ec);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700251
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800252 reqIn.addHeader("Content-Type",
253 "multipart/form-data; "
254 "boundary=---------------------------d74496d66958873e");
255
Nan Zhoucc606192022-06-21 17:47:19 +0000256 EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_HEADER_ENDING);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700257}
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200258
259TEST_F(MultipartTest, TestGoodMultipartParserMultipleHeaders)
260{
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400261 std::string_view body =
262 "-----------------------------d74496d66958873e\r\n"
263 "Content-Disposition: form-data; name=\"Test1\"\r\n"
264 "Other-Header: value=\"v1\"\r\n"
265 "\r\n"
266 "Data1\r\n"
267 "-----------------------------d74496d66958873e--";
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200268
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800269 crow::Request reqIn(body, ec);
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200270
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800271 reqIn.addHeader("Content-Type",
272 "multipart/form-data; "
273 "boundary=---------------------------d74496d66958873e");
274
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200275 ParserError rc = parser.parse(reqIn);
276 ASSERT_EQ(rc, ParserError::PARSER_SUCCESS);
277
278 EXPECT_EQ(parser.boundary,
279 "\r\n-----------------------------d74496d66958873e");
280 ASSERT_EQ(parser.mime_fields.size(), 1);
281
282 EXPECT_EQ(parser.mime_fields[0].fields.at("Content-Disposition"),
283 "form-data; name=\"Test1\"");
284 EXPECT_EQ(parser.mime_fields[0].fields.at("Other-Header"), "value=\"v1\"");
285 EXPECT_EQ(parser.mime_fields[0].content, "Data1");
286}
287
288TEST_F(MultipartTest, TestErrorHeaderWithoutColon)
289{
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400290 std::string_view body =
291 "----end\r\n"
292 "abc\r\n"
293 "\r\n"
294 "Data1\r\n"
295 "----end--\r\n";
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800296 crow::Request reqIn(body, ec);
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200297
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800298 reqIn.addHeader("Content-Type", "multipart/form-data; "
299 "boundary=--end");
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200300
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200301 EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_UNEXPECTED_END_OF_HEADER);
302}
303
304TEST_F(MultipartTest, TestUnknownHeaderIsCorrectlyParsed)
305{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800306 std::string_view body =
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200307 "----end\r\n"
308 "t-DiPpcccc:cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccgcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccaaaaaa\r\n"
309 "\r\n"
310 "Data1\r\n"
311 "----end--\r\n";
312
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800313 crow::Request reqIn(body, ec);
314
315 reqIn.addHeader("Content-Type", "multipart/form-data; "
316 "boundary=--end");
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200317 ParserError rc = parser.parse(reqIn);
318
319 ASSERT_EQ(rc, ParserError::PARSER_SUCCESS);
320
321 EXPECT_EQ(parser.boundary, "\r\n----end");
322 ASSERT_EQ(parser.mime_fields.size(), 1);
323
324 EXPECT_EQ(
325 parser.mime_fields[0].fields.at("t-DiPpcccc"),
326 "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccgcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccaaaaaa");
327 EXPECT_EQ(parser.mime_fields[0].content, "Data1");
328}
329
330TEST_F(MultipartTest, TestErrorMissingSeparatorBetweenMimeFieldsAndData)
331{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800332 std::string_view body =
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200333 "-----------------------------d74496d66958873e\r\n"
334 "t-DiPpcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccgcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccaaaaaa\r\n"
335 "Data1"
336 "-----------------------------d74496d66958873e--";
337
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800338 crow::Request reqIn(body, ec);
339
340 reqIn.addHeader(
341 "Content-Type",
342 "multipart/form-data; boundary=---------------------------d74496d66958873e");
343
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200344 ParserError rc = parser.parse(reqIn);
345
346 EXPECT_EQ(rc, ParserError::ERROR_UNEXPECTED_END_OF_HEADER);
347}
348
349TEST_F(MultipartTest, TestDataWithoutMimeFields)
350{
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400351 std::string_view body =
352 "-----------------------------d74496d66958873e\r\n"
353 "\r\n"
354 "Data1\r\n"
355 "-----------------------------d74496d66958873e--";
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800356
357 crow::Request reqIn(body, ec);
358
359 reqIn.addHeader(
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200360 "Content-Type",
361 "multipart/form-data; boundary=---------------------------d74496d66958873e");
362
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200363 ParserError rc = parser.parse(reqIn);
364
365 ASSERT_EQ(rc, ParserError::PARSER_SUCCESS);
366
367 EXPECT_EQ(parser.boundary,
368 "\r\n-----------------------------d74496d66958873e");
369 ASSERT_EQ(parser.mime_fields.size(), 1);
370
371 EXPECT_EQ(std::distance(parser.mime_fields[0].fields.begin(),
372 parser.mime_fields[0].fields.end()),
373 0);
374 EXPECT_EQ(parser.mime_fields[0].content, "Data1");
375}
376
377TEST_F(MultipartTest, TestErrorMissingFinalBoundry)
378{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800379 std::string_view body =
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200380 "----XX\r\n"
381 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
382 "t-DiPpccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccAAAAAAAAAAAAAAABCDz\r\n"
383 "\335\r\n\r\n";
384
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800385 crow::Request reqIn(body, ec);
386
387 reqIn.addHeader("Content-Type", "multipart/form-data; boundary=--XX");
388
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200389 ParserError rc = parser.parse(reqIn);
390
391 EXPECT_EQ(rc, ParserError::ERROR_UNEXPECTED_END_OF_INPUT);
392}
393
394TEST_F(MultipartTest, TestIgnoreDataAfterFinalBoundary)
395{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800396 std::string_view body =
397 "----XX\r\n"
398 "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n"
399 "Data1\r\n"
400 "----XX--\r\n"
401 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
402 "Data2\r\n"
403 "----XX--\r\n";
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200404
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800405 crow::Request reqIn(body, ec);
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200406
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800407 reqIn.addHeader("Content-Type", "multipart/form-data; boundary=--XX");
408
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200409 ParserError rc = parser.parse(reqIn);
410
411 ASSERT_EQ(rc, ParserError::PARSER_SUCCESS);
412
413 EXPECT_EQ(parser.boundary, "\r\n----XX");
414 EXPECT_EQ(parser.mime_fields.size(), 1);
415
416 EXPECT_EQ(parser.mime_fields[0].fields.at("Content-Disposition"),
417 "form-data; name=\"Test1\"");
418 EXPECT_EQ(parser.mime_fields[0].content, "Data1");
419}
420
421TEST_F(MultipartTest, TestFinalBoundaryIsCorrectlyRecognized)
422{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800423 std::string_view body =
424 "----XX\r\n"
425 "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n"
426 "Data1\r\n"
427 "----XX-abc-\r\n"
428 "StillData1\r\n"
429 "----XX--\r\n";
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200430
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800431 crow::Request reqIn(body, ec);
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200432
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800433 reqIn.addHeader("Content-Type", "multipart/form-data; boundary=--XX");
434
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200435 ParserError rc = parser.parse(reqIn);
436
437 ASSERT_EQ(rc, ParserError::PARSER_SUCCESS);
438
439 EXPECT_EQ(parser.boundary, "\r\n----XX");
440 EXPECT_EQ(parser.mime_fields.size(), 1);
441
442 EXPECT_EQ(parser.mime_fields[0].fields.at("Content-Disposition"),
443 "form-data; name=\"Test1\"");
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400444 EXPECT_EQ(parser.mime_fields[0].content,
445 "Data1\r\n"
446 "----XX-abc-\r\n"
447 "StillData1");
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200448}
449
450} // namespace