Skip to content
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
20 changes: 12 additions & 8 deletions src/rules/requests/request-step-impls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1328,14 +1328,18 @@ export class PassThroughStepImpl extends PassThroughStep {
let beforeRequest: ((req: CompletedRequest) => MaybePromise<CallbackRequestResult | void>) | undefined;
if (data.hasBeforeRequestCallback) {
beforeRequest = async (req: CompletedRequest) => {
const result = withDeserializedCallbackBuffers<CallbackRequestResult>(
await channel.request<
BeforePassthroughRequestRequest,
WithSerializedCallbackBuffers<CallbackRequestResult>
>('beforeRequest', {
args: [await withSerializedBodyReader(req, bodySerializer)]
})
);
const callbackResult = await channel.request<
BeforePassthroughRequestRequest,
WithSerializedCallbackBuffers<CallbackRequestResult> | 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<CallbackRequestResult>(callbackResult);

if (result.response && typeof result.response !== 'string') {
result.response = withDeserializedCallbackBuffers(
Expand Down
19 changes: 19 additions & 0 deletions test/integration/remote-client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => ({
Expand Down
Loading