From e129e0369d18c4cd1c0d580160b47bd6d5de7411 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sun, 9 Mar 2025 22:23:01 +0200 Subject: [PATCH] server(attachments): render empty SVGs properly (closes #1378) --- src/routes/api/image.spec.ts | 32 ++++++++++++++++++++++++++++++++ src/routes/api/image.ts | 4 ++-- 2 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 src/routes/api/image.spec.ts diff --git a/src/routes/api/image.spec.ts b/src/routes/api/image.spec.ts new file mode 100644 index 000000000..b6afdf580 --- /dev/null +++ b/src/routes/api/image.spec.ts @@ -0,0 +1,32 @@ +import { describe, expect, it } from "vitest"; +import { note } from "../../../spec/support/becca_mocking.js"; +import { renderSvgAttachment } from "./image.js"; + +describe("Image API", () => { + it("renders empty SVG properly", () => { + const parentNote = note("note").note; + const response = new MockResponse(); + renderSvgAttachment(parentNote, response as any, "attachment"); + expect(response.headers["Content-Type"]).toBe("image/svg+xml"); + expect(response.body).toBe(``); + }); +}); + +class MockResponse { + + body?: string; + headers: Record; + + constructor() { + this.headers = {}; + } + + set(name: string, value: string) { + this.headers[name] = value; + } + + send(body: string) { + this.body = body; + } + +} diff --git a/src/routes/api/image.ts b/src/routes/api/image.ts index d2f2b5632..aa24e7aae 100644 --- a/src/routes/api/image.ts +++ b/src/routes/api/image.ts @@ -41,8 +41,8 @@ function returnImageInt(image: BNote | BRevision | null, res: Response) { } } -function renderSvgAttachment(image: BNote | BRevision, res: Response, attachmentName: string) { - let svg: string | Buffer = ""; +export function renderSvgAttachment(image: BNote | BRevision, res: Response, attachmentName: string) { + let svg: string | Buffer = ``; const attachment = image.getAttachmentByTitle(attachmentName); if (attachment) {