|
| 1 | +import { createServer } from "node:http"; |
1 | 2 | import { describe, expect, it, vi } from "vitest"; |
2 | 3 | import type { ZaloFetch } from "./api.js"; |
3 | 4 | import { probeZalo } from "./probe.js"; |
@@ -31,6 +32,53 @@ describe("probeZalo", () => { |
31 | 32 | }); |
32 | 33 | }); |
33 | 34 |
|
| 35 | + it("rejects a non-ok HTTP response with a success-shaped body", async () => { |
| 36 | + const server = createServer((_request, response) => { |
| 37 | + response.writeHead(401, { "content-type": "application/json" }); |
| 38 | + response.end( |
| 39 | + JSON.stringify({ |
| 40 | + ok: true, |
| 41 | + result: { |
| 42 | + id: "controlled-bot", |
| 43 | + account_name: "Controlled Bot", |
| 44 | + account_type: "BOT", |
| 45 | + can_join_groups: false, |
| 46 | + }, |
| 47 | + }), |
| 48 | + ); |
| 49 | + }); |
| 50 | + await new Promise<void>((resolve, reject) => { |
| 51 | + server.once("error", reject); |
| 52 | + server.listen(0, "127.0.0.1", () => { |
| 53 | + server.off("error", reject); |
| 54 | + resolve(); |
| 55 | + }); |
| 56 | + }); |
| 57 | + const address = server.address(); |
| 58 | + if (!address || typeof address === "string") { |
| 59 | + throw new Error("expected loopback Zalo API address"); |
| 60 | + } |
| 61 | + const originalApiUrl = process.env.ZALO_API_URL; |
| 62 | + process.env.ZALO_API_URL = `http://127.0.0.1:${String(address.port)}`; |
| 63 | + |
| 64 | + try { |
| 65 | + await expect(probeZalo("controlled-token", 1000)).resolves.toMatchObject({ |
| 66 | + ok: false, |
| 67 | + error: expect.stringContaining("zalo.getMe (401)"), |
| 68 | + }); |
| 69 | + } finally { |
| 70 | + if (originalApiUrl === undefined) { |
| 71 | + delete process.env.ZALO_API_URL; |
| 72 | + } else { |
| 73 | + process.env.ZALO_API_URL = originalApiUrl; |
| 74 | + } |
| 75 | + server.closeAllConnections(); |
| 76 | + await new Promise<void>((resolve, reject) => { |
| 77 | + server.close((error) => (error ? reject(error) : resolve())); |
| 78 | + }); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
34 | 82 | it("preserves timeout errors", async () => { |
35 | 83 | const fetcher = vi.fn<ZaloFetch>(async () => { |
36 | 84 | throw new DOMException("aborted", "AbortError"); |
|
0 commit comments