@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,455 @@
1
+ import "@tsed/ajv";
2
+ import {BodyParams, Context, Controller, HeaderParams, PlatformTest, Post, RawBodyParams} from "@tsed/common";
3
+ import {Default, Description, GenericOf, Generics, Maximum, Minimum, Nullable, Property, Required, Status} from "@tsed/schema";
4
+ import SuperTest from "supertest";
5
+ import {PlatformTestingSdkOpts} from "../interfaces";
6
+
7
+ enum MyEnum {
8
+ TITLE,
9
+ AGE
10
+ }
11
+
12
+ class NestedModel {
13
+ @Property()
14
+ id: string;
15
+ }
16
+
17
+ class NullModel {
18
+ @Property()
19
+ prop1: string;
20
+
21
+ @Property()
22
+ prop2: number;
23
+
24
+ @Property()
25
+ prop3: Date;
26
+
27
+ @Nullable(NestedModel)
28
+ prop4: NestedModel;
29
+ }
30
+
31
+ class FindQuery {
32
+ @Property()
33
+ a?: string;
34
+
35
+ @Property()
36
+ b?: string;
37
+ }
38
+
39
+ @Generics("T")
40
+ class PaginationQuery<T> {
41
+ // things about pagination
42
+ @Minimum(0)
43
+ @Default(0)
44
+ offset?: number;
45
+
46
+ @Minimum(1)
47
+ @Maximum(1000)
48
+ @Default(50)
49
+ limit?: number;
50
+
51
+ @Property("T")
52
+ where?: T;
53
+ }
54
+
55
+ class Param1Type {
56
+ @Required()
57
+ value1!: string;
58
+ }
59
+
60
+ class Param2Type {
61
+ @Required()
62
+ value2!: string;
63
+ }
64
+
65
+ @Controller("/body-params")
66
+ class TestBodyParamsCtrl {
67
+ @Post("/scenario-1")
68
+ @Status(201)
69
+ @Description("retrieve body and header content-type")
70
+ testScenario1(@HeaderParams("Content-type") contentType: string, @BodyParams() payload: any) {
71
+ return {payload, contentType};
72
+ }
73
+
74
+ @Post("/scenario-2")
75
+ @Description("Extract field from body payload as string[]")
76
+ testScenario2(@BodyParams("test") value: string[]): any {
77
+ return {value};
78
+ }
79
+
80
+ @Post("/scenario-3")
81
+ @Description("Extract body payload as string[]")
82
+ testScenario3(@BodyParams() value: string[]): any {
83
+ return {value};
84
+ }
85
+
86
+ @Post("/scenario-4")
87
+ @Description("Extract field from body payload as string[] with required annotation")
88
+ testScenario4(@Required() @BodyParams("test") value: string[]): any {
89
+ return {value};
90
+ }
91
+
92
+ @Post("/scenario-4b")
93
+ @Description("Extract field from body payload as string")
94
+ testScenario4b(@Required() @BodyParams("test") value: string): any {
95
+ return {value};
96
+ }
97
+
98
+ @Post("/scenario-4c")
99
+ @Description("Extract field from body payload as number with required annotation")
100
+ testScenario4c(@Required() @BodyParams("test") value: number): any {
101
+ return {value};
102
+ }
103
+
104
+ @Post("/scenario-5")
105
+ @Description("Extract raw body payload as Buffer")
106
+ testScenario5(@RawBodyParams() raw: Buffer, @BodyParams() payload: any, @Context() context: Context): any {
107
+ return {value: raw.toString("utf8")};
108
+ }
109
+
110
+ @Post("/scenario-6")
111
+ testScenario6(@BodyParams("type", String) type: keyof typeof MyEnum): any {
112
+ return {type};
113
+ }
114
+
115
+ @Post("/scenario-7")
116
+ testScenario7(@BodyParams("test") value: string): any {
117
+ return {value};
118
+ }
119
+
120
+ @Post("/scenario-8")
121
+ testScenario8(@BodyParams() model: NullModel) {
122
+ return {model};
123
+ }
124
+
125
+ @Post("/scenario-9")
126
+ testScenario9(@BodyParams() @GenericOf(FindQuery) q: PaginationQuery<FindQuery>) {
127
+ return {q};
128
+ }
129
+
130
+ @Post("/scenario-10")
131
+ testScenario10(
132
+ @Required() @BodyParams("param1", Param1Type) param1: Param1Type,
133
+ @Required() @BodyParams("param2", Param2Type) param2: Param2Type
134
+ ) {
135
+ return {param1, param2};
136
+ }
137
+ }
138
+
139
+ export function testBodyParams(options: PlatformTestingSdkOpts) {
140
+ let request: SuperTest.SuperTest<SuperTest.Test>;
141
+
142
+ beforeAll(
143
+ PlatformTest.bootstrap(options.server, {
144
+ ...options,
145
+ mount: {
146
+ "/rest": [TestBodyParamsCtrl]
147
+ }
148
+ })
149
+ );
150
+ beforeAll(() => {
151
+ request = SuperTest(PlatformTest.callback());
152
+ });
153
+ afterAll(PlatformTest.reset);
154
+
155
+ describe("Scenario 1: POST /rest/body-params/scenario-1", () => {
156
+ it("should return a 201 response", async () => {
157
+ const response = await request
158
+ .post("/rest/body-params/scenario-1")
159
+ .set({
160
+ "Content-Type": "application/json"
161
+ })
162
+ .send({
163
+ id: "id"
164
+ })
165
+ .expect(201);
166
+
167
+ expect(response.body).toEqual({
168
+ contentType: "application/json",
169
+ payload: {
170
+ id: "id"
171
+ }
172
+ });
173
+ });
174
+ });
175
+ describe("Scenario2: with expression Array<string>", () => {
176
+ it("should return value", async () => {
177
+ const response = await request
178
+ .post("/rest/body-params/scenario-2")
179
+ .send({
180
+ test: ["value"]
181
+ })
182
+ .expect(200);
183
+
184
+ expect(response.body).toEqual({value: ["value"]});
185
+ });
186
+ it("should return an empty array (1)", async () => {
187
+ const response = await request.post("/rest/body-params/scenario-2").send().expect(200);
188
+
189
+ expect(response.body).toEqual({});
190
+ });
191
+ it("should return an empty value (2)", async () => {
192
+ const response = await request.post("/rest/body-params/scenario-2").send({}).expect(200);
193
+
194
+ expect(response.body).toEqual({});
195
+ });
196
+ });
197
+ describe("Scenario3: without expression Array<string>", () => {
198
+ it("should return value", async () => {
199
+ const response = await request.post("/rest/body-params/scenario-3").send(["value"]).expect(200);
200
+
201
+ expect(response.body).toEqual({value: ["value"]});
202
+ });
203
+ it("should return an empty array (1)", async () => {
204
+ const response = await request.post("/rest/body-params/scenario-3").send().expect(200);
205
+
206
+ expect(response.body).toEqual({value: [{}]});
207
+ });
208
+ });
209
+ describe("Scenario4: with expression required Array<string>", () => {
210
+ it("should return value", async () => {
211
+ const response = await request
212
+ .post("/rest/body-params/scenario-4")
213
+ .send({test: ["value"]})
214
+ .expect(200);
215
+
216
+ expect(response.body).toEqual({value: ["value"]});
217
+ });
218
+ it("should throw an exception", async () => {
219
+ const response = await request.post("/rest/body-params/scenario-4").send().expect(400);
220
+
221
+ expect(response.body).toEqual({
222
+ name: "REQUIRED_VALIDATION_ERROR",
223
+ message: "Bad request on parameter \"request.body.test\".\nIt should have required parameter 'test'",
224
+ status: 400,
225
+ errors: [
226
+ {
227
+ dataPath: "",
228
+ keyword: "required",
229
+ message: "It should have required parameter 'test'",
230
+ modelName: "body",
231
+ params: {missingProperty: "test"},
232
+ schemaPath: "#/required"
233
+ }
234
+ ]
235
+ });
236
+ });
237
+ });
238
+
239
+ describe("Scenario4b: with expression required String", () => {
240
+ it("should return value", async () => {
241
+ const response = await request.post("/rest/body-params/scenario-4b").send({test: "value"}).expect(200);
242
+
243
+ expect(response.body).toEqual({value: "value"});
244
+ });
245
+ it("should throw an exception when nothing is sent", async () => {
246
+ const response = await request.post("/rest/body-params/scenario-4b").send().expect(400);
247
+
248
+ expect(response.body).toEqual({
249
+ name: "REQUIRED_VALIDATION_ERROR",
250
+ message: "Bad request on parameter \"request.body.test\".\nIt should have required parameter 'test'",
251
+ status: 400,
252
+ errors: [
253
+ {
254
+ dataPath: "",
255
+ keyword: "required",
256
+ message: "It should have required parameter 'test'",
257
+ modelName: "body",
258
+ params: {missingProperty: "test"},
259
+ schemaPath: "#/required"
260
+ }
261
+ ]
262
+ });
263
+ });
264
+ it("should throw an exception when nothing is an empty string is sent", async () => {
265
+ const response = await request.post("/rest/body-params/scenario-4b").send({test: ""}).expect(400);
266
+
267
+ expect(response.body).toEqual({
268
+ name: "REQUIRED_VALIDATION_ERROR",
269
+ message: "Bad request on parameter \"request.body.test\".\nIt should have required parameter 'test'",
270
+ status: 400,
271
+ errors: [
272
+ {
273
+ dataPath: "",
274
+ keyword: "required",
275
+ message: "It should have required parameter 'test'",
276
+ modelName: "body",
277
+ params: {missingProperty: "test"},
278
+ schemaPath: "#/required"
279
+ }
280
+ ]
281
+ });
282
+ });
283
+ });
284
+ describe("Scenario4c: with expression required Number", () => {
285
+ it("should return value (with 1)", async () => {
286
+ const response = await request.post("/rest/body-params/scenario-4c").send({test: 1}).expect(200);
287
+
288
+ expect(response.body).toEqual({value: 1});
289
+ });
290
+ it("should return value (with 0)", async () => {
291
+ const response = await request.post("/rest/body-params/scenario-4c").send({test: 0}).expect(200);
292
+
293
+ expect(response.body).toEqual({value: 0});
294
+ });
295
+ it("should throw an exception when nothing is sent", async () => {
296
+ const response = await request.post("/rest/body-params/scenario-4b").send().expect(400);
297
+
298
+ expect(response.body).toEqual({
299
+ name: "REQUIRED_VALIDATION_ERROR",
300
+ message: "Bad request on parameter \"request.body.test\".\nIt should have required parameter 'test'",
301
+ status: 400,
302
+ errors: [
303
+ {
304
+ dataPath: "",
305
+ keyword: "required",
306
+ message: "It should have required parameter 'test'",
307
+ modelName: "body",
308
+ params: {missingProperty: "test"},
309
+ schemaPath: "#/required"
310
+ }
311
+ ]
312
+ });
313
+ });
314
+ });
315
+ describe("Scenario5: with raw payload", () => {
316
+ it("should return value", async () => {
317
+ const response = await request.post("/rest/body-params/scenario-5").send('{"test": ["value"]}').expect(200);
318
+
319
+ expect(response.body).toEqual({
320
+ value: '{"test": ["value"]}'
321
+ });
322
+ });
323
+ });
324
+
325
+ describe("Scenario6: Enum", () => {
326
+ it("should return value", async () => {
327
+ const response = await request.post("/rest/body-params/scenario-6").send({
328
+ type: "TITLE"
329
+ });
330
+
331
+ expect(response.body).toEqual({
332
+ type: "TITLE"
333
+ });
334
+ });
335
+ });
336
+
337
+ describe("Scenario7: payload with Null", () => {
338
+ it("should return value", async () => {
339
+ const response = await request.post("/rest/body-params/scenario-7").send({
340
+ test: null
341
+ });
342
+
343
+ expect(response.body).toEqual({
344
+ value: null
345
+ });
346
+ });
347
+ });
348
+
349
+ describe("Scenario8: payload with props to null", () => {
350
+ it("should return value with null value", async () => {
351
+ const response = await request.post("/rest/body-params/scenario-8").send({
352
+ prop1: null,
353
+ prop2: null,
354
+ prop3: null,
355
+ prop4: null
356
+ });
357
+
358
+ expect(response.body).toEqual({
359
+ model: {
360
+ prop1: null,
361
+ prop2: null,
362
+ prop3: null,
363
+ prop4: null
364
+ }
365
+ });
366
+ });
367
+ });
368
+
369
+ describe("Scenario9: payload with Generics", () => {
370
+ it("should return value", async () => {
371
+ const response = await request
372
+ .post("/rest/body-params/scenario-9")
373
+ .send({
374
+ offset: 0,
375
+ limit: 10,
376
+ where: {
377
+ a: "ca",
378
+ b: "cb"
379
+ }
380
+ })
381
+ .expect(200);
382
+
383
+ expect(response.body).toEqual({
384
+ q: {
385
+ limit: 10,
386
+ offset: 0,
387
+ where: {
388
+ a: "ca",
389
+ b: "cb"
390
+ }
391
+ }
392
+ });
393
+ });
394
+ it("should throw a bad request", async () => {
395
+ const response = await request
396
+ .post("/rest/body-params/scenario-9")
397
+ .send({
398
+ offset: 0,
399
+ limit: 0,
400
+ where: {
401
+ a: "ca",
402
+ b: "cb"
403
+ }
404
+ })
405
+ .expect(400);
406
+
407
+ expect(response.body).toEqual({
408
+ errors: [
409
+ {
410
+ data: 0,
411
+ dataPath: ".limit",
412
+ instancePath: "/limit",
413
+ keyword: "minimum",
414
+ message: "must be >= 1",
415
+ modelName: "PaginationQuery",
416
+ params: {
417
+ comparison: ">=",
418
+ limit: 1
419
+ },
420
+ schemaPath: "#/properties/limit/minimum"
421
+ }
422
+ ],
423
+ message: 'Bad request on parameter "request.body".\nPaginationQuery.limit must be >= 1. Given value: 0',
424
+ name: "AJV_VALIDATION_ERROR",
425
+ status: 400
426
+ });
427
+ });
428
+ });
429
+ describe("Scenario10: validate two payload", () => {
430
+ it("should throw a Bad request if body params is missing (1)", async () => {
431
+ await request
432
+ .post("/rest/body-params/scenario-10")
433
+ .send({param1: {}, param2: {value2: "value"}})
434
+ .expect(400);
435
+ });
436
+ it("should throw a Bad request if body params is missing (2)", async () => {
437
+ await request.post("/rest/body-params/scenario-10").send({param1: {}, param2: {}}).expect(400);
438
+ });
439
+ it("should throw a Bad request if body params is missing (3)", async () => {
440
+ await request
441
+ .post("/rest/body-params/scenario-10")
442
+ .send({param1: {value: "value"}, param2: {}})
443
+ .expect(400);
444
+ });
445
+ it("should throw a Bad request if body params is missing (4)", async () => {
446
+ await request.post("/rest/body-params/scenario-10").send({}).expect(400);
447
+ });
448
+ it("should validate the body params", async () => {
449
+ await request
450
+ .post("/rest/body-params/scenario-10")
451
+ .send({param1: {value1: "value"}, param2: {value2: "value"}})
452
+ .expect(200);
453
+ });
454
+ });
455
+ }