@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,323 @@
1
+ import "@tsed/ajv";
2
+ import {Controller, Get, PlatformTest, QueryParams} from "@tsed/common";
3
+ import {Default, GenericOf, Generics, Maximum, Minimum, Property, Required} from "@tsed/schema";
4
+ import SuperTest from "supertest";
5
+ import {PlatformTestingSdkOpts} from "../interfaces";
6
+
7
+ export class RandomStringModel {
8
+ @Maximum(250)
9
+ @Minimum(1)
10
+ @Required()
11
+ length: number;
12
+ }
13
+
14
+ class FindQuery {
15
+ @Property()
16
+ a?: number;
17
+
18
+ @Property()
19
+ b?: number;
20
+ }
21
+
22
+ @Generics("T")
23
+ class PaginationQuery<T> {
24
+ // things about pagination
25
+ @Minimum(0)
26
+ @Default(0)
27
+ offset?: number;
28
+
29
+ @Minimum(1)
30
+ @Maximum(1000)
31
+ @Default(50)
32
+ limit?: number;
33
+
34
+ @Property("T")
35
+ where?: T;
36
+ }
37
+
38
+ @Controller("/query-params")
39
+ class TestQueryParamsCtrl {
40
+ @Get("/scenario-1")
41
+ testScenario1(@QueryParams("test") value: boolean): any {
42
+ return {value};
43
+ }
44
+
45
+ @Get("/scenario-2")
46
+ testScenario2(@QueryParams("test") value: boolean = true): any {
47
+ return {value};
48
+ }
49
+
50
+ @Get("/scenario-3")
51
+ testScenario3(@QueryParams("test") value: number): any {
52
+ return {value};
53
+ }
54
+
55
+ @Get("/scenario-4")
56
+ testScenario4(@QueryParams("test") value: string): any {
57
+ return {value};
58
+ }
59
+
60
+ @Get("/scenario-5")
61
+ testScenario5(@QueryParams() value: RandomStringModel): any {
62
+ return {value};
63
+ }
64
+
65
+ @Get("/scenario-6")
66
+ testScenario6(@QueryParams() @GenericOf(FindQuery) q: PaginationQuery<FindQuery>, @QueryParams() qs: any) {
67
+ return {q};
68
+ }
69
+
70
+ @Get("/scenario-7")
71
+ testScenario7(@QueryParams("q") @GenericOf(FindQuery) q: PaginationQuery<FindQuery>) {
72
+ return {q};
73
+ }
74
+
75
+ @Get("/scenario-8")
76
+ testScenario8(@QueryParams("test", String) value: string[]): any {
77
+ return {value};
78
+ }
79
+ }
80
+
81
+ export function testQueryParams(options: PlatformTestingSdkOpts) {
82
+ let request: SuperTest.SuperTest<SuperTest.Test>;
83
+
84
+ beforeAll(
85
+ PlatformTest.bootstrap(options.server, {
86
+ ...options,
87
+ ajv: {
88
+ verbose: false
89
+ },
90
+ mount: {
91
+ "/rest": [TestQueryParamsCtrl]
92
+ }
93
+ })
94
+ );
95
+ beforeAll(() => {
96
+ request = SuperTest(PlatformTest.callback());
97
+ });
98
+ afterAll(PlatformTest.reset);
99
+ describe("Scenario1: Boolean value", () => {
100
+ it("should return true when query is true", async () => {
101
+ const response = await request.get("/rest/query-params/scenario-1?test=true").expect(200);
102
+
103
+ expect(response.body).toEqual({value: true});
104
+ });
105
+ it("should return true when query is 1", async () => {
106
+ const response = await request.get("/rest/query-params/scenario-1?test=1").expect(200);
107
+
108
+ expect(response.body).toEqual({value: true});
109
+ });
110
+ it("should return false when query is false", async () => {
111
+ const response = await request.get("/rest/query-params/scenario-1?test=false").expect(200);
112
+
113
+ expect(response.body).toEqual({value: false});
114
+ });
115
+ it("should return false when query is 0", async () => {
116
+ const response = await request.get("/rest/query-params/scenario-1?test=0").expect(200);
117
+
118
+ expect(response.body).toEqual({value: false});
119
+ });
120
+ it("should return false when query is null", async () => {
121
+ const response = await request.get("/rest/query-params/scenario-1?test=null").expect(200);
122
+
123
+ expect(response.body).toEqual({value: null});
124
+ });
125
+ it("should return undefined when query is empty", async () => {
126
+ const response = await request.get("/rest/query-params/scenario-1?test=").expect(200);
127
+
128
+ expect(response.body).toEqual({});
129
+ });
130
+ it("should return undefined when no query", async () => {
131
+ const response = await request.get("/rest/query-params/scenario-1").expect(200);
132
+
133
+ expect(response.body).toEqual({});
134
+ });
135
+ });
136
+ describe("Scenario2: Boolean value with default value", () => {
137
+ it("should return true when query is true", async () => {
138
+ const response = await request.get("/rest/query-params/scenario-2?test=true").expect(200);
139
+
140
+ expect(response.body).toEqual({value: true});
141
+ });
142
+ it("should return true when query is 1", async () => {
143
+ const response = await request.get("/rest/query-params/scenario-2?test=1").expect(200);
144
+
145
+ expect(response.body).toEqual({value: true});
146
+ });
147
+ it("should return false when query is false", async () => {
148
+ const response = await request.get("/rest/query-params/scenario-2?test=false").expect(200);
149
+
150
+ expect(response.body).toEqual({value: false});
151
+ });
152
+ it("should return false when query is 0", async () => {
153
+ const response = await request.get("/rest/query-params/scenario-2?test=0").expect(200);
154
+
155
+ expect(response.body).toEqual({value: false});
156
+ });
157
+ it("should return false when query is null", async () => {
158
+ const response = await request.get("/rest/query-params/scenario-2?test=null").expect(200);
159
+
160
+ expect(response.body).toEqual({value: null});
161
+ });
162
+ it("should return undefined when query is empty", async () => {
163
+ const response = await request.get("/rest/query-params/scenario-2?test=").expect(200);
164
+
165
+ expect(response.body).toEqual({value: true});
166
+ });
167
+ it("should return undefined when no query", async () => {
168
+ const response = await request.get("/rest/query-params/scenario-2").expect(200);
169
+
170
+ expect(response.body).toEqual({value: true});
171
+ });
172
+ });
173
+ describe("Scenario3: Number value", () => {
174
+ const endpoint = "/rest/query-params/scenario-3";
175
+ it("should return 0 when query is 0", async () => {
176
+ const response = await request.get(`${endpoint}?test=0`).expect(200);
177
+
178
+ expect(response.body).toEqual({value: 0});
179
+ });
180
+ it("should return 1 when query is 1", async () => {
181
+ const response = await request.get(`${endpoint}?test=1`).expect(200);
182
+
183
+ expect(response.body).toEqual({value: 1});
184
+ });
185
+ it("should return 0.1 when query is 0.1", async () => {
186
+ const response = await request.get(`${endpoint}?test=0.1`).expect(200);
187
+
188
+ expect(response.body).toEqual({value: 0.1});
189
+ });
190
+ it("should throw bad request", async () => {
191
+ const response = await request.get(`${endpoint}?test=error`).expect(400);
192
+ expect(response.body).toEqual({
193
+ name: "AJV_VALIDATION_ERROR",
194
+ message: 'Bad request on parameter "request.query.test".\nValue must be number. Given value: "error"',
195
+ status: 400,
196
+ errors: [
197
+ {
198
+ keyword: "type",
199
+ dataPath: "",
200
+ instancePath: "",
201
+ schemaPath: "#/type",
202
+ params: {type: "number"},
203
+ message: "must be number",
204
+ data: "error"
205
+ }
206
+ ]
207
+ });
208
+ });
209
+ it("should return undefined when query is empty", async () => {
210
+ const response = await request.get(`${endpoint}?test=null`).expect(200);
211
+
212
+ expect(response.body).toEqual({value: null});
213
+ });
214
+ it("should return undefined when query is empty", async () => {
215
+ const response = await request.get(`${endpoint}?test=`).expect(200);
216
+
217
+ expect(response.body).toEqual({});
218
+ });
219
+ it("should return undefined when no query", async () => {
220
+ const response = await request.get(`${endpoint}`).expect(200);
221
+
222
+ expect(response.body).toEqual({});
223
+ });
224
+ });
225
+ describe("Scenario4: String value", () => {
226
+ const endpoint = "/rest/query-params/scenario-4";
227
+ it("should return 0 when query is 0", async () => {
228
+ const response = await request.get(`${endpoint}?test=0`).expect(200);
229
+
230
+ expect(response.body).toEqual({value: "0"});
231
+ });
232
+ it("should return 1 when query is 1", async () => {
233
+ const response = await request.get(`${endpoint}?test=1`).expect(200);
234
+
235
+ expect(response.body).toEqual({value: "1"});
236
+ });
237
+ it("should return 0.1 when query is 0.1", async () => {
238
+ const response = await request.get(`${endpoint}?test=0.1`).expect(200);
239
+
240
+ expect(response.body).toEqual({value: "0.1"});
241
+ });
242
+ it("should return undefined when query is empty", async () => {
243
+ const response = await request.get(`${endpoint}?test=`).expect(200);
244
+
245
+ expect(response.body).toEqual({value: ""});
246
+ });
247
+ it("should return undefined when no query", async () => {
248
+ const response = await request.get(`${endpoint}`).expect(200);
249
+
250
+ expect(response.body).toEqual({});
251
+ });
252
+ });
253
+ describe("Scenario6: DeepObject", () => {
254
+ const endpoint = "/rest/query-params/scenario-6";
255
+ it("should return 0 when query is 0", async () => {
256
+ const response = await request.get(`${endpoint}?offset=0&limit=10&where[a]=0&where[b]=1`).expect(200);
257
+
258
+ expect(response.body).toEqual({
259
+ q: {
260
+ limit: 10,
261
+ offset: 0,
262
+ where: {
263
+ a: 0,
264
+ b: 1
265
+ }
266
+ }
267
+ });
268
+ });
269
+ });
270
+ describe("Scenario7: DeepObject", () => {
271
+ const endpoint = "/rest/query-params/scenario-7";
272
+ it("should return the query value", async () => {
273
+ const response = await request.get(`${endpoint}?q[offset]=0&q[limit]=10&q[where][a]=0&q[where][b]=1`).expect(200);
274
+
275
+ expect(response.body).toEqual({
276
+ q: {
277
+ limit: 10,
278
+ offset: 0,
279
+ where: {
280
+ a: 0,
281
+ b: 1
282
+ }
283
+ }
284
+ });
285
+ });
286
+ it("should throw a bad request", async () => {
287
+ const response = await request.get(`${endpoint}?q[offset]=0&q[limit]=10&q[where][a]=ca&q[where][b]=1`).expect(400);
288
+
289
+ expect(response.body).toEqual({
290
+ errors: [
291
+ {
292
+ data: "ca",
293
+ dataPath: ".where.a",
294
+ instancePath: "/where/a",
295
+ keyword: "type",
296
+ message: "must be number",
297
+ modelName: "PaginationQuery",
298
+ params: {
299
+ type: "number"
300
+ },
301
+ schemaPath: "#/properties/where/properties/a/type"
302
+ }
303
+ ],
304
+ message: 'Bad request on parameter "request.query.q".\nPaginationQuery.where.a must be number. Given value: "ca"',
305
+ name: "AJV_VALIDATION_ERROR",
306
+ status: 400
307
+ });
308
+ });
309
+ });
310
+ describe("Scenario8: String[] value", () => {
311
+ const endpoint = "/rest/query-params/scenario-8";
312
+ it("should return 0 when query is 0", async () => {
313
+ const response = await request.get(`${endpoint}?test=0`).expect(200);
314
+
315
+ expect(response.body).toEqual({value: ["0"]});
316
+ });
317
+ it("should return 1 when query is 1", async () => {
318
+ const response = await request.get(`${endpoint}?test=1&test=2`).expect(200);
319
+
320
+ expect(response.body).toEqual({value: ["1", "2"]});
321
+ });
322
+ });
323
+ }
@@ -0,0 +1,124 @@
1
+ import {Context, Controller, Get, Location, PlatformTest, Redirect} from "@tsed/common";
2
+ import SuperTest from "supertest";
3
+ import {PlatformTestingSdkOpts} from "../interfaces";
4
+
5
+ @Controller("/redirect")
6
+ class RedirectCtrl {
7
+ @Get("/scenario-1")
8
+ @Redirect("/test")
9
+ testScenario1() {
10
+ return "Hello";
11
+ }
12
+
13
+ @Get("/scenario-2")
14
+ @Redirect(301, "/test")
15
+ testScenario2() {
16
+ return "Hello";
17
+ }
18
+
19
+ @Get("/scenario-3")
20
+ @Redirect(301, "/test")
21
+ testScenario3(@Context() ctx: Context) {
22
+ ctx.response.redirect(302, "/another/path");
23
+
24
+ return "Hello";
25
+ }
26
+
27
+ @Get("/scenario-4")
28
+ @Redirect("back")
29
+ testScenario4(@Context() ctx: Context) {
30
+ return "Hello";
31
+ }
32
+
33
+ @Get("/scenario-5")
34
+ @Location("back")
35
+ testScenario5(@Context() ctx: Context) {
36
+ return "Hello";
37
+ }
38
+
39
+ @Get("/scenario-6")
40
+ @Location("/path/to").Status(301)
41
+ testScenario6(@Context() ctx: Context) {
42
+ return "Hello";
43
+ }
44
+
45
+ @Get("/scenario-7")
46
+ @Location("/path/to")
47
+ testScenario7(@Context() ctx: Context) {
48
+ ctx.response.location("/another/path");
49
+ return "Hello";
50
+ }
51
+ }
52
+
53
+ export function testRedirect(options: PlatformTestingSdkOpts) {
54
+ let request: SuperTest.SuperTest<SuperTest.Test>;
55
+
56
+ beforeAll(
57
+ PlatformTest.bootstrap(options.server, {
58
+ ...options,
59
+ mount: {
60
+ "/rest": [RedirectCtrl]
61
+ }
62
+ })
63
+ );
64
+ beforeAll(() => {
65
+ request = SuperTest(PlatformTest.callback());
66
+ });
67
+ afterAll(PlatformTest.reset);
68
+
69
+ it("Scenario1: GET /rest/redirect/scenario-1", async () => {
70
+ const response = await request.get("/rest/redirect/scenario-1").expect(302);
71
+
72
+ expect(response.text).toEqual("Found. Redirecting to /test");
73
+ expect(response.header.location).toEqual("/test");
74
+ });
75
+
76
+ it("Scenario1: HEAD /rest/redirect/scenario-1", async () => {
77
+ const response = await request.head("/rest/redirect/scenario-1").expect(302);
78
+
79
+ expect(response.text).toBeUndefined();
80
+ expect(response.header.location).toEqual("/test");
81
+ });
82
+
83
+ it("Scenario2: GET /rest/redirect/scenario-2", async () => {
84
+ const response = await request.get("/rest/redirect/scenario-2").expect(301);
85
+
86
+ expect(response.text).toEqual("Moved Permanently. Redirecting to /test");
87
+ expect(response.header.location).toEqual("/test");
88
+ });
89
+
90
+ it("Scenario3: GET /rest/redirect/scenario-3", async () => {
91
+ const response = await request.get("/rest/redirect/scenario-3").expect(302);
92
+
93
+ expect(response.header.location).toEqual("/another/path");
94
+ expect(response.text).toEqual("Found. Redirecting to /another/path");
95
+ });
96
+
97
+ it("Scenario4: GET /rest/redirect/scenario-4 with referer", async () => {
98
+ const response = await request.get("/rest/redirect/scenario-4").set("Referrer", "https://referrer.com").expect(302);
99
+
100
+ expect(response.text).toEqual("Found. Redirecting to https://referrer.com");
101
+ expect(response.header.location).toEqual("https://referrer.com");
102
+ });
103
+
104
+ it("Scenario5: GET /rest/redirect/scenario-5", async () => {
105
+ const response = await request.get("/rest/redirect/scenario-5").set("Referrer", "https://referrer.com").expect(200);
106
+
107
+ expect(response.text).toEqual("Hello");
108
+ expect(response.header.location).toEqual("https://referrer.com");
109
+ });
110
+
111
+ it("Scenario6: GET /rest/redirect/scenario-6", async () => {
112
+ const response = await request.get("/rest/redirect/scenario-6").set("Referrer", "https://referrer.com").expect(301);
113
+
114
+ expect(response.text).toEqual("Hello");
115
+ expect(response.header.location).toEqual("/path/to");
116
+ });
117
+
118
+ it("Scenario7: GET /rest/redirect/scenario-7", async () => {
119
+ const response = await request.get("/rest/redirect/scenario-7").set("Referrer", "https://referrer.com").expect(200);
120
+
121
+ expect(response.text).toEqual("Hello");
122
+ expect(response.header.location).toEqual("/another/path");
123
+ });
124
+ }