Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "fix(core): remove light-my-request dependency" #228

Merged
merged 2 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/real-onions-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@labdigital/commercetools-mock": patch
---

Reverts breaking change
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
22 changes: 22 additions & 0 deletions pnpm-lock.yaml

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

42 changes: 23 additions & 19 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 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),
});
Expand All @@ -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);
Expand All @@ -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),
});
Expand All @@ -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),
Expand All @@ -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),
Expand Down
29 changes: 15 additions & 14 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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,
});
Expand All @@ -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,
});
Expand All @@ -138,6 +135,7 @@ test("Options.validateCredentials: false", async () => {
limit: 20,
results: [],
});
ctMock.stop();
});

test("Options.enableAuthentication: false", async () => {
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -234,4 +234,5 @@ test("apiHost mock proxy: querystring", async () => {
limit: 20,
results: [],
});
ctMock.stop();
});