Adds middleware test

This commit is contained in:
Lukasz Ostrowski 2022-08-08 17:10:42 +02:00
parent cba596c910
commit c15caef702
2 changed files with 33 additions and 2 deletions

32
src/middleware.test.ts Normal file
View file

@ -0,0 +1,32 @@
import { Handler, Request } from "retes";
import { Response } from "retes/types";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { withBaseURL } from "./middleware";
const getMockEmptyResponse = async () => ({} as Response);
describe("middleware.test.ts", () => {
describe("withBaseURL", () => {
let mockHandlerFn: Handler = vi.fn(getMockEmptyResponse);
beforeEach(() => {
mockHandlerFn = vi.fn();
});
it("Adds base URL from request header to context and calls handler", async () => {
const mockRequest = {
context: {},
headers: {
host: "my-saleor-env.saleor.cloud",
"x-forwarded-proto": "https",
},
} as unknown as Request;
await withBaseURL(mockHandlerFn)(mockRequest);
expect(mockRequest.context.baseURL).toBe("https://my-saleor-env.saleor.cloud");
expect(mockHandlerFn).toHaveBeenCalledOnce();
});
});
});

View file

@ -12,8 +12,7 @@ export const withBaseURL: Middleware = (handler) => async (request) => {
request.context.baseURL = `${protocol}://${host}`;
const response = await handler(request);
return response;
return handler(request);
};
export const withSaleorDomainPresent: Middleware = (handler) => async (request) => {