add multipart/mixed formatting, base64 line-wrapped encoding to MessageFormatter
This commit is contained in:
@@ -173,4 +173,77 @@ struct MessageFormatterTests {
|
||||
#expect(dateString.contains(","))
|
||||
#expect(dateString.count > 20)
|
||||
}
|
||||
|
||||
// MARK: - Multipart Formatting
|
||||
|
||||
@Test("format with attachments produces multipart/mixed")
|
||||
func multipartWithAttachment() {
|
||||
let message = OutgoingMessage(
|
||||
from: EmailAddress(address: "alice@example.com"),
|
||||
to: [EmailAddress(address: "bob@example.com")],
|
||||
subject: "With attachment",
|
||||
bodyText: "See attached.",
|
||||
messageId: "test@example.com"
|
||||
)
|
||||
let attachments: [(filename: String, mimeType: String, data: Data)] = [
|
||||
(filename: "test.pdf", mimeType: "application/pdf", data: Data("PDF content".utf8)),
|
||||
]
|
||||
|
||||
let formatted = MessageFormatter.formatMultipart(message, attachments: attachments)
|
||||
|
||||
#expect(formatted.contains("Content-Type: multipart/mixed; boundary="))
|
||||
#expect(formatted.contains("Content-Type: text/plain; charset=utf-8"))
|
||||
#expect(formatted.contains("Content-Type: application/pdf; name=\"test.pdf\""))
|
||||
#expect(formatted.contains("Content-Disposition: attachment; filename=\"test.pdf\""))
|
||||
#expect(formatted.contains("Content-Transfer-Encoding: base64"))
|
||||
#expect(formatted.contains("See attached."))
|
||||
}
|
||||
|
||||
@Test("format without attachments produces single-part")
|
||||
func singlePartNoAttachments() {
|
||||
let message = OutgoingMessage(
|
||||
from: EmailAddress(address: "alice@example.com"),
|
||||
to: [EmailAddress(address: "bob@example.com")],
|
||||
subject: "Plain",
|
||||
bodyText: "Just text.",
|
||||
messageId: "test@example.com"
|
||||
)
|
||||
|
||||
let formatted = MessageFormatter.format(message)
|
||||
|
||||
#expect(formatted.contains("Content-Type: text/plain; charset=utf-8"))
|
||||
#expect(!formatted.contains("multipart"))
|
||||
}
|
||||
|
||||
@Test("base64 encoding wraps at 76 characters")
|
||||
func base64LineWrapping() {
|
||||
let data = Data(repeating: 0xFF, count: 100)
|
||||
let encoded = MessageFormatter.base64Encode(data)
|
||||
let lines = encoded.components(separatedBy: "\r\n")
|
||||
for line in lines where !line.isEmpty {
|
||||
#expect(line.count <= 76)
|
||||
}
|
||||
}
|
||||
|
||||
@Test("multiple attachments produce correct number of boundary sections")
|
||||
func multipleAttachments() {
|
||||
let message = OutgoingMessage(
|
||||
from: EmailAddress(address: "alice@example.com"),
|
||||
to: [EmailAddress(address: "bob@example.com")],
|
||||
subject: "Multi",
|
||||
bodyText: "Files.",
|
||||
messageId: "test@example.com"
|
||||
)
|
||||
let attachments: [(filename: String, mimeType: String, data: Data)] = [
|
||||
(filename: "a.pdf", mimeType: "application/pdf", data: Data("A".utf8)),
|
||||
(filename: "b.jpg", mimeType: "image/jpeg", data: Data("B".utf8)),
|
||||
]
|
||||
|
||||
let formatted = MessageFormatter.formatMultipart(message, attachments: attachments)
|
||||
|
||||
#expect(formatted.contains("a.pdf"))
|
||||
#expect(formatted.contains("b.jpg"))
|
||||
#expect(formatted.contains("application/pdf"))
|
||||
#expect(formatted.contains("image/jpeg"))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user