mirror of
https://github.com/MagicMirrorOrg/MagicMirror.git
synced 2026-03-12 17:51:41 +08:00
fix cors proxy getting binary data (e.g. png, webp) (#4030)
fixes #3266 --------- Co-authored-by: Kristjan ESPERANTO <35647502+KristjanESPERANTO@users.noreply.github.com>
This commit is contained in:
@@ -46,8 +46,8 @@ async function cors (req, res) {
|
||||
const headerValue = response.headers.get(header);
|
||||
if (header) res.set(header, headerValue);
|
||||
}
|
||||
const data = await response.text();
|
||||
res.send(data);
|
||||
const arrayBuffer = await response.arrayBuffer();
|
||||
res.send(Buffer.from(arrayBuffer));
|
||||
} else {
|
||||
throw new Error(`Response status: ${response.status}`);
|
||||
}
|
||||
|
||||
@@ -4,19 +4,19 @@ describe("server_functions tests", () => {
|
||||
describe("The cors method", () => {
|
||||
let fetchResponse;
|
||||
let fetchResponseHeadersGet;
|
||||
let fetchResponseHeadersText;
|
||||
let fetchResponseArrayBuffer;
|
||||
let corsResponse;
|
||||
let request;
|
||||
let fetchMock;
|
||||
|
||||
beforeEach(() => {
|
||||
fetchResponseHeadersGet = vi.fn(() => {});
|
||||
fetchResponseHeadersText = vi.fn(() => {});
|
||||
fetchResponseArrayBuffer = vi.fn(() => {});
|
||||
fetchResponse = {
|
||||
headers: {
|
||||
get: fetchResponseHeadersGet
|
||||
},
|
||||
text: fetchResponseHeadersText,
|
||||
arrayBuffer: fetchResponseArrayBuffer,
|
||||
ok: true
|
||||
};
|
||||
|
||||
@@ -78,7 +78,9 @@ describe("server_functions tests", () => {
|
||||
|
||||
it("Sends correct data from response", async () => {
|
||||
const responseData = "some data";
|
||||
fetchResponseHeadersText.mockImplementation(() => responseData);
|
||||
const encoder = new TextEncoder();
|
||||
const arrayBuffer = encoder.encode(responseData).buffer;
|
||||
fetchResponseArrayBuffer.mockImplementation(() => arrayBuffer);
|
||||
|
||||
let sentData;
|
||||
corsResponse.send = vi.fn((input) => {
|
||||
@@ -87,19 +89,19 @@ describe("server_functions tests", () => {
|
||||
|
||||
await cors(request, corsResponse);
|
||||
|
||||
expect(fetchResponseHeadersText.mock.calls).toHaveLength(1);
|
||||
expect(sentData).toBe(responseData);
|
||||
expect(fetchResponseArrayBuffer.mock.calls).toHaveLength(1);
|
||||
expect(sentData).toEqual(Buffer.from(arrayBuffer));
|
||||
});
|
||||
|
||||
it("Sends error data from response", async () => {
|
||||
const error = new Error("error data");
|
||||
fetchResponseHeadersText.mockImplementation(() => {
|
||||
fetchResponseArrayBuffer.mockImplementation(() => {
|
||||
throw error;
|
||||
});
|
||||
|
||||
await cors(request, corsResponse);
|
||||
|
||||
expect(fetchResponseHeadersText.mock.calls).toHaveLength(1);
|
||||
expect(fetchResponseArrayBuffer.mock.calls).toHaveLength(1);
|
||||
expect(corsResponse.status).toHaveBeenCalledWith(500);
|
||||
expect(corsResponse.json).toHaveBeenCalledWith({ error: error.message });
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user