@tsed/platform-test-sdk 7.73.2 → 7.73.3

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