@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,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.testCustom404 = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const common_1 = require("@tsed/common");
6
+ const supertest_1 = tslib_1.__importDefault(require("supertest"));
7
+ function testCustom404(options) {
8
+ class CustomServer extends options.server {
9
+ }
10
+ let request;
11
+ beforeAll(common_1.PlatformTest.bootstrap(CustomServer, {
12
+ ...options,
13
+ mount: {
14
+ "/rest": []
15
+ }
16
+ }));
17
+ beforeAll(() => {
18
+ request = (0, supertest_1.default)(common_1.PlatformTest.callback());
19
+ });
20
+ afterAll(common_1.PlatformTest.reset);
21
+ it("Scenario 1: GET /", async () => {
22
+ const response = await request.get("/").expect(404);
23
+ expect(response.body).toEqual({
24
+ name: "NOT_FOUND",
25
+ message: 'Resource "/" not found',
26
+ status: 404,
27
+ errors: []
28
+ });
29
+ });
30
+ }
31
+ exports.testCustom404 = testCustom404;
32
+ //# sourceMappingURL=testCustom404.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"testCustom404.js","sourceRoot":"","sources":["../../../src/tests/testCustom404.ts"],"names":[],"mappings":";;;;AAAA,yCAA0C;AAC1C,kEAAkC;AAGlC,SAAgB,aAAa,CAAC,OAA+B;IAC3D,MAAM,YAAa,SAAQ,OAAO,CAAC,MAAM;KAAG;IAE5C,IAAI,OAA4C,CAAC;IACjD,SAAS,CACP,qBAAY,CAAC,SAAS,CAAC,YAAY,EAAE;QACnC,GAAG,OAAO;QACV,KAAK,EAAE;YACL,OAAO,EAAE,EAAE;SACZ;KACF,CAAC,CACH,CAAC;IAEF,SAAS,CAAC,GAAG,EAAE;QACb,OAAO,GAAG,IAAA,mBAAS,EAAC,qBAAY,CAAC,QAAQ,EAAE,CAAC,CAAC;IAC/C,CAAC,CAAC,CAAC;IACH,QAAQ,CAAC,qBAAY,CAAC,KAAK,CAAC,CAAC;IAE7B,EAAE,CAAC,mBAAmB,EAAE,KAAK,IAAI,EAAE;QACjC,MAAM,QAAQ,GAAQ,MAAM,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QAEzD,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC;YAC5B,IAAI,EAAE,WAAW;YACjB,OAAO,EAAE,wBAAwB;YACjC,MAAM,EAAE,GAAG;YACX,MAAM,EAAE,EAAE;SACX,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC;AA5BD,sCA4BC","sourcesContent":["import {PlatformTest} from \"@tsed/common\";\nimport SuperTest from \"supertest\";\nimport {PlatformTestingSdkOpts} from \"../interfaces\";\n\nexport function testCustom404(options: PlatformTestingSdkOpts) {\n class CustomServer extends options.server {}\n\n let request: SuperTest.SuperTest<SuperTest.Test>;\n beforeAll(\n PlatformTest.bootstrap(CustomServer, {\n ...options,\n mount: {\n \"/rest\": []\n }\n })\n );\n\n beforeAll(() => {\n request = SuperTest(PlatformTest.callback());\n });\n afterAll(PlatformTest.reset);\n\n it(\"Scenario 1: GET /\", async () => {\n const response: any = await request.get(\"/\").expect(404);\n\n expect(response.body).toEqual({\n name: \"NOT_FOUND\",\n message: 'Resource \"/\" not found',\n status: 404,\n errors: []\n });\n });\n}\n"]}
@@ -0,0 +1,252 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.testErrors = exports.ErrorsCtrl = void 0;
4
+ const tslib_1 = require("tslib");
5
+ require("@tsed/ajv");
6
+ const common_1 = require("@tsed/common");
7
+ const core_1 = require("@tsed/core");
8
+ const exceptions_1 = require("@tsed/exceptions");
9
+ const schema_1 = require("@tsed/schema");
10
+ const supertest_1 = tslib_1.__importDefault(require("supertest"));
11
+ const CustomBadRequest_1 = require("../errors/CustomBadRequest");
12
+ const CustomInternalError_1 = require("../errors/CustomInternalError");
13
+ class CustomModel {
14
+ name;
15
+ }
16
+ tslib_1.__decorate([
17
+ (0, schema_1.Required)(),
18
+ tslib_1.__metadata("design:type", String)
19
+ ], CustomModel.prototype, "name", void 0);
20
+ class CustomPropModel {
21
+ roleItem;
22
+ }
23
+ tslib_1.__decorate([
24
+ (0, schema_1.Name)("role_item"),
25
+ (0, schema_1.Required)(),
26
+ tslib_1.__metadata("design:type", String)
27
+ ], CustomPropModel.prototype, "roleItem", void 0);
28
+ let ErrorMiddleware = class ErrorMiddleware {
29
+ use(error) {
30
+ return {
31
+ message: "no error"
32
+ };
33
+ }
34
+ };
35
+ tslib_1.__decorate([
36
+ tslib_1.__param(0, (0, common_1.Err)()),
37
+ tslib_1.__metadata("design:type", Function),
38
+ tslib_1.__metadata("design:paramtypes", [Object]),
39
+ tslib_1.__metadata("design:returntype", void 0)
40
+ ], ErrorMiddleware.prototype, "use", null);
41
+ ErrorMiddleware = tslib_1.__decorate([
42
+ (0, common_1.Middleware)()
43
+ ], ErrorMiddleware);
44
+ let FakeMiddleware = class FakeMiddleware {
45
+ use() {
46
+ return {
47
+ message: "should not be called by the platform when endpoint throw an error"
48
+ };
49
+ }
50
+ };
51
+ FakeMiddleware = tslib_1.__decorate([
52
+ (0, common_1.Middleware)()
53
+ ], FakeMiddleware);
54
+ let ErrorsCtrl = class ErrorsCtrl {
55
+ scenario1() {
56
+ throw new CustomBadRequest_1.CustomBadRequest("Custom Bad Request");
57
+ }
58
+ scenario2() {
59
+ throw new Error("My error");
60
+ }
61
+ scenario3() {
62
+ throw new CustomInternalError_1.CustomInternalError("My custom error");
63
+ }
64
+ scenario4(name) {
65
+ return name;
66
+ }
67
+ scenario5(model) {
68
+ return model;
69
+ }
70
+ scenario6(model) {
71
+ return model;
72
+ }
73
+ scenario7() {
74
+ throw new CustomInternalError_1.CustomInternalError("My custom error");
75
+ }
76
+ };
77
+ tslib_1.__decorate([
78
+ (0, common_1.Get)("/scenario-1"),
79
+ (0, schema_1.Returns)(500, exceptions_1.InternalServerError).Description("Custom Bad Request"),
80
+ tslib_1.__metadata("design:type", Function),
81
+ tslib_1.__metadata("design:paramtypes", []),
82
+ tslib_1.__metadata("design:returntype", void 0)
83
+ ], ErrorsCtrl.prototype, "scenario1", null);
84
+ tslib_1.__decorate([
85
+ (0, common_1.Get)("/scenario-2"),
86
+ (0, schema_1.Returns)(500).Description("Internal Server Error"),
87
+ tslib_1.__metadata("design:type", Function),
88
+ tslib_1.__metadata("design:paramtypes", []),
89
+ tslib_1.__metadata("design:returntype", void 0)
90
+ ], ErrorsCtrl.prototype, "scenario2", null);
91
+ tslib_1.__decorate([
92
+ (0, common_1.Get)("/scenario-3"),
93
+ (0, schema_1.Returns)(400, CustomInternalError_1.CustomInternalError).Description("Bad request"),
94
+ tslib_1.__metadata("design:type", Function),
95
+ tslib_1.__metadata("design:paramtypes", []),
96
+ tslib_1.__metadata("design:returntype", void 0)
97
+ ], ErrorsCtrl.prototype, "scenario3", null);
98
+ tslib_1.__decorate([
99
+ (0, common_1.Post)("/scenario-4"),
100
+ (0, schema_1.Returns)(400),
101
+ tslib_1.__param(0, (0, schema_1.Required)()),
102
+ tslib_1.__param(0, (0, common_1.BodyParams)("name")),
103
+ tslib_1.__metadata("design:type", Function),
104
+ tslib_1.__metadata("design:paramtypes", [String]),
105
+ tslib_1.__metadata("design:returntype", void 0)
106
+ ], ErrorsCtrl.prototype, "scenario4", null);
107
+ tslib_1.__decorate([
108
+ (0, common_1.Post)("/scenario-5"),
109
+ (0, schema_1.Returns)(400, exceptions_1.BadRequest).Description("Bad request"),
110
+ tslib_1.__param(0, (0, schema_1.Required)()),
111
+ tslib_1.__param(0, (0, common_1.BodyParams)()),
112
+ tslib_1.__metadata("design:type", Function),
113
+ tslib_1.__metadata("design:paramtypes", [CustomModel]),
114
+ tslib_1.__metadata("design:returntype", void 0)
115
+ ], ErrorsCtrl.prototype, "scenario5", null);
116
+ tslib_1.__decorate([
117
+ (0, common_1.Post)("/scenario-6"),
118
+ (0, schema_1.Summary)("Throw a Required prop if prop name is required"),
119
+ (0, schema_1.Description)(`Return a required error`),
120
+ (0, schema_1.Returns)(400).Description("Bad request"),
121
+ tslib_1.__param(0, (0, schema_1.Required)()),
122
+ tslib_1.__param(0, (0, common_1.BodyParams)()),
123
+ tslib_1.__metadata("design:type", Function),
124
+ tslib_1.__metadata("design:paramtypes", [CustomPropModel]),
125
+ tslib_1.__metadata("design:returntype", void 0)
126
+ ], ErrorsCtrl.prototype, "scenario6", null);
127
+ tslib_1.__decorate([
128
+ (0, common_1.Get)("/scenario-7"),
129
+ (0, schema_1.Returns)(400).Description("Bad request"),
130
+ (0, common_1.UseAfter)(FakeMiddleware),
131
+ (0, common_1.UseAfter)(ErrorMiddleware),
132
+ tslib_1.__metadata("design:type", Function),
133
+ tslib_1.__metadata("design:paramtypes", []),
134
+ tslib_1.__metadata("design:returntype", void 0)
135
+ ], ErrorsCtrl.prototype, "scenario7", null);
136
+ ErrorsCtrl = tslib_1.__decorate([
137
+ (0, common_1.Controller)("/errors")
138
+ ], ErrorsCtrl);
139
+ exports.ErrorsCtrl = ErrorsCtrl;
140
+ function testErrors(options) {
141
+ let request;
142
+ beforeAll(common_1.PlatformTest.bootstrap(options.server, {
143
+ ...options,
144
+ env: core_1.Env.TEST,
145
+ mount: {
146
+ "/rest": [ErrorsCtrl]
147
+ }
148
+ }));
149
+ beforeAll(() => {
150
+ request = (0, supertest_1.default)(common_1.PlatformTest.callback());
151
+ });
152
+ afterAll(common_1.PlatformTest.reset);
153
+ it("Scenario 1: GET /rest/errors/scenario-1", async () => {
154
+ const response = await request.get("/rest/errors/scenario-1").expect(400);
155
+ expect(response.headers["x-header-error"]).toEqual("deny");
156
+ expect(response.body).toEqual({
157
+ name: "CUSTOM_BAD_REQUEST",
158
+ message: "Custom Bad Request",
159
+ status: 400,
160
+ errors: ["test"]
161
+ });
162
+ });
163
+ it("Scenario 2: GET /rest/errors/scenario-2", async () => {
164
+ const response = await request.get("/rest/errors/scenario-2").expect(500);
165
+ expect(response.body).toEqual({
166
+ errors: [],
167
+ message: "My error",
168
+ name: "Error",
169
+ status: 500
170
+ });
171
+ });
172
+ it("Scenario 3: GET /rest/errors/scenario-3", async () => {
173
+ const response = await request.get("/rest/errors/scenario-3").expect(500);
174
+ expect(response.headers["x-header-error"]).toEqual("deny");
175
+ expect(response.body).toEqual({
176
+ name: "CUSTOM_INTERNAL_SERVER_ERROR",
177
+ message: "My custom error",
178
+ status: 500,
179
+ errors: ["test"]
180
+ });
181
+ });
182
+ it("Scenario 4: POST /rest/errors/scenario-4", async () => {
183
+ const response = await request.post("/rest/errors/scenario-4").expect(400);
184
+ expect(response.body).toEqual({
185
+ errors: [
186
+ {
187
+ dataPath: "",
188
+ keyword: "required",
189
+ message: "It should have required parameter 'name'",
190
+ modelName: "body",
191
+ params: {
192
+ missingProperty: "name"
193
+ },
194
+ schemaPath: "#/required"
195
+ }
196
+ ],
197
+ message: "Bad request on parameter \"request.body.name\".\nIt should have required parameter 'name'",
198
+ name: "REQUIRED_VALIDATION_ERROR",
199
+ status: 400
200
+ });
201
+ });
202
+ it("Scenario 5: POST /rest/errors/scenario-5", async () => {
203
+ const response = await request.post("/rest/errors/scenario-5").expect(400);
204
+ expect(response.body).toEqual({
205
+ errors: [
206
+ {
207
+ dataPath: "",
208
+ instancePath: "",
209
+ keyword: "required",
210
+ message: "must have required property 'name'",
211
+ modelName: "CustomModel",
212
+ params: {
213
+ missingProperty: "name"
214
+ },
215
+ schemaPath: "#/required"
216
+ }
217
+ ],
218
+ message: 'Bad request on parameter "request.body".\nCustomModel must have required property \'name\'. Given value: "undefined"',
219
+ name: "AJV_VALIDATION_ERROR",
220
+ status: 400
221
+ });
222
+ });
223
+ it("Scenario 6: POST /rest/errors/scenario-6", async () => {
224
+ const response = await request.post(`/rest/errors/scenario-6`).send({}).expect(400);
225
+ expect(response.body).toEqual({
226
+ errors: [
227
+ {
228
+ dataPath: "",
229
+ instancePath: "",
230
+ keyword: "required",
231
+ message: "must have required property 'role_item'",
232
+ modelName: "CustomPropModel",
233
+ params: {
234
+ missingProperty: "role_item"
235
+ },
236
+ schemaPath: "#/required"
237
+ }
238
+ ],
239
+ message: 'Bad request on parameter "request.body".\nCustomPropModel must have required property \'role_item\'. Given value: "undefined"',
240
+ name: "AJV_VALIDATION_ERROR",
241
+ status: 400
242
+ });
243
+ });
244
+ it("Scenario 7: GET /rest/errors/scenario-7", async () => {
245
+ const response = await request.get("/rest/errors/scenario-7").expect(200);
246
+ expect(response.body).toEqual({
247
+ message: "no error"
248
+ });
249
+ });
250
+ }
251
+ exports.testErrors = testErrors;
252
+ //# sourceMappingURL=testErrors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"testErrors.js","sourceRoot":"","sources":["../../../src/tests/testErrors.ts"],"names":[],"mappings":";;;;AAAA,qBAAmB;AACnB,yCAAwG;AACxG,qCAA+B;AAC/B,iDAAiE;AACjE,yCAA2E;AAC3E,kEAAkC;AAClC,iEAA4D;AAC5D,uEAAkE;AAGlE,MAAM,WAAW;IACH,IAAI,CAAS;CAC1B;AADa;IAAX,IAAA,iBAAQ,GAAE;;yCAAc;AAG3B,MAAM,eAAe;IAGnB,QAAQ,CAAS;CAClB;AADC;IAFC,IAAA,aAAI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HD,gCA+HC","sourcesContent":["import \"@tsed/ajv\";\nimport {BodyParams, Controller, Err, Get, Middleware, PlatformTest, Post, UseAfter} from \"@tsed/common\";\nimport {Env} from \"@tsed/core\";\nimport {BadRequest, InternalServerError} from \"@tsed/exceptions\";\nimport {Description, Name, Required, Returns, Summary} from \"@tsed/schema\";\nimport SuperTest from \"supertest\";\nimport {CustomBadRequest} from \"../errors/CustomBadRequest\";\nimport {CustomInternalError} from \"../errors/CustomInternalError\";\nimport {PlatformTestingSdkOpts} from \"../interfaces\";\n\nclass CustomModel {\n @Required() name: string;\n}\n\nclass CustomPropModel {\n @Name(\"role_item\")\n @Required()\n roleItem: string;\n}\n\n@Middleware()\nclass ErrorMiddleware {\n use(@Err() error: any) {\n return {\n message: \"no error\"\n };\n }\n}\n\n@Middleware()\nclass FakeMiddleware {\n use() {\n return {\n message: \"should not be called by the platform when endpoint throw an error\"\n };\n }\n}\n\n@Controller(\"/errors\")\nexport class ErrorsCtrl {\n @Get(\"/scenario-1\")\n @Returns(500, InternalServerError).Description(\"Custom Bad Request\")\n public scenario1() {\n throw new CustomBadRequest(\"Custom Bad Request\");\n }\n\n @Get(\"/scenario-2\")\n @Returns(500).Description(\"Internal Server Error\")\n public scenario2() {\n throw new Error(\"My error\");\n }\n\n @Get(\"/scenario-3\")\n @Returns(400, CustomInternalError).Description(\"Bad request\")\n public scenario3() {\n throw new CustomInternalError(\"My custom error\");\n }\n\n @Post(\"/scenario-4\")\n @Returns(400)\n public scenario4(@Required() @BodyParams(\"name\") name: string) {\n return name;\n }\n\n @Post(\"/scenario-5\")\n @Returns(400, BadRequest).Description(\"Bad request\")\n public scenario5(\n @Required()\n @BodyParams()\n model: CustomModel\n ) {\n return model;\n }\n\n @Post(\"/scenario-6\")\n @Summary(\"Throw a Required prop if prop name is required\")\n @Description(`Return a required error`)\n @Returns(400).Description(\"Bad request\")\n public scenario6(\n @Required()\n @BodyParams()\n model: CustomPropModel\n ) {\n return model;\n }\n\n @Get(\"/scenario-7\")\n @Returns(400).Description(\"Bad request\")\n @UseAfter(FakeMiddleware)\n @UseAfter(ErrorMiddleware)\n public scenario7() {\n throw new CustomInternalError(\"My custom error\");\n }\n}\n\nexport function testErrors(options: PlatformTestingSdkOpts) {\n let request: SuperTest.SuperTest<SuperTest.Test>;\n beforeAll(\n PlatformTest.bootstrap(options.server, {\n ...options,\n env: Env.TEST,\n mount: {\n \"/rest\": [ErrorsCtrl]\n }\n })\n );\n beforeAll(() => {\n request = SuperTest(PlatformTest.callback());\n });\n afterAll(PlatformTest.reset);\n\n it(\"Scenario 1: GET /rest/errors/scenario-1\", async () => {\n const response: any = await request.get(\"/rest/errors/scenario-1\").expect(400);\n\n expect(response.headers[\"x-header-error\"]).toEqual(\"deny\");\n expect(response.body).toEqual({\n name: \"CUSTOM_BAD_REQUEST\",\n message: \"Custom Bad Request\",\n status: 400,\n errors: [\"test\"]\n });\n });\n\n it(\"Scenario 2: GET /rest/errors/scenario-2\", async () => {\n const response: any = await request.get(\"/rest/errors/scenario-2\").expect(500);\n\n expect(response.body).toEqual({\n errors: [],\n message: \"My error\",\n name: \"Error\",\n status: 500\n });\n });\n\n it(\"Scenario 3: GET /rest/errors/scenario-3\", async () => {\n const response: any = await request.get(\"/rest/errors/scenario-3\").expect(500);\n\n expect(response.headers[\"x-header-error\"]).toEqual(\"deny\");\n expect(response.body).toEqual({\n name: \"CUSTOM_INTERNAL_SERVER_ERROR\",\n message: \"My custom error\",\n status: 500,\n errors: [\"test\"]\n });\n });\n\n it(\"Scenario 4: POST /rest/errors/scenario-4\", async () => {\n const response: any = await request.post(\"/rest/errors/scenario-4\").expect(400);\n\n expect(response.body).toEqual({\n errors: [\n {\n dataPath: \"\",\n keyword: \"required\",\n message: \"It should have required parameter 'name'\",\n modelName: \"body\",\n params: {\n missingProperty: \"name\"\n },\n schemaPath: \"#/required\"\n }\n ],\n message: \"Bad request on parameter \\\"request.body.name\\\".\\nIt should have required parameter 'name'\",\n name: \"REQUIRED_VALIDATION_ERROR\",\n status: 400\n });\n });\n\n it(\"Scenario 5: POST /rest/errors/scenario-5\", async () => {\n const response: any = await request.post(\"/rest/errors/scenario-5\").expect(400);\n\n expect(response.body).toEqual({\n errors: [\n {\n dataPath: \"\",\n instancePath: \"\",\n keyword: \"required\",\n message: \"must have required property 'name'\",\n modelName: \"CustomModel\",\n params: {\n missingProperty: \"name\"\n },\n schemaPath: \"#/required\"\n }\n ],\n message: 'Bad request on parameter \"request.body\".\\nCustomModel must have required property \\'name\\'. Given value: \"undefined\"',\n name: \"AJV_VALIDATION_ERROR\",\n status: 400\n });\n });\n\n it(\"Scenario 6: POST /rest/errors/scenario-6\", async () => {\n const response: any = await request.post(`/rest/errors/scenario-6`).send({}).expect(400);\n\n expect(response.body).toEqual({\n errors: [\n {\n dataPath: \"\",\n instancePath: \"\",\n keyword: \"required\",\n message: \"must have required property 'role_item'\",\n modelName: \"CustomPropModel\",\n params: {\n missingProperty: \"role_item\"\n },\n schemaPath: \"#/required\"\n }\n ],\n message:\n 'Bad request on parameter \"request.body\".\\nCustomPropModel must have required property \\'role_item\\'. Given value: \"undefined\"',\n name: \"AJV_VALIDATION_ERROR\",\n status: 400\n });\n });\n\n it(\"Scenario 7: GET /rest/errors/scenario-7\", async () => {\n const response: any = await request.get(\"/rest/errors/scenario-7\").expect(200);\n\n expect(response.body).toEqual({\n message: \"no error\"\n });\n });\n}\n"]}
@@ -0,0 +1,110 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.testHandlers = exports.HandlersCtrl = exports.MyModel = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const common_1 = require("@tsed/common");
6
+ const schema_1 = require("@tsed/schema");
7
+ const supertest_1 = tslib_1.__importDefault(require("supertest"));
8
+ class MyModel {
9
+ id;
10
+ name;
11
+ }
12
+ tslib_1.__decorate([
13
+ (0, schema_1.Title)("ID"),
14
+ (0, schema_1.Property)(),
15
+ tslib_1.__metadata("design:type", String)
16
+ ], MyModel.prototype, "id", void 0);
17
+ tslib_1.__decorate([
18
+ (0, schema_1.Property)(),
19
+ (0, schema_1.Required)(),
20
+ tslib_1.__metadata("design:type", String)
21
+ ], MyModel.prototype, "name", void 0);
22
+ exports.MyModel = MyModel;
23
+ let HandlersCtrl = class HandlersCtrl {
24
+ scenario1(id) {
25
+ const model = new MyModel();
26
+ model.id = id;
27
+ model.name = "test";
28
+ return model;
29
+ }
30
+ scenario2(ctx, id) {
31
+ const model = new MyModel();
32
+ model.id = "1";
33
+ model.name = "test";
34
+ try {
35
+ ctx.response.status(202).body(model);
36
+ }
37
+ catch (er) {
38
+ console.log(er);
39
+ }
40
+ }
41
+ async scenario3(id) {
42
+ const model = new MyModel();
43
+ model.id = id;
44
+ model.name = "test";
45
+ return model;
46
+ }
47
+ };
48
+ tslib_1.__decorate([
49
+ (0, common_1.Get)("/scenario-1/:id") // Express style
50
+ ,
51
+ tslib_1.__param(0, (0, common_1.PathParams)("id")),
52
+ tslib_1.__metadata("design:type", Function),
53
+ tslib_1.__metadata("design:paramtypes", [String]),
54
+ tslib_1.__metadata("design:returntype", MyModel)
55
+ ], HandlersCtrl.prototype, "scenario1", null);
56
+ tslib_1.__decorate([
57
+ (0, common_1.Get)("/scenario-2/:id") // Ts.ED style with response injection
58
+ ,
59
+ tslib_1.__param(0, (0, common_1.Context)()),
60
+ tslib_1.__param(1, (0, common_1.PathParams)("id")),
61
+ tslib_1.__param(1, (0, schema_1.Required)()),
62
+ tslib_1.__metadata("design:type", Function),
63
+ tslib_1.__metadata("design:paramtypes", [Object, String]),
64
+ tslib_1.__metadata("design:returntype", void 0)
65
+ ], HandlersCtrl.prototype, "scenario2", null);
66
+ tslib_1.__decorate([
67
+ (0, common_1.Get)("/scenario-3/:id") // Ts.ED style
68
+ ,
69
+ tslib_1.__param(0, (0, common_1.PathParams)("id")),
70
+ tslib_1.__metadata("design:type", Function),
71
+ tslib_1.__metadata("design:paramtypes", [String]),
72
+ tslib_1.__metadata("design:returntype", Promise)
73
+ ], HandlersCtrl.prototype, "scenario3", null);
74
+ HandlersCtrl = tslib_1.__decorate([
75
+ (0, common_1.Controller)("/handlers")
76
+ ], HandlersCtrl);
77
+ exports.HandlersCtrl = HandlersCtrl;
78
+ function testHandlers(options) {
79
+ let request;
80
+ beforeAll(common_1.PlatformTest.bootstrap(options.server, {
81
+ ...options,
82
+ logger: {
83
+ leve: "info"
84
+ },
85
+ mount: {
86
+ "/rest": [HandlersCtrl]
87
+ }
88
+ }));
89
+ beforeAll(() => {
90
+ request = (0, supertest_1.default)(common_1.PlatformTest.callback());
91
+ });
92
+ afterAll(common_1.PlatformTest.reset);
93
+ it("Scenario 1: GET /rest/handlers/scenario-1/:id", async () => {
94
+ const { body } = await request.get("/rest/handlers/scenario-1/1").expect(200);
95
+ expect(body.id).toEqual("1");
96
+ expect(body.name).toEqual("test");
97
+ });
98
+ it("Scenario 2: GET /rest/handlers/scenario-2/:id", async () => {
99
+ const { body } = await request.get("/rest/handlers/scenario-2/1").expect(202);
100
+ expect(body.id).toEqual("1");
101
+ expect(body.name).toEqual("test");
102
+ });
103
+ it("Scenario 3: GET /rest/handlers/scenario-3/:id", async () => {
104
+ const { body } = await request.get("/rest/handlers/scenario-3/1").expect(200);
105
+ expect(body.id).toEqual("1");
106
+ expect(body.name).toEqual("test");
107
+ });
108
+ }
109
+ exports.testHandlers = testHandlers;
110
+ //# sourceMappingURL=testHandlers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"testHandlers.js","sourceRoot":"","sources":["../../../src/tests/testHandlers.ts"],"names":[],"mappings":";;;;AAAA,yCAAgF;AAChF,yCAAuD;AACvD,kEAAkC;AAGlC,MAAa,OAAO;IAGX,EAAE,CAAS;IAIX,IAAI,CAAS;CACrB;AALC;IAFC,IAAA,cAAK,EAAC,IAAI,CAAC;IACX,IAAA,iBAAQ,GAAE;;mCACO;AAIlB;IAFC,IAAA,iBAAQ,GAAE;IACV,IAAA,iBAAQ,GAAE;;qCACS;AAPtB,0BAQC;AA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M,CAAC,CAAC;IACpC,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,+CAA+C,EAAE,KAAK,IAAI,EAAE;QAC7D,MAAM,EAAC,IAAI,EAAC,GAAQ,MAAM,OAAO,CAAC,GAAG,CAAC,6BAA6B,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QAEjF,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QAC7B,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IACpC,CAAC,CAAC,CAAC;AACL,CAAC;AArCD,oCAqCC","sourcesContent":["import {Context, Controller, Get, PathParams, PlatformTest} from \"@tsed/common\";\nimport {Property, Required, Title} from \"@tsed/schema\";\nimport SuperTest from \"supertest\";\nimport {PlatformTestingSdkOpts} from \"../interfaces\";\n\nexport class MyModel {\n @Title(\"ID\")\n @Property()\n public id: string;\n\n @Property()\n @Required()\n public name: string;\n}\n\n@Controller(\"/handlers\")\nexport class HandlersCtrl {\n @Get(\"/scenario-1/:id\") // Express style\n public scenario1(@PathParams(\"id\") id: string): MyModel {\n const model = new MyModel();\n model.id = id;\n model.name = \"test\";\n\n return model;\n }\n\n @Get(\"/scenario-2/:id\") // Ts.ED style with response injection\n public scenario2(\n @Context() ctx: Context,\n @PathParams(\"id\")\n @Required()\n id: string\n ): void {\n const model = new MyModel();\n model.id = \"1\";\n model.name = \"test\";\n\n try {\n ctx.response.status(202).body(model);\n } catch (er) {\n console.log(er);\n }\n }\n\n @Get(\"/scenario-3/:id\") // Ts.ED style\n public async scenario3(@PathParams(\"id\") id: string): Promise<any> {\n const model = new MyModel();\n model.id = id;\n model.name = \"test\";\n\n return model;\n }\n}\n\nexport function testHandlers(options: PlatformTestingSdkOpts) {\n let request: SuperTest.SuperTest<SuperTest.Test>;\n beforeAll(\n PlatformTest.bootstrap(options.server, {\n ...options,\n logger: {\n leve: \"info\"\n },\n mount: {\n \"/rest\": [HandlersCtrl]\n }\n })\n );\n beforeAll(() => {\n request = SuperTest(PlatformTest.callback());\n });\n afterAll(PlatformTest.reset);\n it(\"Scenario 1: GET /rest/handlers/scenario-1/:id\", async () => {\n const {body}: any = await request.get(\"/rest/handlers/scenario-1/1\").expect(200);\n\n expect(body.id).toEqual(\"1\");\n expect(body.name).toEqual(\"test\");\n });\n\n it(\"Scenario 2: GET /rest/handlers/scenario-2/:id\", async () => {\n const {body}: any = await request.get(\"/rest/handlers/scenario-2/1\").expect(202);\n\n expect(body.id).toEqual(\"1\");\n expect(body.name).toEqual(\"test\");\n });\n\n it(\"Scenario 3: GET /rest/handlers/scenario-3/:id\", async () => {\n const {body}: any = await request.get(\"/rest/handlers/scenario-3/1\").expect(200);\n\n expect(body.id).toEqual(\"1\");\n expect(body.name).toEqual(\"test\");\n });\n}\n"]}
@@ -0,0 +1,166 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.testHeaderParams = exports.HeaderParamsCtrl = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const common_1 = require("@tsed/common");
6
+ const schema_1 = require("@tsed/schema");
7
+ const supertest_1 = tslib_1.__importDefault(require("supertest"));
8
+ let SetId = class SetId {
9
+ use(request, locals, $ctx) {
10
+ request["user"] = 1;
11
+ locals.id = "local-10909";
12
+ $ctx.set("uid", "ctx-10909");
13
+ }
14
+ };
15
+ tslib_1.__decorate([
16
+ tslib_1.__param(0, (0, common_1.Req)()),
17
+ tslib_1.__param(1, (0, common_1.Locals)()),
18
+ tslib_1.__param(2, (0, common_1.Context)()),
19
+ tslib_1.__metadata("design:type", Function),
20
+ tslib_1.__metadata("design:paramtypes", [Object, Object, Object]),
21
+ tslib_1.__metadata("design:returntype", void 0)
22
+ ], SetId.prototype, "use", null);
23
+ SetId = tslib_1.__decorate([
24
+ (0, common_1.Middleware)()
25
+ ], SetId);
26
+ let HeaderParamsCtrl = class HeaderParamsCtrl {
27
+ /**
28
+ * Handle request with a raw middleware + handler
29
+ * Get Authorization from header
30
+ * @param request
31
+ * @param auth
32
+ */
33
+ scenario1(request, auth) {
34
+ return {
35
+ user: request.user,
36
+ token: auth
37
+ };
38
+ }
39
+ scenario2(contentType) {
40
+ return { contentType };
41
+ }
42
+ testScenario3(token) {
43
+ return { token };
44
+ }
45
+ };
46
+ tslib_1.__decorate([
47
+ (0, common_1.Get)("/scenario-1"),
48
+ (0, common_1.Use)(SetId),
49
+ tslib_1.__param(0, (0, common_1.Req)()),
50
+ tslib_1.__param(1, (0, common_1.HeaderParams)("authorization")),
51
+ tslib_1.__metadata("design:type", Function),
52
+ tslib_1.__metadata("design:paramtypes", [Object, String]),
53
+ tslib_1.__metadata("design:returntype", Object)
54
+ ], HeaderParamsCtrl.prototype, "scenario1", null);
55
+ tslib_1.__decorate([
56
+ (0, common_1.Post)("/scenario-2"),
57
+ tslib_1.__param(0, (0, common_1.HeaderParams)("Content-type")),
58
+ tslib_1.__metadata("design:type", Function),
59
+ tslib_1.__metadata("design:paramtypes", [String]),
60
+ tslib_1.__metadata("design:returntype", void 0)
61
+ ], HeaderParamsCtrl.prototype, "scenario2", null);
62
+ tslib_1.__decorate([
63
+ (0, common_1.Get)("/scenario-3"),
64
+ tslib_1.__param(0, (0, common_1.HeaderParams)({
65
+ expression: "x-token",
66
+ useValidation: true
67
+ })),
68
+ tslib_1.__param(0, (0, schema_1.Required)()),
69
+ tslib_1.__param(0, (0, schema_1.Enum)("test", "gcp")),
70
+ tslib_1.__metadata("design:type", Function),
71
+ tslib_1.__metadata("design:paramtypes", [String]),
72
+ tslib_1.__metadata("design:returntype", Object)
73
+ ], HeaderParamsCtrl.prototype, "testScenario3", null);
74
+ HeaderParamsCtrl = tslib_1.__decorate([
75
+ (0, common_1.Controller)("/header-params")
76
+ ], HeaderParamsCtrl);
77
+ exports.HeaderParamsCtrl = HeaderParamsCtrl;
78
+ function testHeaderParams(options) {
79
+ let request;
80
+ beforeAll(common_1.PlatformTest.bootstrap(options.server, {
81
+ ...options,
82
+ mount: {
83
+ "/rest": [HeaderParamsCtrl]
84
+ }
85
+ }));
86
+ beforeAll(() => {
87
+ request = (0, supertest_1.default)(common_1.PlatformTest.callback());
88
+ });
89
+ afterAll(common_1.PlatformTest.reset);
90
+ describe("Scenario 1: GET /rest/header-params/scenario-1", () => {
91
+ it("should return a response with the extracted authorization from the request headers", async () => {
92
+ const { body } = await request
93
+ .get("/rest/header-params/scenario-1")
94
+ .set({
95
+ Authorization: "tokenauth"
96
+ })
97
+ .expect(200);
98
+ expect(body.user).toEqual(1);
99
+ expect(body.token).toEqual("tokenauth");
100
+ });
101
+ });
102
+ describe("Scenario 2: POST with contentType", () => {
103
+ it("should return a response with the extracted contentType from the request headers", async () => {
104
+ const response = await request
105
+ .post("/rest/header-params/scenario-2")
106
+ .set({
107
+ "Content-Type": "application/json"
108
+ })
109
+ .expect(200);
110
+ expect(response.body).toEqual({
111
+ contentType: "application/json"
112
+ });
113
+ });
114
+ });
115
+ describe("Scenario3: GET /rest/headers/scenario-3", () => {
116
+ it("should accept the header", async () => {
117
+ const response = await request.get("/rest/header-params/scenario-3").set("x-token", "test").expect(200);
118
+ expect(response.body).toEqual({
119
+ token: "test"
120
+ });
121
+ });
122
+ it("should not validate the header", async () => {
123
+ const response = await request.get("/rest/header-params/scenario-3").expect(400);
124
+ expect(response.body).toEqual({
125
+ errors: [
126
+ {
127
+ dataPath: "",
128
+ keyword: "required",
129
+ message: "It should have required parameter 'x-token'",
130
+ modelName: "header",
131
+ params: {
132
+ missingProperty: "x-token"
133
+ },
134
+ schemaPath: "#/required"
135
+ }
136
+ ],
137
+ message: "Bad request on parameter \"request.header.x-token\".\nIt should have required parameter 'x-token'",
138
+ name: "REQUIRED_VALIDATION_ERROR",
139
+ status: 400
140
+ });
141
+ });
142
+ it("should not validate the header - enum", async () => {
143
+ const response = await request.get("/rest/header-params/scenario-3").set("x-token", "invalid").expect(400);
144
+ expect(response.body).toEqual({
145
+ errors: [
146
+ {
147
+ data: "invalid",
148
+ dataPath: "",
149
+ instancePath: "",
150
+ keyword: "enum",
151
+ message: "must be equal to one of the allowed values",
152
+ params: {
153
+ allowedValues: ["test", "gcp"]
154
+ },
155
+ schemaPath: "#/enum"
156
+ }
157
+ ],
158
+ message: 'Bad request on parameter "request.header.x-token".\nValue must be equal to one of the allowed values. Given value: "invalid"',
159
+ name: "AJV_VALIDATION_ERROR",
160
+ status: 400
161
+ });
162
+ });
163
+ });
164
+ }
165
+ exports.testHeaderParams = testHeaderParams;
166
+ //# sourceMappingURL=testHeaderParams.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"testHeaderParams.js","sourceRoot":"","sources":["../../../src/tests/testHeaderParams.ts"],"names":[],"mappings":";;;;AAAA,yCAAsH;AACtH,yCAA4C;AAC5C,kEAAkC;AAIlC,IAAM,KAAK,GAAX,MAAM,KAAK;IACT,GAAG,CAAQ,OAAY,EAAY,MAAW,EAAa,IAAa;QACtE,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QACpB,MAAM,CAAC,EAAE,GAAG,aAAa,CAAC;QAC1B,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC;IAC/B,CAAC;CACF,CAAA;AALC;IAAK,mBAAA,IAAA,YAAG,GAAE,CAAA;IAAgB,mBAAA,IAAA,eAAM,GAAE,CAAA;IAAe,mBAAA,IAAA,gBAAO,GAAE,CAAA;;;;gCAIzD;AALG,KAAK;IADV,IAAA,mBAAU,GAAE;GACP,KAAK,CAMV;AAGD,IAAa,gBAAgB,GAA7B,MAAa,gBAAgB;IAC3B;;;;;OAKG;IAGI,SAAS,CAAQ,OAAY,EAAiC,IAAY;QAC/E,OAAO;YACL,IAAI,EAAG,OAAe,CAAC,IAAI;YAC3B,KAAK,EAAE,IAAI;SACZ,CAAC;IACJ,CAAC;IAGD,SAAS,CAA+B,WAAmB;QACzD,OAAO,EAAC,WAAW,EAAC,CAAC;IACvB,CAAC;IAGD,aAAa,CAO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\"@tsed/common\";\nimport {Enum, Required} from \"@tsed/schema\";\nimport SuperTest from \"supertest\";\nimport {PlatformTestingSdkOpts} from \"../interfaces\";\n\n@Middleware()\nclass SetId {\n use(@Req() request: any, @Locals() locals: any, @Context() $ctx: Context) {\n request[\"user\"] = 1;\n locals.id = \"local-10909\";\n $ctx.set(\"uid\", \"ctx-10909\");\n }\n}\n\n@Controller(\"/header-params\")\nexport class HeaderParamsCtrl {\n /**\n * Handle request with a raw middleware + handler\n * Get Authorization from header\n * @param request\n * @param auth\n */\n @Get(\"/scenario-1\")\n @Use(SetId)\n public scenario1(@Req() request: Req, @HeaderParams(\"authorization\") auth: string): any {\n return {\n user: (request as any).user,\n token: auth\n };\n }\n\n @Post(\"/scenario-2\")\n scenario2(@HeaderParams(\"Content-type\") contentType: string) {\n return {contentType};\n }\n\n @Get(\"/scenario-3\")\n testScenario3(\n @HeaderParams({\n expression: \"x-token\",\n useValidation: true\n })\n @Required()\n @Enum(\"test\", \"gcp\")\n token: string\n ): any {\n return {token};\n }\n}\n\nexport function testHeaderParams(options: PlatformTestingSdkOpts) {\n let request: SuperTest.SuperTest<SuperTest.Test>;\n beforeAll(\n PlatformTest.bootstrap(options.server, {\n ...options,\n mount: {\n \"/rest\": [HeaderParamsCtrl]\n }\n })\n );\n beforeAll(() => {\n request = SuperTest(PlatformTest.callback());\n });\n afterAll(PlatformTest.reset);\n\n describe(\"Scenario 1: GET /rest/header-params/scenario-1\", () => {\n it(\"should return a response with the extracted authorization from the request headers\", async () => {\n const {body}: any = await request\n .get(\"/rest/header-params/scenario-1\")\n .set({\n Authorization: \"tokenauth\"\n })\n .expect(200);\n\n expect(body.user).toEqual(1);\n expect(body.token).toEqual(\"tokenauth\");\n });\n });\n\n describe(\"Scenario 2: POST with contentType\", () => {\n it(\"should return a response with the extracted contentType from the request headers\", async () => {\n const response = await request\n .post(\"/rest/header-params/scenario-2\")\n .set({\n \"Content-Type\": \"application/json\"\n })\n .expect(200);\n\n expect(response.body).toEqual({\n contentType: \"application/json\"\n });\n });\n });\n describe(\"Scenario3: GET /rest/headers/scenario-3\", () => {\n it(\"should accept the header\", async () => {\n const response = await request.get(\"/rest/header-params/scenario-3\").set(\"x-token\", \"test\").expect(200);\n\n expect(response.body).toEqual({\n token: \"test\"\n });\n });\n\n it(\"should not validate the header\", async () => {\n const response = await request.get(\"/rest/header-params/scenario-3\").expect(400);\n\n expect(response.body).toEqual({\n errors: [\n {\n dataPath: \"\",\n keyword: \"required\",\n message: \"It should have required parameter 'x-token'\",\n modelName: \"header\",\n params: {\n missingProperty: \"x-token\"\n },\n schemaPath: \"#/required\"\n }\n ],\n message: \"Bad request on parameter \\\"request.header.x-token\\\".\\nIt should have required parameter 'x-token'\",\n name: \"REQUIRED_VALIDATION_ERROR\",\n status: 400\n });\n });\n\n it(\"should not validate the header - enum\", async () => {\n const response = await request.get(\"/rest/header-params/scenario-3\").set(\"x-token\", \"invalid\").expect(400);\n\n expect(response.body).toEqual({\n errors: [\n {\n data: \"invalid\",\n dataPath: \"\",\n instancePath: \"\",\n keyword: \"enum\",\n message: \"must be equal to one of the allowed values\",\n params: {\n allowedValues: [\"test\", \"gcp\"]\n },\n schemaPath: \"#/enum\"\n }\n ],\n message:\n 'Bad request on parameter \"request.header.x-token\".\\nValue must be equal to one of the allowed values. Given value: \"invalid\"',\n name: \"AJV_VALIDATION_ERROR\",\n status: 400\n });\n });\n });\n}\n"]}