4
// $Id: //poco/1.2/Net/testsuite/src/MailMessageTest.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 "MailMessageTest.h"
34
#include "CppUnit/TestCaller.h"
35
#include "CppUnit/TestSuite.h"
36
#include "Poco/Net/MailMessage.h"
37
#include "Poco/Net/MailRecipient.h"
38
#include "Poco/Net/PartHandler.h"
39
#include "Poco/Net/StringPartSource.h"
40
#include "Poco/Net/MediaType.h"
41
#include "Poco/Timestamp.h"
46
using Poco::Net::MailMessage;
47
using Poco::Net::MailRecipient;
48
using Poco::Net::MessageHeader;
49
using Poco::Net::PartHandler;
50
using Poco::Net::MediaType;
51
using Poco::Net::StringPartSource;
52
using Poco::Timestamp;
57
class StringPartHandler: public PartHandler
64
void handlePart(const MessageHeader& header, std::istream& stream)
66
_disp.push_back(header["Content-Disposition"]);
67
_type.push_back(header["Content-Type"]);
69
int ch = stream.get();
75
_data.push_back(data);
78
const std::vector<std::string>& data() const
83
const std::vector<std::string>& disp() const
88
const std::vector<std::string>& type() const
94
std::vector<std::string> _data;
95
std::vector<std::string> _disp;
96
std::vector<std::string> _type;
101
MailMessageTest::MailMessageTest(const std::string& name): CppUnit::TestCase(name)
106
MailMessageTest::~MailMessageTest()
111
void MailMessageTest::testWriteQP()
114
MailRecipient r1(MailRecipient::PRIMARY_RECIPIENT, "john.doe@no.where", "John Doe");
115
MailRecipient r2(MailRecipient::CC_RECIPIENT, "jane.doe@no.where", "Jane Doe");
116
MailRecipient r3(MailRecipient::BCC_RECIPIENT, "walter.foo@no.where", "Frank Foo");
117
MailRecipient r4(MailRecipient::BCC_RECIPIENT, "bernie.bar@no.where", "Bernie Bar");
118
message.addRecipient(r1);
119
message.addRecipient(r2);
120
message.addRecipient(r3);
121
message.addRecipient(r4);
122
message.setSubject("Test Message");
123
message.setSender("poco@appinf.com");
126
"This is a test for the MailMessage class.\r\n"
127
"To test the quoted-printable encoding, we'll put an extra long line here. This should be enough.\r\n"
128
"And here is some more =fe.\r\n"
133
assert (!message.isMultipart());
135
std::ostringstream str;
137
std::string s = str.str();
139
"CC: Jane Doe <jane.doe@no.where>\r\n"
140
"Content-Transfer-Encoding: quoted-printable\r\n"
141
"Content-Type: text/plain\r\n"
142
"Date: Thu, 1 Jan 1970 00:00:00 GMT\r\n"
143
"From: poco@appinf.com\r\n"
144
"Subject: Test Message\r\n"
145
"To: John Doe <john.doe@no.where>\r\n"
148
"This is a test for the MailMessage class.\r\n"
149
"To test the quoted-printable encoding, we'll put an extra long line here. T=\r\n"
150
"his should be enough.\r\n"
151
"And here is some more =3Dfe.\r\n"
156
void MailMessageTest::testWrite8Bit()
159
MailRecipient r1(MailRecipient::PRIMARY_RECIPIENT, "john.doe@no.where", "John Doe");
160
message.addRecipient(r1);
161
message.setSubject("Test Message");
162
message.setSender("poco@appinf.com");
165
"This is a test for the MailMessage class.\r\n",
166
MailMessage::ENCODING_8BIT
171
std::ostringstream str;
173
std::string s = str.str();
175
"Content-Transfer-Encoding: 8bit\r\n"
176
"Content-Type: text/plain\r\n"
177
"Date: Thu, 1 Jan 1970 00:00:00 GMT\r\n"
178
"From: poco@appinf.com\r\n"
179
"Subject: Test Message\r\n"
180
"To: John Doe <john.doe@no.where>\r\n"
183
"This is a test for the MailMessage class.\r\n"
188
void MailMessageTest::testWriteBase64()
191
MailRecipient r1(MailRecipient::PRIMARY_RECIPIENT, "john.doe@no.where", "John Doe");
192
message.addRecipient(r1);
193
message.setSubject("Test Message");
194
message.setSender("poco@appinf.com");
197
"This is a test for the MailMessage class.\r\n",
198
MailMessage::ENCODING_BASE64
203
std::ostringstream str;
205
std::string s = str.str();
207
"Content-Transfer-Encoding: base64\r\n"
208
"Content-Type: text/plain\r\n"
209
"Date: Thu, 1 Jan 1970 00:00:00 GMT\r\n"
210
"From: poco@appinf.com\r\n"
211
"Subject: Test Message\r\n"
212
"To: John Doe <john.doe@no.where>\r\n"
214
"SGVsbG8sIHdvcmxkIQ0KVGhpcyBpcyBhIHRlc3QgZm9yIHRoZSBNYWlsTWVzc2FnZSBjbGFz\r\n"
220
void MailMessageTest::testWriteManyRecipients()
223
MailRecipient r1(MailRecipient::PRIMARY_RECIPIENT, "john.doe@no.where", "John Doe");
224
MailRecipient r2(MailRecipient::PRIMARY_RECIPIENT, "jane.doe@no.where", "Jane Doe");
225
MailRecipient r3(MailRecipient::PRIMARY_RECIPIENT, "walter.foo@no.where", "Frank Foo");
226
MailRecipient r4(MailRecipient::PRIMARY_RECIPIENT, "bernie.bar@no.where", "Bernie Bar");
227
MailRecipient r5(MailRecipient::PRIMARY_RECIPIENT, "joe.spammer@no.where", "Joe Spammer");
228
message.addRecipient(r1);
229
message.addRecipient(r2);
230
message.addRecipient(r3);
231
message.addRecipient(r4);
232
message.addRecipient(r5);
233
message.setSubject("Test Message");
234
message.setSender("poco@appinf.com");
237
"This is a test for the MailMessage class.\r\n",
238
MailMessage::ENCODING_8BIT
243
std::ostringstream str;
245
std::string s = str.str();
247
"Content-Transfer-Encoding: 8bit\r\n"
248
"Content-Type: text/plain\r\n"
249
"Date: Thu, 1 Jan 1970 00:00:00 GMT\r\n"
250
"From: poco@appinf.com\r\n"
251
"Subject: Test Message\r\n"
252
"To: John Doe <john.doe@no.where>, Jane Doe <jane.doe@no.where>, \r\n"
253
"\tFrank Foo <walter.foo@no.where>, Bernie Bar <bernie.bar@no.where>, \r\n"
254
"\tJoe Spammer <joe.spammer@no.where>\r\n"
257
"This is a test for the MailMessage class.\r\n"
262
void MailMessageTest::testWriteMultiPart()
265
MailRecipient r1(MailRecipient::PRIMARY_RECIPIENT, "john.doe@no.where", "John Doe");
266
message.addRecipient(r1);
267
message.setSubject("Test Message");
268
message.setSender("poco@appinf.com");
271
message.addContent(new StringPartSource("Hello World!\r\n", "text/plain"), MailMessage::ENCODING_8BIT);
272
message.addAttachment("sample", new StringPartSource("This is some binary data. Really.", "application/octet-stream", "sample.dat"));
274
assert (message.isMultipart());
276
std::ostringstream str;
278
std::string s = str.str();
280
"Content-Type: multipart/mixed; boundary=$\r\n"
281
"Date: Thu, 1 Jan 1970 00:00:00 GMT\r\n"
282
"From: poco@appinf.com\r\n"
283
"Mime-Version: 1.0\r\n"
284
"Subject: Test Message\r\n"
285
"To: John Doe <john.doe@no.where>\r\n"
288
"Content-Disposition: inline\r\n"
289
"Content-Transfer-Encoding: 8bit\r\n"
290
"Content-Type: text/plain\r\n"
295
"Content-Disposition: attachment; filename=sample.dat\r\n"
296
"Content-Transfer-Encoding: base64\r\n"
297
"Content-Type: application/octet-stream; name=sample\r\n"
299
"VGhpcyBpcyBzb21lIGJpbmFyeSBkYXRhLiBSZWFsbHku\r\n"
302
std::string::size_type p1 = s.find('=') + 1;
303
std::string::size_type p2 = s.find('\r');
304
std::string boundary(s, p1, p2 - p1);
306
for (std::string::const_iterator it = rawMsg.begin(); it != rawMsg.end(); ++it)
317
void MailMessageTest::testReadQP()
319
std::istringstream istr(
320
"Content-Transfer-Encoding: quoted-printable\r\n"
321
"Content-Type: text/plain\r\n"
322
"Date: Thu, 1 Jan 1970 00:00:00 GMT\r\n"
323
"From: poco@appinf.com\r\n"
324
"Subject: Test Message\r\n"
325
"To: John Doe <john.doe@no.where>\r\n"
328
"This is a test for the MailMessage class.\r\n"
329
"To test the quoted-printable encoding, we'll put an extra long line here. T=\r\n"
330
"his should be enough.\r\n"
331
"And here is some more =3Dfe.\r\n"
337
assert (message.getSender() == "poco@appinf.com");
338
assert (message.getContentType() == "text/plain");
339
assert (message.getContent() ==
341
"This is a test for the MailMessage class.\r\n"
342
"To test the quoted-printable encoding, we'll put an extra long line here. This should be enough.\r\n"
343
"And here is some more =fe.\r\n"
348
void MailMessageTest::testRead8Bit()
350
std::istringstream istr(
351
"Content-Transfer-Encoding: 8bit\r\n"
352
"Content-Type: text/plain\r\n"
353
"Date: Thu, 1 Jan 1970 00:00:00 GMT\r\n"
354
"From: poco@appinf.com\r\n"
355
"Subject: Test Message\r\n"
356
"To: John Doe <john.doe@no.where>\r\n"
359
"This is a test for the MailMessage class.\r\n"
365
assert (message.getSender() == "poco@appinf.com");
366
assert (message.getContentType() == "text/plain");
367
assert (message.getContent() ==
369
"This is a test for the MailMessage class.\r\n"
374
void MailMessageTest::testReadMultiPart()
376
std::istringstream istr(
377
"Content-Type: multipart/mixed; boundary=MIME_boundary_01234567\r\n"
378
"Date: Thu, 1 Jan 1970 00:00:00 GMT\r\n"
379
"From: poco@appinf.com\r\n"
380
"Mime-Version: 1.0\r\n"
381
"Subject: Test Message\r\n"
382
"To: John Doe <john.doe@no.where>\r\n"
385
"--MIME_boundary_01234567\r\n"
386
"Content-Disposition: inline\r\n"
387
"Content-Transfer-Encoding: 8bit\r\n"
388
"Content-Type: text/plain\r\n"
392
"--MIME_boundary_01234567\r\n"
393
"Content-Disposition: attachment; filename=sample.dat\r\n"
394
"Content-Transfer-Encoding: base64\r\n"
395
"Content-Type: application/octet-stream; name=sample\r\n"
397
"VGhpcyBpcyBzb21lIGJpbmFyeSBkYXRhLiBSZWFsbHku\r\n"
398
"--MIME_boundary_01234567--\r\n"
401
StringPartHandler handler;
403
message.read(istr, handler);
405
assert (handler.data().size() == 2);
406
assert (handler.data()[0] == "Hello World!\r\n");
407
assert (handler.type()[0] == "text/plain");
408
assert (handler.disp()[0] == "inline");
410
assert (handler.data()[1] == "This is some binary data. Really.");
411
assert (handler.type()[1] == "application/octet-stream; name=sample");
412
assert (handler.disp()[1] == "attachment; filename=sample.dat");
416
void MailMessageTest::setUp()
421
void MailMessageTest::tearDown()
426
CppUnit::Test* MailMessageTest::suite()
428
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("MailMessageTest");
430
CppUnit_addTest(pSuite, MailMessageTest, testWriteQP);
431
CppUnit_addTest(pSuite, MailMessageTest, testWrite8Bit);
432
CppUnit_addTest(pSuite, MailMessageTest, testWriteBase64);
433
CppUnit_addTest(pSuite, MailMessageTest, testWriteManyRecipients);
434
CppUnit_addTest(pSuite, MailMessageTest, testWriteMultiPart);
435
CppUnit_addTest(pSuite, MailMessageTest, testReadQP);
436
CppUnit_addTest(pSuite, MailMessageTest, testRead8Bit);
437
CppUnit_addTest(pSuite, MailMessageTest, testReadMultiPart);