blob: 0ac201f100e7c7c5e6228fc150eed821021a6e5c [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>
Nan Zhouca0cb4c2022-06-21 17:47:09 +00005#include <boost/beast/http/message.hpp>
Ed Tanousaf4edf62020-07-21 08:46:25 -07006
Nan Zhoud5c80ad2022-07-11 01:16:31 +00007#include <memory>
8#include <string_view>
Nan Zhouca0cb4c2022-06-21 17:47:09 +00009#include <system_error>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000010#include <vector>
Ed Tanousaf4edf62020-07-21 08:46:25 -070011
Nan Zhoud5c80ad2022-07-11 01:16:31 +000012#include <gtest/gtest.h> // IWYU pragma: keep
13
14// IWYU pragma: no_include <gtest/gtest-message.h>
15// IWYU pragma: no_include <gtest/gtest-test-part.h>
16// IWYU pragma: no_include "gtest/gtest_pred_impl.h"
17// IWYU pragma: no_include <boost/beast/http/impl/fields.hpp>
18// IWYU pragma: no_include <boost/intrusive/detail/list_iterator.hpp>
19// IWYU pragma: no_include <boost/intrusive/detail/tree_iterator.hpp>
Ed Tanousaf4edf62020-07-21 08:46:25 -070020
Nan Zhoue52d1562022-06-21 17:47:15 +000021namespace
22{
23using ::testing::Test;
24
25class MultipartTest : public Test
Ed Tanousaf4edf62020-07-21 08:46:25 -070026{
27 public:
Ed Tanousaf4edf62020-07-21 08:46:25 -070028 MultipartParser parser;
29 std::error_code ec;
30};
31
32TEST_F(MultipartTest, TestGoodMultipartParser)
33{
Ed Tanous6fb96ce2024-01-28 21:30:06 -080034 std::string_view body =
35 "-----------------------------d74496d66958873e\r\n"
36 "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n"
37 "111111111111111111111111112222222222222222222222222222222\r\n"
38 "-----------------------------d74496d66958873e\r\n"
39 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
40 "{\r\n-----------------------------d74496d66958873e123456\r\n"
41 "-----------------------------d74496d66958873e\r\n"
42 "Content-Disposition: form-data; name=\"Test3\"\r\n\r\n"
43 "{\r\n--------d74496d6695887}\r\n"
44 "-----------------------------d74496d66958873e--\r\n";
Ed Tanousaf4edf62020-07-21 08:46:25 -070045
Ed Tanous6fb96ce2024-01-28 21:30:06 -080046 crow::Request reqIn(body, ec);
Ed Tanousaf4edf62020-07-21 08:46:25 -070047
Ed Tanous6fb96ce2024-01-28 21:30:06 -080048 reqIn.addHeader("Content-Type",
49 "multipart/form-data; "
50 "boundary=---------------------------d74496d66958873e");
51
Ed Tanousaf4edf62020-07-21 08:46:25 -070052 ParserError rc = parser.parse(reqIn);
53 ASSERT_EQ(rc, ParserError::PARSER_SUCCESS);
54
Nan Zhoucc606192022-06-21 17:47:19 +000055 EXPECT_EQ(parser.boundary,
Ed Tanousaf4edf62020-07-21 08:46:25 -070056 "\r\n-----------------------------d74496d66958873e");
Nan Zhoucc606192022-06-21 17:47:19 +000057 EXPECT_EQ(parser.mime_fields.size(), 3);
Ed Tanousaf4edf62020-07-21 08:46:25 -070058
Nan Zhoucc606192022-06-21 17:47:19 +000059 EXPECT_EQ(parser.mime_fields[0].fields.at("Content-Disposition"),
Ed Tanousaf4edf62020-07-21 08:46:25 -070060 "form-data; name=\"Test1\"");
Nan Zhoucc606192022-06-21 17:47:19 +000061 EXPECT_EQ(parser.mime_fields[0].content,
Ed Tanousaf4edf62020-07-21 08:46:25 -070062 "111111111111111111111111112222222222222222222222222222222");
63
Nan Zhoucc606192022-06-21 17:47:19 +000064 EXPECT_EQ(parser.mime_fields[1].fields.at("Content-Disposition"),
Ed Tanousaf4edf62020-07-21 08:46:25 -070065 "form-data; name=\"Test2\"");
Nan Zhoucc606192022-06-21 17:47:19 +000066 EXPECT_EQ(parser.mime_fields[1].content,
Ed Tanousaf4edf62020-07-21 08:46:25 -070067 "{\r\n-----------------------------d74496d66958873e123456");
Nan Zhoucc606192022-06-21 17:47:19 +000068 EXPECT_EQ(parser.mime_fields[2].fields.at("Content-Disposition"),
Ed Tanousaf4edf62020-07-21 08:46:25 -070069 "form-data; name=\"Test3\"");
Nan Zhoucc606192022-06-21 17:47:19 +000070 EXPECT_EQ(parser.mime_fields[2].content, "{\r\n--------d74496d6695887}");
Ed Tanousaf4edf62020-07-21 08:46:25 -070071}
72
73TEST_F(MultipartTest, TestBadMultipartParser1)
74{
Ed Tanous6fb96ce2024-01-28 21:30:06 -080075 std::string_view body =
76 "-----------------------------d74496d66958873e\r\n"
77 "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n"
78 "1234567890\r\n"
79 "-----------------------------d74496d66958873e\r-\r\n";
Ed Tanousaf4edf62020-07-21 08:46:25 -070080
Ed Tanous6fb96ce2024-01-28 21:30:06 -080081 crow::Request reqIn(body, ec);
Ed Tanousaf4edf62020-07-21 08:46:25 -070082
Ed Tanous6fb96ce2024-01-28 21:30:06 -080083 reqIn.addHeader("Content-Type",
84 "multipart/form-data; "
85 "boundary=---------------------------d74496d66958873e");
86
Ed Tanousaf4edf62020-07-21 08:46:25 -070087 ParserError rc = parser.parse(reqIn);
Ed Tanousaf4edf62020-07-21 08:46:25 -070088
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +020089 EXPECT_EQ(rc, ParserError::ERROR_UNEXPECTED_END_OF_INPUT);
Ed Tanousaf4edf62020-07-21 08:46:25 -070090}
91
92TEST_F(MultipartTest, TestBadMultipartParser2)
93{
Ed Tanous6fb96ce2024-01-28 21:30:06 -080094 std::string_view body =
95 "-----------------------------d74496d66958873e\r\n"
96 "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n"
97 "abcd\r\n"
98 "-----------------------------d74496d66958873e-\r\n";
99 crow::Request reqIn(body, ec);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700100
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800101 reqIn.addHeader("Content-Type",
102 "multipart/form-data; "
103 "boundary=---------------------------d74496d66958873e");
Ed Tanousaf4edf62020-07-21 08:46:25 -0700104
Ed Tanousaf4edf62020-07-21 08:46:25 -0700105 ParserError rc = parser.parse(reqIn);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700106
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200107 EXPECT_EQ(rc, ParserError::ERROR_UNEXPECTED_END_OF_INPUT);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700108}
109
110TEST_F(MultipartTest, TestErrorBoundaryFormat)
111{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800112 std::string_view body =
113 "-----------------------------d74496d66958873e\r\n"
114 "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n"
115 "{\"Key1\": 11223333333333333333333333333333333333333333}\r\n"
116 "-----------------------------d74496d66958873e\r\n"
117 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
118 "123456\r\n"
119 "-----------------------------d74496d66958873e--\r\n";
Ed Tanousaf4edf62020-07-21 08:46:25 -0700120
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800121 crow::Request reqIn(body, ec);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700122
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800123 reqIn.addHeader("Content-Type",
124 "multipart/form-data; "
125 "boundary+=-----------------------------d74496d66958873e");
126
Nan Zhoucc606192022-06-21 17:47:19 +0000127 EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_BOUNDARY_FORMAT);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700128}
129
130TEST_F(MultipartTest, TestErrorBoundaryCR)
131{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800132 std::string_view body =
133 "-----------------------------d74496d66958873e"
134 "Content-Disposition: form-data; name=\"Test1\"\r\n\r"
135 "{\"Key1\": 112233}\r\n"
136 "-----------------------------d74496d66958873e\r\n"
137 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
138 "123456\r\n"
139 "-----------------------------d74496d66958873e--\r\n";
140 crow::Request reqIn(body, ec);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700141
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800142 reqIn.addHeader("Content-Type",
143 "multipart/form-data; "
144 "boundary=---------------------------d74496d66958873e");
Ed Tanousaf4edf62020-07-21 08:46:25 -0700145
Nan Zhoucc606192022-06-21 17:47:19 +0000146 EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_BOUNDARY_CR);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700147}
148
149TEST_F(MultipartTest, TestErrorBoundaryLF)
150{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800151 std::string_view body =
152 "-----------------------------d74496d66958873e\r"
153 "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n"
154 "{\"Key1\": 112233}\r\n"
155 "-----------------------------d74496d66958873e\r\n"
156 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
157 "123456\r\n"
158 "-----------------------------d74496d66958873e--\r\n";
Ed Tanousaf4edf62020-07-21 08:46:25 -0700159
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800160 crow::Request reqIn(body, ec);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700161
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800162 reqIn.addHeader("Content-Type",
163 "multipart/form-data; "
164 "boundary=---------------------------d74496d66958873e");
165
Nan Zhoucc606192022-06-21 17:47:19 +0000166 EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_BOUNDARY_LF);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700167}
168
169TEST_F(MultipartTest, TestErrorBoundaryData)
170{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800171 std::string_view body =
172 "-----------------------------d74496d66958873e\r\n"
173 "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n"
174 "{\"Key1\": 112233}\r\n"
175 "-----------------------------d74496d66958873e\r\n"
176 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
177 "123456\r\n"
178 "-----------------------------d74496d66958873e--\r\n";
Ed Tanousaf4edf62020-07-21 08:46:25 -0700179
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800180 crow::Request reqIn(body, ec);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700181
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800182 reqIn.addHeader("Content-Type",
183 "multipart/form-data; "
184 "boundary=---------------------------d7449sd6d66958873e");
185
Nan Zhoucc606192022-06-21 17:47:19 +0000186 EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_BOUNDARY_DATA);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700187}
188
189TEST_F(MultipartTest, TestErrorEmptyHeader)
190{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800191 std::string_view body =
192 "-----------------------------d74496d66958873e\r\n"
193 ": form-data; name=\"Test1\"\r\n"
194 "{\"Key1\": 112233}\r\n"
195 "-----------------------------d74496d66958873e\r\n"
196 "Content-Disposition: form-data; name=\"Test2\"\r\n"
197 "123456\r\n"
198 "-----------------------------d74496d66958873e--\r\n";
199 crow::Request reqIn(body, ec);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700200
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800201 reqIn.addHeader("Content-Type",
202 "multipart/form-data; "
203 "boundary=---------------------------d74496d66958873e");
Ed Tanousaf4edf62020-07-21 08:46:25 -0700204
Nan Zhoucc606192022-06-21 17:47:19 +0000205 EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_EMPTY_HEADER);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700206}
207
208TEST_F(MultipartTest, TestErrorHeaderName)
209{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800210 std::string_view body =
211 "-----------------------------d74496d66958873e\r\n"
212 "Content-!!Disposition: form-data; name=\"Test1\"\r\n"
213 "{\"Key1\": 112233}\r\n"
214 "-----------------------------d74496d66958873e\r\n"
215 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
216 "123456\r\n"
217 "-----------------------------d74496d66958873e--\r\n";
218 crow::Request reqIn(body, ec);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700219
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800220 reqIn.addHeader("Content-Type",
221 "multipart/form-data; "
222 "boundary=---------------------------d74496d66958873e");
Ed Tanousaf4edf62020-07-21 08:46:25 -0700223
Nan Zhoucc606192022-06-21 17:47:19 +0000224 EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_HEADER_NAME);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700225}
226
227TEST_F(MultipartTest, TestErrorHeaderValue)
228{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800229 std::string_view body =
230 "-----------------------------d74496d66958873e\r\n"
231 "Content-Disposition: form-data; name=\"Test1\"\r"
232 "{\"Key1\": 112233}\r\n"
233 "-----------------------------d74496d66958873e\r\n"
234 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
235 "123456\r\n"
236 "-----------------------------d74496d66958873e--\r\n";
Ed Tanousaf4edf62020-07-21 08:46:25 -0700237
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800238 crow::Request reqIn(body, ec);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700239
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800240 reqIn.addHeader("Content-Type",
241 "multipart/form-data; "
242 "boundary=---------------------------d74496d66958873e");
243
Nan Zhoucc606192022-06-21 17:47:19 +0000244 EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_HEADER_VALUE);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700245}
246
247TEST_F(MultipartTest, TestErrorHeaderEnding)
248{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800249 std::string_view body =
250 "-----------------------------d74496d66958873e\r\n"
251 "Content-Disposition: form-data; name=\"Test1\"\r\n\r"
252 "{\"Key1\": 112233}\r\n"
253 "-----------------------------d74496d66958873e\r\n"
254 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
255 "123456\r\n"
256 "-----------------------------d74496d66958873e--\r\n";
Ed Tanousaf4edf62020-07-21 08:46:25 -0700257
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800258 crow::Request reqIn(body, ec);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700259
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800260 reqIn.addHeader("Content-Type",
261 "multipart/form-data; "
262 "boundary=---------------------------d74496d66958873e");
263
Nan Zhoucc606192022-06-21 17:47:19 +0000264 EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_HEADER_ENDING);
Ed Tanousaf4edf62020-07-21 08:46:25 -0700265}
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200266
267TEST_F(MultipartTest, TestGoodMultipartParserMultipleHeaders)
268{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800269 std::string_view body = "-----------------------------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{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800297 std::string_view body = "----end\r\n"
298 "abc\r\n"
299 "\r\n"
300 "Data1\r\n"
301 "----end--\r\n";
302 crow::Request reqIn(body, ec);
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200303
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800304 reqIn.addHeader("Content-Type", "multipart/form-data; "
305 "boundary=--end");
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200306
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200307 EXPECT_EQ(parser.parse(reqIn), ParserError::ERROR_UNEXPECTED_END_OF_HEADER);
308}
309
310TEST_F(MultipartTest, TestUnknownHeaderIsCorrectlyParsed)
311{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800312 std::string_view body =
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200313 "----end\r\n"
314 "t-DiPpcccc:cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccgcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccaaaaaa\r\n"
315 "\r\n"
316 "Data1\r\n"
317 "----end--\r\n";
318
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800319 crow::Request reqIn(body, ec);
320
321 reqIn.addHeader("Content-Type", "multipart/form-data; "
322 "boundary=--end");
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200323 ParserError rc = parser.parse(reqIn);
324
325 ASSERT_EQ(rc, ParserError::PARSER_SUCCESS);
326
327 EXPECT_EQ(parser.boundary, "\r\n----end");
328 ASSERT_EQ(parser.mime_fields.size(), 1);
329
330 EXPECT_EQ(
331 parser.mime_fields[0].fields.at("t-DiPpcccc"),
332 "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccgcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccaaaaaa");
333 EXPECT_EQ(parser.mime_fields[0].content, "Data1");
334}
335
336TEST_F(MultipartTest, TestErrorMissingSeparatorBetweenMimeFieldsAndData)
337{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800338 std::string_view body =
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200339 "-----------------------------d74496d66958873e\r\n"
340 "t-DiPpcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccgcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccaaaaaa\r\n"
341 "Data1"
342 "-----------------------------d74496d66958873e--";
343
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800344 crow::Request reqIn(body, ec);
345
346 reqIn.addHeader(
347 "Content-Type",
348 "multipart/form-data; boundary=---------------------------d74496d66958873e");
349
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200350 ParserError rc = parser.parse(reqIn);
351
352 EXPECT_EQ(rc, ParserError::ERROR_UNEXPECTED_END_OF_HEADER);
353}
354
355TEST_F(MultipartTest, TestDataWithoutMimeFields)
356{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800357 std::string_view body = "-----------------------------d74496d66958873e\r\n"
358 "\r\n"
359 "Data1\r\n"
360 "-----------------------------d74496d66958873e--";
361
362 crow::Request reqIn(body, ec);
363
364 reqIn.addHeader(
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200365 "Content-Type",
366 "multipart/form-data; boundary=---------------------------d74496d66958873e");
367
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200368 ParserError rc = parser.parse(reqIn);
369
370 ASSERT_EQ(rc, ParserError::PARSER_SUCCESS);
371
372 EXPECT_EQ(parser.boundary,
373 "\r\n-----------------------------d74496d66958873e");
374 ASSERT_EQ(parser.mime_fields.size(), 1);
375
376 EXPECT_EQ(std::distance(parser.mime_fields[0].fields.begin(),
377 parser.mime_fields[0].fields.end()),
378 0);
379 EXPECT_EQ(parser.mime_fields[0].content, "Data1");
380}
381
382TEST_F(MultipartTest, TestErrorMissingFinalBoundry)
383{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800384 std::string_view body =
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200385 "----XX\r\n"
386 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
387 "t-DiPpccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccAAAAAAAAAAAAAAABCDz\r\n"
388 "\335\r\n\r\n";
389
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800390 crow::Request reqIn(body, ec);
391
392 reqIn.addHeader("Content-Type", "multipart/form-data; boundary=--XX");
393
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200394 ParserError rc = parser.parse(reqIn);
395
396 EXPECT_EQ(rc, ParserError::ERROR_UNEXPECTED_END_OF_INPUT);
397}
398
399TEST_F(MultipartTest, TestIgnoreDataAfterFinalBoundary)
400{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800401 std::string_view body =
402 "----XX\r\n"
403 "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n"
404 "Data1\r\n"
405 "----XX--\r\n"
406 "Content-Disposition: form-data; name=\"Test2\"\r\n\r\n"
407 "Data2\r\n"
408 "----XX--\r\n";
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200409
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800410 crow::Request reqIn(body, ec);
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200411
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800412 reqIn.addHeader("Content-Type", "multipart/form-data; boundary=--XX");
413
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200414 ParserError rc = parser.parse(reqIn);
415
416 ASSERT_EQ(rc, ParserError::PARSER_SUCCESS);
417
418 EXPECT_EQ(parser.boundary, "\r\n----XX");
419 EXPECT_EQ(parser.mime_fields.size(), 1);
420
421 EXPECT_EQ(parser.mime_fields[0].fields.at("Content-Disposition"),
422 "form-data; name=\"Test1\"");
423 EXPECT_EQ(parser.mime_fields[0].content, "Data1");
424}
425
426TEST_F(MultipartTest, TestFinalBoundaryIsCorrectlyRecognized)
427{
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800428 std::string_view body =
429 "----XX\r\n"
430 "Content-Disposition: form-data; name=\"Test1\"\r\n\r\n"
431 "Data1\r\n"
432 "----XX-abc-\r\n"
433 "StillData1\r\n"
434 "----XX--\r\n";
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200435
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800436 crow::Request reqIn(body, ec);
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200437
Ed Tanous6fb96ce2024-01-28 21:30:06 -0800438 reqIn.addHeader("Content-Type", "multipart/form-data; boundary=--XX");
439
Krzysztof Grobelny18e3f7f2022-08-24 09:24:33 +0200440 ParserError rc = parser.parse(reqIn);
441
442 ASSERT_EQ(rc, ParserError::PARSER_SUCCESS);
443
444 EXPECT_EQ(parser.boundary, "\r\n----XX");
445 EXPECT_EQ(parser.mime_fields.size(), 1);
446
447 EXPECT_EQ(parser.mime_fields[0].fields.at("Content-Disposition"),
448 "form-data; name=\"Test1\"");
449 EXPECT_EQ(parser.mime_fields[0].content, "Data1\r\n"
450 "----XX-abc-\r\n"
451 "StillData1");
452}
453
454} // namespace