Merge pull request #22654 from knksmith57/knksmith57/support-token-fetchurlreader

feat(backend-common/FetchUrlReader): support token authentication
This commit is contained in:
Patrik Oldsberg
2024-02-06 10:57:01 +01:00
committed by GitHub
3 changed files with 27 additions and 0 deletions
+5
View File
@@ -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.