2022-10-30 18:14:02 +01:00
|
|
|
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
|
|
|
|
};
|
2023-09-09 21:12:31 +02:00
|
|
|
// eslint-disable-next-line
|
|
|
|
fetch = jest.fn();
|
|
|
|
fetch.mockImplementation(() => fetchResponse);
|
2022-10-30 18:14:02 +01:00
|
|
|
|
2023-09-09 21:12:31 +02:00
|
|
|
fetchMock = fetch;
|
2022-10-30 18:14:02 +01:00
|
|
|
|
|
|
|
corsResponse = {
|
|
|
|
set: jest.fn(() => {}),
|
|
|
|
send: jest.fn(() => {})
|
|
|
|
};
|
|
|
|
|
|
|
|
request = {
|
2023-12-25 08:17:11 +01:00
|
|
|
url: "/cors?url=www.test.com"
|
2022-10-30 18:14:02 +01:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2023-11-20 20:11:41 +01:00
|
|
|
it("Calls correct URL once", async () => {
|
2022-10-30 18:14:02 +01:00
|
|
|
const urlToCall = "http://www.test.com/path?param1=value1";
|
|
|
|
request.url = `/cors?url=${urlToCall}`;
|
|
|
|
|
|
|
|
await cors(request, corsResponse);
|
|
|
|
|
2023-11-20 20:11:41 +01:00
|
|
|
expect(fetchMock.mock.calls).toHaveLength(1);
|
2022-10-30 18:14:02 +01:00
|
|
|
expect(fetchMock.mock.calls[0][0]).toBe(urlToCall);
|
|
|
|
});
|
|
|
|
|
2023-11-20 20:11:41 +01:00
|
|
|
it("Forewards Content-Type if json", async () => {
|
2022-10-30 18:14:02 +01:00
|
|
|
fetchResponseHeadersGet.mockImplementation(() => "json");
|
|
|
|
|
|
|
|
await cors(request, corsResponse);
|
|
|
|
|
2023-11-20 20:11:41 +01:00
|
|
|
expect(fetchResponseHeadersGet.mock.calls).toHaveLength(1);
|
2022-10-30 18:14:02 +01:00
|
|
|
expect(fetchResponseHeadersGet.mock.calls[0][0]).toBe("Content-Type");
|
|
|
|
|
2023-11-20 20:11:41 +01:00
|
|
|
expect(corsResponse.set.mock.calls).toHaveLength(1);
|
2022-10-30 18:14:02 +01:00
|
|
|
expect(corsResponse.set.mock.calls[0][0]).toBe("Content-Type");
|
|
|
|
expect(corsResponse.set.mock.calls[0][1]).toBe("json");
|
|
|
|
});
|
|
|
|
|
2023-11-20 20:11:41 +01:00
|
|
|
it("Forewards Content-Type if xml", async () => {
|
2022-10-30 18:14:02 +01:00
|
|
|
fetchResponseHeadersGet.mockImplementation(() => "xml");
|
|
|
|
|
|
|
|
await cors(request, corsResponse);
|
|
|
|
|
2023-11-20 20:11:41 +01:00
|
|
|
expect(fetchResponseHeadersGet.mock.calls).toHaveLength(1);
|
2022-10-30 18:14:02 +01:00
|
|
|
expect(fetchResponseHeadersGet.mock.calls[0][0]).toBe("Content-Type");
|
|
|
|
|
2023-11-20 20:11:41 +01:00
|
|
|
expect(corsResponse.set.mock.calls).toHaveLength(1);
|
2022-10-30 18:14:02 +01:00
|
|
|
expect(corsResponse.set.mock.calls[0][0]).toBe("Content-Type");
|
|
|
|
expect(corsResponse.set.mock.calls[0][1]).toBe("xml");
|
|
|
|
});
|
|
|
|
|
2023-11-20 20:11:41 +01:00
|
|
|
it("Sends correct data from response", async () => {
|
2022-10-30 18:14:02 +01:00
|
|
|
const responseData = "some data";
|
|
|
|
fetchResponseHeadersText.mockImplementation(() => responseData);
|
|
|
|
|
|
|
|
let sentData;
|
|
|
|
corsResponse.send = jest.fn((input) => {
|
|
|
|
sentData = input;
|
|
|
|
});
|
|
|
|
|
|
|
|
await cors(request, corsResponse);
|
|
|
|
|
2023-11-20 20:11:41 +01:00
|
|
|
expect(fetchResponseHeadersText.mock.calls).toHaveLength(1);
|
2022-10-30 18:14:02 +01:00
|
|
|
expect(sentData).toBe(responseData);
|
|
|
|
});
|
|
|
|
|
2023-11-20 20:11:41 +01:00
|
|
|
it("Sends error data from response", async () => {
|
2022-10-30 18:14:02 +01:00
|
|
|
const error = new Error("error data");
|
|
|
|
fetchResponseHeadersText.mockImplementation(() => {
|
|
|
|
throw error;
|
|
|
|
});
|
|
|
|
|
|
|
|
let sentData;
|
|
|
|
corsResponse.send = jest.fn((input) => {
|
|
|
|
sentData = input;
|
|
|
|
});
|
|
|
|
|
|
|
|
await cors(request, corsResponse);
|
|
|
|
|
2023-11-20 20:11:41 +01:00
|
|
|
expect(fetchResponseHeadersText.mock.calls).toHaveLength(1);
|
2022-10-30 18:14:02 +01:00
|
|
|
expect(sentData).toBe(error);
|
|
|
|
});
|
|
|
|
|
2023-11-20 20:11:41 +01:00
|
|
|
it("Fetches with user agent by default", async () => {
|
2022-10-30 18:14:02 +01:00
|
|
|
await cors(request, corsResponse);
|
|
|
|
|
2023-11-20 20:11:41 +01:00
|
|
|
expect(fetchMock.mock.calls).toHaveLength(1);
|
2022-10-30 18:14:02 +01:00
|
|
|
expect(fetchMock.mock.calls[0][1]).toHaveProperty("headers");
|
|
|
|
expect(fetchMock.mock.calls[0][1].headers).toHaveProperty("User-Agent");
|
|
|
|
});
|
|
|
|
|
2023-11-20 20:11:41 +01:00
|
|
|
it("Fetches with specified headers", async () => {
|
2022-10-30 18:14:02 +01:00
|
|
|
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);
|
|
|
|
|
2023-11-20 20:11:41 +01:00
|
|
|
expect(fetchMock.mock.calls).toHaveLength(1);
|
2022-10-30 18:14:02 +01:00
|
|
|
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");
|
|
|
|
});
|
|
|
|
|
2023-11-20 20:11:41 +01:00
|
|
|
it("Sends specified headers", async () => {
|
2022-10-30 18:14:02 +01:00
|
|
|
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);
|
|
|
|
|
2023-11-20 20:11:41 +01:00
|
|
|
expect(fetchMock.mock.calls).toHaveLength(1);
|
2022-10-30 18:14:02 +01:00
|
|
|
expect(fetchMock.mock.calls[0][1]).toHaveProperty("headers");
|
2023-11-20 20:11:41 +01:00
|
|
|
expect(corsResponse.set.mock.calls).toHaveLength(3);
|
2022-10-30 18:14:02 +01:00
|
|
|
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");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|