@tsed/platform-test-sdk 8.11.1 → 8.12.0-rc.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.
Files changed (119) hide show
  1. package/lib/esm/components/PlatformTestSdk.js +0 -1
  2. package/lib/esm/errors/CustomBadRequest.js +0 -1
  3. package/lib/esm/errors/CustomInternalError.js +0 -1
  4. package/lib/esm/index.js +0 -1
  5. package/lib/esm/interfaces/PlatformTestingSdkOpts.js +0 -1
  6. package/lib/esm/interfaces/index.js +0 -1
  7. package/lib/esm/modules/feature/FeatureModule.js +0 -1
  8. package/lib/esm/modules/feature/controllers/FeatureController.js +0 -1
  9. package/lib/esm/tests/exports.js +0 -1
  10. package/lib/esm/tests/testAcceptMime.js +0 -1
  11. package/lib/esm/tests/testAuth.js +0 -1
  12. package/lib/esm/tests/testBodyParams.js +0 -1
  13. package/lib/esm/tests/testCache.js +0 -1
  14. package/lib/esm/tests/testChildrenControllers.js +0 -1
  15. package/lib/esm/tests/testCookies.js +0 -1
  16. package/lib/esm/tests/testCustom404.js +0 -1
  17. package/lib/esm/tests/testDeepQueryParams.js +0 -1
  18. package/lib/esm/tests/testErrors.js +0 -1
  19. package/lib/esm/tests/testHandlers.js +0 -1
  20. package/lib/esm/tests/testHeaderParams.js +0 -1
  21. package/lib/esm/tests/testHeaders.js +0 -1
  22. package/lib/esm/tests/testInheritanceController.js +0 -1
  23. package/lib/esm/tests/testLocals.js +0 -1
  24. package/lib/esm/tests/testLocation.js +0 -1
  25. package/lib/esm/tests/testMiddlewares.js +0 -1
  26. package/lib/esm/tests/testModule.js +0 -1
  27. package/lib/esm/tests/testMulter.js +0 -1
  28. package/lib/esm/tests/testPathParams.js +0 -1
  29. package/lib/esm/tests/testQueryParams.js +0 -1
  30. package/lib/esm/tests/testRedirect.js +0 -1
  31. package/lib/esm/tests/testResponse.js +0 -1
  32. package/lib/esm/tests/testResponseFilter.js +0 -1
  33. package/lib/esm/tests/testRouting.js +0 -1
  34. package/lib/esm/tests/testScopeRequest.js +0 -1
  35. package/lib/esm/tests/testSession.js +0 -1
  36. package/lib/esm/tests/testStatics.js +0 -1
  37. package/lib/esm/tests/testStream.js +0 -1
  38. package/lib/esm/tests/testView.js +0 -1
  39. package/package.json +18 -15
  40. package/.barrelsby.json +0 -5
  41. package/lib/esm/components/PlatformTestSdk.js.map +0 -1
  42. package/lib/esm/errors/CustomBadRequest.js.map +0 -1
  43. package/lib/esm/errors/CustomInternalError.js.map +0 -1
  44. package/lib/esm/index.js.map +0 -1
  45. package/lib/esm/interfaces/PlatformTestingSdkOpts.js.map +0 -1
  46. package/lib/esm/interfaces/index.js.map +0 -1
  47. package/lib/esm/modules/feature/FeatureModule.js.map +0 -1
  48. package/lib/esm/modules/feature/controllers/FeatureController.js.map +0 -1
  49. package/lib/esm/tests/exports.js.map +0 -1
  50. package/lib/esm/tests/testAcceptMime.js.map +0 -1
  51. package/lib/esm/tests/testAuth.js.map +0 -1
  52. package/lib/esm/tests/testBodyParams.js.map +0 -1
  53. package/lib/esm/tests/testCache.js.map +0 -1
  54. package/lib/esm/tests/testChildrenControllers.js.map +0 -1
  55. package/lib/esm/tests/testCookies.js.map +0 -1
  56. package/lib/esm/tests/testCustom404.js.map +0 -1
  57. package/lib/esm/tests/testDeepQueryParams.js.map +0 -1
  58. package/lib/esm/tests/testErrors.js.map +0 -1
  59. package/lib/esm/tests/testHandlers.js.map +0 -1
  60. package/lib/esm/tests/testHeaderParams.js.map +0 -1
  61. package/lib/esm/tests/testHeaders.js.map +0 -1
  62. package/lib/esm/tests/testInheritanceController.js.map +0 -1
  63. package/lib/esm/tests/testLocals.js.map +0 -1
  64. package/lib/esm/tests/testLocation.js.map +0 -1
  65. package/lib/esm/tests/testMiddlewares.js.map +0 -1
  66. package/lib/esm/tests/testModule.js.map +0 -1
  67. package/lib/esm/tests/testMulter.js.map +0 -1
  68. package/lib/esm/tests/testPathParams.js.map +0 -1
  69. package/lib/esm/tests/testQueryParams.js.map +0 -1
  70. package/lib/esm/tests/testRedirect.js.map +0 -1
  71. package/lib/esm/tests/testResponse.js.map +0 -1
  72. package/lib/esm/tests/testResponseFilter.js.map +0 -1
  73. package/lib/esm/tests/testRouting.js.map +0 -1
  74. package/lib/esm/tests/testScopeRequest.js.map +0 -1
  75. package/lib/esm/tests/testSession.js.map +0 -1
  76. package/lib/esm/tests/testStatics.js.map +0 -1
  77. package/lib/esm/tests/testStream.js.map +0 -1
  78. package/lib/esm/tests/testView.js.map +0 -1
  79. package/src/components/PlatformTestSdk.ts +0 -30
  80. package/src/data/file.txt +0 -1
  81. package/src/data/response.data.json +0 -3
  82. package/src/data/swagger.json +0 -32
  83. package/src/errors/CustomBadRequest.ts +0 -16
  84. package/src/errors/CustomInternalError.ts +0 -16
  85. package/src/index.ts +0 -40
  86. package/src/interfaces/PlatformTestingSdkOpts.ts +0 -10
  87. package/src/interfaces/index.ts +0 -4
  88. package/src/modules/feature/FeatureModule.ts +0 -10
  89. package/src/modules/feature/controllers/FeatureController.ts +0 -11
  90. package/src/tests/exports.ts +0 -71
  91. package/src/tests/testAcceptMime.ts +0 -99
  92. package/src/tests/testAuth.ts +0 -345
  93. package/src/tests/testBodyParams.ts +0 -525
  94. package/src/tests/testCache.ts +0 -283
  95. package/src/tests/testChildrenControllers.ts +0 -59
  96. package/src/tests/testCookies.ts +0 -41
  97. package/src/tests/testCustom404.ts +0 -35
  98. package/src/tests/testDeepQueryParams.ts +0 -125
  99. package/src/tests/testErrors.ts +0 -233
  100. package/src/tests/testHandlers.ts +0 -94
  101. package/src/tests/testHeaderParams.ts +0 -158
  102. package/src/tests/testHeaders.ts +0 -78
  103. package/src/tests/testInheritanceController.ts +0 -206
  104. package/src/tests/testLocals.ts +0 -53
  105. package/src/tests/testLocation.ts +0 -40
  106. package/src/tests/testMiddlewares.ts +0 -155
  107. package/src/tests/testModule.ts +0 -50
  108. package/src/tests/testMulter.ts +0 -186
  109. package/src/tests/testPathParams.ts +0 -127
  110. package/src/tests/testQueryParams.ts +0 -238
  111. package/src/tests/testRedirect.ts +0 -129
  112. package/src/tests/testResponse.ts +0 -573
  113. package/src/tests/testResponseFilter.ts +0 -106
  114. package/src/tests/testRouting.ts +0 -71
  115. package/src/tests/testScopeRequest.ts +0 -116
  116. package/src/tests/testSession.ts +0 -109
  117. package/src/tests/testStatics.ts +0 -64
  118. package/src/tests/testStream.ts +0 -63
  119. package/src/tests/testView.ts +0 -75
