@tsed/platform-test-sdk 8.11.1 → 8.11.2-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,206 +0,0 @@
1
- import {Controller} from "@tsed/di";
2
- import {NotFound} from "@tsed/exceptions";
3
- import {PlatformTest} from "@tsed/platform-http/testing";
4
- import {Middleware, UseAuth} from "@tsed/platform-middlewares";
5
- import {BodyParams, PathParams, QueryParams} from "@tsed/platform-params";
6
- import {Description, Get, MaxLength, MinLength, Post, Property, Returns, Summary} from "@tsed/schema";
7
- import SuperTest from "supertest";
8
- import {v4} from "uuid";
9
- import {afterAll, beforeAll, describe, expect, it} from "vitest";
10
-
11
- import {PlatformTestingSdkOpts} from "../interfaces/index.js";
12
-
13
- export class Resource {
14
- @Property()
15
- id: string;
16
-
17
- @Property()
18
- @MinLength(3)
19
- @MaxLength(100)
20
- name: string;
21
- }
22
-
23
- export class BaseController<T extends {id: string}> {
24
- resources: T[];
25
-
26
- @Get("/:id")
27
- @Summary("Return an element by his resource")
28
- get(
29
- @Description("Id of the resource")
30
- @PathParams("id")
31
- id: string
32
- ): Promise<any> {
33
- const resource = this.resources.find((resource) => resource.id === id);
34
-
35
- if (!resource) {
36
- return Promise.reject(new NotFound("Not found"));
37
- }
38
-
39
- return Promise.resolve(resource);
40
- }
41
-
42
- @Get("/")
43
- @Summary("Return all elements from a service")
44
- list(): Promise<any[]> {
45
- return Promise.resolve(this.resources);
46
- }
47
- }
48
-
49
- @Controller("/resources")
50
- export class ResourcesCtrl extends BaseController<Resource> {
51
- resources = [{id: "1", name: "John"}];
52
-
53
- @Get("/:id")
54
- @Summary("Return an element by his resource")
55
- async get(
56
- @Description("Id of the resource")
57
- @PathParams("id")
58
- id: string
59
- ): Promise<any> {
60
- const resource = await super.get(id);
61
-
62
- resource.name = resource.name + " hello You!";
63
-
64
- return resource;
65
- }
66
-
67
- @Post("/")
68
- @(Returns(201).Type(Resource))
69
- post(@BodyParams() resource: Resource) {
70
- resource.id = v4();
71
- this.resources.push(resource);
72
-
73
- return resource;
74
- }
75
- }
76
-
77
- @Middleware()
78
- class AuthMiddleware {
79
- use() {
80
- return true;
81
- }
82
- }
83
-
84
- abstract class AttachmentController {
85
- @Get("/:parentID/attachments")
86
- getAll(@PathParams("parentID") parentID: string) {
87
- return `All attachments of ${parentID}`;
88
- }
89
- }
90
-
91
- @Controller("/findings")
92
- @UseAuth(AuthMiddleware)
93
- export class FindingsController extends AttachmentController {
94
- @Get("/")
95
- get() {
96
- return "hello Finding";
97
- }
98
- }
99
-
100
- export abstract class TestBaseController {
101
- @Get("/")
102
- scenario3(@QueryParams("search") search: string) {
103
- return {
104
- search
105
- };
106
- }
107
-
108
- @Get("/override")
109
- scenario5(@QueryParams("q") q: string) {
110
- return {data: q};
111
- }
112
- }
113
-
114
- @Controller("/test")
115
- export class TestChildController extends TestBaseController {
116
- @Get("/:id")
117
- scenario4(@PathParams("id") id: string): any {
118
- return {id};
119
- }
120
-
121
- @Get("/override")
122
- scenario5(@QueryParams("s") s: string) {
123
- return {data: s};
124
- }
125
- }
126
-
127
- export function testInheritanceController(options: PlatformTestingSdkOpts) {
128
- let request: SuperTest.Agent;
129
-
130
- beforeAll(
131
- PlatformTest.bootstrap(options.server, {
132
- ...options,
133
- mount: {
134
- "/rest": [ResourcesCtrl, FindingsController, TestChildController]
135
- }
136
- })
137
- );
138
- beforeAll(() => {
139
- request = SuperTest(PlatformTest.callback());
140
- });
141
- afterAll(PlatformTest.reset);
142
-
143
- describe("Scenario 1:", () => {
144
- it("should return list", async () => {
145
- const {body} = await request.get("/rest/resources").expect(200);
146
-
147
- expect(body).toEqual([{id: "1", name: "John"}]);
148
- });
149
-
150
- it("should return a resource", async () => {
151
- const {body} = await request.get("/rest/resources/1").expect(200);
152
-
153
- expect(body).toEqual({
154
- id: "1",
155
- name: "John hello You!"
156
- });
157
- });
158
-
159
- it("should add a resource", async () => {
160
- const {body} = await request
161
- .post("/rest/resources")
162
- .send({
163
- name: "july"
164
- })
165
- .expect(201);
166
-
167
- expect(body.name).toEqual("july");
168
- expect(typeof body.id).toBe("string");
169
-
170
- const {body: resource} = await request.get(`/rest/resources/${body.id}`).expect(200);
171
-
172
- expect(resource.id).toEqual(body.id);
173
- });
174
- });
175
-
176
- describe("scenario2: FindingsController", () => {
177
- it("should call /rest/findings/:parentID/attachments", async () => {
178
- const {text} = await request.get("/rest/findings/1/attachments").expect(200);
179
-
180
- expect(text).toEqual("All attachments of 1");
181
- });
182
-
183
- it("should call /rest/findings", async () => {
184
- const {text} = await request.get("/rest/findings").expect(200);
185
-
186
- expect(text).toEqual("hello Finding");
187
- });
188
- });
189
-
190
- it("Scenario3: should call inherited method", async () => {
191
- const {body} = await request.get("/rest/test?search=test").expect(200);
192
- expect(body).toEqual({
193
- search: "test"
194
- });
195
- });
196
-
197
- it("Scenario4: should the Child method", async () => {
198
- const {body} = await request.get("/rest/test/1").expect(200);
199
- expect(body).toEqual({id: "1"});
200
- });
201
-
202
- it("Scenario5: should call the Child method and not the base method", async () => {
203
- const {body} = await request.get("/rest/test/1").expect(200);
204
- expect(body).toEqual({id: "1"});
205
- });
206
- }
@@ -1,53 +0,0 @@
1
- import {Controller} from "@tsed/di";
2
- import {PlatformContext, Req} from "@tsed/platform-http";
3
- import {PlatformTest} from "@tsed/platform-http/testing";
4
- import {Middleware, Use} from "@tsed/platform-middlewares";
5
- import {Context, Locals} from "@tsed/platform-params";
6
- import {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
- @Middleware()
13
- class LocalsMiddleware {
14
- use(@Req() request: any, @Locals() locals: any, @Context() ctx: PlatformContext) {
15
- locals.id = "local-10909";
16
- ctx.set("uid", "ctx-10909");
17
- }
18
- }
19
-
20
- @Controller("/locals")
21
- class LocalsCtrl {
22
- @Get("/scenario-1")
23
- @Use(LocalsMiddleware)
24
- public testStackMiddlewares(@Locals("id") id: any, @Context("uid") uid: string): any {
25
- return {
26
- id: `${id}-${uid}`
27
- };
28
- }
29
- }
30
-
31
- export function testLocals(options: PlatformTestingSdkOpts) {
32
- let request: SuperTest.Agent;
33
- beforeAll(
34
- PlatformTest.bootstrap(options.server, {
35
- ...options,
36
- mount: {
37
- "/rest": [LocalsCtrl]
38
- }
39
- })
40
- );
41
- beforeAll(() => {
42
- request = SuperTest(PlatformTest.callback());
43
- });
44
- afterAll(PlatformTest.reset);
45
-
46
- describe("Scenario 1: GET /rest/locals/scenario-1", () => {
47
- it("should call middleware and set a id in locals", async () => {
48
- const {body}: any = await request.get("/rest/locals/scenario-1").expect(200);
49
-
50
- expect(body.id).toEqual("local-10909-ctx-10909");
51
- });
52
- });
53
- }
@@ -1,40 +0,0 @@
1
- import {Controller} from "@tsed/di";
2
- import {PlatformTest} from "@tsed/platform-http/testing";
3
- import {Get, Location} from "@tsed/schema";
4
- import SuperTest from "supertest";
5
- import {afterAll, beforeAll, expect, it} from "vitest";
6
-
7
- import {PlatformTestingSdkOpts} from "../interfaces/index.js";
8
-
9
- @Controller("/location")
10
- class LocationCtrl {
11
- @Get("/scenario-1")
12
- @Location("/test")
13
- testScenario1() {
14
- return "Hello";
15
- }
16
- }
17
-
18
- export function testLocation(options: PlatformTestingSdkOpts) {
19
- let request: SuperTest.Agent;
20
-
21
- beforeAll(
22
- PlatformTest.bootstrap(options.server, {
23
- ...options,
24
- mount: {
25
- "/rest": [LocationCtrl]
26
- }
27
- })
28
- );
29
- beforeAll(() => {
30
- request = SuperTest(PlatformTest.callback());
31
- });
32
- afterAll(PlatformTest.reset);
33
-
34
- it("Scenario1: GET /rest/location/scenario-1", async () => {
35
- const response = await request.get("/rest/location/scenario-1").expect(200);
36
-
37
- expect(response.text).toEqual("Hello");
38
- expect(response.header.location).toEqual("/test");
39
- });
40
- }
@@ -1,155 +0,0 @@
1
- import "@tsed/ajv";
2
-
3
- import {Controller} from "@tsed/di";
4
- import {BadRequest, Exception, Unauthorized} from "@tsed/exceptions";
5
- import {Err} from "@tsed/platform-http";
6
- import {PlatformTest} from "@tsed/platform-http/testing";
7
- import {Middleware, MiddlewareMethods, Use, UseAfter, UseBefore} from "@tsed/platform-middlewares";
8
- import {Context, QueryParams} from "@tsed/platform-params";
9
- import {Get} from "@tsed/schema";
10
- import SuperTest from "supertest";
11
- import {afterAll, beforeAll, describe, expect, it} from "vitest";
12
-
13
- import {PlatformTestingSdkOpts} from "../interfaces/index.js";
14
-
15
- @Middleware()
16
- class BeforeCustomMiddleware {
17
- use(@Context() ctx: Context) {
18
- ctx.set("stacks", ["UseBefore - Ctrl"]);
19
- }
20
- }
21
-
22
- @Middleware()
23
- class BeforeEndpointCustomMiddleware {
24
- use(@Context() ctx: Context) {
25
- ctx.get("stacks").push("UseBefore - endpoint");
26
- }
27
- }
28
-
29
- @Middleware()
30
- class AfterCustomMiddleware {
31
- use(@Context() ctx: Context) {
32
- ctx.get("stacks").push("UseAfter - Ctrl");
33
- }
34
- }
35
-
36
- @Middleware()
37
- class UseCustomMiddleware {
38
- use(@Context() ctx: Context) {
39
- ctx.get("stacks").push("Use - Ctrl");
40
- }
41
- }
42
-
43
- @Middleware()
44
- class AfterEndpointCustomMiddleware {
45
- use(@Context() ctx: Context) {
46
- ctx.get("stacks").push("UseAfter - endpoint");
47
- }
48
- }
49
-
50
- @Middleware()
51
- class UseMiddleware {
52
- use(@Context() ctx: Context) {
53
- ctx.get("stacks").push("Use - Ctrl");
54
- }
55
- }
56
-
57
- @Middleware()
58
- class UseEndpointMiddleware {
59
- use(@Context() ctx: Context) {
60
- ctx.get("stacks").push("Use - endpoint");
61
- }
62
- }
63
-
64
- @Middleware()
65
- export class CatchErrorMiddleware implements MiddlewareMethods {
66
- use(@Err() err: Exception) {
67
- if ([401].includes(err.status)) {
68
- throw new BadRequest("Bad format");
69
- }
70
-
71
- throw err;
72
- }
73
- }
74
-
75
- @Controller("/middlewares")
76
- @UseBefore(BeforeCustomMiddleware)
77
- @UseAfter(AfterCustomMiddleware)
78
- @UseAfter(CatchErrorMiddleware)
79
- @Use(UseCustomMiddleware)
80
- class TestMiddlewaresCtrl {
81
- @Get("/scenario-1")
82
- @UseBefore(BeforeEndpointCustomMiddleware)
83
- @UseAfter(AfterEndpointCustomMiddleware)
84
- @Use(UseEndpointMiddleware)
85
- scenario1(@Context() context: Context) {
86
- context.get("stacks").push("endpoint");
87
-
88
- return {stacks: context.get("stacks")};
89
- }
90
-
91
- @Get("/scenario-2")
92
- scenario2(@QueryParams("test") test: string) {
93
- if (test === "error") {
94
- throw new Unauthorized("Unauthorized");
95
- }
96
-
97
- return {
98
- hello: "world"
99
- };
100
- }
101
- }
102
-
103
- export function testMiddlewares(options: PlatformTestingSdkOpts) {
104
- let request: SuperTest.Agent;
105
-
106
- beforeAll(
107
- PlatformTest.bootstrap(options.server, {
108
- ...options,
109
- mount: {
110
- "/rest": [TestMiddlewaresCtrl]
111
- }
112
- })
113
- );
114
- beforeAll(() => {
115
- request = SuperTest(PlatformTest.callback());
116
- });
117
- afterAll(PlatformTest.reset);
118
-
119
- describe("Scenario 1: GET /rest/middlewares/scenario-1", () => {
120
- it("should call all middlewares", async () => {
121
- const response = await request.get("/rest/middlewares/scenario-1").expect(200);
122
-
123
- expect(response.body).toEqual({
124
- stacks: [
125
- "UseBefore - Ctrl",
126
- "UseBefore - endpoint",
127
- "Use - Ctrl",
128
- "Use - endpoint",
129
- "endpoint",
130
- "UseAfter - endpoint",
131
- "UseAfter - Ctrl"
132
- ]
133
- });
134
- });
135
- });
136
- describe("Scenario 2: GET /rest/middlewares/scenario-2", () => {
137
- it("should call the endpoint without triggering middleware", async () => {
138
- const response = await request.get("/rest/middlewares/scenario-2").expect(200);
139
-
140
- expect(response.body).toEqual({
141
- hello: "world"
142
- });
143
- });
144
- it("should call the endpoint, throw an error and transform the error by calling the error middleware", async () => {
145
- const response = await request.get("/rest/middlewares/scenario-2?test=error").expect(400);
146
-
147
- expect(response.body).toEqual({
148
- errors: [],
149
- message: "Bad format",
150
- name: "BAD_REQUEST",
151
- status: 400
152
- });
153
- });
154
- });
155
- }
@@ -1,50 +0,0 @@
1
- import {Controller} from "@tsed/di";
2
- import {PlatformTest} from "@tsed/platform-http/testing";
3
- import {Get} from "@tsed/schema";
4
- import SuperTest from "supertest";
5
- import {afterAll, beforeAll, describe, expect, it} from "vitest";
6
-
7
- import {PlatformTestingSdkOpts} from "../interfaces/index.js";
8
- import {FeatureModule} from "../modules/feature/FeatureModule.js";
9
-
10
- @Controller("/root")
11
- class TestRootCtrl {
12
- @Get("/")
13
- testScenario1() {
14
- return "From root";
15
- }
16
- }
17
-
18
- export function testModule(options: PlatformTestingSdkOpts) {
19
- let request: SuperTest.Agent;
20
-
21
- beforeAll(
22
- PlatformTest.bootstrap(options.server, {
23
- ...options,
24
- mount: {
25
- "/rest": [TestRootCtrl]
26
- },
27
- imports: [FeatureModule]
28
- })
29
- );
30
- beforeAll(() => {
31
- request = SuperTest(PlatformTest.callback());
32
- });
33
- afterAll(PlatformTest.reset);
34
-
35
- describe("Scenario1: GET /rest/root", () => {
36
- it("should get content from root controller", async () => {
37
- const response = await request.get("/rest/root").expect(200);
38
-
39
- expect(response.text).toEqual("From root");
40
- });
41
- });
42
-
43
- describe("Scenario2: GET /rest/features", () => {
44
- it("should get content from a module with his controller", async () => {
45
- const response = await request.get("/rest/features").expect(200);
46
-
47
- expect(response.text).toEqual("From feature");
48
- });
49
- });
50
- }
@@ -1,186 +0,0 @@
1
- import {dirname, join} from "node:path";
2
-
3
- import {Controller} from "@tsed/di";
4
- import {MulterOptions, MultipartFile, PlatformMulterFile} from "@tsed/platform-http";
5
- import {PlatformTest} from "@tsed/platform-http/testing";
6
- import {BodyParams} from "@tsed/platform-params";
7
- import {CollectionOf, Post, Property, Required, Status} from "@tsed/schema";
8
- import multer, {FileFilterCallback} from "multer";
9
- import readPkgUp from "read-pkg-up";
10
- import SuperTest from "supertest";
11
- import {afterAll, beforeAll, describe, expect, it, vi} from "vitest";
12
-
13
- import {PlatformTestingSdkOpts} from "../interfaces/index.js";
14
-
15
- const rootDir = import.meta.dirname; // automatically replaced by import.meta.dirname on build
16
-
17
- export class Task {
18
- @Property()
19
- id: string;
20
-
21
- @Property()
22
- title: string;
23
- }
24
-
25
- export class Event {
26
- @Property()
27
- @Required()
28
- id: string;
29
-
30
- @CollectionOf(Task)
31
- tasks: Task[];
32
- }
33
-
34
- const fileFilterStub = vi.fn();
35
- const fileFilter = (req: any, file: PlatformMulterFile, callback: FileFilterCallback) => {
36
- fileFilterStub();
37
- callback(null, true);
38
- };
39
-
40
- function getFileConfig(): any {
41
- return {
42
- storage: multer.diskStorage({
43
- destination: (req: any, _fileItem, cb) => {
44
- const path = `${rootDir}/../.tmp`;
45
- cb(null, path);
46
- }
47
- }),
48
- fileFilter
49
- };
50
- }
51
-
52
- @Controller("/multer")
53
- export class TestMulterController {
54
- @Post("/scenario-1")
55
- @Status(201)
56
- @MulterOptions({dest: `${rootDir}/../.tmp`})
57
- uploadWithName(@Required() @MultipartFile("media") media: PlatformMulterFile) {
58
- return media.originalname;
59
- }
60
-
61
- @Post("/scenario-2")
62
- @Status(201)
63
- @MulterOptions({dest: `${rootDir}/../.tmp`})
64
- uploadWithPayload(@MultipartFile("media") media: PlatformMulterFile, @BodyParams("form_id") formId: string) {
65
- return {
66
- file: media.originalname,
67
- formId
68
- };
69
- }
70
-
71
- @Post("/scenario-3")
72
- @Status(201)
73
- @MulterOptions(getFileConfig())
74
- uploadWithOptions(@MultipartFile("media") media: PlatformMulterFile) {
75
- return {
76
- file: media.originalname
77
- };
78
- }
79
-
80
- @Post("/scenario-4")
81
- @Status(201)
82
- uploadTwoFiles(@MultipartFile("media1") media: PlatformMulterFile, @MultipartFile("media2") media2: PlatformMulterFile) {
83
- return {
84
- file1: media.originalname,
85
- file2: media2.originalname
86
- };
87
- }
88
- }
89
-
90
- export function testMulter(options: PlatformTestingSdkOpts) {
91
- const pkg = readPkgUp.sync({
92
- cwd: import.meta.dirname
93
- });
94
- const rootDir = join(dirname(pkg?.path || ""), "src");
95
-
96
- beforeAll(
97
- PlatformTest.bootstrap(options.server, {
98
- ...options,
99
- mount: {
100
- "/rest": [TestMulterController]
101
- }
102
- })
103
- );
104
- beforeAll(() => {});
105
- afterAll(PlatformTest.reset);
106
- beforeAll(() => {
107
- vi.resetAllMocks();
108
- });
109
- describe("Scenario 1: POST /rest/multer/scenario-1", () => {
110
- it("should upload file with multer", async () => {
111
- const request = SuperTest(PlatformTest.callback());
112
- const result = await request.post("/rest/multer/scenario-1").attach("media", `${rootDir}/data/file.txt`).expect(201);
113
-
114
- expect(result.text).toEqual("file.txt");
115
- });
116
-
117
- it("should throw an exception when there is no file", async () => {
118
- const request = SuperTest(PlatformTest.callback());
119
- const result = await request.post("/rest/multer/scenario-1").expect(400);
120
-
121
- expect(result.body).toEqual({
122
- name: "REQUIRED_VALIDATION_ERROR",
123
- message: "Bad request on parameter \"request.files.media.0\".\nIt should have required parameter 'media.0'",
124
- status: 400,
125
- errors: [
126
- {
127
- dataPath: ".media.0",
128
- requestPath: "files",
129
- keyword: "required",
130
- message: "It should have required parameter 'media.0'",
131
- modelName: "files",
132
- params: {missingProperty: "media.0"},
133
- schemaPath: "#/required"
134
- }
135
- ]
136
- });
137
- });
138
- });
139
- describe("Scenario 2: POST /rest/multer/scenario-2", () => {
140
- it("should upload file with multer", async () => {
141
- const request = SuperTest(PlatformTest.callback());
142
- const result = await request
143
- .post("/rest/multer/scenario-2")
144
- .attach("media", `${rootDir}/data/file.txt`)
145
- .field({
146
- form_id: "form_id"
147
- })
148
- .expect(201);
149
-
150
- expect(result.body).toEqual({file: "file.txt", formId: "form_id"});
151
- });
152
- });
153
- describe("Scenario 3: POST /rest/multer/scenario-3", () => {
154
- it("should upload file with multer", async () => {
155
- const request = SuperTest(PlatformTest.callback());
156
- const result = await request.post("/rest/multer/scenario-3").attach("media", `${rootDir}/data/file.txt`).field({}).expect(201);
157
-
158
- expect(result.body).toEqual({
159
- file: "file.txt"
160
- });
161
-
162
- // filterFilter is called
163
- expect(fileFilterStub).toBeCalled();
164
- });
165
- });
166
-
167
- describe("Scenario 4: POST /rest/multer/scenario-4", () => {
168
- it("should upload two files using multer", async () => {
169
- const request = SuperTest(PlatformTest.callback());
170
- const result = await request
171
- .post("/rest/multer/scenario-4")
172
- .attach("media1", `${rootDir}/data/file.txt`)
173
- .attach("media2", `${rootDir}/data/file.txt`)
174
- .field({})
175
- .expect(201);
176
-
177
- expect(result.body).toEqual({
178
- file1: "file.txt",
179
- file2: "file.txt"
180
- });
181
-
182
- // filterFilter is called
183
- expect(fileFilterStub).toBeCalled();
184
- });
185
- });
186
- }