UNPKG

@tsed/platform-test-sdk

Version:
208 lines (207 loc) 8.34 kB
import { __decorate, __metadata, __param } from "tslib"; import { Controller } from "@tsed/di"; import { PlatformTest } from "@tsed/platform-http/testing"; import { Context, PathParams } from "@tsed/platform-params"; import { Get, Pattern, Post } from "@tsed/schema"; import SuperTest from "supertest"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; let TestPathParamsCtrl = class TestPathParamsCtrl { testScenario1(scope) { // Here scope will be {0: 'a', 1: 'b', 2: 'c'} instead of 'abc' in version 5.47.0 expect(scope).toEqual("abc"); return Promise.resolve(scope); } testScenario2(scope) { // This way it works in version 5.47.0 expect(scope).toEqual("abc"); return scope; } testScenario3(scope) { expect(scope).toEqual("abc"); // Here the function will return {0: 'a', 1: 'b', 2: 'c'} instead of ['a','b','c'] in version 5.44.13 return Promise.resolve(["a", "b", "c"]); } testScenario4(scope, ctx) { expect(scope).toEqual("abc"); // This way it works in version 5.44.13 ctx.response.body(["a", "b", "c"]); } testScenario5(scopeId, ctx) { return "test"; } testScenario6(id) { return { id }; } // test path syntaxes testSyntax1(wildcard, ctx) { return { wildcard }; } testSyntax2(wildcard, ctx) { return { wildcard }; } testSyntax3(wildcard, ctx) { return { wildcard }; } testSyntax4(named, ctx) { return { named }; } }; __decorate([ Get("/scenario-1/:scope/:scopeId"), __param(0, PathParams("scope")), __metadata("design:type", Function), __metadata("design:paramtypes", [String]), __metadata("design:returntype", void 0) ], TestPathParamsCtrl.prototype, "testScenario1", null); __decorate([ Get("/scenario-2/:scope/:scopeId"), __param(0, PathParams("scope")), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", void 0) ], TestPathParamsCtrl.prototype, "testScenario2", null); __decorate([ Post("/scenario-3/:scope/:scopeId"), __param(0, PathParams("scope")), __metadata("design:type", Function), __metadata("design:paramtypes", [String]), __metadata("design:returntype", Promise) ], TestPathParamsCtrl.prototype, "testScenario3", null); __decorate([ Post("/scenario-4/:scope/:scopeId"), __param(0, PathParams("scope")), __param(1, Context()), __metadata("design:type", Function), __metadata("design:paramtypes", [String, Object]), __metadata("design:returntype", void 0) ], TestPathParamsCtrl.prototype, "testScenario4", null); __decorate([ Get("/scenario-5/:scopeId"), __param(0, PathParams("scopeId")), __param(0, Pattern(/^[0-9a-fA-F]{24}$/)), __param(1, Context()), __metadata("design:type", Function), __metadata("design:paramtypes", [String, Object]), __metadata("design:returntype", void 0) ], TestPathParamsCtrl.prototype, "testScenario5", null); __decorate([ Get("/scenario-6/:id"), __param(0, PathParams("id")), __metadata("design:type", Function), __metadata("design:paramtypes", [Number]), __metadata("design:returntype", void 0) ], TestPathParamsCtrl.prototype, "testScenario6", null); __decorate([ Get("/syntax-1/*"), __param(0, PathParams("*")), __param(1, Context()), __metadata("design:type", Function), __metadata("design:paramtypes", [String, Function]), __metadata("design:returntype", void 0) ], TestPathParamsCtrl.prototype, "testSyntax1", null); __decorate([ Get("/syntax-2/(.*)"), __param(0, PathParams("*")), __param(1, Context()), __metadata("design:type", Function), __metadata("design:paramtypes", [String, Function]), __metadata("design:returntype", void 0) ], TestPathParamsCtrl.prototype, "testSyntax2", null); __decorate([ Get("/syntax-3/:named*"), __param(0, PathParams("named")), __param(1, Context()), __metadata("design:type", Function), __metadata("design:paramtypes", [String, Function]), __metadata("design:returntype", void 0) ], TestPathParamsCtrl.prototype, "testSyntax3", null); __decorate([ Get("/syntax-4/:named?"), __param(0, PathParams("named")), __param(1, Context()), __metadata("design:type", Function), __metadata("design:paramtypes", [String, Function]), __metadata("design:returntype", void 0) ], TestPathParamsCtrl.prototype, "testSyntax4", null); TestPathParamsCtrl = __decorate([ Controller("/path-params") ], TestPathParamsCtrl); export function testPathParams(options) { let request; beforeAll(PlatformTest.bootstrap(options.server, { ...options, // logger: { // level: "info" // }, mount: { "/rest": [TestPathParamsCtrl] } })); beforeAll(() => { request = SuperTest(PlatformTest.callback()); }); afterAll(PlatformTest.reset); it("Scenario 1: GET /rest/path-params/scenario-1/scope/scopeId", async () => { const response = await request.get("/rest/path-params/scenario-1/abc/1").expect(200); expect(response.text).toEqual("abc"); }); it("Scenario 2: GET /rest/path-params/scenario-2/scopeId", async () => { const response = await request.get("/rest/path-params/scenario-2/abc/1").expect(200); expect(response.text).toEqual("abc"); }); it("Scenario 3: POST /rest/path-params/scenario-3/scope/scopeId", async () => { const response = await request.post("/rest/path-params/scenario-3/abc/1").expect(200); expect(response.body).toEqual(["a", "b", "c"]); }); it("Scenario 4: POST /rest/path-params/scenario-4/scope/scopeId", async () => { const response = await request.post("/rest/path-params/scenario-4/abc/1").expect(200); expect(response.body).toEqual(["a", "b", "c"]); }); it("Scenario 5 a: GET /rest/path-params/scenario-5/scopeId", async () => { const response = await request.get("/rest/path-params/scenario-5/1").expect(400); expect(response.body).toEqual({ errors: [ { data: "1", requestPath: "path", dataPath: ".scopeId", instancePath: "", keyword: "pattern", message: 'must match pattern "^[0-9a-fA-F]{24}$"', params: { pattern: "^[0-9a-fA-F]{24}$" }, schemaPath: "#/pattern" } ], message: 'Bad request on parameter "request.path.scopeId".\nValue must match pattern "^[0-9a-fA-F]{24}$". Given value: "1"', name: "AJV_VALIDATION_ERROR", status: 400 }); }); it("Scenario 5b: GET /rest/path-params/scenario-5/scopeId", async () => { await request.get("/rest/path-params/scenario-5/5ce4ee471495836c5e2e4cb0").expect(200); }); it("Scenario 6: GET /rest/path-params/scenario-6/id", async () => { const response = await request.get("/rest/path-params/scenario-6/1").expect(200); expect(response.body).toEqual({ id: 1 }); }); describe("path syntaxes", () => { it("Syntax 1: GET /rest/path-params/syntax-1/*", async () => { const response = await request.get("/rest/path-params/syntax-1/abc/def").expect(200); expect(response.body).toEqual({ wildcard: "abc/def" }); }); it("Syntax 2: GET /rest/path-params/syntax-2/(.*)", async () => { const response = await request.get("/rest/path-params/syntax-2/abc/def").expect(200); expect(response.body).toEqual({ wildcard: "abc/def" }); }); it("Syntax 3: GET /rest/path-params/syntax-3/:named*", async () => { const response = await request.get("/rest/path-params/syntax-3/abc/def").expect(200); expect(response.body).toEqual({ wildcard: "abc/def" }); }); it("Syntax 4: GET /rest/path-params/syntax-4/:named?", async () => { const response = await request.get("/rest/path-params/syntax-4/named").expect(200); expect(response.body).toEqual({ named: "named" }); }); }); }