|
3 | 3 | // by the request deadline, not only by the per-chunk idle guard. This exercises the |
4 | 4 | // production containerRpcRequest -> containerRestRequest -> readSignalRestText path |
5 | 5 | // without mocking fetch, unlike the fake-timer unit tests. |
| 6 | +import { mkdtemp, rm, writeFile } from "node:fs/promises"; |
6 | 7 | import http from "node:http"; |
7 | 8 | import type { AddressInfo } from "node:net"; |
| 9 | +import { tmpdir } from "node:os"; |
| 10 | +import { join } from "node:path"; |
8 | 11 | import { afterEach, describe, expect, it } from "vitest"; |
9 | 12 | import { containerRpcRequest } from "./client-container.js"; |
10 | 13 |
|
@@ -95,4 +98,99 @@ describe("signal REST real-server deadline", () => { |
95 | 98 | ); |
96 | 99 | expect(result).toEqual({ versions: ["v1"], build: 2 }); |
97 | 100 | }); |
| 101 | + |
| 102 | + it.each([ |
| 103 | + { |
| 104 | + stagedFilename: "report---a1b2c3d4-5678-90ab-cdef-1234567890ab.jpg", |
| 105 | + expectedFilename: "report.jpg", |
| 106 | + }, |
| 107 | + { |
| 108 | + stagedFilename: "quarter;final---a1b2c3d4-5678-90ab-cdef-1234567890ab.jpg", |
| 109 | + expectedFilename: "quarter_final.jpg", |
| 110 | + }, |
| 111 | + { |
| 112 | + stagedFilename: "first;middle;last---a1b2c3d4-5678-90ab-cdef-1234567890ab.jpg", |
| 113 | + expectedFilename: "first_middle_last.jpg", |
| 114 | + }, |
| 115 | + { |
| 116 | + stagedFilename: "quarter,final---a1b2c3d4-5678-90ab-cdef-1234567890ab.jpg", |
| 117 | + expectedFilename: "quarter_final.jpg", |
| 118 | + }, |
| 119 | + { |
| 120 | + stagedFilename: "first,middle,last---a1b2c3d4-5678-90ab-cdef-1234567890ab.jpg", |
| 121 | + expectedFilename: "first_middle_last.jpg", |
| 122 | + }, |
| 123 | + { |
| 124 | + stagedFilename: "hash#name---a1b2c3d4-5678-90ab-cdef-1234567890ab.jpg", |
| 125 | + expectedFilename: "hash_name.jpg", |
| 126 | + }, |
| 127 | + { |
| 128 | + stagedFilename: "mixed;comma,hash#name---a1b2c3d4-5678-90ab-cdef-1234567890ab.jpg", |
| 129 | + expectedFilename: "mixed_comma_hash_name.jpg", |
| 130 | + }, |
| 131 | + { stagedFilename: "quarter;final.jpg", expectedFilename: "quarter_final.jpg" }, |
| 132 | + { stagedFilename: "quarter,final.jpg", expectedFilename: "quarter_final.jpg" }, |
| 133 | + { stagedFilename: "hash#name.jpg", expectedFilename: "hash_name.jpg" }, |
| 134 | + { |
| 135 | + stagedFilename: "quarter final---a1b2c3d4-5678-90ab-cdef-1234567890ab.jpg", |
| 136 | + expectedFilename: "quarter final.jpg", |
| 137 | + }, |
| 138 | + ])( |
| 139 | + "posts the provider-safe original filename $expectedFilename", |
| 140 | + async ({ stagedFilename, expectedFilename }) => { |
| 141 | + let receivedPayload: unknown; |
| 142 | + const server = await startServer((req, res) => { |
| 143 | + if (req.method !== "POST" || req.url !== "/v2/send") { |
| 144 | + res.writeHead(404); |
| 145 | + res.end(); |
| 146 | + return; |
| 147 | + } |
| 148 | + |
| 149 | + const chunks: Buffer[] = []; |
| 150 | + req.on("data", (chunk: Buffer | string) => { |
| 151 | + chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk); |
| 152 | + }); |
| 153 | + req.on("end", () => { |
| 154 | + receivedPayload = JSON.parse(Buffer.concat(chunks).toString("utf8")); |
| 155 | + res.writeHead(200, { "content-type": "application/json" }); |
| 156 | + res.end(JSON.stringify({ timestamp: "1735689600000" })); |
| 157 | + }); |
| 158 | + }); |
| 159 | + |
| 160 | + const mediaDir = await mkdtemp(join(tmpdir(), "signal-real-filename-")); |
| 161 | + const content = Buffer.from([0xff, 0xd8, 0xff, 0xe0]); |
| 162 | + const stagedFile = join(mediaDir, stagedFilename); |
| 163 | + |
| 164 | + try { |
| 165 | + await writeFile(stagedFile, content); |
| 166 | + await expect( |
| 167 | + containerRpcRequest( |
| 168 | + "send", |
| 169 | + { |
| 170 | + account: "+14259798283", |
| 171 | + recipient: ["+15550001111"], |
| 172 | + message: "Photo", |
| 173 | + attachments: [stagedFile], |
| 174 | + }, |
| 175 | + { baseUrl: server.baseUrl, timeoutMs: 1_000 }, |
| 176 | + ), |
| 177 | + ).resolves.toEqual({ timestamp: 1735689600000 }); |
| 178 | + |
| 179 | + expect(receivedPayload).toEqual({ |
| 180 | + message: "Photo", |
| 181 | + number: "+14259798283", |
| 182 | + recipients: ["+15550001111"], |
| 183 | + base64_attachments: [ |
| 184 | + `data:image/jpeg;filename=${expectedFilename};base64,${content.toString("base64")}`, |
| 185 | + ], |
| 186 | + }); |
| 187 | + const attachment = (receivedPayload as { base64_attachments: [string] }) |
| 188 | + .base64_attachments[0]; |
| 189 | + const decoded = await (await fetch(attachment)).arrayBuffer(); |
| 190 | + expect(Buffer.from(decoded)).toEqual(content); |
| 191 | + } finally { |
| 192 | + await rm(mediaDir, { recursive: true, force: true }); |
| 193 | + } |
| 194 | + }, |
| 195 | + ); |
98 | 196 | }); |
0 commit comments