UNPKG

@tsed/platform-test-sdk

Version:
614 lines (613 loc) 22.8 kB
import { __decorate, __metadata, __param } from "tslib"; import { Controller, getContext } from "@tsed/di"; import { PlatformResponse, Res } from "@tsed/platform-http"; import { PlatformTest } from "@tsed/platform-http/testing"; import { Context, PathParams } from "@tsed/platform-params"; import { CollectionOf, Enum, ForwardGroups, Get, Groups, Ignore, Name, Post, Property, Required, Returns, Status } from "@tsed/schema"; import axios from "axios"; import { of } from "rxjs"; import { agent } from "superagent"; import SuperTest from "supertest"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; class Base { } __decorate([ Ignore(), __metadata("design:type", String) ], Base.prototype, "ignoreMeBase", void 0); __decorate([ Property(), __metadata("design:type", String) ], Base.prototype, "fooBase", void 0); class MyModel extends Base { } __decorate([ Property(), __metadata("design:type", String) ], MyModel.prototype, "foo", void 0); __decorate([ Ignore(), __metadata("design:type", String) ], MyModel.prototype, "ignoreMe", void 0); __decorate([ Ignore((value, ctx) => ctx.endpoint), __metadata("design:type", String) ], MyModel.prototype, "ignoreMe2", void 0); class EmptyModel { } export var EnumValue; (function (EnumValue) { EnumValue["One"] = "one"; EnumValue["Two"] = "two"; })(EnumValue || (EnumValue = {})); export class NestedEnum { } __decorate([ Required(), Enum(EnumValue), __metadata("design:type", String) ], NestedEnum.prototype, "value", void 0); export class TestNestedEnum { } __decorate([ Property(), __metadata("design:type", NestedEnum) ], TestNestedEnum.prototype, "nested", void 0); export class ModelGroup { } __decorate([ Property(), __metadata("design:type", String) ], ModelGroup.prototype, "id", void 0); __decorate([ Groups("!creation"), __metadata("design:type", String) ], ModelGroup.prototype, "groups", void 0); class TeamModel { } __decorate([ Required(), Name("teamName"), __metadata("design:type", String) ], TeamModel.prototype, "name", void 0); class TeamsModel { } __decorate([ Required(), CollectionOf(TeamModel), ForwardGroups(), __metadata("design:type", Array) ], TeamsModel.prototype, "teams", void 0); class AllowedModel { } __decorate([ Property(), __metadata("design:type", String) ], AllowedModel.prototype, "id", void 0); __decorate([ Property(), __metadata("design:type", String) ], AllowedModel.prototype, "description", void 0); __decorate([ Groups("summary"), __metadata("design:type", String) ], AllowedModel.prototype, "prop1", void 0); __decorate([ Groups("details"), __metadata("design:type", String) ], AllowedModel.prototype, "prop2", void 0); __decorate([ Groups("admin"), __metadata("design:type", String) ], AllowedModel.prototype, "sensitiveProp", void 0); let TestResponseParamsCtrl = class TestResponseParamsCtrl { testScenario1Get(id) { return `hello-${id}`; } testScenario3EmptyResponse(id, ctx) { if (id) { return; } ctx.response.status(201); return { id: 1 }; } async testScenario5Promise() { await new Promise((resolve) => { resolve(undefined); }); return { id: 1 }; } testScenario6Observable() { return of({ id: 1 }); } testScenario6bObservable() { return Promise.resolve(of({ id: 1 })); } testScenario9Get() { return "value"; } testScenario9WithDynamicParam(id) { return "value" + id; } testScenario10() { const model = new MyModel(); model.foo = "foo"; model.ignoreMe = "ignoreMe"; model.ignoreMe2 = "ignoreMe2"; model.fooBase = "fooBase"; model.ignoreMeBase = "ignoreMeBase"; return model; } testScenario11() { const t = new EmptyModel(); t.raw = 1; t.affected = 1; return t; } testScenario12(res) { res.attachment("filename"); return Buffer.from("Hello"); } async testScenario13(res) { const http = agent(); const image_res = await http.get("https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"); res.setHeader("Content-Disposition", "inline;filename=googlelogo_color_272x92dp.png;"); res.setHeader("X-Content-Type-Options", "nosniff"); res.setHeader("Content-Type", "image/png"); return image_res.body; } scenario14() { return { jsonexample: 1 }; } testScenario15a() { return axios.get(`https://tsed.dev/api.json`, { responseType: "json" }); } testScenario15b() { return axios.get(`https://api.tsed.dev/rest/github/typed/test`, { responseType: "json" }); } testScenario16() { const test = new TestNestedEnum(); const nested = new NestedEnum(); nested.value = EnumValue.One; test.nested = nested; return Promise.resolve(test); } testScenario17() { const model = new ModelGroup(); model.id = "id"; model.groups = "groups"; return Promise.resolve(model); } testScenario18() { const data = new TeamsModel(); const team = {}; team.name = "name"; data.teams = [team]; return data; } testScenario19() { const model = new AllowedModel(); model.id = "id"; model.prop1 = "prop1"; model.description = "description"; model.prop2 = "prop2"; model.sensitiveProp = "sensitiveProp"; return Promise.resolve(model); } testScenario20() { const $ctx = getContext(); return { id: $ctx?.request.query.id }; } testScenario21() { return Promise.resolve(true); } testScenario22() { return Promise.resolve(1); } testScenario23() { return Promise.resolve(null); } }; __decorate([ Get("/scenario1/:id"), __param(0, PathParams("id")), __metadata("design:type", Function), __metadata("design:paramtypes", [Number]), __metadata("design:returntype", void 0) ], TestResponseParamsCtrl.prototype, "testScenario1Get", null); __decorate([ Post("/scenario3/:id?"), Status(204), __param(0, PathParams("id")), __param(1, Context()), __metadata("design:type", Function), __metadata("design:paramtypes", [Number, Object]), __metadata("design:returntype", void 0) ], TestResponseParamsCtrl.prototype, "testScenario3EmptyResponse", null); __decorate([ Get("/scenario5"), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], TestResponseParamsCtrl.prototype, "testScenario5Promise", null); __decorate([ Get("/scenario6"), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], TestResponseParamsCtrl.prototype, "testScenario6Observable", null); __decorate([ Get("/scenario6b"), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], TestResponseParamsCtrl.prototype, "testScenario6bObservable", null); __decorate([ Get("/scenario9/static"), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", String) ], TestResponseParamsCtrl.prototype, "testScenario9Get", null); __decorate([ Get("/scenario9/:id"), __param(0, PathParams("id")), __metadata("design:type", Function), __metadata("design:paramtypes", [Number]), __metadata("design:returntype", String) ], TestResponseParamsCtrl.prototype, "testScenario9WithDynamicParam", null); __decorate([ Get("/scenario10"), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], TestResponseParamsCtrl.prototype, "testScenario10", null); __decorate([ Get("/scenario11"), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], TestResponseParamsCtrl.prototype, "testScenario11", null); __decorate([ Get("/scenario12"), __param(0, Res()), __metadata("design:type", Function), __metadata("design:paramtypes", [PlatformResponse]), __metadata("design:returntype", void 0) ], TestResponseParamsCtrl.prototype, "testScenario12", null); __decorate([ Get("/scenario13"), __param(0, Res()), __metadata("design:type", Function), __metadata("design:paramtypes", [PlatformResponse]), __metadata("design:returntype", Promise) ], TestResponseParamsCtrl.prototype, "testScenario13", null); __decorate([ Get("/scenario14"), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], TestResponseParamsCtrl.prototype, "scenario14", null); __decorate([ Get("/scenario15a"), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], TestResponseParamsCtrl.prototype, "testScenario15a", null); __decorate([ Get("/scenario15b"), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], TestResponseParamsCtrl.prototype, "testScenario15b", null); __decorate([ Get("/scenario16"), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], TestResponseParamsCtrl.prototype, "testScenario16", null); __decorate([ Get("/scenario17"), (Returns(201, ModelGroup).Groups("creation")), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], TestResponseParamsCtrl.prototype, "testScenario17", null); __decorate([ Get("/scenario18"), Returns(200, TeamsModel), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], TestResponseParamsCtrl.prototype, "testScenario18", null); __decorate([ Get("/scenario19"), (Returns(200, AllowedModel).Groups("!admin").AllowedGroups("summary", "details")), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], TestResponseParamsCtrl.prototype, "testScenario19", null); __decorate([ Get("/scenario20"), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], TestResponseParamsCtrl.prototype, "testScenario20", null); __decorate([ Get("/scenario21"), Returns(200), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], TestResponseParamsCtrl.prototype, "testScenario21", null); __decorate([ Get("/scenario22"), Returns(200), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], TestResponseParamsCtrl.prototype, "testScenario22", null); __decorate([ Get("/scenario23"), Returns(200), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], TestResponseParamsCtrl.prototype, "testScenario23", null); TestResponseParamsCtrl = __decorate([ Controller("/response") ], TestResponseParamsCtrl); export function testResponse(options) { let request; beforeAll(PlatformTest.bootstrap(options.server, { ...options, mount: { "/rest": [TestResponseParamsCtrl] } })); beforeAll(() => { request = SuperTest(PlatformTest.callback()); }); afterAll(PlatformTest.reset); describe("Scenario1: return the id (classic)", () => { describe("GET /rest/response/scenario1/:id", () => { it("should return the id + test", async () => { const response = await request.get("/rest/response/scenario1/10").expect(200); expect(response.text).toEqual("hello-10"); }); }); }); describe("Scenario3: when response is empty or created", () => { describe("GET /rest/response/scenario3/:id?", () => { it("should return nothing with a 204 status", async () => { const response = await request.post("/rest/response/scenario3/10").expect(204); expect(response.text).toEqual(""); }); it("should return a body", async () => { const response = await request.post("/rest/response/scenario3").expect(201); expect(response.body).toEqual({ id: 1 }); }); }); }); describe("Scenario5: when endpoint response from promise", () => { describe("GET /rest/response/scenario5", () => { it("should return a body", async () => { const response = await request.get("/rest/response/scenario5"); expect(response.body).toEqual({ id: 1 }); }); }); }); describe("Scenario6: when endpoint return an observable", () => { describe("GET /rest/response/scenario6", () => { it("should return a body", async () => { const response = await request.get("/rest/response/scenario6"); expect(response.body).toEqual({ id: 1 }); }); }); describe("GET /rest/response/scenario6b", () => { it("should return a body", async () => { const response = await request.get("/rest/response/scenario6b"); expect(response.body).toEqual({ id: 1 }); }); }); }); describe("Scenario9: routes without parameters must be defined first", () => { describe("GET /rest/response/scenario9/static", () => { it("should return the test", async () => { const response = await request.get("/rest/response/scenario9/static").expect(200); expect(response.text).toEqual("value"); }); }); describe("GET /rest/response/scenario9/:dynamic", () => { it("should return the test + id", async () => { const response = await request.get("/rest/response/scenario9/10").expect(200); expect(response.text).toEqual("value10"); }); }); describe("GET /rest/response/scenario9/:dynamic", () => { it("should throw a badRequest when path params isn't set as number", async () => { const response = await request.get("/rest/response/scenario9/kkk").expect(400); expect(response.body).toEqual({ errors: [ { data: "kkk", requestPath: "path", dataPath: ".id", instancePath: "", keyword: "type", message: "must be number", params: { type: "number" }, schemaPath: "#/type" } ], message: 'Bad request on parameter "request.path.id".\nValue must be number. Given value: "kkk"', name: "AJV_VALIDATION_ERROR", status: 400 }); }); }); }); describe("Scenario10: return a model with ignored props", () => { it("should return a body", async () => { const response = await request.get("/rest/response/scenario10"); expect(response.body).toEqual({ foo: "foo", fooBase: "fooBase" }); }); }); describe("Scenario11: return models without props", () => { it("should return a body", async () => { const response = await request.get("/rest/response/scenario11"); expect(response.body).toEqual({ affected: 1, raw: 1 }); }); }); describe("Scenario12: Return buffer", () => { it("should return a body", async () => { const response = await request.get("/rest/response/scenario12"); expect(response.headers["content-disposition"]).toEqual('attachment; filename="filename"'); expect(response.headers["content-type"]).toContain("application/octet-stream"); expect(response.headers["content-length"]).toEqual("5"); expect(response.body.toString()).toEqual("Hello"); }); }); describe("Scenario13: Return buffer", () => { it("should return image", async () => { const response = await request.get("/rest/response/scenario13"); expect(response.headers["content-disposition"]).toEqual("inline;filename=googlelogo_color_272x92dp.png;"); expect(response.headers["content-type"]).toContain("image/png"); expect(response.headers["content-length"]).toEqual("5969"); }); }); describe("Scenario14: Return application/json when Accept is */*", () => { it("should return a */* content-type", async () => { const response = await request .get("/rest/response/scenario14") .set("Accept", "*/*") .set("Content-Type", "application/json") .expect(200); expect(response.headers["content-type"]).toEqual("application/json; charset=utf-8"); expect(response.body).toEqual({ jsonexample: 1 }); }); }); describe("Scenario15: Use axios as proxy", () => { it("should return the response (200)", async () => { const response = await request.get("/rest/response/scenario15a").expect(200); expect(response.headers["content-type"]).toEqual("application/json; charset=utf-8"); }); it("should return the response (401)", async () => { const response = await request.get("/rest/response/scenario15b").expect(401); expect(response.headers["content-type"]).toEqual("application/json; charset=utf-8"); }); }); describe("Scenario16: Return response with nested enum", () => { it("should return the response (200)", async () => { const response = await request.get("/rest/response/scenario16").expect(200); expect(response.body).toEqual({ nested: { value: "one" } }); }); }); describe("Scenario17: Return response with groups on 201", () => { it("should return the response (200)", async () => { const response = await request.get("/rest/response/scenario17").expect(201); expect(response.body).toEqual({ id: "id" }); }); }); describe("Scenario18: Return data with alias", () => { it("should return the response (200)", async () => { const response = await request.get("/rest/response/scenario18").expect(200); expect(response.body).toEqual({ teams: [ { teamName: "name" } ] }); }); }); describe("Scenario19: includes options", () => { it("should return the response (200)", async () => { const response = await request.get("/rest/response/scenario19").expect(200); expect(response.body).toEqual({ description: "description", id: "id", prop1: "prop1", prop2: "prop2" }); }); it("should return the response (200 + [summary])", async () => { const response = await request.get("/rest/response/scenario19?includes=summary").expect(200); expect(response.body).toEqual({ description: "description", id: "id", prop1: "prop1" }); }); it("should return the response (200 + [summary, details])", async () => { const response = await request.get("/rest/response/scenario19?includes=summary&includes=details").expect(200); expect(response.body).toEqual({ description: "description", id: "id", prop2: "prop2", prop1: "prop1" }); }); it("should return the response (200 + [summary, details, admin])", async () => { const response = await request.get("/rest/response/scenario19?includes=summary&includes=details&includes=admin").expect(200); expect(response.body).toEqual({ description: "description", id: "id", prop2: "prop2", prop1: "prop1" }); }); it("should return the response (200 + 'summary,details,admin')", async () => { const response = await request.get("/rest/response/scenario19?includes=summary,details,admin").expect(200); expect(response.body).toEqual({ description: "description", id: "id", prop2: "prop2", prop1: "prop1" }); }); }); describe("Scenario20: should return query data from Context", () => { it("should return the response (200)", async () => { const response = await request.get("/rest/response/scenario20?id=id").expect(200); expect(response.body).toEqual({ id: "id" }); }); }); describe("Scenario21: should return boolean", () => { it("should return the response (200)", async () => { const response = await request.get("/rest/response/scenario21").expect(200); expect(response.body).toEqual(true); }); }); describe("Scenario22: should return number", () => { it("should return the response (200)", async () => { const response = await request.get("/rest/response/scenario22").expect(200); expect(response.body).toEqual(1); }); }); describe("Scenario23: should return null", () => { it("should return the response (204)", async () => { const response = await request.get("/rest/response/scenario23").expect(204); expect(response.body).toEqual({}); }); }); }