@tsed/platform-test-sdk 7.73.2 → 7.73.3
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/package.json +14 -14
- package/lib/cjs/components/PlatformTestSdk.js.map +0 -1
- package/lib/cjs/errors/CustomBadRequest.js.map +0 -1
- package/lib/cjs/errors/CustomInternalError.js.map +0 -1
- package/lib/cjs/index.js.map +0 -1
- package/lib/cjs/interfaces/PlatformTestingSdkOpts.js.map +0 -1
- package/lib/cjs/interfaces/index.js.map +0 -1
- package/lib/cjs/modules/feature/FeatureModule.js.map +0 -1
- package/lib/cjs/modules/feature/controllers/FeatureController.js.map +0 -1
- package/lib/cjs/tests/exports.js.map +0 -1
- package/lib/cjs/tests/testAcceptMime.js.map +0 -1
- package/lib/cjs/tests/testAuth.js.map +0 -1
- package/lib/cjs/tests/testBodyParams.js.map +0 -1
- package/lib/cjs/tests/testCache.js.map +0 -1
- package/lib/cjs/tests/testChildrenControllers.js.map +0 -1
- package/lib/cjs/tests/testCookies.js.map +0 -1
- package/lib/cjs/tests/testCustom404.js.map +0 -1
- package/lib/cjs/tests/testDeepQueryParams.js.map +0 -1
- package/lib/cjs/tests/testErrors.js.map +0 -1
- package/lib/cjs/tests/testHandlers.js.map +0 -1
- package/lib/cjs/tests/testHeaderParams.js.map +0 -1
- package/lib/cjs/tests/testHeaders.js.map +0 -1
- package/lib/cjs/tests/testInheritanceController.js.map +0 -1
- package/lib/cjs/tests/testLocals.js.map +0 -1
- package/lib/cjs/tests/testLocation.js.map +0 -1
- package/lib/cjs/tests/testMiddlewares.js.map +0 -1
- package/lib/cjs/tests/testModule.js.map +0 -1
- package/lib/cjs/tests/testMulter.js.map +0 -1
- package/lib/cjs/tests/testPathParams.js.map +0 -1
- package/lib/cjs/tests/testQueryParams.js.map +0 -1
- package/lib/cjs/tests/testRedirect.js.map +0 -1
- package/lib/cjs/tests/testResponse.js.map +0 -1
- package/lib/cjs/tests/testResponseFilter.js.map +0 -1
- package/lib/cjs/tests/testRouting.js.map +0 -1
- package/lib/cjs/tests/testScopeRequest.js.map +0 -1
- package/lib/cjs/tests/testSession.js.map +0 -1
- package/lib/cjs/tests/testStatics.js.map +0 -1
- package/lib/cjs/tests/testStream.js.map +0 -1
- package/lib/cjs/tests/testView.js.map +0 -1
- package/lib/esm/components/PlatformTestSdk.js.map +0 -1
- package/lib/esm/errors/CustomBadRequest.js.map +0 -1
- package/lib/esm/errors/CustomInternalError.js.map +0 -1
- package/lib/esm/index.js.map +0 -1
- package/lib/esm/interfaces/PlatformTestingSdkOpts.js.map +0 -1
- package/lib/esm/interfaces/index.js.map +0 -1
- package/lib/esm/modules/feature/FeatureModule.js.map +0 -1
- package/lib/esm/modules/feature/controllers/FeatureController.js.map +0 -1
- package/lib/esm/tests/exports.js.map +0 -1
- package/lib/esm/tests/testAcceptMime.js.map +0 -1
- package/lib/esm/tests/testAuth.js.map +0 -1
- package/lib/esm/tests/testBodyParams.js.map +0 -1
- package/lib/esm/tests/testCache.js.map +0 -1
- package/lib/esm/tests/testChildrenControllers.js.map +0 -1
- package/lib/esm/tests/testCookies.js.map +0 -1
- package/lib/esm/tests/testCustom404.js.map +0 -1
- package/lib/esm/tests/testDeepQueryParams.js.map +0 -1
- package/lib/esm/tests/testErrors.js.map +0 -1
- package/lib/esm/tests/testHandlers.js.map +0 -1
- package/lib/esm/tests/testHeaderParams.js.map +0 -1
- package/lib/esm/tests/testHeaders.js.map +0 -1
- package/lib/esm/tests/testInheritanceController.js.map +0 -1
- package/lib/esm/tests/testLocals.js.map +0 -1
- package/lib/esm/tests/testLocation.js.map +0 -1
- package/lib/esm/tests/testMiddlewares.js.map +0 -1
- package/lib/esm/tests/testModule.js.map +0 -1
- package/lib/esm/tests/testMulter.js.map +0 -1
- package/lib/esm/tests/testPathParams.js.map +0 -1
- package/lib/esm/tests/testQueryParams.js.map +0 -1
- package/lib/esm/tests/testRedirect.js.map +0 -1
- package/lib/esm/tests/testResponse.js.map +0 -1
- package/lib/esm/tests/testResponseFilter.js.map +0 -1
- package/lib/esm/tests/testRouting.js.map +0 -1
- package/lib/esm/tests/testScopeRequest.js.map +0 -1
- package/lib/esm/tests/testSession.js.map +0 -1
- package/lib/esm/tests/testStatics.js.map +0 -1
- package/lib/esm/tests/testStream.js.map +0 -1
- package/lib/esm/tests/testView.js.map +0 -1
- package/lib/tsconfig.esm.tsbuildinfo +0 -1
- package/lib/tsconfig.tsbuildinfo +0 -1
- package/src/components/PlatformTestSdk.ts +0 -29
- package/src/data/file.txt +0 -1
- package/src/data/response.data.json +0 -3
- package/src/data/swagger.json +0 -32
- package/src/errors/CustomBadRequest.ts +0 -16
- package/src/errors/CustomInternalError.ts +0 -16
- package/src/index.ts +0 -40
- package/src/interfaces/PlatformTestingSdkOpts.ts +0 -10
- package/src/interfaces/index.ts +0 -4
- package/src/modules/feature/FeatureModule.ts +0 -9
- package/src/modules/feature/controllers/FeatureController.ts +0 -11
- package/src/tests/exports.ts +0 -71
- package/src/tests/testAcceptMime.ts +0 -95
- package/src/tests/testAuth.ts +0 -339
- package/src/tests/testBodyParams.ts +0 -483
- package/src/tests/testCache.ts +0 -279
- package/src/tests/testChildrenControllers.ts +0 -55
- package/src/tests/testCookies.ts +0 -36
- package/src/tests/testCustom404.ts +0 -33
- package/src/tests/testDeepQueryParams.ts +0 -120
- package/src/tests/testErrors.ts +0 -226
- package/src/tests/testHandlers.ts +0 -90
- package/src/tests/testHeaderParams.ts +0 -152
- package/src/tests/testHeaders.ts +0 -91
- package/src/tests/testInheritanceController.ts +0 -200
- package/src/tests/testLocals.ts +0 -46
- package/src/tests/testLocation.ts +0 -37
- package/src/tests/testMiddlewares.ts +0 -159
- package/src/tests/testModule.ts +0 -46
- package/src/tests/testMulter.ts +0 -151
- package/src/tests/testPathParams.ts +0 -123
- package/src/tests/testQueryParams.ts +0 -233
- package/src/tests/testRedirect.ts +0 -124
- package/src/tests/testResponse.ts +0 -528
- package/src/tests/testResponseFilter.ts +0 -101
- package/src/tests/testRouting.ts +0 -65
- package/src/tests/testScopeRequest.ts +0 -111
- package/src/tests/testSession.ts +0 -103
- package/src/tests/testStatics.ts +0 -61
- package/src/tests/testStream.ts +0 -59
- package/src/tests/testView.ts +0 -68
- package/tsconfig.json +0 -63
package/src/tests/testErrors.ts
DELETED
|
@@ -1,226 +0,0 @@
|
|
|
1
|
-
import "@tsed/ajv";
|
|
2
|
-
import {BodyParams, Controller, Err, Get, Middleware, PlatformTest, Post, UseAfter} from "@tsed/common";
|
|
3
|
-
import {Env} from "@tsed/core";
|
|
4
|
-
import {BadRequest, InternalServerError} from "@tsed/exceptions";
|
|
5
|
-
import {Description, Name, Required, Returns, Summary} from "@tsed/schema";
|
|
6
|
-
import SuperTest from "supertest";
|
|
7
|
-
import {CustomBadRequest} from "../errors/CustomBadRequest.js";
|
|
8
|
-
import {CustomInternalError} from "../errors/CustomInternalError.js";
|
|
9
|
-
import {PlatformTestingSdkOpts} from "../interfaces/index.js";
|
|
10
|
-
|
|
11
|
-
class CustomModel {
|
|
12
|
-
@Required() name: string;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
class CustomPropModel {
|
|
16
|
-
@Name("role_item")
|
|
17
|
-
@Required()
|
|
18
|
-
roleItem: string;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
@Middleware()
|
|
22
|
-
class ErrorMiddleware {
|
|
23
|
-
use(@Err() error: any) {
|
|
24
|
-
return {
|
|
25
|
-
message: "no error"
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
@Middleware()
|
|
31
|
-
class FakeMiddleware {
|
|
32
|
-
use() {
|
|
33
|
-
return {
|
|
34
|
-
message: "should not be called by the platform when endpoint throw an error"
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
@Controller("/errors")
|
|
40
|
-
export class ErrorsCtrl {
|
|
41
|
-
@Get("/scenario-1")
|
|
42
|
-
@Returns(500, InternalServerError).Description("Custom Bad Request")
|
|
43
|
-
public scenario1() {
|
|
44
|
-
throw new CustomBadRequest("Custom Bad Request");
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
@Get("/scenario-2")
|
|
48
|
-
@Returns(500).Description("Internal Server Error")
|
|
49
|
-
public scenario2() {
|
|
50
|
-
throw new Error("My error");
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
@Get("/scenario-3")
|
|
54
|
-
@Returns(400, CustomInternalError).Description("Bad request")
|
|
55
|
-
public scenario3() {
|
|
56
|
-
throw new CustomInternalError("My custom error");
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
@Post("/scenario-4")
|
|
60
|
-
@Returns(400)
|
|
61
|
-
public scenario4(@Required() @BodyParams("name") name: string) {
|
|
62
|
-
return name;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
@Post("/scenario-5")
|
|
66
|
-
@Returns(400, BadRequest).Description("Bad request")
|
|
67
|
-
public scenario5(
|
|
68
|
-
@Required()
|
|
69
|
-
@BodyParams()
|
|
70
|
-
model: CustomModel
|
|
71
|
-
) {
|
|
72
|
-
return model;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
@Post("/scenario-6")
|
|
76
|
-
@Summary("Throw a Required prop if prop name is required")
|
|
77
|
-
@Description(`Return a required error`)
|
|
78
|
-
@Returns(400).Description("Bad request")
|
|
79
|
-
public scenario6(
|
|
80
|
-
@Required()
|
|
81
|
-
@BodyParams()
|
|
82
|
-
model: CustomPropModel
|
|
83
|
-
) {
|
|
84
|
-
return model;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
@Get("/scenario-7")
|
|
88
|
-
@Returns(400).Description("Bad request")
|
|
89
|
-
@UseAfter(FakeMiddleware)
|
|
90
|
-
@UseAfter(ErrorMiddleware)
|
|
91
|
-
public scenario7() {
|
|
92
|
-
throw new CustomInternalError("My custom error");
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export function testErrors(options: PlatformTestingSdkOpts) {
|
|
97
|
-
let request: SuperTest.Agent;
|
|
98
|
-
beforeAll(
|
|
99
|
-
PlatformTest.bootstrap(options.server, {
|
|
100
|
-
...options,
|
|
101
|
-
env: Env.TEST,
|
|
102
|
-
mount: {
|
|
103
|
-
"/rest": [ErrorsCtrl]
|
|
104
|
-
}
|
|
105
|
-
})
|
|
106
|
-
);
|
|
107
|
-
beforeAll(() => {
|
|
108
|
-
request = SuperTest(PlatformTest.callback());
|
|
109
|
-
});
|
|
110
|
-
afterAll(PlatformTest.reset);
|
|
111
|
-
|
|
112
|
-
it("Scenario 1: GET /rest/errors/scenario-1", async () => {
|
|
113
|
-
const response: any = await request.get("/rest/errors/scenario-1").expect(400);
|
|
114
|
-
|
|
115
|
-
expect(response.headers["x-header-error"]).toEqual("deny");
|
|
116
|
-
expect(response.body).toEqual({
|
|
117
|
-
name: "CUSTOM_BAD_REQUEST",
|
|
118
|
-
message: "Custom Bad Request",
|
|
119
|
-
status: 400,
|
|
120
|
-
errors: ["test"]
|
|
121
|
-
});
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
it("Scenario 2: GET /rest/errors/scenario-2", async () => {
|
|
125
|
-
const response: any = await request.get("/rest/errors/scenario-2").expect(500);
|
|
126
|
-
|
|
127
|
-
expect(response.body).toEqual({
|
|
128
|
-
errors: [],
|
|
129
|
-
message: "My error",
|
|
130
|
-
name: "Error",
|
|
131
|
-
status: 500
|
|
132
|
-
});
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
it("Scenario 3: GET /rest/errors/scenario-3", async () => {
|
|
136
|
-
const response: any = await request.get("/rest/errors/scenario-3").expect(500);
|
|
137
|
-
|
|
138
|
-
expect(response.headers["x-header-error"]).toEqual("deny");
|
|
139
|
-
expect(response.body).toEqual({
|
|
140
|
-
name: "CUSTOM_INTERNAL_SERVER_ERROR",
|
|
141
|
-
message: "My custom error",
|
|
142
|
-
status: 500,
|
|
143
|
-
errors: ["test"]
|
|
144
|
-
});
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
it("Scenario 4: POST /rest/errors/scenario-4", async () => {
|
|
148
|
-
const response: any = await request.post("/rest/errors/scenario-4").expect(400);
|
|
149
|
-
|
|
150
|
-
expect(response.body).toEqual({
|
|
151
|
-
errors: [
|
|
152
|
-
{
|
|
153
|
-
dataPath: ".name",
|
|
154
|
-
requestPath: "body",
|
|
155
|
-
keyword: "required",
|
|
156
|
-
message: "It should have required parameter 'name'",
|
|
157
|
-
modelName: "body",
|
|
158
|
-
params: {
|
|
159
|
-
missingProperty: "name"
|
|
160
|
-
},
|
|
161
|
-
schemaPath: "#/required"
|
|
162
|
-
}
|
|
163
|
-
],
|
|
164
|
-
message: "Bad request on parameter \"request.body.name\".\nIt should have required parameter 'name'",
|
|
165
|
-
name: "REQUIRED_VALIDATION_ERROR",
|
|
166
|
-
status: 400
|
|
167
|
-
});
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
it("Scenario 5: POST /rest/errors/scenario-5", async () => {
|
|
171
|
-
const response: any = await request.post("/rest/errors/scenario-5").send({}).expect(400);
|
|
172
|
-
|
|
173
|
-
expect(response.body).toEqual({
|
|
174
|
-
errors: [
|
|
175
|
-
{
|
|
176
|
-
dataPath: ".name",
|
|
177
|
-
requestPath: "body",
|
|
178
|
-
instancePath: "",
|
|
179
|
-
keyword: "required",
|
|
180
|
-
message: "must have required property 'name'",
|
|
181
|
-
modelName: "CustomModel",
|
|
182
|
-
params: {
|
|
183
|
-
missingProperty: "name"
|
|
184
|
-
},
|
|
185
|
-
schemaPath: "#/required"
|
|
186
|
-
}
|
|
187
|
-
],
|
|
188
|
-
message: 'Bad request on parameter "request.body".\nCustomModel must have required property \'name\'. Given value: "undefined"',
|
|
189
|
-
name: "AJV_VALIDATION_ERROR",
|
|
190
|
-
status: 400
|
|
191
|
-
});
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
it("Scenario 6: POST /rest/errors/scenario-6", async () => {
|
|
195
|
-
const response: any = await request.post(`/rest/errors/scenario-6`).send({}).expect(400);
|
|
196
|
-
|
|
197
|
-
expect(response.body).toEqual({
|
|
198
|
-
errors: [
|
|
199
|
-
{
|
|
200
|
-
dataPath: ".role_item",
|
|
201
|
-
requestPath: "body",
|
|
202
|
-
instancePath: "",
|
|
203
|
-
keyword: "required",
|
|
204
|
-
message: "must have required property 'role_item'",
|
|
205
|
-
modelName: "CustomPropModel",
|
|
206
|
-
params: {
|
|
207
|
-
missingProperty: "role_item"
|
|
208
|
-
},
|
|
209
|
-
schemaPath: "#/required"
|
|
210
|
-
}
|
|
211
|
-
],
|
|
212
|
-
message:
|
|
213
|
-
'Bad request on parameter "request.body".\nCustomPropModel must have required property \'role_item\'. Given value: "undefined"',
|
|
214
|
-
name: "AJV_VALIDATION_ERROR",
|
|
215
|
-
status: 400
|
|
216
|
-
});
|
|
217
|
-
});
|
|
218
|
-
|
|
219
|
-
it("Scenario 7: GET /rest/errors/scenario-7", async () => {
|
|
220
|
-
const response: any = await request.get("/rest/errors/scenario-7").expect(200);
|
|
221
|
-
|
|
222
|
-
expect(response.body).toEqual({
|
|
223
|
-
message: "no error"
|
|
224
|
-
});
|
|
225
|
-
});
|
|
226
|
-
}
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import {Context, Controller, Get, PathParams, PlatformTest} from "@tsed/common";
|
|
2
|
-
import {Property, Required, Title} from "@tsed/schema";
|
|
3
|
-
import SuperTest from "supertest";
|
|
4
|
-
import {PlatformTestingSdkOpts} from "../interfaces/index.js";
|
|
5
|
-
|
|
6
|
-
export class MyModel {
|
|
7
|
-
@Title("ID")
|
|
8
|
-
@Property()
|
|
9
|
-
public id: string;
|
|
10
|
-
|
|
11
|
-
@Property()
|
|
12
|
-
@Required()
|
|
13
|
-
public name: string;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
@Controller("/handlers")
|
|
17
|
-
export class HandlersCtrl {
|
|
18
|
-
@Get("/scenario-1/:id") // Express style
|
|
19
|
-
public scenario1(@PathParams("id") id: string): MyModel {
|
|
20
|
-
const model = new MyModel();
|
|
21
|
-
model.id = id;
|
|
22
|
-
model.name = "test";
|
|
23
|
-
|
|
24
|
-
return model;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
@Get("/scenario-2/:id") // Ts.ED style with response injection
|
|
28
|
-
public scenario2(
|
|
29
|
-
@Context() ctx: Context,
|
|
30
|
-
@PathParams("id")
|
|
31
|
-
@Required()
|
|
32
|
-
id: string
|
|
33
|
-
): void {
|
|
34
|
-
const model = new MyModel();
|
|
35
|
-
model.id = "1";
|
|
36
|
-
model.name = "test";
|
|
37
|
-
|
|
38
|
-
try {
|
|
39
|
-
ctx.response.status(202).body(model);
|
|
40
|
-
} catch (er) {
|
|
41
|
-
console.log(er);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
@Get("/scenario-3/:id") // Ts.ED style
|
|
46
|
-
public scenario3(@PathParams("id") id: string): Promise<any> {
|
|
47
|
-
const model = new MyModel();
|
|
48
|
-
model.id = id;
|
|
49
|
-
model.name = "test";
|
|
50
|
-
|
|
51
|
-
return Promise.resolve(model);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export function testHandlers(options: PlatformTestingSdkOpts) {
|
|
56
|
-
let request: SuperTest.Agent;
|
|
57
|
-
beforeAll(
|
|
58
|
-
PlatformTest.bootstrap(options.server, {
|
|
59
|
-
...options,
|
|
60
|
-
logger: {},
|
|
61
|
-
mount: {
|
|
62
|
-
"/rest": [HandlersCtrl]
|
|
63
|
-
}
|
|
64
|
-
})
|
|
65
|
-
);
|
|
66
|
-
beforeAll(() => {
|
|
67
|
-
request = SuperTest(PlatformTest.callback());
|
|
68
|
-
});
|
|
69
|
-
afterAll(PlatformTest.reset);
|
|
70
|
-
it("Scenario 1: GET /rest/handlers/scenario-1/:id", async () => {
|
|
71
|
-
const {body}: any = await request.get("/rest/handlers/scenario-1/1").expect(200);
|
|
72
|
-
|
|
73
|
-
expect(body.id).toEqual("1");
|
|
74
|
-
expect(body.name).toEqual("test");
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it("Scenario 2: GET /rest/handlers/scenario-2/:id", async () => {
|
|
78
|
-
const {body}: any = await request.get("/rest/handlers/scenario-2/1").expect(202);
|
|
79
|
-
|
|
80
|
-
expect(body.id).toEqual("1");
|
|
81
|
-
expect(body.name).toEqual("test");
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
it("Scenario 3: GET /rest/handlers/scenario-3/:id", async () => {
|
|
85
|
-
const {body}: any = await request.get("/rest/handlers/scenario-3/1").expect(200);
|
|
86
|
-
|
|
87
|
-
expect(body.id).toEqual("1");
|
|
88
|
-
expect(body.name).toEqual("test");
|
|
89
|
-
});
|
|
90
|
-
}
|
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
import {Context, Controller, Get, HeaderParams, Locals, Middleware, PlatformTest, Post, Req, Use} from "@tsed/common";
|
|
2
|
-
import {Enum, Required} from "@tsed/schema";
|
|
3
|
-
import SuperTest from "supertest";
|
|
4
|
-
import {PlatformTestingSdkOpts} from "../interfaces/index.js";
|
|
5
|
-
|
|
6
|
-
@Middleware()
|
|
7
|
-
class SetId {
|
|
8
|
-
use(@Req() request: any, @Locals() locals: any, @Context() $ctx: Context) {
|
|
9
|
-
request["user"] = 1;
|
|
10
|
-
locals.id = "local-10909";
|
|
11
|
-
$ctx.set("uid", "ctx-10909");
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
@Controller("/header-params")
|
|
16
|
-
export class HeaderParamsCtrl {
|
|
17
|
-
/**
|
|
18
|
-
* Handle request with a raw middleware + handler
|
|
19
|
-
* Get Authorization from header
|
|
20
|
-
* @param request
|
|
21
|
-
* @param auth
|
|
22
|
-
*/
|
|
23
|
-
@Get("/scenario-1")
|
|
24
|
-
@Use(SetId)
|
|
25
|
-
public scenario1(@Req() request: Req, @HeaderParams("authorization") auth: string): any {
|
|
26
|
-
return {
|
|
27
|
-
user: (request as any).user,
|
|
28
|
-
token: auth
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
@Post("/scenario-2")
|
|
33
|
-
scenario2(@HeaderParams("Content-type") contentType: string) {
|
|
34
|
-
return {contentType};
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
@Get("/scenario-3")
|
|
38
|
-
testScenario3(
|
|
39
|
-
@HeaderParams({
|
|
40
|
-
expression: "x-token",
|
|
41
|
-
useValidation: true
|
|
42
|
-
})
|
|
43
|
-
@Required()
|
|
44
|
-
@Enum("test", "gcp")
|
|
45
|
-
token: string
|
|
46
|
-
): any {
|
|
47
|
-
return {token};
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export function testHeaderParams(options: PlatformTestingSdkOpts) {
|
|
52
|
-
let request: SuperTest.Agent;
|
|
53
|
-
beforeAll(
|
|
54
|
-
PlatformTest.bootstrap(options.server, {
|
|
55
|
-
...options,
|
|
56
|
-
mount: {
|
|
57
|
-
"/rest": [HeaderParamsCtrl]
|
|
58
|
-
}
|
|
59
|
-
})
|
|
60
|
-
);
|
|
61
|
-
beforeAll(() => {
|
|
62
|
-
request = SuperTest(PlatformTest.callback());
|
|
63
|
-
});
|
|
64
|
-
afterAll(PlatformTest.reset);
|
|
65
|
-
|
|
66
|
-
describe("Scenario 1: GET /rest/header-params/scenario-1", () => {
|
|
67
|
-
it("should return a response with the extracted authorization from the request headers", async () => {
|
|
68
|
-
const {body}: any = await request
|
|
69
|
-
.get("/rest/header-params/scenario-1")
|
|
70
|
-
.set({
|
|
71
|
-
Authorization: "tokenauth"
|
|
72
|
-
})
|
|
73
|
-
.expect(200);
|
|
74
|
-
|
|
75
|
-
expect(body.user).toEqual(1);
|
|
76
|
-
expect(body.token).toEqual("tokenauth");
|
|
77
|
-
});
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
describe("Scenario 2: POST with contentType", () => {
|
|
81
|
-
it("should return a response with the extracted contentType from the request headers", async () => {
|
|
82
|
-
const response = await request
|
|
83
|
-
.post("/rest/header-params/scenario-2")
|
|
84
|
-
.send({})
|
|
85
|
-
.set({
|
|
86
|
-
"Content-Type": "application/json"
|
|
87
|
-
})
|
|
88
|
-
.expect(200);
|
|
89
|
-
|
|
90
|
-
expect(response.body).toEqual({
|
|
91
|
-
contentType: "application/json"
|
|
92
|
-
});
|
|
93
|
-
});
|
|
94
|
-
});
|
|
95
|
-
describe("Scenario3: GET /rest/headers/scenario-3", () => {
|
|
96
|
-
it("should accept the header", async () => {
|
|
97
|
-
const response = await request.get("/rest/header-params/scenario-3").set("x-token", "test").expect(200);
|
|
98
|
-
|
|
99
|
-
expect(response.body).toEqual({
|
|
100
|
-
token: "test"
|
|
101
|
-
});
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
it("should not validate the header", async () => {
|
|
105
|
-
const response = await request.get("/rest/header-params/scenario-3").expect(400);
|
|
106
|
-
|
|
107
|
-
expect(response.body).toEqual({
|
|
108
|
-
errors: [
|
|
109
|
-
{
|
|
110
|
-
requestPath: "header",
|
|
111
|
-
dataPath: ".x-token",
|
|
112
|
-
keyword: "required",
|
|
113
|
-
message: "It should have required parameter 'x-token'",
|
|
114
|
-
modelName: "header",
|
|
115
|
-
params: {
|
|
116
|
-
missingProperty: "x-token"
|
|
117
|
-
},
|
|
118
|
-
schemaPath: "#/required"
|
|
119
|
-
}
|
|
120
|
-
],
|
|
121
|
-
message: "Bad request on parameter \"request.header.x-token\".\nIt should have required parameter 'x-token'",
|
|
122
|
-
name: "REQUIRED_VALIDATION_ERROR",
|
|
123
|
-
status: 400
|
|
124
|
-
});
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
it("should not validate the header - enum", async () => {
|
|
128
|
-
const response = await request.get("/rest/header-params/scenario-3").set("x-token", "invalid").expect(400);
|
|
129
|
-
|
|
130
|
-
expect(response.body).toEqual({
|
|
131
|
-
errors: [
|
|
132
|
-
{
|
|
133
|
-
data: "invalid",
|
|
134
|
-
requestPath: "header",
|
|
135
|
-
dataPath: ".x-token",
|
|
136
|
-
instancePath: "",
|
|
137
|
-
keyword: "enum",
|
|
138
|
-
message: "must be equal to one of the allowed values",
|
|
139
|
-
params: {
|
|
140
|
-
allowedValues: ["test", "gcp"]
|
|
141
|
-
},
|
|
142
|
-
schemaPath: "#/enum"
|
|
143
|
-
}
|
|
144
|
-
],
|
|
145
|
-
message:
|
|
146
|
-
'Bad request on parameter "request.header.x-token".\nValue must be equal to one of the allowed values. Given value: "invalid"',
|
|
147
|
-
name: "AJV_VALIDATION_ERROR",
|
|
148
|
-
status: 400
|
|
149
|
-
});
|
|
150
|
-
});
|
|
151
|
-
});
|
|
152
|
-
}
|
package/src/tests/testHeaders.ts
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import {BodyParams, Controller, Get, PlatformResponse, PlatformTest, Res} from "@tsed/common";
|
|
2
|
-
import {Returns} from "@tsed/schema";
|
|
3
|
-
import SuperTest from "supertest";
|
|
4
|
-
import {PlatformTestingSdkOpts} from "../interfaces/index.js";
|
|
5
|
-
|
|
6
|
-
@Controller("/headers")
|
|
7
|
-
export class HeadersCtrl {
|
|
8
|
-
@Get("/scenario-1")
|
|
9
|
-
@Returns(200, String).Header("test", "x-token")
|
|
10
|
-
testScenario1(@BodyParams("test") value: string[]): any {
|
|
11
|
-
return "hello";
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
@Get("/scenario-2")
|
|
15
|
-
@Returns(200, String).Header("x-token-test", "test").Header("x-token-test-2", "test2").ContentType("application/xml")
|
|
16
|
-
testScenario2() {
|
|
17
|
-
return "<xml></xml>";
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
@Get("/scenario-3")
|
|
21
|
-
@Returns(200, String).Headers({
|
|
22
|
-
Location: {
|
|
23
|
-
description: "URL to the new xxx",
|
|
24
|
-
type: "string",
|
|
25
|
-
value: "/v1/location/header"
|
|
26
|
-
}
|
|
27
|
-
})
|
|
28
|
-
testScenario3(@Res() response: PlatformResponse) {
|
|
29
|
-
response.setHeader("Location", `/v1/location`);
|
|
30
|
-
return "Hello";
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export function testHeaders(options: PlatformTestingSdkOpts) {
|
|
35
|
-
let request: SuperTest.Agent;
|
|
36
|
-
|
|
37
|
-
beforeAll(
|
|
38
|
-
PlatformTest.bootstrap(options.server, {
|
|
39
|
-
...options,
|
|
40
|
-
logger: {
|
|
41
|
-
level: "off"
|
|
42
|
-
},
|
|
43
|
-
mount: {
|
|
44
|
-
"/rest": [HeadersCtrl]
|
|
45
|
-
}
|
|
46
|
-
})
|
|
47
|
-
);
|
|
48
|
-
beforeAll(() => {
|
|
49
|
-
request = SuperTest(PlatformTest.callback());
|
|
50
|
-
});
|
|
51
|
-
afterAll(PlatformTest.reset);
|
|
52
|
-
|
|
53
|
-
it("Scenario1: GET /rest/headers/scenario-1", async () => {
|
|
54
|
-
const response = await request.get("/rest/headers/scenario-1").expect(200);
|
|
55
|
-
|
|
56
|
-
expect(response.text).toEqual("hello");
|
|
57
|
-
expect(response.header["test"]).toEqual("x-token");
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
it("Scenario2: GET /rest/headers/scenario-2", (done: Function) => {
|
|
61
|
-
request
|
|
62
|
-
.get("/rest/headers/scenario-2")
|
|
63
|
-
.expect(200)
|
|
64
|
-
.end((err: any, response: any) => {
|
|
65
|
-
if (err) {
|
|
66
|
-
throw err;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
expect(response.headers["x-token-test"]).toEqual("test");
|
|
70
|
-
expect(response.headers["x-token-test-2"]).toEqual("test2");
|
|
71
|
-
expect(response.headers["content-type"]).toContain("application/xml");
|
|
72
|
-
|
|
73
|
-
done();
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it("Scenario3: GET /rest/headers/scenario-3", (done: Function) => {
|
|
78
|
-
request
|
|
79
|
-
.get("/rest/headers/scenario-3")
|
|
80
|
-
.expect(200)
|
|
81
|
-
.end((err: any, response: any) => {
|
|
82
|
-
if (err) {
|
|
83
|
-
throw err;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
expect(response.headers["location"]).toEqual("/v1/location");
|
|
87
|
-
|
|
88
|
-
done();
|
|
89
|
-
});
|
|
90
|
-
});
|
|
91
|
-
}
|