@berlingske-media/bm.node-module.gateway_jwt
Version:
AuthGateway JWT verification library based on public JWKS endpoint
83 lines • 3.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const request_1 = require("./request");
const node_http_1 = tslib_1.__importStar(require("node:http"));
const keys_1 = require("../keys");
const express_1 = tslib_1.__importDefault(require("express"));
describe('Request wrapper tests', () => {
const jwksHost = 'http://localhost:5555';
const uri = `${jwksHost}/.well-known/jwks.json`;
let server;
let app;
beforeAll(() => {
server = (0, express_1.default)();
server.get('/.well-known/jwks.json', function (req, res) {
return res.status(200).json({ keys: keys_1.keys });
});
app = server.listen({ port: 5555 });
});
afterAll(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
yield new Promise((resolve) => app.close(resolve));
}));
it('should make a successful request to specified uri', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const data = yield (0, request_1.request)({ uri });
expect(data).toEqual({ keys: keys_1.keys });
}));
it('should handle errors', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
jest.spyOn(node_http_1.default, 'request').mockImplementationOnce(() => {
throw new Error('Internal Server Error');
});
try {
yield (0, request_1.request)({ uri });
}
catch (err) {
expect(err.message).toEqual('Internal Server Error');
}
}));
it('should set a timeout when specified', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const timeout = 999999;
const spy = jest.spyOn(node_http_1.default, 'request');
yield (0, request_1.request)({ uri, timeout });
expect(spy).toHaveBeenCalledWith(expect.objectContaining({ timeout }), expect.any(Function));
}));
it('should destroy the request when reaches the timeout', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const timeout = 5;
const latency = timeout + 5;
jest.spyOn(node_http_1.default, 'request').mockImplementationOnce((options, callback) => {
const mockRequest = new node_http_1.ClientRequest(options);
setTimeout(() => {
const error = new Error('Mocked ECONNRESET error');
error.code = 'ECONNRESET';
mockRequest.emit('error', error);
}, latency);
return mockRequest;
});
try {
yield (0, request_1.request)({ uri, timeout });
}
catch (err) {
expect(err.code).toEqual('ECONNRESET');
}
}));
it('should set modify headers when specified in options', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const headers = { 'test': '123' };
const spy = jest.spyOn(node_http_1.default, 'request');
yield (0, request_1.request)({ uri, headers });
expect(spy).toHaveBeenCalledWith(expect.objectContaining({ headers }), expect.any(Function));
}));
it('should set an agent when specified', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const agent = new node_http_1.default.Agent();
const spy = jest.spyOn(node_http_1.default, 'request');
yield (0, request_1.request)({ uri, agent });
expect(spy).toHaveBeenCalledWith(expect.objectContaining({ agent }), expect.any(Function));
}));
describe('when fetcher is specified', () => {
it('should use the specified fetcher to make the request', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const customFetcher = jest.fn().mockResolvedValue(keys_1.keys);
yield (0, request_1.request)({ uri, fetcher: customFetcher });
expect(customFetcher).toHaveBeenCalledWith(uri);
}));
});
});
//# sourceMappingURL=request.test.js.map