@tsed/platform-test-sdk 7.12.0

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 (231) hide show
  1. package/lib/cjs/components/PlatformTestSdk.js +31 -0
  2. package/lib/cjs/components/PlatformTestSdk.js.map +1 -0
  3. package/lib/cjs/data/swagger.json +32 -0
  4. package/lib/cjs/errors/CustomBadRequest.js +18 -0
  5. package/lib/cjs/errors/CustomBadRequest.js.map +1 -0
  6. package/lib/cjs/errors/CustomInternalError.js +18 -0
  7. package/lib/cjs/errors/CustomInternalError.js.map +1 -0
  8. package/lib/cjs/index.js +42 -0
  9. package/lib/cjs/index.js.map +1 -0
  10. package/lib/cjs/interfaces/PlatformTestingSdkOpts.js +3 -0
  11. package/lib/cjs/interfaces/PlatformTestingSdkOpts.js.map +1 -0
  12. package/lib/cjs/interfaces/index.js +8 -0
  13. package/lib/cjs/interfaces/index.js.map +1 -0
  14. package/lib/cjs/modules/feature/FeatureModule.js +17 -0
  15. package/lib/cjs/modules/feature/FeatureModule.js.map +1 -0
  16. package/lib/cjs/modules/feature/controllers/FeatureController.js +23 -0
  17. package/lib/cjs/modules/feature/controllers/FeatureController.js.map +1 -0
  18. package/lib/cjs/package.json +3 -0
  19. package/lib/cjs/tests/exports.js +66 -0
  20. package/lib/cjs/tests/exports.js.map +1 -0
  21. package/lib/cjs/tests/testAcceptMime.js +103 -0
  22. package/lib/cjs/tests/testAcceptMime.js.map +1 -0
  23. package/lib/cjs/tests/testAuth.js +343 -0
  24. package/lib/cjs/tests/testAuth.js.map +1 -0
  25. package/lib/cjs/tests/testBodyParams.js +526 -0
  26. package/lib/cjs/tests/testBodyParams.js.map +1 -0
  27. package/lib/cjs/tests/testCache.js +292 -0
  28. package/lib/cjs/tests/testCache.js.map +1 -0
  29. package/lib/cjs/tests/testChildrenControllers.js +64 -0
  30. package/lib/cjs/tests/testChildrenControllers.js.map +1 -0
  31. package/lib/cjs/tests/testCookies.js +45 -0
  32. package/lib/cjs/tests/testCookies.js.map +1 -0
  33. package/lib/cjs/tests/testCtrlInheritance.js +161 -0
  34. package/lib/cjs/tests/testCtrlInheritance.js.map +1 -0
  35. package/lib/cjs/tests/testCustom404.js +32 -0
  36. package/lib/cjs/tests/testCustom404.js.map +1 -0
  37. package/lib/cjs/tests/testErrors.js +252 -0
  38. package/lib/cjs/tests/testErrors.js.map +1 -0
  39. package/lib/cjs/tests/testHandlers.js +110 -0
  40. package/lib/cjs/tests/testHandlers.js.map +1 -0
  41. package/lib/cjs/tests/testHeaderParams.js +166 -0
  42. package/lib/cjs/tests/testHeaderParams.js.map +1 -0
  43. package/lib/cjs/tests/testHeaders.js +101 -0
  44. package/lib/cjs/tests/testHeaders.js.map +1 -0
  45. package/lib/cjs/tests/testInheritanceController.js +173 -0
  46. package/lib/cjs/tests/testInheritanceController.js.map +1 -0
  47. package/lib/cjs/tests/testLocals.js +63 -0
  48. package/lib/cjs/tests/testLocals.js.map +1 -0
  49. package/lib/cjs/tests/testLocation.js +42 -0
  50. package/lib/cjs/tests/testLocation.js.map +1 -0
  51. package/lib/cjs/tests/testMiddlewares.js +210 -0
  52. package/lib/cjs/tests/testMiddlewares.js.map +1 -0
  53. package/lib/cjs/tests/testModule.js +49 -0
  54. package/lib/cjs/tests/testModule.js.map +1 -0
  55. package/lib/cjs/tests/testMulter.js +162 -0
  56. package/lib/cjs/tests/testMulter.js.map +1 -0
  57. package/lib/cjs/tests/testPathParams.js +142 -0
  58. package/lib/cjs/tests/testPathParams.js.map +1 -0
  59. package/lib/cjs/tests/testQueryParams.js +354 -0
  60. package/lib/cjs/tests/testQueryParams.js.map +1 -0
  61. package/lib/cjs/tests/testRedirect.js +143 -0
  62. package/lib/cjs/tests/testRedirect.js.map +1 -0
  63. package/lib/cjs/tests/testResponse.js +622 -0
  64. package/lib/cjs/tests/testResponse.js.map +1 -0
  65. package/lib/cjs/tests/testResponseFilter.js +107 -0
  66. package/lib/cjs/tests/testResponseFilter.js.map +1 -0
  67. package/lib/cjs/tests/testRouting.js +75 -0
  68. package/lib/cjs/tests/testRouting.js.map +1 -0
  69. package/lib/cjs/tests/testScopeRequest.js +116 -0
  70. package/lib/cjs/tests/testScopeRequest.js.map +1 -0
  71. package/lib/cjs/tests/testSession.js +128 -0
  72. package/lib/cjs/tests/testSession.js.map +1 -0
  73. package/lib/cjs/tests/testStatics.js +58 -0
  74. package/lib/cjs/tests/testStatics.js.map +1 -0
  75. package/lib/cjs/tests/testView.js +83 -0
  76. package/lib/cjs/tests/testView.js.map +1 -0
  77. package/lib/esm/components/PlatformTestSdk.js +27 -0
  78. package/lib/esm/components/PlatformTestSdk.js.map +1 -0
  79. package/lib/esm/data/swagger.json +32 -0
  80. package/lib/esm/errors/CustomBadRequest.js +14 -0
  81. package/lib/esm/errors/CustomBadRequest.js.map +1 -0
  82. package/lib/esm/errors/CustomInternalError.js +14 -0
  83. package/lib/esm/errors/CustomInternalError.js.map +1 -0
  84. package/lib/esm/index.js +39 -0
  85. package/lib/esm/index.js.map +1 -0
  86. package/lib/esm/interfaces/PlatformTestingSdkOpts.js +2 -0
  87. package/lib/esm/interfaces/PlatformTestingSdkOpts.js.map +1 -0
  88. package/lib/esm/interfaces/index.js +5 -0
  89. package/lib/esm/interfaces/index.js.map +1 -0
  90. package/lib/esm/modules/feature/FeatureModule.js +14 -0
  91. package/lib/esm/modules/feature/FeatureModule.js.map +1 -0
  92. package/lib/esm/modules/feature/controllers/FeatureController.js +20 -0
  93. package/lib/esm/modules/feature/controllers/FeatureController.js.map +1 -0
  94. package/lib/esm/package.json +3 -0
  95. package/lib/esm/tests/exports.js +63 -0
  96. package/lib/esm/tests/exports.js.map +1 -0
  97. package/lib/esm/tests/testAcceptMime.js +99 -0
  98. package/lib/esm/tests/testAcceptMime.js.map +1 -0
  99. package/lib/esm/tests/testAuth.js +338 -0
  100. package/lib/esm/tests/testAuth.js.map +1 -0
  101. package/lib/esm/tests/testBodyParams.js +522 -0
  102. package/lib/esm/tests/testBodyParams.js.map +1 -0
  103. package/lib/esm/tests/testCache.js +288 -0
  104. package/lib/esm/tests/testCache.js.map +1 -0
  105. package/lib/esm/tests/testChildrenControllers.js +60 -0
  106. package/lib/esm/tests/testChildrenControllers.js.map +1 -0
  107. package/lib/esm/tests/testCookies.js +41 -0
  108. package/lib/esm/tests/testCookies.js.map +1 -0
  109. package/lib/esm/tests/testCtrlInheritance.js +156 -0
  110. package/lib/esm/tests/testCtrlInheritance.js.map +1 -0
  111. package/lib/esm/tests/testCustom404.js +27 -0
  112. package/lib/esm/tests/testCustom404.js.map +1 -0
  113. package/lib/esm/tests/testErrors.js +248 -0
  114. package/lib/esm/tests/testErrors.js.map +1 -0
  115. package/lib/esm/tests/testHandlers.js +105 -0
  116. package/lib/esm/tests/testHandlers.js.map +1 -0
  117. package/lib/esm/tests/testHeaderParams.js +162 -0
  118. package/lib/esm/tests/testHeaderParams.js.map +1 -0
  119. package/lib/esm/tests/testHeaders.js +97 -0
  120. package/lib/esm/tests/testHeaders.js.map +1 -0
  121. package/lib/esm/tests/testInheritanceController.js +167 -0
  122. package/lib/esm/tests/testInheritanceController.js.map +1 -0
  123. package/lib/esm/tests/testLocals.js +59 -0
  124. package/lib/esm/tests/testLocals.js.map +1 -0
  125. package/lib/esm/tests/testLocation.js +38 -0
  126. package/lib/esm/tests/testLocation.js.map +1 -0
  127. package/lib/esm/tests/testMiddlewares.js +206 -0
  128. package/lib/esm/tests/testMiddlewares.js.map +1 -0
  129. package/lib/esm/tests/testModule.js +45 -0
  130. package/lib/esm/tests/testModule.js.map +1 -0
  131. package/lib/esm/tests/testMulter.js +156 -0
  132. package/lib/esm/tests/testMulter.js.map +1 -0
  133. package/lib/esm/tests/testPathParams.js +138 -0
  134. package/lib/esm/tests/testPathParams.js.map +1 -0
  135. package/lib/esm/tests/testQueryParams.js +349 -0
  136. package/lib/esm/tests/testQueryParams.js.map +1 -0
  137. package/lib/esm/tests/testRedirect.js +139 -0
  138. package/lib/esm/tests/testRedirect.js.map +1 -0
  139. package/lib/esm/tests/testResponse.js +615 -0
  140. package/lib/esm/tests/testResponse.js.map +1 -0
  141. package/lib/esm/tests/testResponseFilter.js +103 -0
  142. package/lib/esm/tests/testResponseFilter.js.map +1 -0
  143. package/lib/esm/tests/testRouting.js +71 -0
  144. package/lib/esm/tests/testRouting.js.map +1 -0
  145. package/lib/esm/tests/testScopeRequest.js +112 -0
  146. package/lib/esm/tests/testScopeRequest.js.map +1 -0
  147. package/lib/esm/tests/testSession.js +122 -0
  148. package/lib/esm/tests/testSession.js.map +1 -0
  149. package/lib/esm/tests/testStatics.js +54 -0
  150. package/lib/esm/tests/testStatics.js.map +1 -0
  151. package/lib/esm/tests/testView.js +79 -0
  152. package/lib/esm/tests/testView.js.map +1 -0
  153. package/lib/types/components/PlatformTestSdk.d.ts +9 -0
  154. package/lib/types/errors/CustomBadRequest.d.ts +8 -0
  155. package/lib/types/errors/CustomInternalError.d.ts +8 -0
  156. package/lib/types/index.d.ts +38 -0
  157. package/lib/types/interfaces/PlatformTestingSdkOpts.d.ts +8 -0
  158. package/lib/types/interfaces/index.d.ts +4 -0
  159. package/lib/types/modules/feature/FeatureModule.d.ts +2 -0
  160. package/lib/types/modules/feature/controllers/FeatureController.d.ts +3 -0
  161. package/lib/types/tests/exports.d.ts +2 -0
  162. package/lib/types/tests/testAcceptMime.d.ts +2 -0
  163. package/lib/types/tests/testAuth.d.ts +8 -0
  164. package/lib/types/tests/testBodyParams.d.ts +3 -0
  165. package/lib/types/tests/testCache.d.ts +2 -0
  166. package/lib/types/tests/testChildrenControllers.d.ts +2 -0
  167. package/lib/types/tests/testCookies.d.ts +11 -0
  168. package/lib/types/tests/testCtrlInheritance.d.ts +18 -0
  169. package/lib/types/tests/testCustom404.d.ts +2 -0
  170. package/lib/types/tests/testErrors.d.ts +19 -0
  171. package/lib/types/tests/testHandlers.d.ts +12 -0
  172. package/lib/types/tests/testHeaderParams.d.ts +16 -0
  173. package/lib/types/tests/testHeaders.d.ts +8 -0
  174. package/lib/types/tests/testInheritanceController.d.ts +28 -0
  175. package/lib/types/tests/testLocals.d.ts +2 -0
  176. package/lib/types/tests/testLocation.d.ts +2 -0
  177. package/lib/types/tests/testMiddlewares.d.ts +8 -0
  178. package/lib/types/tests/testModule.d.ts +2 -0
  179. package/lib/types/tests/testMulter.d.ts +21 -0
  180. package/lib/types/tests/testPathParams.d.ts +2 -0
  181. package/lib/types/tests/testQueryParams.d.ts +6 -0
  182. package/lib/types/tests/testRedirect.d.ts +2 -0
  183. package/lib/types/tests/testResponse.d.ts +16 -0
  184. package/lib/types/tests/testResponseFilter.d.ts +2 -0
  185. package/lib/types/tests/testRouting.d.ts +7 -0
  186. package/lib/types/tests/testScopeRequest.d.ts +12 -0
  187. package/lib/types/tests/testSession.d.ts +16 -0
  188. package/lib/types/tests/testStatics.d.ts +2 -0
  189. package/lib/types/tests/testView.d.ts +2 -0
  190. package/package.json +44 -0
  191. package/src/components/PlatformTestSdk.ts +29 -0
  192. package/src/data/file.txt +1 -0
  193. package/src/data/response.data.json +3 -0
  194. package/src/data/swagger.json +32 -0
  195. package/src/errors/CustomBadRequest.ts +16 -0
  196. package/src/errors/CustomInternalError.ts +16 -0
  197. package/src/index.ts +39 -0
  198. package/src/interfaces/PlatformTestingSdkOpts.ts +10 -0
  199. package/src/interfaces/index.ts +5 -0
  200. package/src/modules/feature/FeatureModule.ts +9 -0
  201. package/src/modules/feature/controllers/FeatureController.ts +11 -0
  202. package/src/tests/exports.ts +69 -0
  203. package/src/tests/testAcceptMime.ts +95 -0
  204. package/src/tests/testAuth.ts +339 -0
  205. package/src/tests/testBodyParams.ts +455 -0
  206. package/src/tests/testCache.ts +270 -0
  207. package/src/tests/testChildrenControllers.ts +55 -0
  208. package/src/tests/testCookies.ts +36 -0
  209. package/src/tests/testCtrlInheritance.ts +140 -0
  210. package/src/tests/testCustom404.ts +33 -0
  211. package/src/tests/testErrors.ts +223 -0
  212. package/src/tests/testHandlers.ts +92 -0
  213. package/src/tests/testHeaderParams.ts +149 -0
  214. package/src/tests/testHeaders.ts +91 -0
  215. package/src/tests/testInheritanceController.ts +156 -0
  216. package/src/tests/testLocals.ts +46 -0
  217. package/src/tests/testLocation.ts +37 -0
  218. package/src/tests/testMiddlewares.ts +159 -0
  219. package/src/tests/testModule.ts +46 -0
  220. package/src/tests/testMulter.ts +140 -0
  221. package/src/tests/testPathParams.ts +122 -0
  222. package/src/tests/testQueryParams.ts +323 -0
  223. package/src/tests/testRedirect.ts +124 -0
  224. package/src/tests/testResponse.ts +558 -0
  225. package/src/tests/testResponseFilter.ts +101 -0
  226. package/src/tests/testRouting.ts +65 -0
  227. package/src/tests/testScopeRequest.ts +111 -0
  228. package/src/tests/testSession.ts +104 -0
  229. package/src/tests/testStatics.ts +61 -0
  230. package/src/tests/testView.ts +68 -0
  231. package/tsconfig.compile.esm.json +12 -0
