diff --git a/src/rules/requests/request-step-impls.ts b/src/rules/requests/request-step-impls.ts index 4ac122875..54af70222 100644 --- a/src/rules/requests/request-step-impls.ts +++ b/src/rules/requests/request-step-impls.ts @@ -1328,14 +1328,18 @@ export class PassThroughStepImpl extends PassThroughStep { let beforeRequest: ((req: CompletedRequest) => MaybePromise) | undefined; if (data.hasBeforeRequestCallback) { beforeRequest = async (req: CompletedRequest) => { - const result = withDeserializedCallbackBuffers( - await channel.request< - BeforePassthroughRequestRequest, - WithSerializedCallbackBuffers - >('beforeRequest', { - args: [await withSerializedBodyReader(req, bodySerializer)] - }) - ); + const callbackResult = await channel.request< + BeforePassthroughRequestRequest, + WithSerializedCallbackBuffers | undefined + >('beforeRequest', { + args: [await withSerializedBodyReader(req, bodySerializer)] + }); + + // Inspection-only callbacks return nothing at all, and the client + // sends that through as undefined, so there's nothing to deserialize: + if (!callbackResult) return callbackResult; + + const result = withDeserializedCallbackBuffers(callbackResult); if (result.response && typeof result.response !== 'string') { result.response = withDeserializedCallbackBuffers( diff --git a/test/integration/remote-client.spec.ts b/test/integration/remote-client.spec.ts index 2c0534ca0..6c2672733 100644 --- a/test/integration/remote-client.spec.ts +++ b/test/integration/remote-client.spec.ts @@ -154,6 +154,25 @@ nodeOnly(() => { expect(await targetEndpoint.getSeenRequests()).to.deep.equal([]); }); + it("should successfully pass through requests when beforeRequest returns nothing", async () => { + await targetServer.forGet('/').thenReply(200, 'target response'); + + let seenMethod: string | undefined; + await remoteServer.forGet(targetServer.url).thenPassThrough({ + beforeRequest: (req) => { + // Inspection-only callbacks return nothing at all: + seenMethod = req.method; + } + }); + + const response = await request.get(targetServer.url, { + proxy: remoteServer.url + }); + + expect(seenMethod).to.equal('GET'); + expect(response).to.equal('target response'); + }); + it("should successfully replace request & response bodies", async () => { // Echo the incoming request await targetServer.forAnyRequest().thenCallback(async (req) => ({