mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-06-27 11:50:00 +00:00
related to #2649 I was able to move to internal fetch and all tests seems fine so far. But we have one problem with the calendar module. In the docs we have several authentication methods and one of them is `digest`. For this we used `digest-fetch` which needs `node-fetch` (this is not so clear from code but I was not able to get it working). So we have 3 options: - remove `digest` as authentication method for calendar module (this is what this PR does at the moment) - find an alternative npm package or implement the digest stuff ourselves - use `digest-fetch` and `node-fetch` for calendar module (so they would remain as dependencies in `package.json`) Opinions? @KristjanESPERANTO @rejas @sdetweil @MichMich
147 lines
4.5 KiB
JavaScript
147 lines
4.5 KiB
JavaScript
const { cors } = require("../../../js/server_functions");
|
|
|
|
describe("server_functions tests", () => {
|
|
describe("The cors method", () => {
|
|
let fetchResponse;
|
|
let fetchResponseHeadersGet;
|
|
let fetchResponseHeadersText;
|
|
let corsResponse;
|
|
let request;
|
|
|
|
let fetchMock;
|
|
|
|
beforeEach(() => {
|
|
fetchResponseHeadersGet = jest.fn(() => {});
|
|
fetchResponseHeadersText = jest.fn(() => {});
|
|
fetchResponse = {
|
|
headers: {
|
|
get: fetchResponseHeadersGet
|
|
},
|
|
text: fetchResponseHeadersText
|
|
};
|
|
// eslint-disable-next-line
|
|
fetch = jest.fn();
|
|
fetch.mockImplementation(() => fetchResponse);
|
|
|
|
fetchMock = fetch;
|
|
|
|
corsResponse = {
|
|
set: jest.fn(() => {}),
|
|
send: jest.fn(() => {})
|
|
};
|
|
|
|
request = {
|
|
url: `/cors?url=www.test.com`
|
|
};
|
|
});
|
|
|
|
test("Calls correct URL once", async () => {
|
|
const urlToCall = "http://www.test.com/path?param1=value1";
|
|
request.url = `/cors?url=${urlToCall}`;
|
|
|
|
await cors(request, corsResponse);
|
|
|
|
expect(fetchMock.mock.calls.length).toBe(1);
|
|
expect(fetchMock.mock.calls[0][0]).toBe(urlToCall);
|
|
});
|
|
|
|
test("Forewards Content-Type if json", async () => {
|
|
fetchResponseHeadersGet.mockImplementation(() => "json");
|
|
|
|
await cors(request, corsResponse);
|
|
|
|
expect(fetchResponseHeadersGet.mock.calls.length).toBe(1);
|
|
expect(fetchResponseHeadersGet.mock.calls[0][0]).toBe("Content-Type");
|
|
|
|
expect(corsResponse.set.mock.calls.length).toBe(1);
|
|
expect(corsResponse.set.mock.calls[0][0]).toBe("Content-Type");
|
|
expect(corsResponse.set.mock.calls[0][1]).toBe("json");
|
|
});
|
|
|
|
test("Forewards Content-Type if xml", async () => {
|
|
fetchResponseHeadersGet.mockImplementation(() => "xml");
|
|
|
|
await cors(request, corsResponse);
|
|
|
|
expect(fetchResponseHeadersGet.mock.calls.length).toBe(1);
|
|
expect(fetchResponseHeadersGet.mock.calls[0][0]).toBe("Content-Type");
|
|
|
|
expect(corsResponse.set.mock.calls.length).toBe(1);
|
|
expect(corsResponse.set.mock.calls[0][0]).toBe("Content-Type");
|
|
expect(corsResponse.set.mock.calls[0][1]).toBe("xml");
|
|
});
|
|
|
|
test("Sends correct data from response", async () => {
|
|
const responseData = "some data";
|
|
fetchResponseHeadersText.mockImplementation(() => responseData);
|
|
|
|
let sentData;
|
|
corsResponse.send = jest.fn((input) => {
|
|
sentData = input;
|
|
});
|
|
|
|
await cors(request, corsResponse);
|
|
|
|
expect(fetchResponseHeadersText.mock.calls.length).toBe(1);
|
|
expect(sentData).toBe(responseData);
|
|
});
|
|
|
|
test("Sends error data from response", async () => {
|
|
const error = new Error("error data");
|
|
fetchResponseHeadersText.mockImplementation(() => {
|
|
throw error;
|
|
});
|
|
|
|
let sentData;
|
|
corsResponse.send = jest.fn((input) => {
|
|
sentData = input;
|
|
});
|
|
|
|
await cors(request, corsResponse);
|
|
|
|
expect(fetchResponseHeadersText.mock.calls.length).toBe(1);
|
|
expect(sentData).toBe(error);
|
|
});
|
|
|
|
test("Fetches with user agent by default", async () => {
|
|
await cors(request, corsResponse);
|
|
|
|
expect(fetchMock.mock.calls.length).toBe(1);
|
|
expect(fetchMock.mock.calls[0][1]).toHaveProperty("headers");
|
|
expect(fetchMock.mock.calls[0][1].headers).toHaveProperty("User-Agent");
|
|
});
|
|
|
|
test("Fetches with specified headers", async () => {
|
|
const headersParam = "sendheaders=header1:value1,header2:value2";
|
|
const urlParam = "http://www.test.com/path?param1=value1";
|
|
request.url = `/cors?${headersParam}&url=${urlParam}`;
|
|
|
|
await cors(request, corsResponse);
|
|
|
|
expect(fetchMock.mock.calls.length).toBe(1);
|
|
expect(fetchMock.mock.calls[0][1]).toHaveProperty("headers");
|
|
expect(fetchMock.mock.calls[0][1].headers).toHaveProperty("header1", "value1");
|
|
expect(fetchMock.mock.calls[0][1].headers).toHaveProperty("header2", "value2");
|
|
});
|
|
|
|
test("Sends specified headers", async () => {
|
|
fetchResponseHeadersGet.mockImplementation((input) => input.replace("header", "value"));
|
|
|
|
const expectedheaders = "expectedheaders=header1,header2";
|
|
const urlParam = "http://www.test.com/path?param1=value1";
|
|
request.url = `/cors?${expectedheaders}&url=${urlParam}`;
|
|
|
|
await cors(request, corsResponse);
|
|
|
|
expect(fetchMock.mock.calls.length).toBe(1);
|
|
expect(fetchMock.mock.calls[0][1]).toHaveProperty("headers");
|
|
expect(corsResponse.set.mock.calls.length).toBe(3);
|
|
expect(corsResponse.set.mock.calls[0][0]).toBe("Content-Type");
|
|
expect(corsResponse.set.mock.calls[1][0]).toBe("header1");
|
|
expect(corsResponse.set.mock.calls[1][1]).toBe("value1");
|
|
expect(corsResponse.set.mock.calls[2][0]).toBe("header2");
|
|
expect(corsResponse.set.mock.calls[2][1]).toBe("value2");
|
|
});
|
|
});
|
|
});
|