@@ -0,0 +1,46 @@
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";
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.SuperTest<SuperTest.Test>;
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
+ }
@@ -0,0 +1,37 @@
1
+ import {Controller, Get, PlatformTest} from "@tsed/common";
2
+ import SuperTest from "supertest";
3
+ import {Location} from "@tsed/schema";
4
+ import {PlatformTestingSdkOpts} from "../interfaces";
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.SuperTest<SuperTest.Test>;
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
+ }
@@ -0,0 +1,159 @@
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";
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.SuperTest<SuperTest.Test>;
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
+ }
@@ -0,0 +1,46 @@
1
+ import {Controller, Get, PlatformTest} from "@tsed/common";
2
+ import SuperTest from "supertest";
3
+ import {PlatformTestingSdkOpts} from "../interfaces";
4
+ import {FeatureModule} from "../modules/feature/FeatureModule";
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.SuperTest<SuperTest.Test>;
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
+ }
@@ -0,0 +1,140 @@
1
+ import {BodyParams, Controller, MulterOptions, MultipartFile, PlatformMulterFile, PlatformTest, Post} from "@tsed/common";
2
+ import {CollectionOf, Property, Required, Status} from "@tsed/schema";
3
+ import {Request} from "express";
4
+ import multer, {FileFilterCallback} from "multer";
5
+ import SuperTest from "supertest";
6
+ import {PlatformTestingSdkOpts} from "../interfaces";
7
+
8
+ export class Task {
9
+ @Property()
10
+ id: string;
11
+
12
+ @Property()
13
+ title: string;
14
+ }
15
+
16
+ export class Event {
17
+ @Property()
18
+ @Required()
19
+ id: string;
20
+
21
+ @CollectionOf(Task)
22
+ tasks: Task[];
23
+ }
24
+
25
+ const fileFilterStub = jest.fn();
26
+ const fileFilter = (req: Request, file: PlatformMulterFile, callback: FileFilterCallback) => {
27
+ fileFilterStub();
28
+ callback(null, true);
29
+ };
30
+
31
+ function getFileConfig(): any {
32
+ return {
33
+ storage: multer.diskStorage({
34
+ destination: (req: Express.Request, _fileItem, cb) => {
35
+ const path = `${__dirname}/../.tmp`;
36
+ cb(null, path);
37
+ }
38
+ }),
39
+ fileFilter
40
+ };
41
+ }
42
+
43
+ @Controller("/multer")
44
+ export class TestMulterController {
45
+ @Post("/scenario-1")
46
+ @Status(201)
47
+ @MulterOptions({dest: `${__dirname}/../.tmp`})
48
+ uploadWithName(@Required() @MultipartFile("media") media: PlatformMulterFile) {
49
+ return media.originalname;
50
+ }
51
+
52
+ @Post("/scenario-2")
53
+ @Status(201)
54
+ @MulterOptions({dest: `${__dirname}/../.tmp`})
55
+ uploadWithPayload(@MultipartFile("media") media: PlatformMulterFile, @BodyParams("form_id") formId: string) {
56
+ return {
57
+ file: media.originalname,
58
+ formId
59
+ };
60
+ }
61
+
62
+ @Post("/scenario-3")
63
+ @Status(201)
64
+ @MulterOptions(getFileConfig())
65
+ uploadWithOptions(@MultipartFile("media") media: PlatformMulterFile) {
66
+ return {
67
+ file: media.originalname
68
+ };
69
+ }
70
+ }
71
+
72
+ export function testMulter(options: PlatformTestingSdkOpts) {
73
+ let request: SuperTest.SuperTest<SuperTest.Test>;
74
+
75
+ beforeAll(
76
+ PlatformTest.bootstrap(options.server, {
77
+ ...options,
78
+ mount: {
79
+ "/rest": [TestMulterController]
80
+ }
81
+ })
82
+ );
83
+ beforeAll(() => {
84
+ request = SuperTest(PlatformTest.callback());
85
+ });
86
+ afterAll(PlatformTest.reset);
87
+ beforeAll(() => jest.resetAllMocks());
88
+ describe("Scenario 1: POST /rest/multer/scenario-1", () => {
89
+ it("should upload file with multer", async () => {
90
+ const result = await request.post("/rest/multer/scenario-1").attach("media", `${__dirname}/../data/file.txt`).expect(201);
91
+
92
+ expect(result.text).toEqual("file.txt");
93
+ });
94
+
95
+ it("should throw an exception when there is no file", async () => {
96
+ const result = await request.post("/rest/multer/scenario-1").expect(400);
97
+
98
+ expect(result.body).toEqual({
99
+ name: "REQUIRED_VALIDATION_ERROR",
100
+ message: "Bad request on parameter \"request.files.media.0\".\nIt should have required parameter 'media.0'",
101
+ status: 400,
102
+ errors: [
103
+ {
104
+ dataPath: "",
105
+ keyword: "required",
106
+ message: "It should have required parameter 'media.0'",
107
+ modelName: "files",
108
+ params: {missingProperty: "media.0"},
109
+ schemaPath: "#/required"
110
+ }
111
+ ]
112
+ });
113
+ });
114
+ });
115
+ describe("Scenario 2: POST /rest/multer/scenario-2", () => {
116
+ it("should upload file with multer", async () => {
117
+ const result = await request
118
+ .post("/rest/multer/scenario-2")
119
+ .attach("media", `${__dirname}/../data/file.txt`)
120
+ .field({
121
+ form_id: "form_id"
122
+ })
123
+ .expect(201);
124
+
125
+ expect(result.body).toEqual({file: "file.txt", formId: "form_id"});
126
+ });
127
+ });
128
+ describe("Scenario 3: POST /rest/multer/scenario-3", () => {
129
+ it("should upload file with multer", async () => {
130
+ const result = await request.post("/rest/multer/scenario-3").attach("media", `${__dirname}/../data/file.txt`).field({}).expect(201);
131
+
132
+ expect(result.body).toEqual({
133
+ file: "file.txt"
134
+ });
135
+
136
+ // filterFilter is called
137
+ expect(fileFilterStub).toBeCalled();
138
+ });
139
+ });
140
+ }
@@ -0,0 +1,122 @@
1
+ import {Context, Controller, Get, PathParams, PlatformTest, Post} from "@tsed/common";
2
+ import {Pattern} from "@tsed/schema";
3
+ import SuperTest from "supertest";
4
+ import {PlatformTestingSdkOpts} from "../interfaces";
5
+
6
+ @Controller("/path-params")
7
+ class TestPathParamsCtrl {
8
+ @Get("/scenario-1/:scope/:scopeId")
9
+ async testScenario1(@PathParams("scope") scope: string) {
10
+ // Here scope will be {0: 'a', 1: 'b', 2: 'c'} instead of 'abc' in version 5.47.0
11
+ expect(scope).toEqual("abc");
12
+
13
+ return scope;
14
+ }
15
+
16
+ @Get("/scenario-2/:scope/:scopeId")
17
+ async testScenario2(@PathParams("scope") scope: any) {
18
+ // This way it works in version 5.47.0
19
+ expect(scope).toEqual("abc");
20
+
21
+ return scope;
22
+ }
23
+
24
+ @Post("/scenario-3/:scope/:scopeId")
25
+ async testScenario3(@PathParams("scope") scope: string): Promise<any[]> {
26
+ expect(scope).toEqual("abc");
27
+
28
+ // Here the function will return {0: 'a', 1: 'b', 2: 'c'} instead of ['a','b','c'] in version 5.44.13
29
+ return ["a", "b", "c"];
30
+ }
31
+
32
+ @Post("/scenario-4/:scope/:scopeId")
33
+ async testScenario4(@PathParams("scope") scope: string, @Context() ctx: Context) {
34
+ expect(scope).toEqual("abc");
35
+
36
+ // This way it works in version 5.44.13
37
+ ctx.response.body(["a", "b", "c"]);
38
+ }
39
+
40
+ @Get("/scenario-5/:scopeId")
41
+ async testScenario5(@PathParams("scopeId") @Pattern(/^[0-9a-fA-F]{24}$/) scopeId: string, @Context() ctx: Context) {
42
+ return "test";
43
+ }
44
+
45
+ @Get("/scenario-6/:id")
46
+ async testScenario6(@PathParams("id") id: number) {
47
+ return {id};
48
+ }
49
+ }
50
+
51
+ export function testPathParams(options: PlatformTestingSdkOpts) {
52
+ let request: SuperTest.SuperTest<SuperTest.Test>;
53
+
54
+ beforeAll(
55
+ PlatformTest.bootstrap(options.server, {
56
+ ...options,
57
+ mount: {
58
+ "/rest": [TestPathParamsCtrl]
59
+ }
60
+ })
61
+ );
62
+ beforeAll(() => {
63
+ request = SuperTest(PlatformTest.callback());
64
+ });
65
+ afterAll(PlatformTest.reset);
66
+
67
+ it("Scenario 1: GET /rest/path-params/scenario-1/scope/scopeId", async () => {
68
+ const response = await request.get("/rest/path-params/scenario-1/abc/1").expect(200);
69
+
70
+ expect(response.text).toEqual("abc");
71
+ });
72
+
73
+ it("Scenario 2: GET /rest/path-params/scenario-2/scopeId", async () => {
74
+ const response = await request.get("/rest/path-params/scenario-2/abc/1").expect(200);
75
+
76
+ expect(response.text).toEqual("abc");
77
+ });
78
+
79
+ it("Scenario 3: POST /rest/path-params/scenario-3/scope/scopeId", async () => {
80
+ const response = await request.post("/rest/path-params/scenario-3/abc/1").expect(200);
81
+
82
+ expect(response.body).toEqual(["a", "b", "c"]);
83
+ });
84
+
85
+ it("Scenario 4: POST /rest/path-params/scenario-4/scope/scopeId", async () => {
86
+ const response = await request.post("/rest/path-params/scenario-4/abc/1").expect(200);
87
+
88
+ expect(response.body).toEqual(["a", "b", "c"]);
89
+ });
90
+
91
+ it("Scenario 5 a: GET /rest/path-params/scenario-5/scopeId", async () => {
92
+ const response = await request.get("/rest/path-params/scenario-5/1").expect(400);
93
+
94
+ expect(response.body).toEqual({
95
+ errors: [
96
+ {
97
+ data: "1",
98
+ dataPath: "",
99
+ instancePath: "",
100
+ keyword: "pattern",
101
+ message: 'must match pattern "^[0-9a-fA-F]{24}$"',
102
+ params: {
103
+ pattern: "^[0-9a-fA-F]{24}$"
104
+ },
105
+ schemaPath: "#/pattern"
106
+ }
107
+ ],
108
+ message: 'Bad request on parameter "request.path.scopeId".\nValue must match pattern "^[0-9a-fA-F]{24}$". Given value: "1"',
109
+ name: "AJV_VALIDATION_ERROR",
110
+ status: 400
111
+ });
112
+ });
113
+ it("Scenario 5b: GET /rest/path-params/scenario-5/scopeId", async () => {
114
+ await request.get("/rest/path-params/scenario-5/5ce4ee471495836c5e2e4cb0").expect(200);
115
+ });
116
+
117
+ it("Scenario 6: GET /rest/path-params/scenario-6/id", async () => {
118
+ const response = await request.get("/rest/path-params/scenario-6/1").expect(200);
119
+
120
+ expect(response.body).toEqual({id: 1});
121
+ });
122
+ }