Skip to content

Commit

Permalink
fix(core): Remove light-my-request dependency (#226)
Browse files Browse the repository at this point in the history
Removes the dependency `light-my-request` which was added in v2.2.0 to
circumvent an internal issue of msw
(mswjs/interceptors#443). This has since been
fixed and this should therefore no longer be necessary.
  • Loading branch information
alexfaxe authored Oct 8, 2024
1 parent a9ab64b commit a86d8c3
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 61 deletions.
5 changes: 5 additions & 0 deletions .changeset/shy-lobsters-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@labdigital/commercetools-mock": minor
---

Removes the light-my-request dependency
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"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",
Expand Down
22 changes: 0 additions & 22 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 19 additions & 23 deletions src/ctMock.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -193,12 +193,12 @@ export class CommercetoolsMock {
const url = new URL(request.url);
const headers = copyHeaders(request.headers);

const res = await inject(app)
const res = await supertest(app)
.post(url.pathname + "?" + url.searchParams.toString())
.body(body)
.headers(headers)
.end();
return new HttpResponse(res.body, {
.send(body)
.set(headers);

return new HttpResponse(JSON.stringify(res.body), {
status: res.statusCode,
headers: mapHeaderType(res.headers),
});
Expand All @@ -208,11 +208,10 @@ export class CommercetoolsMock {
const url = new URL(request.url);
const headers = copyHeaders(request.headers);

const res = await inject(app)
const res = await supertest(app)
.get(url.pathname + "?" + url.searchParams.toString())
.body(body)
.headers(headers)
.end();
.send(body)
.set(headers);

if (res.statusCode === 200) {
const parsedBody = JSON.parse(res.body);
Expand All @@ -239,12 +238,11 @@ export class CommercetoolsMock {
const url = new URL(request.url);
const headers = copyHeaders(request.headers);

const res = await inject(app)
const res = await supertest(app)
.get(url.pathname + "?" + url.searchParams.toString())
.body(body)
.headers(headers)
.end();
return new HttpResponse(res.body, {
.send(body)
.set(headers);
return new HttpResponse(JSON.stringify(res.body), {
status: res.statusCode,
headers: mapHeaderType(res.headers),
});
Expand All @@ -254,11 +252,10 @@ export class CommercetoolsMock {
const url = new URL(request.url);
const headers = copyHeaders(request.headers);

const res = await inject(app)
const res = await supertest(app)
.post(url.pathname + "?" + url.searchParams.toString())
.body(body)
.headers(headers)
.end();
.send(body)
.set(headers);
return new HttpResponse(res.body, {
status: res.statusCode,
headers: mapHeaderType(res.headers),
Expand All @@ -269,11 +266,10 @@ export class CommercetoolsMock {
const url = new URL(request.url);
const headers = copyHeaders(request.headers);

const res = await inject(app)
const res = await supertest(app)
.delete(url.pathname + "?" + url.searchParams.toString())
.body(body)
.headers(headers)
.end();
.send(body)
.set(headers);
return new HttpResponse(res.body, {
status: res.statusCode,
headers: mapHeaderType(res.headers),
Expand Down
29 changes: 14 additions & 15 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { type InvalidTokenError } from "@commercetools/platform-sdk";
import got from "got";
import { expect, test } from "vitest";
import { afterEach, expect, test } from "vitest";
import { CommercetoolsMock } from "./index";

let ctMock: CommercetoolsMock;

afterEach(() => {
ctMock.stop();
});

test("node:fetch client", async () => {
const ctMock = new CommercetoolsMock({
ctMock = new CommercetoolsMock({
enableAuthentication: true,
validateCredentials: true,
apiHost: "https://localhost",
Expand Down Expand Up @@ -44,11 +50,10 @@ test("node:fetch client", async () => {
limit: 20,
results: [],
});
ctMock.stop();
});

test("got client", async () => {
const ctMock = new CommercetoolsMock({
ctMock = new CommercetoolsMock({
enableAuthentication: true,
validateCredentials: true,
apiHost: "https://localhost",
Expand Down Expand Up @@ -86,11 +91,10 @@ test("got client", async () => {
limit: 20,
results: [],
});
ctMock.stop();
});

test("Options.validateCredentials: true (error)", async () => {
const ctMock = new CommercetoolsMock({
ctMock = new CommercetoolsMock({
enableAuthentication: true,
validateCredentials: true,
});
Expand All @@ -108,11 +112,10 @@ 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 () => {
const ctMock = new CommercetoolsMock({
ctMock = new CommercetoolsMock({
enableAuthentication: true,
validateCredentials: false,
});
Expand All @@ -135,7 +138,6 @@ test("Options.validateCredentials: false", async () => {
limit: 20,
results: [],
});
ctMock.stop();
});

test("Options.enableAuthentication: false", async () => {
Expand All @@ -159,11 +161,10 @@ test("Options.enableAuthentication: false", async () => {
limit: 20,
results: [],
});
ctMock.stop();
});

test("Options.apiHost: is overridden is set", async () => {
const ctMock = new CommercetoolsMock({
ctMock = new CommercetoolsMock({
enableAuthentication: false,
validateCredentials: false,
apiHost: "http://api.localhost",
Expand All @@ -181,11 +182,10 @@ test("Options.apiHost: is overridden is set", async () => {
limit: 20,
results: [],
});
ctMock.stop();
});

test("Options.authHost: is set", async () => {
const ctMock = new CommercetoolsMock({
ctMock = new CommercetoolsMock({
enableAuthentication: true,
validateCredentials: true,
authHost: "http://auth.localhost",
Expand All @@ -211,7 +211,7 @@ test("Options.authHost: is set", async () => {
});

test("apiHost mock proxy: querystring", async () => {
const ctMock = new CommercetoolsMock({
ctMock = new CommercetoolsMock({
enableAuthentication: false,
validateCredentials: false,
apiHost: "http://api.localhost",
Expand All @@ -234,5 +234,4 @@ test("apiHost mock proxy: querystring", async () => {
limit: 20,
results: [],
});
ctMock.stop();
});

0 comments on commit a86d8c3

Please sign in to comment.