|
1 | 1 | import { readFile } from "node:fs/promises"; |
2 | 2 | import { LogLevel, Miniflare } from "miniflare"; |
3 | 3 | import dedent from "ts-dedent"; |
4 | | -import { test, vi } from "vitest"; |
| 4 | +import { type ExpectStatic, test, vi } from "vitest"; |
5 | 5 | import { TestLog, useDispose } from "../../test-shared"; |
6 | 6 |
|
7 | 7 | const SEND_EMAIL_WORKER = dedent /* javascript */ ` |
@@ -1508,3 +1508,103 @@ test("MessageBuilder backward compatibility - old EmailMessage API still works", |
1508 | 1508 | expect(await res.text()).toBe("ok"); |
1509 | 1509 | expect(res.status).toBe(200); |
1510 | 1510 | }); |
| 1511 | + |
| 1512 | +const SEND_EMAIL_RETURNS_RESULT_WORKER = dedent /* javascript */ ` |
| 1513 | + import { EmailMessage } from "cloudflare:email"; |
| 1514 | +
|
| 1515 | + export default { |
| 1516 | + async fetch(request, env) { |
| 1517 | + const url = new URL(request.url); |
| 1518 | + const result = await env.SEND_EMAIL.send(new EmailMessage( |
| 1519 | + url.searchParams.get("from"), |
| 1520 | + url.searchParams.get("to"), |
| 1521 | + request.body |
| 1522 | + )); |
| 1523 | + return Response.json(result); |
| 1524 | + }, |
| 1525 | + }; |
| 1526 | +`; |
| 1527 | + |
| 1528 | +// Both branches return an id in the shape production returns: |
| 1529 | +// `<{36 alphanumeric chars}@{sender domain}>`, angle brackets included. |
| 1530 | +function synthesizedMessageId(expect: ExpectStatic, domain: string) { |
| 1531 | + return expect.stringMatching( |
| 1532 | + new RegExp(`^<[A-Za-z0-9]{36}@${domain.replace(/\./g, "\\.")}>$`) |
| 1533 | + ); |
| 1534 | +} |
| 1535 | + |
| 1536 | +test("send() on an EmailMessage returns a synthesized messageId", async ({ |
| 1537 | + expect, |
| 1538 | +}) => { |
| 1539 | + const mf = new Miniflare({ |
| 1540 | + modules: true, |
| 1541 | + script: SEND_EMAIL_RETURNS_RESULT_WORKER, |
| 1542 | + email: { |
| 1543 | + send_email: [{ name: "SEND_EMAIL" }], |
| 1544 | + }, |
| 1545 | + compatibilityDate: "2025-03-17", |
| 1546 | + }); |
| 1547 | + |
| 1548 | + useDispose(mf); |
| 1549 | + |
| 1550 | + const email = dedent` |
| 1551 | + From: someone <someone@sender.domain> |
| 1552 | + To: someone else <someone-else@example.com> |
| 1553 | + Message-ID: <do-not-echo-this@example.com> |
| 1554 | + MIME-Version: 1.0 |
| 1555 | + Content-Type: text/plain |
| 1556 | +
|
| 1557 | + body`; |
| 1558 | + |
| 1559 | + const res = await mf.dispatchFetch( |
| 1560 | + "http://localhost/?" + |
| 1561 | + new URLSearchParams({ |
| 1562 | + from: "someone@sender.domain", |
| 1563 | + to: "someone-else@example.com", |
| 1564 | + }).toString(), |
| 1565 | + { body: email, method: "POST" } |
| 1566 | + ); |
| 1567 | + |
| 1568 | + expect(res.status).toBe(200); |
| 1569 | + expect(await res.json()).toEqual({ |
| 1570 | + messageId: synthesizedMessageId(expect, "sender.domain"), |
| 1571 | + }); |
| 1572 | +}); |
| 1573 | + |
| 1574 | +test("send() on a MessageBuilder returns a synthesized messageId", async ({ |
| 1575 | + expect, |
| 1576 | +}) => { |
| 1577 | + const mf = new Miniflare({ |
| 1578 | + modules: true, |
| 1579 | + script: dedent /* javascript */ ` |
| 1580 | + export default { |
| 1581 | + async fetch(request, env) { |
| 1582 | + const builder = await request.json(); |
| 1583 | + const result = await env.SEND_EMAIL.send(builder); |
| 1584 | + return Response.json(result); |
| 1585 | + }, |
| 1586 | + }; |
| 1587 | + `, |
| 1588 | + email: { |
| 1589 | + send_email: [{ name: "SEND_EMAIL" }], |
| 1590 | + }, |
| 1591 | + compatibilityDate: "2025-03-17", |
| 1592 | + }); |
| 1593 | + |
| 1594 | + useDispose(mf); |
| 1595 | + |
| 1596 | + const res = await mf.dispatchFetch("http://localhost", { |
| 1597 | + method: "POST", |
| 1598 | + body: JSON.stringify({ |
| 1599 | + from: "sender@sender.domain", |
| 1600 | + to: "recipient@example.com", |
| 1601 | + subject: "s", |
| 1602 | + text: "t", |
| 1603 | + }), |
| 1604 | + }); |
| 1605 | + |
| 1606 | + expect(res.status).toBe(200); |
| 1607 | + expect(await res.json()).toEqual({ |
| 1608 | + messageId: synthesizedMessageId(expect, "sender.domain"), |
| 1609 | + }); |
| 1610 | +}); |
0 commit comments