@tsed/platform-test-sdk
Version:
Package to test platform adapter integration with Ts.ED
85 lines (84 loc) • 3.1 kB
JavaScript
import { __decorate, __metadata, __param } from "tslib";
import { Controller } from "@tsed/di";
import { PlatformResponse, Res } from "@tsed/platform-http";
import { PlatformTest } from "@tsed/platform-http/testing";
import { BodyParams } from "@tsed/platform-params";
import { Get, Returns } from "@tsed/schema";
import SuperTest from "supertest";
import { afterAll, beforeAll, expect, it } from "vitest";
let HeadersCtrl = class HeadersCtrl {
testScenario1(value) {
return "hello";
}
testScenario2() {
return "<xml></xml>";
}
testScenario3(response) {
response.setHeader("Location", `/v1/location`);
return "Hello";
}
};
__decorate([
Get("/scenario-1"),
(Returns(200, String).Header("test", "x-token")),
__param(0, BodyParams("test")),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Array]),
__metadata("design:returntype", Object)
], HeadersCtrl.prototype, "testScenario1", null);
__decorate([
Get("/scenario-2"),
(Returns(200, String).Header("x-token-test", "test").Header("x-token-test-2", "test2").ContentType("application/xml")),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], HeadersCtrl.prototype, "testScenario2", null);
__decorate([
Get("/scenario-3"),
(Returns(200, String).Headers({
Location: {
description: "URL to the new xxx",
type: "string",
value: "/v1/location/header"
}
})),
__param(0, Res()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [PlatformResponse]),
__metadata("design:returntype", void 0)
], HeadersCtrl.prototype, "testScenario3", null);
HeadersCtrl = __decorate([
Controller("/headers")
], HeadersCtrl);
export { HeadersCtrl };
export function testHeaders(options) {
let request;
beforeAll(PlatformTest.bootstrap(options.server, {
...options,
logger: {
level: "off"
},
mount: {
"/rest": [HeadersCtrl]
}
}));
beforeAll(() => {
request = SuperTest(PlatformTest.callback());
});
afterAll(PlatformTest.reset);
it("Scenario1: GET /rest/headers/scenario-1", async () => {
const response = await request.get("/rest/headers/scenario-1").expect(200);
expect(response.text).toEqual("hello");
expect(response.header["test"]).toEqual("x-token");
});
it("Scenario2: GET /rest/headers/scenario-2", async () => {
const response = await request.get("/rest/headers/scenario-2").expect(200);
expect(response.headers["x-token-test"]).toEqual("test");
expect(response.headers["x-token-test-2"]).toEqual("test2");
expect(response.headers["content-type"]).toContain("application/xml");
});
it("Scenario3: GET /rest/headers/scenario-3", async () => {
const response = await request.get("/rest/headers/scenario-3").expect(200);
expect(response.headers["location"]).toEqual("/v1/location");
});
}