@substrate/api-sidecar
Version:
REST service that makes it easy to interact with blockchain nodes built using Substrate's FRAME framework.
116 lines • 4.27 kB
JavaScript
;
// Copyright 2017-2025 Parity Technologies (UK) Ltd.
// This file is part of Substrate API Sidecar.
//
// Substrate API Sidecar is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
Object.defineProperty(exports, "__esModule", { value: true });
exports.catchesErrWithResponse = exports.catchesErrWithStatus = exports.callsNextWithErr = void 0;
exports.callsNextWithSentHeaders = callsNextWithSentHeaders;
/**
* Empty mock Express Request.
*/
const mockReq = {};
/**
* Assert that a middleware function (`ware`) calls `next` when passed `err`.
*
* @param ware the error handling middleware to test
* @param name name of the error type
* @param err error
*/
const callsNextWithErr = (ware) => (name, err) => {
it(`calls next on error of type ${name}`, async () => {
const next = jest.fn();
const send = jest.fn();
const status = jest.fn((_code) => {
return {
send,
};
});
await ware(err, mockReq, { headersSent: false, status }, next);
expect(status).not.toHaveBeenCalled();
expect(send).not.toHaveBeenCalled();
expect(next).toHaveBeenCalledTimes(1);
});
};
exports.callsNextWithErr = callsNextWithErr;
/**
* Assert that a middleware function (`ware`) will catch `err` and set status to
* `code`.
*
* @param ware the error handling middleware to test
* @param name name of the error type
* @param err error
* @param code expected code to be sent as status
*/
const catchesErrWithStatus = (ware) => (name, err, code) => {
it(`catches ${name} and sends status code ${code}`, async () => {
const next = jest.fn();
const send = jest.fn();
const status = jest.fn((_code) => {
return {
send,
};
});
await ware(err, mockReq, { headersSent: false, status }, next);
expect(send).toHaveBeenCalledTimes(1);
expect(status).toHaveBeenCalledWith(code);
expect(status).toHaveBeenCalledTimes(1);
expect(next).not.toHaveBeenCalled();
});
};
exports.catchesErrWithStatus = catchesErrWithStatus;
/**
* Assert that a middleware function (`ware`) will catch `err`, set status to
* `code`, and send `response`.
*
* @param ware the error handling middleware to test
* @param name name of the error type
* @param err error
* @param code expected code to be sent as status
* @param response expected response body
*/
const catchesErrWithResponse = (ware) => (name, err, code, response) => {
it(`catches ${name} and sends status code ${code}`, async () => {
const next = jest.fn();
const send = jest.fn();
const status = jest.fn((_code) => {
return {
send,
};
});
await ware(err, mockReq, { headersSent: false, status }, next);
expect(send).toHaveBeenCalledTimes(1);
expect(send).toHaveBeenCalledWith(response);
expect(status).toHaveBeenCalledWith(code);
expect(status).toHaveBeenCalledTimes(1);
expect(next).not.toHaveBeenCalled();
});
};
exports.catchesErrWithResponse = catchesErrWithResponse;
function callsNextWithSentHeaders(ware, err) {
it('calls next if the headers have been sent', async () => {
const next = jest.fn();
const send = jest.fn();
const status = jest.fn((_code) => {
return {
send,
};
});
await ware(err, mockReq, { headersSent: true, status }, next);
expect(send).not.toBeCalled();
expect(status).not.toBeCalled();
expect(next).toBeCalledTimes(1);
});
}
//# sourceMappingURL=testTools.js.map