UNPKG

@microsoft.azure/autorest.testserver

Version:
143 lines 5.07 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MockApiRouter = void 0; const express_promise_router_1 = __importDefault(require("express-promise-router")); const logger_1 = require("../logger"); const services_1 = require("../services"); const request_processor_1 = require("./request-processor"); class MockApiRouter { constructor() { this.registeredRoutes = new Map(); this.router = (0, express_promise_router_1.default)(); } /** * Set the category for the route definition inside of the provided function. * @param category Category. * @param callback Callback where are defined the mock routes. */ category(category, callback) { this.currentCategory = category; callback(); this.currentCategory = undefined; } /** * Register a GET request for the provided uri. * @param uri URI to match. * @param name Name of the scenario(For coverage). * @param func Request handler. */ get(uri, name, func) { this.request("get", uri, name, func); } /** * Register a POST request for the provided uri. * @param uri URI to match. * @param name Name of the scenario(For coverage). * @param func Request handler. */ post(uri, name, func) { this.request("post", uri, name, func); } /** * Register a PUT request for the provided uri. * @param uri URI to match. * @param name Name of the scenario(For coverage). * @param func Request handler. */ put(uri, name, func) { this.request("put", uri, name, func); } /** * Register a PATCH request for the provided uri. * @param uri URI to match. * @param name Name of the scenario(For coverage). * @param func Request handler. */ patch(uri, name, func) { this.request("patch", uri, name, func); } /** * Register a DELETE request for the provided uri. * @param uri URI to match. * @param name Name of the scenario(For coverage). * @param func Request handler. */ delete(uri, name, func) { this.request("delete", uri, name, func); } /** * Register a Options request for the provided uri. * @param uri URI to match. * @param name Name of the scenario(For coverage). * @param func Request handler. */ options(uri, name, func) { this.request("options", uri, name, func); } /** * Register a HEAD request for the provided uri. * @param uri URI to match. * @param name Name of the scenario(For coverage). * @param func Request handler. */ head(uri, name, func) { if (this.hasRegistration(uri, "get")) { throw new Error(`A GET handler for the path ${uri} has been defined already. Make sure head registrations are added before GET`); } this.request("head", uri, name, func); } /** * Register a request for the provided uri. * @param method Method to use. * @param uri URI to match. * @param name Name of the scenario(For coverage). * @param func Request handler. * * @note prefer to use the corresponding method method directly instead of `#request()`(i.e `#get(), #post()`) */ request(method, uri, name, func) { logger_1.logger.info(`Registering route ${method} ${uri} (${name})`); this.trackRegistration(uri, method, name); if (this.currentCategory === undefined) { throw new Error([ `Cannot register route ${method} ${uri} (${name}), missing category.`, `Please wrap it in:`, `app.category("vanilla" | "azure", () => {`, ` // app.get(...`, `});`, "", ].join("\n")); } const category = this.currentCategory; if (name) { services_1.coverageService.register(category, name); } this.router.route(uri)[method](async (req, res) => { await (0, request_processor_1.processRequest)(category, name, req, res, func); }); } hasRegistration(uri, method) { const pathRegistrations = this.registeredRoutes.get(uri); if (!pathRegistrations) { return false; } return pathRegistrations[method] !== undefined; } trackRegistration(uri, method, name = "") { const pathRegistrations = this.registeredRoutes.get(uri); if (!pathRegistrations) { this.registeredRoutes.set(uri, { [method]: name }); return; } if (pathRegistrations[method]) { throw new Error(`A handler for ${method} ${uri} has been registered already`); } pathRegistrations[method] = name; return; } } exports.MockApiRouter = MockApiRouter; //# sourceMappingURL=mock-api-router.js.map