diff --git a/.changeset/real-onions-sparkle.md b/.changeset/real-onions-sparkle.md new file mode 100644 index 00000000..b1dfcdc7 --- /dev/null +++ b/.changeset/real-onions-sparkle.md @@ -0,0 +1,5 @@ +--- +"@labdigital/commercetools-mock": patch +--- + +Reverts breaking change diff --git a/package.json b/package.json index e2a73b87..0530245f 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "body-parser": "^1.20.2", "deep-equal": "^2.2.3", "express": "^4.19.2", + "light-my-request": "^5.11.1", "lodash.isequal": "^4.5.0", "morgan": "^1.10.0", "msw": "^2.2.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 52588ecf..8b6bc02a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -20,6 +20,9 @@ importers: express: specifier: ^4.19.2 version: 4.19.2 + light-my-request: + specifier: ^5.11.1 + version: 5.11.1 lodash.isequal: specifier: ^4.5.0 version: 4.5.0 @@ -2066,6 +2069,9 @@ packages: resolution: {integrity: sha512-+bT2uH4E5LGE7h/n3evcS/sQlJXCpIp6ym8OWJ5eV6+67Dsql/LaaT7qJBAt2rzfoa/5QBGBhxDix1dMt2kQKQ==} engines: {node: '>= 0.8.0'} + light-my-request@5.11.1: + resolution: {integrity: sha512-KXAh2m6VRlkWCk2KfmHE7tLBXKh30JE0tXUJY4dNxje4oLmPKUqlUfImiEQZLphx+Z9KTQcVv4DjGnJxkVOIbA==} + lilconfig@3.1.1: resolution: {integrity: sha512-O18pf7nyvHTckunPWCV1XUNXU1piu01y2b7ATJ0ppkUkk8ocqVWBrYjJBCwHDjD/ZWcfyrA0P4gKhzWGi5EINQ==} engines: {node: '>=14'} @@ -2533,6 +2539,9 @@ packages: resolution: {integrity: sha512-Pdlw/oPxN+aXdmM9R00JVC9WVFoCLTKJvDVLgmJ+qAffBMxsV85l/Lu7sNx4zSzPyoL2euImuEwHhOXdEgNFZQ==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} + process-warning@2.3.2: + resolution: {integrity: sha512-n9wh8tvBe5sFmsqlg+XQhaQLumwpqoAUruLwjCopgTmUBjJ/fjtBsJzKleCaIGBOMXYEhp1YfKl4d7rJ5ZKJGA==} + proxy-addr@2.0.7: resolution: {integrity: sha512-llQsMLSUDUPT44jdrU/O37qlnifitDP+ZwrmmZcoSKyLKvtZxpyV0n2/bD/N4tBAAZ/gJEdZU7KMraoK1+XYAg==} engines: {node: '>= 0.10'} @@ -2684,6 +2693,9 @@ packages: set-blocking@2.0.0: resolution: {integrity: sha512-KiKBS8AnWGEyLzofFfmvKwpdPzqiy16LvQfK3yv/fVH7Bj13/wl3JSR1J+rfgRE9q7xUJK4qvgS8raSOeLUehw==} + set-cookie-parser@2.6.0: + resolution: {integrity: sha512-RVnVQxTXuerk653XfuliOxBP81Sf0+qfQE73LIYKcyMYHG94AuH0kgrQpRDuTZnSmjpysHmzxJXKNfa6PjFhyQ==} + set-function-length@1.2.1: resolution: {integrity: sha512-j4t6ccc+VsKwYHso+kElc5neZpjtq9EnRICFZtWyBsLojhmeF/ZBd/elqm22WJh/BziDe/SBiOeAt0m2mfLD0g==} engines: {node: '>= 0.4'} @@ -5403,6 +5415,12 @@ snapshots: prelude-ls: 1.2.1 type-check: 0.4.0 + light-my-request@5.11.1: + dependencies: + cookie: 0.6.0 + process-warning: 2.3.2 + set-cookie-parser: 2.6.0 + lilconfig@3.1.1: {} lines-and-columns@1.2.4: {} @@ -5813,6 +5831,8 @@ snapshots: ansi-styles: 5.2.0 react-is: 18.2.0 + process-warning@2.3.2: {} + proxy-addr@2.0.7: dependencies: forwarded: 0.2.0 @@ -5990,6 +6010,8 @@ snapshots: set-blocking@2.0.0: {} + set-cookie-parser@2.6.0: {} + set-function-length@1.2.1: dependencies: define-data-property: 1.1.4 diff --git a/src/ctMock.ts b/src/ctMock.ts index a0f63063..f4d0fd84 100644 --- a/src/ctMock.ts +++ b/src/ctMock.ts @@ -1,8 +1,8 @@ import express, { NextFunction, Request, Response } from "express"; +import inject from "light-my-request"; import morgan from "morgan"; import { http, HttpResponse } from "msw"; import { setupServer, SetupServer, SetupServerApi } from "msw/node"; -import supertest from "supertest"; import { DEFAULT_API_HOSTNAME, DEFAULT_AUTH_HOSTNAME } from "./constants"; import { CommercetoolsError } from "./exceptions"; import { copyHeaders } from "./lib/proxy"; @@ -193,12 +193,12 @@ export class CommercetoolsMock { const url = new URL(request.url); const headers = copyHeaders(request.headers); - const res = await supertest(app) + const res = await inject(app) .post(url.pathname + "?" + url.searchParams.toString()) - .send(body) - .set(headers); - - return new HttpResponse(JSON.stringify(res.body), { + .body(body) + .headers(headers) + .end(); + return new HttpResponse(res.body, { status: res.statusCode, headers: mapHeaderType(res.headers), }); @@ -208,10 +208,11 @@ export class CommercetoolsMock { const url = new URL(request.url); const headers = copyHeaders(request.headers); - const res = await supertest(app) + const res = await inject(app) .get(url.pathname + "?" + url.searchParams.toString()) - .send(body) - .set(headers); + .body(body) + .headers(headers) + .end(); if (res.statusCode === 200) { const parsedBody = JSON.parse(res.body); @@ -238,11 +239,12 @@ export class CommercetoolsMock { const url = new URL(request.url); const headers = copyHeaders(request.headers); - const res = await supertest(app) + const res = await inject(app) .get(url.pathname + "?" + url.searchParams.toString()) - .send(body) - .set(headers); - return new HttpResponse(JSON.stringify(res.body), { + .body(body) + .headers(headers) + .end(); + return new HttpResponse(res.body, { status: res.statusCode, headers: mapHeaderType(res.headers), }); @@ -252,10 +254,11 @@ export class CommercetoolsMock { const url = new URL(request.url); const headers = copyHeaders(request.headers); - const res = await supertest(app) + const res = await inject(app) .post(url.pathname + "?" + url.searchParams.toString()) - .send(body) - .set(headers); + .body(body) + .headers(headers) + .end(); return new HttpResponse(res.body, { status: res.statusCode, headers: mapHeaderType(res.headers), @@ -266,10 +269,11 @@ export class CommercetoolsMock { const url = new URL(request.url); const headers = copyHeaders(request.headers); - const res = await supertest(app) + const res = await inject(app) .delete(url.pathname + "?" + url.searchParams.toString()) - .send(body) - .set(headers); + .body(body) + .headers(headers) + .end(); return new HttpResponse(res.body, { status: res.statusCode, headers: mapHeaderType(res.headers), diff --git a/src/index.test.ts b/src/index.test.ts index 0fcc3004..ecad4c3c 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -1,16 +1,10 @@ import { type InvalidTokenError } from "@commercetools/platform-sdk"; import got from "got"; -import { afterEach, expect, test } from "vitest"; +import { expect, test } from "vitest"; import { CommercetoolsMock } from "./index"; -let ctMock: CommercetoolsMock; - -afterEach(() => { - ctMock.stop(); -}); - test("node:fetch client", async () => { - ctMock = new CommercetoolsMock({ + const ctMock = new CommercetoolsMock({ enableAuthentication: true, validateCredentials: true, apiHost: "https://localhost", @@ -50,10 +44,11 @@ test("node:fetch client", async () => { limit: 20, results: [], }); + ctMock.stop(); }); test("got client", async () => { - ctMock = new CommercetoolsMock({ + const ctMock = new CommercetoolsMock({ enableAuthentication: true, validateCredentials: true, apiHost: "https://localhost", @@ -91,10 +86,11 @@ test("got client", async () => { limit: 20, results: [], }); + ctMock.stop(); }); test("Options.validateCredentials: true (error)", async () => { - ctMock = new CommercetoolsMock({ + const ctMock = new CommercetoolsMock({ enableAuthentication: true, validateCredentials: true, }); @@ -112,10 +108,11 @@ test("Options.validateCredentials: true (error)", async () => { ); expect(response.statusCode).toBe(401); expect(response.body.message).toBe("invalid_token"); + ctMock.stop(); }); test("Options.validateCredentials: false", async () => { - ctMock = new CommercetoolsMock({ + const ctMock = new CommercetoolsMock({ enableAuthentication: true, validateCredentials: false, }); @@ -138,6 +135,7 @@ test("Options.validateCredentials: false", async () => { limit: 20, results: [], }); + ctMock.stop(); }); test("Options.enableAuthentication: false", async () => { @@ -161,10 +159,11 @@ test("Options.enableAuthentication: false", async () => { limit: 20, results: [], }); + ctMock.stop(); }); test("Options.apiHost: is overridden is set", async () => { - ctMock = new CommercetoolsMock({ + const ctMock = new CommercetoolsMock({ enableAuthentication: false, validateCredentials: false, apiHost: "http://api.localhost", @@ -182,10 +181,11 @@ test("Options.apiHost: is overridden is set", async () => { limit: 20, results: [], }); + ctMock.stop(); }); test("Options.authHost: is set", async () => { - ctMock = new CommercetoolsMock({ + const ctMock = new CommercetoolsMock({ enableAuthentication: true, validateCredentials: true, authHost: "http://auth.localhost", @@ -211,7 +211,7 @@ test("Options.authHost: is set", async () => { }); test("apiHost mock proxy: querystring", async () => { - ctMock = new CommercetoolsMock({ + const ctMock = new CommercetoolsMock({ enableAuthentication: false, validateCredentials: false, apiHost: "http://api.localhost", @@ -234,4 +234,5 @@ test("apiHost mock proxy: querystring", async () => { limit: 20, results: [], }); + ctMock.stop(); });