feat(backend-common/FetchUrlReader): support token authentication
Builds on interface enhancements introduced by #22521 to support token-based authentication in FetchUrlReader#readUrl(). Signed-off-by: Kyle Smith <knksmith57@gmail.com>
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@backstage/backend-common': patch
|
||||
---
|
||||
|
||||
`FetchUrlReader#readUrl()` now supports passing an optional `token` to authenticate requests.
|
||||
@@ -223,6 +223,27 @@ describe('FetchUrlReader', () => {
|
||||
).rejects.toThrow(NotModifiedError);
|
||||
});
|
||||
|
||||
it('should send Authorization header if token is provided', async () => {
|
||||
expect.assertions(1);
|
||||
|
||||
worker.use(
|
||||
rest.get(
|
||||
'https://backstage.io/requires-authentication',
|
||||
(req, res, ctx) => {
|
||||
expect(req.headers.get('authorization')).toBe('Bearer mytoken');
|
||||
return res(ctx.status(200));
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
await fetchUrlReader.readUrl(
|
||||
'https://backstage.io/requires-authentication',
|
||||
{
|
||||
token: 'mytoken',
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
it('should return etag from the response', async () => {
|
||||
const response = await fetchUrlReader.readUrl(
|
||||
'https://backstage.io/some-resource',
|
||||
|
||||
@@ -131,6 +131,7 @@ export class FetchUrlReader implements UrlReader {
|
||||
...(options?.lastModifiedAfter && {
|
||||
'If-Modified-Since': options.lastModifiedAfter.toUTCString(),
|
||||
}),
|
||||
...(options?.token && { Authorization: `Bearer ${options.token}` }),
|
||||
},
|
||||
// TODO(freben): The signal cast is there because pre-3.x versions of
|
||||
// node-fetch have a very slightly deviating AbortSignal type signature.
|
||||
|
||||
Reference in New Issue
Block a user