inbound-mail.test.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import assert from 'node:assert/strict';
  2. import { test } from 'node:test';
  3. import { parseInboundMessage } from '../src/inbound-mail.js';
  4. test('parseInboundMessage extracts common headers and text bodies from MIME', async () => {
  5. const rawMessage = [
  6. 'From: Alice <alice@example.net>',
  7. 'To: Support <support@inbound.example>',
  8. 'Subject: =?UTF-8?B?5pS25L+h5rWL6K+V?=',
  9. 'Message-ID: <mime-test@example.net>',
  10. 'MIME-Version: 1.0',
  11. 'Content-Type: multipart/alternative; boundary="alt"',
  12. '',
  13. '--alt',
  14. 'Content-Type: text/plain; charset=UTF-8',
  15. 'Content-Transfer-Encoding: base64',
  16. '',
  17. Buffer.from('Hello plain body.', 'utf8').toString('base64'),
  18. '--alt',
  19. 'Content-Type: text/html; charset=UTF-8',
  20. 'Content-Transfer-Encoding: quoted-printable',
  21. '',
  22. '<p>Hello <strong>HTML</strong> body.</p>',
  23. '--alt--',
  24. ''
  25. ].join('\r\n');
  26. const parsed = await parseInboundMessage(rawMessage, ['support@inbound.example']);
  27. assert.equal(parsed.sender, 'alice@example.net');
  28. assert.deepEqual(parsed.recipients, ['support@inbound.example']);
  29. assert.equal(parsed.subject, '收信测试');
  30. assert.equal(parsed.messageId, '<mime-test@example.net>');
  31. assert.equal(parsed.textBody, 'Hello plain body.');
  32. assert.match(parsed.htmlBody, /<strong>HTML<\/strong>/);
  33. assert.equal(parsed.preview, 'Hello plain body.');
  34. });