@tufjs/repo-mock
Version:
HTTP mocking for TUF repository requests
24 lines (23 loc) • 783 B
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.mock = mock;
const nock_1 = __importDefault(require("nock"));
// Sets-up nock-based mocking for the given handler
function mock(base, handler) {
(0, nock_1.default)(base).get(handler.path).reply(adapt(handler.fn));
}
// Adapts our HandlerFn to nock's NockHandler format
function adapt(handler) {
/* istanbul ignore next */
return () => {
const { statusCode, response, contentType } = handler();
return [
statusCode,
response,
{ 'Content-Type': contentType || 'text/plain' },
];
};
}