@@ -1,71 +0,0 @@
1
- import {Controller} from "@tsed/di";
2
- import {PlatformTest} from "@tsed/platform-http/testing";
3
- import {Get, Put} from "@tsed/schema";
4
- import SuperTest from "supertest";
5
- import {afterAll, afterEach, beforeAll, expect, it, vi} from "vitest";
6
-
7
- import {PlatformTestingSdkOpts} from "../interfaces/index.js";
8
-
9
- const stub = vi.fn();
10
-
11
- @Controller("/routing")
12
- export class TestRoutingController {
13
- @Get("/1")
14
- scenario1() {
15
- stub("scenario1");
16
-
17
- return "test1";
18
- }
19
-
20
- @Put("") // empty string has an effect on the calls orders
21
- scenario2() {
22
- stub("scenario2");
23
-
24
- return "shouldNotRun";
25
- }
26
-
27
- @Get("/3")
28
- scenario3() {
29
- stub("scenario3");
30
-
31
- return "test3";
32
- }
33
- }
34
-
35
- export function testRouting(options: PlatformTestingSdkOpts) {
36
- let request: SuperTest.Agent;
37
-
38
- beforeAll(
39
- PlatformTest.bootstrap(options.server, {
40
- ...options,
41
- mount: {
42
- "/rest": [TestRoutingController]
43
- }
44
- })
45
- );
46
- beforeAll(() => {
47
- request = SuperTest(PlatformTest.callback());
48
- });
49
- afterAll(PlatformTest.reset);
50
- afterEach(() => {
51
- vi.resetAllMocks();
52
- });
53
-
54
- it("Scenario1: should call scenario1 only", async () => {
55
- const {text} = await request.get("/rest/routing/1").expect(200);
56
- expect(text).toEqual("test1");
57
- expect(stub).toHaveBeenCalledWith("scenario1");
58
- });
59
-
60
- it("Scenario2: should call scenario2 only", async () => {
61
- const {text} = await request.put("/rest/routing/").expect(200);
62
- expect(text).toEqual("shouldNotRun");
63
- expect(stub).toHaveBeenCalledWith("scenario2");
64
- });
65
-
66
- it("Scenario3: should call scenario3 only", async () => {
67
- const {text} = await request.get("/rest/routing/3").expect(200);
68
- expect(text).toEqual("test3");
69
- expect(stub).toHaveBeenCalledWith("scenario3");
70
- });
71
- }
@@ -1,116 +0,0 @@
1
- import {Controller, ProviderScope, Scope, Service} from "@tsed/di";
2
- import {PlatformTest} from "@tsed/platform-http/testing";
3
- import {PathParams} from "@tsed/platform-params";
4
- import {Get} from "@tsed/schema";
5
- import SuperTest from "supertest";
6
- import {afterAll, beforeAll, describe, expect, it} from "vitest";
7
-
8
- import {PlatformTestingSdkOpts} from "../interfaces/index.js";
9
-
10
- @Service()
11
- @Scope(ProviderScope.REQUEST)
12
- export class ScopeRequestService {
13
- user: string;
14
- }
15
-
16
- @Controller("/scopes-request")
17
- @Scope(ProviderScope.REQUEST)
18
- export class ScopeRequestCtrl {
19
- userId: string | null;
20
-
21
- constructor(public scopeRequestService: ScopeRequestService) {}
22
-
23
- $onDestroy() {
24
- this.userId = null;
25
- }
26
-
27
- @Get("/:id")
28
- testPerRequest(@PathParams("id") userId: string): Promise<any> {
29
- this.scopeRequestService.user = userId;
30
- this.userId = userId;
31
-
32
- return new Promise((resolve, reject) => {
33
- if (userId === "0") {
34
- setTimeout(() => {
35
- resolve({userId, idSrv: this.scopeRequestService.user, idCtrl: this.userId});
36
- }, 500);
37
- }
38
-
39
- if (userId === "1") {
40
- setTimeout(() => {
41
- resolve({userId, idSrv: this.scopeRequestService.user, idCtrl: this.userId});
42
- }, 300);
43
- }
44
-
45
- if (userId === "2") {
46
- setTimeout(() => {
47
- resolve({userId, idSrv: this.scopeRequestService.user, idCtrl: this.userId});
48
- }, 150);
49
- }
50
- });
51
- }
52
- }
53
-
54
- export function testScopeRequest(options: PlatformTestingSdkOpts) {
55
- let request: SuperTest.Agent;
56
-
57
- beforeAll(
58
- PlatformTest.bootstrap(options.server, {
59
- ...options,
60
- mount: {
61
- "/rest": [ScopeRequestCtrl]
62
- }
63
- })
64
- );
65
- beforeAll(() => {
66
- request = SuperTest(PlatformTest.callback());
67
- });
68
- afterAll(PlatformTest.reset);
69
-
70
- describe("GET /rest/scopes-request/:id", () => {
71
- const send = (id: string) =>
72
- new Promise((resolve, reject) => {
73
- request
74
- .get(`/rest/scopes-request/${id}`)
75
- .expect(200)
76
- .end((err: any, response: any) => {
77
- if (err) {
78
- reject(err);
79
- } else {
80
- resolve({id, ...JSON.parse(response.text)});
81
- }
82
- });
83
- });
84
-
85
- it("should respond with the right userId per request", () => {
86
- const promises = [];
87
-
88
- promises.push(send("0"));
89
- promises.push(send("1"));
90
- promises.push(send("2"));
91
-
92
- return Promise.all(promises).then((responses) => {
93
- expect(responses).toEqual([
94
- {
95
- id: "0",
96
- idCtrl: "0",
97
- idSrv: "0",
98
- userId: "0"
99
- },
100
- {
101
- id: "1",
102
- idCtrl: "1",
103
- idSrv: "1",
104
- userId: "1"
105
- },
106
- {
107
- id: "2",
108
- idCtrl: "2",
109
- idSrv: "2",
110
- userId: "2"
111
- }
112
- ]);
113
- });
114
- });
115
- });
116
- }
@@ -1,109 +0,0 @@
1
- import {promisify} from "node:util";
2
-
3
- import {Constant, Controller} from "@tsed/di";
4
- import {NotFound} from "@tsed/exceptions";
5
- import {Req} from "@tsed/platform-http";
6
- import {PlatformTest} from "@tsed/platform-http/testing";
7
- import {BodyParams, Session} from "@tsed/platform-params";
8
- import {Allow, Email, Get, Ignore, MinLength, Post, Property, Required, Returns} from "@tsed/schema";
9
- import SuperTest from "supertest";
10
- import {afterAll, beforeAll, describe, expect, it} from "vitest";
11
-
12
- import {PlatformTestingSdkOpts} from "../interfaces/index.js";
13
-
14
- export class UserCreation {
15
- @Property()
16
- name: string;
17
-
18
- @Required()
19
- @Email()
20
- @Allow(null)
21
- email: string;
22
-
23
- @Required()
24
- @MinLength(6)
25
- @Allow(null)
26
- password: string;
27
- }
28
-
29
- export class User extends UserCreation {
30
- @Ignore()
31
- declare password: string;
32
- }
33
-
34
- @Controller("/session")
35
- export class SessionCtrl {
36
- @Constant("PLATFORM_NAME")
37
- platformName: string;
38
-
39
- @Post("/connect")
40
- connect(@Session() session: any, @BodyParams() user: UserCreation) {
41
- session.user = user;
42
-
43
- return Promise.resolve(user);
44
- }
45
-
46
- @Get("/userinfo")
47
- @Returns(200, User)
48
- userInfo(@Session("user") user: User): Promise<User> {
49
- if (!user) {
50
- return Promise.reject(new NotFound("User not found"));
51
- }
52
-
53
- return Promise.resolve(user);
54
- }
55
-
56
- @Get("/logout")
57
- @Returns(204)
58
- logout(@Req() req: any) {
59
- switch (this.platformName) {
60
- case "koa":
61
- return Promise.resolve((req.ctx.session = null));
62
- case "express":
63
- delete req.session.user;
64
-
65
- return promisify(req.session.destroy.bind(req.session))();
66
- }
67
-
68
- return Promise.resolve();
69
- }
70
- }
71
-
72
- export function testSession(options: PlatformTestingSdkOpts) {
73
- let request: SuperTest.Agent;
74
- beforeAll(
75
- PlatformTest.bootstrap(options.server, {
76
- ...options,
77
- mount: {
78
- "/rest": [SessionCtrl]
79
- }
80
- })
81
- );
82
- beforeAll(() => {
83
- request = SuperTest.agent(PlatformTest.callback());
84
- });
85
- afterAll(PlatformTest.reset);
86
-
87
- describe("Scenario1: POST /rest/session/connected", () => {
88
- it("should keep connected user in session and destroy session", async () => {
89
- await request.get("/rest/session/userinfo").expect(404);
90
-
91
- await request.post("/rest/session/connect").send({
92
- name: "name",
93
- email: "test@test.fr",
94
- password: "password1234"
95
- });
96
-
97
- // @ts-ignore
98
- const response = await request.get("/rest/session/userinfo").expect(200);
99
-
100
- expect(response.body).toEqual({
101
- email: "test@test.fr",
102
- name: "name"
103
- });
104
-
105
- await request.get("/rest/session/logout").expect(204);
106
- await request.get("/rest/session/userinfo").expect(404);
107
- });
108
- });
109
- }
@@ -1,64 +0,0 @@
1
- import {readFileSync} from "node:fs";
2
-
3
- import {getValue} from "@tsed/core";
4
- import {Configuration, Controller} from "@tsed/di";
5
- import {PlatformTest} from "@tsed/platform-http/testing";
6
- import {PlatformRouter} from "@tsed/platform-router";
7
- import SuperTest from "supertest";
8
- import {afterEach, beforeEach, expect, it} from "vitest";
9
-
10
- import {PlatformTestingSdkOpts} from "../interfaces/index.js";
11
-
12
- @Controller("/statics")
13
- class CustomStaticsCtrl {
14
- constructor(router: PlatformRouter, @Configuration() config: Configuration) {
15
- router.statics("/", {
16
- root: String(getValue(config, "statics./"))
17
- });
18
- }
19
- }
20
-
21
- export function testStatics(options: PlatformTestingSdkOpts) {
22
- let request: SuperTest.Agent;
23
- beforeEach(
24
- PlatformTest.bootstrap(options.server, {
25
- ...options,
26
- mount: {
27
- "/rest": [CustomStaticsCtrl]
28
- }
29
- })
30
- );
31
- beforeEach(() => {
32
- request = SuperTest(PlatformTest.callback());
33
- });
34
- afterEach(PlatformTest.reset);
35
-
36
- it("should return index HTML (1)", async () => {
37
- const response = await request.get("/").expect(200);
38
-
39
- expect(response.text).toEqual(readFileSync(`${options.rootDir}/public/index.html`, {encoding: "utf8"}));
40
- });
41
-
42
- it("should return index HTML (2)", async () => {
43
- const response = await request.get("/index.html").expect(200);
44
-
45
- expect(response.text).toEqual(readFileSync(`${options.rootDir}/public/index.html`, {encoding: "utf8"}));
46
- });
47
-
48
- it("should return index HTML (3)", async () => {
49
- const response = await request.get("/rest/statics/index.html").expect(200);
50
-
51
- expect(response.text).toEqual(readFileSync(`${options.rootDir}/public/index.html`, {encoding: "utf8"}));
52
- });
53
-
54
- it("should return 404", async () => {
55
- const response = await request.get("/index-te.html").expect(404);
56
-
57
- expect(response.body).toEqual({
58
- errors: [],
59
- message: 'Resource "/index-te.html" not found',
60
- name: "NOT_FOUND",
61
- status: 404
62
- });
63
- });
64
- }
@@ -1,63 +0,0 @@
1
- import {createReadStream} from "node:fs";
2
- import {join} from "node:path";
3
-
4
- import {Controller} from "@tsed/di";
5
- import {PlatformTest} from "@tsed/platform-http/testing";
6
- import {ContentType, Get} from "@tsed/schema";
7
- import SuperTest from "supertest";
8
- import {afterAll, beforeAll, describe, expect, it} from "vitest";
9
-
10
- import {PlatformTestingSdkOpts} from "../interfaces/index.js";
11
-
12
- const rootDir = import.meta.dirname; // automatically replaced by import.meta.dirname on build
13
-
14
- @Controller("/stream")
15
- class TestStreamCtrl {
16
- @Get("/scenario1")
17
- @ContentType("application/json")
18
- testScenario1() {
19
- return createReadStream(join(rootDir, "../data/response.data.json"), "utf-8");
20
- }
21
-
22
- @Get("/scenario1b")
23
- @ContentType("application/json")
24
- testScenario2() {
25
- return Promise.resolve(createReadStream(join(rootDir, "../data/response.data.json"), "utf-8"));
26
- }
27
- }
28
-
29
- export function testStream(options: PlatformTestingSdkOpts) {
30
- let request: SuperTest.Agent;
31
-
32
- beforeAll(
33
- PlatformTest.bootstrap(options.server, {
34
- ...options,
35
- mount: {
36
- "/rest": [TestStreamCtrl]
37
- }
38
- })
39
- );
40
- beforeAll(() => {
41
- request = SuperTest(PlatformTest.callback());
42
- });
43
- afterAll(PlatformTest.reset);
44
-
45
- describe("Scenario1: when endpoint return a stream", () => {
46
- describe("GET /rest/stream/scenario1", () => {
47
- it("should return a body", async () => {
48
- const response = await request.get("/rest/stream/scenario1");
49
-
50
- expect(response.headers["content-type"]).toContain("application/json");
51
- expect(response.body).toEqual({id: "1"});
52
- });
53
- });
54
-
55
- describe("GET /rest/stream/scenario1b", () => {
56
- it("should return a body", async () => {
57
- const response = await request.get("/rest/stream/scenario1b");
58
-
59
- expect(response.body).toEqual({id: "1"});
60
- });
61
- });
62
- });
63
- }
@@ -1,75 +0,0 @@
1
- import {EOL} from "node:os";
2
-
3
- import {Controller} from "@tsed/di";
4
- import {PlatformTest} from "@tsed/platform-http/testing";
5
- import {Middleware, UseBefore} from "@tsed/platform-middlewares";
6
- import {Locals} from "@tsed/platform-params";
7
- import {View} from "@tsed/platform-views";
8
- import {Get} from "@tsed/schema";
9
- import SuperTest from "supertest";
10
- import {afterAll, beforeAll, describe, expect, it} from "vitest";
11
-
12
- import {PlatformTestingSdkOpts} from "../interfaces/index.js";
13
-
14
- @Middleware()
15
- class LocalsMiddleware {
16
- use(@Locals() locals: any) {
17
- locals.localID = "local-10909";
18
- }
19
- }
20
-
21
- @Controller("/views")
22
- class ViewCtrl {
23
- @Get("/scenario-1")
24
- @UseBefore(LocalsMiddleware)
25
- @View("view.ejs", {opts: "opts"})
26
- testScenario1() {
27
- return {
28
- world: "world"
29
- };
30
- }
31
-
32
- @Get("/scenario-2")
33
- @View("view", {opts: "opts"}) // missing extension
34
- testScenario2() {
35
- return {
36
- world: "world"
37
- };
38
- }
39
- }
40
-
41
- export function testView(options: PlatformTestingSdkOpts) {
42
- let request: SuperTest.Agent;
43
-
44
- beforeAll(
45
- PlatformTest.bootstrap(options.server, {
46
- ...options,
47
- mount: {
48
- "/rest": [ViewCtrl]
49
- }
50
- })
51
- );
52
- beforeAll(() => {
53
- request = SuperTest(PlatformTest.callback());
54
- });
55
- afterAll(() => PlatformTest.reset());
56
-
57
- describe("Scenario1: GET /rest/views/scenario-1", () => {
58
- it("should render a view", async () => {
59
- const response = await request.get("/rest/views/scenario-1").expect(200);
60
-
61
- expect(response.headers["content-type"]).toContain("text/html");
62
- expect(response.text).toEqual(`<p>Hello world with opts and ID local-10909</p>${EOL}`);
63
- });
64
- });
65
-
66
- describe("Scenario2: GET /rest/views/scenario-2", () => {
67
- it("should throw an error when extension is not defined", async () => {
68
- const response = await request.get("/rest/views/scenario-2").expect(500);
69
-
70
- expect(response.body.name).toEqual("TEMPLATE_RENDER_ERROR");
71
- expect(response.body.message).toContain("Template rendering error: ViewCtrl.testScenario2()");
72
- expect(response.body.status).toEqual(500);
73
- });
74
- });
75
- }