Merge pull request #10315 from alexrybch/plugin-fetch-api

RequestInit instead of Request in PluginProtocolResolverFetchMiddleware
This commit is contained in:
Fredrik Adelöw
2022-03-21 17:33:42 +01:00
committed by GitHub
3 changed files with 34 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/core-app-api': patch
---
fixed empty body issue for POST requests using FetchAPI with 'plugin://' prefix
@@ -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, request);
return next(target, typeof input === 'string' ? init : input);
};
}
}