Skip to content

Commit

Permalink
fix: ignore passthrough requests for expectRequestsToMatchHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieugicquel committed Oct 14, 2024
1 parent bfee889 commit 817a54c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 12 deletions.
38 changes: 35 additions & 3 deletions src/expectRequestsToMatchHandlers.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { expect, it, afterEach } from "vitest";

import { createMockServer, installInterceptor, resetMockServers } from ".";
import { expect, it, afterEach, beforeAll, describe } from "vitest";

import {
createMockServer,
installInterceptor,
passthrough,
resetMockServers,
uninstallInterceptor,
} from ".";
import { expectRequestsToMatchHandlers } from "./expectRequestsToMatchHandlers";

installInterceptor();
Expand Down Expand Up @@ -201,3 +207,29 @@ it("logs a persistent handler mismatch for an unhandled request", async () => {
"
`);
});

describe("passthrough", () => {
beforeAll(() => {
uninstallInterceptor();
installInterceptor({ onUnhandled: passthrough });

return () => {
uninstallInterceptor();
installInterceptor();
};
});

it("doesn't throw because of passthrough request", async () => {
const localMockServer = createMockServer("http://0.0.0.0");

// control
localMockServer.get("/test", body);
const response = await fetch("http://0.0.0.0/test");
expect(response.ok).toEqual(true);

await expect(() => fetch("http://0.0.0.0/not-mocked")).rejects.toThrowError();

// real expectation
expect(getThrownMessage()).toBeUndefined();
});
});
17 changes: 14 additions & 3 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from "axios";
import { expect, it, describe, afterEach, vi, beforeAll } from "vitest";
import { expect, it, describe, afterEach, vi, beforeEach } from "vitest";

import {
createMockServer,
Expand Down Expand Up @@ -548,9 +548,8 @@ describe("server-level config", () => {
});

describe("interceptor-level config", () => {
beforeAll(() => {
beforeEach(() => {
uninstallInterceptor();
installInterceptor({ onUnhandled: passthrough });

return () => {
uninstallInterceptor();
Expand All @@ -559,6 +558,8 @@ describe("interceptor-level config", () => {
});

it("lets a request passthrough", async () => {
installInterceptor({ onUnhandled: passthrough });

const localMockServer = createMockServer("http://0.0.0.0");

// control
Expand All @@ -569,4 +570,14 @@ describe("interceptor-level config", () => {
// real expectation
await expect(() => fetch("http://0.0.0.0/not-mocked")).rejects.toThrowError();
});

it("uses custom onUnhandled", async () => {
installInterceptor({ onUnhandled: () => new Response("custom onUnhandled", { status: 500 }) });

const response = await fetch("https://test.com/test");

expect(response.ok).toEqual(false);
expect(response.status).toEqual(500);
expect(await response.text()).toEqual("custom onUnhandled");
});
});
19 changes: 13 additions & 6 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export const createServer = (setupInterceptor: SetupInterceptor, config?: Interc
const HandledRequests = new Map<Handler, Request>();
const MatchingLog = new Set<MatchingLogEntry>();

const handleUnhandled =
config?.onUnhandled ?? (() => new Response("No matching handler", { status: 404 }));

const dispose = setupInterceptor((request) => {
const requestLog = new Set<MatchingLogEntry>();
const explain = (handler: Handler) => (message: string) => {
Expand All @@ -32,14 +35,18 @@ export const createServer = (setupInterceptor: SetupInterceptor, config?: Interc
}
}

for (const logEntry of requestLog) MatchingLog.add(logEntry);
UnhandledRequests.add(request);
const unhandledResponse = handleUnhandled(request);

if (unhandledResponse) {
// This request should have been handled
for (const logEntry of requestLog) MatchingLog.add(logEntry);
UnhandledRequests.add(request);

const onUnhandled =
config?.onUnhandled ??
(() => new Response(JSON.stringify({ message: "No matching handler" }), { status: 404 }));
return unhandledResponse;
}

return onUnhandled(request);
// let the request passthrough
return;
});

return {
Expand Down

0 comments on commit 817a54c

Please sign in to comment.