Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | import { Response, Router } from "express";
import PromiseRouter from "express-promise-router";
import { logger } from "../logger";
import { RequestExt } from "../server";
import { coverageService } from "../services";
import { MockRequestHandler, processRequest } from "./request-processor";
export type HttpMethod = "get" | "post" | "put" | "patch" | "delete" | "head" | "options";
export type Category = "vanilla" | "azure" | "dpg" | "optional";
export class MockApiRouter {
public router: Router;
private currentCategory: Category | undefined;
private registeredRoutes: Map<string, Record<string, string>>;
public constructor() {
this.registeredRoutes = new Map();
this.router = PromiseRouter();
}
/**
* Set the category for the route definition inside of the provided function.
* @param category Category.
* @param callback Callback where are defined the mock routes.
*/
public category(category: Category, callback: () => void): void {
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.
*/
public get(uri: string, name: string | undefined, func: MockRequestHandler): void {
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.
*/
public post(uri: string, name: string | undefined, func: MockRequestHandler): void {
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.
*/
public put(uri: string, name: string | undefined, func: MockRequestHandler): void {
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.
*/
public patch(uri: string, name: string | undefined, func: MockRequestHandler): void {
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.
*/
public delete(uri: string, name: string | undefined, func: MockRequestHandler): void {
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.
*/
public options(uri: string, name: string | undefined, func: MockRequestHandler): void {
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.
*/
public head(uri: string, name: string | undefined, func: MockRequestHandler): void {
Iif (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()`)
*/
public request(method: HttpMethod, uri: string, name: string | undefined, func: MockRequestHandler): void {
logger.info(`Registering route ${method} ${uri} (${name})`);
this.trackRegistration(uri, method, name);
Iif (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;
Iif (name) {
coverageService.register(category, name);
}
this.router.route(uri)[method](async (req: RequestExt, res: Response) => {
await processRequest(category, name, req, res, func);
});
}
private hasRegistration(uri: string, method: HttpMethod): boolean {
const pathRegistrations = this.registeredRoutes.get(uri);
Iif (!pathRegistrations) {
return false;
}
return pathRegistrations[method] !== undefined;
}
private trackRegistration(uri: string, method: HttpMethod, name = "") {
const pathRegistrations = this.registeredRoutes.get(uri);
Iif (!pathRegistrations) {
this.registeredRoutes.set(uri, { [method]: name });
return;
}
Iif (pathRegistrations[method]) {
throw new Error(`A handler for ${method} ${uri} has been registered already`);
}
pathRegistrations[method] = name;
return;
}
}
|