Skip to content

Commit

Permalink
Remove axios 1.11 (#7730)
Browse files Browse the repository at this point in the history
* Move from axios to fetch

* Allow weak etags

* Force exit after all tests complete

* Fix missing semicolons
  • Loading branch information
ylavoie authored Nov 24, 2023
1 parent dd99acf commit d0ca3f5
Show file tree
Hide file tree
Showing 19 changed files with 826 additions and 643 deletions.
2 changes: 1 addition & 1 deletion UI/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ module.exports = {

// Force Jest to exit after all tests have completed running.
// This is useful when resources set up by test code cannot be adequately cleaned up.
forceExit: false,
forceExit: true,

// Force coverage collection from ignored files using an array of glob patterns
forceCoverageMatch: [],
Expand Down
2 changes: 1 addition & 1 deletion UI/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
"@redocly/cli": "^1.0.2",
"@vue/test-utils": "2.4.1",
"@vue/vue3-jest": "29.2.6",
"axios": "1.6.1",
"babel-jest": "29.7.0",
"babel-loader": "9.1.3",
"browserslist": "4.22.1",
Expand All @@ -67,6 +66,7 @@
"clean-webpack-plugin": "4.0.0",
"compression-webpack-plugin": "10.0.0",
"copy-webpack-plugin": "11.0.0",
"core-js": "3.33.3",
"coveralls-next": "4.2.0",
"css-loader": "6.8.1",
"css-minimizer-webpack-plugin": "5.0.1",
Expand Down
1 change: 1 addition & 0 deletions UI/tests/common/jest-setup.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-console */

import { jest, beforeAll, afterAll, beforeEach, afterEach } from "@jest/globals";
import 'core-js';
import { setGlobalOrigin } from 'undici';

import "./mocks/lsmb_elements";
Expand Down
2 changes: 2 additions & 0 deletions UI/tests/common/jest.polyfills.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* global globalThis */

const { TextEncoder, TextDecoder } = require('node:util')
const { performance } = require('node:perf_hooks')

Object.defineProperties(globalThis, {
TextDecoder: { value: TextDecoder },
TextEncoder: { value: TextEncoder },
performance: { value: performance }
})

const { Blob } = require('node:buffer')
Expand Down
145 changes: 86 additions & 59 deletions UI/tests/specs/openapi/API-contacts-business_types.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

// Import test packages
import axios from "axios";
import jestOpenAPI from "jest-openapi";
import { StatusCodes } from "http-status-codes";
import { create_database, drop_database } from "./database";
Expand Down Expand Up @@ -35,7 +34,6 @@ let headers = {};

// For all tests
beforeAll(() => {
axios.defaults.adapter = 'http';
create_database(username, password, company);

// Establish API mocking before all tests.
Expand All @@ -48,33 +46,49 @@ afterAll(() => {
drop_database(company);
});

const emulateAxiosResponse = async(res) => {
return {
data: await res.json(),
status: res.status,
statusText: res.statusText,
headers: res.headers,
request: {
path: res.url,
method: 'GET'
}
};
};

// Log in before each test
beforeEach(async () => {
let r = await axios.post(
let r = await fetch(
serverUrl + "/login.pl?action=authenticate&company=" + encodeURI(company),
{
company: company,
password: password,
login: username
},
{
method: "POST",
body: JSON.stringify({
company: company,
password: password,
login: username
}),
headers: {
"X-Requested-With": "XMLHttpRequest",
"Content-Type": "application/json"
}
}
);
if (r.status === StatusCodes.OK) {
const data = await r.json();
headers = {
cookie: r.headers["set-cookie"],
referer: serverUrl + "/" + r.data.target,
cookie: r.headers.get("set-cookie"),
referer: serverUrl + "/" + data.target,
authorization: "Basic " + btoa(username + ":" + password)
};
}
});

// Log out after each test
afterEach(async () => {
let r = await axios.get(serverUrl + "/login.pl?action=logout&target=_top");
let r = await fetch(serverUrl + "/login.pl?action=logout&target=_top");
if (r.status === StatusCodes.OK) {
headers = {};
}
Expand All @@ -84,7 +98,7 @@ afterEach(async () => {
describe("Retrieving all Business Types", () => {
it("GET /contacts/business-types should satisfy OpenAPI spec", async () => {
// Get an HTTP response from your serverUrl
let res = await axios.get(
let res = await fetch(
serverUrl + "/" + api + "/contacts/business-types",
{
headers: headers
Expand All @@ -93,56 +107,53 @@ describe("Retrieving all Business Types", () => {
expect(res.status).toEqual(StatusCodes.OK);

// Assert that the HTTP response satisfies the OpenAPI spec
res = await emulateAxiosResponse(res);
expect(res).toSatisfyApiSpec();
});
});

describe("Retrieving all Business Types with old syntax should fail", () => {
it("GET /contacts/business-types/ should fail", async () => {
await expect(
axios.get(serverUrl + "/" + api + "/contacts/business-types/", {
headers: headers
})
).rejects.toThrow(
"Request failed with status code " + StatusCodes.BAD_REQUEST
);
const res = await fetch(serverUrl + "/" + api + "/contacts/business-types/", {
headers: headers
});
expect(res.status).toEqual(StatusCodes.BAD_REQUEST);
});
});

describe("Retrieve non-existant Business Type", () => {
it("GET /contacts/business-types/1 should not retrieve our Business Types", async () => {
await expect(
axios.get(serverUrl + "/" + api + "/contacts/business-types/1", {
headers: headers
})
).rejects.toThrow(
"Request failed with status code " + StatusCodes.NOT_FOUND
);
const res = await fetch(serverUrl + "/" + api + "/contacts/business-types/1", {
headers: headers
});
expect(res.status).toEqual(StatusCodes.NOT_FOUND);
});
});

describe("Adding the IT Business Types", () => {
it("POST /contacts/business-types/1 should allow adding Business Type", async () => {
let res = await axios.post(
let res = await fetch(
serverUrl + "/" + api + "/contacts/business-types",
{
description: "Big customer",
discount: 0.05
},
{
headers: headers
method: "POST",
body: JSON.stringify({
description: "Big customer",
discount: "0.05"
}),
headers: { ...headers, "Content-Type": "application/json" }
}
);
expect(res.status).toEqual(StatusCodes.CREATED);

// Assert that the HTTP response satisfies the OpenAPI spec
res = await emulateAxiosResponse(res);
expect(res.data).toSatisfySchemaInApiSpec("NewBusinessType");
});
});

describe("Validate against the default Business Type", () => {
it("GET /contacts/business-types/1 should validate the new Business Type", async () => {
let res = await axios.get(serverUrl + "/" + api + "/contacts/business-types/1", {
let res = await fetch(serverUrl + "/" + api + "/contacts/business-types/1", {
headers: headers
});
expect(res.status).toEqual(StatusCodes.OK);
Expand All @@ -151,34 +162,42 @@ describe("Validate against the default Business Type", () => {
const businessTypeExample = API_yaml.components.examples.validBusinessType.value;

// Assert that the response matches the example in the spec
res = await emulateAxiosResponse(res);
expect(res.data).toEqual(businessTypeExample);
});
});

describe("Modifying the new Business Type", () => {
it("PUT /contacts/business-types/1 should allow updating Business Type", async () => {
let res = await axios.get(
let res = await fetch(
serverUrl + "/" + api + "/contacts/business-types/1",
{
headers: headers
}
);
expect(res.status).toEqual(StatusCodes.OK);
expect(res.headers.etag).toBeDefined();
res = await axios.put(
const etag = res.headers.get("etag");
expect(etag).toBeDefined();
res = await fetch(
serverUrl + "/" + api + "/contacts/business-types/1",
{
id: 1,
description: "Bigger customer",
discount: "0.1"
},
{
headers: { ...headers, "If-Match": res.headers.etag }
method: "PUT",
body: JSON.stringify({
id: 1,
description: "Bigger customer",
discount: "0.1"
}),
headers: {
...headers,
"Content-Type": "application/json",
"If-Match": etag
}
}
);
expect(res.status).toEqual(StatusCodes.OK);

// Assert that the HTTP response satisfies the OpenAPI spec
res = await emulateAxiosResponse(res);
expect(res).toSatisfyApiSpec();

// Assert that the HTTP response satisfies the OpenAPI spec
Expand All @@ -190,24 +209,32 @@ describe("Modifying the new Business Type", () => {
* Not implemented yet
describe("Updating the new Business Type", () => {
it("PATCH /contacts/business-types/1 should allow updating IT Business Types", async () => {
let res = await axios.get(serverUrl + "/" + api + "/contacts/business-types/1", {
let res = await fetch(serverUrl + "/" + api + "/contacts/business-types/1", {
headers: headers
});
expect(res.status).toEqual(StatusCodes.OK);
expect(res.headers.etag).toBeDefined();
res = await axios.patch(
const etag = res.headers.get("etag");
expect(etag).toBeDefined();
res = await fetch(
serverUrl + "/" + api + "/contacts/business-types/1",
{
id: "1",
description: "Navaho"
},
{
headers: { ...headers, "If-Match": res.headers.etag }
method: "PATCH",
body: JSON.stringify({
id: 1,
description: "Bigger customer",
discount: "0.1"
}),
headers: {
...headers,
"Content-Type": "application/json",
"If-Match": etag
},
}
);
expect(res.status).toEqual(StatusCodes.OK);
// Assert that the HTTP response satisfies the OpenAPI spec
res = await emulateAxiosResponse(res);
expect(res).toSatisfyApiSpec();
// Assert that the HTTP response satisfies the OpenAPI spec
Expand All @@ -218,21 +245,21 @@ describe("Updating the new Business Type", () => {

describe("Not removing the new IT Business Types", () => {
it("DELETE /contacts/business-types/1 should not allow deleting Business Type", async () => {
let res = await axios.get(
let res = await fetch(
serverUrl + "/" + api + "/contacts/business-types/1",
{
headers: headers
}
);
expect(res.status).toEqual(StatusCodes.OK);
expect(res.headers.etag).toBeDefined();

await expect(
axios.delete(serverUrl + "/" + api + "/contacts/business-types/1", {
headers: { ...headers, "If-Match": res.headers.etag }
})
).rejects.toThrow(
"Request failed with status code " + StatusCodes.FORBIDDEN
const etag = res.headers.get("etag");
expect(etag).toBeDefined();
res = await fetch(
serverUrl + "/" + api + "/contacts/business-types/1", {
method: "DELETE",
headers: { ...headers, "If-Match": etag }
}
);
expect(res.status).toEqual(StatusCodes.FORBIDDEN);
});
});
Loading

0 comments on commit d0ca3f5

Please sign in to comment.