@tsed/platform-test-sdk 8.11.0 → 8.11.2-rc.1

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