void MailMessageTest::testWrite8Bit()
{
	MailMessage message;
	MailRecipient r1(MailRecipient::PRIMARY_RECIPIENT, "*****@*****.**", "John Doe");
	message.addRecipient(r1);
	message.setSubject("Test Message");
	message.setSender("*****@*****.**");
	message.setContent(
		"Hello, world!\r\n"
		"This is a test for the MailMessage class.\r\n",
		MailMessage::ENCODING_8BIT
	);
	Timestamp ts(0);
	message.setDate(ts);
	
	std::ostringstream str;
	message.write(str);
	std::string s = str.str();
	assert (s == 
		"Date: Thu, 1 Jan 1970 00:00:00 GMT\r\n"
		"Content-Type: text/plain\r\n"
		"Subject: Test Message\r\n"
		"From: [email protected]\r\n"
		"Content-Transfer-Encoding: 8bit\r\n"
		"To: John Doe <*****@*****.**>\r\n"
		"\r\n"
		"Hello, world!\r\n"
		"This is a test for the MailMessage class.\r\n"
	);
}
void MailMessageTest::testWriteBase64()
{
	MailMessage message;
	MailRecipient r1(MailRecipient::PRIMARY_RECIPIENT, "*****@*****.**", "John Doe");
	message.addRecipient(r1);
	message.setSubject("Test Message");
	message.setSender("*****@*****.**");
	message.setContent(
		"Hello, world!\r\n"
		"This is a test for the MailMessage class.\r\n",
		MailMessage::ENCODING_BASE64
	);
	Timestamp ts(0);
	message.setDate(ts);

	std::ostringstream str;
	message.write(str);
	std::string s = str.str();
	assert (s == 
		"Date: Thu, 1 Jan 1970 00:00:00 GMT\r\n"
		"Content-Type: text/plain\r\n"
		"Subject: Test Message\r\n"
		"From: [email protected]\r\n"
		"Content-Transfer-Encoding: base64\r\n"
		"To: John Doe <*****@*****.**>\r\n"
		"\r\n"
		"SGVsbG8sIHdvcmxkIQ0KVGhpcyBpcyBhIHRlc3QgZm9yIHRoZSBNYWlsTWVzc2FnZSBjbGFz\r\n"
		"cy4NCg=="
	);
}
void MailMessageTest::testWriteMultiPart()
{
	MailMessage message;
	MailRecipient r1(MailRecipient::PRIMARY_RECIPIENT, "*****@*****.**", "John Doe");
	message.addRecipient(r1);
	message.setSubject("Test Message");
	message.setSender("*****@*****.**");
	Timestamp ts(0);
	message.setDate(ts);
	message.addContent(new StringPartSource("Hello World!\r\n", "text/plain"), MailMessage::ENCODING_8BIT);
	StringPartSource* pSPS = new StringPartSource("This is some binary data. Really.", "application/octet-stream", "sample.dat");
	pSPS->headers().set("Content-ID", "abcd1234");
	message.addAttachment("sample", pSPS);

	assert (message.isMultipart());

	std::ostringstream str;
	message.write(str);
	std::string s = str.str();
	std::string rawMsg(
		"Date: Thu, 1 Jan 1970 00:00:00 GMT\r\n"
		"Content-Type: multipart/mixed; boundary=$\r\n"
		"Subject: Test Message\r\n"
		"From: [email protected]\r\n"
		"To: John Doe <*****@*****.**>\r\n"
		"Mime-Version: 1.0\r\n"
		"\r\n"
		"--$\r\n"
		"Content-Type: text/plain\r\n"
		"Content-Transfer-Encoding: 8bit\r\n"
		"Content-Disposition: inline\r\n"
		"\r\n"
		"Hello World!\r\n"
		"\r\n"
		"--$\r\n"
		"Content-ID: abcd1234\r\n"
		"Content-Type: application/octet-stream; name=sample\r\n"
		"Content-Transfer-Encoding: base64\r\n"
		"Content-Disposition: attachment; filename=sample.dat\r\n"
		"\r\n"
		"VGhpcyBpcyBzb21lIGJpbmFyeSBkYXRhLiBSZWFsbHku\r\n"
		"--$--\r\n"
	);
	std::string::size_type p1 = s.find('=') + 1;
	std::string::size_type p2 = s.find('\r', p1);
	std::string boundary(s, p1, p2 - p1);
	std::string msg;
	for (std::string::const_iterator it = rawMsg.begin(); it != rawMsg.end(); ++it)
	{
		if (*it == '$')
			msg += boundary;
		else
			msg += *it;
	}

	assert (s == msg);
}
void MailMessageTest::testWriteQP()
{
	MailMessage message;
	MailRecipient r1(MailRecipient::PRIMARY_RECIPIENT, "*****@*****.**", "John Doe");
	MailRecipient r2(MailRecipient::CC_RECIPIENT, "*****@*****.**", "Jane Doe");
	MailRecipient r3(MailRecipient::BCC_RECIPIENT, "*****@*****.**", "Frank Foo");
	MailRecipient r4(MailRecipient::BCC_RECIPIENT, "*****@*****.**", "Bernie Bar");
	message.addRecipient(r1);
	message.addRecipient(r2);
	message.addRecipient(r3);
	message.addRecipient(r4);
	message.setSubject("Test Message");
	message.setSender("*****@*****.**");
	message.setContent(
		"Hello, world!\r\n"
		"This is a test for the MailMessage class.\r\n"
		"To test the quoted-printable encoding, we'll put an extra long line here. This should be enough.\r\n"
		"And here is some more =fe.\r\n"
	);
	Timestamp ts(0);
	message.setDate(ts);
	
	assert (!message.isMultipart());
	
	std::ostringstream str;
	message.write(str);
	std::string s = str.str();

	assert (s == 
		"Date: Thu, 1 Jan 1970 00:00:00 GMT\r\n"
		"Content-Type: text/plain\r\n"
		"Subject: Test Message\r\n"
		"From: [email protected]\r\n"
		"Content-Transfer-Encoding: quoted-printable\r\n"
		"To: John Doe <*****@*****.**>\r\n"
		"CC: Jane Doe <*****@*****.**>\r\n"
		"\r\n"
		"Hello, world!\r\n"
		"This is a test for the MailMessage class.\r\n"
		"To test the quoted-printable encoding, we'll put an extra long line here. T=\r\n"
		"his should be enough.\r\n"
		"And here is some more =3Dfe.\r\n"
	);
}
void MailMessageTest::testWriteManyRecipients()
{
	MailMessage message;
	MailRecipient r1(MailRecipient::PRIMARY_RECIPIENT, "*****@*****.**", "John Doe");
	MailRecipient r2(MailRecipient::PRIMARY_RECIPIENT, "*****@*****.**", "Jane Doe");
	MailRecipient r3(MailRecipient::PRIMARY_RECIPIENT, "*****@*****.**", "Frank Foo");
	MailRecipient r4(MailRecipient::PRIMARY_RECIPIENT, "*****@*****.**", "Bernie Bar");
	MailRecipient r5(MailRecipient::PRIMARY_RECIPIENT, "*****@*****.**", "Joe Spammer");
	message.addRecipient(r1);
	message.addRecipient(r2);
	message.addRecipient(r3);
	message.addRecipient(r4);
	message.addRecipient(r5);
	message.setSubject("Test Message");
	message.setSender("*****@*****.**");
	message.setContent(
		"Hello, world!\r\n"
		"This is a test for the MailMessage class.\r\n",
		MailMessage::ENCODING_8BIT
	);
	Timestamp ts(0);
	message.setDate(ts);
	
	std::ostringstream str;
	message.write(str);
	std::string s = str.str();
	assert (s == 
		"Date: Thu, 1 Jan 1970 00:00:00 GMT\r\n"
		"Content-Type: text/plain\r\n"
		"Subject: Test Message\r\n"
		"From: [email protected]\r\n"
		"Content-Transfer-Encoding: 8bit\r\n"
		"To: John Doe <*****@*****.**>, Jane Doe <*****@*****.**>, \r\n"
		"\tFrank Foo <*****@*****.**>, Bernie Bar <*****@*****.**>, \r\n"
		"\tJoe Spammer <*****@*****.**>\r\n"
		"\r\n"
		"Hello, world!\r\n"
		"This is a test for the MailMessage class.\r\n"
	);
}