@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.
- package/lib/cjs/components/PlatformTestSdk.js +31 -0
- package/lib/cjs/components/PlatformTestSdk.js.map +1 -0
- package/lib/cjs/data/swagger.json +32 -0
- package/lib/cjs/errors/CustomBadRequest.js +18 -0
- package/lib/cjs/errors/CustomBadRequest.js.map +1 -0
- package/lib/cjs/errors/CustomInternalError.js +18 -0
- package/lib/cjs/errors/CustomInternalError.js.map +1 -0
- package/lib/cjs/index.js +42 -0
- package/lib/cjs/index.js.map +1 -0
- package/lib/cjs/interfaces/PlatformTestingSdkOpts.js +3 -0
- package/lib/cjs/interfaces/PlatformTestingSdkOpts.js.map +1 -0
- package/lib/cjs/interfaces/index.js +8 -0
- package/lib/cjs/interfaces/index.js.map +1 -0
- package/lib/cjs/modules/feature/FeatureModule.js +17 -0
- package/lib/cjs/modules/feature/FeatureModule.js.map +1 -0
- package/lib/cjs/modules/feature/controllers/FeatureController.js +23 -0
- package/lib/cjs/modules/feature/controllers/FeatureController.js.map +1 -0
- package/lib/cjs/package.json +3 -0
- package/lib/cjs/tests/exports.js +66 -0
- package/lib/cjs/tests/exports.js.map +1 -0
- package/lib/cjs/tests/testAcceptMime.js +103 -0
- package/lib/cjs/tests/testAcceptMime.js.map +1 -0
- package/lib/cjs/tests/testAuth.js +343 -0
- package/lib/cjs/tests/testAuth.js.map +1 -0
- package/lib/cjs/tests/testBodyParams.js +526 -0
- package/lib/cjs/tests/testBodyParams.js.map +1 -0
- package/lib/cjs/tests/testCache.js +292 -0
- package/lib/cjs/tests/testCache.js.map +1 -0
- package/lib/cjs/tests/testChildrenControllers.js +64 -0
- package/lib/cjs/tests/testChildrenControllers.js.map +1 -0
- package/lib/cjs/tests/testCookies.js +45 -0
- package/lib/cjs/tests/testCookies.js.map +1 -0
- package/lib/cjs/tests/testCtrlInheritance.js +161 -0
- package/lib/cjs/tests/testCtrlInheritance.js.map +1 -0
- package/lib/cjs/tests/testCustom404.js +32 -0
- package/lib/cjs/tests/testCustom404.js.map +1 -0
- package/lib/cjs/tests/testErrors.js +252 -0
- package/lib/cjs/tests/testErrors.js.map +1 -0
- package/lib/cjs/tests/testHandlers.js +110 -0
- package/lib/cjs/tests/testHandlers.js.map +1 -0
- package/lib/cjs/tests/testHeaderParams.js +166 -0
- package/lib/cjs/tests/testHeaderParams.js.map +1 -0
- package/lib/cjs/tests/testHeaders.js +101 -0
- package/lib/cjs/tests/testHeaders.js.map +1 -0
- package/lib/cjs/tests/testInheritanceController.js +173 -0
- package/lib/cjs/tests/testInheritanceController.js.map +1 -0
- package/lib/cjs/tests/testLocals.js +63 -0
- package/lib/cjs/tests/testLocals.js.map +1 -0
- package/lib/cjs/tests/testLocation.js +42 -0
- package/lib/cjs/tests/testLocation.js.map +1 -0
- package/lib/cjs/tests/testMiddlewares.js +210 -0
- package/lib/cjs/tests/testMiddlewares.js.map +1 -0
- package/lib/cjs/tests/testModule.js +49 -0
- package/lib/cjs/tests/testModule.js.map +1 -0
- package/lib/cjs/tests/testMulter.js +162 -0
- package/lib/cjs/tests/testMulter.js.map +1 -0
- package/lib/cjs/tests/testPathParams.js +142 -0
- package/lib/cjs/tests/testPathParams.js.map +1 -0
- package/lib/cjs/tests/testQueryParams.js +354 -0
- package/lib/cjs/tests/testQueryParams.js.map +1 -0
- package/lib/cjs/tests/testRedirect.js +143 -0
- package/lib/cjs/tests/testRedirect.js.map +1 -0
- package/lib/cjs/tests/testResponse.js +622 -0
- package/lib/cjs/tests/testResponse.js.map +1 -0
- package/lib/cjs/tests/testResponseFilter.js +107 -0
- package/lib/cjs/tests/testResponseFilter.js.map +1 -0
- package/lib/cjs/tests/testRouting.js +75 -0
- package/lib/cjs/tests/testRouting.js.map +1 -0
- package/lib/cjs/tests/testScopeRequest.js +116 -0
- package/lib/cjs/tests/testScopeRequest.js.map +1 -0
- package/lib/cjs/tests/testSession.js +128 -0
- package/lib/cjs/tests/testSession.js.map +1 -0
- package/lib/cjs/tests/testStatics.js +58 -0
- package/lib/cjs/tests/testStatics.js.map +1 -0
- package/lib/cjs/tests/testView.js +83 -0
- package/lib/cjs/tests/testView.js.map +1 -0
- package/lib/esm/components/PlatformTestSdk.js +27 -0
- package/lib/esm/components/PlatformTestSdk.js.map +1 -0
- package/lib/esm/data/swagger.json +32 -0
- package/lib/esm/errors/CustomBadRequest.js +14 -0
- package/lib/esm/errors/CustomBadRequest.js.map +1 -0
- package/lib/esm/errors/CustomInternalError.js +14 -0
- package/lib/esm/errors/CustomInternalError.js.map +1 -0
- package/lib/esm/index.js +39 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/interfaces/PlatformTestingSdkOpts.js +2 -0
- package/lib/esm/interfaces/PlatformTestingSdkOpts.js.map +1 -0
- package/lib/esm/interfaces/index.js +5 -0
- package/lib/esm/interfaces/index.js.map +1 -0
- package/lib/esm/modules/feature/FeatureModule.js +14 -0
- package/lib/esm/modules/feature/FeatureModule.js.map +1 -0
- package/lib/esm/modules/feature/controllers/FeatureController.js +20 -0
- package/lib/esm/modules/feature/controllers/FeatureController.js.map +1 -0
- package/lib/esm/package.json +3 -0
- package/lib/esm/tests/exports.js +63 -0
- package/lib/esm/tests/exports.js.map +1 -0
- package/lib/esm/tests/testAcceptMime.js +99 -0
- package/lib/esm/tests/testAcceptMime.js.map +1 -0
- package/lib/esm/tests/testAuth.js +338 -0
- package/lib/esm/tests/testAuth.js.map +1 -0
- package/lib/esm/tests/testBodyParams.js +522 -0
- package/lib/esm/tests/testBodyParams.js.map +1 -0
- package/lib/esm/tests/testCache.js +288 -0
- package/lib/esm/tests/testCache.js.map +1 -0
- package/lib/esm/tests/testChildrenControllers.js +60 -0
- package/lib/esm/tests/testChildrenControllers.js.map +1 -0
- package/lib/esm/tests/testCookies.js +41 -0
- package/lib/esm/tests/testCookies.js.map +1 -0
- package/lib/esm/tests/testCtrlInheritance.js +156 -0
- package/lib/esm/tests/testCtrlInheritance.js.map +1 -0
- package/lib/esm/tests/testCustom404.js +27 -0
- package/lib/esm/tests/testCustom404.js.map +1 -0
- package/lib/esm/tests/testErrors.js +248 -0
- package/lib/esm/tests/testErrors.js.map +1 -0
- package/lib/esm/tests/testHandlers.js +105 -0
- package/lib/esm/tests/testHandlers.js.map +1 -0
- package/lib/esm/tests/testHeaderParams.js +162 -0
- package/lib/esm/tests/testHeaderParams.js.map +1 -0
- package/lib/esm/tests/testHeaders.js +97 -0
- package/lib/esm/tests/testHeaders.js.map +1 -0
- package/lib/esm/tests/testInheritanceController.js +167 -0
- package/lib/esm/tests/testInheritanceController.js.map +1 -0
- package/lib/esm/tests/testLocals.js +59 -0
- package/lib/esm/tests/testLocals.js.map +1 -0
- package/lib/esm/tests/testLocation.js +38 -0
- package/lib/esm/tests/testLocation.js.map +1 -0
- package/lib/esm/tests/testMiddlewares.js +206 -0
- package/lib/esm/tests/testMiddlewares.js.map +1 -0
- package/lib/esm/tests/testModule.js +45 -0
- package/lib/esm/tests/testModule.js.map +1 -0
- package/lib/esm/tests/testMulter.js +156 -0
- package/lib/esm/tests/testMulter.js.map +1 -0
- package/lib/esm/tests/testPathParams.js +138 -0
- package/lib/esm/tests/testPathParams.js.map +1 -0
- package/lib/esm/tests/testQueryParams.js +349 -0
- package/lib/esm/tests/testQueryParams.js.map +1 -0
- package/lib/esm/tests/testRedirect.js +139 -0
- package/lib/esm/tests/testRedirect.js.map +1 -0
- package/lib/esm/tests/testResponse.js +615 -0
- package/lib/esm/tests/testResponse.js.map +1 -0
- package/lib/esm/tests/testResponseFilter.js +103 -0
- package/lib/esm/tests/testResponseFilter.js.map +1 -0
- package/lib/esm/tests/testRouting.js +71 -0
- package/lib/esm/tests/testRouting.js.map +1 -0
- package/lib/esm/tests/testScopeRequest.js +112 -0
- package/lib/esm/tests/testScopeRequest.js.map +1 -0
- package/lib/esm/tests/testSession.js +122 -0
- package/lib/esm/tests/testSession.js.map +1 -0
- package/lib/esm/tests/testStatics.js +54 -0
- package/lib/esm/tests/testStatics.js.map +1 -0
- package/lib/esm/tests/testView.js +79 -0
- package/lib/esm/tests/testView.js.map +1 -0
- package/lib/types/components/PlatformTestSdk.d.ts +9 -0
- package/lib/types/errors/CustomBadRequest.d.ts +8 -0
- package/lib/types/errors/CustomInternalError.d.ts +8 -0
- package/lib/types/index.d.ts +38 -0
- package/lib/types/interfaces/PlatformTestingSdkOpts.d.ts +8 -0
- package/lib/types/interfaces/index.d.ts +4 -0
- package/lib/types/modules/feature/FeatureModule.d.ts +2 -0
- package/lib/types/modules/feature/controllers/FeatureController.d.ts +3 -0
- package/lib/types/tests/exports.d.ts +2 -0
- package/lib/types/tests/testAcceptMime.d.ts +2 -0
- package/lib/types/tests/testAuth.d.ts +8 -0
- package/lib/types/tests/testBodyParams.d.ts +3 -0
- package/lib/types/tests/testCache.d.ts +2 -0
- package/lib/types/tests/testChildrenControllers.d.ts +2 -0
- package/lib/types/tests/testCookies.d.ts +11 -0
- package/lib/types/tests/testCtrlInheritance.d.ts +18 -0
- package/lib/types/tests/testCustom404.d.ts +2 -0
- package/lib/types/tests/testErrors.d.ts +19 -0
- package/lib/types/tests/testHandlers.d.ts +12 -0
- package/lib/types/tests/testHeaderParams.d.ts +16 -0
- package/lib/types/tests/testHeaders.d.ts +8 -0
- package/lib/types/tests/testInheritanceController.d.ts +28 -0
- package/lib/types/tests/testLocals.d.ts +2 -0
- package/lib/types/tests/testLocation.d.ts +2 -0
- package/lib/types/tests/testMiddlewares.d.ts +8 -0
- package/lib/types/tests/testModule.d.ts +2 -0
- package/lib/types/tests/testMulter.d.ts +21 -0
- package/lib/types/tests/testPathParams.d.ts +2 -0
- package/lib/types/tests/testQueryParams.d.ts +6 -0
- package/lib/types/tests/testRedirect.d.ts +2 -0
- package/lib/types/tests/testResponse.d.ts +16 -0
- package/lib/types/tests/testResponseFilter.d.ts +2 -0
- package/lib/types/tests/testRouting.d.ts +7 -0
- package/lib/types/tests/testScopeRequest.d.ts +12 -0
- package/lib/types/tests/testSession.d.ts +16 -0
- package/lib/types/tests/testStatics.d.ts +2 -0
- package/lib/types/tests/testView.d.ts +2 -0
- package/package.json +44 -0
- package/src/components/PlatformTestSdk.ts +29 -0
- package/src/data/file.txt +1 -0
- package/src/data/response.data.json +3 -0
- package/src/data/swagger.json +32 -0
- package/src/errors/CustomBadRequest.ts +16 -0
- package/src/errors/CustomInternalError.ts +16 -0
- package/src/index.ts +39 -0
- package/src/interfaces/PlatformTestingSdkOpts.ts +10 -0
- package/src/interfaces/index.ts +5 -0
- package/src/modules/feature/FeatureModule.ts +9 -0
- package/src/modules/feature/controllers/FeatureController.ts +11 -0
- package/src/tests/exports.ts +69 -0
- package/src/tests/testAcceptMime.ts +95 -0
- package/src/tests/testAuth.ts +339 -0
- package/src/tests/testBodyParams.ts +455 -0
- package/src/tests/testCache.ts +270 -0
- package/src/tests/testChildrenControllers.ts +55 -0
- package/src/tests/testCookies.ts +36 -0
- package/src/tests/testCtrlInheritance.ts +140 -0
- package/src/tests/testCustom404.ts +33 -0
- package/src/tests/testErrors.ts +223 -0
- package/src/tests/testHandlers.ts +92 -0
- package/src/tests/testHeaderParams.ts +149 -0
- package/src/tests/testHeaders.ts +91 -0
- package/src/tests/testInheritanceController.ts +156 -0
- package/src/tests/testLocals.ts +46 -0
- package/src/tests/testLocation.ts +37 -0
- package/src/tests/testMiddlewares.ts +159 -0
- package/src/tests/testModule.ts +46 -0
- package/src/tests/testMulter.ts +140 -0
- package/src/tests/testPathParams.ts +122 -0
- package/src/tests/testQueryParams.ts +323 -0
- package/src/tests/testRedirect.ts +124 -0
- package/src/tests/testResponse.ts +558 -0
- package/src/tests/testResponseFilter.ts +101 -0
- package/src/tests/testRouting.ts +65 -0
- package/src/tests/testScopeRequest.ts +111 -0
- package/src/tests/testSession.ts +104 -0
- package/src/tests/testStatics.ts +61 -0
- package/src/tests/testView.ts +68 -0
- package/tsconfig.compile.esm.json +12 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"testCustom404.js","sourceRoot":"","sources":["../../../src/tests/testCustom404.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAC,MAAM,cAAc,CAAC;AAC1C,OAAO,SAAS,MAAM,WAAW,CAAC;AAGlC,MAAM,UAAU,aAAa,CAAC,OAA+B;IAC3D,MAAM,YAAa,SAAQ,OAAO,CAAC,MAAM;KAAG;IAE5C,IAAI,OAA4C,CAAC;IACjD,SAAS,CACP,YAAY,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,SAAS,CAAC,YAAY,CAAC,QAAQ,EAAE,CAAC,CAAC;IAC/C,CAAC,CAAC,CAAC;IACH,QAAQ,CAAC,YAAY,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","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,248 @@
|
|
|
1
|
+
import { __decorate, __metadata, __param } from "tslib";
|
|
2
|
+
import "@tsed/ajv";
|
|
3
|
+
import { BodyParams, Controller, Err, Get, Middleware, PlatformTest, Post, UseAfter } from "@tsed/common";
|
|
4
|
+
import { Env } from "@tsed/core";
|
|
5
|
+
import { BadRequest, InternalServerError } from "@tsed/exceptions";
|
|
6
|
+
import { Description, Name, Required, Returns, Summary } from "@tsed/schema";
|
|
7
|
+
import SuperTest from "supertest";
|
|
8
|
+
import { CustomBadRequest } from "../errors/CustomBadRequest.js";
|
|
9
|
+
import { CustomInternalError } from "../errors/CustomInternalError.js";
|
|
10
|
+
class CustomModel {
|
|
11
|
+
name;
|
|
12
|
+
}
|
|
13
|
+
__decorate([
|
|
14
|
+
Required(),
|
|
15
|
+
__metadata("design:type", String)
|
|
16
|
+
], CustomModel.prototype, "name", void 0);
|
|
17
|
+
class CustomPropModel {
|
|
18
|
+
roleItem;
|
|
19
|
+
}
|
|
20
|
+
__decorate([
|
|
21
|
+
Name("role_item"),
|
|
22
|
+
Required(),
|
|
23
|
+
__metadata("design:type", String)
|
|
24
|
+
], CustomPropModel.prototype, "roleItem", void 0);
|
|
25
|
+
let ErrorMiddleware = class ErrorMiddleware {
|
|
26
|
+
use(error) {
|
|
27
|
+
return {
|
|
28
|
+
message: "no error"
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
__decorate([
|
|
33
|
+
__param(0, Err()),
|
|
34
|
+
__metadata("design:type", Function),
|
|
35
|
+
__metadata("design:paramtypes", [Object]),
|
|
36
|
+
__metadata("design:returntype", void 0)
|
|
37
|
+
], ErrorMiddleware.prototype, "use", null);
|
|
38
|
+
ErrorMiddleware = __decorate([
|
|
39
|
+
Middleware()
|
|
40
|
+
], ErrorMiddleware);
|
|
41
|
+
let FakeMiddleware = class FakeMiddleware {
|
|
42
|
+
use() {
|
|
43
|
+
return {
|
|
44
|
+
message: "should not be called by the platform when endpoint throw an error"
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
FakeMiddleware = __decorate([
|
|
49
|
+
Middleware()
|
|
50
|
+
], FakeMiddleware);
|
|
51
|
+
let ErrorsCtrl = class ErrorsCtrl {
|
|
52
|
+
scenario1() {
|
|
53
|
+
throw new CustomBadRequest("Custom Bad Request");
|
|
54
|
+
}
|
|
55
|
+
scenario2() {
|
|
56
|
+
throw new Error("My error");
|
|
57
|
+
}
|
|
58
|
+
scenario3() {
|
|
59
|
+
throw new CustomInternalError("My custom error");
|
|
60
|
+
}
|
|
61
|
+
scenario4(name) {
|
|
62
|
+
return name;
|
|
63
|
+
}
|
|
64
|
+
scenario5(model) {
|
|
65
|
+
return model;
|
|
66
|
+
}
|
|
67
|
+
scenario6(model) {
|
|
68
|
+
return model;
|
|
69
|
+
}
|
|
70
|
+
scenario7() {
|
|
71
|
+
throw new CustomInternalError("My custom error");
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
__decorate([
|
|
75
|
+
Get("/scenario-1"),
|
|
76
|
+
Returns(500, InternalServerError).Description("Custom Bad Request"),
|
|
77
|
+
__metadata("design:type", Function),
|
|
78
|
+
__metadata("design:paramtypes", []),
|
|
79
|
+
__metadata("design:returntype", void 0)
|
|
80
|
+
], ErrorsCtrl.prototype, "scenario1", null);
|
|
81
|
+
__decorate([
|
|
82
|
+
Get("/scenario-2"),
|
|
83
|
+
Returns(500).Description("Internal Server Error"),
|
|
84
|
+
__metadata("design:type", Function),
|
|
85
|
+
__metadata("design:paramtypes", []),
|
|
86
|
+
__metadata("design:returntype", void 0)
|
|
87
|
+
], ErrorsCtrl.prototype, "scenario2", null);
|
|
88
|
+
__decorate([
|
|
89
|
+
Get("/scenario-3"),
|
|
90
|
+
Returns(400, CustomInternalError).Description("Bad request"),
|
|
91
|
+
__metadata("design:type", Function),
|
|
92
|
+
__metadata("design:paramtypes", []),
|
|
93
|
+
__metadata("design:returntype", void 0)
|
|
94
|
+
], ErrorsCtrl.prototype, "scenario3", null);
|
|
95
|
+
__decorate([
|
|
96
|
+
Post("/scenario-4"),
|
|
97
|
+
Returns(400),
|
|
98
|
+
__param(0, Required()),
|
|
99
|
+
__param(0, BodyParams("name")),
|
|
100
|
+
__metadata("design:type", Function),
|
|
101
|
+
__metadata("design:paramtypes", [String]),
|
|
102
|
+
__metadata("design:returntype", void 0)
|
|
103
|
+
], ErrorsCtrl.prototype, "scenario4", null);
|
|
104
|
+
__decorate([
|
|
105
|
+
Post("/scenario-5"),
|
|
106
|
+
Returns(400, BadRequest).Description("Bad request"),
|
|
107
|
+
__param(0, Required()),
|
|
108
|
+
__param(0, BodyParams()),
|
|
109
|
+
__metadata("design:type", Function),
|
|
110
|
+
__metadata("design:paramtypes", [CustomModel]),
|
|
111
|
+
__metadata("design:returntype", void 0)
|
|
112
|
+
], ErrorsCtrl.prototype, "scenario5", null);
|
|
113
|
+
__decorate([
|
|
114
|
+
Post("/scenario-6"),
|
|
115
|
+
Summary("Throw a Required prop if prop name is required"),
|
|
116
|
+
Description(`Return a required error`),
|
|
117
|
+
Returns(400).Description("Bad request"),
|
|
118
|
+
__param(0, Required()),
|
|
119
|
+
__param(0, BodyParams()),
|
|
120
|
+
__metadata("design:type", Function),
|
|
121
|
+
__metadata("design:paramtypes", [CustomPropModel]),
|
|
122
|
+
__metadata("design:returntype", void 0)
|
|
123
|
+
], ErrorsCtrl.prototype, "scenario6", null);
|
|
124
|
+
__decorate([
|
|
125
|
+
Get("/scenario-7"),
|
|
126
|
+
Returns(400).Description("Bad request"),
|
|
127
|
+
UseAfter(FakeMiddleware),
|
|
128
|
+
UseAfter(ErrorMiddleware),
|
|
129
|
+
__metadata("design:type", Function),
|
|
130
|
+
__metadata("design:paramtypes", []),
|
|
131
|
+
__metadata("design:returntype", void 0)
|
|
132
|
+
], ErrorsCtrl.prototype, "scenario7", null);
|
|
133
|
+
ErrorsCtrl = __decorate([
|
|
134
|
+
Controller("/errors")
|
|
135
|
+
], ErrorsCtrl);
|
|
136
|
+
export { ErrorsCtrl };
|
|
137
|
+
export function testErrors(options) {
|
|
138
|
+
let request;
|
|
139
|
+
beforeAll(PlatformTest.bootstrap(options.server, {
|
|
140
|
+
...options,
|
|
141
|
+
env: Env.TEST,
|
|
142
|
+
mount: {
|
|
143
|
+
"/rest": [ErrorsCtrl]
|
|
144
|
+
}
|
|
145
|
+
}));
|
|
146
|
+
beforeAll(() => {
|
|
147
|
+
request = SuperTest(PlatformTest.callback());
|
|
148
|
+
});
|
|
149
|
+
afterAll(PlatformTest.reset);
|
|
150
|
+
it("Scenario 1: GET /rest/errors/scenario-1", async () => {
|
|
151
|
+
const response = await request.get("/rest/errors/scenario-1").expect(400);
|
|
152
|
+
expect(response.headers["x-header-error"]).toEqual("deny");
|
|
153
|
+
expect(response.body).toEqual({
|
|
154
|
+
name: "CUSTOM_BAD_REQUEST",
|
|
155
|
+
message: "Custom Bad Request",
|
|
156
|
+
status: 400,
|
|
157
|
+
errors: ["test"]
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
it("Scenario 2: GET /rest/errors/scenario-2", async () => {
|
|
161
|
+
const response = await request.get("/rest/errors/scenario-2").expect(500);
|
|
162
|
+
expect(response.body).toEqual({
|
|
163
|
+
errors: [],
|
|
164
|
+
message: "My error",
|
|
165
|
+
name: "Error",
|
|
166
|
+
status: 500
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
it("Scenario 3: GET /rest/errors/scenario-3", async () => {
|
|
170
|
+
const response = await request.get("/rest/errors/scenario-3").expect(500);
|
|
171
|
+
expect(response.headers["x-header-error"]).toEqual("deny");
|
|
172
|
+
expect(response.body).toEqual({
|
|
173
|
+
name: "CUSTOM_INTERNAL_SERVER_ERROR",
|
|
174
|
+
message: "My custom error",
|
|
175
|
+
status: 500,
|
|
176
|
+
errors: ["test"]
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
it("Scenario 4: POST /rest/errors/scenario-4", async () => {
|
|
180
|
+
const response = await request.post("/rest/errors/scenario-4").expect(400);
|
|
181
|
+
expect(response.body).toEqual({
|
|
182
|
+
errors: [
|
|
183
|
+
{
|
|
184
|
+
dataPath: "",
|
|
185
|
+
keyword: "required",
|
|
186
|
+
message: "It should have required parameter 'name'",
|
|
187
|
+
modelName: "body",
|
|
188
|
+
params: {
|
|
189
|
+
missingProperty: "name"
|
|
190
|
+
},
|
|
191
|
+
schemaPath: "#/required"
|
|
192
|
+
}
|
|
193
|
+
],
|
|
194
|
+
message: "Bad request on parameter \"request.body.name\".\nIt should have required parameter 'name'",
|
|
195
|
+
name: "REQUIRED_VALIDATION_ERROR",
|
|
196
|
+
status: 400
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
it("Scenario 5: POST /rest/errors/scenario-5", async () => {
|
|
200
|
+
const response = await request.post("/rest/errors/scenario-5").expect(400);
|
|
201
|
+
expect(response.body).toEqual({
|
|
202
|
+
errors: [
|
|
203
|
+
{
|
|
204
|
+
dataPath: "",
|
|
205
|
+
instancePath: "",
|
|
206
|
+
keyword: "required",
|
|
207
|
+
message: "must have required property 'name'",
|
|
208
|
+
modelName: "CustomModel",
|
|
209
|
+
params: {
|
|
210
|
+
missingProperty: "name"
|
|
211
|
+
},
|
|
212
|
+
schemaPath: "#/required"
|
|
213
|
+
}
|
|
214
|
+
],
|
|
215
|
+
message: 'Bad request on parameter "request.body".\nCustomModel must have required property \'name\'. Given value: "undefined"',
|
|
216
|
+
name: "AJV_VALIDATION_ERROR",
|
|
217
|
+
status: 400
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
it("Scenario 6: POST /rest/errors/scenario-6", async () => {
|
|
221
|
+
const response = await request.post(`/rest/errors/scenario-6`).send({}).expect(400);
|
|
222
|
+
expect(response.body).toEqual({
|
|
223
|
+
errors: [
|
|
224
|
+
{
|
|
225
|
+
dataPath: "",
|
|
226
|
+
instancePath: "",
|
|
227
|
+
keyword: "required",
|
|
228
|
+
message: "must have required property 'role_item'",
|
|
229
|
+
modelName: "CustomPropModel",
|
|
230
|
+
params: {
|
|
231
|
+
missingProperty: "role_item"
|
|
232
|
+
},
|
|
233
|
+
schemaPath: "#/required"
|
|
234
|
+
}
|
|
235
|
+
],
|
|
236
|
+
message: 'Bad request on parameter "request.body".\nCustomPropModel must have required property \'role_item\'. Given value: "undefined"',
|
|
237
|
+
name: "AJV_VALIDATION_ERROR",
|
|
238
|
+
status: 400
|
|
239
|
+
});
|
|
240
|
+
});
|
|
241
|
+
it("Scenario 7: GET /rest/errors/scenario-7", async () => {
|
|
242
|
+
const response = await request.get("/rest/errors/scenario-7").expect(200);
|
|
243
|
+
expect(response.body).toEqual({
|
|
244
|
+
message: "no error"
|
|
245
|
+
});
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
//# sourceMappingURL=testErrors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"testErrors.js","sourceRoot":"","sources":["../../../src/tests/testErrors.ts"],"names":[],"mappings":";AAAA,OAAO,WAAW,CAAC;AACnB,OAAO,EAAC,UAAU,EAAE,UAAU,EAAE,GAAG,EAAE,GAAG,EAAE,UAAU,EAAE,YAAY,EAAE,IAAI,EAAE,QAAQ,EAAC,MAAM,cAAc,CAAC;AACxG,OAAO,EAAC,GAAG,EAAC,MAAM,YAAY,CAAC;AAC/B,OAAO,EAAC,UAAU,EAAE,mBAAmB,EAAC,MAAM,kBAAkB,CAAC;AACjE,OAAO,EAAC,WAAW,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,OAAO,EAAC,MAAM,cAAc,CAAC;AAC3E,OAAO,SAAS,MAAM,WAAW,CAAC;AAClC,OAAO,EAAC,gBAAgB,EAAC,MA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\"@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,105 @@
|
|
|
1
|
+
import { __decorate, __metadata, __param } from "tslib";
|
|
2
|
+
import { Context, Controller, Get, PathParams, PlatformTest } from "@tsed/common";
|
|
3
|
+
import { Property, Required, Title } from "@tsed/schema";
|
|
4
|
+
import SuperTest from "supertest";
|
|
5
|
+
export class MyModel {
|
|
6
|
+
id;
|
|
7
|
+
name;
|
|
8
|
+
}
|
|
9
|
+
__decorate([
|
|
10
|
+
Title("ID"),
|
|
11
|
+
Property(),
|
|
12
|
+
__metadata("design:type", String)
|
|
13
|
+
], MyModel.prototype, "id", void 0);
|
|
14
|
+
__decorate([
|
|
15
|
+
Property(),
|
|
16
|
+
Required(),
|
|
17
|
+
__metadata("design:type", String)
|
|
18
|
+
], MyModel.prototype, "name", void 0);
|
|
19
|
+
let HandlersCtrl = class HandlersCtrl {
|
|
20
|
+
scenario1(id) {
|
|
21
|
+
const model = new MyModel();
|
|
22
|
+
model.id = id;
|
|
23
|
+
model.name = "test";
|
|
24
|
+
return model;
|
|
25
|
+
}
|
|
26
|
+
scenario2(ctx, id) {
|
|
27
|
+
const model = new MyModel();
|
|
28
|
+
model.id = "1";
|
|
29
|
+
model.name = "test";
|
|
30
|
+
try {
|
|
31
|
+
ctx.response.status(202).body(model);
|
|
32
|
+
}
|
|
33
|
+
catch (er) {
|
|
34
|
+
console.log(er);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
async scenario3(id) {
|
|
38
|
+
const model = new MyModel();
|
|
39
|
+
model.id = id;
|
|
40
|
+
model.name = "test";
|
|
41
|
+
return model;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
__decorate([
|
|
45
|
+
Get("/scenario-1/:id") // Express style
|
|
46
|
+
,
|
|
47
|
+
__param(0, PathParams("id")),
|
|
48
|
+
__metadata("design:type", Function),
|
|
49
|
+
__metadata("design:paramtypes", [String]),
|
|
50
|
+
__metadata("design:returntype", MyModel)
|
|
51
|
+
], HandlersCtrl.prototype, "scenario1", null);
|
|
52
|
+
__decorate([
|
|
53
|
+
Get("/scenario-2/:id") // Ts.ED style with response injection
|
|
54
|
+
,
|
|
55
|
+
__param(0, Context()),
|
|
56
|
+
__param(1, PathParams("id")),
|
|
57
|
+
__param(1, Required()),
|
|
58
|
+
__metadata("design:type", Function),
|
|
59
|
+
__metadata("design:paramtypes", [Object, String]),
|
|
60
|
+
__metadata("design:returntype", void 0)
|
|
61
|
+
], HandlersCtrl.prototype, "scenario2", null);
|
|
62
|
+
__decorate([
|
|
63
|
+
Get("/scenario-3/:id") // Ts.ED style
|
|
64
|
+
,
|
|
65
|
+
__param(0, PathParams("id")),
|
|
66
|
+
__metadata("design:type", Function),
|
|
67
|
+
__metadata("design:paramtypes", [String]),
|
|
68
|
+
__metadata("design:returntype", Promise)
|
|
69
|
+
], HandlersCtrl.prototype, "scenario3", null);
|
|
70
|
+
HandlersCtrl = __decorate([
|
|
71
|
+
Controller("/handlers")
|
|
72
|
+
], HandlersCtrl);
|
|
73
|
+
export { HandlersCtrl };
|
|
74
|
+
export function testHandlers(options) {
|
|
75
|
+
let request;
|
|
76
|
+
beforeAll(PlatformTest.bootstrap(options.server, {
|
|
77
|
+
...options,
|
|
78
|
+
logger: {
|
|
79
|
+
leve: "info"
|
|
80
|
+
},
|
|
81
|
+
mount: {
|
|
82
|
+
"/rest": [HandlersCtrl]
|
|
83
|
+
}
|
|
84
|
+
}));
|
|
85
|
+
beforeAll(() => {
|
|
86
|
+
request = SuperTest(PlatformTest.callback());
|
|
87
|
+
});
|
|
88
|
+
afterAll(PlatformTest.reset);
|
|
89
|
+
it("Scenario 1: GET /rest/handlers/scenario-1/:id", async () => {
|
|
90
|
+
const { body } = await request.get("/rest/handlers/scenario-1/1").expect(200);
|
|
91
|
+
expect(body.id).toEqual("1");
|
|
92
|
+
expect(body.name).toEqual("test");
|
|
93
|
+
});
|
|
94
|
+
it("Scenario 2: GET /rest/handlers/scenario-2/:id", async () => {
|
|
95
|
+
const { body } = await request.get("/rest/handlers/scenario-2/1").expect(202);
|
|
96
|
+
expect(body.id).toEqual("1");
|
|
97
|
+
expect(body.name).toEqual("test");
|
|
98
|
+
});
|
|
99
|
+
it("Scenario 3: GET /rest/handlers/scenario-3/:id", async () => {
|
|
100
|
+
const { body } = await request.get("/rest/handlers/scenario-3/1").expect(200);
|
|
101
|
+
expect(body.id).toEqual("1");
|
|
102
|
+
expect(body.name).toEqual("test");
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
//# sourceMappingURL=testHandlers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"testHandlers.js","sourceRoot":"","s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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;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","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,162 @@
|
|
|
1
|
+
import { __decorate, __metadata, __param } from "tslib";
|
|
2
|
+
import { Context, Controller, Get, HeaderParams, Locals, Middleware, PlatformTest, Post, Req, Use } from "@tsed/common";
|
|
3
|
+
import { Enum, Required } from "@tsed/schema";
|
|
4
|
+
import SuperTest from "supertest";
|
|
5
|
+
let SetId = class SetId {
|
|
6
|
+
use(request, locals, $ctx) {
|
|
7
|
+
request["user"] = 1;
|
|
8
|
+
locals.id = "local-10909";
|
|
9
|
+
$ctx.set("uid", "ctx-10909");
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
__decorate([
|
|
13
|
+
__param(0, Req()),
|
|
14
|
+
__param(1, Locals()),
|
|
15
|
+
__param(2, Context()),
|
|
16
|
+
__metadata("design:type", Function),
|
|
17
|
+
__metadata("design:paramtypes", [Object, Object, Object]),
|
|
18
|
+
__metadata("design:returntype", void 0)
|
|
19
|
+
], SetId.prototype, "use", null);
|
|
20
|
+
SetId = __decorate([
|
|
21
|
+
Middleware()
|
|
22
|
+
], SetId);
|
|
23
|
+
let HeaderParamsCtrl = class HeaderParamsCtrl {
|
|
24
|
+
/**
|
|
25
|
+
* Handle request with a raw middleware + handler
|
|
26
|
+
* Get Authorization from header
|
|
27
|
+
* @param request
|
|
28
|
+
* @param auth
|
|
29
|
+
*/
|
|
30
|
+
scenario1(request, auth) {
|
|
31
|
+
return {
|
|
32
|
+
user: request.user,
|
|
33
|
+
token: auth
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
scenario2(contentType) {
|
|
37
|
+
return { contentType };
|
|
38
|
+
}
|
|
39
|
+
testScenario3(token) {
|
|
40
|
+
return { token };
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
__decorate([
|
|
44
|
+
Get("/scenario-1"),
|
|
45
|
+
Use(SetId),
|
|
46
|
+
__param(0, Req()),
|
|
47
|
+
__param(1, HeaderParams("authorization")),
|
|
48
|
+
__metadata("design:type", Function),
|
|
49
|
+
__metadata("design:paramtypes", [Object, String]),
|
|
50
|
+
__metadata("design:returntype", Object)
|
|
51
|
+
], HeaderParamsCtrl.prototype, "scenario1", null);
|
|
52
|
+
__decorate([
|
|
53
|
+
Post("/scenario-2"),
|
|
54
|
+
__param(0, HeaderParams("Content-type")),
|
|
55
|
+
__metadata("design:type", Function),
|
|
56
|
+
__metadata("design:paramtypes", [String]),
|
|
57
|
+
__metadata("design:returntype", void 0)
|
|
58
|
+
], HeaderParamsCtrl.prototype, "scenario2", null);
|
|
59
|
+
__decorate([
|
|
60
|
+
Get("/scenario-3"),
|
|
61
|
+
__param(0, HeaderParams({
|
|
62
|
+
expression: "x-token",
|
|
63
|
+
useValidation: true
|
|
64
|
+
})),
|
|
65
|
+
__param(0, Required()),
|
|
66
|
+
__param(0, Enum("test", "gcp")),
|
|
67
|
+
__metadata("design:type", Function),
|
|
68
|
+
__metadata("design:paramtypes", [String]),
|
|
69
|
+
__metadata("design:returntype", Object)
|
|
70
|
+
], HeaderParamsCtrl.prototype, "testScenario3", null);
|
|
71
|
+
HeaderParamsCtrl = __decorate([
|
|
72
|
+
Controller("/header-params")
|
|
73
|
+
], HeaderParamsCtrl);
|
|
74
|
+
export { HeaderParamsCtrl };
|
|
75
|
+
export function testHeaderParams(options) {
|
|
76
|
+
let request;
|
|
77
|
+
beforeAll(PlatformTest.bootstrap(options.server, {
|
|
78
|
+
...options,
|
|
79
|
+
mount: {
|
|
80
|
+
"/rest": [HeaderParamsCtrl]
|
|
81
|
+
}
|
|
82
|
+
}));
|
|
83
|
+
beforeAll(() => {
|
|
84
|
+
request = SuperTest(PlatformTest.callback());
|
|
85
|
+
});
|
|
86
|
+
afterAll(PlatformTest.reset);
|
|
87
|
+
describe("Scenario 1: GET /rest/header-params/scenario-1", () => {
|
|
88
|
+
it("should return a response with the extracted authorization from the request headers", async () => {
|
|
89
|
+
const { body } = await request
|
|
90
|
+
.get("/rest/header-params/scenario-1")
|
|
91
|
+
.set({
|
|
92
|
+
Authorization: "tokenauth"
|
|
93
|
+
})
|
|
94
|
+
.expect(200);
|
|
95
|
+
expect(body.user).toEqual(1);
|
|
96
|
+
expect(body.token).toEqual("tokenauth");
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
describe("Scenario 2: POST with contentType", () => {
|
|
100
|
+
it("should return a response with the extracted contentType from the request headers", async () => {
|
|
101
|
+
const response = await request
|
|
102
|
+
.post("/rest/header-params/scenario-2")
|
|
103
|
+
.set({
|
|
104
|
+
"Content-Type": "application/json"
|
|
105
|
+
})
|
|
106
|
+
.expect(200);
|
|
107
|
+
expect(response.body).toEqual({
|
|
108
|
+
contentType: "application/json"
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
describe("Scenario3: GET /rest/headers/scenario-3", () => {
|
|
113
|
+
it("should accept the header", async () => {
|
|
114
|
+
const response = await request.get("/rest/header-params/scenario-3").set("x-token", "test").expect(200);
|
|
115
|
+
expect(response.body).toEqual({
|
|
116
|
+
token: "test"
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
it("should not validate the header", async () => {
|
|
120
|
+
const response = await request.get("/rest/header-params/scenario-3").expect(400);
|
|
121
|
+
expect(response.body).toEqual({
|
|
122
|
+
errors: [
|
|
123
|
+
{
|
|
124
|
+
dataPath: "",
|
|
125
|
+
keyword: "required",
|
|
126
|
+
message: "It should have required parameter 'x-token'",
|
|
127
|
+
modelName: "header",
|
|
128
|
+
params: {
|
|
129
|
+
missingProperty: "x-token"
|
|
130
|
+
},
|
|
131
|
+
schemaPath: "#/required"
|
|
132
|
+
}
|
|
133
|
+
],
|
|
134
|
+
message: "Bad request on parameter \"request.header.x-token\".\nIt should have required parameter 'x-token'",
|
|
135
|
+
name: "REQUIRED_VALIDATION_ERROR",
|
|
136
|
+
status: 400
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
it("should not validate the header - enum", async () => {
|
|
140
|
+
const response = await request.get("/rest/header-params/scenario-3").set("x-token", "invalid").expect(400);
|
|
141
|
+
expect(response.body).toEqual({
|
|
142
|
+
errors: [
|
|
143
|
+
{
|
|
144
|
+
data: "invalid",
|
|
145
|
+
dataPath: "",
|
|
146
|
+
instancePath: "",
|
|
147
|
+
keyword: "enum",
|
|
148
|
+
message: "must be equal to one of the allowed values",
|
|
149
|
+
params: {
|
|
150
|
+
allowedValues: ["test", "gcp"]
|
|
151
|
+
},
|
|
152
|
+
schemaPath: "#/enum"
|
|
153
|
+
}
|
|
154
|
+
],
|
|
155
|
+
message: 'Bad request on parameter "request.header.x-token".\nValue must be equal to one of the allowed values. Given value: "invalid"',
|
|
156
|
+
name: "AJV_VALIDATION_ERROR",
|
|
157
|
+
status: 400
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
//# sourceMappingURL=testHeaderParams.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"testHeaderParams.js","sourceRoot":"","sources":["../../../src/tests/testHeaderParams.ts"],"names":[],"mappings":";AAAA,OAAO,EAAC,OAAO,EAAE,UAAU,EAAE,GAAG,EAAE,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,YAAY,EAAE,IAAI,EAAE,GAAG,EAAE,GAAG,EAAC,MAAM,cAAc,CAAC;AACtH,OAAO,EAAC,IAAI,EAAE,QAAQ,EAAC,MAAM,cAAc,CAAC;AAC5C,OAAO,SAAS,MAAM,WAAW,CAAC;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,WAAA,GAAG,EAAE,CAAA;IAAgB,WAAA,MAAM,EAAE,CAAA;IAAe,WAAA,OAAO,EAAE,CAAA;;;;gCAIzD;AALG,KAAK;IADV,UAAU,EAAE;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,a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M,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC;gBAC5B,MAAM,EAAE;oBACN;wBACE,QAAQ,EAAE,EAAE;wBACZ,OAAO,EAAE,UAAU;wBACnB,OAAO,EAAE,6CAA6C;wBACtD,SAAS,EAAE,QAAQ;wBACnB,MAAM,EAAE;4BACN,eAAe,EAAE,SAAS;yBAC3B;wBACD,UAAU,EAAE,YAAY;qBACzB;iBACF;gBACD,OAAO,EAAE,mGAAmG;gBAC5G,IAAI,EAAE,2BAA2B;gBACjC,MAAM,EAAE,GAAG;aACZ,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;QAEH,EAAE,CAAC,uCAAuC,EAAE,KAAK,IAAI,EAAE;YACrD,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,gCAAgC,CAAC,CAAC,GAAG,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;YAE3G,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC;gBAC5B,MAAM,EAAE;oBACN;wBACE,IAAI,EAAE,SAAS;wBACf,QAAQ,EAAE,EAAE;wBACZ,YAAY,EAAE,EAAE;wBAChB,OAAO,EAAE,MAAM;wBACf,OAAO,EAAE,4CAA4C;wBACrD,MAAM,EAAE;4BACN,aAAa,EAAE,CAAC,MAAM,EAAE,KAAK,CAAC;yBAC/B;wBACD,UAAU,EAAE,QAAQ;qBACrB;iBACF;gBACD,OAAO,EACL,8HAA8H;gBAChI,IAAI,EAAE,sBAAsB;gBAC5B,MAAM,EAAE,GAAG;aACZ,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import {Context, Controller, Get, HeaderParams, Locals, Middleware, PlatformTest, Post, Req, Use} from \"@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"]}
|