2
// MultipartReaderTest.cpp
4
// $Id: //poco/1.2/Net/testsuite/src/MultipartReaderTest.cpp#2 $
6
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
9
// Permission is hereby granted, free of charge, to any person or organization
10
// obtaining a copy of the software and accompanying documentation covered by
11
// this license (the "Software") to use, reproduce, display, distribute,
12
// execute, and transmit the Software, and to prepare derivative works of the
13
// Software, and to permit third-parties to whom the Software is furnished to
14
// do so, all subject to the following:
16
// The copyright notices in the Software and this entire statement, including
17
// the above license grant, this restriction and the following disclaimer,
18
// must be included in all copies of the Software, in whole or in part, and
19
// all derivative works of the Software, unless such copies or derivative
20
// works are solely in the form of machine-executable object code generated by
21
// a source language processor.
23
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
26
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
27
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
28
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29
// DEALINGS IN THE SOFTWARE.
33
#include "MultipartReaderTest.h"
34
#include "CppUnit/TestCaller.h"
35
#include "CppUnit/TestSuite.h"
36
#include "Poco/Net/MultipartReader.h"
37
#include "Poco/Net/MessageHeader.h"
38
#include "Poco/Net/NetException.h"
42
using Poco::Net::MultipartReader;
43
using Poco::Net::MessageHeader;
44
using Poco::Net::MultipartException;
47
MultipartReaderTest::MultipartReaderTest(const std::string& name): CppUnit::TestCase(name)
52
MultipartReaderTest::~MultipartReaderTest()
57
void MultipartReaderTest::testReadOnePart()
59
std::string s("\r\n--MIME_boundary_01234567\r\nname1: value1\r\n\r\nthis is part 1\r\n--MIME_boundary_01234567--\r\n");
60
std::istringstream istr(s);
61
MultipartReader r(istr, "MIME_boundary_01234567");
62
assert (r.boundary() == "MIME_boundary_01234567");
63
assert (r.hasNextPart());
66
assert (h.size() == 1);
67
assert (h["name1"] == "value1");
68
std::istream& i = r.stream();
76
assert (part == "this is part 1");
77
assert (!r.hasNextPart());
81
fail("no more parts - must throw");
83
catch (MultipartException&)
89
void MultipartReaderTest::testReadTwoParts()
91
std::string s("\r\n--MIME_boundary_01234567\r\nname1: value1\r\n\r\nthis is part 1\r\n--MIME_boundary_01234567\r\n\r\nthis is part 2\r\n\r\n--MIME_boundary_01234567--\r\n");
92
std::istringstream istr(s);
93
MultipartReader r(istr, "MIME_boundary_01234567");
94
assert (r.hasNextPart());
97
assert (h.size() == 1);
98
assert (h["name1"] == "value1");
99
std::istream& i = r.stream();
107
assert (part == "this is part 1");
108
assert (r.hasNextPart());
111
std::istream& ii = r.stream();
119
assert (part == "this is part 2\r\n");
124
fail("no more parts - must throw");
126
catch (MultipartException&)
132
void MultipartReaderTest::testReadEmptyLines()
134
std::string s("\r\n--MIME_boundary_01234567\r\nname1: value1\r\n\r\nthis is\r\npart 1\r\n\r\n--MIME_boundary_01234567\r\n\r\nthis\r\n\r\nis part 2\r\n\r\n\r\n--MIME_boundary_01234567--\r\n");
135
std::istringstream istr(s);
136
MultipartReader r(istr, "MIME_boundary_01234567");
137
assert (r.hasNextPart());
140
assert (h.size() == 1);
141
assert (h["name1"] == "value1");
142
std::istream& i = r.stream();
150
assert (part == "this is\r\npart 1\r\n");
151
assert (r.hasNextPart());
154
std::istream& ii = r.stream();
162
assert (part == "this\r\n\r\nis part 2\r\n\r\n");
167
fail("no more parts - must throw");
169
catch (MultipartException&)
175
void MultipartReaderTest::testReadLongPart()
177
std::string longPart(3000, 'X');
178
std::string s("\r\n--MIME_boundary_01234567\r\nname1: value1\r\n\r\n");
180
s.append("\r\n--MIME_boundary_01234567\r\n\r\nthis is part 2\r\n--MIME_boundary_01234567--\r\n");
181
std::istringstream istr(s);
182
MultipartReader r(istr, "MIME_boundary_01234567");
183
assert (r.hasNextPart());
186
assert (h.size() == 1);
187
assert (h["name1"] == "value1");
188
std::istream& i = r.stream();
196
assert (part == longPart);
197
assert (r.hasNextPart());
200
std::istream& ii = r.stream();
208
assert (part == "this is part 2");
213
fail("no more parts - must throw");
215
catch (MultipartException&)
221
void MultipartReaderTest::testGuessBoundary()
223
std::string s("\r\n--MIME_boundary_01234567\r\nname1: value1\r\n\r\nthis is part 1\r\n--MIME_boundary_01234567--\r\n");
224
std::istringstream istr(s);
225
MultipartReader r(istr);
226
assert (r.hasNextPart());
229
assert (r.boundary() == "MIME_boundary_01234567");
230
assert (h.size() == 1);
231
assert (h["name1"] == "value1");
232
std::istream& i = r.stream();
240
assert (part == "this is part 1");
241
assert (!r.hasNextPart());
245
fail("no more parts - must throw");
247
catch (MultipartException&)
253
void MultipartReaderTest::testPreamble()
255
std::string s("this is the\r\npreamble\r\n--MIME_boundary_01234567\r\nname1: value1\r\n\r\nthis is part 1\r\n--MIME_boundary_01234567--\r\n");
256
std::istringstream istr(s);
257
MultipartReader r(istr, "MIME_boundary_01234567");
258
assert (r.hasNextPart());
261
assert (h.size() == 1);
262
assert (h["name1"] == "value1");
263
std::istream& i = r.stream();
271
assert (part == "this is part 1");
272
assert (!r.hasNextPart());
276
fail("no more parts - must throw");
278
catch (MultipartException&)
284
void MultipartReaderTest::testBadBoundary()
286
std::string s("\r\n--MIME_boundary_01234567\r\nname1: value1\r\n\r\nthis is part 1\r\n--MIME_boundary_01234567--\r\n");
287
std::istringstream istr(s);
288
MultipartReader r(istr, "MIME_boundary_7654321");
289
assert (r.hasNextPart());
295
catch (MultipartException&)
301
void MultipartReaderTest::testRobustness()
303
std::string s("--MIME_boundary_01234567\rname1: value1\r\n\nthis is part 1\n--MIME_boundary_01234567--");
304
std::istringstream istr(s);
305
MultipartReader r(istr, "MIME_boundary_01234567");
306
assert (r.hasNextPart());
309
assert (h.size() == 1);
310
assert (h["name1"] == "value1");
311
std::istream& i = r.stream();
319
assert (part == "this is part 1");
320
assert (!r.hasNextPart());
324
fail("no more parts - must throw");
326
catch (MultipartException&)
332
void MultipartReaderTest::testUnixLineEnds()
334
std::string s("\n--MIME_boundary_01234567\nname1: value1\n\nthis is part 1\n--MIME_boundary_01234567\n\nthis is part 2\n\n--MIME_boundary_01234567--\n");
335
std::istringstream istr(s);
336
MultipartReader r(istr, "MIME_boundary_01234567");
337
assert (r.hasNextPart());
340
assert (h.size() == 1);
341
assert (h["name1"] == "value1");
342
std::istream& i = r.stream();
350
assert (part == "this is part 1");
351
assert (r.hasNextPart());
354
std::istream& ii = r.stream();
362
assert (part == "this is part 2\n");
367
fail("no more parts - must throw");
369
catch (MultipartException&)
375
void MultipartReaderTest::setUp()
380
void MultipartReaderTest::tearDown()
385
CppUnit::Test* MultipartReaderTest::suite()
387
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("MultipartReaderTest");
389
CppUnit_addTest(pSuite, MultipartReaderTest, testReadOnePart);
390
CppUnit_addTest(pSuite, MultipartReaderTest, testReadTwoParts);
391
CppUnit_addTest(pSuite, MultipartReaderTest, testReadEmptyLines);
392
CppUnit_addTest(pSuite, MultipartReaderTest, testReadLongPart);
393
CppUnit_addTest(pSuite, MultipartReaderTest, testGuessBoundary);
394
CppUnit_addTest(pSuite, MultipartReaderTest, testPreamble);
395
CppUnit_addTest(pSuite, MultipartReaderTest, testBadBoundary);
396
CppUnit_addTest(pSuite, MultipartReaderTest, testRobustness);
397
CppUnit_addTest(pSuite, MultipartReaderTest, testUnixLineEnds);