| 1 | // |
| 2 | // MultipartWriterTest.cpp |
| 3 | // |
| 4 | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. |
| 5 | // and Contributors. |
| 6 | // |
| 7 | // SPDX-License-Identifier: BSL-1.0 |
| 8 | // |
| 9 | |
| 10 | |
| 11 | #include "MultipartWriterTest.h" |
| 12 | #include "Poco/CppUnit/TestCaller.h" |
| 13 | #include "Poco/CppUnit/TestSuite.h" |
| 14 | #include "Poco/Net/MultipartWriter.h" |
| 15 | #include "Poco/Net/MessageHeader.h" |
| 16 | #include <sstream> |
| 17 | |
| 18 | |
| 19 | using Poco::Net::MultipartWriter; |
| 20 | using Poco::Net::MessageHeader; |
| 21 | |
| 22 | |
| 23 | MultipartWriterTest::MultipartWriterTest(const std::string& name): CppUnit::TestCase(name) |
| 24 | { |
| 25 | } |
| 26 | |
| 27 | |
| 28 | MultipartWriterTest::~MultipartWriterTest() |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | |
| 33 | void MultipartWriterTest::testWriteOnePart() |
| 34 | { |
| 35 | std::ostringstream ostr; |
| 36 | MultipartWriter w(ostr, "MIME_boundary_01234567" ); |
| 37 | assertTrue (w.boundary() == "MIME_boundary_01234567" ); |
| 38 | MessageHeader h; |
| 39 | h.set("name1" , "value1" ); |
| 40 | w.nextPart(h); |
| 41 | ostr << "this is part 1" ; |
| 42 | w.close(); |
| 43 | std::string s = ostr.str(); |
| 44 | assertTrue (s == "--MIME_boundary_01234567\r\nname1: value1\r\n\r\nthis is part 1\r\n--MIME_boundary_01234567--\r\n" ); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | void MultipartWriterTest::testWriteTwoParts() |
| 49 | { |
| 50 | std::ostringstream ostr; |
| 51 | MultipartWriter w(ostr, "MIME_boundary_01234567" ); |
| 52 | MessageHeader h; |
| 53 | h.set("name1" , "value1" ); |
| 54 | w.nextPart(h); |
| 55 | ostr << "this is part 1" ; |
| 56 | h.clear(); |
| 57 | w.nextPart(h); |
| 58 | ostr << "this is part 2" ; |
| 59 | w.close(); |
| 60 | std::string s = ostr.str(); |
| 61 | assertTrue (s == "--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--MIME_boundary_01234567--\r\n" ); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | void MultipartWriterTest::testBoundary() |
| 66 | { |
| 67 | std::ostringstream ostr; |
| 68 | MultipartWriter w(ostr); |
| 69 | std::string boundary = w.boundary(); |
| 70 | assertTrue (boundary.substr(0, 14) == "MIME_boundary_" ); |
| 71 | assertTrue (boundary.length() == 14 + 16); |
| 72 | } |
| 73 | |
| 74 | |
| 75 | void MultipartWriterTest::setUp() |
| 76 | { |
| 77 | } |
| 78 | |
| 79 | |
| 80 | void MultipartWriterTest::tearDown() |
| 81 | { |
| 82 | } |
| 83 | |
| 84 | |
| 85 | CppUnit::Test* MultipartWriterTest::suite() |
| 86 | { |
| 87 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("MultipartWriterTest" ); |
| 88 | |
| 89 | CppUnit_addTest(pSuite, MultipartWriterTest, testWriteOnePart); |
| 90 | CppUnit_addTest(pSuite, MultipartWriterTest, testWriteTwoParts); |
| 91 | CppUnit_addTest(pSuite, MultipartWriterTest, testBoundary); |
| 92 | |
| 93 | return pSuite; |
| 94 | } |
| 95 | |