works with the Request input as well

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2022-03-21 15:50:26 +01:00
parent a7bb762dab
commit 86c32c4902
2 changed files with 29 additions and 1 deletions
@@ -90,4 +90,32 @@ describe('PluginProtocolResolverFetchMiddleware', () => {
expect(resolve).toHaveBeenLastCalledWith(host);
},
);
it('properly supports transferring request bodies too', async () => {
const resolve = jest.fn();
const discoveryApi = { getBaseUrl: resolve } as unknown as DiscoveryApi;
const middleware = new PluginProtocolResolverFetchMiddleware(discoveryApi);
const inner = jest.fn();
const outer = middleware.apply(inner);
resolve.mockResolvedValue('https://elsewhere.com');
await outer('plugin://a', {
method: 'POST',
body: '123',
});
expect(inner.mock.calls[0][0]).toBe('https://elsewhere.com');
expect(inner.mock.calls[0][1].body).toBe('123');
await outer(
new Request('plugin://a', {
method: 'POST',
body: '123',
}),
);
expect(inner.mock.calls[1][0]).toBe('https://elsewhere.com');
expect(inner.mock.calls[1][1].body).toEqual(Buffer.from('123', 'utf8'));
});
});
@@ -55,7 +55,7 @@ export class PluginProtocolResolverFetchMiddleware implements FetchMiddleware {
}
const target = `${join(base, pathname)}${search}${hash}`;
return next(target, init);
return next(target, typeof input === 'string' ? init : input);
};
}
}