Mail.td REST API 为端到端测试设计 — 验证注册、密码重置和邮件触发的工作流是否正确。本指南聚焦常见测试工具的集成模式。
原始 endpoint 参考(请求/响应结构),看 API 页 或 docs.mail.td。
Selenium / Playwright / Cypress
模式:每次测试前创建一个新的收件箱,用这个地址在你应用上注册,然后轮询 API 拿验证邮件。
// 伪代码 — 适用于任何测试框架
async function signUpFlow(page) {
// 1. 创建临时邮箱
const mailbox = await fetch("https://api.mail.td/api/accounts", {
method: "POST",
headers: {
Authorization: "Bearer td_xxx",
"Content-Type": "application/json"
},
body: JSON.stringify({
address: `test-${Date.now()}@mail.td`,
password: "secret"
})
}).then(r => r.json());
// 2. 用 mailbox.address 在你应用上注册
await page.goto("/signup");
await page.fill("[name=email]", mailbox.address);
await page.click("button[type=submit]");
// 3. 轮询验证邮件(最长 30 秒)
for (let i = 0; i < 30; i++) {
const list = await fetch(
`https://api.mail.td/api/accounts/${mailbox.id}/messages`,
{ headers: { Authorization: "Bearer td_xxx" } }
).then(r => r.json());
if (list.messages.length > 0) {
// 列表端点不返回正文,需要再取详情拿 text_body
const msg = await fetch(
`https://api.mail.td/api/accounts/${mailbox.id}/messages/${list.messages[0].id}`,
{ headers: { Authorization: "Bearer td_xxx" } }
).then(r => r.json());
const link = extractLink(msg.text_body);
await page.goto(link);
break;
}
await new Promise(r => setTimeout(r, 1000));
}
}
CI/CD 流水线
GitHub Actions、GitLab CI、CircleCI 或任何流水线:
- 把一个 API token 存为 CI 密钥(如果泄露就在控制台轮转)
- 每个测试 job 创建自己的邮箱 — 避免并行 job 间的竞态
- 每个测试用唯一地址(如
test-${RUN_ID}-${TEST_ID}@mail.td),失败排查更容易 - 不用清理 — 邮箱自动过期,不需要清理脚本
Webhook 加速流水线(Pro)
轮询给每个测试增加延迟。长时间跑的测试套件,可以注册一个 webhook URL,新邮件到达时收到 POST。你的测试等 webhook 投递而不是轮询 30 次。把典型 5–15 秒的等待砍到 1 秒以内。
Webhook 用 HMAC-SHA256 签名 — 处理前先校验 signature header。
SDK 安装
npm install mailtd # Node.js / TypeScript
pip install mailtd # Python
go get github.com/mailtd/mailtd-go # Go
完整 endpoint 参考,看 mail.td/api。