@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,558 @@
1
+ import {Context, Controller, Get, getContext, PathParams, PlatformResponse, PlatformTest, Post, Res} from "@tsed/common";
2
+ import {CollectionOf, ContentType, Enum, ForwardGroups, Groups, Ignore, Name, Property, Required, Returns, Status} from "@tsed/schema";
3
+ import axios from "axios";
4
+ import {createReadStream} from "fs";
5
+ import {join} from "path";
6
+ import {of} from "rxjs";
7
+ import {agent, SuperAgentStatic} from "superagent";
8
+ import SuperTest from "supertest";
9
+ import {PlatformTestingSdkOpts} from "../interfaces";
10
+
11
+ class Base {
12
+ @Ignore()
13
+ ignoreMeBase: string;
14
+
15
+ @Property()
16
+ fooBase: string;
17
+ }
18
+
19
+ class MyModel extends Base {
20
+ @Property()
21
+ foo: string;
22
+
23
+ @Ignore()
24
+ ignoreMe: string;
25
+
26
+ @Ignore((value, ctx) => ctx.endpoint)
27
+ ignoreMe2: string;
28
+ }
29
+
30
+ class EmptyModel {
31
+ raw: any;
32
+ affected?: number | null;
33
+ }
34
+
35
+ export enum EnumValue {
36
+ One = "one",
37
+ Two = "two"
38
+ }
39
+
40
+ export class NestedEnum {
41
+ @Required()
42
+ @Enum(EnumValue)
43
+ value: EnumValue;
44
+ }
45
+
46
+ export class TestNestedEnum {
47
+ @Property()
48
+ nested: NestedEnum;
49
+ }
50
+
51
+ export class ModelGroup {
52
+ @Property()
53
+ id: string;
54
+
55
+ @Groups("!creation")
56
+ groups: string;
57
+ }
58
+
59
+ class TeamModel {
60
+ @Required()
61
+ @Name("teamName")
62
+ name: string;
63
+ }
64
+
65
+ class TeamsModel {
66
+ @Required()
67
+ @CollectionOf(TeamModel)
68
+ @ForwardGroups()
69
+ teams: TeamModel[];
70
+ }
71
+
72
+ class AllowedModel {
73
+ @Property()
74
+ id: string;
75
+
76
+ @Property()
77
+ description: string;
78
+
79
+ @Groups("summary")
80
+ prop1: string; // not display by default
81
+
82
+ @Groups("details")
83
+ prop2: string; // not display by default
84
+
85
+ @Groups("admin")
86
+ sensitiveProp: string; // not displayed because it's a sensitive props
87
+ }
88
+
89
+ @Controller("/response")
90
+ class TestResponseParamsCtrl {
91
+ @Get("/scenario1/:id")
92
+ public testScenario1Get(@PathParams("id") id: number) {
93
+ return `hello-${id}`;
94
+ }
95
+
96
+ @Post("/scenario3/:id?")
97
+ @Status(204)
98
+ public testScenario3EmptyResponse(@PathParams("id") id: number, @Context() ctx: Context) {
99
+ if (id) {
100
+ return;
101
+ }
102
+
103
+ ctx.response.status(201);
104
+
105
+ return {
106
+ id: 1
107
+ };
108
+ }
109
+
110
+ @Get("/scenario5")
111
+ async testScenario5Promise() {
112
+ await new Promise((resolve) => {
113
+ resolve(undefined);
114
+ });
115
+
116
+ return {
117
+ id: 1
118
+ };
119
+ }
120
+
121
+ @Get("/scenario6")
122
+ testScenario6Observable() {
123
+ return of({id: 1});
124
+ }
125
+
126
+ @Get("/scenario6b")
127
+ async testScenario6bObservable() {
128
+ return of({id: 1});
129
+ }
130
+
131
+ @Get("/scenario7")
132
+ @ContentType("application/json")
133
+ testScenario7Stream() {
134
+ return createReadStream(join(__dirname, "../data/response.data.json"));
135
+ }
136
+
137
+ @Get("/scenario7b")
138
+ @ContentType("application/json")
139
+ async testScenario7bStream() {
140
+ return createReadStream(join(__dirname, "../data/response.data.json"));
141
+ }
142
+
143
+ @Get("/scenario9/static")
144
+ public testScenario9Get(): string {
145
+ return "value";
146
+ }
147
+
148
+ @Get("/scenario9/:id")
149
+ public testScenario9WithDynamicParam(@PathParams("id") id: number): string {
150
+ return "value" + id;
151
+ }
152
+
153
+ @Get("/scenario10")
154
+ public testScenario10() {
155
+ const model = new MyModel();
156
+ model.foo = "foo";
157
+ model.ignoreMe = "ignoreMe";
158
+ model.ignoreMe2 = "ignoreMe2";
159
+ model.fooBase = "fooBase";
160
+ model.ignoreMeBase = "ignoreMeBase";
161
+
162
+ return model;
163
+ }
164
+
165
+ @Get("/scenario11")
166
+ testScenario11() {
167
+ const t = new EmptyModel();
168
+
169
+ t.raw = 1;
170
+ t.affected = 1;
171
+
172
+ return t;
173
+ }
174
+
175
+ @Get("/scenario12")
176
+ testScenario12(@Res() res: PlatformResponse) {
177
+ res.attachment("filename");
178
+
179
+ return Buffer.from("Hello");
180
+ }
181
+
182
+ @Get("/scenario13")
183
+ async testScenario13(@Res() res: PlatformResponse) {
184
+ const http: SuperAgentStatic = agent();
185
+
186
+ const image_res = await http.get("https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png");
187
+
188
+ res.setHeader("Content-Disposition", "inline;filename=googlelogo_color_272x92dp.png;");
189
+ res.setHeader("X-Content-Type-Options", "nosniff");
190
+ res.setHeader("Content-Type", "image/png");
191
+
192
+ return image_res.body;
193
+ }
194
+
195
+ @Get("/scenario14")
196
+ public scenario14() {
197
+ return {
198
+ jsonexample: 1
199
+ };
200
+ }
201
+
202
+ @Get("/scenario15a")
203
+ testScenario15a() {
204
+ return axios.get(`https://tsed.io/api.json`, {
205
+ responseType: "json"
206
+ });
207
+ }
208
+
209
+ @Get("/scenario15b")
210
+ async testScenario15b() {
211
+ return axios.get(`https://api.tsed.io/rest/github/typed/test`, {
212
+ responseType: "json"
213
+ });
214
+ }
215
+
216
+ @Get("/scenario16")
217
+ async testScenario16(): Promise<TestNestedEnum> {
218
+ const test = new TestNestedEnum();
219
+ const nested = new NestedEnum();
220
+ nested.value = EnumValue.One;
221
+ test.nested = nested;
222
+ return test;
223
+ }
224
+
225
+ @Get("/scenario17")
226
+ @Returns(201, ModelGroup).Groups("creation")
227
+ async testScenario17(): Promise<ModelGroup> {
228
+ const model = new ModelGroup();
229
+ model.id = "id";
230
+ model.groups = "groups";
231
+
232
+ return model;
233
+ }
234
+
235
+ @Get("/scenario18")
236
+ @Returns(200, TeamsModel)
237
+ testScenario18() {
238
+ const data = new TeamsModel();
239
+ const team: any = {};
240
+ team.name = "name";
241
+ data.teams = [team];
242
+
243
+ return data;
244
+ }
245
+
246
+ @Get("/scenario19")
247
+ @Returns(200, AllowedModel).Groups("!admin").AllowedGroups("summary", "details")
248
+ async testScenario19(): Promise<AllowedModel> {
249
+ const model = new AllowedModel();
250
+ model.id = "id";
251
+ model.prop1 = "prop1";
252
+ model.description = "description";
253
+ model.prop2 = "prop2";
254
+ model.sensitiveProp = "sensitiveProp";
255
+
256
+ return model;
257
+ }
258
+
259
+ @Get("/scenario20")
260
+ async testScenario20() {
261
+ const $ctx = getContext();
262
+
263
+ return {id: $ctx?.request.query.id};
264
+ }
265
+ }
266
+
267
+ export function testResponse(options: PlatformTestingSdkOpts) {
268
+ let request: SuperTest.SuperTest<SuperTest.Test>;
269
+
270
+ beforeAll(
271
+ PlatformTest.bootstrap(options.server, {
272
+ ...options,
273
+ mount: {
274
+ "/rest": [TestResponseParamsCtrl]
275
+ }
276
+ })
277
+ );
278
+ beforeAll(() => {
279
+ request = SuperTest(PlatformTest.callback());
280
+ });
281
+ afterAll(PlatformTest.reset);
282
+
283
+ describe("Scenario1: return the id (classic)", () => {
284
+ describe("GET /rest/response/scenario1/:id", () => {
285
+ it("should return the id + test", async () => {
286
+ const response = await request.get("/rest/response/scenario1/10").expect(200);
287
+
288
+ expect(response.text).toEqual("hello-10");
289
+ });
290
+ });
291
+ });
292
+
293
+ describe("Scenario3: when response is empty or created", () => {
294
+ describe("GET /rest/response/scenario3/:id?", () => {
295
+ it("should return nothing with a 204 status", async () => {
296
+ const response = await request.post("/rest/response/scenario3/10").expect(204);
297
+
298
+ expect(response.text).toEqual("");
299
+ });
300
+
301
+ it("should return a body", async () => {
302
+ const response = await request.post("/rest/response/scenario3").expect(201);
303
+
304
+ expect(response.body).toEqual({id: 1});
305
+ });
306
+ });
307
+ });
308
+
309
+ describe("Scenario5: when endpoint response from promise", () => {
310
+ describe("GET /rest/response/scenario5", () => {
311
+ it("should return a body", async () => {
312
+ const response = await request.get("/rest/response/scenario5");
313
+
314
+ expect(response.body).toEqual({id: 1});
315
+ });
316
+ });
317
+ });
318
+
319
+ describe("Scenario6: when endpoint return an observable", () => {
320
+ describe("GET /rest/response/scenario6", () => {
321
+ it("should return a body", async () => {
322
+ const response = await request.get("/rest/response/scenario6");
323
+
324
+ expect(response.body).toEqual({id: 1});
325
+ });
326
+ });
327
+
328
+ describe("GET /rest/response/scenario6b", () => {
329
+ it("should return a body", async () => {
330
+ const response = await request.get("/rest/response/scenario6b");
331
+
332
+ expect(response.body).toEqual({id: 1});
333
+ });
334
+ });
335
+ });
336
+
337
+ describe("Scenario7: when endpoint return a stream", () => {
338
+ describe("GET /rest/response/scenario7", () => {
339
+ it("should return a body", async () => {
340
+ const response = await request.get("/rest/response/scenario7");
341
+
342
+ expect(response.body).toEqual({id: "1"});
343
+ });
344
+ });
345
+
346
+ describe("GET /rest/response/scenario7b", () => {
347
+ it("should return a body", async () => {
348
+ const response = await request.get("/rest/response/scenario7b");
349
+
350
+ expect(response.body).toEqual({id: "1"});
351
+ });
352
+ });
353
+ });
354
+
355
+ describe("Scenario9: routes without parameters must be defined first", () => {
356
+ describe("GET /rest/response/scenario9/static", () => {
357
+ it("should return the test", async () => {
358
+ const response = await request.get("/rest/response/scenario9/static").expect(200);
359
+
360
+ expect(response.text).toEqual("value");
361
+ });
362
+ });
363
+
364
+ describe("GET /rest/response/scenario9/:dynamic", () => {
365
+ it("should return the test + id", async () => {
366
+ const response = await request.get("/rest/response/scenario9/10").expect(200);
367
+
368
+ expect(response.text).toEqual("value10");
369
+ });
370
+ });
371
+
372
+ describe("GET /rest/response/scenario9/:dynamic", () => {
373
+ it("should throw a badRequest when path params isn't set as number", async () => {
374
+ const response = await request.get("/rest/response/scenario9/kkk").expect(400);
375
+
376
+ expect(response.body).toEqual({
377
+ errors: [
378
+ {
379
+ data: "kkk",
380
+ dataPath: "",
381
+ instancePath: "",
382
+ keyword: "type",
383
+ message: "must be number",
384
+ params: {
385
+ type: "number"
386
+ },
387
+ schemaPath: "#/type"
388
+ }
389
+ ],
390
+ message: 'Bad request on parameter "request.path.id".\nValue must be number. Given value: "kkk"',
391
+ name: "AJV_VALIDATION_ERROR",
392
+ status: 400
393
+ });
394
+ });
395
+ });
396
+ });
397
+
398
+ describe("Scenario10: return a model with ignored props", () => {
399
+ it("should return a body", async () => {
400
+ const response = await request.get("/rest/response/scenario10");
401
+
402
+ expect(response.body).toEqual({
403
+ foo: "foo",
404
+ fooBase: "fooBase"
405
+ });
406
+ });
407
+ });
408
+
409
+ describe("Scenario11: return models without props", () => {
410
+ it("should return a body", async () => {
411
+ const response = await request.get("/rest/response/scenario11");
412
+
413
+ expect(response.body).toEqual({
414
+ affected: 1,
415
+ raw: 1
416
+ });
417
+ });
418
+ });
419
+
420
+ describe("Scenario12: Return buffer", () => {
421
+ it("should return a body", async () => {
422
+ const response = await request.get("/rest/response/scenario12");
423
+
424
+ expect(response.headers["content-disposition"]).toEqual('attachment; filename="filename"');
425
+ expect(response.headers["content-type"]).toContain("application/octet-stream");
426
+ expect(response.headers["content-length"]).toEqual("5");
427
+ expect(response.body.toString()).toEqual("Hello");
428
+ });
429
+ });
430
+
431
+ describe("Scenario13: Return buffer", () => {
432
+ it("should return image", async () => {
433
+ const response = await request.get("/rest/response/scenario13");
434
+
435
+ expect(response.headers["content-disposition"]).toEqual("inline;filename=googlelogo_color_272x92dp.png;");
436
+ expect(response.headers["content-type"]).toContain("image/png");
437
+ expect(response.headers["content-length"]).toEqual("5969");
438
+ });
439
+ });
440
+
441
+ describe("Scenario14: Return application/json when Accept is */*", () => {
442
+ it("should return a */* content-type", async () => {
443
+ const response = await request
444
+ .get("/rest/response/scenario14")
445
+ .set("Accept", "*/*")
446
+ .set("Content-Type", "application/json")
447
+ .expect(200);
448
+
449
+ expect(response.headers["content-type"]).toEqual("application/json; charset=utf-8");
450
+ expect(response.body).toEqual({
451
+ jsonexample: 1
452
+ });
453
+ });
454
+ });
455
+ describe("Scenario15: Use axios as proxy", () => {
456
+ it("should return the response (200)", async () => {
457
+ const response = await request.get("/rest/response/scenario15a").expect(200);
458
+
459
+ expect(response.headers["content-type"]).toEqual("application/json; charset=utf-8");
460
+ });
461
+
462
+ it("should return the response (401)", async () => {
463
+ const response = await request.get("/rest/response/scenario15b").expect(401);
464
+
465
+ expect(response.headers["content-type"]).toEqual("application/json; charset=utf-8");
466
+ });
467
+ });
468
+
469
+ describe("Scenario16: Return response with nested enum", () => {
470
+ it("should return the response (200)", async () => {
471
+ const response = await request.get("/rest/response/scenario16").expect(200);
472
+
473
+ expect(response.body).toEqual({nested: {value: "one"}});
474
+ });
475
+ });
476
+
477
+ describe("Scenario17: Return response with groups on 201", () => {
478
+ it("should return the response (200)", async () => {
479
+ const response = await request.get("/rest/response/scenario17").expect(201);
480
+
481
+ expect(response.body).toEqual({id: "id"});
482
+ });
483
+ });
484
+
485
+ describe("Scenario18: Return data with alias", () => {
486
+ it("should return the response (200)", async () => {
487
+ const response = await request.get("/rest/response/scenario18").expect(200);
488
+
489
+ expect(response.body).toEqual({
490
+ teams: [
491
+ {
492
+ teamName: "name"
493
+ }
494
+ ]
495
+ });
496
+ });
497
+ });
498
+
499
+ describe("Scenario19: includes options", () => {
500
+ it("should return the response (200)", async () => {
501
+ const response = await request.get("/rest/response/scenario19").expect(200);
502
+
503
+ expect(response.body).toEqual({
504
+ description: "description",
505
+ id: "id",
506
+ prop1: "prop1",
507
+ prop2: "prop2"
508
+ });
509
+ });
510
+ it("should return the response (200 + [summary])", async () => {
511
+ const response = await request.get("/rest/response/scenario19?includes=summary").expect(200);
512
+
513
+ expect(response.body).toEqual({
514
+ description: "description",
515
+ id: "id",
516
+ prop1: "prop1"
517
+ });
518
+ });
519
+ it("should return the response (200 + [summary, details])", async () => {
520
+ const response = await request.get("/rest/response/scenario19?includes=summary&includes=details").expect(200);
521
+
522
+ expect(response.body).toEqual({
523
+ description: "description",
524
+ id: "id",
525
+ prop2: "prop2",
526
+ prop1: "prop1"
527
+ });
528
+ });
529
+ it("should return the response (200 + [summary, details, admin])", async () => {
530
+ const response = await request.get("/rest/response/scenario19?includes=summary&includes=details&includes=admin").expect(200);
531
+
532
+ expect(response.body).toEqual({
533
+ description: "description",
534
+ id: "id",
535
+ prop2: "prop2",
536
+ prop1: "prop1"
537
+ });
538
+ });
539
+ it("should return the response (200 + 'summary,details,admin')", async () => {
540
+ const response = await request.get("/rest/response/scenario19?includes=summary,details,admin").expect(200);
541
+
542
+ expect(response.body).toEqual({
543
+ description: "description",
544
+ id: "id",
545
+ prop2: "prop2",
546
+ prop1: "prop1"
547
+ });
548
+ });
549
+ });
550
+
551
+ describe("Scenario20: should return query data from Context", () => {
552
+ it("should return the response (200)", async () => {
553
+ const response = await request.get("/rest/response/scenario20?id=id").expect(200);
554
+
555
+ expect(response.body).toEqual({id: "id"});
556
+ });
557
+ });
558
+ }
@@ -0,0 +1,101 @@
1
+ import {Controller, Get, PlatformContext, PlatformTest, ResponseFilter, ResponseFilterMethods} from "@tsed/common";
2
+ import {Property, Returns} from "@tsed/schema";
3
+ import SuperTest from "supertest";
4
+ import {PlatformTestingSdkOpts} from "../interfaces";
5
+
6
+ class ResponseFilterModel {
7
+ @Property()
8
+ id: string;
9
+ }
10
+
11
+ @Controller("/response-filter")
12
+ class TestResponseFilterCtrl {
13
+ @Get("/scenario1/:id")
14
+ @Returns(200, ResponseFilterModel).Description("description")
15
+ @Returns(200, String).ContentType("text/xml")
16
+ public testScenario1() {
17
+ return {id: "id"};
18
+ }
19
+ }
20
+
21
+ @ResponseFilter("text/xml")
22
+ class XmlResponseFilter implements ResponseFilterMethods {
23
+ transform(data: unknown, ctx: PlatformContext) {
24
+ return "<xml>test</xml>";
25
+ }
26
+ }
27
+
28
+ @ResponseFilter("*/*")
29
+ class AnyResponseFilter implements ResponseFilterMethods {
30
+ transform(data: unknown, ctx: PlatformContext) {
31
+ return {data, errors: []};
32
+ }
33
+ }
34
+
35
+ export function testResponseFilter(options: PlatformTestingSdkOpts) {
36
+ let request: SuperTest.SuperTest<SuperTest.Test>;
37
+
38
+ beforeAll(
39
+ PlatformTest.bootstrap(options.server, {
40
+ ...options,
41
+ mount: {
42
+ "/rest": [TestResponseFilterCtrl]
43
+ },
44
+ responseFilters: [XmlResponseFilter, AnyResponseFilter]
45
+ })
46
+ );
47
+ beforeAll(() => {
48
+ request = SuperTest(PlatformTest.callback());
49
+ });
50
+ afterAll(PlatformTest.reset);
51
+
52
+ describe("Scenario1: when have multiple contentType", () => {
53
+ describe("GET /rest/response/scenario1/:id", () => {
54
+ it("should return the xml format", async () => {
55
+ const response = await request
56
+ .get("/rest/response-filter/scenario1/10")
57
+ .set({
58
+ Accept: "text/xml"
59
+ })
60
+ .expect(200);
61
+
62
+ expect(response.text).toEqual("<xml>test</xml>");
63
+ });
64
+ it("should return the xml format when Accept text/xml and application/json", async () => {
65
+ const response = await request
66
+ .get("/rest/response-filter/scenario1/10")
67
+ .set({
68
+ Accept: "text/xml, application/json"
69
+ })
70
+ .expect(200);
71
+
72
+ expect(response.text).toEqual("<xml>test</xml>");
73
+ });
74
+ it("should return the json format", async () => {
75
+ const response = await request
76
+ .get("/rest/response-filter/scenario1/10")
77
+ .set({
78
+ Accept: "application/json"
79
+ })
80
+ .expect(200);
81
+
82
+ expect(response.body).toEqual({
83
+ data: {
84
+ id: "id"
85
+ },
86
+ errors: []
87
+ });
88
+ });
89
+ it("should return the json format by default", async () => {
90
+ const response = await request.get("/rest/response-filter/scenario1/10").expect(200);
91
+
92
+ expect(response.body).toEqual({
93
+ data: {
94
+ id: "id"
95
+ },
96
+ errors: []
97
+ });
98
+ });
99
+ });
100
+ });
101
+ }