@wocker/testing 1.0.3 → 1.0.4-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,16 +0,0 @@
1
- import Modem from "docker-modem";
2
- import type { ConstructorOptions, DialOptions, RequestCallback } from "docker-modem";
3
- import { DockerStorage } from "./DockerStorage";
4
- import { Fixtures } from "./Fixtures";
5
- type Options = ConstructorOptions & {
6
- mockFixtures?: Fixtures;
7
- };
8
- export declare class ModemMock extends Modem {
9
- protected storage: DockerStorage;
10
- protected version: string;
11
- constructor({ mockFixtures, ...rest }: Options);
12
- dial(options: DialOptions, callback?: RequestCallback): void;
13
- registerFixtures(fixtures: Fixtures): void;
14
- reset(): void;
15
- }
16
- export {};
@@ -1,46 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.ModemMock = void 0;
7
- const docker_modem_1 = __importDefault(require("docker-modem"));
8
- const DockerStorage_1 = require("./DockerStorage");
9
- class ModemMock extends docker_modem_1.default {
10
- constructor({ mockFixtures, ...rest }) {
11
- super(rest);
12
- this.storage = new DockerStorage_1.DockerStorage();
13
- if (mockFixtures) {
14
- this.storage.registerFixtures(mockFixtures);
15
- }
16
- }
17
- dial(options, callback) {
18
- const { method, options: body, file } = options;
19
- if (this.version) {
20
- options.path = `/${this.version}${options.path}`;
21
- }
22
- (async () => {
23
- if (file && typeof file !== "string" && "on" in file) {
24
- await new Promise((resolve) => {
25
- file.on("data", () => undefined);
26
- file.on("close", resolve);
27
- file.on("error", resolve);
28
- });
29
- }
30
- try {
31
- const result = await this.storage.exec(method, options.path, body);
32
- callback && callback(null, result);
33
- }
34
- catch (err) {
35
- callback && callback(err, null);
36
- }
37
- })();
38
- }
39
- registerFixtures(fixtures) {
40
- this.storage.registerFixtures(fixtures);
41
- }
42
- reset() {
43
- this.storage.reset();
44
- }
45
- }
46
- exports.ModemMock = ModemMock;
@@ -1,14 +0,0 @@
1
- import Modem, { ConstructorOptions, DialOptions, RequestCallback } from "docker-modem";
2
- import { Fixtures } from "./Fixtures";
3
- import { Router } from "../router";
4
- type Options = ConstructorOptions & {
5
- recordFixtures?: Fixtures;
6
- };
7
- export declare class ModemRecorder extends Modem {
8
- protected router: Router;
9
- protected fixtures?: Fixtures;
10
- constructor({ recordFixtures, ...rest }: Options);
11
- dial(options: DialOptions, callback?: RequestCallback): void;
12
- setFixtures(fixtures: Fixtures): this;
13
- }
14
- export {};
@@ -1,60 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.ModemRecorder = void 0;
7
- const docker_modem_1 = __importDefault(require("docker-modem"));
8
- const cli_1 = require("@kearisp/cli");
9
- const router_1 = require("../router");
10
- class ModemRecorder extends docker_modem_1.default {
11
- constructor({ recordFixtures, ...rest }) {
12
- super(rest);
13
- this.router = new router_1.Router();
14
- this.fixtures = recordFixtures;
15
- this.router.get(["/containers/json", "/:version/containers/json"], (req, res) => {
16
- const { body: result } = req;
17
- if (this.fixtures) {
18
- this.fixtures.recordJSON(result);
19
- }
20
- res.send(null);
21
- });
22
- this.router.get(["/images/:tag/json", "/:version/images/:tag/json"], (req, res) => {
23
- const { params: { version = "v1", tag: fullName }, body: result } = req;
24
- const [, image, tag] = /^([^:]+):(.*)$/.exec(fullName);
25
- if (this.fixtures) {
26
- this.fixtures.saveImage(version, image, tag, result);
27
- }
28
- res.send(null);
29
- });
30
- this.router.post(["/images/create", "/:version/images/create"], async (req, res) => {
31
- const { params: { version = "v1" }, options: { fromImage, tag }, body: stream } = req;
32
- if (this.fixtures && stream) {
33
- await this.fixtures.recordPullStream(version, fromImage, tag, stream);
34
- }
35
- res.send(null);
36
- });
37
- this.router.post(["/build", "/:version/build"], async (req, res) => {
38
- const { params: { version = "v1" }, options: { t: fullName, version: buildVersion = "1" }, body: stream } = req;
39
- if (this.fixtures && stream) {
40
- const [image, tag] = fullName.split(":");
41
- cli_1.Logger.info("Building...", version, buildVersion, image, tag);
42
- await this.fixtures.recordBuildStream(version, buildVersion, image, tag, stream);
43
- }
44
- res.send(null);
45
- });
46
- }
47
- dial(options, callback) {
48
- super.dial(options, (err, result) => {
49
- const { path, method, options: body } = options;
50
- cli_1.Logger.info("Recorder dial:", method, path);
51
- this.router.exec(method, path, result, body).catch(() => undefined);
52
- callback(err, result);
53
- });
54
- }
55
- setFixtures(fixtures) {
56
- this.fixtures = fixtures;
57
- return this;
58
- }
59
- }
60
- exports.ModemRecorder = ModemRecorder;
@@ -1,9 +0,0 @@
1
- import { HttpMethod } from "../types/HttpMethod";
2
- export declare class Request {
3
- readonly method: HttpMethod;
4
- readonly path: string;
5
- readonly params: Record<string, any>;
6
- readonly body: any;
7
- readonly options: any;
8
- constructor(method: HttpMethod, path: string, params: Record<string, any>, body: any, options: any);
9
- }
@@ -1,13 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Request = void 0;
4
- class Request {
5
- constructor(method, path, params, body, options) {
6
- this.method = method;
7
- this.path = path;
8
- this.params = params;
9
- this.body = body;
10
- this.options = options;
11
- }
12
- }
13
- exports.Request = Request;
@@ -1,7 +0,0 @@
1
- export declare class Response {
2
- protected _status: number;
3
- protected _body: any;
4
- constructor();
5
- status(status: number): this;
6
- send(body: any): this;
7
- }
@@ -1,17 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Response = void 0;
4
- class Response {
5
- constructor() {
6
- this._status = 200;
7
- }
8
- status(status) {
9
- this._status = status;
10
- return this;
11
- }
12
- send(body) {
13
- this._body = body;
14
- return this;
15
- }
16
- }
17
- exports.Response = Response;
@@ -1,20 +0,0 @@
1
- import { Request } from "./Request";
2
- import { Response } from "./Response";
3
- import { HttpMethod } from "../types/HttpMethod";
4
- export type RouteHandler = (req: Request, res: Response) => void | Promise<void>;
5
- export declare class Router {
6
- protected routes: {
7
- method: HttpMethod;
8
- pattern: string;
9
- handler: RouteHandler;
10
- }[];
11
- protected request(method: HttpMethod, pattern: string | string[], handler: RouteHandler): this;
12
- get(pattern: string | string[], handler: RouteHandler): this;
13
- post(pattern: string | string[], handler: RouteHandler): this;
14
- put(pattern: string | string[], handler: RouteHandler): this;
15
- patch(pattern: string | string[], handler: RouteHandler): this;
16
- delete(pattern: string | string[], handler: RouteHandler): this;
17
- parseRoute(method: HttpMethod, path: string): [RouteHandler, Record<string, any>];
18
- protected matchRoute(path: string, pattern: string): Record<string, string> | null;
19
- exec(method: HttpMethod, path: string, body: any, options?: any): Promise<any>;
20
- }
@@ -1,104 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Router = void 0;
4
- const Request_1 = require("./Request");
5
- const Response_1 = require("./Response");
6
- class Router {
7
- constructor() {
8
- this.routes = [];
9
- }
10
- request(method, pattern, handler) {
11
- if (Array.isArray(pattern)) {
12
- pattern.forEach((pattern) => {
13
- this.routes.push({
14
- method,
15
- pattern,
16
- handler
17
- });
18
- });
19
- }
20
- else {
21
- this.routes.push({
22
- method,
23
- pattern,
24
- handler
25
- });
26
- }
27
- return this;
28
- }
29
- get(pattern, handler) {
30
- return this.request("GET", pattern, handler);
31
- }
32
- post(pattern, handler) {
33
- return this.request("POST", pattern, handler);
34
- }
35
- put(pattern, handler) {
36
- return this.request("PUT", pattern, handler);
37
- }
38
- patch(pattern, handler) {
39
- return this.request("PATCH", pattern, handler);
40
- }
41
- delete(pattern, handler) {
42
- return this.request("DELETE", pattern, handler);
43
- }
44
- parseRoute(method, path) {
45
- for (const route of this.routes) {
46
- if (route.method !== method) {
47
- continue;
48
- }
49
- const params = this.matchRoute(path, route.pattern);
50
- if (!params) {
51
- continue;
52
- }
53
- return [route.handler, params];
54
- }
55
- return [null, null];
56
- }
57
- matchRoute(path, pattern) {
58
- const paramMatches = pattern.match(/:[a-zA-Z0-9_]+/g);
59
- if (!paramMatches) {
60
- return path === pattern ? {} : null;
61
- }
62
- let regexPattern = pattern;
63
- const paramNames = [];
64
- paramMatches.forEach(param => {
65
- const paramName = param.substring(1);
66
- paramNames.push(paramName);
67
- const isLastParam = pattern.indexOf(param) + param.length === pattern.length;
68
- if (isLastParam) {
69
- regexPattern = regexPattern.replace(param, "(.+)");
70
- }
71
- else {
72
- const restOfPattern = pattern.substring(pattern.indexOf(param) + param.length);
73
- const nextFixedPart = restOfPattern.match(/^\/[^:][^\/]*/);
74
- if (nextFixedPart) {
75
- regexPattern = regexPattern.replace(param, "(.+?)");
76
- }
77
- else {
78
- regexPattern = regexPattern.replace(param, "(.+)");
79
- }
80
- }
81
- });
82
- const regex = new RegExp(`^${regexPattern}$`);
83
- const match = path.match(regex);
84
- if (!match) {
85
- return null;
86
- }
87
- const params = {};
88
- for (let i = 0; i < paramNames.length; i++) {
89
- params[paramNames[i]] = match[i + 1];
90
- }
91
- return params;
92
- }
93
- async exec(method, path, body, options = {}) {
94
- const url = new URL(path, "https://localhost"), [handler, params] = this.parseRoute(method, url.pathname);
95
- if (!handler) {
96
- throw new Error(`Route ${method} ${path} not found`);
97
- }
98
- const request = new Request_1.Request(method, url.pathname, params, body, options);
99
- const response = new Response_1.Response();
100
- await handler(request, response);
101
- return response._body;
102
- }
103
- }
104
- exports.Router = Router;
@@ -1,3 +0,0 @@
1
- export * from "./Request";
2
- export * from "./Response";
3
- export * from "./Router";
@@ -1,10 +0,0 @@
1
- export type Container = {
2
- Id: string;
3
- Name: string;
4
- Image: string;
5
- State: {
6
- Status: string;
7
- Running: boolean;
8
- };
9
- Created: number;
10
- };
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1 +0,0 @@
1
- export type HttpMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,8 +0,0 @@
1
- export type Image = {
2
- Id: string;
3
- RepoTags: string[];
4
- ParentId?: string;
5
- Labels: {
6
- [name: string]: string;
7
- };
8
- };
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });